2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "cp-cilkplus.h"
45 #include "gcc-rich-location.h"
46 #include "tree-iterator.h"
47 #include "c-family/name-hint.h"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token
=
57 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant
{
63 /* floating-point literal */
67 /* %<__FUNCTION__%> */
69 /* %<__PRETTY_FUNCTION__%> */
77 /* %<typeid%> operator */
79 /* non-constant compound literals */
87 /* an array reference */
93 /* the address of a label */
107 /* calls to overloaded operators */
111 /* a comma operator */
113 /* a call to a constructor */
115 /* a transaction expression */
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error
{
125 /* is not a class or namespace */
127 /* is not a class, namespace, or enumeration */
131 /* The various kinds of required token */
132 enum required_token
{
134 RT_SEMICOLON
, /* ';' */
135 RT_OPEN_PAREN
, /* '(' */
136 RT_CLOSE_BRACE
, /* '}' */
137 RT_OPEN_BRACE
, /* '{' */
138 RT_CLOSE_SQUARE
, /* ']' */
139 RT_OPEN_SQUARE
, /* '[' */
143 RT_GREATER
, /* '>' */
145 RT_ELLIPSIS
, /* '...' */
149 RT_COLON_SCOPE
, /* ':' or '::' */
150 RT_CLOSE_PAREN
, /* ')' */
151 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
152 RT_PRAGMA_EOL
, /* end of line */
153 RT_NAME
, /* identifier */
155 /* The type is CPP_KEYWORD */
157 RT_DELETE
, /* delete */
158 RT_RETURN
, /* return */
159 RT_WHILE
, /* while */
160 RT_EXTERN
, /* extern */
161 RT_STATIC_ASSERT
, /* static_assert */
162 RT_DECLTYPE
, /* decltype */
163 RT_OPERATOR
, /* operator */
164 RT_CLASS
, /* class */
165 RT_TEMPLATE
, /* template */
166 RT_NAMESPACE
, /* namespace */
167 RT_USING
, /* using */
170 RT_CATCH
, /* catch */
171 RT_THROW
, /* throw */
172 RT_LABEL
, /* __label__ */
173 RT_AT_TRY
, /* @try */
174 RT_AT_SYNCHRONIZED
, /* @synchronized */
175 RT_AT_THROW
, /* @throw */
177 RT_SELECT
, /* selection-statement */
178 RT_ITERATION
, /* iteration-statement */
179 RT_JUMP
, /* jump-statement */
180 RT_CLASS_KEY
, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
195 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
197 saved (parser
->in_type_id_in_expr_p
)
198 { parser
->in_type_id_in_expr_p
= set
; }
199 ~type_id_in_expr_sentinel ()
200 { parser
->in_type_id_in_expr_p
= saved
; }
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static void cp_parser_cilk_simd
255 (cp_parser
*, cp_token
*, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser
*, tree
, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser
*, tree
, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser
*, tree
, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream
;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand
;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
285 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
286 cp_token
*start_token
, unsigned num
,
287 cp_token
*curr_token
)
289 unsigned i
, nprinted
;
293 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
299 num
= buffer
->length ();
301 if (start_token
== NULL
)
302 start_token
= buffer
->address ();
304 if (start_token
> buffer
->address ())
306 cp_lexer_print_token (file
, &(*buffer
)[0]);
307 fprintf (file
, " ... ");
312 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
314 if (token
== start_token
)
321 if (token
== curr_token
)
322 fprintf (file
, "[[");
324 cp_lexer_print_token (file
, token
);
326 if (token
== curr_token
)
327 fprintf (file
, "]]");
333 case CPP_CLOSE_BRACE
:
343 if (i
== num
&& i
< buffer
->length ())
345 fprintf (file
, " ... ");
346 cp_lexer_print_token (file
, &buffer
->last ());
349 fprintf (file
, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
356 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
358 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> &ref
)
364 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
368 debug (vec
<cp_token
, va_gc
> *ptr
)
373 fprintf (stderr
, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
381 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
385 fprintf (file
, "%s: ", desc
);
386 print_node_brief (file
, "", t
, 0);
391 /* Dump parser context C to FILE. */
394 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
396 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
398 print_node_brief (file
, "", c
->object_type
, 0);
399 fprintf (file
, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
406 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
409 cp_parser_context
*c
;
411 fprintf (file
, "Parsing context stack:\n");
412 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
414 fprintf (file
, "\t#%u: ", i
);
415 cp_debug_print_context (file
, c
);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
423 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
426 fprintf (file
, "%s: true\n", desc
);
430 /* Print an unparsed function entry UF to FILE. */
433 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
436 cp_default_arg_entry
*default_arg_fn
;
439 fprintf (file
, "\tFunctions with default args:\n");
441 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
444 fprintf (file
, "\t\tClass type: ");
445 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
446 fprintf (file
, "\t\tDeclaration: ");
447 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
448 fprintf (file
, "\n");
451 fprintf (file
, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
455 print_node_brief (file
, "", fn
, 0);
458 fprintf (file
, "\n");
460 fprintf (file
, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
464 print_node_brief (file
, "", fn
, 0);
467 fprintf (file
, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
474 cp_debug_print_unparsed_queues (FILE *file
,
475 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
478 cp_unparsed_functions_entry
*uf
;
480 fprintf (file
, "Unparsed functions\n");
481 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
483 fprintf (file
, "#%u:\n", i
);
484 cp_debug_print_unparsed_function (file
, uf
);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
493 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
495 cp_token
*next_token
, *first_token
, *start_token
;
500 next_token
= parser
->lexer
->next_token
;
501 first_token
= parser
->lexer
->buffer
->address ();
502 start_token
= (next_token
> first_token
+ window_size
/ 2)
503 ? next_token
- window_size
/ 2
505 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
514 cp_debug_parser (FILE *file
, cp_parser
*parser
)
516 const size_t window_size
= 20;
518 expanded_location eloc
;
523 fprintf (file
, "Parser state\n\n");
524 fprintf (file
, "Number of tokens: %u\n",
525 vec_safe_length (parser
->lexer
->buffer
));
526 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
527 cp_debug_print_tree_if_set (file
, "Object scope",
528 parser
->object_scope
);
529 cp_debug_print_tree_if_set (file
, "Qualifying scope",
530 parser
->qualifying_scope
);
531 cp_debug_print_context_stack (file
, parser
->context
);
532 cp_debug_print_flag (file
, "Allow GNU extensions",
533 parser
->allow_gnu_extensions_p
);
534 cp_debug_print_flag (file
, "'>' token is greater-than",
535 parser
->greater_than_is_operator_p
);
536 cp_debug_print_flag (file
, "Default args allowed in current "
537 "parameter list", parser
->default_arg_ok_p
);
538 cp_debug_print_flag (file
, "Parsing integral constant-expression",
539 parser
->integral_constant_expression_p
);
540 cp_debug_print_flag (file
, "Allow non-constant expression in current "
541 "constant-expression",
542 parser
->allow_non_integral_constant_expression_p
);
543 cp_debug_print_flag (file
, "Seen non-constant expression",
544 parser
->non_integral_constant_expression_p
);
545 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
547 parser
->local_variables_forbidden_p
);
548 cp_debug_print_flag (file
, "In unbraced linkage specification",
549 parser
->in_unbraced_linkage_specification_p
);
550 cp_debug_print_flag (file
, "Parsing a declarator",
551 parser
->in_declarator_p
);
552 cp_debug_print_flag (file
, "In template argument list",
553 parser
->in_template_argument_list_p
);
554 cp_debug_print_flag (file
, "Parsing an iteration statement",
555 parser
->in_statement
& IN_ITERATION_STMT
);
556 cp_debug_print_flag (file
, "Parsing a switch statement",
557 parser
->in_statement
& IN_SWITCH_STMT
);
558 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
559 parser
->in_statement
& IN_OMP_BLOCK
);
560 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
561 parser
->in_statement
& IN_CILK_SIMD_FOR
);
562 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
563 parser
->in_statement
& IN_OMP_FOR
);
564 cp_debug_print_flag (file
, "Parsing an if statement",
565 parser
->in_statement
& IN_IF_STMT
);
566 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
567 "context", parser
->in_type_id_in_expr_p
);
568 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
569 parser
->implicit_extern_c
);
570 cp_debug_print_flag (file
, "String expressions should be translated "
571 "to execution character set",
572 parser
->translate_strings_p
);
573 cp_debug_print_flag (file
, "Parsing function body outside of a "
574 "local class", parser
->in_function_body
);
575 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
576 parser
->colon_corrects_to_scope_p
);
577 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
578 parser
->colon_doesnt_start_class_def_p
);
579 if (parser
->type_definition_forbidden_message
)
580 fprintf (file
, "Error message for forbidden type definitions: %s\n",
581 parser
->type_definition_forbidden_message
);
582 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
583 fprintf (file
, "Number of class definitions in progress: %u\n",
584 parser
->num_classes_being_defined
);
585 fprintf (file
, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser
->num_template_parameter_lists
);
587 cp_debug_parser_tokens (file
, parser
, window_size
);
588 token
= parser
->lexer
->next_token
;
589 fprintf (file
, "Next token to parse:\n");
590 fprintf (file
, "\tToken: ");
591 cp_lexer_print_token (file
, token
);
592 eloc
= expand_location (token
->location
);
593 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
594 fprintf (file
, "\tLine: %d\n", eloc
.line
);
595 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
599 debug (cp_parser
&ref
)
601 cp_debug_parser (stderr
, &ref
);
605 debug (cp_parser
*ptr
)
610 fprintf (stderr
, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
616 cp_lexer_alloc (void)
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
625 /* Initially we are not debugging. */
626 lexer
->debugging_p
= false;
628 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
630 /* Create the buffer. */
631 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
641 cp_lexer_new_main (void)
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token
);
651 lexer
= cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer
->buffer
->quick_push (token
);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token
.type
!= CPP_EOF
)
659 cp_lexer_get_preprocessor_token (lexer
, &token
);
660 vec_safe_push (lexer
->buffer
, token
);
663 lexer
->last_token
= lexer
->buffer
->address ()
664 + lexer
->buffer
->length ()
666 lexer
->next_token
= lexer
->buffer
->length ()
667 ? lexer
->buffer
->address ()
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
674 gcc_assert (!lexer
->next_token
->purged_p
);
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
682 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
684 cp_token
*first
= cache
->first
;
685 cp_token
*last
= cache
->last
;
686 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
688 /* We do not own the buffer. */
689 lexer
->buffer
= NULL
;
690 lexer
->next_token
= first
== last
? &eof_token
: first
;
691 lexer
->last_token
= last
;
693 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
695 /* Initially we are not debugging. */
696 lexer
->debugging_p
= false;
698 gcc_assert (!lexer
->next_token
->purged_p
);
702 /* Frees all resources associated with LEXER. */
705 cp_lexer_destroy (cp_lexer
*lexer
)
707 vec_free (lexer
->buffer
);
708 lexer
->saved_tokens
.release ();
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
717 #define LEXER_DEBUGGING_ENABLED_P false
719 /* Returns nonzero if debugging information should be output. */
722 cp_lexer_debugging_p (cp_lexer
*lexer
)
724 if (!LEXER_DEBUGGING_ENABLED_P
)
727 return lexer
->debugging_p
;
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
734 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
736 return lexer
->next_token
- previous_p
;
739 static inline cp_token
*
740 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
746 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
748 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer
*lexer
)
754 if (lexer
->next_token
== &eof_token
)
755 return lexer
->last_token
- 1;
757 return cp_lexer_token_position (lexer
, true);
760 static inline cp_token
*
761 cp_lexer_previous_token (cp_lexer
*lexer
)
763 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
765 /* Skip past purged tokens. */
768 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
772 return cp_lexer_token_at (lexer
, tp
);
775 /* nonzero if we are presently saving tokens. */
778 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
780 return lexer
->saved_tokens
.length () != 0;
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
789 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
791 static int is_extern_c
= 0;
793 /* Get a new token from the preprocessor. */
795 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
796 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
797 token
->keyword
= RID_MAX
;
798 token
->purged_p
= false;
799 token
->error_reported
= false;
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c
+= pending_lang_change
;
805 pending_lang_change
= 0;
806 token
->implicit_extern_c
= is_extern_c
> 0;
808 /* Check to see if this token is a keyword. */
809 if (token
->type
== CPP_NAME
)
811 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
813 /* Mark this token as a keyword. */
814 token
->type
= CPP_KEYWORD
;
815 /* Record which keyword. */
816 token
->keyword
= C_RID_CODE (token
->u
.value
);
820 if (warn_cxx11_compat
821 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
822 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
824 /* Warn about the C++0x keyword (but still treat it as
826 warning (OPT_Wc__11_compat
,
827 "identifier %qE is a keyword in C++11",
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
835 token
->keyword
= RID_MAX
;
838 else if (token
->type
== CPP_AT_NAME
)
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token
->type
= CPP_KEYWORD
;
842 switch (C_RID_CODE (token
->u
.value
))
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
852 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
853 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
854 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
855 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
856 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
857 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
858 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
859 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
860 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
865 /* Update the globals input_location and the input file stack from TOKEN. */
867 cp_lexer_set_source_position_from_token (cp_token
*token
)
869 if (token
->type
!= CPP_EOF
)
871 input_location
= token
->location
;
875 /* Update the globals input_location and the input file stack from LEXER. */
877 cp_lexer_set_source_position (cp_lexer
*lexer
)
879 cp_token
*token
= cp_lexer_peek_token (lexer
);
880 cp_lexer_set_source_position_from_token (token
);
883 /* Return a pointer to the next token in the token stream, but do not
886 static inline cp_token
*
887 cp_lexer_peek_token (cp_lexer
*lexer
)
889 if (cp_lexer_debugging_p (lexer
))
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
892 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
893 putc ('\n', cp_lexer_debug_stream
);
895 return lexer
->next_token
;
898 /* Return true if the next token has the indicated TYPE. */
901 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
903 return cp_lexer_peek_token (lexer
)->type
== type
;
906 /* Return true if the next token does not have the indicated TYPE. */
909 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
911 return !cp_lexer_next_token_is (lexer
, type
);
914 /* Return true if the next token is the indicated KEYWORD. */
917 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
919 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
923 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
925 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
929 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
931 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
934 /* Return true if the next token is not the indicated KEYWORD. */
937 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
939 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
942 /* Return true if KEYWORD can start a decl-specifier. */
945 cp_keyword_starts_decl_specifier_p (enum rid 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
:
988 if (keyword
>= RID_FIRST_INT_N
989 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
990 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
996 /* Return true if the next token is a keyword for a decl-specifier. */
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1003 token
= cp_lexer_peek_token (lexer
);
1004 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1007 /* Returns TRUE iff the token T begins a decltype type. */
1010 token_is_decltype (cp_token
*t
)
1012 return (t
->keyword
== RID_DECLTYPE
1013 || t
->type
== CPP_DECLTYPE
);
1016 /* Returns TRUE iff the next token begins a decltype type. */
1019 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1021 cp_token
*t
= cp_lexer_peek_token (lexer
);
1022 return token_is_decltype (t
);
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1029 saved_checks_value (struct tree_check
*check_value
)
1031 /* Perform any access checks that were deferred. */
1032 vec
<deferred_access_check
, va_gc
> *checks
;
1033 deferred_access_check
*chk
;
1034 checks
= check_value
->checks
;
1038 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1039 perform_or_defer_access_check (chk
->binfo
,
1041 chk
->diag_decl
, tf_warning_or_error
);
1043 /* Return the stored value. */
1044 return check_value
->value
;
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1054 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1058 /* N is 1-based, not zero-based. */
1061 if (cp_lexer_debugging_p (lexer
))
1062 fprintf (cp_lexer_debug_stream
,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1066 token
= lexer
->next_token
;
1067 gcc_assert (!n
|| token
!= &eof_token
);
1071 if (token
== lexer
->last_token
)
1077 if (!token
->purged_p
)
1081 if (cp_lexer_debugging_p (lexer
))
1083 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1084 putc ('\n', cp_lexer_debug_stream
);
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1094 cp_lexer_consume_token (cp_lexer
* lexer
)
1096 cp_token
*token
= lexer
->next_token
;
1098 gcc_assert (token
!= &eof_token
);
1099 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1103 lexer
->next_token
++;
1104 if (lexer
->next_token
== lexer
->last_token
)
1106 lexer
->next_token
= &eof_token
;
1111 while (lexer
->next_token
->purged_p
);
1113 cp_lexer_set_source_position_from_token (token
);
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer
))
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1119 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1120 putc ('\n', cp_lexer_debug_stream
);
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1131 cp_lexer_purge_token (cp_lexer
*lexer
)
1133 cp_token
*tok
= lexer
->next_token
;
1135 gcc_assert (tok
!= &eof_token
);
1136 tok
->purged_p
= true;
1137 tok
->location
= UNKNOWN_LOCATION
;
1138 tok
->u
.value
= NULL_TREE
;
1139 tok
->keyword
= RID_MAX
;
1144 if (tok
== lexer
->last_token
)
1150 while (tok
->purged_p
);
1151 lexer
->next_token
= tok
;
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1159 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1161 cp_token
*peek
= lexer
->next_token
;
1163 if (peek
== &eof_token
)
1164 peek
= lexer
->last_token
;
1166 gcc_assert (tok
< peek
);
1168 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1170 tok
->purged_p
= true;
1171 tok
->location
= UNKNOWN_LOCATION
;
1172 tok
->u
.value
= NULL_TREE
;
1173 tok
->keyword
= RID_MAX
;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1181 cp_lexer_save_tokens (cp_lexer
* lexer
)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer
))
1185 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1187 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1190 /* Commit to the portion of the token stream most recently saved. */
1193 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer
))
1197 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1199 lexer
->saved_tokens
.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1206 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer
))
1210 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1212 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1220 struct saved_token_sentinel
1225 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1227 len
= lexer
->saved_tokens
.length ();
1228 cp_lexer_save_tokens (lexer
);
1232 cp_lexer_rollback_tokens (lexer
);
1235 ~saved_token_sentinel()
1238 cp_lexer_commit_tokens (lexer
);
1239 gcc_assert (lexer
->saved_tokens
.length () == len
);
1243 /* Print a representation of the TOKEN on the STREAM. */
1246 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names
[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1257 /* C++ parser token types - see "Manifest constants", above. */
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token
->type
)
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token
->u
.value
))
1273 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1280 case CPP_UTF8STRING
:
1281 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1285 print_generic_expr (stream
, token
->u
.value
);
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token
->type
< ARRAY_SIZE(token_names
))
1292 fputs (token_names
[token
->type
], stream
);
1294 fprintf (stream
, "[%d]", token
->type
);
1300 debug (cp_token
&ref
)
1302 cp_lexer_print_token (stderr
, &ref
);
1303 fprintf (stderr
, "\n");
1307 debug (cp_token
*ptr
)
1312 fprintf (stderr
, "<nil>\n");
1316 /* Start emitting debugging information. */
1319 cp_lexer_start_debugging (cp_lexer
* lexer
)
1321 if (!LEXER_DEBUGGING_ENABLED_P
)
1322 fatal_error (input_location
,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 lexer
->debugging_p
= true;
1326 cp_lexer_debug_stream
= stderr
;
1329 /* Stop emitting debugging information. */
1332 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1334 if (!LEXER_DEBUGGING_ENABLED_P
)
1335 fatal_error (input_location
,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338 lexer
->debugging_p
= false;
1339 cp_lexer_debug_stream
= NULL
;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache
*
1345 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1347 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1348 cache
->first
= first
;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1357 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1359 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser
->omp_declare_simd
= NULL
;
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1371 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1373 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1375 if (fndecl
== error_mark_node
)
1377 parser
->omp_declare_simd
= NULL
;
1380 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1382 cp_ensure_no_omp_declare_simd (parser
);
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1392 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1394 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1396 error_at (parser
->oacc_routine
->loc
,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser
->oacc_routine
= NULL
;
1403 /* Decl-specifiers. */
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1408 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1410 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420 static cp_declarator
*make_call_declarator
1421 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1422 static cp_declarator
*make_array_declarator
1423 (cp_declarator
*, tree
);
1424 static cp_declarator
*make_pointer_declarator
1425 (cp_cv_quals
, cp_declarator
*, tree
);
1426 static cp_declarator
*make_reference_declarator
1427 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1428 static cp_declarator
*make_ptrmem_declarator
1429 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1431 /* An erroneous declarator. */
1432 static cp_declarator
*cp_error_declarator
;
1434 /* The obstack on which declarators and related data structures are
1436 static struct obstack declarator_obstack
;
1438 /* Alloc BYTES from the declarator memory pool. */
1440 static inline void *
1441 alloc_declarator (size_t bytes
)
1443 return obstack_alloc (&declarator_obstack
, bytes
);
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1449 static cp_declarator
*
1450 make_declarator (cp_declarator_kind kind
)
1452 cp_declarator
*declarator
;
1454 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1455 declarator
->kind
= kind
;
1456 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1457 declarator
->attributes
= NULL_TREE
;
1458 declarator
->std_attributes
= NULL_TREE
;
1459 declarator
->declarator
= NULL
;
1460 declarator
->parameter_pack_p
= false;
1461 declarator
->id_loc
= UNKNOWN_LOCATION
;
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1472 static cp_declarator
*
1473 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1474 special_function_kind sfk
)
1476 cp_declarator
*declarator
;
1478 /* It is valid to write:
1480 class C { void f(); };
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1488 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1489 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1491 gcc_assert (identifier_p (unqualified_name
)
1492 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1495 declarator
= make_declarator (cdk_id
);
1496 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1497 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1498 declarator
->u
.id
.sfk
= sfk
;
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1512 cp_declarator
*declarator
;
1514 declarator
= make_declarator (cdk_pointer
);
1515 declarator
->declarator
= target
;
1516 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1517 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1520 declarator
->id_loc
= target
->id_loc
;
1521 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1522 target
->parameter_pack_p
= false;
1525 declarator
->parameter_pack_p
= false;
1527 declarator
->std_attributes
= attributes
;
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1537 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1538 bool rvalue_ref
, tree attributes
)
1540 cp_declarator
*declarator
;
1542 declarator
= make_declarator (cdk_reference
);
1543 declarator
->declarator
= target
;
1544 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1545 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1548 declarator
->id_loc
= target
->id_loc
;
1549 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1550 target
->parameter_pack_p
= false;
1553 declarator
->parameter_pack_p
= false;
1555 declarator
->std_attributes
= attributes
;
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1566 cp_declarator
*pointee
,
1569 cp_declarator
*declarator
;
1571 declarator
= make_declarator (cdk_ptrmem
);
1572 declarator
->declarator
= pointee
;
1573 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1574 declarator
->u
.pointer
.class_type
= class_type
;
1578 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1579 pointee
->parameter_pack_p
= false;
1582 declarator
->parameter_pack_p
= false;
1584 declarator
->std_attributes
= attributes
;
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1595 make_call_declarator (cp_declarator
*target
,
1597 cp_cv_quals cv_qualifiers
,
1598 cp_virt_specifiers virt_specifiers
,
1599 cp_ref_qualifier ref_qualifier
,
1601 tree exception_specification
,
1602 tree late_return_type
,
1603 tree requires_clause
)
1605 cp_declarator
*declarator
;
1607 declarator
= make_declarator (cdk_function
);
1608 declarator
->declarator
= target
;
1609 declarator
->u
.function
.parameters
= parms
;
1610 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1611 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1612 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1613 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1614 declarator
->u
.function
.exception_specification
= exception_specification
;
1615 declarator
->u
.function
.late_return_type
= late_return_type
;
1616 declarator
->u
.function
.requires_clause
= requires_clause
;
1619 declarator
->id_loc
= target
->id_loc
;
1620 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1621 target
->parameter_pack_p
= false;
1624 declarator
->parameter_pack_p
= false;
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1633 make_array_declarator (cp_declarator
*element
, tree bounds
)
1635 cp_declarator
*declarator
;
1637 declarator
= make_declarator (cdk_array
);
1638 declarator
->declarator
= element
;
1639 declarator
->u
.array
.bounds
= bounds
;
1642 declarator
->id_loc
= element
->id_loc
;
1643 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1644 element
->parameter_pack_p
= false;
1647 declarator
->parameter_pack_p
= false;
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1655 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1657 if (declarator
&& declarator
->parameter_pack_p
)
1658 /* We already saw an ellipsis. */
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1666 while (declarator
&& !found
)
1668 switch ((int)declarator
->kind
)
1680 declarator
= declarator
->declarator
;
1688 cp_parameter_declarator
*no_parameters
;
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1693 cp_parameter_declarator
*
1694 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1695 cp_declarator
*declarator
,
1696 tree default_argument
,
1698 bool template_parameter_pack_p
= false)
1700 cp_parameter_declarator
*parameter
;
1702 parameter
= ((cp_parameter_declarator
*)
1703 alloc_declarator (sizeof (cp_parameter_declarator
)));
1704 parameter
->next
= NULL
;
1705 if (decl_specifiers
)
1706 parameter
->decl_specifiers
= *decl_specifiers
;
1708 clear_decl_specs (¶meter
->decl_specifiers
);
1709 parameter
->declarator
= declarator
;
1710 parameter
->default_argument
= default_argument
;
1711 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1712 parameter
->loc
= loc
;
1717 /* Returns true iff DECLARATOR is a declaration for a function. */
1720 function_declarator_p (const cp_declarator
*declarator
)
1724 if (declarator
->kind
== cdk_function
1725 && declarator
->declarator
->kind
== cdk_id
)
1727 if (declarator
->kind
== cdk_id
1728 || declarator
->kind
== cdk_decomp
1729 || declarator
->kind
== cdk_error
)
1731 declarator
= declarator
->declarator
;
1741 A cp_parser parses the token stream as specified by the C++
1742 grammar. Its job is purely parsing, not semantic analysis. For
1743 example, the parser breaks the token stream into declarators,
1744 expressions, statements, and other similar syntactic constructs.
1745 It does not check that the types of the expressions on either side
1746 of an assignment-statement are compatible, or that a function is
1747 not declared with a parameter of type `void'.
1749 The parser invokes routines elsewhere in the compiler to perform
1750 semantic analysis and to build up the abstract syntax tree for the
1753 The parser (and the template instantiation code, which is, in a
1754 way, a close relative of parsing) are the only parts of the
1755 compiler that should be calling push_scope and pop_scope, or
1756 related functions. The parser (and template instantiation code)
1757 keeps track of what scope is presently active; everything else
1758 should simply honor that. (The code that generates static
1759 initializers may also need to set the scope, in order to check
1760 access control correctly when emitting the initializers.)
1765 The parser is of the standard recursive-descent variety. Upcoming
1766 tokens in the token stream are examined in order to determine which
1767 production to use when parsing a non-terminal. Some C++ constructs
1768 require arbitrary look ahead to disambiguate. For example, it is
1769 impossible, in the general case, to tell whether a statement is an
1770 expression or declaration without scanning the entire statement.
1771 Therefore, the parser is capable of "parsing tentatively." When the
1772 parser is not sure what construct comes next, it enters this mode.
1773 Then, while we attempt to parse the construct, the parser queues up
1774 error messages, rather than issuing them immediately, and saves the
1775 tokens it consumes. If the construct is parsed successfully, the
1776 parser "commits", i.e., it issues any queued error messages and
1777 the tokens that were being preserved are permanently discarded.
1778 If, however, the construct is not parsed successfully, the parser
1779 rolls back its state completely so that it can resume parsing using
1780 a different alternative.
1785 The performance of the parser could probably be improved substantially.
1786 We could often eliminate the need to parse tentatively by looking ahead
1787 a little bit. In some places, this approach might not entirely eliminate
1788 the need to parse tentatively, but it might still speed up the average
1791 /* Flags that are passed to some parsing functions. These values can
1792 be bitwise-ored together. */
1797 CP_PARSER_FLAGS_NONE
= 0x0,
1798 /* The construct is optional. If it is not present, then no error
1799 should be issued. */
1800 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1801 /* When parsing a type-specifier, treat user-defined type-names
1802 as non-type identifiers. */
1803 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1804 /* When parsing a type-specifier, do not try to parse a class-specifier
1805 or enum-specifier. */
1806 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1807 /* When parsing a decl-specifier-seq, only allow type-specifier or
1809 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1810 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1811 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1814 /* This type is used for parameters and variables which hold
1815 combinations of the above flags. */
1816 typedef int cp_parser_flags
;
1818 /* The different kinds of declarators we want to parse. */
1820 enum cp_parser_declarator_kind
1822 /* We want an abstract declarator. */
1823 CP_PARSER_DECLARATOR_ABSTRACT
,
1824 /* We want a named declarator. */
1825 CP_PARSER_DECLARATOR_NAMED
,
1826 /* We don't mind, but the name must be an unqualified-id. */
1827 CP_PARSER_DECLARATOR_EITHER
1830 /* The precedence values used to parse binary expressions. The minimum value
1831 of PREC must be 1, because zero is reserved to quickly discriminate
1832 binary operators from other tokens. */
1837 PREC_LOGICAL_OR_EXPRESSION
,
1838 PREC_LOGICAL_AND_EXPRESSION
,
1839 PREC_INCLUSIVE_OR_EXPRESSION
,
1840 PREC_EXCLUSIVE_OR_EXPRESSION
,
1841 PREC_AND_EXPRESSION
,
1842 PREC_EQUALITY_EXPRESSION
,
1843 PREC_RELATIONAL_EXPRESSION
,
1844 PREC_SHIFT_EXPRESSION
,
1845 PREC_ADDITIVE_EXPRESSION
,
1846 PREC_MULTIPLICATIVE_EXPRESSION
,
1848 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1851 /* A mapping from a token type to a corresponding tree node type, with a
1852 precedence value. */
1854 struct cp_parser_binary_operations_map_node
1856 /* The token type. */
1857 enum cpp_ttype token_type
;
1858 /* The corresponding tree code. */
1859 enum tree_code tree_type
;
1860 /* The precedence of this operator. */
1861 enum cp_parser_prec prec
;
1864 struct cp_parser_expression_stack_entry
1866 /* Left hand side of the binary operation we are currently
1869 /* Original tree code for left hand side, if it was a binary
1870 expression itself (used for -Wparentheses). */
1871 enum tree_code lhs_type
;
1872 /* Tree code for the binary operation we are parsing. */
1873 enum tree_code tree_type
;
1874 /* Precedence of the binary operation we are parsing. */
1875 enum cp_parser_prec prec
;
1876 /* Location of the binary operation we are parsing. */
1880 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1881 entries because precedence levels on the stack are monotonically
1883 typedef struct cp_parser_expression_stack_entry
1884 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1888 /* Constructors and destructors. */
1890 static cp_parser_context
*cp_parser_context_new
1891 (cp_parser_context
*);
1893 /* Class variables. */
1895 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1897 /* The operator-precedence table used by cp_parser_binary_expression.
1898 Transformed into an associative array (binops_by_token) by
1901 static const cp_parser_binary_operations_map_node binops
[] = {
1902 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1903 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1905 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1906 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1907 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1909 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1910 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1912 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1913 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1915 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1916 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1917 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1918 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1920 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1921 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1923 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1925 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1927 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1929 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1931 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1934 /* The same as binops, but initialized by cp_parser_new so that
1935 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1937 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1939 /* Constructors and destructors. */
1941 /* Construct a new context. The context below this one on the stack
1942 is given by NEXT. */
1944 static cp_parser_context
*
1945 cp_parser_context_new (cp_parser_context
* next
)
1947 cp_parser_context
*context
;
1949 /* Allocate the storage. */
1950 if (cp_parser_context_free_list
!= NULL
)
1952 /* Pull the first entry from the free list. */
1953 context
= cp_parser_context_free_list
;
1954 cp_parser_context_free_list
= context
->next
;
1955 memset (context
, 0, sizeof (*context
));
1958 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1960 /* No errors have occurred yet in this context. */
1961 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1962 /* If this is not the bottommost context, copy information that we
1963 need from the previous context. */
1966 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1967 expression, then we are parsing one in this context, too. */
1968 context
->object_type
= next
->object_type
;
1969 /* Thread the stack. */
1970 context
->next
= next
;
1976 /* Managing the unparsed function queues. */
1978 #define unparsed_funs_with_default_args \
1979 parser->unparsed_queues->last ().funs_with_default_args
1980 #define unparsed_funs_with_definitions \
1981 parser->unparsed_queues->last ().funs_with_definitions
1982 #define unparsed_nsdmis \
1983 parser->unparsed_queues->last ().nsdmis
1984 #define unparsed_classes \
1985 parser->unparsed_queues->last ().classes
1988 push_unparsed_function_queues (cp_parser
*parser
)
1990 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1991 vec_safe_push (parser
->unparsed_queues
, e
);
1995 pop_unparsed_function_queues (cp_parser
*parser
)
1997 release_tree_vector (unparsed_funs_with_definitions
);
1998 parser
->unparsed_queues
->pop ();
2003 /* Constructors and destructors. */
2005 static cp_parser
*cp_parser_new
2008 /* Routines to parse various constructs.
2010 Those that return `tree' will return the error_mark_node (rather
2011 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2012 Sometimes, they will return an ordinary node if error-recovery was
2013 attempted, even though a parse error occurred. So, to check
2014 whether or not a parse error occurred, you should always use
2015 cp_parser_error_occurred. If the construct is optional (indicated
2016 either by an `_opt' in the name of the function that does the
2017 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2018 the construct is not present. */
2020 /* Lexical conventions [gram.lex] */
2022 static cp_expr cp_parser_identifier
2024 static cp_expr cp_parser_string_literal
2025 (cp_parser
*, bool, bool, bool);
2026 static cp_expr cp_parser_userdef_char_literal
2028 static tree cp_parser_userdef_string_literal
2030 static cp_expr cp_parser_userdef_numeric_literal
2033 /* Basic concepts [gram.basic] */
2035 static bool cp_parser_translation_unit
2038 /* Expressions [gram.expr] */
2040 static cp_expr cp_parser_primary_expression
2041 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2042 static cp_expr cp_parser_id_expression
2043 (cp_parser
*, bool, bool, bool *, bool, bool);
2044 static cp_expr cp_parser_unqualified_id
2045 (cp_parser
*, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier_opt
2047 (cp_parser
*, bool, bool, bool, bool, bool = false);
2048 static tree cp_parser_nested_name_specifier
2049 (cp_parser
*, bool, bool, bool, bool);
2050 static tree cp_parser_qualifying_entity
2051 (cp_parser
*, bool, bool, bool, bool, bool);
2052 static cp_expr cp_parser_postfix_expression
2053 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2054 static tree cp_parser_postfix_open_square_expression
2055 (cp_parser
*, tree
, bool, bool);
2056 static tree cp_parser_postfix_dot_deref_expression
2057 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2058 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2059 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
);
2060 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2061 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2062 static void cp_parser_pseudo_destructor_name
2063 (cp_parser
*, tree
, tree
*, tree
*);
2064 static cp_expr cp_parser_unary_expression
2065 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2066 static enum tree_code cp_parser_unary_operator
2068 static tree cp_parser_new_expression
2070 static vec
<tree
, va_gc
> *cp_parser_new_placement
2072 static tree cp_parser_new_type_id
2073 (cp_parser
*, tree
*);
2074 static cp_declarator
*cp_parser_new_declarator_opt
2076 static cp_declarator
*cp_parser_direct_new_declarator
2078 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2080 static tree cp_parser_delete_expression
2082 static cp_expr cp_parser_cast_expression
2083 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2084 static cp_expr cp_parser_binary_expression
2085 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2086 static tree cp_parser_question_colon_clause
2087 (cp_parser
*, cp_expr
);
2088 static cp_expr cp_parser_assignment_expression
2089 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2090 static enum tree_code cp_parser_assignment_operator_opt
2092 static cp_expr cp_parser_expression
2093 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2094 static cp_expr cp_parser_constant_expression
2095 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2096 static cp_expr cp_parser_builtin_offsetof
2098 static cp_expr cp_parser_lambda_expression
2100 static void cp_parser_lambda_introducer
2101 (cp_parser
*, tree
);
2102 static bool cp_parser_lambda_declarator_opt
2103 (cp_parser
*, tree
);
2104 static void cp_parser_lambda_body
2105 (cp_parser
*, tree
);
2107 /* Statements [gram.stmt.stmt] */
2109 static void cp_parser_statement
2110 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2111 static void cp_parser_label_for_labeled_statement
2112 (cp_parser
*, tree
);
2113 static tree cp_parser_expression_statement
2114 (cp_parser
*, tree
);
2115 static tree cp_parser_compound_statement
2116 (cp_parser
*, tree
, int, bool);
2117 static void cp_parser_statement_seq_opt
2118 (cp_parser
*, tree
);
2119 static tree cp_parser_selection_statement
2120 (cp_parser
*, bool *, vec
<tree
> *);
2121 static tree cp_parser_condition
2123 static tree cp_parser_iteration_statement
2124 (cp_parser
*, bool *, bool);
2125 static bool cp_parser_init_statement
2126 (cp_parser
*, tree
*decl
);
2127 static tree cp_parser_for
2128 (cp_parser
*, bool);
2129 static tree cp_parser_c_for
2130 (cp_parser
*, tree
, tree
, bool);
2131 static tree cp_parser_range_for
2132 (cp_parser
*, tree
, tree
, tree
, bool);
2133 static void do_range_for_auto_deduction
2135 static tree cp_parser_perform_range_for_lookup
2136 (tree
, tree
*, tree
*);
2137 static tree cp_parser_range_for_member_function
2139 static tree cp_parser_jump_statement
2141 static void cp_parser_declaration_statement
2144 static tree cp_parser_implicitly_scoped_statement
2145 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2146 static void cp_parser_already_scoped_statement
2147 (cp_parser
*, bool *, const token_indent_info
&);
2149 /* Declarations [gram.dcl.dcl] */
2151 static void cp_parser_declaration_seq_opt
2153 static void cp_parser_declaration
2155 static void cp_parser_block_declaration
2156 (cp_parser
*, bool);
2157 static void cp_parser_simple_declaration
2158 (cp_parser
*, bool, tree
*);
2159 static void cp_parser_decl_specifier_seq
2160 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2161 static tree cp_parser_storage_class_specifier_opt
2163 static tree cp_parser_function_specifier_opt
2164 (cp_parser
*, cp_decl_specifier_seq
*);
2165 static tree cp_parser_type_specifier
2166 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2168 static tree cp_parser_simple_type_specifier
2169 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2170 static tree cp_parser_type_name
2171 (cp_parser
*, bool);
2172 static tree cp_parser_type_name
2174 static tree cp_parser_nonclass_name
2175 (cp_parser
* parser
);
2176 static tree cp_parser_elaborated_type_specifier
2177 (cp_parser
*, bool, bool);
2178 static tree cp_parser_enum_specifier
2180 static void cp_parser_enumerator_list
2181 (cp_parser
*, tree
);
2182 static void cp_parser_enumerator_definition
2183 (cp_parser
*, tree
);
2184 static tree cp_parser_namespace_name
2186 static void cp_parser_namespace_definition
2188 static void cp_parser_namespace_body
2190 static tree cp_parser_qualified_namespace_specifier
2192 static void cp_parser_namespace_alias_definition
2194 static bool cp_parser_using_declaration
2195 (cp_parser
*, bool);
2196 static void cp_parser_using_directive
2198 static tree cp_parser_alias_declaration
2200 static void cp_parser_asm_definition
2202 static void cp_parser_linkage_specification
2204 static void cp_parser_static_assert
2205 (cp_parser
*, bool);
2206 static tree cp_parser_decltype
2208 static tree cp_parser_decomposition_declaration
2209 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2211 /* Declarators [gram.dcl.decl] */
2213 static tree cp_parser_init_declarator
2214 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2215 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2216 static cp_declarator
*cp_parser_declarator
2217 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2218 static cp_declarator
*cp_parser_direct_declarator
2219 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2220 static enum tree_code cp_parser_ptr_operator
2221 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2222 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2224 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2226 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2228 static tree cp_parser_tx_qualifier_opt
2230 static tree cp_parser_late_return_type_opt
2231 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2232 static tree cp_parser_declarator_id
2233 (cp_parser
*, bool);
2234 static tree cp_parser_type_id
2236 static tree cp_parser_template_type_arg
2238 static tree
cp_parser_trailing_type_id (cp_parser
*);
2239 static tree cp_parser_type_id_1
2240 (cp_parser
*, bool, bool);
2241 static void cp_parser_type_specifier_seq
2242 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2243 static tree cp_parser_parameter_declaration_clause
2245 static tree cp_parser_parameter_declaration_list
2246 (cp_parser
*, bool *);
2247 static cp_parameter_declarator
*cp_parser_parameter_declaration
2248 (cp_parser
*, bool, bool *);
2249 static tree cp_parser_default_argument
2250 (cp_parser
*, bool);
2251 static void cp_parser_function_body
2252 (cp_parser
*, bool);
2253 static tree cp_parser_initializer
2254 (cp_parser
*, bool *, bool *);
2255 static cp_expr cp_parser_initializer_clause
2256 (cp_parser
*, bool *);
2257 static cp_expr cp_parser_braced_list
2258 (cp_parser
*, bool*);
2259 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2260 (cp_parser
*, bool *);
2262 static void cp_parser_ctor_initializer_opt_and_function_body
2263 (cp_parser
*, bool);
2265 static tree cp_parser_late_parsing_omp_declare_simd
2266 (cp_parser
*, tree
);
2268 static tree cp_parser_late_parsing_cilk_simd_fn_info
2269 (cp_parser
*, tree
);
2271 static tree cp_parser_late_parsing_oacc_routine
2272 (cp_parser
*, tree
);
2274 static tree synthesize_implicit_template_parm
2275 (cp_parser
*, tree
);
2276 static tree finish_fully_implicit_template
2277 (cp_parser
*, tree
);
2279 /* Classes [gram.class] */
2281 static tree cp_parser_class_name
2282 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2283 static tree cp_parser_class_specifier
2285 static tree cp_parser_class_head
2286 (cp_parser
*, bool *);
2287 static enum tag_types cp_parser_class_key
2289 static void cp_parser_type_parameter_key
2290 (cp_parser
* parser
);
2291 static void cp_parser_member_specification_opt
2293 static void cp_parser_member_declaration
2295 static tree cp_parser_pure_specifier
2297 static tree cp_parser_constant_initializer
2300 /* Derived classes [gram.class.derived] */
2302 static tree cp_parser_base_clause
2304 static tree cp_parser_base_specifier
2307 /* Special member functions [gram.special] */
2309 static tree cp_parser_conversion_function_id
2311 static tree cp_parser_conversion_type_id
2313 static cp_declarator
*cp_parser_conversion_declarator_opt
2315 static void cp_parser_ctor_initializer_opt
2317 static void cp_parser_mem_initializer_list
2319 static tree cp_parser_mem_initializer
2321 static tree cp_parser_mem_initializer_id
2324 /* Overloading [gram.over] */
2326 static cp_expr cp_parser_operator_function_id
2328 static cp_expr cp_parser_operator
2331 /* Templates [gram.temp] */
2333 static void cp_parser_template_declaration
2334 (cp_parser
*, bool);
2335 static tree cp_parser_template_parameter_list
2337 static tree cp_parser_template_parameter
2338 (cp_parser
*, bool *, bool *);
2339 static tree cp_parser_type_parameter
2340 (cp_parser
*, bool *);
2341 static tree cp_parser_template_id
2342 (cp_parser
*, bool, bool, enum tag_types
, bool);
2343 static tree cp_parser_template_name
2344 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2345 static tree cp_parser_template_argument_list
2347 static tree cp_parser_template_argument
2349 static void cp_parser_explicit_instantiation
2351 static void cp_parser_explicit_specialization
2354 /* Exception handling [gram.exception] */
2356 static tree cp_parser_try_block
2358 static void cp_parser_function_try_block
2360 static void cp_parser_handler_seq
2362 static void cp_parser_handler
2364 static tree cp_parser_exception_declaration
2366 static tree cp_parser_throw_expression
2368 static tree cp_parser_exception_specification_opt
2370 static tree cp_parser_type_id_list
2373 /* GNU Extensions */
2375 static tree cp_parser_asm_specification_opt
2377 static tree cp_parser_asm_operand_list
2379 static tree cp_parser_asm_clobber_list
2381 static tree cp_parser_asm_label_list
2383 static bool cp_next_tokens_can_be_attribute_p
2385 static bool cp_next_tokens_can_be_gnu_attribute_p
2387 static bool cp_next_tokens_can_be_std_attribute_p
2389 static bool cp_nth_tokens_can_be_std_attribute_p
2390 (cp_parser
*, size_t);
2391 static bool cp_nth_tokens_can_be_gnu_attribute_p
2392 (cp_parser
*, size_t);
2393 static bool cp_nth_tokens_can_be_attribute_p
2394 (cp_parser
*, size_t);
2395 static tree cp_parser_attributes_opt
2397 static tree cp_parser_gnu_attributes_opt
2399 static tree cp_parser_gnu_attribute_list
2401 static tree cp_parser_std_attribute
2402 (cp_parser
*, tree
);
2403 static tree cp_parser_std_attribute_spec
2405 static tree cp_parser_std_attribute_spec_seq
2407 static bool cp_parser_extension_opt
2408 (cp_parser
*, int *);
2409 static void cp_parser_label_declaration
2412 /* Concept Extensions */
2414 static tree cp_parser_requires_clause
2416 static tree cp_parser_requires_clause_opt
2418 static tree cp_parser_requires_expression
2420 static tree cp_parser_requirement_parameter_list
2422 static tree cp_parser_requirement_body
2424 static tree cp_parser_requirement_list
2426 static tree cp_parser_requirement
2428 static tree cp_parser_simple_requirement
2430 static tree cp_parser_compound_requirement
2432 static tree cp_parser_type_requirement
2434 static tree cp_parser_nested_requirement
2437 /* Transactional Memory Extensions */
2439 static tree cp_parser_transaction
2440 (cp_parser
*, cp_token
*);
2441 static tree cp_parser_transaction_expression
2442 (cp_parser
*, enum rid
);
2443 static void cp_parser_function_transaction
2444 (cp_parser
*, enum rid
);
2445 static tree cp_parser_transaction_cancel
2448 enum pragma_context
{
2455 static bool cp_parser_pragma
2456 (cp_parser
*, enum pragma_context
, bool *);
2458 /* Objective-C++ Productions */
2460 static tree cp_parser_objc_message_receiver
2462 static tree cp_parser_objc_message_args
2464 static tree cp_parser_objc_message_expression
2466 static cp_expr cp_parser_objc_encode_expression
2468 static tree cp_parser_objc_defs_expression
2470 static tree cp_parser_objc_protocol_expression
2472 static tree cp_parser_objc_selector_expression
2474 static cp_expr cp_parser_objc_expression
2476 static bool cp_parser_objc_selector_p
2478 static tree cp_parser_objc_selector
2480 static tree cp_parser_objc_protocol_refs_opt
2482 static void cp_parser_objc_declaration
2483 (cp_parser
*, tree
);
2484 static tree cp_parser_objc_statement
2486 static bool cp_parser_objc_valid_prefix_attributes
2487 (cp_parser
*, tree
*);
2488 static void cp_parser_objc_at_property_declaration
2490 static void cp_parser_objc_at_synthesize_declaration
2492 static void cp_parser_objc_at_dynamic_declaration
2494 static tree cp_parser_objc_struct_declaration
2497 /* Utility Routines */
2499 static cp_expr cp_parser_lookup_name
2500 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2501 static tree cp_parser_lookup_name_simple
2502 (cp_parser
*, tree
, location_t
);
2503 static tree cp_parser_maybe_treat_template_as_class
2505 static bool cp_parser_check_declarator_template_parameters
2506 (cp_parser
*, cp_declarator
*, location_t
);
2507 static bool cp_parser_check_template_parameters
2508 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2509 static cp_expr cp_parser_simple_cast_expression
2511 static tree cp_parser_global_scope_opt
2512 (cp_parser
*, bool);
2513 static bool cp_parser_constructor_declarator_p
2514 (cp_parser
*, bool);
2515 static tree cp_parser_function_definition_from_specifiers_and_declarator
2516 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2517 static tree cp_parser_function_definition_after_declarator
2518 (cp_parser
*, bool);
2519 static bool cp_parser_template_declaration_after_export
2520 (cp_parser
*, bool);
2521 static void cp_parser_perform_template_parameter_access_checks
2522 (vec
<deferred_access_check
, va_gc
> *);
2523 static tree cp_parser_single_declaration
2524 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2525 static cp_expr cp_parser_functional_cast
2526 (cp_parser
*, tree
);
2527 static tree cp_parser_save_member_function_body
2528 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2529 static tree cp_parser_save_nsdmi
2531 static tree cp_parser_enclosed_template_argument_list
2533 static void cp_parser_save_default_args
2534 (cp_parser
*, tree
);
2535 static void cp_parser_late_parsing_for_member
2536 (cp_parser
*, tree
);
2537 static tree cp_parser_late_parse_one_default_arg
2538 (cp_parser
*, tree
, tree
, tree
);
2539 static void cp_parser_late_parsing_nsdmi
2540 (cp_parser
*, tree
);
2541 static void cp_parser_late_parsing_default_args
2542 (cp_parser
*, tree
);
2543 static tree cp_parser_sizeof_operand
2544 (cp_parser
*, enum rid
);
2545 static tree cp_parser_trait_expr
2546 (cp_parser
*, enum rid
);
2547 static bool cp_parser_declares_only_class_p
2549 static void cp_parser_set_storage_class
2550 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2551 static void cp_parser_set_decl_spec_type
2552 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2553 static void set_and_check_decl_spec_loc
2554 (cp_decl_specifier_seq
*decl_specs
,
2555 cp_decl_spec ds
, cp_token
*);
2556 static bool cp_parser_friend_p
2557 (const cp_decl_specifier_seq
*);
2558 static void cp_parser_required_error
2559 (cp_parser
*, required_token
, bool, location_t
);
2560 static cp_token
*cp_parser_require
2561 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2562 static cp_token
*cp_parser_require_keyword
2563 (cp_parser
*, enum rid
, required_token
);
2564 static bool cp_parser_token_starts_function_definition_p
2566 static bool cp_parser_next_token_starts_class_definition_p
2568 static bool cp_parser_next_token_ends_template_argument_p
2570 static bool cp_parser_nth_token_starts_template_argument_list_p
2571 (cp_parser
*, size_t);
2572 static enum tag_types cp_parser_token_is_class_key
2574 static enum tag_types cp_parser_token_is_type_parameter_key
2576 static void cp_parser_check_class_key
2577 (enum tag_types
, tree type
);
2578 static void cp_parser_check_access_in_redeclaration
2579 (tree type
, location_t location
);
2580 static bool cp_parser_optional_template_keyword
2582 static void cp_parser_pre_parsed_nested_name_specifier
2584 static bool cp_parser_cache_group
2585 (cp_parser
*, enum cpp_ttype
, unsigned);
2586 static tree cp_parser_cache_defarg
2587 (cp_parser
*parser
, bool nsdmi
);
2588 static void cp_parser_parse_tentatively
2590 static void cp_parser_commit_to_tentative_parse
2592 static void cp_parser_commit_to_topmost_tentative_parse
2594 static void cp_parser_abort_tentative_parse
2596 static bool cp_parser_parse_definitely
2598 static inline bool cp_parser_parsing_tentatively
2600 static bool cp_parser_uncommitted_to_tentative_parse_p
2602 static void cp_parser_error
2603 (cp_parser
*, const char *);
2604 static void cp_parser_name_lookup_error
2605 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2606 static bool cp_parser_simulate_error
2608 static bool cp_parser_check_type_definition
2610 static void cp_parser_check_for_definition_in_return_type
2611 (cp_declarator
*, tree
, location_t type_location
);
2612 static void cp_parser_check_for_invalid_template_id
2613 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2614 static bool cp_parser_non_integral_constant_expression
2615 (cp_parser
*, non_integral_constant
);
2616 static void cp_parser_diagnose_invalid_type_name
2617 (cp_parser
*, tree
, location_t
);
2618 static bool cp_parser_parse_and_diagnose_invalid_type_name
2620 static int cp_parser_skip_to_closing_parenthesis
2621 (cp_parser
*, bool, bool, bool);
2622 static void cp_parser_skip_to_end_of_statement
2624 static void cp_parser_consume_semicolon_at_end_of_statement
2626 static void cp_parser_skip_to_end_of_block_or_statement
2628 static bool cp_parser_skip_to_closing_brace
2630 static void cp_parser_skip_to_end_of_template_parameter_list
2632 static void cp_parser_skip_to_pragma_eol
2633 (cp_parser
*, cp_token
*);
2634 static bool cp_parser_error_occurred
2636 static bool cp_parser_allow_gnu_extensions_p
2638 static bool cp_parser_is_pure_string_literal
2640 static bool cp_parser_is_string_literal
2642 static bool cp_parser_is_keyword
2643 (cp_token
*, enum rid
);
2644 static tree cp_parser_make_typename_type
2645 (cp_parser
*, tree
, location_t location
);
2646 static cp_declarator
* cp_parser_make_indirect_declarator
2647 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2648 static bool cp_parser_compound_literal_p
2650 static bool cp_parser_array_designator_p
2652 static bool cp_parser_init_statement_p
2654 static bool cp_parser_skip_to_closing_square_bracket
2657 /* Concept-related syntactic transformations */
2659 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2660 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2662 // -------------------------------------------------------------------------- //
2663 // Unevaluated Operand Guard
2665 // Implementation of an RAII helper for unevaluated operand parsing.
2666 cp_unevaluated::cp_unevaluated ()
2668 ++cp_unevaluated_operand
;
2669 ++c_inhibit_evaluation_warnings
;
2672 cp_unevaluated::~cp_unevaluated ()
2674 --c_inhibit_evaluation_warnings
;
2675 --cp_unevaluated_operand
;
2678 // -------------------------------------------------------------------------- //
2679 // Tentative Parsing
2681 /* Returns nonzero if we are parsing tentatively. */
2684 cp_parser_parsing_tentatively (cp_parser
* parser
)
2686 return parser
->context
->next
!= NULL
;
2689 /* Returns nonzero if TOKEN is a string literal. */
2692 cp_parser_is_pure_string_literal (cp_token
* token
)
2694 return (token
->type
== CPP_STRING
||
2695 token
->type
== CPP_STRING16
||
2696 token
->type
== CPP_STRING32
||
2697 token
->type
== CPP_WSTRING
||
2698 token
->type
== CPP_UTF8STRING
);
2701 /* Returns nonzero if TOKEN is a string literal
2702 of a user-defined string literal. */
2705 cp_parser_is_string_literal (cp_token
* token
)
2707 return (cp_parser_is_pure_string_literal (token
) ||
2708 token
->type
== CPP_STRING_USERDEF
||
2709 token
->type
== CPP_STRING16_USERDEF
||
2710 token
->type
== CPP_STRING32_USERDEF
||
2711 token
->type
== CPP_WSTRING_USERDEF
||
2712 token
->type
== CPP_UTF8STRING_USERDEF
);
2715 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2718 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2720 return token
->keyword
== keyword
;
2723 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2726 static enum pragma_kind
2727 cp_parser_pragma_kind (cp_token
*token
)
2729 if (token
->type
!= CPP_PRAGMA
)
2731 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2732 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2735 /* Helper function for cp_parser_error.
2736 Having peeked a token of kind TOK1_KIND that might signify
2737 a conflict marker, peek successor tokens to determine
2738 if we actually do have a conflict marker.
2739 Specifically, we consider a run of 7 '<', '=' or '>' characters
2740 at the start of a line as a conflict marker.
2741 These come through the lexer as three pairs and a single,
2742 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2743 If it returns true, *OUT_LOC is written to with the location/range
2747 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2748 location_t
*out_loc
)
2750 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2751 if (token2
->type
!= tok1_kind
)
2753 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2754 if (token3
->type
!= tok1_kind
)
2756 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2757 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2760 /* It must be at the start of the line. */
2761 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2762 if (LOCATION_COLUMN (start_loc
) != 1)
2765 /* We have a conflict marker. Construct a location of the form:
2768 with start == caret, finishing at the end of the marker. */
2769 location_t finish_loc
= get_finish (token4
->location
);
2770 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2775 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2779 get_matching_symbol (required_token token_desc
)
2786 case RT_CLOSE_BRACE
:
2788 case RT_CLOSE_PAREN
:
2793 /* Attempt to convert TOKEN_DESC from a required_token to an
2794 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2796 static enum cpp_ttype
2797 get_required_cpp_ttype (required_token token_desc
)
2802 return CPP_SEMICOLON
;
2804 return CPP_OPEN_PAREN
;
2805 case RT_CLOSE_BRACE
:
2806 return CPP_CLOSE_BRACE
;
2808 return CPP_OPEN_BRACE
;
2809 case RT_CLOSE_SQUARE
:
2810 return CPP_CLOSE_SQUARE
;
2811 case RT_OPEN_SQUARE
:
2812 return CPP_OPEN_SQUARE
;
2817 case RT_CLOSE_PAREN
:
2818 return CPP_CLOSE_PAREN
;
2821 /* Use CPP_EOF as a "no completions possible" code. */
2827 /* Subroutine of cp_parser_error and cp_parser_required_error.
2829 Issue a diagnostic of the form
2830 FILE:LINE: MESSAGE before TOKEN
2831 where TOKEN is the next token in the input stream. MESSAGE
2832 (specified by the caller) is usually of the form "expected
2835 This bypasses the check for tentative passing, and potentially
2836 adds material needed by cp_parser_required_error.
2838 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2839 suggesting insertion of the missing token.
2841 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2842 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2846 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2847 required_token missing_token_desc
,
2848 location_t matching_location
)
2850 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2851 /* This diagnostic makes more sense if it is tagged to the line
2852 of the token we just peeked at. */
2853 cp_lexer_set_source_position_from_token (token
);
2855 if (token
->type
== CPP_PRAGMA
)
2857 error_at (token
->location
,
2858 "%<#pragma%> is not allowed here");
2859 cp_parser_skip_to_pragma_eol (parser
, token
);
2863 /* If this is actually a conflict marker, report it as such. */
2864 if (token
->type
== CPP_LSHIFT
2865 || token
->type
== CPP_RSHIFT
2866 || token
->type
== CPP_EQ_EQ
)
2869 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2871 error_at (loc
, "version control conflict marker in file");
2876 gcc_rich_location
richloc (input_location
);
2878 bool added_matching_location
= false;
2880 if (missing_token_desc
!= RT_NONE
)
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser
->lexer
)->location
;
2888 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location
!= UNKNOWN_LOCATION
)
2894 added_matching_location
2895 = richloc
.add_location_if_nearby (matching_location
);
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid
,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2903 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2904 token
->u
.value
, token
->flags
, &richloc
);
2906 if (missing_token_desc
!= RT_NONE
)
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location
!= UNKNOWN_LOCATION
2911 && !added_matching_location
)
2912 inform (matching_location
, "to match this %qs",
2913 get_matching_symbol (missing_token_desc
));
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2924 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2926 if (!cp_parser_simulate_error (parser
))
2927 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2936 cp_parser_name_lookup_error (cp_parser
* parser
,
2939 name_lookup_error desired
,
2940 location_t location
)
2942 /* If name lookup completely failed, tell the user that NAME was not
2944 if (decl
== error_mark_node
)
2946 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2947 error_at (location
, "%<%E::%E%> has not been declared",
2948 parser
->scope
, name
);
2949 else if (parser
->scope
== global_namespace
)
2950 error_at (location
, "%<::%E%> has not been declared", name
);
2951 else if (parser
->object_scope
2952 && !CLASS_TYPE_P (parser
->object_scope
))
2953 error_at (location
, "request for member %qE in non-class type %qT",
2954 name
, parser
->object_scope
);
2955 else if (parser
->object_scope
)
2956 error_at (location
, "%<%T::%E%> has not been declared",
2957 parser
->object_scope
, name
);
2959 error_at (location
, "%qE has not been declared", name
);
2961 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2966 error_at (location
, "%<%E::%E%> is not a type",
2967 parser
->scope
, name
);
2970 error_at (location
, "%<%E::%E%> is not a class or namespace",
2971 parser
->scope
, name
);
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser
->scope
, name
);
2983 else if (parser
->scope
== global_namespace
)
2988 error_at (location
, "%<::%E%> is not a type", name
);
2991 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2995 "%<::%E%> is not a class, namespace, or enumeration",
3007 error_at (location
, "%qE is not a type", name
);
3010 error_at (location
, "%qE is not a class or namespace", name
);
3014 "%qE is not a class, namespace, or enumeration", name
);
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3027 cp_parser_simulate_error (cp_parser
* parser
)
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3031 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3042 cp_parser_check_type_definition (cp_parser
* parser
)
3044 /* If types are forbidden here, issue a message. */
3045 if (parser
->type_definition_forbidden_message
)
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser
->type_definition_forbidden_message
);
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3062 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3063 tree type
, location_t type_location
)
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3069 && (declarator
->kind
== cdk_pointer
3070 || declarator
->kind
== cdk_reference
3071 || declarator
->kind
== cdk_ptrmem
))
3072 declarator
= declarator
->declarator
;
3074 && declarator
->kind
== cdk_function
)
3076 error_at (type_location
,
3077 "new types may not be defined in a return type");
3078 inform (type_location
,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3091 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3093 enum tag_types tag_type
,
3094 location_t location
)
3096 cp_token_position start
= 0;
3098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3100 if (TREE_CODE (type
) == TYPE_DECL
)
3101 type
= TREE_TYPE (type
);
3102 if (TYPE_P (type
) && !template_placeholder_p (type
))
3103 error_at (location
, "%qT is not a template", type
);
3104 else if (identifier_p (type
))
3106 if (tag_type
!= none_type
)
3107 error_at (location
, "%qE is not a class template", type
);
3109 error_at (location
, "%qE is not a template", type
);
3112 error_at (location
, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3115 start
= cp_lexer_token_position (parser
->lexer
, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser
->lexer
);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser
);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3123 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3133 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3134 non_integral_constant thing
)
3136 parser
->non_integral_constant_expression_p
= true;
3137 if (parser
->integral_constant_expression_p
)
3139 if (!parser
->allow_non_integral_constant_expression_p
)
3141 const char *msg
= NULL
;
3145 pedwarn (input_location
, OPT_Wpedantic
,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3178 case NIC_ADDR_LABEL
:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3182 case NIC_OVERLOADED
:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3186 case NIC_ASSIGNMENT
:
3187 error ("an assignment cannot appear in a constant-expression");
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3193 case NIC_CONSTRUCTOR
:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3197 case NIC_TRANSACTION
:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3205 msg
= "__FUNCTION__";
3207 case NIC_PRETTY_FUNC
:
3208 msg
= "__PRETTY_FUNCTION__";
3228 case NIC_PREINCREMENT
:
3231 case NIC_PREDECREMENT
:
3244 error ("%qs cannot appear in a constant-expression", msg
);
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3257 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3258 location_t location
)
3260 tree decl
, ambiguous_decls
;
3261 cp_parser_commit_to_tentative_parse (parser
);
3262 /* Try to lookup the identifier. */
3263 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls
, location
);
3268 if (ambiguous_decls
)
3269 /* If the lookup was ambiguous, an error will already have
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl
))
3277 "invalid use of template-name %qE without an argument list",
3279 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3280 inform (location
, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3284 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3285 error_at (location
, "invalid use of destructor %qD as a type", id
);
3286 else if (TREE_CODE (decl
) == TYPE_DECL
)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location
, "invalid combination of multiple type-specifiers");
3289 else if (!parser
->scope
)
3291 /* Issue an error message. */
3293 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3294 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3297 gcc_rich_location
richloc (location
);
3298 richloc
.add_fixit_replace (hint
.suggestion ());
3300 "%qE does not name a type; did you mean %qs?",
3301 id
, hint
.suggestion ());
3304 error_at (location
, "%qE does not name a type", id
);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3313 inform (location
, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3316 inform (location
, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect
< cxx11
3319 && TREE_CODE (id
) == IDENTIFIER_NODE
3320 && id_equal (id
, "thread_local"))
3321 inform (location
, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3324 inform (location
, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl
&& current_class_type
3326 && TYPE_BINFO (current_class_type
))
3330 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3334 tree base_type
= BINFO_TYPE (b
);
3335 if (CLASS_TYPE_P (base_type
)
3336 && dependent_type_p (base_type
))
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3343 for (field
= TYPE_FIELDS (base_type
);
3345 field
= DECL_CHAIN (field
))
3346 if (TREE_CODE (field
) == TYPE_DECL
3347 && DECL_NAME (field
) == id
)
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b
), id
);
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser
->scope
!= error_mark_node
)
3364 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3367 error_at (location_of (id
),
3368 "%qE in namespace %qE does not name a template type",
3370 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3371 error_at (location_of (id
),
3372 "%qE in namespace %qE does not name a template type",
3373 TREE_OPERAND (id
, 0), parser
->scope
);
3375 error_at (location_of (id
),
3376 "%qE in namespace %qE does not name a type",
3379 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3381 else if (CLASS_TYPE_P (parser
->scope
)
3382 && constructor_name_p (id
, parser
->scope
))
3385 error_at (location
, "%<%T::%E%> names the constructor, not"
3386 " the type", parser
->scope
, id
);
3387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3388 error_at (location
, "and %qT has no template constructors",
3391 else if (TYPE_P (parser
->scope
)
3392 && dependent_scope_p (parser
->scope
))
3394 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3396 "need %<typename%> before %<%T::%D::%E%> because "
3397 "%<%T::%D%> is a dependent scope",
3398 TYPE_CONTEXT (parser
->scope
),
3399 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3401 TYPE_CONTEXT (parser
->scope
),
3402 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3404 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3405 "%qT is a dependent scope",
3406 parser
->scope
, id
, parser
->scope
);
3408 else if (TYPE_P (parser
->scope
))
3410 if (!COMPLETE_TYPE_P (parser
->scope
))
3411 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3413 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3417 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a template type",
3420 TREE_OPERAND (id
, 0), parser
->scope
);
3422 error_at (location_of (id
),
3423 "%qE in %q#T does not name a type",
3426 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3433 /* Check for a common situation where a type-name should be present,
3434 but is not, and issue a sensible error message. Returns true if an
3435 invalid type-name was detected.
3437 The situation handled by this function are variable declarations of the
3438 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3439 Usually, `ID' should name a type, but if we got here it means that it
3440 does not. We try to emit the best possible error message depending on
3441 how exactly the id-expression looks like. */
3444 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3449 /* Avoid duplicate error about ambiguous lookup. */
3450 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3452 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3453 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3457 cp_parser_parse_tentatively (parser
);
3458 id
= cp_parser_id_expression (parser
,
3459 /*template_keyword_p=*/false,
3460 /*check_dependency_p=*/true,
3461 /*template_p=*/NULL
,
3462 /*declarator_p=*/true,
3463 /*optional_p=*/false);
3464 /* If the next token is a (, this is a function with no explicit return
3465 type, i.e. constructor, destructor or conversion op. */
3466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3467 || TREE_CODE (id
) == TYPE_DECL
)
3469 cp_parser_abort_tentative_parse (parser
);
3472 if (!cp_parser_parse_definitely (parser
))
3475 /* Emit a diagnostic for the invalid type. */
3476 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3478 /* If we aren't in the middle of a declarator (i.e. in a
3479 parameter-declaration-clause), skip to the end of the declaration;
3480 there's no point in trying to process it. */
3481 if (!parser
->in_declarator_p
)
3482 cp_parser_skip_to_end_of_block_or_statement (parser
);
3486 /* Consume tokens up to, and including, the next non-nested closing `)'.
3487 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3488 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3489 found an unnested token of that type. */
3492 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3497 unsigned paren_depth
= 0;
3498 unsigned brace_depth
= 0;
3499 unsigned square_depth
= 0;
3501 if (recovering
&& or_ttype
== CPP_EOF
3502 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3507 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3509 /* Have we found what we're looking for before the closing paren? */
3510 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3511 && !brace_depth
&& !paren_depth
&& !square_depth
)
3514 switch (token
->type
)
3517 case CPP_PRAGMA_EOL
:
3518 /* If we've run out of tokens, then there is no closing `)'. */
3521 /* This is good for lambda expression capture-lists. */
3522 case CPP_OPEN_SQUARE
:
3525 case CPP_CLOSE_SQUARE
:
3526 if (!square_depth
--)
3531 /* This matches the processing in skip_to_end_of_statement. */
3536 case CPP_OPEN_BRACE
:
3539 case CPP_CLOSE_BRACE
:
3544 case CPP_OPEN_PAREN
:
3549 case CPP_CLOSE_PAREN
:
3550 if (!brace_depth
&& !paren_depth
--)
3553 cp_lexer_consume_token (parser
->lexer
);
3562 /* Consume the token. */
3563 cp_lexer_consume_token (parser
->lexer
);
3567 /* Consume tokens up to, and including, the next non-nested closing `)'.
3568 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3569 are doing error recovery. Returns -1 if OR_COMMA is true and we
3570 found an unnested token of that type. */
3573 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3578 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3579 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3580 ttype
, consume_paren
);
3583 /* Consume tokens until we reach the end of the current statement.
3584 Normally, that will be just before consuming a `;'. However, if a
3585 non-nested `}' comes first, then we stop before consuming that. */
3588 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3590 unsigned nesting_depth
= 0;
3592 /* Unwind generic function template scope if necessary. */
3593 if (parser
->fully_implicit_function_template_p
)
3594 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3598 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3600 switch (token
->type
)
3603 case CPP_PRAGMA_EOL
:
3604 /* If we've run out of tokens, stop. */
3608 /* If the next token is a `;', we have reached the end of the
3614 case CPP_CLOSE_BRACE
:
3615 /* If this is a non-nested '}', stop before consuming it.
3616 That way, when confronted with something like:
3620 we stop before consuming the closing '}', even though we
3621 have not yet reached a `;'. */
3622 if (nesting_depth
== 0)
3625 /* If it is the closing '}' for a block that we have
3626 scanned, stop -- but only after consuming the token.
3632 we will stop after the body of the erroneously declared
3633 function, but before consuming the following `typedef'
3635 if (--nesting_depth
== 0)
3637 cp_lexer_consume_token (parser
->lexer
);
3642 case CPP_OPEN_BRACE
:
3650 /* Consume the token. */
3651 cp_lexer_consume_token (parser
->lexer
);
3655 /* This function is called at the end of a statement or declaration.
3656 If the next token is a semicolon, it is consumed; otherwise, error
3657 recovery is attempted. */
3660 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3662 /* Look for the trailing `;'. */
3663 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3665 /* If there is additional (erroneous) input, skip to the end of
3667 cp_parser_skip_to_end_of_statement (parser
);
3668 /* If the next token is now a `;', consume it. */
3669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3670 cp_lexer_consume_token (parser
->lexer
);
3674 /* Skip tokens until we have consumed an entire block, or until we
3675 have consumed a non-nested `;'. */
3678 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3680 int nesting_depth
= 0;
3682 /* Unwind generic function template scope if necessary. */
3683 if (parser
->fully_implicit_function_template_p
)
3684 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3686 while (nesting_depth
>= 0)
3688 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3690 switch (token
->type
)
3693 case CPP_PRAGMA_EOL
:
3694 /* If we've run out of tokens, stop. */
3698 /* Stop if this is an unnested ';'. */
3703 case CPP_CLOSE_BRACE
:
3704 /* Stop if this is an unnested '}', or closes the outermost
3707 if (nesting_depth
< 0)
3713 case CPP_OPEN_BRACE
:
3722 /* Consume the token. */
3723 cp_lexer_consume_token (parser
->lexer
);
3727 /* Skip tokens until a non-nested closing curly brace is the next
3728 token, or there are no more tokens. Return true in the first case,
3732 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3734 unsigned nesting_depth
= 0;
3738 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3740 switch (token
->type
)
3743 case CPP_PRAGMA_EOL
:
3744 /* If we've run out of tokens, stop. */
3747 case CPP_CLOSE_BRACE
:
3748 /* If the next token is a non-nested `}', then we have reached
3749 the end of the current block. */
3750 if (nesting_depth
-- == 0)
3754 case CPP_OPEN_BRACE
:
3755 /* If it the next token is a `{', then we are entering a new
3756 block. Consume the entire block. */
3764 /* Consume the token. */
3765 cp_lexer_consume_token (parser
->lexer
);
3769 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3770 parameter is the PRAGMA token, allowing us to purge the entire pragma
3774 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3778 parser
->lexer
->in_pragma
= false;
3781 token
= cp_lexer_consume_token (parser
->lexer
);
3782 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3784 /* Ensure that the pragma is not parsed again. */
3785 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3788 /* Require pragma end of line, resyncing with it as necessary. The
3789 arguments are as for cp_parser_skip_to_pragma_eol. */
3792 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3794 parser
->lexer
->in_pragma
= false;
3795 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3796 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3799 /* This is a simple wrapper around make_typename_type. When the id is
3800 an unresolved identifier node, we can provide a superior diagnostic
3801 using cp_parser_diagnose_invalid_type_name. */
3804 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3805 location_t id_location
)
3808 if (identifier_p (id
))
3810 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3811 /*complain=*/tf_none
);
3812 if (result
== error_mark_node
)
3813 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3816 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3819 /* This is a wrapper around the
3820 make_{pointer,ptrmem,reference}_declarator functions that decides
3821 which one to call based on the CODE and CLASS_TYPE arguments. The
3822 CODE argument should be one of the values returned by
3823 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3824 appertain to the pointer or reference. */
3826 static cp_declarator
*
3827 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3828 cp_cv_quals cv_qualifiers
,
3829 cp_declarator
*target
,
3832 if (code
== ERROR_MARK
)
3833 return cp_error_declarator
;
3835 if (code
== INDIRECT_REF
)
3836 if (class_type
== NULL_TREE
)
3837 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3839 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3840 target
, attributes
);
3841 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3842 return make_reference_declarator (cv_qualifiers
, target
,
3844 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3845 return make_reference_declarator (cv_qualifiers
, target
,
3850 /* Create a new C++ parser. */
3853 cp_parser_new (void)
3859 /* cp_lexer_new_main is called before doing GC allocation because
3860 cp_lexer_new_main might load a PCH file. */
3861 lexer
= cp_lexer_new_main ();
3863 /* Initialize the binops_by_token so that we can get the tree
3864 directly from the token. */
3865 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3866 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3868 parser
= ggc_cleared_alloc
<cp_parser
> ();
3869 parser
->lexer
= lexer
;
3870 parser
->context
= cp_parser_context_new (NULL
);
3872 /* For now, we always accept GNU extensions. */
3873 parser
->allow_gnu_extensions_p
= 1;
3875 /* The `>' token is a greater-than operator, not the end of a
3877 parser
->greater_than_is_operator_p
= true;
3879 parser
->default_arg_ok_p
= true;
3881 /* We are not parsing a constant-expression. */
3882 parser
->integral_constant_expression_p
= false;
3883 parser
->allow_non_integral_constant_expression_p
= false;
3884 parser
->non_integral_constant_expression_p
= false;
3886 /* Local variable names are not forbidden. */
3887 parser
->local_variables_forbidden_p
= false;
3889 /* We are not processing an `extern "C"' declaration. */
3890 parser
->in_unbraced_linkage_specification_p
= false;
3892 /* We are not processing a declarator. */
3893 parser
->in_declarator_p
= false;
3895 /* We are not processing a template-argument-list. */
3896 parser
->in_template_argument_list_p
= false;
3898 /* We are not in an iteration statement. */
3899 parser
->in_statement
= 0;
3901 /* We are not in a switch statement. */
3902 parser
->in_switch_statement_p
= false;
3904 /* We are not parsing a type-id inside an expression. */
3905 parser
->in_type_id_in_expr_p
= false;
3907 /* Declarations aren't implicitly extern "C". */
3908 parser
->implicit_extern_c
= false;
3910 /* String literals should be translated to the execution character set. */
3911 parser
->translate_strings_p
= true;
3913 /* We are not parsing a function body. */
3914 parser
->in_function_body
= false;
3916 /* We can correct until told otherwise. */
3917 parser
->colon_corrects_to_scope_p
= true;
3919 /* The unparsed function queue is empty. */
3920 push_unparsed_function_queues (parser
);
3922 /* There are no classes being defined. */
3923 parser
->num_classes_being_defined
= 0;
3925 /* No template parameters apply. */
3926 parser
->num_template_parameter_lists
= 0;
3928 /* Special parsing data structures. */
3929 parser
->omp_declare_simd
= NULL
;
3930 parser
->cilk_simd_fn_info
= NULL
;
3931 parser
->oacc_routine
= NULL
;
3933 /* Not declaring an implicit function template. */
3934 parser
->auto_is_implicit_function_template_parm_p
= false;
3935 parser
->fully_implicit_function_template_p
= false;
3936 parser
->implicit_template_parms
= 0;
3937 parser
->implicit_template_scope
= 0;
3939 /* Allow constrained-type-specifiers. */
3940 parser
->prevent_constrained_type_specifiers
= 0;
3942 /* We haven't yet seen an 'extern "C"'. */
3943 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3948 /* Create a cp_lexer structure which will emit the tokens in CACHE
3949 and push it onto the parser's lexer stack. This is used for delayed
3950 parsing of in-class method bodies and default arguments, and should
3951 not be confused with tentative parsing. */
3953 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3955 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3956 lexer
->next
= parser
->lexer
;
3957 parser
->lexer
= lexer
;
3959 /* Move the current source position to that of the first token in the
3961 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3964 /* Pop the top lexer off the parser stack. This is never used for the
3965 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3967 cp_parser_pop_lexer (cp_parser
*parser
)
3969 cp_lexer
*lexer
= parser
->lexer
;
3970 parser
->lexer
= lexer
->next
;
3971 cp_lexer_destroy (lexer
);
3973 /* Put the current source position back where it was before this
3974 lexer was pushed. */
3975 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3978 /* Lexical conventions [gram.lex] */
3980 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3984 cp_parser_identifier (cp_parser
* parser
)
3988 /* Look for the identifier. */
3989 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3990 /* Return the value. */
3992 return cp_expr (token
->u
.value
, token
->location
);
3994 return error_mark_node
;
3997 /* Parse a sequence of adjacent string constants. Returns a
3998 TREE_STRING representing the combined, nul-terminated string
3999 constant. If TRANSLATE is true, translate the string to the
4000 execution character set. If WIDE_OK is true, a wide string is
4003 C++98 [lex.string] says that if a narrow string literal token is
4004 adjacent to a wide string literal token, the behavior is undefined.
4005 However, C99 6.4.5p4 says that this results in a wide string literal.
4006 We follow C99 here, for consistency with the C front end.
4008 This code is largely lifted from lex_string() in c-lex.c.
4010 FUTURE: ObjC++ will need to handle @-strings here. */
4012 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4013 bool lookup_udlit
= true)
4017 struct obstack str_ob
;
4018 cpp_string str
, istr
, *strs
;
4020 enum cpp_ttype type
, curr_type
;
4021 int have_suffix_p
= 0;
4023 tree suffix_id
= NULL_TREE
;
4024 bool curr_tok_is_userdef_p
= false;
4026 tok
= cp_lexer_peek_token (parser
->lexer
);
4027 if (!cp_parser_is_string_literal (tok
))
4029 cp_parser_error (parser
, "expected string-literal");
4030 return error_mark_node
;
4033 location_t loc
= tok
->location
;
4035 if (cpp_userdef_string_p (tok
->type
))
4037 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4038 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4039 curr_tok_is_userdef_p
= true;
4043 string_tree
= tok
->u
.value
;
4044 curr_type
= tok
->type
;
4048 /* Try to avoid the overhead of creating and destroying an obstack
4049 for the common case of just one string. */
4050 if (!cp_parser_is_string_literal
4051 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4053 cp_lexer_consume_token (parser
->lexer
);
4055 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4056 str
.len
= TREE_STRING_LENGTH (string_tree
);
4059 if (curr_tok_is_userdef_p
)
4061 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4063 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4066 curr_type
= tok
->type
;
4072 location_t last_tok_loc
= tok
->location
;
4073 gcc_obstack_init (&str_ob
);
4078 cp_lexer_consume_token (parser
->lexer
);
4080 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4081 str
.len
= TREE_STRING_LENGTH (string_tree
);
4083 if (curr_tok_is_userdef_p
)
4085 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4086 if (have_suffix_p
== 0)
4088 suffix_id
= curr_suffix_id
;
4091 else if (have_suffix_p
== 1
4092 && curr_suffix_id
!= suffix_id
)
4094 error ("inconsistent user-defined literal suffixes"
4095 " %qD and %qD in string literal",
4096 suffix_id
, curr_suffix_id
);
4099 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4102 curr_type
= tok
->type
;
4104 if (type
!= curr_type
)
4106 if (type
== CPP_STRING
)
4108 else if (curr_type
!= CPP_STRING
)
4110 rich_location
rich_loc (line_table
, tok
->location
);
4111 rich_loc
.add_range (last_tok_loc
, false);
4112 error_at (&rich_loc
,
4113 "unsupported non-standard concatenation "
4114 "of string literals");
4118 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4120 last_tok_loc
= tok
->location
;
4122 tok
= cp_lexer_peek_token (parser
->lexer
);
4123 if (cpp_userdef_string_p (tok
->type
))
4125 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4126 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4127 curr_tok_is_userdef_p
= true;
4131 string_tree
= tok
->u
.value
;
4132 curr_type
= tok
->type
;
4133 curr_tok_is_userdef_p
= false;
4136 while (cp_parser_is_string_literal (tok
));
4138 /* A string literal built by concatenation has its caret=start at
4139 the start of the initial string, and its finish at the finish of
4140 the final string literal. */
4141 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4143 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4146 if (type
!= CPP_STRING
&& !wide_ok
)
4148 cp_parser_error (parser
, "a wide string is invalid in this context");
4152 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4153 (parse_in
, strs
, count
, &istr
, type
))
4155 value
= build_string (istr
.len
, (const char *)istr
.text
);
4156 free (CONST_CAST (unsigned char *, istr
.text
));
4162 case CPP_UTF8STRING
:
4163 TREE_TYPE (value
) = char_array_type_node
;
4166 TREE_TYPE (value
) = char16_array_type_node
;
4169 TREE_TYPE (value
) = char32_array_type_node
;
4172 TREE_TYPE (value
) = wchar_array_type_node
;
4176 value
= fix_string_type (value
);
4180 tree literal
= build_userdef_literal (suffix_id
, value
,
4181 OT_NONE
, NULL_TREE
);
4183 value
= cp_parser_userdef_string_literal (literal
);
4189 /* cpp_interpret_string has issued an error. */
4190 value
= error_mark_node
;
4193 obstack_free (&str_ob
, 0);
4195 return cp_expr (value
, loc
);
4198 /* Look up a literal operator with the name and the exact arguments. */
4201 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4204 decl
= lookup_name (name
);
4205 if (!decl
|| !is_overloaded_fn (decl
))
4206 return error_mark_node
;
4208 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4213 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4214 if (parmtypes
!= NULL_TREE
)
4216 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4217 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4219 tree tparm
= TREE_VALUE (parmtypes
);
4220 tree targ
= TREE_TYPE ((*args
)[ix
]);
4221 bool ptr
= TYPE_PTR_P (tparm
);
4222 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4223 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4225 || !same_type_p (TREE_TYPE (tparm
),
4230 && ix
== vec_safe_length (args
)
4231 /* May be this should be sufficient_parms_p instead,
4232 depending on how exactly should user-defined literals
4233 work in presence of default arguments on the literal
4234 operator parameters. */
4235 && parmtypes
== void_list_node
)
4240 return error_mark_node
;
4243 /* Parse a user-defined char constant. Returns a call to a user-defined
4244 literal operator taking the character as an argument. */
4247 cp_parser_userdef_char_literal (cp_parser
*parser
)
4249 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4250 tree literal
= token
->u
.value
;
4251 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4252 tree value
= USERDEF_LITERAL_VALUE (literal
);
4253 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4256 /* Build up a call to the user-defined operator */
4257 /* Lookup the name we got back from the id-expression. */
4258 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4259 vec_safe_push (args
, value
);
4260 decl
= lookup_literal_operator (name
, args
);
4261 if (!decl
|| decl
== error_mark_node
)
4263 error ("unable to find character literal operator %qD with %qT argument",
4264 name
, TREE_TYPE (value
));
4265 release_tree_vector (args
);
4266 return error_mark_node
;
4268 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4269 release_tree_vector (args
);
4273 /* A subroutine of cp_parser_userdef_numeric_literal to
4274 create a char... template parameter pack from a string node. */
4277 make_char_string_pack (tree value
)
4280 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4281 const char *str
= TREE_STRING_POINTER (value
);
4282 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4283 tree argvec
= make_tree_vec (1);
4285 /* Fill in CHARVEC with all of the parameters. */
4286 charvec
= make_tree_vec (len
);
4287 for (i
= 0; i
< len
; ++i
)
4288 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4290 /* Build the argument packs. */
4291 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4293 TREE_VEC_ELT (argvec
, 0) = argpack
;
4298 /* A subroutine of cp_parser_userdef_numeric_literal to
4299 create a char... template parameter pack from a string node. */
4302 make_string_pack (tree value
)
4305 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4306 const unsigned char *str
4307 = (const unsigned char *) TREE_STRING_POINTER (value
);
4308 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4309 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4310 tree argvec
= make_tree_vec (2);
4312 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4313 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4315 /* First template parm is character type. */
4316 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4318 /* Fill in CHARVEC with all of the parameters. */
4319 charvec
= make_tree_vec (len
);
4320 for (int i
= 0; i
< len
; ++i
)
4321 TREE_VEC_ELT (charvec
, i
)
4322 = double_int_to_tree (str_char_type_node
,
4323 double_int::from_buffer (str
+ i
* sz
, sz
));
4325 /* Build the argument packs. */
4326 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4328 TREE_VEC_ELT (argvec
, 1) = argpack
;
4333 /* Parse a user-defined numeric constant. returns a call to a user-defined
4334 literal operator. */
4337 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4339 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4340 tree literal
= token
->u
.value
;
4341 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4342 tree value
= USERDEF_LITERAL_VALUE (literal
);
4343 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4344 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4345 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4347 vec
<tree
, va_gc
> *args
;
4349 /* Look for a literal operator taking the exact type of numeric argument
4350 as the literal value. */
4351 args
= make_tree_vector ();
4352 vec_safe_push (args
, value
);
4353 decl
= lookup_literal_operator (name
, args
);
4354 if (decl
&& decl
!= error_mark_node
)
4356 result
= finish_call_expr (decl
, &args
, false, true,
4357 tf_warning_or_error
);
4359 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4361 warning_at (token
->location
, OPT_Woverflow
,
4362 "integer literal exceeds range of %qT type",
4363 long_long_unsigned_type_node
);
4368 warning_at (token
->location
, OPT_Woverflow
,
4369 "floating literal exceeds range of %qT type",
4370 long_double_type_node
);
4371 else if (overflow
< 0)
4372 warning_at (token
->location
, OPT_Woverflow
,
4373 "floating literal truncated to zero");
4376 release_tree_vector (args
);
4379 release_tree_vector (args
);
4381 /* If the numeric argument didn't work, look for a raw literal
4382 operator taking a const char* argument consisting of the number
4383 in string format. */
4384 args
= make_tree_vector ();
4385 vec_safe_push (args
, num_string
);
4386 decl
= lookup_literal_operator (name
, args
);
4387 if (decl
&& decl
!= error_mark_node
)
4389 result
= finish_call_expr (decl
, &args
, false, true,
4390 tf_warning_or_error
);
4391 release_tree_vector (args
);
4394 release_tree_vector (args
);
4396 /* If the raw literal didn't work, look for a non-type template
4397 function with parameter pack char.... Call the function with
4398 template parameter characters representing the number. */
4399 args
= make_tree_vector ();
4400 decl
= lookup_literal_operator (name
, args
);
4401 if (decl
&& decl
!= error_mark_node
)
4403 tree tmpl_args
= make_char_string_pack (num_string
);
4404 decl
= lookup_template_function (decl
, tmpl_args
);
4405 result
= finish_call_expr (decl
, &args
, false, true,
4406 tf_warning_or_error
);
4407 release_tree_vector (args
);
4411 release_tree_vector (args
);
4413 error ("unable to find numeric literal operator %qD", name
);
4414 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4415 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4416 "to enable more built-in suffixes");
4417 return error_mark_node
;
4420 /* Parse a user-defined string constant. Returns a call to a user-defined
4421 literal operator taking a character pointer and the length of the string
4425 cp_parser_userdef_string_literal (tree literal
)
4427 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4428 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4429 tree value
= USERDEF_LITERAL_VALUE (literal
);
4430 int len
= TREE_STRING_LENGTH (value
)
4431 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4433 vec
<tree
, va_gc
> *args
;
4435 /* Build up a call to the user-defined operator. */
4436 /* Lookup the name we got back from the id-expression. */
4437 args
= make_tree_vector ();
4438 vec_safe_push (args
, value
);
4439 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4440 decl
= lookup_literal_operator (name
, args
);
4442 if (decl
&& decl
!= error_mark_node
)
4444 result
= finish_call_expr (decl
, &args
, false, true,
4445 tf_warning_or_error
);
4446 release_tree_vector (args
);
4449 release_tree_vector (args
);
4451 /* Look for a template function with typename parameter CharT
4452 and parameter pack CharT... Call the function with
4453 template parameter characters representing the string. */
4454 args
= make_tree_vector ();
4455 decl
= lookup_literal_operator (name
, args
);
4456 if (decl
&& decl
!= error_mark_node
)
4458 tree tmpl_args
= make_string_pack (value
);
4459 decl
= lookup_template_function (decl
, tmpl_args
);
4460 result
= finish_call_expr (decl
, &args
, false, true,
4461 tf_warning_or_error
);
4462 release_tree_vector (args
);
4465 release_tree_vector (args
);
4467 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4468 name
, TREE_TYPE (value
), size_type_node
);
4469 return error_mark_node
;
4473 /* Basic concepts [gram.basic] */
4475 /* Parse a translation-unit.
4478 declaration-seq [opt]
4480 Returns TRUE if all went well. */
4483 cp_parser_translation_unit (cp_parser
* parser
)
4485 /* The address of the first non-permanent object on the declarator
4487 static void *declarator_obstack_base
;
4491 /* Create the declarator obstack, if necessary. */
4492 if (!cp_error_declarator
)
4494 gcc_obstack_init (&declarator_obstack
);
4495 /* Create the error declarator. */
4496 cp_error_declarator
= make_declarator (cdk_error
);
4497 /* Create the empty parameter list. */
4498 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4500 /* Remember where the base of the declarator obstack lies. */
4501 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4504 cp_parser_declaration_seq_opt (parser
);
4506 /* If there are no tokens left then all went well. */
4507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4509 /* Get rid of the token array; we don't need it any more. */
4510 cp_lexer_destroy (parser
->lexer
);
4511 parser
->lexer
= NULL
;
4513 /* This file might have been a context that's implicitly extern
4514 "C". If so, pop the lang context. (Only relevant for PCH.) */
4515 if (parser
->implicit_extern_c
)
4517 pop_lang_context ();
4518 parser
->implicit_extern_c
= false;
4522 finish_translation_unit ();
4528 cp_parser_error (parser
, "expected declaration");
4532 /* Make sure the declarator obstack was fully cleaned up. */
4533 gcc_assert (obstack_next_free (&declarator_obstack
)
4534 == declarator_obstack_base
);
4536 /* All went well. */
4540 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4541 decltype context. */
4543 static inline tsubst_flags_t
4544 complain_flags (bool decltype_p
)
4546 tsubst_flags_t complain
= tf_warning_or_error
;
4548 complain
|= tf_decltype
;
4552 /* We're about to parse a collection of statements. If we're currently
4553 parsing tentatively, set up a firewall so that any nested
4554 cp_parser_commit_to_tentative_parse won't affect the current context. */
4556 static cp_token_position
4557 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4559 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4562 cp_parser_parse_tentatively (parser
);
4563 cp_parser_commit_to_topmost_tentative_parse (parser
);
4564 return cp_lexer_token_position (parser
->lexer
, false);
4567 /* We've finished parsing the collection of statements. Wrap up the
4568 firewall and replace the relevant tokens with the parsed form. */
4571 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4577 /* Finish the firewall level. */
4578 cp_parser_parse_definitely (parser
);
4579 /* And remember the result of the parse for when we try again. */
4580 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4581 token
->type
= CPP_PREPARSED_EXPR
;
4582 token
->u
.value
= expr
;
4583 token
->keyword
= RID_MAX
;
4584 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4587 /* Like the above functions, but let the user modify the tokens. Used by
4588 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4589 later parses, so it makes sense to localize the effects of
4590 cp_parser_commit_to_tentative_parse. */
4592 struct tentative_firewall
4597 tentative_firewall (cp_parser
*p
): parser(p
)
4599 /* If we're currently parsing tentatively, start a committed level as a
4600 firewall and then an inner tentative parse. */
4601 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4603 cp_parser_parse_tentatively (parser
);
4604 cp_parser_commit_to_topmost_tentative_parse (parser
);
4605 cp_parser_parse_tentatively (parser
);
4609 ~tentative_firewall()
4613 /* Finish the inner tentative parse and the firewall, propagating any
4614 uncommitted error state to the outer tentative parse. */
4615 bool err
= cp_parser_error_occurred (parser
);
4616 cp_parser_parse_definitely (parser
);
4617 cp_parser_parse_definitely (parser
);
4619 cp_parser_simulate_error (parser
);
4624 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4625 This class is for tracking such a matching pair of symbols.
4626 In particular, it tracks the location of the first token,
4627 so that if the second token is missing, we can highlight the
4628 location of the first token when notifying the user about the
4631 template <typename traits_t
>
4635 /* token_pair's ctor. */
4636 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4638 /* If the next token is the opening symbol for this pair, consume it and
4640 Otherwise, issue an error and return false.
4641 In either case, record the location of the opening token. */
4643 bool require_open (cp_parser
*parser
)
4645 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4646 return cp_parser_require (parser
, traits_t::open_token_type
,
4647 traits_t::required_token_open
);
4650 /* Consume the next token from PARSER, recording its location as
4651 that of the opening token within the pair. */
4653 cp_token
* consume_open (cp_parser
*parser
)
4655 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4656 gcc_assert (tok
->type
== traits_t::open_token_type
);
4657 m_open_loc
= tok
->location
;
4661 /* If the next token is the closing symbol for this pair, consume it
4663 Otherwise, issue an error, highlighting the location of the
4664 corresponding opening token, and return NULL. */
4666 cp_token
*require_close (cp_parser
*parser
) const
4668 return cp_parser_require (parser
, traits_t::close_token_type
,
4669 traits_t::required_token_close
,
4674 location_t m_open_loc
;
4677 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4679 struct matching_paren_traits
4681 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4682 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4683 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4684 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4687 /* "matching_parens" is a token_pair<T> class for tracking matching
4688 pairs of parentheses. */
4690 typedef token_pair
<matching_paren_traits
> matching_parens
;
4692 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4694 struct matching_brace_traits
4696 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4697 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4698 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4699 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4702 /* "matching_braces" is a token_pair<T> class for tracking matching
4705 typedef token_pair
<matching_brace_traits
> matching_braces
;
4708 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4709 enclosing parentheses. */
4712 cp_parser_statement_expr (cp_parser
*parser
)
4714 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4716 /* Consume the '('. */
4717 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4718 matching_parens parens
;
4719 parens
.consume_open (parser
);
4720 /* Start the statement-expression. */
4721 tree expr
= begin_stmt_expr ();
4722 /* Parse the compound-statement. */
4723 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4725 expr
= finish_stmt_expr (expr
, false);
4726 /* Consume the ')'. */
4727 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4728 if (!parens
.require_close (parser
))
4729 cp_parser_skip_to_end_of_statement (parser
);
4731 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4732 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4733 return cp_expr (expr
, combined_loc
);
4736 /* Expressions [gram.expr] */
4738 /* Parse a fold-operator.
4741 - * / % ^ & | = < > << >>
4742 = -= *= /= %= ^= &= |= <<= >>=
4743 == != <= >= && || , .* ->*
4745 This returns the tree code corresponding to the matched operator
4746 as an int. When the current token matches a compound assignment
4747 opertor, the resulting tree code is the negative value of the
4748 non-assignment operator. */
4751 cp_parser_fold_operator (cp_token
*token
)
4753 switch (token
->type
)
4755 case CPP_PLUS
: return PLUS_EXPR
;
4756 case CPP_MINUS
: return MINUS_EXPR
;
4757 case CPP_MULT
: return MULT_EXPR
;
4758 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4759 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4760 case CPP_XOR
: return BIT_XOR_EXPR
;
4761 case CPP_AND
: return BIT_AND_EXPR
;
4762 case CPP_OR
: return BIT_IOR_EXPR
;
4763 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4764 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4766 case CPP_EQ
: return -NOP_EXPR
;
4767 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4768 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4769 case CPP_MULT_EQ
: return -MULT_EXPR
;
4770 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4771 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4772 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4773 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4774 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4775 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4776 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4778 case CPP_EQ_EQ
: return EQ_EXPR
;
4779 case CPP_NOT_EQ
: return NE_EXPR
;
4780 case CPP_LESS
: return LT_EXPR
;
4781 case CPP_GREATER
: return GT_EXPR
;
4782 case CPP_LESS_EQ
: return LE_EXPR
;
4783 case CPP_GREATER_EQ
: return GE_EXPR
;
4785 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4786 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4788 case CPP_COMMA
: return COMPOUND_EXPR
;
4790 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4791 case CPP_DEREF_STAR
: return MEMBER_REF
;
4793 default: return ERROR_MARK
;
4797 /* Returns true if CODE indicates a binary expression, which is not allowed in
4798 the LHS of a fold-expression. More codes will need to be added to use this
4799 function in other contexts. */
4802 is_binary_op (tree_code code
)
4807 case POINTER_PLUS_EXPR
:
4810 case TRUNC_DIV_EXPR
:
4811 case TRUNC_MOD_EXPR
:
4827 case TRUTH_ANDIF_EXPR
:
4828 case TRUTH_ORIF_EXPR
:
4841 /* If the next token is a suitable fold operator, consume it and return as
4842 the function above. */
4845 cp_parser_fold_operator (cp_parser
*parser
)
4847 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4848 int code
= cp_parser_fold_operator (token
);
4849 if (code
!= ERROR_MARK
)
4850 cp_lexer_consume_token (parser
->lexer
);
4854 /* Parse a fold-expression.
4857 ( ... folding-operator cast-expression)
4858 ( cast-expression folding-operator ... )
4859 ( cast-expression folding operator ... folding-operator cast-expression)
4861 Note that the '(' and ')' are matched in primary expression. */
4864 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4871 cp_lexer_consume_token (parser
->lexer
);
4872 int op
= cp_parser_fold_operator (parser
);
4873 if (op
== ERROR_MARK
)
4875 cp_parser_error (parser
, "expected binary operator");
4876 return error_mark_node
;
4879 tree expr
= cp_parser_cast_expression (parser
, false, false,
4881 if (expr
== error_mark_node
)
4882 return error_mark_node
;
4883 return finish_left_unary_fold_expr (expr
, op
);
4886 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4887 int op
= cp_parser_fold_operator (parser
);
4888 if (op
== ERROR_MARK
)
4890 cp_parser_error (parser
, "expected binary operator");
4891 return error_mark_node
;
4894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4896 cp_parser_error (parser
, "expected ...");
4897 return error_mark_node
;
4899 cp_lexer_consume_token (parser
->lexer
);
4901 /* The operands of a fold-expression are cast-expressions, so binary or
4902 conditional expressions are not allowed. We check this here to avoid
4903 tentative parsing. */
4904 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4905 /* OK, the expression was parenthesized. */;
4906 else if (is_binary_op (TREE_CODE (expr1
)))
4907 error_at (location_of (expr1
),
4908 "binary expression in operand of fold-expression");
4909 else if (TREE_CODE (expr1
) == COND_EXPR
)
4910 error_at (location_of (expr1
),
4911 "conditional expression in operand of fold-expression");
4914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4915 return finish_right_unary_fold_expr (expr1
, op
);
4917 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4919 cp_parser_error (parser
, "mismatched operator in fold-expression");
4920 return error_mark_node
;
4922 cp_lexer_consume_token (parser
->lexer
);
4924 // Binary left or right fold.
4925 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4926 if (expr2
== error_mark_node
)
4927 return error_mark_node
;
4928 return finish_binary_fold_expr (expr1
, expr2
, op
);
4931 /* Parse a primary-expression.
4938 lambda-expression (C++11)
4943 ( compound-statement )
4944 __builtin_va_arg ( assignment-expression , type-id )
4945 __builtin_offsetof ( type-id , offsetof-expression )
4948 __has_nothrow_assign ( type-id )
4949 __has_nothrow_constructor ( type-id )
4950 __has_nothrow_copy ( type-id )
4951 __has_trivial_assign ( type-id )
4952 __has_trivial_constructor ( type-id )
4953 __has_trivial_copy ( type-id )
4954 __has_trivial_destructor ( type-id )
4955 __has_virtual_destructor ( type-id )
4956 __is_abstract ( type-id )
4957 __is_base_of ( type-id , type-id )
4958 __is_class ( type-id )
4959 __is_empty ( type-id )
4960 __is_enum ( type-id )
4961 __is_final ( type-id )
4962 __is_literal_type ( type-id )
4963 __is_pod ( type-id )
4964 __is_polymorphic ( type-id )
4965 __is_std_layout ( type-id )
4966 __is_trivial ( type-id )
4967 __is_union ( type-id )
4969 Objective-C++ Extension:
4977 ADDRESS_P is true iff this expression was immediately preceded by
4978 "&" and therefore might denote a pointer-to-member. CAST_P is true
4979 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4980 true iff this expression is a template argument.
4982 Returns a representation of the expression. Upon return, *IDK
4983 indicates what kind of id-expression (if any) was present. */
4986 cp_parser_primary_expression (cp_parser
*parser
,
4989 bool template_arg_p
,
4993 cp_token
*token
= NULL
;
4995 /* Assume the primary expression is not an id-expression. */
4996 *idk
= CP_ID_KIND_NONE
;
4998 /* Peek at the next token. */
4999 token
= cp_lexer_peek_token (parser
->lexer
);
5000 switch ((int) token
->type
)
5009 user-defined-literal */
5016 case CPP_PREPARSED_EXPR
:
5017 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5018 return cp_parser_userdef_numeric_literal (parser
);
5019 token
= cp_lexer_consume_token (parser
->lexer
);
5020 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5022 error_at (token
->location
,
5023 "fixed-point types not supported in C++");
5024 return error_mark_node
;
5026 /* Floating-point literals are only allowed in an integral
5027 constant expression if they are cast to an integral or
5028 enumeration type. */
5029 if (TREE_CODE (token
->u
.value
) == REAL_CST
5030 && parser
->integral_constant_expression_p
5033 /* CAST_P will be set even in invalid code like "int(2.7 +
5034 ...)". Therefore, we have to check that the next token
5035 is sure to end the cast. */
5038 cp_token
*next_token
;
5040 next_token
= cp_lexer_peek_token (parser
->lexer
);
5041 if (/* The comma at the end of an
5042 enumerator-definition. */
5043 next_token
->type
!= CPP_COMMA
5044 /* The curly brace at the end of an enum-specifier. */
5045 && next_token
->type
!= CPP_CLOSE_BRACE
5046 /* The end of a statement. */
5047 && next_token
->type
!= CPP_SEMICOLON
5048 /* The end of the cast-expression. */
5049 && next_token
->type
!= CPP_CLOSE_PAREN
5050 /* The end of an array bound. */
5051 && next_token
->type
!= CPP_CLOSE_SQUARE
5052 /* The closing ">" in a template-argument-list. */
5053 && (next_token
->type
!= CPP_GREATER
5054 || parser
->greater_than_is_operator_p
)
5055 /* C++0x only: A ">>" treated like two ">" tokens,
5056 in a template-argument-list. */
5057 && (next_token
->type
!= CPP_RSHIFT
5058 || (cxx_dialect
== cxx98
)
5059 || parser
->greater_than_is_operator_p
))
5063 /* If we are within a cast, then the constraint that the
5064 cast is to an integral or enumeration type will be
5065 checked at that point. If we are not within a cast, then
5066 this code is invalid. */
5068 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5070 return cp_expr (token
->u
.value
, token
->location
);
5072 case CPP_CHAR_USERDEF
:
5073 case CPP_CHAR16_USERDEF
:
5074 case CPP_CHAR32_USERDEF
:
5075 case CPP_WCHAR_USERDEF
:
5076 case CPP_UTF8CHAR_USERDEF
:
5077 return cp_parser_userdef_char_literal (parser
);
5083 case CPP_UTF8STRING
:
5084 case CPP_STRING_USERDEF
:
5085 case CPP_STRING16_USERDEF
:
5086 case CPP_STRING32_USERDEF
:
5087 case CPP_WSTRING_USERDEF
:
5088 case CPP_UTF8STRING_USERDEF
:
5089 /* ??? Should wide strings be allowed when parser->translate_strings_p
5090 is false (i.e. in attributes)? If not, we can kill the third
5091 argument to cp_parser_string_literal. */
5092 return cp_parser_string_literal (parser
,
5093 parser
->translate_strings_p
,
5096 case CPP_OPEN_PAREN
:
5097 /* If we see `( { ' then we are looking at the beginning of
5098 a GNU statement-expression. */
5099 if (cp_parser_allow_gnu_extensions_p (parser
)
5100 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5102 /* Statement-expressions are not allowed by the standard. */
5103 pedwarn (token
->location
, OPT_Wpedantic
,
5104 "ISO C++ forbids braced-groups within expressions");
5106 /* And they're not allowed outside of a function-body; you
5107 cannot, for example, write:
5109 int i = ({ int j = 3; j + 1; });
5111 at class or namespace scope. */
5112 if (!parser
->in_function_body
5113 || parser
->in_template_argument_list_p
)
5115 error_at (token
->location
,
5116 "statement-expressions are not allowed outside "
5117 "functions nor in template-argument lists");
5118 cp_parser_skip_to_end_of_block_or_statement (parser
);
5119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5120 cp_lexer_consume_token (parser
->lexer
);
5121 return error_mark_node
;
5124 return cp_parser_statement_expr (parser
);
5126 /* Otherwise it's a normal parenthesized expression. */
5129 bool saved_greater_than_is_operator_p
;
5131 location_t open_paren_loc
= token
->location
;
5133 /* Consume the `('. */
5134 matching_parens parens
;
5135 parens
.consume_open (parser
);
5136 /* Within a parenthesized expression, a `>' token is always
5137 the greater-than operator. */
5138 saved_greater_than_is_operator_p
5139 = parser
->greater_than_is_operator_p
;
5140 parser
->greater_than_is_operator_p
= true;
5142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5143 /* Left fold expression. */
5146 /* Parse the parenthesized expression. */
5147 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5149 token
= cp_lexer_peek_token (parser
->lexer
);
5150 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5152 expr
= cp_parser_fold_expression (parser
, expr
);
5153 if (expr
!= error_mark_node
5154 && cxx_dialect
< cxx17
5155 && !in_system_header_at (input_location
))
5156 pedwarn (input_location
, 0, "fold-expressions only available "
5157 "with -std=c++17 or -std=gnu++17");
5160 /* Let the front end know that this expression was
5161 enclosed in parentheses. This matters in case, for
5162 example, the expression is of the form `A::B', since
5163 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5165 expr
= finish_parenthesized_expr (expr
);
5167 /* DR 705: Wrapping an unqualified name in parentheses
5168 suppresses arg-dependent lookup. We want to pass back
5169 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5170 (c++/37862), but none of the others. */
5171 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5172 *idk
= CP_ID_KIND_NONE
;
5174 /* The `>' token might be the end of a template-id or
5175 template-parameter-list now. */
5176 parser
->greater_than_is_operator_p
5177 = saved_greater_than_is_operator_p
;
5179 /* Consume the `)'. */
5180 token
= cp_lexer_peek_token (parser
->lexer
);
5181 location_t close_paren_loc
= token
->location
;
5182 expr
.set_range (open_paren_loc
, close_paren_loc
);
5183 if (!parens
.require_close (parser
)
5184 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5185 cp_parser_skip_to_end_of_statement (parser
);
5190 case CPP_OPEN_SQUARE
:
5192 if (c_dialect_objc ())
5194 /* We might have an Objective-C++ message. */
5195 cp_parser_parse_tentatively (parser
);
5196 tree msg
= cp_parser_objc_message_expression (parser
);
5197 /* If that works out, we're done ... */
5198 if (cp_parser_parse_definitely (parser
))
5200 /* ... else, fall though to see if it's a lambda. */
5202 cp_expr lam
= cp_parser_lambda_expression (parser
);
5203 /* Don't warn about a failed tentative parse. */
5204 if (cp_parser_error_occurred (parser
))
5205 return error_mark_node
;
5206 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5210 case CPP_OBJC_STRING
:
5211 if (c_dialect_objc ())
5212 /* We have an Objective-C++ string literal. */
5213 return cp_parser_objc_expression (parser
);
5214 cp_parser_error (parser
, "expected primary-expression");
5215 return error_mark_node
;
5218 switch (token
->keyword
)
5220 /* These two are the boolean literals. */
5222 cp_lexer_consume_token (parser
->lexer
);
5223 return cp_expr (boolean_true_node
, token
->location
);
5225 cp_lexer_consume_token (parser
->lexer
);
5226 return cp_expr (boolean_false_node
, token
->location
);
5228 /* The `__null' literal. */
5230 cp_lexer_consume_token (parser
->lexer
);
5231 return cp_expr (null_node
, token
->location
);
5233 /* The `nullptr' literal. */
5235 cp_lexer_consume_token (parser
->lexer
);
5236 return cp_expr (nullptr_node
, token
->location
);
5238 /* Recognize the `this' keyword. */
5240 cp_lexer_consume_token (parser
->lexer
);
5241 if (parser
->local_variables_forbidden_p
)
5243 error_at (token
->location
,
5244 "%<this%> may not be used in this context");
5245 return error_mark_node
;
5247 /* Pointers cannot appear in constant-expressions. */
5248 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5249 return error_mark_node
;
5250 return cp_expr (finish_this_expr (), token
->location
);
5252 /* The `operator' keyword can be the beginning of an
5257 case RID_FUNCTION_NAME
:
5258 case RID_PRETTY_FUNCTION_NAME
:
5259 case RID_C99_FUNCTION_NAME
:
5261 non_integral_constant name
;
5263 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5264 __func__ are the names of variables -- but they are
5265 treated specially. Therefore, they are handled here,
5266 rather than relying on the generic id-expression logic
5267 below. Grammatically, these names are id-expressions.
5269 Consume the token. */
5270 token
= cp_lexer_consume_token (parser
->lexer
);
5272 switch (token
->keyword
)
5274 case RID_FUNCTION_NAME
:
5275 name
= NIC_FUNC_NAME
;
5277 case RID_PRETTY_FUNCTION_NAME
:
5278 name
= NIC_PRETTY_FUNC
;
5280 case RID_C99_FUNCTION_NAME
:
5281 name
= NIC_C99_FUNC
;
5287 if (cp_parser_non_integral_constant_expression (parser
, name
))
5288 return error_mark_node
;
5290 /* Look up the name. */
5291 return finish_fname (token
->u
.value
);
5298 source_location type_location
;
5299 location_t start_loc
5300 = cp_lexer_peek_token (parser
->lexer
)->location
;
5301 /* The `__builtin_va_arg' construct is used to handle
5302 `va_arg'. Consume the `__builtin_va_arg' token. */
5303 cp_lexer_consume_token (parser
->lexer
);
5304 /* Look for the opening `('. */
5305 matching_parens parens
;
5306 parens
.require_open (parser
);
5307 /* Now, parse the assignment-expression. */
5308 expression
= cp_parser_assignment_expression (parser
);
5309 /* Look for the `,'. */
5310 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5311 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5312 /* Parse the type-id. */
5314 type_id_in_expr_sentinel
s (parser
);
5315 type
= cp_parser_type_id (parser
);
5317 /* Look for the closing `)'. */
5318 location_t finish_loc
5319 = cp_lexer_peek_token (parser
->lexer
)->location
;
5320 parens
.require_close (parser
);
5321 /* Using `va_arg' in a constant-expression is not
5323 if (cp_parser_non_integral_constant_expression (parser
,
5325 return error_mark_node
;
5326 /* Construct a location of the form:
5327 __builtin_va_arg (v, int)
5328 ~~~~~~~~~~~~~~~~~~~~~^~~~
5329 with the caret at the type, ranging from the start of the
5330 "__builtin_va_arg" token to the close paren. */
5331 location_t combined_loc
5332 = make_location (type_location
, start_loc
, finish_loc
);
5333 return build_x_va_arg (combined_loc
, expression
, type
);
5337 return cp_parser_builtin_offsetof (parser
);
5339 case RID_HAS_NOTHROW_ASSIGN
:
5340 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5341 case RID_HAS_NOTHROW_COPY
:
5342 case RID_HAS_TRIVIAL_ASSIGN
:
5343 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5344 case RID_HAS_TRIVIAL_COPY
:
5345 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5346 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5347 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5348 case RID_IS_ABSTRACT
:
5349 case RID_IS_AGGREGATE
:
5350 case RID_IS_BASE_OF
:
5355 case RID_IS_LITERAL_TYPE
:
5357 case RID_IS_POLYMORPHIC
:
5358 case RID_IS_SAME_AS
:
5359 case RID_IS_STD_LAYOUT
:
5360 case RID_IS_TRIVIAL
:
5361 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5362 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5363 case RID_IS_TRIVIALLY_COPYABLE
:
5365 case RID_IS_ASSIGNABLE
:
5366 case RID_IS_CONSTRUCTIBLE
:
5367 return cp_parser_trait_expr (parser
, token
->keyword
);
5371 return cp_parser_requires_expression (parser
);
5373 /* Objective-C++ expressions. */
5375 case RID_AT_PROTOCOL
:
5376 case RID_AT_SELECTOR
:
5377 return cp_parser_objc_expression (parser
);
5380 if (parser
->in_function_body
5381 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5384 error_at (token
->location
,
5385 "a template declaration cannot appear at block scope");
5386 cp_parser_skip_to_end_of_block_or_statement (parser
);
5387 return error_mark_node
;
5391 cp_parser_error (parser
, "expected primary-expression");
5392 return error_mark_node
;
5395 /* An id-expression can start with either an identifier, a
5396 `::' as the beginning of a qualified-id, or the "operator"
5400 case CPP_TEMPLATE_ID
:
5401 case CPP_NESTED_NAME_SPECIFIER
:
5404 cp_expr id_expression
;
5406 const char *error_msg
;
5409 cp_token
*id_expr_token
;
5411 /* Parse the id-expression. */
5413 = cp_parser_id_expression (parser
,
5414 /*template_keyword_p=*/false,
5415 /*check_dependency_p=*/true,
5417 /*declarator_p=*/false,
5418 /*optional_p=*/false);
5419 if (id_expression
== error_mark_node
)
5420 return error_mark_node
;
5421 id_expr_token
= token
;
5422 token
= cp_lexer_peek_token (parser
->lexer
);
5423 done
= (token
->type
!= CPP_OPEN_SQUARE
5424 && token
->type
!= CPP_OPEN_PAREN
5425 && token
->type
!= CPP_DOT
5426 && token
->type
!= CPP_DEREF
5427 && token
->type
!= CPP_PLUS_PLUS
5428 && token
->type
!= CPP_MINUS_MINUS
);
5429 /* If we have a template-id, then no further lookup is
5430 required. If the template-id was for a template-class, we
5431 will sometimes have a TYPE_DECL at this point. */
5432 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5433 || TREE_CODE (id_expression
) == TYPE_DECL
)
5434 decl
= id_expression
;
5435 /* Look up the name. */
5438 tree ambiguous_decls
;
5440 /* If we already know that this lookup is ambiguous, then
5441 we've already issued an error message; there's no reason
5443 if (id_expr_token
->type
== CPP_NAME
5444 && id_expr_token
->error_reported
)
5446 cp_parser_simulate_error (parser
);
5447 return error_mark_node
;
5450 decl
= cp_parser_lookup_name (parser
, id_expression
,
5453 /*is_namespace=*/false,
5454 /*check_dependency=*/true,
5456 id_expr_token
->location
);
5457 /* If the lookup was ambiguous, an error will already have
5459 if (ambiguous_decls
)
5460 return error_mark_node
;
5462 /* In Objective-C++, we may have an Objective-C 2.0
5463 dot-syntax for classes here. */
5464 if (c_dialect_objc ()
5465 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5466 && TREE_CODE (decl
) == TYPE_DECL
5467 && objc_is_class_name (decl
))
5470 cp_lexer_consume_token (parser
->lexer
);
5471 component
= cp_parser_identifier (parser
);
5472 if (component
== error_mark_node
)
5473 return error_mark_node
;
5475 tree result
= objc_build_class_component_ref (id_expression
,
5477 /* Build a location of the form:
5480 with caret at the start of the component name (at
5481 input_location), ranging from the start of the id_expression
5482 to the end of the component name. */
5483 location_t combined_loc
5484 = make_location (input_location
, id_expression
.get_start (),
5485 get_finish (input_location
));
5486 protected_set_expr_location (result
, combined_loc
);
5490 /* In Objective-C++, an instance variable (ivar) may be preferred
5491 to whatever cp_parser_lookup_name() found.
5492 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5493 rest of c-family, we have to do a little extra work to preserve
5494 any location information in cp_expr "decl". Given that
5495 objc_lookup_ivar is implemented in "c-family" and "objc", we
5496 have a trip through the pure "tree" type, rather than cp_expr.
5497 Naively copying it back to "decl" would implicitly give the
5498 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5499 store an EXPR_LOCATION. Hence we only update "decl" (and
5500 hence its location_t) if we get back a different tree node. */
5501 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5503 if (decl_tree
!= decl
.get_value ())
5504 decl
= cp_expr (decl_tree
);
5506 /* If name lookup gives us a SCOPE_REF, then the
5507 qualifying scope was dependent. */
5508 if (TREE_CODE (decl
) == SCOPE_REF
)
5510 /* At this point, we do not know if DECL is a valid
5511 integral constant expression. We assume that it is
5512 in fact such an expression, so that code like:
5514 template <int N> struct A {
5518 is accepted. At template-instantiation time, we
5519 will check that B<N>::i is actually a constant. */
5522 /* Check to see if DECL is a local variable in a context
5523 where that is forbidden. */
5524 if (parser
->local_variables_forbidden_p
5525 && local_variable_p (decl
))
5527 /* It might be that we only found DECL because we are
5528 trying to be generous with pre-ISO scoping rules.
5529 For example, consider:
5533 for (int i = 0; i < 10; ++i) {}
5534 extern void f(int j = i);
5537 Here, name look up will originally find the out
5538 of scope `i'. We need to issue a warning message,
5539 but then use the global `i'. */
5540 decl
= check_for_out_of_scope_variable (decl
);
5541 if (local_variable_p (decl
))
5543 error_at (id_expr_token
->location
,
5544 "local variable %qD may not appear in this context",
5546 return error_mark_node
;
5551 decl
= (finish_id_expression
5552 (id_expression
, decl
, parser
->scope
,
5554 parser
->integral_constant_expression_p
,
5555 parser
->allow_non_integral_constant_expression_p
,
5556 &parser
->non_integral_constant_expression_p
,
5557 template_p
, done
, address_p
,
5560 id_expression
.get_location ()));
5562 cp_parser_error (parser
, error_msg
);
5563 decl
.set_location (id_expr_token
->location
);
5567 /* Anything else is an error. */
5569 cp_parser_error (parser
, "expected primary-expression");
5570 return error_mark_node
;
5574 static inline cp_expr
5575 cp_parser_primary_expression (cp_parser
*parser
,
5578 bool template_arg_p
,
5581 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5582 /*decltype*/false, idk
);
5585 /* Parse an id-expression.
5592 :: [opt] nested-name-specifier template [opt] unqualified-id
5594 :: operator-function-id
5597 Return a representation of the unqualified portion of the
5598 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5599 a `::' or nested-name-specifier.
5601 Often, if the id-expression was a qualified-id, the caller will
5602 want to make a SCOPE_REF to represent the qualified-id. This
5603 function does not do this in order to avoid wastefully creating
5604 SCOPE_REFs when they are not required.
5606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5609 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5610 uninstantiated templates.
5612 If *TEMPLATE_P is non-NULL, it is set to true iff the
5613 `template' keyword is used to explicitly indicate that the entity
5614 named is a template.
5616 If DECLARATOR_P is true, the id-expression is appearing as part of
5617 a declarator, rather than as part of an expression. */
5620 cp_parser_id_expression (cp_parser
*parser
,
5621 bool template_keyword_p
,
5622 bool check_dependency_p
,
5627 bool global_scope_p
;
5628 bool nested_name_specifier_p
;
5630 /* Assume the `template' keyword was not used. */
5632 *template_p
= template_keyword_p
;
5634 /* Look for the optional `::' operator. */
5636 = (!template_keyword_p
5637 && (cp_parser_global_scope_opt (parser
,
5638 /*current_scope_valid_p=*/false)
5641 /* Look for the optional nested-name-specifier. */
5642 nested_name_specifier_p
5643 = (cp_parser_nested_name_specifier_opt (parser
,
5644 /*typename_keyword_p=*/false,
5651 /* If there is a nested-name-specifier, then we are looking at
5652 the first qualified-id production. */
5653 if (nested_name_specifier_p
)
5656 tree saved_object_scope
;
5657 tree saved_qualifying_scope
;
5658 cp_expr unqualified_id
;
5661 /* See if the next token is the `template' keyword. */
5663 template_p
= &is_template
;
5664 *template_p
= cp_parser_optional_template_keyword (parser
);
5665 /* Name lookup we do during the processing of the
5666 unqualified-id might obliterate SCOPE. */
5667 saved_scope
= parser
->scope
;
5668 saved_object_scope
= parser
->object_scope
;
5669 saved_qualifying_scope
= parser
->qualifying_scope
;
5670 /* Process the final unqualified-id. */
5671 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5674 /*optional_p=*/false);
5675 /* Restore the SAVED_SCOPE for our caller. */
5676 parser
->scope
= saved_scope
;
5677 parser
->object_scope
= saved_object_scope
;
5678 parser
->qualifying_scope
= saved_qualifying_scope
;
5680 return unqualified_id
;
5682 /* Otherwise, if we are in global scope, then we are looking at one
5683 of the other qualified-id productions. */
5684 else if (global_scope_p
)
5689 /* Peek at the next token. */
5690 token
= cp_lexer_peek_token (parser
->lexer
);
5692 /* If it's an identifier, and the next token is not a "<", then
5693 we can avoid the template-id case. This is an optimization
5694 for this common case. */
5695 if (token
->type
== CPP_NAME
5696 && !cp_parser_nth_token_starts_template_argument_list_p
5698 return cp_parser_identifier (parser
);
5700 cp_parser_parse_tentatively (parser
);
5701 /* Try a template-id. */
5702 id
= cp_parser_template_id (parser
,
5703 /*template_keyword_p=*/false,
5704 /*check_dependency_p=*/true,
5707 /* If that worked, we're done. */
5708 if (cp_parser_parse_definitely (parser
))
5711 /* Peek at the next token. (Changes in the token buffer may
5712 have invalidated the pointer obtained above.) */
5713 token
= cp_lexer_peek_token (parser
->lexer
);
5715 switch (token
->type
)
5718 return cp_parser_identifier (parser
);
5721 if (token
->keyword
== RID_OPERATOR
)
5722 return cp_parser_operator_function_id (parser
);
5726 cp_parser_error (parser
, "expected id-expression");
5727 return error_mark_node
;
5731 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5732 /*check_dependency_p=*/true,
5737 /* Parse an unqualified-id.
5741 operator-function-id
5742 conversion-function-id
5746 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5747 keyword, in a construct like `A::template ...'.
5749 Returns a representation of unqualified-id. For the `identifier'
5750 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5751 production a BIT_NOT_EXPR is returned; the operand of the
5752 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5753 other productions, see the documentation accompanying the
5754 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5755 names are looked up in uninstantiated templates. If DECLARATOR_P
5756 is true, the unqualified-id is appearing as part of a declarator,
5757 rather than as part of an expression. */
5760 cp_parser_unqualified_id (cp_parser
* parser
,
5761 bool template_keyword_p
,
5762 bool check_dependency_p
,
5768 /* Peek at the next token. */
5769 token
= cp_lexer_peek_token (parser
->lexer
);
5771 switch ((int) token
->type
)
5777 /* We don't know yet whether or not this will be a
5779 cp_parser_parse_tentatively (parser
);
5780 /* Try a template-id. */
5781 id
= cp_parser_template_id (parser
, template_keyword_p
,
5785 /* If it worked, we're done. */
5786 if (cp_parser_parse_definitely (parser
))
5788 /* Otherwise, it's an ordinary identifier. */
5789 return cp_parser_identifier (parser
);
5792 case CPP_TEMPLATE_ID
:
5793 return cp_parser_template_id (parser
, template_keyword_p
,
5801 tree qualifying_scope
;
5806 /* Consume the `~' token. */
5807 cp_lexer_consume_token (parser
->lexer
);
5808 /* Parse the class-name. The standard, as written, seems to
5811 template <typename T> struct S { ~S (); };
5812 template <typename T> S<T>::~S() {}
5814 is invalid, since `~' must be followed by a class-name, but
5815 `S<T>' is dependent, and so not known to be a class.
5816 That's not right; we need to look in uninstantiated
5817 templates. A further complication arises from:
5819 template <typename T> void f(T t) {
5823 Here, it is not possible to look up `T' in the scope of `T'
5824 itself. We must look in both the current scope, and the
5825 scope of the containing complete expression.
5827 Yet another issue is:
5836 The standard does not seem to say that the `S' in `~S'
5837 should refer to the type `S' and not the data member
5840 /* DR 244 says that we look up the name after the "~" in the
5841 same scope as we looked up the qualifying name. That idea
5842 isn't fully worked out; it's more complicated than that. */
5843 scope
= parser
->scope
;
5844 object_scope
= parser
->object_scope
;
5845 qualifying_scope
= parser
->qualifying_scope
;
5847 /* Check for invalid scopes. */
5848 if (scope
== error_mark_node
)
5850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5851 cp_lexer_consume_token (parser
->lexer
);
5852 return error_mark_node
;
5854 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5857 error_at (token
->location
,
5858 "scope %qT before %<~%> is not a class-name",
5860 cp_parser_simulate_error (parser
);
5861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5862 cp_lexer_consume_token (parser
->lexer
);
5863 return error_mark_node
;
5865 gcc_assert (!scope
|| TYPE_P (scope
));
5867 /* If the name is of the form "X::~X" it's OK even if X is a
5869 token
= cp_lexer_peek_token (parser
->lexer
);
5871 && token
->type
== CPP_NAME
5872 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5874 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5875 || (CLASS_TYPE_P (scope
)
5876 && constructor_name_p (token
->u
.value
, scope
))))
5878 cp_lexer_consume_token (parser
->lexer
);
5879 return build_nt (BIT_NOT_EXPR
, scope
);
5882 /* ~auto means the destructor of whatever the object is. */
5883 if (cp_parser_is_keyword (token
, RID_AUTO
))
5885 if (cxx_dialect
< cxx14
)
5886 pedwarn (input_location
, 0,
5887 "%<~auto%> only available with "
5888 "-std=c++14 or -std=gnu++14");
5889 cp_lexer_consume_token (parser
->lexer
);
5890 return build_nt (BIT_NOT_EXPR
, make_auto ());
5893 /* If there was an explicit qualification (S::~T), first look
5894 in the scope given by the qualification (i.e., S).
5896 Note: in the calls to cp_parser_class_name below we pass
5897 typename_type so that lookup finds the injected-class-name
5898 rather than the constructor. */
5900 type_decl
= NULL_TREE
;
5903 cp_parser_parse_tentatively (parser
);
5904 type_decl
= cp_parser_class_name (parser
,
5905 /*typename_keyword_p=*/false,
5906 /*template_keyword_p=*/false,
5908 /*check_dependency=*/false,
5909 /*class_head_p=*/false,
5911 if (cp_parser_parse_definitely (parser
))
5914 /* In "N::S::~S", look in "N" as well. */
5915 if (!done
&& scope
&& qualifying_scope
)
5917 cp_parser_parse_tentatively (parser
);
5918 parser
->scope
= qualifying_scope
;
5919 parser
->object_scope
= NULL_TREE
;
5920 parser
->qualifying_scope
= NULL_TREE
;
5922 = cp_parser_class_name (parser
,
5923 /*typename_keyword_p=*/false,
5924 /*template_keyword_p=*/false,
5926 /*check_dependency=*/false,
5927 /*class_head_p=*/false,
5929 if (cp_parser_parse_definitely (parser
))
5932 /* In "p->S::~T", look in the scope given by "*p" as well. */
5933 else if (!done
&& object_scope
)
5935 cp_parser_parse_tentatively (parser
);
5936 parser
->scope
= object_scope
;
5937 parser
->object_scope
= NULL_TREE
;
5938 parser
->qualifying_scope
= NULL_TREE
;
5940 = cp_parser_class_name (parser
,
5941 /*typename_keyword_p=*/false,
5942 /*template_keyword_p=*/false,
5944 /*check_dependency=*/false,
5945 /*class_head_p=*/false,
5947 if (cp_parser_parse_definitely (parser
))
5950 /* Look in the surrounding context. */
5953 parser
->scope
= NULL_TREE
;
5954 parser
->object_scope
= NULL_TREE
;
5955 parser
->qualifying_scope
= NULL_TREE
;
5956 if (processing_template_decl
)
5957 cp_parser_parse_tentatively (parser
);
5959 = cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (processing_template_decl
5967 && ! cp_parser_parse_definitely (parser
))
5969 /* We couldn't find a type with this name. If we're parsing
5970 tentatively, fail and try something else. */
5971 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5973 cp_parser_simulate_error (parser
);
5974 return error_mark_node
;
5976 /* Otherwise, accept it and check for a match at instantiation
5978 type_decl
= cp_parser_identifier (parser
);
5979 if (type_decl
!= error_mark_node
)
5980 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5984 /* If an error occurred, assume that the name of the
5985 destructor is the same as the name of the qualifying
5986 class. That allows us to keep parsing after running
5987 into ill-formed destructor names. */
5988 if (type_decl
== error_mark_node
&& scope
)
5989 return build_nt (BIT_NOT_EXPR
, scope
);
5990 else if (type_decl
== error_mark_node
)
5991 return error_mark_node
;
5993 /* Check that destructor name and scope match. */
5994 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5996 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5997 error_at (token
->location
,
5998 "declaration of %<~%T%> as member of %qT",
6000 cp_parser_simulate_error (parser
);
6001 return error_mark_node
;
6006 A typedef-name that names a class shall not be used as the
6007 identifier in the declarator for a destructor declaration. */
6009 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6010 && !DECL_SELF_REFERENCE_P (type_decl
)
6011 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6012 error_at (token
->location
,
6013 "typedef-name %qD used as destructor declarator",
6016 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6020 if (token
->keyword
== RID_OPERATOR
)
6024 /* This could be a template-id, so we try that first. */
6025 cp_parser_parse_tentatively (parser
);
6026 /* Try a template-id. */
6027 id
= cp_parser_template_id (parser
, template_keyword_p
,
6028 /*check_dependency_p=*/true,
6031 /* If that worked, we're done. */
6032 if (cp_parser_parse_definitely (parser
))
6034 /* We still don't know whether we're looking at an
6035 operator-function-id or a conversion-function-id. */
6036 cp_parser_parse_tentatively (parser
);
6037 /* Try an operator-function-id. */
6038 id
= cp_parser_operator_function_id (parser
);
6039 /* If that didn't work, try a conversion-function-id. */
6040 if (!cp_parser_parse_definitely (parser
))
6041 id
= cp_parser_conversion_function_id (parser
);
6042 else if (UDLIT_OPER_P (id
))
6045 const char *name
= UDLIT_OP_SUFFIX (id
);
6046 if (name
[0] != '_' && !in_system_header_at (input_location
)
6048 warning (OPT_Wliteral_suffix
,
6049 "literal operator suffixes not preceded by %<_%>"
6050 " are reserved for future standardization");
6060 cp_parser_error (parser
, "expected unqualified-id");
6061 return error_mark_node
;
6065 /* Parse an (optional) nested-name-specifier.
6067 nested-name-specifier: [C++98]
6068 class-or-namespace-name :: nested-name-specifier [opt]
6069 class-or-namespace-name :: template nested-name-specifier [opt]
6071 nested-name-specifier: [C++0x]
6074 nested-name-specifier identifier ::
6075 nested-name-specifier template [opt] simple-template-id ::
6077 PARSER->SCOPE should be set appropriately before this function is
6078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6082 Sets PARSER->SCOPE to the class (TYPE) or namespace
6083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6084 it unchanged if there is no nested-name-specifier. Returns the new
6085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6088 part of a declaration and/or decl-specifier. */
6091 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6092 bool typename_keyword_p
,
6093 bool check_dependency_p
,
6095 bool is_declaration
,
6096 bool template_keyword_p
/* = false */)
6098 bool success
= false;
6099 cp_token_position start
= 0;
6102 /* Remember where the nested-name-specifier starts. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6105 start
= cp_lexer_token_position (parser
->lexer
, false);
6106 push_deferring_access_checks (dk_deferred
);
6113 tree saved_qualifying_scope
;
6115 /* Spot cases that cannot be the beginning of a
6116 nested-name-specifier. */
6117 token
= cp_lexer_peek_token (parser
->lexer
);
6119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6120 the already parsed nested-name-specifier. */
6121 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6123 /* Grab the nested-name-specifier and continue the loop. */
6124 cp_parser_pre_parsed_nested_name_specifier (parser
);
6125 /* If we originally encountered this nested-name-specifier
6126 with IS_DECLARATION set to false, we will not have
6127 resolved TYPENAME_TYPEs, so we must do so here. */
6129 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6131 new_scope
= resolve_typename_type (parser
->scope
,
6132 /*only_current_p=*/false);
6133 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6134 parser
->scope
= new_scope
;
6140 /* Spot cases that cannot be the beginning of a
6141 nested-name-specifier. On the second and subsequent times
6142 through the loop, we look for the `template' keyword. */
6143 if (success
&& token
->keyword
== RID_TEMPLATE
)
6145 /* A template-id can start a nested-name-specifier. */
6146 else if (token
->type
== CPP_TEMPLATE_ID
)
6148 /* DR 743: decltype can be used in a nested-name-specifier. */
6149 else if (token_is_decltype (token
))
6153 /* If the next token is not an identifier, then it is
6154 definitely not a type-name or namespace-name. */
6155 if (token
->type
!= CPP_NAME
)
6157 /* If the following token is neither a `<' (to begin a
6158 template-id), nor a `::', then we are not looking at a
6159 nested-name-specifier. */
6160 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6162 if (token
->type
== CPP_COLON
6163 && parser
->colon_corrects_to_scope_p
6164 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6166 gcc_rich_location
richloc (token
->location
);
6167 richloc
.add_fixit_replace ("::");
6169 "found %<:%> in nested-name-specifier, "
6171 token
->type
= CPP_SCOPE
;
6174 if (token
->type
!= CPP_SCOPE
6175 && !cp_parser_nth_token_starts_template_argument_list_p
6180 /* The nested-name-specifier is optional, so we parse
6182 cp_parser_parse_tentatively (parser
);
6184 /* Look for the optional `template' keyword, if this isn't the
6185 first time through the loop. */
6187 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6189 /* Save the old scope since the name lookup we are about to do
6190 might destroy it. */
6191 old_scope
= parser
->scope
;
6192 saved_qualifying_scope
= parser
->qualifying_scope
;
6193 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6194 look up names in "X<T>::I" in order to determine that "Y" is
6195 a template. So, if we have a typename at this point, we make
6196 an effort to look through it. */
6198 && !typename_keyword_p
6200 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6201 parser
->scope
= resolve_typename_type (parser
->scope
,
6202 /*only_current_p=*/false);
6203 /* Parse the qualifying entity. */
6205 = cp_parser_qualifying_entity (parser
,
6211 /* Look for the `::' token. */
6212 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6214 /* If we found what we wanted, we keep going; otherwise, we're
6216 if (!cp_parser_parse_definitely (parser
))
6218 bool error_p
= false;
6220 /* Restore the OLD_SCOPE since it was valid before the
6221 failed attempt at finding the last
6222 class-or-namespace-name. */
6223 parser
->scope
= old_scope
;
6224 parser
->qualifying_scope
= saved_qualifying_scope
;
6226 /* If the next token is a decltype, and the one after that is a
6227 `::', then the decltype has failed to resolve to a class or
6228 enumeration type. Give this error even when parsing
6229 tentatively since it can't possibly be valid--and we're going
6230 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6231 won't get another chance.*/
6232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6233 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6236 token
= cp_lexer_consume_token (parser
->lexer
);
6237 error_at (token
->location
, "decltype evaluates to %qT, "
6238 "which is not a class or enumeration type",
6239 token
->u
.tree_check_value
->value
);
6240 parser
->scope
= error_mark_node
;
6244 cp_lexer_consume_token (parser
->lexer
);
6247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6248 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6250 /* If we have a non-type template-id followed by ::, it can't
6251 possibly be valid. */
6252 token
= cp_lexer_peek_token (parser
->lexer
);
6253 tree tid
= token
->u
.tree_check_value
->value
;
6254 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6255 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6257 tree tmpl
= NULL_TREE
;
6258 if (is_overloaded_fn (tid
))
6260 tree fns
= get_fns (tid
);
6261 if (OVL_SINGLE_P (fns
))
6262 tmpl
= OVL_FIRST (fns
);
6263 error_at (token
->location
, "function template-id %qD "
6264 "in nested-name-specifier", tid
);
6268 /* Variable template. */
6269 tmpl
= TREE_OPERAND (tid
, 0);
6270 gcc_assert (variable_template_p (tmpl
));
6271 error_at (token
->location
, "variable template-id %qD "
6272 "in nested-name-specifier", tid
);
6275 inform (DECL_SOURCE_LOCATION (tmpl
),
6276 "%qD declared here", tmpl
);
6278 parser
->scope
= error_mark_node
;
6282 cp_lexer_consume_token (parser
->lexer
);
6283 cp_lexer_consume_token (parser
->lexer
);
6287 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6289 /* If the next token is an identifier, and the one after
6290 that is a `::', then any valid interpretation would have
6291 found a class-or-namespace-name. */
6292 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6293 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6295 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6298 token
= cp_lexer_consume_token (parser
->lexer
);
6301 if (!token
->error_reported
)
6304 tree ambiguous_decls
;
6306 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6308 /*is_template=*/false,
6309 /*is_namespace=*/false,
6310 /*check_dependency=*/true,
6313 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6314 error_at (token
->location
,
6315 "%qD used without template parameters",
6317 else if (ambiguous_decls
)
6319 // cp_parser_lookup_name has the same diagnostic,
6320 // thus make sure to emit it at most once.
6321 if (cp_parser_uncommitted_to_tentative_parse_p
6324 error_at (token
->location
,
6325 "reference to %qD is ambiguous",
6327 print_candidates (ambiguous_decls
);
6329 decl
= error_mark_node
;
6333 if (cxx_dialect
!= cxx98
)
6334 cp_parser_name_lookup_error
6335 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6338 cp_parser_name_lookup_error
6339 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6343 parser
->scope
= error_mark_node
;
6345 /* Treat this as a successful nested-name-specifier
6350 If the name found is not a class-name (clause
6351 _class_) or namespace-name (_namespace.def_), the
6352 program is ill-formed. */
6355 cp_lexer_consume_token (parser
->lexer
);
6359 /* We've found one valid nested-name-specifier. */
6361 /* Name lookup always gives us a DECL. */
6362 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6363 new_scope
= TREE_TYPE (new_scope
);
6364 /* Uses of "template" must be followed by actual templates. */
6365 if (template_keyword_p
6366 && !(CLASS_TYPE_P (new_scope
)
6367 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6368 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6369 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6370 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6371 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6372 == TEMPLATE_ID_EXPR
)))
6373 permerror (input_location
, TYPE_P (new_scope
)
6374 ? G_("%qT is not a template")
6375 : G_("%qD is not a template"),
6377 /* If it is a class scope, try to complete it; we are about to
6378 be looking up names inside the class. */
6379 if (TYPE_P (new_scope
)
6380 /* Since checking types for dependency can be expensive,
6381 avoid doing it if the type is already complete. */
6382 && !COMPLETE_TYPE_P (new_scope
)
6383 /* Do not try to complete dependent types. */
6384 && !dependent_type_p (new_scope
))
6386 new_scope
= complete_type (new_scope
);
6387 /* If it is a typedef to current class, use the current
6388 class instead, as the typedef won't have any names inside
6390 if (!COMPLETE_TYPE_P (new_scope
)
6391 && currently_open_class (new_scope
))
6392 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6394 /* Make sure we look in the right scope the next time through
6396 parser
->scope
= new_scope
;
6399 /* If parsing tentatively, replace the sequence of tokens that makes
6400 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6401 token. That way, should we re-parse the token stream, we will
6402 not have to repeat the effort required to do the parse, nor will
6403 we issue duplicate error messages. */
6404 if (success
&& start
)
6408 token
= cp_lexer_token_at (parser
->lexer
, start
);
6409 /* Reset the contents of the START token. */
6410 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6411 /* Retrieve any deferred checks. Do not pop this access checks yet
6412 so the memory will not be reclaimed during token replacing below. */
6413 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6414 token
->u
.tree_check_value
->value
= parser
->scope
;
6415 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6416 token
->u
.tree_check_value
->qualifying_scope
=
6417 parser
->qualifying_scope
;
6418 token
->keyword
= RID_MAX
;
6420 /* Purge all subsequent tokens. */
6421 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6425 pop_to_parent_deferring_access_checks ();
6427 return success
? parser
->scope
: NULL_TREE
;
6430 /* Parse a nested-name-specifier. See
6431 cp_parser_nested_name_specifier_opt for details. This function
6432 behaves identically, except that it will an issue an error if no
6433 nested-name-specifier is present. */
6436 cp_parser_nested_name_specifier (cp_parser
*parser
,
6437 bool typename_keyword_p
,
6438 bool check_dependency_p
,
6440 bool is_declaration
)
6444 /* Look for the nested-name-specifier. */
6445 scope
= cp_parser_nested_name_specifier_opt (parser
,
6450 /* If it was not present, issue an error message. */
6453 cp_parser_error (parser
, "expected nested-name-specifier");
6454 parser
->scope
= NULL_TREE
;
6460 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6461 this is either a class-name or a namespace-name (which corresponds
6462 to the class-or-namespace-name production in the grammar). For
6463 C++0x, it can also be a type-name that refers to an enumeration
6464 type or a simple-template-id.
6466 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6467 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6468 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6469 TYPE_P is TRUE iff the next name should be taken as a class-name,
6470 even the same name is declared to be another entity in the same
6473 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6474 specified by the class-or-namespace-name. If neither is found the
6475 ERROR_MARK_NODE is returned. */
6478 cp_parser_qualifying_entity (cp_parser
*parser
,
6479 bool typename_keyword_p
,
6480 bool template_keyword_p
,
6481 bool check_dependency_p
,
6483 bool is_declaration
)
6486 tree saved_qualifying_scope
;
6487 tree saved_object_scope
;
6490 bool successful_parse_p
;
6492 /* DR 743: decltype can appear in a nested-name-specifier. */
6493 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6495 scope
= cp_parser_decltype (parser
);
6496 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6497 && !MAYBE_CLASS_TYPE_P (scope
))
6499 cp_parser_simulate_error (parser
);
6500 return error_mark_node
;
6502 if (TYPE_NAME (scope
))
6503 scope
= TYPE_NAME (scope
);
6507 /* Before we try to parse the class-name, we must save away the
6508 current PARSER->SCOPE since cp_parser_class_name will destroy
6510 saved_scope
= parser
->scope
;
6511 saved_qualifying_scope
= parser
->qualifying_scope
;
6512 saved_object_scope
= parser
->object_scope
;
6513 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6514 there is no need to look for a namespace-name. */
6515 only_class_p
= template_keyword_p
6516 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6518 cp_parser_parse_tentatively (parser
);
6519 scope
= cp_parser_class_name (parser
,
6522 type_p
? class_type
: none_type
,
6524 /*class_head_p=*/false,
6526 /*enum_ok=*/cxx_dialect
> cxx98
);
6527 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6528 /* If that didn't work, try for a namespace-name. */
6529 if (!only_class_p
&& !successful_parse_p
)
6531 /* Restore the saved scope. */
6532 parser
->scope
= saved_scope
;
6533 parser
->qualifying_scope
= saved_qualifying_scope
;
6534 parser
->object_scope
= saved_object_scope
;
6535 /* If we are not looking at an identifier followed by the scope
6536 resolution operator, then this is not part of a
6537 nested-name-specifier. (Note that this function is only used
6538 to parse the components of a nested-name-specifier.) */
6539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6540 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6541 return error_mark_node
;
6542 scope
= cp_parser_namespace_name (parser
);
6548 /* Return true if we are looking at a compound-literal, false otherwise. */
6551 cp_parser_compound_literal_p (cp_parser
*parser
)
6553 cp_lexer_save_tokens (parser
->lexer
);
6555 /* Skip tokens until the next token is a closing parenthesis.
6556 If we find the closing `)', and the next token is a `{', then
6557 we are looking at a compound-literal. */
6558 bool compound_literal_p
6559 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6560 /*consume_paren=*/true)
6561 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6563 /* Roll back the tokens we skipped. */
6564 cp_lexer_rollback_tokens (parser
->lexer
);
6566 return compound_literal_p
;
6569 /* Parse a postfix-expression.
6573 postfix-expression [ expression ]
6574 postfix-expression ( expression-list [opt] )
6575 simple-type-specifier ( expression-list [opt] )
6576 typename :: [opt] nested-name-specifier identifier
6577 ( expression-list [opt] )
6578 typename :: [opt] nested-name-specifier template [opt] template-id
6579 ( expression-list [opt] )
6580 postfix-expression . template [opt] id-expression
6581 postfix-expression -> template [opt] id-expression
6582 postfix-expression . pseudo-destructor-name
6583 postfix-expression -> pseudo-destructor-name
6584 postfix-expression ++
6585 postfix-expression --
6586 dynamic_cast < type-id > ( expression )
6587 static_cast < type-id > ( expression )
6588 reinterpret_cast < type-id > ( expression )
6589 const_cast < type-id > ( expression )
6590 typeid ( expression )
6596 ( type-id ) { initializer-list , [opt] }
6598 This extension is a GNU version of the C99 compound-literal
6599 construct. (The C99 grammar uses `type-name' instead of `type-id',
6600 but they are essentially the same concept.)
6602 If ADDRESS_P is true, the postfix expression is the operand of the
6603 `&' operator. CAST_P is true if this expression is the target of a
6606 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6607 class member access expressions [expr.ref].
6609 Returns a representation of the expression. */
6612 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6613 bool member_access_only_p
, bool decltype_p
,
6614 cp_id_kind
* pidk_return
)
6619 cp_id_kind idk
= CP_ID_KIND_NONE
;
6620 cp_expr postfix_expression
= NULL_TREE
;
6621 bool is_member_access
= false;
6622 int saved_in_statement
= -1;
6624 /* Peek at the next token. */
6625 token
= cp_lexer_peek_token (parser
->lexer
);
6626 loc
= token
->location
;
6627 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6629 /* Some of the productions are determined by keywords. */
6630 keyword
= token
->keyword
;
6640 const char *saved_message
;
6641 bool saved_in_type_id_in_expr_p
;
6643 /* All of these can be handled in the same way from the point
6644 of view of parsing. Begin by consuming the token
6645 identifying the cast. */
6646 cp_lexer_consume_token (parser
->lexer
);
6648 /* New types cannot be defined in the cast. */
6649 saved_message
= parser
->type_definition_forbidden_message
;
6650 parser
->type_definition_forbidden_message
6651 = G_("types may not be defined in casts");
6653 /* Look for the opening `<'. */
6654 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6655 /* Parse the type to which we are casting. */
6656 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6657 parser
->in_type_id_in_expr_p
= true;
6658 type
= cp_parser_type_id (parser
);
6659 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6660 /* Look for the closing `>'. */
6661 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6662 /* Restore the old message. */
6663 parser
->type_definition_forbidden_message
= saved_message
;
6665 bool saved_greater_than_is_operator_p
6666 = parser
->greater_than_is_operator_p
;
6667 parser
->greater_than_is_operator_p
= true;
6669 /* And the expression which is being cast. */
6670 matching_parens parens
;
6671 parens
.require_open (parser
);
6672 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6673 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6675 location_t end_loc
= close_paren
?
6676 close_paren
->location
: UNKNOWN_LOCATION
;
6678 parser
->greater_than_is_operator_p
6679 = saved_greater_than_is_operator_p
;
6681 /* Only type conversions to integral or enumeration types
6682 can be used in constant-expressions. */
6683 if (!cast_valid_in_integral_constant_expression_p (type
)
6684 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6686 postfix_expression
= error_mark_node
;
6694 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6698 = build_static_cast (type
, expression
, tf_warning_or_error
);
6702 = build_reinterpret_cast (type
, expression
,
6703 tf_warning_or_error
);
6707 = build_const_cast (type
, expression
, tf_warning_or_error
);
6713 /* Construct a location e.g. :
6714 reinterpret_cast <int *> (expr)
6715 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6716 ranging from the start of the "*_cast" token to the final closing
6717 paren, with the caret at the start. */
6718 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6719 postfix_expression
.set_location (cp_cast_loc
);
6726 const char *saved_message
;
6727 bool saved_in_type_id_in_expr_p
;
6729 /* Consume the `typeid' token. */
6730 cp_lexer_consume_token (parser
->lexer
);
6731 /* Look for the `(' token. */
6732 matching_parens parens
;
6733 parens
.require_open (parser
);
6734 /* Types cannot be defined in a `typeid' expression. */
6735 saved_message
= parser
->type_definition_forbidden_message
;
6736 parser
->type_definition_forbidden_message
6737 = G_("types may not be defined in a %<typeid%> expression");
6738 /* We can't be sure yet whether we're looking at a type-id or an
6740 cp_parser_parse_tentatively (parser
);
6741 /* Try a type-id first. */
6742 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6743 parser
->in_type_id_in_expr_p
= true;
6744 type
= cp_parser_type_id (parser
);
6745 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6746 /* Look for the `)' token. Otherwise, we can't be sure that
6747 we're not looking at an expression: consider `typeid (int
6748 (3))', for example. */
6749 cp_token
*close_paren
= parens
.require_close (parser
);
6750 /* If all went well, simply lookup the type-id. */
6751 if (cp_parser_parse_definitely (parser
))
6752 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6753 /* Otherwise, fall back to the expression variant. */
6758 /* Look for an expression. */
6759 expression
= cp_parser_expression (parser
, & idk
);
6760 /* Compute its typeid. */
6761 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6762 /* Look for the `)' token. */
6763 close_paren
= parens
.require_close (parser
);
6765 /* Restore the saved message. */
6766 parser
->type_definition_forbidden_message
= saved_message
;
6767 /* `typeid' may not appear in an integral constant expression. */
6768 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6769 postfix_expression
= error_mark_node
;
6771 /* Construct a location e.g. :
6774 ranging from the start of the "typeid" token to the final closing
6775 paren, with the caret at the start. */
6778 location_t typeid_loc
6779 = make_location (start_loc
, start_loc
, close_paren
->location
);
6780 postfix_expression
.set_location (typeid_loc
);
6788 /* The syntax permitted here is the same permitted for an
6789 elaborated-type-specifier. */
6790 ++parser
->prevent_constrained_type_specifiers
;
6791 type
= cp_parser_elaborated_type_specifier (parser
,
6792 /*is_friend=*/false,
6793 /*is_declaration=*/false);
6794 --parser
->prevent_constrained_type_specifiers
;
6795 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6799 case RID_CILK_SPAWN
:
6801 location_t cilk_spawn_loc
6802 = cp_lexer_peek_token (parser
->lexer
)->location
;
6803 cp_lexer_consume_token (parser
->lexer
);
6804 token
= cp_lexer_peek_token (parser
->lexer
);
6805 if (token
->type
== CPP_SEMICOLON
)
6807 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6809 postfix_expression
= error_mark_node
;
6812 else if (!current_function_decl
)
6814 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6815 "inside a function");
6816 postfix_expression
= error_mark_node
;
6821 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6822 saved_in_statement
= parser
->in_statement
;
6823 parser
->in_statement
|= IN_CILK_SPAWN
;
6825 cfun
->calls_cilk_spawn
= 1;
6826 postfix_expression
=
6827 cp_parser_postfix_expression (parser
, false, false,
6828 false, false, &idk
);
6831 error_at (token
->location
, "-fcilkplus must be enabled to use"
6832 " %<_Cilk_spawn%>");
6833 cfun
->calls_cilk_spawn
= 0;
6835 else if (saved_in_statement
& IN_CILK_SPAWN
)
6837 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6838 "are not permitted");
6839 postfix_expression
= error_mark_node
;
6840 cfun
->calls_cilk_spawn
= 0;
6844 location_t loc
= postfix_expression
.get_location ();
6845 postfix_expression
= build_cilk_spawn (token
->location
,
6846 postfix_expression
);
6847 /* Build a location of the form:
6850 with caret at the expr, ranging from the start of the
6851 _Cilk_spawn token to the end of the expression. */
6852 location_t combined_loc
=
6853 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6854 postfix_expression
.set_location (combined_loc
);
6855 if (postfix_expression
!= error_mark_node
)
6856 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6857 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6863 case RID_BUILTIN_SHUFFLE
:
6864 case RID_BUILTIN_LAUNDER
:
6866 vec
<tree
, va_gc
> *vec
;
6870 cp_lexer_consume_token (parser
->lexer
);
6871 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6872 /*cast_p=*/false, /*allow_expansion_p=*/true,
6873 /*non_constant_p=*/NULL
);
6876 postfix_expression
= error_mark_node
;
6880 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6886 if (vec
->length () == 1)
6888 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6891 error_at (loc
, "wrong number of arguments to "
6892 "%<__builtin_addressof%>");
6893 postfix_expression
= error_mark_node
;
6897 case RID_BUILTIN_LAUNDER
:
6898 if (vec
->length () == 1)
6899 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6900 tf_warning_or_error
);
6903 error_at (loc
, "wrong number of arguments to "
6904 "%<__builtin_launder%>");
6905 postfix_expression
= error_mark_node
;
6909 case RID_BUILTIN_SHUFFLE
:
6910 if (vec
->length () == 2)
6912 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6913 (*vec
)[1], tf_warning_or_error
);
6914 else if (vec
->length () == 3)
6916 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6917 (*vec
)[2], tf_warning_or_error
);
6920 error_at (loc
, "wrong number of arguments to "
6921 "%<__builtin_shuffle%>");
6922 postfix_expression
= error_mark_node
;
6936 /* If the next thing is a simple-type-specifier, we may be
6937 looking at a functional cast. We could also be looking at
6938 an id-expression. So, we try the functional cast, and if
6939 that doesn't work we fall back to the primary-expression. */
6940 cp_parser_parse_tentatively (parser
);
6941 /* Look for the simple-type-specifier. */
6942 ++parser
->prevent_constrained_type_specifiers
;
6943 type
= cp_parser_simple_type_specifier (parser
,
6944 /*decl_specs=*/NULL
,
6945 CP_PARSER_FLAGS_NONE
);
6946 --parser
->prevent_constrained_type_specifiers
;
6947 /* Parse the cast itself. */
6948 if (!cp_parser_error_occurred (parser
))
6950 = cp_parser_functional_cast (parser
, type
);
6951 /* If that worked, we're done. */
6952 if (cp_parser_parse_definitely (parser
))
6955 /* If the functional-cast didn't work out, try a
6956 compound-literal. */
6957 if (cp_parser_allow_gnu_extensions_p (parser
)
6958 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6960 cp_expr initializer
= NULL_TREE
;
6962 cp_parser_parse_tentatively (parser
);
6964 matching_parens parens
;
6965 parens
.consume_open (parser
);
6967 /* Avoid calling cp_parser_type_id pointlessly, see comment
6968 in cp_parser_cast_expression about c++/29234. */
6969 if (!cp_parser_compound_literal_p (parser
))
6970 cp_parser_simulate_error (parser
);
6973 /* Parse the type. */
6974 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6975 parser
->in_type_id_in_expr_p
= true;
6976 type
= cp_parser_type_id (parser
);
6977 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6978 parens
.require_close (parser
);
6981 /* If things aren't going well, there's no need to
6983 if (!cp_parser_error_occurred (parser
))
6985 bool non_constant_p
;
6986 /* Parse the brace-enclosed initializer list. */
6987 initializer
= cp_parser_braced_list (parser
,
6990 /* If that worked, we're definitely looking at a
6991 compound-literal expression. */
6992 if (cp_parser_parse_definitely (parser
))
6994 /* Warn the user that a compound literal is not
6995 allowed in standard C++. */
6996 pedwarn (input_location
, OPT_Wpedantic
,
6997 "ISO C++ forbids compound-literals");
6998 /* For simplicity, we disallow compound literals in
6999 constant-expressions. We could
7000 allow compound literals of integer type, whose
7001 initializer was a constant, in constant
7002 expressions. Permitting that usage, as a further
7003 extension, would not change the meaning of any
7004 currently accepted programs. (Of course, as
7005 compound literals are not part of ISO C++, the
7006 standard has nothing to say.) */
7007 if (cp_parser_non_integral_constant_expression (parser
,
7010 postfix_expression
= error_mark_node
;
7013 /* Form the representation of the compound-literal. */
7015 = finish_compound_literal (type
, initializer
,
7016 tf_warning_or_error
, fcl_c99
);
7017 postfix_expression
.set_location (initializer
.get_location ());
7022 /* It must be a primary-expression. */
7024 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7025 /*template_arg_p=*/false,
7032 /* Note that we don't need to worry about calling build_cplus_new on a
7033 class-valued CALL_EXPR in decltype when it isn't the end of the
7034 postfix-expression; unary_complex_lvalue will take care of that for
7037 /* Keep looping until the postfix-expression is complete. */
7040 if (idk
== CP_ID_KIND_UNQUALIFIED
7041 && identifier_p (postfix_expression
)
7042 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7043 /* It is not a Koenig lookup function call. */
7045 = unqualified_name_lookup_error (postfix_expression
);
7047 /* Peek at the next token. */
7048 token
= cp_lexer_peek_token (parser
->lexer
);
7050 switch (token
->type
)
7052 case CPP_OPEN_SQUARE
:
7053 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7055 cp_parser_error (parser
,
7056 "two consecutive %<[%> shall "
7057 "only introduce an attribute");
7058 return error_mark_node
;
7061 = cp_parser_postfix_open_square_expression (parser
,
7065 postfix_expression
.set_range (start_loc
,
7066 postfix_expression
.get_location ());
7068 idk
= CP_ID_KIND_NONE
;
7069 is_member_access
= false;
7072 case CPP_OPEN_PAREN
:
7073 /* postfix-expression ( expression-list [opt] ) */
7076 bool is_builtin_constant_p
;
7077 bool saved_integral_constant_expression_p
= false;
7078 bool saved_non_integral_constant_expression_p
= false;
7079 tsubst_flags_t complain
= complain_flags (decltype_p
);
7080 vec
<tree
, va_gc
> *args
;
7081 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7083 is_member_access
= false;
7085 is_builtin_constant_p
7086 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7087 if (is_builtin_constant_p
)
7089 /* The whole point of __builtin_constant_p is to allow
7090 non-constant expressions to appear as arguments. */
7091 saved_integral_constant_expression_p
7092 = parser
->integral_constant_expression_p
;
7093 saved_non_integral_constant_expression_p
7094 = parser
->non_integral_constant_expression_p
;
7095 parser
->integral_constant_expression_p
= false;
7097 args
= (cp_parser_parenthesized_expression_list
7099 /*cast_p=*/false, /*allow_expansion_p=*/true,
7100 /*non_constant_p=*/NULL
,
7101 /*close_paren_loc=*/&close_paren_loc
));
7102 if (is_builtin_constant_p
)
7104 parser
->integral_constant_expression_p
7105 = saved_integral_constant_expression_p
;
7106 parser
->non_integral_constant_expression_p
7107 = saved_non_integral_constant_expression_p
;
7112 postfix_expression
= error_mark_node
;
7116 /* Function calls are not permitted in
7117 constant-expressions. */
7118 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7119 && cp_parser_non_integral_constant_expression (parser
,
7122 postfix_expression
= error_mark_node
;
7123 release_tree_vector (args
);
7128 if (idk
== CP_ID_KIND_UNQUALIFIED
7129 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7131 if (identifier_p (postfix_expression
))
7133 if (!args
->is_empty ())
7136 if (!any_type_dependent_arguments_p (args
))
7138 = perform_koenig_lookup (postfix_expression
, args
,
7143 = unqualified_fn_lookup_error (postfix_expression
);
7145 /* We do not perform argument-dependent lookup if
7146 normal lookup finds a non-function, in accordance
7147 with the expected resolution of DR 218. */
7148 else if (!args
->is_empty ()
7149 && is_overloaded_fn (postfix_expression
))
7151 tree fn
= get_first_fn (postfix_expression
);
7152 fn
= STRIP_TEMPLATE (fn
);
7154 /* Do not do argument dependent lookup if regular
7155 lookup finds a member function or a block-scope
7156 function declaration. [basic.lookup.argdep]/3 */
7157 if (!DECL_FUNCTION_MEMBER_P (fn
)
7158 && !DECL_LOCAL_FUNCTION_P (fn
))
7161 if (!any_type_dependent_arguments_p (args
))
7163 = perform_koenig_lookup (postfix_expression
, args
,
7169 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7170 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7171 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7172 && vec_safe_length (args
) == 3)
7174 tree arg0
= (*args
)[0];
7175 tree arg1
= (*args
)[1];
7176 tree arg2
= (*args
)[2];
7177 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
7178 | (!!integer_zerop (arg2
) << 2));
7179 if (TREE_CODE (arg2
) == CONST_DECL
)
7180 arg2
= DECL_INITIAL (arg2
);
7181 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7184 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7186 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7187 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7189 if (processing_template_decl
7190 && (type_dependent_object_expression_p (instance
)
7191 || (!BASELINK_P (fn
)
7192 && TREE_CODE (fn
) != FIELD_DECL
)
7193 || type_dependent_expression_p (fn
)
7194 || any_type_dependent_arguments_p (args
)))
7196 maybe_generic_this_capture (instance
, fn
);
7198 = build_min_nt_call_vec (postfix_expression
, args
);
7199 release_tree_vector (args
);
7203 if (BASELINK_P (fn
))
7206 = (build_new_method_call
7207 (instance
, fn
, &args
, NULL_TREE
,
7208 (idk
== CP_ID_KIND_QUALIFIED
7209 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7216 = finish_call_expr (postfix_expression
, &args
,
7217 /*disallow_virtual=*/false,
7221 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7222 || TREE_CODE (postfix_expression
) == MEMBER_REF
7223 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7224 postfix_expression
= (build_offset_ref_call_from_tree
7225 (postfix_expression
, &args
,
7227 else if (idk
== CP_ID_KIND_QUALIFIED
)
7228 /* A call to a static class member, or a namespace-scope
7231 = finish_call_expr (postfix_expression
, &args
,
7232 /*disallow_virtual=*/true,
7236 /* All other function calls. */
7238 = finish_call_expr (postfix_expression
, &args
,
7239 /*disallow_virtual=*/false,
7243 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7245 location_t combined_loc
= make_location (token
->location
,
7248 postfix_expression
.set_location (combined_loc
);
7251 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7252 idk
= CP_ID_KIND_NONE
;
7254 release_tree_vector (args
);
7260 /* postfix-expression . template [opt] id-expression
7261 postfix-expression . pseudo-destructor-name
7262 postfix-expression -> template [opt] id-expression
7263 postfix-expression -> pseudo-destructor-name */
7265 /* Consume the `.' or `->' operator. */
7266 cp_lexer_consume_token (parser
->lexer
);
7269 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7273 is_member_access
= true;
7277 /* postfix-expression ++ */
7278 /* Consume the `++' token. */
7279 cp_lexer_consume_token (parser
->lexer
);
7280 /* Generate a representation for the complete expression. */
7282 = finish_increment_expr (postfix_expression
,
7283 POSTINCREMENT_EXPR
);
7284 /* Increments may not appear in constant-expressions. */
7285 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7286 postfix_expression
= error_mark_node
;
7287 idk
= CP_ID_KIND_NONE
;
7288 is_member_access
= false;
7291 case CPP_MINUS_MINUS
:
7292 /* postfix-expression -- */
7293 /* Consume the `--' token. */
7294 cp_lexer_consume_token (parser
->lexer
);
7295 /* Generate a representation for the complete expression. */
7297 = finish_increment_expr (postfix_expression
,
7298 POSTDECREMENT_EXPR
);
7299 /* Decrements may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7301 postfix_expression
= error_mark_node
;
7302 idk
= CP_ID_KIND_NONE
;
7303 is_member_access
= false;
7307 if (pidk_return
!= NULL
)
7308 * pidk_return
= idk
;
7309 if (member_access_only_p
)
7310 return is_member_access
7311 ? postfix_expression
7312 : cp_expr (error_mark_node
);
7314 return postfix_expression
;
7318 /* We should never get here. */
7320 return error_mark_node
;
7323 /* This function parses Cilk Plus array notations. If a normal array expr. is
7324 parsed then the array index is passed back to the caller through *INIT_INDEX
7325 and the function returns a NULL_TREE. If array notation expr. is parsed,
7326 then *INIT_INDEX is ignored by the caller and the function returns
7327 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7331 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
7334 cp_token
*token
= NULL
;
7335 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
7336 if (!array_value
|| array_value
== error_mark_node
)
7338 cp_parser_skip_to_end_of_statement (parser
);
7339 return error_mark_node
;
7342 array_type
= TREE_TYPE (array_value
);
7344 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7345 parser
->colon_corrects_to_scope_p
= false;
7346 token
= cp_lexer_peek_token (parser
->lexer
);
7350 cp_parser_error (parser
, "expected %<:%> or numeral");
7351 return error_mark_node
;
7353 else if (token
->type
== CPP_COLON
)
7355 /* Consume the ':'. */
7356 cp_lexer_consume_token (parser
->lexer
);
7358 /* If we are here, then we have a case like this A[:]. */
7359 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7361 cp_parser_error (parser
, "expected %<]%>");
7362 cp_parser_skip_to_end_of_statement (parser
);
7363 return error_mark_node
;
7365 *init_index
= NULL_TREE
;
7367 length_index
= NULL_TREE
;
7371 /* If we are here, then there are three valid possibilities:
7373 2. ARRAY [ EXP : EXP ]
7374 3. ARRAY [ EXP : EXP : EXP ] */
7376 *init_index
= cp_parser_expression (parser
);
7377 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7379 /* This indicates that we have a normal array expression. */
7380 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7384 /* Consume the ':'. */
7385 cp_lexer_consume_token (parser
->lexer
);
7386 length_index
= cp_parser_expression (parser
);
7387 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7389 cp_lexer_consume_token (parser
->lexer
);
7390 stride
= cp_parser_expression (parser
);
7393 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7395 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7396 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7398 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7399 cp_lexer_consume_token (parser
->lexer
);
7400 return error_mark_node
;
7402 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7404 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7405 length_index
, stride
, array_type
);
7409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7410 by cp_parser_builtin_offsetof. We're looking for
7412 postfix-expression [ expression ]
7413 postfix-expression [ braced-init-list ] (C++11)
7415 FOR_OFFSETOF is set if we're being called in that context, which
7416 changes how we deal with integer constant expressions. */
7419 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7420 tree postfix_expression
,
7424 tree index
= NULL_TREE
;
7425 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7426 bool saved_greater_than_is_operator_p
;
7428 /* Consume the `[' token. */
7429 cp_lexer_consume_token (parser
->lexer
);
7431 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7432 parser
->greater_than_is_operator_p
= true;
7434 /* Parse the index expression. */
7435 /* ??? For offsetof, there is a question of what to allow here. If
7436 offsetof is not being used in an integral constant expression context,
7437 then we *could* get the right answer by computing the value at runtime.
7438 If we are in an integral constant expression context, then we might
7439 could accept any constant expression; hard to say without analysis.
7440 Rather than open the barn door too wide right away, allow only integer
7441 constant expressions here. */
7443 index
= cp_parser_constant_expression (parser
);
7446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7448 bool expr_nonconst_p
;
7449 cp_lexer_set_source_position (parser
->lexer
);
7450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7451 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7453 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7455 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7456 "braced list index is not allowed with array "
7458 cp_parser_skip_to_end_of_statement (parser
);
7459 return error_mark_node
;
7462 else if (flag_cilkplus
)
7464 /* Here are have these two options:
7465 ARRAY[EXP : EXP] - Array notation expr with default
7467 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7469 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7470 postfix_expression
);
7475 index
= cp_parser_expression (parser
);
7478 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7480 /* Look for the closing `]'. */
7481 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7483 /* Build the ARRAY_REF. */
7484 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7487 /* When not doing offsetof, array references are not permitted in
7488 constant-expressions. */
7490 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7491 postfix_expression
= error_mark_node
;
7493 return postfix_expression
;
7496 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7497 by cp_parser_builtin_offsetof. We're looking for
7499 postfix-expression . template [opt] id-expression
7500 postfix-expression . pseudo-destructor-name
7501 postfix-expression -> template [opt] id-expression
7502 postfix-expression -> pseudo-destructor-name
7504 FOR_OFFSETOF is set if we're being called in that context. That sorta
7505 limits what of the above we'll actually accept, but nevermind.
7506 TOKEN_TYPE is the "." or "->" token, which will already have been
7507 removed from the stream. */
7510 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7511 enum cpp_ttype token_type
,
7512 cp_expr postfix_expression
,
7513 bool for_offsetof
, cp_id_kind
*idk
,
7514 location_t location
)
7518 bool pseudo_destructor_p
;
7519 tree scope
= NULL_TREE
;
7520 location_t start_loc
= postfix_expression
.get_start ();
7522 /* If this is a `->' operator, dereference the pointer. */
7523 if (token_type
== CPP_DEREF
)
7524 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7525 tf_warning_or_error
);
7526 /* Check to see whether or not the expression is type-dependent and
7527 not the current instantiation. */
7528 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7529 /* The identifier following the `->' or `.' is not qualified. */
7530 parser
->scope
= NULL_TREE
;
7531 parser
->qualifying_scope
= NULL_TREE
;
7532 parser
->object_scope
= NULL_TREE
;
7533 *idk
= CP_ID_KIND_NONE
;
7535 /* Enter the scope corresponding to the type of the object
7536 given by the POSTFIX_EXPRESSION. */
7539 scope
= TREE_TYPE (postfix_expression
);
7540 /* According to the standard, no expression should ever have
7541 reference type. Unfortunately, we do not currently match
7542 the standard in this respect in that our internal representation
7543 of an expression may have reference type even when the standard
7544 says it does not. Therefore, we have to manually obtain the
7545 underlying type here. */
7546 scope
= non_reference (scope
);
7547 /* The type of the POSTFIX_EXPRESSION must be complete. */
7548 /* Unlike the object expression in other contexts, *this is not
7549 required to be of complete type for purposes of class member
7550 access (5.2.5) outside the member function body. */
7551 if (postfix_expression
!= current_class_ref
7552 && scope
!= error_mark_node
7553 && !(processing_template_decl
7554 && current_class_type
7555 && (same_type_ignoring_top_level_qualifiers_p
7556 (scope
, current_class_type
))))
7558 scope
= complete_type (scope
);
7559 if (!COMPLETE_TYPE_P (scope
)
7560 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7561 && EXPR_P (postfix_expression
))
7563 /* In a template, be permissive by treating an object expression
7564 of incomplete type as dependent (after a pedwarn). */
7565 diagnostic_t kind
= (processing_template_decl
7566 && MAYBE_CLASS_TYPE_P (scope
)
7569 cxx_incomplete_type_diagnostic
7570 (location_of (postfix_expression
),
7571 postfix_expression
, scope
, kind
);
7572 if (!MAYBE_CLASS_TYPE_P (scope
))
7573 return error_mark_node
;
7574 if (processing_template_decl
)
7577 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7584 /* Let the name lookup machinery know that we are processing a
7585 class member access expression. */
7586 parser
->context
->object_type
= scope
;
7587 /* If something went wrong, we want to be able to discern that case,
7588 as opposed to the case where there was no SCOPE due to the type
7589 of expression being dependent. */
7591 scope
= error_mark_node
;
7592 /* If the SCOPE was erroneous, make the various semantic analysis
7593 functions exit quickly -- and without issuing additional error
7595 if (scope
== error_mark_node
)
7596 postfix_expression
= error_mark_node
;
7601 /* Tell cp_parser_lookup_name that there was an object, even though it's
7603 parser
->context
->object_type
= unknown_type_node
;
7605 /* Assume this expression is not a pseudo-destructor access. */
7606 pseudo_destructor_p
= false;
7608 /* If the SCOPE is a scalar type, then, if this is a valid program,
7609 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7610 is type dependent, it can be pseudo-destructor-name or something else.
7611 Try to parse it as pseudo-destructor-name first. */
7612 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7617 cp_parser_parse_tentatively (parser
);
7618 /* Parse the pseudo-destructor-name. */
7620 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7623 && (cp_parser_error_occurred (parser
)
7624 || !SCALAR_TYPE_P (type
)))
7625 cp_parser_abort_tentative_parse (parser
);
7626 else if (cp_parser_parse_definitely (parser
))
7628 pseudo_destructor_p
= true;
7630 = finish_pseudo_destructor_expr (postfix_expression
,
7635 if (!pseudo_destructor_p
)
7637 /* If the SCOPE is not a scalar type, we are looking at an
7638 ordinary class member access expression, rather than a
7639 pseudo-destructor-name. */
7641 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7642 /* Parse the id-expression. */
7643 name
= (cp_parser_id_expression
7645 cp_parser_optional_template_keyword (parser
),
7646 /*check_dependency_p=*/true,
7648 /*declarator_p=*/false,
7649 /*optional_p=*/false));
7650 /* In general, build a SCOPE_REF if the member name is qualified.
7651 However, if the name was not dependent and has already been
7652 resolved; there is no need to build the SCOPE_REF. For example;
7654 struct X { void f(); };
7655 template <typename T> void f(T* t) { t->X::f(); }
7657 Even though "t" is dependent, "X::f" is not and has been resolved
7658 to a BASELINK; there is no need to include scope information. */
7660 /* But we do need to remember that there was an explicit scope for
7661 virtual function calls. */
7663 *idk
= CP_ID_KIND_QUALIFIED
;
7665 /* If the name is a template-id that names a type, we will get a
7666 TYPE_DECL here. That is invalid code. */
7667 if (TREE_CODE (name
) == TYPE_DECL
)
7669 error_at (token
->location
, "invalid use of %qD", name
);
7670 postfix_expression
= error_mark_node
;
7674 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7676 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7678 error_at (token
->location
, "%<%D::%D%> is not a class member",
7679 parser
->scope
, name
);
7680 postfix_expression
= error_mark_node
;
7683 name
= build_qualified_name (/*type=*/NULL_TREE
,
7687 parser
->scope
= NULL_TREE
;
7688 parser
->qualifying_scope
= NULL_TREE
;
7689 parser
->object_scope
= NULL_TREE
;
7691 if (parser
->scope
&& name
&& BASELINK_P (name
))
7692 adjust_result_of_qualified_name_lookup
7693 (name
, parser
->scope
, scope
);
7695 = finish_class_member_access_expr (postfix_expression
, name
,
7697 tf_warning_or_error
);
7698 /* Build a location e.g.:
7701 where the caret is at the deref token, ranging from
7702 the start of postfix_expression to the end of the access expr. */
7704 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7705 location_t combined_loc
7706 = make_location (input_location
, start_loc
, end_loc
);
7707 protected_set_expr_location (postfix_expression
, combined_loc
);
7711 /* We no longer need to look up names in the scope of the object on
7712 the left-hand side of the `.' or `->' operator. */
7713 parser
->context
->object_type
= NULL_TREE
;
7715 /* Outside of offsetof, these operators may not appear in
7716 constant-expressions. */
7718 && (cp_parser_non_integral_constant_expression
7719 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7720 postfix_expression
= error_mark_node
;
7722 return postfix_expression
;
7725 /* Parse a parenthesized expression-list.
7728 assignment-expression
7729 expression-list, assignment-expression
7734 identifier, expression-list
7736 CAST_P is true if this expression is the target of a cast.
7738 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7741 Returns a vector of trees. Each element is a representation of an
7742 assignment-expression. NULL is returned if the ( and or ) are
7743 missing. An empty, but allocated, vector is returned on no
7744 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7745 if we are parsing an attribute list for an attribute that wants a
7746 plain identifier argument, normal_attr for an attribute that wants
7747 an expression, or non_attr if we aren't parsing an attribute list. If
7748 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7749 not all of the expressions in the list were constant.
7750 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7751 will be written to with the location of the closing parenthesis. If
7752 an error occurs, it may or may not be written to. */
7754 static vec
<tree
, va_gc
> *
7755 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7756 int is_attribute_list
,
7758 bool allow_expansion_p
,
7759 bool *non_constant_p
,
7760 location_t
*close_paren_loc
)
7762 vec
<tree
, va_gc
> *expression_list
;
7763 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7764 tree identifier
= NULL_TREE
;
7765 bool saved_greater_than_is_operator_p
;
7767 /* Assume all the expressions will be constant. */
7769 *non_constant_p
= false;
7771 matching_parens parens
;
7772 if (!parens
.require_open (parser
))
7775 expression_list
= make_tree_vector ();
7777 /* Within a parenthesized expression, a `>' token is always
7778 the greater-than operator. */
7779 saved_greater_than_is_operator_p
7780 = parser
->greater_than_is_operator_p
;
7781 parser
->greater_than_is_operator_p
= true;
7783 /* Consume expressions until there are no more. */
7784 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7789 /* At the beginning of attribute lists, check to see if the
7790 next token is an identifier. */
7791 if (is_attribute_list
== id_attr
7792 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7796 /* Consume the identifier. */
7797 token
= cp_lexer_consume_token (parser
->lexer
);
7798 /* Save the identifier. */
7799 identifier
= token
->u
.value
;
7803 bool expr_non_constant_p
;
7805 /* Parse the next assignment-expression. */
7806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7808 /* A braced-init-list. */
7809 cp_lexer_set_source_position (parser
->lexer
);
7810 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7811 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7812 if (non_constant_p
&& expr_non_constant_p
)
7813 *non_constant_p
= true;
7815 else if (non_constant_p
)
7817 expr
= (cp_parser_constant_expression
7818 (parser
, /*allow_non_constant_p=*/true,
7819 &expr_non_constant_p
));
7820 if (expr_non_constant_p
)
7821 *non_constant_p
= true;
7824 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7828 expr
= instantiate_non_dependent_expr (expr
);
7830 /* If we have an ellipsis, then this is an expression
7832 if (allow_expansion_p
7833 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7835 /* Consume the `...'. */
7836 cp_lexer_consume_token (parser
->lexer
);
7838 /* Build the argument pack. */
7839 expr
= make_pack_expansion (expr
);
7842 /* Add it to the list. We add error_mark_node
7843 expressions to the list, so that we can still tell if
7844 the correct form for a parenthesized expression-list
7845 is found. That gives better errors. */
7846 vec_safe_push (expression_list
, expr
);
7848 if (expr
== error_mark_node
)
7852 /* After the first item, attribute lists look the same as
7853 expression lists. */
7854 is_attribute_list
= non_attr
;
7857 /* If the next token isn't a `,', then we are done. */
7858 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7861 /* Otherwise, consume the `,' and keep going. */
7862 cp_lexer_consume_token (parser
->lexer
);
7865 if (close_paren_loc
)
7866 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7868 if (!parens
.require_close (parser
))
7873 /* We try and resync to an unnested comma, as that will give the
7874 user better diagnostics. */
7875 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7876 /*recovering=*/true,
7878 /*consume_paren=*/true);
7883 parser
->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p
;
7889 parser
->greater_than_is_operator_p
7890 = saved_greater_than_is_operator_p
;
7893 vec_safe_insert (expression_list
, 0, identifier
);
7895 return expression_list
;
7898 /* Parse a pseudo-destructor-name.
7900 pseudo-destructor-name:
7901 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7902 :: [opt] nested-name-specifier template template-id :: ~ type-name
7903 :: [opt] nested-name-specifier [opt] ~ type-name
7905 If either of the first two productions is used, sets *SCOPE to the
7906 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7907 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7908 or ERROR_MARK_NODE if the parse fails. */
7911 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7916 bool nested_name_specifier_p
;
7919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7920 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7921 && !type_dependent_expression_p (object
))
7923 if (cxx_dialect
< cxx14
)
7924 pedwarn (input_location
, 0,
7925 "%<~auto%> only available with "
7926 "-std=c++14 or -std=gnu++14");
7927 cp_lexer_consume_token (parser
->lexer
);
7928 cp_lexer_consume_token (parser
->lexer
);
7930 *type
= TREE_TYPE (object
);
7934 /* Assume that things will not work out. */
7935 *type
= error_mark_node
;
7937 /* Look for the optional `::' operator. */
7938 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7939 /* Look for the optional nested-name-specifier. */
7940 nested_name_specifier_p
7941 = (cp_parser_nested_name_specifier_opt (parser
,
7942 /*typename_keyword_p=*/false,
7943 /*check_dependency_p=*/true,
7945 /*is_declaration=*/false)
7947 /* Now, if we saw a nested-name-specifier, we might be doing the
7948 second production. */
7949 if (nested_name_specifier_p
7950 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7952 /* Consume the `template' keyword. */
7953 cp_lexer_consume_token (parser
->lexer
);
7954 /* Parse the template-id. */
7955 cp_parser_template_id (parser
,
7956 /*template_keyword_p=*/true,
7957 /*check_dependency_p=*/false,
7959 /*is_declaration=*/true);
7960 /* Look for the `::' token. */
7961 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7963 /* If the next token is not a `~', then there might be some
7964 additional qualification. */
7965 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7967 /* At this point, we're looking for "type-name :: ~". The type-name
7968 must not be a class-name, since this is a pseudo-destructor. So,
7969 it must be either an enum-name, or a typedef-name -- both of which
7970 are just identifiers. So, we peek ahead to check that the "::"
7971 and "~" tokens are present; if they are not, then we can avoid
7972 calling type_name. */
7973 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7974 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7975 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7977 cp_parser_error (parser
, "non-scalar type");
7981 /* Look for the type-name. */
7982 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7983 if (*scope
== error_mark_node
)
7986 /* Look for the `::' token. */
7987 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7992 /* Look for the `~'. */
7993 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7995 /* Once we see the ~, this has to be a pseudo-destructor. */
7996 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7997 cp_parser_commit_to_topmost_tentative_parse (parser
);
7999 /* Look for the type-name again. We are not responsible for
8000 checking that it matches the first type-name. */
8001 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8004 /* Parse a unary-expression.
8010 unary-operator cast-expression
8011 sizeof unary-expression
8013 alignof ( type-id ) [C++0x]
8020 __extension__ cast-expression
8021 __alignof__ unary-expression
8022 __alignof__ ( type-id )
8023 alignof unary-expression [C++0x]
8024 __real__ cast-expression
8025 __imag__ cast-expression
8027 sizeof ( type-id ) { initializer-list , [opt] }
8028 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8029 __alignof__ ( type-id ) { initializer-list , [opt] }
8031 ADDRESS_P is true iff the unary-expression is appearing as the
8032 operand of the `&' operator. CAST_P is true if this expression is
8033 the target of a cast.
8035 Returns a representation of the expression. */
8038 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8039 bool address_p
, bool cast_p
, bool decltype_p
)
8042 enum tree_code unary_operator
;
8044 /* Peek at the next token. */
8045 token
= cp_lexer_peek_token (parser
->lexer
);
8046 /* Some keywords give away the kind of expression. */
8047 if (token
->type
== CPP_KEYWORD
)
8049 enum rid keyword
= token
->keyword
;
8058 location_t start_loc
= token
->location
;
8060 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8061 /* Consume the token. */
8062 cp_lexer_consume_token (parser
->lexer
);
8063 /* Parse the operand. */
8064 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8066 if (TYPE_P (operand
))
8067 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8073 if (id_equal (token
->u
.value
, "alignof"))
8074 pedwarn (token
->location
, OPT_Wpedantic
,
8075 "ISO C++ does not allow %<alignof%> "
8078 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8084 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8086 if (!processing_template_decl
&& TYPE_P (operand
))
8088 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8089 build1 (NOP_EXPR
, operand
,
8091 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8094 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8095 TREE_SIDE_EFFECTS (ret
) = 0;
8096 TREE_READONLY (ret
) = 1;
8100 /* Construct a location e.g. :
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser
->lexer
)->location
;
8107 location_t compound_loc
8108 = make_location (start_loc
, start_loc
, finish_loc
);
8110 cp_expr
ret_expr (ret
);
8111 ret_expr
.set_location (compound_loc
);
8116 return cp_parser_new_expression (parser
);
8119 return cp_parser_delete_expression (parser
);
8123 /* The saved value of the PEDANTIC flag. */
8127 /* Save away the PEDANTIC flag. */
8128 cp_parser_extension_opt (parser
, &saved_pedantic
);
8129 /* Parse the cast-expression. */
8130 expr
= cp_parser_simple_cast_expression (parser
);
8131 /* Restore the PEDANTIC flag. */
8132 pedantic
= saved_pedantic
;
8142 /* Consume the `__real__' or `__imag__' token. */
8143 cp_lexer_consume_token (parser
->lexer
);
8144 /* Parse the cast-expression. */
8145 expression
= cp_parser_simple_cast_expression (parser
);
8146 /* Create the complete representation. */
8147 return build_x_unary_op (token
->location
,
8148 (keyword
== RID_REALPART
8149 ? REALPART_EXPR
: IMAGPART_EXPR
),
8151 tf_warning_or_error
);
8155 case RID_TRANSACTION_ATOMIC
:
8156 case RID_TRANSACTION_RELAXED
:
8157 return cp_parser_transaction_expression (parser
, keyword
);
8162 const char *saved_message
;
8163 bool saved_integral_constant_expression_p
;
8164 bool saved_non_integral_constant_expression_p
;
8165 bool saved_greater_than_is_operator_p
;
8167 cp_lexer_consume_token (parser
->lexer
);
8168 matching_parens parens
;
8169 parens
.require_open (parser
);
8171 saved_message
= parser
->type_definition_forbidden_message
;
8172 parser
->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8175 saved_integral_constant_expression_p
8176 = parser
->integral_constant_expression_p
;
8177 saved_non_integral_constant_expression_p
8178 = parser
->non_integral_constant_expression_p
;
8179 parser
->integral_constant_expression_p
= false;
8181 saved_greater_than_is_operator_p
8182 = parser
->greater_than_is_operator_p
;
8183 parser
->greater_than_is_operator_p
= true;
8185 ++cp_unevaluated_operand
;
8186 ++c_inhibit_evaluation_warnings
;
8187 ++cp_noexcept_operand
;
8188 expr
= cp_parser_expression (parser
);
8189 --cp_noexcept_operand
;
8190 --c_inhibit_evaluation_warnings
;
8191 --cp_unevaluated_operand
;
8193 parser
->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p
;
8196 parser
->integral_constant_expression_p
8197 = saved_integral_constant_expression_p
;
8198 parser
->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p
;
8201 parser
->type_definition_forbidden_message
= saved_message
;
8203 parens
.require_close (parser
);
8204 return finish_noexcept_expr (expr
, tf_warning_or_error
);
8212 /* Look for the `:: new' and `:: delete', which also signal the
8213 beginning of a new-expression, or delete-expression,
8214 respectively. If the next token is `::', then it might be one of
8216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8220 /* See if the token after the `::' is one of the keywords in
8221 which we're interested. */
8222 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8223 /* If it's `new', we have a new-expression. */
8224 if (keyword
== RID_NEW
)
8225 return cp_parser_new_expression (parser
);
8226 /* Similarly, for `delete'. */
8227 else if (keyword
== RID_DELETE
)
8228 return cp_parser_delete_expression (parser
);
8231 /* Look for a unary operator. */
8232 unary_operator
= cp_parser_unary_operator (token
);
8233 /* The `++' and `--' operators can be handled similarly, even though
8234 they are not technically unary-operators in the grammar. */
8235 if (unary_operator
== ERROR_MARK
)
8237 if (token
->type
== CPP_PLUS_PLUS
)
8238 unary_operator
= PREINCREMENT_EXPR
;
8239 else if (token
->type
== CPP_MINUS_MINUS
)
8240 unary_operator
= PREDECREMENT_EXPR
;
8241 /* Handle the GNU address-of-label extension. */
8242 else if (cp_parser_allow_gnu_extensions_p (parser
)
8243 && token
->type
== CPP_AND_AND
)
8247 location_t start_loc
= token
->location
;
8249 /* Consume the '&&' token. */
8250 cp_lexer_consume_token (parser
->lexer
);
8251 /* Look for the identifier. */
8252 location_t finish_loc
8253 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8254 identifier
= cp_parser_identifier (parser
);
8255 /* Construct a location of the form:
8258 with caret==start at the "&&", finish at the end of the label. */
8259 location_t combined_loc
8260 = make_location (start_loc
, start_loc
, finish_loc
);
8261 /* Create an expression representing the address. */
8262 expression
= finish_label_address_expr (identifier
, combined_loc
);
8263 if (cp_parser_non_integral_constant_expression (parser
,
8265 expression
= error_mark_node
;
8269 if (unary_operator
!= ERROR_MARK
)
8271 cp_expr cast_expression
;
8272 cp_expr expression
= error_mark_node
;
8273 non_integral_constant non_constant_p
= NIC_NONE
;
8274 location_t loc
= token
->location
;
8275 tsubst_flags_t complain
= complain_flags (decltype_p
);
8277 /* Consume the operator token. */
8278 token
= cp_lexer_consume_token (parser
->lexer
);
8279 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8281 /* Parse the cast-expression. */
8283 = cp_parser_cast_expression (parser
,
8284 unary_operator
== ADDR_EXPR
,
8290 OP_TOKEN CAST_EXPRESSION
8291 ^~~~~~~~~~~~~~~~~~~~~~~~~
8292 with start==caret at the operator token, and
8293 extending to the end of the cast_expression. */
8294 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8296 /* Now, build an appropriate representation. */
8297 switch (unary_operator
)
8300 non_constant_p
= NIC_STAR
;
8301 expression
= build_x_indirect_ref (loc
, cast_expression
,
8304 /* TODO: build_x_indirect_ref does not always honor the
8305 location, so ensure it is set. */
8306 expression
.set_location (loc
);
8310 non_constant_p
= NIC_ADDR
;
8313 expression
= build_x_unary_op (loc
, unary_operator
,
8316 /* TODO: build_x_unary_op does not always honor the location,
8317 so ensure it is set. */
8318 expression
.set_location (loc
);
8321 case PREINCREMENT_EXPR
:
8322 case PREDECREMENT_EXPR
:
8323 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8324 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8327 /* Immediately fold negation of a constant, unless the constant is 0
8328 (since -0 == 0) or it would overflow. */
8329 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8330 && CONSTANT_CLASS_P (cast_expression
)
8331 && !integer_zerop (cast_expression
)
8332 && !TREE_OVERFLOW (cast_expression
))
8334 tree folded
= fold_build1 (unary_operator
,
8335 TREE_TYPE (cast_expression
),
8337 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8339 expression
= cp_expr (folded
, loc
);
8344 case UNARY_PLUS_EXPR
:
8345 case TRUTH_NOT_EXPR
:
8346 expression
= finish_unary_op_expr (loc
, unary_operator
,
8347 cast_expression
, complain
);
8354 if (non_constant_p
!= NIC_NONE
8355 && cp_parser_non_integral_constant_expression (parser
,
8357 expression
= error_mark_node
;
8362 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8363 /*member_access_only_p=*/false,
8368 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8369 unary-operator, the corresponding tree code is returned. */
8371 static enum tree_code
8372 cp_parser_unary_operator (cp_token
* token
)
8374 switch (token
->type
)
8377 return INDIRECT_REF
;
8383 return UNARY_PLUS_EXPR
;
8389 return TRUTH_NOT_EXPR
;
8392 return BIT_NOT_EXPR
;
8399 /* Parse a new-expression.
8402 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8403 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8405 Returns a representation of the expression. */
8408 cp_parser_new_expression (cp_parser
* parser
)
8410 bool global_scope_p
;
8411 vec
<tree
, va_gc
> *placement
;
8413 vec
<tree
, va_gc
> *initializer
;
8414 tree nelts
= NULL_TREE
;
8417 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8419 /* Look for the optional `::' operator. */
8421 = (cp_parser_global_scope_opt (parser
,
8422 /*current_scope_valid_p=*/false)
8424 /* Look for the `new' operator. */
8425 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8426 /* There's no easy way to tell a new-placement from the
8427 `( type-id )' construct. */
8428 cp_parser_parse_tentatively (parser
);
8429 /* Look for a new-placement. */
8430 placement
= cp_parser_new_placement (parser
);
8431 /* If that didn't work out, there's no new-placement. */
8432 if (!cp_parser_parse_definitely (parser
))
8434 if (placement
!= NULL
)
8435 release_tree_vector (placement
);
8439 /* If the next token is a `(', then we have a parenthesized
8441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8444 const char *saved_message
= parser
->type_definition_forbidden_message
;
8446 /* Consume the `('. */
8447 matching_parens parens
;
8448 parens
.consume_open (parser
);
8450 /* Parse the type-id. */
8451 parser
->type_definition_forbidden_message
8452 = G_("types may not be defined in a new-expression");
8454 type_id_in_expr_sentinel
s (parser
);
8455 type
= cp_parser_type_id (parser
);
8457 parser
->type_definition_forbidden_message
= saved_message
;
8459 /* Look for the closing `)'. */
8460 parens
.require_close (parser
);
8461 token
= cp_lexer_peek_token (parser
->lexer
);
8462 /* There should not be a direct-new-declarator in this production,
8463 but GCC used to allowed this, so we check and emit a sensible error
8464 message for this case. */
8465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8467 error_at (token
->location
,
8468 "array bound forbidden after parenthesized type-id");
8469 inform (token
->location
,
8470 "try removing the parentheses around the type-id");
8471 cp_parser_direct_new_declarator (parser
);
8474 /* Otherwise, there must be a new-type-id. */
8476 type
= cp_parser_new_type_id (parser
, &nelts
);
8478 /* If the next token is a `(' or '{', then we have a new-initializer. */
8479 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8480 if (token
->type
== CPP_OPEN_PAREN
8481 || token
->type
== CPP_OPEN_BRACE
)
8482 initializer
= cp_parser_new_initializer (parser
);
8486 /* A new-expression may not appear in an integral constant
8488 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8489 ret
= error_mark_node
;
8490 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8491 of a new-type-id or type-id of a new-expression, the new-expression shall
8492 contain a new-initializer of the form ( assignment-expression )".
8493 Additionally, consistently with the spirit of DR 1467, we want to accept
8494 'new auto { 2 }' too. */
8495 else if ((ret
= type_uses_auto (type
))
8496 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8497 && (vec_safe_length (initializer
) != 1
8498 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8499 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8501 error_at (token
->location
,
8502 "initialization of new-expression for type %<auto%> "
8503 "requires exactly one element");
8504 ret
= error_mark_node
;
8508 /* Construct a location e.g.:
8511 with caret == start at the start of the "new" token, and the end
8512 at the end of the final token we consumed. */
8513 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8514 location_t end_loc
= get_finish (end_tok
->location
);
8515 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8517 /* Create a representation of the new-expression. */
8518 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8519 tf_warning_or_error
);
8520 protected_set_expr_location (ret
, combined_loc
);
8523 if (placement
!= NULL
)
8524 release_tree_vector (placement
);
8525 if (initializer
!= NULL
)
8526 release_tree_vector (initializer
);
8531 /* Parse a new-placement.
8536 Returns the same representation as for an expression-list. */
8538 static vec
<tree
, va_gc
> *
8539 cp_parser_new_placement (cp_parser
* parser
)
8541 vec
<tree
, va_gc
> *expression_list
;
8543 /* Parse the expression-list. */
8544 expression_list
= (cp_parser_parenthesized_expression_list
8545 (parser
, non_attr
, /*cast_p=*/false,
8546 /*allow_expansion_p=*/true,
8547 /*non_constant_p=*/NULL
));
8549 if (expression_list
&& expression_list
->is_empty ())
8550 error ("expected expression-list or type-id");
8552 return expression_list
;
8555 /* Parse a new-type-id.
8558 type-specifier-seq new-declarator [opt]
8560 Returns the TYPE allocated. If the new-type-id indicates an array
8561 type, *NELTS is set to the number of elements in the last array
8562 bound; the TYPE will not include the last array bound. */
8565 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8567 cp_decl_specifier_seq type_specifier_seq
;
8568 cp_declarator
*new_declarator
;
8569 cp_declarator
*declarator
;
8570 cp_declarator
*outer_declarator
;
8571 const char *saved_message
;
8573 /* The type-specifier sequence must not contain type definitions.
8574 (It cannot contain declarations of new types either, but if they
8575 are not definitions we will catch that because they are not
8577 saved_message
= parser
->type_definition_forbidden_message
;
8578 parser
->type_definition_forbidden_message
8579 = G_("types may not be defined in a new-type-id");
8580 /* Parse the type-specifier-seq. */
8581 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8582 /*is_trailing_return=*/false,
8583 &type_specifier_seq
);
8584 /* Restore the old message. */
8585 parser
->type_definition_forbidden_message
= saved_message
;
8587 if (type_specifier_seq
.type
== error_mark_node
)
8588 return error_mark_node
;
8590 /* Parse the new-declarator. */
8591 new_declarator
= cp_parser_new_declarator_opt (parser
);
8593 /* Determine the number of elements in the last array dimension, if
8596 /* Skip down to the last array dimension. */
8597 declarator
= new_declarator
;
8598 outer_declarator
= NULL
;
8599 while (declarator
&& (declarator
->kind
== cdk_pointer
8600 || declarator
->kind
== cdk_ptrmem
))
8602 outer_declarator
= declarator
;
8603 declarator
= declarator
->declarator
;
8606 && declarator
->kind
== cdk_array
8607 && declarator
->declarator
8608 && declarator
->declarator
->kind
== cdk_array
)
8610 outer_declarator
= declarator
;
8611 declarator
= declarator
->declarator
;
8614 if (declarator
&& declarator
->kind
== cdk_array
)
8616 *nelts
= declarator
->u
.array
.bounds
;
8617 if (*nelts
== error_mark_node
)
8618 *nelts
= integer_one_node
;
8620 if (outer_declarator
)
8621 outer_declarator
->declarator
= declarator
->declarator
;
8623 new_declarator
= NULL
;
8626 return groktypename (&type_specifier_seq
, new_declarator
, false);
8629 /* Parse an (optional) new-declarator.
8632 ptr-operator new-declarator [opt]
8633 direct-new-declarator
8635 Returns the declarator. */
8637 static cp_declarator
*
8638 cp_parser_new_declarator_opt (cp_parser
* parser
)
8640 enum tree_code code
;
8641 tree type
, std_attributes
= NULL_TREE
;
8642 cp_cv_quals cv_quals
;
8644 /* We don't know if there's a ptr-operator next, or not. */
8645 cp_parser_parse_tentatively (parser
);
8646 /* Look for a ptr-operator. */
8647 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8648 /* If that worked, look for more new-declarators. */
8649 if (cp_parser_parse_definitely (parser
))
8651 cp_declarator
*declarator
;
8653 /* Parse another optional declarator. */
8654 declarator
= cp_parser_new_declarator_opt (parser
);
8656 declarator
= cp_parser_make_indirect_declarator
8657 (code
, type
, cv_quals
, declarator
, std_attributes
);
8662 /* If the next token is a `[', there is a direct-new-declarator. */
8663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8664 return cp_parser_direct_new_declarator (parser
);
8669 /* Parse a direct-new-declarator.
8671 direct-new-declarator:
8673 direct-new-declarator [constant-expression]
8677 static cp_declarator
*
8678 cp_parser_direct_new_declarator (cp_parser
* parser
)
8680 cp_declarator
*declarator
= NULL
;
8687 /* Look for the opening `['. */
8688 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8690 token
= cp_lexer_peek_token (parser
->lexer
);
8691 expression
= cp_parser_expression (parser
);
8692 /* The standard requires that the expression have integral
8693 type. DR 74 adds enumeration types. We believe that the
8694 real intent is that these expressions be handled like the
8695 expression in a `switch' condition, which also allows
8696 classes with a single conversion to integral or
8697 enumeration type. */
8698 if (!processing_template_decl
)
8701 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8706 error_at (token
->location
,
8707 "expression in new-declarator must have integral "
8708 "or enumeration type");
8709 expression
= error_mark_node
;
8713 /* Look for the closing `]'. */
8714 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8716 /* Add this bound to the declarator. */
8717 declarator
= make_array_declarator (declarator
, expression
);
8719 /* If the next token is not a `[', then there are no more
8721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8728 /* Parse a new-initializer.
8731 ( expression-list [opt] )
8734 Returns a representation of the expression-list. */
8736 static vec
<tree
, va_gc
> *
8737 cp_parser_new_initializer (cp_parser
* parser
)
8739 vec
<tree
, va_gc
> *expression_list
;
8741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8744 bool expr_non_constant_p
;
8745 cp_lexer_set_source_position (parser
->lexer
);
8746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8747 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8748 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8749 expression_list
= make_tree_vector_single (t
);
8752 expression_list
= (cp_parser_parenthesized_expression_list
8753 (parser
, non_attr
, /*cast_p=*/false,
8754 /*allow_expansion_p=*/true,
8755 /*non_constant_p=*/NULL
));
8757 return expression_list
;
8760 /* Parse a delete-expression.
8763 :: [opt] delete cast-expression
8764 :: [opt] delete [ ] cast-expression
8766 Returns a representation of the expression. */
8769 cp_parser_delete_expression (cp_parser
* parser
)
8771 bool global_scope_p
;
8775 /* Look for the optional `::' operator. */
8777 = (cp_parser_global_scope_opt (parser
,
8778 /*current_scope_valid_p=*/false)
8780 /* Look for the `delete' keyword. */
8781 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8782 /* See if the array syntax is in use. */
8783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8785 /* Consume the `[' token. */
8786 cp_lexer_consume_token (parser
->lexer
);
8787 /* Look for the `]' token. */
8788 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8789 /* Remember that this is the `[]' construct. */
8795 /* Parse the cast-expression. */
8796 expression
= cp_parser_simple_cast_expression (parser
);
8798 /* A delete-expression may not appear in an integral constant
8800 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8801 return error_mark_node
;
8803 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8804 tf_warning_or_error
);
8807 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8808 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8812 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8815 switch (token
->type
)
8821 case CPP_CLOSE_SQUARE
:
8822 case CPP_CLOSE_PAREN
:
8823 case CPP_CLOSE_BRACE
:
8824 case CPP_OPEN_BRACE
:
8828 case CPP_DEREF_STAR
:
8836 case CPP_GREATER_EQ
:
8857 case CPP_OPEN_PAREN
:
8858 /* In ((type ()) () the last () isn't a valid cast-expression,
8859 so the whole must be parsed as postfix-expression. */
8860 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8863 case CPP_OPEN_SQUARE
:
8864 /* '[' may start a primary-expression in obj-c++ and in C++11,
8865 as a lambda-expression, eg, '(void)[]{}'. */
8866 if (cxx_dialect
>= cxx11
)
8868 return c_dialect_objc ();
8871 case CPP_MINUS_MINUS
:
8872 /* '++' and '--' may or may not start a cast-expression:
8874 struct T { void operator++(int); };
8875 void f() { (T())++; }
8888 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8889 in the order: const_cast, static_cast, reinterpret_cast.
8891 Don't suggest dynamic_cast.
8893 Return the first legal cast kind found, or NULL otherwise. */
8896 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8900 /* Reuse the parser logic by attempting to build the various kinds of
8901 cast, with "complain" disabled.
8902 Identify the first such cast that is valid. */
8904 /* Don't attempt to run such logic within template processing. */
8905 if (processing_template_decl
)
8908 /* First try const_cast. */
8909 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8910 if (trial
!= error_mark_node
)
8911 return "const_cast";
8913 /* If that fails, try static_cast. */
8914 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8915 if (trial
!= error_mark_node
)
8916 return "static_cast";
8918 /* Finally, try reinterpret_cast. */
8919 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8920 if (trial
!= error_mark_node
)
8921 return "reinterpret_cast";
8923 /* No such cast possible. */
8927 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8928 suggesting how to convert a C-style cast of the form:
8932 to a C++-style cast.
8934 The primary range of RICHLOC is asssumed to be that of the original
8935 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8936 of the parens in the C-style cast. */
8939 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8940 location_t close_paren_loc
, tree orig_expr
,
8943 /* This function is non-trivial, so bail out now if the warning isn't
8944 going to be emitted. */
8945 if (!warn_old_style_cast
)
8948 /* Try to find a legal C++ cast, trying them in order:
8949 const_cast, static_cast, reinterpret_cast. */
8950 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8951 if (!cast_suggestion
)
8954 /* Replace the open paren with "CAST_SUGGESTION<". */
8956 pp_printf (&pp
, "%s<", cast_suggestion
);
8957 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8959 /* Replace the close paren with "> (". */
8960 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8962 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8963 rich_loc
->add_fixit_insert_after (")");
8967 /* Parse a cast-expression.
8971 ( type-id ) cast-expression
8973 ADDRESS_P is true iff the unary-expression is appearing as the
8974 operand of the `&' operator. CAST_P is true if this expression is
8975 the target of a cast.
8977 Returns a representation of the expression. */
8980 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8981 bool decltype_p
, cp_id_kind
* pidk
)
8983 /* If it's a `(', then we might be looking at a cast. */
8984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8986 tree type
= NULL_TREE
;
8987 cp_expr
expr (NULL_TREE
);
8988 int cast_expression
= 0;
8989 const char *saved_message
;
8991 /* There's no way to know yet whether or not this is a cast.
8992 For example, `(int (3))' is a unary-expression, while `(int)
8993 3' is a cast. So, we resort to parsing tentatively. */
8994 cp_parser_parse_tentatively (parser
);
8995 /* Types may not be defined in a cast. */
8996 saved_message
= parser
->type_definition_forbidden_message
;
8997 parser
->type_definition_forbidden_message
8998 = G_("types may not be defined in casts");
8999 /* Consume the `('. */
9000 matching_parens parens
;
9001 cp_token
*open_paren
= parens
.consume_open (parser
);
9002 location_t open_paren_loc
= open_paren
->location
;
9003 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9005 /* A very tricky bit is that `(struct S) { 3 }' is a
9006 compound-literal (which we permit in C++ as an extension).
9007 But, that construct is not a cast-expression -- it is a
9008 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9009 is legal; if the compound-literal were a cast-expression,
9010 you'd need an extra set of parentheses.) But, if we parse
9011 the type-id, and it happens to be a class-specifier, then we
9012 will commit to the parse at that point, because we cannot
9013 undo the action that is done when creating a new class. So,
9014 then we cannot back up and do a postfix-expression.
9016 Another tricky case is the following (c++/29234):
9018 struct S { void operator () (); };
9025 As a type-id we parse the parenthesized S()() as a function
9026 returning a function, groktypename complains and we cannot
9027 back up in this case either.
9029 Therefore, we scan ahead to the closing `)', and check to see
9030 if the tokens after the `)' can start a cast-expression. Otherwise
9031 we are dealing with an unary-expression, a postfix-expression
9034 Yet another tricky case, in C++11, is the following (c++/54891):
9038 The issue is that usually, besides the case of lambda-expressions,
9039 the parenthesized type-id cannot be followed by '[', and, eg, we
9040 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9041 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9042 we don't commit, we try a cast-expression, then an unary-expression.
9044 Save tokens so that we can put them back. */
9045 cp_lexer_save_tokens (parser
->lexer
);
9047 /* We may be looking at a cast-expression. */
9048 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9049 /*consume_paren=*/true))
9051 = cp_parser_tokens_start_cast_expression (parser
);
9053 /* Roll back the tokens we skipped. */
9054 cp_lexer_rollback_tokens (parser
->lexer
);
9055 /* If we aren't looking at a cast-expression, simulate an error so
9056 that the call to cp_parser_error_occurred below returns true. */
9057 if (!cast_expression
)
9058 cp_parser_simulate_error (parser
);
9061 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9062 parser
->in_type_id_in_expr_p
= true;
9063 /* Look for the type-id. */
9064 type
= cp_parser_type_id (parser
);
9065 /* Look for the closing `)'. */
9066 cp_token
*close_paren
= parens
.require_close (parser
);
9068 close_paren_loc
= close_paren
->location
;
9069 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9072 /* Restore the saved message. */
9073 parser
->type_definition_forbidden_message
= saved_message
;
9075 /* At this point this can only be either a cast or a
9076 parenthesized ctor such as `(T ())' that looks like a cast to
9077 function returning T. */
9078 if (!cp_parser_error_occurred (parser
))
9080 /* Only commit if the cast-expression doesn't start with
9081 '++', '--', or '[' in C++11. */
9082 if (cast_expression
> 0)
9083 cp_parser_commit_to_topmost_tentative_parse (parser
);
9085 expr
= cp_parser_cast_expression (parser
,
9086 /*address_p=*/false,
9088 /*decltype_p=*/false,
9091 if (cp_parser_parse_definitely (parser
))
9093 /* Warn about old-style casts, if so requested. */
9094 if (warn_old_style_cast
9095 && !in_system_header_at (input_location
)
9096 && !VOID_TYPE_P (type
)
9097 && current_lang_name
!= lang_name_c
)
9099 gcc_rich_location
rich_loc (input_location
);
9100 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9102 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9103 "use of old-style cast to %q#T", type
);
9106 /* Only type conversions to integral or enumeration types
9107 can be used in constant-expressions. */
9108 if (!cast_valid_in_integral_constant_expression_p (type
)
9109 && cp_parser_non_integral_constant_expression (parser
,
9111 return error_mark_node
;
9113 /* Perform the cast. */
9117 with start==caret at the open paren, extending to the
9119 location_t cast_loc
= make_location (open_paren_loc
,
9121 expr
.get_finish ());
9122 expr
= build_c_cast (cast_loc
, type
, expr
);
9127 cp_parser_abort_tentative_parse (parser
);
9130 /* If we get here, then it's not a cast, so it must be a
9131 unary-expression. */
9132 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9133 cast_p
, decltype_p
);
9136 /* Parse a binary expression of the general form:
9140 pm-expression .* cast-expression
9141 pm-expression ->* cast-expression
9143 multiplicative-expression:
9145 multiplicative-expression * pm-expression
9146 multiplicative-expression / pm-expression
9147 multiplicative-expression % pm-expression
9149 additive-expression:
9150 multiplicative-expression
9151 additive-expression + multiplicative-expression
9152 additive-expression - multiplicative-expression
9156 shift-expression << additive-expression
9157 shift-expression >> additive-expression
9159 relational-expression:
9161 relational-expression < shift-expression
9162 relational-expression > shift-expression
9163 relational-expression <= shift-expression
9164 relational-expression >= shift-expression
9168 relational-expression:
9169 relational-expression <? shift-expression
9170 relational-expression >? shift-expression
9172 equality-expression:
9173 relational-expression
9174 equality-expression == relational-expression
9175 equality-expression != relational-expression
9179 and-expression & equality-expression
9181 exclusive-or-expression:
9183 exclusive-or-expression ^ and-expression
9185 inclusive-or-expression:
9186 exclusive-or-expression
9187 inclusive-or-expression | exclusive-or-expression
9189 logical-and-expression:
9190 inclusive-or-expression
9191 logical-and-expression && inclusive-or-expression
9193 logical-or-expression:
9194 logical-and-expression
9195 logical-or-expression || logical-and-expression
9197 All these are implemented with a single function like:
9200 simple-cast-expression
9201 binary-expression <token> binary-expression
9203 CAST_P is true if this expression is the target of a cast.
9205 The binops_by_token map is used to get the tree codes for each <token> type.
9206 binary-expressions are associated according to a precedence table. */
9208 #define TOKEN_PRECEDENCE(token) \
9209 (((token->type == CPP_GREATER \
9210 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9211 && !parser->greater_than_is_operator_p) \
9212 ? PREC_NOT_OPERATOR \
9213 : binops_by_token[token->type].prec)
9216 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9217 bool no_toplevel_fold_p
,
9219 enum cp_parser_prec prec
,
9222 cp_parser_expression_stack stack
;
9223 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9224 cp_parser_expression_stack_entry current
;
9227 enum tree_code rhs_type
;
9228 enum cp_parser_prec new_prec
, lookahead_prec
;
9231 /* Parse the first expression. */
9232 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9233 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9234 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9235 cast_p
, decltype_p
, pidk
);
9236 current
.prec
= prec
;
9238 if (cp_parser_error_occurred (parser
))
9239 return error_mark_node
;
9243 /* Get an operator token. */
9244 token
= cp_lexer_peek_token (parser
->lexer
);
9246 if (warn_cxx11_compat
9247 && token
->type
== CPP_RSHIFT
9248 && !parser
->greater_than_is_operator_p
)
9250 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9251 "%<>>%> operator is treated"
9252 " as two right angle brackets in C++11"))
9253 inform (token
->location
,
9254 "suggest parentheses around %<>>%> expression");
9257 new_prec
= TOKEN_PRECEDENCE (token
);
9258 if (new_prec
!= PREC_NOT_OPERATOR
9259 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9260 /* This is a fold-expression; handle it later. */
9261 new_prec
= PREC_NOT_OPERATOR
;
9263 /* Popping an entry off the stack means we completed a subexpression:
9264 - either we found a token which is not an operator (`>' where it is not
9265 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9266 will happen repeatedly;
9267 - or, we found an operator which has lower priority. This is the case
9268 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9270 if (new_prec
<= current
.prec
)
9279 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9280 current
.loc
= token
->location
;
9282 /* We used the operator token. */
9283 cp_lexer_consume_token (parser
->lexer
);
9285 /* For "false && x" or "true || x", x will never be executed;
9286 disable warnings while evaluating it. */
9287 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9288 c_inhibit_evaluation_warnings
+=
9289 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9290 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9291 c_inhibit_evaluation_warnings
+=
9292 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9294 /* Extract another operand. It may be the RHS of this expression
9295 or the LHS of a new, higher priority expression. */
9296 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9297 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9298 rhs
= cp_parser_simple_cast_expression (parser
);
9300 /* Get another operator token. Look up its precedence to avoid
9301 building a useless (immediately popped) stack entry for common
9302 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9303 token
= cp_lexer_peek_token (parser
->lexer
);
9304 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9305 if (lookahead_prec
!= PREC_NOT_OPERATOR
9306 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9307 lookahead_prec
= PREC_NOT_OPERATOR
;
9308 if (lookahead_prec
> new_prec
)
9310 /* ... and prepare to parse the RHS of the new, higher priority
9311 expression. Since precedence levels on the stack are
9312 monotonically increasing, we do not have to care about
9317 current
.lhs_type
= rhs_type
;
9318 current
.prec
= new_prec
;
9319 new_prec
= lookahead_prec
;
9323 lookahead_prec
= new_prec
;
9324 /* If the stack is not empty, we have parsed into LHS the right side
9325 (`4' in the example above) of an expression we had suspended.
9326 We can use the information on the stack to recover the LHS (`3')
9327 from the stack together with the tree code (`MULT_EXPR'), and
9328 the precedence of the higher level subexpression
9329 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9330 which will be used to actually build the additive expression. */
9332 rhs_type
= current
.lhs_type
;
9337 /* Undo the disabling of warnings done above. */
9338 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9339 c_inhibit_evaluation_warnings
-=
9340 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9341 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9342 c_inhibit_evaluation_warnings
-=
9343 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9345 if (warn_logical_not_paren
9346 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9347 && current
.lhs_type
== TRUTH_NOT_EXPR
9348 /* Avoid warning for !!x == y. */
9349 && (TREE_CODE (current
.lhs
) != NE_EXPR
9350 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9351 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9352 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9353 /* Avoid warning for !b == y where b is boolean. */
9354 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9355 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9357 /* Avoid warning for !!b == y where b is boolean. */
9358 && (!DECL_P (current
.lhs
)
9359 || TREE_TYPE (current
.lhs
) == NULL_TREE
9360 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9361 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9362 current
.lhs
, maybe_constant_value (rhs
));
9366 location_t combined_loc
= make_location (current
.loc
,
9367 current
.lhs
.get_start (),
9370 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9371 ERROR_MARK for everything that is not a binary expression.
9372 This makes warn_about_parentheses miss some warnings that
9373 involve unary operators. For unary expressions we should
9374 pass the correct tree_code unless the unary expression was
9375 surrounded by parentheses.
9377 if (no_toplevel_fold_p
9378 && lookahead_prec
<= current
.prec
9380 current
.lhs
= build2_loc (combined_loc
,
9382 TREE_CODE_CLASS (current
.tree_type
)
9384 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9388 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9389 current
.lhs
, current
.lhs_type
,
9390 rhs
, rhs_type
, &overload
,
9391 complain_flags (decltype_p
));
9392 /* TODO: build_x_binary_op doesn't always honor the location. */
9393 current
.lhs
.set_location (combined_loc
);
9395 current
.lhs_type
= current
.tree_type
;
9397 /* If the binary operator required the use of an overloaded operator,
9398 then this expression cannot be an integral constant-expression.
9399 An overloaded operator can be used even if both operands are
9400 otherwise permissible in an integral constant-expression if at
9401 least one of the operands is of enumeration type. */
9404 && cp_parser_non_integral_constant_expression (parser
,
9406 return error_mark_node
;
9413 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9414 bool no_toplevel_fold_p
,
9415 enum cp_parser_prec prec
,
9418 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9419 /*decltype*/false, prec
, pidk
);
9422 /* Parse the `? expression : assignment-expression' part of a
9423 conditional-expression. The LOGICAL_OR_EXPR is the
9424 logical-or-expression that started the conditional-expression.
9425 Returns a representation of the entire conditional-expression.
9427 This routine is used by cp_parser_assignment_expression.
9429 ? expression : assignment-expression
9433 ? : assignment-expression */
9436 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9438 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9439 cp_expr assignment_expr
;
9440 struct cp_token
*token
;
9441 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9443 /* Consume the `?' token. */
9444 cp_lexer_consume_token (parser
->lexer
);
9445 token
= cp_lexer_peek_token (parser
->lexer
);
9446 if (cp_parser_allow_gnu_extensions_p (parser
)
9447 && token
->type
== CPP_COLON
)
9449 pedwarn (token
->location
, OPT_Wpedantic
,
9450 "ISO C++ does not allow ?: with omitted middle operand");
9451 /* Implicit true clause. */
9453 c_inhibit_evaluation_warnings
+=
9454 folded_logical_or_expr
== truthvalue_true_node
;
9455 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9459 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9460 parser
->colon_corrects_to_scope_p
= false;
9461 /* Parse the expression. */
9462 c_inhibit_evaluation_warnings
+=
9463 folded_logical_or_expr
== truthvalue_false_node
;
9464 expr
= cp_parser_expression (parser
);
9465 c_inhibit_evaluation_warnings
+=
9466 ((folded_logical_or_expr
== truthvalue_true_node
)
9467 - (folded_logical_or_expr
== truthvalue_false_node
));
9468 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9471 /* The next token should be a `:'. */
9472 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9473 /* Parse the assignment-expression. */
9474 assignment_expr
= cp_parser_assignment_expression (parser
);
9475 c_inhibit_evaluation_warnings
-=
9476 folded_logical_or_expr
== truthvalue_true_node
;
9479 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9480 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9481 with the caret at the "?", ranging from the start of
9482 the logical_or_expr to the end of the assignment_expr. */
9483 loc
= make_location (loc
,
9484 logical_or_expr
.get_start (),
9485 assignment_expr
.get_finish ());
9487 /* Build the conditional-expression. */
9488 return build_x_conditional_expr (loc
, logical_or_expr
,
9491 tf_warning_or_error
);
9494 /* Parse an assignment-expression.
9496 assignment-expression:
9497 conditional-expression
9498 logical-or-expression assignment-operator assignment_expression
9501 CAST_P is true if this expression is the target of a cast.
9502 DECLTYPE_P is true if this expression is the operand of decltype.
9504 Returns a representation for the expression. */
9507 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9508 bool cast_p
, bool decltype_p
)
9512 /* If the next token is the `throw' keyword, then we're looking at
9513 a throw-expression. */
9514 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9515 expr
= cp_parser_throw_expression (parser
);
9516 /* Otherwise, it must be that we are looking at a
9517 logical-or-expression. */
9520 /* Parse the binary expressions (logical-or-expression). */
9521 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9523 PREC_NOT_OPERATOR
, pidk
);
9524 /* If the next token is a `?' then we're actually looking at a
9525 conditional-expression. */
9526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9527 return cp_parser_question_colon_clause (parser
, expr
);
9530 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9532 /* If it's an assignment-operator, we're using the second
9534 enum tree_code assignment_operator
9535 = cp_parser_assignment_operator_opt (parser
);
9536 if (assignment_operator
!= ERROR_MARK
)
9538 bool non_constant_p
;
9540 /* Parse the right-hand side of the assignment. */
9541 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9544 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9547 /* An assignment may not appear in a
9548 constant-expression. */
9549 if (cp_parser_non_integral_constant_expression (parser
,
9551 return error_mark_node
;
9552 /* Build the assignment expression. Its default
9556 is the location of the '=' token as the
9557 caret, ranging from the start of the lhs to the
9559 loc
= make_location (loc
,
9562 expr
= build_x_modify_expr (loc
, expr
,
9563 assignment_operator
,
9565 complain_flags (decltype_p
));
9566 /* TODO: build_x_modify_expr doesn't honor the location,
9567 so we must set it here. */
9568 expr
.set_location (loc
);
9576 /* Parse an (optional) assignment-operator.
9578 assignment-operator: one of
9579 = *= /= %= += -= >>= <<= &= ^= |=
9583 assignment-operator: one of
9586 If the next token is an assignment operator, the corresponding tree
9587 code is returned, and the token is consumed. For example, for
9588 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9589 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9590 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9591 operator, ERROR_MARK is returned. */
9593 static enum tree_code
9594 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9599 /* Peek at the next token. */
9600 token
= cp_lexer_peek_token (parser
->lexer
);
9602 switch (token
->type
)
9613 op
= TRUNC_DIV_EXPR
;
9617 op
= TRUNC_MOD_EXPR
;
9649 /* Nothing else is an assignment operator. */
9653 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9654 if (op
!= ERROR_MARK
9655 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9658 /* If it was an assignment operator, consume it. */
9659 if (op
!= ERROR_MARK
)
9660 cp_lexer_consume_token (parser
->lexer
);
9665 /* Parse an expression.
9668 assignment-expression
9669 expression , assignment-expression
9671 CAST_P is true if this expression is the target of a cast.
9672 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9673 except possibly parenthesized or on the RHS of a comma (N3276).
9675 Returns a representation of the expression. */
9678 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9679 bool cast_p
, bool decltype_p
)
9681 cp_expr expression
= NULL_TREE
;
9682 location_t loc
= UNKNOWN_LOCATION
;
9686 cp_expr assignment_expression
;
9688 /* Parse the next assignment-expression. */
9689 assignment_expression
9690 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9692 /* We don't create a temporary for a call that is the immediate operand
9693 of decltype or on the RHS of a comma. But when we see a comma, we
9694 need to create a temporary for a call on the LHS. */
9695 if (decltype_p
&& !processing_template_decl
9696 && TREE_CODE (assignment_expression
) == CALL_EXPR
9697 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9698 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9699 assignment_expression
9700 = build_cplus_new (TREE_TYPE (assignment_expression
),
9701 assignment_expression
, tf_warning_or_error
);
9703 /* If this is the first assignment-expression, we can just
9706 expression
= assignment_expression
;
9709 /* Create a location with caret at the comma, ranging
9710 from the start of the LHS to the end of the RHS. */
9711 loc
= make_location (loc
,
9712 expression
.get_start (),
9713 assignment_expression
.get_finish ());
9714 expression
= build_x_compound_expr (loc
, expression
,
9715 assignment_expression
,
9716 complain_flags (decltype_p
));
9717 expression
.set_location (loc
);
9719 /* If the next token is not a comma, or we're in a fold-expression, then
9720 we are done with the expression. */
9721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9722 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9724 /* Consume the `,'. */
9725 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9726 cp_lexer_consume_token (parser
->lexer
);
9727 /* A comma operator cannot appear in a constant-expression. */
9728 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9729 expression
= error_mark_node
;
9735 /* Parse a constant-expression.
9737 constant-expression:
9738 conditional-expression
9740 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9741 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9742 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9743 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9744 only parse a conditional-expression, otherwise parse an
9745 assignment-expression. See below for rationale. */
9748 cp_parser_constant_expression (cp_parser
* parser
,
9749 bool allow_non_constant_p
,
9750 bool *non_constant_p
,
9753 bool saved_integral_constant_expression_p
;
9754 bool saved_allow_non_integral_constant_expression_p
;
9755 bool saved_non_integral_constant_expression_p
;
9758 /* It might seem that we could simply parse the
9759 conditional-expression, and then check to see if it were
9760 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9761 one that the compiler can figure out is constant, possibly after
9762 doing some simplifications or optimizations. The standard has a
9763 precise definition of constant-expression, and we must honor
9764 that, even though it is somewhat more restrictive.
9770 is not a legal declaration, because `(2, 3)' is not a
9771 constant-expression. The `,' operator is forbidden in a
9772 constant-expression. However, GCC's constant-folding machinery
9773 will fold this operation to an INTEGER_CST for `3'. */
9775 /* Save the old settings. */
9776 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9777 saved_allow_non_integral_constant_expression_p
9778 = parser
->allow_non_integral_constant_expression_p
;
9779 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9780 /* We are now parsing a constant-expression. */
9781 parser
->integral_constant_expression_p
= true;
9782 parser
->allow_non_integral_constant_expression_p
9783 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9784 parser
->non_integral_constant_expression_p
= false;
9785 /* Although the grammar says "conditional-expression", when not STRICT_P,
9786 we parse an "assignment-expression", which also permits
9787 "throw-expression" and the use of assignment operators. In the case
9788 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9789 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9790 actually essential that we look for an assignment-expression.
9791 For example, cp_parser_initializer_clauses uses this function to
9792 determine whether a particular assignment-expression is in fact
9796 /* Parse the binary expressions (logical-or-expression). */
9797 expression
= cp_parser_binary_expression (parser
, false, false, false,
9798 PREC_NOT_OPERATOR
, NULL
);
9799 /* If the next token is a `?' then we're actually looking at
9800 a conditional-expression; otherwise we're done. */
9801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9802 expression
= cp_parser_question_colon_clause (parser
, expression
);
9805 expression
= cp_parser_assignment_expression (parser
);
9806 /* Restore the old settings. */
9807 parser
->integral_constant_expression_p
9808 = saved_integral_constant_expression_p
;
9809 parser
->allow_non_integral_constant_expression_p
9810 = saved_allow_non_integral_constant_expression_p
;
9811 if (cxx_dialect
>= cxx11
)
9813 /* Require an rvalue constant expression here; that's what our
9814 callers expect. Reference constant expressions are handled
9815 separately in e.g. cp_parser_template_argument. */
9816 tree decay
= expression
;
9817 if (TREE_TYPE (expression
)
9818 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9819 decay
= build_address (expression
);
9820 bool is_const
= potential_rvalue_constant_expression (decay
);
9821 parser
->non_integral_constant_expression_p
= !is_const
;
9822 if (!is_const
&& !allow_non_constant_p
)
9823 require_potential_rvalue_constant_expression (decay
);
9825 if (allow_non_constant_p
)
9826 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9827 parser
->non_integral_constant_expression_p
9828 = saved_non_integral_constant_expression_p
;
9833 /* Parse __builtin_offsetof.
9835 offsetof-expression:
9836 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9838 offsetof-member-designator:
9840 | offsetof-member-designator "." id-expression
9841 | offsetof-member-designator "[" expression "]"
9842 | offsetof-member-designator "->" id-expression */
9845 cp_parser_builtin_offsetof (cp_parser
*parser
)
9847 int save_ice_p
, save_non_ice_p
;
9852 location_t finish_loc
;
9854 /* We're about to accept non-integral-constant things, but will
9855 definitely yield an integral constant expression. Save and
9856 restore these values around our local parsing. */
9857 save_ice_p
= parser
->integral_constant_expression_p
;
9858 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9860 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9862 /* Consume the "__builtin_offsetof" token. */
9863 cp_lexer_consume_token (parser
->lexer
);
9864 /* Consume the opening `('. */
9865 matching_parens parens
;
9866 parens
.require_open (parser
);
9867 /* Parse the type-id. */
9868 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9869 type
= cp_parser_type_id (parser
);
9870 /* Look for the `,'. */
9871 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9872 token
= cp_lexer_peek_token (parser
->lexer
);
9874 /* Build the (type *)null that begins the traditional offsetof macro. */
9876 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9877 tf_warning_or_error
);
9879 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9880 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9881 true, &dummy
, token
->location
);
9884 token
= cp_lexer_peek_token (parser
->lexer
);
9885 switch (token
->type
)
9887 case CPP_OPEN_SQUARE
:
9888 /* offsetof-member-designator "[" expression "]" */
9889 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9894 /* offsetof-member-designator "->" identifier */
9895 expr
= grok_array_decl (token
->location
, expr
,
9896 integer_zero_node
, false);
9900 /* offsetof-member-designator "." identifier */
9901 cp_lexer_consume_token (parser
->lexer
);
9902 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9907 case CPP_CLOSE_PAREN
:
9908 /* Consume the ")" token. */
9909 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9910 cp_lexer_consume_token (parser
->lexer
);
9914 /* Error. We know the following require will fail, but
9915 that gives the proper error message. */
9916 parens
.require_close (parser
);
9917 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9918 expr
= error_mark_node
;
9924 /* Make a location of the form:
9925 __builtin_offsetof (struct s, f)
9926 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9927 with caret at the type-id, ranging from the start of the
9928 "_builtin_offsetof" token to the close paren. */
9929 loc
= make_location (loc
, start_loc
, finish_loc
);
9930 /* The result will be an INTEGER_CST, so we need to explicitly
9931 preserve the location. */
9932 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9935 parser
->integral_constant_expression_p
= save_ice_p
;
9936 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9941 /* Parse a trait expression.
9943 Returns a representation of the expression, the underlying type
9944 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9947 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9950 tree type1
, type2
= NULL_TREE
;
9951 bool binary
= false;
9952 bool variadic
= false;
9956 case RID_HAS_NOTHROW_ASSIGN
:
9957 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9959 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9960 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9962 case RID_HAS_NOTHROW_COPY
:
9963 kind
= CPTK_HAS_NOTHROW_COPY
;
9965 case RID_HAS_TRIVIAL_ASSIGN
:
9966 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9968 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9969 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9971 case RID_HAS_TRIVIAL_COPY
:
9972 kind
= CPTK_HAS_TRIVIAL_COPY
;
9974 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9975 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9977 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9978 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9980 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9981 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9983 case RID_IS_ABSTRACT
:
9984 kind
= CPTK_IS_ABSTRACT
;
9986 case RID_IS_AGGREGATE
:
9987 kind
= CPTK_IS_AGGREGATE
;
9989 case RID_IS_BASE_OF
:
9990 kind
= CPTK_IS_BASE_OF
;
9994 kind
= CPTK_IS_CLASS
;
9997 kind
= CPTK_IS_EMPTY
;
10000 kind
= CPTK_IS_ENUM
;
10003 kind
= CPTK_IS_FINAL
;
10005 case RID_IS_LITERAL_TYPE
:
10006 kind
= CPTK_IS_LITERAL_TYPE
;
10009 kind
= CPTK_IS_POD
;
10011 case RID_IS_POLYMORPHIC
:
10012 kind
= CPTK_IS_POLYMORPHIC
;
10014 case RID_IS_SAME_AS
:
10015 kind
= CPTK_IS_SAME_AS
;
10018 case RID_IS_STD_LAYOUT
:
10019 kind
= CPTK_IS_STD_LAYOUT
;
10021 case RID_IS_TRIVIAL
:
10022 kind
= CPTK_IS_TRIVIAL
;
10024 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10025 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10028 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10029 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10032 case RID_IS_TRIVIALLY_COPYABLE
:
10033 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10036 kind
= CPTK_IS_UNION
;
10038 case RID_UNDERLYING_TYPE
:
10039 kind
= CPTK_UNDERLYING_TYPE
;
10044 case RID_DIRECT_BASES
:
10045 kind
= CPTK_DIRECT_BASES
;
10047 case RID_IS_ASSIGNABLE
:
10048 kind
= CPTK_IS_ASSIGNABLE
;
10051 case RID_IS_CONSTRUCTIBLE
:
10052 kind
= CPTK_IS_CONSTRUCTIBLE
;
10056 gcc_unreachable ();
10059 /* Consume the token. */
10060 cp_lexer_consume_token (parser
->lexer
);
10062 matching_parens parens
;
10063 parens
.require_open (parser
);
10066 type_id_in_expr_sentinel
s (parser
);
10067 type1
= cp_parser_type_id (parser
);
10070 if (type1
== error_mark_node
)
10071 return error_mark_node
;
10075 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10078 type_id_in_expr_sentinel
s (parser
);
10079 type2
= cp_parser_type_id (parser
);
10082 if (type2
== error_mark_node
)
10083 return error_mark_node
;
10087 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10089 cp_lexer_consume_token (parser
->lexer
);
10090 tree elt
= cp_parser_type_id (parser
);
10091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10093 cp_lexer_consume_token (parser
->lexer
);
10094 elt
= make_pack_expansion (elt
);
10096 if (elt
== error_mark_node
)
10097 return error_mark_node
;
10098 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10102 parens
.require_close (parser
);
10104 /* Complete the trait expression, which may mean either processing
10105 the trait expr now or saving it for template instantiation. */
10108 case CPTK_UNDERLYING_TYPE
:
10109 return finish_underlying_type (type1
);
10111 return finish_bases (type1
, false);
10112 case CPTK_DIRECT_BASES
:
10113 return finish_bases (type1
, true);
10115 return finish_trait_expr (kind
, type1
, type2
);
10119 /* Parse a lambda expression.
10122 lambda-introducer lambda-declarator [opt] compound-statement
10124 Returns a representation of the expression. */
10127 cp_parser_lambda_expression (cp_parser
* parser
)
10129 tree lambda_expr
= build_lambda_expr ();
10132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10133 cp_token_position start
= 0;
10135 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10137 if (cp_unevaluated_operand
)
10139 if (!token
->error_reported
)
10141 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10142 "lambda-expression in unevaluated context");
10143 token
->error_reported
= true;
10147 else if (parser
->in_template_argument_list_p
)
10149 if (!token
->error_reported
)
10151 error_at (token
->location
, "lambda-expression in template-argument");
10152 token
->error_reported
= true;
10157 /* We may be in the middle of deferred access check. Disable
10159 push_deferring_access_checks (dk_no_deferred
);
10161 cp_parser_lambda_introducer (parser
, lambda_expr
);
10163 type
= begin_lambda_type (lambda_expr
);
10164 if (type
== error_mark_node
)
10165 return error_mark_node
;
10167 record_lambda_scope (lambda_expr
);
10169 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10170 determine_visibility (TYPE_NAME (type
));
10172 /* Now that we've started the type, add the capture fields for any
10173 explicit captures. */
10174 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10177 /* Inside the class, surrounding template-parameter-lists do not apply. */
10178 unsigned int saved_num_template_parameter_lists
10179 = parser
->num_template_parameter_lists
;
10180 unsigned char in_statement
= parser
->in_statement
;
10181 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10182 bool fully_implicit_function_template_p
10183 = parser
->fully_implicit_function_template_p
;
10184 tree implicit_template_parms
= parser
->implicit_template_parms
;
10185 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10186 bool auto_is_implicit_function_template_parm_p
10187 = parser
->auto_is_implicit_function_template_parm_p
;
10189 parser
->num_template_parameter_lists
= 0;
10190 parser
->in_statement
= 0;
10191 parser
->in_switch_statement_p
= false;
10192 parser
->fully_implicit_function_template_p
= false;
10193 parser
->implicit_template_parms
= 0;
10194 parser
->implicit_template_scope
= 0;
10195 parser
->auto_is_implicit_function_template_parm_p
= false;
10197 /* By virtue of defining a local class, a lambda expression has access to
10198 the private variables of enclosing classes. */
10200 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10202 if (ok
&& cp_parser_error_occurred (parser
))
10207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10208 && cp_parser_start_tentative_firewall (parser
))
10210 cp_parser_lambda_body (parser
, lambda_expr
);
10212 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10214 if (cp_parser_skip_to_closing_brace (parser
))
10215 cp_lexer_consume_token (parser
->lexer
);
10218 /* The capture list was built up in reverse order; fix that now. */
10219 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10220 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10223 maybe_add_lambda_conv_op (type
);
10225 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10227 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10228 parser
->in_statement
= in_statement
;
10229 parser
->in_switch_statement_p
= in_switch_statement_p
;
10230 parser
->fully_implicit_function_template_p
10231 = fully_implicit_function_template_p
;
10232 parser
->implicit_template_parms
= implicit_template_parms
;
10233 parser
->implicit_template_scope
= implicit_template_scope
;
10234 parser
->auto_is_implicit_function_template_parm_p
10235 = auto_is_implicit_function_template_parm_p
;
10238 /* This field is only used during parsing of the lambda. */
10239 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10241 /* This lambda shouldn't have any proxies left at this point. */
10242 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10243 /* And now that we're done, push proxies for an enclosing lambda. */
10244 insert_pending_capture_proxies ();
10247 lambda_expr
= build_lambda_object (lambda_expr
);
10249 lambda_expr
= error_mark_node
;
10251 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10253 pop_deferring_access_checks ();
10255 return lambda_expr
;
10258 /* Parse the beginning of a lambda expression.
10261 [ lambda-capture [opt] ]
10263 LAMBDA_EXPR is the current representation of the lambda expression. */
10266 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10268 /* Need commas after the first capture. */
10271 /* Eat the leading `['. */
10272 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10274 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10276 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10278 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10279 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10281 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10283 cp_lexer_consume_token (parser
->lexer
);
10287 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10289 cp_token
* capture_token
;
10291 tree capture_init_expr
;
10292 cp_id_kind idk
= CP_ID_KIND_NONE
;
10293 bool explicit_init_p
= false;
10295 enum capture_kind_type
10300 enum capture_kind_type capture_kind
= BY_COPY
;
10302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10304 error ("expected end of capture-list");
10311 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10313 /* Possibly capture `this'. */
10314 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10316 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10317 if (cxx_dialect
< cxx2a
10318 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10319 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10320 "with by-copy capture default");
10321 cp_lexer_consume_token (parser
->lexer
);
10322 add_capture (lambda_expr
,
10323 /*id=*/this_identifier
,
10324 /*initializer=*/finish_this_expr (),
10325 /*by_reference_p=*/true,
10330 /* Possibly capture `*this'. */
10331 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10332 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10334 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10335 if (cxx_dialect
< cxx17
)
10336 pedwarn (loc
, 0, "%<*this%> capture only available with "
10337 "-std=c++17 or -std=gnu++17");
10338 cp_lexer_consume_token (parser
->lexer
);
10339 cp_lexer_consume_token (parser
->lexer
);
10340 add_capture (lambda_expr
,
10341 /*id=*/this_identifier
,
10342 /*initializer=*/finish_this_expr (),
10343 /*by_reference_p=*/false,
10348 /* Remember whether we want to capture as a reference or not. */
10349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10351 capture_kind
= BY_REFERENCE
;
10352 cp_lexer_consume_token (parser
->lexer
);
10355 /* Get the identifier. */
10356 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10357 capture_id
= cp_parser_identifier (parser
);
10359 if (capture_id
== error_mark_node
)
10360 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10361 delimiters, but I modified this to stop on unnested ']' as well. It
10362 was already changed to stop on unnested '}', so the
10363 "closing_parenthesis" name is no more misleading with my change. */
10365 cp_parser_skip_to_closing_parenthesis (parser
,
10366 /*recovering=*/true,
10368 /*consume_paren=*/true);
10372 /* Find the initializer for this capture. */
10373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10374 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10375 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10377 bool direct
, non_constant
;
10378 /* An explicit initializer exists. */
10379 if (cxx_dialect
< cxx14
)
10380 pedwarn (input_location
, 0,
10381 "lambda capture initializers "
10382 "only available with -std=c++14 or -std=gnu++14");
10383 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10385 explicit_init_p
= true;
10386 if (capture_init_expr
== NULL_TREE
)
10388 error ("empty initializer for lambda init-capture");
10389 capture_init_expr
= error_mark_node
;
10394 const char* error_msg
;
10396 /* Turn the identifier into an id-expression. */
10398 = cp_parser_lookup_name_simple (parser
, capture_id
,
10399 capture_token
->location
);
10401 if (capture_init_expr
== error_mark_node
)
10403 unqualified_name_lookup_error (capture_id
);
10406 else if (DECL_P (capture_init_expr
)
10407 && (!VAR_P (capture_init_expr
)
10408 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10410 error_at (capture_token
->location
,
10411 "capture of non-variable %qD ",
10412 capture_init_expr
);
10413 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10414 "%q#D declared here", capture_init_expr
);
10417 if (VAR_P (capture_init_expr
)
10418 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10420 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10421 "%qD with non-automatic storage duration",
10422 capture_init_expr
))
10423 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10424 "%q#D declared here", capture_init_expr
);
10429 = finish_id_expression
10434 /*integral_constant_expression_p=*/false,
10435 /*allow_non_integral_constant_expression_p=*/false,
10436 /*non_integral_constant_expression_p=*/NULL
,
10437 /*template_p=*/false,
10439 /*address_p=*/false,
10440 /*template_arg_p=*/false,
10442 capture_token
->location
);
10444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10446 cp_lexer_consume_token (parser
->lexer
);
10447 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10450 check_for_bare_parameter_packs (capture_init_expr
);
10453 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10454 && !explicit_init_p
)
10456 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10457 && capture_kind
== BY_COPY
)
10458 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10459 "of %qD redundant with by-copy capture default",
10461 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10462 && capture_kind
== BY_REFERENCE
)
10463 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10464 "capture of %qD redundant with by-reference capture "
10465 "default", capture_id
);
10468 add_capture (lambda_expr
,
10471 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10475 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10478 /* Parse the (optional) middle of a lambda expression.
10481 < template-parameter-list [opt] >
10482 ( parameter-declaration-clause [opt] )
10483 attribute-specifier [opt]
10484 decl-specifier-seq [opt]
10485 exception-specification [opt]
10486 lambda-return-type-clause [opt]
10488 LAMBDA_EXPR is the current representation of the lambda expression. */
10491 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10493 /* 5.1.1.4 of the standard says:
10494 If a lambda-expression does not include a lambda-declarator, it is as if
10495 the lambda-declarator were ().
10496 This means an empty parameter list, no attributes, and no exception
10498 tree param_list
= void_list_node
;
10499 tree attributes
= NULL_TREE
;
10500 tree exception_spec
= NULL_TREE
;
10501 tree template_param_list
= NULL_TREE
;
10502 tree tx_qual
= NULL_TREE
;
10503 tree return_type
= NULL_TREE
;
10504 cp_decl_specifier_seq lambda_specs
;
10505 clear_decl_specs (&lambda_specs
);
10507 /* The template-parameter-list is optional, but must begin with
10508 an opening angle if present. */
10509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10511 if (cxx_dialect
< cxx14
)
10512 pedwarn (parser
->lexer
->next_token
->location
, 0,
10513 "lambda templates are only available with "
10514 "-std=c++14 or -std=gnu++14");
10515 else if (cxx_dialect
< cxx2a
)
10516 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10517 "lambda templates are only available with "
10518 "-std=c++2a or -std=gnu++2a");
10520 cp_lexer_consume_token (parser
->lexer
);
10522 template_param_list
= cp_parser_template_parameter_list (parser
);
10524 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10526 /* We just processed one more parameter list. */
10527 ++parser
->num_template_parameter_lists
;
10530 /* The parameter-declaration-clause is optional (unless
10531 template-parameter-list was given), but must begin with an
10532 opening parenthesis if present. */
10533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10535 matching_parens parens
;
10536 parens
.consume_open (parser
);
10538 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10540 /* Parse parameters. */
10541 param_list
= cp_parser_parameter_declaration_clause (parser
);
10543 /* Default arguments shall not be specified in the
10544 parameter-declaration-clause of a lambda-declarator. */
10545 if (cxx_dialect
< cxx14
)
10546 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10547 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10548 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10549 "default argument specified for lambda parameter");
10551 parens
.require_close (parser
);
10553 attributes
= cp_parser_attributes_opt (parser
);
10555 /* In the decl-specifier-seq of the lambda-declarator, each
10556 decl-specifier shall either be mutable or constexpr. */
10557 int declares_class_or_enum
;
10558 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10559 cp_parser_decl_specifier_seq (parser
,
10560 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10561 &lambda_specs
, &declares_class_or_enum
);
10562 if (lambda_specs
.storage_class
== sc_mutable
)
10564 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10565 if (lambda_specs
.conflicting_specifiers_p
)
10566 error_at (lambda_specs
.locations
[ds_storage_class
],
10567 "duplicate %<mutable%>");
10570 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10572 /* Parse optional exception specification. */
10573 exception_spec
= cp_parser_exception_specification_opt (parser
);
10575 /* Parse optional trailing return type. */
10576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10578 cp_lexer_consume_token (parser
->lexer
);
10579 return_type
= cp_parser_trailing_type_id (parser
);
10582 /* The function parameters must be in scope all the way until after the
10583 trailing-return-type in case of decltype. */
10584 pop_bindings_and_leave_scope ();
10586 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10587 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10589 /* Create the function call operator.
10591 Messing with declarators like this is no uglier than building up the
10592 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10595 cp_decl_specifier_seq return_type_specs
;
10596 cp_declarator
* declarator
;
10601 clear_decl_specs (&return_type_specs
);
10603 return_type_specs
.type
= return_type
;
10605 /* Maybe we will deduce the return type later. */
10606 return_type_specs
.type
= make_auto ();
10608 if (lambda_specs
.locations
[ds_constexpr
])
10610 if (cxx_dialect
>= cxx17
)
10611 return_type_specs
.locations
[ds_constexpr
]
10612 = lambda_specs
.locations
[ds_constexpr
];
10614 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10615 "lambda only available with -std=c++17 or -std=gnu++17");
10618 p
= obstack_alloc (&declarator_obstack
, 0);
10620 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10622 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10623 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10624 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10625 VIRT_SPEC_UNSPECIFIED
,
10629 /*late_return_type=*/NULL_TREE
,
10630 /*requires_clause*/NULL_TREE
);
10631 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10633 fco
= grokmethod (&return_type_specs
,
10636 if (fco
!= error_mark_node
)
10638 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10639 DECL_ARTIFICIAL (fco
) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10643 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10645 if (template_param_list
)
10647 fco
= finish_member_template_decl (fco
);
10648 finish_template_decl (template_param_list
);
10649 --parser
->num_template_parameter_lists
;
10651 else if (parser
->fully_implicit_function_template_p
)
10652 fco
= finish_fully_implicit_template (parser
, fco
);
10654 finish_member_declaration (fco
);
10656 obstack_free (&declarator_obstack
, p
);
10658 return (fco
!= error_mark_node
);
10662 /* Parse the body of a lambda expression, which is simply
10666 but which requires special handling.
10667 LAMBDA_EXPR is the current representation of the lambda expression. */
10670 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10672 bool nested
= (current_function_decl
!= NULL_TREE
);
10673 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10674 bool in_function_body
= parser
->in_function_body
;
10676 push_function_context ();
10678 /* Still increment function_depth so that we don't GC in the
10679 middle of an expression. */
10681 vec
<tree
> omp_privatization_save
;
10682 save_omp_privatization_clauses (omp_privatization_save
);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser
->local_variables_forbidden_p
= false;
10685 parser
->in_function_body
= true;
10687 /* Finish the function call operator
10689 + late_parsing_for_member
10690 + function_definition_after_declarator
10691 + ctor_initializer_opt_and_function_body */
10693 local_specialization_stack
s (lss_copy
);
10695 tree fco
= lambda_function (lambda_expr
);
10696 tree body
= start_lambda_function (fco
, lambda_expr
);
10698 tree compound_stmt
;
10700 matching_braces braces
;
10701 if (!braces
.require_open (parser
))
10704 compound_stmt
= begin_compound_stmt (0);
10706 /* 5.1.1.4 of the standard says:
10707 If a lambda-expression does not include a trailing-return-type, it
10708 is as if the trailing-return-type denotes the following type:
10709 * if the compound-statement is of the form
10710 { return attribute-specifier [opt] expression ; }
10711 the type of the returned expression after lvalue-to-rvalue
10712 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10713 (_conv.array_ 4.2), and function-to-pointer conversion
10715 * otherwise, void. */
10717 /* In a lambda that has neither a lambda-return-type-clause
10718 nor a deducible form, errors should be reported for return statements
10719 in the body. Since we used void as the placeholder return type, parsing
10720 the body as usual will give such desired behavior. */
10721 if (is_auto (TREE_TYPE (TREE_TYPE (fco
)))
10722 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10723 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10725 tree expr
= NULL_TREE
;
10726 cp_id_kind idk
= CP_ID_KIND_NONE
;
10728 /* Parse tentatively in case there's more after the initial return
10730 cp_parser_parse_tentatively (parser
);
10732 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10734 expr
= cp_parser_expression (parser
, &idk
);
10736 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10737 braces
.require_close (parser
);
10739 if (cp_parser_parse_definitely (parser
))
10741 if (!processing_template_decl
)
10743 tree type
= lambda_return_type (expr
);
10744 apply_deduced_return_type (fco
, type
);
10745 if (type
== error_mark_node
)
10746 expr
= error_mark_node
;
10749 /* Will get error here if type not deduced yet. */
10750 finish_return_stmt (expr
);
10758 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10759 cp_parser_label_declaration (parser
);
10760 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10761 braces
.require_close (parser
);
10764 finish_compound_stmt (compound_stmt
);
10767 finish_lambda_function (body
);
10770 restore_omp_privatization_clauses (omp_privatization_save
);
10771 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10772 parser
->in_function_body
= in_function_body
;
10774 pop_function_context();
10779 /* Statements [gram.stmt.stmt] */
10781 /* Parse a statement.
10785 expression-statement
10787 selection-statement
10788 iteration-statement
10790 declaration-statement
10797 attribute-specifier-seq (opt) expression-statement
10798 attribute-specifier-seq (opt) compound-statement
10799 attribute-specifier-seq (opt) selection-statement
10800 attribute-specifier-seq (opt) iteration-statement
10801 attribute-specifier-seq (opt) jump-statement
10802 declaration-statement
10803 attribute-specifier-seq (opt) try-block
10806 expression-statement
10814 IN_COMPOUND is true when the statement is nested inside a
10815 cp_parser_compound_statement; this matters for certain pragmas.
10817 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10818 is a (possibly labeled) if statement which is not enclosed in braces
10819 and has an else clause. This is used to implement -Wparentheses.
10821 CHAIN is a vector of if-else-if conditions. */
10824 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10825 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10826 location_t
*loc_after_labels
)
10828 tree statement
, std_attrs
= NULL_TREE
;
10830 location_t statement_location
, attrs_location
;
10835 /* There is no statement yet. */
10836 statement
= NULL_TREE
;
10838 saved_token_sentinel
saved_tokens (parser
->lexer
);
10839 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10840 if (c_dialect_objc ())
10841 /* In obj-c++, seeing '[[' might be the either the beginning of
10842 c++11 attributes, or a nested objc-message-expression. So
10843 let's parse the c++11 attributes tentatively. */
10844 cp_parser_parse_tentatively (parser
);
10845 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10846 if (c_dialect_objc ())
10848 if (!cp_parser_parse_definitely (parser
))
10849 std_attrs
= NULL_TREE
;
10852 /* Peek at the next token. */
10853 token
= cp_lexer_peek_token (parser
->lexer
);
10854 /* Remember the location of the first token in the statement. */
10855 statement_location
= token
->location
;
10856 /* If this is a keyword, then that will often determine what kind of
10857 statement we have. */
10858 if (token
->type
== CPP_KEYWORD
)
10860 enum rid keyword
= token
->keyword
;
10866 /* Looks like a labeled-statement with a case label.
10867 Parse the label, and then use tail recursion to parse
10869 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10870 in_compound
= false;
10875 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10881 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10885 if (!flag_cilkplus
)
10887 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10888 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10889 cp_lexer_consume_token (parser
->lexer
);
10890 statement
= error_mark_node
;
10893 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10900 statement
= cp_parser_jump_statement (parser
);
10903 case RID_CILK_SYNC
:
10904 cp_lexer_consume_token (parser
->lexer
);
10907 tree sync_expr
= build_cilk_sync ();
10908 SET_EXPR_LOCATION (sync_expr
,
10910 statement
= finish_expr_stmt (sync_expr
);
10914 error_at (token
->location
, "-fcilkplus must be enabled to use"
10915 " %<_Cilk_sync%>");
10916 statement
= error_mark_node
;
10918 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10921 /* Objective-C++ exception-handling constructs. */
10924 case RID_AT_FINALLY
:
10925 case RID_AT_SYNCHRONIZED
:
10927 statement
= cp_parser_objc_statement (parser
);
10931 statement
= cp_parser_try_block (parser
);
10934 case RID_NAMESPACE
:
10935 /* This must be a namespace alias definition. */
10936 cp_parser_declaration_statement (parser
);
10939 case RID_TRANSACTION_ATOMIC
:
10940 case RID_TRANSACTION_RELAXED
:
10941 case RID_SYNCHRONIZED
:
10942 case RID_ATOMIC_NOEXCEPT
:
10943 case RID_ATOMIC_CANCEL
:
10944 statement
= cp_parser_transaction (parser
, token
);
10946 case RID_TRANSACTION_CANCEL
:
10947 statement
= cp_parser_transaction_cancel (parser
);
10951 /* It might be a keyword like `int' that can start a
10952 declaration-statement. */
10956 else if (token
->type
== CPP_NAME
)
10958 /* If the next token is a `:', then we are looking at a
10959 labeled-statement. */
10960 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10961 if (token
->type
== CPP_COLON
)
10963 /* Looks like a labeled-statement with an ordinary label.
10964 Parse the label, and then use tail recursion to parse
10967 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10968 in_compound
= false;
10972 /* Anything that starts with a `{' must be a compound-statement. */
10973 else if (token
->type
== CPP_OPEN_BRACE
)
10974 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10975 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10976 a statement all its own. */
10977 else if (token
->type
== CPP_PRAGMA
)
10979 /* Only certain OpenMP pragmas are attached to statements, and thus
10980 are considered statements themselves. All others are not. In
10981 the context of a compound, accept the pragma as a "statement" and
10982 return so that we can check for a close brace. Otherwise we
10983 require a real statement and must go back and read one. */
10985 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10986 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10990 else if (token
->type
== CPP_EOF
)
10992 cp_parser_error (parser
, "expected statement");
10996 /* Everything else must be a declaration-statement or an
10997 expression-statement. Try for the declaration-statement
10998 first, unless we are looking at a `;', in which case we know that
10999 we have an expression-statement. */
11002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11004 if (std_attrs
!= NULL_TREE
)
11006 /* Attributes should be parsed as part of the the
11007 declaration, so let's un-parse them. */
11008 saved_tokens
.rollback();
11009 std_attrs
= NULL_TREE
;
11012 cp_parser_parse_tentatively (parser
);
11013 /* Try to parse the declaration-statement. */
11014 cp_parser_declaration_statement (parser
);
11015 /* If that worked, we're done. */
11016 if (cp_parser_parse_definitely (parser
))
11019 /* All preceding labels have been parsed at this point. */
11020 if (loc_after_labels
!= NULL
)
11021 *loc_after_labels
= statement_location
;
11023 /* Look for an expression-statement instead. */
11024 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11026 /* Handle [[fallthrough]];. */
11027 if (attribute_fallthrough_p (std_attrs
))
11029 /* The next token after the fallthrough attribute is ';'. */
11030 if (statement
== NULL_TREE
)
11032 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11033 statement
= build_call_expr_internal_loc (statement_location
,
11035 void_type_node
, 0);
11036 finish_expr_stmt (statement
);
11039 warning_at (statement_location
, OPT_Wattributes
,
11040 "%<fallthrough%> attribute not followed by %<;%>");
11041 std_attrs
= NULL_TREE
;
11045 /* Set the line number for the statement. */
11046 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11047 SET_EXPR_LOCATION (statement
, statement_location
);
11049 /* Allow "[[fallthrough]];", but warn otherwise. */
11050 if (std_attrs
!= NULL_TREE
)
11051 warning_at (attrs_location
,
11053 "attributes at the beginning of statement are ignored");
11056 /* Parse the label for a labeled-statement, i.e.
11059 case constant-expression :
11063 case constant-expression ... constant-expression : statement
11065 When a label is parsed without errors, the label is added to the
11066 parse tree by the finish_* functions, so this function doesn't
11067 have to return the label. */
11070 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11073 tree label
= NULL_TREE
;
11074 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11076 /* The next token should be an identifier. */
11077 token
= cp_lexer_peek_token (parser
->lexer
);
11078 if (token
->type
!= CPP_NAME
11079 && token
->type
!= CPP_KEYWORD
)
11081 cp_parser_error (parser
, "expected labeled-statement");
11085 /* Remember whether this case or a user-defined label is allowed to fall
11087 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11089 parser
->colon_corrects_to_scope_p
= false;
11090 switch (token
->keyword
)
11094 tree expr
, expr_hi
;
11095 cp_token
*ellipsis
;
11097 /* Consume the `case' token. */
11098 cp_lexer_consume_token (parser
->lexer
);
11099 /* Parse the constant-expression. */
11100 expr
= cp_parser_constant_expression (parser
);
11101 if (check_for_bare_parameter_packs (expr
))
11102 expr
= error_mark_node
;
11104 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11105 if (ellipsis
->type
== CPP_ELLIPSIS
)
11107 /* Consume the `...' token. */
11108 cp_lexer_consume_token (parser
->lexer
);
11109 expr_hi
= cp_parser_constant_expression (parser
);
11110 if (check_for_bare_parameter_packs (expr_hi
))
11111 expr_hi
= error_mark_node
;
11113 /* We don't need to emit warnings here, as the common code
11114 will do this for us. */
11117 expr_hi
= NULL_TREE
;
11119 if (parser
->in_switch_statement_p
)
11121 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11122 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11123 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11126 error_at (token
->location
,
11127 "case label %qE not within a switch statement",
11133 /* Consume the `default' token. */
11134 cp_lexer_consume_token (parser
->lexer
);
11136 if (parser
->in_switch_statement_p
)
11138 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11139 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11140 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11143 error_at (token
->location
, "case label not within a switch statement");
11147 /* Anything else must be an ordinary label. */
11148 label
= finish_label_stmt (cp_parser_identifier (parser
));
11149 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11150 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11154 /* Require the `:' token. */
11155 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11157 /* An ordinary label may optionally be followed by attributes.
11158 However, this is only permitted if the attributes are then
11159 followed by a semicolon. This is because, for backward
11160 compatibility, when parsing
11161 lab: __attribute__ ((unused)) int i;
11162 we want the attribute to attach to "i", not "lab". */
11163 if (label
!= NULL_TREE
11164 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11167 cp_parser_parse_tentatively (parser
);
11168 attrs
= cp_parser_gnu_attributes_opt (parser
);
11169 if (attrs
== NULL_TREE
11170 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11171 cp_parser_abort_tentative_parse (parser
);
11172 else if (!cp_parser_parse_definitely (parser
))
11175 attributes
= chainon (attributes
, attrs
);
11178 if (attributes
!= NULL_TREE
)
11179 cplus_decl_attributes (&label
, attributes
, 0);
11181 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11184 /* Parse an expression-statement.
11186 expression-statement:
11189 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11190 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11191 indicates whether this expression-statement is part of an
11192 expression statement. */
11195 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11197 tree statement
= NULL_TREE
;
11198 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11199 location_t loc
= token
->location
;
11201 /* There might be attribute fallthrough. */
11202 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11204 /* If the next token is a ';', then there is no expression
11206 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11208 statement
= cp_parser_expression (parser
);
11209 if (statement
== error_mark_node
11210 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11212 cp_parser_skip_to_end_of_block_or_statement (parser
);
11213 return error_mark_node
;
11217 /* Handle [[fallthrough]];. */
11218 if (attribute_fallthrough_p (attr
))
11220 /* The next token after the fallthrough attribute is ';'. */
11221 if (statement
== NULL_TREE
)
11222 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11223 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11224 void_type_node
, 0);
11226 warning_at (loc
, OPT_Wattributes
,
11227 "%<fallthrough%> attribute not followed by %<;%>");
11231 /* Allow "[[fallthrough]];", but warn otherwise. */
11232 if (attr
!= NULL_TREE
)
11233 warning_at (loc
, OPT_Wattributes
,
11234 "attributes at the beginning of statement are ignored");
11236 /* Give a helpful message for "A<T>::type t;" and the like. */
11237 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11238 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11240 if (TREE_CODE (statement
) == SCOPE_REF
)
11241 error_at (token
->location
, "need %<typename%> before %qE because "
11242 "%qT is a dependent scope",
11243 statement
, TREE_OPERAND (statement
, 0));
11244 else if (is_overloaded_fn (statement
)
11245 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11248 tree fn
= get_first_fn (statement
);
11249 error_at (token
->location
,
11250 "%<%T::%D%> names the constructor, not the type",
11251 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11255 /* Consume the final `;'. */
11256 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11258 if (in_statement_expr
11259 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11260 /* This is the final expression statement of a statement
11262 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11263 else if (statement
)
11264 statement
= finish_expr_stmt (statement
);
11269 /* Parse a compound-statement.
11271 compound-statement:
11272 { statement-seq [opt] }
11276 compound-statement:
11277 { label-declaration-seq [opt] statement-seq [opt] }
11279 label-declaration-seq:
11281 label-declaration-seq label-declaration
11283 Returns a tree representing the statement. */
11286 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11287 int bcs_flags
, bool function_body
)
11289 tree compound_stmt
;
11290 matching_braces braces
;
11292 /* Consume the `{'. */
11293 if (!braces
.require_open (parser
))
11294 return error_mark_node
;
11295 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11296 && !function_body
&& cxx_dialect
< cxx14
)
11297 pedwarn (input_location
, OPT_Wpedantic
,
11298 "compound-statement in %<constexpr%> function");
11299 /* Begin the compound-statement. */
11300 compound_stmt
= begin_compound_stmt (bcs_flags
);
11301 /* If the next keyword is `__label__' we have a label declaration. */
11302 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11303 cp_parser_label_declaration (parser
);
11304 /* Parse an (optional) statement-seq. */
11305 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11306 /* Finish the compound-statement. */
11307 finish_compound_stmt (compound_stmt
);
11308 /* Consume the `}'. */
11309 braces
.require_close (parser
);
11311 return compound_stmt
;
11314 /* Parse an (optional) statement-seq.
11318 statement-seq [opt] statement */
11321 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11323 /* Scan statements until there aren't any more. */
11326 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11328 /* If we are looking at a `}', then we have run out of
11329 statements; the same is true if we have reached the end
11330 of file, or have stumbled upon a stray '@end'. */
11331 if (token
->type
== CPP_CLOSE_BRACE
11332 || token
->type
== CPP_EOF
11333 || token
->type
== CPP_PRAGMA_EOL
11334 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11337 /* If we are in a compound statement and find 'else' then
11338 something went wrong. */
11339 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11341 if (parser
->in_statement
& IN_IF_STMT
)
11345 token
= cp_lexer_consume_token (parser
->lexer
);
11346 error_at (token
->location
, "%<else%> without a previous %<if%>");
11350 /* Parse the statement. */
11351 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11355 /* Return true if we're looking at (init; cond), false otherwise. */
11358 cp_parser_init_statement_p (cp_parser
*parser
)
11360 /* Save tokens so that we can put them back. */
11361 cp_lexer_save_tokens (parser
->lexer
);
11363 /* Look for ';' that is not nested in () or {}. */
11364 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11365 /*recovering=*/false,
11367 /*consume_paren=*/false);
11369 /* Roll back the tokens we skipped. */
11370 cp_lexer_rollback_tokens (parser
->lexer
);
11375 /* Parse a selection-statement.
11377 selection-statement:
11378 if ( init-statement [opt] condition ) statement
11379 if ( init-statement [opt] condition ) statement else statement
11380 switch ( init-statement [opt] condition ) statement
11382 Returns the new IF_STMT or SWITCH_STMT.
11384 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11385 is a (possibly labeled) if statement which is not enclosed in
11386 braces and has an else clause. This is used to implement
11389 CHAIN is a vector of if-else-if conditions. This is used to implement
11390 -Wduplicated-cond. */
11393 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11398 token_indent_info guard_tinfo
;
11403 /* Peek at the next token. */
11404 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11405 guard_tinfo
= get_token_indent_info (token
);
11407 /* See what kind of keyword it is. */
11408 keyword
= token
->keyword
;
11418 if (keyword
== RID_IF
11419 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11423 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11424 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11425 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11426 "with -std=c++17 or -std=gnu++17");
11429 /* Look for the `('. */
11430 matching_parens parens
;
11431 if (!parens
.require_open (parser
))
11433 cp_parser_skip_to_end_of_statement (parser
);
11434 return error_mark_node
;
11437 /* Begin the selection-statement. */
11438 if (keyword
== RID_IF
)
11440 statement
= begin_if_stmt ();
11441 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11444 statement
= begin_switch_stmt ();
11446 /* Parse the optional init-statement. */
11447 if (cp_parser_init_statement_p (parser
))
11450 if (cxx_dialect
< cxx17
)
11451 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11452 "init-statement in selection statements only available "
11453 "with -std=c++17 or -std=gnu++17");
11454 cp_parser_init_statement (parser
, &decl
);
11457 /* Parse the condition. */
11458 condition
= cp_parser_condition (parser
);
11459 /* Look for the `)'. */
11460 if (!parens
.require_close (parser
))
11461 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11462 /*consume_paren=*/true);
11464 if (keyword
== RID_IF
)
11467 unsigned char in_statement
;
11469 /* Add the condition. */
11470 condition
= finish_if_stmt_cond (condition
, statement
);
11472 if (warn_duplicated_cond
)
11473 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11476 /* Parse the then-clause. */
11477 in_statement
= parser
->in_statement
;
11478 parser
->in_statement
|= IN_IF_STMT
;
11480 /* Outside a template, the non-selected branch of a constexpr
11481 if is a 'discarded statement', i.e. unevaluated. */
11482 bool was_discarded
= in_discarded_stmt
;
11483 bool discard_then
= (cx
&& !processing_template_decl
11484 && integer_zerop (condition
));
11487 in_discarded_stmt
= true;
11488 ++c_inhibit_evaluation_warnings
;
11491 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11494 parser
->in_statement
= in_statement
;
11496 finish_then_clause (statement
);
11500 THEN_CLAUSE (statement
) = NULL_TREE
;
11501 in_discarded_stmt
= was_discarded
;
11502 --c_inhibit_evaluation_warnings
;
11505 /* If the next token is `else', parse the else-clause. */
11506 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11509 bool discard_else
= (cx
&& !processing_template_decl
11510 && integer_nonzerop (condition
));
11513 in_discarded_stmt
= true;
11514 ++c_inhibit_evaluation_warnings
;
11518 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11519 /* Consume the `else' keyword. */
11520 cp_lexer_consume_token (parser
->lexer
);
11521 if (warn_duplicated_cond
)
11523 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11527 /* We've got "if (COND) else if (COND2)". Start
11528 the condition chain and add COND as the first
11530 chain
= new vec
<tree
> ();
11531 if (!CONSTANT_CLASS_P (condition
)
11532 && !TREE_SIDE_EFFECTS (condition
))
11534 /* Wrap it in a NOP_EXPR so that we can set the
11535 location of the condition. */
11536 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11538 SET_EXPR_LOCATION (e
, token
->location
);
11539 chain
->safe_push (e
);
11542 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11545 /* This is if-else without subsequent if. Zap the
11546 condition chain; we would have already warned at
11552 begin_else_clause (statement
);
11553 /* Parse the else-clause. */
11554 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11555 guard_tinfo
, chain
);
11557 finish_else_clause (statement
);
11559 /* If we are currently parsing a then-clause, then
11560 IF_P will not be NULL. We set it to true to
11561 indicate that this if statement has an else clause.
11562 This may trigger the Wparentheses warning below
11563 when we get back up to the parent if statement. */
11569 ELSE_CLAUSE (statement
) = NULL_TREE
;
11570 in_discarded_stmt
= was_discarded
;
11571 --c_inhibit_evaluation_warnings
;
11576 /* This if statement does not have an else clause. If
11577 NESTED_IF is true, then the then-clause has an if
11578 statement which does have an else clause. We warn
11579 about the potential ambiguity. */
11581 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11582 "suggest explicit braces to avoid ambiguous"
11584 if (warn_duplicated_cond
)
11586 /* We don't need the condition chain anymore. */
11592 /* Now we're all done with the if-statement. */
11593 finish_if_stmt (statement
);
11597 bool in_switch_statement_p
;
11598 unsigned char in_statement
;
11600 /* Add the condition. */
11601 finish_switch_cond (condition
, statement
);
11603 /* Parse the body of the switch-statement. */
11604 in_switch_statement_p
= parser
->in_switch_statement_p
;
11605 in_statement
= parser
->in_statement
;
11606 parser
->in_switch_statement_p
= true;
11607 parser
->in_statement
|= IN_SWITCH_STMT
;
11608 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11610 parser
->in_switch_statement_p
= in_switch_statement_p
;
11611 parser
->in_statement
= in_statement
;
11613 /* Now we're all done with the switch-statement. */
11614 finish_switch_stmt (statement
);
11622 cp_parser_error (parser
, "expected selection-statement");
11623 return error_mark_node
;
11627 /* Parse a condition.
11631 type-specifier-seq declarator = initializer-clause
11632 type-specifier-seq declarator braced-init-list
11637 type-specifier-seq declarator asm-specification [opt]
11638 attributes [opt] = assignment-expression
11640 Returns the expression that should be tested. */
11643 cp_parser_condition (cp_parser
* parser
)
11645 cp_decl_specifier_seq type_specifiers
;
11646 const char *saved_message
;
11647 int declares_class_or_enum
;
11649 /* Try the declaration first. */
11650 cp_parser_parse_tentatively (parser
);
11651 /* New types are not allowed in the type-specifier-seq for a
11653 saved_message
= parser
->type_definition_forbidden_message
;
11654 parser
->type_definition_forbidden_message
11655 = G_("types may not be defined in conditions");
11656 /* Parse the type-specifier-seq. */
11657 cp_parser_decl_specifier_seq (parser
,
11658 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11660 &declares_class_or_enum
);
11661 /* Restore the saved message. */
11662 parser
->type_definition_forbidden_message
= saved_message
;
11663 /* If all is well, we might be looking at a declaration. */
11664 if (!cp_parser_error_occurred (parser
))
11667 tree asm_specification
;
11669 cp_declarator
*declarator
;
11670 tree initializer
= NULL_TREE
;
11672 /* Parse the declarator. */
11673 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11674 /*ctor_dtor_or_conv_p=*/NULL
,
11675 /*parenthesized_p=*/NULL
,
11676 /*member_p=*/false,
11677 /*friend_p=*/false);
11678 /* Parse the attributes. */
11679 attributes
= cp_parser_attributes_opt (parser
);
11680 /* Parse the asm-specification. */
11681 asm_specification
= cp_parser_asm_specification_opt (parser
);
11682 /* If the next token is not an `=' or '{', then we might still be
11683 looking at an expression. For example:
11687 looks like a decl-specifier-seq and a declarator -- but then
11688 there is no `=', so this is an expression. */
11689 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11690 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11691 cp_parser_simulate_error (parser
);
11693 /* If we did see an `=' or '{', then we are looking at a declaration
11695 if (cp_parser_parse_definitely (parser
))
11698 bool non_constant_p
;
11699 int flags
= LOOKUP_ONLYCONVERTING
;
11701 /* Create the declaration. */
11702 decl
= start_decl (declarator
, &type_specifiers
,
11703 /*initialized_p=*/true,
11704 attributes
, /*prefix_attributes=*/NULL_TREE
,
11707 /* Parse the initializer. */
11708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11710 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11711 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11716 /* Consume the `='. */
11717 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11718 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11720 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11721 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11723 /* Process the initializer. */
11724 cp_finish_decl (decl
,
11725 initializer
, !non_constant_p
,
11730 pop_scope (pushed_scope
);
11732 return convert_from_reference (decl
);
11735 /* If we didn't even get past the declarator successfully, we are
11736 definitely not looking at a declaration. */
11738 cp_parser_abort_tentative_parse (parser
);
11740 /* Otherwise, we are looking at an expression. */
11741 return cp_parser_expression (parser
);
11744 /* Parses a for-statement or range-for-statement until the closing ')',
11748 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11750 tree init
, scope
, decl
;
11753 /* Begin the for-statement. */
11754 scope
= begin_for_scope (&init
);
11756 /* Parse the initialization. */
11757 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11760 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11762 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11766 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11768 /* Normal for loop */
11769 tree condition
= NULL_TREE
;
11770 tree expression
= NULL_TREE
;
11773 stmt
= begin_for_stmt (scope
, init
);
11774 /* The init-statement has already been parsed in
11775 cp_parser_init_statement, so no work is needed here. */
11776 finish_init_stmt (stmt
);
11778 /* If there's a condition, process it. */
11779 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11780 condition
= cp_parser_condition (parser
);
11783 cp_parser_error (parser
, "missing loop condition in loop with "
11784 "%<GCC ivdep%> pragma");
11785 condition
= error_mark_node
;
11787 finish_for_cond (condition
, stmt
, ivdep
);
11788 /* Look for the `;'. */
11789 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11791 /* If there's an expression, process it. */
11792 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11793 expression
= cp_parser_expression (parser
);
11794 finish_for_expr (expression
, stmt
);
11799 /* Tries to parse a range-based for-statement:
11802 decl-specifier-seq declarator : expression
11804 The decl-specifier-seq declarator and the `:' are already parsed by
11805 cp_parser_init_statement. If processing_template_decl it returns a
11806 newly created RANGE_FOR_STMT; if not, it is converted to a
11807 regular FOR_STMT. */
11810 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11813 tree stmt
, range_expr
;
11814 auto_vec
<cxx_binding
*, 16> bindings
;
11815 auto_vec
<tree
, 16> names
;
11816 tree decomp_first_name
= NULL_TREE
;
11817 unsigned int decomp_cnt
= 0;
11819 /* Get the range declaration momentarily out of the way so that
11820 the range expression doesn't clash with it. */
11821 if (range_decl
!= error_mark_node
)
11823 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11825 tree v
= DECL_VALUE_EXPR (range_decl
);
11826 /* For decomposition declaration get all of the corresponding
11827 declarations out of the way. */
11828 if (TREE_CODE (v
) == ARRAY_REF
11829 && VAR_P (TREE_OPERAND (v
, 0))
11830 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11832 tree d
= range_decl
;
11833 range_decl
= TREE_OPERAND (v
, 0);
11834 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11835 decomp_first_name
= d
;
11836 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11838 tree name
= DECL_NAME (d
);
11839 names
.safe_push (name
);
11840 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11841 IDENTIFIER_BINDING (name
)
11842 = IDENTIFIER_BINDING (name
)->previous
;
11846 if (names
.is_empty ())
11848 tree name
= DECL_NAME (range_decl
);
11849 names
.safe_push (name
);
11850 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11851 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11857 bool expr_non_constant_p
;
11858 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11861 range_expr
= cp_parser_expression (parser
);
11863 /* Put the range declaration(s) back into scope. */
11864 for (unsigned int i
= 0; i
< names
.length (); i
++)
11866 cxx_binding
*binding
= bindings
[i
];
11867 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11868 IDENTIFIER_BINDING (names
[i
]) = binding
;
11871 /* If in template, STMT is converted to a normal for-statement
11872 at instantiation. If not, it is done just ahead. */
11873 if (processing_template_decl
)
11875 if (check_for_bare_parameter_packs (range_expr
))
11876 range_expr
= error_mark_node
;
11877 stmt
= begin_range_for_stmt (scope
, init
);
11879 RANGE_FOR_IVDEP (stmt
) = 1;
11880 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11881 if (!type_dependent_expression_p (range_expr
)
11882 /* do_auto_deduction doesn't mess with template init-lists. */
11883 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11884 do_range_for_auto_deduction (range_decl
, range_expr
);
11888 stmt
= begin_for_stmt (scope
, init
);
11889 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11890 decomp_first_name
, decomp_cnt
, ivdep
);
11895 /* Subroutine of cp_convert_range_for: given the initializer expression,
11896 builds up the range temporary. */
11899 build_range_temp (tree range_expr
)
11901 tree range_type
, range_temp
;
11903 /* Find out the type deduced by the declaration
11904 `auto &&__range = range_expr'. */
11905 range_type
= cp_build_reference_type (make_auto (), true);
11906 range_type
= do_auto_deduction (range_type
, range_expr
,
11907 type_uses_auto (range_type
));
11909 /* Create the __range variable. */
11910 range_temp
= build_decl (input_location
, VAR_DECL
,
11911 get_identifier ("__for_range"), range_type
);
11912 TREE_USED (range_temp
) = 1;
11913 DECL_ARTIFICIAL (range_temp
) = 1;
11918 /* Used by cp_parser_range_for in template context: we aren't going to
11919 do a full conversion yet, but we still need to resolve auto in the
11920 type of the for-range-declaration if present. This is basically
11921 a shortcut version of cp_convert_range_for. */
11924 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11926 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11929 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11930 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11931 iter_type
= (cp_parser_perform_range_for_lookup
11932 (range_temp
, &begin_dummy
, &end_dummy
));
11935 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11937 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11939 tf_warning_or_error
);
11940 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11941 iter_decl
, auto_node
);
11946 /* Converts a range-based for-statement into a normal
11947 for-statement, as per the definition.
11949 for (RANGE_DECL : RANGE_EXPR)
11952 should be equivalent to:
11955 auto &&__range = RANGE_EXPR;
11956 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11960 RANGE_DECL = *__begin;
11965 If RANGE_EXPR is an array:
11966 BEGIN_EXPR = __range
11967 END_EXPR = __range + ARRAY_SIZE(__range)
11968 Else if RANGE_EXPR has a member 'begin' or 'end':
11969 BEGIN_EXPR = __range.begin()
11970 END_EXPR = __range.end()
11972 BEGIN_EXPR = begin(__range)
11973 END_EXPR = end(__range);
11975 If __range has a member 'begin' but not 'end', or vice versa, we must
11976 still use the second alternative (it will surely fail, however).
11977 When calling begin()/end() in the third alternative we must use
11978 argument dependent lookup, but always considering 'std' as an associated
11982 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11983 tree decomp_first_name
, unsigned int decomp_cnt
,
11987 tree iter_type
, begin_expr
, end_expr
;
11988 tree condition
, expression
;
11990 range_expr
= mark_lvalue_use (range_expr
);
11992 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11993 /* If an error happened previously do nothing or else a lot of
11994 unhelpful errors would be issued. */
11995 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12000 if (VAR_P (range_expr
)
12001 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12002 /* Can't bind a reference to an array of runtime bound. */
12003 range_temp
= range_expr
;
12006 range_temp
= build_range_temp (range_expr
);
12007 pushdecl (range_temp
);
12008 cp_finish_decl (range_temp
, range_expr
,
12009 /*is_constant_init*/false, NULL_TREE
,
12010 LOOKUP_ONLYCONVERTING
);
12011 range_temp
= convert_from_reference (range_temp
);
12013 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12014 &begin_expr
, &end_expr
);
12017 /* The new for initialization statement. */
12018 begin
= build_decl (input_location
, VAR_DECL
,
12019 get_identifier ("__for_begin"), iter_type
);
12020 TREE_USED (begin
) = 1;
12021 DECL_ARTIFICIAL (begin
) = 1;
12023 cp_finish_decl (begin
, begin_expr
,
12024 /*is_constant_init*/false, NULL_TREE
,
12025 LOOKUP_ONLYCONVERTING
);
12027 if (cxx_dialect
>= cxx17
)
12028 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12029 end
= build_decl (input_location
, VAR_DECL
,
12030 get_identifier ("__for_end"), iter_type
);
12031 TREE_USED (end
) = 1;
12032 DECL_ARTIFICIAL (end
) = 1;
12034 cp_finish_decl (end
, end_expr
,
12035 /*is_constant_init*/false, NULL_TREE
,
12036 LOOKUP_ONLYCONVERTING
);
12038 finish_init_stmt (statement
);
12040 /* The new for condition. */
12041 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12044 NULL
, tf_warning_or_error
);
12045 finish_for_cond (condition
, statement
, ivdep
);
12047 /* The new increment expression. */
12048 expression
= finish_unary_op_expr (input_location
,
12049 PREINCREMENT_EXPR
, begin
,
12050 tf_warning_or_error
);
12051 finish_for_expr (expression
, statement
);
12053 /* The declaration is initialized with *__begin inside the loop body. */
12054 cp_finish_decl (range_decl
,
12055 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12056 tf_warning_or_error
),
12057 /*is_constant_init*/false, NULL_TREE
,
12058 LOOKUP_ONLYCONVERTING
);
12059 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12060 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12065 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12066 We need to solve both at the same time because the method used
12067 depends on the existence of members begin or end.
12068 Returns the type deduced for the iterator expression. */
12071 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12073 if (error_operand_p (range
))
12075 *begin
= *end
= error_mark_node
;
12076 return error_mark_node
;
12079 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12081 error ("range-based %<for%> expression of type %qT "
12082 "has incomplete type", TREE_TYPE (range
));
12083 *begin
= *end
= error_mark_node
;
12084 return error_mark_node
;
12086 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12088 /* If RANGE is an array, we will use pointer arithmetic. */
12089 *begin
= decay_conversion (range
, tf_warning_or_error
);
12090 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12092 array_type_nelts_top (TREE_TYPE (range
)),
12094 return TREE_TYPE (*begin
);
12098 /* If it is not an array, we must do a bit of magic. */
12099 tree id_begin
, id_end
;
12100 tree member_begin
, member_end
;
12102 *begin
= *end
= error_mark_node
;
12104 id_begin
= get_identifier ("begin");
12105 id_end
= get_identifier ("end");
12106 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12107 /*protect=*/2, /*want_type=*/false,
12108 tf_warning_or_error
);
12109 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12110 /*protect=*/2, /*want_type=*/false,
12111 tf_warning_or_error
);
12113 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
12115 /* Use the member functions. */
12116 if (member_begin
!= NULL_TREE
)
12117 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12119 error ("range-based %<for%> expression of type %qT has an "
12120 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12122 if (member_end
!= NULL_TREE
)
12123 *end
= cp_parser_range_for_member_function (range
, id_end
);
12125 error ("range-based %<for%> expression of type %qT has a "
12126 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12130 /* Use global functions with ADL. */
12131 vec
<tree
, va_gc
> *vec
;
12132 vec
= make_tree_vector ();
12134 vec_safe_push (vec
, range
);
12136 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12137 tf_warning_or_error
);
12138 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12139 tf_warning_or_error
);
12140 member_end
= perform_koenig_lookup (id_end
, vec
,
12141 tf_warning_or_error
);
12142 *end
= finish_call_expr (member_end
, &vec
, false, true,
12143 tf_warning_or_error
);
12145 release_tree_vector (vec
);
12148 /* Last common checks. */
12149 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12151 /* If one of the expressions is an error do no more checks. */
12152 *begin
= *end
= error_mark_node
;
12153 return error_mark_node
;
12155 else if (type_dependent_expression_p (*begin
)
12156 || type_dependent_expression_p (*end
))
12157 /* Can happen, when, eg, in a template context, Koenig lookup
12158 can't resolve begin/end (c++/58503). */
12162 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12163 /* The unqualified type of the __begin and __end temporaries should
12164 be the same, as required by the multiple auto declaration. */
12165 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12167 if (cxx_dialect
>= cxx17
12168 && (build_x_binary_op (input_location
, NE_EXPR
,
12169 *begin
, ERROR_MARK
,
12172 != error_mark_node
))
12173 /* P0184R0 allows __begin and __end to have different types,
12174 but make sure they are comparable so we can give a better
12177 error ("inconsistent begin/end types in range-based %<for%> "
12178 "statement: %qT and %qT",
12179 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12186 /* Helper function for cp_parser_perform_range_for_lookup.
12187 Builds a tree for RANGE.IDENTIFIER(). */
12190 cp_parser_range_for_member_function (tree range
, tree identifier
)
12193 vec
<tree
, va_gc
> *vec
;
12195 member
= finish_class_member_access_expr (range
, identifier
,
12196 false, tf_warning_or_error
);
12197 if (member
== error_mark_node
)
12198 return error_mark_node
;
12200 vec
= make_tree_vector ();
12201 res
= finish_call_expr (member
, &vec
,
12202 /*disallow_virtual=*/false,
12203 /*koenig_p=*/false,
12204 tf_warning_or_error
);
12205 release_tree_vector (vec
);
12209 /* Parse an iteration-statement.
12211 iteration-statement:
12212 while ( condition ) statement
12213 do statement while ( expression ) ;
12214 for ( init-statement condition [opt] ; expression [opt] )
12217 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12220 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
12225 unsigned char in_statement
;
12226 token_indent_info guard_tinfo
;
12228 /* Peek at the next token. */
12229 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12231 return error_mark_node
;
12233 guard_tinfo
= get_token_indent_info (token
);
12235 /* Remember whether or not we are already within an iteration
12237 in_statement
= parser
->in_statement
;
12239 /* See what kind of keyword it is. */
12240 keyword
= token
->keyword
;
12247 /* Begin the while-statement. */
12248 statement
= begin_while_stmt ();
12249 /* Look for the `('. */
12250 matching_parens parens
;
12251 parens
.require_open (parser
);
12252 /* Parse the condition. */
12253 condition
= cp_parser_condition (parser
);
12254 finish_while_stmt_cond (condition
, statement
, ivdep
);
12255 /* Look for the `)'. */
12256 parens
.require_close (parser
);
12257 /* Parse the dependent statement. */
12258 parser
->in_statement
= IN_ITERATION_STMT
;
12259 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12260 parser
->in_statement
= in_statement
;
12261 /* We're done with the while-statement. */
12262 finish_while_stmt (statement
);
12270 /* Begin the do-statement. */
12271 statement
= begin_do_stmt ();
12272 /* Parse the body of the do-statement. */
12273 parser
->in_statement
= IN_ITERATION_STMT
;
12274 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12275 parser
->in_statement
= in_statement
;
12276 finish_do_body (statement
);
12277 /* Look for the `while' keyword. */
12278 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12279 /* Look for the `('. */
12280 matching_parens parens
;
12281 parens
.require_open (parser
);
12282 /* Parse the expression. */
12283 expression
= cp_parser_expression (parser
);
12284 /* We're done with the do-statement. */
12285 finish_do_stmt (expression
, statement
, ivdep
);
12286 /* Look for the `)'. */
12287 parens
.require_close (parser
);
12288 /* Look for the `;'. */
12289 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12295 /* Look for the `('. */
12296 matching_parens parens
;
12297 parens
.require_open (parser
);
12299 statement
= cp_parser_for (parser
, ivdep
);
12301 /* Look for the `)'. */
12302 parens
.require_close (parser
);
12304 /* Parse the body of the for-statement. */
12305 parser
->in_statement
= IN_ITERATION_STMT
;
12306 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12307 parser
->in_statement
= in_statement
;
12309 /* We're done with the for-statement. */
12310 finish_for_stmt (statement
);
12315 cp_parser_error (parser
, "expected iteration-statement");
12316 statement
= error_mark_node
;
12323 /* Parse a init-statement or the declarator of a range-based-for.
12324 Returns true if a range-based-for declaration is seen.
12327 expression-statement
12328 simple-declaration */
12331 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12333 /* If the next token is a `;', then we have an empty
12334 expression-statement. Grammatically, this is also a
12335 simple-declaration, but an invalid one, because it does not
12336 declare anything. Therefore, if we did not handle this case
12337 specially, we would issue an error message about an invalid
12339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12341 bool is_range_for
= false;
12342 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12344 /* A colon is used in range-based for. */
12345 parser
->colon_corrects_to_scope_p
= false;
12347 /* We're going to speculatively look for a declaration, falling back
12348 to an expression, if necessary. */
12349 cp_parser_parse_tentatively (parser
);
12350 /* Parse the declaration. */
12351 cp_parser_simple_declaration (parser
,
12352 /*function_definition_allowed_p=*/false,
12354 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12357 /* It is a range-for, consume the ':' */
12358 cp_lexer_consume_token (parser
->lexer
);
12359 is_range_for
= true;
12360 if (cxx_dialect
< cxx11
)
12362 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12363 "range-based %<for%> loops only available with "
12364 "-std=c++11 or -std=gnu++11");
12365 *decl
= error_mark_node
;
12369 /* The ';' is not consumed yet because we told
12370 cp_parser_simple_declaration not to. */
12371 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12373 if (cp_parser_parse_definitely (parser
))
12374 return is_range_for
;
12375 /* If the tentative parse failed, then we shall need to look for an
12376 expression-statement. */
12378 /* If we are here, it is an expression-statement. */
12379 cp_parser_expression_statement (parser
, NULL_TREE
);
12383 /* Parse a jump-statement.
12388 return expression [opt] ;
12389 return braced-init-list ;
12395 goto * expression ;
12397 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12400 cp_parser_jump_statement (cp_parser
* parser
)
12402 tree statement
= error_mark_node
;
12405 unsigned char in_statement
;
12407 /* Peek at the next token. */
12408 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12410 return error_mark_node
;
12412 /* See what kind of keyword it is. */
12413 keyword
= token
->keyword
;
12417 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12418 switch (in_statement
)
12421 error_at (token
->location
, "break statement not within loop or switch");
12424 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12425 || in_statement
== IN_ITERATION_STMT
);
12426 statement
= finish_break_stmt ();
12427 if (in_statement
== IN_ITERATION_STMT
)
12428 break_maybe_infinite_loop ();
12431 error_at (token
->location
, "invalid exit from OpenMP structured block");
12434 error_at (token
->location
, "break statement used with OpenMP for loop");
12436 case IN_CILK_SIMD_FOR
:
12437 error_at (token
->location
, "break statement used with Cilk Plus for loop");
12440 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12444 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12447 error_at (token
->location
, "continue statement not within a loop");
12449 case IN_CILK_SIMD_FOR
:
12450 error_at (token
->location
,
12451 "continue statement within %<#pragma simd%> loop body");
12452 /* Fall through. */
12453 case IN_ITERATION_STMT
:
12455 statement
= finish_continue_stmt ();
12458 error_at (token
->location
, "invalid exit from OpenMP structured block");
12461 gcc_unreachable ();
12463 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12469 bool expr_non_constant_p
;
12471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12473 cp_lexer_set_source_position (parser
->lexer
);
12474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12475 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12477 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12478 expr
= cp_parser_expression (parser
);
12480 /* If the next token is a `;', then there is no
12483 /* Build the return-statement. */
12484 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12485 /* Don't deduce from a discarded return statement. */;
12487 statement
= finish_return_stmt (expr
);
12488 /* Look for the final `;'. */
12489 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12494 if (parser
->in_function_body
12495 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12497 error ("%<goto%> in %<constexpr%> function");
12498 cp_function_chain
->invalid_constexpr
= true;
12501 /* Create the goto-statement. */
12502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12504 /* Issue a warning about this use of a GNU extension. */
12505 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12506 /* Consume the '*' token. */
12507 cp_lexer_consume_token (parser
->lexer
);
12508 /* Parse the dependent expression. */
12509 finish_goto_stmt (cp_parser_expression (parser
));
12512 finish_goto_stmt (cp_parser_identifier (parser
));
12513 /* Look for the final `;'. */
12514 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12518 cp_parser_error (parser
, "expected jump-statement");
12525 /* Parse a declaration-statement.
12527 declaration-statement:
12528 block-declaration */
12531 cp_parser_declaration_statement (cp_parser
* parser
)
12535 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12536 p
= obstack_alloc (&declarator_obstack
, 0);
12538 /* Parse the block-declaration. */
12539 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12541 /* Free any declarators allocated. */
12542 obstack_free (&declarator_obstack
, p
);
12545 /* Some dependent statements (like `if (cond) statement'), are
12546 implicitly in their own scope. In other words, if the statement is
12547 a single statement (as opposed to a compound-statement), it is
12548 none-the-less treated as if it were enclosed in braces. Any
12549 declarations appearing in the dependent statement are out of scope
12550 after control passes that point. This function parses a statement,
12551 but ensures that is in its own scope, even if it is not a
12552 compound-statement.
12554 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12555 is a (possibly labeled) if statement which is not enclosed in
12556 braces and has an else clause. This is used to implement
12559 CHAIN is a vector of if-else-if conditions. This is used to implement
12562 Returns the new statement. */
12565 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12566 const token_indent_info
&guard_tinfo
,
12570 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12571 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12572 token_indent_info body_tinfo
12573 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12578 /* Mark if () ; with a special NOP_EXPR. */
12579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12581 cp_lexer_consume_token (parser
->lexer
);
12582 statement
= add_stmt (build_empty_stmt (body_loc
));
12584 if (guard_tinfo
.keyword
== RID_IF
12585 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12586 warning_at (body_loc
, OPT_Wempty_body
,
12587 "suggest braces around empty body in an %<if%> statement");
12588 else if (guard_tinfo
.keyword
== RID_ELSE
)
12589 warning_at (body_loc
, OPT_Wempty_body
,
12590 "suggest braces around empty body in an %<else%> statement");
12592 /* if a compound is opened, we simply parse the statement directly. */
12593 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12594 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12595 /* If the token is not a `{', then we must take special action. */
12598 /* Create a compound-statement. */
12599 statement
= begin_compound_stmt (0);
12600 /* Parse the dependent-statement. */
12601 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12602 &body_loc_after_labels
);
12603 /* Finish the dummy compound-statement. */
12604 finish_compound_stmt (statement
);
12607 token_indent_info next_tinfo
12608 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12609 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12611 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12612 && next_tinfo
.type
!= CPP_SEMICOLON
)
12613 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12614 guard_tinfo
.location
, guard_tinfo
.keyword
);
12616 /* Return the statement. */
12620 /* For some dependent statements (like `while (cond) statement'), we
12621 have already created a scope. Therefore, even if the dependent
12622 statement is a compound-statement, we do not want to create another
12626 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12627 const token_indent_info
&guard_tinfo
)
12629 /* If the token is a `{', then we must take special action. */
12630 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12634 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12636 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12637 &loc_after_labels
);
12638 token_indent_info next_tinfo
12639 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12640 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12642 if (loc_after_labels
!= UNKNOWN_LOCATION
12643 && next_tinfo
.type
!= CPP_SEMICOLON
)
12644 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12645 guard_tinfo
.location
,
12646 guard_tinfo
.keyword
);
12650 /* Avoid calling cp_parser_compound_statement, so that we
12651 don't create a new scope. Do everything else by hand. */
12652 matching_braces braces
;
12653 braces
.require_open (parser
);
12654 /* If the next keyword is `__label__' we have a label declaration. */
12655 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12656 cp_parser_label_declaration (parser
);
12657 /* Parse an (optional) statement-seq. */
12658 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12659 braces
.require_close (parser
);
12663 /* Declarations [gram.dcl.dcl] */
12665 /* Parse an optional declaration-sequence.
12669 declaration-seq declaration */
12672 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12678 token
= cp_lexer_peek_token (parser
->lexer
);
12680 if (token
->type
== CPP_CLOSE_BRACE
12681 || token
->type
== CPP_EOF
12682 || token
->type
== CPP_PRAGMA_EOL
)
12685 if (token
->type
== CPP_SEMICOLON
)
12687 /* A declaration consisting of a single semicolon is
12688 invalid. Allow it unless we're being pedantic. */
12689 cp_lexer_consume_token (parser
->lexer
);
12690 if (!in_system_header_at (input_location
))
12691 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12695 /* If we're entering or exiting a region that's implicitly
12696 extern "C", modify the lang context appropriately. */
12697 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12699 push_lang_context (lang_name_c
);
12700 parser
->implicit_extern_c
= true;
12702 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12704 pop_lang_context ();
12705 parser
->implicit_extern_c
= false;
12708 if (token
->type
== CPP_PRAGMA
)
12710 /* A top-level declaration can consist solely of a #pragma.
12711 A nested declaration cannot, so this is done here and not
12712 in cp_parser_declaration. (A #pragma at block scope is
12713 handled in cp_parser_statement.) */
12714 cp_parser_pragma (parser
, pragma_external
, NULL
);
12718 /* Parse the declaration itself. */
12719 cp_parser_declaration (parser
);
12723 /* Parse a declaration.
12727 function-definition
12728 template-declaration
12729 explicit-instantiation
12730 explicit-specialization
12731 linkage-specification
12732 namespace-definition
12740 __extension__ declaration */
12743 cp_parser_declaration (cp_parser
* parser
)
12747 int saved_pedantic
;
12749 tree attributes
= NULL_TREE
;
12751 /* Check for the `__extension__' keyword. */
12752 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12754 /* Parse the qualified declaration. */
12755 cp_parser_declaration (parser
);
12756 /* Restore the PEDANTIC flag. */
12757 pedantic
= saved_pedantic
;
12762 /* Try to figure out what kind of declaration is present. */
12763 token1
= *cp_lexer_peek_token (parser
->lexer
);
12765 if (token1
.type
!= CPP_EOF
)
12766 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12769 token2
.type
= CPP_EOF
;
12770 token2
.keyword
= RID_MAX
;
12773 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12774 p
= obstack_alloc (&declarator_obstack
, 0);
12776 /* If the next token is `extern' and the following token is a string
12777 literal, then we have a linkage specification. */
12778 if (token1
.keyword
== RID_EXTERN
12779 && cp_parser_is_pure_string_literal (&token2
))
12780 cp_parser_linkage_specification (parser
);
12781 /* If the next token is `template', then we have either a template
12782 declaration, an explicit instantiation, or an explicit
12784 else if (token1
.keyword
== RID_TEMPLATE
)
12786 /* `template <>' indicates a template specialization. */
12787 if (token2
.type
== CPP_LESS
12788 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12789 cp_parser_explicit_specialization (parser
);
12790 /* `template <' indicates a template declaration. */
12791 else if (token2
.type
== CPP_LESS
)
12792 cp_parser_template_declaration (parser
, /*member_p=*/false);
12793 /* Anything else must be an explicit instantiation. */
12795 cp_parser_explicit_instantiation (parser
);
12797 /* If the next token is `export', then we have a template
12799 else if (token1
.keyword
== RID_EXPORT
)
12800 cp_parser_template_declaration (parser
, /*member_p=*/false);
12801 /* If the next token is `extern', 'static' or 'inline' and the one
12802 after that is `template', we have a GNU extended explicit
12803 instantiation directive. */
12804 else if (cp_parser_allow_gnu_extensions_p (parser
)
12805 && (token1
.keyword
== RID_EXTERN
12806 || token1
.keyword
== RID_STATIC
12807 || token1
.keyword
== RID_INLINE
)
12808 && token2
.keyword
== RID_TEMPLATE
)
12809 cp_parser_explicit_instantiation (parser
);
12810 /* If the next token is `namespace', check for a named or unnamed
12811 namespace definition. */
12812 else if (token1
.keyword
== RID_NAMESPACE
12813 && (/* A named namespace definition. */
12814 (token2
.type
== CPP_NAME
12815 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12817 || (token2
.type
== CPP_OPEN_SQUARE
12818 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12819 == CPP_OPEN_SQUARE
)
12820 /* An unnamed namespace definition. */
12821 || token2
.type
== CPP_OPEN_BRACE
12822 || token2
.keyword
== RID_ATTRIBUTE
))
12823 cp_parser_namespace_definition (parser
);
12824 /* An inline (associated) namespace definition. */
12825 else if (token1
.keyword
== RID_INLINE
12826 && token2
.keyword
== RID_NAMESPACE
)
12827 cp_parser_namespace_definition (parser
);
12828 /* Objective-C++ declaration/definition. */
12829 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12830 cp_parser_objc_declaration (parser
, NULL_TREE
);
12831 else if (c_dialect_objc ()
12832 && token1
.keyword
== RID_ATTRIBUTE
12833 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12834 cp_parser_objc_declaration (parser
, attributes
);
12835 /* At this point we may have a template declared by a concept
12837 else if (flag_concepts
12838 && cp_parser_template_declaration_after_export (parser
,
12839 /*member_p=*/false))
12842 /* Try to parse a block-declaration, or a function-definition. */
12843 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12845 /* Free any declarators allocated. */
12846 obstack_free (&declarator_obstack
, p
);
12849 /* Parse a block-declaration.
12854 namespace-alias-definition
12861 __extension__ block-declaration
12866 static_assert-declaration
12868 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12869 part of a declaration-statement. */
12872 cp_parser_block_declaration (cp_parser
*parser
,
12876 int saved_pedantic
;
12878 /* Check for the `__extension__' keyword. */
12879 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12881 /* Parse the qualified declaration. */
12882 cp_parser_block_declaration (parser
, statement_p
);
12883 /* Restore the PEDANTIC flag. */
12884 pedantic
= saved_pedantic
;
12889 /* Peek at the next token to figure out which kind of declaration is
12891 token1
= cp_lexer_peek_token (parser
->lexer
);
12893 /* If the next keyword is `asm', we have an asm-definition. */
12894 if (token1
->keyword
== RID_ASM
)
12897 cp_parser_commit_to_tentative_parse (parser
);
12898 cp_parser_asm_definition (parser
);
12900 /* If the next keyword is `namespace', we have a
12901 namespace-alias-definition. */
12902 else if (token1
->keyword
== RID_NAMESPACE
)
12903 cp_parser_namespace_alias_definition (parser
);
12904 /* If the next keyword is `using', we have a
12905 using-declaration, a using-directive, or an alias-declaration. */
12906 else if (token1
->keyword
== RID_USING
)
12911 cp_parser_commit_to_tentative_parse (parser
);
12912 /* If the token after `using' is `namespace', then we have a
12913 using-directive. */
12914 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12915 if (token2
->keyword
== RID_NAMESPACE
)
12916 cp_parser_using_directive (parser
);
12917 /* If the second token after 'using' is '=', then we have an
12918 alias-declaration. */
12919 else if (cxx_dialect
>= cxx11
12920 && token2
->type
== CPP_NAME
12921 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12922 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12923 cp_parser_alias_declaration (parser
);
12924 /* Otherwise, it's a using-declaration. */
12926 cp_parser_using_declaration (parser
,
12927 /*access_declaration_p=*/false);
12929 /* If the next keyword is `__label__' we have a misplaced label
12931 else if (token1
->keyword
== RID_LABEL
)
12933 cp_lexer_consume_token (parser
->lexer
);
12934 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12935 cp_parser_skip_to_end_of_statement (parser
);
12936 /* If the next token is now a `;', consume it. */
12937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12938 cp_lexer_consume_token (parser
->lexer
);
12940 /* If the next token is `static_assert' we have a static assertion. */
12941 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12942 cp_parser_static_assert (parser
, /*member_p=*/false);
12943 /* Anything else must be a simple-declaration. */
12945 cp_parser_simple_declaration (parser
, !statement_p
,
12946 /*maybe_range_for_decl*/NULL
);
12949 /* Parse a simple-declaration.
12951 simple-declaration:
12952 decl-specifier-seq [opt] init-declarator-list [opt] ;
12953 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12954 brace-or-equal-initializer ;
12956 init-declarator-list:
12958 init-declarator-list , init-declarator
12960 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12961 function-definition as a simple-declaration.
12963 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12964 parsed declaration if it is an uninitialized single declarator not followed
12965 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12966 if present, will not be consumed. */
12969 cp_parser_simple_declaration (cp_parser
* parser
,
12970 bool function_definition_allowed_p
,
12971 tree
*maybe_range_for_decl
)
12973 cp_decl_specifier_seq decl_specifiers
;
12974 int declares_class_or_enum
;
12975 bool saw_declarator
;
12976 location_t comma_loc
= UNKNOWN_LOCATION
;
12977 location_t init_loc
= UNKNOWN_LOCATION
;
12979 if (maybe_range_for_decl
)
12980 *maybe_range_for_decl
= NULL_TREE
;
12982 /* Defer access checks until we know what is being declared; the
12983 checks for names appearing in the decl-specifier-seq should be
12984 done as if we were in the scope of the thing being declared. */
12985 push_deferring_access_checks (dk_deferred
);
12987 /* Parse the decl-specifier-seq. We have to keep track of whether
12988 or not the decl-specifier-seq declares a named class or
12989 enumeration type, since that is the only case in which the
12990 init-declarator-list is allowed to be empty.
12994 In a simple-declaration, the optional init-declarator-list can be
12995 omitted only when declaring a class or enumeration, that is when
12996 the decl-specifier-seq contains either a class-specifier, an
12997 elaborated-type-specifier, or an enum-specifier. */
12998 cp_parser_decl_specifier_seq (parser
,
12999 CP_PARSER_FLAGS_OPTIONAL
,
13001 &declares_class_or_enum
);
13002 /* We no longer need to defer access checks. */
13003 stop_deferring_access_checks ();
13005 /* In a block scope, a valid declaration must always have a
13006 decl-specifier-seq. By not trying to parse declarators, we can
13007 resolve the declaration/expression ambiguity more quickly. */
13008 if (!function_definition_allowed_p
13009 && !decl_specifiers
.any_specifiers_p
)
13011 cp_parser_error (parser
, "expected declaration");
13015 /* If the next two tokens are both identifiers, the code is
13016 erroneous. The usual cause of this situation is code like:
13020 where "T" should name a type -- but does not. */
13021 if (!decl_specifiers
.any_type_specifiers_p
13022 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13024 /* If parsing tentatively, we should commit; we really are
13025 looking at a declaration. */
13026 cp_parser_commit_to_tentative_parse (parser
);
13031 /* If we have seen at least one decl-specifier, and the next token
13032 is not a parenthesis, then we must be looking at a declaration.
13033 (After "int (" we might be looking at a functional cast.) */
13034 if (decl_specifiers
.any_specifiers_p
13035 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
13036 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
13037 && !cp_parser_error_occurred (parser
))
13038 cp_parser_commit_to_tentative_parse (parser
);
13040 /* Look for C++17 decomposition declaration. */
13041 for (size_t n
= 1; ; n
++)
13042 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13043 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13045 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13046 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13047 && decl_specifiers
.any_specifiers_p
)
13050 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13051 maybe_range_for_decl
,
13054 /* The next token should be either a `,' or a `;'. */
13055 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13056 /* If it's a `;', we are done. */
13057 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
13059 /* Anything else is an error. */
13062 /* If we have already issued an error message we don't need
13063 to issue another one. */
13064 if ((decl
!= error_mark_node
13065 && DECL_INITIAL (decl
) != error_mark_node
)
13066 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13067 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13068 /* Skip tokens until we reach the end of the statement. */
13069 cp_parser_skip_to_end_of_statement (parser
);
13070 /* If the next token is now a `;', consume it. */
13071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13072 cp_lexer_consume_token (parser
->lexer
);
13080 bool auto_specifier_p
;
13081 /* NULL_TREE if both variable and function declaration are allowed,
13082 error_mark_node if function declaration are not allowed and
13083 a FUNCTION_DECL that should be diagnosed if it is followed by
13084 variable declarations. */
13085 tree auto_function_declaration
;
13087 last_type
= NULL_TREE
;
13089 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13090 auto_function_declaration
= NULL_TREE
;
13092 /* Keep going until we hit the `;' at the end of the simple
13094 saw_declarator
= false;
13095 while (cp_lexer_next_token_is_not (parser
->lexer
,
13099 bool function_definition_p
;
13101 tree auto_result
= NULL_TREE
;
13103 if (saw_declarator
)
13105 /* If we are processing next declarator, comma is expected */
13106 token
= cp_lexer_peek_token (parser
->lexer
);
13107 gcc_assert (token
->type
== CPP_COMMA
);
13108 cp_lexer_consume_token (parser
->lexer
);
13109 if (maybe_range_for_decl
)
13111 *maybe_range_for_decl
= error_mark_node
;
13112 if (comma_loc
== UNKNOWN_LOCATION
)
13113 comma_loc
= token
->location
;
13117 saw_declarator
= true;
13119 /* Parse the init-declarator. */
13120 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13122 function_definition_allowed_p
,
13123 /*member_p=*/false,
13124 declares_class_or_enum
,
13125 &function_definition_p
,
13126 maybe_range_for_decl
,
13129 /* If an error occurred while parsing tentatively, exit quickly.
13130 (That usually happens when in the body of a function; each
13131 statement is treated as a declaration-statement until proven
13133 if (cp_parser_error_occurred (parser
))
13136 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13138 /* If the init-declarator-list contains more than one
13139 init-declarator, they shall all form declarations of
13141 if (auto_function_declaration
== NULL_TREE
)
13142 auto_function_declaration
13143 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13144 else if (TREE_CODE (decl
) == FUNCTION_DECL
13145 || auto_function_declaration
!= error_mark_node
)
13147 error_at (decl_specifiers
.locations
[ds_type_spec
],
13148 "non-variable %qD in declaration with more than one "
13149 "declarator with placeholder type",
13150 TREE_CODE (decl
) == FUNCTION_DECL
13151 ? decl
: auto_function_declaration
);
13152 auto_function_declaration
= error_mark_node
;
13157 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13160 && last_type
!= error_mark_node
13161 && !same_type_p (auto_result
, last_type
))
13163 /* If the list of declarators contains more than one declarator,
13164 the type of each declared variable is determined as described
13165 above. If the type deduced for the template parameter U is not
13166 the same in each deduction, the program is ill-formed. */
13167 error_at (decl_specifiers
.locations
[ds_type_spec
],
13168 "inconsistent deduction for %qT: %qT and then %qT",
13169 decl_specifiers
.type
, last_type
, auto_result
);
13170 last_type
= error_mark_node
;
13173 last_type
= auto_result
;
13176 /* Handle function definitions specially. */
13177 if (function_definition_p
)
13179 /* If the next token is a `,', then we are probably
13180 processing something like:
13184 which is erroneous. */
13185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13187 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13188 error_at (token
->location
,
13190 " declarations and function-definitions is forbidden");
13192 /* Otherwise, we're done with the list of declarators. */
13195 pop_deferring_access_checks ();
13199 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13200 *maybe_range_for_decl
= decl
;
13201 /* The next token should be either a `,' or a `;'. */
13202 token
= cp_lexer_peek_token (parser
->lexer
);
13203 /* If it's a `,', there are more declarators to come. */
13204 if (token
->type
== CPP_COMMA
)
13205 /* will be consumed next time around */;
13206 /* If it's a `;', we are done. */
13207 else if (token
->type
== CPP_SEMICOLON
)
13209 else if (maybe_range_for_decl
)
13211 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13212 permerror (decl_specifiers
.locations
[ds_type_spec
],
13213 "types may not be defined in a for-range-declaration");
13216 /* Anything else is an error. */
13219 /* If we have already issued an error message we don't need
13220 to issue another one. */
13221 if ((decl
!= error_mark_node
13222 && DECL_INITIAL (decl
) != error_mark_node
)
13223 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13224 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13225 /* Skip tokens until we reach the end of the statement. */
13226 cp_parser_skip_to_end_of_statement (parser
);
13227 /* If the next token is now a `;', consume it. */
13228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13229 cp_lexer_consume_token (parser
->lexer
);
13232 /* After the first time around, a function-definition is not
13233 allowed -- even if it was OK at first. For example:
13238 function_definition_allowed_p
= false;
13241 /* Issue an error message if no declarators are present, and the
13242 decl-specifier-seq does not itself declare a class or
13243 enumeration: [dcl.dcl]/3. */
13244 if (!saw_declarator
)
13246 if (cp_parser_declares_only_class_p (parser
))
13248 if (!declares_class_or_enum
13249 && decl_specifiers
.type
13250 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13251 /* Ensure an error is issued anyway when finish_decltype_type,
13252 called via cp_parser_decl_specifier_seq, returns a class or
13253 an enumeration (c++/51786). */
13254 decl_specifiers
.type
= NULL_TREE
;
13255 shadow_tag (&decl_specifiers
);
13257 /* Perform any deferred access checks. */
13258 perform_deferred_access_checks (tf_warning_or_error
);
13261 /* Consume the `;'. */
13263 if (!maybe_range_for_decl
)
13264 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13265 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13267 if (init_loc
!= UNKNOWN_LOCATION
)
13268 error_at (init_loc
, "initializer in range-based %<for%> loop");
13269 if (comma_loc
!= UNKNOWN_LOCATION
)
13270 error_at (comma_loc
,
13271 "multiple declarations in range-based %<for%> loop");
13275 pop_deferring_access_checks ();
13278 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13279 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13283 cp_parser_decomposition_declaration (cp_parser
*parser
,
13284 cp_decl_specifier_seq
*decl_specifiers
,
13285 tree
*maybe_range_for_decl
,
13286 location_t
*init_loc
)
13288 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13289 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13290 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13292 /* Parse the identifier-list. */
13293 auto_vec
<cp_expr
, 10> v
;
13294 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13297 cp_expr e
= cp_parser_identifier (parser
);
13298 if (e
.get_value () == error_mark_node
)
13301 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13303 cp_lexer_consume_token (parser
->lexer
);
13306 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13307 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13309 end_loc
= UNKNOWN_LOCATION
;
13310 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13313 cp_lexer_consume_token (parser
->lexer
);
13316 cp_parser_skip_to_end_of_statement (parser
);
13317 return error_mark_node
;
13321 if (cxx_dialect
< cxx17
)
13322 pedwarn (loc
, 0, "structured bindings only available with "
13323 "-std=c++17 or -std=gnu++17");
13326 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13327 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13328 declarator
->id_loc
= loc
;
13329 if (ref_qual
!= REF_QUAL_NONE
)
13330 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13331 ref_qual
== REF_QUAL_RVALUE
,
13333 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13334 NULL_TREE
, decl_specifiers
->attributes
,
13336 tree orig_decl
= decl
;
13340 cp_decl_specifier_seq decl_specs
;
13341 clear_decl_specs (&decl_specs
);
13342 decl_specs
.type
= make_auto ();
13344 FOR_EACH_VEC_ELT (v
, i
, e
)
13347 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13349 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13350 declarator
->id_loc
= e
.get_location ();
13351 tree elt_pushed_scope
;
13352 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13353 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13354 if (decl2
== error_mark_node
)
13355 decl
= error_mark_node
;
13356 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13358 /* Ensure we've diagnosed redeclaration if we aren't creating
13360 gcc_assert (errorcount
);
13361 decl
= error_mark_node
;
13365 if (elt_pushed_scope
)
13366 pop_scope (elt_pushed_scope
);
13371 error_at (loc
, "empty structured binding declaration");
13372 decl
= error_mark_node
;
13375 if (maybe_range_for_decl
== NULL
13376 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13378 bool non_constant_p
= false, is_direct_init
= false;
13379 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13380 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13382 if (initializer
== NULL_TREE
13383 || (TREE_CODE (initializer
) == TREE_LIST
13384 && TREE_CHAIN (initializer
))
13386 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13387 && CONSTRUCTOR_NELTS (initializer
) != 1))
13389 error_at (loc
, "invalid initializer for structured binding "
13391 initializer
= error_mark_node
;
13394 if (decl
!= error_mark_node
)
13396 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13397 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13398 cp_finish_decomp (decl
, prev
, v
.length ());
13401 else if (decl
!= error_mark_node
)
13403 *maybe_range_for_decl
= prev
;
13404 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13405 the underlying DECL. */
13406 cp_finish_decomp (decl
, prev
, v
.length ());
13410 pop_scope (pushed_scope
);
13412 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13414 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13415 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13421 /* Parse a decl-specifier-seq.
13423 decl-specifier-seq:
13424 decl-specifier-seq [opt] decl-specifier
13425 decl-specifier attribute-specifier-seq [opt] (C++11)
13428 storage-class-specifier
13439 Concepts Extension:
13444 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13446 The parser flags FLAGS is used to control type-specifier parsing.
13448 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13451 1: one of the decl-specifiers is an elaborated-type-specifier
13452 (i.e., a type declaration)
13453 2: one of the decl-specifiers is an enum-specifier or a
13454 class-specifier (i.e., a type definition)
13459 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13460 cp_parser_flags flags
,
13461 cp_decl_specifier_seq
*decl_specs
,
13462 int* declares_class_or_enum
)
13464 bool constructor_possible_p
= !parser
->in_declarator_p
;
13465 bool found_decl_spec
= false;
13466 cp_token
*start_token
= NULL
;
13469 /* Clear DECL_SPECS. */
13470 clear_decl_specs (decl_specs
);
13472 /* Assume no class or enumeration type is declared. */
13473 *declares_class_or_enum
= 0;
13475 /* Keep reading specifiers until there are no more to read. */
13478 bool constructor_p
;
13482 /* Peek at the next token. */
13483 token
= cp_lexer_peek_token (parser
->lexer
);
13485 /* Save the first token of the decl spec list for error
13488 start_token
= token
;
13489 /* Handle attributes. */
13490 if (cp_next_tokens_can_be_attribute_p (parser
))
13492 /* Parse the attributes. */
13493 tree attrs
= cp_parser_attributes_opt (parser
);
13495 /* In a sequence of declaration specifiers, c++11 attributes
13496 appertain to the type that precede them. In that case
13499 The attribute-specifier-seq affects the type only for
13500 the declaration it appears in, not other declarations
13501 involving the same type.
13503 But for now let's force the user to position the
13504 attribute either at the beginning of the declaration or
13505 after the declarator-id, which would clearly mean that it
13506 applies to the declarator. */
13507 if (cxx11_attribute_p (attrs
))
13509 if (!found_decl_spec
)
13510 /* The c++11 attribute is at the beginning of the
13511 declaration. It appertains to the entity being
13515 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13517 /* This is an attribute following a
13518 class-specifier. */
13519 if (decl_specs
->type_definition_p
)
13520 warn_misplaced_attr_for_class_type (token
->location
,
13526 decl_specs
->std_attributes
13527 = chainon (decl_specs
->std_attributes
,
13529 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13530 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13536 decl_specs
->attributes
13537 = chainon (decl_specs
->attributes
,
13539 if (decl_specs
->locations
[ds_attribute
] == 0)
13540 decl_specs
->locations
[ds_attribute
] = token
->location
;
13543 /* Assume we will find a decl-specifier keyword. */
13544 found_decl_spec
= true;
13545 /* If the next token is an appropriate keyword, we can simply
13546 add it to the list. */
13547 switch (token
->keyword
)
13553 if (!at_class_scope_p ())
13555 gcc_rich_location
richloc (token
->location
);
13556 richloc
.add_fixit_remove ();
13557 error_at (&richloc
, "%<friend%> used outside of class");
13558 cp_lexer_purge_token (parser
->lexer
);
13563 /* Consume the token. */
13564 cp_lexer_consume_token (parser
->lexer
);
13568 case RID_CONSTEXPR
:
13570 cp_lexer_consume_token (parser
->lexer
);
13575 cp_lexer_consume_token (parser
->lexer
);
13578 /* function-specifier:
13585 cp_parser_function_specifier_opt (parser
, decl_specs
);
13592 /* Consume the token. */
13593 cp_lexer_consume_token (parser
->lexer
);
13594 /* A constructor declarator cannot appear in a typedef. */
13595 constructor_possible_p
= false;
13596 /* The "typedef" keyword can only occur in a declaration; we
13597 may as well commit at this point. */
13598 cp_parser_commit_to_tentative_parse (parser
);
13600 if (decl_specs
->storage_class
!= sc_none
)
13601 decl_specs
->conflicting_specifiers_p
= true;
13604 /* storage-class-specifier:
13614 if (cxx_dialect
== cxx98
)
13616 /* Consume the token. */
13617 cp_lexer_consume_token (parser
->lexer
);
13619 /* Complain about `auto' as a storage specifier, if
13620 we're complaining about C++0x compatibility. */
13621 gcc_rich_location
richloc (token
->location
);
13622 richloc
.add_fixit_remove ();
13623 warning_at (&richloc
, OPT_Wc__11_compat
,
13624 "%<auto%> changes meaning in C++11; "
13625 "please remove it");
13627 /* Set the storage class anyway. */
13628 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13632 /* C++0x auto type-specifier. */
13633 found_decl_spec
= false;
13640 /* Consume the token. */
13641 cp_lexer_consume_token (parser
->lexer
);
13642 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13646 /* Consume the token. */
13648 cp_lexer_consume_token (parser
->lexer
);
13652 /* We did not yet find a decl-specifier yet. */
13653 found_decl_spec
= false;
13657 if (found_decl_spec
13658 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13659 && token
->keyword
!= RID_CONSTEXPR
)
13660 error ("decl-specifier invalid in condition");
13662 if (found_decl_spec
13663 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13664 && token
->keyword
!= RID_MUTABLE
13665 && token
->keyword
!= RID_CONSTEXPR
)
13666 error_at (token
->location
, "%qD invalid in lambda",
13667 ridpointers
[token
->keyword
]);
13670 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13672 /* Constructors are a special case. The `S' in `S()' is not a
13673 decl-specifier; it is the beginning of the declarator. */
13675 = (!found_decl_spec
13676 && constructor_possible_p
13677 && (cp_parser_constructor_declarator_p
13678 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13680 /* If we don't have a DECL_SPEC yet, then we must be looking at
13681 a type-specifier. */
13682 if (!found_decl_spec
&& !constructor_p
)
13684 int decl_spec_declares_class_or_enum
;
13685 bool is_cv_qualifier
;
13689 = cp_parser_type_specifier (parser
, flags
,
13691 /*is_declaration=*/true,
13692 &decl_spec_declares_class_or_enum
,
13694 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13696 /* If this type-specifier referenced a user-defined type
13697 (a typedef, class-name, etc.), then we can't allow any
13698 more such type-specifiers henceforth.
13702 The longest sequence of decl-specifiers that could
13703 possibly be a type name is taken as the
13704 decl-specifier-seq of a declaration. The sequence shall
13705 be self-consistent as described below.
13709 As a general rule, at most one type-specifier is allowed
13710 in the complete decl-specifier-seq of a declaration. The
13711 only exceptions are the following:
13713 -- const or volatile can be combined with any other
13716 -- signed or unsigned can be combined with char, long,
13724 void g (const int Pc);
13726 Here, Pc is *not* part of the decl-specifier seq; it's
13727 the declarator. Therefore, once we see a type-specifier
13728 (other than a cv-qualifier), we forbid any additional
13729 user-defined types. We *do* still allow things like `int
13730 int' to be considered a decl-specifier-seq, and issue the
13731 error message later. */
13732 if (type_spec
&& !is_cv_qualifier
)
13733 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13734 /* A constructor declarator cannot follow a type-specifier. */
13737 constructor_possible_p
= false;
13738 found_decl_spec
= true;
13739 if (!is_cv_qualifier
)
13740 decl_specs
->any_type_specifiers_p
= true;
13744 /* If we still do not have a DECL_SPEC, then there are no more
13745 decl-specifiers. */
13746 if (!found_decl_spec
)
13749 decl_specs
->any_specifiers_p
= true;
13750 /* After we see one decl-specifier, further decl-specifiers are
13751 always optional. */
13752 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13755 /* Don't allow a friend specifier with a class definition. */
13756 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13757 && (*declares_class_or_enum
& 2))
13758 error_at (decl_specs
->locations
[ds_friend
],
13759 "class definition may not be declared a friend");
13762 /* Parse an (optional) storage-class-specifier.
13764 storage-class-specifier:
13773 storage-class-specifier:
13776 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13779 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13781 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13784 if (cxx_dialect
!= cxx98
)
13786 /* Fall through for C++98. */
13787 gcc_fallthrough ();
13794 /* Consume the token. */
13795 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13802 /* Parse an (optional) function-specifier.
13804 function-specifier:
13809 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13810 Updates DECL_SPECS, if it is non-NULL. */
13813 cp_parser_function_specifier_opt (cp_parser
* parser
,
13814 cp_decl_specifier_seq
*decl_specs
)
13816 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13817 switch (token
->keyword
)
13820 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13824 /* 14.5.2.3 [temp.mem]
13826 A member function template shall not be virtual. */
13827 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13828 && current_class_type
)
13829 error_at (token
->location
, "templates may not be %<virtual%>");
13831 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13835 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13842 /* Consume the token. */
13843 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13846 /* Parse a linkage-specification.
13848 linkage-specification:
13849 extern string-literal { declaration-seq [opt] }
13850 extern string-literal declaration */
13853 cp_parser_linkage_specification (cp_parser
* parser
)
13857 /* Look for the `extern' keyword. */
13858 cp_token
*extern_token
13859 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13861 /* Look for the string-literal. */
13862 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13863 linkage
= cp_parser_string_literal (parser
, false, false);
13865 /* Transform the literal into an identifier. If the literal is a
13866 wide-character string, or contains embedded NULs, then we can't
13867 handle it as the user wants. */
13868 if (strlen (TREE_STRING_POINTER (linkage
))
13869 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13871 cp_parser_error (parser
, "invalid linkage-specification");
13872 /* Assume C++ linkage. */
13873 linkage
= lang_name_cplusplus
;
13876 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13878 /* We're now using the new linkage. */
13879 push_lang_context (linkage
);
13881 /* Preserve the location of the the innermost linkage specification,
13882 tracking the locations of nested specifications via a local. */
13883 location_t saved_location
13884 = parser
->innermost_linkage_specification_location
;
13885 /* Construct a location ranging from the start of the "extern" to
13886 the end of the string-literal, with the caret at the start, e.g.:
13890 parser
->innermost_linkage_specification_location
13891 = make_location (extern_token
->location
,
13892 extern_token
->location
,
13893 get_finish (string_token
->location
));
13895 /* If the next token is a `{', then we're using the first
13897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13899 cp_ensure_no_omp_declare_simd (parser
);
13900 cp_ensure_no_oacc_routine (parser
);
13902 /* Consume the `{' token. */
13903 matching_braces braces
;
13904 braces
.consume_open (parser
)->location
;
13905 /* Parse the declarations. */
13906 cp_parser_declaration_seq_opt (parser
);
13907 /* Look for the closing `}'. */
13908 braces
.require_close (parser
);
13910 /* Otherwise, there's just one declaration. */
13913 bool saved_in_unbraced_linkage_specification_p
;
13915 saved_in_unbraced_linkage_specification_p
13916 = parser
->in_unbraced_linkage_specification_p
;
13917 parser
->in_unbraced_linkage_specification_p
= true;
13918 cp_parser_declaration (parser
);
13919 parser
->in_unbraced_linkage_specification_p
13920 = saved_in_unbraced_linkage_specification_p
;
13923 /* We're done with the linkage-specification. */
13924 pop_lang_context ();
13926 /* Restore location of parent linkage specification, if any. */
13927 parser
->innermost_linkage_specification_location
= saved_location
;
13930 /* Parse a static_assert-declaration.
13932 static_assert-declaration:
13933 static_assert ( constant-expression , string-literal ) ;
13934 static_assert ( constant-expression ) ; (C++17)
13936 If MEMBER_P, this static_assert is a class member. */
13939 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13944 location_t saved_loc
;
13947 /* Peek at the `static_assert' token so we can keep track of exactly
13948 where the static assertion started. */
13949 token
= cp_lexer_peek_token (parser
->lexer
);
13950 saved_loc
= token
->location
;
13952 /* Look for the `static_assert' keyword. */
13953 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13957 /* We know we are in a static assertion; commit to any tentative
13959 if (cp_parser_parsing_tentatively (parser
))
13960 cp_parser_commit_to_tentative_parse (parser
);
13962 /* Parse the `(' starting the static assertion condition. */
13963 matching_parens parens
;
13964 parens
.require_open (parser
);
13966 /* Parse the constant-expression. Allow a non-constant expression
13967 here in order to give better diagnostics in finish_static_assert. */
13969 cp_parser_constant_expression (parser
,
13970 /*allow_non_constant_p=*/true,
13971 /*non_constant_p=*/&dummy
);
13973 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13975 if (cxx_dialect
< cxx17
)
13976 pedwarn (input_location
, OPT_Wpedantic
,
13977 "static_assert without a message "
13978 "only available with -std=c++17 or -std=gnu++17");
13980 cp_lexer_consume_token (parser
->lexer
);
13981 message
= build_string (1, "");
13982 TREE_TYPE (message
) = char_array_type_node
;
13983 fix_string_type (message
);
13987 /* Parse the separating `,'. */
13988 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13990 /* Parse the string-literal message. */
13991 message
= cp_parser_string_literal (parser
,
13992 /*translate=*/false,
13995 /* A `)' completes the static assertion. */
13996 if (!parens
.require_close (parser
))
13997 cp_parser_skip_to_closing_parenthesis (parser
,
13998 /*recovering=*/true,
13999 /*or_comma=*/false,
14000 /*consume_paren=*/true);
14003 /* A semicolon terminates the declaration. */
14004 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14006 /* Complete the static assertion, which may mean either processing
14007 the static assert now or saving it for template instantiation. */
14008 finish_static_assert (condition
, message
, saved_loc
, member_p
);
14011 /* Parse the expression in decltype ( expression ). */
14014 cp_parser_decltype_expr (cp_parser
*parser
,
14015 bool &id_expression_or_member_access_p
)
14017 cp_token
*id_expr_start_token
;
14020 /* Since we're going to preserve any side-effects from this parse, set up a
14021 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14022 in the expression. */
14023 tentative_firewall
firewall (parser
);
14025 /* First, try parsing an id-expression. */
14026 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14027 cp_parser_parse_tentatively (parser
);
14028 expr
= cp_parser_id_expression (parser
,
14029 /*template_keyword_p=*/false,
14030 /*check_dependency_p=*/true,
14031 /*template_p=*/NULL
,
14032 /*declarator_p=*/false,
14033 /*optional_p=*/false);
14035 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14037 bool non_integral_constant_expression_p
= false;
14038 tree id_expression
= expr
;
14040 const char *error_msg
;
14042 if (identifier_p (expr
))
14043 /* Lookup the name we got back from the id-expression. */
14044 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14045 id_expr_start_token
->location
);
14048 && expr
!= error_mark_node
14049 && TREE_CODE (expr
) != TYPE_DECL
14050 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14051 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14052 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14054 /* Complete lookup of the id-expression. */
14055 expr
= (finish_id_expression
14056 (id_expression
, expr
, parser
->scope
, &idk
,
14057 /*integral_constant_expression_p=*/false,
14058 /*allow_non_integral_constant_expression_p=*/true,
14059 &non_integral_constant_expression_p
,
14060 /*template_p=*/false,
14062 /*address_p=*/false,
14063 /*template_arg_p=*/false,
14065 id_expr_start_token
->location
));
14067 if (expr
== error_mark_node
)
14068 /* We found an id-expression, but it was something that we
14069 should not have found. This is an error, not something
14070 we can recover from, so note that we found an
14071 id-expression and we'll recover as gracefully as
14073 id_expression_or_member_access_p
= true;
14077 && expr
!= error_mark_node
14078 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14079 /* We have an id-expression. */
14080 id_expression_or_member_access_p
= true;
14083 if (!id_expression_or_member_access_p
)
14085 /* Abort the id-expression parse. */
14086 cp_parser_abort_tentative_parse (parser
);
14088 /* Parsing tentatively, again. */
14089 cp_parser_parse_tentatively (parser
);
14091 /* Parse a class member access. */
14092 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14093 /*cast_p=*/false, /*decltype*/true,
14094 /*member_access_only_p=*/true, NULL
);
14097 && expr
!= error_mark_node
14098 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14099 /* We have an id-expression. */
14100 id_expression_or_member_access_p
= true;
14103 if (id_expression_or_member_access_p
)
14104 /* We have parsed the complete id-expression or member access. */
14105 cp_parser_parse_definitely (parser
);
14108 /* Abort our attempt to parse an id-expression or member access
14110 cp_parser_abort_tentative_parse (parser
);
14112 /* Parse a full expression. */
14113 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14114 /*decltype_p=*/true);
14120 /* Parse a `decltype' type. Returns the type.
14122 simple-type-specifier:
14123 decltype ( expression )
14125 decltype ( auto ) */
14128 cp_parser_decltype (cp_parser
*parser
)
14131 bool id_expression_or_member_access_p
= false;
14132 const char *saved_message
;
14133 bool saved_integral_constant_expression_p
;
14134 bool saved_non_integral_constant_expression_p
;
14135 bool saved_greater_than_is_operator_p
;
14136 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14138 if (start_token
->type
== CPP_DECLTYPE
)
14140 /* Already parsed. */
14141 cp_lexer_consume_token (parser
->lexer
);
14142 return saved_checks_value (start_token
->u
.tree_check_value
);
14145 /* Look for the `decltype' token. */
14146 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14147 return error_mark_node
;
14149 /* Parse the opening `('. */
14150 matching_parens parens
;
14151 if (!parens
.require_open (parser
))
14152 return error_mark_node
;
14154 /* decltype (auto) */
14155 if (cxx_dialect
>= cxx14
14156 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14158 cp_lexer_consume_token (parser
->lexer
);
14159 if (!parens
.require_close (parser
))
14160 return error_mark_node
;
14161 expr
= make_decltype_auto ();
14162 AUTO_IS_DECLTYPE (expr
) = true;
14166 /* Types cannot be defined in a `decltype' expression. Save away the
14168 saved_message
= parser
->type_definition_forbidden_message
;
14170 /* And create the new one. */
14171 parser
->type_definition_forbidden_message
14172 = G_("types may not be defined in %<decltype%> expressions");
14174 /* The restrictions on constant-expressions do not apply inside
14175 decltype expressions. */
14176 saved_integral_constant_expression_p
14177 = parser
->integral_constant_expression_p
;
14178 saved_non_integral_constant_expression_p
14179 = parser
->non_integral_constant_expression_p
;
14180 parser
->integral_constant_expression_p
= false;
14182 /* Within a parenthesized expression, a `>' token is always
14183 the greater-than operator. */
14184 saved_greater_than_is_operator_p
14185 = parser
->greater_than_is_operator_p
;
14186 parser
->greater_than_is_operator_p
= true;
14188 /* Do not actually evaluate the expression. */
14189 ++cp_unevaluated_operand
;
14191 /* Do not warn about problems with the expression. */
14192 ++c_inhibit_evaluation_warnings
;
14194 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14196 /* Go back to evaluating expressions. */
14197 --cp_unevaluated_operand
;
14198 --c_inhibit_evaluation_warnings
;
14200 /* The `>' token might be the end of a template-id or
14201 template-parameter-list now. */
14202 parser
->greater_than_is_operator_p
14203 = saved_greater_than_is_operator_p
;
14205 /* Restore the old message and the integral constant expression
14207 parser
->type_definition_forbidden_message
= saved_message
;
14208 parser
->integral_constant_expression_p
14209 = saved_integral_constant_expression_p
;
14210 parser
->non_integral_constant_expression_p
14211 = saved_non_integral_constant_expression_p
;
14213 /* Parse to the closing `)'. */
14214 if (!parens
.require_close (parser
))
14216 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14217 /*consume_paren=*/true);
14218 return error_mark_node
;
14221 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14222 tf_warning_or_error
);
14225 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14227 start_token
->type
= CPP_DECLTYPE
;
14228 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14229 start_token
->u
.tree_check_value
->value
= expr
;
14230 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14231 start_token
->keyword
= RID_MAX
;
14232 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14237 /* Special member functions [gram.special] */
14239 /* Parse a conversion-function-id.
14241 conversion-function-id:
14242 operator conversion-type-id
14244 Returns an IDENTIFIER_NODE representing the operator. */
14247 cp_parser_conversion_function_id (cp_parser
* parser
)
14251 tree saved_qualifying_scope
;
14252 tree saved_object_scope
;
14253 tree pushed_scope
= NULL_TREE
;
14255 /* Look for the `operator' token. */
14256 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14257 return error_mark_node
;
14258 /* When we parse the conversion-type-id, the current scope will be
14259 reset. However, we need that information in able to look up the
14260 conversion function later, so we save it here. */
14261 saved_scope
= parser
->scope
;
14262 saved_qualifying_scope
= parser
->qualifying_scope
;
14263 saved_object_scope
= parser
->object_scope
;
14264 /* We must enter the scope of the class so that the names of
14265 entities declared within the class are available in the
14266 conversion-type-id. For example, consider:
14273 S::operator I() { ... }
14275 In order to see that `I' is a type-name in the definition, we
14276 must be in the scope of `S'. */
14278 pushed_scope
= push_scope (saved_scope
);
14279 /* Parse the conversion-type-id. */
14280 type
= cp_parser_conversion_type_id (parser
);
14281 /* Leave the scope of the class, if any. */
14283 pop_scope (pushed_scope
);
14284 /* Restore the saved scope. */
14285 parser
->scope
= saved_scope
;
14286 parser
->qualifying_scope
= saved_qualifying_scope
;
14287 parser
->object_scope
= saved_object_scope
;
14288 /* If the TYPE is invalid, indicate failure. */
14289 if (type
== error_mark_node
)
14290 return error_mark_node
;
14291 return make_conv_op_name (type
);
14294 /* Parse a conversion-type-id:
14296 conversion-type-id:
14297 type-specifier-seq conversion-declarator [opt]
14299 Returns the TYPE specified. */
14302 cp_parser_conversion_type_id (cp_parser
* parser
)
14305 cp_decl_specifier_seq type_specifiers
;
14306 cp_declarator
*declarator
;
14307 tree type_specified
;
14308 const char *saved_message
;
14310 /* Parse the attributes. */
14311 attributes
= cp_parser_attributes_opt (parser
);
14313 saved_message
= parser
->type_definition_forbidden_message
;
14314 parser
->type_definition_forbidden_message
14315 = G_("types may not be defined in a conversion-type-id");
14317 /* Parse the type-specifiers. */
14318 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14319 /*is_trailing_return=*/false,
14322 parser
->type_definition_forbidden_message
= saved_message
;
14324 /* If that didn't work, stop. */
14325 if (type_specifiers
.type
== error_mark_node
)
14326 return error_mark_node
;
14327 /* Parse the conversion-declarator. */
14328 declarator
= cp_parser_conversion_declarator_opt (parser
);
14330 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14331 /*initialized=*/0, &attributes
);
14333 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14335 /* Don't give this error when parsing tentatively. This happens to
14336 work because we always parse this definitively once. */
14337 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14338 && type_uses_auto (type_specified
))
14340 if (cxx_dialect
< cxx14
)
14342 error ("invalid use of %<auto%> in conversion operator");
14343 return error_mark_node
;
14345 else if (template_parm_scope_p ())
14346 warning (0, "use of %<auto%> in member template "
14347 "conversion operator can never be deduced");
14350 return type_specified
;
14353 /* Parse an (optional) conversion-declarator.
14355 conversion-declarator:
14356 ptr-operator conversion-declarator [opt]
14360 static cp_declarator
*
14361 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14363 enum tree_code code
;
14364 tree class_type
, std_attributes
= NULL_TREE
;
14365 cp_cv_quals cv_quals
;
14367 /* We don't know if there's a ptr-operator next, or not. */
14368 cp_parser_parse_tentatively (parser
);
14369 /* Try the ptr-operator. */
14370 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14372 /* If it worked, look for more conversion-declarators. */
14373 if (cp_parser_parse_definitely (parser
))
14375 cp_declarator
*declarator
;
14377 /* Parse another optional declarator. */
14378 declarator
= cp_parser_conversion_declarator_opt (parser
);
14380 declarator
= cp_parser_make_indirect_declarator
14381 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14389 /* Parse an (optional) ctor-initializer.
14392 : mem-initializer-list */
14395 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14397 /* If the next token is not a `:', then there is no
14398 ctor-initializer. */
14399 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14401 /* Do default initialization of any bases and members. */
14402 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14403 finish_mem_initializers (NULL_TREE
);
14407 /* Consume the `:' token. */
14408 cp_lexer_consume_token (parser
->lexer
);
14409 /* And the mem-initializer-list. */
14410 cp_parser_mem_initializer_list (parser
);
14413 /* Parse a mem-initializer-list.
14415 mem-initializer-list:
14416 mem-initializer ... [opt]
14417 mem-initializer ... [opt] , mem-initializer-list */
14420 cp_parser_mem_initializer_list (cp_parser
* parser
)
14422 tree mem_initializer_list
= NULL_TREE
;
14423 tree target_ctor
= error_mark_node
;
14424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14426 /* Let the semantic analysis code know that we are starting the
14427 mem-initializer-list. */
14428 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14429 error_at (token
->location
,
14430 "only constructors take member initializers");
14432 /* Loop through the list. */
14435 tree mem_initializer
;
14437 token
= cp_lexer_peek_token (parser
->lexer
);
14438 /* Parse the mem-initializer. */
14439 mem_initializer
= cp_parser_mem_initializer (parser
);
14440 /* If the next token is a `...', we're expanding member initializers. */
14441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14443 /* Consume the `...'. */
14444 cp_lexer_consume_token (parser
->lexer
);
14446 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14447 can be expanded but members cannot. */
14448 if (mem_initializer
!= error_mark_node
14449 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14451 error_at (token
->location
,
14452 "cannot expand initializer for member %qD",
14453 TREE_PURPOSE (mem_initializer
));
14454 mem_initializer
= error_mark_node
;
14457 /* Construct the pack expansion type. */
14458 if (mem_initializer
!= error_mark_node
)
14459 mem_initializer
= make_pack_expansion (mem_initializer
);
14461 if (target_ctor
!= error_mark_node
14462 && mem_initializer
!= error_mark_node
)
14464 error ("mem-initializer for %qD follows constructor delegation",
14465 TREE_PURPOSE (mem_initializer
));
14466 mem_initializer
= error_mark_node
;
14468 /* Look for a target constructor. */
14469 if (mem_initializer
!= error_mark_node
14470 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14471 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14473 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14474 if (mem_initializer_list
)
14476 error ("constructor delegation follows mem-initializer for %qD",
14477 TREE_PURPOSE (mem_initializer_list
));
14478 mem_initializer
= error_mark_node
;
14480 target_ctor
= mem_initializer
;
14482 /* Add it to the list, unless it was erroneous. */
14483 if (mem_initializer
!= error_mark_node
)
14485 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14486 mem_initializer_list
= mem_initializer
;
14488 /* If the next token is not a `,', we're done. */
14489 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14491 /* Consume the `,' token. */
14492 cp_lexer_consume_token (parser
->lexer
);
14495 /* Perform semantic analysis. */
14496 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14497 finish_mem_initializers (mem_initializer_list
);
14500 /* Parse a mem-initializer.
14503 mem-initializer-id ( expression-list [opt] )
14504 mem-initializer-id braced-init-list
14509 ( expression-list [opt] )
14511 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14512 class) or FIELD_DECL (for a non-static data member) to initialize;
14513 the TREE_VALUE is the expression-list. An empty initialization
14514 list is represented by void_list_node. */
14517 cp_parser_mem_initializer (cp_parser
* parser
)
14519 tree mem_initializer_id
;
14520 tree expression_list
;
14522 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14524 /* Find out what is being initialized. */
14525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14527 permerror (token
->location
,
14528 "anachronistic old-style base class initializer");
14529 mem_initializer_id
= NULL_TREE
;
14533 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14534 if (mem_initializer_id
== error_mark_node
)
14535 return mem_initializer_id
;
14537 member
= expand_member_init (mem_initializer_id
);
14538 if (member
&& !DECL_P (member
))
14539 in_base_initializer
= 1;
14541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14543 bool expr_non_constant_p
;
14544 cp_lexer_set_source_position (parser
->lexer
);
14545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14546 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14547 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14548 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14552 vec
<tree
, va_gc
> *vec
;
14553 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14555 /*allow_expansion_p=*/true,
14556 /*non_constant_p=*/NULL
);
14558 return error_mark_node
;
14559 expression_list
= build_tree_list_vec (vec
);
14560 release_tree_vector (vec
);
14563 if (expression_list
== error_mark_node
)
14564 return error_mark_node
;
14565 if (!expression_list
)
14566 expression_list
= void_type_node
;
14568 in_base_initializer
= 0;
14570 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14573 /* Parse a mem-initializer-id.
14575 mem-initializer-id:
14576 :: [opt] nested-name-specifier [opt] class-name
14577 decltype-specifier (C++11)
14580 Returns a TYPE indicating the class to be initialized for the first
14581 production (and the second in C++11). Returns an IDENTIFIER_NODE
14582 indicating the data member to be initialized for the last production. */
14585 cp_parser_mem_initializer_id (cp_parser
* parser
)
14587 bool global_scope_p
;
14588 bool nested_name_specifier_p
;
14589 bool template_p
= false;
14592 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14594 /* `typename' is not allowed in this context ([temp.res]). */
14595 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14597 error_at (token
->location
,
14598 "keyword %<typename%> not allowed in this context (a qualified "
14599 "member initializer is implicitly a type)");
14600 cp_lexer_consume_token (parser
->lexer
);
14602 /* Look for the optional `::' operator. */
14604 = (cp_parser_global_scope_opt (parser
,
14605 /*current_scope_valid_p=*/false)
14607 /* Look for the optional nested-name-specifier. The simplest way to
14612 The keyword `typename' is not permitted in a base-specifier or
14613 mem-initializer; in these contexts a qualified name that
14614 depends on a template-parameter is implicitly assumed to be a
14617 is to assume that we have seen the `typename' keyword at this
14619 nested_name_specifier_p
14620 = (cp_parser_nested_name_specifier_opt (parser
,
14621 /*typename_keyword_p=*/true,
14622 /*check_dependency_p=*/true,
14624 /*is_declaration=*/true)
14626 if (nested_name_specifier_p
)
14627 template_p
= cp_parser_optional_template_keyword (parser
);
14628 /* If there is a `::' operator or a nested-name-specifier, then we
14629 are definitely looking for a class-name. */
14630 if (global_scope_p
|| nested_name_specifier_p
)
14631 return cp_parser_class_name (parser
,
14632 /*typename_keyword_p=*/true,
14633 /*template_keyword_p=*/template_p
,
14635 /*check_dependency_p=*/true,
14636 /*class_head_p=*/false,
14637 /*is_declaration=*/true);
14638 /* Otherwise, we could also be looking for an ordinary identifier. */
14639 cp_parser_parse_tentatively (parser
);
14640 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14641 /* Try a decltype-specifier. */
14642 id
= cp_parser_decltype (parser
);
14644 /* Otherwise, try a class-name. */
14645 id
= cp_parser_class_name (parser
,
14646 /*typename_keyword_p=*/true,
14647 /*template_keyword_p=*/false,
14649 /*check_dependency_p=*/true,
14650 /*class_head_p=*/false,
14651 /*is_declaration=*/true);
14652 /* If we found one, we're done. */
14653 if (cp_parser_parse_definitely (parser
))
14655 /* Otherwise, look for an ordinary identifier. */
14656 return cp_parser_identifier (parser
);
14659 /* Overloading [gram.over] */
14661 /* Parse an operator-function-id.
14663 operator-function-id:
14666 Returns an IDENTIFIER_NODE for the operator which is a
14667 human-readable spelling of the identifier, e.g., `operator +'. */
14670 cp_parser_operator_function_id (cp_parser
* parser
)
14672 /* Look for the `operator' keyword. */
14673 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14674 return error_mark_node
;
14675 /* And then the name of the operator itself. */
14676 return cp_parser_operator (parser
);
14679 /* Return an identifier node for a user-defined literal operator.
14680 The suffix identifier is chained to the operator name identifier. */
14683 cp_literal_operator_id (const char* name
)
14686 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14687 + strlen (name
) + 10);
14688 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14689 identifier
= get_identifier (buffer
);
14694 /* Parse an operator.
14697 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14698 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14699 || ++ -- , ->* -> () []
14706 Returns an IDENTIFIER_NODE for the operator which is a
14707 human-readable spelling of the identifier, e.g., `operator +'. */
14710 cp_parser_operator (cp_parser
* parser
)
14712 tree id
= NULL_TREE
;
14716 /* Peek at the next token. */
14717 token
= cp_lexer_peek_token (parser
->lexer
);
14719 location_t start_loc
= token
->location
;
14721 /* Figure out which operator we have. */
14722 enum tree_code op
= ERROR_MARK
;
14723 bool assop
= false;
14724 bool consumed
= false;
14725 switch (token
->type
)
14729 /* The keyword should be either `new' or `delete'. */
14730 if (token
->keyword
== RID_NEW
)
14732 else if (token
->keyword
== RID_DELETE
)
14737 /* Consume the `new' or `delete' token. */
14738 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14740 /* Peek at the next token. */
14741 token
= cp_lexer_peek_token (parser
->lexer
);
14742 /* If it's a `[' token then this is the array variant of the
14744 if (token
->type
== CPP_OPEN_SQUARE
)
14746 /* Consume the `[' token. */
14747 cp_lexer_consume_token (parser
->lexer
);
14748 /* Look for the `]' token. */
14749 if (cp_token
*close_token
14750 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14751 end_loc
= close_token
->location
;
14752 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14754 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14772 op
= TRUNC_DIV_EXPR
;
14776 op
= TRUNC_MOD_EXPR
;
14796 op
= TRUTH_NOT_EXPR
;
14829 op
= TRUNC_DIV_EXPR
;
14834 op
= TRUNC_MOD_EXPR
;
14860 case CPP_LSHIFT_EQ
:
14865 case CPP_RSHIFT_EQ
:
14882 case CPP_GREATER_EQ
:
14887 op
= TRUTH_ANDIF_EXPR
;
14891 op
= TRUTH_ORIF_EXPR
;
14894 case CPP_PLUS_PLUS
:
14895 op
= POSTINCREMENT_EXPR
;
14898 case CPP_MINUS_MINUS
:
14899 op
= PREDECREMENT_EXPR
;
14903 op
= COMPOUND_EXPR
;
14906 case CPP_DEREF_STAR
:
14911 op
= COMPONENT_REF
;
14914 case CPP_OPEN_PAREN
:
14916 /* Consume the `('. */
14917 matching_parens parens
;
14918 parens
.consume_open (parser
);
14919 /* Look for the matching `)'. */
14920 parens
.require_close (parser
);
14926 case CPP_OPEN_SQUARE
:
14927 /* Consume the `['. */
14928 cp_lexer_consume_token (parser
->lexer
);
14929 /* Look for the matching `]'. */
14930 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14935 case CPP_UTF8STRING
:
14936 case CPP_UTF8STRING_USERDEF
:
14943 case CPP_STRING_USERDEF
:
14944 case CPP_WSTRING_USERDEF
:
14945 case CPP_STRING16_USERDEF
:
14946 case CPP_STRING32_USERDEF
:
14948 tree str
, string_tree
;
14951 if (cxx_dialect
== cxx98
)
14952 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14954 /* Consume the string. */
14955 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14956 /*wide_ok=*/true, /*lookup_udlit=*/false);
14957 if (str
== error_mark_node
)
14958 return error_mark_node
;
14959 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14961 string_tree
= USERDEF_LITERAL_VALUE (str
);
14962 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14967 /* Look for the suffix identifier. */
14968 token
= cp_lexer_peek_token (parser
->lexer
);
14969 if (token
->type
== CPP_NAME
)
14970 id
= cp_parser_identifier (parser
);
14971 else if (token
->type
== CPP_KEYWORD
)
14973 error ("unexpected keyword;"
14974 " remove space between quotes and suffix identifier");
14975 return error_mark_node
;
14979 error ("expected suffix identifier");
14980 return error_mark_node
;
14983 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14984 (TREE_TYPE (TREE_TYPE (string_tree
))));
14985 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14988 error ("expected empty string after %<operator%> keyword");
14989 return error_mark_node
;
14991 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14994 error ("invalid encoding prefix in literal operator");
14995 return error_mark_node
;
14997 if (id
!= error_mark_node
)
14999 const char *name
= IDENTIFIER_POINTER (id
);
15000 id
= cp_literal_operator_id (name
);
15006 /* Anything else is an error. */
15010 /* If we have selected an identifier, we need to consume the
15012 if (op
!= ERROR_MARK
)
15014 id
= ovl_op_identifier (assop
, op
);
15016 cp_lexer_consume_token (parser
->lexer
);
15018 /* Otherwise, no valid operator name was present. */
15021 cp_parser_error (parser
, "expected operator");
15022 id
= error_mark_node
;
15025 return cp_expr (id
, start_loc
);
15028 /* Parse a template-declaration.
15030 template-declaration:
15031 export [opt] template < template-parameter-list > declaration
15033 If MEMBER_P is TRUE, this template-declaration occurs within a
15036 The grammar rule given by the standard isn't correct. What
15037 is really meant is:
15039 template-declaration:
15040 export [opt] template-parameter-list-seq
15041 decl-specifier-seq [opt] init-declarator [opt] ;
15042 export [opt] template-parameter-list-seq
15043 function-definition
15045 template-parameter-list-seq:
15046 template-parameter-list-seq [opt]
15047 template < template-parameter-list >
15049 Concept Extensions:
15051 template-parameter-list-seq:
15052 template < template-parameter-list > requires-clause [opt]
15055 requires logical-or-expression */
15058 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15060 /* Check for `export'. */
15061 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15063 /* Consume the `export' token. */
15064 cp_lexer_consume_token (parser
->lexer
);
15065 /* Warn that we do not support `export'. */
15066 warning (0, "keyword %<export%> not implemented, and will be ignored");
15069 cp_parser_template_declaration_after_export (parser
, member_p
);
15072 /* Parse a template-parameter-list.
15074 template-parameter-list:
15076 template-parameter-list , template-parameter
15078 Returns a TREE_LIST. Each node represents a template parameter.
15079 The nodes are connected via their TREE_CHAINs. */
15082 cp_parser_template_parameter_list (cp_parser
* parser
)
15084 tree parameter_list
= NULL_TREE
;
15086 begin_template_parm_list ();
15088 /* The loop below parses the template parms. We first need to know
15089 the total number of template parms to be able to compute proper
15090 canonical types of each dependent type. So after the loop, when
15091 we know the total number of template parms,
15092 end_template_parm_list computes the proper canonical types and
15093 fixes up the dependent types accordingly. */
15098 bool is_parameter_pack
;
15099 location_t parm_loc
;
15101 /* Parse the template-parameter. */
15102 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15103 parameter
= cp_parser_template_parameter (parser
,
15105 &is_parameter_pack
);
15106 /* Add it to the list. */
15107 if (parameter
!= error_mark_node
)
15108 parameter_list
= process_template_parm (parameter_list
,
15112 is_parameter_pack
);
15115 tree err_parm
= build_tree_list (parameter
, parameter
);
15116 parameter_list
= chainon (parameter_list
, err_parm
);
15119 /* If the next token is not a `,', we're done. */
15120 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15122 /* Otherwise, consume the `,' token. */
15123 cp_lexer_consume_token (parser
->lexer
);
15126 return end_template_parm_list (parameter_list
);
15129 /* Parse a introduction-list.
15132 introduced-parameter
15133 introduction-list , introduced-parameter
15135 introduced-parameter:
15136 ...[opt] identifier
15138 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15139 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15140 WILDCARD_DECL will also have DECL_NAME set and token location in
15141 DECL_SOURCE_LOCATION. */
15144 cp_parser_introduction_list (cp_parser
*parser
)
15146 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15150 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15152 cp_lexer_consume_token (parser
->lexer
);
15154 /* Build placeholder. */
15155 tree parm
= build_nt (WILDCARD_DECL
);
15156 DECL_SOURCE_LOCATION (parm
)
15157 = cp_lexer_peek_token (parser
->lexer
)->location
;
15158 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15159 WILDCARD_PACK_P (parm
) = is_pack
;
15160 vec_safe_push (introduction_vec
, parm
);
15162 /* If the next token is not a `,', we're done. */
15163 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15165 /* Otherwise, consume the `,' token. */
15166 cp_lexer_consume_token (parser
->lexer
);
15169 /* Convert the vec into a TREE_VEC. */
15170 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15173 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15174 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15176 release_tree_vector (introduction_vec
);
15177 return introduction_list
;
15180 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15181 is an abstract declarator. */
15183 static inline cp_declarator
*
15184 get_id_declarator (cp_declarator
*declarator
)
15186 cp_declarator
*d
= declarator
;
15187 while (d
&& d
->kind
!= cdk_id
)
15192 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15193 is an abstract declarator. */
15196 get_unqualified_id (cp_declarator
*declarator
)
15198 declarator
= get_id_declarator (declarator
);
15200 return declarator
->u
.id
.unqualified_name
;
15205 /* Returns true if DECL represents a constrained-parameter. */
15208 is_constrained_parameter (tree decl
)
15211 && TREE_CODE (decl
) == TYPE_DECL
15212 && CONSTRAINED_PARM_CONCEPT (decl
)
15213 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15216 /* Returns true if PARM declares a constrained-parameter. */
15219 is_constrained_parameter (cp_parameter_declarator
*parm
)
15221 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15224 /* Check that the type parameter is only a declarator-id, and that its
15225 type is not cv-qualified. */
15228 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15229 cp_parameter_declarator
*parm
)
15231 if (!parm
->declarator
)
15234 if (parm
->declarator
->kind
!= cdk_id
)
15236 cp_parser_error (parser
, "invalid constrained type parameter");
15240 /* Don't allow cv-qualified type parameters. */
15241 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15242 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15244 cp_parser_error (parser
, "cv-qualified type parameter");
15251 /* Finish parsing/processing a template type parameter and checking
15252 various restrictions. */
15255 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15257 cp_parameter_declarator
* parmdecl
)
15259 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15260 return finish_template_type_parm (class_type_node
, id
);
15262 return error_mark_node
;
15266 finish_constrained_template_template_parm (tree proto
, tree id
)
15268 /* FIXME: This should probably be copied, and we may need to adjust
15269 the template parameter depths. */
15270 tree saved_parms
= current_template_parms
;
15271 begin_template_parm_list ();
15272 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15273 end_template_parm_list ();
15275 tree parm
= finish_template_template_parm (class_type_node
, id
);
15276 current_template_parms
= saved_parms
;
15281 /* Finish parsing/processing a template template parameter by borrowing
15282 the template parameter list from the prototype parameter. */
15285 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15288 cp_parameter_declarator
*parmdecl
)
15290 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15291 return error_mark_node
;
15292 return finish_constrained_template_template_parm (proto
, id
);
15295 /* Create a new non-type template parameter from the given PARM
15299 constrained_non_type_template_parm (bool *is_non_type
,
15300 cp_parameter_declarator
*parm
)
15302 *is_non_type
= true;
15303 cp_declarator
*decl
= parm
->declarator
;
15304 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15305 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15306 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15309 /* Build a constrained template parameter based on the PARMDECL
15310 declarator. The type of PARMDECL is the constrained type, which
15311 refers to the prototype template parameter that ultimately
15312 specifies the type of the declared parameter. */
15315 finish_constrained_parameter (cp_parser
*parser
,
15316 cp_parameter_declarator
*parmdecl
,
15318 bool *is_parameter_pack
)
15320 tree decl
= parmdecl
->decl_specifiers
.type
;
15321 tree id
= get_unqualified_id (parmdecl
->declarator
);
15322 tree def
= parmdecl
->default_argument
;
15323 tree proto
= DECL_INITIAL (decl
);
15325 /* A template parameter constrained by a variadic concept shall also
15326 be declared as a template parameter pack. */
15327 bool is_variadic
= template_parameter_pack_p (proto
);
15328 if (is_variadic
&& !*is_parameter_pack
)
15329 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15331 /* Build the parameter. Return an error if the declarator was invalid. */
15333 if (TREE_CODE (proto
) == TYPE_DECL
)
15334 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15335 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15336 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15339 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15340 if (parm
== error_mark_node
)
15341 return error_mark_node
;
15343 /* Finish the parameter decl and create a node attaching the
15344 default argument and constraint. */
15345 parm
= build_tree_list (def
, parm
);
15346 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15351 /* Returns true if the parsed type actually represents the declaration
15352 of a type template-parameter. */
15355 declares_constrained_type_template_parameter (tree type
)
15357 return (is_constrained_parameter (type
)
15358 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15362 /* Returns true if the parsed type actually represents the declaration of
15363 a template template-parameter. */
15366 declares_constrained_template_template_parameter (tree type
)
15368 return (is_constrained_parameter (type
)
15369 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15372 /* Parse a default argument for a type template-parameter.
15373 Note that diagnostics are handled in cp_parser_template_parameter. */
15376 cp_parser_default_type_template_argument (cp_parser
*parser
)
15378 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15380 /* Consume the `=' token. */
15381 cp_lexer_consume_token (parser
->lexer
);
15383 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15385 /* Parse the default-argument. */
15386 push_deferring_access_checks (dk_no_deferred
);
15387 tree default_argument
= cp_parser_type_id (parser
);
15388 pop_deferring_access_checks ();
15390 if (flag_concepts
&& type_uses_auto (default_argument
))
15392 error_at (token
->location
,
15393 "invalid use of %<auto%> in default template argument");
15394 return error_mark_node
;
15397 return default_argument
;
15400 /* Parse a default argument for a template template-parameter. */
15403 cp_parser_default_template_template_argument (cp_parser
*parser
)
15405 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15409 /* Consume the `='. */
15410 cp_lexer_consume_token (parser
->lexer
);
15411 /* Parse the id-expression. */
15412 push_deferring_access_checks (dk_no_deferred
);
15413 /* save token before parsing the id-expression, for error
15415 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15416 tree default_argument
15417 = cp_parser_id_expression (parser
,
15418 /*template_keyword_p=*/false,
15419 /*check_dependency_p=*/true,
15420 /*template_p=*/&is_template
,
15421 /*declarator_p=*/false,
15422 /*optional_p=*/false);
15423 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15424 /* If the id-expression was a template-id that refers to
15425 a template-class, we already have the declaration here,
15426 so no further lookup is needed. */
15429 /* Look up the name. */
15431 = cp_parser_lookup_name (parser
, default_argument
,
15433 /*is_template=*/is_template
,
15434 /*is_namespace=*/false,
15435 /*check_dependency=*/true,
15436 /*ambiguous_decls=*/NULL
,
15438 /* See if the default argument is valid. */
15439 default_argument
= check_template_template_default_arg (default_argument
);
15440 pop_deferring_access_checks ();
15441 return default_argument
;
15444 /* Parse a template-parameter.
15446 template-parameter:
15448 parameter-declaration
15450 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15451 the parameter. The TREE_PURPOSE is the default value, if any.
15452 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15453 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15454 set to true iff this parameter is a parameter pack. */
15457 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15458 bool *is_parameter_pack
)
15461 cp_parameter_declarator
*parameter_declarator
;
15464 /* Assume it is a type parameter or a template parameter. */
15465 *is_non_type
= false;
15466 /* Assume it not a parameter pack. */
15467 *is_parameter_pack
= false;
15468 /* Peek at the next token. */
15469 token
= cp_lexer_peek_token (parser
->lexer
);
15470 /* If it is `template', we have a type-parameter. */
15471 if (token
->keyword
== RID_TEMPLATE
)
15472 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15473 /* If it is `class' or `typename' we do not know yet whether it is a
15474 type parameter or a non-type parameter. Consider:
15476 template <typename T, typename T::X X> ...
15480 template <class C, class D*> ...
15482 Here, the first parameter is a type parameter, and the second is
15483 a non-type parameter. We can tell by looking at the token after
15484 the identifier -- if it is a `,', `=', or `>' then we have a type
15486 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15488 /* Peek at the token after `class' or `typename'. */
15489 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15490 /* If it's an ellipsis, we have a template type parameter
15492 if (token
->type
== CPP_ELLIPSIS
)
15493 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15494 /* If it's an identifier, skip it. */
15495 if (token
->type
== CPP_NAME
)
15496 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15497 /* Now, see if the token looks like the end of a template
15499 if (token
->type
== CPP_COMMA
15500 || token
->type
== CPP_EQ
15501 || token
->type
== CPP_GREATER
)
15502 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15505 /* Otherwise, it is a non-type parameter or a constrained parameter.
15509 When parsing a default template-argument for a non-type
15510 template-parameter, the first non-nested `>' is taken as the end
15511 of the template parameter-list rather than a greater-than
15513 parameter_declarator
15514 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15515 /*parenthesized_p=*/NULL
);
15517 if (!parameter_declarator
)
15518 return error_mark_node
;
15520 /* If the parameter declaration is marked as a parameter pack, set
15521 *IS_PARAMETER_PACK to notify the caller. */
15522 if (parameter_declarator
->template_parameter_pack_p
)
15523 *is_parameter_pack
= true;
15525 if (parameter_declarator
->default_argument
)
15527 /* Can happen in some cases of erroneous input (c++/34892). */
15528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15529 /* Consume the `...' for better error recovery. */
15530 cp_lexer_consume_token (parser
->lexer
);
15533 // The parameter may have been constrained.
15534 if (is_constrained_parameter (parameter_declarator
))
15535 return finish_constrained_parameter (parser
,
15536 parameter_declarator
,
15538 is_parameter_pack
);
15540 // Now we're sure that the parameter is a non-type parameter.
15541 *is_non_type
= true;
15543 parm
= grokdeclarator (parameter_declarator
->declarator
,
15544 ¶meter_declarator
->decl_specifiers
,
15545 TPARM
, /*initialized=*/0,
15546 /*attrlist=*/NULL
);
15547 if (parm
== error_mark_node
)
15548 return error_mark_node
;
15550 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15553 /* Parse a type-parameter.
15556 class identifier [opt]
15557 class identifier [opt] = type-id
15558 typename identifier [opt]
15559 typename identifier [opt] = type-id
15560 template < template-parameter-list > class identifier [opt]
15561 template < template-parameter-list > class identifier [opt]
15564 GNU Extension (variadic templates):
15567 class ... identifier [opt]
15568 typename ... identifier [opt]
15570 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15571 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15572 the declaration of the parameter.
15574 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15577 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15582 /* Look for a keyword to tell us what kind of parameter this is. */
15583 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15585 return error_mark_node
;
15587 switch (token
->keyword
)
15593 tree default_argument
;
15595 /* If the next token is an ellipsis, we have a template
15597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15599 /* Consume the `...' token. */
15600 cp_lexer_consume_token (parser
->lexer
);
15601 maybe_warn_variadic_templates ();
15603 *is_parameter_pack
= true;
15606 /* If the next token is an identifier, then it names the
15608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15609 identifier
= cp_parser_identifier (parser
);
15611 identifier
= NULL_TREE
;
15613 /* Create the parameter. */
15614 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15616 /* If the next token is an `=', we have a default argument. */
15617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15620 = cp_parser_default_type_template_argument (parser
);
15622 /* Template parameter packs cannot have default
15624 if (*is_parameter_pack
)
15627 error_at (token
->location
,
15628 "template parameter pack %qD cannot have a "
15629 "default argument", identifier
);
15631 error_at (token
->location
,
15632 "template parameter packs cannot have "
15633 "default arguments");
15634 default_argument
= NULL_TREE
;
15636 else if (check_for_bare_parameter_packs (default_argument
))
15637 default_argument
= error_mark_node
;
15640 default_argument
= NULL_TREE
;
15642 /* Create the combined representation of the parameter and the
15643 default argument. */
15644 parameter
= build_tree_list (default_argument
, parameter
);
15651 tree default_argument
;
15653 /* Look for the `<'. */
15654 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15655 /* Parse the template-parameter-list. */
15656 cp_parser_template_parameter_list (parser
);
15657 /* Look for the `>'. */
15658 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15660 // If template requirements are present, parse them.
15663 tree reqs
= get_shorthand_constraints (current_template_parms
);
15664 if (tree r
= cp_parser_requires_clause_opt (parser
))
15665 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15666 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15669 /* Look for the `class' or 'typename' keywords. */
15670 cp_parser_type_parameter_key (parser
);
15671 /* If the next token is an ellipsis, we have a template
15673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15675 /* Consume the `...' token. */
15676 cp_lexer_consume_token (parser
->lexer
);
15677 maybe_warn_variadic_templates ();
15679 *is_parameter_pack
= true;
15681 /* If the next token is an `=', then there is a
15682 default-argument. If the next token is a `>', we are at
15683 the end of the parameter-list. If the next token is a `,',
15684 then we are at the end of this parameter. */
15685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15687 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15689 identifier
= cp_parser_identifier (parser
);
15690 /* Treat invalid names as if the parameter were nameless. */
15691 if (identifier
== error_mark_node
)
15692 identifier
= NULL_TREE
;
15695 identifier
= NULL_TREE
;
15697 /* Create the template parameter. */
15698 parameter
= finish_template_template_parm (class_type_node
,
15701 /* If the next token is an `=', then there is a
15702 default-argument. */
15703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15706 = cp_parser_default_template_template_argument (parser
);
15708 /* Template parameter packs cannot have default
15710 if (*is_parameter_pack
)
15713 error_at (token
->location
,
15714 "template parameter pack %qD cannot "
15715 "have a default argument",
15718 error_at (token
->location
, "template parameter packs cannot "
15719 "have default arguments");
15720 default_argument
= NULL_TREE
;
15724 default_argument
= NULL_TREE
;
15726 /* Create the combined representation of the parameter and the
15727 default argument. */
15728 parameter
= build_tree_list (default_argument
, parameter
);
15733 gcc_unreachable ();
15740 /* Parse a template-id.
15743 template-name < template-argument-list [opt] >
15745 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15746 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15747 returned. Otherwise, if the template-name names a function, or set
15748 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15749 names a class, returns a TYPE_DECL for the specialization.
15751 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15752 uninstantiated templates. */
15755 cp_parser_template_id (cp_parser
*parser
,
15756 bool template_keyword_p
,
15757 bool check_dependency_p
,
15758 enum tag_types tag_type
,
15759 bool is_declaration
)
15764 cp_token_position start_of_id
= 0;
15765 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15766 bool is_identifier
;
15768 /* If the next token corresponds to a template-id, there is no need
15770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15771 if (token
->type
== CPP_TEMPLATE_ID
)
15773 cp_lexer_consume_token (parser
->lexer
);
15774 return saved_checks_value (token
->u
.tree_check_value
);
15777 /* Avoid performing name lookup if there is no possibility of
15778 finding a template-id. */
15779 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15780 || (token
->type
== CPP_NAME
15781 && !cp_parser_nth_token_starts_template_argument_list_p
15784 cp_parser_error (parser
, "expected template-id");
15785 return error_mark_node
;
15788 /* Remember where the template-id starts. */
15789 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15790 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15792 push_deferring_access_checks (dk_deferred
);
15794 /* Parse the template-name. */
15795 is_identifier
= false;
15796 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15797 check_dependency_p
,
15801 if (templ
== error_mark_node
|| is_identifier
)
15803 pop_deferring_access_checks ();
15807 /* Since we're going to preserve any side-effects from this parse, set up a
15808 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15809 in the template arguments. */
15810 tentative_firewall
firewall (parser
);
15812 /* If we find the sequence `[:' after a template-name, it's probably
15813 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15814 parse correctly the argument list. */
15815 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15816 == CPP_OPEN_SQUARE
)
15817 && next_token
->flags
& DIGRAPH
15818 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15820 && !(next_token_2
->flags
& PREV_WHITE
))
15822 cp_parser_parse_tentatively (parser
);
15823 /* Change `:' into `::'. */
15824 next_token_2
->type
= CPP_SCOPE
;
15825 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15827 cp_lexer_consume_token (parser
->lexer
);
15829 /* Parse the arguments. */
15830 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15831 if (!cp_parser_parse_definitely (parser
))
15833 /* If we couldn't parse an argument list, then we revert our changes
15834 and return simply an error. Maybe this is not a template-id
15836 next_token_2
->type
= CPP_COLON
;
15837 cp_parser_error (parser
, "expected %<<%>");
15838 pop_deferring_access_checks ();
15839 return error_mark_node
;
15841 /* Otherwise, emit an error about the invalid digraph, but continue
15842 parsing because we got our argument list. */
15843 if (permerror (next_token
->location
,
15844 "%<<::%> cannot begin a template-argument list"))
15846 static bool hint
= false;
15847 inform (next_token
->location
,
15848 "%<<:%> is an alternate spelling for %<[%>."
15849 " Insert whitespace between %<<%> and %<::%>");
15850 if (!hint
&& !flag_permissive
)
15852 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15853 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15854 "accept your code)");
15861 /* Look for the `<' that starts the template-argument-list. */
15862 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15864 pop_deferring_access_checks ();
15865 return error_mark_node
;
15867 /* Parse the arguments. */
15868 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15871 /* Set the location to be of the form:
15872 template-name < template-argument-list [opt] >
15873 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15874 with caret == start at the start of the template-name,
15875 ranging until the closing '>'. */
15876 location_t finish_loc
15877 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15878 location_t combined_loc
15879 = make_location (token
->location
, token
->location
, finish_loc
);
15881 /* Build a representation of the specialization. */
15882 if (identifier_p (templ
))
15883 template_id
= build_min_nt_loc (combined_loc
,
15886 else if (DECL_TYPE_TEMPLATE_P (templ
)
15887 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15889 bool entering_scope
;
15890 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15891 template (rather than some instantiation thereof) only if
15892 is not nested within some other construct. For example, in
15893 "template <typename T> void f(T) { A<T>::", A<T> is just an
15894 instantiation of A. */
15895 entering_scope
= (template_parm_scope_p ()
15896 && cp_lexer_next_token_is (parser
->lexer
,
15899 = finish_template_type (templ
, arguments
, entering_scope
);
15901 /* A template-like identifier may be a partial concept id. */
15902 else if (flag_concepts
15903 && (template_id
= (cp_parser_maybe_partial_concept_id
15904 (parser
, templ
, arguments
))))
15905 return template_id
;
15906 else if (variable_template_p (templ
))
15908 template_id
= lookup_template_variable (templ
, arguments
);
15909 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15910 SET_EXPR_LOCATION (template_id
, combined_loc
);
15914 /* If it's not a class-template or a template-template, it should be
15915 a function-template. */
15916 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15917 || TREE_CODE (templ
) == OVERLOAD
15918 || BASELINK_P (templ
)));
15920 template_id
= lookup_template_function (templ
, arguments
);
15921 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15922 SET_EXPR_LOCATION (template_id
, combined_loc
);
15925 /* If parsing tentatively, replace the sequence of tokens that makes
15926 up the template-id with a CPP_TEMPLATE_ID token. That way,
15927 should we re-parse the token stream, we will not have to repeat
15928 the effort required to do the parse, nor will we issue duplicate
15929 error messages about problems during instantiation of the
15932 /* Don't do this if we had a parse error in a declarator; re-parsing
15933 might succeed if a name changes meaning (60361). */
15934 && !(cp_parser_error_occurred (parser
)
15935 && cp_parser_parsing_tentatively (parser
)
15936 && parser
->in_declarator_p
))
15938 /* Reset the contents of the START_OF_ID token. */
15939 token
->type
= CPP_TEMPLATE_ID
;
15940 token
->location
= combined_loc
;
15942 /* We must mark the lookup as kept, so we don't throw it away on
15943 the first parse. */
15944 if (is_overloaded_fn (template_id
))
15945 lookup_keep (get_fns (template_id
), true);
15947 /* Retrieve any deferred checks. Do not pop this access checks yet
15948 so the memory will not be reclaimed during token replacing below. */
15949 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15950 token
->u
.tree_check_value
->value
= template_id
;
15951 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15952 token
->keyword
= RID_MAX
;
15954 /* Purge all subsequent tokens. */
15955 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15957 /* ??? Can we actually assume that, if template_id ==
15958 error_mark_node, we will have issued a diagnostic to the
15959 user, as opposed to simply marking the tentative parse as
15961 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15962 error_at (token
->location
, "parse error in template argument list");
15965 pop_to_parent_deferring_access_checks ();
15966 return template_id
;
15969 /* Parse a template-name.
15974 The standard should actually say:
15978 operator-function-id
15980 A defect report has been filed about this issue.
15982 A conversion-function-id cannot be a template name because they cannot
15983 be part of a template-id. In fact, looking at this code:
15985 a.operator K<int>()
15987 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15988 It is impossible to call a templated conversion-function-id with an
15989 explicit argument list, since the only allowed template parameter is
15990 the type to which it is converting.
15992 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15993 `template' keyword, in a construction like:
15997 In that case `f' is taken to be a template-name, even though there
15998 is no way of knowing for sure.
16000 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16001 name refers to a set of overloaded functions, at least one of which
16002 is a template, or an IDENTIFIER_NODE with the name of the template,
16003 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16004 names are looked up inside uninstantiated templates. */
16007 cp_parser_template_name (cp_parser
* parser
,
16008 bool template_keyword_p
,
16009 bool check_dependency_p
,
16010 bool is_declaration
,
16011 enum tag_types tag_type
,
16012 bool *is_identifier
)
16016 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16018 /* If the next token is `operator', then we have either an
16019 operator-function-id or a conversion-function-id. */
16020 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16022 /* We don't know whether we're looking at an
16023 operator-function-id or a conversion-function-id. */
16024 cp_parser_parse_tentatively (parser
);
16025 /* Try an operator-function-id. */
16026 identifier
= cp_parser_operator_function_id (parser
);
16027 /* If that didn't work, try a conversion-function-id. */
16028 if (!cp_parser_parse_definitely (parser
))
16030 cp_parser_error (parser
, "expected template-name");
16031 return error_mark_node
;
16034 /* Look for the identifier. */
16036 identifier
= cp_parser_identifier (parser
);
16038 /* If we didn't find an identifier, we don't have a template-id. */
16039 if (identifier
== error_mark_node
)
16040 return error_mark_node
;
16042 /* If the name immediately followed the `template' keyword, then it
16043 is a template-name. However, if the next token is not `<', then
16044 we do not treat it as a template-name, since it is not being used
16045 as part of a template-id. This enables us to handle constructs
16048 template <typename T> struct S { S(); };
16049 template <typename T> S<T>::S();
16051 correctly. We would treat `S' as a template -- if it were `S<T>'
16052 -- but we do not if there is no `<'. */
16054 if (processing_template_decl
16055 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16057 /* In a declaration, in a dependent context, we pretend that the
16058 "template" keyword was present in order to improve error
16059 recovery. For example, given:
16061 template <typename T> void f(T::X<int>);
16063 we want to treat "X<int>" as a template-id. */
16065 && !template_keyword_p
16066 && parser
->scope
&& TYPE_P (parser
->scope
)
16067 && check_dependency_p
16068 && dependent_scope_p (parser
->scope
)
16069 /* Do not do this for dtors (or ctors), since they never
16070 need the template keyword before their name. */
16071 && !constructor_name_p (identifier
, parser
->scope
))
16073 cp_token_position start
= 0;
16075 /* Explain what went wrong. */
16076 error_at (token
->location
, "non-template %qD used as template",
16078 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16079 parser
->scope
, identifier
);
16080 /* If parsing tentatively, find the location of the "<" token. */
16081 if (cp_parser_simulate_error (parser
))
16082 start
= cp_lexer_token_position (parser
->lexer
, true);
16083 /* Parse the template arguments so that we can issue error
16084 messages about them. */
16085 cp_lexer_consume_token (parser
->lexer
);
16086 cp_parser_enclosed_template_argument_list (parser
);
16087 /* Skip tokens until we find a good place from which to
16088 continue parsing. */
16089 cp_parser_skip_to_closing_parenthesis (parser
,
16090 /*recovering=*/true,
16092 /*consume_paren=*/false);
16093 /* If parsing tentatively, permanently remove the
16094 template argument list. That will prevent duplicate
16095 error messages from being issued about the missing
16096 "template" keyword. */
16098 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16100 *is_identifier
= true;
16101 parser
->context
->object_type
= NULL_TREE
;
16105 /* If the "template" keyword is present, then there is generally
16106 no point in doing name-lookup, so we just return IDENTIFIER.
16107 But, if the qualifying scope is non-dependent then we can
16108 (and must) do name-lookup normally. */
16109 if (template_keyword_p
)
16111 tree scope
= (parser
->scope
? parser
->scope
16112 : parser
->context
->object_type
);
16113 if (scope
&& TYPE_P (scope
)
16114 && (!CLASS_TYPE_P (scope
)
16115 || (check_dependency_p
&& dependent_type_p (scope
))))
16117 /* We're optimizing away the call to cp_parser_lookup_name, but
16118 we still need to do this. */
16119 parser
->context
->object_type
= NULL_TREE
;
16125 /* Look up the name. */
16126 decl
= cp_parser_lookup_name (parser
, identifier
,
16128 /*is_template=*/true,
16129 /*is_namespace=*/false,
16130 check_dependency_p
,
16131 /*ambiguous_decls=*/NULL
,
16134 decl
= strip_using_decl (decl
);
16136 /* If DECL is a template, then the name was a template-name. */
16137 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16139 if (TREE_DEPRECATED (decl
)
16140 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16141 warn_deprecated_use (decl
, NULL_TREE
);
16145 /* The standard does not explicitly indicate whether a name that
16146 names a set of overloaded declarations, some of which are
16147 templates, is a template-name. However, such a name should
16148 be a template-name; otherwise, there is no way to form a
16149 template-id for the overloaded templates. */
16150 bool found
= false;
16152 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16153 !found
&& iter
; ++iter
)
16154 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16159 /* The name does not name a template. */
16160 cp_parser_error (parser
, "expected template-name");
16161 return error_mark_node
;
16165 /* If DECL is dependent, and refers to a function, then just return
16166 its name; we will look it up again during template instantiation. */
16167 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16169 tree scope
= ovl_scope (decl
);
16170 if (TYPE_P (scope
) && dependent_type_p (scope
))
16177 /* Parse a template-argument-list.
16179 template-argument-list:
16180 template-argument ... [opt]
16181 template-argument-list , template-argument ... [opt]
16183 Returns a TREE_VEC containing the arguments. */
16186 cp_parser_template_argument_list (cp_parser
* parser
)
16188 tree fixed_args
[10];
16189 unsigned n_args
= 0;
16190 unsigned alloced
= 10;
16191 tree
*arg_ary
= fixed_args
;
16193 bool saved_in_template_argument_list_p
;
16195 bool saved_non_ice_p
;
16197 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16198 parser
->in_template_argument_list_p
= true;
16199 /* Even if the template-id appears in an integral
16200 constant-expression, the contents of the argument list do
16202 saved_ice_p
= parser
->integral_constant_expression_p
;
16203 parser
->integral_constant_expression_p
= false;
16204 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16205 parser
->non_integral_constant_expression_p
= false;
16207 /* Parse the arguments. */
16213 /* Consume the comma. */
16214 cp_lexer_consume_token (parser
->lexer
);
16216 /* Parse the template-argument. */
16217 argument
= cp_parser_template_argument (parser
);
16219 /* If the next token is an ellipsis, we're expanding a template
16221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16223 if (argument
== error_mark_node
)
16225 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16226 error_at (token
->location
,
16227 "expected parameter pack before %<...%>");
16229 /* Consume the `...' token. */
16230 cp_lexer_consume_token (parser
->lexer
);
16232 /* Make the argument into a TYPE_PACK_EXPANSION or
16233 EXPR_PACK_EXPANSION. */
16234 argument
= make_pack_expansion (argument
);
16237 if (n_args
== alloced
)
16241 if (arg_ary
== fixed_args
)
16243 arg_ary
= XNEWVEC (tree
, alloced
);
16244 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16247 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16249 arg_ary
[n_args
++] = argument
;
16251 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16253 vec
= make_tree_vec (n_args
);
16256 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16258 if (arg_ary
!= fixed_args
)
16260 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16261 parser
->integral_constant_expression_p
= saved_ice_p
;
16262 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16264 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16268 /* Parse a template-argument.
16271 assignment-expression
16275 The representation is that of an assignment-expression, type-id, or
16276 id-expression -- except that the qualified id-expression is
16277 evaluated, so that the value returned is either a DECL or an
16280 Although the standard says "assignment-expression", it forbids
16281 throw-expressions or assignments in the template argument.
16282 Therefore, we use "conditional-expression" instead. */
16285 cp_parser_template_argument (cp_parser
* parser
)
16290 bool maybe_type_id
= false;
16291 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16292 location_t loc
= 0;
16295 /* There's really no way to know what we're looking at, so we just
16296 try each alternative in order.
16300 In a template-argument, an ambiguity between a type-id and an
16301 expression is resolved to a type-id, regardless of the form of
16302 the corresponding template-parameter.
16304 Therefore, we try a type-id first. */
16305 cp_parser_parse_tentatively (parser
);
16306 argument
= cp_parser_template_type_arg (parser
);
16307 /* If there was no error parsing the type-id but the next token is a
16308 '>>', our behavior depends on which dialect of C++ we're
16309 parsing. In C++98, we probably found a typo for '> >'. But there
16310 are type-id which are also valid expressions. For instance:
16312 struct X { int operator >> (int); };
16313 template <int V> struct Foo {};
16316 Here 'X()' is a valid type-id of a function type, but the user just
16317 wanted to write the expression "X() >> 5". Thus, we remember that we
16318 found a valid type-id, but we still try to parse the argument as an
16319 expression to see what happens.
16321 In C++0x, the '>>' will be considered two separate '>'
16323 if (!cp_parser_error_occurred (parser
)
16324 && cxx_dialect
== cxx98
16325 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16327 maybe_type_id
= true;
16328 cp_parser_abort_tentative_parse (parser
);
16332 /* If the next token isn't a `,' or a `>', then this argument wasn't
16333 really finished. This means that the argument is not a valid
16335 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16336 cp_parser_error (parser
, "expected template-argument");
16337 /* If that worked, we're done. */
16338 if (cp_parser_parse_definitely (parser
))
16341 /* We're still not sure what the argument will be. */
16342 cp_parser_parse_tentatively (parser
);
16343 /* Try a template. */
16344 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16345 argument
= cp_parser_id_expression (parser
,
16346 /*template_keyword_p=*/false,
16347 /*check_dependency_p=*/true,
16349 /*declarator_p=*/false,
16350 /*optional_p=*/false);
16351 /* If the next token isn't a `,' or a `>', then this argument wasn't
16352 really finished. */
16353 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16354 cp_parser_error (parser
, "expected template-argument");
16355 if (!cp_parser_error_occurred (parser
))
16357 /* Figure out what is being referred to. If the id-expression
16358 was for a class template specialization, then we will have a
16359 TYPE_DECL at this point. There is no need to do name lookup
16360 at this point in that case. */
16361 if (TREE_CODE (argument
) != TYPE_DECL
)
16362 argument
= cp_parser_lookup_name (parser
, argument
,
16364 /*is_template=*/template_p
,
16365 /*is_namespace=*/false,
16366 /*check_dependency=*/true,
16367 /*ambiguous_decls=*/NULL
,
16368 argument_start_token
->location
);
16369 /* Handle a constrained-type-specifier for a non-type template
16371 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16373 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16374 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16375 cp_parser_error (parser
, "expected template-name");
16377 if (cp_parser_parse_definitely (parser
))
16379 if (TREE_DEPRECATED (argument
))
16380 warn_deprecated_use (argument
, NULL_TREE
);
16383 /* It must be a non-type argument. In C++17 any constant-expression is
16385 if (cxx_dialect
> cxx14
)
16388 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16390 -- an integral constant-expression of integral or enumeration
16393 -- the name of a non-type template-parameter; or
16395 -- the name of an object or function with external linkage...
16397 -- the address of an object or function with external linkage...
16399 -- a pointer to member... */
16400 /* Look for a non-type template parameter. */
16401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16403 cp_parser_parse_tentatively (parser
);
16404 argument
= cp_parser_primary_expression (parser
,
16405 /*address_p=*/false,
16407 /*template_arg_p=*/true,
16409 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16410 || !cp_parser_next_token_ends_template_argument_p (parser
))
16411 cp_parser_simulate_error (parser
);
16412 if (cp_parser_parse_definitely (parser
))
16416 /* If the next token is "&", the argument must be the address of an
16417 object or function with external linkage. */
16418 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16421 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16422 cp_lexer_consume_token (parser
->lexer
);
16424 /* See if we might have an id-expression. */
16425 token
= cp_lexer_peek_token (parser
->lexer
);
16426 if (token
->type
== CPP_NAME
16427 || token
->keyword
== RID_OPERATOR
16428 || token
->type
== CPP_SCOPE
16429 || token
->type
== CPP_TEMPLATE_ID
16430 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16432 cp_parser_parse_tentatively (parser
);
16433 argument
= cp_parser_primary_expression (parser
,
16436 /*template_arg_p=*/true,
16438 if (cp_parser_error_occurred (parser
)
16439 || !cp_parser_next_token_ends_template_argument_p (parser
))
16440 cp_parser_abort_tentative_parse (parser
);
16445 if (INDIRECT_REF_P (argument
))
16447 /* Strip the dereference temporarily. */
16448 gcc_assert (REFERENCE_REF_P (argument
));
16449 argument
= TREE_OPERAND (argument
, 0);
16452 /* If we're in a template, we represent a qualified-id referring
16453 to a static data member as a SCOPE_REF even if the scope isn't
16454 dependent so that we can check access control later. */
16456 if (TREE_CODE (probe
) == SCOPE_REF
)
16457 probe
= TREE_OPERAND (probe
, 1);
16460 /* A variable without external linkage might still be a
16461 valid constant-expression, so no error is issued here
16462 if the external-linkage check fails. */
16463 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16464 cp_parser_simulate_error (parser
);
16466 else if (is_overloaded_fn (argument
))
16467 /* All overloaded functions are allowed; if the external
16468 linkage test does not pass, an error will be issued
16472 && (TREE_CODE (argument
) == OFFSET_REF
16473 || TREE_CODE (argument
) == SCOPE_REF
))
16474 /* A pointer-to-member. */
16476 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16479 cp_parser_simulate_error (parser
);
16481 if (cp_parser_parse_definitely (parser
))
16484 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16485 tf_warning_or_error
);
16487 argument
= convert_from_reference (argument
);
16492 /* If the argument started with "&", there are no other valid
16493 alternatives at this point. */
16496 cp_parser_error (parser
, "invalid non-type template argument");
16497 return error_mark_node
;
16501 /* If the argument wasn't successfully parsed as a type-id followed
16502 by '>>', the argument can only be a constant expression now.
16503 Otherwise, we try parsing the constant-expression tentatively,
16504 because the argument could really be a type-id. */
16506 cp_parser_parse_tentatively (parser
);
16508 if (cxx_dialect
<= cxx14
)
16509 argument
= cp_parser_constant_expression (parser
);
16512 /* With C++17 generalized non-type template arguments we need to handle
16513 lvalue constant expressions, too. */
16514 argument
= cp_parser_assignment_expression (parser
);
16515 require_potential_constant_expression (argument
);
16518 if (!maybe_type_id
)
16520 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16521 cp_parser_error (parser
, "expected template-argument");
16522 if (cp_parser_parse_definitely (parser
))
16524 /* We did our best to parse the argument as a non type-id, but that
16525 was the only alternative that matched (albeit with a '>' after
16526 it). We can assume it's just a typo from the user, and a
16527 diagnostic will then be issued. */
16528 return cp_parser_template_type_arg (parser
);
16531 /* Parse an explicit-instantiation.
16533 explicit-instantiation:
16534 template declaration
16536 Although the standard says `declaration', what it really means is:
16538 explicit-instantiation:
16539 template decl-specifier-seq [opt] declarator [opt] ;
16541 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16542 supposed to be allowed. A defect report has been filed about this
16547 explicit-instantiation:
16548 storage-class-specifier template
16549 decl-specifier-seq [opt] declarator [opt] ;
16550 function-specifier template
16551 decl-specifier-seq [opt] declarator [opt] ; */
16554 cp_parser_explicit_instantiation (cp_parser
* parser
)
16556 int declares_class_or_enum
;
16557 cp_decl_specifier_seq decl_specifiers
;
16558 tree extension_specifier
= NULL_TREE
;
16560 timevar_push (TV_TEMPLATE_INST
);
16562 /* Look for an (optional) storage-class-specifier or
16563 function-specifier. */
16564 if (cp_parser_allow_gnu_extensions_p (parser
))
16566 extension_specifier
16567 = cp_parser_storage_class_specifier_opt (parser
);
16568 if (!extension_specifier
)
16569 extension_specifier
16570 = cp_parser_function_specifier_opt (parser
,
16571 /*decl_specs=*/NULL
);
16574 /* Look for the `template' keyword. */
16575 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16576 /* Let the front end know that we are processing an explicit
16578 begin_explicit_instantiation ();
16579 /* [temp.explicit] says that we are supposed to ignore access
16580 control while processing explicit instantiation directives. */
16581 push_deferring_access_checks (dk_no_check
);
16582 /* Parse a decl-specifier-seq. */
16583 cp_parser_decl_specifier_seq (parser
,
16584 CP_PARSER_FLAGS_OPTIONAL
,
16586 &declares_class_or_enum
);
16587 /* If there was exactly one decl-specifier, and it declared a class,
16588 and there's no declarator, then we have an explicit type
16590 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16594 type
= check_tag_decl (&decl_specifiers
,
16595 /*explicit_type_instantiation_p=*/true);
16596 /* Turn access control back on for names used during
16597 template instantiation. */
16598 pop_deferring_access_checks ();
16600 do_type_instantiation (type
, extension_specifier
,
16601 /*complain=*/tf_error
);
16605 cp_declarator
*declarator
;
16608 /* Parse the declarator. */
16610 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16611 /*ctor_dtor_or_conv_p=*/NULL
,
16612 /*parenthesized_p=*/NULL
,
16613 /*member_p=*/false,
16614 /*friend_p=*/false);
16615 if (declares_class_or_enum
& 2)
16616 cp_parser_check_for_definition_in_return_type (declarator
,
16617 decl_specifiers
.type
,
16618 decl_specifiers
.locations
[ds_type_spec
]);
16619 if (declarator
!= cp_error_declarator
)
16621 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16622 permerror (decl_specifiers
.locations
[ds_inline
],
16623 "explicit instantiation shall not use"
16624 " %<inline%> specifier");
16625 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16626 permerror (decl_specifiers
.locations
[ds_constexpr
],
16627 "explicit instantiation shall not use"
16628 " %<constexpr%> specifier");
16630 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16631 NORMAL
, 0, &decl_specifiers
.attributes
);
16632 /* Turn access control back on for names used during
16633 template instantiation. */
16634 pop_deferring_access_checks ();
16635 /* Do the explicit instantiation. */
16636 do_decl_instantiation (decl
, extension_specifier
);
16640 pop_deferring_access_checks ();
16641 /* Skip the body of the explicit instantiation. */
16642 cp_parser_skip_to_end_of_statement (parser
);
16645 /* We're done with the instantiation. */
16646 end_explicit_instantiation ();
16648 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16650 timevar_pop (TV_TEMPLATE_INST
);
16653 /* Parse an explicit-specialization.
16655 explicit-specialization:
16656 template < > declaration
16658 Although the standard says `declaration', what it really means is:
16660 explicit-specialization:
16661 template <> decl-specifier [opt] init-declarator [opt] ;
16662 template <> function-definition
16663 template <> explicit-specialization
16664 template <> template-declaration */
16667 cp_parser_explicit_specialization (cp_parser
* parser
)
16669 bool need_lang_pop
;
16670 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16672 /* Look for the `template' keyword. */
16673 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16674 /* Look for the `<'. */
16675 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16676 /* Look for the `>'. */
16677 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16678 /* We have processed another parameter list. */
16679 ++parser
->num_template_parameter_lists
;
16682 A template ... explicit specialization ... shall not have C
16684 if (current_lang_name
== lang_name_c
)
16686 error_at (token
->location
, "template specialization with C linkage");
16687 maybe_show_extern_c_location ();
16688 /* Give it C++ linkage to avoid confusing other parts of the
16690 push_lang_context (lang_name_cplusplus
);
16691 need_lang_pop
= true;
16694 need_lang_pop
= false;
16695 /* Let the front end know that we are beginning a specialization. */
16696 if (!begin_specialization ())
16698 end_specialization ();
16702 /* If the next keyword is `template', we need to figure out whether
16703 or not we're looking a template-declaration. */
16704 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16706 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16707 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16708 cp_parser_template_declaration_after_export (parser
,
16709 /*member_p=*/false);
16711 cp_parser_explicit_specialization (parser
);
16714 /* Parse the dependent declaration. */
16715 cp_parser_single_declaration (parser
,
16717 /*member_p=*/false,
16718 /*explicit_specialization_p=*/true,
16719 /*friend_p=*/NULL
);
16720 /* We're done with the specialization. */
16721 end_specialization ();
16722 /* For the erroneous case of a template with C linkage, we pushed an
16723 implicit C++ linkage scope; exit that scope now. */
16725 pop_lang_context ();
16726 /* We're done with this parameter list. */
16727 --parser
->num_template_parameter_lists
;
16730 /* Parse a type-specifier.
16733 simple-type-specifier
16736 elaborated-type-specifier
16744 Returns a representation of the type-specifier. For a
16745 class-specifier, enum-specifier, or elaborated-type-specifier, a
16746 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16748 The parser flags FLAGS is used to control type-specifier parsing.
16750 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16751 in a decl-specifier-seq.
16753 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16754 class-specifier, enum-specifier, or elaborated-type-specifier, then
16755 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16756 if a type is declared; 2 if it is defined. Otherwise, it is set to
16759 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16760 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16761 is set to FALSE. */
16764 cp_parser_type_specifier (cp_parser
* parser
,
16765 cp_parser_flags flags
,
16766 cp_decl_specifier_seq
*decl_specs
,
16767 bool is_declaration
,
16768 int* declares_class_or_enum
,
16769 bool* is_cv_qualifier
)
16771 tree type_spec
= NULL_TREE
;
16774 cp_decl_spec ds
= ds_last
;
16776 /* Assume this type-specifier does not declare a new type. */
16777 if (declares_class_or_enum
)
16778 *declares_class_or_enum
= 0;
16779 /* And that it does not specify a cv-qualifier. */
16780 if (is_cv_qualifier
)
16781 *is_cv_qualifier
= false;
16782 /* Peek at the next token. */
16783 token
= cp_lexer_peek_token (parser
->lexer
);
16785 /* If we're looking at a keyword, we can use that to guide the
16786 production we choose. */
16787 keyword
= token
->keyword
;
16791 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16792 goto elaborated_type_specifier
;
16794 /* Look for the enum-specifier. */
16795 type_spec
= cp_parser_enum_specifier (parser
);
16796 /* If that worked, we're done. */
16799 if (declares_class_or_enum
)
16800 *declares_class_or_enum
= 2;
16802 cp_parser_set_decl_spec_type (decl_specs
,
16805 /*type_definition_p=*/true);
16809 goto elaborated_type_specifier
;
16811 /* Any of these indicate either a class-specifier, or an
16812 elaborated-type-specifier. */
16816 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16817 goto elaborated_type_specifier
;
16819 /* Parse tentatively so that we can back up if we don't find a
16820 class-specifier. */
16821 cp_parser_parse_tentatively (parser
);
16822 /* Look for the class-specifier. */
16823 type_spec
= cp_parser_class_specifier (parser
);
16824 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16825 /* If that worked, we're done. */
16826 if (cp_parser_parse_definitely (parser
))
16828 if (declares_class_or_enum
)
16829 *declares_class_or_enum
= 2;
16831 cp_parser_set_decl_spec_type (decl_specs
,
16834 /*type_definition_p=*/true);
16838 /* Fall through. */
16839 elaborated_type_specifier
:
16840 /* We're declaring (not defining) a class or enum. */
16841 if (declares_class_or_enum
)
16842 *declares_class_or_enum
= 1;
16844 /* Fall through. */
16846 /* Look for an elaborated-type-specifier. */
16848 = (cp_parser_elaborated_type_specifier
16850 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16853 cp_parser_set_decl_spec_type (decl_specs
,
16856 /*type_definition_p=*/false);
16861 if (is_cv_qualifier
)
16862 *is_cv_qualifier
= true;
16867 if (is_cv_qualifier
)
16868 *is_cv_qualifier
= true;
16873 if (is_cv_qualifier
)
16874 *is_cv_qualifier
= true;
16878 /* The `__complex__' keyword is a GNU extension. */
16886 /* Handle simple keywords. */
16891 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16892 decl_specs
->any_specifiers_p
= true;
16894 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16897 /* If we do not already have a type-specifier, assume we are looking
16898 at a simple-type-specifier. */
16899 type_spec
= cp_parser_simple_type_specifier (parser
,
16903 /* If we didn't find a type-specifier, and a type-specifier was not
16904 optional in this context, issue an error message. */
16905 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16907 cp_parser_error (parser
, "expected type specifier");
16908 return error_mark_node
;
16914 /* Parse a simple-type-specifier.
16916 simple-type-specifier:
16917 :: [opt] nested-name-specifier [opt] type-name
16918 :: [opt] nested-name-specifier template template-id
16933 simple-type-specifier:
16935 decltype ( expression )
16938 __underlying_type ( type-id )
16942 nested-name-specifier(opt) template-name
16946 simple-type-specifier:
16948 __typeof__ unary-expression
16949 __typeof__ ( type-id )
16950 __typeof__ ( type-id ) { initializer-list , [opt] }
16952 Concepts Extension:
16954 simple-type-specifier:
16955 constrained-type-specifier
16957 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16958 appropriately updated. */
16961 cp_parser_simple_type_specifier (cp_parser
* parser
,
16962 cp_decl_specifier_seq
*decl_specs
,
16963 cp_parser_flags flags
)
16965 tree type
= NULL_TREE
;
16969 /* Peek at the next token. */
16970 token
= cp_lexer_peek_token (parser
->lexer
);
16972 /* If we're looking at a keyword, things are easy. */
16973 switch (token
->keyword
)
16977 decl_specs
->explicit_char_p
= true;
16978 type
= char_type_node
;
16981 type
= char16_type_node
;
16984 type
= char32_type_node
;
16987 type
= wchar_type_node
;
16990 type
= boolean_type_node
;
16993 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16994 type
= short_integer_type_node
;
16998 decl_specs
->explicit_int_p
= true;
16999 type
= integer_type_node
;
17005 idx
= token
->keyword
- RID_INT_N_0
;
17006 if (! int_n_enabled_p
[idx
])
17010 decl_specs
->explicit_intN_p
= true;
17011 decl_specs
->int_n_idx
= idx
;
17013 type
= int_n_trees
[idx
].signed_type
;
17017 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17018 type
= long_integer_type_node
;
17021 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17022 type
= integer_type_node
;
17025 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17026 type
= unsigned_type_node
;
17029 type
= float_type_node
;
17032 type
= double_type_node
;
17035 type
= void_type_node
;
17039 maybe_warn_cpp0x (CPP0X_AUTO
);
17040 if (parser
->auto_is_implicit_function_template_parm_p
)
17042 /* The 'auto' might be the placeholder return type for a function decl
17043 with trailing return type. */
17044 bool have_trailing_return_fn_decl
= false;
17046 cp_parser_parse_tentatively (parser
);
17047 cp_lexer_consume_token (parser
->lexer
);
17048 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17049 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17050 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17051 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17055 cp_lexer_consume_token (parser
->lexer
);
17056 cp_parser_skip_to_closing_parenthesis (parser
,
17057 /*recovering*/false,
17059 /*consume_paren*/true);
17063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17065 have_trailing_return_fn_decl
= true;
17069 cp_lexer_consume_token (parser
->lexer
);
17071 cp_parser_abort_tentative_parse (parser
);
17073 if (have_trailing_return_fn_decl
)
17075 type
= make_auto ();
17079 if (cxx_dialect
>= cxx14
)
17081 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17082 type
= TREE_TYPE (type
);
17085 type
= error_mark_node
;
17087 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17089 if (cxx_dialect
< cxx14
)
17090 error_at (token
->location
,
17091 "use of %<auto%> in lambda parameter declaration "
17092 "only available with "
17093 "-std=c++14 or -std=gnu++14");
17095 else if (cxx_dialect
< cxx14
)
17096 error_at (token
->location
,
17097 "use of %<auto%> in parameter declaration "
17098 "only available with "
17099 "-std=c++14 or -std=gnu++14");
17100 else if (!flag_concepts
)
17101 pedwarn (token
->location
, OPT_Wpedantic
,
17102 "ISO C++ forbids use of %<auto%> in parameter "
17106 type
= make_auto ();
17110 /* Since DR 743, decltype can either be a simple-type-specifier by
17111 itself or begin a nested-name-specifier. Parsing it will replace
17112 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17113 handling below decide what to do. */
17114 cp_parser_decltype (parser
);
17115 cp_lexer_set_token_position (parser
->lexer
, token
);
17119 /* Consume the `typeof' token. */
17120 cp_lexer_consume_token (parser
->lexer
);
17121 /* Parse the operand to `typeof'. */
17122 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17123 /* If it is not already a TYPE, take its type. */
17124 if (!TYPE_P (type
))
17125 type
= finish_typeof (type
);
17128 cp_parser_set_decl_spec_type (decl_specs
, type
,
17130 /*type_definition_p=*/false);
17134 case RID_UNDERLYING_TYPE
:
17135 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17137 cp_parser_set_decl_spec_type (decl_specs
, type
,
17139 /*type_definition_p=*/false);
17144 case RID_DIRECT_BASES
:
17145 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17147 cp_parser_set_decl_spec_type (decl_specs
, type
,
17149 /*type_definition_p=*/false);
17155 /* If token is an already-parsed decltype not followed by ::,
17156 it's a simple-type-specifier. */
17157 if (token
->type
== CPP_DECLTYPE
17158 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17160 type
= saved_checks_value (token
->u
.tree_check_value
);
17163 cp_parser_set_decl_spec_type (decl_specs
, type
,
17165 /*type_definition_p=*/false);
17166 /* Remember that we are handling a decltype in order to
17167 implement the resolution of DR 1510 when the argument
17168 isn't instantiation dependent. */
17169 decl_specs
->decltype_p
= true;
17171 cp_lexer_consume_token (parser
->lexer
);
17175 /* If the type-specifier was for a built-in type, we're done. */
17178 /* Record the type. */
17180 && (token
->keyword
!= RID_SIGNED
17181 && token
->keyword
!= RID_UNSIGNED
17182 && token
->keyword
!= RID_SHORT
17183 && token
->keyword
!= RID_LONG
))
17184 cp_parser_set_decl_spec_type (decl_specs
,
17187 /*type_definition_p=*/false);
17189 decl_specs
->any_specifiers_p
= true;
17191 /* Consume the token. */
17192 cp_lexer_consume_token (parser
->lexer
);
17194 if (type
== error_mark_node
)
17195 return error_mark_node
;
17197 /* There is no valid C++ program where a non-template type is
17198 followed by a "<". That usually indicates that the user thought
17199 that the type was a template. */
17200 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17203 return TYPE_NAME (type
);
17206 /* The type-specifier must be a user-defined type. */
17207 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17212 /* Don't gobble tokens or issue error messages if this is an
17213 optional type-specifier. */
17214 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17215 cp_parser_parse_tentatively (parser
);
17217 token
= cp_lexer_peek_token (parser
->lexer
);
17219 /* Look for the optional `::' operator. */
17221 = (cp_parser_global_scope_opt (parser
,
17222 /*current_scope_valid_p=*/false)
17224 /* Look for the nested-name specifier. */
17226 = (cp_parser_nested_name_specifier_opt (parser
,
17227 /*typename_keyword_p=*/false,
17228 /*check_dependency_p=*/true,
17230 /*is_declaration=*/false)
17232 /* If we have seen a nested-name-specifier, and the next token
17233 is `template', then we are using the template-id production. */
17235 && cp_parser_optional_template_keyword (parser
))
17237 /* Look for the template-id. */
17238 type
= cp_parser_template_id (parser
,
17239 /*template_keyword_p=*/true,
17240 /*check_dependency_p=*/true,
17242 /*is_declaration=*/false);
17243 /* If the template-id did not name a type, we are out of
17245 if (TREE_CODE (type
) != TYPE_DECL
)
17247 cp_parser_error (parser
, "expected template-id for type");
17251 /* Otherwise, look for a type-name. */
17253 type
= cp_parser_type_name (parser
);
17254 /* Keep track of all name-lookups performed in class scopes. */
17258 && TREE_CODE (type
) == TYPE_DECL
17259 && identifier_p (DECL_NAME (type
)))
17260 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17261 /* If it didn't work out, we don't have a TYPE. */
17262 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17263 && !cp_parser_parse_definitely (parser
))
17265 if (!type
&& cxx_dialect
>= cxx17
)
17267 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17268 cp_parser_parse_tentatively (parser
);
17270 cp_parser_global_scope_opt (parser
,
17271 /*current_scope_valid_p=*/false);
17272 cp_parser_nested_name_specifier_opt (parser
,
17273 /*typename_keyword_p=*/false,
17274 /*check_dependency_p=*/true,
17276 /*is_declaration=*/false);
17277 tree name
= cp_parser_identifier (parser
);
17278 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17279 && parser
->scope
!= error_mark_node
)
17281 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17283 /*is_template=*/false,
17284 /*is_namespace=*/false,
17285 /*check_dependency=*/true,
17286 /*ambiguous_decls=*/NULL
,
17288 if (tmpl
&& tmpl
!= error_mark_node
17289 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17290 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17291 type
= make_template_placeholder (tmpl
);
17294 type
= error_mark_node
;
17295 if (!cp_parser_simulate_error (parser
))
17296 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17297 NLE_TYPE
, token
->location
);
17301 type
= error_mark_node
;
17303 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17304 && !cp_parser_parse_definitely (parser
))
17307 if (type
&& decl_specs
)
17308 cp_parser_set_decl_spec_type (decl_specs
, type
,
17310 /*type_definition_p=*/false);
17313 /* If we didn't get a type-name, issue an error message. */
17314 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17316 cp_parser_error (parser
, "expected type-name");
17317 return error_mark_node
;
17320 if (type
&& type
!= error_mark_node
)
17322 /* See if TYPE is an Objective-C type, and if so, parse and
17323 accept any protocol references following it. Do this before
17324 the cp_parser_check_for_invalid_template_id() call, because
17325 Objective-C types can be followed by '<...>' which would
17326 enclose protocol names rather than template arguments, and so
17327 everything is fine. */
17328 if (c_dialect_objc () && !parser
->scope
17329 && (objc_is_id (type
) || objc_is_class_name (type
)))
17331 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17332 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17334 /* Clobber the "unqualified" type previously entered into
17335 DECL_SPECS with the new, improved protocol-qualified version. */
17337 decl_specs
->type
= qual_type
;
17342 /* There is no valid C++ program where a non-template type is
17343 followed by a "<". That usually indicates that the user
17344 thought that the type was a template. */
17345 cp_parser_check_for_invalid_template_id (parser
, type
,
17353 /* Parse a type-name.
17359 simple-template-id [in c++0x]
17376 Returns a TYPE_DECL for the type. */
17379 cp_parser_type_name (cp_parser
* parser
)
17381 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17386 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17390 /* We can't know yet whether it is a class-name or not. */
17391 cp_parser_parse_tentatively (parser
);
17392 /* Try a class-name. */
17393 type_decl
= cp_parser_class_name (parser
,
17394 typename_keyword_p
,
17395 /*template_keyword_p=*/false,
17397 /*check_dependency_p=*/true,
17398 /*class_head_p=*/false,
17399 /*is_declaration=*/false);
17400 /* If it's not a class-name, keep looking. */
17401 if (!cp_parser_parse_definitely (parser
))
17403 if (cxx_dialect
< cxx11
)
17404 /* It must be a typedef-name or an enum-name. */
17405 return cp_parser_nonclass_name (parser
);
17407 cp_parser_parse_tentatively (parser
);
17408 /* It is either a simple-template-id representing an
17409 instantiation of an alias template... */
17410 type_decl
= cp_parser_template_id (parser
,
17411 /*template_keyword_p=*/false,
17412 /*check_dependency_p=*/true,
17414 /*is_declaration=*/false);
17415 /* Note that this must be an instantiation of an alias template
17416 because [temp.names]/6 says:
17418 A template-id that names an alias template specialization
17421 Whereas [temp.names]/7 says:
17423 A simple-template-id that names a class template
17424 specialization is a class-name.
17426 With concepts, this could also be a partial-concept-id that
17427 declares a non-type template parameter. */
17428 if (type_decl
!= NULL_TREE
17429 && TREE_CODE (type_decl
) == TYPE_DECL
17430 && TYPE_DECL_ALIAS_P (type_decl
))
17431 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17432 else if (is_constrained_parameter (type_decl
))
17433 /* Don't do anything. */ ;
17435 cp_parser_simulate_error (parser
);
17437 if (!cp_parser_parse_definitely (parser
))
17438 /* ... Or a typedef-name or an enum-name. */
17439 return cp_parser_nonclass_name (parser
);
17445 /* Check if DECL and ARGS can form a constrained-type-specifier.
17446 If ARGS is non-null, we try to form a concept check of the
17447 form DECL<?, ARGS> where ? is a wildcard that matches any
17448 kind of template argument. If ARGS is NULL, then we try to
17449 form a concept check of the form DECL<?>. */
17452 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17453 tree decl
, tree args
)
17455 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17457 /* If we a constrained-type-specifier cannot be deduced. */
17458 if (parser
->prevent_constrained_type_specifiers
)
17461 /* A constrained type specifier can only be found in an
17462 overload set or as a reference to a template declaration.
17464 FIXME: This might be masking a bug. It's possible that
17465 that the deduction below is causing template specializations
17466 to be formed with the wildcard as an argument. */
17467 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17470 /* Try to build a call expression that evaluates the
17471 concept. This can fail if the overload set refers
17472 only to non-templates. */
17473 tree placeholder
= build_nt (WILDCARD_DECL
);
17474 tree check
= build_concept_check (decl
, placeholder
, args
);
17475 if (check
== error_mark_node
)
17478 /* Deduce the checked constraint and the prototype parameter.
17480 FIXME: In certain cases, failure to deduce should be a
17481 diagnosable error. */
17484 if (!deduce_constrained_parameter (check
, conc
, proto
))
17487 /* In template parameter scope, this results in a constrained
17488 parameter. Return a descriptor of that parm. */
17489 if (processing_template_parmlist
)
17490 return build_constrained_parameter (conc
, proto
, args
);
17492 /* In a parameter-declaration-clause, constrained-type
17493 specifiers result in invented template parameters. */
17494 if (parser
->auto_is_implicit_function_template_parm_p
)
17496 tree x
= build_constrained_parameter (conc
, proto
, args
);
17497 return synthesize_implicit_template_parm (parser
, x
);
17501 /* Otherwise, we're in a context where the constrained
17502 type name is deduced and the constraint applies
17503 after deduction. */
17504 return make_constrained_auto (conc
, args
);
17510 /* If DECL refers to a concept, return a TYPE_DECL representing
17511 the result of using the constrained type specifier in the
17512 current context. DECL refers to a concept if
17514 - it is an overload set containing a function concept taking a single
17517 - it is a variable concept taking a single type argument. */
17520 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17523 && (TREE_CODE (decl
) == OVERLOAD
17524 || BASELINK_P (decl
)
17525 || variable_concept_p (decl
)))
17526 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17531 /* Check if DECL and ARGS form a partial-concept-id. If so,
17532 assign ID to the resulting constrained placeholder.
17534 Returns true if the partial-concept-id designates a placeholder
17535 and false otherwise. Note that *id is set to NULL_TREE in
17539 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17541 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17544 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17556 Returns a TYPE_DECL for the type. */
17559 cp_parser_nonclass_name (cp_parser
* parser
)
17564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17565 identifier
= cp_parser_identifier (parser
);
17566 if (identifier
== error_mark_node
)
17567 return error_mark_node
;
17569 /* Look up the type-name. */
17570 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17572 type_decl
= strip_using_decl (type_decl
);
17574 /* If we found an overload set, then it may refer to a concept-name. */
17575 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17578 if (TREE_CODE (type_decl
) != TYPE_DECL
17579 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17581 /* See if this is an Objective-C type. */
17582 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17583 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17585 type_decl
= TYPE_NAME (type
);
17588 /* Issue an error if we did not find a type-name. */
17589 if (TREE_CODE (type_decl
) != TYPE_DECL
17590 /* In Objective-C, we have the complication that class names are
17591 normally type names and start declarations (eg, the
17592 "NSObject" in "NSObject *object;"), but can be used in an
17593 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17594 is an expression. So, a classname followed by a dot is not a
17595 valid type-name. */
17596 || (objc_is_class_name (TREE_TYPE (type_decl
))
17597 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17599 if (!cp_parser_simulate_error (parser
))
17600 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17601 NLE_TYPE
, token
->location
);
17602 return error_mark_node
;
17604 /* Remember that the name was used in the definition of the
17605 current class so that we can check later to see if the
17606 meaning would have been different after the class was
17607 entirely defined. */
17608 else if (type_decl
!= error_mark_node
17610 maybe_note_name_used_in_class (identifier
, type_decl
);
17615 /* Parse an elaborated-type-specifier. Note that the grammar given
17616 here incorporates the resolution to DR68.
17618 elaborated-type-specifier:
17619 class-key :: [opt] nested-name-specifier [opt] identifier
17620 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17621 enum-key :: [opt] nested-name-specifier [opt] identifier
17622 typename :: [opt] nested-name-specifier identifier
17623 typename :: [opt] nested-name-specifier template [opt]
17628 elaborated-type-specifier:
17629 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17630 class-key attributes :: [opt] nested-name-specifier [opt]
17631 template [opt] template-id
17632 enum attributes :: [opt] nested-name-specifier [opt] identifier
17634 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17635 declared `friend'. If IS_DECLARATION is TRUE, then this
17636 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17637 something is being declared.
17639 Returns the TYPE specified. */
17642 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17644 bool is_declaration
)
17646 enum tag_types tag_type
;
17648 tree type
= NULL_TREE
;
17649 tree attributes
= NULL_TREE
;
17651 cp_token
*token
= NULL
;
17653 /* See if we're looking at the `enum' keyword. */
17654 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17656 /* Consume the `enum' token. */
17657 cp_lexer_consume_token (parser
->lexer
);
17658 /* Remember that it's an enumeration type. */
17659 tag_type
= enum_type
;
17660 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17661 enums) is used here. */
17662 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17663 if (cp_parser_is_keyword (token
, RID_CLASS
)
17664 || cp_parser_is_keyword (token
, RID_STRUCT
))
17666 gcc_rich_location
richloc (token
->location
);
17667 richloc
.add_range (input_location
, false);
17668 richloc
.add_fixit_remove ();
17669 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17670 "a scoped enum must not use the %qD keyword",
17672 /* Consume the `struct' or `class' and parse it anyway. */
17673 cp_lexer_consume_token (parser
->lexer
);
17675 /* Parse the attributes. */
17676 attributes
= cp_parser_attributes_opt (parser
);
17678 /* Or, it might be `typename'. */
17679 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17682 /* Consume the `typename' token. */
17683 cp_lexer_consume_token (parser
->lexer
);
17684 /* Remember that it's a `typename' type. */
17685 tag_type
= typename_type
;
17687 /* Otherwise it must be a class-key. */
17690 tag_type
= cp_parser_class_key (parser
);
17691 if (tag_type
== none_type
)
17692 return error_mark_node
;
17693 /* Parse the attributes. */
17694 attributes
= cp_parser_attributes_opt (parser
);
17697 /* Look for the `::' operator. */
17698 globalscope
= cp_parser_global_scope_opt (parser
,
17699 /*current_scope_valid_p=*/false);
17700 /* Look for the nested-name-specifier. */
17701 tree nested_name_specifier
;
17702 if (tag_type
== typename_type
&& !globalscope
)
17704 nested_name_specifier
17705 = cp_parser_nested_name_specifier (parser
,
17706 /*typename_keyword_p=*/true,
17707 /*check_dependency_p=*/true,
17710 if (!nested_name_specifier
)
17711 return error_mark_node
;
17714 /* Even though `typename' is not present, the proposed resolution
17715 to Core Issue 180 says that in `class A<T>::B', `B' should be
17716 considered a type-name, even if `A<T>' is dependent. */
17717 nested_name_specifier
17718 = cp_parser_nested_name_specifier_opt (parser
,
17719 /*typename_keyword_p=*/true,
17720 /*check_dependency_p=*/true,
17723 /* For everything but enumeration types, consider a template-id.
17724 For an enumeration type, consider only a plain identifier. */
17725 if (tag_type
!= enum_type
)
17727 bool template_p
= false;
17730 /* Allow the `template' keyword. */
17731 template_p
= cp_parser_optional_template_keyword (parser
);
17732 /* If we didn't see `template', we don't know if there's a
17733 template-id or not. */
17735 cp_parser_parse_tentatively (parser
);
17736 /* Parse the template-id. */
17737 token
= cp_lexer_peek_token (parser
->lexer
);
17738 decl
= cp_parser_template_id (parser
, template_p
,
17739 /*check_dependency_p=*/true,
17742 /* If we didn't find a template-id, look for an ordinary
17744 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17746 /* We can get here when cp_parser_template_id, called by
17747 cp_parser_class_name with tag_type == none_type, succeeds
17748 and caches a BASELINK. Then, when called again here,
17749 instead of failing and returning an error_mark_node
17750 returns it (see template/typename17.C in C++11).
17751 ??? Could we diagnose this earlier? */
17752 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17754 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17755 type
= error_mark_node
;
17757 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17758 in effect, then we must assume that, upon instantiation, the
17759 template will correspond to a class. */
17760 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17761 && tag_type
== typename_type
)
17762 type
= make_typename_type (parser
->scope
, decl
,
17764 /*complain=*/tf_error
);
17765 /* If the `typename' keyword is in effect and DECL is not a type
17766 decl, then type is non existent. */
17767 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17769 else if (TREE_CODE (decl
) == TYPE_DECL
)
17771 type
= check_elaborated_type_specifier (tag_type
, decl
,
17772 /*allow_template_p=*/true);
17774 /* If the next token is a semicolon, this must be a specialization,
17775 instantiation, or friend declaration. Check the scope while we
17776 still know whether or not we had a nested-name-specifier. */
17777 if (type
!= error_mark_node
17778 && !nested_name_specifier
&& !is_friend
17779 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17780 check_unqualified_spec_or_inst (type
, token
->location
);
17782 else if (decl
== error_mark_node
)
17783 type
= error_mark_node
;
17788 token
= cp_lexer_peek_token (parser
->lexer
);
17789 identifier
= cp_parser_identifier (parser
);
17791 if (identifier
== error_mark_node
)
17793 parser
->scope
= NULL_TREE
;
17794 return error_mark_node
;
17797 /* For a `typename', we needn't call xref_tag. */
17798 if (tag_type
== typename_type
17799 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17800 return cp_parser_make_typename_type (parser
, identifier
,
17803 /* Template parameter lists apply only if we are not within a
17804 function parameter list. */
17805 bool template_parm_lists_apply
17806 = parser
->num_template_parameter_lists
;
17807 if (template_parm_lists_apply
)
17808 for (cp_binding_level
*s
= current_binding_level
;
17809 s
&& s
->kind
!= sk_template_parms
;
17810 s
= s
->level_chain
)
17811 if (s
->kind
== sk_function_parms
)
17812 template_parm_lists_apply
= false;
17814 /* Look up a qualified name in the usual way. */
17818 tree ambiguous_decls
;
17820 decl
= cp_parser_lookup_name (parser
, identifier
,
17822 /*is_template=*/false,
17823 /*is_namespace=*/false,
17824 /*check_dependency=*/true,
17828 /* If the lookup was ambiguous, an error will already have been
17830 if (ambiguous_decls
)
17831 return error_mark_node
;
17833 /* If we are parsing friend declaration, DECL may be a
17834 TEMPLATE_DECL tree node here. However, we need to check
17835 whether this TEMPLATE_DECL results in valid code. Consider
17836 the following example:
17839 template <class T> class C {};
17842 template <class T> friend class N::C; // #1, valid code
17844 template <class T> class Y {
17845 friend class N::C; // #2, invalid code
17848 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17849 name lookup of `N::C'. We see that friend declaration must
17850 be template for the code to be valid. Note that
17851 processing_template_decl does not work here since it is
17852 always 1 for the above two cases. */
17854 decl
= (cp_parser_maybe_treat_template_as_class
17855 (decl
, /*tag_name_p=*/is_friend
17856 && template_parm_lists_apply
));
17858 if (TREE_CODE (decl
) != TYPE_DECL
)
17860 cp_parser_diagnose_invalid_type_name (parser
,
17863 return error_mark_node
;
17866 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17868 bool allow_template
= (template_parm_lists_apply
17869 || DECL_SELF_REFERENCE_P (decl
));
17870 type
= check_elaborated_type_specifier (tag_type
, decl
,
17873 if (type
== error_mark_node
)
17874 return error_mark_node
;
17877 /* Forward declarations of nested types, such as
17882 are invalid unless all components preceding the final '::'
17883 are complete. If all enclosing types are complete, these
17884 declarations become merely pointless.
17886 Invalid forward declarations of nested types are errors
17887 caught elsewhere in parsing. Those that are pointless arrive
17890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17891 && !is_friend
&& !processing_explicit_instantiation
)
17892 warning (0, "declaration %qD does not declare anything", decl
);
17894 type
= TREE_TYPE (decl
);
17898 /* An elaborated-type-specifier sometimes introduces a new type and
17899 sometimes names an existing type. Normally, the rule is that it
17900 introduces a new type only if there is not an existing type of
17901 the same name already in scope. For example, given:
17904 void f() { struct S s; }
17906 the `struct S' in the body of `f' is the same `struct S' as in
17907 the global scope; the existing definition is used. However, if
17908 there were no global declaration, this would introduce a new
17909 local class named `S'.
17911 An exception to this rule applies to the following code:
17913 namespace N { struct S; }
17915 Here, the elaborated-type-specifier names a new type
17916 unconditionally; even if there is already an `S' in the
17917 containing scope this declaration names a new type.
17918 This exception only applies if the elaborated-type-specifier
17919 forms the complete declaration:
17923 A declaration consisting solely of `class-key identifier ;' is
17924 either a redeclaration of the name in the current scope or a
17925 forward declaration of the identifier as a class name. It
17926 introduces the name into the current scope.
17928 We are in this situation precisely when the next token is a `;'.
17930 An exception to the exception is that a `friend' declaration does
17931 *not* name a new type; i.e., given:
17933 struct S { friend struct T; };
17935 `T' is not a new type in the scope of `S'.
17937 Also, `new struct S' or `sizeof (struct S)' never results in the
17938 definition of a new type; a new type can only be declared in a
17939 declaration context. */
17945 /* Friends have special name lookup rules. */
17946 ts
= ts_within_enclosing_non_class
;
17947 else if (is_declaration
17948 && cp_lexer_next_token_is (parser
->lexer
,
17950 /* This is a `class-key identifier ;' */
17956 (template_parm_lists_apply
17957 && (cp_parser_next_token_starts_class_definition_p (parser
)
17958 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17959 /* An unqualified name was used to reference this type, so
17960 there were no qualifying templates. */
17961 if (template_parm_lists_apply
17962 && !cp_parser_check_template_parameters (parser
,
17963 /*num_templates=*/0,
17965 /*declarator=*/NULL
))
17966 return error_mark_node
;
17967 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17971 if (type
== error_mark_node
)
17972 return error_mark_node
;
17974 /* Allow attributes on forward declarations of classes. */
17977 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17978 warning (OPT_Wattributes
,
17979 "attributes ignored on uninstantiated type");
17980 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17981 && ! processing_explicit_instantiation
)
17982 warning (OPT_Wattributes
,
17983 "attributes ignored on template instantiation");
17984 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17985 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17987 warning (OPT_Wattributes
,
17988 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17991 if (tag_type
!= enum_type
)
17993 /* Indicate whether this class was declared as a `class' or as a
17995 if (CLASS_TYPE_P (type
))
17996 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17997 cp_parser_check_class_key (tag_type
, type
);
18000 /* A "<" cannot follow an elaborated type specifier. If that
18001 happens, the user was probably trying to form a template-id. */
18002 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18008 /* Parse an enum-specifier.
18011 enum-head { enumerator-list [opt] }
18012 enum-head { enumerator-list , } [C++0x]
18015 enum-key identifier [opt] enum-base [opt]
18016 enum-key nested-name-specifier identifier enum-base [opt]
18021 enum struct [C++0x]
18024 : type-specifier-seq
18026 opaque-enum-specifier:
18027 enum-key identifier enum-base [opt] ;
18030 enum-key attributes[opt] identifier [opt] enum-base [opt]
18031 { enumerator-list [opt] }attributes[opt]
18032 enum-key attributes[opt] identifier [opt] enum-base [opt]
18033 { enumerator-list, }attributes[opt] [C++0x]
18035 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18036 if the token stream isn't an enum-specifier after all. */
18039 cp_parser_enum_specifier (cp_parser
* parser
)
18042 tree type
= NULL_TREE
;
18044 tree nested_name_specifier
= NULL_TREE
;
18046 bool scoped_enum_p
= false;
18047 bool has_underlying_type
= false;
18048 bool nested_being_defined
= false;
18049 bool new_value_list
= false;
18050 bool is_new_type
= false;
18051 bool is_unnamed
= false;
18052 tree underlying_type
= NULL_TREE
;
18053 cp_token
*type_start_token
= NULL
;
18054 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18056 parser
->colon_corrects_to_scope_p
= false;
18058 /* Parse tentatively so that we can back up if we don't find a
18060 cp_parser_parse_tentatively (parser
);
18062 /* Caller guarantees that the current token is 'enum', an identifier
18063 possibly follows, and the token after that is an opening brace.
18064 If we don't have an identifier, fabricate an anonymous name for
18065 the enumeration being defined. */
18066 cp_lexer_consume_token (parser
->lexer
);
18068 /* Parse the "class" or "struct", which indicates a scoped
18069 enumeration type in C++0x. */
18070 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18071 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18073 if (cxx_dialect
< cxx11
)
18074 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18076 /* Consume the `struct' or `class' token. */
18077 cp_lexer_consume_token (parser
->lexer
);
18079 scoped_enum_p
= true;
18082 attributes
= cp_parser_attributes_opt (parser
);
18084 /* Clear the qualification. */
18085 parser
->scope
= NULL_TREE
;
18086 parser
->qualifying_scope
= NULL_TREE
;
18087 parser
->object_scope
= NULL_TREE
;
18089 /* Figure out in what scope the declaration is being placed. */
18090 prev_scope
= current_scope ();
18092 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18094 push_deferring_access_checks (dk_no_check
);
18095 nested_name_specifier
18096 = cp_parser_nested_name_specifier_opt (parser
,
18097 /*typename_keyword_p=*/true,
18098 /*check_dependency_p=*/false,
18100 /*is_declaration=*/false);
18102 if (nested_name_specifier
)
18106 identifier
= cp_parser_identifier (parser
);
18107 name
= cp_parser_lookup_name (parser
, identifier
,
18109 /*is_template=*/false,
18110 /*is_namespace=*/false,
18111 /*check_dependency=*/true,
18112 /*ambiguous_decls=*/NULL
,
18114 if (name
&& name
!= error_mark_node
)
18116 type
= TREE_TYPE (name
);
18117 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18119 /* Are template enums allowed in ISO? */
18120 if (template_parm_scope_p ())
18121 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18122 "%qD is an enumeration template", name
);
18123 /* ignore a typename reference, for it will be solved by name
18128 else if (nested_name_specifier
== error_mark_node
)
18129 /* We already issued an error. */;
18132 error_at (type_start_token
->location
,
18133 "%qD does not name an enumeration in %qT",
18134 identifier
, nested_name_specifier
);
18135 nested_name_specifier
= error_mark_node
;
18140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18141 identifier
= cp_parser_identifier (parser
);
18144 identifier
= make_anon_name ();
18147 error_at (type_start_token
->location
,
18148 "unnamed scoped enum is not allowed");
18151 pop_deferring_access_checks ();
18153 /* Check for the `:' that denotes a specified underlying type in C++0x.
18154 Note that a ':' could also indicate a bitfield width, however. */
18155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18157 cp_decl_specifier_seq type_specifiers
;
18159 /* Consume the `:'. */
18160 cp_lexer_consume_token (parser
->lexer
);
18162 /* Parse the type-specifier-seq. */
18163 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18164 /*is_trailing_return=*/false,
18167 /* At this point this is surely not elaborated type specifier. */
18168 if (!cp_parser_parse_definitely (parser
))
18171 if (cxx_dialect
< cxx11
)
18172 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18174 has_underlying_type
= true;
18176 /* If that didn't work, stop. */
18177 if (type_specifiers
.type
!= error_mark_node
)
18179 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18180 /*initialized=*/0, NULL
);
18181 if (underlying_type
== error_mark_node
18182 || check_for_bare_parameter_packs (underlying_type
))
18183 underlying_type
= NULL_TREE
;
18187 /* Look for the `{' but don't consume it yet. */
18188 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18190 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18192 cp_parser_error (parser
, "expected %<{%>");
18193 if (has_underlying_type
)
18199 /* An opaque-enum-specifier must have a ';' here. */
18200 if ((scoped_enum_p
|| underlying_type
)
18201 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18203 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18204 if (has_underlying_type
)
18212 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18215 if (nested_name_specifier
)
18217 if (CLASS_TYPE_P (nested_name_specifier
))
18219 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18220 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18221 push_scope (nested_name_specifier
);
18223 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18225 push_nested_namespace (nested_name_specifier
);
18229 /* Issue an error message if type-definitions are forbidden here. */
18230 if (!cp_parser_check_type_definition (parser
))
18231 type
= error_mark_node
;
18233 /* Create the new type. We do this before consuming the opening
18234 brace so the enum will be recorded as being on the line of its
18235 tag (or the 'enum' keyword, if there is no tag). */
18236 type
= start_enum (identifier
, type
, underlying_type
,
18237 attributes
, scoped_enum_p
, &is_new_type
);
18239 /* If the next token is not '{' it is an opaque-enum-specifier or an
18240 elaborated-type-specifier. */
18241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18243 timevar_push (TV_PARSE_ENUM
);
18244 if (nested_name_specifier
18245 && nested_name_specifier
!= error_mark_node
)
18247 /* The following catches invalid code such as:
18248 enum class S<int>::E { A, B, C }; */
18249 if (!processing_specialization
18250 && CLASS_TYPE_P (nested_name_specifier
)
18251 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18252 error_at (type_start_token
->location
, "cannot add an enumerator "
18253 "list to a template instantiation");
18255 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18257 error_at (type_start_token
->location
,
18258 "%<%T::%E%> has not been declared",
18259 TYPE_CONTEXT (nested_name_specifier
),
18260 nested_name_specifier
);
18261 type
= error_mark_node
;
18263 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18264 && !CLASS_TYPE_P (nested_name_specifier
))
18266 error_at (type_start_token
->location
, "nested name specifier "
18267 "%qT for enum declaration does not name a class "
18268 "or namespace", nested_name_specifier
);
18269 type
= error_mark_node
;
18271 /* If that scope does not contain the scope in which the
18272 class was originally declared, the program is invalid. */
18273 else if (prev_scope
&& !is_ancestor (prev_scope
,
18274 nested_name_specifier
))
18276 if (at_namespace_scope_p ())
18277 error_at (type_start_token
->location
,
18278 "declaration of %qD in namespace %qD which does not "
18280 type
, prev_scope
, nested_name_specifier
);
18282 error_at (type_start_token
->location
,
18283 "declaration of %qD in %qD which does not "
18285 type
, prev_scope
, nested_name_specifier
);
18286 type
= error_mark_node
;
18288 /* If that scope is the scope where the declaration is being placed
18289 the program is invalid. */
18290 else if (CLASS_TYPE_P (nested_name_specifier
)
18291 && CLASS_TYPE_P (prev_scope
)
18292 && same_type_p (nested_name_specifier
, prev_scope
))
18294 permerror (type_start_token
->location
,
18295 "extra qualification not allowed");
18296 nested_name_specifier
= NULL_TREE
;
18301 begin_scope (sk_scoped_enum
, type
);
18303 /* Consume the opening brace. */
18304 matching_braces braces
;
18305 braces
.consume_open (parser
);
18307 if (type
== error_mark_node
)
18308 ; /* Nothing to add */
18309 else if (OPAQUE_ENUM_P (type
)
18310 || (cxx_dialect
> cxx98
&& processing_specialization
))
18312 new_value_list
= true;
18313 SET_OPAQUE_ENUM_P (type
, false);
18314 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18318 error_at (type_start_token
->location
,
18319 "multiple definition of %q#T", type
);
18320 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18321 "previous definition here");
18322 type
= error_mark_node
;
18325 if (type
== error_mark_node
)
18326 cp_parser_skip_to_end_of_block_or_statement (parser
);
18327 /* If the next token is not '}', then there are some enumerators. */
18328 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18330 if (is_unnamed
&& !scoped_enum_p
)
18331 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18332 "ISO C++ forbids empty unnamed enum");
18335 cp_parser_enumerator_list (parser
, type
);
18337 /* Consume the final '}'. */
18338 braces
.require_close (parser
);
18342 timevar_pop (TV_PARSE_ENUM
);
18346 /* If a ';' follows, then it is an opaque-enum-specifier
18347 and additional restrictions apply. */
18348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18351 error_at (type_start_token
->location
,
18352 "opaque-enum-specifier without name");
18353 else if (nested_name_specifier
)
18354 error_at (type_start_token
->location
,
18355 "opaque-enum-specifier must use a simple identifier");
18359 /* Look for trailing attributes to apply to this enumeration, and
18360 apply them if appropriate. */
18361 if (cp_parser_allow_gnu_extensions_p (parser
))
18363 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18364 cplus_decl_attributes (&type
,
18366 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18369 /* Finish up the enumeration. */
18370 if (type
!= error_mark_node
)
18372 if (new_value_list
)
18373 finish_enum_value_list (type
);
18375 finish_enum (type
);
18378 if (nested_name_specifier
)
18380 if (CLASS_TYPE_P (nested_name_specifier
))
18382 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18383 pop_scope (nested_name_specifier
);
18385 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18387 pop_nested_namespace (nested_name_specifier
);
18391 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18395 /* Parse an enumerator-list. The enumerators all have the indicated
18399 enumerator-definition
18400 enumerator-list , enumerator-definition */
18403 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18407 /* Parse an enumerator-definition. */
18408 cp_parser_enumerator_definition (parser
, type
);
18410 /* If the next token is not a ',', we've reached the end of
18412 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18414 /* Otherwise, consume the `,' and keep going. */
18415 cp_lexer_consume_token (parser
->lexer
);
18416 /* If the next token is a `}', there is a trailing comma. */
18417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18419 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18420 pedwarn (input_location
, OPT_Wpedantic
,
18421 "comma at end of enumerator list");
18427 /* Parse an enumerator-definition. The enumerator has the indicated
18430 enumerator-definition:
18432 enumerator = constant-expression
18439 enumerator-definition:
18440 enumerator attributes [opt]
18441 enumerator attributes [opt] = constant-expression */
18444 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18450 /* Save the input location because we are interested in the location
18451 of the identifier and not the location of the explicit value. */
18452 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18454 /* Look for the identifier. */
18455 identifier
= cp_parser_identifier (parser
);
18456 if (identifier
== error_mark_node
)
18459 /* Parse any specified attributes. */
18460 tree attrs
= cp_parser_attributes_opt (parser
);
18462 /* If the next token is an '=', then there is an explicit value. */
18463 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18465 /* Consume the `=' token. */
18466 cp_lexer_consume_token (parser
->lexer
);
18467 /* Parse the value. */
18468 value
= cp_parser_constant_expression (parser
);
18473 /* If we are processing a template, make sure the initializer of the
18474 enumerator doesn't contain any bare template parameter pack. */
18475 if (check_for_bare_parameter_packs (value
))
18476 value
= error_mark_node
;
18478 /* Create the enumerator. */
18479 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18482 /* Parse a namespace-name.
18485 original-namespace-name
18488 Returns the NAMESPACE_DECL for the namespace. */
18491 cp_parser_namespace_name (cp_parser
* parser
)
18494 tree namespace_decl
;
18496 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18498 /* Get the name of the namespace. */
18499 identifier
= cp_parser_identifier (parser
);
18500 if (identifier
== error_mark_node
)
18501 return error_mark_node
;
18503 /* Look up the identifier in the currently active scope. Look only
18504 for namespaces, due to:
18506 [basic.lookup.udir]
18508 When looking up a namespace-name in a using-directive or alias
18509 definition, only namespace names are considered.
18513 [basic.lookup.qual]
18515 During the lookup of a name preceding the :: scope resolution
18516 operator, object, function, and enumerator names are ignored.
18518 (Note that cp_parser_qualifying_entity only calls this
18519 function if the token after the name is the scope resolution
18521 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18523 /*is_template=*/false,
18524 /*is_namespace=*/true,
18525 /*check_dependency=*/true,
18526 /*ambiguous_decls=*/NULL
,
18528 /* If it's not a namespace, issue an error. */
18529 if (namespace_decl
== error_mark_node
18530 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18532 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18533 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18534 cp_parser_error (parser
, "expected namespace-name");
18535 namespace_decl
= error_mark_node
;
18538 return namespace_decl
;
18541 /* Parse a namespace-definition.
18543 namespace-definition:
18544 named-namespace-definition
18545 unnamed-namespace-definition
18547 named-namespace-definition:
18548 original-namespace-definition
18549 extension-namespace-definition
18551 original-namespace-definition:
18552 namespace identifier { namespace-body }
18554 extension-namespace-definition:
18555 namespace original-namespace-name { namespace-body }
18557 unnamed-namespace-definition:
18558 namespace { namespace-body } */
18561 cp_parser_namespace_definition (cp_parser
* parser
)
18564 int nested_definition_count
= 0;
18566 cp_ensure_no_omp_declare_simd (parser
);
18567 cp_ensure_no_oacc_routine (parser
);
18569 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18573 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18574 cp_lexer_consume_token (parser
->lexer
);
18577 /* Look for the `namespace' keyword. */
18579 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18581 /* Parse any specified attributes before the identifier. */
18582 tree attribs
= cp_parser_attributes_opt (parser
);
18586 identifier
= NULL_TREE
;
18588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18590 identifier
= cp_parser_identifier (parser
);
18592 /* Parse any attributes specified after the identifier. */
18593 attribs
= chainon (attribs
, cp_parser_attributes_opt (parser
));
18596 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18599 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18600 pedwarn (input_location
, OPT_Wpedantic
,
18601 "nested namespace definitions only available with "
18602 "-std=c++17 or -std=gnu++17");
18604 /* Nested namespace names can create new namespaces (unlike
18605 other qualified-ids). */
18606 if (int count
= identifier
? push_namespace (identifier
) : 0)
18607 nested_definition_count
+= count
;
18609 cp_parser_error (parser
, "nested namespace name required");
18610 cp_lexer_consume_token (parser
->lexer
);
18613 if (nested_definition_count
&& !identifier
)
18614 cp_parser_error (parser
, "namespace name required");
18616 if (nested_definition_count
&& attribs
)
18617 error_at (token
->location
,
18618 "a nested namespace definition cannot have attributes");
18619 if (nested_definition_count
&& is_inline
)
18620 error_at (token
->location
,
18621 "a nested namespace definition cannot be inline");
18623 /* Start the namespace. */
18624 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18626 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18628 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18630 /* Look for the `{' to validate starting the namespace. */
18631 matching_braces braces
;
18632 if (braces
.require_open (parser
))
18634 /* Parse the body of the namespace. */
18635 cp_parser_namespace_body (parser
);
18637 /* Look for the final `}'. */
18638 braces
.require_close (parser
);
18641 if (has_visibility
)
18642 pop_visibility (1);
18644 /* Pop the nested namespace definitions. */
18645 while (nested_definition_count
--)
18649 /* Parse a namespace-body.
18652 declaration-seq [opt] */
18655 cp_parser_namespace_body (cp_parser
* parser
)
18657 cp_parser_declaration_seq_opt (parser
);
18660 /* Parse a namespace-alias-definition.
18662 namespace-alias-definition:
18663 namespace identifier = qualified-namespace-specifier ; */
18666 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18669 tree namespace_specifier
;
18671 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18673 /* Look for the `namespace' keyword. */
18674 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18675 /* Look for the identifier. */
18676 identifier
= cp_parser_identifier (parser
);
18677 if (identifier
== error_mark_node
)
18679 /* Look for the `=' token. */
18680 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18681 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18683 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18684 /* Skip the definition. */
18685 cp_lexer_consume_token (parser
->lexer
);
18686 if (cp_parser_skip_to_closing_brace (parser
))
18687 cp_lexer_consume_token (parser
->lexer
);
18690 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18691 /* Look for the qualified-namespace-specifier. */
18692 namespace_specifier
18693 = cp_parser_qualified_namespace_specifier (parser
);
18694 /* Look for the `;' token. */
18695 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18697 /* Register the alias in the symbol table. */
18698 do_namespace_alias (identifier
, namespace_specifier
);
18701 /* Parse a qualified-namespace-specifier.
18703 qualified-namespace-specifier:
18704 :: [opt] nested-name-specifier [opt] namespace-name
18706 Returns a NAMESPACE_DECL corresponding to the specified
18710 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18712 /* Look for the optional `::'. */
18713 cp_parser_global_scope_opt (parser
,
18714 /*current_scope_valid_p=*/false);
18716 /* Look for the optional nested-name-specifier. */
18717 cp_parser_nested_name_specifier_opt (parser
,
18718 /*typename_keyword_p=*/false,
18719 /*check_dependency_p=*/true,
18721 /*is_declaration=*/true);
18723 return cp_parser_namespace_name (parser
);
18726 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18727 access declaration.
18730 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18731 using :: unqualified-id ;
18733 access-declaration:
18739 cp_parser_using_declaration (cp_parser
* parser
,
18740 bool access_declaration_p
)
18743 bool typename_p
= false;
18744 bool global_scope_p
;
18748 int oldcount
= errorcount
;
18749 cp_token
*diag_token
= NULL
;
18751 if (access_declaration_p
)
18753 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18754 cp_parser_parse_tentatively (parser
);
18758 /* Look for the `using' keyword. */
18759 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18762 /* Peek at the next token. */
18763 token
= cp_lexer_peek_token (parser
->lexer
);
18764 /* See if it's `typename'. */
18765 if (token
->keyword
== RID_TYPENAME
)
18767 /* Remember that we've seen it. */
18769 /* Consume the `typename' token. */
18770 cp_lexer_consume_token (parser
->lexer
);
18774 /* Look for the optional global scope qualification. */
18776 = (cp_parser_global_scope_opt (parser
,
18777 /*current_scope_valid_p=*/false)
18780 /* If we saw `typename', or didn't see `::', then there must be a
18781 nested-name-specifier present. */
18782 if (typename_p
|| !global_scope_p
)
18784 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18785 /*check_dependency_p=*/true,
18787 /*is_declaration=*/true);
18788 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18790 cp_parser_skip_to_end_of_block_or_statement (parser
);
18794 /* Otherwise, we could be in either of the two productions. In that
18795 case, treat the nested-name-specifier as optional. */
18797 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18798 /*typename_keyword_p=*/false,
18799 /*check_dependency_p=*/true,
18801 /*is_declaration=*/true);
18803 qscope
= global_namespace
;
18804 else if (UNSCOPED_ENUM_P (qscope
))
18805 qscope
= CP_TYPE_CONTEXT (qscope
);
18807 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18808 /* Something has already gone wrong; there's no need to parse
18809 further. Since an error has occurred, the return value of
18810 cp_parser_parse_definitely will be false, as required. */
18811 return cp_parser_parse_definitely (parser
);
18813 token
= cp_lexer_peek_token (parser
->lexer
);
18814 /* Parse the unqualified-id. */
18815 identifier
= cp_parser_unqualified_id (parser
,
18816 /*template_keyword_p=*/false,
18817 /*check_dependency_p=*/true,
18818 /*declarator_p=*/true,
18819 /*optional_p=*/false);
18821 if (access_declaration_p
)
18823 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18824 cp_parser_simulate_error (parser
);
18825 if (!cp_parser_parse_definitely (parser
))
18828 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18830 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18831 if (cxx_dialect
< cxx17
18832 && !in_system_header_at (ell
->location
))
18833 pedwarn (ell
->location
, 0,
18834 "pack expansion in using-declaration only available "
18835 "with -std=c++17 or -std=gnu++17");
18836 qscope
= make_pack_expansion (qscope
);
18839 /* The function we call to handle a using-declaration is different
18840 depending on what scope we are in. */
18841 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18843 else if (!identifier_p (identifier
)
18844 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18845 /* [namespace.udecl]
18847 A using declaration shall not name a template-id. */
18848 error_at (token
->location
,
18849 "a template-id may not appear in a using-declaration");
18852 if (at_class_scope_p ())
18854 /* Create the USING_DECL. */
18855 decl
= do_class_using_decl (qscope
, identifier
);
18857 if (decl
&& typename_p
)
18858 USING_DECL_TYPENAME_P (decl
) = 1;
18860 if (check_for_bare_parameter_packs (decl
))
18862 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18866 /* Add it to the list of members in this class. */
18867 finish_member_declaration (decl
);
18871 decl
= cp_parser_lookup_name_simple (parser
,
18874 if (decl
== error_mark_node
)
18875 cp_parser_name_lookup_error (parser
, identifier
,
18878 else if (check_for_bare_parameter_packs (decl
))
18880 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18883 else if (!at_namespace_scope_p ())
18884 finish_local_using_decl (decl
, qscope
, identifier
);
18886 finish_namespace_using_decl (decl
, qscope
, identifier
);
18890 if (!access_declaration_p
18891 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18893 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18894 if (cxx_dialect
< cxx17
)
18895 pedwarn (comma
->location
, 0,
18896 "comma-separated list in using-declaration only available "
18897 "with -std=c++17 or -std=gnu++17");
18901 /* Look for the final `;'. */
18902 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18904 if (access_declaration_p
&& errorcount
== oldcount
)
18905 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18906 "access declarations are deprecated "
18907 "in favour of using-declarations; "
18908 "suggestion: add the %<using%> keyword");
18913 /* Parse an alias-declaration.
18916 using identifier attribute-specifier-seq [opt] = type-id */
18919 cp_parser_alias_declaration (cp_parser
* parser
)
18921 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18922 location_t id_location
;
18923 cp_declarator
*declarator
;
18924 cp_decl_specifier_seq decl_specs
;
18926 const char *saved_message
= NULL
;
18928 /* Look for the `using' keyword. */
18929 cp_token
*using_token
18930 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18931 if (using_token
== NULL
)
18932 return error_mark_node
;
18934 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18935 id
= cp_parser_identifier (parser
);
18936 if (id
== error_mark_node
)
18937 return error_mark_node
;
18939 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18940 attributes
= cp_parser_attributes_opt (parser
);
18941 if (attributes
== error_mark_node
)
18942 return error_mark_node
;
18944 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18946 if (cp_parser_error_occurred (parser
))
18947 return error_mark_node
;
18949 cp_parser_commit_to_tentative_parse (parser
);
18951 /* Now we are going to parse the type-id of the declaration. */
18956 "A type-specifier-seq shall not define a class or enumeration
18957 unless it appears in the type-id of an alias-declaration (7.1.3) that
18958 is not the declaration of a template-declaration."
18960 In other words, if we currently are in an alias template, the
18961 type-id should not define a type.
18963 So let's set parser->type_definition_forbidden_message in that
18964 case; cp_parser_check_type_definition (called by
18965 cp_parser_class_specifier) will then emit an error if a type is
18966 defined in the type-id. */
18967 if (parser
->num_template_parameter_lists
)
18969 saved_message
= parser
->type_definition_forbidden_message
;
18970 parser
->type_definition_forbidden_message
=
18971 G_("types may not be defined in alias template declarations");
18974 type
= cp_parser_type_id (parser
);
18976 /* Restore the error message if need be. */
18977 if (parser
->num_template_parameter_lists
)
18978 parser
->type_definition_forbidden_message
= saved_message
;
18980 if (type
== error_mark_node
18981 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18983 cp_parser_skip_to_end_of_block_or_statement (parser
);
18984 return error_mark_node
;
18987 /* A typedef-name can also be introduced by an alias-declaration. The
18988 identifier following the using keyword becomes a typedef-name. It has
18989 the same semantics as if it were introduced by the typedef
18990 specifier. In particular, it does not define a new type and it shall
18991 not appear in the type-id. */
18993 clear_decl_specs (&decl_specs
);
18994 decl_specs
.type
= type
;
18995 if (attributes
!= NULL_TREE
)
18997 decl_specs
.attributes
= attributes
;
18998 set_and_check_decl_spec_loc (&decl_specs
,
19002 set_and_check_decl_spec_loc (&decl_specs
,
19005 set_and_check_decl_spec_loc (&decl_specs
,
19009 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19010 declarator
->id_loc
= id_location
;
19012 member_p
= at_class_scope_p ();
19014 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19015 NULL_TREE
, attributes
);
19017 decl
= start_decl (declarator
, &decl_specs
, 0,
19018 attributes
, NULL_TREE
, &pushed_scope
);
19019 if (decl
== error_mark_node
)
19022 // Attach constraints to the alias declaration.
19023 if (flag_concepts
&& current_template_parms
)
19025 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19026 tree constr
= build_constraints (reqs
, NULL_TREE
);
19027 set_constraints (decl
, constr
);
19030 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19033 pop_scope (pushed_scope
);
19035 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19036 added into the symbol table; otherwise, return the TYPE_DECL. */
19037 if (DECL_LANG_SPECIFIC (decl
)
19038 && DECL_TEMPLATE_INFO (decl
)
19039 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19041 decl
= DECL_TI_TEMPLATE (decl
);
19043 check_member_template (decl
);
19049 /* Parse a using-directive.
19052 using namespace :: [opt] nested-name-specifier [opt]
19053 namespace-name ; */
19056 cp_parser_using_directive (cp_parser
* parser
)
19058 tree namespace_decl
;
19061 /* Look for the `using' keyword. */
19062 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19063 /* And the `namespace' keyword. */
19064 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19065 /* Look for the optional `::' operator. */
19066 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19067 /* And the optional nested-name-specifier. */
19068 cp_parser_nested_name_specifier_opt (parser
,
19069 /*typename_keyword_p=*/false,
19070 /*check_dependency_p=*/true,
19072 /*is_declaration=*/true);
19073 /* Get the namespace being used. */
19074 namespace_decl
= cp_parser_namespace_name (parser
);
19075 /* And any specified attributes. */
19076 attribs
= cp_parser_attributes_opt (parser
);
19078 /* Update the symbol table. */
19079 if (namespace_bindings_p ())
19080 finish_namespace_using_directive (namespace_decl
, attribs
);
19082 finish_local_using_directive (namespace_decl
, attribs
);
19084 /* Look for the final `;'. */
19085 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19088 /* Parse an asm-definition.
19091 asm ( string-literal ) ;
19096 asm volatile [opt] ( string-literal ) ;
19097 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19098 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19099 : asm-operand-list [opt] ) ;
19100 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19101 : asm-operand-list [opt]
19102 : asm-clobber-list [opt] ) ;
19103 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19104 : asm-clobber-list [opt]
19105 : asm-goto-list ) ; */
19108 cp_parser_asm_definition (cp_parser
* parser
)
19111 tree outputs
= NULL_TREE
;
19112 tree inputs
= NULL_TREE
;
19113 tree clobbers
= NULL_TREE
;
19114 tree labels
= NULL_TREE
;
19116 bool volatile_p
= false;
19117 bool extended_p
= false;
19118 bool invalid_inputs_p
= false;
19119 bool invalid_outputs_p
= false;
19120 bool goto_p
= false;
19121 required_token missing
= RT_NONE
;
19123 /* Look for the `asm' keyword. */
19124 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19126 if (parser
->in_function_body
19127 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19129 error ("%<asm%> in %<constexpr%> function");
19130 cp_function_chain
->invalid_constexpr
= true;
19133 /* See if the next token is `volatile'. */
19134 if (cp_parser_allow_gnu_extensions_p (parser
)
19135 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19137 /* Remember that we saw the `volatile' keyword. */
19139 /* Consume the token. */
19140 cp_lexer_consume_token (parser
->lexer
);
19142 if (cp_parser_allow_gnu_extensions_p (parser
)
19143 && parser
->in_function_body
19144 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19146 /* Remember that we saw the `goto' keyword. */
19148 /* Consume the token. */
19149 cp_lexer_consume_token (parser
->lexer
);
19151 /* Look for the opening `('. */
19152 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19154 /* Look for the string. */
19155 string
= cp_parser_string_literal (parser
, false, false);
19156 if (string
== error_mark_node
)
19158 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19159 /*consume_paren=*/true);
19163 /* If we're allowing GNU extensions, check for the extended assembly
19164 syntax. Unfortunately, the `:' tokens need not be separated by
19165 a space in C, and so, for compatibility, we tolerate that here
19166 too. Doing that means that we have to treat the `::' operator as
19168 if (cp_parser_allow_gnu_extensions_p (parser
)
19169 && parser
->in_function_body
19170 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19171 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19173 bool inputs_p
= false;
19174 bool clobbers_p
= false;
19175 bool labels_p
= false;
19177 /* The extended syntax was used. */
19180 /* Look for outputs. */
19181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19183 /* Consume the `:'. */
19184 cp_lexer_consume_token (parser
->lexer
);
19185 /* Parse the output-operands. */
19186 if (cp_lexer_next_token_is_not (parser
->lexer
,
19188 && cp_lexer_next_token_is_not (parser
->lexer
,
19190 && cp_lexer_next_token_is_not (parser
->lexer
,
19194 outputs
= cp_parser_asm_operand_list (parser
);
19195 if (outputs
== error_mark_node
)
19196 invalid_outputs_p
= true;
19199 /* If the next token is `::', there are no outputs, and the
19200 next token is the beginning of the inputs. */
19201 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19202 /* The inputs are coming next. */
19205 /* Look for inputs. */
19207 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19209 /* Consume the `:' or `::'. */
19210 cp_lexer_consume_token (parser
->lexer
);
19211 /* Parse the output-operands. */
19212 if (cp_lexer_next_token_is_not (parser
->lexer
,
19214 && cp_lexer_next_token_is_not (parser
->lexer
,
19216 && cp_lexer_next_token_is_not (parser
->lexer
,
19219 inputs
= cp_parser_asm_operand_list (parser
);
19220 if (inputs
== error_mark_node
)
19221 invalid_inputs_p
= true;
19224 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19225 /* The clobbers are coming next. */
19228 /* Look for clobbers. */
19230 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19233 /* Consume the `:' or `::'. */
19234 cp_lexer_consume_token (parser
->lexer
);
19235 /* Parse the clobbers. */
19236 if (cp_lexer_next_token_is_not (parser
->lexer
,
19238 && cp_lexer_next_token_is_not (parser
->lexer
,
19240 clobbers
= cp_parser_asm_clobber_list (parser
);
19243 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19244 /* The labels are coming next. */
19247 /* Look for labels. */
19249 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19252 /* Consume the `:' or `::'. */
19253 cp_lexer_consume_token (parser
->lexer
);
19254 /* Parse the labels. */
19255 labels
= cp_parser_asm_label_list (parser
);
19258 if (goto_p
&& !labels_p
)
19259 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19262 missing
= RT_COLON_SCOPE
;
19264 /* Look for the closing `)'. */
19265 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19266 missing
? missing
: RT_CLOSE_PAREN
))
19267 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19268 /*consume_paren=*/true);
19269 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19271 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19273 /* Create the ASM_EXPR. */
19274 if (parser
->in_function_body
)
19276 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19277 inputs
, clobbers
, labels
);
19278 /* If the extended syntax was not used, mark the ASM_EXPR. */
19281 tree temp
= asm_stmt
;
19282 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19283 temp
= TREE_OPERAND (temp
, 0);
19285 ASM_INPUT_P (temp
) = 1;
19289 symtab
->finalize_toplevel_asm (string
);
19293 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19294 type that comes from the decl-specifier-seq. */
19297 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19299 for (cp_declarator
*d
= declarator
; d
;)
19309 if (TYPE_PTRMEMFUNC_P (type
))
19310 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19311 type
= TREE_TYPE (type
);
19319 /* Declarators [gram.dcl.decl] */
19321 /* Parse an init-declarator.
19324 declarator initializer [opt]
19329 declarator asm-specification [opt] attributes [opt] initializer [opt]
19331 function-definition:
19332 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19334 decl-specifier-seq [opt] declarator function-try-block
19338 function-definition:
19339 __extension__ function-definition
19343 function-definition:
19344 decl-specifier-seq [opt] declarator function-transaction-block
19346 The DECL_SPECIFIERS apply to this declarator. Returns a
19347 representation of the entity declared. If MEMBER_P is TRUE, then
19348 this declarator appears in a class scope. The new DECL created by
19349 this declarator is returned.
19351 The CHECKS are access checks that should be performed once we know
19352 what entity is being declared (and, therefore, what classes have
19355 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19356 for a function-definition here as well. If the declarator is a
19357 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19358 be TRUE upon return. By that point, the function-definition will
19359 have been completely parsed.
19361 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19364 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19365 parsed declaration if it is an uninitialized single declarator not followed
19366 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19367 if present, will not be consumed. If returned, this declarator will be
19368 created with SD_INITIALIZED but will not call cp_finish_decl.
19370 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19371 and there is an initializer, the pointed location_t is set to the
19372 location of the '=' or `(', or '{' in C++11 token introducing the
19376 cp_parser_init_declarator (cp_parser
* parser
,
19377 cp_decl_specifier_seq
*decl_specifiers
,
19378 vec
<deferred_access_check
, va_gc
> *checks
,
19379 bool function_definition_allowed_p
,
19381 int declares_class_or_enum
,
19382 bool* function_definition_p
,
19383 tree
* maybe_range_for_decl
,
19384 location_t
* init_loc
,
19387 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19388 *attributes_start_token
= NULL
;
19389 cp_declarator
*declarator
;
19390 tree prefix_attributes
;
19391 tree attributes
= NULL
;
19392 tree asm_specification
;
19394 tree decl
= NULL_TREE
;
19396 int is_initialized
;
19397 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19398 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19400 enum cpp_ttype initialization_kind
;
19401 bool is_direct_init
= false;
19402 bool is_non_constant_init
;
19403 int ctor_dtor_or_conv_p
;
19404 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19405 tree pushed_scope
= NULL_TREE
;
19406 bool range_for_decl_p
= false;
19407 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19408 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19410 /* Gather the attributes that were provided with the
19411 decl-specifiers. */
19412 prefix_attributes
= decl_specifiers
->attributes
;
19414 /* Assume that this is not the declarator for a function
19416 if (function_definition_p
)
19417 *function_definition_p
= false;
19419 /* Default arguments are only permitted for function parameters. */
19420 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19421 parser
->default_arg_ok_p
= false;
19423 /* Defer access checks while parsing the declarator; we cannot know
19424 what names are accessible until we know what is being
19426 resume_deferring_access_checks ();
19428 token
= cp_lexer_peek_token (parser
->lexer
);
19430 /* Parse the declarator. */
19432 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19433 &ctor_dtor_or_conv_p
,
19434 /*parenthesized_p=*/NULL
,
19435 member_p
, friend_p
);
19436 /* Gather up the deferred checks. */
19437 stop_deferring_access_checks ();
19439 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19441 /* If the DECLARATOR was erroneous, there's no need to go
19443 if (declarator
== cp_error_declarator
)
19444 return error_mark_node
;
19446 /* Check that the number of template-parameter-lists is OK. */
19447 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19449 return error_mark_node
;
19451 if (declares_class_or_enum
& 2)
19452 cp_parser_check_for_definition_in_return_type (declarator
,
19453 decl_specifiers
->type
,
19454 decl_specifiers
->locations
[ds_type_spec
]);
19456 /* Figure out what scope the entity declared by the DECLARATOR is
19457 located in. `grokdeclarator' sometimes changes the scope, so
19458 we compute it now. */
19459 scope
= get_scope_of_declarator (declarator
);
19461 /* Perform any lookups in the declared type which were thought to be
19462 dependent, but are not in the scope of the declarator. */
19463 decl_specifiers
->type
19464 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19466 /* If we're allowing GNU extensions, look for an
19467 asm-specification. */
19468 if (cp_parser_allow_gnu_extensions_p (parser
))
19470 /* Look for an asm-specification. */
19471 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19472 asm_specification
= cp_parser_asm_specification_opt (parser
);
19475 asm_specification
= NULL_TREE
;
19477 /* Look for attributes. */
19478 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19479 attributes
= cp_parser_attributes_opt (parser
);
19481 /* Peek at the next token. */
19482 token
= cp_lexer_peek_token (parser
->lexer
);
19484 bool bogus_implicit_tmpl
= false;
19486 if (function_declarator_p (declarator
))
19488 /* Handle C++17 deduction guides. */
19489 if (!decl_specifiers
->type
19490 && ctor_dtor_or_conv_p
<= 0
19491 && cxx_dialect
>= cxx17
)
19493 cp_declarator
*id
= get_id_declarator (declarator
);
19494 tree name
= id
->u
.id
.unqualified_name
;
19495 parser
->scope
= id
->u
.id
.qualifying_scope
;
19496 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19498 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19499 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19501 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19502 id
->u
.id
.sfk
= sfk_deduction_guide
;
19503 ctor_dtor_or_conv_p
= 1;
19507 /* Check to see if the token indicates the start of a
19508 function-definition. */
19509 if (cp_parser_token_starts_function_definition_p (token
))
19511 if (!function_definition_allowed_p
)
19513 /* If a function-definition should not appear here, issue an
19515 cp_parser_error (parser
,
19516 "a function-definition is not allowed here");
19517 return error_mark_node
;
19520 location_t func_brace_location
19521 = cp_lexer_peek_token (parser
->lexer
)->location
;
19523 /* Neither attributes nor an asm-specification are allowed
19524 on a function-definition. */
19525 if (asm_specification
)
19526 error_at (asm_spec_start_token
->location
,
19527 "an asm-specification is not allowed "
19528 "on a function-definition");
19530 error_at (attributes_start_token
->location
,
19531 "attributes are not allowed "
19532 "on a function-definition");
19533 /* This is a function-definition. */
19534 *function_definition_p
= true;
19536 /* Parse the function definition. */
19538 decl
= cp_parser_save_member_function_body (parser
,
19541 prefix_attributes
);
19544 (cp_parser_function_definition_from_specifiers_and_declarator
19545 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19547 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19549 /* This is where the prologue starts... */
19550 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19551 = func_brace_location
;
19557 else if (parser
->fully_implicit_function_template_p
)
19559 /* A non-template declaration involving a function parameter list
19560 containing an implicit template parameter will be made into a
19561 template. If the resulting declaration is not going to be an
19562 actual function then finish the template scope here to prevent it.
19563 An error message will be issued once we have a decl to talk about.
19565 FIXME probably we should do type deduction rather than create an
19566 implicit template, but the standard currently doesn't allow it. */
19567 bogus_implicit_tmpl
= true;
19568 finish_fully_implicit_template (parser
, NULL_TREE
);
19573 Only in function declarations for constructors, destructors, type
19574 conversions, and deduction guides can the decl-specifier-seq be omitted.
19576 We explicitly postpone this check past the point where we handle
19577 function-definitions because we tolerate function-definitions
19578 that are missing their return types in some modes. */
19579 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19581 cp_parser_error (parser
,
19582 "expected constructor, destructor, or type conversion");
19583 return error_mark_node
;
19586 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19587 if (token
->type
== CPP_EQ
19588 || token
->type
== CPP_OPEN_PAREN
19589 || token
->type
== CPP_OPEN_BRACE
)
19591 is_initialized
= SD_INITIALIZED
;
19592 initialization_kind
= token
->type
;
19593 if (maybe_range_for_decl
)
19594 *maybe_range_for_decl
= error_mark_node
;
19595 tmp_init_loc
= token
->location
;
19596 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19597 *init_loc
= tmp_init_loc
;
19599 if (token
->type
== CPP_EQ
19600 && function_declarator_p (declarator
))
19602 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19603 if (t2
->keyword
== RID_DEFAULT
)
19604 is_initialized
= SD_DEFAULTED
;
19605 else if (t2
->keyword
== RID_DELETE
)
19606 is_initialized
= SD_DELETED
;
19611 /* If the init-declarator isn't initialized and isn't followed by a
19612 `,' or `;', it's not a valid init-declarator. */
19613 if (token
->type
!= CPP_COMMA
19614 && token
->type
!= CPP_SEMICOLON
)
19616 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19617 range_for_decl_p
= true;
19620 if (!maybe_range_for_decl
)
19621 cp_parser_error (parser
, "expected initializer");
19622 return error_mark_node
;
19625 is_initialized
= SD_UNINITIALIZED
;
19626 initialization_kind
= CPP_EOF
;
19629 /* Because start_decl has side-effects, we should only call it if we
19630 know we're going ahead. By this point, we know that we cannot
19631 possibly be looking at any other construct. */
19632 cp_parser_commit_to_tentative_parse (parser
);
19634 /* Enter the newly declared entry in the symbol table. If we're
19635 processing a declaration in a class-specifier, we wait until
19636 after processing the initializer. */
19639 if (parser
->in_unbraced_linkage_specification_p
)
19640 decl_specifiers
->storage_class
= sc_extern
;
19641 decl
= start_decl (declarator
, decl_specifiers
,
19642 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19643 attributes
, prefix_attributes
, &pushed_scope
);
19644 cp_finalize_omp_declare_simd (parser
, decl
);
19645 cp_finalize_oacc_routine (parser
, decl
, false);
19646 /* Adjust location of decl if declarator->id_loc is more appropriate:
19647 set, and decl wasn't merged with another decl, in which case its
19648 location would be different from input_location, and more accurate. */
19650 && declarator
->id_loc
!= UNKNOWN_LOCATION
19651 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19652 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19655 /* Enter the SCOPE. That way unqualified names appearing in the
19656 initializer will be looked up in SCOPE. */
19657 pushed_scope
= push_scope (scope
);
19659 /* Perform deferred access control checks, now that we know in which
19660 SCOPE the declared entity resides. */
19661 if (!member_p
&& decl
)
19663 tree saved_current_function_decl
= NULL_TREE
;
19665 /* If the entity being declared is a function, pretend that we
19666 are in its scope. If it is a `friend', it may have access to
19667 things that would not otherwise be accessible. */
19668 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19670 saved_current_function_decl
= current_function_decl
;
19671 current_function_decl
= decl
;
19674 /* Perform access checks for template parameters. */
19675 cp_parser_perform_template_parameter_access_checks (checks
);
19677 /* Perform the access control checks for the declarator and the
19678 decl-specifiers. */
19679 perform_deferred_access_checks (tf_warning_or_error
);
19681 /* Restore the saved value. */
19682 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19683 current_function_decl
= saved_current_function_decl
;
19686 /* Parse the initializer. */
19687 initializer
= NULL_TREE
;
19688 is_direct_init
= false;
19689 is_non_constant_init
= true;
19690 if (is_initialized
)
19692 if (function_declarator_p (declarator
))
19694 if (initialization_kind
== CPP_EQ
)
19695 initializer
= cp_parser_pure_specifier (parser
);
19698 /* If the declaration was erroneous, we don't really
19699 know what the user intended, so just silently
19700 consume the initializer. */
19701 if (decl
!= error_mark_node
)
19702 error_at (tmp_init_loc
, "initializer provided for function");
19703 cp_parser_skip_to_closing_parenthesis (parser
,
19704 /*recovering=*/true,
19705 /*or_comma=*/false,
19706 /*consume_paren=*/true);
19711 /* We want to record the extra mangling scope for in-class
19712 initializers of class members and initializers of static data
19713 member templates. The former involves deferring
19714 parsing of the initializer until end of class as with default
19715 arguments. So right here we only handle the latter. */
19716 if (!member_p
&& processing_template_decl
)
19717 start_lambda_scope (decl
);
19718 initializer
= cp_parser_initializer (parser
,
19720 &is_non_constant_init
);
19721 if (!member_p
&& processing_template_decl
)
19722 finish_lambda_scope ();
19723 if (initializer
== error_mark_node
)
19724 cp_parser_skip_to_end_of_statement (parser
);
19728 /* The old parser allows attributes to appear after a parenthesized
19729 initializer. Mark Mitchell proposed removing this functionality
19730 on the GCC mailing lists on 2002-08-13. This parser accepts the
19731 attributes -- but ignores them. */
19732 if (cp_parser_allow_gnu_extensions_p (parser
)
19733 && initialization_kind
== CPP_OPEN_PAREN
)
19734 if (cp_parser_attributes_opt (parser
))
19735 warning (OPT_Wattributes
,
19736 "attributes after parenthesized initializer ignored");
19738 /* And now complain about a non-function implicit template. */
19739 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19740 error_at (DECL_SOURCE_LOCATION (decl
),
19741 "non-function %qD declared as implicit template", decl
);
19743 /* For an in-class declaration, use `grokfield' to create the
19749 pop_scope (pushed_scope
);
19750 pushed_scope
= NULL_TREE
;
19752 decl
= grokfield (declarator
, decl_specifiers
,
19753 initializer
, !is_non_constant_init
,
19754 /*asmspec=*/NULL_TREE
,
19755 chainon (attributes
, prefix_attributes
));
19756 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19757 cp_parser_save_default_args (parser
, decl
);
19758 cp_finalize_omp_declare_simd (parser
, decl
);
19759 cp_finalize_oacc_routine (parser
, decl
, false);
19762 /* Finish processing the declaration. But, skip member
19764 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19766 cp_finish_decl (decl
,
19767 initializer
, !is_non_constant_init
,
19769 /* If the initializer is in parentheses, then this is
19770 a direct-initialization, which means that an
19771 `explicit' constructor is OK. Otherwise, an
19772 `explicit' constructor cannot be used. */
19773 ((is_direct_init
|| !is_initialized
)
19774 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19776 else if ((cxx_dialect
!= cxx98
) && friend_p
19777 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19778 /* Core issue #226 (C++0x only): A default template-argument
19779 shall not be specified in a friend class template
19781 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19782 /*is_partial=*/false, /*is_friend_decl=*/1);
19784 if (!friend_p
&& pushed_scope
)
19785 pop_scope (pushed_scope
);
19787 if (function_declarator_p (declarator
)
19788 && parser
->fully_implicit_function_template_p
)
19791 decl
= finish_fully_implicit_template (parser
, decl
);
19793 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19796 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19797 && type_uses_auto (decl_specifiers
->type
))
19798 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19803 /* Parse a declarator.
19807 ptr-operator declarator
19809 abstract-declarator:
19810 ptr-operator abstract-declarator [opt]
19811 direct-abstract-declarator
19816 attributes [opt] direct-declarator
19817 attributes [opt] ptr-operator declarator
19819 abstract-declarator:
19820 attributes [opt] ptr-operator abstract-declarator [opt]
19821 attributes [opt] direct-abstract-declarator
19823 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19824 detect constructors, destructors, deduction guides, or conversion operators.
19825 It is set to -1 if the declarator is a name, and +1 if it is a
19826 function. Otherwise it is set to zero. Usually you just want to
19827 test for >0, but internally the negative value is used.
19829 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19830 a decl-specifier-seq unless it declares a constructor, destructor,
19831 or conversion. It might seem that we could check this condition in
19832 semantic analysis, rather than parsing, but that makes it difficult
19833 to handle something like `f()'. We want to notice that there are
19834 no decl-specifiers, and therefore realize that this is an
19835 expression, not a declaration.)
19837 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19838 the declarator is a direct-declarator of the form "(...)".
19840 MEMBER_P is true iff this declarator is a member-declarator.
19842 FRIEND_P is true iff this declarator is a friend. */
19844 static cp_declarator
*
19845 cp_parser_declarator (cp_parser
* parser
,
19846 cp_parser_declarator_kind dcl_kind
,
19847 int* ctor_dtor_or_conv_p
,
19848 bool* parenthesized_p
,
19849 bool member_p
, bool friend_p
)
19851 cp_declarator
*declarator
;
19852 enum tree_code code
;
19853 cp_cv_quals cv_quals
;
19855 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19857 /* Assume this is not a constructor, destructor, or type-conversion
19859 if (ctor_dtor_or_conv_p
)
19860 *ctor_dtor_or_conv_p
= 0;
19862 if (cp_parser_allow_gnu_extensions_p (parser
))
19863 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19865 /* Check for the ptr-operator production. */
19866 cp_parser_parse_tentatively (parser
);
19867 /* Parse the ptr-operator. */
19868 code
= cp_parser_ptr_operator (parser
,
19873 /* If that worked, then we have a ptr-operator. */
19874 if (cp_parser_parse_definitely (parser
))
19876 /* If a ptr-operator was found, then this declarator was not
19878 if (parenthesized_p
)
19879 *parenthesized_p
= true;
19880 /* The dependent declarator is optional if we are parsing an
19881 abstract-declarator. */
19882 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19883 cp_parser_parse_tentatively (parser
);
19885 /* Parse the dependent declarator. */
19886 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19887 /*ctor_dtor_or_conv_p=*/NULL
,
19888 /*parenthesized_p=*/NULL
,
19889 /*member_p=*/false,
19892 /* If we are parsing an abstract-declarator, we must handle the
19893 case where the dependent declarator is absent. */
19894 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19895 && !cp_parser_parse_definitely (parser
))
19898 declarator
= cp_parser_make_indirect_declarator
19899 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19901 /* Everything else is a direct-declarator. */
19904 if (parenthesized_p
)
19905 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19907 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19908 ctor_dtor_or_conv_p
,
19909 member_p
, friend_p
);
19912 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19913 declarator
->attributes
= gnu_attributes
;
19917 /* Parse a direct-declarator or direct-abstract-declarator.
19921 direct-declarator ( parameter-declaration-clause )
19922 cv-qualifier-seq [opt]
19923 ref-qualifier [opt]
19924 exception-specification [opt]
19925 direct-declarator [ constant-expression [opt] ]
19928 direct-abstract-declarator:
19929 direct-abstract-declarator [opt]
19930 ( parameter-declaration-clause )
19931 cv-qualifier-seq [opt]
19932 ref-qualifier [opt]
19933 exception-specification [opt]
19934 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19935 ( abstract-declarator )
19937 Returns a representation of the declarator. DCL_KIND is
19938 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19939 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19940 we are parsing a direct-declarator. It is
19941 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19942 of ambiguity we prefer an abstract declarator, as per
19943 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19944 as for cp_parser_declarator. */
19946 static cp_declarator
*
19947 cp_parser_direct_declarator (cp_parser
* parser
,
19948 cp_parser_declarator_kind dcl_kind
,
19949 int* ctor_dtor_or_conv_p
,
19950 bool member_p
, bool friend_p
)
19953 cp_declarator
*declarator
= NULL
;
19954 tree scope
= NULL_TREE
;
19955 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19956 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19958 tree pushed_scope
= NULL_TREE
;
19959 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19963 /* Peek at the next token. */
19964 token
= cp_lexer_peek_token (parser
->lexer
);
19965 if (token
->type
== CPP_OPEN_PAREN
)
19967 /* This is either a parameter-declaration-clause, or a
19968 parenthesized declarator. When we know we are parsing a
19969 named declarator, it must be a parenthesized declarator
19970 if FIRST is true. For instance, `(int)' is a
19971 parameter-declaration-clause, with an omitted
19972 direct-abstract-declarator. But `((*))', is a
19973 parenthesized abstract declarator. Finally, when T is a
19974 template parameter `(T)' is a
19975 parameter-declaration-clause, and not a parenthesized
19978 We first try and parse a parameter-declaration-clause,
19979 and then try a nested declarator (if FIRST is true).
19981 It is not an error for it not to be a
19982 parameter-declaration-clause, even when FIRST is
19988 The first is the declaration of a function while the
19989 second is the definition of a variable, including its
19992 Having seen only the parenthesis, we cannot know which of
19993 these two alternatives should be selected. Even more
19994 complex are examples like:
19999 The former is a function-declaration; the latter is a
20000 variable initialization.
20002 Thus again, we try a parameter-declaration-clause, and if
20003 that fails, we back out and return. */
20005 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20008 bool is_declarator
= false;
20012 /* In a member-declarator, the only valid interpretation
20013 of a parenthesis is the start of a
20014 parameter-declaration-clause. (It is invalid to
20015 initialize a static data member with a parenthesized
20016 initializer; only the "=" form of initialization is
20019 cp_parser_parse_tentatively (parser
);
20021 /* Consume the `('. */
20022 matching_parens parens
;
20023 parens
.consume_open (parser
);
20026 /* If this is going to be an abstract declarator, we're
20027 in a declarator and we can't have default args. */
20028 parser
->default_arg_ok_p
= false;
20029 parser
->in_declarator_p
= true;
20032 begin_scope (sk_function_parms
, NULL_TREE
);
20034 /* Parse the parameter-declaration-clause. */
20035 params
= cp_parser_parameter_declaration_clause (parser
);
20037 /* Consume the `)'. */
20038 parens
.require_close (parser
);
20040 /* If all went well, parse the cv-qualifier-seq,
20041 ref-qualifier and the exception-specification. */
20042 if (member_p
|| cp_parser_parse_definitely (parser
))
20044 cp_cv_quals cv_quals
;
20045 cp_virt_specifiers virt_specifiers
;
20046 cp_ref_qualifier ref_qual
;
20047 tree exception_specification
;
20050 bool memfn
= (member_p
|| (pushed_scope
20051 && CLASS_TYPE_P (pushed_scope
)));
20053 is_declarator
= true;
20055 if (ctor_dtor_or_conv_p
)
20056 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20059 /* Parse the cv-qualifier-seq. */
20060 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20061 /* Parse the ref-qualifier. */
20062 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20063 /* Parse the tx-qualifier. */
20064 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20065 /* And the exception-specification. */
20066 exception_specification
20067 = cp_parser_exception_specification_opt (parser
);
20069 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20071 /* In here, we handle cases where attribute is used after
20072 the function declaration. For example:
20073 void func (int x) __attribute__((vector(..))); */
20074 tree gnu_attrs
= NULL_TREE
;
20076 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
20078 cp_parser_parse_tentatively (parser
);
20079 tree attr
= cp_parser_gnu_attributes_opt (parser
);
20080 if (cp_lexer_next_token_is_not (parser
->lexer
,
20082 && cp_lexer_next_token_is_not (parser
->lexer
,
20084 cp_parser_abort_tentative_parse (parser
);
20085 else if (!cp_parser_parse_definitely (parser
))
20090 tree requires_clause
= NULL_TREE
;
20091 late_return
= (cp_parser_late_return_type_opt
20092 (parser
, declarator
, requires_clause
,
20093 memfn
? cv_quals
: -1));
20095 /* Parse the virt-specifier-seq. */
20096 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20098 /* Create the function-declarator. */
20099 declarator
= make_call_declarator (declarator
,
20105 exception_specification
,
20108 declarator
->std_attributes
= attrs
;
20109 declarator
->attributes
= gnu_attrs
;
20110 /* Any subsequent parameter lists are to do with
20111 return type, so are not those of the declared
20113 parser
->default_arg_ok_p
= false;
20116 /* Remove the function parms from scope. */
20117 pop_bindings_and_leave_scope ();
20120 /* Repeat the main loop. */
20124 /* If this is the first, we can try a parenthesized
20128 bool saved_in_type_id_in_expr_p
;
20130 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20131 parser
->in_declarator_p
= saved_in_declarator_p
;
20133 open_paren
= token
;
20134 /* Consume the `('. */
20135 matching_parens parens
;
20136 parens
.consume_open (parser
);
20137 /* Parse the nested declarator. */
20138 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20139 parser
->in_type_id_in_expr_p
= true;
20141 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20142 /*parenthesized_p=*/NULL
,
20143 member_p
, friend_p
);
20144 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20146 /* Expect a `)'. */
20147 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20148 if (!parens
.require_close (parser
))
20149 declarator
= cp_error_declarator
;
20150 if (declarator
== cp_error_declarator
)
20153 goto handle_declarator
;
20155 /* Otherwise, we must be done. */
20159 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20160 && token
->type
== CPP_OPEN_SQUARE
20161 && !cp_next_tokens_can_be_attribute_p (parser
))
20163 /* Parse an array-declarator. */
20164 tree bounds
, attrs
;
20166 if (ctor_dtor_or_conv_p
)
20167 *ctor_dtor_or_conv_p
= 0;
20171 parser
->default_arg_ok_p
= false;
20172 parser
->in_declarator_p
= true;
20173 /* Consume the `['. */
20174 cp_lexer_consume_token (parser
->lexer
);
20175 /* Peek at the next token. */
20176 token
= cp_lexer_peek_token (parser
->lexer
);
20177 /* If the next token is `]', then there is no
20178 constant-expression. */
20179 if (token
->type
!= CPP_CLOSE_SQUARE
)
20181 bool non_constant_p
;
20183 = cp_parser_constant_expression (parser
,
20184 /*allow_non_constant=*/true,
20186 if (!non_constant_p
)
20188 else if (error_operand_p (bounds
))
20189 /* Already gave an error. */;
20190 else if (!parser
->in_function_body
20191 || current_binding_level
->kind
== sk_function_parms
)
20193 /* Normally, the array bound must be an integral constant
20194 expression. However, as an extension, we allow VLAs
20195 in function scopes as long as they aren't part of a
20196 parameter declaration. */
20197 cp_parser_error (parser
,
20198 "array bound is not an integer constant");
20199 bounds
= error_mark_node
;
20201 else if (processing_template_decl
20202 && !type_dependent_expression_p (bounds
))
20204 /* Remember this wasn't a constant-expression. */
20205 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20206 TREE_SIDE_EFFECTS (bounds
) = 1;
20210 bounds
= NULL_TREE
;
20211 /* Look for the closing `]'. */
20212 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20214 declarator
= cp_error_declarator
;
20218 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20219 declarator
= make_array_declarator (declarator
, bounds
);
20220 declarator
->std_attributes
= attrs
;
20222 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20225 tree qualifying_scope
;
20226 tree unqualified_name
;
20228 special_function_kind sfk
;
20230 bool pack_expansion_p
= false;
20231 cp_token
*declarator_id_start_token
;
20233 /* Parse a declarator-id */
20234 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20237 cp_parser_parse_tentatively (parser
);
20239 /* If we see an ellipsis, we should be looking at a
20241 if (token
->type
== CPP_ELLIPSIS
)
20243 /* Consume the `...' */
20244 cp_lexer_consume_token (parser
->lexer
);
20246 pack_expansion_p
= true;
20250 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20252 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20253 qualifying_scope
= parser
->scope
;
20258 if (!unqualified_name
&& pack_expansion_p
)
20260 /* Check whether an error occurred. */
20261 okay
= !cp_parser_error_occurred (parser
);
20263 /* We already consumed the ellipsis to mark a
20264 parameter pack, but we have no way to report it,
20265 so abort the tentative parse. We will be exiting
20266 immediately anyway. */
20267 cp_parser_abort_tentative_parse (parser
);
20270 okay
= cp_parser_parse_definitely (parser
);
20273 unqualified_name
= error_mark_node
;
20274 else if (unqualified_name
20275 && (qualifying_scope
20276 || (!identifier_p (unqualified_name
))))
20278 cp_parser_error (parser
, "expected unqualified-id");
20279 unqualified_name
= error_mark_node
;
20283 if (!unqualified_name
)
20285 if (unqualified_name
== error_mark_node
)
20287 declarator
= cp_error_declarator
;
20288 pack_expansion_p
= false;
20289 declarator
->parameter_pack_p
= false;
20293 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20295 if (qualifying_scope
&& at_namespace_scope_p ()
20296 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20298 /* In the declaration of a member of a template class
20299 outside of the class itself, the SCOPE will sometimes
20300 be a TYPENAME_TYPE. For example, given:
20302 template <typename T>
20303 int S<T>::R::i = 3;
20305 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20306 this context, we must resolve S<T>::R to an ordinary
20307 type, rather than a typename type.
20309 The reason we normally avoid resolving TYPENAME_TYPEs
20310 is that a specialization of `S' might render
20311 `S<T>::R' not a type. However, if `S' is
20312 specialized, then this `i' will not be used, so there
20313 is no harm in resolving the types here. */
20316 /* Resolve the TYPENAME_TYPE. */
20317 type
= resolve_typename_type (qualifying_scope
,
20318 /*only_current_p=*/false);
20319 /* If that failed, the declarator is invalid. */
20320 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20322 if (typedef_variant_p (type
))
20323 error_at (declarator_id_start_token
->location
,
20324 "cannot define member of dependent typedef "
20327 error_at (declarator_id_start_token
->location
,
20328 "%<%T::%E%> is not a type",
20329 TYPE_CONTEXT (qualifying_scope
),
20330 TYPE_IDENTIFIER (qualifying_scope
));
20332 qualifying_scope
= type
;
20337 if (unqualified_name
)
20341 if (qualifying_scope
20342 && CLASS_TYPE_P (qualifying_scope
))
20343 class_type
= qualifying_scope
;
20345 class_type
= current_class_type
;
20347 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20349 tree name_type
= TREE_TYPE (unqualified_name
);
20351 if (!class_type
|| !same_type_p (name_type
, class_type
))
20353 /* We do not attempt to print the declarator
20354 here because we do not have enough
20355 information about its original syntactic
20357 cp_parser_error (parser
, "invalid declarator");
20358 declarator
= cp_error_declarator
;
20361 else if (qualifying_scope
20362 && CLASSTYPE_USE_TEMPLATE (name_type
))
20364 error_at (declarator_id_start_token
->location
,
20365 "invalid use of constructor as a template");
20366 inform (declarator_id_start_token
->location
,
20367 "use %<%T::%D%> instead of %<%T::%D%> to "
20368 "name the constructor in a qualified name",
20370 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20371 class_type
, name_type
);
20372 declarator
= cp_error_declarator
;
20375 unqualified_name
= constructor_name (class_type
);
20380 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20381 sfk
= sfk_destructor
;
20382 else if (identifier_p (unqualified_name
)
20383 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20384 sfk
= sfk_conversion
;
20385 else if (/* There's no way to declare a constructor
20386 for an unnamed type, even if the type
20387 got a name for linkage purposes. */
20388 !TYPE_WAS_UNNAMED (class_type
)
20389 /* Handle correctly (c++/19200):
20403 friend void N::S();
20405 && (!friend_p
|| class_type
== qualifying_scope
)
20406 && constructor_name_p (unqualified_name
,
20408 sfk
= sfk_constructor
;
20409 else if (is_overloaded_fn (unqualified_name
)
20410 && DECL_CONSTRUCTOR_P (get_first_fn
20411 (unqualified_name
)))
20412 sfk
= sfk_constructor
;
20414 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20415 *ctor_dtor_or_conv_p
= -1;
20418 declarator
= make_id_declarator (qualifying_scope
,
20421 declarator
->std_attributes
= attrs
;
20422 declarator
->id_loc
= token
->location
;
20423 declarator
->parameter_pack_p
= pack_expansion_p
;
20425 if (pack_expansion_p
)
20426 maybe_warn_variadic_templates ();
20429 handle_declarator
:;
20430 scope
= get_scope_of_declarator (declarator
);
20433 /* Any names that appear after the declarator-id for a
20434 member are looked up in the containing scope. */
20435 if (at_function_scope_p ())
20437 /* But declarations with qualified-ids can't appear in a
20439 cp_parser_error (parser
, "qualified-id in declaration");
20440 declarator
= cp_error_declarator
;
20443 pushed_scope
= push_scope (scope
);
20445 parser
->in_declarator_p
= true;
20446 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20447 || (declarator
&& declarator
->kind
== cdk_id
))
20448 /* Default args are only allowed on function
20450 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20452 parser
->default_arg_ok_p
= false;
20461 /* For an abstract declarator, we might wind up with nothing at this
20462 point. That's an error; the declarator is not optional. */
20464 cp_parser_error (parser
, "expected declarator");
20465 else if (open_paren
)
20467 /* Record overly parenthesized declarator so we can give a
20468 diagnostic about confusing decl/expr disambiguation. */
20469 if (declarator
->kind
== cdk_array
)
20471 /* If the open and close parens are on different lines, this
20472 is probably a formatting thing, so ignore. */
20473 expanded_location open
= expand_location (open_paren
->location
);
20474 expanded_location close
= expand_location (close_paren
->location
);
20475 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20479 declarator
->parenthesized
= open_paren
->location
;
20482 /* If we entered a scope, we must exit it now. */
20484 pop_scope (pushed_scope
);
20486 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20487 parser
->in_declarator_p
= saved_in_declarator_p
;
20492 /* Parse a ptr-operator.
20495 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20496 * cv-qualifier-seq [opt]
20498 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20499 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20504 & cv-qualifier-seq [opt]
20506 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20507 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20508 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20509 filled in with the TYPE containing the member. *CV_QUALS is
20510 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20511 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20512 Note that the tree codes returned by this function have nothing
20513 to do with the types of trees that will be eventually be created
20514 to represent the pointer or reference type being parsed. They are
20515 just constants with suggestive names. */
20516 static enum tree_code
20517 cp_parser_ptr_operator (cp_parser
* parser
,
20519 cp_cv_quals
*cv_quals
,
20522 enum tree_code code
= ERROR_MARK
;
20524 tree attrs
= NULL_TREE
;
20526 /* Assume that it's not a pointer-to-member. */
20528 /* And that there are no cv-qualifiers. */
20529 *cv_quals
= TYPE_UNQUALIFIED
;
20531 /* Peek at the next token. */
20532 token
= cp_lexer_peek_token (parser
->lexer
);
20534 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20535 if (token
->type
== CPP_MULT
)
20536 code
= INDIRECT_REF
;
20537 else if (token
->type
== CPP_AND
)
20539 else if ((cxx_dialect
!= cxx98
) &&
20540 token
->type
== CPP_AND_AND
) /* C++0x only */
20541 code
= NON_LVALUE_EXPR
;
20543 if (code
!= ERROR_MARK
)
20545 /* Consume the `*', `&' or `&&'. */
20546 cp_lexer_consume_token (parser
->lexer
);
20548 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20549 `&', if we are allowing GNU extensions. (The only qualifier
20550 that can legally appear after `&' is `restrict', but that is
20551 enforced during semantic analysis. */
20552 if (code
== INDIRECT_REF
20553 || cp_parser_allow_gnu_extensions_p (parser
))
20554 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20556 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20557 if (attributes
!= NULL
)
20558 *attributes
= attrs
;
20562 /* Try the pointer-to-member case. */
20563 cp_parser_parse_tentatively (parser
);
20564 /* Look for the optional `::' operator. */
20565 cp_parser_global_scope_opt (parser
,
20566 /*current_scope_valid_p=*/false);
20567 /* Look for the nested-name specifier. */
20568 token
= cp_lexer_peek_token (parser
->lexer
);
20569 cp_parser_nested_name_specifier (parser
,
20570 /*typename_keyword_p=*/false,
20571 /*check_dependency_p=*/true,
20573 /*is_declaration=*/false);
20574 /* If we found it, and the next token is a `*', then we are
20575 indeed looking at a pointer-to-member operator. */
20576 if (!cp_parser_error_occurred (parser
)
20577 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20579 /* Indicate that the `*' operator was used. */
20580 code
= INDIRECT_REF
;
20582 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20583 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20584 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20585 error_at (token
->location
, "cannot form pointer to member of "
20586 "non-class %q#T", parser
->scope
);
20589 /* The type of which the member is a member is given by the
20591 *type
= parser
->scope
;
20592 /* The next name will not be qualified. */
20593 parser
->scope
= NULL_TREE
;
20594 parser
->qualifying_scope
= NULL_TREE
;
20595 parser
->object_scope
= NULL_TREE
;
20596 /* Look for optional c++11 attributes. */
20597 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20598 if (attributes
!= NULL
)
20599 *attributes
= attrs
;
20600 /* Look for the optional cv-qualifier-seq. */
20601 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20604 /* If that didn't work we don't have a ptr-operator. */
20605 if (!cp_parser_parse_definitely (parser
))
20606 cp_parser_error (parser
, "expected ptr-operator");
20612 /* Parse an (optional) cv-qualifier-seq.
20615 cv-qualifier cv-qualifier-seq [opt]
20626 Returns a bitmask representing the cv-qualifiers. */
20629 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20631 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20636 cp_cv_quals cv_qualifier
;
20638 /* Peek at the next token. */
20639 token
= cp_lexer_peek_token (parser
->lexer
);
20640 /* See if it's a cv-qualifier. */
20641 switch (token
->keyword
)
20644 cv_qualifier
= TYPE_QUAL_CONST
;
20648 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20652 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20656 cv_qualifier
= TYPE_UNQUALIFIED
;
20663 if (cv_quals
& cv_qualifier
)
20665 gcc_rich_location
richloc (token
->location
);
20666 richloc
.add_fixit_remove ();
20667 error_at (&richloc
, "duplicate cv-qualifier");
20668 cp_lexer_purge_token (parser
->lexer
);
20672 cp_lexer_consume_token (parser
->lexer
);
20673 cv_quals
|= cv_qualifier
;
20680 /* Parse an (optional) ref-qualifier
20686 Returns cp_ref_qualifier representing ref-qualifier. */
20688 static cp_ref_qualifier
20689 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20691 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20693 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20694 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20699 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20700 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20702 switch (token
->type
)
20705 curr_ref_qual
= REF_QUAL_LVALUE
;
20709 curr_ref_qual
= REF_QUAL_RVALUE
;
20713 curr_ref_qual
= REF_QUAL_NONE
;
20717 if (!curr_ref_qual
)
20721 error_at (token
->location
, "multiple ref-qualifiers");
20722 cp_lexer_purge_token (parser
->lexer
);
20726 ref_qual
= curr_ref_qual
;
20727 cp_lexer_consume_token (parser
->lexer
);
20734 /* Parse an optional tx-qualifier.
20738 transaction_safe_dynamic */
20741 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20744 if (token
->type
== CPP_NAME
)
20746 tree name
= token
->u
.value
;
20747 const char *p
= IDENTIFIER_POINTER (name
);
20748 const int len
= strlen ("transaction_safe");
20749 if (!strncmp (p
, "transaction_safe", len
))
20753 || !strcmp (p
, "_dynamic"))
20755 cp_lexer_consume_token (parser
->lexer
);
20758 error ("%qE requires %<-fgnu-tm%>", name
);
20769 /* Parse an (optional) virt-specifier-seq.
20771 virt-specifier-seq:
20772 virt-specifier virt-specifier-seq [opt]
20778 Returns a bitmask representing the virt-specifiers. */
20780 static cp_virt_specifiers
20781 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20783 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20788 cp_virt_specifiers virt_specifier
;
20790 /* Peek at the next token. */
20791 token
= cp_lexer_peek_token (parser
->lexer
);
20792 /* See if it's a virt-specifier-qualifier. */
20793 if (token
->type
!= CPP_NAME
)
20795 if (id_equal (token
->u
.value
, "override"))
20797 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20798 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20800 else if (id_equal (token
->u
.value
, "final"))
20802 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20803 virt_specifier
= VIRT_SPEC_FINAL
;
20805 else if (id_equal (token
->u
.value
, "__final"))
20807 virt_specifier
= VIRT_SPEC_FINAL
;
20812 if (virt_specifiers
& virt_specifier
)
20814 gcc_rich_location
richloc (token
->location
);
20815 richloc
.add_fixit_remove ();
20816 error_at (&richloc
, "duplicate virt-specifier");
20817 cp_lexer_purge_token (parser
->lexer
);
20821 cp_lexer_consume_token (parser
->lexer
);
20822 virt_specifiers
|= virt_specifier
;
20825 return virt_specifiers
;
20828 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20829 is in scope even though it isn't real. */
20832 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20836 if (current_class_ptr
)
20838 /* We don't clear this between NSDMIs. Is it already what we want? */
20839 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20840 if (DECL_P (current_class_ptr
)
20841 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20842 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20843 && cp_type_quals (type
) == quals
)
20847 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20848 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20849 current_class_ptr
= NULL_TREE
;
20851 = cp_build_fold_indirect_ref (this_parm
);
20852 current_class_ptr
= this_parm
;
20855 /* Return true iff our current scope is a non-static data member
20859 parsing_nsdmi (void)
20861 /* We recognize NSDMI context by the context-less 'this' pointer set up
20862 by the function above. */
20863 if (current_class_ptr
20864 && TREE_CODE (current_class_ptr
) == PARM_DECL
20865 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20870 /* Parse a late-specified return type, if any. This is not a separate
20871 non-terminal, but part of a function declarator, which looks like
20873 -> trailing-type-specifier-seq abstract-declarator(opt)
20875 Returns the type indicated by the type-id.
20877 In addition to this, parse any queued up #pragma omp declare simd
20878 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20879 #pragma acc routine clauses.
20881 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20885 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20886 tree
& requires_clause
, cp_cv_quals quals
)
20889 tree type
= NULL_TREE
;
20890 bool declare_simd_p
= (parser
->omp_declare_simd
20892 && declarator
->kind
== cdk_id
);
20894 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
20895 && declarator
&& declarator
->kind
== cdk_id
);
20897 bool oacc_routine_p
= (parser
->oacc_routine
20899 && declarator
->kind
== cdk_id
);
20901 /* Peek at the next token. */
20902 token
= cp_lexer_peek_token (parser
->lexer
);
20903 /* A late-specified return type is indicated by an initial '->'. */
20904 if (token
->type
!= CPP_DEREF
20905 && token
->keyword
!= RID_REQUIRES
20906 && !(token
->type
== CPP_NAME
20907 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20908 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
20911 tree save_ccp
= current_class_ptr
;
20912 tree save_ccr
= current_class_ref
;
20915 /* DR 1207: 'this' is in scope in the trailing return type. */
20916 inject_this_parameter (current_class_type
, quals
);
20919 if (token
->type
== CPP_DEREF
)
20921 /* Consume the ->. */
20922 cp_lexer_consume_token (parser
->lexer
);
20924 type
= cp_parser_trailing_type_id (parser
);
20927 /* Function declarations may be followed by a trailing
20928 requires-clause. */
20929 requires_clause
= cp_parser_requires_clause_opt (parser
);
20931 if (cilk_simd_fn_vector_p
)
20932 declarator
->attributes
20933 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
20934 declarator
->attributes
);
20935 if (declare_simd_p
)
20936 declarator
->attributes
20937 = cp_parser_late_parsing_omp_declare_simd (parser
,
20938 declarator
->attributes
);
20939 if (oacc_routine_p
)
20940 declarator
->attributes
20941 = cp_parser_late_parsing_oacc_routine (parser
,
20942 declarator
->attributes
);
20946 current_class_ptr
= save_ccp
;
20947 current_class_ref
= save_ccr
;
20953 /* Parse a declarator-id.
20957 :: [opt] nested-name-specifier [opt] type-name
20959 In the `id-expression' case, the value returned is as for
20960 cp_parser_id_expression if the id-expression was an unqualified-id.
20961 If the id-expression was a qualified-id, then a SCOPE_REF is
20962 returned. The first operand is the scope (either a NAMESPACE_DECL
20963 or TREE_TYPE), but the second is still just a representation of an
20967 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20970 /* The expression must be an id-expression. Assume that qualified
20971 names are the names of types so that:
20974 int S<T>::R::i = 3;
20976 will work; we must treat `S<T>::R' as the name of a type.
20977 Similarly, assume that qualified names are templates, where
20981 int S<T>::R<T>::i = 3;
20984 id
= cp_parser_id_expression (parser
,
20985 /*template_keyword_p=*/false,
20986 /*check_dependency_p=*/false,
20987 /*template_p=*/NULL
,
20988 /*declarator_p=*/true,
20990 if (id
&& BASELINK_P (id
))
20991 id
= BASELINK_FUNCTIONS (id
);
20995 /* Parse a type-id.
20998 type-specifier-seq abstract-declarator [opt]
21000 Returns the TYPE specified. */
21003 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21004 bool is_trailing_return
)
21006 cp_decl_specifier_seq type_specifier_seq
;
21007 cp_declarator
*abstract_declarator
;
21009 /* Parse the type-specifier-seq. */
21010 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21011 is_trailing_return
,
21012 &type_specifier_seq
);
21013 if (is_template_arg
&& type_specifier_seq
.type
21014 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21015 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21016 /* A bare template name as a template argument is a template template
21017 argument, not a placeholder, so fail parsing it as a type argument. */
21019 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21020 cp_parser_simulate_error (parser
);
21021 return error_mark_node
;
21023 if (type_specifier_seq
.type
== error_mark_node
)
21024 return error_mark_node
;
21026 /* There might or might not be an abstract declarator. */
21027 cp_parser_parse_tentatively (parser
);
21028 /* Look for the declarator. */
21029 abstract_declarator
21030 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21031 /*parenthesized_p=*/NULL
,
21032 /*member_p=*/false,
21033 /*friend_p=*/false);
21034 /* Check to see if there really was a declarator. */
21035 if (!cp_parser_parse_definitely (parser
))
21036 abstract_declarator
= NULL
;
21038 if (type_specifier_seq
.type
21039 /* The concepts TS allows 'auto' as a type-id. */
21040 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21041 /* None of the valid uses of 'auto' in C++14 involve the type-id
21042 nonterminal, but it is valid in a trailing-return-type. */
21043 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21044 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21046 /* A type-id with type 'auto' is only ok if the abstract declarator
21047 is a function declarator with a late-specified return type.
21049 A type-id with 'auto' is also valid in a trailing-return-type
21050 in a compound-requirement. */
21051 if (abstract_declarator
21052 && abstract_declarator
->kind
== cdk_function
21053 && abstract_declarator
->u
.function
.late_return_type
)
21055 else if (parser
->in_result_type_constraint_p
)
21059 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21060 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21062 error_at (loc
, "missing template arguments after %qT",
21064 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21068 error_at (loc
, "invalid use of %qT", auto_node
);
21069 return error_mark_node
;
21073 return groktypename (&type_specifier_seq
, abstract_declarator
,
21078 cp_parser_type_id (cp_parser
*parser
)
21080 return cp_parser_type_id_1 (parser
, false, false);
21084 cp_parser_template_type_arg (cp_parser
*parser
)
21087 const char *saved_message
= parser
->type_definition_forbidden_message
;
21088 parser
->type_definition_forbidden_message
21089 = G_("types may not be defined in template arguments");
21090 r
= cp_parser_type_id_1 (parser
, true, false);
21091 parser
->type_definition_forbidden_message
= saved_message
;
21092 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21094 error ("invalid use of %<auto%> in template argument");
21095 r
= error_mark_node
;
21101 cp_parser_trailing_type_id (cp_parser
*parser
)
21103 return cp_parser_type_id_1 (parser
, false, true);
21106 /* Parse a type-specifier-seq.
21108 type-specifier-seq:
21109 type-specifier type-specifier-seq [opt]
21113 type-specifier-seq:
21114 attributes type-specifier-seq [opt]
21116 If IS_DECLARATION is true, we are at the start of a "condition" or
21117 exception-declaration, so we might be followed by a declarator-id.
21119 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21120 i.e. we've just seen "->".
21122 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21125 cp_parser_type_specifier_seq (cp_parser
* parser
,
21126 bool is_declaration
,
21127 bool is_trailing_return
,
21128 cp_decl_specifier_seq
*type_specifier_seq
)
21130 bool seen_type_specifier
= false;
21131 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21132 cp_token
*start_token
= NULL
;
21134 /* Clear the TYPE_SPECIFIER_SEQ. */
21135 clear_decl_specs (type_specifier_seq
);
21137 /* In the context of a trailing return type, enum E { } is an
21138 elaborated-type-specifier followed by a function-body, not an
21140 if (is_trailing_return
)
21141 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21143 /* Parse the type-specifiers and attributes. */
21146 tree type_specifier
;
21147 bool is_cv_qualifier
;
21149 /* Check for attributes first. */
21150 if (cp_next_tokens_can_be_attribute_p (parser
))
21152 type_specifier_seq
->attributes
=
21153 chainon (type_specifier_seq
->attributes
,
21154 cp_parser_attributes_opt (parser
));
21158 /* record the token of the beginning of the type specifier seq,
21159 for error reporting purposes*/
21161 start_token
= cp_lexer_peek_token (parser
->lexer
);
21163 /* Look for the type-specifier. */
21164 type_specifier
= cp_parser_type_specifier (parser
,
21166 type_specifier_seq
,
21167 /*is_declaration=*/false,
21170 if (!type_specifier
)
21172 /* If the first type-specifier could not be found, this is not a
21173 type-specifier-seq at all. */
21174 if (!seen_type_specifier
)
21176 /* Set in_declarator_p to avoid skipping to the semicolon. */
21177 int in_decl
= parser
->in_declarator_p
;
21178 parser
->in_declarator_p
= true;
21180 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21181 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21182 cp_parser_error (parser
, "expected type-specifier");
21184 parser
->in_declarator_p
= in_decl
;
21186 type_specifier_seq
->type
= error_mark_node
;
21189 /* If subsequent type-specifiers could not be found, the
21190 type-specifier-seq is complete. */
21194 seen_type_specifier
= true;
21195 /* The standard says that a condition can be:
21197 type-specifier-seq declarator = assignment-expression
21204 we should treat the "S" as a declarator, not as a
21205 type-specifier. The standard doesn't say that explicitly for
21206 type-specifier-seq, but it does say that for
21207 decl-specifier-seq in an ordinary declaration. Perhaps it
21208 would be clearer just to allow a decl-specifier-seq here, and
21209 then add a semantic restriction that if any decl-specifiers
21210 that are not type-specifiers appear, the program is invalid. */
21211 if (is_declaration
&& !is_cv_qualifier
)
21212 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21216 /* Return whether the function currently being declared has an associated
21217 template parameter list. */
21220 function_being_declared_is_template_p (cp_parser
* parser
)
21222 if (!current_template_parms
|| processing_template_parmlist
)
21225 if (parser
->implicit_template_scope
)
21228 if (at_class_scope_p ()
21229 && TYPE_BEING_DEFINED (current_class_type
))
21230 return parser
->num_template_parameter_lists
!= 0;
21232 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21233 (current_class_type
));
21236 /* Parse a parameter-declaration-clause.
21238 parameter-declaration-clause:
21239 parameter-declaration-list [opt] ... [opt]
21240 parameter-declaration-list , ...
21242 Returns a representation for the parameter declarations. A return
21243 value of NULL indicates a parameter-declaration-clause consisting
21244 only of an ellipsis. */
21247 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21256 int auto_is_implicit_function_template_parm_p
;
21258 parser
->auto_is_implicit_function_template_parm_p
21259 = auto_is_implicit_function_template_parm_p
;
21261 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21265 if (!processing_specialization
21266 && !processing_template_parmlist
21267 && !processing_explicit_instantiation
)
21268 if (!current_function_decl
21269 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21270 parser
->auto_is_implicit_function_template_parm_p
= true;
21272 /* Peek at the next token. */
21273 token
= cp_lexer_peek_token (parser
->lexer
);
21274 /* Check for trivial parameter-declaration-clauses. */
21275 if (token
->type
== CPP_ELLIPSIS
)
21277 /* Consume the `...' token. */
21278 cp_lexer_consume_token (parser
->lexer
);
21281 else if (token
->type
== CPP_CLOSE_PAREN
)
21282 /* There are no parameters. */
21284 #ifndef NO_IMPLICIT_EXTERN_C
21285 if (in_system_header_at (input_location
)
21286 && current_class_type
== NULL
21287 && current_lang_name
== lang_name_c
)
21291 return void_list_node
;
21293 /* Check for `(void)', too, which is a special case. */
21294 else if (token
->keyword
== RID_VOID
21295 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21296 == CPP_CLOSE_PAREN
))
21298 /* Consume the `void' token. */
21299 cp_lexer_consume_token (parser
->lexer
);
21300 /* There are no parameters. */
21301 return void_list_node
;
21304 /* Parse the parameter-declaration-list. */
21305 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21306 /* If a parse error occurred while parsing the
21307 parameter-declaration-list, then the entire
21308 parameter-declaration-clause is erroneous. */
21312 /* Peek at the next token. */
21313 token
= cp_lexer_peek_token (parser
->lexer
);
21314 /* If it's a `,', the clause should terminate with an ellipsis. */
21315 if (token
->type
== CPP_COMMA
)
21317 /* Consume the `,'. */
21318 cp_lexer_consume_token (parser
->lexer
);
21319 /* Expect an ellipsis. */
21321 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21323 /* It might also be `...' if the optional trailing `,' was
21325 else if (token
->type
== CPP_ELLIPSIS
)
21327 /* Consume the `...' token. */
21328 cp_lexer_consume_token (parser
->lexer
);
21329 /* And remember that we saw it. */
21333 ellipsis_p
= false;
21335 /* Finish the parameter list. */
21337 parameters
= chainon (parameters
, void_list_node
);
21342 /* Parse a parameter-declaration-list.
21344 parameter-declaration-list:
21345 parameter-declaration
21346 parameter-declaration-list , parameter-declaration
21348 Returns a representation of the parameter-declaration-list, as for
21349 cp_parser_parameter_declaration_clause. However, the
21350 `void_list_node' is never appended to the list. Upon return,
21351 *IS_ERROR will be true iff an error occurred. */
21354 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21356 tree parameters
= NULL_TREE
;
21357 tree
*tail
= ¶meters
;
21358 bool saved_in_unbraced_linkage_specification_p
;
21361 /* Assume all will go well. */
21363 /* The special considerations that apply to a function within an
21364 unbraced linkage specifications do not apply to the parameters
21365 to the function. */
21366 saved_in_unbraced_linkage_specification_p
21367 = parser
->in_unbraced_linkage_specification_p
;
21368 parser
->in_unbraced_linkage_specification_p
= false;
21370 /* Look for more parameters. */
21373 cp_parameter_declarator
*parameter
;
21374 tree decl
= error_mark_node
;
21375 bool parenthesized_p
= false;
21376 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21377 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21378 (current_template_parms
)) : 0);
21380 /* Parse the parameter. */
21382 = cp_parser_parameter_declaration (parser
,
21383 /*template_parm_p=*/false,
21386 /* We don't know yet if the enclosing context is deprecated, so wait
21387 and warn in grokparms if appropriate. */
21388 deprecated_state
= DEPRECATED_SUPPRESS
;
21392 /* If a function parameter pack was specified and an implicit template
21393 parameter was introduced during cp_parser_parameter_declaration,
21394 change any implicit parameters introduced into packs. */
21395 if (parser
->implicit_template_parms
21396 && parameter
->declarator
21397 && parameter
->declarator
->parameter_pack_p
)
21399 int latest_template_parm_idx
= TREE_VEC_LENGTH
21400 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21402 if (latest_template_parm_idx
!= template_parm_idx
)
21403 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21404 (parameter
->decl_specifiers
.type
,
21405 template_parm_idx
, latest_template_parm_idx
);
21408 decl
= grokdeclarator (parameter
->declarator
,
21409 ¶meter
->decl_specifiers
,
21411 parameter
->default_argument
!= NULL_TREE
,
21412 ¶meter
->decl_specifiers
.attributes
);
21413 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21414 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21417 deprecated_state
= DEPRECATED_NORMAL
;
21419 /* If a parse error occurred parsing the parameter declaration,
21420 then the entire parameter-declaration-list is erroneous. */
21421 if (decl
== error_mark_node
)
21424 parameters
= error_mark_node
;
21428 if (parameter
->decl_specifiers
.attributes
)
21429 cplus_decl_attributes (&decl
,
21430 parameter
->decl_specifiers
.attributes
,
21432 if (DECL_NAME (decl
))
21433 decl
= pushdecl (decl
);
21435 if (decl
!= error_mark_node
)
21437 retrofit_lang_decl (decl
);
21438 DECL_PARM_INDEX (decl
) = ++index
;
21439 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21442 /* Add the new parameter to the list. */
21443 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21444 tail
= &TREE_CHAIN (*tail
);
21446 /* Peek at the next token. */
21447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21448 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21449 /* These are for Objective-C++ */
21450 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21451 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21452 /* The parameter-declaration-list is complete. */
21454 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21458 /* Peek at the next token. */
21459 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21460 /* If it's an ellipsis, then the list is complete. */
21461 if (token
->type
== CPP_ELLIPSIS
)
21463 /* Otherwise, there must be more parameters. Consume the
21465 cp_lexer_consume_token (parser
->lexer
);
21466 /* When parsing something like:
21468 int i(float f, double d)
21470 we can tell after seeing the declaration for "f" that we
21471 are not looking at an initialization of a variable "i",
21472 but rather at the declaration of a function "i".
21474 Due to the fact that the parsing of template arguments
21475 (as specified to a template-id) requires backtracking we
21476 cannot use this technique when inside a template argument
21478 if (!parser
->in_template_argument_list_p
21479 && !parser
->in_type_id_in_expr_p
21480 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21481 /* However, a parameter-declaration of the form
21482 "float(f)" (which is a valid declaration of a
21483 parameter "f") can also be interpreted as an
21484 expression (the conversion of "f" to "float"). */
21485 && !parenthesized_p
)
21486 cp_parser_commit_to_tentative_parse (parser
);
21490 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21491 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21492 cp_parser_skip_to_closing_parenthesis (parser
,
21493 /*recovering=*/true,
21494 /*or_comma=*/false,
21495 /*consume_paren=*/false);
21500 parser
->in_unbraced_linkage_specification_p
21501 = saved_in_unbraced_linkage_specification_p
;
21503 /* Reset implicit_template_scope if we are about to leave the function
21504 parameter list that introduced it. Note that for out-of-line member
21505 definitions, there will be one or more class scopes before we get to
21506 the template parameter scope. */
21508 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21509 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21511 while (maybe_its
->kind
== sk_class
)
21512 maybe_its
= maybe_its
->level_chain
;
21513 if (maybe_its
== its
)
21515 parser
->implicit_template_parms
= 0;
21516 parser
->implicit_template_scope
= 0;
21523 /* Parse a parameter declaration.
21525 parameter-declaration:
21526 decl-specifier-seq ... [opt] declarator
21527 decl-specifier-seq declarator = assignment-expression
21528 decl-specifier-seq ... [opt] abstract-declarator [opt]
21529 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21531 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21532 declares a template parameter. (In that case, a non-nested `>'
21533 token encountered during the parsing of the assignment-expression
21534 is not interpreted as a greater-than operator.)
21536 Returns a representation of the parameter, or NULL if an error
21537 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21538 true iff the declarator is of the form "(p)". */
21540 static cp_parameter_declarator
*
21541 cp_parser_parameter_declaration (cp_parser
*parser
,
21542 bool template_parm_p
,
21543 bool *parenthesized_p
)
21545 int declares_class_or_enum
;
21546 cp_decl_specifier_seq decl_specifiers
;
21547 cp_declarator
*declarator
;
21548 tree default_argument
;
21549 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21550 const char *saved_message
;
21551 bool template_parameter_pack_p
= false;
21553 /* In a template parameter, `>' is not an operator.
21557 When parsing a default template-argument for a non-type
21558 template-parameter, the first non-nested `>' is taken as the end
21559 of the template parameter-list rather than a greater-than
21562 /* Type definitions may not appear in parameter types. */
21563 saved_message
= parser
->type_definition_forbidden_message
;
21564 parser
->type_definition_forbidden_message
21565 = G_("types may not be defined in parameter types");
21567 /* Parse the declaration-specifiers. */
21568 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21569 cp_parser_decl_specifier_seq (parser
,
21570 CP_PARSER_FLAGS_NONE
,
21572 &declares_class_or_enum
);
21574 /* Complain about missing 'typename' or other invalid type names. */
21575 if (!decl_specifiers
.any_type_specifiers_p
21576 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21577 decl_specifiers
.type
= error_mark_node
;
21579 /* If an error occurred, there's no reason to attempt to parse the
21580 rest of the declaration. */
21581 if (cp_parser_error_occurred (parser
))
21583 parser
->type_definition_forbidden_message
= saved_message
;
21587 /* Peek at the next token. */
21588 token
= cp_lexer_peek_token (parser
->lexer
);
21590 /* If the next token is a `)', `,', `=', `>', or `...', then there
21591 is no declarator. However, when variadic templates are enabled,
21592 there may be a declarator following `...'. */
21593 if (token
->type
== CPP_CLOSE_PAREN
21594 || token
->type
== CPP_COMMA
21595 || token
->type
== CPP_EQ
21596 || token
->type
== CPP_GREATER
)
21599 if (parenthesized_p
)
21600 *parenthesized_p
= false;
21602 /* Otherwise, there should be a declarator. */
21605 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21606 parser
->default_arg_ok_p
= false;
21608 /* After seeing a decl-specifier-seq, if the next token is not a
21609 "(", there is no possibility that the code is a valid
21610 expression. Therefore, if parsing tentatively, we commit at
21612 if (!parser
->in_template_argument_list_p
21613 /* In an expression context, having seen:
21617 we cannot be sure whether we are looking at a
21618 function-type (taking a "char" as a parameter) or a cast
21619 of some object of type "char" to "int". */
21620 && !parser
->in_type_id_in_expr_p
21621 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21622 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21623 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21624 cp_parser_commit_to_tentative_parse (parser
);
21625 /* Parse the declarator. */
21626 declarator_token_start
= token
;
21627 declarator
= cp_parser_declarator (parser
,
21628 CP_PARSER_DECLARATOR_EITHER
,
21629 /*ctor_dtor_or_conv_p=*/NULL
,
21631 /*member_p=*/false,
21632 /*friend_p=*/false);
21633 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21634 /* After the declarator, allow more attributes. */
21635 decl_specifiers
.attributes
21636 = chainon (decl_specifiers
.attributes
,
21637 cp_parser_attributes_opt (parser
));
21639 /* If the declarator is a template parameter pack, remember that and
21640 clear the flag in the declarator itself so we don't get errors
21641 from grokdeclarator. */
21642 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21644 declarator
->parameter_pack_p
= false;
21645 template_parameter_pack_p
= true;
21649 /* If the next token is an ellipsis, and we have not seen a declarator
21650 name, and if either the type of the declarator contains parameter
21651 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21652 for, eg, abbreviated integral type names), then we actually have a
21653 parameter pack expansion expression. Otherwise, leave the ellipsis
21654 for a C-style variadic function. */
21655 token
= cp_lexer_peek_token (parser
->lexer
);
21656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21658 tree type
= decl_specifiers
.type
;
21660 if (type
&& DECL_P (type
))
21661 type
= TREE_TYPE (type
);
21664 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21665 && (template_parm_p
|| uses_parameter_packs (type
)))
21666 || (!type
&& template_parm_p
))
21667 && declarator_can_be_parameter_pack (declarator
))
21669 /* Consume the `...'. */
21670 cp_lexer_consume_token (parser
->lexer
);
21671 maybe_warn_variadic_templates ();
21673 /* Build a pack expansion type */
21674 if (template_parm_p
)
21675 template_parameter_pack_p
= true;
21676 else if (declarator
)
21677 declarator
->parameter_pack_p
= true;
21679 decl_specifiers
.type
= make_pack_expansion (type
);
21683 /* The restriction on defining new types applies only to the type
21684 of the parameter, not to the default argument. */
21685 parser
->type_definition_forbidden_message
= saved_message
;
21687 /* If the next token is `=', then process a default argument. */
21688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21690 tree type
= decl_specifiers
.type
;
21691 token
= cp_lexer_peek_token (parser
->lexer
);
21692 /* If we are defining a class, then the tokens that make up the
21693 default argument must be saved and processed later. */
21694 if (!template_parm_p
&& at_class_scope_p ()
21695 && TYPE_BEING_DEFINED (current_class_type
)
21696 && !LAMBDA_TYPE_P (current_class_type
))
21697 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21699 // A constrained-type-specifier may declare a type template-parameter.
21700 else if (declares_constrained_type_template_parameter (type
))
21702 = cp_parser_default_type_template_argument (parser
);
21704 // A constrained-type-specifier may declare a template-template-parameter.
21705 else if (declares_constrained_template_template_parameter (type
))
21707 = cp_parser_default_template_template_argument (parser
);
21709 /* Outside of a class definition, we can just parse the
21710 assignment-expression. */
21713 = cp_parser_default_argument (parser
, template_parm_p
);
21715 if (!parser
->default_arg_ok_p
)
21717 permerror (token
->location
,
21718 "default arguments are only "
21719 "permitted for function parameters");
21721 else if ((declarator
&& declarator
->parameter_pack_p
)
21722 || template_parameter_pack_p
21723 || (decl_specifiers
.type
21724 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21726 /* Find the name of the parameter pack. */
21727 cp_declarator
*id_declarator
= declarator
;
21728 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21729 id_declarator
= id_declarator
->declarator
;
21731 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21732 error_at (declarator_token_start
->location
,
21734 ? G_("template parameter pack %qD "
21735 "cannot have a default argument")
21736 : G_("parameter pack %qD cannot have "
21737 "a default argument"),
21738 id_declarator
->u
.id
.unqualified_name
);
21740 error_at (declarator_token_start
->location
,
21742 ? G_("template parameter pack cannot have "
21743 "a default argument")
21744 : G_("parameter pack cannot have a "
21745 "default argument"));
21747 default_argument
= NULL_TREE
;
21751 default_argument
= NULL_TREE
;
21753 /* Generate a location for the parameter, ranging from the start of the
21754 initial token to the end of the final token (using input_location for
21755 the latter, set up by cp_lexer_set_source_position_from_token when
21758 If we have a identifier, then use it for the caret location, e.g.
21760 extern int callee (int one, int (*two)(int, int), float three);
21761 ~~~~~~^~~~~~~~~~~~~~
21763 otherwise, reuse the start location for the caret location e.g.:
21765 extern int callee (int one, int (*)(int, int), float three);
21769 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21770 ? declarator
->id_loc
21771 : decl_spec_token_start
->location
);
21772 location_t param_loc
= make_location (caret_loc
,
21773 decl_spec_token_start
->location
,
21776 return make_parameter_declarator (&decl_specifiers
,
21780 template_parameter_pack_p
);
21783 /* Parse a default argument and return it.
21785 TEMPLATE_PARM_P is true if this is a default argument for a
21786 non-type template parameter. */
21788 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21790 tree default_argument
= NULL_TREE
;
21791 bool saved_greater_than_is_operator_p
;
21792 bool saved_local_variables_forbidden_p
;
21793 bool non_constant_p
, is_direct_init
;
21795 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21797 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21798 parser
->greater_than_is_operator_p
= !template_parm_p
;
21799 /* Local variable names (and the `this' keyword) may not
21800 appear in a default argument. */
21801 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21802 parser
->local_variables_forbidden_p
= true;
21803 /* Parse the assignment-expression. */
21804 if (template_parm_p
)
21805 push_deferring_access_checks (dk_no_deferred
);
21806 tree saved_class_ptr
= NULL_TREE
;
21807 tree saved_class_ref
= NULL_TREE
;
21808 /* The "this" pointer is not valid in a default argument. */
21811 saved_class_ptr
= current_class_ptr
;
21812 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21813 saved_class_ref
= current_class_ref
;
21814 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21817 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21818 /* Restore the "this" pointer. */
21821 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21822 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21824 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21825 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21826 if (template_parm_p
)
21827 pop_deferring_access_checks ();
21828 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21829 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21831 return default_argument
;
21834 /* Parse a function-body.
21837 compound_statement */
21840 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21842 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21843 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21847 /* Parse a ctor-initializer-opt followed by a function-body. Return
21848 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21849 is true we are parsing a function-try-block. */
21852 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21853 bool in_function_try_block
)
21856 const bool check_body_p
=
21857 DECL_CONSTRUCTOR_P (current_function_decl
)
21858 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21861 /* Begin the function body. */
21862 body
= begin_function_body ();
21863 /* Parse the optional ctor-initializer. */
21864 cp_parser_ctor_initializer_opt (parser
);
21866 /* If we're parsing a constexpr constructor definition, we need
21867 to check that the constructor body is indeed empty. However,
21868 before we get to cp_parser_function_body lot of junk has been
21869 generated, so we can't just check that we have an empty block.
21870 Rather we take a snapshot of the outermost block, and check whether
21871 cp_parser_function_body changed its state. */
21874 list
= cur_stmt_list
;
21875 if (STATEMENT_LIST_TAIL (list
))
21876 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21878 /* Parse the function-body. */
21879 cp_parser_function_body (parser
, in_function_try_block
);
21881 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21882 /* Finish the function body. */
21883 finish_function_body (body
);
21886 /* Parse an initializer.
21889 = initializer-clause
21890 ( expression-list )
21892 Returns an expression representing the initializer. If no
21893 initializer is present, NULL_TREE is returned.
21895 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21896 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21897 set to TRUE if there is no initializer present. If there is an
21898 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21899 is set to true; otherwise it is set to false. */
21902 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21903 bool* non_constant_p
)
21908 /* Peek at the next token. */
21909 token
= cp_lexer_peek_token (parser
->lexer
);
21911 /* Let our caller know whether or not this initializer was
21913 *is_direct_init
= (token
->type
!= CPP_EQ
);
21914 /* Assume that the initializer is constant. */
21915 *non_constant_p
= false;
21917 if (token
->type
== CPP_EQ
)
21919 /* Consume the `='. */
21920 cp_lexer_consume_token (parser
->lexer
);
21921 /* Parse the initializer-clause. */
21922 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21924 else if (token
->type
== CPP_OPEN_PAREN
)
21926 vec
<tree
, va_gc
> *vec
;
21927 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21929 /*allow_expansion_p=*/true,
21932 return error_mark_node
;
21933 init
= build_tree_list_vec (vec
);
21934 release_tree_vector (vec
);
21936 else if (token
->type
== CPP_OPEN_BRACE
)
21938 cp_lexer_set_source_position (parser
->lexer
);
21939 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21940 init
= cp_parser_braced_list (parser
, non_constant_p
);
21941 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21945 /* Anything else is an error. */
21946 cp_parser_error (parser
, "expected initializer");
21947 init
= error_mark_node
;
21950 if (check_for_bare_parameter_packs (init
))
21951 init
= error_mark_node
;
21956 /* Parse an initializer-clause.
21958 initializer-clause:
21959 assignment-expression
21962 Returns an expression representing the initializer.
21964 If the `assignment-expression' production is used the value
21965 returned is simply a representation for the expression.
21967 Otherwise, calls cp_parser_braced_list. */
21970 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21972 cp_expr initializer
;
21974 /* Assume the expression is constant. */
21975 *non_constant_p
= false;
21977 /* If it is not a `{', then we are looking at an
21978 assignment-expression. */
21979 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21982 = cp_parser_constant_expression (parser
,
21983 /*allow_non_constant_p=*/true,
21987 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21989 return initializer
;
21992 /* Parse a brace-enclosed initializer list.
21995 { initializer-list , [opt] }
21996 { designated-initializer-list , [opt] }
21999 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22000 the elements of the initializer-list (or NULL, if the last
22001 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22002 NULL_TREE. There is no way to detect whether or not the optional
22003 trailing `,' was provided. NON_CONSTANT_P is as for
22004 cp_parser_initializer. */
22007 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22010 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22012 /* Consume the `{' token. */
22013 matching_braces braces
;
22014 braces
.consume_open (parser
);
22015 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22016 initializer
= make_node (CONSTRUCTOR
);
22017 /* If it's not a `}', then there is a non-trivial initializer. */
22018 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22020 /* Parse the initializer list. */
22021 CONSTRUCTOR_ELTS (initializer
)
22022 = cp_parser_initializer_list (parser
, non_constant_p
);
22023 /* A trailing `,' token is allowed. */
22024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22025 cp_lexer_consume_token (parser
->lexer
);
22028 *non_constant_p
= false;
22029 /* Now, there should be a trailing `}'. */
22030 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22031 braces
.require_close (parser
);
22032 TREE_TYPE (initializer
) = init_list_type_node
;
22034 cp_expr
result (initializer
);
22035 /* Build a location of the form:
22038 with caret==start at the open brace, finish at the close brace. */
22039 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22040 result
.set_location (combined_loc
);
22044 /* Consume tokens up to, and including, the next non-nested closing `]'.
22045 Returns true iff we found a closing `]'. */
22048 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22050 unsigned square_depth
= 0;
22054 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22056 switch (token
->type
)
22059 case CPP_PRAGMA_EOL
:
22060 /* If we've run out of tokens, then there is no closing `]'. */
22063 case CPP_OPEN_SQUARE
:
22067 case CPP_CLOSE_SQUARE
:
22068 if (!square_depth
--)
22070 cp_lexer_consume_token (parser
->lexer
);
22079 /* Consume the token. */
22080 cp_lexer_consume_token (parser
->lexer
);
22084 /* Return true if we are looking at an array-designator, false otherwise. */
22087 cp_parser_array_designator_p (cp_parser
*parser
)
22089 /* Consume the `['. */
22090 cp_lexer_consume_token (parser
->lexer
);
22092 cp_lexer_save_tokens (parser
->lexer
);
22094 /* Skip tokens until the next token is a closing square bracket.
22095 If we find the closing `]', and the next token is a `=', then
22096 we are looking at an array designator. */
22097 bool array_designator_p
22098 = (cp_parser_skip_to_closing_square_bracket (parser
)
22099 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22101 /* Roll back the tokens we skipped. */
22102 cp_lexer_rollback_tokens (parser
->lexer
);
22104 return array_designator_p
;
22107 /* Parse an initializer-list.
22110 initializer-clause ... [opt]
22111 initializer-list , initializer-clause ... [opt]
22115 designated-initializer-list:
22116 designated-initializer-clause
22117 designated-initializer-list , designated-initializer-clause
22119 designated-initializer-clause:
22120 designator brace-or-equal-initializer
22128 designation initializer-clause ...[opt]
22129 initializer-list , designation initializer-clause ...[opt]
22134 [ constant-expression ] =
22136 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22137 for the initializer. If the INDEX of the elt is non-NULL, it is the
22138 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22139 as for cp_parser_initializer. */
22141 static vec
<constructor_elt
, va_gc
> *
22142 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22144 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22145 bool first_p
= true;
22146 tree first_designator
= NULL_TREE
;
22148 /* Assume all of the expressions are constant. */
22149 *non_constant_p
= false;
22151 /* Parse the rest of the list. */
22157 bool clause_non_constant_p
;
22158 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22160 /* Handle the C++2A syntax, '. id ='. */
22161 if ((cxx_dialect
>= cxx2a
22162 || cp_parser_allow_gnu_extensions_p (parser
))
22163 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22164 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22165 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22166 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22167 == CPP_OPEN_BRACE
)))
22169 if (cxx_dialect
< cxx2a
)
22170 pedwarn (loc
, OPT_Wpedantic
,
22171 "C++ designated initializers only available with "
22172 "-std=c++2a or -std=gnu++2a");
22173 /* Consume the `.'. */
22174 cp_lexer_consume_token (parser
->lexer
);
22175 /* Consume the identifier. */
22176 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22178 /* Consume the `='. */
22179 cp_lexer_consume_token (parser
->lexer
);
22181 /* Also, if the next token is an identifier and the following one is a
22182 colon, we are looking at the GNU designated-initializer
22184 else if (cp_parser_allow_gnu_extensions_p (parser
)
22185 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22186 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22189 /* Warn the user that they are using an extension. */
22190 pedwarn (loc
, OPT_Wpedantic
,
22191 "ISO C++ does not allow GNU designated initializers");
22192 /* Consume the identifier. */
22193 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22194 /* Consume the `:'. */
22195 cp_lexer_consume_token (parser
->lexer
);
22197 /* Also handle C99 array designators, '[ const ] ='. */
22198 else if (cp_parser_allow_gnu_extensions_p (parser
)
22199 && !c_dialect_objc ()
22200 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22202 /* In C++11, [ could start a lambda-introducer. */
22203 bool non_const
= false;
22205 cp_parser_parse_tentatively (parser
);
22207 if (!cp_parser_array_designator_p (parser
))
22209 cp_parser_simulate_error (parser
);
22210 designator
= NULL_TREE
;
22214 designator
= cp_parser_constant_expression (parser
, true,
22216 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22217 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22220 if (!cp_parser_parse_definitely (parser
))
22221 designator
= NULL_TREE
;
22222 else if (non_const
)
22223 require_potential_rvalue_constant_expression (designator
);
22225 /* Warn the user that they are using an extension. */
22226 pedwarn (loc
, OPT_Wpedantic
,
22227 "ISO C++ does not allow C99 designated initializers");
22230 designator
= NULL_TREE
;
22234 first_designator
= designator
;
22237 else if (cxx_dialect
>= cxx2a
22238 && first_designator
!= error_mark_node
22239 && (!first_designator
!= !designator
))
22241 error_at (loc
, "either all initializer clauses should be designated "
22242 "or none of them should be");
22243 first_designator
= error_mark_node
;
22245 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22246 first_designator
= designator
;
22248 /* Parse the initializer. */
22249 initializer
= cp_parser_initializer_clause (parser
,
22250 &clause_non_constant_p
);
22251 /* If any clause is non-constant, so is the entire initializer. */
22252 if (clause_non_constant_p
)
22253 *non_constant_p
= true;
22255 /* If we have an ellipsis, this is an initializer pack
22257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22259 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22261 /* Consume the `...'. */
22262 cp_lexer_consume_token (parser
->lexer
);
22264 if (designator
&& cxx_dialect
>= cxx2a
)
22266 "%<...%> not allowed in designated initializer list");
22268 /* Turn the initializer into an initializer expansion. */
22269 initializer
= make_pack_expansion (initializer
);
22272 /* Add it to the vector. */
22273 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22275 /* If the next token is not a comma, we have reached the end of
22277 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22280 /* Peek at the next token. */
22281 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22282 /* If the next token is a `}', then we're still done. An
22283 initializer-clause can have a trailing `,' after the
22284 initializer-list and before the closing `}'. */
22285 if (token
->type
== CPP_CLOSE_BRACE
)
22288 /* Consume the `,' token. */
22289 cp_lexer_consume_token (parser
->lexer
);
22292 /* The same identifier shall not appear in multiple designators
22293 of a designated-initializer-list. */
22294 if (first_designator
)
22297 tree designator
, val
;
22298 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22299 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22301 if (IDENTIFIER_MARKED (designator
))
22303 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22304 "%<.%s%> designator used multiple times in "
22305 "the same initializer list",
22306 IDENTIFIER_POINTER (designator
));
22307 (*v
)[i
].index
= NULL_TREE
;
22310 IDENTIFIER_MARKED (designator
) = 1;
22312 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22313 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22314 IDENTIFIER_MARKED (designator
) = 0;
22320 /* Classes [gram.class] */
22322 /* Parse a class-name.
22328 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22329 to indicate that names looked up in dependent types should be
22330 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22331 keyword has been used to indicate that the name that appears next
22332 is a template. TAG_TYPE indicates the explicit tag given before
22333 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22334 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22335 is the class being defined in a class-head. If ENUM_OK is TRUE,
22336 enum-names are also accepted.
22338 Returns the TYPE_DECL representing the class. */
22341 cp_parser_class_name (cp_parser
*parser
,
22342 bool typename_keyword_p
,
22343 bool template_keyword_p
,
22344 enum tag_types tag_type
,
22345 bool check_dependency_p
,
22347 bool is_declaration
,
22354 tree identifier
= NULL_TREE
;
22356 /* All class-names start with an identifier. */
22357 token
= cp_lexer_peek_token (parser
->lexer
);
22358 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22360 cp_parser_error (parser
, "expected class-name");
22361 return error_mark_node
;
22364 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22365 to a template-id, so we save it here. */
22366 scope
= parser
->scope
;
22367 if (scope
== error_mark_node
)
22368 return error_mark_node
;
22370 /* Any name names a type if we're following the `typename' keyword
22371 in a qualified name where the enclosing scope is type-dependent. */
22372 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22373 && dependent_type_p (scope
));
22374 /* Handle the common case (an identifier, but not a template-id)
22376 if (token
->type
== CPP_NAME
22377 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22379 cp_token
*identifier_token
;
22382 /* Look for the identifier. */
22383 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22384 ambiguous_p
= identifier_token
->error_reported
;
22385 identifier
= cp_parser_identifier (parser
);
22386 /* If the next token isn't an identifier, we are certainly not
22387 looking at a class-name. */
22388 if (identifier
== error_mark_node
)
22389 decl
= error_mark_node
;
22390 /* If we know this is a type-name, there's no need to look it
22392 else if (typename_p
)
22396 tree ambiguous_decls
;
22397 /* If we already know that this lookup is ambiguous, then
22398 we've already issued an error message; there's no reason
22402 cp_parser_simulate_error (parser
);
22403 return error_mark_node
;
22405 /* If the next token is a `::', then the name must be a type
22408 [basic.lookup.qual]
22410 During the lookup for a name preceding the :: scope
22411 resolution operator, object, function, and enumerator
22412 names are ignored. */
22413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22414 tag_type
= scope_type
;
22415 /* Look up the name. */
22416 decl
= cp_parser_lookup_name (parser
, identifier
,
22418 /*is_template=*/false,
22419 /*is_namespace=*/false,
22420 check_dependency_p
,
22422 identifier_token
->location
);
22423 if (ambiguous_decls
)
22425 if (cp_parser_parsing_tentatively (parser
))
22426 cp_parser_simulate_error (parser
);
22427 return error_mark_node
;
22433 /* Try a template-id. */
22434 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22435 check_dependency_p
,
22438 if (decl
== error_mark_node
)
22439 return error_mark_node
;
22442 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22444 /* If this is a typename, create a TYPENAME_TYPE. */
22445 if (typename_p
&& decl
!= error_mark_node
)
22447 decl
= make_typename_type (scope
, decl
, typename_type
,
22448 /*complain=*/tf_error
);
22449 if (decl
!= error_mark_node
)
22450 decl
= TYPE_NAME (decl
);
22453 decl
= strip_using_decl (decl
);
22455 /* Check to see that it is really the name of a class. */
22456 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22457 && identifier_p (TREE_OPERAND (decl
, 0))
22458 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22459 /* Situations like this:
22461 template <typename T> struct A {
22462 typename T::template X<int>::I i;
22465 are problematic. Is `T::template X<int>' a class-name? The
22466 standard does not seem to be definitive, but there is no other
22467 valid interpretation of the following `::'. Therefore, those
22468 names are considered class-names. */
22470 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22471 if (decl
!= error_mark_node
)
22472 decl
= TYPE_NAME (decl
);
22474 else if (TREE_CODE (decl
) != TYPE_DECL
22475 || TREE_TYPE (decl
) == error_mark_node
22476 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22477 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22478 /* In Objective-C 2.0, a classname followed by '.' starts a
22479 dot-syntax expression, and it's not a type-name. */
22480 || (c_dialect_objc ()
22481 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22482 && objc_is_class_name (decl
)))
22483 decl
= error_mark_node
;
22485 if (decl
== error_mark_node
)
22486 cp_parser_error (parser
, "expected class-name");
22487 else if (identifier
&& !parser
->scope
)
22488 maybe_note_name_used_in_class (identifier
, decl
);
22493 /* Parse a class-specifier.
22496 class-head { member-specification [opt] }
22498 Returns the TREE_TYPE representing the class. */
22501 cp_parser_class_specifier_1 (cp_parser
* parser
)
22504 tree attributes
= NULL_TREE
;
22505 bool nested_name_specifier_p
;
22506 unsigned saved_num_template_parameter_lists
;
22507 bool saved_in_function_body
;
22508 unsigned char in_statement
;
22509 bool in_switch_statement_p
;
22510 bool saved_in_unbraced_linkage_specification_p
;
22511 tree old_scope
= NULL_TREE
;
22512 tree scope
= NULL_TREE
;
22513 cp_token
*closing_brace
;
22515 push_deferring_access_checks (dk_no_deferred
);
22517 /* Parse the class-head. */
22518 type
= cp_parser_class_head (parser
,
22519 &nested_name_specifier_p
);
22520 /* If the class-head was a semantic disaster, skip the entire body
22524 cp_parser_skip_to_end_of_block_or_statement (parser
);
22525 pop_deferring_access_checks ();
22526 return error_mark_node
;
22529 /* Look for the `{'. */
22530 matching_braces braces
;
22531 if (!braces
.require_open (parser
))
22533 pop_deferring_access_checks ();
22534 return error_mark_node
;
22537 cp_ensure_no_omp_declare_simd (parser
);
22538 cp_ensure_no_oacc_routine (parser
);
22540 /* Issue an error message if type-definitions are forbidden here. */
22541 cp_parser_check_type_definition (parser
);
22542 /* Remember that we are defining one more class. */
22543 ++parser
->num_classes_being_defined
;
22544 /* Inside the class, surrounding template-parameter-lists do not
22546 saved_num_template_parameter_lists
22547 = parser
->num_template_parameter_lists
;
22548 parser
->num_template_parameter_lists
= 0;
22549 /* We are not in a function body. */
22550 saved_in_function_body
= parser
->in_function_body
;
22551 parser
->in_function_body
= false;
22552 /* Or in a loop. */
22553 in_statement
= parser
->in_statement
;
22554 parser
->in_statement
= 0;
22555 /* Or in a switch. */
22556 in_switch_statement_p
= parser
->in_switch_statement_p
;
22557 parser
->in_switch_statement_p
= false;
22558 /* We are not immediately inside an extern "lang" block. */
22559 saved_in_unbraced_linkage_specification_p
22560 = parser
->in_unbraced_linkage_specification_p
;
22561 parser
->in_unbraced_linkage_specification_p
= false;
22563 // Associate constraints with the type.
22565 type
= associate_classtype_constraints (type
);
22567 /* Start the class. */
22568 if (nested_name_specifier_p
)
22570 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22571 old_scope
= push_inner_scope (scope
);
22573 type
= begin_class_definition (type
);
22575 if (type
== error_mark_node
)
22576 /* If the type is erroneous, skip the entire body of the class. */
22577 cp_parser_skip_to_closing_brace (parser
);
22579 /* Parse the member-specification. */
22580 cp_parser_member_specification_opt (parser
);
22582 /* Look for the trailing `}'. */
22583 closing_brace
= braces
.require_close (parser
);
22584 /* Look for trailing attributes to apply to this class. */
22585 if (cp_parser_allow_gnu_extensions_p (parser
))
22586 attributes
= cp_parser_gnu_attributes_opt (parser
);
22587 if (type
!= error_mark_node
)
22588 type
= finish_struct (type
, attributes
);
22589 if (nested_name_specifier_p
)
22590 pop_inner_scope (old_scope
, scope
);
22592 /* We've finished a type definition. Check for the common syntax
22593 error of forgetting a semicolon after the definition. We need to
22594 be careful, as we can't just check for not-a-semicolon and be done
22595 with it; the user might have typed:
22597 class X { } c = ...;
22598 class X { } *p = ...;
22600 and so forth. Instead, enumerate all the possible tokens that
22601 might follow this production; if we don't see one of them, then
22602 complain and silently insert the semicolon. */
22604 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22605 bool want_semicolon
= true;
22607 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22608 /* Don't try to parse c++11 attributes here. As per the
22609 grammar, that should be a task for
22610 cp_parser_decl_specifier_seq. */
22611 want_semicolon
= false;
22613 switch (token
->type
)
22616 case CPP_SEMICOLON
:
22619 case CPP_OPEN_PAREN
:
22620 case CPP_CLOSE_PAREN
:
22622 want_semicolon
= false;
22625 /* While it's legal for type qualifiers and storage class
22626 specifiers to follow type definitions in the grammar, only
22627 compiler testsuites contain code like that. Assume that if
22628 we see such code, then what we're really seeing is a case
22632 const <type> var = ...;
22637 static <type> func (...) ...
22639 i.e. the qualifier or specifier applies to the next
22640 declaration. To do so, however, we need to look ahead one
22641 more token to see if *that* token is a type specifier.
22643 This code could be improved to handle:
22646 static const <type> var = ...; */
22648 if (keyword_is_decl_specifier (token
->keyword
))
22650 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22652 /* Handling user-defined types here would be nice, but very
22655 = (lookahead
->type
== CPP_KEYWORD
22656 && keyword_begins_type_specifier (lookahead
->keyword
));
22663 /* If we don't have a type, then something is very wrong and we
22664 shouldn't try to do anything clever. Likewise for not seeing the
22666 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22668 /* Locate the closing brace. */
22669 cp_token_position prev
22670 = cp_lexer_previous_token_position (parser
->lexer
);
22671 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22672 location_t loc
= prev_token
->location
;
22674 /* We want to suggest insertion of a ';' immediately *after* the
22675 closing brace, so, if we can, offset the location by 1 column. */
22676 location_t next_loc
= loc
;
22677 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22678 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22680 rich_location
richloc (line_table
, next_loc
);
22682 /* If we successfully offset the location, suggest the fix-it. */
22683 if (next_loc
!= loc
)
22684 richloc
.add_fixit_insert_before (next_loc
, ";");
22686 if (CLASSTYPE_DECLARED_CLASS (type
))
22687 error_at (&richloc
,
22688 "expected %<;%> after class definition");
22689 else if (TREE_CODE (type
) == RECORD_TYPE
)
22690 error_at (&richloc
,
22691 "expected %<;%> after struct definition");
22692 else if (TREE_CODE (type
) == UNION_TYPE
)
22693 error_at (&richloc
,
22694 "expected %<;%> after union definition");
22696 gcc_unreachable ();
22698 /* Unget one token and smash it to look as though we encountered
22699 a semicolon in the input stream. */
22700 cp_lexer_set_token_position (parser
->lexer
, prev
);
22701 token
= cp_lexer_peek_token (parser
->lexer
);
22702 token
->type
= CPP_SEMICOLON
;
22703 token
->keyword
= RID_MAX
;
22707 /* If this class is not itself within the scope of another class,
22708 then we need to parse the bodies of all of the queued function
22709 definitions. Note that the queued functions defined in a class
22710 are not always processed immediately following the
22711 class-specifier for that class. Consider:
22714 struct B { void f() { sizeof (A); } };
22717 If `f' were processed before the processing of `A' were
22718 completed, there would be no way to compute the size of `A'.
22719 Note that the nesting we are interested in here is lexical --
22720 not the semantic nesting given by TYPE_CONTEXT. In particular,
22723 struct A { struct B; };
22724 struct A::B { void f() { } };
22726 there is no need to delay the parsing of `A::B::f'. */
22727 if (--parser
->num_classes_being_defined
== 0)
22730 tree class_type
= NULL_TREE
;
22731 tree pushed_scope
= NULL_TREE
;
22733 cp_default_arg_entry
*e
;
22734 tree save_ccp
, save_ccr
;
22736 /* In a first pass, parse default arguments to the functions.
22737 Then, in a second pass, parse the bodies of the functions.
22738 This two-phased approach handles cases like:
22746 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22749 /* If there are default arguments that have not yet been processed,
22750 take care of them now. */
22751 if (class_type
!= e
->class_type
)
22754 pop_scope (pushed_scope
);
22755 class_type
= e
->class_type
;
22756 pushed_scope
= push_scope (class_type
);
22758 /* Make sure that any template parameters are in scope. */
22759 maybe_begin_member_template_processing (decl
);
22760 /* Parse the default argument expressions. */
22761 cp_parser_late_parsing_default_args (parser
, decl
);
22762 /* Remove any template parameters from the symbol table. */
22763 maybe_end_member_template_processing ();
22765 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22766 /* Now parse any NSDMIs. */
22767 save_ccp
= current_class_ptr
;
22768 save_ccr
= current_class_ref
;
22769 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22771 if (class_type
!= DECL_CONTEXT (decl
))
22774 pop_scope (pushed_scope
);
22775 class_type
= DECL_CONTEXT (decl
);
22776 pushed_scope
= push_scope (class_type
);
22778 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22779 cp_parser_late_parsing_nsdmi (parser
, decl
);
22781 vec_safe_truncate (unparsed_nsdmis
, 0);
22782 current_class_ptr
= save_ccp
;
22783 current_class_ref
= save_ccr
;
22785 pop_scope (pushed_scope
);
22787 /* Now do some post-NSDMI bookkeeping. */
22788 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22789 after_nsdmi_defaulted_late_checks (class_type
);
22790 vec_safe_truncate (unparsed_classes
, 0);
22791 after_nsdmi_defaulted_late_checks (type
);
22793 /* Now parse the body of the functions. */
22796 /* OpenMP UDRs need to be parsed before all other functions. */
22797 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22798 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22799 cp_parser_late_parsing_for_member (parser
, decl
);
22800 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22801 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22802 cp_parser_late_parsing_for_member (parser
, decl
);
22805 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22806 cp_parser_late_parsing_for_member (parser
, decl
);
22807 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22810 vec_safe_push (unparsed_classes
, type
);
22812 /* Put back any saved access checks. */
22813 pop_deferring_access_checks ();
22815 /* Restore saved state. */
22816 parser
->in_switch_statement_p
= in_switch_statement_p
;
22817 parser
->in_statement
= in_statement
;
22818 parser
->in_function_body
= saved_in_function_body
;
22819 parser
->num_template_parameter_lists
22820 = saved_num_template_parameter_lists
;
22821 parser
->in_unbraced_linkage_specification_p
22822 = saved_in_unbraced_linkage_specification_p
;
22828 cp_parser_class_specifier (cp_parser
* parser
)
22831 timevar_push (TV_PARSE_STRUCT
);
22832 ret
= cp_parser_class_specifier_1 (parser
);
22833 timevar_pop (TV_PARSE_STRUCT
);
22837 /* Parse a class-head.
22840 class-key identifier [opt] base-clause [opt]
22841 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22842 class-key nested-name-specifier [opt] template-id
22845 class-virt-specifier:
22849 class-key attributes identifier [opt] base-clause [opt]
22850 class-key attributes nested-name-specifier identifier base-clause [opt]
22851 class-key attributes nested-name-specifier [opt] template-id
22854 Upon return BASES is initialized to the list of base classes (or
22855 NULL, if there are none) in the same form returned by
22856 cp_parser_base_clause.
22858 Returns the TYPE of the indicated class. Sets
22859 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22860 involving a nested-name-specifier was used, and FALSE otherwise.
22862 Returns error_mark_node if this is not a class-head.
22864 Returns NULL_TREE if the class-head is syntactically valid, but
22865 semantically invalid in a way that means we should skip the entire
22866 body of the class. */
22869 cp_parser_class_head (cp_parser
* parser
,
22870 bool* nested_name_specifier_p
)
22872 tree nested_name_specifier
;
22873 enum tag_types class_key
;
22874 tree id
= NULL_TREE
;
22875 tree type
= NULL_TREE
;
22878 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22879 bool template_id_p
= false;
22880 bool qualified_p
= false;
22881 bool invalid_nested_name_p
= false;
22882 bool invalid_explicit_specialization_p
= false;
22883 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22884 tree pushed_scope
= NULL_TREE
;
22885 unsigned num_templates
;
22886 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22887 /* Assume no nested-name-specifier will be present. */
22888 *nested_name_specifier_p
= false;
22889 /* Assume no template parameter lists will be used in defining the
22892 parser
->colon_corrects_to_scope_p
= false;
22894 /* Look for the class-key. */
22895 class_key
= cp_parser_class_key (parser
);
22896 if (class_key
== none_type
)
22897 return error_mark_node
;
22899 location_t class_head_start_location
= input_location
;
22901 /* Parse the attributes. */
22902 attributes
= cp_parser_attributes_opt (parser
);
22904 /* If the next token is `::', that is invalid -- but sometimes
22905 people do try to write:
22909 Handle this gracefully by accepting the extra qualifier, and then
22910 issuing an error about it later if this really is a
22911 class-head. If it turns out just to be an elaborated type
22912 specifier, remain silent. */
22913 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22914 qualified_p
= true;
22916 push_deferring_access_checks (dk_no_check
);
22918 /* Determine the name of the class. Begin by looking for an
22919 optional nested-name-specifier. */
22920 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22921 nested_name_specifier
22922 = cp_parser_nested_name_specifier_opt (parser
,
22923 /*typename_keyword_p=*/false,
22924 /*check_dependency_p=*/false,
22926 /*is_declaration=*/false);
22927 /* If there was a nested-name-specifier, then there *must* be an
22930 cp_token
*bad_template_keyword
= NULL
;
22932 if (nested_name_specifier
)
22934 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22935 /* Although the grammar says `identifier', it really means
22936 `class-name' or `template-name'. You are only allowed to
22937 define a class that has already been declared with this
22940 The proposed resolution for Core Issue 180 says that wherever
22941 you see `class T::X' you should treat `X' as a type-name.
22943 It is OK to define an inaccessible class; for example:
22945 class A { class B; };
22948 We do not know if we will see a class-name, or a
22949 template-name. We look for a class-name first, in case the
22950 class-name is a template-id; if we looked for the
22951 template-name first we would stop after the template-name. */
22952 cp_parser_parse_tentatively (parser
);
22953 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22954 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22955 type
= cp_parser_class_name (parser
,
22956 /*typename_keyword_p=*/false,
22957 /*template_keyword_p=*/false,
22959 /*check_dependency_p=*/false,
22960 /*class_head_p=*/true,
22961 /*is_declaration=*/false);
22962 /* If that didn't work, ignore the nested-name-specifier. */
22963 if (!cp_parser_parse_definitely (parser
))
22965 invalid_nested_name_p
= true;
22966 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22967 id
= cp_parser_identifier (parser
);
22968 if (id
== error_mark_node
)
22971 /* If we could not find a corresponding TYPE, treat this
22972 declaration like an unqualified declaration. */
22973 if (type
== error_mark_node
)
22974 nested_name_specifier
= NULL_TREE
;
22975 /* Otherwise, count the number of templates used in TYPE and its
22976 containing scopes. */
22981 for (scope
= TREE_TYPE (type
);
22982 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22983 scope
= get_containing_scope (scope
))
22985 && CLASS_TYPE_P (scope
)
22986 && CLASSTYPE_TEMPLATE_INFO (scope
)
22987 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22988 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22989 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22993 /* Otherwise, the identifier is optional. */
22996 /* We don't know whether what comes next is a template-id,
22997 an identifier, or nothing at all. */
22998 cp_parser_parse_tentatively (parser
);
22999 /* Check for a template-id. */
23000 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23001 id
= cp_parser_template_id (parser
,
23002 /*template_keyword_p=*/false,
23003 /*check_dependency_p=*/true,
23005 /*is_declaration=*/true);
23006 /* If that didn't work, it could still be an identifier. */
23007 if (!cp_parser_parse_definitely (parser
))
23009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23011 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23012 id
= cp_parser_identifier (parser
);
23019 template_id_p
= true;
23024 pop_deferring_access_checks ();
23028 cp_parser_check_for_invalid_template_id (parser
, id
,
23030 type_start_token
->location
);
23032 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23034 /* If it's not a `:' or a `{' then we can't really be looking at a
23035 class-head, since a class-head only appears as part of a
23036 class-specifier. We have to detect this situation before calling
23037 xref_tag, since that has irreversible side-effects. */
23038 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23040 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23041 type
= error_mark_node
;
23045 /* At this point, we're going ahead with the class-specifier, even
23046 if some other problem occurs. */
23047 cp_parser_commit_to_tentative_parse (parser
);
23048 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23050 cp_parser_error (parser
,
23051 "cannot specify %<override%> for a class");
23052 type
= error_mark_node
;
23055 /* Issue the error about the overly-qualified name now. */
23058 cp_parser_error (parser
,
23059 "global qualification of class name is invalid");
23060 type
= error_mark_node
;
23063 else if (invalid_nested_name_p
)
23065 cp_parser_error (parser
,
23066 "qualified name does not name a class");
23067 type
= error_mark_node
;
23070 else if (nested_name_specifier
)
23074 if (bad_template_keyword
)
23075 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23076 keyword template shall not appear at the top level. */
23077 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23078 "keyword %<template%> not allowed in class-head-name");
23080 /* Reject typedef-names in class heads. */
23081 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23083 error_at (type_start_token
->location
,
23084 "invalid class name in declaration of %qD",
23090 /* Figure out in what scope the declaration is being placed. */
23091 scope
= current_scope ();
23092 /* If that scope does not contain the scope in which the
23093 class was originally declared, the program is invalid. */
23094 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23096 if (at_namespace_scope_p ())
23097 error_at (type_start_token
->location
,
23098 "declaration of %qD in namespace %qD which does not "
23100 type
, scope
, nested_name_specifier
);
23102 error_at (type_start_token
->location
,
23103 "declaration of %qD in %qD which does not enclose %qD",
23104 type
, scope
, nested_name_specifier
);
23110 A declarator-id shall not be qualified except for the
23111 definition of a ... nested class outside of its class
23112 ... [or] the definition or explicit instantiation of a
23113 class member of a namespace outside of its namespace. */
23114 if (scope
== nested_name_specifier
)
23116 permerror (nested_name_specifier_token_start
->location
,
23117 "extra qualification not allowed");
23118 nested_name_specifier
= NULL_TREE
;
23122 /* An explicit-specialization must be preceded by "template <>". If
23123 it is not, try to recover gracefully. */
23124 if (at_namespace_scope_p ()
23125 && parser
->num_template_parameter_lists
== 0
23126 && !processing_template_parmlist
23129 /* Build a location of this form:
23130 struct typename <ARGS>
23131 ^~~~~~~~~~~~~~~~~~~~~~
23132 with caret==start at the start token, and
23133 finishing at the end of the type. */
23134 location_t reported_loc
23135 = make_location (class_head_start_location
,
23136 class_head_start_location
,
23137 get_finish (type_start_token
->location
));
23138 rich_location
richloc (line_table
, reported_loc
);
23139 richloc
.add_fixit_insert_before (class_head_start_location
,
23141 error_at (&richloc
,
23142 "an explicit specialization must be preceded by"
23143 " %<template <>%>");
23144 invalid_explicit_specialization_p
= true;
23145 /* Take the same action that would have been taken by
23146 cp_parser_explicit_specialization. */
23147 ++parser
->num_template_parameter_lists
;
23148 begin_specialization ();
23150 /* There must be no "return" statements between this point and the
23151 end of this function; set "type "to the correct return value and
23152 use "goto done;" to return. */
23153 /* Make sure that the right number of template parameters were
23155 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23156 type_start_token
->location
,
23157 /*declarator=*/NULL
))
23159 /* If something went wrong, there is no point in even trying to
23160 process the class-definition. */
23165 /* Look up the type. */
23168 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23169 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23170 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23172 error_at (type_start_token
->location
,
23173 "function template %qD redeclared as a class template", id
);
23174 type
= error_mark_node
;
23178 type
= TREE_TYPE (id
);
23179 type
= maybe_process_partial_specialization (type
);
23181 /* Check the scope while we still know whether or not we had a
23182 nested-name-specifier. */
23183 if (type
!= error_mark_node
)
23184 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23186 if (nested_name_specifier
)
23187 pushed_scope
= push_scope (nested_name_specifier
);
23189 else if (nested_name_specifier
)
23195 template <typename T> struct S { struct T };
23196 template <typename T> struct S<T>::T { };
23198 we will get a TYPENAME_TYPE when processing the definition of
23199 `S::T'. We need to resolve it to the actual type before we
23200 try to define it. */
23201 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23203 class_type
= resolve_typename_type (TREE_TYPE (type
),
23204 /*only_current_p=*/false);
23205 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23206 type
= TYPE_NAME (class_type
);
23209 cp_parser_error (parser
, "could not resolve typename type");
23210 type
= error_mark_node
;
23214 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23215 == error_mark_node
)
23221 class_type
= current_class_type
;
23222 /* Enter the scope indicated by the nested-name-specifier. */
23223 pushed_scope
= push_scope (nested_name_specifier
);
23224 /* Get the canonical version of this type. */
23225 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23226 /* Call push_template_decl if it seems like we should be defining a
23227 template either from the template headers or the type we're
23228 defining, so that we diagnose both extra and missing headers. */
23229 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23230 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23231 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23233 type
= push_template_decl (type
);
23234 if (type
== error_mark_node
)
23241 type
= TREE_TYPE (type
);
23242 *nested_name_specifier_p
= true;
23244 else /* The name is not a nested name. */
23246 /* If the class was unnamed, create a dummy name. */
23248 id
= make_anon_name ();
23249 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23250 ? ts_within_enclosing_non_class
23252 type
= xref_tag (class_key
, id
, tag_scope
,
23253 parser
->num_template_parameter_lists
);
23256 /* Indicate whether this class was declared as a `class' or as a
23258 if (TREE_CODE (type
) == RECORD_TYPE
)
23259 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23260 cp_parser_check_class_key (class_key
, type
);
23262 /* If this type was already complete, and we see another definition,
23263 that's an error. */
23264 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23266 error_at (type_start_token
->location
, "redefinition of %q#T",
23268 inform (location_of (type
), "previous definition of %q#T",
23273 else if (type
== error_mark_node
)
23278 /* Apply attributes now, before any use of the class as a template
23279 argument in its base list. */
23280 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23281 fixup_attribute_variants (type
);
23284 /* We will have entered the scope containing the class; the names of
23285 base classes should be looked up in that context. For example:
23287 struct A { struct B {}; struct C; };
23288 struct A::C : B {};
23292 /* Get the list of base-classes, if there is one. */
23293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23295 /* PR59482: enter the class scope so that base-specifiers are looked
23299 bases
= cp_parser_base_clause (parser
);
23300 /* PR59482: get out of the previously pushed class scope so that the
23301 subsequent pops pop the right thing. */
23308 /* If we're really defining a class, process the base classes.
23309 If they're invalid, fail. */
23310 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23311 xref_basetypes (type
, bases
);
23314 /* Leave the scope given by the nested-name-specifier. We will
23315 enter the class scope itself while processing the members. */
23317 pop_scope (pushed_scope
);
23319 if (invalid_explicit_specialization_p
)
23321 end_specialization ();
23322 --parser
->num_template_parameter_lists
;
23326 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23327 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23328 CLASSTYPE_FINAL (type
) = 1;
23330 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23334 /* Parse a class-key.
23341 Returns the kind of class-key specified, or none_type to indicate
23344 static enum tag_types
23345 cp_parser_class_key (cp_parser
* parser
)
23348 enum tag_types tag_type
;
23350 /* Look for the class-key. */
23351 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23355 /* Check to see if the TOKEN is a class-key. */
23356 tag_type
= cp_parser_token_is_class_key (token
);
23358 cp_parser_error (parser
, "expected class-key");
23362 /* Parse a type-parameter-key.
23364 type-parameter-key:
23370 cp_parser_type_parameter_key (cp_parser
* parser
)
23372 /* Look for the type-parameter-key. */
23373 enum tag_types tag_type
= none_type
;
23374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23375 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23377 cp_lexer_consume_token (parser
->lexer
);
23378 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23379 /* typename is not allowed in a template template parameter
23380 by the standard until C++17. */
23381 pedwarn (token
->location
, OPT_Wpedantic
,
23382 "ISO C++ forbids typename key in template template parameter;"
23383 " use -std=c++17 or -std=gnu++17");
23386 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23391 /* Parse an (optional) member-specification.
23393 member-specification:
23394 member-declaration member-specification [opt]
23395 access-specifier : member-specification [opt] */
23398 cp_parser_member_specification_opt (cp_parser
* parser
)
23405 /* Peek at the next token. */
23406 token
= cp_lexer_peek_token (parser
->lexer
);
23407 /* If it's a `}', or EOF then we've seen all the members. */
23408 if (token
->type
== CPP_CLOSE_BRACE
23409 || token
->type
== CPP_EOF
23410 || token
->type
== CPP_PRAGMA_EOL
)
23413 /* See if this token is a keyword. */
23414 keyword
= token
->keyword
;
23418 case RID_PROTECTED
:
23420 /* Consume the access-specifier. */
23421 cp_lexer_consume_token (parser
->lexer
);
23422 /* Remember which access-specifier is active. */
23423 current_access_specifier
= token
->u
.value
;
23424 /* Look for the `:'. */
23425 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23429 /* Accept #pragmas at class scope. */
23430 if (token
->type
== CPP_PRAGMA
)
23432 cp_parser_pragma (parser
, pragma_member
, NULL
);
23436 /* Otherwise, the next construction must be a
23437 member-declaration. */
23438 cp_parser_member_declaration (parser
);
23443 /* Parse a member-declaration.
23445 member-declaration:
23446 decl-specifier-seq [opt] member-declarator-list [opt] ;
23447 function-definition ; [opt]
23448 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23450 template-declaration
23453 member-declarator-list:
23455 member-declarator-list , member-declarator
23458 declarator pure-specifier [opt]
23459 declarator constant-initializer [opt]
23460 identifier [opt] : constant-expression
23464 member-declaration:
23465 __extension__ member-declaration
23468 declarator attributes [opt] pure-specifier [opt]
23469 declarator attributes [opt] constant-initializer [opt]
23470 identifier [opt] attributes [opt] : constant-expression
23474 member-declaration:
23475 static_assert-declaration */
23478 cp_parser_member_declaration (cp_parser
* parser
)
23480 cp_decl_specifier_seq decl_specifiers
;
23481 tree prefix_attributes
;
23483 int declares_class_or_enum
;
23485 cp_token
*token
= NULL
;
23486 cp_token
*decl_spec_token_start
= NULL
;
23487 cp_token
*initializer_token_start
= NULL
;
23488 int saved_pedantic
;
23489 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23491 /* Check for the `__extension__' keyword. */
23492 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23495 cp_parser_member_declaration (parser
);
23496 /* Restore the old value of the PEDANTIC flag. */
23497 pedantic
= saved_pedantic
;
23502 /* Check for a template-declaration. */
23503 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23505 /* An explicit specialization here is an error condition, and we
23506 expect the specialization handler to detect and report this. */
23507 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23508 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23509 cp_parser_explicit_specialization (parser
);
23511 cp_parser_template_declaration (parser
, /*member_p=*/true);
23515 /* Check for a template introduction. */
23516 else if (cp_parser_template_declaration_after_export (parser
, true))
23519 /* Check for a using-declaration. */
23520 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23522 if (cxx_dialect
< cxx11
)
23524 /* Parse the using-declaration. */
23525 cp_parser_using_declaration (parser
,
23526 /*access_declaration_p=*/false);
23532 bool alias_decl_expected
;
23533 cp_parser_parse_tentatively (parser
);
23534 decl
= cp_parser_alias_declaration (parser
);
23535 /* Note that if we actually see the '=' token after the
23536 identifier, cp_parser_alias_declaration commits the
23537 tentative parse. In that case, we really expect an
23538 alias-declaration. Otherwise, we expect a using
23540 alias_decl_expected
=
23541 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23542 cp_parser_parse_definitely (parser
);
23544 if (alias_decl_expected
)
23545 finish_member_declaration (decl
);
23547 cp_parser_using_declaration (parser
,
23548 /*access_declaration_p=*/false);
23553 /* Check for @defs. */
23554 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23557 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23558 ivar
= ivar_chains
;
23562 ivar
= TREE_CHAIN (member
);
23563 TREE_CHAIN (member
) = NULL_TREE
;
23564 finish_member_declaration (member
);
23569 /* If the next token is `static_assert' we have a static assertion. */
23570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23572 cp_parser_static_assert (parser
, /*member_p=*/true);
23576 parser
->colon_corrects_to_scope_p
= false;
23578 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23581 /* Parse the decl-specifier-seq. */
23582 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23583 cp_parser_decl_specifier_seq (parser
,
23584 CP_PARSER_FLAGS_OPTIONAL
,
23586 &declares_class_or_enum
);
23587 /* Check for an invalid type-name. */
23588 if (!decl_specifiers
.any_type_specifiers_p
23589 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23591 /* If there is no declarator, then the decl-specifier-seq should
23593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23595 /* If there was no decl-specifier-seq, and the next token is a
23596 `;', then we have something like:
23602 Each member-declaration shall declare at least one member
23603 name of the class. */
23604 if (!decl_specifiers
.any_specifiers_p
)
23606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23607 if (!in_system_header_at (token
->location
))
23609 gcc_rich_location
richloc (token
->location
);
23610 richloc
.add_fixit_remove ();
23611 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23618 /* See if this declaration is a friend. */
23619 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23620 /* If there were decl-specifiers, check to see if there was
23621 a class-declaration. */
23622 type
= check_tag_decl (&decl_specifiers
,
23623 /*explicit_type_instantiation_p=*/false);
23624 /* Nested classes have already been added to the class, but
23625 a `friend' needs to be explicitly registered. */
23628 /* If the `friend' keyword was present, the friend must
23629 be introduced with a class-key. */
23630 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23631 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23632 "in C++03 a class-key must be used "
23633 "when declaring a friend");
23636 template <typename T> struct A {
23637 friend struct A<T>::B;
23640 A<T>::B will be represented by a TYPENAME_TYPE, and
23641 therefore not recognized by check_tag_decl. */
23644 type
= decl_specifiers
.type
;
23645 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23646 type
= TREE_TYPE (type
);
23648 if (!type
|| !TYPE_P (type
))
23649 error_at (decl_spec_token_start
->location
,
23650 "friend declaration does not name a class or "
23653 make_friend_class (current_class_type
, type
,
23654 /*complain=*/true);
23656 /* If there is no TYPE, an error message will already have
23658 else if (!type
|| type
== error_mark_node
)
23660 /* An anonymous aggregate has to be handled specially; such
23661 a declaration really declares a data member (with a
23662 particular type), as opposed to a nested class. */
23663 else if (ANON_AGGR_TYPE_P (type
))
23666 if (decl_specifiers
.storage_class
!= sc_none
)
23667 error_at (decl_spec_token_start
->location
,
23668 "a storage class on an anonymous aggregate "
23669 "in class scope is not allowed");
23671 /* Remove constructors and such from TYPE, now that we
23672 know it is an anonymous aggregate. */
23673 fixup_anonymous_aggr (type
);
23674 /* And make the corresponding data member. */
23675 decl
= build_decl (decl_spec_token_start
->location
,
23676 FIELD_DECL
, NULL_TREE
, type
);
23677 /* Add it to the class. */
23678 finish_member_declaration (decl
);
23681 cp_parser_check_access_in_redeclaration
23683 decl_spec_token_start
->location
);
23688 bool assume_semicolon
= false;
23690 /* Clear attributes from the decl_specifiers but keep them
23691 around as prefix attributes that apply them to the entity
23693 prefix_attributes
= decl_specifiers
.attributes
;
23694 decl_specifiers
.attributes
= NULL_TREE
;
23696 /* See if these declarations will be friends. */
23697 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23699 /* Keep going until we hit the `;' at the end of the
23701 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23703 tree attributes
= NULL_TREE
;
23704 tree first_attribute
;
23706 bool is_bitfld
= false;
23707 bool named_bitfld
= false;
23709 /* Peek at the next token. */
23710 token
= cp_lexer_peek_token (parser
->lexer
);
23712 /* The following code wants to know early if it is a bit-field
23713 or some other declaration. Attributes can appear before
23714 the `:' token, but are hopefully rare enough that the
23715 simplicity of the tentative lookup pays off. */
23716 if (cp_next_tokens_can_be_attribute_p (parser
)
23717 || (token
->type
== CPP_NAME
23718 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23719 && (named_bitfld
= true)))
23721 cp_parser_parse_tentatively (parser
);
23723 cp_lexer_consume_token (parser
->lexer
);
23724 cp_parser_attributes_opt (parser
);
23725 token
= cp_lexer_peek_token (parser
->lexer
);
23726 is_bitfld
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
23727 cp_parser_abort_tentative_parse (parser
);
23730 /* Check for a bitfield declaration. */
23732 || token
->type
== CPP_COLON
23733 || (token
->type
== CPP_NAME
23734 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23735 && (named_bitfld
= true)))
23739 tree late_attributes
= NULL_TREE
;
23742 identifier
= cp_parser_identifier (parser
);
23744 identifier
= NULL_TREE
;
23746 /* Look for attributes that apply to the bitfield. */
23747 attributes
= cp_parser_attributes_opt (parser
);
23749 /* Consume the `:' token. */
23750 cp_lexer_consume_token (parser
->lexer
);
23752 /* Get the width of the bitfield. */
23753 width
= cp_parser_constant_expression (parser
, false, NULL
,
23754 cxx_dialect
>= cxx11
);
23756 /* In C++2A and as extension for C++11 and above we allow
23757 default member initializers for bit-fields. */
23758 initializer
= NULL_TREE
;
23759 if (cxx_dialect
>= cxx11
23760 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23761 || cp_lexer_next_token_is (parser
->lexer
,
23765 = cp_lexer_peek_token (parser
->lexer
)->location
;
23766 if (cxx_dialect
< cxx2a
23767 && !in_system_header_at (loc
)
23768 && identifier
!= NULL_TREE
)
23770 "default member initializers for bit-fields "
23771 "only available with -std=c++2a or "
23774 initializer
= cp_parser_save_nsdmi (parser
);
23775 if (identifier
== NULL_TREE
)
23777 error_at (loc
, "default member initializer for "
23778 "unnamed bit-field");
23779 initializer
= NULL_TREE
;
23784 /* Look for attributes that apply to the bitfield after
23785 the `:' token and width. This is where GCC used to
23786 parse attributes in the past, pedwarn if there is
23787 a std attribute. */
23788 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23789 pedwarn (input_location
, OPT_Wpedantic
,
23790 "ISO C++ allows bit-field attributes only "
23791 "before the %<:%> token");
23793 late_attributes
= cp_parser_attributes_opt (parser
);
23796 attributes
= chainon (attributes
, late_attributes
);
23798 /* Remember which attributes are prefix attributes and
23800 first_attribute
= attributes
;
23801 /* Combine the attributes. */
23802 attributes
= chainon (prefix_attributes
, attributes
);
23804 /* Create the bitfield declaration. */
23805 decl
= grokbitfield (identifier
23806 ? make_id_declarator (NULL_TREE
,
23811 width
, initializer
,
23816 cp_declarator
*declarator
;
23817 tree asm_specification
;
23818 int ctor_dtor_or_conv_p
;
23820 /* Parse the declarator. */
23822 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23823 &ctor_dtor_or_conv_p
,
23824 /*parenthesized_p=*/NULL
,
23828 /* If something went wrong parsing the declarator, make sure
23829 that we at least consume some tokens. */
23830 if (declarator
== cp_error_declarator
)
23832 /* Skip to the end of the statement. */
23833 cp_parser_skip_to_end_of_statement (parser
);
23834 /* If the next token is not a semicolon, that is
23835 probably because we just skipped over the body of
23836 a function. So, we consume a semicolon if
23837 present, but do not issue an error message if it
23839 if (cp_lexer_next_token_is (parser
->lexer
,
23841 cp_lexer_consume_token (parser
->lexer
);
23845 if (declares_class_or_enum
& 2)
23846 cp_parser_check_for_definition_in_return_type
23847 (declarator
, decl_specifiers
.type
,
23848 decl_specifiers
.locations
[ds_type_spec
]);
23850 /* Look for an asm-specification. */
23851 asm_specification
= cp_parser_asm_specification_opt (parser
);
23852 /* Look for attributes that apply to the declaration. */
23853 attributes
= cp_parser_attributes_opt (parser
);
23854 /* Remember which attributes are prefix attributes and
23856 first_attribute
= attributes
;
23857 /* Combine the attributes. */
23858 attributes
= chainon (prefix_attributes
, attributes
);
23860 /* If it's an `=', then we have a constant-initializer or a
23861 pure-specifier. It is not correct to parse the
23862 initializer before registering the member declaration
23863 since the member declaration should be in scope while
23864 its initializer is processed. However, the rest of the
23865 front end does not yet provide an interface that allows
23866 us to handle this correctly. */
23867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23871 A pure-specifier shall be used only in the declaration of
23872 a virtual function.
23874 A member-declarator can contain a constant-initializer
23875 only if it declares a static member of integral or
23878 Therefore, if the DECLARATOR is for a function, we look
23879 for a pure-specifier; otherwise, we look for a
23880 constant-initializer. When we call `grokfield', it will
23881 perform more stringent semantics checks. */
23882 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23883 if (function_declarator_p (declarator
)
23884 || (decl_specifiers
.type
23885 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23886 && declarator
->kind
== cdk_id
23887 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23888 == FUNCTION_TYPE
)))
23889 initializer
= cp_parser_pure_specifier (parser
);
23890 else if (decl_specifiers
.storage_class
!= sc_static
)
23891 initializer
= cp_parser_save_nsdmi (parser
);
23892 else if (cxx_dialect
>= cxx11
)
23895 /* Don't require a constant rvalue in C++11, since we
23896 might want a reference constant. We'll enforce
23897 constancy later. */
23898 cp_lexer_consume_token (parser
->lexer
);
23899 /* Parse the initializer. */
23900 initializer
= cp_parser_initializer_clause (parser
,
23904 /* Parse the initializer. */
23905 initializer
= cp_parser_constant_initializer (parser
);
23907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23908 && !function_declarator_p (declarator
))
23911 if (decl_specifiers
.storage_class
!= sc_static
)
23912 initializer
= cp_parser_save_nsdmi (parser
);
23914 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23916 /* Otherwise, there is no initializer. */
23918 initializer
= NULL_TREE
;
23920 /* See if we are probably looking at a function
23921 definition. We are certainly not looking at a
23922 member-declarator. Calling `grokfield' has
23923 side-effects, so we must not do it unless we are sure
23924 that we are looking at a member-declarator. */
23925 if (cp_parser_token_starts_function_definition_p
23926 (cp_lexer_peek_token (parser
->lexer
)))
23928 /* The grammar does not allow a pure-specifier to be
23929 used when a member function is defined. (It is
23930 possible that this fact is an oversight in the
23931 standard, since a pure function may be defined
23932 outside of the class-specifier. */
23933 if (initializer
&& initializer_token_start
)
23934 error_at (initializer_token_start
->location
,
23935 "pure-specifier on function-definition");
23936 decl
= cp_parser_save_member_function_body (parser
,
23940 if (parser
->fully_implicit_function_template_p
)
23941 decl
= finish_fully_implicit_template (parser
, decl
);
23942 /* If the member was not a friend, declare it here. */
23944 finish_member_declaration (decl
);
23945 /* Peek at the next token. */
23946 token
= cp_lexer_peek_token (parser
->lexer
);
23947 /* If the next token is a semicolon, consume it. */
23948 if (token
->type
== CPP_SEMICOLON
)
23950 location_t semicolon_loc
23951 = cp_lexer_consume_token (parser
->lexer
)->location
;
23952 gcc_rich_location
richloc (semicolon_loc
);
23953 richloc
.add_fixit_remove ();
23954 warning_at (&richloc
, OPT_Wextra_semi
,
23955 "extra %<;%> after in-class "
23956 "function definition");
23961 if (declarator
->kind
== cdk_function
)
23962 declarator
->id_loc
= token
->location
;
23963 /* Create the declaration. */
23964 decl
= grokfield (declarator
, &decl_specifiers
,
23965 initializer
, /*init_const_expr_p=*/true,
23966 asm_specification
, attributes
);
23967 if (parser
->fully_implicit_function_template_p
)
23970 finish_fully_implicit_template (parser
, 0);
23972 decl
= finish_fully_implicit_template (parser
, decl
);
23976 cp_finalize_omp_declare_simd (parser
, decl
);
23977 cp_finalize_oacc_routine (parser
, decl
, false);
23979 /* Reset PREFIX_ATTRIBUTES. */
23980 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23981 attributes
= TREE_CHAIN (attributes
);
23983 TREE_CHAIN (attributes
) = NULL_TREE
;
23985 /* If there is any qualification still in effect, clear it
23986 now; we will be starting fresh with the next declarator. */
23987 parser
->scope
= NULL_TREE
;
23988 parser
->qualifying_scope
= NULL_TREE
;
23989 parser
->object_scope
= NULL_TREE
;
23990 /* If it's a `,', then there are more declarators. */
23991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23993 cp_lexer_consume_token (parser
->lexer
);
23994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23996 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23997 gcc_rich_location
richloc (token
->location
);
23998 richloc
.add_fixit_remove ();
23999 error_at (&richloc
, "stray %<,%> at end of "
24000 "member declaration");
24003 /* If the next token isn't a `;', then we have a parse error. */
24004 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24007 /* The next token might be a ways away from where the
24008 actual semicolon is missing. Find the previous token
24009 and use that for our error position. */
24010 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24011 gcc_rich_location
richloc (token
->location
);
24012 richloc
.add_fixit_insert_after (";");
24013 error_at (&richloc
, "expected %<;%> at end of "
24014 "member declaration");
24016 /* Assume that the user meant to provide a semicolon. If
24017 we were to cp_parser_skip_to_end_of_statement, we might
24018 skip to a semicolon inside a member function definition
24019 and issue nonsensical error messages. */
24020 assume_semicolon
= true;
24025 /* Add DECL to the list of members. */
24027 /* Explicitly include, eg, NSDMIs, for better error
24028 recovery (c++/58650). */
24029 || !DECL_DECLARES_FUNCTION_P (decl
))
24030 finish_member_declaration (decl
);
24032 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24033 cp_parser_save_default_args (parser
, decl
);
24034 else if (TREE_CODE (decl
) == FIELD_DECL
24035 && DECL_INITIAL (decl
))
24036 /* Add DECL to the queue of NSDMI to be parsed later. */
24037 vec_safe_push (unparsed_nsdmis
, decl
);
24040 if (assume_semicolon
)
24045 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24047 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24050 /* Parse a pure-specifier.
24055 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24056 Otherwise, ERROR_MARK_NODE is returned. */
24059 cp_parser_pure_specifier (cp_parser
* parser
)
24063 /* Look for the `=' token. */
24064 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24065 return error_mark_node
;
24066 /* Look for the `0' token. */
24067 token
= cp_lexer_peek_token (parser
->lexer
);
24069 if (token
->type
== CPP_EOF
24070 || token
->type
== CPP_PRAGMA_EOL
)
24071 return error_mark_node
;
24073 cp_lexer_consume_token (parser
->lexer
);
24075 /* Accept = default or = delete in c++0x mode. */
24076 if (token
->keyword
== RID_DEFAULT
24077 || token
->keyword
== RID_DELETE
)
24079 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24080 return token
->u
.value
;
24083 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24084 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24086 cp_parser_error (parser
,
24087 "invalid pure specifier (only %<= 0%> is allowed)");
24088 cp_parser_skip_to_end_of_statement (parser
);
24089 return error_mark_node
;
24091 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24093 error_at (token
->location
, "templates may not be %<virtual%>");
24094 return error_mark_node
;
24097 return integer_zero_node
;
24100 /* Parse a constant-initializer.
24102 constant-initializer:
24103 = constant-expression
24105 Returns a representation of the constant-expression. */
24108 cp_parser_constant_initializer (cp_parser
* parser
)
24110 /* Look for the `=' token. */
24111 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24112 return error_mark_node
;
24114 /* It is invalid to write:
24116 struct S { static const int i = { 7 }; };
24119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24121 cp_parser_error (parser
,
24122 "a brace-enclosed initializer is not allowed here");
24123 /* Consume the opening brace. */
24124 matching_braces braces
;
24125 braces
.consume_open (parser
);
24126 /* Skip the initializer. */
24127 cp_parser_skip_to_closing_brace (parser
);
24128 /* Look for the trailing `}'. */
24129 braces
.require_close (parser
);
24131 return error_mark_node
;
24134 return cp_parser_constant_expression (parser
);
24137 /* Derived classes [gram.class.derived] */
24139 /* Parse a base-clause.
24142 : base-specifier-list
24144 base-specifier-list:
24145 base-specifier ... [opt]
24146 base-specifier-list , base-specifier ... [opt]
24148 Returns a TREE_LIST representing the base-classes, in the order in
24149 which they were declared. The representation of each node is as
24150 described by cp_parser_base_specifier.
24152 In the case that no bases are specified, this function will return
24153 NULL_TREE, not ERROR_MARK_NODE. */
24156 cp_parser_base_clause (cp_parser
* parser
)
24158 tree bases
= NULL_TREE
;
24160 /* Look for the `:' that begins the list. */
24161 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24163 /* Scan the base-specifier-list. */
24168 bool pack_expansion_p
= false;
24170 /* Look for the base-specifier. */
24171 base
= cp_parser_base_specifier (parser
);
24172 /* Look for the (optional) ellipsis. */
24173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24175 /* Consume the `...'. */
24176 cp_lexer_consume_token (parser
->lexer
);
24178 pack_expansion_p
= true;
24181 /* Add BASE to the front of the list. */
24182 if (base
&& base
!= error_mark_node
)
24184 if (pack_expansion_p
)
24185 /* Make this a pack expansion type. */
24186 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24188 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24190 TREE_CHAIN (base
) = bases
;
24194 /* Peek at the next token. */
24195 token
= cp_lexer_peek_token (parser
->lexer
);
24196 /* If it's not a comma, then the list is complete. */
24197 if (token
->type
!= CPP_COMMA
)
24199 /* Consume the `,'. */
24200 cp_lexer_consume_token (parser
->lexer
);
24203 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24204 base class had a qualified name. However, the next name that
24205 appears is certainly not qualified. */
24206 parser
->scope
= NULL_TREE
;
24207 parser
->qualifying_scope
= NULL_TREE
;
24208 parser
->object_scope
= NULL_TREE
;
24210 return nreverse (bases
);
24213 /* Parse a base-specifier.
24216 :: [opt] nested-name-specifier [opt] class-name
24217 virtual access-specifier [opt] :: [opt] nested-name-specifier
24219 access-specifier virtual [opt] :: [opt] nested-name-specifier
24222 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24223 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24224 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24225 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24228 cp_parser_base_specifier (cp_parser
* parser
)
24232 bool virtual_p
= false;
24233 bool duplicate_virtual_error_issued_p
= false;
24234 bool duplicate_access_error_issued_p
= false;
24235 bool class_scope_p
, template_p
;
24236 tree access
= access_default_node
;
24239 /* Process the optional `virtual' and `access-specifier'. */
24242 /* Peek at the next token. */
24243 token
= cp_lexer_peek_token (parser
->lexer
);
24244 /* Process `virtual'. */
24245 switch (token
->keyword
)
24248 /* If `virtual' appears more than once, issue an error. */
24249 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24251 cp_parser_error (parser
,
24252 "%<virtual%> specified more than once in base-specifier");
24253 duplicate_virtual_error_issued_p
= true;
24258 /* Consume the `virtual' token. */
24259 cp_lexer_consume_token (parser
->lexer
);
24264 case RID_PROTECTED
:
24266 /* If more than one access specifier appears, issue an
24268 if (access
!= access_default_node
24269 && !duplicate_access_error_issued_p
)
24271 cp_parser_error (parser
,
24272 "more than one access specifier in base-specifier");
24273 duplicate_access_error_issued_p
= true;
24276 access
= ridpointers
[(int) token
->keyword
];
24278 /* Consume the access-specifier. */
24279 cp_lexer_consume_token (parser
->lexer
);
24288 /* It is not uncommon to see programs mechanically, erroneously, use
24289 the 'typename' keyword to denote (dependent) qualified types
24290 as base classes. */
24291 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24293 token
= cp_lexer_peek_token (parser
->lexer
);
24294 if (!processing_template_decl
)
24295 error_at (token
->location
,
24296 "keyword %<typename%> not allowed outside of templates");
24298 error_at (token
->location
,
24299 "keyword %<typename%> not allowed in this context "
24300 "(the base class is implicitly a type)");
24301 cp_lexer_consume_token (parser
->lexer
);
24304 /* Look for the optional `::' operator. */
24305 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24306 /* Look for the nested-name-specifier. The simplest way to
24311 The keyword `typename' is not permitted in a base-specifier or
24312 mem-initializer; in these contexts a qualified name that
24313 depends on a template-parameter is implicitly assumed to be a
24316 is to pretend that we have seen the `typename' keyword at this
24318 cp_parser_nested_name_specifier_opt (parser
,
24319 /*typename_keyword_p=*/true,
24320 /*check_dependency_p=*/true,
24322 /*is_declaration=*/true);
24323 /* If the base class is given by a qualified name, assume that names
24324 we see are type names or templates, as appropriate. */
24325 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24326 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24329 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24330 /* DR 950 allows decltype as a base-specifier. */
24331 type
= cp_parser_decltype (parser
);
24334 /* Otherwise, look for the class-name. */
24335 type
= cp_parser_class_name (parser
,
24339 /*check_dependency_p=*/true,
24340 /*class_head_p=*/false,
24341 /*is_declaration=*/true);
24342 type
= TREE_TYPE (type
);
24345 if (type
== error_mark_node
)
24346 return error_mark_node
;
24348 return finish_base_specifier (type
, access
, virtual_p
);
24351 /* Exception handling [gram.exception] */
24353 /* Parse an (optional) noexcept-specification.
24355 noexcept-specification:
24356 noexcept ( constant-expression ) [opt]
24358 If no noexcept-specification is present, returns NULL_TREE.
24359 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24360 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24361 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24362 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24363 in which case a boolean condition is returned instead. */
24366 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24367 bool require_constexpr
,
24368 bool* consumed_expr
,
24372 const char *saved_message
;
24374 /* Peek at the next token. */
24375 token
= cp_lexer_peek_token (parser
->lexer
);
24377 /* Is it a noexcept-specification? */
24378 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24381 cp_lexer_consume_token (parser
->lexer
);
24383 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24385 matching_parens parens
;
24386 parens
.consume_open (parser
);
24388 if (require_constexpr
)
24390 /* Types may not be defined in an exception-specification. */
24391 saved_message
= parser
->type_definition_forbidden_message
;
24392 parser
->type_definition_forbidden_message
24393 = G_("types may not be defined in an exception-specification");
24395 expr
= cp_parser_constant_expression (parser
);
24397 /* Restore the saved message. */
24398 parser
->type_definition_forbidden_message
= saved_message
;
24402 expr
= cp_parser_expression (parser
);
24403 *consumed_expr
= true;
24406 parens
.require_close (parser
);
24410 expr
= boolean_true_node
;
24411 if (!require_constexpr
)
24412 *consumed_expr
= false;
24415 /* We cannot build a noexcept-spec right away because this will check
24416 that expr is a constexpr. */
24418 return build_noexcept_spec (expr
, tf_warning_or_error
);
24426 /* Parse an (optional) exception-specification.
24428 exception-specification:
24429 throw ( type-id-list [opt] )
24431 Returns a TREE_LIST representing the exception-specification. The
24432 TREE_VALUE of each node is a type. */
24435 cp_parser_exception_specification_opt (cp_parser
* parser
)
24439 const char *saved_message
;
24441 /* Peek at the next token. */
24442 token
= cp_lexer_peek_token (parser
->lexer
);
24444 /* Is it a noexcept-specification? */
24445 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24447 if (type_id_list
!= NULL_TREE
)
24448 return type_id_list
;
24450 /* If it's not `throw', then there's no exception-specification. */
24451 if (!cp_parser_is_keyword (token
, RID_THROW
))
24454 location_t loc
= token
->location
;
24456 /* Consume the `throw'. */
24457 cp_lexer_consume_token (parser
->lexer
);
24459 /* Look for the `('. */
24460 matching_parens parens
;
24461 parens
.require_open (parser
);
24463 /* Peek at the next token. */
24464 token
= cp_lexer_peek_token (parser
->lexer
);
24465 /* If it's not a `)', then there is a type-id-list. */
24466 if (token
->type
!= CPP_CLOSE_PAREN
)
24468 /* Types may not be defined in an exception-specification. */
24469 saved_message
= parser
->type_definition_forbidden_message
;
24470 parser
->type_definition_forbidden_message
24471 = G_("types may not be defined in an exception-specification");
24472 /* Parse the type-id-list. */
24473 type_id_list
= cp_parser_type_id_list (parser
);
24474 /* Restore the saved message. */
24475 parser
->type_definition_forbidden_message
= saved_message
;
24477 if (cxx_dialect
>= cxx17
)
24479 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24481 type_id_list
= NULL_TREE
;
24483 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24484 warning_at (loc
, OPT_Wdeprecated
,
24485 "dynamic exception specifications are deprecated in "
24488 /* In C++17, throw() is equivalent to noexcept (true). throw()
24489 is deprecated in C++11 and above as well, but is still widely used,
24490 so don't warn about it yet. */
24491 else if (cxx_dialect
>= cxx17
)
24492 type_id_list
= noexcept_true_spec
;
24494 type_id_list
= empty_except_spec
;
24496 /* Look for the `)'. */
24497 parens
.require_close (parser
);
24499 return type_id_list
;
24502 /* Parse an (optional) type-id-list.
24506 type-id-list , type-id ... [opt]
24508 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24509 in the order that the types were presented. */
24512 cp_parser_type_id_list (cp_parser
* parser
)
24514 tree types
= NULL_TREE
;
24521 token
= cp_lexer_peek_token (parser
->lexer
);
24523 /* Get the next type-id. */
24524 type
= cp_parser_type_id (parser
);
24525 /* Check for invalid 'auto'. */
24526 if (flag_concepts
&& type_uses_auto (type
))
24528 error_at (token
->location
,
24529 "invalid use of %<auto%> in exception-specification");
24530 type
= error_mark_node
;
24532 /* Parse the optional ellipsis. */
24533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24535 /* Consume the `...'. */
24536 cp_lexer_consume_token (parser
->lexer
);
24538 /* Turn the type into a pack expansion expression. */
24539 type
= make_pack_expansion (type
);
24541 /* Add it to the list. */
24542 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24543 /* Peek at the next token. */
24544 token
= cp_lexer_peek_token (parser
->lexer
);
24545 /* If it is not a `,', we are done. */
24546 if (token
->type
!= CPP_COMMA
)
24548 /* Consume the `,'. */
24549 cp_lexer_consume_token (parser
->lexer
);
24552 return nreverse (types
);
24555 /* Parse a try-block.
24558 try compound-statement handler-seq */
24561 cp_parser_try_block (cp_parser
* parser
)
24565 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24566 if (parser
->in_function_body
24567 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24568 error ("%<try%> in %<constexpr%> function");
24570 try_block
= begin_try_block ();
24571 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24572 finish_try_block (try_block
);
24573 cp_parser_handler_seq (parser
);
24574 finish_handler_sequence (try_block
);
24579 /* Parse a function-try-block.
24581 function-try-block:
24582 try ctor-initializer [opt] function-body handler-seq */
24585 cp_parser_function_try_block (cp_parser
* parser
)
24587 tree compound_stmt
;
24590 /* Look for the `try' keyword. */
24591 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24593 /* Let the rest of the front end know where we are. */
24594 try_block
= begin_function_try_block (&compound_stmt
);
24595 /* Parse the function-body. */
24596 cp_parser_ctor_initializer_opt_and_function_body
24597 (parser
, /*in_function_try_block=*/true);
24598 /* We're done with the `try' part. */
24599 finish_function_try_block (try_block
);
24600 /* Parse the handlers. */
24601 cp_parser_handler_seq (parser
);
24602 /* We're done with the handlers. */
24603 finish_function_handler_sequence (try_block
, compound_stmt
);
24606 /* Parse a handler-seq.
24609 handler handler-seq [opt] */
24612 cp_parser_handler_seq (cp_parser
* parser
)
24618 /* Parse the handler. */
24619 cp_parser_handler (parser
);
24620 /* Peek at the next token. */
24621 token
= cp_lexer_peek_token (parser
->lexer
);
24622 /* If it's not `catch' then there are no more handlers. */
24623 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24628 /* Parse a handler.
24631 catch ( exception-declaration ) compound-statement */
24634 cp_parser_handler (cp_parser
* parser
)
24639 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24640 handler
= begin_handler ();
24641 matching_parens parens
;
24642 parens
.require_open (parser
);
24643 declaration
= cp_parser_exception_declaration (parser
);
24644 finish_handler_parms (declaration
, handler
);
24645 parens
.require_close (parser
);
24646 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24647 finish_handler (handler
);
24650 /* Parse an exception-declaration.
24652 exception-declaration:
24653 type-specifier-seq declarator
24654 type-specifier-seq abstract-declarator
24658 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24659 ellipsis variant is used. */
24662 cp_parser_exception_declaration (cp_parser
* parser
)
24664 cp_decl_specifier_seq type_specifiers
;
24665 cp_declarator
*declarator
;
24666 const char *saved_message
;
24668 /* If it's an ellipsis, it's easy to handle. */
24669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24671 /* Consume the `...' token. */
24672 cp_lexer_consume_token (parser
->lexer
);
24676 /* Types may not be defined in exception-declarations. */
24677 saved_message
= parser
->type_definition_forbidden_message
;
24678 parser
->type_definition_forbidden_message
24679 = G_("types may not be defined in exception-declarations");
24681 /* Parse the type-specifier-seq. */
24682 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24683 /*is_trailing_return=*/false,
24685 /* If it's a `)', then there is no declarator. */
24686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24689 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24690 /*ctor_dtor_or_conv_p=*/NULL
,
24691 /*parenthesized_p=*/NULL
,
24692 /*member_p=*/false,
24693 /*friend_p=*/false);
24695 /* Restore the saved message. */
24696 parser
->type_definition_forbidden_message
= saved_message
;
24698 if (!type_specifiers
.any_specifiers_p
)
24699 return error_mark_node
;
24701 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24704 /* Parse a throw-expression.
24707 throw assignment-expression [opt]
24709 Returns a THROW_EXPR representing the throw-expression. */
24712 cp_parser_throw_expression (cp_parser
* parser
)
24717 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24718 token
= cp_lexer_peek_token (parser
->lexer
);
24719 /* Figure out whether or not there is an assignment-expression
24720 following the "throw" keyword. */
24721 if (token
->type
== CPP_COMMA
24722 || token
->type
== CPP_SEMICOLON
24723 || token
->type
== CPP_CLOSE_PAREN
24724 || token
->type
== CPP_CLOSE_SQUARE
24725 || token
->type
== CPP_CLOSE_BRACE
24726 || token
->type
== CPP_COLON
)
24727 expression
= NULL_TREE
;
24729 expression
= cp_parser_assignment_expression (parser
);
24731 return build_throw (expression
);
24734 /* GNU Extensions */
24736 /* Parse an (optional) asm-specification.
24739 asm ( string-literal )
24741 If the asm-specification is present, returns a STRING_CST
24742 corresponding to the string-literal. Otherwise, returns
24746 cp_parser_asm_specification_opt (cp_parser
* parser
)
24749 tree asm_specification
;
24751 /* Peek at the next token. */
24752 token
= cp_lexer_peek_token (parser
->lexer
);
24753 /* If the next token isn't the `asm' keyword, then there's no
24754 asm-specification. */
24755 if (!cp_parser_is_keyword (token
, RID_ASM
))
24758 /* Consume the `asm' token. */
24759 cp_lexer_consume_token (parser
->lexer
);
24760 /* Look for the `('. */
24761 matching_parens parens
;
24762 parens
.require_open (parser
);
24764 /* Look for the string-literal. */
24765 asm_specification
= cp_parser_string_literal (parser
, false, false);
24767 /* Look for the `)'. */
24768 parens
.require_close (parser
);
24770 return asm_specification
;
24773 /* Parse an asm-operand-list.
24777 asm-operand-list , asm-operand
24780 string-literal ( expression )
24781 [ string-literal ] string-literal ( expression )
24783 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24784 each node is the expression. The TREE_PURPOSE is itself a
24785 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24786 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24787 is a STRING_CST for the string literal before the parenthesis. Returns
24788 ERROR_MARK_NODE if any of the operands are invalid. */
24791 cp_parser_asm_operand_list (cp_parser
* parser
)
24793 tree asm_operands
= NULL_TREE
;
24794 bool invalid_operands
= false;
24798 tree string_literal
;
24802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24804 /* Consume the `[' token. */
24805 cp_lexer_consume_token (parser
->lexer
);
24806 /* Read the operand name. */
24807 name
= cp_parser_identifier (parser
);
24808 if (name
!= error_mark_node
)
24809 name
= build_string (IDENTIFIER_LENGTH (name
),
24810 IDENTIFIER_POINTER (name
));
24811 /* Look for the closing `]'. */
24812 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24816 /* Look for the string-literal. */
24817 string_literal
= cp_parser_string_literal (parser
, false, false);
24819 /* Look for the `('. */
24820 matching_parens parens
;
24821 parens
.require_open (parser
);
24822 /* Parse the expression. */
24823 expression
= cp_parser_expression (parser
);
24824 /* Look for the `)'. */
24825 parens
.require_close (parser
);
24827 if (name
== error_mark_node
24828 || string_literal
== error_mark_node
24829 || expression
== error_mark_node
)
24830 invalid_operands
= true;
24832 /* Add this operand to the list. */
24833 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24836 /* If the next token is not a `,', there are no more
24838 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24840 /* Consume the `,'. */
24841 cp_lexer_consume_token (parser
->lexer
);
24844 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24847 /* Parse an asm-clobber-list.
24851 asm-clobber-list , string-literal
24853 Returns a TREE_LIST, indicating the clobbers in the order that they
24854 appeared. The TREE_VALUE of each node is a STRING_CST. */
24857 cp_parser_asm_clobber_list (cp_parser
* parser
)
24859 tree clobbers
= NULL_TREE
;
24863 tree string_literal
;
24865 /* Look for the string literal. */
24866 string_literal
= cp_parser_string_literal (parser
, false, false);
24867 /* Add it to the list. */
24868 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24869 /* If the next token is not a `,', then the list is
24871 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24873 /* Consume the `,' token. */
24874 cp_lexer_consume_token (parser
->lexer
);
24880 /* Parse an asm-label-list.
24884 asm-label-list , identifier
24886 Returns a TREE_LIST, indicating the labels in the order that they
24887 appeared. The TREE_VALUE of each node is a label. */
24890 cp_parser_asm_label_list (cp_parser
* parser
)
24892 tree labels
= NULL_TREE
;
24896 tree identifier
, label
, name
;
24898 /* Look for the identifier. */
24899 identifier
= cp_parser_identifier (parser
);
24900 if (!error_operand_p (identifier
))
24902 label
= lookup_label (identifier
);
24903 if (TREE_CODE (label
) == LABEL_DECL
)
24905 TREE_USED (label
) = 1;
24906 check_goto (label
);
24907 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24908 IDENTIFIER_POINTER (identifier
));
24909 labels
= tree_cons (name
, label
, labels
);
24912 /* If the next token is not a `,', then the list is
24914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24916 /* Consume the `,' token. */
24917 cp_lexer_consume_token (parser
->lexer
);
24920 return nreverse (labels
);
24923 /* Return TRUE iff the next tokens in the stream are possibly the
24924 beginning of a GNU extension attribute. */
24927 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24929 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24932 /* Return TRUE iff the next tokens in the stream are possibly the
24933 beginning of a standard C++-11 attribute specifier. */
24936 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24938 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24941 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24942 beginning of a standard C++-11 attribute specifier. */
24945 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24947 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24949 return (cxx_dialect
>= cxx11
24950 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24951 || (token
->type
== CPP_OPEN_SQUARE
24952 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24953 && token
->type
== CPP_OPEN_SQUARE
)));
24956 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24957 beginning of a GNU extension attribute. */
24960 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24962 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24964 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24967 /* Return true iff the next tokens can be the beginning of either a
24968 GNU attribute list, or a standard C++11 attribute sequence. */
24971 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24973 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24974 || cp_next_tokens_can_be_std_attribute_p (parser
));
24977 /* Return true iff the next Nth tokens can be the beginning of either
24978 a GNU attribute list, or a standard C++11 attribute sequence. */
24981 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24983 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24984 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24987 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24988 of GNU attributes, or return NULL. */
24991 cp_parser_attributes_opt (cp_parser
*parser
)
24993 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24994 return cp_parser_gnu_attributes_opt (parser
);
24995 return cp_parser_std_attribute_spec_seq (parser
);
24998 #define CILK_SIMD_FN_CLAUSE_MASK \
24999 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
25000 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
25001 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
25002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
25003 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
25005 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
25006 vector [(<clauses>)] */
25009 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
25011 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
25012 cp_token
*token
= v_token
;
25015 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
25016 parser
->cilk_simd_fn_info
->error_seen
= false;
25017 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
25018 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
25019 parser
->cilk_simd_fn_info
->clauses
= NULL_TREE
;
25021 int paren_scope
= 0;
25022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25024 cp_lexer_consume_token (parser
->lexer
);
25025 v_token
= cp_lexer_peek_token (parser
->lexer
);
25028 while (paren_scope
> 0)
25030 token
= cp_lexer_peek_token (parser
->lexer
);
25031 if (token
->type
== CPP_OPEN_PAREN
)
25033 else if (token
->type
== CPP_CLOSE_PAREN
)
25035 /* Do not push the last ')' */
25036 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
25037 cp_lexer_consume_token (parser
->lexer
);
25040 token
->type
= CPP_PRAGMA_EOL
;
25041 parser
->lexer
->next_token
= token
;
25042 cp_lexer_consume_token (parser
->lexer
);
25044 struct cp_token_cache
*cp
25045 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
25046 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
25049 /* Parse an (optional) series of attributes.
25052 attributes attribute
25055 __attribute__ (( attribute-list [opt] ))
25057 The return value is as for cp_parser_gnu_attribute_list. */
25060 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25062 tree attributes
= NULL_TREE
;
25067 tree attribute_list
;
25070 /* Peek at the next token. */
25071 token
= cp_lexer_peek_token (parser
->lexer
);
25072 /* If it's not `__attribute__', then we're done. */
25073 if (token
->keyword
!= RID_ATTRIBUTE
)
25076 /* Consume the `__attribute__' keyword. */
25077 cp_lexer_consume_token (parser
->lexer
);
25078 /* Look for the two `(' tokens. */
25079 matching_parens outer_parens
;
25080 outer_parens
.require_open (parser
);
25081 matching_parens inner_parens
;
25082 inner_parens
.require_open (parser
);
25084 /* Peek at the next token. */
25085 token
= cp_lexer_peek_token (parser
->lexer
);
25086 if (token
->type
!= CPP_CLOSE_PAREN
)
25087 /* Parse the attribute-list. */
25088 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25090 /* If the next token is a `)', then there is no attribute
25092 attribute_list
= NULL
;
25094 /* Look for the two `)' tokens. */
25095 if (!inner_parens
.require_close (parser
))
25097 if (!outer_parens
.require_close (parser
))
25100 cp_parser_skip_to_end_of_statement (parser
);
25102 /* Add these new attributes to the list. */
25103 attributes
= chainon (attributes
, attribute_list
);
25109 /* Parse a GNU attribute-list.
25113 attribute-list , attribute
25117 identifier ( identifier )
25118 identifier ( identifier , expression-list )
25119 identifier ( expression-list )
25121 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25122 to an attribute. The TREE_PURPOSE of each node is the identifier
25123 indicating which attribute is in use. The TREE_VALUE represents
25124 the arguments, if any. */
25127 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25129 tree attribute_list
= NULL_TREE
;
25130 bool save_translate_strings_p
= parser
->translate_strings_p
;
25132 parser
->translate_strings_p
= false;
25139 /* Look for the identifier. We also allow keywords here; for
25140 example `__attribute__ ((const))' is legal. */
25141 token
= cp_lexer_peek_token (parser
->lexer
);
25142 if (token
->type
== CPP_NAME
25143 || token
->type
== CPP_KEYWORD
)
25145 tree arguments
= NULL_TREE
;
25147 /* Consume the token, but save it since we need it for the
25148 SIMD enabled function parsing. */
25149 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25151 /* Save away the identifier that indicates which attribute
25153 identifier
= (token
->type
== CPP_KEYWORD
)
25154 /* For keywords, use the canonical spelling, not the
25155 parsed identifier. */
25156 ? ridpointers
[(int) token
->keyword
]
25157 : id_token
->u
.value
;
25159 identifier
= canonicalize_attr_name (identifier
);
25160 attribute
= build_tree_list (identifier
, NULL_TREE
);
25162 /* Peek at the next token. */
25163 token
= cp_lexer_peek_token (parser
->lexer
);
25164 /* If it's an `(', then parse the attribute arguments. */
25165 if (token
->type
== CPP_OPEN_PAREN
)
25167 vec
<tree
, va_gc
> *vec
;
25168 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25169 ? id_attr
: normal_attr
);
25170 if (is_cilkplus_vector_p (identifier
))
25172 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
25176 vec
= cp_parser_parenthesized_expression_list
25177 (parser
, attr_flag
, /*cast_p=*/false,
25178 /*allow_expansion_p=*/false,
25179 /*non_constant_p=*/NULL
);
25181 arguments
= error_mark_node
;
25184 arguments
= build_tree_list_vec (vec
);
25185 release_tree_vector (vec
);
25187 /* Save the arguments away. */
25188 TREE_VALUE (attribute
) = arguments
;
25190 else if (is_cilkplus_vector_p (identifier
))
25192 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
25196 if (arguments
!= error_mark_node
)
25198 /* Add this attribute to the list. */
25199 TREE_CHAIN (attribute
) = attribute_list
;
25200 attribute_list
= attribute
;
25203 token
= cp_lexer_peek_token (parser
->lexer
);
25205 /* Now, look for more attributes. If the next token isn't a
25206 `,', we're done. */
25207 if (token
->type
!= CPP_COMMA
)
25210 /* Consume the comma and keep going. */
25211 cp_lexer_consume_token (parser
->lexer
);
25213 parser
->translate_strings_p
= save_translate_strings_p
;
25215 /* We built up the list in reverse order. */
25216 return nreverse (attribute_list
);
25219 /* Parse a standard C++11 attribute.
25221 The returned representation is a TREE_LIST which TREE_PURPOSE is
25222 the scoped name of the attribute, and the TREE_VALUE is its
25225 Note that the scoped name of the attribute is itself a TREE_LIST
25226 which TREE_PURPOSE is the namespace of the attribute, and
25227 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25228 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25229 and which TREE_PURPOSE is directly the attribute name.
25231 Clients of the attribute code should use get_attribute_namespace
25232 and get_attribute_name to get the actual namespace and name of
25233 attributes, regardless of their being GNU or C++11 attributes.
25236 attribute-token attribute-argument-clause [opt]
25240 attribute-scoped-token
25242 attribute-scoped-token:
25243 attribute-namespace :: identifier
25245 attribute-namespace:
25248 attribute-argument-clause:
25249 ( balanced-token-seq )
25251 balanced-token-seq:
25252 balanced-token [opt]
25253 balanced-token-seq balanced-token
25256 ( balanced-token-seq )
25257 [ balanced-token-seq ]
25258 { balanced-token-seq }. */
25261 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25263 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25266 /* First, parse name of the attribute, a.k.a attribute-token. */
25268 token
= cp_lexer_peek_token (parser
->lexer
);
25269 if (token
->type
== CPP_NAME
)
25270 attr_id
= token
->u
.value
;
25271 else if (token
->type
== CPP_KEYWORD
)
25272 attr_id
= ridpointers
[(int) token
->keyword
];
25273 else if (token
->flags
& NAMED_OP
)
25274 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25276 if (attr_id
== NULL_TREE
)
25279 cp_lexer_consume_token (parser
->lexer
);
25281 token
= cp_lexer_peek_token (parser
->lexer
);
25282 if (token
->type
== CPP_SCOPE
)
25284 /* We are seeing a scoped attribute token. */
25286 cp_lexer_consume_token (parser
->lexer
);
25288 error_at (token
->location
, "attribute using prefix used together "
25289 "with scoped attribute token");
25292 token
= cp_lexer_consume_token (parser
->lexer
);
25293 if (token
->type
== CPP_NAME
)
25294 attr_id
= token
->u
.value
;
25295 else if (token
->type
== CPP_KEYWORD
)
25296 attr_id
= ridpointers
[(int) token
->keyword
];
25297 else if (token
->flags
& NAMED_OP
)
25298 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25301 error_at (token
->location
,
25302 "expected an identifier for the attribute name");
25303 return error_mark_node
;
25306 attr_id
= canonicalize_attr_name (attr_id
);
25307 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25309 token
= cp_lexer_peek_token (parser
->lexer
);
25312 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25316 attr_id
= canonicalize_attr_name (attr_id
);
25317 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25319 /* C++11 noreturn attribute is equivalent to GNU's. */
25320 if (is_attribute_p ("noreturn", attr_id
))
25321 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25322 /* C++14 deprecated attribute is equivalent to GNU's. */
25323 else if (is_attribute_p ("deprecated", attr_id
))
25324 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25325 /* C++17 fallthrough attribute is equivalent to GNU's. */
25326 else if (is_attribute_p ("fallthrough", attr_id
))
25327 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25328 /* Transactional Memory TS optimize_for_synchronized attribute is
25329 equivalent to GNU transaction_callable. */
25330 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25331 TREE_PURPOSE (attribute
)
25332 = get_identifier ("transaction_callable");
25333 /* Transactional Memory attributes are GNU attributes. */
25334 else if (tm_attr_to_mask (attr_id
))
25335 TREE_PURPOSE (attribute
) = attr_id
;
25338 /* Now parse the optional argument clause of the attribute. */
25340 if (token
->type
!= CPP_OPEN_PAREN
)
25344 vec
<tree
, va_gc
> *vec
;
25345 int attr_flag
= normal_attr
;
25347 if (attr_ns
== get_identifier ("gnu")
25348 && attribute_takes_identifier_p (attr_id
))
25349 /* A GNU attribute that takes an identifier in parameter. */
25350 attr_flag
= id_attr
;
25352 vec
= cp_parser_parenthesized_expression_list
25353 (parser
, attr_flag
, /*cast_p=*/false,
25354 /*allow_expansion_p=*/true,
25355 /*non_constant_p=*/NULL
);
25357 arguments
= error_mark_node
;
25360 arguments
= build_tree_list_vec (vec
);
25361 release_tree_vector (vec
);
25364 if (arguments
== error_mark_node
)
25365 attribute
= error_mark_node
;
25367 TREE_VALUE (attribute
) = arguments
;
25373 /* Check that the attribute ATTRIBUTE appears at most once in the
25374 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25375 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25376 isn't implemented yet in GCC. */
25379 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25383 tree name
= get_attribute_name (attribute
);
25384 if (is_attribute_p ("noreturn", name
)
25385 && lookup_attribute ("noreturn", attributes
))
25386 error ("attribute %<noreturn%> can appear at most once "
25387 "in an attribute-list");
25388 else if (is_attribute_p ("deprecated", name
)
25389 && lookup_attribute ("deprecated", attributes
))
25390 error ("attribute %<deprecated%> can appear at most once "
25391 "in an attribute-list");
25395 /* Parse a list of standard C++-11 attributes.
25399 attribute-list , attribute[opt]
25401 attribute-list , attribute ...
25405 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25407 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25408 cp_token
*token
= NULL
;
25412 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25413 if (attribute
== error_mark_node
)
25415 if (attribute
!= NULL_TREE
)
25417 cp_parser_check_std_attribute (attributes
, attribute
);
25418 TREE_CHAIN (attribute
) = attributes
;
25419 attributes
= attribute
;
25421 token
= cp_lexer_peek_token (parser
->lexer
);
25422 if (token
->type
== CPP_ELLIPSIS
)
25424 cp_lexer_consume_token (parser
->lexer
);
25425 if (attribute
== NULL_TREE
)
25426 error_at (token
->location
,
25427 "expected attribute before %<...%>");
25430 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25431 if (pack
== error_mark_node
)
25432 return error_mark_node
;
25433 TREE_VALUE (attribute
) = pack
;
25435 token
= cp_lexer_peek_token (parser
->lexer
);
25437 if (token
->type
!= CPP_COMMA
)
25439 cp_lexer_consume_token (parser
->lexer
);
25441 attributes
= nreverse (attributes
);
25445 /* Parse a standard C++-11 attribute specifier.
25447 attribute-specifier:
25448 [ [ attribute-using-prefix [opt] attribute-list ] ]
25449 alignment-specifier
25451 attribute-using-prefix:
25452 using attribute-namespace :
25454 alignment-specifier:
25455 alignas ( type-id ... [opt] )
25456 alignas ( alignment-expression ... [opt] ). */
25459 cp_parser_std_attribute_spec (cp_parser
*parser
)
25461 tree attributes
= NULL_TREE
;
25462 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25464 if (token
->type
== CPP_OPEN_SQUARE
25465 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25467 tree attr_ns
= NULL_TREE
;
25469 cp_lexer_consume_token (parser
->lexer
);
25470 cp_lexer_consume_token (parser
->lexer
);
25472 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25474 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25475 if (token
->type
== CPP_NAME
)
25476 attr_ns
= token
->u
.value
;
25477 else if (token
->type
== CPP_KEYWORD
)
25478 attr_ns
= ridpointers
[(int) token
->keyword
];
25479 else if (token
->flags
& NAMED_OP
)
25480 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25483 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25485 if (cxx_dialect
< cxx17
25486 && !in_system_header_at (input_location
))
25487 pedwarn (input_location
, 0,
25488 "attribute using prefix only available "
25489 "with -std=c++17 or -std=gnu++17");
25491 cp_lexer_consume_token (parser
->lexer
);
25492 cp_lexer_consume_token (parser
->lexer
);
25493 cp_lexer_consume_token (parser
->lexer
);
25496 attr_ns
= NULL_TREE
;
25499 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25501 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25502 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25503 cp_parser_skip_to_end_of_statement (parser
);
25505 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25506 when we are sure that we have actually parsed them. */
25507 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25513 /* Look for an alignment-specifier. */
25515 token
= cp_lexer_peek_token (parser
->lexer
);
25517 if (token
->type
!= CPP_KEYWORD
25518 || token
->keyword
!= RID_ALIGNAS
)
25521 cp_lexer_consume_token (parser
->lexer
);
25522 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25524 matching_parens parens
;
25525 if (!parens
.require_open (parser
))
25527 cp_parser_error (parser
, "expected %<(%>");
25528 return error_mark_node
;
25531 cp_parser_parse_tentatively (parser
);
25532 alignas_expr
= cp_parser_type_id (parser
);
25534 if (!cp_parser_parse_definitely (parser
))
25536 alignas_expr
= cp_parser_assignment_expression (parser
);
25537 if (alignas_expr
== error_mark_node
)
25538 cp_parser_skip_to_end_of_statement (parser
);
25539 if (alignas_expr
== NULL_TREE
25540 || alignas_expr
== error_mark_node
)
25541 return alignas_expr
;
25544 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25545 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25547 /* Handle alignas (pack...). */
25548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25550 cp_lexer_consume_token (parser
->lexer
);
25551 alignas_expr
= make_pack_expansion (alignas_expr
);
25554 /* Something went wrong, so don't build the attribute. */
25555 if (alignas_expr
== error_mark_node
)
25556 return error_mark_node
;
25558 if (!parens
.require_close (parser
))
25560 cp_parser_error (parser
, "expected %<)%>");
25561 return error_mark_node
;
25564 /* Build the C++-11 representation of an 'aligned'
25567 build_tree_list (build_tree_list (get_identifier ("gnu"),
25568 get_identifier ("aligned")),
25575 /* Parse a standard C++-11 attribute-specifier-seq.
25577 attribute-specifier-seq:
25578 attribute-specifier-seq [opt] attribute-specifier
25582 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25584 tree attr_specs
= NULL_TREE
;
25585 tree attr_last
= NULL_TREE
;
25589 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25590 if (attr_spec
== NULL_TREE
)
25592 if (attr_spec
== error_mark_node
)
25593 return error_mark_node
;
25596 TREE_CHAIN (attr_last
) = attr_spec
;
25598 attr_specs
= attr_last
= attr_spec
;
25599 attr_last
= tree_last (attr_last
);
25605 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25606 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25607 current value of the PEDANTIC flag, regardless of whether or not
25608 the `__extension__' keyword is present. The caller is responsible
25609 for restoring the value of the PEDANTIC flag. */
25612 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25614 /* Save the old value of the PEDANTIC flag. */
25615 *saved_pedantic
= pedantic
;
25617 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25619 /* Consume the `__extension__' token. */
25620 cp_lexer_consume_token (parser
->lexer
);
25621 /* We're not being pedantic while the `__extension__' keyword is
25631 /* Parse a label declaration.
25634 __label__ label-declarator-seq ;
25636 label-declarator-seq:
25637 identifier , label-declarator-seq
25641 cp_parser_label_declaration (cp_parser
* parser
)
25643 /* Look for the `__label__' keyword. */
25644 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25650 /* Look for an identifier. */
25651 identifier
= cp_parser_identifier (parser
);
25652 /* If we failed, stop. */
25653 if (identifier
== error_mark_node
)
25655 /* Declare it as a label. */
25656 finish_label_decl (identifier
);
25657 /* If the next token is a `;', stop. */
25658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25660 /* Look for the `,' separating the label declarations. */
25661 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25664 /* Look for the final `;'. */
25665 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25668 // -------------------------------------------------------------------------- //
25671 // Parse a requires clause.
25673 // requires-clause:
25674 // 'requires' logical-or-expression
25676 // The required logical-or-expression must be a constant expression. Note
25677 // that we don't check that the expression is constepxr here. We defer until
25678 // we analyze constraints and then, we only check atomic constraints.
25680 cp_parser_requires_clause (cp_parser
*parser
)
25682 // Parse the requires clause so that it is not automatically folded.
25683 ++processing_template_decl
;
25684 tree expr
= cp_parser_binary_expression (parser
, false, false,
25685 PREC_NOT_OPERATOR
, NULL
);
25686 if (check_for_bare_parameter_packs (expr
))
25687 expr
= error_mark_node
;
25688 --processing_template_decl
;
25692 // Optionally parse a requires clause:
25694 cp_parser_requires_clause_opt (cp_parser
*parser
)
25696 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25697 if (tok
->keyword
!= RID_REQUIRES
)
25699 if (!flag_concepts
&& tok
->type
== CPP_NAME
25700 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25702 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25703 "%<requires%> only available with -fconcepts");
25704 /* Parse and discard the requires-clause. */
25705 cp_lexer_consume_token (parser
->lexer
);
25706 cp_parser_requires_clause (parser
);
25710 cp_lexer_consume_token (parser
->lexer
);
25711 return cp_parser_requires_clause (parser
);
25715 /*---------------------------------------------------------------------------
25716 Requires expressions
25717 ---------------------------------------------------------------------------*/
25719 /* Parse a requires expression
25721 requirement-expression:
25722 'requires' requirement-parameter-list [opt] requirement-body */
25724 cp_parser_requires_expression (cp_parser
*parser
)
25726 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25727 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25729 /* A requires-expression shall appear only within a concept
25730 definition or a requires-clause.
25732 TODO: Implement this diagnostic correctly. */
25733 if (!processing_template_decl
)
25735 error_at (loc
, "a requires expression cannot appear outside a template");
25736 cp_parser_skip_to_end_of_statement (parser
);
25737 return error_mark_node
;
25742 /* Local parameters are delared as variables within the scope
25743 of the expression. They are not visible past the end of
25744 the expression. Expressions within the requires-expression
25745 are unevaluated. */
25746 struct scope_sentinel
25750 ++cp_unevaluated_operand
;
25751 begin_scope (sk_block
, NULL_TREE
);
25756 pop_bindings_and_leave_scope ();
25757 --cp_unevaluated_operand
;
25761 /* Parse the optional parameter list. */
25762 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25764 parms
= cp_parser_requirement_parameter_list (parser
);
25765 if (parms
== error_mark_node
)
25766 return error_mark_node
;
25771 /* Parse the requirement body. */
25772 reqs
= cp_parser_requirement_body (parser
);
25773 if (reqs
== error_mark_node
)
25774 return error_mark_node
;
25777 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25779 grokparms (parms
, &parms
);
25780 return finish_requires_expr (parms
, reqs
);
25783 /* Parse a parameterized requirement.
25785 requirement-parameter-list:
25786 '(' parameter-declaration-clause ')' */
25788 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25790 matching_parens parens
;
25791 if (!parens
.require_open (parser
))
25792 return error_mark_node
;
25794 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25796 if (!parens
.require_close (parser
))
25797 return error_mark_node
;
25802 /* Parse the body of a requirement.
25805 '{' requirement-list '}' */
25807 cp_parser_requirement_body (cp_parser
*parser
)
25809 matching_braces braces
;
25810 if (!braces
.require_open (parser
))
25811 return error_mark_node
;
25813 tree reqs
= cp_parser_requirement_list (parser
);
25815 if (!braces
.require_close (parser
))
25816 return error_mark_node
;
25821 /* Parse a list of requirements.
25825 requirement-list ';' requirement[opt] */
25827 cp_parser_requirement_list (cp_parser
*parser
)
25829 tree result
= NULL_TREE
;
25832 tree req
= cp_parser_requirement (parser
);
25833 if (req
== error_mark_node
)
25834 return error_mark_node
;
25836 result
= tree_cons (NULL_TREE
, req
, result
);
25838 /* If we see a semi-colon, consume it. */
25839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25840 cp_lexer_consume_token (parser
->lexer
);
25842 /* Stop processing at the end of the list. */
25843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25847 /* Reverse the order of requirements so they are analyzed in
25848 declaration order. */
25849 return nreverse (result
);
25852 /* Parse a syntactic requirement or type requirement.
25856 compound-requirement
25858 nested-requirement */
25860 cp_parser_requirement (cp_parser
*parser
)
25862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25863 return cp_parser_compound_requirement (parser
);
25864 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25865 return cp_parser_type_requirement (parser
);
25866 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25867 return cp_parser_nested_requirement (parser
);
25869 return cp_parser_simple_requirement (parser
);
25872 /* Parse a simple requirement.
25874 simple-requirement:
25877 cp_parser_simple_requirement (cp_parser
*parser
)
25879 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25880 if (!expr
|| expr
== error_mark_node
)
25881 return error_mark_node
;
25883 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25884 return error_mark_node
;
25886 return finish_simple_requirement (expr
);
25889 /* Parse a type requirement
25892 nested-name-specifier [opt] required-type-name ';'
25894 required-type-name:
25896 'template' [opt] simple-template-id */
25898 cp_parser_type_requirement (cp_parser
*parser
)
25900 cp_lexer_consume_token (parser
->lexer
);
25902 // Save the scope before parsing name specifiers.
25903 tree saved_scope
= parser
->scope
;
25904 tree saved_object_scope
= parser
->object_scope
;
25905 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25906 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25907 cp_parser_nested_name_specifier_opt (parser
,
25908 /*typename_keyword_p=*/true,
25909 /*check_dependency_p=*/false,
25911 /*is_declaration=*/false);
25914 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25916 cp_lexer_consume_token (parser
->lexer
);
25917 type
= cp_parser_template_id (parser
,
25918 /*template_keyword_p=*/true,
25919 /*check_dependency=*/false,
25920 /*tag_type=*/none_type
,
25921 /*is_declaration=*/false);
25922 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25923 /*complain=*/tf_error
);
25926 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25928 if (TREE_CODE (type
) == TYPE_DECL
)
25929 type
= TREE_TYPE (type
);
25931 parser
->scope
= saved_scope
;
25932 parser
->object_scope
= saved_object_scope
;
25933 parser
->qualifying_scope
= saved_qualifying_scope
;
25935 if (type
== error_mark_node
)
25936 cp_parser_skip_to_end_of_statement (parser
);
25938 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25939 return error_mark_node
;
25940 if (type
== error_mark_node
)
25941 return error_mark_node
;
25943 return finish_type_requirement (type
);
25946 /* Parse a compound requirement
25948 compound-requirement:
25949 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25951 cp_parser_compound_requirement (cp_parser
*parser
)
25953 /* Parse an expression enclosed in '{ }'s. */
25954 matching_braces braces
;
25955 if (!braces
.require_open (parser
))
25956 return error_mark_node
;
25958 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25959 if (!expr
|| expr
== error_mark_node
)
25960 return error_mark_node
;
25962 if (!braces
.require_close (parser
))
25963 return error_mark_node
;
25965 /* Parse the optional noexcept. */
25966 bool noexcept_p
= false;
25967 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25969 cp_lexer_consume_token (parser
->lexer
);
25973 /* Parse the optional trailing return type. */
25974 tree type
= NULL_TREE
;
25975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25977 cp_lexer_consume_token (parser
->lexer
);
25978 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25979 parser
->in_result_type_constraint_p
= true;
25980 type
= cp_parser_trailing_type_id (parser
);
25981 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25982 if (type
== error_mark_node
)
25983 return error_mark_node
;
25986 return finish_compound_requirement (expr
, type
, noexcept_p
);
25989 /* Parse a nested requirement. This is the same as a requires clause.
25991 nested-requirement:
25994 cp_parser_nested_requirement (cp_parser
*parser
)
25996 cp_lexer_consume_token (parser
->lexer
);
25997 tree req
= cp_parser_requires_clause (parser
);
25998 if (req
== error_mark_node
)
25999 return error_mark_node
;
26000 return finish_nested_requirement (req
);
26003 /* Support Functions */
26005 /* Return the appropriate prefer_type argument for lookup_name_real based on
26006 tag_type and template_mem_access. */
26009 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26011 /* DR 141: When looking in the current enclosing context for a template-name
26012 after -> or ., only consider class templates. */
26013 if (template_mem_access
)
26017 case none_type
: return 0; // No preference.
26018 case scope_type
: return 1; // Type or namespace.
26019 default: return 2; // Type only.
26023 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26024 NAME should have one of the representations used for an
26025 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26026 is returned. If PARSER->SCOPE is a dependent type, then a
26027 SCOPE_REF is returned.
26029 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26030 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26031 was formed. Abstractly, such entities should not be passed to this
26032 function, because they do not need to be looked up, but it is
26033 simpler to check for this special case here, rather than at the
26036 In cases not explicitly covered above, this function returns a
26037 DECL, OVERLOAD, or baselink representing the result of the lookup.
26038 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26041 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26042 (e.g., "struct") that was used. In that case bindings that do not
26043 refer to types are ignored.
26045 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26048 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26051 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26054 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26055 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26056 NULL_TREE otherwise. */
26059 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26060 enum tag_types tag_type
,
26063 bool check_dependency
,
26064 tree
*ambiguous_decls
,
26065 location_t name_location
)
26068 tree object_type
= parser
->context
->object_type
;
26070 /* Assume that the lookup will be unambiguous. */
26071 if (ambiguous_decls
)
26072 *ambiguous_decls
= NULL_TREE
;
26074 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26075 no longer valid. Note that if we are parsing tentatively, and
26076 the parse fails, OBJECT_TYPE will be automatically restored. */
26077 parser
->context
->object_type
= NULL_TREE
;
26079 if (name
== error_mark_node
)
26080 return error_mark_node
;
26082 /* A template-id has already been resolved; there is no lookup to
26084 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26086 if (BASELINK_P (name
))
26088 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26089 == TEMPLATE_ID_EXPR
);
26093 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26094 it should already have been checked to make sure that the name
26095 used matches the type being destroyed. */
26096 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26100 /* Figure out to which type this destructor applies. */
26102 type
= parser
->scope
;
26103 else if (object_type
)
26104 type
= object_type
;
26106 type
= current_class_type
;
26107 /* If that's not a class type, there is no destructor. */
26108 if (!type
|| !CLASS_TYPE_P (type
))
26109 return error_mark_node
;
26111 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26112 lazily_declare_fn (sfk_destructor
, type
);
26114 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26117 return error_mark_node
;
26120 /* By this point, the NAME should be an ordinary identifier. If
26121 the id-expression was a qualified name, the qualifying scope is
26122 stored in PARSER->SCOPE at this point. */
26123 gcc_assert (identifier_p (name
));
26125 /* Perform the lookup. */
26130 if (parser
->scope
== error_mark_node
)
26131 return error_mark_node
;
26133 /* If the SCOPE is dependent, the lookup must be deferred until
26134 the template is instantiated -- unless we are explicitly
26135 looking up names in uninstantiated templates. Even then, we
26136 cannot look up the name if the scope is not a class type; it
26137 might, for example, be a template type parameter. */
26138 dependent_p
= (TYPE_P (parser
->scope
)
26139 && dependent_scope_p (parser
->scope
));
26140 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26142 /* Defer lookup. */
26143 decl
= error_mark_node
;
26146 tree pushed_scope
= NULL_TREE
;
26148 /* If PARSER->SCOPE is a dependent type, then it must be a
26149 class type, and we must not be checking dependencies;
26150 otherwise, we would have processed this lookup above. So
26151 that PARSER->SCOPE is not considered a dependent base by
26152 lookup_member, we must enter the scope here. */
26154 pushed_scope
= push_scope (parser
->scope
);
26156 /* If the PARSER->SCOPE is a template specialization, it
26157 may be instantiated during name lookup. In that case,
26158 errors may be issued. Even if we rollback the current
26159 tentative parse, those errors are valid. */
26160 decl
= lookup_qualified_name (parser
->scope
, name
,
26161 prefer_type_arg (tag_type
),
26162 /*complain=*/true);
26164 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26165 lookup result and the nested-name-specifier nominates a class C:
26166 * if the name specified after the nested-name-specifier, when
26167 looked up in C, is the injected-class-name of C (Clause 9), or
26168 * if the name specified after the nested-name-specifier is the
26169 same as the identifier or the simple-template-id's template-
26170 name in the last component of the nested-name-specifier,
26171 the name is instead considered to name the constructor of
26172 class C. [ Note: for example, the constructor is not an
26173 acceptable lookup result in an elaborated-type-specifier so
26174 the constructor would not be used in place of the
26175 injected-class-name. --end note ] Such a constructor name
26176 shall be used only in the declarator-id of a declaration that
26177 names a constructor or in a using-declaration. */
26178 if (tag_type
== none_type
26179 && DECL_SELF_REFERENCE_P (decl
)
26180 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26181 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26182 prefer_type_arg (tag_type
),
26183 /*complain=*/true);
26185 /* If we have a single function from a using decl, pull it out. */
26186 if (TREE_CODE (decl
) == OVERLOAD
26187 && !really_overloaded_fn (decl
))
26188 decl
= OVL_FUNCTION (decl
);
26191 pop_scope (pushed_scope
);
26194 /* If the scope is a dependent type and either we deferred lookup or
26195 we did lookup but didn't find the name, rememeber the name. */
26196 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26197 && dependent_type_p (parser
->scope
))
26203 /* The resolution to Core Issue 180 says that `struct
26204 A::B' should be considered a type-name, even if `A'
26206 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26207 /*complain=*/tf_error
);
26208 if (type
!= error_mark_node
)
26209 decl
= TYPE_NAME (type
);
26211 else if (is_template
26212 && (cp_parser_next_token_ends_template_argument_p (parser
)
26213 || cp_lexer_next_token_is (parser
->lexer
,
26215 decl
= make_unbound_class_template (parser
->scope
,
26217 /*complain=*/tf_error
);
26219 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26220 parser
->scope
, name
,
26223 parser
->qualifying_scope
= parser
->scope
;
26224 parser
->object_scope
= NULL_TREE
;
26226 else if (object_type
)
26228 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26229 OBJECT_TYPE is not a class. */
26230 if (CLASS_TYPE_P (object_type
))
26231 /* If the OBJECT_TYPE is a template specialization, it may
26232 be instantiated during name lookup. In that case, errors
26233 may be issued. Even if we rollback the current tentative
26234 parse, those errors are valid. */
26235 decl
= lookup_member (object_type
,
26238 prefer_type_arg (tag_type
),
26239 tf_warning_or_error
);
26244 /* Look it up in the enclosing context. DR 141: When looking for a
26245 template-name after -> or ., only consider class templates. */
26246 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26248 /*block_p=*/true, is_namespace
, 0);
26249 if (object_type
== unknown_type_node
)
26250 /* The object is type-dependent, so we can't look anything up; we used
26251 this to get the DR 141 behavior. */
26252 object_type
= NULL_TREE
;
26253 parser
->object_scope
= object_type
;
26254 parser
->qualifying_scope
= NULL_TREE
;
26258 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26260 /*block_p=*/true, is_namespace
, 0);
26261 parser
->qualifying_scope
= NULL_TREE
;
26262 parser
->object_scope
= NULL_TREE
;
26265 /* If the lookup failed, let our caller know. */
26266 if (!decl
|| decl
== error_mark_node
)
26267 return error_mark_node
;
26269 /* Pull out the template from an injected-class-name (or multiple). */
26271 decl
= maybe_get_template_decl_from_type_decl (decl
);
26273 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26274 if (TREE_CODE (decl
) == TREE_LIST
)
26276 if (ambiguous_decls
)
26277 *ambiguous_decls
= decl
;
26278 /* The error message we have to print is too complicated for
26279 cp_parser_error, so we incorporate its actions directly. */
26280 if (!cp_parser_simulate_error (parser
))
26282 error_at (name_location
, "reference to %qD is ambiguous",
26284 print_candidates (decl
);
26286 return error_mark_node
;
26289 gcc_assert (DECL_P (decl
)
26290 || TREE_CODE (decl
) == OVERLOAD
26291 || TREE_CODE (decl
) == SCOPE_REF
26292 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26293 || BASELINK_P (decl
));
26295 /* If we have resolved the name of a member declaration, check to
26296 see if the declaration is accessible. When the name resolves to
26297 set of overloaded functions, accessibility is checked when
26298 overload resolution is done.
26300 During an explicit instantiation, access is not checked at all,
26301 as per [temp.explicit]. */
26303 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26305 maybe_record_typedef_use (decl
);
26307 return cp_expr (decl
, name_location
);
26310 /* Like cp_parser_lookup_name, but for use in the typical case where
26311 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26312 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26315 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26317 return cp_parser_lookup_name (parser
, name
,
26319 /*is_template=*/false,
26320 /*is_namespace=*/false,
26321 /*check_dependency=*/true,
26322 /*ambiguous_decls=*/NULL
,
26326 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26327 the current context, return the TYPE_DECL. If TAG_NAME_P is
26328 true, the DECL indicates the class being defined in a class-head,
26329 or declared in an elaborated-type-specifier.
26331 Otherwise, return DECL. */
26334 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26336 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26337 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26340 template <typename T> struct B;
26343 template <typename T> struct A::B {};
26345 Similarly, in an elaborated-type-specifier:
26347 namespace N { struct X{}; }
26350 template <typename T> friend struct N::X;
26353 However, if the DECL refers to a class type, and we are in
26354 the scope of the class, then the name lookup automatically
26355 finds the TYPE_DECL created by build_self_reference rather
26356 than a TEMPLATE_DECL. For example, in:
26358 template <class T> struct S {
26362 there is no need to handle such case. */
26364 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26365 return DECL_TEMPLATE_RESULT (decl
);
26370 /* If too many, or too few, template-parameter lists apply to the
26371 declarator, issue an error message. Returns TRUE if all went well,
26372 and FALSE otherwise. */
26375 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26376 cp_declarator
*declarator
,
26377 location_t declarator_location
)
26379 switch (declarator
->kind
)
26383 unsigned num_templates
= 0;
26384 tree scope
= declarator
->u
.id
.qualifying_scope
;
26387 num_templates
= num_template_headers_for_class (scope
);
26388 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26389 == TEMPLATE_ID_EXPR
)
26390 /* If the DECLARATOR has the form `X<y>' then it uses one
26391 additional level of template parameters. */
26394 return cp_parser_check_template_parameters
26395 (parser
, num_templates
, declarator_location
, declarator
);
26401 case cdk_reference
:
26403 return (cp_parser_check_declarator_template_parameters
26404 (parser
, declarator
->declarator
, declarator_location
));
26411 gcc_unreachable ();
26416 /* NUM_TEMPLATES were used in the current declaration. If that is
26417 invalid, return FALSE and issue an error messages. Otherwise,
26418 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26419 declarator and we can print more accurate diagnostics. */
26422 cp_parser_check_template_parameters (cp_parser
* parser
,
26423 unsigned num_templates
,
26424 location_t location
,
26425 cp_declarator
*declarator
)
26427 /* If there are the same number of template classes and parameter
26428 lists, that's OK. */
26429 if (parser
->num_template_parameter_lists
== num_templates
)
26431 /* If there are more, but only one more, then we are referring to a
26432 member template. That's OK too. */
26433 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26435 /* If there are more template classes than parameter lists, we have
26438 template <class T> void S<T>::R<T>::f (); */
26439 if (parser
->num_template_parameter_lists
< num_templates
)
26441 if (declarator
&& !current_function_decl
)
26442 error_at (location
, "specializing member %<%T::%E%> "
26443 "requires %<template<>%> syntax",
26444 declarator
->u
.id
.qualifying_scope
,
26445 declarator
->u
.id
.unqualified_name
);
26446 else if (declarator
)
26447 error_at (location
, "invalid declaration of %<%T::%E%>",
26448 declarator
->u
.id
.qualifying_scope
,
26449 declarator
->u
.id
.unqualified_name
);
26451 error_at (location
, "too few template-parameter-lists");
26454 /* Otherwise, there are too many template parameter lists. We have
26457 template <class T> template <class U> void S::f(); */
26458 error_at (location
, "too many template-parameter-lists");
26462 /* Parse an optional `::' token indicating that the following name is
26463 from the global namespace. If so, PARSER->SCOPE is set to the
26464 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26465 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26466 Returns the new value of PARSER->SCOPE, if the `::' token is
26467 present, and NULL_TREE otherwise. */
26470 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26474 /* Peek at the next token. */
26475 token
= cp_lexer_peek_token (parser
->lexer
);
26476 /* If we're looking at a `::' token then we're starting from the
26477 global namespace, not our current location. */
26478 if (token
->type
== CPP_SCOPE
)
26480 /* Consume the `::' token. */
26481 cp_lexer_consume_token (parser
->lexer
);
26482 /* Set the SCOPE so that we know where to start the lookup. */
26483 parser
->scope
= global_namespace
;
26484 parser
->qualifying_scope
= global_namespace
;
26485 parser
->object_scope
= NULL_TREE
;
26487 return parser
->scope
;
26489 else if (!current_scope_valid_p
)
26491 parser
->scope
= NULL_TREE
;
26492 parser
->qualifying_scope
= NULL_TREE
;
26493 parser
->object_scope
= NULL_TREE
;
26499 /* Returns TRUE if the upcoming token sequence is the start of a
26500 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26501 declarator is preceded by the `friend' specifier. */
26504 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26506 bool constructor_p
;
26507 bool outside_class_specifier_p
;
26508 tree nested_name_specifier
;
26509 cp_token
*next_token
;
26511 /* The common case is that this is not a constructor declarator, so
26512 try to avoid doing lots of work if at all possible. It's not
26513 valid declare a constructor at function scope. */
26514 if (parser
->in_function_body
)
26516 /* And only certain tokens can begin a constructor declarator. */
26517 next_token
= cp_lexer_peek_token (parser
->lexer
);
26518 if (next_token
->type
!= CPP_NAME
26519 && next_token
->type
!= CPP_SCOPE
26520 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26521 && next_token
->type
!= CPP_TEMPLATE_ID
)
26524 /* Parse tentatively; we are going to roll back all of the tokens
26526 cp_parser_parse_tentatively (parser
);
26527 /* Assume that we are looking at a constructor declarator. */
26528 constructor_p
= true;
26530 /* Look for the optional `::' operator. */
26531 cp_parser_global_scope_opt (parser
,
26532 /*current_scope_valid_p=*/false);
26533 /* Look for the nested-name-specifier. */
26534 nested_name_specifier
26535 = (cp_parser_nested_name_specifier_opt (parser
,
26536 /*typename_keyword_p=*/false,
26537 /*check_dependency_p=*/false,
26539 /*is_declaration=*/false));
26541 outside_class_specifier_p
= (!at_class_scope_p ()
26542 || !TYPE_BEING_DEFINED (current_class_type
)
26545 /* Outside of a class-specifier, there must be a
26546 nested-name-specifier. Except in C++17 mode, where we
26547 might be declaring a guiding declaration. */
26548 if (!nested_name_specifier
&& outside_class_specifier_p
26549 && cxx_dialect
< cxx17
)
26550 constructor_p
= false;
26551 else if (nested_name_specifier
== error_mark_node
)
26552 constructor_p
= false;
26554 /* If we have a class scope, this is easy; DR 147 says that S::S always
26555 names the constructor, and no other qualified name could. */
26556 if (constructor_p
&& nested_name_specifier
26557 && CLASS_TYPE_P (nested_name_specifier
))
26559 tree id
= cp_parser_unqualified_id (parser
,
26560 /*template_keyword_p=*/false,
26561 /*check_dependency_p=*/false,
26562 /*declarator_p=*/true,
26563 /*optional_p=*/false);
26564 if (is_overloaded_fn (id
))
26565 id
= DECL_NAME (get_first_fn (id
));
26566 if (!constructor_name_p (id
, nested_name_specifier
))
26567 constructor_p
= false;
26569 /* If we still think that this might be a constructor-declarator,
26570 look for a class-name. */
26571 else if (constructor_p
)
26575 template <typename T> struct S {
26579 we must recognize that the nested `S' names a class. */
26580 if (cxx_dialect
>= cxx17
)
26581 cp_parser_parse_tentatively (parser
);
26584 type_decl
= cp_parser_class_name (parser
,
26585 /*typename_keyword_p=*/false,
26586 /*template_keyword_p=*/false,
26588 /*check_dependency_p=*/false,
26589 /*class_head_p=*/false,
26590 /*is_declaration=*/false);
26592 if (cxx_dialect
>= cxx17
26593 && !cp_parser_parse_definitely (parser
))
26595 type_decl
= NULL_TREE
;
26596 tree tmpl
= cp_parser_template_name (parser
,
26597 /*template_keyword*/false,
26598 /*check_dependency_p*/false,
26599 /*is_declaration*/false,
26601 /*is_identifier*/NULL
);
26602 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26603 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26604 /* It's a deduction guide, return true. */;
26606 cp_parser_simulate_error (parser
);
26609 /* If there was no class-name, then this is not a constructor.
26610 Otherwise, if we are in a class-specifier and we aren't
26611 handling a friend declaration, check that its type matches
26612 current_class_type (c++/38313). Note: error_mark_node
26613 is left alone for error recovery purposes. */
26614 constructor_p
= (!cp_parser_error_occurred (parser
)
26615 && (outside_class_specifier_p
26616 || type_decl
== NULL_TREE
26617 || type_decl
== error_mark_node
26618 || same_type_p (current_class_type
,
26619 TREE_TYPE (type_decl
))));
26621 /* If we're still considering a constructor, we have to see a `(',
26622 to begin the parameter-declaration-clause, followed by either a
26623 `)', an `...', or a decl-specifier. We need to check for a
26624 type-specifier to avoid being fooled into thinking that:
26628 is a constructor. (It is actually a function named `f' that
26629 takes one parameter (of type `int') and returns a value of type
26632 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26633 constructor_p
= false;
26636 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26637 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26638 /* A parameter declaration begins with a decl-specifier,
26639 which is either the "attribute" keyword, a storage class
26640 specifier, or (usually) a type-specifier. */
26641 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26644 tree pushed_scope
= NULL_TREE
;
26645 unsigned saved_num_template_parameter_lists
;
26647 /* Names appearing in the type-specifier should be looked up
26648 in the scope of the class. */
26649 if (current_class_type
)
26651 else if (type_decl
)
26653 type
= TREE_TYPE (type_decl
);
26654 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26656 type
= resolve_typename_type (type
,
26657 /*only_current_p=*/false);
26658 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26660 cp_parser_abort_tentative_parse (parser
);
26664 pushed_scope
= push_scope (type
);
26667 /* Inside the constructor parameter list, surrounding
26668 template-parameter-lists do not apply. */
26669 saved_num_template_parameter_lists
26670 = parser
->num_template_parameter_lists
;
26671 parser
->num_template_parameter_lists
= 0;
26673 /* Look for the type-specifier. */
26674 cp_parser_type_specifier (parser
,
26675 CP_PARSER_FLAGS_NONE
,
26676 /*decl_specs=*/NULL
,
26677 /*is_declarator=*/true,
26678 /*declares_class_or_enum=*/NULL
,
26679 /*is_cv_qualifier=*/NULL
);
26681 parser
->num_template_parameter_lists
26682 = saved_num_template_parameter_lists
;
26684 /* Leave the scope of the class. */
26686 pop_scope (pushed_scope
);
26688 constructor_p
= !cp_parser_error_occurred (parser
);
26692 /* We did not really want to consume any tokens. */
26693 cp_parser_abort_tentative_parse (parser
);
26695 return constructor_p
;
26698 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26699 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26700 they must be performed once we are in the scope of the function.
26702 Returns the function defined. */
26705 cp_parser_function_definition_from_specifiers_and_declarator
26706 (cp_parser
* parser
,
26707 cp_decl_specifier_seq
*decl_specifiers
,
26709 const cp_declarator
*declarator
)
26714 /* Begin the function-definition. */
26715 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26717 /* The things we're about to see are not directly qualified by any
26718 template headers we've seen thus far. */
26719 reset_specialization ();
26721 /* If there were names looked up in the decl-specifier-seq that we
26722 did not check, check them now. We must wait until we are in the
26723 scope of the function to perform the checks, since the function
26724 might be a friend. */
26725 perform_deferred_access_checks (tf_warning_or_error
);
26729 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26730 parser
->omp_declare_simd
= NULL
;
26731 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26732 parser
->oacc_routine
= NULL
;
26737 /* Skip the entire function. */
26738 cp_parser_skip_to_end_of_block_or_statement (parser
);
26739 fn
= error_mark_node
;
26741 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26743 /* Seen already, skip it. An error message has already been output. */
26744 cp_parser_skip_to_end_of_block_or_statement (parser
);
26745 fn
= current_function_decl
;
26746 current_function_decl
= NULL_TREE
;
26747 /* If this is a function from a class, pop the nested class. */
26748 if (current_class_name
)
26749 pop_nested_class ();
26754 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26755 tv
= TV_PARSE_INLINE
;
26757 tv
= TV_PARSE_FUNC
;
26759 fn
= cp_parser_function_definition_after_declarator (parser
,
26760 /*inline_p=*/false);
26767 /* Parse the part of a function-definition that follows the
26768 declarator. INLINE_P is TRUE iff this function is an inline
26769 function defined within a class-specifier.
26771 Returns the function defined. */
26774 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26778 bool saved_in_unbraced_linkage_specification_p
;
26779 bool saved_in_function_body
;
26780 unsigned saved_num_template_parameter_lists
;
26782 bool fully_implicit_function_template_p
26783 = parser
->fully_implicit_function_template_p
;
26784 parser
->fully_implicit_function_template_p
= false;
26785 tree implicit_template_parms
26786 = parser
->implicit_template_parms
;
26787 parser
->implicit_template_parms
= 0;
26788 cp_binding_level
* implicit_template_scope
26789 = parser
->implicit_template_scope
;
26790 parser
->implicit_template_scope
= 0;
26792 saved_in_function_body
= parser
->in_function_body
;
26793 parser
->in_function_body
= true;
26794 /* If the next token is `return', then the code may be trying to
26795 make use of the "named return value" extension that G++ used to
26797 token
= cp_lexer_peek_token (parser
->lexer
);
26798 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26800 /* Consume the `return' keyword. */
26801 cp_lexer_consume_token (parser
->lexer
);
26802 /* Look for the identifier that indicates what value is to be
26804 cp_parser_identifier (parser
);
26805 /* Issue an error message. */
26806 error_at (token
->location
,
26807 "named return values are no longer supported");
26808 /* Skip tokens until we reach the start of the function body. */
26811 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26812 if (token
->type
== CPP_OPEN_BRACE
26813 || token
->type
== CPP_EOF
26814 || token
->type
== CPP_PRAGMA_EOL
)
26816 cp_lexer_consume_token (parser
->lexer
);
26819 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26820 anything declared inside `f'. */
26821 saved_in_unbraced_linkage_specification_p
26822 = parser
->in_unbraced_linkage_specification_p
;
26823 parser
->in_unbraced_linkage_specification_p
= false;
26824 /* Inside the function, surrounding template-parameter-lists do not
26826 saved_num_template_parameter_lists
26827 = parser
->num_template_parameter_lists
;
26828 parser
->num_template_parameter_lists
= 0;
26830 /* If the next token is `try', `__transaction_atomic', or
26831 `__transaction_relaxed`, then we are looking at either function-try-block
26832 or function-transaction-block. Note that all of these include the
26834 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26835 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26836 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26837 RID_TRANSACTION_RELAXED
))
26838 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26839 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26840 cp_parser_function_try_block (parser
);
26842 cp_parser_ctor_initializer_opt_and_function_body
26843 (parser
, /*in_function_try_block=*/false);
26845 /* Finish the function. */
26846 fn
= finish_function (inline_p
);
26847 /* Generate code for it, if necessary. */
26848 expand_or_defer_fn (fn
);
26849 /* Restore the saved values. */
26850 parser
->in_unbraced_linkage_specification_p
26851 = saved_in_unbraced_linkage_specification_p
;
26852 parser
->num_template_parameter_lists
26853 = saved_num_template_parameter_lists
;
26854 parser
->in_function_body
= saved_in_function_body
;
26856 parser
->fully_implicit_function_template_p
26857 = fully_implicit_function_template_p
;
26858 parser
->implicit_template_parms
26859 = implicit_template_parms
;
26860 parser
->implicit_template_scope
26861 = implicit_template_scope
;
26863 if (parser
->fully_implicit_function_template_p
)
26864 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26869 /* Parse a template-declaration body (following argument list). */
26872 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26873 tree parameter_list
,
26876 tree decl
= NULL_TREE
;
26877 bool friend_p
= false;
26879 /* We just processed one more parameter list. */
26880 ++parser
->num_template_parameter_lists
;
26882 /* Get the deferred access checks from the parameter list. These
26883 will be checked once we know what is being declared, as for a
26884 member template the checks must be performed in the scope of the
26885 class containing the member. */
26886 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26888 /* Tentatively parse for a new template parameter list, which can either be
26889 the template keyword or a template introduction. */
26890 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26892 else if (cxx_dialect
>= cxx11
26893 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26894 decl
= cp_parser_alias_declaration (parser
);
26897 /* There are no access checks when parsing a template, as we do not
26898 know if a specialization will be a friend. */
26899 push_deferring_access_checks (dk_no_check
);
26900 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26901 decl
= cp_parser_single_declaration (parser
,
26904 /*explicit_specialization_p=*/false,
26906 pop_deferring_access_checks ();
26908 /* If this is a member template declaration, let the front
26910 if (member_p
&& !friend_p
&& decl
)
26912 if (TREE_CODE (decl
) == TYPE_DECL
)
26913 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26915 decl
= finish_member_template_decl (decl
);
26917 else if (friend_p
&& decl
26918 && DECL_DECLARES_TYPE_P (decl
))
26919 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26920 /*complain=*/true);
26922 /* We are done with the current parameter list. */
26923 --parser
->num_template_parameter_lists
;
26925 pop_deferring_access_checks ();
26928 finish_template_decl (parameter_list
);
26930 /* Check the template arguments for a literal operator template. */
26932 && DECL_DECLARES_FUNCTION_P (decl
)
26933 && UDLIT_OPER_P (DECL_NAME (decl
)))
26936 if (parameter_list
== NULL_TREE
)
26940 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26941 if (num_parms
== 1)
26943 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26944 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26945 if (TREE_TYPE (parm
) != char_type_node
26946 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26949 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26951 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26952 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26953 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26954 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26955 if (parm
== error_mark_node
26956 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26957 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26965 if (cxx_dialect
>= cxx14
)
26966 error ("literal operator template %qD has invalid parameter list."
26967 " Expected non-type template argument pack <char...>"
26968 " or <typename CharT, CharT...>",
26971 error ("literal operator template %qD has invalid parameter list."
26972 " Expected non-type template argument pack <char...>",
26977 /* Register member declarations. */
26978 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26979 finish_member_declaration (decl
);
26980 /* If DECL is a function template, we must return to parse it later.
26981 (Even though there is no definition, there might be default
26982 arguments that need handling.) */
26983 if (member_p
&& decl
26984 && DECL_DECLARES_FUNCTION_P (decl
))
26985 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26988 /* Parse a template introduction header for a template-declaration. Returns
26989 false if tentative parse fails. */
26992 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26994 cp_parser_parse_tentatively (parser
);
26996 tree saved_scope
= parser
->scope
;
26997 tree saved_object_scope
= parser
->object_scope
;
26998 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27000 /* Look for the optional `::' operator. */
27001 cp_parser_global_scope_opt (parser
,
27002 /*current_scope_valid_p=*/false);
27003 /* Look for the nested-name-specifier. */
27004 cp_parser_nested_name_specifier_opt (parser
,
27005 /*typename_keyword_p=*/false,
27006 /*check_dependency_p=*/true,
27008 /*is_declaration=*/false);
27010 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27011 tree concept_name
= cp_parser_identifier (parser
);
27013 /* Look up the concept for which we will be matching
27014 template parameters. */
27015 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27017 parser
->scope
= saved_scope
;
27018 parser
->object_scope
= saved_object_scope
;
27019 parser
->qualifying_scope
= saved_qualifying_scope
;
27021 if (concept_name
== error_mark_node
)
27022 cp_parser_simulate_error (parser
);
27024 /* Look for opening brace for introduction. */
27025 matching_braces braces
;
27026 braces
.require_open (parser
);
27028 if (!cp_parser_parse_definitely (parser
))
27031 push_deferring_access_checks (dk_deferred
);
27033 /* Build vector of placeholder parameters and grab
27034 matching identifiers. */
27035 tree introduction_list
= cp_parser_introduction_list (parser
);
27037 /* The introduction-list shall not be empty. */
27038 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27041 error ("empty introduction-list");
27045 /* Look for closing brace for introduction. */
27046 if (!braces
.require_close (parser
))
27049 if (tmpl_decl
== error_mark_node
)
27051 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27056 /* Build and associate the constraint. */
27057 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27058 if (parms
&& parms
!= error_mark_node
)
27060 cp_parser_template_declaration_after_parameters (parser
, parms
,
27065 error_at (token
->location
, "no matching concept for template-introduction");
27069 /* Parse a normal template-declaration following the template keyword. */
27072 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27074 tree parameter_list
;
27075 bool need_lang_pop
;
27076 location_t location
= input_location
;
27078 /* Look for the `<' token. */
27079 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27081 if (at_class_scope_p () && current_function_decl
)
27083 /* 14.5.2.2 [temp.mem]
27085 A local class shall not have member templates. */
27086 error_at (location
,
27087 "invalid declaration of member template in local class");
27088 cp_parser_skip_to_end_of_block_or_statement (parser
);
27093 A template ... shall not have C linkage. */
27094 if (current_lang_name
== lang_name_c
)
27096 error_at (location
, "template with C linkage");
27097 maybe_show_extern_c_location ();
27098 /* Give it C++ linkage to avoid confusing other parts of the
27100 push_lang_context (lang_name_cplusplus
);
27101 need_lang_pop
= true;
27104 need_lang_pop
= false;
27106 /* We cannot perform access checks on the template parameter
27107 declarations until we know what is being declared, just as we
27108 cannot check the decl-specifier list. */
27109 push_deferring_access_checks (dk_deferred
);
27111 /* If the next token is `>', then we have an invalid
27112 specialization. Rather than complain about an invalid template
27113 parameter, issue an error message here. */
27114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27116 cp_parser_error (parser
, "invalid explicit specialization");
27117 begin_specialization ();
27118 parameter_list
= NULL_TREE
;
27122 /* Parse the template parameters. */
27123 parameter_list
= cp_parser_template_parameter_list (parser
);
27126 /* Look for the `>'. */
27127 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27129 /* Manage template requirements */
27132 tree reqs
= get_shorthand_constraints (current_template_parms
);
27133 if (tree r
= cp_parser_requires_clause_opt (parser
))
27134 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27135 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27138 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27141 /* For the erroneous case of a template with C linkage, we pushed an
27142 implicit C++ linkage scope; exit that scope now. */
27144 pop_lang_context ();
27147 /* Parse a template-declaration, assuming that the `export' (and
27148 `extern') keywords, if present, has already been scanned. MEMBER_P
27149 is as for cp_parser_template_declaration. */
27152 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27154 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27156 cp_lexer_consume_token (parser
->lexer
);
27157 cp_parser_explicit_template_declaration (parser
, member_p
);
27160 else if (flag_concepts
)
27161 return cp_parser_template_introduction (parser
, member_p
);
27166 /* Perform the deferred access checks from a template-parameter-list.
27167 CHECKS is a TREE_LIST of access checks, as returned by
27168 get_deferred_access_checks. */
27171 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27173 ++processing_template_parmlist
;
27174 perform_access_checks (checks
, tf_warning_or_error
);
27175 --processing_template_parmlist
;
27178 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27179 `function-definition' sequence that follows a template header.
27180 If MEMBER_P is true, this declaration appears in a class scope.
27182 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27183 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27186 cp_parser_single_declaration (cp_parser
* parser
,
27187 vec
<deferred_access_check
, va_gc
> *checks
,
27189 bool explicit_specialization_p
,
27192 int declares_class_or_enum
;
27193 tree decl
= NULL_TREE
;
27194 cp_decl_specifier_seq decl_specifiers
;
27195 bool function_definition_p
= false;
27196 cp_token
*decl_spec_token_start
;
27198 /* This function is only used when processing a template
27200 gcc_assert (innermost_scope_kind () == sk_template_parms
27201 || innermost_scope_kind () == sk_template_spec
);
27203 /* Defer access checks until we know what is being declared. */
27204 push_deferring_access_checks (dk_deferred
);
27206 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27208 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27209 cp_parser_decl_specifier_seq (parser
,
27210 CP_PARSER_FLAGS_OPTIONAL
,
27212 &declares_class_or_enum
);
27214 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27216 /* There are no template typedefs. */
27217 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27219 error_at (decl_spec_token_start
->location
,
27220 "template declaration of %<typedef%>");
27221 decl
= error_mark_node
;
27224 /* Gather up the access checks that occurred the
27225 decl-specifier-seq. */
27226 stop_deferring_access_checks ();
27228 /* Check for the declaration of a template class. */
27229 if (declares_class_or_enum
)
27231 if (cp_parser_declares_only_class_p (parser
)
27232 || (declares_class_or_enum
& 2))
27234 // If this is a declaration, but not a definition, associate
27235 // any constraints with the type declaration. Constraints
27236 // are associated with definitions in cp_parser_class_specifier.
27237 if (declares_class_or_enum
== 1)
27238 associate_classtype_constraints (decl_specifiers
.type
);
27240 decl
= shadow_tag (&decl_specifiers
);
27245 friend template <typename T> struct A<T>::B;
27248 A<T>::B will be represented by a TYPENAME_TYPE, and
27249 therefore not recognized by shadow_tag. */
27250 if (friend_p
&& *friend_p
27252 && decl_specifiers
.type
27253 && TYPE_P (decl_specifiers
.type
))
27254 decl
= decl_specifiers
.type
;
27256 if (decl
&& decl
!= error_mark_node
)
27257 decl
= TYPE_NAME (decl
);
27259 decl
= error_mark_node
;
27261 /* Perform access checks for template parameters. */
27262 cp_parser_perform_template_parameter_access_checks (checks
);
27264 /* Give a helpful diagnostic for
27265 template <class T> struct A { } a;
27266 if we aren't already recovering from an error. */
27267 if (!cp_parser_declares_only_class_p (parser
)
27270 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27271 "a class template declaration must not declare "
27273 cp_parser_skip_to_end_of_block_or_statement (parser
);
27279 /* Complain about missing 'typename' or other invalid type names. */
27280 if (!decl_specifiers
.any_type_specifiers_p
27281 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27283 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27284 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27285 the rest of this declaration. */
27286 decl
= error_mark_node
;
27290 /* If it's not a template class, try for a template function. If
27291 the next token is a `;', then this declaration does not declare
27292 anything. But, if there were errors in the decl-specifiers, then
27293 the error might well have come from an attempted class-specifier.
27294 In that case, there's no need to warn about a missing declarator. */
27296 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27297 || decl_specifiers
.type
!= error_mark_node
))
27299 decl
= cp_parser_init_declarator (parser
,
27302 /*function_definition_allowed_p=*/true,
27304 declares_class_or_enum
,
27305 &function_definition_p
,
27308 /* 7.1.1-1 [dcl.stc]
27310 A storage-class-specifier shall not be specified in an explicit
27311 specialization... */
27313 && explicit_specialization_p
27314 && decl_specifiers
.storage_class
!= sc_none
)
27316 error_at (decl_spec_token_start
->location
,
27317 "explicit template specialization cannot have a storage class");
27318 decl
= error_mark_node
;
27321 if (decl
&& VAR_P (decl
))
27322 check_template_variable (decl
);
27325 /* Look for a trailing `;' after the declaration. */
27326 if (!function_definition_p
27327 && (decl
== error_mark_node
27328 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27329 cp_parser_skip_to_end_of_block_or_statement (parser
);
27332 pop_deferring_access_checks ();
27334 /* Clear any current qualification; whatever comes next is the start
27335 of something new. */
27336 parser
->scope
= NULL_TREE
;
27337 parser
->qualifying_scope
= NULL_TREE
;
27338 parser
->object_scope
= NULL_TREE
;
27343 /* Parse a cast-expression that is not the operand of a unary "&". */
27346 cp_parser_simple_cast_expression (cp_parser
*parser
)
27348 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27349 /*cast_p=*/false, /*decltype*/false, NULL
);
27352 /* Parse a functional cast to TYPE. Returns an expression
27353 representing the cast. */
27356 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27358 vec
<tree
, va_gc
> *vec
;
27359 tree expression_list
;
27363 location_t start_loc
= input_location
;
27366 type
= error_mark_node
;
27368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27370 cp_lexer_set_source_position (parser
->lexer
);
27371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27372 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27373 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27374 if (TREE_CODE (type
) == TYPE_DECL
)
27375 type
= TREE_TYPE (type
);
27377 cast
= finish_compound_literal (type
, expression_list
,
27378 tf_warning_or_error
, fcl_functional
);
27379 /* Create a location of the form:
27382 with caret == start at the start of the type name,
27383 finishing at the closing brace. */
27384 location_t finish_loc
27385 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27386 location_t combined_loc
= make_location (start_loc
, start_loc
,
27388 cast
.set_location (combined_loc
);
27393 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27395 /*allow_expansion_p=*/true,
27396 /*non_constant_p=*/NULL
);
27398 expression_list
= error_mark_node
;
27401 expression_list
= build_tree_list_vec (vec
);
27402 release_tree_vector (vec
);
27405 cast
= build_functional_cast (type
, expression_list
,
27406 tf_warning_or_error
);
27407 /* [expr.const]/1: In an integral constant expression "only type
27408 conversions to integral or enumeration type can be used". */
27409 if (TREE_CODE (type
) == TYPE_DECL
)
27410 type
= TREE_TYPE (type
);
27411 if (cast
!= error_mark_node
27412 && !cast_valid_in_integral_constant_expression_p (type
)
27413 && cp_parser_non_integral_constant_expression (parser
,
27415 return error_mark_node
;
27417 /* Create a location of the form:
27420 with caret == start at the start of the type name,
27421 finishing at the closing paren. */
27422 location_t finish_loc
27423 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27424 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27425 cast
.set_location (combined_loc
);
27429 /* Save the tokens that make up the body of a member function defined
27430 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27431 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27432 specifiers applied to the declaration. Returns the FUNCTION_DECL
27433 for the member function. */
27436 cp_parser_save_member_function_body (cp_parser
* parser
,
27437 cp_decl_specifier_seq
*decl_specifiers
,
27438 cp_declarator
*declarator
,
27444 bool function_try_block
= false;
27446 /* Create the FUNCTION_DECL. */
27447 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27448 cp_finalize_omp_declare_simd (parser
, fn
);
27449 cp_finalize_oacc_routine (parser
, fn
, true);
27450 /* If something went badly wrong, bail out now. */
27451 if (fn
== error_mark_node
)
27453 /* If there's a function-body, skip it. */
27454 if (cp_parser_token_starts_function_definition_p
27455 (cp_lexer_peek_token (parser
->lexer
)))
27456 cp_parser_skip_to_end_of_block_or_statement (parser
);
27457 return error_mark_node
;
27460 /* Remember it, if there default args to post process. */
27461 cp_parser_save_default_args (parser
, fn
);
27463 /* Save away the tokens that make up the body of the
27465 first
= parser
->lexer
->next_token
;
27467 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27468 cp_lexer_consume_token (parser
->lexer
);
27469 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27470 RID_TRANSACTION_ATOMIC
))
27472 cp_lexer_consume_token (parser
->lexer
);
27473 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27475 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27476 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27477 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27478 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27479 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27481 cp_lexer_consume_token (parser
->lexer
);
27482 cp_lexer_consume_token (parser
->lexer
);
27483 cp_lexer_consume_token (parser
->lexer
);
27484 cp_lexer_consume_token (parser
->lexer
);
27485 cp_lexer_consume_token (parser
->lexer
);
27488 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27489 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27491 cp_lexer_consume_token (parser
->lexer
);
27492 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27497 /* Handle function try blocks. */
27498 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27500 cp_lexer_consume_token (parser
->lexer
);
27501 function_try_block
= true;
27503 /* We can have braced-init-list mem-initializers before the fn body. */
27504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27506 cp_lexer_consume_token (parser
->lexer
);
27507 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27509 /* cache_group will stop after an un-nested { } pair, too. */
27510 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27513 /* variadic mem-inits have ... after the ')'. */
27514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27515 cp_lexer_consume_token (parser
->lexer
);
27518 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27519 /* Handle function try blocks. */
27520 if (function_try_block
)
27521 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27522 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27523 last
= parser
->lexer
->next_token
;
27525 /* Save away the inline definition; we will process it when the
27526 class is complete. */
27527 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27528 DECL_PENDING_INLINE_P (fn
) = 1;
27530 /* We need to know that this was defined in the class, so that
27531 friend templates are handled correctly. */
27532 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27534 /* Add FN to the queue of functions to be parsed later. */
27535 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27540 /* Save the tokens that make up the in-class initializer for a non-static
27541 data member. Returns a DEFAULT_ARG. */
27544 cp_parser_save_nsdmi (cp_parser
* parser
)
27546 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27549 /* Parse a template-argument-list, as well as the trailing ">" (but
27550 not the opening "<"). See cp_parser_template_argument_list for the
27554 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27558 tree saved_qualifying_scope
;
27559 tree saved_object_scope
;
27560 bool saved_greater_than_is_operator_p
;
27561 int saved_unevaluated_operand
;
27562 int saved_inhibit_evaluation_warnings
;
27566 When parsing a template-id, the first non-nested `>' is taken as
27567 the end of the template-argument-list rather than a greater-than
27569 saved_greater_than_is_operator_p
27570 = parser
->greater_than_is_operator_p
;
27571 parser
->greater_than_is_operator_p
= false;
27572 /* Parsing the argument list may modify SCOPE, so we save it
27574 saved_scope
= parser
->scope
;
27575 saved_qualifying_scope
= parser
->qualifying_scope
;
27576 saved_object_scope
= parser
->object_scope
;
27577 /* We need to evaluate the template arguments, even though this
27578 template-id may be nested within a "sizeof". */
27579 saved_unevaluated_operand
= cp_unevaluated_operand
;
27580 cp_unevaluated_operand
= 0;
27581 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27582 c_inhibit_evaluation_warnings
= 0;
27583 /* Parse the template-argument-list itself. */
27584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27585 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27586 arguments
= NULL_TREE
;
27588 arguments
= cp_parser_template_argument_list (parser
);
27589 /* Look for the `>' that ends the template-argument-list. If we find
27590 a '>>' instead, it's probably just a typo. */
27591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27593 if (cxx_dialect
!= cxx98
)
27595 /* In C++0x, a `>>' in a template argument list or cast
27596 expression is considered to be two separate `>'
27597 tokens. So, change the current token to a `>', but don't
27598 consume it: it will be consumed later when the outer
27599 template argument list (or cast expression) is parsed.
27600 Note that this replacement of `>' for `>>' is necessary
27601 even if we are parsing tentatively: in the tentative
27602 case, after calling
27603 cp_parser_enclosed_template_argument_list we will always
27604 throw away all of the template arguments and the first
27605 closing `>', either because the template argument list
27606 was erroneous or because we are replacing those tokens
27607 with a CPP_TEMPLATE_ID token. The second `>' (which will
27608 not have been thrown away) is needed either to close an
27609 outer template argument list or to complete a new-style
27611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27612 token
->type
= CPP_GREATER
;
27614 else if (!saved_greater_than_is_operator_p
)
27616 /* If we're in a nested template argument list, the '>>' has
27617 to be a typo for '> >'. We emit the error message, but we
27618 continue parsing and we push a '>' as next token, so that
27619 the argument list will be parsed correctly. Note that the
27620 global source location is still on the token before the
27621 '>>', so we need to say explicitly where we want it. */
27622 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27623 gcc_rich_location
richloc (token
->location
);
27624 richloc
.add_fixit_replace ("> >");
27625 error_at (&richloc
, "%<>>%> should be %<> >%> "
27626 "within a nested template argument list");
27628 token
->type
= CPP_GREATER
;
27632 /* If this is not a nested template argument list, the '>>'
27633 is a typo for '>'. Emit an error message and continue.
27634 Same deal about the token location, but here we can get it
27635 right by consuming the '>>' before issuing the diagnostic. */
27636 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27637 error_at (token
->location
,
27638 "spurious %<>>%>, use %<>%> to terminate "
27639 "a template argument list");
27643 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27644 /* The `>' token might be a greater-than operator again now. */
27645 parser
->greater_than_is_operator_p
27646 = saved_greater_than_is_operator_p
;
27647 /* Restore the SAVED_SCOPE. */
27648 parser
->scope
= saved_scope
;
27649 parser
->qualifying_scope
= saved_qualifying_scope
;
27650 parser
->object_scope
= saved_object_scope
;
27651 cp_unevaluated_operand
= saved_unevaluated_operand
;
27652 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27657 /* MEMBER_FUNCTION is a member function, or a friend. If default
27658 arguments, or the body of the function have not yet been parsed,
27662 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27664 timevar_push (TV_PARSE_INMETH
);
27665 /* If this member is a template, get the underlying
27667 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27668 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27670 /* There should not be any class definitions in progress at this
27671 point; the bodies of members are only parsed outside of all class
27673 gcc_assert (parser
->num_classes_being_defined
== 0);
27674 /* While we're parsing the member functions we might encounter more
27675 classes. We want to handle them right away, but we don't want
27676 them getting mixed up with functions that are currently in the
27678 push_unparsed_function_queues (parser
);
27680 /* Make sure that any template parameters are in scope. */
27681 maybe_begin_member_template_processing (member_function
);
27683 /* If the body of the function has not yet been parsed, parse it
27685 if (DECL_PENDING_INLINE_P (member_function
))
27687 tree function_scope
;
27688 cp_token_cache
*tokens
;
27690 /* The function is no longer pending; we are processing it. */
27691 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27692 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27693 DECL_PENDING_INLINE_P (member_function
) = 0;
27695 /* If this is a local class, enter the scope of the containing
27697 function_scope
= current_function_decl
;
27698 if (function_scope
)
27699 push_function_context ();
27701 /* Push the body of the function onto the lexer stack. */
27702 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27704 /* Let the front end know that we going to be defining this
27706 start_preparsed_function (member_function
, NULL_TREE
,
27707 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27709 /* Don't do access checking if it is a templated function. */
27710 if (processing_template_decl
)
27711 push_deferring_access_checks (dk_no_check
);
27713 /* #pragma omp declare reduction needs special parsing. */
27714 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27716 parser
->lexer
->in_pragma
= true;
27717 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27718 finish_function (/*inline_p=*/true);
27719 cp_check_omp_declare_reduction (member_function
);
27722 /* Now, parse the body of the function. */
27723 cp_parser_function_definition_after_declarator (parser
,
27724 /*inline_p=*/true);
27726 if (processing_template_decl
)
27727 pop_deferring_access_checks ();
27729 /* Leave the scope of the containing function. */
27730 if (function_scope
)
27731 pop_function_context ();
27732 cp_parser_pop_lexer (parser
);
27735 /* Remove any template parameters from the symbol table. */
27736 maybe_end_member_template_processing ();
27738 /* Restore the queue. */
27739 pop_unparsed_function_queues (parser
);
27740 timevar_pop (TV_PARSE_INMETH
);
27743 /* If DECL contains any default args, remember it on the unparsed
27744 functions queue. */
27747 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27751 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27753 probe
= TREE_CHAIN (probe
))
27754 if (TREE_PURPOSE (probe
))
27756 cp_default_arg_entry entry
= {current_class_type
, decl
};
27757 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27762 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27763 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27764 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27765 from the parameter-type-list. */
27768 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27769 tree default_arg
, tree parmtype
)
27771 cp_token_cache
*tokens
;
27775 if (default_arg
== error_mark_node
)
27776 return error_mark_node
;
27778 /* Push the saved tokens for the default argument onto the parser's
27780 tokens
= DEFARG_TOKENS (default_arg
);
27781 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27783 start_lambda_scope (decl
);
27785 /* Parse the default argument. */
27786 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27787 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27788 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27790 finish_lambda_scope ();
27792 if (parsed_arg
== error_mark_node
)
27793 cp_parser_skip_to_end_of_statement (parser
);
27795 if (!processing_template_decl
)
27797 /* In a non-template class, check conversions now. In a template,
27798 we'll wait and instantiate these as needed. */
27799 if (TREE_CODE (decl
) == PARM_DECL
)
27800 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27801 tf_warning_or_error
);
27802 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27803 parsed_arg
= error_mark_node
;
27805 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27808 /* If the token stream has not been completely used up, then
27809 there was extra junk after the end of the default
27811 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27813 if (TREE_CODE (decl
) == PARM_DECL
)
27814 cp_parser_error (parser
, "expected %<,%>");
27816 cp_parser_error (parser
, "expected %<;%>");
27819 /* Revert to the main lexer. */
27820 cp_parser_pop_lexer (parser
);
27825 /* FIELD is a non-static data member with an initializer which we saved for
27826 later; parse it now. */
27829 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27833 maybe_begin_member_template_processing (field
);
27835 push_unparsed_function_queues (parser
);
27836 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27837 DECL_INITIAL (field
),
27839 pop_unparsed_function_queues (parser
);
27841 maybe_end_member_template_processing ();
27843 DECL_INITIAL (field
) = def
;
27846 /* FN is a FUNCTION_DECL which may contains a parameter with an
27847 unparsed DEFAULT_ARG. Parse the default args now. This function
27848 assumes that the current scope is the scope in which the default
27849 argument should be processed. */
27852 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27854 bool saved_local_variables_forbidden_p
;
27855 tree parm
, parmdecl
;
27857 /* While we're parsing the default args, we might (due to the
27858 statement expression extension) encounter more classes. We want
27859 to handle them right away, but we don't want them getting mixed
27860 up with default args that are currently in the queue. */
27861 push_unparsed_function_queues (parser
);
27863 /* Local variable names (and the `this' keyword) may not appear
27864 in a default argument. */
27865 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27866 parser
->local_variables_forbidden_p
= true;
27868 push_defarg_context (fn
);
27870 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27871 parmdecl
= DECL_ARGUMENTS (fn
);
27872 parm
&& parm
!= void_list_node
;
27873 parm
= TREE_CHAIN (parm
),
27874 parmdecl
= DECL_CHAIN (parmdecl
))
27876 tree default_arg
= TREE_PURPOSE (parm
);
27878 vec
<tree
, va_gc
> *insts
;
27885 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27886 /* This can happen for a friend declaration for a function
27887 already declared with default arguments. */
27891 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27893 TREE_VALUE (parm
));
27894 TREE_PURPOSE (parm
) = parsed_arg
;
27896 /* Update any instantiations we've already created. */
27897 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27898 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27899 TREE_PURPOSE (copy
) = parsed_arg
;
27902 pop_defarg_context ();
27904 /* Make sure no default arg is missing. */
27905 check_default_args (fn
);
27907 /* Restore the state of local_variables_forbidden_p. */
27908 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27910 /* Restore the queue. */
27911 pop_unparsed_function_queues (parser
);
27914 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27916 sizeof ... ( identifier )
27918 where the 'sizeof' token has already been consumed. */
27921 cp_parser_sizeof_pack (cp_parser
*parser
)
27923 /* Consume the `...'. */
27924 cp_lexer_consume_token (parser
->lexer
);
27925 maybe_warn_variadic_templates ();
27927 matching_parens parens
;
27928 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27930 parens
.consume_open (parser
);
27932 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27933 "%<sizeof...%> argument must be surrounded by parentheses");
27935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27936 tree name
= cp_parser_identifier (parser
);
27937 if (name
== error_mark_node
)
27938 return error_mark_node
;
27939 /* The name is not qualified. */
27940 parser
->scope
= NULL_TREE
;
27941 parser
->qualifying_scope
= NULL_TREE
;
27942 parser
->object_scope
= NULL_TREE
;
27943 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27944 if (expr
== error_mark_node
)
27945 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27947 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27948 expr
= TREE_TYPE (expr
);
27949 else if (TREE_CODE (expr
) == CONST_DECL
)
27950 expr
= DECL_INITIAL (expr
);
27951 expr
= make_pack_expansion (expr
);
27952 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27955 parens
.require_close (parser
);
27960 /* Parse the operand of `sizeof' (or a similar operator). Returns
27961 either a TYPE or an expression, depending on the form of the
27962 input. The KEYWORD indicates which kind of expression we have
27966 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27968 tree expr
= NULL_TREE
;
27969 const char *saved_message
;
27971 bool saved_integral_constant_expression_p
;
27972 bool saved_non_integral_constant_expression_p
;
27974 /* If it's a `...', then we are computing the length of a parameter
27976 if (keyword
== RID_SIZEOF
27977 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27978 return cp_parser_sizeof_pack (parser
);
27980 /* Types cannot be defined in a `sizeof' expression. Save away the
27982 saved_message
= parser
->type_definition_forbidden_message
;
27983 /* And create the new one. */
27984 tmp
= concat ("types may not be defined in %<",
27985 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27986 "%> expressions", NULL
);
27987 parser
->type_definition_forbidden_message
= tmp
;
27989 /* The restrictions on constant-expressions do not apply inside
27990 sizeof expressions. */
27991 saved_integral_constant_expression_p
27992 = parser
->integral_constant_expression_p
;
27993 saved_non_integral_constant_expression_p
27994 = parser
->non_integral_constant_expression_p
;
27995 parser
->integral_constant_expression_p
= false;
27997 /* Do not actually evaluate the expression. */
27998 ++cp_unevaluated_operand
;
27999 ++c_inhibit_evaluation_warnings
;
28000 /* If it's a `(', then we might be looking at the type-id
28002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28004 tree type
= NULL_TREE
;
28006 /* We can't be sure yet whether we're looking at a type-id or an
28008 cp_parser_parse_tentatively (parser
);
28010 matching_parens parens
;
28011 parens
.consume_open (parser
);
28013 /* Note: as a GNU Extension, compound literals are considered
28014 postfix-expressions as they are in C99, so they are valid
28015 arguments to sizeof. See comment in cp_parser_cast_expression
28017 if (cp_parser_compound_literal_p (parser
))
28018 cp_parser_simulate_error (parser
);
28021 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28022 parser
->in_type_id_in_expr_p
= true;
28023 /* Look for the type-id. */
28024 type
= cp_parser_type_id (parser
);
28025 /* Look for the closing `)'. */
28026 parens
.require_close (parser
);
28027 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28030 /* If all went well, then we're done. */
28031 if (cp_parser_parse_definitely (parser
))
28033 cp_decl_specifier_seq decl_specs
;
28035 /* Build a trivial decl-specifier-seq. */
28036 clear_decl_specs (&decl_specs
);
28037 decl_specs
.type
= type
;
28039 /* Call grokdeclarator to figure out what type this is. */
28040 expr
= grokdeclarator (NULL
,
28044 /*attrlist=*/NULL
);
28048 /* If the type-id production did not work out, then we must be
28049 looking at the unary-expression production. */
28051 expr
= cp_parser_unary_expression (parser
);
28053 /* Go back to evaluating expressions. */
28054 --cp_unevaluated_operand
;
28055 --c_inhibit_evaluation_warnings
;
28057 /* Free the message we created. */
28059 /* And restore the old one. */
28060 parser
->type_definition_forbidden_message
= saved_message
;
28061 parser
->integral_constant_expression_p
28062 = saved_integral_constant_expression_p
;
28063 parser
->non_integral_constant_expression_p
28064 = saved_non_integral_constant_expression_p
;
28069 /* If the current declaration has no declarator, return true. */
28072 cp_parser_declares_only_class_p (cp_parser
*parser
)
28074 /* If the next token is a `;' or a `,' then there is no
28076 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28077 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28080 /* Update the DECL_SPECS to reflect the storage class indicated by
28084 cp_parser_set_storage_class (cp_parser
*parser
,
28085 cp_decl_specifier_seq
*decl_specs
,
28089 cp_storage_class storage_class
;
28091 if (parser
->in_unbraced_linkage_specification_p
)
28093 error_at (token
->location
, "invalid use of %qD in linkage specification",
28094 ridpointers
[keyword
]);
28097 else if (decl_specs
->storage_class
!= sc_none
)
28099 decl_specs
->conflicting_specifiers_p
= true;
28103 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28104 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28105 && decl_specs
->gnu_thread_keyword_p
)
28107 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28108 "%<__thread%> before %qD", ridpointers
[keyword
]);
28114 storage_class
= sc_auto
;
28117 storage_class
= sc_register
;
28120 storage_class
= sc_static
;
28123 storage_class
= sc_extern
;
28126 storage_class
= sc_mutable
;
28129 gcc_unreachable ();
28131 decl_specs
->storage_class
= storage_class
;
28132 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28134 /* A storage class specifier cannot be applied alongside a typedef
28135 specifier. If there is a typedef specifier present then set
28136 conflicting_specifiers_p which will trigger an error later
28137 on in grokdeclarator. */
28138 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28139 decl_specs
->conflicting_specifiers_p
= true;
28142 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28143 is true, the type is a class or enum definition. */
28146 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28149 bool type_definition_p
)
28151 decl_specs
->any_specifiers_p
= true;
28153 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28154 (with, for example, in "typedef int wchar_t;") we remember that
28155 this is what happened. In system headers, we ignore these
28156 declarations so that G++ can work with system headers that are not
28158 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28159 && !type_definition_p
28160 && (type_spec
== boolean_type_node
28161 || type_spec
== char16_type_node
28162 || type_spec
== char32_type_node
28163 || type_spec
== wchar_type_node
)
28164 && (decl_specs
->type
28165 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28166 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28167 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28168 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28170 decl_specs
->redefined_builtin_type
= type_spec
;
28171 set_and_check_decl_spec_loc (decl_specs
,
28172 ds_redefined_builtin_type_spec
,
28174 if (!decl_specs
->type
)
28176 decl_specs
->type
= type_spec
;
28177 decl_specs
->type_definition_p
= false;
28178 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28181 else if (decl_specs
->type
)
28182 decl_specs
->multiple_types_p
= true;
28185 decl_specs
->type
= type_spec
;
28186 decl_specs
->type_definition_p
= type_definition_p
;
28187 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28188 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28192 /* True iff TOKEN is the GNU keyword __thread. */
28195 token_is__thread (cp_token
*token
)
28197 gcc_assert (token
->keyword
== RID_THREAD
);
28198 return id_equal (token
->u
.value
, "__thread");
28201 /* Set the location for a declarator specifier and check if it is
28204 DECL_SPECS is the sequence of declarator specifiers onto which to
28207 DS is the single declarator specifier to set which location is to
28208 be set onto the existing sequence of declarators.
28210 LOCATION is the location for the declarator specifier to
28214 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28215 cp_decl_spec ds
, cp_token
*token
)
28217 gcc_assert (ds
< ds_last
);
28219 if (decl_specs
== NULL
)
28222 source_location location
= token
->location
;
28224 if (decl_specs
->locations
[ds
] == 0)
28226 decl_specs
->locations
[ds
] = location
;
28227 if (ds
== ds_thread
)
28228 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28234 if (decl_specs
->locations
[ds_long_long
] != 0)
28235 error_at (location
,
28236 "%<long long long%> is too long for GCC");
28239 decl_specs
->locations
[ds_long_long
] = location
;
28240 pedwarn_cxx98 (location
,
28242 "ISO C++ 1998 does not support %<long long%>");
28245 else if (ds
== ds_thread
)
28247 bool gnu
= token_is__thread (token
);
28248 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28249 error_at (location
,
28250 "both %<__thread%> and %<thread_local%> specified");
28253 gcc_rich_location
richloc (location
);
28254 richloc
.add_fixit_remove ();
28255 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28260 static const char *const decl_spec_names
[] = {
28277 gcc_rich_location
richloc (location
);
28278 richloc
.add_fixit_remove ();
28279 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28284 /* Return true iff the declarator specifier DS is present in the
28285 sequence of declarator specifiers DECL_SPECS. */
28288 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28291 gcc_assert (ds
< ds_last
);
28293 if (decl_specs
== NULL
)
28296 return decl_specs
->locations
[ds
] != 0;
28299 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28300 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28303 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28305 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28308 /* Issue an error message indicating that TOKEN_DESC was expected.
28309 If KEYWORD is true, it indicated this function is called by
28310 cp_parser_require_keword and the required token can only be
28311 a indicated keyword.
28313 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28314 within any error as the location of an "opening" token matching
28315 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28316 RT_CLOSE_PAREN). */
28319 cp_parser_required_error (cp_parser
*parser
,
28320 required_token token_desc
,
28322 location_t matching_location
)
28324 if (cp_parser_simulate_error (parser
))
28327 const char *gmsgid
= NULL
;
28328 switch (token_desc
)
28331 gmsgid
= G_("expected %<new%>");
28334 gmsgid
= G_("expected %<delete%>");
28337 gmsgid
= G_("expected %<return%>");
28340 gmsgid
= G_("expected %<while%>");
28343 gmsgid
= G_("expected %<extern%>");
28345 case RT_STATIC_ASSERT
:
28346 gmsgid
= G_("expected %<static_assert%>");
28349 gmsgid
= G_("expected %<decltype%>");
28352 gmsgid
= G_("expected %<operator%>");
28355 gmsgid
= G_("expected %<class%>");
28358 gmsgid
= G_("expected %<template%>");
28361 gmsgid
= G_("expected %<namespace%>");
28364 gmsgid
= G_("expected %<using%>");
28367 gmsgid
= G_("expected %<asm%>");
28370 gmsgid
= G_("expected %<try%>");
28373 gmsgid
= G_("expected %<catch%>");
28376 gmsgid
= G_("expected %<throw%>");
28379 gmsgid
= G_("expected %<__label__%>");
28382 gmsgid
= G_("expected %<@try%>");
28384 case RT_AT_SYNCHRONIZED
:
28385 gmsgid
= G_("expected %<@synchronized%>");
28388 gmsgid
= G_("expected %<@throw%>");
28390 case RT_TRANSACTION_ATOMIC
:
28391 gmsgid
= G_("expected %<__transaction_atomic%>");
28393 case RT_TRANSACTION_RELAXED
:
28394 gmsgid
= G_("expected %<__transaction_relaxed%>");
28400 if (!gmsgid
&& !keyword
)
28402 switch (token_desc
)
28405 gmsgid
= G_("expected %<;%>");
28407 case RT_OPEN_PAREN
:
28408 gmsgid
= G_("expected %<(%>");
28410 case RT_CLOSE_BRACE
:
28411 gmsgid
= G_("expected %<}%>");
28413 case RT_OPEN_BRACE
:
28414 gmsgid
= G_("expected %<{%>");
28416 case RT_CLOSE_SQUARE
:
28417 gmsgid
= G_("expected %<]%>");
28419 case RT_OPEN_SQUARE
:
28420 gmsgid
= G_("expected %<[%>");
28423 gmsgid
= G_("expected %<,%>");
28426 gmsgid
= G_("expected %<::%>");
28429 gmsgid
= G_("expected %<<%>");
28432 gmsgid
= G_("expected %<>%>");
28435 gmsgid
= G_("expected %<=%>");
28438 gmsgid
= G_("expected %<...%>");
28441 gmsgid
= G_("expected %<*%>");
28444 gmsgid
= G_("expected %<~%>");
28447 gmsgid
= G_("expected %<:%>");
28449 case RT_COLON_SCOPE
:
28450 gmsgid
= G_("expected %<:%> or %<::%>");
28452 case RT_CLOSE_PAREN
:
28453 gmsgid
= G_("expected %<)%>");
28455 case RT_COMMA_CLOSE_PAREN
:
28456 gmsgid
= G_("expected %<,%> or %<)%>");
28458 case RT_PRAGMA_EOL
:
28459 gmsgid
= G_("expected end of line");
28462 gmsgid
= G_("expected identifier");
28465 gmsgid
= G_("expected selection-statement");
28468 gmsgid
= G_("expected iteration-statement");
28471 gmsgid
= G_("expected jump-statement");
28474 gmsgid
= G_("expected class-key");
28476 case RT_CLASS_TYPENAME_TEMPLATE
:
28477 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28480 gcc_unreachable ();
28485 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28489 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28490 issue an error message indicating that TOKEN_DESC was expected.
28492 Returns the token consumed, if the token had the appropriate type.
28493 Otherwise, returns NULL.
28495 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28496 within any error as the location of an "opening" token matching
28497 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28498 RT_CLOSE_PAREN). */
28501 cp_parser_require (cp_parser
* parser
,
28502 enum cpp_ttype type
,
28503 required_token token_desc
,
28504 location_t matching_location
)
28506 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28507 return cp_lexer_consume_token (parser
->lexer
);
28510 /* Output the MESSAGE -- unless we're parsing tentatively. */
28511 if (!cp_parser_simulate_error (parser
))
28512 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28513 matching_location
);
28518 /* An error message is produced if the next token is not '>'.
28519 All further tokens are skipped until the desired token is
28520 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28523 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28525 /* Current level of '< ... >'. */
28526 unsigned level
= 0;
28527 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28528 unsigned nesting_depth
= 0;
28530 /* Are we ready, yet? If not, issue error message. */
28531 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28534 /* Skip tokens until the desired token is found. */
28537 /* Peek at the next token. */
28538 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28541 if (!nesting_depth
)
28546 if (cxx_dialect
== cxx98
)
28547 /* C++0x views the `>>' operator as two `>' tokens, but
28550 else if (!nesting_depth
&& level
-- == 0)
28552 /* We've hit a `>>' where the first `>' closes the
28553 template argument list, and the second `>' is
28554 spurious. Just consume the `>>' and stop; we've
28555 already produced at least one error. */
28556 cp_lexer_consume_token (parser
->lexer
);
28559 /* Fall through for C++0x, so we handle the second `>' in
28561 gcc_fallthrough ();
28564 if (!nesting_depth
&& level
-- == 0)
28566 /* We've reached the token we want, consume it and stop. */
28567 cp_lexer_consume_token (parser
->lexer
);
28572 case CPP_OPEN_PAREN
:
28573 case CPP_OPEN_SQUARE
:
28577 case CPP_CLOSE_PAREN
:
28578 case CPP_CLOSE_SQUARE
:
28579 if (nesting_depth
-- == 0)
28584 case CPP_PRAGMA_EOL
:
28585 case CPP_SEMICOLON
:
28586 case CPP_OPEN_BRACE
:
28587 case CPP_CLOSE_BRACE
:
28588 /* The '>' was probably forgotten, don't look further. */
28595 /* Consume this token. */
28596 cp_lexer_consume_token (parser
->lexer
);
28600 /* If the next token is the indicated keyword, consume it. Otherwise,
28601 issue an error message indicating that TOKEN_DESC was expected.
28603 Returns the token consumed, if the token had the appropriate type.
28604 Otherwise, returns NULL. */
28607 cp_parser_require_keyword (cp_parser
* parser
,
28609 required_token token_desc
)
28611 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28613 if (token
&& token
->keyword
!= keyword
)
28615 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28623 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28624 function-definition. */
28627 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28629 return (/* An ordinary function-body begins with an `{'. */
28630 token
->type
== CPP_OPEN_BRACE
28631 /* A ctor-initializer begins with a `:'. */
28632 || token
->type
== CPP_COLON
28633 /* A function-try-block begins with `try'. */
28634 || token
->keyword
== RID_TRY
28635 /* A function-transaction-block begins with `__transaction_atomic'
28636 or `__transaction_relaxed'. */
28637 || token
->keyword
== RID_TRANSACTION_ATOMIC
28638 || token
->keyword
== RID_TRANSACTION_RELAXED
28639 /* The named return value extension begins with `return'. */
28640 || token
->keyword
== RID_RETURN
);
28643 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28647 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28651 token
= cp_lexer_peek_token (parser
->lexer
);
28652 return (token
->type
== CPP_OPEN_BRACE
28653 || (token
->type
== CPP_COLON
28654 && !parser
->colon_doesnt_start_class_def_p
));
28657 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28658 C++0x) ending a template-argument. */
28661 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28665 token
= cp_lexer_peek_token (parser
->lexer
);
28666 return (token
->type
== CPP_COMMA
28667 || token
->type
== CPP_GREATER
28668 || token
->type
== CPP_ELLIPSIS
28669 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28672 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28673 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28676 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28681 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28682 if (token
->type
== CPP_LESS
)
28684 /* Check for the sequence `<::' in the original code. It would be lexed as
28685 `[:', where `[' is a digraph, and there is no whitespace before
28687 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28690 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28691 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28697 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28698 or none_type otherwise. */
28700 static enum tag_types
28701 cp_parser_token_is_class_key (cp_token
* token
)
28703 switch (token
->keyword
)
28708 return record_type
;
28717 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28718 or none_type otherwise or if the token is null. */
28720 static enum tag_types
28721 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28726 switch (token
->keyword
)
28731 return typename_type
;
28738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28741 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28743 if (type
== error_mark_node
)
28745 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28747 if (permerror (input_location
, "%qs tag used in naming %q#T",
28748 class_key
== union_type
? "union"
28749 : class_key
== record_type
? "struct" : "class",
28751 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28752 "%q#T was previously declared here", type
);
28756 /* Issue an error message if DECL is redeclared with different
28757 access than its original declaration [class.access.spec/3].
28758 This applies to nested classes, nested class templates and
28759 enumerations [class.mem/1]. */
28762 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28765 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28766 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28769 if ((TREE_PRIVATE (decl
)
28770 != (current_access_specifier
== access_private_node
))
28771 || (TREE_PROTECTED (decl
)
28772 != (current_access_specifier
== access_protected_node
)))
28773 error_at (location
, "%qD redeclared with different access", decl
);
28776 /* Look for the `template' keyword, as a syntactic disambiguator.
28777 Return TRUE iff it is present, in which case it will be
28781 cp_parser_optional_template_keyword (cp_parser
*parser
)
28783 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28785 /* In C++98 the `template' keyword can only be used within templates;
28786 outside templates the parser can always figure out what is a
28787 template and what is not. In C++11, per the resolution of DR 468,
28788 `template' is allowed in cases where it is not strictly necessary. */
28789 if (!processing_template_decl
28790 && pedantic
&& cxx_dialect
== cxx98
)
28792 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28793 pedwarn (token
->location
, OPT_Wpedantic
,
28794 "in C++98 %<template%> (as a disambiguator) is only "
28795 "allowed within templates");
28796 /* If this part of the token stream is rescanned, the same
28797 error message would be generated. So, we purge the token
28798 from the stream. */
28799 cp_lexer_purge_token (parser
->lexer
);
28804 /* Consume the `template' keyword. */
28805 cp_lexer_consume_token (parser
->lexer
);
28812 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28813 set PARSER->SCOPE, and perform other related actions. */
28816 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28818 struct tree_check
*check_value
;
28820 /* Get the stored value. */
28821 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28822 /* Set the scope from the stored value. */
28823 parser
->scope
= saved_checks_value (check_value
);
28824 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28825 parser
->object_scope
= NULL_TREE
;
28828 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28829 encounter the end of a block before what we were looking for. */
28832 cp_parser_cache_group (cp_parser
*parser
,
28833 enum cpp_ttype end
,
28838 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28840 /* Abort a parenthesized expression if we encounter a semicolon. */
28841 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28842 && token
->type
== CPP_SEMICOLON
)
28844 /* If we've reached the end of the file, stop. */
28845 if (token
->type
== CPP_EOF
28846 || (end
!= CPP_PRAGMA_EOL
28847 && token
->type
== CPP_PRAGMA_EOL
))
28849 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28850 /* We've hit the end of an enclosing block, so there's been some
28851 kind of syntax error. */
28854 /* Consume the token. */
28855 cp_lexer_consume_token (parser
->lexer
);
28856 /* See if it starts a new group. */
28857 if (token
->type
== CPP_OPEN_BRACE
)
28859 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28860 /* In theory this should probably check end == '}', but
28861 cp_parser_save_member_function_body needs it to exit
28862 after either '}' or ')' when called with ')'. */
28866 else if (token
->type
== CPP_OPEN_PAREN
)
28868 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28869 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28872 else if (token
->type
== CPP_PRAGMA
)
28873 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28874 else if (token
->type
== end
)
28879 /* Like above, for caching a default argument or NSDMI. Both of these are
28880 terminated by a non-nested comma, but it can be unclear whether or not a
28881 comma is nested in a template argument list unless we do more parsing.
28882 In order to handle this ambiguity, when we encounter a ',' after a '<'
28883 we try to parse what follows as a parameter-declaration-list (in the
28884 case of a default argument) or a member-declarator (in the case of an
28885 NSDMI). If that succeeds, then we stop caching. */
28888 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28890 unsigned depth
= 0;
28891 int maybe_template_id
= 0;
28892 cp_token
*first_token
;
28894 tree default_argument
;
28896 /* Add tokens until we have processed the entire default
28897 argument. We add the range [first_token, token). */
28898 first_token
= cp_lexer_peek_token (parser
->lexer
);
28899 if (first_token
->type
== CPP_OPEN_BRACE
)
28901 /* For list-initialization, this is straightforward. */
28902 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28903 token
= cp_lexer_peek_token (parser
->lexer
);
28909 /* Peek at the next token. */
28910 token
= cp_lexer_peek_token (parser
->lexer
);
28911 /* What we do depends on what token we have. */
28912 switch (token
->type
)
28914 /* In valid code, a default argument must be
28915 immediately followed by a `,' `)', or `...'. */
28917 if (depth
== 0 && maybe_template_id
)
28919 /* If we've seen a '<', we might be in a
28920 template-argument-list. Until Core issue 325 is
28921 resolved, we don't know how this situation ought
28922 to be handled, so try to DTRT. We check whether
28923 what comes after the comma is a valid parameter
28924 declaration list. If it is, then the comma ends
28925 the default argument; otherwise the default
28926 argument continues. */
28927 bool error
= false;
28930 /* Set ITALP so cp_parser_parameter_declaration_list
28931 doesn't decide to commit to this parse. */
28932 bool saved_italp
= parser
->in_template_argument_list_p
;
28933 parser
->in_template_argument_list_p
= true;
28935 cp_parser_parse_tentatively (parser
);
28939 /* Parse declarators until we reach a non-comma or
28940 somthing that cannot be an initializer.
28941 Just checking whether we're looking at a single
28942 declarator is insufficient. Consider:
28943 int var = tuple<T,U>::x;
28944 The template parameter 'U' looks exactly like a
28948 int ctor_dtor_or_conv_p
;
28949 cp_lexer_consume_token (parser
->lexer
);
28950 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28951 &ctor_dtor_or_conv_p
,
28952 /*parenthesized_p=*/NULL
,
28954 /*friend_p=*/false);
28955 peek
= cp_lexer_peek_token (parser
->lexer
);
28956 if (cp_parser_error_occurred (parser
))
28959 while (peek
->type
== CPP_COMMA
);
28960 /* If we met an '=' or ';' then the original comma
28961 was the end of the NSDMI. Otherwise assume
28962 we're still in the NSDMI. */
28963 error
= (peek
->type
!= CPP_EQ
28964 && peek
->type
!= CPP_SEMICOLON
);
28968 cp_lexer_consume_token (parser
->lexer
);
28969 begin_scope (sk_function_parms
, NULL_TREE
);
28970 cp_parser_parameter_declaration_list (parser
, &error
);
28971 pop_bindings_and_leave_scope ();
28973 if (!cp_parser_error_occurred (parser
) && !error
)
28975 cp_parser_abort_tentative_parse (parser
);
28977 parser
->in_template_argument_list_p
= saved_italp
;
28981 case CPP_CLOSE_PAREN
:
28983 /* If we run into a non-nested `;', `}', or `]',
28984 then the code is invalid -- but the default
28985 argument is certainly over. */
28986 case CPP_SEMICOLON
:
28987 case CPP_CLOSE_BRACE
:
28988 case CPP_CLOSE_SQUARE
:
28990 /* Handle correctly int n = sizeof ... ( p ); */
28991 && token
->type
!= CPP_ELLIPSIS
)
28993 /* Update DEPTH, if necessary. */
28994 else if (token
->type
== CPP_CLOSE_PAREN
28995 || token
->type
== CPP_CLOSE_BRACE
28996 || token
->type
== CPP_CLOSE_SQUARE
)
29000 case CPP_OPEN_PAREN
:
29001 case CPP_OPEN_SQUARE
:
29002 case CPP_OPEN_BRACE
:
29008 /* This might be the comparison operator, or it might
29009 start a template argument list. */
29010 ++maybe_template_id
;
29014 if (cxx_dialect
== cxx98
)
29016 /* Fall through for C++0x, which treats the `>>'
29017 operator like two `>' tokens in certain
29019 gcc_fallthrough ();
29024 /* This might be an operator, or it might close a
29025 template argument list. But if a previous '<'
29026 started a template argument list, this will have
29027 closed it, so we can't be in one anymore. */
29028 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29029 if (maybe_template_id
< 0)
29030 maybe_template_id
= 0;
29034 /* If we run out of tokens, issue an error message. */
29036 case CPP_PRAGMA_EOL
:
29037 error_at (token
->location
, "file ends in default argument");
29038 return error_mark_node
;
29042 /* In these cases, we should look for template-ids.
29043 For example, if the default argument is
29044 `X<int, double>()', we need to do name lookup to
29045 figure out whether or not `X' is a template; if
29046 so, the `,' does not end the default argument.
29048 That is not yet done. */
29055 /* If we've reached the end, stop. */
29059 /* Add the token to the token block. */
29060 token
= cp_lexer_consume_token (parser
->lexer
);
29063 /* Create a DEFAULT_ARG to represent the unparsed default
29065 default_argument
= make_node (DEFAULT_ARG
);
29066 DEFARG_TOKENS (default_argument
)
29067 = cp_token_cache_new (first_token
, token
);
29068 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29070 return default_argument
;
29073 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29076 defarg_location (tree default_argument
)
29078 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29079 location_t start
= tokens
->first
->location
;
29080 location_t end
= tokens
->last
->location
;
29081 return make_location (start
, start
, end
);
29084 /* Begin parsing tentatively. We always save tokens while parsing
29085 tentatively so that if the tentative parsing fails we can restore the
29089 cp_parser_parse_tentatively (cp_parser
* parser
)
29091 /* Enter a new parsing context. */
29092 parser
->context
= cp_parser_context_new (parser
->context
);
29093 /* Begin saving tokens. */
29094 cp_lexer_save_tokens (parser
->lexer
);
29095 /* In order to avoid repetitive access control error messages,
29096 access checks are queued up until we are no longer parsing
29098 push_deferring_access_checks (dk_deferred
);
29101 /* Commit to the currently active tentative parse. */
29104 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29106 cp_parser_context
*context
;
29109 /* Mark all of the levels as committed. */
29110 lexer
= parser
->lexer
;
29111 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29113 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29115 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29116 while (!cp_lexer_saving_tokens (lexer
))
29117 lexer
= lexer
->next
;
29118 cp_lexer_commit_tokens (lexer
);
29122 /* Commit to the topmost currently active tentative parse.
29124 Note that this function shouldn't be called when there are
29125 irreversible side-effects while in a tentative state. For
29126 example, we shouldn't create a permanent entry in the symbol
29127 table, or issue an error message that might not apply if the
29128 tentative parse is aborted. */
29131 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29133 cp_parser_context
*context
= parser
->context
;
29134 cp_lexer
*lexer
= parser
->lexer
;
29138 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29140 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29142 while (!cp_lexer_saving_tokens (lexer
))
29143 lexer
= lexer
->next
;
29144 cp_lexer_commit_tokens (lexer
);
29148 /* Abort the currently active tentative parse. All consumed tokens
29149 will be rolled back, and no diagnostics will be issued. */
29152 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29154 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29155 || errorcount
> 0);
29156 cp_parser_simulate_error (parser
);
29157 /* Now, pretend that we want to see if the construct was
29158 successfully parsed. */
29159 cp_parser_parse_definitely (parser
);
29162 /* Stop parsing tentatively. If a parse error has occurred, restore the
29163 token stream. Otherwise, commit to the tokens we have consumed.
29164 Returns true if no error occurred; false otherwise. */
29167 cp_parser_parse_definitely (cp_parser
* parser
)
29169 bool error_occurred
;
29170 cp_parser_context
*context
;
29172 /* Remember whether or not an error occurred, since we are about to
29173 destroy that information. */
29174 error_occurred
= cp_parser_error_occurred (parser
);
29175 /* Remove the topmost context from the stack. */
29176 context
= parser
->context
;
29177 parser
->context
= context
->next
;
29178 /* If no parse errors occurred, commit to the tentative parse. */
29179 if (!error_occurred
)
29181 /* Commit to the tokens read tentatively, unless that was
29183 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29184 cp_lexer_commit_tokens (parser
->lexer
);
29186 pop_to_parent_deferring_access_checks ();
29188 /* Otherwise, if errors occurred, roll back our state so that things
29189 are just as they were before we began the tentative parse. */
29192 cp_lexer_rollback_tokens (parser
->lexer
);
29193 pop_deferring_access_checks ();
29195 /* Add the context to the front of the free list. */
29196 context
->next
= cp_parser_context_free_list
;
29197 cp_parser_context_free_list
= context
;
29199 return !error_occurred
;
29202 /* Returns true if we are parsing tentatively and are not committed to
29203 this tentative parse. */
29206 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29208 return (cp_parser_parsing_tentatively (parser
)
29209 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29212 /* Returns nonzero iff an error has occurred during the most recent
29213 tentative parse. */
29216 cp_parser_error_occurred (cp_parser
* parser
)
29218 return (cp_parser_parsing_tentatively (parser
)
29219 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29222 /* Returns nonzero if GNU extensions are allowed. */
29225 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29227 return parser
->allow_gnu_extensions_p
;
29230 /* Objective-C++ Productions */
29233 /* Parse an Objective-C expression, which feeds into a primary-expression
29237 objc-message-expression
29238 objc-string-literal
29239 objc-encode-expression
29240 objc-protocol-expression
29241 objc-selector-expression
29243 Returns a tree representation of the expression. */
29246 cp_parser_objc_expression (cp_parser
* parser
)
29248 /* Try to figure out what kind of declaration is present. */
29249 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29253 case CPP_OPEN_SQUARE
:
29254 return cp_parser_objc_message_expression (parser
);
29256 case CPP_OBJC_STRING
:
29257 kwd
= cp_lexer_consume_token (parser
->lexer
);
29258 return objc_build_string_object (kwd
->u
.value
);
29261 switch (kwd
->keyword
)
29263 case RID_AT_ENCODE
:
29264 return cp_parser_objc_encode_expression (parser
);
29266 case RID_AT_PROTOCOL
:
29267 return cp_parser_objc_protocol_expression (parser
);
29269 case RID_AT_SELECTOR
:
29270 return cp_parser_objc_selector_expression (parser
);
29276 error_at (kwd
->location
,
29277 "misplaced %<@%D%> Objective-C++ construct",
29279 cp_parser_skip_to_end_of_block_or_statement (parser
);
29282 return error_mark_node
;
29285 /* Parse an Objective-C message expression.
29287 objc-message-expression:
29288 [ objc-message-receiver objc-message-args ]
29290 Returns a representation of an Objective-C message. */
29293 cp_parser_objc_message_expression (cp_parser
* parser
)
29295 tree receiver
, messageargs
;
29297 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29298 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29299 receiver
= cp_parser_objc_message_receiver (parser
);
29300 messageargs
= cp_parser_objc_message_args (parser
);
29301 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29302 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29304 tree result
= objc_build_message_expr (receiver
, messageargs
);
29306 /* Construct a location e.g.
29309 ranging from the '[' to the ']', with the caret at the start. */
29310 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29311 protected_set_expr_location (result
, combined_loc
);
29316 /* Parse an objc-message-receiver.
29318 objc-message-receiver:
29320 simple-type-specifier
29322 Returns a representation of the type or expression. */
29325 cp_parser_objc_message_receiver (cp_parser
* parser
)
29329 /* An Objective-C message receiver may be either (1) a type
29330 or (2) an expression. */
29331 cp_parser_parse_tentatively (parser
);
29332 rcv
= cp_parser_expression (parser
);
29334 /* If that worked out, fine. */
29335 if (cp_parser_parse_definitely (parser
))
29338 cp_parser_parse_tentatively (parser
);
29339 rcv
= cp_parser_simple_type_specifier (parser
,
29340 /*decl_specs=*/NULL
,
29341 CP_PARSER_FLAGS_NONE
);
29343 if (cp_parser_parse_definitely (parser
))
29344 return objc_get_class_reference (rcv
);
29346 cp_parser_error (parser
, "objective-c++ message receiver expected");
29347 return error_mark_node
;
29350 /* Parse the arguments and selectors comprising an Objective-C message.
29355 objc-selector-args , objc-comma-args
29357 objc-selector-args:
29358 objc-selector [opt] : assignment-expression
29359 objc-selector-args objc-selector [opt] : assignment-expression
29362 assignment-expression
29363 objc-comma-args , assignment-expression
29365 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29366 selector arguments and TREE_VALUE containing a list of comma
29370 cp_parser_objc_message_args (cp_parser
* parser
)
29372 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29373 bool maybe_unary_selector_p
= true;
29374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29376 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29378 tree selector
= NULL_TREE
, arg
;
29380 if (token
->type
!= CPP_COLON
)
29381 selector
= cp_parser_objc_selector (parser
);
29383 /* Detect if we have a unary selector. */
29384 if (maybe_unary_selector_p
29385 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29386 return build_tree_list (selector
, NULL_TREE
);
29388 maybe_unary_selector_p
= false;
29389 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29390 arg
= cp_parser_assignment_expression (parser
);
29393 = chainon (sel_args
,
29394 build_tree_list (selector
, arg
));
29396 token
= cp_lexer_peek_token (parser
->lexer
);
29399 /* Handle non-selector arguments, if any. */
29400 while (token
->type
== CPP_COMMA
)
29404 cp_lexer_consume_token (parser
->lexer
);
29405 arg
= cp_parser_assignment_expression (parser
);
29408 = chainon (addl_args
,
29409 build_tree_list (NULL_TREE
, arg
));
29411 token
= cp_lexer_peek_token (parser
->lexer
);
29414 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29416 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29417 return build_tree_list (error_mark_node
, error_mark_node
);
29420 return build_tree_list (sel_args
, addl_args
);
29423 /* Parse an Objective-C encode expression.
29425 objc-encode-expression:
29426 @encode objc-typename
29428 Returns an encoded representation of the type argument. */
29431 cp_parser_objc_encode_expression (cp_parser
* parser
)
29435 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29437 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29438 matching_parens parens
;
29439 parens
.require_open (parser
);
29440 token
= cp_lexer_peek_token (parser
->lexer
);
29441 type
= complete_type (cp_parser_type_id (parser
));
29442 parens
.require_close (parser
);
29446 error_at (token
->location
,
29447 "%<@encode%> must specify a type as an argument");
29448 return error_mark_node
;
29451 /* This happens if we find @encode(T) (where T is a template
29452 typename or something dependent on a template typename) when
29453 parsing a template. In that case, we can't compile it
29454 immediately, but we rather create an AT_ENCODE_EXPR which will
29455 need to be instantiated when the template is used.
29457 if (dependent_type_p (type
))
29459 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29460 TREE_READONLY (value
) = 1;
29465 /* Build a location of the form:
29468 with caret==start at the @ token, finishing at the close paren. */
29469 location_t combined_loc
29470 = make_location (start_loc
, start_loc
,
29471 cp_lexer_previous_token (parser
->lexer
)->location
);
29473 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29476 /* Parse an Objective-C @defs expression. */
29479 cp_parser_objc_defs_expression (cp_parser
*parser
)
29483 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29484 matching_parens parens
;
29485 parens
.require_open (parser
);
29486 name
= cp_parser_identifier (parser
);
29487 parens
.require_close (parser
);
29489 return objc_get_class_ivars (name
);
29492 /* Parse an Objective-C protocol expression.
29494 objc-protocol-expression:
29495 @protocol ( identifier )
29497 Returns a representation of the protocol expression. */
29500 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29503 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29505 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29506 matching_parens parens
;
29507 parens
.require_open (parser
);
29508 proto
= cp_parser_identifier (parser
);
29509 parens
.require_close (parser
);
29511 /* Build a location of the form:
29514 with caret==start at the @ token, finishing at the close paren. */
29515 location_t combined_loc
29516 = make_location (start_loc
, start_loc
,
29517 cp_lexer_previous_token (parser
->lexer
)->location
);
29518 tree result
= objc_build_protocol_expr (proto
);
29519 protected_set_expr_location (result
, combined_loc
);
29523 /* Parse an Objective-C selector expression.
29525 objc-selector-expression:
29526 @selector ( objc-method-signature )
29528 objc-method-signature:
29534 objc-selector-seq objc-selector :
29536 Returns a representation of the method selector. */
29539 cp_parser_objc_selector_expression (cp_parser
* parser
)
29541 tree sel_seq
= NULL_TREE
;
29542 bool maybe_unary_selector_p
= true;
29544 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29546 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29547 matching_parens parens
;
29548 parens
.require_open (parser
);
29549 token
= cp_lexer_peek_token (parser
->lexer
);
29551 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29552 || token
->type
== CPP_SCOPE
)
29554 tree selector
= NULL_TREE
;
29556 if (token
->type
!= CPP_COLON
29557 || token
->type
== CPP_SCOPE
)
29558 selector
= cp_parser_objc_selector (parser
);
29560 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29561 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29563 /* Detect if we have a unary selector. */
29564 if (maybe_unary_selector_p
)
29566 sel_seq
= selector
;
29567 goto finish_selector
;
29571 cp_parser_error (parser
, "expected %<:%>");
29574 maybe_unary_selector_p
= false;
29575 token
= cp_lexer_consume_token (parser
->lexer
);
29577 if (token
->type
== CPP_SCOPE
)
29580 = chainon (sel_seq
,
29581 build_tree_list (selector
, NULL_TREE
));
29583 = chainon (sel_seq
,
29584 build_tree_list (NULL_TREE
, NULL_TREE
));
29588 = chainon (sel_seq
,
29589 build_tree_list (selector
, NULL_TREE
));
29591 token
= cp_lexer_peek_token (parser
->lexer
);
29595 parens
.require_close (parser
);
29598 /* Build a location of the form:
29601 with caret==start at the @ token, finishing at the close paren. */
29602 location_t combined_loc
29603 = make_location (loc
, loc
,
29604 cp_lexer_previous_token (parser
->lexer
)->location
);
29605 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29606 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29607 protected_set_expr_location (result
, combined_loc
);
29611 /* Parse a list of identifiers.
29613 objc-identifier-list:
29615 objc-identifier-list , identifier
29617 Returns a TREE_LIST of identifier nodes. */
29620 cp_parser_objc_identifier_list (cp_parser
* parser
)
29626 identifier
= cp_parser_identifier (parser
);
29627 if (identifier
== error_mark_node
)
29628 return error_mark_node
;
29630 list
= build_tree_list (NULL_TREE
, identifier
);
29631 sep
= cp_lexer_peek_token (parser
->lexer
);
29633 while (sep
->type
== CPP_COMMA
)
29635 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29636 identifier
= cp_parser_identifier (parser
);
29637 if (identifier
== error_mark_node
)
29640 list
= chainon (list
, build_tree_list (NULL_TREE
,
29642 sep
= cp_lexer_peek_token (parser
->lexer
);
29648 /* Parse an Objective-C alias declaration.
29650 objc-alias-declaration:
29651 @compatibility_alias identifier identifier ;
29653 This function registers the alias mapping with the Objective-C front end.
29654 It returns nothing. */
29657 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29661 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29662 alias
= cp_parser_identifier (parser
);
29663 orig
= cp_parser_identifier (parser
);
29664 objc_declare_alias (alias
, orig
);
29665 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29668 /* Parse an Objective-C class forward-declaration.
29670 objc-class-declaration:
29671 @class objc-identifier-list ;
29673 The function registers the forward declarations with the Objective-C
29674 front end. It returns nothing. */
29677 cp_parser_objc_class_declaration (cp_parser
* parser
)
29679 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29684 id
= cp_parser_identifier (parser
);
29685 if (id
== error_mark_node
)
29688 objc_declare_class (id
);
29690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29691 cp_lexer_consume_token (parser
->lexer
);
29695 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29698 /* Parse a list of Objective-C protocol references.
29700 objc-protocol-refs-opt:
29701 objc-protocol-refs [opt]
29703 objc-protocol-refs:
29704 < objc-identifier-list >
29706 Returns a TREE_LIST of identifiers, if any. */
29709 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29711 tree protorefs
= NULL_TREE
;
29713 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29715 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29716 protorefs
= cp_parser_objc_identifier_list (parser
);
29717 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29723 /* Parse a Objective-C visibility specification. */
29726 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29728 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29730 switch (vis
->keyword
)
29732 case RID_AT_PRIVATE
:
29733 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29735 case RID_AT_PROTECTED
:
29736 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29738 case RID_AT_PUBLIC
:
29739 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29741 case RID_AT_PACKAGE
:
29742 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29748 /* Eat '@private'/'@protected'/'@public'. */
29749 cp_lexer_consume_token (parser
->lexer
);
29752 /* Parse an Objective-C method type. Return 'true' if it is a class
29753 (+) method, and 'false' if it is an instance (-) method. */
29756 cp_parser_objc_method_type (cp_parser
* parser
)
29758 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29764 /* Parse an Objective-C protocol qualifier. */
29767 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29769 tree quals
= NULL_TREE
, node
;
29770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29772 node
= token
->u
.value
;
29774 while (node
&& identifier_p (node
)
29775 && (node
== ridpointers
[(int) RID_IN
]
29776 || node
== ridpointers
[(int) RID_OUT
]
29777 || node
== ridpointers
[(int) RID_INOUT
]
29778 || node
== ridpointers
[(int) RID_BYCOPY
]
29779 || node
== ridpointers
[(int) RID_BYREF
]
29780 || node
== ridpointers
[(int) RID_ONEWAY
]))
29782 quals
= tree_cons (NULL_TREE
, node
, quals
);
29783 cp_lexer_consume_token (parser
->lexer
);
29784 token
= cp_lexer_peek_token (parser
->lexer
);
29785 node
= token
->u
.value
;
29791 /* Parse an Objective-C typename. */
29794 cp_parser_objc_typename (cp_parser
* parser
)
29796 tree type_name
= NULL_TREE
;
29798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29800 tree proto_quals
, cp_type
= NULL_TREE
;
29802 matching_parens parens
;
29803 parens
.consume_open (parser
); /* Eat '('. */
29804 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29806 /* An ObjC type name may consist of just protocol qualifiers, in which
29807 case the type shall default to 'id'. */
29808 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29810 cp_type
= cp_parser_type_id (parser
);
29812 /* If the type could not be parsed, an error has already
29813 been produced. For error recovery, behave as if it had
29814 not been specified, which will use the default type
29816 if (cp_type
== error_mark_node
)
29818 cp_type
= NULL_TREE
;
29819 /* We need to skip to the closing parenthesis as
29820 cp_parser_type_id() does not seem to do it for
29822 cp_parser_skip_to_closing_parenthesis (parser
,
29823 /*recovering=*/true,
29824 /*or_comma=*/false,
29825 /*consume_paren=*/false);
29829 parens
.require_close (parser
);
29830 type_name
= build_tree_list (proto_quals
, cp_type
);
29836 /* Check to see if TYPE refers to an Objective-C selector name. */
29839 cp_parser_objc_selector_p (enum cpp_ttype type
)
29841 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29842 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29843 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29844 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29845 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29848 /* Parse an Objective-C selector. */
29851 cp_parser_objc_selector (cp_parser
* parser
)
29853 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29855 if (!cp_parser_objc_selector_p (token
->type
))
29857 error_at (token
->location
, "invalid Objective-C++ selector name");
29858 return error_mark_node
;
29861 /* C++ operator names are allowed to appear in ObjC selectors. */
29862 switch (token
->type
)
29864 case CPP_AND_AND
: return get_identifier ("and");
29865 case CPP_AND_EQ
: return get_identifier ("and_eq");
29866 case CPP_AND
: return get_identifier ("bitand");
29867 case CPP_OR
: return get_identifier ("bitor");
29868 case CPP_COMPL
: return get_identifier ("compl");
29869 case CPP_NOT
: return get_identifier ("not");
29870 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29871 case CPP_OR_OR
: return get_identifier ("or");
29872 case CPP_OR_EQ
: return get_identifier ("or_eq");
29873 case CPP_XOR
: return get_identifier ("xor");
29874 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29875 default: return token
->u
.value
;
29879 /* Parse an Objective-C params list. */
29882 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29884 tree params
= NULL_TREE
;
29885 bool maybe_unary_selector_p
= true;
29886 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29888 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29890 tree selector
= NULL_TREE
, type_name
, identifier
;
29891 tree parm_attr
= NULL_TREE
;
29893 if (token
->keyword
== RID_ATTRIBUTE
)
29896 if (token
->type
!= CPP_COLON
)
29897 selector
= cp_parser_objc_selector (parser
);
29899 /* Detect if we have a unary selector. */
29900 if (maybe_unary_selector_p
29901 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29903 params
= selector
; /* Might be followed by attributes. */
29907 maybe_unary_selector_p
= false;
29908 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29910 /* Something went quite wrong. There should be a colon
29911 here, but there is not. Stop parsing parameters. */
29914 type_name
= cp_parser_objc_typename (parser
);
29915 /* New ObjC allows attributes on parameters too. */
29916 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29917 parm_attr
= cp_parser_attributes_opt (parser
);
29918 identifier
= cp_parser_identifier (parser
);
29922 objc_build_keyword_decl (selector
,
29927 token
= cp_lexer_peek_token (parser
->lexer
);
29930 if (params
== NULL_TREE
)
29932 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29933 return error_mark_node
;
29936 /* We allow tail attributes for the method. */
29937 if (token
->keyword
== RID_ATTRIBUTE
)
29939 *attributes
= cp_parser_attributes_opt (parser
);
29940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29941 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29943 cp_parser_error (parser
,
29944 "method attributes must be specified at the end");
29945 return error_mark_node
;
29948 if (params
== NULL_TREE
)
29950 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29951 return error_mark_node
;
29956 /* Parse the non-keyword Objective-C params. */
29959 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29962 tree params
= make_node (TREE_LIST
);
29963 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29964 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29966 while (token
->type
== CPP_COMMA
)
29968 cp_parameter_declarator
*parmdecl
;
29971 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29972 token
= cp_lexer_peek_token (parser
->lexer
);
29974 if (token
->type
== CPP_ELLIPSIS
)
29976 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29978 token
= cp_lexer_peek_token (parser
->lexer
);
29982 /* TODO: parse attributes for tail parameters. */
29983 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29984 parm
= grokdeclarator (parmdecl
->declarator
,
29985 &parmdecl
->decl_specifiers
,
29986 PARM
, /*initialized=*/0,
29987 /*attrlist=*/NULL
);
29989 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29990 token
= cp_lexer_peek_token (parser
->lexer
);
29993 /* We allow tail attributes for the method. */
29994 if (token
->keyword
== RID_ATTRIBUTE
)
29996 if (*attributes
== NULL_TREE
)
29998 *attributes
= cp_parser_attributes_opt (parser
);
29999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30000 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30004 /* We have an error, but parse the attributes, so that we can
30006 *attributes
= cp_parser_attributes_opt (parser
);
30008 cp_parser_error (parser
,
30009 "method attributes must be specified at the end");
30010 return error_mark_node
;
30016 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30019 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30021 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30023 /* If the next token is `extern' and the following token is a string
30024 literal, then we have a linkage specification. */
30025 if (token
->keyword
== RID_EXTERN
30026 && cp_parser_is_pure_string_literal
30027 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30028 cp_parser_linkage_specification (parser
);
30029 /* Handle #pragma, if any. */
30030 else if (token
->type
== CPP_PRAGMA
)
30031 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30032 /* Allow stray semicolons. */
30033 else if (token
->type
== CPP_SEMICOLON
)
30034 cp_lexer_consume_token (parser
->lexer
);
30035 /* Mark methods as optional or required, when building protocols. */
30036 else if (token
->keyword
== RID_AT_OPTIONAL
)
30038 cp_lexer_consume_token (parser
->lexer
);
30039 objc_set_method_opt (true);
30041 else if (token
->keyword
== RID_AT_REQUIRED
)
30043 cp_lexer_consume_token (parser
->lexer
);
30044 objc_set_method_opt (false);
30046 else if (token
->keyword
== RID_NAMESPACE
)
30047 cp_parser_namespace_definition (parser
);
30048 /* Other stray characters must generate errors. */
30049 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30051 cp_lexer_consume_token (parser
->lexer
);
30052 error ("stray %qs between Objective-C++ methods",
30053 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30055 /* Finally, try to parse a block-declaration, or a function-definition. */
30057 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30060 /* Parse a method signature. */
30063 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30065 tree rettype
, kwdparms
, optparms
;
30066 bool ellipsis
= false;
30067 bool is_class_method
;
30069 is_class_method
= cp_parser_objc_method_type (parser
);
30070 rettype
= cp_parser_objc_typename (parser
);
30071 *attributes
= NULL_TREE
;
30072 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30073 if (kwdparms
== error_mark_node
)
30074 return error_mark_node
;
30075 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30076 if (optparms
== error_mark_node
)
30077 return error_mark_node
;
30079 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30083 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30086 cp_lexer_save_tokens (parser
->lexer
);
30087 tattr
= cp_parser_attributes_opt (parser
);
30088 gcc_assert (tattr
) ;
30090 /* If the attributes are followed by a method introducer, this is not allowed.
30091 Dump the attributes and flag the situation. */
30092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30093 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30096 /* Otherwise, the attributes introduce some interstitial code, possibly so
30097 rewind to allow that check. */
30098 cp_lexer_rollback_tokens (parser
->lexer
);
30102 /* Parse an Objective-C method prototype list. */
30105 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30107 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30109 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30111 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30113 tree attributes
, sig
;
30114 bool is_class_method
;
30115 if (token
->type
== CPP_PLUS
)
30116 is_class_method
= true;
30118 is_class_method
= false;
30119 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30120 if (sig
== error_mark_node
)
30122 cp_parser_skip_to_end_of_block_or_statement (parser
);
30123 token
= cp_lexer_peek_token (parser
->lexer
);
30126 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30127 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30129 else if (token
->keyword
== RID_AT_PROPERTY
)
30130 cp_parser_objc_at_property_declaration (parser
);
30131 else if (token
->keyword
== RID_ATTRIBUTE
30132 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30133 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30135 "prefix attributes are ignored for methods");
30137 /* Allow for interspersed non-ObjC++ code. */
30138 cp_parser_objc_interstitial_code (parser
);
30140 token
= cp_lexer_peek_token (parser
->lexer
);
30143 if (token
->type
!= CPP_EOF
)
30144 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30146 cp_parser_error (parser
, "expected %<@end%>");
30148 objc_finish_interface ();
30151 /* Parse an Objective-C method definition list. */
30154 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30156 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30158 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30162 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30165 tree sig
, attribute
;
30166 bool is_class_method
;
30167 if (token
->type
== CPP_PLUS
)
30168 is_class_method
= true;
30170 is_class_method
= false;
30171 push_deferring_access_checks (dk_deferred
);
30172 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30173 if (sig
== error_mark_node
)
30175 cp_parser_skip_to_end_of_block_or_statement (parser
);
30176 token
= cp_lexer_peek_token (parser
->lexer
);
30179 objc_start_method_definition (is_class_method
, sig
, attribute
,
30182 /* For historical reasons, we accept an optional semicolon. */
30183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30184 cp_lexer_consume_token (parser
->lexer
);
30186 ptk
= cp_lexer_peek_token (parser
->lexer
);
30187 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30188 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30190 perform_deferred_access_checks (tf_warning_or_error
);
30191 stop_deferring_access_checks ();
30192 meth
= cp_parser_function_definition_after_declarator (parser
,
30194 pop_deferring_access_checks ();
30195 objc_finish_method_definition (meth
);
30198 /* The following case will be removed once @synthesize is
30199 completely implemented. */
30200 else if (token
->keyword
== RID_AT_PROPERTY
)
30201 cp_parser_objc_at_property_declaration (parser
);
30202 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30203 cp_parser_objc_at_synthesize_declaration (parser
);
30204 else if (token
->keyword
== RID_AT_DYNAMIC
)
30205 cp_parser_objc_at_dynamic_declaration (parser
);
30206 else if (token
->keyword
== RID_ATTRIBUTE
30207 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30208 warning_at (token
->location
, OPT_Wattributes
,
30209 "prefix attributes are ignored for methods");
30211 /* Allow for interspersed non-ObjC++ code. */
30212 cp_parser_objc_interstitial_code (parser
);
30214 token
= cp_lexer_peek_token (parser
->lexer
);
30217 if (token
->type
!= CPP_EOF
)
30218 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30220 cp_parser_error (parser
, "expected %<@end%>");
30222 objc_finish_implementation ();
30225 /* Parse Objective-C ivars. */
30228 cp_parser_objc_class_ivars (cp_parser
* parser
)
30230 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30232 if (token
->type
!= CPP_OPEN_BRACE
)
30233 return; /* No ivars specified. */
30235 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30236 token
= cp_lexer_peek_token (parser
->lexer
);
30238 while (token
->type
!= CPP_CLOSE_BRACE
30239 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30241 cp_decl_specifier_seq declspecs
;
30242 int decl_class_or_enum_p
;
30243 tree prefix_attributes
;
30245 cp_parser_objc_visibility_spec (parser
);
30247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30250 cp_parser_decl_specifier_seq (parser
,
30251 CP_PARSER_FLAGS_OPTIONAL
,
30253 &decl_class_or_enum_p
);
30255 /* auto, register, static, extern, mutable. */
30256 if (declspecs
.storage_class
!= sc_none
)
30258 cp_parser_error (parser
, "invalid type for instance variable");
30259 declspecs
.storage_class
= sc_none
;
30262 /* thread_local. */
30263 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30265 cp_parser_error (parser
, "invalid type for instance variable");
30266 declspecs
.locations
[ds_thread
] = 0;
30270 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30272 cp_parser_error (parser
, "invalid type for instance variable");
30273 declspecs
.locations
[ds_typedef
] = 0;
30276 prefix_attributes
= declspecs
.attributes
;
30277 declspecs
.attributes
= NULL_TREE
;
30279 /* Keep going until we hit the `;' at the end of the
30281 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30283 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30284 cp_declarator
*declarator
= NULL
;
30285 int ctor_dtor_or_conv_p
;
30287 /* Check for a (possibly unnamed) bitfield declaration. */
30288 token
= cp_lexer_peek_token (parser
->lexer
);
30289 if (token
->type
== CPP_COLON
)
30292 if (token
->type
== CPP_NAME
30293 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30296 /* Get the name of the bitfield. */
30297 declarator
= make_id_declarator (NULL_TREE
,
30298 cp_parser_identifier (parser
),
30302 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30303 /* Get the width of the bitfield. */
30305 = cp_parser_constant_expression (parser
);
30309 /* Parse the declarator. */
30311 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30312 &ctor_dtor_or_conv_p
,
30313 /*parenthesized_p=*/NULL
,
30314 /*member_p=*/false,
30315 /*friend_p=*/false);
30318 /* Look for attributes that apply to the ivar. */
30319 attributes
= cp_parser_attributes_opt (parser
);
30320 /* Remember which attributes are prefix attributes and
30322 first_attribute
= attributes
;
30323 /* Combine the attributes. */
30324 attributes
= chainon (prefix_attributes
, attributes
);
30327 /* Create the bitfield declaration. */
30328 decl
= grokbitfield (declarator
, &declspecs
,
30329 width
, NULL_TREE
, attributes
);
30331 decl
= grokfield (declarator
, &declspecs
,
30332 NULL_TREE
, /*init_const_expr_p=*/false,
30333 NULL_TREE
, attributes
);
30335 /* Add the instance variable. */
30336 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30337 objc_add_instance_variable (decl
);
30339 /* Reset PREFIX_ATTRIBUTES. */
30340 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30341 attributes
= TREE_CHAIN (attributes
);
30343 TREE_CHAIN (attributes
) = NULL_TREE
;
30345 token
= cp_lexer_peek_token (parser
->lexer
);
30347 if (token
->type
== CPP_COMMA
)
30349 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30355 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30356 token
= cp_lexer_peek_token (parser
->lexer
);
30359 if (token
->keyword
== RID_AT_END
)
30360 cp_parser_error (parser
, "expected %<}%>");
30362 /* Do not consume the RID_AT_END, so it will be read again as terminating
30363 the @interface of @implementation. */
30364 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30365 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30367 /* For historical reasons, we accept an optional semicolon. */
30368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30369 cp_lexer_consume_token (parser
->lexer
);
30372 /* Parse an Objective-C protocol declaration. */
30375 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30377 tree proto
, protorefs
;
30380 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30381 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30383 tok
= cp_lexer_peek_token (parser
->lexer
);
30384 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30385 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30389 /* See if we have a forward declaration or a definition. */
30390 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30392 /* Try a forward declaration first. */
30393 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30399 id
= cp_parser_identifier (parser
);
30400 if (id
== error_mark_node
)
30403 objc_declare_protocol (id
, attributes
);
30405 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30406 cp_lexer_consume_token (parser
->lexer
);
30410 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30413 /* Ok, we got a full-fledged definition (or at least should). */
30416 proto
= cp_parser_identifier (parser
);
30417 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30418 objc_start_protocol (proto
, protorefs
, attributes
);
30419 cp_parser_objc_method_prototype_list (parser
);
30423 /* Parse an Objective-C superclass or category. */
30426 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30429 tree
*categ
, bool *is_class_extension
)
30431 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30433 *super
= *categ
= NULL_TREE
;
30434 *is_class_extension
= false;
30435 if (next
->type
== CPP_COLON
)
30437 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30438 *super
= cp_parser_identifier (parser
);
30440 else if (next
->type
== CPP_OPEN_PAREN
)
30442 matching_parens parens
;
30443 parens
.consume_open (parser
); /* Eat '('. */
30445 /* If there is no category name, and this is an @interface, we
30446 have a class extension. */
30447 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30449 *categ
= NULL_TREE
;
30450 *is_class_extension
= true;
30453 *categ
= cp_parser_identifier (parser
);
30455 parens
.require_close (parser
);
30459 /* Parse an Objective-C class interface. */
30462 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30464 tree name
, super
, categ
, protos
;
30465 bool is_class_extension
;
30467 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30468 name
= cp_parser_identifier (parser
);
30469 if (name
== error_mark_node
)
30471 /* It's hard to recover because even if valid @interface stuff
30472 is to follow, we can't compile it (or validate it) if we
30473 don't even know which class it refers to. Let's assume this
30474 was a stray '@interface' token in the stream and skip it.
30478 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30479 &is_class_extension
);
30480 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30482 /* We have either a class or a category on our hands. */
30483 if (categ
|| is_class_extension
)
30484 objc_start_category_interface (name
, categ
, protos
, attributes
);
30487 objc_start_class_interface (name
, super
, protos
, attributes
);
30488 /* Handle instance variable declarations, if any. */
30489 cp_parser_objc_class_ivars (parser
);
30490 objc_continue_interface ();
30493 cp_parser_objc_method_prototype_list (parser
);
30496 /* Parse an Objective-C class implementation. */
30499 cp_parser_objc_class_implementation (cp_parser
* parser
)
30501 tree name
, super
, categ
;
30502 bool is_class_extension
;
30504 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30505 name
= cp_parser_identifier (parser
);
30506 if (name
== error_mark_node
)
30508 /* It's hard to recover because even if valid @implementation
30509 stuff is to follow, we can't compile it (or validate it) if
30510 we don't even know which class it refers to. Let's assume
30511 this was a stray '@implementation' token in the stream and
30516 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30517 &is_class_extension
);
30519 /* We have either a class or a category on our hands. */
30521 objc_start_category_implementation (name
, categ
);
30524 objc_start_class_implementation (name
, super
);
30525 /* Handle instance variable declarations, if any. */
30526 cp_parser_objc_class_ivars (parser
);
30527 objc_continue_implementation ();
30530 cp_parser_objc_method_definition_list (parser
);
30533 /* Consume the @end token and finish off the implementation. */
30536 cp_parser_objc_end_implementation (cp_parser
* parser
)
30538 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30539 objc_finish_implementation ();
30542 /* Parse an Objective-C declaration. */
30545 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30547 /* Try to figure out what kind of declaration is present. */
30548 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30551 switch (kwd
->keyword
)
30556 error_at (kwd
->location
, "attributes may not be specified before"
30557 " the %<@%D%> Objective-C++ keyword",
30561 case RID_AT_IMPLEMENTATION
:
30562 warning_at (kwd
->location
, OPT_Wattributes
,
30563 "prefix attributes are ignored before %<@%D%>",
30570 switch (kwd
->keyword
)
30573 cp_parser_objc_alias_declaration (parser
);
30576 cp_parser_objc_class_declaration (parser
);
30578 case RID_AT_PROTOCOL
:
30579 cp_parser_objc_protocol_declaration (parser
, attributes
);
30581 case RID_AT_INTERFACE
:
30582 cp_parser_objc_class_interface (parser
, attributes
);
30584 case RID_AT_IMPLEMENTATION
:
30585 cp_parser_objc_class_implementation (parser
);
30588 cp_parser_objc_end_implementation (parser
);
30591 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30593 cp_parser_skip_to_end_of_block_or_statement (parser
);
30597 /* Parse an Objective-C try-catch-finally statement.
30599 objc-try-catch-finally-stmt:
30600 @try compound-statement objc-catch-clause-seq [opt]
30601 objc-finally-clause [opt]
30603 objc-catch-clause-seq:
30604 objc-catch-clause objc-catch-clause-seq [opt]
30607 @catch ( objc-exception-declaration ) compound-statement
30609 objc-finally-clause:
30610 @finally compound-statement
30612 objc-exception-declaration:
30613 parameter-declaration
30616 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30620 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30621 for C. Keep them in sync. */
30624 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30626 location_t location
;
30629 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30630 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30631 objc_maybe_warn_exceptions (location
);
30632 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30633 node, lest it get absorbed into the surrounding block. */
30634 stmt
= push_stmt_list ();
30635 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30636 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30638 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30640 cp_parameter_declarator
*parm
;
30641 tree parameter_declaration
= error_mark_node
;
30642 bool seen_open_paren
= false;
30643 matching_parens parens
;
30645 cp_lexer_consume_token (parser
->lexer
);
30646 if (parens
.require_open (parser
))
30647 seen_open_paren
= true;
30648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30650 /* We have "@catch (...)" (where the '...' are literally
30651 what is in the code). Skip the '...'.
30652 parameter_declaration is set to NULL_TREE, and
30653 objc_being_catch_clauses() knows that that means
30655 cp_lexer_consume_token (parser
->lexer
);
30656 parameter_declaration
= NULL_TREE
;
30660 /* We have "@catch (NSException *exception)" or something
30661 like that. Parse the parameter declaration. */
30662 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30664 parameter_declaration
= error_mark_node
;
30666 parameter_declaration
= grokdeclarator (parm
->declarator
,
30667 &parm
->decl_specifiers
,
30668 PARM
, /*initialized=*/0,
30669 /*attrlist=*/NULL
);
30671 if (seen_open_paren
)
30672 parens
.require_close (parser
);
30675 /* If there was no open parenthesis, we are recovering from
30676 an error, and we are trying to figure out what mistake
30677 the user has made. */
30679 /* If there is an immediate closing parenthesis, the user
30680 probably forgot the opening one (ie, they typed "@catch
30681 NSException *e)". Parse the closing parenthesis and keep
30683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30684 cp_lexer_consume_token (parser
->lexer
);
30686 /* If these is no immediate closing parenthesis, the user
30687 probably doesn't know that parenthesis are required at
30688 all (ie, they typed "@catch NSException *e"). So, just
30689 forget about the closing parenthesis and keep going. */
30691 objc_begin_catch_clause (parameter_declaration
);
30692 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30693 objc_finish_catch_clause ();
30695 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30697 cp_lexer_consume_token (parser
->lexer
);
30698 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30699 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30700 node, lest it get absorbed into the surrounding block. */
30701 stmt
= push_stmt_list ();
30702 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30703 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30706 return objc_finish_try_stmt ();
30709 /* Parse an Objective-C synchronized statement.
30711 objc-synchronized-stmt:
30712 @synchronized ( expression ) compound-statement
30714 Returns NULL_TREE. */
30717 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30719 location_t location
;
30722 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30724 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30725 objc_maybe_warn_exceptions (location
);
30726 matching_parens parens
;
30727 parens
.require_open (parser
);
30728 lock
= cp_parser_expression (parser
);
30729 parens
.require_close (parser
);
30731 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30732 node, lest it get absorbed into the surrounding block. */
30733 stmt
= push_stmt_list ();
30734 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30736 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30739 /* Parse an Objective-C throw statement.
30742 @throw assignment-expression [opt] ;
30744 Returns a constructed '@throw' statement. */
30747 cp_parser_objc_throw_statement (cp_parser
*parser
)
30749 tree expr
= NULL_TREE
;
30750 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30752 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30754 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30755 expr
= cp_parser_expression (parser
);
30757 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30759 return objc_build_throw_stmt (loc
, expr
);
30762 /* Parse an Objective-C statement. */
30765 cp_parser_objc_statement (cp_parser
* parser
)
30767 /* Try to figure out what kind of declaration is present. */
30768 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30770 switch (kwd
->keyword
)
30773 return cp_parser_objc_try_catch_finally_statement (parser
);
30774 case RID_AT_SYNCHRONIZED
:
30775 return cp_parser_objc_synchronized_statement (parser
);
30777 return cp_parser_objc_throw_statement (parser
);
30779 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30781 cp_parser_skip_to_end_of_block_or_statement (parser
);
30784 return error_mark_node
;
30787 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30788 look ahead to see if an objc keyword follows the attributes. This
30789 is to detect the use of prefix attributes on ObjC @interface and
30793 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30795 cp_lexer_save_tokens (parser
->lexer
);
30796 *attrib
= cp_parser_attributes_opt (parser
);
30797 gcc_assert (*attrib
);
30798 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30800 cp_lexer_commit_tokens (parser
->lexer
);
30803 cp_lexer_rollback_tokens (parser
->lexer
);
30807 /* This routine is a minimal replacement for
30808 c_parser_struct_declaration () used when parsing the list of
30809 types/names or ObjC++ properties. For example, when parsing the
30812 @property (readonly) int a, b, c;
30814 this function is responsible for parsing "int a, int b, int c" and
30815 returning the declarations as CHAIN of DECLs.
30817 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30818 similar parsing. */
30820 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30822 tree decls
= NULL_TREE
;
30823 cp_decl_specifier_seq declspecs
;
30824 int decl_class_or_enum_p
;
30825 tree prefix_attributes
;
30827 cp_parser_decl_specifier_seq (parser
,
30828 CP_PARSER_FLAGS_NONE
,
30830 &decl_class_or_enum_p
);
30832 if (declspecs
.type
== error_mark_node
)
30833 return error_mark_node
;
30835 /* auto, register, static, extern, mutable. */
30836 if (declspecs
.storage_class
!= sc_none
)
30838 cp_parser_error (parser
, "invalid type for property");
30839 declspecs
.storage_class
= sc_none
;
30842 /* thread_local. */
30843 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30845 cp_parser_error (parser
, "invalid type for property");
30846 declspecs
.locations
[ds_thread
] = 0;
30850 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30852 cp_parser_error (parser
, "invalid type for property");
30853 declspecs
.locations
[ds_typedef
] = 0;
30856 prefix_attributes
= declspecs
.attributes
;
30857 declspecs
.attributes
= NULL_TREE
;
30859 /* Keep going until we hit the `;' at the end of the declaration. */
30860 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30862 tree attributes
, first_attribute
, decl
;
30863 cp_declarator
*declarator
;
30866 /* Parse the declarator. */
30867 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30868 NULL
, NULL
, false, false);
30870 /* Look for attributes that apply to the ivar. */
30871 attributes
= cp_parser_attributes_opt (parser
);
30872 /* Remember which attributes are prefix attributes and
30874 first_attribute
= attributes
;
30875 /* Combine the attributes. */
30876 attributes
= chainon (prefix_attributes
, attributes
);
30878 decl
= grokfield (declarator
, &declspecs
,
30879 NULL_TREE
, /*init_const_expr_p=*/false,
30880 NULL_TREE
, attributes
);
30882 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30883 return error_mark_node
;
30885 /* Reset PREFIX_ATTRIBUTES. */
30886 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30887 attributes
= TREE_CHAIN (attributes
);
30889 TREE_CHAIN (attributes
) = NULL_TREE
;
30891 DECL_CHAIN (decl
) = decls
;
30894 token
= cp_lexer_peek_token (parser
->lexer
);
30895 if (token
->type
== CPP_COMMA
)
30897 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30906 /* Parse an Objective-C @property declaration. The syntax is:
30908 objc-property-declaration:
30909 '@property' objc-property-attributes[opt] struct-declaration ;
30911 objc-property-attributes:
30912 '(' objc-property-attribute-list ')'
30914 objc-property-attribute-list:
30915 objc-property-attribute
30916 objc-property-attribute-list, objc-property-attribute
30918 objc-property-attribute
30919 'getter' = identifier
30920 'setter' = identifier
30929 @property NSString *name;
30930 @property (readonly) id object;
30931 @property (retain, nonatomic, getter=getTheName) id name;
30932 @property int a, b, c;
30934 PS: This function is identical to
30935 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30937 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30939 /* The following variables hold the attributes of the properties as
30940 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30941 seen. When we see an attribute, we set them to 'true' (if they
30942 are boolean properties) or to the identifier (if they have an
30943 argument, ie, for getter and setter). Note that here we only
30944 parse the list of attributes, check the syntax and accumulate the
30945 attributes that we find. objc_add_property_declaration() will
30946 then process the information. */
30947 bool property_assign
= false;
30948 bool property_copy
= false;
30949 tree property_getter_ident
= NULL_TREE
;
30950 bool property_nonatomic
= false;
30951 bool property_readonly
= false;
30952 bool property_readwrite
= false;
30953 bool property_retain
= false;
30954 tree property_setter_ident
= NULL_TREE
;
30956 /* 'properties' is the list of properties that we read. Usually a
30957 single one, but maybe more (eg, in "@property int a, b, c;" there
30962 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30964 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30966 /* Parse the optional attribute list... */
30967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30970 matching_parens parens
;
30971 parens
.consume_open (parser
);
30975 bool syntax_error
= false;
30976 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30979 if (token
->type
!= CPP_NAME
)
30981 cp_parser_error (parser
, "expected identifier");
30984 keyword
= C_RID_CODE (token
->u
.value
);
30985 cp_lexer_consume_token (parser
->lexer
);
30988 case RID_ASSIGN
: property_assign
= true; break;
30989 case RID_COPY
: property_copy
= true; break;
30990 case RID_NONATOMIC
: property_nonatomic
= true; break;
30991 case RID_READONLY
: property_readonly
= true; break;
30992 case RID_READWRITE
: property_readwrite
= true; break;
30993 case RID_RETAIN
: property_retain
= true; break;
30997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30999 if (keyword
== RID_GETTER
)
31000 cp_parser_error (parser
,
31001 "missing %<=%> (after %<getter%> attribute)");
31003 cp_parser_error (parser
,
31004 "missing %<=%> (after %<setter%> attribute)");
31005 syntax_error
= true;
31008 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31009 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31011 cp_parser_error (parser
, "expected identifier");
31012 syntax_error
= true;
31015 if (keyword
== RID_SETTER
)
31017 if (property_setter_ident
!= NULL_TREE
)
31019 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31020 cp_lexer_consume_token (parser
->lexer
);
31023 property_setter_ident
= cp_parser_objc_selector (parser
);
31024 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31025 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31027 cp_lexer_consume_token (parser
->lexer
);
31031 if (property_getter_ident
!= NULL_TREE
)
31033 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31034 cp_lexer_consume_token (parser
->lexer
);
31037 property_getter_ident
= cp_parser_objc_selector (parser
);
31041 cp_parser_error (parser
, "unknown property attribute");
31042 syntax_error
= true;
31049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31050 cp_lexer_consume_token (parser
->lexer
);
31055 /* FIXME: "@property (setter, assign);" will generate a spurious
31056 "error: expected ‘)’ before ‘,’ token". This is because
31057 cp_parser_require, unlike the C counterpart, will produce an
31058 error even if we are in error recovery. */
31059 if (!parens
.require_close (parser
))
31061 cp_parser_skip_to_closing_parenthesis (parser
,
31062 /*recovering=*/true,
31063 /*or_comma=*/false,
31064 /*consume_paren=*/true);
31068 /* ... and the property declaration(s). */
31069 properties
= cp_parser_objc_struct_declaration (parser
);
31071 if (properties
== error_mark_node
)
31073 cp_parser_skip_to_end_of_statement (parser
);
31074 /* If the next token is now a `;', consume it. */
31075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31076 cp_lexer_consume_token (parser
->lexer
);
31080 if (properties
== NULL_TREE
)
31081 cp_parser_error (parser
, "expected identifier");
31084 /* Comma-separated properties are chained together in
31085 reverse order; add them one by one. */
31086 properties
= nreverse (properties
);
31088 for (; properties
; properties
= TREE_CHAIN (properties
))
31089 objc_add_property_declaration (loc
, copy_node (properties
),
31090 property_readonly
, property_readwrite
,
31091 property_assign
, property_retain
,
31092 property_copy
, property_nonatomic
,
31093 property_getter_ident
, property_setter_ident
);
31096 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31099 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31101 objc-synthesize-declaration:
31102 @synthesize objc-synthesize-identifier-list ;
31104 objc-synthesize-identifier-list:
31105 objc-synthesize-identifier
31106 objc-synthesize-identifier-list, objc-synthesize-identifier
31108 objc-synthesize-identifier
31110 identifier = identifier
31113 @synthesize MyProperty;
31114 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31116 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31117 for C. Keep them in sync.
31120 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31122 tree list
= NULL_TREE
;
31124 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31126 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31129 tree property
, ivar
;
31130 property
= cp_parser_identifier (parser
);
31131 if (property
== error_mark_node
)
31133 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31138 cp_lexer_consume_token (parser
->lexer
);
31139 ivar
= cp_parser_identifier (parser
);
31140 if (ivar
== error_mark_node
)
31142 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31148 list
= chainon (list
, build_tree_list (ivar
, property
));
31149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31150 cp_lexer_consume_token (parser
->lexer
);
31154 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31155 objc_add_synthesize_declaration (loc
, list
);
31158 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31160 objc-dynamic-declaration:
31161 @dynamic identifier-list ;
31164 @dynamic MyProperty;
31165 @dynamic MyProperty, AnotherProperty;
31167 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31168 for C. Keep them in sync.
31171 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31173 tree list
= NULL_TREE
;
31175 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31177 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31181 property
= cp_parser_identifier (parser
);
31182 if (property
== error_mark_node
)
31184 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31187 list
= chainon (list
, build_tree_list (NULL
, property
));
31188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31189 cp_lexer_consume_token (parser
->lexer
);
31193 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31194 objc_add_dynamic_declaration (loc
, list
);
31198 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31200 /* Returns name of the next clause.
31201 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31202 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31203 returned and the token is consumed. */
31205 static pragma_omp_clause
31206 cp_parser_omp_clause_name (cp_parser
*parser
)
31208 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31210 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31211 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31212 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31213 result
= PRAGMA_OMP_CLAUSE_IF
;
31214 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31215 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31216 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31217 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31218 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31219 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31220 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31221 result
= PRAGMA_OMP_CLAUSE_FOR
;
31222 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31224 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31225 const char *p
= IDENTIFIER_POINTER (id
);
31230 if (!strcmp ("aligned", p
))
31231 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31232 else if (!strcmp ("async", p
))
31233 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31236 if (!strcmp ("collapse", p
))
31237 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31238 else if (!strcmp ("copy", p
))
31239 result
= PRAGMA_OACC_CLAUSE_COPY
;
31240 else if (!strcmp ("copyin", p
))
31241 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31242 else if (!strcmp ("copyout", p
))
31243 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31244 else if (!strcmp ("copyprivate", p
))
31245 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31246 else if (!strcmp ("create", p
))
31247 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31250 if (!strcmp ("defaultmap", p
))
31251 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31252 else if (!strcmp ("depend", p
))
31253 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31254 else if (!strcmp ("device", p
))
31255 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31256 else if (!strcmp ("deviceptr", p
))
31257 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31258 else if (!strcmp ("device_resident", p
))
31259 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31260 else if (!strcmp ("dist_schedule", p
))
31261 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31264 if (!strcmp ("final", p
))
31265 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31266 else if (!strcmp ("firstprivate", p
))
31267 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31268 else if (!strcmp ("from", p
))
31269 result
= PRAGMA_OMP_CLAUSE_FROM
;
31272 if (!strcmp ("gang", p
))
31273 result
= PRAGMA_OACC_CLAUSE_GANG
;
31274 else if (!strcmp ("grainsize", p
))
31275 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31278 if (!strcmp ("hint", p
))
31279 result
= PRAGMA_OMP_CLAUSE_HINT
;
31280 else if (!strcmp ("host", p
))
31281 result
= PRAGMA_OACC_CLAUSE_HOST
;
31284 if (!strcmp ("inbranch", p
))
31285 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31286 else if (!strcmp ("independent", p
))
31287 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31288 else if (!strcmp ("is_device_ptr", p
))
31289 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31292 if (!strcmp ("lastprivate", p
))
31293 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31294 else if (!strcmp ("linear", p
))
31295 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31296 else if (!strcmp ("link", p
))
31297 result
= PRAGMA_OMP_CLAUSE_LINK
;
31300 if (!strcmp ("map", p
))
31301 result
= PRAGMA_OMP_CLAUSE_MAP
;
31302 else if (!strcmp ("mergeable", p
))
31303 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31304 else if (flag_cilkplus
&& !strcmp ("mask", p
))
31305 result
= PRAGMA_CILK_CLAUSE_MASK
;
31308 if (!strcmp ("nogroup", p
))
31309 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31310 else if (!strcmp ("notinbranch", p
))
31311 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31312 else if (!strcmp ("nowait", p
))
31313 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31314 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
31315 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
31316 else if (!strcmp ("num_gangs", p
))
31317 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31318 else if (!strcmp ("num_tasks", p
))
31319 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31320 else if (!strcmp ("num_teams", p
))
31321 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31322 else if (!strcmp ("num_threads", p
))
31323 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31324 else if (!strcmp ("num_workers", p
))
31325 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31328 if (!strcmp ("ordered", p
))
31329 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31332 if (!strcmp ("parallel", p
))
31333 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31334 else if (!strcmp ("present", p
))
31335 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31336 else if (!strcmp ("present_or_copy", p
)
31337 || !strcmp ("pcopy", p
))
31338 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31339 else if (!strcmp ("present_or_copyin", p
)
31340 || !strcmp ("pcopyin", p
))
31341 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31342 else if (!strcmp ("present_or_copyout", p
)
31343 || !strcmp ("pcopyout", p
))
31344 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31345 else if (!strcmp ("present_or_create", p
)
31346 || !strcmp ("pcreate", p
))
31347 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31348 else if (!strcmp ("priority", p
))
31349 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31350 else if (!strcmp ("proc_bind", p
))
31351 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31354 if (!strcmp ("reduction", p
))
31355 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31358 if (!strcmp ("safelen", p
))
31359 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31360 else if (!strcmp ("schedule", p
))
31361 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31362 else if (!strcmp ("sections", p
))
31363 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31364 else if (!strcmp ("self", p
))
31365 result
= PRAGMA_OACC_CLAUSE_SELF
;
31366 else if (!strcmp ("seq", p
))
31367 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31368 else if (!strcmp ("shared", p
))
31369 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31370 else if (!strcmp ("simd", p
))
31371 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31372 else if (!strcmp ("simdlen", p
))
31373 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31376 if (!strcmp ("taskgroup", p
))
31377 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31378 else if (!strcmp ("thread_limit", p
))
31379 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31380 else if (!strcmp ("threads", p
))
31381 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31382 else if (!strcmp ("tile", p
))
31383 result
= PRAGMA_OACC_CLAUSE_TILE
;
31384 else if (!strcmp ("to", p
))
31385 result
= PRAGMA_OMP_CLAUSE_TO
;
31388 if (!strcmp ("uniform", p
))
31389 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31390 else if (!strcmp ("untied", p
))
31391 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31392 else if (!strcmp ("use_device", p
))
31393 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31394 else if (!strcmp ("use_device_ptr", p
))
31395 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31398 if (!strcmp ("vector", p
))
31399 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31400 else if (!strcmp ("vector_length", p
))
31401 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31402 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
31403 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
31406 if (!strcmp ("wait", p
))
31407 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31408 else if (!strcmp ("worker", p
))
31409 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31414 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31415 cp_lexer_consume_token (parser
->lexer
);
31420 /* Validate that a clause of the given type does not already exist. */
31423 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31424 const char *name
, location_t location
)
31428 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31429 if (OMP_CLAUSE_CODE (c
) == code
)
31431 error_at (location
, "too many %qs clauses", name
);
31439 variable-list , identifier
31441 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31442 colon). An opening parenthesis will have been consumed by the caller.
31444 If KIND is nonzero, create the appropriate node and install the decl
31445 in OMP_CLAUSE_DECL and add the node to the head of the list.
31447 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31448 return the list created.
31450 COLON can be NULL if only closing parenthesis should end the list,
31451 or pointer to bool which will receive false if the list is terminated
31452 by closing parenthesis or true if the list is terminated by colon. */
31455 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31456 tree list
, bool *colon
)
31459 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31462 parser
->colon_corrects_to_scope_p
= false;
31469 token
= cp_lexer_peek_token (parser
->lexer
);
31471 && current_class_ptr
31472 && cp_parser_is_keyword (token
, RID_THIS
))
31474 decl
= finish_this_expr ();
31475 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31476 || CONVERT_EXPR_P (decl
))
31477 decl
= TREE_OPERAND (decl
, 0);
31478 cp_lexer_consume_token (parser
->lexer
);
31482 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31483 /*check_dependency_p=*/true,
31484 /*template_p=*/NULL
,
31485 /*declarator_p=*/false,
31486 /*optional_p=*/false);
31487 if (name
== error_mark_node
)
31490 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31491 if (decl
== error_mark_node
)
31492 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31495 if (decl
== error_mark_node
)
31497 else if (kind
!= 0)
31501 case OMP_CLAUSE__CACHE_
:
31502 /* The OpenACC cache directive explicitly only allows "array
31503 elements or subarrays". */
31504 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31506 error_at (token
->location
, "expected %<[%>");
31507 decl
= error_mark_node
;
31511 case OMP_CLAUSE_MAP
:
31512 case OMP_CLAUSE_FROM
:
31513 case OMP_CLAUSE_TO
:
31514 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31517 = cp_lexer_peek_token (parser
->lexer
)->location
;
31518 cp_id_kind idk
= CP_ID_KIND_NONE
;
31519 cp_lexer_consume_token (parser
->lexer
);
31520 decl
= convert_from_reference (decl
);
31522 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31527 case OMP_CLAUSE_DEPEND
:
31528 case OMP_CLAUSE_REDUCTION
:
31529 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31531 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31533 parser
->colon_corrects_to_scope_p
= false;
31534 cp_lexer_consume_token (parser
->lexer
);
31535 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31536 low_bound
= cp_parser_expression (parser
);
31538 parser
->colon_corrects_to_scope_p
31539 = saved_colon_corrects_to_scope_p
;
31540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31541 length
= integer_one_node
;
31544 /* Look for `:'. */
31545 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31547 if (!cp_lexer_next_token_is (parser
->lexer
,
31549 length
= cp_parser_expression (parser
);
31551 /* Look for the closing `]'. */
31552 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31556 decl
= tree_cons (low_bound
, length
, decl
);
31563 tree u
= build_omp_clause (token
->location
, kind
);
31564 OMP_CLAUSE_DECL (u
) = decl
;
31565 OMP_CLAUSE_CHAIN (u
) = list
;
31569 list
= tree_cons (decl
, NULL_TREE
, list
);
31572 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31574 cp_lexer_consume_token (parser
->lexer
);
31578 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31580 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31583 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31587 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31591 /* Try to resync to an unnested comma. Copied from
31592 cp_parser_parenthesized_expression_list. */
31595 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31596 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31597 /*recovering=*/true,
31599 /*consume_paren=*/true);
31607 /* Similarly, but expect leading and trailing parenthesis. This is a very
31608 common case for omp clauses. */
31611 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31613 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31614 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31619 copy ( variable-list )
31620 copyin ( variable-list )
31621 copyout ( variable-list )
31622 create ( variable-list )
31623 delete ( variable-list )
31624 present ( variable-list )
31625 present_or_copy ( variable-list )
31626 pcopy ( variable-list )
31627 present_or_copyin ( variable-list )
31628 pcopyin ( variable-list )
31629 present_or_copyout ( variable-list )
31630 pcopyout ( variable-list )
31631 present_or_create ( variable-list )
31632 pcreate ( variable-list ) */
31635 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31638 enum gomp_map_kind kind
;
31641 case PRAGMA_OACC_CLAUSE_COPY
:
31642 kind
= GOMP_MAP_FORCE_TOFROM
;
31644 case PRAGMA_OACC_CLAUSE_COPYIN
:
31645 kind
= GOMP_MAP_FORCE_TO
;
31647 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31648 kind
= GOMP_MAP_FORCE_FROM
;
31650 case PRAGMA_OACC_CLAUSE_CREATE
:
31651 kind
= GOMP_MAP_FORCE_ALLOC
;
31653 case PRAGMA_OACC_CLAUSE_DELETE
:
31654 kind
= GOMP_MAP_DELETE
;
31656 case PRAGMA_OACC_CLAUSE_DEVICE
:
31657 kind
= GOMP_MAP_FORCE_TO
;
31659 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31660 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31662 case PRAGMA_OACC_CLAUSE_HOST
:
31663 case PRAGMA_OACC_CLAUSE_SELF
:
31664 kind
= GOMP_MAP_FORCE_FROM
;
31666 case PRAGMA_OACC_CLAUSE_LINK
:
31667 kind
= GOMP_MAP_LINK
;
31669 case PRAGMA_OACC_CLAUSE_PRESENT
:
31670 kind
= GOMP_MAP_FORCE_PRESENT
;
31672 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31673 kind
= GOMP_MAP_TOFROM
;
31675 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31676 kind
= GOMP_MAP_TO
;
31678 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31679 kind
= GOMP_MAP_FROM
;
31681 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31682 kind
= GOMP_MAP_ALLOC
;
31685 gcc_unreachable ();
31688 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31690 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31691 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31697 deviceptr ( variable-list ) */
31700 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31702 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31705 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31706 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31707 variable-list must only allow for pointer variables. */
31708 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31709 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31711 tree v
= TREE_PURPOSE (t
);
31712 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31713 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31714 OMP_CLAUSE_DECL (u
) = v
;
31715 OMP_CLAUSE_CHAIN (u
) = list
;
31729 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31730 enum omp_clause_code code
,
31731 tree list
, location_t location
)
31733 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31734 tree c
= build_omp_clause (location
, code
);
31735 OMP_CLAUSE_CHAIN (c
) = list
;
31740 num_gangs ( expression )
31741 num_workers ( expression )
31742 vector_length ( expression ) */
31745 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31746 const char *str
, tree list
)
31748 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31750 matching_parens parens
;
31751 if (!parens
.require_open (parser
))
31754 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31756 if (t
== error_mark_node
31757 || !parens
.require_close (parser
))
31759 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31760 /*or_comma=*/false,
31761 /*consume_paren=*/true);
31765 check_no_duplicate_clause (list
, code
, str
, loc
);
31767 tree c
= build_omp_clause (loc
, code
);
31768 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31769 OMP_CLAUSE_CHAIN (c
) = list
;
31775 gang [( gang-arg-list )]
31776 worker [( [num:] int-expr )]
31777 vector [( [length:] int-expr )]
31779 where gang-arg is one of:
31784 and size-expr may be:
31791 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31792 const char *str
, tree list
)
31794 const char *id
= "num";
31795 cp_lexer
*lexer
= parser
->lexer
;
31796 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31797 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31799 if (kind
== OMP_CLAUSE_VECTOR
)
31802 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31804 matching_parens parens
;
31805 parens
.consume_open (parser
);
31809 cp_token
*next
= cp_lexer_peek_token (lexer
);
31812 /* Gang static argument. */
31813 if (kind
== OMP_CLAUSE_GANG
31814 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31816 cp_lexer_consume_token (lexer
);
31818 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31819 goto cleanup_error
;
31822 if (ops
[idx
] != NULL
)
31824 cp_parser_error (parser
, "too many %<static%> arguments");
31825 goto cleanup_error
;
31828 /* Check for the '*' argument. */
31829 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31830 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31831 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31834 cp_lexer_consume_token (lexer
);
31835 ops
[idx
] = integer_minus_one_node
;
31837 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31839 cp_lexer_consume_token (lexer
);
31845 /* Worker num: argument and vector length: arguments. */
31846 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31847 && id_equal (next
->u
.value
, id
)
31848 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31850 cp_lexer_consume_token (lexer
); /* id */
31851 cp_lexer_consume_token (lexer
); /* ':' */
31854 /* Now collect the actual argument. */
31855 if (ops
[idx
] != NULL_TREE
)
31857 cp_parser_error (parser
, "unexpected argument");
31858 goto cleanup_error
;
31861 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31863 if (expr
== error_mark_node
)
31864 goto cleanup_error
;
31866 mark_exp_read (expr
);
31869 if (kind
== OMP_CLAUSE_GANG
31870 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31872 cp_lexer_consume_token (lexer
);
31879 if (!parens
.require_close (parser
))
31880 goto cleanup_error
;
31883 check_no_duplicate_clause (list
, kind
, str
, loc
);
31885 c
= build_omp_clause (loc
, kind
);
31888 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31890 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31891 OMP_CLAUSE_CHAIN (c
) = list
;
31896 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31901 tile ( size-expr-list ) */
31904 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31906 tree c
, expr
= error_mark_node
;
31907 tree tile
= NULL_TREE
;
31909 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31910 so, but the spec authors never considered such a case and have
31911 differing opinions on what it might mean, including 'not
31913 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31914 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31917 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31922 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31926 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31927 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31929 cp_lexer_consume_token (parser
->lexer
);
31930 expr
= integer_zero_node
;
31933 expr
= cp_parser_constant_expression (parser
);
31935 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31937 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31939 /* Consume the trailing ')'. */
31940 cp_lexer_consume_token (parser
->lexer
);
31942 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31943 tile
= nreverse (tile
);
31944 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31945 OMP_CLAUSE_CHAIN (c
) = list
;
31950 Parse wait clause or directive parameters. */
31953 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31955 vec
<tree
, va_gc
> *args
;
31958 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31960 /*allow_expansion_p=*/true,
31961 /*non_constant_p=*/NULL
);
31963 if (args
== NULL
|| args
->length () == 0)
31965 cp_parser_error (parser
, "expected integer expression before ')'");
31967 release_tree_vector (args
);
31971 args_tree
= build_tree_list_vec (args
);
31973 release_tree_vector (args
);
31975 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31977 tree targ
= TREE_VALUE (t
);
31979 if (targ
!= error_mark_node
)
31981 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31982 error ("%<wait%> expression must be integral");
31985 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31987 targ
= mark_rvalue_use (targ
);
31988 OMP_CLAUSE_DECL (c
) = targ
;
31989 OMP_CLAUSE_CHAIN (c
) = list
;
31999 wait ( int-expr-list ) */
32002 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32004 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32006 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32009 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32015 collapse ( constant-expression ) */
32018 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32024 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32025 matching_parens parens
;
32026 if (!parens
.require_open (parser
))
32029 num
= cp_parser_constant_expression (parser
);
32031 if (!parens
.require_close (parser
))
32032 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32033 /*or_comma=*/false,
32034 /*consume_paren=*/true);
32036 if (num
== error_mark_node
)
32038 num
= fold_non_dependent_expr (num
);
32039 if (!tree_fits_shwi_p (num
)
32040 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32041 || (n
= tree_to_shwi (num
)) <= 0
32044 error_at (loc
, "collapse argument needs positive constant integer expression");
32048 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32049 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32050 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32051 OMP_CLAUSE_CHAIN (c
) = list
;
32052 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32058 default ( none | shared )
32061 default ( none | present ) */
32064 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32065 location_t location
, bool is_oacc
)
32067 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32070 matching_parens parens
;
32071 if (!parens
.require_open (parser
))
32073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32075 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32076 const char *p
= IDENTIFIER_POINTER (id
);
32081 if (strcmp ("none", p
) != 0)
32083 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32087 if (strcmp ("present", p
) != 0 || !is_oacc
)
32089 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32093 if (strcmp ("shared", p
) != 0 || is_oacc
)
32095 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32102 cp_lexer_consume_token (parser
->lexer
);
32108 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32110 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32113 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32114 || !parens
.require_close (parser
))
32115 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32116 /*or_comma=*/false,
32117 /*consume_paren=*/true);
32119 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32122 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32123 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32124 OMP_CLAUSE_CHAIN (c
) = list
;
32125 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32131 final ( expression ) */
32134 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32138 matching_parens parens
;
32139 if (!parens
.require_open (parser
))
32142 t
= cp_parser_condition (parser
);
32144 if (t
== error_mark_node
32145 || !parens
.require_close (parser
))
32146 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32147 /*or_comma=*/false,
32148 /*consume_paren=*/true);
32150 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32152 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32153 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32154 OMP_CLAUSE_CHAIN (c
) = list
;
32163 if ( directive-name-modifier : expression )
32165 directive-name-modifier:
32166 parallel | task | taskloop | target data | target | target update
32167 | target enter data | target exit data */
32170 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32174 enum tree_code if_modifier
= ERROR_MARK
;
32176 matching_parens parens
;
32177 if (!parens
.require_open (parser
))
32180 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32182 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32183 const char *p
= IDENTIFIER_POINTER (id
);
32186 if (strcmp ("parallel", p
) == 0)
32187 if_modifier
= OMP_PARALLEL
;
32188 else if (strcmp ("task", p
) == 0)
32189 if_modifier
= OMP_TASK
;
32190 else if (strcmp ("taskloop", p
) == 0)
32191 if_modifier
= OMP_TASKLOOP
;
32192 else if (strcmp ("target", p
) == 0)
32194 if_modifier
= OMP_TARGET
;
32195 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32197 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32198 p
= IDENTIFIER_POINTER (id
);
32199 if (strcmp ("data", p
) == 0)
32200 if_modifier
= OMP_TARGET_DATA
;
32201 else if (strcmp ("update", p
) == 0)
32202 if_modifier
= OMP_TARGET_UPDATE
;
32203 else if (strcmp ("enter", p
) == 0)
32204 if_modifier
= OMP_TARGET_ENTER_DATA
;
32205 else if (strcmp ("exit", p
) == 0)
32206 if_modifier
= OMP_TARGET_EXIT_DATA
;
32207 if (if_modifier
!= OMP_TARGET
)
32212 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32213 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32215 if_modifier
= ERROR_MARK
;
32217 if (if_modifier
== OMP_TARGET_ENTER_DATA
32218 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32220 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32222 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32223 p
= IDENTIFIER_POINTER (id
);
32224 if (strcmp ("data", p
) == 0)
32230 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32231 error_at (loc
, "expected %<data%>");
32232 if_modifier
= ERROR_MARK
;
32237 if (if_modifier
!= ERROR_MARK
)
32239 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32242 cp_lexer_consume_token (parser
->lexer
);
32249 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32250 error_at (loc
, "expected %<:%>");
32252 if_modifier
= ERROR_MARK
;
32257 t
= cp_parser_condition (parser
);
32259 if (t
== error_mark_node
32260 || !parens
.require_close (parser
))
32261 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32262 /*or_comma=*/false,
32263 /*consume_paren=*/true);
32265 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32266 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32268 if (if_modifier
!= ERROR_MARK
32269 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32271 const char *p
= NULL
;
32272 switch (if_modifier
)
32274 case OMP_PARALLEL
: p
= "parallel"; break;
32275 case OMP_TASK
: p
= "task"; break;
32276 case OMP_TASKLOOP
: p
= "taskloop"; break;
32277 case OMP_TARGET_DATA
: p
= "target data"; break;
32278 case OMP_TARGET
: p
= "target"; break;
32279 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32280 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32281 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32282 default: gcc_unreachable ();
32284 error_at (location
, "too many %<if%> clauses with %qs modifier",
32288 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32291 error_at (location
, "too many %<if%> clauses");
32293 error_at (location
, "too many %<if%> clauses without modifier");
32296 else if (if_modifier
== ERROR_MARK
32297 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32299 error_at (location
, "if any %<if%> clause has modifier, then all "
32300 "%<if%> clauses have to use modifier");
32305 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32306 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32307 OMP_CLAUSE_IF_EXPR (c
) = t
;
32308 OMP_CLAUSE_CHAIN (c
) = list
;
32317 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32318 tree list
, location_t location
)
32322 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32325 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32326 OMP_CLAUSE_CHAIN (c
) = list
;
32334 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32335 tree list
, location_t location
)
32339 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32341 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32342 OMP_CLAUSE_CHAIN (c
) = list
;
32347 num_threads ( expression ) */
32350 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32351 location_t location
)
32355 matching_parens parens
;
32356 if (!parens
.require_open (parser
))
32359 t
= cp_parser_expression (parser
);
32361 if (t
== error_mark_node
32362 || !parens
.require_close (parser
))
32363 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32364 /*or_comma=*/false,
32365 /*consume_paren=*/true);
32367 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32368 "num_threads", location
);
32370 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32371 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32372 OMP_CLAUSE_CHAIN (c
) = list
;
32378 num_tasks ( expression ) */
32381 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32382 location_t location
)
32386 matching_parens parens
;
32387 if (!parens
.require_open (parser
))
32390 t
= cp_parser_expression (parser
);
32392 if (t
== error_mark_node
32393 || !parens
.require_close (parser
))
32394 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32395 /*or_comma=*/false,
32396 /*consume_paren=*/true);
32398 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32399 "num_tasks", location
);
32401 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32402 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32403 OMP_CLAUSE_CHAIN (c
) = list
;
32409 grainsize ( expression ) */
32412 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32413 location_t location
)
32417 matching_parens parens
;
32418 if (!parens
.require_open (parser
))
32421 t
= cp_parser_expression (parser
);
32423 if (t
== error_mark_node
32424 || !parens
.require_close (parser
))
32425 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32426 /*or_comma=*/false,
32427 /*consume_paren=*/true);
32429 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32430 "grainsize", location
);
32432 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32433 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32434 OMP_CLAUSE_CHAIN (c
) = list
;
32440 priority ( expression ) */
32443 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32444 location_t location
)
32448 matching_parens parens
;
32449 if (!parens
.require_open (parser
))
32452 t
= cp_parser_expression (parser
);
32454 if (t
== error_mark_node
32455 || !parens
.require_close (parser
))
32456 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32457 /*or_comma=*/false,
32458 /*consume_paren=*/true);
32460 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32461 "priority", location
);
32463 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32464 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32465 OMP_CLAUSE_CHAIN (c
) = list
;
32471 hint ( expression ) */
32474 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32475 location_t location
)
32479 matching_parens parens
;
32480 if (!parens
.require_open (parser
))
32483 t
= cp_parser_expression (parser
);
32485 if (t
== error_mark_node
32486 || !parens
.require_close (parser
))
32487 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32488 /*or_comma=*/false,
32489 /*consume_paren=*/true);
32491 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32493 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32494 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32495 OMP_CLAUSE_CHAIN (c
) = list
;
32501 defaultmap ( tofrom : scalar ) */
32504 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32505 location_t location
)
32510 matching_parens parens
;
32511 if (!parens
.require_open (parser
))
32514 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32516 cp_parser_error (parser
, "expected %<tofrom%>");
32519 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32520 p
= IDENTIFIER_POINTER (id
);
32521 if (strcmp (p
, "tofrom") != 0)
32523 cp_parser_error (parser
, "expected %<tofrom%>");
32526 cp_lexer_consume_token (parser
->lexer
);
32527 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32530 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32532 cp_parser_error (parser
, "expected %<scalar%>");
32535 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32536 p
= IDENTIFIER_POINTER (id
);
32537 if (strcmp (p
, "scalar") != 0)
32539 cp_parser_error (parser
, "expected %<scalar%>");
32542 cp_lexer_consume_token (parser
->lexer
);
32543 if (!parens
.require_close (parser
))
32546 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32549 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32550 OMP_CLAUSE_CHAIN (c
) = list
;
32554 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32555 /*or_comma=*/false,
32556 /*consume_paren=*/true);
32564 ordered ( constant-expression ) */
32567 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32568 tree list
, location_t location
)
32570 tree c
, num
= NULL_TREE
;
32573 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32574 "ordered", location
);
32576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32578 matching_parens parens
;
32579 parens
.consume_open (parser
);
32581 num
= cp_parser_constant_expression (parser
);
32583 if (!parens
.require_close (parser
))
32584 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32585 /*or_comma=*/false,
32586 /*consume_paren=*/true);
32588 if (num
== error_mark_node
)
32590 num
= fold_non_dependent_expr (num
);
32591 if (!tree_fits_shwi_p (num
)
32592 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32593 || (n
= tree_to_shwi (num
)) <= 0
32596 error_at (location
,
32597 "ordered argument needs positive constant integer "
32603 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32604 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32605 OMP_CLAUSE_CHAIN (c
) = list
;
32610 reduction ( reduction-operator : variable-list )
32612 reduction-operator:
32613 One of: + * - & ^ | && ||
32617 reduction-operator:
32618 One of: + * - & ^ | && || min max
32622 reduction-operator:
32623 One of: + * - & ^ | && ||
32627 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32629 enum tree_code code
= ERROR_MARK
;
32630 tree nlist
, c
, id
= NULL_TREE
;
32632 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32635 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32637 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32638 case CPP_MULT
: code
= MULT_EXPR
; break;
32639 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32640 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32641 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32642 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32643 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32644 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32648 if (code
!= ERROR_MARK
)
32649 cp_lexer_consume_token (parser
->lexer
);
32652 bool saved_colon_corrects_to_scope_p
;
32653 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32654 parser
->colon_corrects_to_scope_p
= false;
32655 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32656 /*check_dependency_p=*/true,
32657 /*template_p=*/NULL
,
32658 /*declarator_p=*/false,
32659 /*optional_p=*/false);
32660 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32661 if (identifier_p (id
))
32663 const char *p
= IDENTIFIER_POINTER (id
);
32665 if (strcmp (p
, "min") == 0)
32667 else if (strcmp (p
, "max") == 0)
32669 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32671 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32673 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32675 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32676 code
= BIT_AND_EXPR
;
32677 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32678 code
= BIT_IOR_EXPR
;
32679 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32680 code
= BIT_XOR_EXPR
;
32681 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32682 code
= TRUTH_ANDIF_EXPR
;
32683 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32684 code
= TRUTH_ORIF_EXPR
;
32685 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32686 tree scope
= parser
->scope
;
32688 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32689 parser
->scope
= NULL_TREE
;
32690 parser
->qualifying_scope
= NULL_TREE
;
32691 parser
->object_scope
= NULL_TREE
;
32695 error ("invalid reduction-identifier");
32697 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32698 /*or_comma=*/false,
32699 /*consume_paren=*/true);
32704 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32707 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32709 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32711 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32712 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32719 schedule ( schedule-kind )
32720 schedule ( schedule-kind , expression )
32723 static | dynamic | guided | runtime | auto
32726 schedule ( schedule-modifier : schedule-kind )
32727 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32735 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32738 int modifiers
= 0, nmodifiers
= 0;
32740 matching_parens parens
;
32741 if (!parens
.require_open (parser
))
32744 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32746 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32748 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32749 const char *p
= IDENTIFIER_POINTER (id
);
32750 if (strcmp ("simd", p
) == 0)
32751 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32752 else if (strcmp ("monotonic", p
) == 0)
32753 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32754 else if (strcmp ("nonmonotonic", p
) == 0)
32755 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32758 cp_lexer_consume_token (parser
->lexer
);
32759 if (nmodifiers
++ == 0
32760 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32761 cp_lexer_consume_token (parser
->lexer
);
32764 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
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
);
32777 if (strcmp ("dynamic", p
) != 0)
32779 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32783 if (strcmp ("guided", p
) != 0)
32785 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32789 if (strcmp ("runtime", p
) != 0)
32791 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32798 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32799 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32800 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32801 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32804 cp_lexer_consume_token (parser
->lexer
);
32806 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32807 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32808 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32809 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32811 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32819 cp_lexer_consume_token (parser
->lexer
);
32821 token
= cp_lexer_peek_token (parser
->lexer
);
32822 t
= cp_parser_assignment_expression (parser
);
32824 if (t
== error_mark_node
)
32826 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32827 error_at (token
->location
, "schedule %<runtime%> does not take "
32828 "a %<chunk_size%> parameter");
32829 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32830 error_at (token
->location
, "schedule %<auto%> does not take "
32831 "a %<chunk_size%> parameter");
32833 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32835 if (!parens
.require_close (parser
))
32838 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32841 OMP_CLAUSE_SCHEDULE_KIND (c
)
32842 = (enum omp_clause_schedule_kind
)
32843 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32845 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32846 OMP_CLAUSE_CHAIN (c
) = list
;
32850 cp_parser_error (parser
, "invalid schedule kind");
32852 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32853 /*or_comma=*/false,
32854 /*consume_paren=*/true);
32862 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32863 tree list
, location_t location
)
32867 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32869 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32870 OMP_CLAUSE_CHAIN (c
) = list
;
32879 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32880 tree list
, location_t location
)
32882 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32883 tree c
= build_omp_clause (location
, code
);
32884 OMP_CLAUSE_CHAIN (c
) = list
;
32895 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32896 enum omp_clause_code code
,
32897 tree list
, location_t location
)
32899 tree c
= build_omp_clause (location
, code
);
32900 OMP_CLAUSE_CHAIN (c
) = list
;
32908 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32909 tree list
, location_t location
)
32911 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32912 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32913 OMP_CLAUSE_CHAIN (c
) = list
;
32922 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32923 enum omp_clause_code code
,
32924 tree list
, location_t location
)
32926 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32927 tree c
= build_omp_clause (location
, code
);
32928 OMP_CLAUSE_CHAIN (c
) = list
;
32933 num_teams ( expression ) */
32936 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32937 location_t location
)
32941 matching_parens parens
;
32942 if (!parens
.require_open (parser
))
32945 t
= cp_parser_expression (parser
);
32947 if (t
== error_mark_node
32948 || !parens
.require_close (parser
))
32949 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32950 /*or_comma=*/false,
32951 /*consume_paren=*/true);
32953 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32954 "num_teams", location
);
32956 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32957 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32958 OMP_CLAUSE_CHAIN (c
) = list
;
32964 thread_limit ( expression ) */
32967 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32968 location_t location
)
32972 matching_parens parens
;
32973 if (!parens
.require_open (parser
))
32976 t
= cp_parser_expression (parser
);
32978 if (t
== error_mark_node
32979 || !parens
.require_close (parser
))
32980 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32981 /*or_comma=*/false,
32982 /*consume_paren=*/true);
32984 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32985 "thread_limit", location
);
32987 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32988 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32989 OMP_CLAUSE_CHAIN (c
) = list
;
32995 aligned ( variable-list )
32996 aligned ( variable-list : constant-expression ) */
32999 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33001 tree nlist
, c
, alignment
= NULL_TREE
;
33004 matching_parens parens
;
33005 if (!parens
.require_open (parser
))
33008 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33013 alignment
= cp_parser_constant_expression (parser
);
33015 if (!parens
.require_close (parser
))
33016 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33017 /*or_comma=*/false,
33018 /*consume_paren=*/true);
33020 if (alignment
== error_mark_node
)
33021 alignment
= NULL_TREE
;
33024 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33025 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33031 linear ( variable-list )
33032 linear ( variable-list : expression )
33035 linear ( modifier ( variable-list ) )
33036 linear ( modifier ( variable-list ) : expression ) */
33039 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33040 bool is_cilk_simd_fn
, bool declare_simd
)
33042 tree nlist
, c
, step
= integer_one_node
;
33044 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33046 matching_parens parens
;
33047 if (!parens
.require_open (parser
))
33050 if (!is_cilk_simd_fn
33051 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33053 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33054 const char *p
= IDENTIFIER_POINTER (id
);
33056 if (strcmp ("ref", p
) == 0)
33057 kind
= OMP_CLAUSE_LINEAR_REF
;
33058 else if (strcmp ("val", p
) == 0)
33059 kind
= OMP_CLAUSE_LINEAR_VAL
;
33060 else if (strcmp ("uval", p
) == 0)
33061 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33062 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33063 cp_lexer_consume_token (parser
->lexer
);
33065 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33068 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33069 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33073 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33074 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33076 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33077 else if (!parens
.require_close (parser
))
33078 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33079 /*or_comma=*/false,
33080 /*consume_paren=*/true);
33087 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33088 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33090 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33091 cp_parser_parse_tentatively (parser
);
33092 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33093 /*check_dependency_p=*/true,
33094 /*template_p=*/NULL
,
33095 /*declarator_p=*/false,
33096 /*optional_p=*/false);
33097 if (step
!= error_mark_node
)
33098 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33099 if (step
== error_mark_node
)
33102 cp_parser_abort_tentative_parse (parser
);
33104 else if (!cp_parser_parse_definitely (parser
))
33108 step
= cp_parser_expression (parser
);
33110 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
33112 sorry ("using parameters for %<linear%> step is not supported yet");
33113 step
= integer_one_node
;
33115 if (!parens
.require_close (parser
))
33116 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33117 /*or_comma=*/false,
33118 /*consume_paren=*/true);
33120 if (step
== error_mark_node
)
33124 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33126 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33127 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33134 safelen ( constant-expression ) */
33137 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33138 location_t location
)
33142 matching_parens parens
;
33143 if (!parens
.require_open (parser
))
33146 t
= cp_parser_constant_expression (parser
);
33148 if (t
== error_mark_node
33149 || !parens
.require_close (parser
))
33150 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33151 /*or_comma=*/false,
33152 /*consume_paren=*/true);
33154 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33156 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33157 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33158 OMP_CLAUSE_CHAIN (c
) = list
;
33164 simdlen ( constant-expression ) */
33167 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33168 location_t location
)
33172 matching_parens parens
;
33173 if (!parens
.require_open (parser
))
33176 t
= cp_parser_constant_expression (parser
);
33178 if (t
== error_mark_node
33179 || !parens
.require_close (parser
))
33180 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33181 /*or_comma=*/false,
33182 /*consume_paren=*/true);
33184 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33186 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33187 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33188 OMP_CLAUSE_CHAIN (c
) = list
;
33195 identifier [+/- integer]
33196 vec , identifier [+/- integer]
33200 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33207 cp_parser_error (parser
, "expected identifier");
33211 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33213 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33214 tree t
, identifier
= cp_parser_identifier (parser
);
33215 tree addend
= NULL
;
33217 if (identifier
== error_mark_node
)
33218 t
= error_mark_node
;
33221 t
= cp_parser_lookup_name_simple
33222 (parser
, identifier
,
33223 cp_lexer_peek_token (parser
->lexer
)->location
);
33224 if (t
== error_mark_node
)
33225 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33232 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33234 addend
= integer_zero_node
;
33235 goto add_to_vector
;
33237 cp_lexer_consume_token (parser
->lexer
);
33239 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33241 cp_parser_error (parser
, "expected integer");
33245 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33246 if (TREE_CODE (addend
) != INTEGER_CST
)
33248 cp_parser_error (parser
, "expected integer");
33251 cp_lexer_consume_token (parser
->lexer
);
33254 if (t
!= error_mark_node
)
33256 vec
= tree_cons (addend
, t
, vec
);
33258 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33261 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33264 cp_lexer_consume_token (parser
->lexer
);
33267 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33269 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33270 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33271 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33272 OMP_CLAUSE_CHAIN (u
) = list
;
33279 depend ( depend-kind : variable-list )
33287 depend ( sink : vec ) */
33290 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33293 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33295 matching_parens parens
;
33296 if (!parens
.require_open (parser
))
33299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33301 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33302 const char *p
= IDENTIFIER_POINTER (id
);
33304 if (strcmp ("in", p
) == 0)
33305 kind
= OMP_CLAUSE_DEPEND_IN
;
33306 else if (strcmp ("inout", p
) == 0)
33307 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33308 else if (strcmp ("out", p
) == 0)
33309 kind
= OMP_CLAUSE_DEPEND_OUT
;
33310 else if (strcmp ("source", p
) == 0)
33311 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33312 else if (strcmp ("sink", p
) == 0)
33313 kind
= OMP_CLAUSE_DEPEND_SINK
;
33320 cp_lexer_consume_token (parser
->lexer
);
33322 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33324 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33325 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33326 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33327 OMP_CLAUSE_CHAIN (c
) = list
;
33328 if (!parens
.require_close (parser
))
33329 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33330 /*or_comma=*/false,
33331 /*consume_paren=*/true);
33335 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33338 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33339 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33342 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33345 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33346 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33351 cp_parser_error (parser
, "invalid depend kind");
33353 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33354 /*or_comma=*/false,
33355 /*consume_paren=*/true);
33360 map ( map-kind : variable-list )
33361 map ( variable-list )
33364 alloc | to | from | tofrom
33368 alloc | to | from | tofrom | release | delete
33370 map ( always [,] map-kind: variable-list ) */
33373 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33376 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33377 bool always
= false;
33379 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33384 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33385 const char *p
= IDENTIFIER_POINTER (id
);
33387 if (strcmp ("always", p
) == 0)
33390 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33392 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33393 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33395 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33399 cp_lexer_consume_token (parser
->lexer
);
33401 cp_lexer_consume_token (parser
->lexer
);
33406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33407 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33409 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33410 const char *p
= IDENTIFIER_POINTER (id
);
33412 if (strcmp ("alloc", p
) == 0)
33413 kind
= GOMP_MAP_ALLOC
;
33414 else if (strcmp ("to", p
) == 0)
33415 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33416 else if (strcmp ("from", p
) == 0)
33417 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33418 else if (strcmp ("tofrom", p
) == 0)
33419 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33420 else if (strcmp ("release", p
) == 0)
33421 kind
= GOMP_MAP_RELEASE
;
33424 cp_parser_error (parser
, "invalid map kind");
33425 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33426 /*or_comma=*/false,
33427 /*consume_paren=*/true);
33430 cp_lexer_consume_token (parser
->lexer
);
33431 cp_lexer_consume_token (parser
->lexer
);
33433 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33434 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33436 kind
= GOMP_MAP_DELETE
;
33437 cp_lexer_consume_token (parser
->lexer
);
33438 cp_lexer_consume_token (parser
->lexer
);
33441 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33444 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33445 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33451 device ( expression ) */
33454 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33455 location_t location
)
33459 matching_parens parens
;
33460 if (!parens
.require_open (parser
))
33463 t
= cp_parser_expression (parser
);
33465 if (t
== error_mark_node
33466 || !parens
.require_close (parser
))
33467 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33468 /*or_comma=*/false,
33469 /*consume_paren=*/true);
33471 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33472 "device", location
);
33474 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33475 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33476 OMP_CLAUSE_CHAIN (c
) = list
;
33482 dist_schedule ( static )
33483 dist_schedule ( static , expression ) */
33486 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33487 location_t location
)
33491 matching_parens parens
;
33492 if (!parens
.require_open (parser
))
33495 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33497 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33499 cp_lexer_consume_token (parser
->lexer
);
33501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33503 cp_lexer_consume_token (parser
->lexer
);
33505 t
= cp_parser_assignment_expression (parser
);
33507 if (t
== error_mark_node
)
33509 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33511 if (!parens
.require_close (parser
))
33514 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33517 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33519 OMP_CLAUSE_CHAIN (c
) = list
;
33523 cp_parser_error (parser
, "invalid dist_schedule kind");
33525 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33526 /*or_comma=*/false,
33527 /*consume_paren=*/true);
33532 proc_bind ( proc-bind-kind )
33535 master | close | spread */
33538 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33539 location_t location
)
33542 enum omp_clause_proc_bind_kind kind
;
33544 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33549 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33550 const char *p
= IDENTIFIER_POINTER (id
);
33552 if (strcmp ("master", p
) == 0)
33553 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33554 else if (strcmp ("close", p
) == 0)
33555 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33556 else if (strcmp ("spread", p
) == 0)
33557 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33564 cp_lexer_consume_token (parser
->lexer
);
33565 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33568 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33569 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33571 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33572 OMP_CLAUSE_CHAIN (c
) = list
;
33576 cp_parser_error (parser
, "invalid depend kind");
33578 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33579 /*or_comma=*/false,
33580 /*consume_paren=*/true);
33585 async [( int-expr )] */
33588 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33591 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33593 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33595 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33597 matching_parens parens
;
33598 parens
.consume_open (parser
);
33600 t
= cp_parser_expression (parser
);
33601 if (t
== error_mark_node
33602 || !parens
.require_close (parser
))
33603 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33604 /*or_comma=*/false,
33605 /*consume_paren=*/true);
33608 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33610 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33611 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33612 OMP_CLAUSE_CHAIN (c
) = list
;
33618 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33619 is a bitmask in MASK. Return the list of clauses found. */
33622 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33623 const char *where
, cp_token
*pragma_tok
,
33624 bool finish_p
= true)
33626 tree clauses
= NULL
;
33629 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33632 pragma_omp_clause c_kind
;
33633 omp_clause_code code
;
33634 const char *c_name
;
33635 tree prev
= clauses
;
33637 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33638 cp_lexer_consume_token (parser
->lexer
);
33640 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33641 c_kind
= cp_parser_omp_clause_name (parser
);
33645 case PRAGMA_OACC_CLAUSE_ASYNC
:
33646 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33649 case PRAGMA_OACC_CLAUSE_AUTO
:
33650 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33654 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33655 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33656 c_name
= "collapse";
33658 case PRAGMA_OACC_CLAUSE_COPY
:
33659 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33662 case PRAGMA_OACC_CLAUSE_COPYIN
:
33663 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33666 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33667 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33668 c_name
= "copyout";
33670 case PRAGMA_OACC_CLAUSE_CREATE
:
33671 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33674 case PRAGMA_OACC_CLAUSE_DELETE
:
33675 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33678 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33679 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33680 c_name
= "default";
33682 case PRAGMA_OACC_CLAUSE_DEVICE
:
33683 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33686 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33687 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33688 c_name
= "deviceptr";
33690 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33691 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33692 c_name
= "device_resident";
33694 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33695 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33697 c_name
= "firstprivate";
33699 case PRAGMA_OACC_CLAUSE_GANG
:
33701 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33704 case PRAGMA_OACC_CLAUSE_HOST
:
33705 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33708 case PRAGMA_OACC_CLAUSE_IF
:
33709 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33712 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33713 clauses
= cp_parser_oacc_simple_clause (parser
,
33714 OMP_CLAUSE_INDEPENDENT
,
33716 c_name
= "independent";
33718 case PRAGMA_OACC_CLAUSE_LINK
:
33719 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33722 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33723 code
= OMP_CLAUSE_NUM_GANGS
;
33724 c_name
= "num_gangs";
33725 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33728 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33729 c_name
= "num_workers";
33730 code
= OMP_CLAUSE_NUM_WORKERS
;
33731 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33734 case PRAGMA_OACC_CLAUSE_PRESENT
:
33735 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33736 c_name
= "present";
33738 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33739 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33740 c_name
= "present_or_copy";
33742 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33743 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33744 c_name
= "present_or_copyin";
33746 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33747 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33748 c_name
= "present_or_copyout";
33750 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33751 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33752 c_name
= "present_or_create";
33754 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33755 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33757 c_name
= "private";
33759 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33760 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33761 c_name
= "reduction";
33763 case PRAGMA_OACC_CLAUSE_SELF
:
33764 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33767 case PRAGMA_OACC_CLAUSE_SEQ
:
33768 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33772 case PRAGMA_OACC_CLAUSE_TILE
:
33773 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33776 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33777 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33779 c_name
= "use_device";
33781 case PRAGMA_OACC_CLAUSE_VECTOR
:
33783 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33786 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33787 c_name
= "vector_length";
33788 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33789 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33792 case PRAGMA_OACC_CLAUSE_WAIT
:
33793 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33796 case PRAGMA_OACC_CLAUSE_WORKER
:
33798 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33802 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33808 if (((mask
>> c_kind
) & 1) == 0)
33810 /* Remove the invalid clause(s) from the list to avoid
33811 confusing the rest of the compiler. */
33813 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33818 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33821 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33826 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33827 is a bitmask in MASK. Return the list of clauses found; the result
33828 of clause default goes in *pdefault. */
33831 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33832 const char *where
, cp_token
*pragma_tok
,
33833 bool finish_p
= true)
33835 tree clauses
= NULL
;
33837 cp_token
*token
= NULL
;
33839 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33841 pragma_omp_clause c_kind
;
33842 const char *c_name
;
33843 tree prev
= clauses
;
33845 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33846 cp_lexer_consume_token (parser
->lexer
);
33848 token
= cp_lexer_peek_token (parser
->lexer
);
33849 c_kind
= cp_parser_omp_clause_name (parser
);
33853 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33854 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33856 c_name
= "collapse";
33858 case PRAGMA_OMP_CLAUSE_COPYIN
:
33859 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33862 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33863 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33865 c_name
= "copyprivate";
33867 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33868 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33869 token
->location
, false);
33870 c_name
= "default";
33872 case PRAGMA_OMP_CLAUSE_FINAL
:
33873 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33876 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33877 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33879 c_name
= "firstprivate";
33881 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33882 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33884 c_name
= "grainsize";
33886 case PRAGMA_OMP_CLAUSE_HINT
:
33887 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33891 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33892 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33894 c_name
= "defaultmap";
33896 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33897 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33899 c_name
= "use_device_ptr";
33901 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33902 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33904 c_name
= "is_device_ptr";
33906 case PRAGMA_OMP_CLAUSE_IF
:
33907 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33911 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33912 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33914 c_name
= "lastprivate";
33916 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33917 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33919 c_name
= "mergeable";
33921 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33922 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33925 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33926 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33928 c_name
= "num_tasks";
33930 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33931 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33933 c_name
= "num_threads";
33935 case PRAGMA_OMP_CLAUSE_ORDERED
:
33936 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33938 c_name
= "ordered";
33940 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33941 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33943 c_name
= "priority";
33945 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33946 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33948 c_name
= "private";
33950 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33951 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33952 c_name
= "reduction";
33954 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33955 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33957 c_name
= "schedule";
33959 case PRAGMA_OMP_CLAUSE_SHARED
:
33960 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33964 case PRAGMA_OMP_CLAUSE_UNTIED
:
33965 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33969 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33970 case PRAGMA_CILK_CLAUSE_MASK
:
33971 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33972 clauses
, token
->location
);
33973 c_name
= "inbranch";
33975 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33976 case PRAGMA_CILK_CLAUSE_NOMASK
:
33977 clauses
= cp_parser_omp_clause_branch (parser
,
33978 OMP_CLAUSE_NOTINBRANCH
,
33979 clauses
, token
->location
);
33980 c_name
= "notinbranch";
33982 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33983 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33984 clauses
, token
->location
);
33985 c_name
= "parallel";
33989 error_at (token
->location
, "%qs must be the first clause of %qs",
33994 case PRAGMA_OMP_CLAUSE_FOR
:
33995 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33996 clauses
, token
->location
);
33999 goto clause_not_first
;
34001 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34002 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34003 clauses
, token
->location
);
34004 c_name
= "sections";
34006 goto clause_not_first
;
34008 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34009 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34010 clauses
, token
->location
);
34011 c_name
= "taskgroup";
34013 goto clause_not_first
;
34015 case PRAGMA_OMP_CLAUSE_LINK
:
34016 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34019 case PRAGMA_OMP_CLAUSE_TO
:
34020 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34021 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34024 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34027 case PRAGMA_OMP_CLAUSE_FROM
:
34028 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34031 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34032 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34034 c_name
= "uniform";
34036 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34037 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34039 c_name
= "num_teams";
34041 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34042 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34044 c_name
= "thread_limit";
34046 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34047 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34048 c_name
= "aligned";
34050 case PRAGMA_OMP_CLAUSE_LINEAR
:
34052 bool cilk_simd_fn
= false, declare_simd
= false;
34053 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
34054 cilk_simd_fn
= true;
34055 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34056 declare_simd
= true;
34057 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
34058 cilk_simd_fn
, declare_simd
);
34062 case PRAGMA_OMP_CLAUSE_DEPEND
:
34063 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34067 case PRAGMA_OMP_CLAUSE_MAP
:
34068 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34071 case PRAGMA_OMP_CLAUSE_DEVICE
:
34072 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34076 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34077 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34079 c_name
= "dist_schedule";
34081 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34082 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34084 c_name
= "proc_bind";
34086 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34087 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34089 c_name
= "safelen";
34091 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34092 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34094 c_name
= "simdlen";
34096 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34097 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34099 c_name
= "nogroup";
34101 case PRAGMA_OMP_CLAUSE_THREADS
:
34103 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34104 clauses
, token
->location
);
34105 c_name
= "threads";
34107 case PRAGMA_OMP_CLAUSE_SIMD
:
34109 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34110 clauses
, token
->location
);
34113 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
34114 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
34115 c_name
= "simdlen";
34118 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34124 if (((mask
>> c_kind
) & 1) == 0)
34126 /* Remove the invalid clause(s) from the list to avoid
34127 confusing the rest of the compiler. */
34129 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34133 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
34134 no reason to skip to the end. */
34135 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
34136 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34139 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34140 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34142 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34151 In practice, we're also interested in adding the statement to an
34152 outer node. So it is convenient if we work around the fact that
34153 cp_parser_statement calls add_stmt. */
34156 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34158 unsigned save
= parser
->in_statement
;
34160 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34161 This preserves the "not within loop or switch" style error messages
34162 for nonsense cases like
34168 if (parser
->in_statement
)
34169 parser
->in_statement
= IN_OMP_BLOCK
;
34175 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34177 parser
->in_statement
= save
;
34181 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34183 tree stmt
= begin_omp_structured_block ();
34184 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34186 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34188 cp_parser_end_omp_structured_block (parser
, save
);
34189 return finish_omp_structured_block (stmt
);
34193 # pragma omp atomic new-line
34197 x binop= expr | x++ | ++x | x-- | --x
34199 +, *, -, /, &, ^, |, <<, >>
34201 where x is an lvalue expression with scalar type.
34204 # pragma omp atomic new-line
34207 # pragma omp atomic read new-line
34210 # pragma omp atomic write new-line
34213 # pragma omp atomic update new-line
34216 # pragma omp atomic capture new-line
34219 # pragma omp atomic capture new-line
34227 expression-stmt | x = x binop expr
34229 v = expression-stmt
34231 { v = x; update-stmt; } | { update-stmt; v = x; }
34235 expression-stmt | x = x binop expr | x = expr binop x
34239 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34241 where x and v are lvalue expressions with scalar type. */
34244 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34246 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34247 tree rhs1
= NULL_TREE
, orig_lhs
;
34248 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34249 bool structured_block
= false;
34250 bool seq_cst
= false;
34252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34254 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34255 const char *p
= IDENTIFIER_POINTER (id
);
34257 if (!strcmp (p
, "seq_cst"))
34260 cp_lexer_consume_token (parser
->lexer
);
34261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34262 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34263 cp_lexer_consume_token (parser
->lexer
);
34266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34268 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34269 const char *p
= IDENTIFIER_POINTER (id
);
34271 if (!strcmp (p
, "read"))
34272 code
= OMP_ATOMIC_READ
;
34273 else if (!strcmp (p
, "write"))
34275 else if (!strcmp (p
, "update"))
34277 else if (!strcmp (p
, "capture"))
34278 code
= OMP_ATOMIC_CAPTURE_NEW
;
34282 cp_lexer_consume_token (parser
->lexer
);
34286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34287 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34288 cp_lexer_consume_token (parser
->lexer
);
34290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34292 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34293 const char *p
= IDENTIFIER_POINTER (id
);
34295 if (!strcmp (p
, "seq_cst"))
34298 cp_lexer_consume_token (parser
->lexer
);
34302 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34306 case OMP_ATOMIC_READ
:
34307 case NOP_EXPR
: /* atomic write */
34308 v
= cp_parser_unary_expression (parser
);
34309 if (v
== error_mark_node
)
34311 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34313 if (code
== NOP_EXPR
)
34314 lhs
= cp_parser_expression (parser
);
34316 lhs
= cp_parser_unary_expression (parser
);
34317 if (lhs
== error_mark_node
)
34319 if (code
== NOP_EXPR
)
34321 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34329 case OMP_ATOMIC_CAPTURE_NEW
:
34330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34332 cp_lexer_consume_token (parser
->lexer
);
34333 structured_block
= true;
34337 v
= cp_parser_unary_expression (parser
);
34338 if (v
== error_mark_node
)
34340 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34348 lhs
= cp_parser_unary_expression (parser
);
34350 switch (TREE_CODE (lhs
))
34355 case POSTINCREMENT_EXPR
:
34356 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34357 code
= OMP_ATOMIC_CAPTURE_OLD
;
34359 case PREINCREMENT_EXPR
:
34360 lhs
= TREE_OPERAND (lhs
, 0);
34361 opcode
= PLUS_EXPR
;
34362 rhs
= integer_one_node
;
34365 case POSTDECREMENT_EXPR
:
34366 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34367 code
= OMP_ATOMIC_CAPTURE_OLD
;
34369 case PREDECREMENT_EXPR
:
34370 lhs
= TREE_OPERAND (lhs
, 0);
34371 opcode
= MINUS_EXPR
;
34372 rhs
= integer_one_node
;
34375 case COMPOUND_EXPR
:
34376 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34377 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34378 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34379 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34381 (TREE_OPERAND (lhs
, 1), 0), 0)))
34383 /* Undo effects of boolean_increment for post {in,de}crement. */
34384 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34387 if (TREE_CODE (lhs
) == MODIFY_EXPR
34388 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34390 /* Undo effects of boolean_increment. */
34391 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34393 /* This is pre or post increment. */
34394 rhs
= TREE_OPERAND (lhs
, 1);
34395 lhs
= TREE_OPERAND (lhs
, 0);
34397 if (code
== OMP_ATOMIC_CAPTURE_NEW
34398 && !structured_block
34399 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34400 code
= OMP_ATOMIC_CAPTURE_OLD
;
34406 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34409 opcode
= MULT_EXPR
;
34412 opcode
= TRUNC_DIV_EXPR
;
34415 opcode
= PLUS_EXPR
;
34418 opcode
= MINUS_EXPR
;
34420 case CPP_LSHIFT_EQ
:
34421 opcode
= LSHIFT_EXPR
;
34423 case CPP_RSHIFT_EQ
:
34424 opcode
= RSHIFT_EXPR
;
34427 opcode
= BIT_AND_EXPR
;
34430 opcode
= BIT_IOR_EXPR
;
34433 opcode
= BIT_XOR_EXPR
;
34436 enum cp_parser_prec oprec
;
34438 cp_lexer_consume_token (parser
->lexer
);
34439 cp_parser_parse_tentatively (parser
);
34440 rhs1
= cp_parser_simple_cast_expression (parser
);
34441 if (rhs1
== error_mark_node
)
34443 cp_parser_abort_tentative_parse (parser
);
34444 cp_parser_simple_cast_expression (parser
);
34447 token
= cp_lexer_peek_token (parser
->lexer
);
34448 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34450 cp_parser_abort_tentative_parse (parser
);
34451 cp_parser_parse_tentatively (parser
);
34452 rhs
= cp_parser_binary_expression (parser
, false, true,
34453 PREC_NOT_OPERATOR
, NULL
);
34454 if (rhs
== error_mark_node
)
34456 cp_parser_abort_tentative_parse (parser
);
34457 cp_parser_binary_expression (parser
, false, true,
34458 PREC_NOT_OPERATOR
, NULL
);
34461 switch (TREE_CODE (rhs
))
34464 case TRUNC_DIV_EXPR
:
34473 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34475 if (cp_parser_parse_definitely (parser
))
34477 opcode
= TREE_CODE (rhs
);
34478 rhs1
= TREE_OPERAND (rhs
, 0);
34479 rhs
= TREE_OPERAND (rhs
, 1);
34489 cp_parser_abort_tentative_parse (parser
);
34490 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34492 rhs
= cp_parser_expression (parser
);
34493 if (rhs
== error_mark_node
)
34499 cp_parser_error (parser
,
34500 "invalid form of %<#pragma omp atomic%>");
34503 if (!cp_parser_parse_definitely (parser
))
34505 switch (token
->type
)
34507 case CPP_SEMICOLON
:
34508 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34510 code
= OMP_ATOMIC_CAPTURE_OLD
;
34515 cp_lexer_consume_token (parser
->lexer
);
34518 else if (structured_block
)
34525 cp_parser_error (parser
,
34526 "invalid form of %<#pragma omp atomic%>");
34529 opcode
= MULT_EXPR
;
34532 opcode
= TRUNC_DIV_EXPR
;
34535 opcode
= PLUS_EXPR
;
34538 opcode
= MINUS_EXPR
;
34541 opcode
= LSHIFT_EXPR
;
34544 opcode
= RSHIFT_EXPR
;
34547 opcode
= BIT_AND_EXPR
;
34550 opcode
= BIT_IOR_EXPR
;
34553 opcode
= BIT_XOR_EXPR
;
34556 cp_parser_error (parser
,
34557 "invalid operator for %<#pragma omp atomic%>");
34560 oprec
= TOKEN_PRECEDENCE (token
);
34561 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34562 if (commutative_tree_code (opcode
))
34563 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34564 cp_lexer_consume_token (parser
->lexer
);
34565 rhs
= cp_parser_binary_expression (parser
, false, false,
34567 if (rhs
== error_mark_node
)
34572 cp_parser_error (parser
,
34573 "invalid operator for %<#pragma omp atomic%>");
34576 cp_lexer_consume_token (parser
->lexer
);
34578 rhs
= cp_parser_expression (parser
);
34579 if (rhs
== error_mark_node
)
34584 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34586 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34588 v
= cp_parser_unary_expression (parser
);
34589 if (v
== error_mark_node
)
34591 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34593 lhs1
= cp_parser_unary_expression (parser
);
34594 if (lhs1
== error_mark_node
)
34597 if (structured_block
)
34599 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34600 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34603 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34604 if (!structured_block
)
34605 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34609 cp_parser_skip_to_end_of_block_or_statement (parser
);
34610 if (structured_block
)
34612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34613 cp_lexer_consume_token (parser
->lexer
);
34614 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34616 cp_parser_skip_to_end_of_block_or_statement (parser
);
34617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34618 cp_lexer_consume_token (parser
->lexer
);
34625 # pragma omp barrier new-line */
34628 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34630 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34631 finish_omp_barrier ();
34635 # pragma omp critical [(name)] new-line
34639 # pragma omp critical [(name) [hint(expression)]] new-line
34640 structured-block */
34642 #define OMP_CRITICAL_CLAUSE_MASK \
34643 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34646 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34648 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34652 matching_parens parens
;
34653 parens
.consume_open (parser
);
34655 name
= cp_parser_identifier (parser
);
34657 if (name
== error_mark_node
34658 || !parens
.require_close (parser
))
34659 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34660 /*or_comma=*/false,
34661 /*consume_paren=*/true);
34662 if (name
== error_mark_node
)
34665 clauses
= cp_parser_omp_all_clauses (parser
,
34666 OMP_CRITICAL_CLAUSE_MASK
,
34667 "#pragma omp critical", pragma_tok
);
34670 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34672 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34673 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34677 # pragma omp flush flush-vars[opt] new-line
34680 ( variable-list ) */
34683 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34686 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34687 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34689 finish_omp_flush ();
34692 /* Helper function, to parse omp for increment expression. */
34695 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
34697 tree cond
= cp_parser_binary_expression (parser
, false, true,
34698 PREC_NOT_OPERATOR
, NULL
);
34699 if (cond
== error_mark_node
34700 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34702 cp_parser_skip_to_end_of_statement (parser
);
34703 return error_mark_node
;
34706 switch (TREE_CODE (cond
))
34714 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34716 /* Fall through: OpenMP disallows NE_EXPR. */
34717 gcc_fallthrough ();
34719 return error_mark_node
;
34722 /* If decl is an iterator, preserve LHS and RHS of the relational
34723 expr until finish_omp_for. */
34725 && (type_dependent_expression_p (decl
)
34726 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34729 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34731 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34732 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34733 /*overload=*/NULL
, tf_warning_or_error
);
34736 /* Helper function, to parse omp for increment expression. */
34739 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34747 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34749 op
= (token
->type
== CPP_PLUS_PLUS
34750 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34751 cp_lexer_consume_token (parser
->lexer
);
34752 lhs
= cp_parser_simple_cast_expression (parser
);
34754 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34755 return error_mark_node
;
34756 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34759 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34761 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34762 return error_mark_node
;
34764 token
= cp_lexer_peek_token (parser
->lexer
);
34765 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34767 op
= (token
->type
== CPP_PLUS_PLUS
34768 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34769 cp_lexer_consume_token (parser
->lexer
);
34770 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34773 op
= cp_parser_assignment_operator_opt (parser
);
34774 if (op
== ERROR_MARK
)
34775 return error_mark_node
;
34777 if (op
!= NOP_EXPR
)
34779 rhs
= cp_parser_assignment_expression (parser
);
34780 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34781 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34784 lhs
= cp_parser_binary_expression (parser
, false, false,
34785 PREC_ADDITIVE_EXPRESSION
, NULL
);
34786 token
= cp_lexer_peek_token (parser
->lexer
);
34787 decl_first
= (lhs
== decl
34788 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34791 if (token
->type
!= CPP_PLUS
34792 && token
->type
!= CPP_MINUS
)
34793 return error_mark_node
;
34797 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34798 cp_lexer_consume_token (parser
->lexer
);
34799 rhs
= cp_parser_binary_expression (parser
, false, false,
34800 PREC_ADDITIVE_EXPRESSION
, NULL
);
34801 token
= cp_lexer_peek_token (parser
->lexer
);
34802 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34804 if (lhs
== NULL_TREE
)
34806 if (op
== PLUS_EXPR
)
34809 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34810 tf_warning_or_error
);
34813 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34814 ERROR_MARK
, NULL
, tf_warning_or_error
);
34817 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34822 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34823 || op
== MINUS_EXPR
)
34824 return error_mark_node
;
34825 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34828 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34830 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34833 /* Parse the initialization statement of either an OpenMP for loop or
34834 a Cilk Plus for loop.
34836 Return true if the resulting construct should have an
34837 OMP_CLAUSE_PRIVATE added to it. */
34840 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34841 enum tree_code code
,
34842 tree
&this_pre_body
,
34843 vec
<tree
, va_gc
> *for_block
,
34849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34852 tree add_private_clause
= NULL_TREE
;
34854 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34858 integer-type var = lb
34859 random-access-iterator-type var = lb
34860 pointer-type var = lb
34862 cp_decl_specifier_seq type_specifiers
;
34864 /* First, try to parse as an initialized declaration. See
34865 cp_parser_condition, from whence the bulk of this is copied. */
34867 cp_parser_parse_tentatively (parser
);
34868 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34869 /*is_trailing_return=*/false,
34871 if (cp_parser_parse_definitely (parser
))
34873 /* If parsing a type specifier seq succeeded, then this
34874 MUST be a initialized declaration. */
34875 tree asm_specification
, attributes
;
34876 cp_declarator
*declarator
;
34878 declarator
= cp_parser_declarator (parser
,
34879 CP_PARSER_DECLARATOR_NAMED
,
34880 /*ctor_dtor_or_conv_p=*/NULL
,
34881 /*parenthesized_p=*/NULL
,
34882 /*member_p=*/false,
34883 /*friend_p=*/false);
34884 attributes
= cp_parser_attributes_opt (parser
);
34885 asm_specification
= cp_parser_asm_specification_opt (parser
);
34887 if (declarator
== cp_error_declarator
)
34888 cp_parser_skip_to_end_of_statement (parser
);
34892 tree pushed_scope
, auto_node
;
34894 decl
= start_decl (declarator
, &type_specifiers
,
34895 SD_INITIALIZED
, attributes
,
34896 /*prefix_attributes=*/NULL_TREE
,
34899 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34900 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34902 if (cp_lexer_next_token_is (parser
->lexer
,
34905 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
34906 error ("parenthesized initialization is not allowed in "
34907 "OpenMP %<for%> loop");
34909 error ("parenthesized initialization is "
34910 "not allowed in for-loop");
34913 /* Trigger an error. */
34914 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34916 init
= error_mark_node
;
34917 cp_parser_skip_to_end_of_statement (parser
);
34919 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34920 || type_dependent_expression_p (decl
)
34923 bool is_direct_init
, is_non_constant_init
;
34925 init
= cp_parser_initializer (parser
,
34927 &is_non_constant_init
);
34932 = do_auto_deduction (TREE_TYPE (decl
), init
,
34935 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34936 && !type_dependent_expression_p (decl
))
34940 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34942 LOOKUP_ONLYCONVERTING
);
34944 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34946 vec_safe_push (for_block
, this_pre_body
);
34951 init
= pop_stmt_list (this_pre_body
);
34952 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34954 tree_stmt_iterator i
= tsi_start (init
);
34955 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34956 while (!tsi_end_p (i
))
34958 tree t
= tsi_stmt (i
);
34959 if (TREE_CODE (t
) == DECL_EXPR
34960 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34963 vec_safe_push (for_block
, t
);
34968 if (tsi_one_before_end_p (i
))
34970 tree t
= tsi_stmt (i
);
34972 free_stmt_list (init
);
34977 this_pre_body
= NULL_TREE
;
34982 cp_lexer_consume_token (parser
->lexer
);
34983 init
= cp_parser_assignment_expression (parser
);
34986 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34987 init
= error_mark_node
;
34989 cp_finish_decl (decl
, NULL_TREE
,
34990 /*init_const_expr_p=*/false,
34992 LOOKUP_ONLYCONVERTING
);
34996 pop_scope (pushed_scope
);
35002 /* If parsing a type specifier sequence failed, then
35003 this MUST be a simple expression. */
35004 if (code
== CILK_FOR
)
35005 error ("%<_Cilk_for%> allows expression instead of declaration only "
35006 "in C, not in C++");
35007 cp_parser_parse_tentatively (parser
);
35008 decl
= cp_parser_primary_expression (parser
, false, false,
35010 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35011 if (!cp_parser_error_occurred (parser
)
35013 && (TREE_CODE (decl
) == COMPONENT_REF
35014 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35016 cp_parser_abort_tentative_parse (parser
);
35017 cp_parser_parse_tentatively (parser
);
35018 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35019 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35020 /*check_dependency_p=*/true,
35021 /*template_p=*/NULL
,
35022 /*declarator_p=*/false,
35023 /*optional_p=*/false);
35024 if (name
!= error_mark_node
35025 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35027 decl
= cp_parser_lookup_name_simple (parser
, name
,
35029 if (TREE_CODE (decl
) == FIELD_DECL
)
35030 add_private_clause
= omp_privatize_field (decl
, false);
35032 cp_parser_abort_tentative_parse (parser
);
35033 cp_parser_parse_tentatively (parser
);
35034 decl
= cp_parser_primary_expression (parser
, false, false,
35037 if (!cp_parser_error_occurred (parser
)
35040 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35044 cp_parser_parse_definitely (parser
);
35045 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35046 rhs
= cp_parser_assignment_expression (parser
);
35048 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35051 tf_warning_or_error
));
35052 if (!add_private_clause
)
35053 add_private_clause
= decl
;
35058 cp_parser_abort_tentative_parse (parser
);
35059 init
= cp_parser_expression (parser
);
35062 if (TREE_CODE (init
) == MODIFY_EXPR
35063 || TREE_CODE (init
) == MODOP_EXPR
)
35064 real_decl
= TREE_OPERAND (init
, 0);
35068 return add_private_clause
;
35071 /* Parse the restricted form of the for statement allowed by OpenMP. */
35074 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35075 tree
*cclauses
, bool *if_p
)
35077 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35078 tree real_decl
, initv
, condv
, incrv
, declv
;
35079 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35080 location_t loc_first
;
35081 bool collapse_err
= false;
35082 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35083 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35084 auto_vec
<tree
, 4> orig_inits
;
35085 bool tiling
= false;
35087 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35088 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35089 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35090 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35093 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35095 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35096 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35099 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35102 if (ordered
&& ordered
< collapse
)
35104 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35105 "%<ordered%> clause parameter is less than %<collapse%>");
35106 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35107 = build_int_cst (NULL_TREE
, collapse
);
35108 ordered
= collapse
;
35112 for (tree
*pc
= &clauses
; *pc
; )
35113 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35115 error_at (OMP_CLAUSE_LOCATION (*pc
),
35116 "%<linear%> clause may not be specified together "
35117 "with %<ordered%> clause with a parameter");
35118 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35121 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35124 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35125 count
= ordered
? ordered
: collapse
;
35127 declv
= make_tree_vec (count
);
35128 initv
= make_tree_vec (count
);
35129 condv
= make_tree_vec (count
);
35130 incrv
= make_tree_vec (count
);
35132 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35134 for (i
= 0; i
< count
; i
++)
35136 int bracecount
= 0;
35137 tree add_private_clause
= NULL_TREE
;
35140 if (code
!= CILK_FOR
35141 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35144 cp_parser_error (parser
, "for statement expected");
35147 if (code
== CILK_FOR
35148 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
35151 cp_parser_error (parser
, "_Cilk_for statement expected");
35154 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35156 matching_parens parens
;
35157 if (!parens
.require_open (parser
))
35160 init
= orig_init
= decl
= real_decl
= NULL
;
35161 this_pre_body
= push_stmt_list ();
35164 = cp_parser_omp_for_loop_init (parser
, code
,
35165 this_pre_body
, for_block
,
35166 init
, orig_init
, decl
, real_decl
);
35168 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35171 this_pre_body
= pop_stmt_list (this_pre_body
);
35175 pre_body
= push_stmt_list ();
35177 add_stmt (this_pre_body
);
35178 pre_body
= pop_stmt_list (pre_body
);
35181 pre_body
= this_pre_body
;
35186 if (cclauses
!= NULL
35187 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35188 && real_decl
!= NULL_TREE
)
35191 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35192 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35193 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35195 error_at (loc
, "iteration variable %qD"
35196 " should not be firstprivate", real_decl
);
35197 *c
= OMP_CLAUSE_CHAIN (*c
);
35199 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35200 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35202 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35204 *c
= OMP_CLAUSE_CHAIN (*c
);
35205 if (code
== OMP_SIMD
)
35207 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35208 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35212 OMP_CLAUSE_CHAIN (l
) = clauses
;
35215 add_private_clause
= NULL_TREE
;
35219 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35220 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35221 add_private_clause
= NULL_TREE
;
35222 c
= &OMP_CLAUSE_CHAIN (*c
);
35226 if (add_private_clause
)
35229 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35231 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35232 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35233 && OMP_CLAUSE_DECL (c
) == decl
)
35235 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35236 && OMP_CLAUSE_DECL (c
) == decl
)
35237 error_at (loc
, "iteration variable %qD "
35238 "should not be firstprivate",
35240 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35241 && OMP_CLAUSE_DECL (c
) == decl
)
35242 error_at (loc
, "iteration variable %qD should not be reduction",
35247 if (code
!= OMP_SIMD
)
35248 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35249 else if (collapse
== 1)
35250 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35252 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35253 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35254 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35257 OMP_CLAUSE_CHAIN (c
) = clauses
;
35259 /* For linear, signal that we need to fill up
35260 the so far unknown linear step. */
35261 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35262 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35268 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35269 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
35270 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35273 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35275 /* If decl is an iterator, preserve the operator on decl
35276 until finish_omp_for. */
35278 && ((processing_template_decl
35279 && (TREE_TYPE (real_decl
) == NULL_TREE
35280 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35281 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35282 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35284 incr
= cp_parser_expression (parser
);
35285 if (!EXPR_HAS_LOCATION (incr
))
35286 protected_set_expr_location (incr
, input_location
);
35289 if (!parens
.require_close (parser
))
35290 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35291 /*or_comma=*/false,
35292 /*consume_paren=*/true);
35294 TREE_VEC_ELT (declv
, i
) = decl
;
35295 TREE_VEC_ELT (initv
, i
) = init
;
35296 TREE_VEC_ELT (condv
, i
) = cond
;
35297 TREE_VEC_ELT (incrv
, i
) = incr
;
35300 orig_inits
.safe_grow_cleared (i
+ 1);
35301 orig_inits
[i
] = orig_init
;
35304 if (i
== count
- 1)
35307 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35308 in between the collapsed for loops to be still considered perfectly
35309 nested. Hopefully the final version clarifies this.
35310 For now handle (multiple) {'s and empty statements. */
35311 cp_parser_parse_tentatively (parser
);
35314 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35316 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35318 cp_lexer_consume_token (parser
->lexer
);
35321 else if (bracecount
35322 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35323 cp_lexer_consume_token (parser
->lexer
);
35326 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35327 error_at (loc
, "not enough for loops to collapse");
35328 collapse_err
= true;
35329 cp_parser_abort_tentative_parse (parser
);
35337 cp_parser_parse_definitely (parser
);
35338 nbraces
+= bracecount
;
35345 /* Note that we saved the original contents of this flag when we entered
35346 the structured block, and so we don't need to re-save it here. */
35347 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
35348 parser
->in_statement
= IN_CILK_SIMD_FOR
;
35350 parser
->in_statement
= IN_OMP_FOR
;
35352 /* Note that the grammar doesn't call for a structured block here,
35353 though the loop as a whole is a structured block. */
35354 body
= push_stmt_list ();
35355 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35356 body
= pop_stmt_list (body
);
35358 if (declv
== NULL_TREE
)
35361 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35362 body
, pre_body
, &orig_inits
, clauses
);
35366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35368 cp_lexer_consume_token (parser
->lexer
);
35371 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35372 cp_lexer_consume_token (parser
->lexer
);
35377 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35378 "collapsed loops not perfectly nested");
35380 collapse_err
= true;
35381 cp_parser_statement_seq_opt (parser
, NULL
);
35382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35387 while (!for_block
->is_empty ())
35389 tree t
= for_block
->pop ();
35390 if (TREE_CODE (t
) == STATEMENT_LIST
)
35391 add_stmt (pop_stmt_list (t
));
35395 release_tree_vector (for_block
);
35400 /* Helper function for OpenMP parsing, split clauses and call
35401 finish_omp_clauses on each of the set of clauses afterwards. */
35404 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35405 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35408 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35409 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35411 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35415 #pragma omp simd simd-clause[optseq] new-line
35418 #define OMP_SIMD_CLAUSE_MASK \
35419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35429 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35430 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35433 tree clauses
, sb
, ret
;
35435 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35437 strcat (p_name
, " simd");
35438 mask
|= OMP_SIMD_CLAUSE_MASK
;
35440 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35444 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35445 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35446 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35447 OMP_CLAUSE_ORDERED
);
35448 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35450 error_at (OMP_CLAUSE_LOCATION (c
),
35451 "%<ordered%> clause with parameter may not be specified "
35452 "on %qs construct", p_name
);
35453 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35457 sb
= begin_omp_structured_block ();
35458 save
= cp_parser_begin_omp_structured_block (parser
);
35460 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35462 cp_parser_end_omp_structured_block (parser
, save
);
35463 add_stmt (finish_omp_structured_block (sb
));
35469 #pragma omp for for-clause[optseq] new-line
35473 #pragma omp for simd for-simd-clause[optseq] new-line
35476 #define OMP_FOR_CLAUSE_MASK \
35477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35488 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35489 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35492 tree clauses
, sb
, ret
;
35494 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35496 strcat (p_name
, " for");
35497 mask
|= OMP_FOR_CLAUSE_MASK
;
35498 /* parallel for{, simd} disallows nowait clause, but for
35499 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35500 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35501 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35502 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35503 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35504 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35508 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35509 const char *p
= IDENTIFIER_POINTER (id
);
35511 if (strcmp (p
, "simd") == 0)
35513 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35514 if (cclauses
== NULL
)
35515 cclauses
= cclauses_buf
;
35517 cp_lexer_consume_token (parser
->lexer
);
35518 if (!flag_openmp
) /* flag_openmp_simd */
35519 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35521 sb
= begin_omp_structured_block ();
35522 save
= cp_parser_begin_omp_structured_block (parser
);
35523 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35525 cp_parser_end_omp_structured_block (parser
, save
);
35526 tree body
= finish_omp_structured_block (sb
);
35529 ret
= make_node (OMP_FOR
);
35530 TREE_TYPE (ret
) = void_type_node
;
35531 OMP_FOR_BODY (ret
) = body
;
35532 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35533 SET_EXPR_LOCATION (ret
, loc
);
35538 if (!flag_openmp
) /* flag_openmp_simd */
35540 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35544 /* Composite distribute parallel for disallows linear clause. */
35545 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35546 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35548 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35552 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35553 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35556 sb
= begin_omp_structured_block ();
35557 save
= cp_parser_begin_omp_structured_block (parser
);
35559 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35561 cp_parser_end_omp_structured_block (parser
, save
);
35562 add_stmt (finish_omp_structured_block (sb
));
35568 # pragma omp master new-line
35569 structured-block */
35572 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35574 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35575 return c_finish_omp_master (input_location
,
35576 cp_parser_omp_structured_block (parser
, if_p
));
35580 # pragma omp ordered new-line
35584 # pragma omp ordered ordered-clauses new-line
35585 structured-block */
35587 #define OMP_ORDERED_CLAUSE_MASK \
35588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35591 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35592 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35595 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35596 enum pragma_context context
, bool *if_p
)
35598 location_t loc
= pragma_tok
->location
;
35600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35602 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35603 const char *p
= IDENTIFIER_POINTER (id
);
35605 if (strcmp (p
, "depend") == 0)
35607 if (!flag_openmp
) /* flag_openmp_simd */
35609 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35612 if (context
== pragma_stmt
)
35614 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35615 "%<depend%> clause may only be used in compound "
35617 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35621 = cp_parser_omp_all_clauses (parser
,
35622 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35623 "#pragma omp ordered", pragma_tok
);
35624 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35630 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35631 "#pragma omp ordered", pragma_tok
);
35633 if (!flag_openmp
/* flag_openmp_simd */
35634 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35637 c_finish_omp_ordered (loc
, clauses
,
35638 cp_parser_omp_structured_block (parser
, if_p
));
35645 { section-sequence }
35648 section-directive[opt] structured-block
35649 section-sequence section-directive structured-block */
35652 cp_parser_omp_sections_scope (cp_parser
*parser
)
35654 tree stmt
, substmt
;
35655 bool error_suppress
= false;
35658 matching_braces braces
;
35659 if (!braces
.require_open (parser
))
35662 stmt
= push_stmt_list ();
35664 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35665 != PRAGMA_OMP_SECTION
)
35667 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35668 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35669 add_stmt (substmt
);
35674 tok
= cp_lexer_peek_token (parser
->lexer
);
35675 if (tok
->type
== CPP_CLOSE_BRACE
)
35677 if (tok
->type
== CPP_EOF
)
35680 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35682 cp_lexer_consume_token (parser
->lexer
);
35683 cp_parser_require_pragma_eol (parser
, tok
);
35684 error_suppress
= false;
35686 else if (!error_suppress
)
35688 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35689 error_suppress
= true;
35692 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35693 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35694 add_stmt (substmt
);
35696 braces
.require_close (parser
);
35698 substmt
= pop_stmt_list (stmt
);
35700 stmt
= make_node (OMP_SECTIONS
);
35701 TREE_TYPE (stmt
) = void_type_node
;
35702 OMP_SECTIONS_BODY (stmt
) = substmt
;
35709 # pragma omp sections sections-clause[optseq] newline
35712 #define OMP_SECTIONS_CLAUSE_MASK \
35713 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35720 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35721 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35724 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35726 strcat (p_name
, " sections");
35727 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35729 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35731 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35735 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35736 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35739 ret
= cp_parser_omp_sections_scope (parser
);
35741 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35747 # pragma omp parallel parallel-clause[optseq] new-line
35749 # pragma omp parallel for parallel-for-clause[optseq] new-line
35751 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35755 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35756 structured-block */
35758 #define OMP_PARALLEL_CLAUSE_MASK \
35759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35770 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35771 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35774 tree stmt
, clauses
, block
;
35776 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35778 strcat (p_name
, " parallel");
35779 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35780 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35781 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35782 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35783 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35785 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35787 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35788 if (cclauses
== NULL
)
35789 cclauses
= cclauses_buf
;
35791 cp_lexer_consume_token (parser
->lexer
);
35792 if (!flag_openmp
) /* flag_openmp_simd */
35793 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35795 block
= begin_omp_parallel ();
35796 save
= cp_parser_begin_omp_structured_block (parser
);
35797 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35799 cp_parser_end_omp_structured_block (parser
, save
);
35800 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35802 if (ret
== NULL_TREE
)
35804 OMP_PARALLEL_COMBINED (stmt
) = 1;
35807 /* When combined with distribute, parallel has to be followed by for.
35808 #pragma omp target parallel is allowed though. */
35810 && (mask
& (OMP_CLAUSE_MASK_1
35811 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35813 error_at (loc
, "expected %<for%> after %qs", p_name
);
35814 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35817 else if (!flag_openmp
) /* flag_openmp_simd */
35819 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35822 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35824 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35825 const char *p
= IDENTIFIER_POINTER (id
);
35826 if (strcmp (p
, "sections") == 0)
35828 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35829 cclauses
= cclauses_buf
;
35831 cp_lexer_consume_token (parser
->lexer
);
35832 block
= begin_omp_parallel ();
35833 save
= cp_parser_begin_omp_structured_block (parser
);
35834 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35835 cp_parser_end_omp_structured_block (parser
, save
);
35836 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35838 OMP_PARALLEL_COMBINED (stmt
) = 1;
35843 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35847 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35848 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35851 block
= begin_omp_parallel ();
35852 save
= cp_parser_begin_omp_structured_block (parser
);
35853 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35854 cp_parser_end_omp_structured_block (parser
, save
);
35855 stmt
= finish_omp_parallel (clauses
, block
);
35860 # pragma omp single single-clause[optseq] new-line
35861 structured-block */
35863 #define OMP_SINGLE_CLAUSE_MASK \
35864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35870 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35872 tree stmt
= make_node (OMP_SINGLE
);
35873 TREE_TYPE (stmt
) = void_type_node
;
35875 OMP_SINGLE_CLAUSES (stmt
)
35876 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35877 "#pragma omp single", pragma_tok
);
35878 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35880 return add_stmt (stmt
);
35884 # pragma omp task task-clause[optseq] new-line
35885 structured-block */
35887 #define OMP_TASK_CLAUSE_MASK \
35888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35900 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35902 tree clauses
, block
;
35905 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35906 "#pragma omp task", pragma_tok
);
35907 block
= begin_omp_task ();
35908 save
= cp_parser_begin_omp_structured_block (parser
);
35909 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35910 cp_parser_end_omp_structured_block (parser
, save
);
35911 return finish_omp_task (clauses
, block
);
35915 # pragma omp taskwait new-line */
35918 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35920 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35921 finish_omp_taskwait ();
35925 # pragma omp taskyield new-line */
35928 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35930 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35931 finish_omp_taskyield ();
35935 # pragma omp taskgroup new-line
35936 structured-block */
35939 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35941 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35942 return c_finish_omp_taskgroup (input_location
,
35943 cp_parser_omp_structured_block (parser
,
35949 # pragma omp threadprivate (variable-list) */
35952 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35956 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35957 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35959 finish_omp_threadprivate (vars
);
35963 # pragma omp cancel cancel-clause[optseq] new-line */
35965 #define OMP_CANCEL_CLAUSE_MASK \
35966 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35973 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35975 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35976 "#pragma omp cancel", pragma_tok
);
35977 finish_omp_cancel (clauses
);
35981 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35983 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35990 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35991 enum pragma_context context
)
35994 bool point_seen
= false;
35996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35998 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35999 const char *p
= IDENTIFIER_POINTER (id
);
36001 if (strcmp (p
, "point") == 0)
36003 cp_lexer_consume_token (parser
->lexer
);
36009 cp_parser_error (parser
, "expected %<point%>");
36010 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36014 if (context
!= pragma_compound
)
36016 if (context
== pragma_stmt
)
36017 error_at (pragma_tok
->location
,
36018 "%<#pragma %s%> may only be used in compound statements",
36019 "omp cancellation point");
36021 cp_parser_error (parser
, "expected declaration specifiers");
36022 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36026 clauses
= cp_parser_omp_all_clauses (parser
,
36027 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36028 "#pragma omp cancellation point",
36030 finish_omp_cancellation_point (clauses
);
36034 #pragma omp distribute distribute-clause[optseq] new-line
36037 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36045 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36046 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36049 tree clauses
, sb
, ret
;
36051 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36053 strcat (p_name
, " distribute");
36054 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36058 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36059 const char *p
= IDENTIFIER_POINTER (id
);
36061 bool parallel
= false;
36063 if (strcmp (p
, "simd") == 0)
36066 parallel
= strcmp (p
, "parallel") == 0;
36067 if (parallel
|| simd
)
36069 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36070 if (cclauses
== NULL
)
36071 cclauses
= cclauses_buf
;
36072 cp_lexer_consume_token (parser
->lexer
);
36073 if (!flag_openmp
) /* flag_openmp_simd */
36076 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36079 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36082 sb
= begin_omp_structured_block ();
36083 save
= cp_parser_begin_omp_structured_block (parser
);
36085 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36088 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36090 cp_parser_end_omp_structured_block (parser
, save
);
36091 tree body
= finish_omp_structured_block (sb
);
36094 ret
= make_node (OMP_DISTRIBUTE
);
36095 TREE_TYPE (ret
) = void_type_node
;
36096 OMP_FOR_BODY (ret
) = body
;
36097 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36098 SET_EXPR_LOCATION (ret
, loc
);
36103 if (!flag_openmp
) /* flag_openmp_simd */
36105 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36109 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36113 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36114 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36117 sb
= begin_omp_structured_block ();
36118 save
= cp_parser_begin_omp_structured_block (parser
);
36120 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36122 cp_parser_end_omp_structured_block (parser
, save
);
36123 add_stmt (finish_omp_structured_block (sb
));
36129 # pragma omp teams teams-clause[optseq] new-line
36130 structured-block */
36132 #define OMP_TEAMS_CLAUSE_MASK \
36133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36142 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36143 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36146 tree clauses
, sb
, ret
;
36148 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36150 strcat (p_name
, " teams");
36151 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36155 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36156 const char *p
= IDENTIFIER_POINTER (id
);
36157 if (strcmp (p
, "distribute") == 0)
36159 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36160 if (cclauses
== NULL
)
36161 cclauses
= cclauses_buf
;
36163 cp_lexer_consume_token (parser
->lexer
);
36164 if (!flag_openmp
) /* flag_openmp_simd */
36165 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36167 sb
= begin_omp_structured_block ();
36168 save
= cp_parser_begin_omp_structured_block (parser
);
36169 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36171 cp_parser_end_omp_structured_block (parser
, save
);
36172 tree body
= finish_omp_structured_block (sb
);
36175 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36176 ret
= make_node (OMP_TEAMS
);
36177 TREE_TYPE (ret
) = void_type_node
;
36178 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36179 OMP_TEAMS_BODY (ret
) = body
;
36180 OMP_TEAMS_COMBINED (ret
) = 1;
36181 SET_EXPR_LOCATION (ret
, loc
);
36182 return add_stmt (ret
);
36185 if (!flag_openmp
) /* flag_openmp_simd */
36187 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36191 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36195 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36196 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36199 tree stmt
= make_node (OMP_TEAMS
);
36200 TREE_TYPE (stmt
) = void_type_node
;
36201 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36202 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36203 SET_EXPR_LOCATION (stmt
, loc
);
36205 return add_stmt (stmt
);
36209 # pragma omp target data target-data-clause[optseq] new-line
36210 structured-block */
36212 #define OMP_TARGET_DATA_CLAUSE_MASK \
36213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36219 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36222 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36223 "#pragma omp target data", pragma_tok
);
36225 for (tree
*pc
= &clauses
; *pc
;)
36227 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36228 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36231 case GOMP_MAP_ALWAYS_TO
:
36232 case GOMP_MAP_FROM
:
36233 case GOMP_MAP_ALWAYS_FROM
:
36234 case GOMP_MAP_TOFROM
:
36235 case GOMP_MAP_ALWAYS_TOFROM
:
36236 case GOMP_MAP_ALLOC
:
36239 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36240 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36241 case GOMP_MAP_ALWAYS_POINTER
:
36245 error_at (OMP_CLAUSE_LOCATION (*pc
),
36246 "%<#pragma omp target data%> with map-type other "
36247 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36248 "on %<map%> clause");
36249 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36252 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36258 error_at (pragma_tok
->location
,
36259 "%<#pragma omp target data%> must contain at least "
36260 "one %<map%> clause");
36264 tree stmt
= make_node (OMP_TARGET_DATA
);
36265 TREE_TYPE (stmt
) = void_type_node
;
36266 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36268 keep_next_level (true);
36269 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36271 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36272 return add_stmt (stmt
);
36276 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36277 structured-block */
36279 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36287 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36288 enum pragma_context context
)
36290 bool data_seen
= false;
36291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36293 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36294 const char *p
= IDENTIFIER_POINTER (id
);
36296 if (strcmp (p
, "data") == 0)
36298 cp_lexer_consume_token (parser
->lexer
);
36304 cp_parser_error (parser
, "expected %<data%>");
36305 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36309 if (context
== pragma_stmt
)
36311 error_at (pragma_tok
->location
,
36312 "%<#pragma %s%> may only be used in compound statements",
36313 "omp target enter data");
36314 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36319 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36320 "#pragma omp target enter data", pragma_tok
);
36322 for (tree
*pc
= &clauses
; *pc
;)
36324 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36325 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36328 case GOMP_MAP_ALWAYS_TO
:
36329 case GOMP_MAP_ALLOC
:
36332 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36333 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36334 case GOMP_MAP_ALWAYS_POINTER
:
36338 error_at (OMP_CLAUSE_LOCATION (*pc
),
36339 "%<#pragma omp target enter data%> with map-type other "
36340 "than %<to%> or %<alloc%> on %<map%> clause");
36341 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36344 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36350 error_at (pragma_tok
->location
,
36351 "%<#pragma omp target enter data%> must contain at least "
36352 "one %<map%> clause");
36356 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36357 TREE_TYPE (stmt
) = void_type_node
;
36358 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36359 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36360 return add_stmt (stmt
);
36364 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36365 structured-block */
36367 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36375 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36376 enum pragma_context context
)
36378 bool data_seen
= false;
36379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36381 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36382 const char *p
= IDENTIFIER_POINTER (id
);
36384 if (strcmp (p
, "data") == 0)
36386 cp_lexer_consume_token (parser
->lexer
);
36392 cp_parser_error (parser
, "expected %<data%>");
36393 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36397 if (context
== pragma_stmt
)
36399 error_at (pragma_tok
->location
,
36400 "%<#pragma %s%> may only be used in compound statements",
36401 "omp target exit data");
36402 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36407 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36408 "#pragma omp target exit data", pragma_tok
);
36410 for (tree
*pc
= &clauses
; *pc
;)
36412 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36413 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36415 case GOMP_MAP_FROM
:
36416 case GOMP_MAP_ALWAYS_FROM
:
36417 case GOMP_MAP_RELEASE
:
36418 case GOMP_MAP_DELETE
:
36421 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36422 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36423 case GOMP_MAP_ALWAYS_POINTER
:
36427 error_at (OMP_CLAUSE_LOCATION (*pc
),
36428 "%<#pragma omp target exit data%> with map-type other "
36429 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36431 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36434 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36440 error_at (pragma_tok
->location
,
36441 "%<#pragma omp target exit data%> must contain at least "
36442 "one %<map%> clause");
36446 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36447 TREE_TYPE (stmt
) = void_type_node
;
36448 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36449 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36450 return add_stmt (stmt
);
36454 # pragma omp target update target-update-clause[optseq] new-line */
36456 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36465 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36466 enum pragma_context context
)
36468 if (context
== pragma_stmt
)
36470 error_at (pragma_tok
->location
,
36471 "%<#pragma %s%> may only be used in compound statements",
36472 "omp target update");
36473 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36478 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36479 "#pragma omp target update", pragma_tok
);
36480 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36481 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36483 error_at (pragma_tok
->location
,
36484 "%<#pragma omp target update%> must contain at least one "
36485 "%<from%> or %<to%> clauses");
36489 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36490 TREE_TYPE (stmt
) = void_type_node
;
36491 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36492 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36498 # pragma omp target target-clause[optseq] new-line
36499 structured-block */
36501 #define OMP_TARGET_CLAUSE_MASK \
36502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36513 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36514 enum pragma_context context
, bool *if_p
)
36516 tree
*pc
= NULL
, stmt
;
36518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36520 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36521 const char *p
= IDENTIFIER_POINTER (id
);
36522 enum tree_code ccode
= ERROR_MARK
;
36524 if (strcmp (p
, "teams") == 0)
36526 else if (strcmp (p
, "parallel") == 0)
36527 ccode
= OMP_PARALLEL
;
36528 else if (strcmp (p
, "simd") == 0)
36530 if (ccode
!= ERROR_MARK
)
36532 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36533 char p_name
[sizeof ("#pragma omp target teams distribute "
36534 "parallel for simd")];
36536 cp_lexer_consume_token (parser
->lexer
);
36537 strcpy (p_name
, "#pragma omp target");
36538 if (!flag_openmp
) /* flag_openmp_simd */
36544 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36545 OMP_TARGET_CLAUSE_MASK
,
36549 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36550 OMP_TARGET_CLAUSE_MASK
,
36554 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36555 OMP_TARGET_CLAUSE_MASK
,
36559 gcc_unreachable ();
36561 return stmt
!= NULL_TREE
;
36563 keep_next_level (true);
36564 tree sb
= begin_omp_structured_block (), ret
;
36565 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36569 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36570 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36574 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36575 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36579 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36580 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36584 gcc_unreachable ();
36586 cp_parser_end_omp_structured_block (parser
, save
);
36587 tree body
= finish_omp_structured_block (sb
);
36588 if (ret
== NULL_TREE
)
36590 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36592 /* For combined target teams, ensure the num_teams and
36593 thread_limit clause expressions are evaluated on the host,
36594 before entering the target construct. */
36596 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36597 c
; c
= OMP_CLAUSE_CHAIN (c
))
36598 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36599 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36600 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36602 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36603 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36604 if (expr
== error_mark_node
)
36606 tree tmp
= TARGET_EXPR_SLOT (expr
);
36608 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36609 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36610 OMP_CLAUSE_FIRSTPRIVATE
);
36611 OMP_CLAUSE_DECL (tc
) = tmp
;
36612 OMP_CLAUSE_CHAIN (tc
)
36613 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36614 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36617 tree stmt
= make_node (OMP_TARGET
);
36618 TREE_TYPE (stmt
) = void_type_node
;
36619 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36620 OMP_TARGET_BODY (stmt
) = body
;
36621 OMP_TARGET_COMBINED (stmt
) = 1;
36622 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36624 pc
= &OMP_TARGET_CLAUSES (stmt
);
36625 goto check_clauses
;
36627 else if (!flag_openmp
) /* flag_openmp_simd */
36629 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36632 else if (strcmp (p
, "data") == 0)
36634 cp_lexer_consume_token (parser
->lexer
);
36635 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36638 else if (strcmp (p
, "enter") == 0)
36640 cp_lexer_consume_token (parser
->lexer
);
36641 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36644 else if (strcmp (p
, "exit") == 0)
36646 cp_lexer_consume_token (parser
->lexer
);
36647 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36650 else if (strcmp (p
, "update") == 0)
36652 cp_lexer_consume_token (parser
->lexer
);
36653 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36656 if (!flag_openmp
) /* flag_openmp_simd */
36658 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36662 stmt
= make_node (OMP_TARGET
);
36663 TREE_TYPE (stmt
) = void_type_node
;
36665 OMP_TARGET_CLAUSES (stmt
)
36666 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36667 "#pragma omp target", pragma_tok
);
36668 pc
= &OMP_TARGET_CLAUSES (stmt
);
36669 keep_next_level (true);
36670 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36672 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36678 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36679 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36682 case GOMP_MAP_ALWAYS_TO
:
36683 case GOMP_MAP_FROM
:
36684 case GOMP_MAP_ALWAYS_FROM
:
36685 case GOMP_MAP_TOFROM
:
36686 case GOMP_MAP_ALWAYS_TOFROM
:
36687 case GOMP_MAP_ALLOC
:
36688 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36689 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36690 case GOMP_MAP_ALWAYS_POINTER
:
36693 error_at (OMP_CLAUSE_LOCATION (*pc
),
36694 "%<#pragma omp target%> with map-type other "
36695 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36696 "on %<map%> clause");
36697 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36700 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36706 # pragma acc cache (variable-list) new-line
36710 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36712 tree stmt
, clauses
;
36714 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36715 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36717 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36719 stmt
= make_node (OACC_CACHE
);
36720 TREE_TYPE (stmt
) = void_type_node
;
36721 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36722 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36729 # pragma acc data oacc-data-clause[optseq] new-line
36730 structured-block */
36732 #define OACC_DATA_CLAUSE_MASK \
36733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36746 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36748 tree stmt
, clauses
, block
;
36751 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36752 "#pragma acc data", pragma_tok
);
36754 block
= begin_omp_parallel ();
36755 save
= cp_parser_begin_omp_structured_block (parser
);
36756 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36757 cp_parser_end_omp_structured_block (parser
, save
);
36758 stmt
= finish_oacc_data (clauses
, block
);
36763 # pragma acc host_data <clauses> new-line
36764 structured-block */
36766 #define OACC_HOST_DATA_CLAUSE_MASK \
36767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36770 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36772 tree stmt
, clauses
, block
;
36775 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36776 "#pragma acc host_data", pragma_tok
);
36778 block
= begin_omp_parallel ();
36779 save
= cp_parser_begin_omp_structured_block (parser
);
36780 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36781 cp_parser_end_omp_structured_block (parser
, save
);
36782 stmt
= finish_oacc_host_data (clauses
, block
);
36787 # pragma acc declare oacc-data-clause[optseq] new-line
36790 #define OACC_DECLARE_CLAUSE_MASK \
36791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36805 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36807 tree clauses
, stmt
;
36808 bool error
= false;
36810 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36811 "#pragma acc declare", pragma_tok
, true);
36814 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36816 error_at (pragma_tok
->location
,
36817 "no valid clauses specified in %<#pragma acc declare%>");
36821 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36823 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36824 tree decl
= OMP_CLAUSE_DECL (t
);
36825 if (!DECL_P (decl
))
36827 error_at (loc
, "array section in %<#pragma acc declare%>");
36831 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36832 switch (OMP_CLAUSE_MAP_KIND (t
))
36834 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36835 case GOMP_MAP_FORCE_ALLOC
:
36836 case GOMP_MAP_FORCE_TO
:
36837 case GOMP_MAP_FORCE_DEVICEPTR
:
36838 case GOMP_MAP_DEVICE_RESIDENT
:
36841 case GOMP_MAP_LINK
:
36842 if (!global_bindings_p ()
36843 && (TREE_STATIC (decl
)
36844 || !DECL_EXTERNAL (decl
)))
36847 "%qD must be a global variable in "
36848 "%<#pragma acc declare link%>",
36856 if (global_bindings_p ())
36858 error_at (loc
, "invalid OpenACC clause at file scope");
36862 if (DECL_EXTERNAL (decl
))
36865 "invalid use of %<extern%> variable %qD "
36866 "in %<#pragma acc declare%>", decl
);
36870 else if (TREE_PUBLIC (decl
))
36873 "invalid use of %<global%> variable %qD "
36874 "in %<#pragma acc declare%>", decl
);
36881 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36882 || lookup_attribute ("omp declare target link",
36883 DECL_ATTRIBUTES (decl
)))
36885 error_at (loc
, "variable %qD used more than once with "
36886 "%<#pragma acc declare%>", decl
);
36895 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36896 id
= get_identifier ("omp declare target link");
36898 id
= get_identifier ("omp declare target");
36900 DECL_ATTRIBUTES (decl
)
36901 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36902 if (global_bindings_p ())
36904 symtab_node
*node
= symtab_node::get (decl
);
36907 node
->offloadable
= 1;
36908 if (ENABLE_OFFLOADING
)
36910 g
->have_offload
= true;
36911 if (is_a
<varpool_node
*> (node
))
36912 vec_safe_push (offload_vars
, decl
);
36919 if (error
|| global_bindings_p ())
36922 stmt
= make_node (OACC_DECLARE
);
36923 TREE_TYPE (stmt
) = void_type_node
;
36924 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36925 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36933 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36937 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36939 LOC is the location of the #pragma token.
36942 #define OACC_ENTER_DATA_CLAUSE_MASK \
36943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36951 #define OACC_EXIT_DATA_CLAUSE_MASK \
36952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36959 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36962 location_t loc
= pragma_tok
->location
;
36963 tree stmt
, clauses
;
36964 const char *p
= "";
36966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36967 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36969 if (strcmp (p
, "data") != 0)
36971 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36972 enter
? "enter" : "exit");
36973 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36977 cp_lexer_consume_token (parser
->lexer
);
36980 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36981 "#pragma acc enter data", pragma_tok
);
36983 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36984 "#pragma acc exit data", pragma_tok
);
36986 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36988 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36989 enter
? "enter" : "exit");
36993 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36994 TREE_TYPE (stmt
) = void_type_node
;
36995 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36996 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37002 # pragma acc loop oacc-loop-clause[optseq] new-line
37003 structured-block */
37005 #define OACC_LOOP_CLAUSE_MASK \
37006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37018 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37019 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37021 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37023 strcat (p_name
, " loop");
37024 mask
|= OACC_LOOP_CLAUSE_MASK
;
37026 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37030 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37032 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37034 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37037 tree block
= begin_omp_structured_block ();
37038 int save
= cp_parser_begin_omp_structured_block (parser
);
37039 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37040 cp_parser_end_omp_structured_block (parser
, save
);
37041 add_stmt (finish_omp_structured_block (block
));
37047 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37052 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37056 #define OACC_KERNELS_CLAUSE_MASK \
37057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37075 #define OACC_PARALLEL_CLAUSE_MASK \
37076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37098 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37099 char *p_name
, bool *if_p
)
37101 omp_clause_mask mask
;
37102 enum tree_code code
;
37103 switch (cp_parser_pragma_kind (pragma_tok
))
37105 case PRAGMA_OACC_KERNELS
:
37106 strcat (p_name
, " kernels");
37107 mask
= OACC_KERNELS_CLAUSE_MASK
;
37108 code
= OACC_KERNELS
;
37110 case PRAGMA_OACC_PARALLEL
:
37111 strcat (p_name
, " parallel");
37112 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37113 code
= OACC_PARALLEL
;
37116 gcc_unreachable ();
37119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37122 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37123 if (strcmp (p
, "loop") == 0)
37125 cp_lexer_consume_token (parser
->lexer
);
37126 tree block
= begin_omp_parallel ();
37128 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37130 return finish_omp_construct (code
, block
, clauses
);
37134 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37136 tree block
= begin_omp_parallel ();
37137 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37138 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37139 cp_parser_end_omp_structured_block (parser
, save
);
37140 return finish_omp_construct (code
, block
, clauses
);
37144 # pragma acc update oacc-update-clause[optseq] new-line
37147 #define OACC_UPDATE_CLAUSE_MASK \
37148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37156 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37158 tree stmt
, clauses
;
37160 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37161 "#pragma acc update", pragma_tok
);
37163 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37165 error_at (pragma_tok
->location
,
37166 "%<#pragma acc update%> must contain at least one "
37167 "%<device%> or %<host%> or %<self%> clause");
37171 stmt
= make_node (OACC_UPDATE
);
37172 TREE_TYPE (stmt
) = void_type_node
;
37173 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37174 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37180 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37182 LOC is the location of the #pragma token.
37185 #define OACC_WAIT_CLAUSE_MASK \
37186 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37189 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37191 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37192 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37194 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37195 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37197 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37198 "#pragma acc wait", pragma_tok
);
37200 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37201 stmt
= finish_expr_stmt (stmt
);
37207 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37209 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37218 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37219 enum pragma_context context
)
37221 bool first_p
= parser
->omp_declare_simd
== NULL
;
37222 cp_omp_declare_simd_data data
;
37225 data
.error_seen
= false;
37226 data
.fndecl_seen
= false;
37227 data
.tokens
= vNULL
;
37228 data
.clauses
= NULL_TREE
;
37229 /* It is safe to take the address of a local variable; it will only be
37230 used while this scope is live. */
37231 parser
->omp_declare_simd
= &data
;
37234 /* Store away all pragma tokens. */
37235 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37236 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37237 cp_lexer_consume_token (parser
->lexer
);
37238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37239 parser
->omp_declare_simd
->error_seen
= true;
37240 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37241 struct cp_token_cache
*cp
37242 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37243 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37247 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37248 cp_parser_pragma (parser
, context
, NULL
);
37251 case pragma_external
:
37252 cp_parser_declaration (parser
);
37254 case pragma_member
:
37255 cp_parser_member_declaration (parser
);
37257 case pragma_objc_icode
:
37258 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37261 cp_parser_declaration_statement (parser
);
37264 if (parser
->omp_declare_simd
37265 && !parser
->omp_declare_simd
->error_seen
37266 && !parser
->omp_declare_simd
->fndecl_seen
)
37267 error_at (pragma_tok
->location
,
37268 "%<#pragma omp declare simd%> not immediately followed by "
37269 "function declaration or definition");
37270 data
.tokens
.release ();
37271 parser
->omp_declare_simd
= NULL
;
37275 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37276 This function is modelled similar to the late parsing of omp declare
37280 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
37282 struct cp_token_cache
*ce
;
37283 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
37286 if (parser
->omp_declare_simd
!= NULL
37287 || lookup_attribute ("simd", attrs
))
37289 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37290 "used in the same function marked as a Cilk Plus SIMD-enabled "
37292 parser
->cilk_simd_fn_info
->tokens
.release ();
37293 XDELETE (parser
->cilk_simd_fn_info
);
37294 parser
->cilk_simd_fn_info
= NULL
;
37297 if (!info
->error_seen
&& info
->fndecl_seen
)
37299 error ("vector attribute not immediately followed by a single function"
37300 " declaration or definition");
37301 info
->error_seen
= true;
37303 if (info
->error_seen
)
37306 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
37310 cp_parser_push_lexer_for_tokens (parser
, ce
);
37311 parser
->lexer
->in_pragma
= true;
37312 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
37313 "SIMD-enabled functions attribute",
37315 cp_parser_pop_lexer (parser
);
37317 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37319 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
37320 TREE_CHAIN (c
) = attrs
;
37323 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37324 TREE_CHAIN (c
) = attrs
;
37325 if (processing_template_decl
)
37326 ATTR_IS_DEPENDENT (c
) = 1;
37329 info
->fndecl_seen
= true;
37330 parser
->cilk_simd_fn_info
->tokens
.release ();
37331 XDELETE (parser
->cilk_simd_fn_info
);
37332 parser
->cilk_simd_fn_info
= NULL
;
37336 /* Finalize #pragma omp declare simd clauses after direct declarator has
37337 been parsed, and put that into "omp declare simd" attribute. */
37340 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37342 struct cp_token_cache
*ce
;
37343 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37346 if (!data
->error_seen
&& data
->fndecl_seen
)
37348 error ("%<#pragma omp declare simd%> not immediately followed by "
37349 "a single function declaration or definition");
37350 data
->error_seen
= true;
37352 if (data
->error_seen
)
37355 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37359 cp_parser_push_lexer_for_tokens (parser
, ce
);
37360 parser
->lexer
->in_pragma
= true;
37361 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37362 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37363 cp_lexer_consume_token (parser
->lexer
);
37364 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37365 "#pragma omp declare simd", pragma_tok
);
37366 cp_parser_pop_lexer (parser
);
37368 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37369 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37370 TREE_CHAIN (c
) = attrs
;
37371 if (processing_template_decl
)
37372 ATTR_IS_DEPENDENT (c
) = 1;
37376 data
->fndecl_seen
= true;
37382 # pragma omp declare target new-line
37383 declarations and definitions
37384 # pragma omp end declare target new-line
37387 # pragma omp declare target ( extended-list ) new-line
37389 # pragma omp declare target declare-target-clauses[seq] new-line */
37391 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37396 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37398 tree clauses
= NULL_TREE
;
37399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37401 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37402 "#pragma omp declare target", pragma_tok
);
37403 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37405 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37407 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37408 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37412 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37413 scope_chain
->omp_declare_target_attribute
++;
37416 if (scope_chain
->omp_declare_target_attribute
)
37417 error_at (pragma_tok
->location
,
37418 "%<#pragma omp declare target%> with clauses in between "
37419 "%<#pragma omp declare target%> without clauses and "
37420 "%<#pragma omp end declare target%>");
37421 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37423 tree t
= OMP_CLAUSE_DECL (c
), id
;
37424 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37425 tree at2
= lookup_attribute ("omp declare target link",
37426 DECL_ATTRIBUTES (t
));
37427 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37429 id
= get_identifier ("omp declare target link");
37430 std::swap (at1
, at2
);
37433 id
= get_identifier ("omp declare target");
37436 error_at (OMP_CLAUSE_LOCATION (c
),
37437 "%qD specified both in declare target %<link%> and %<to%>"
37443 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37444 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37447 symtab_node
*node
= symtab_node::get (t
);
37450 node
->offloadable
= 1;
37451 if (ENABLE_OFFLOADING
)
37453 g
->have_offload
= true;
37454 if (is_a
<varpool_node
*> (node
))
37455 vec_safe_push (offload_vars
, t
);
37463 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37465 const char *p
= "";
37466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37468 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37469 p
= IDENTIFIER_POINTER (id
);
37471 if (strcmp (p
, "declare") == 0)
37473 cp_lexer_consume_token (parser
->lexer
);
37475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37477 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37478 p
= IDENTIFIER_POINTER (id
);
37480 if (strcmp (p
, "target") == 0)
37481 cp_lexer_consume_token (parser
->lexer
);
37484 cp_parser_error (parser
, "expected %<target%>");
37485 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37491 cp_parser_error (parser
, "expected %<declare%>");
37492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37495 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37496 if (!scope_chain
->omp_declare_target_attribute
)
37497 error_at (pragma_tok
->location
,
37498 "%<#pragma omp end declare target%> without corresponding "
37499 "%<#pragma omp declare target%>");
37501 scope_chain
->omp_declare_target_attribute
--;
37504 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37505 expression and optional initializer clause of
37506 #pragma omp declare reduction. We store the expression(s) as
37507 either 3, 6 or 7 special statements inside of the artificial function's
37508 body. The first two statements are DECL_EXPRs for the artificial
37509 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37510 expression that uses those variables.
37511 If there was any INITIALIZER clause, this is followed by further statements,
37512 the fourth and fifth statements are DECL_EXPRs for the artificial
37513 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37514 constructor variant (first token after open paren is not omp_priv),
37515 then the sixth statement is a statement with the function call expression
37516 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37517 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37518 to initialize the OMP_PRIV artificial variable and there is seventh
37519 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37522 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37524 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37525 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37526 type
= TREE_TYPE (type
);
37527 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37528 DECL_ARTIFICIAL (omp_out
) = 1;
37529 pushdecl (omp_out
);
37530 add_decl_expr (omp_out
);
37531 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37532 DECL_ARTIFICIAL (omp_in
) = 1;
37534 add_decl_expr (omp_in
);
37536 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37538 keep_next_level (true);
37539 tree block
= begin_omp_structured_block ();
37540 combiner
= cp_parser_expression (parser
);
37541 finish_expr_stmt (combiner
);
37542 block
= finish_omp_structured_block (block
);
37545 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37548 const char *p
= "";
37549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37551 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37552 p
= IDENTIFIER_POINTER (id
);
37555 if (strcmp (p
, "initializer") == 0)
37557 cp_lexer_consume_token (parser
->lexer
);
37558 matching_parens parens
;
37559 if (!parens
.require_open (parser
))
37563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37565 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37566 p
= IDENTIFIER_POINTER (id
);
37569 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37570 DECL_ARTIFICIAL (omp_priv
) = 1;
37571 pushdecl (omp_priv
);
37572 add_decl_expr (omp_priv
);
37573 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37574 DECL_ARTIFICIAL (omp_orig
) = 1;
37575 pushdecl (omp_orig
);
37576 add_decl_expr (omp_orig
);
37578 keep_next_level (true);
37579 block
= begin_omp_structured_block ();
37582 if (strcmp (p
, "omp_priv") == 0)
37584 bool is_direct_init
, is_non_constant_init
;
37586 cp_lexer_consume_token (parser
->lexer
);
37587 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37589 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37590 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37592 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37593 == CPP_CLOSE_PAREN
))
37595 finish_omp_structured_block (block
);
37596 error ("invalid initializer clause");
37599 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37600 &is_non_constant_init
);
37601 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37602 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37606 cp_parser_parse_tentatively (parser
);
37607 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37608 /*check_dependency_p=*/true,
37609 /*template_p=*/NULL
,
37610 /*declarator_p=*/false,
37611 /*optional_p=*/false);
37612 vec
<tree
, va_gc
> *args
;
37613 if (fn_name
== error_mark_node
37614 || cp_parser_error_occurred (parser
)
37615 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37616 || ((args
= cp_parser_parenthesized_expression_list
37617 (parser
, non_attr
, /*cast_p=*/false,
37618 /*allow_expansion_p=*/true,
37619 /*non_constant_p=*/NULL
)),
37620 cp_parser_error_occurred (parser
)))
37622 finish_omp_structured_block (block
);
37623 cp_parser_abort_tentative_parse (parser
);
37624 cp_parser_error (parser
, "expected id-expression (arguments)");
37629 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37630 if (arg
== omp_priv
37631 || (TREE_CODE (arg
) == ADDR_EXPR
37632 && TREE_OPERAND (arg
, 0) == omp_priv
))
37634 cp_parser_abort_tentative_parse (parser
);
37635 if (arg
== NULL_TREE
)
37636 error ("one of the initializer call arguments should be %<omp_priv%>"
37637 " or %<&omp_priv%>");
37638 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37640 finish_expr_stmt (initializer
);
37643 block
= finish_omp_structured_block (block
);
37644 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37648 add_decl_expr (omp_orig
);
37650 if (!parens
.require_close (parser
))
37654 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37655 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37662 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37663 initializer-clause[opt] new-line
37665 initializer-clause:
37666 initializer (omp_priv initializer)
37667 initializer (function-name (argument-list)) */
37670 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37671 enum pragma_context
)
37673 auto_vec
<tree
> types
;
37674 enum tree_code reduc_code
= ERROR_MARK
;
37675 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37677 cp_token
*first_token
;
37678 cp_token_cache
*cp
;
37682 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37683 p
= obstack_alloc (&declarator_obstack
, 0);
37685 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37688 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37691 reduc_code
= PLUS_EXPR
;
37694 reduc_code
= MULT_EXPR
;
37697 reduc_code
= MINUS_EXPR
;
37700 reduc_code
= BIT_AND_EXPR
;
37703 reduc_code
= BIT_XOR_EXPR
;
37706 reduc_code
= BIT_IOR_EXPR
;
37709 reduc_code
= TRUTH_ANDIF_EXPR
;
37712 reduc_code
= TRUTH_ORIF_EXPR
;
37715 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37718 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37719 "%<|%>, %<&&%>, %<||%> or identifier");
37723 if (reduc_code
!= ERROR_MARK
)
37724 cp_lexer_consume_token (parser
->lexer
);
37726 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37727 if (reduc_id
== error_mark_node
)
37730 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37733 /* Types may not be defined in declare reduction type list. */
37734 const char *saved_message
;
37735 saved_message
= parser
->type_definition_forbidden_message
;
37736 parser
->type_definition_forbidden_message
37737 = G_("types may not be defined in declare reduction type list");
37738 bool saved_colon_corrects_to_scope_p
;
37739 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37740 parser
->colon_corrects_to_scope_p
= false;
37741 bool saved_colon_doesnt_start_class_def_p
;
37742 saved_colon_doesnt_start_class_def_p
37743 = parser
->colon_doesnt_start_class_def_p
;
37744 parser
->colon_doesnt_start_class_def_p
= true;
37748 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37749 type
= cp_parser_type_id (parser
);
37750 if (type
== error_mark_node
)
37752 else if (ARITHMETIC_TYPE_P (type
)
37753 && (orig_reduc_id
== NULL_TREE
37754 || (TREE_CODE (type
) != COMPLEX_TYPE
37755 && (id_equal (orig_reduc_id
, "min")
37756 || id_equal (orig_reduc_id
, "max")))))
37757 error_at (loc
, "predeclared arithmetic type %qT in "
37758 "%<#pragma omp declare reduction%>", type
);
37759 else if (TREE_CODE (type
) == FUNCTION_TYPE
37760 || TREE_CODE (type
) == METHOD_TYPE
37761 || TREE_CODE (type
) == ARRAY_TYPE
)
37762 error_at (loc
, "function or array type %qT in "
37763 "%<#pragma omp declare reduction%>", type
);
37764 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37765 error_at (loc
, "reference type %qT in "
37766 "%<#pragma omp declare reduction%>", type
);
37767 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37768 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37769 "%<#pragma omp declare reduction%>", type
);
37771 types
.safe_push (type
);
37773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37774 cp_lexer_consume_token (parser
->lexer
);
37779 /* Restore the saved message. */
37780 parser
->type_definition_forbidden_message
= saved_message
;
37781 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37782 parser
->colon_doesnt_start_class_def_p
37783 = saved_colon_doesnt_start_class_def_p
;
37785 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37786 || types
.is_empty ())
37789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37793 first_token
= cp_lexer_peek_token (parser
->lexer
);
37796 FOR_EACH_VEC_ELT (types
, i
, type
)
37799 = build_function_type_list (void_type_node
,
37800 cp_build_reference_type (type
, false),
37802 tree this_reduc_id
= reduc_id
;
37803 if (!dependent_type_p (type
))
37804 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37805 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37806 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37807 DECL_ARTIFICIAL (fndecl
) = 1;
37808 DECL_EXTERNAL (fndecl
) = 1;
37809 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37810 DECL_IGNORED_P (fndecl
) = 1;
37811 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37812 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37813 DECL_ATTRIBUTES (fndecl
)
37814 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37815 DECL_ATTRIBUTES (fndecl
));
37816 if (processing_template_decl
)
37817 fndecl
= push_template_decl (fndecl
);
37818 bool block_scope
= false;
37819 tree block
= NULL_TREE
;
37820 if (current_function_decl
)
37822 block_scope
= true;
37823 DECL_CONTEXT (fndecl
) = global_namespace
;
37824 if (!processing_template_decl
)
37827 else if (current_class_type
)
37831 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37832 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37833 cp_lexer_consume_token (parser
->lexer
);
37834 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37836 cp
= cp_token_cache_new (first_token
,
37837 cp_lexer_peek_nth_token (parser
->lexer
,
37840 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37841 finish_member_declaration (fndecl
);
37842 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37843 DECL_PENDING_INLINE_P (fndecl
) = 1;
37844 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37849 DECL_CONTEXT (fndecl
) = current_namespace
;
37853 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37855 block
= begin_omp_structured_block ();
37858 cp_parser_push_lexer_for_tokens (parser
, cp
);
37859 parser
->lexer
->in_pragma
= true;
37861 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37864 finish_function (/*inline_p=*/false);
37866 DECL_CONTEXT (fndecl
) = current_function_decl
;
37868 cp_parser_pop_lexer (parser
);
37872 cp_parser_pop_lexer (parser
);
37874 finish_function (/*inline_p=*/false);
37877 DECL_CONTEXT (fndecl
) = current_function_decl
;
37878 block
= finish_omp_structured_block (block
);
37879 if (TREE_CODE (block
) == BIND_EXPR
)
37880 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37881 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37882 DECL_SAVED_TREE (fndecl
) = block
;
37883 if (processing_template_decl
)
37884 add_decl_expr (fndecl
);
37886 cp_check_omp_declare_reduction (fndecl
);
37887 if (cp
== NULL
&& types
.length () > 1)
37888 cp
= cp_token_cache_new (first_token
,
37889 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37890 if (errs
!= errorcount
)
37894 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37897 /* Free any declarators allocated. */
37898 obstack_free (&declarator_obstack
, p
);
37902 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37903 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37904 initializer-clause[opt] new-line
37905 #pragma omp declare target new-line */
37908 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37909 enum pragma_context context
)
37911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37913 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37914 const char *p
= IDENTIFIER_POINTER (id
);
37916 if (strcmp (p
, "simd") == 0)
37918 cp_lexer_consume_token (parser
->lexer
);
37919 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37923 cp_ensure_no_omp_declare_simd (parser
);
37924 if (strcmp (p
, "reduction") == 0)
37926 cp_lexer_consume_token (parser
->lexer
);
37927 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37931 if (!flag_openmp
) /* flag_openmp_simd */
37933 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37936 if (strcmp (p
, "target") == 0)
37938 cp_lexer_consume_token (parser
->lexer
);
37939 cp_parser_omp_declare_target (parser
, pragma_tok
);
37943 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37945 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37950 #pragma omp taskloop taskloop-clause[optseq] new-line
37953 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37956 #define OMP_TASKLOOP_CLAUSE_MASK \
37957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37973 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37974 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37977 tree clauses
, sb
, ret
;
37979 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37981 strcat (p_name
, " taskloop");
37982 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37986 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37987 const char *p
= IDENTIFIER_POINTER (id
);
37989 if (strcmp (p
, "simd") == 0)
37991 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37992 if (cclauses
== NULL
)
37993 cclauses
= cclauses_buf
;
37995 cp_lexer_consume_token (parser
->lexer
);
37996 if (!flag_openmp
) /* flag_openmp_simd */
37997 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37999 sb
= begin_omp_structured_block ();
38000 save
= cp_parser_begin_omp_structured_block (parser
);
38001 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38003 cp_parser_end_omp_structured_block (parser
, save
);
38004 tree body
= finish_omp_structured_block (sb
);
38007 ret
= make_node (OMP_TASKLOOP
);
38008 TREE_TYPE (ret
) = void_type_node
;
38009 OMP_FOR_BODY (ret
) = body
;
38010 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38011 SET_EXPR_LOCATION (ret
, loc
);
38016 if (!flag_openmp
) /* flag_openmp_simd */
38018 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38022 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38026 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38027 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38030 sb
= begin_omp_structured_block ();
38031 save
= cp_parser_begin_omp_structured_block (parser
);
38033 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38036 cp_parser_end_omp_structured_block (parser
, save
);
38037 add_stmt (finish_omp_structured_block (sb
));
38044 # pragma acc routine oacc-routine-clause[optseq] new-line
38045 function-definition
38047 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38050 #define OACC_ROUTINE_CLAUSE_MASK \
38051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38057 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38058 component, which must resolve to a declared namespace-scope
38059 function. The clauses are either processed directly (for a named
38060 function), or defered until the immediatley following declaration
38064 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38065 enum pragma_context context
)
38067 gcc_checking_assert (context
== pragma_external
);
38068 /* The checking for "another pragma following this one" in the "no optional
38069 '( name )'" case makes sure that we dont re-enter. */
38070 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38072 cp_oacc_routine_data data
;
38073 data
.error_seen
= false;
38074 data
.fndecl_seen
= false;
38075 data
.tokens
= vNULL
;
38076 data
.clauses
= NULL_TREE
;
38077 data
.loc
= pragma_tok
->location
;
38078 /* It is safe to take the address of a local variable; it will only be
38079 used while this scope is live. */
38080 parser
->oacc_routine
= &data
;
38082 /* Look for optional '( name )'. */
38083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38085 matching_parens parens
;
38086 parens
.consume_open (parser
); /* '(' */
38088 /* We parse the name as an id-expression. If it resolves to
38089 anything other than a non-overloaded function at namespace
38090 scope, it's an error. */
38091 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38092 tree name
= cp_parser_id_expression (parser
,
38093 /*template_keyword_p=*/false,
38094 /*check_dependency_p=*/false,
38095 /*template_p=*/NULL
,
38096 /*declarator_p=*/false,
38097 /*optional_p=*/false);
38098 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
38099 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38100 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38102 if (decl
== error_mark_node
38103 || !parens
.require_close (parser
))
38105 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38106 parser
->oacc_routine
= NULL
;
38111 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38112 "#pragma acc routine",
38113 cp_lexer_peek_token (parser
->lexer
));
38115 if (decl
&& is_overloaded_fn (decl
)
38116 && (TREE_CODE (decl
) != FUNCTION_DECL
38117 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38119 error_at (name_loc
,
38120 "%<#pragma acc routine%> names a set of overloads");
38121 parser
->oacc_routine
= NULL
;
38125 /* Perhaps we should use the same rule as declarations in different
38127 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38129 error_at (name_loc
,
38130 "%qD does not refer to a namespace scope function", decl
);
38131 parser
->oacc_routine
= NULL
;
38135 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38137 error_at (name_loc
, "%qD does not refer to a function", decl
);
38138 parser
->oacc_routine
= NULL
;
38142 cp_finalize_oacc_routine (parser
, decl
, false);
38143 parser
->oacc_routine
= NULL
;
38145 else /* No optional '( name )'. */
38147 /* Store away all pragma tokens. */
38148 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38149 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38150 cp_lexer_consume_token (parser
->lexer
);
38151 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38152 parser
->oacc_routine
->error_seen
= true;
38153 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38154 struct cp_token_cache
*cp
38155 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38156 parser
->oacc_routine
->tokens
.safe_push (cp
);
38158 /* Emit a helpful diagnostic if there's another pragma following this
38160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38162 cp_ensure_no_oacc_routine (parser
);
38163 data
.tokens
.release ();
38164 /* ..., and then just keep going. */
38168 /* We only have to consider the pragma_external case here. */
38169 cp_parser_declaration (parser
);
38170 if (parser
->oacc_routine
38171 && !parser
->oacc_routine
->fndecl_seen
)
38172 cp_ensure_no_oacc_routine (parser
);
38174 parser
->oacc_routine
= NULL
;
38175 data
.tokens
.release ();
38179 /* Finalize #pragma acc routine clauses after direct declarator has
38183 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38185 struct cp_token_cache
*ce
;
38186 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38188 if (!data
->error_seen
&& data
->fndecl_seen
)
38190 error_at (data
->loc
,
38191 "%<#pragma acc routine%> not immediately followed by "
38192 "a single function declaration or definition");
38193 data
->error_seen
= true;
38195 if (data
->error_seen
)
38198 gcc_checking_assert (data
->tokens
.length () == 1);
38199 ce
= data
->tokens
[0];
38201 cp_parser_push_lexer_for_tokens (parser
, ce
);
38202 parser
->lexer
->in_pragma
= true;
38203 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38205 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38206 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38207 parser
->oacc_routine
->clauses
38208 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38209 "#pragma acc routine", pragma_tok
);
38210 cp_parser_pop_lexer (parser
);
38211 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38217 /* Apply any saved OpenACC routine clauses to a just-parsed
38221 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38223 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38225 /* Keep going if we're in error reporting mode. */
38226 if (parser
->oacc_routine
->error_seen
38227 || fndecl
== error_mark_node
)
38230 if (parser
->oacc_routine
->fndecl_seen
)
38232 error_at (parser
->oacc_routine
->loc
,
38233 "%<#pragma acc routine%> not immediately followed by"
38234 " a single function declaration or definition");
38235 parser
->oacc_routine
= NULL
;
38238 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38240 cp_ensure_no_oacc_routine (parser
);
38244 if (oacc_get_fn_attrib (fndecl
))
38246 error_at (parser
->oacc_routine
->loc
,
38247 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38248 parser
->oacc_routine
= NULL
;
38252 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38254 error_at (parser
->oacc_routine
->loc
,
38256 ? G_("%<#pragma acc routine%> must be applied before use")
38257 : G_("%<#pragma acc routine%> must be applied before "
38259 parser
->oacc_routine
= NULL
;
38263 /* Process the routine's dimension clauses. */
38264 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38265 oacc_replace_fn_attrib (fndecl
, dims
);
38267 /* Add an "omp declare target" attribute. */
38268 DECL_ATTRIBUTES (fndecl
)
38269 = tree_cons (get_identifier ("omp declare target"),
38270 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38272 /* Don't unset parser->oacc_routine here: we may still need it to
38273 diagnose wrong usage. But, remember that we've used this "#pragma acc
38275 parser
->oacc_routine
->fndecl_seen
= true;
38279 /* Main entry point to OpenMP statement pragmas. */
38282 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38285 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38286 omp_clause_mask
mask (0);
38288 switch (cp_parser_pragma_kind (pragma_tok
))
38290 case PRAGMA_OACC_ATOMIC
:
38291 cp_parser_omp_atomic (parser
, pragma_tok
);
38293 case PRAGMA_OACC_CACHE
:
38294 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38296 case PRAGMA_OACC_DATA
:
38297 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38299 case PRAGMA_OACC_ENTER_DATA
:
38300 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38302 case PRAGMA_OACC_EXIT_DATA
:
38303 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38305 case PRAGMA_OACC_HOST_DATA
:
38306 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38308 case PRAGMA_OACC_KERNELS
:
38309 case PRAGMA_OACC_PARALLEL
:
38310 strcpy (p_name
, "#pragma acc");
38311 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38314 case PRAGMA_OACC_LOOP
:
38315 strcpy (p_name
, "#pragma acc");
38316 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38319 case PRAGMA_OACC_UPDATE
:
38320 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38322 case PRAGMA_OACC_WAIT
:
38323 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38325 case PRAGMA_OMP_ATOMIC
:
38326 cp_parser_omp_atomic (parser
, pragma_tok
);
38328 case PRAGMA_OMP_CRITICAL
:
38329 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38331 case PRAGMA_OMP_DISTRIBUTE
:
38332 strcpy (p_name
, "#pragma omp");
38333 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38336 case PRAGMA_OMP_FOR
:
38337 strcpy (p_name
, "#pragma omp");
38338 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38341 case PRAGMA_OMP_MASTER
:
38342 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38344 case PRAGMA_OMP_PARALLEL
:
38345 strcpy (p_name
, "#pragma omp");
38346 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38349 case PRAGMA_OMP_SECTIONS
:
38350 strcpy (p_name
, "#pragma omp");
38351 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38353 case PRAGMA_OMP_SIMD
:
38354 strcpy (p_name
, "#pragma omp");
38355 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38358 case PRAGMA_OMP_SINGLE
:
38359 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38361 case PRAGMA_OMP_TASK
:
38362 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38364 case PRAGMA_OMP_TASKGROUP
:
38365 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38367 case PRAGMA_OMP_TASKLOOP
:
38368 strcpy (p_name
, "#pragma omp");
38369 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38372 case PRAGMA_OMP_TEAMS
:
38373 strcpy (p_name
, "#pragma omp");
38374 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38378 gcc_unreachable ();
38381 protected_set_expr_location (stmt
, pragma_tok
->location
);
38384 /* Transactional Memory parsing routines. */
38386 /* Parse a transaction attribute.
38392 We use this instead of cp_parser_attributes_opt for transactions to avoid
38393 the pedwarn in C++98 mode. */
38396 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38399 tree attr_name
, attr
= NULL
;
38401 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38402 return cp_parser_attributes_opt (parser
);
38404 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38406 cp_lexer_consume_token (parser
->lexer
);
38407 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38410 token
= cp_lexer_peek_token (parser
->lexer
);
38411 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38413 token
= cp_lexer_consume_token (parser
->lexer
);
38415 attr_name
= (token
->type
== CPP_KEYWORD
38416 /* For keywords, use the canonical spelling,
38417 not the parsed identifier. */
38418 ? ridpointers
[(int) token
->keyword
]
38420 attr
= build_tree_list (attr_name
, NULL_TREE
);
38423 cp_parser_error (parser
, "expected identifier");
38425 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38427 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38431 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38433 transaction-statement:
38434 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38436 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38440 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38442 unsigned char old_in
= parser
->in_transaction
;
38443 unsigned char this_in
= 1, new_in
;
38444 enum rid keyword
= token
->keyword
;
38445 tree stmt
, attrs
, noex
;
38447 cp_lexer_consume_token (parser
->lexer
);
38449 if (keyword
== RID_TRANSACTION_RELAXED
38450 || keyword
== RID_SYNCHRONIZED
)
38451 this_in
|= TM_STMT_ATTR_RELAXED
;
38454 attrs
= cp_parser_txn_attribute_opt (parser
);
38456 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38459 /* Parse a noexcept specification. */
38460 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38461 noex
= boolean_true_node
;
38462 else if (keyword
== RID_ATOMIC_CANCEL
)
38464 /* cancel-and-throw is unimplemented. */
38465 sorry ("atomic_cancel");
38469 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38471 /* Keep track if we're in the lexical scope of an outer transaction. */
38472 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38474 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38476 parser
->in_transaction
= new_in
;
38477 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38478 parser
->in_transaction
= old_in
;
38480 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38485 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38487 transaction-expression:
38488 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38489 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38493 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38495 unsigned char old_in
= parser
->in_transaction
;
38496 unsigned char this_in
= 1;
38500 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38502 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38503 || keyword
== RID_TRANSACTION_RELAXED
);
38507 keyword
== RID_TRANSACTION_RELAXED
38508 ? G_("%<__transaction_relaxed%> without transactional memory "
38510 : G_("%<__transaction_atomic%> without transactional memory "
38511 "support enabled"));
38513 token
= cp_parser_require_keyword (parser
, keyword
,
38514 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38515 : RT_TRANSACTION_RELAXED
));
38516 gcc_assert (token
!= NULL
);
38518 if (keyword
== RID_TRANSACTION_RELAXED
)
38519 this_in
|= TM_STMT_ATTR_RELAXED
;
38521 /* Set this early. This might mean that we allow transaction_cancel in
38522 an expression that we find out later actually has to be a constexpr.
38523 However, we expect that cxx_constant_value will be able to deal with
38524 this; also, if the noexcept has no constexpr, then what we parse next
38525 really is a transaction's body. */
38526 parser
->in_transaction
= this_in
;
38528 /* Parse a noexcept specification. */
38529 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38532 if (!noex
|| !noex_expr
38533 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38535 matching_parens parens
;
38536 parens
.require_open (parser
);
38538 expr
= cp_parser_expression (parser
);
38539 expr
= finish_parenthesized_expr (expr
);
38541 parens
.require_close (parser
);
38545 /* The only expression that is available got parsed for the noexcept
38546 already. noexcept is true then. */
38548 noex
= boolean_true_node
;
38551 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38552 parser
->in_transaction
= old_in
;
38554 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38555 return error_mark_node
;
38557 return (flag_tm
? expr
: error_mark_node
);
38560 /* Parse a function-transaction-block.
38562 function-transaction-block:
38563 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38565 __transaction_atomic txn-attribute[opt] function-try-block
38566 __transaction_relaxed ctor-initializer[opt] function-body
38567 __transaction_relaxed function-try-block
38571 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38573 unsigned char old_in
= parser
->in_transaction
;
38574 unsigned char new_in
= 1;
38575 tree compound_stmt
, stmt
, attrs
;
38578 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38579 || keyword
== RID_TRANSACTION_RELAXED
);
38580 token
= cp_parser_require_keyword (parser
, keyword
,
38581 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38582 : RT_TRANSACTION_RELAXED
));
38583 gcc_assert (token
!= NULL
);
38585 if (keyword
== RID_TRANSACTION_RELAXED
)
38586 new_in
|= TM_STMT_ATTR_RELAXED
;
38589 attrs
= cp_parser_txn_attribute_opt (parser
);
38591 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38594 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38596 parser
->in_transaction
= new_in
;
38598 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38599 cp_parser_function_try_block (parser
);
38601 cp_parser_ctor_initializer_opt_and_function_body
38602 (parser
, /*in_function_try_block=*/false);
38604 parser
->in_transaction
= old_in
;
38606 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38609 /* Parse a __transaction_cancel statement.
38612 __transaction_cancel txn-attribute[opt] ;
38613 __transaction_cancel txn-attribute[opt] throw-expression ;
38615 ??? Cancel and throw is not yet implemented. */
38618 cp_parser_transaction_cancel (cp_parser
*parser
)
38621 bool is_outer
= false;
38624 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38625 RT_TRANSACTION_CANCEL
);
38626 gcc_assert (token
!= NULL
);
38628 attrs
= cp_parser_txn_attribute_opt (parser
);
38630 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38632 /* ??? Parse cancel-and-throw here. */
38634 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38638 error_at (token
->location
, "%<__transaction_cancel%> without "
38639 "transactional memory support enabled");
38640 return error_mark_node
;
38642 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38644 error_at (token
->location
, "%<__transaction_cancel%> within a "
38645 "%<__transaction_relaxed%>");
38646 return error_mark_node
;
38650 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38651 && !is_tm_may_cancel_outer (current_function_decl
))
38653 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38654 "within outer %<__transaction_atomic%>");
38655 error_at (token
->location
,
38656 " or a %<transaction_may_cancel_outer%> function");
38657 return error_mark_node
;
38660 else if (parser
->in_transaction
== 0)
38662 error_at (token
->location
, "%<__transaction_cancel%> not within "
38663 "%<__transaction_atomic%>");
38664 return error_mark_node
;
38667 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38675 static GTY (()) cp_parser
*the_parser
;
38678 /* Special handling for the first token or line in the file. The first
38679 thing in the file might be #pragma GCC pch_preprocess, which loads a
38680 PCH file, which is a GC collection point. So we need to handle this
38681 first pragma without benefit of an existing lexer structure.
38683 Always returns one token to the caller in *FIRST_TOKEN. This is
38684 either the true first token of the file, or the first token after
38685 the initial pragma. */
38688 cp_parser_initial_pragma (cp_token
*first_token
)
38692 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38693 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38696 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38697 if (first_token
->type
== CPP_STRING
)
38699 name
= first_token
->u
.value
;
38701 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38702 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38703 error_at (first_token
->location
,
38704 "junk at end of %<#pragma GCC pch_preprocess%>");
38707 error_at (first_token
->location
, "expected string literal");
38709 /* Skip to the end of the pragma. */
38710 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38711 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38713 /* Now actually load the PCH file. */
38715 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38717 /* Read one more token to return to our caller. We have to do this
38718 after reading the PCH file in, since its pointers have to be
38720 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38723 /* Parses the grainsize pragma for the _Cilk_for statement.
38725 #pragma cilk grainsize = <VALUE>. */
38728 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38730 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38732 tree exp
= cp_parser_binary_expression (parser
, false, false,
38733 PREC_NOT_OPERATOR
, NULL
);
38734 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38735 if (!exp
|| exp
== error_mark_node
)
38737 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
38741 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
38743 cp_parser_cilk_for (parser
, exp
, if_p
);
38745 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
38746 "%<#pragma cilk grainsize%> is not followed by "
38750 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38753 /* Normal parsing of a pragma token. Here we can (and must) use the
38757 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38759 cp_token
*pragma_tok
;
38764 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38765 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38766 parser
->lexer
->in_pragma
= true;
38768 id
= cp_parser_pragma_kind (pragma_tok
);
38769 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38770 cp_ensure_no_omp_declare_simd (parser
);
38773 case PRAGMA_GCC_PCH_PREPROCESS
:
38774 error_at (pragma_tok
->location
,
38775 "%<#pragma GCC pch_preprocess%> must be first");
38778 case PRAGMA_OMP_BARRIER
:
38781 case pragma_compound
:
38782 cp_parser_omp_barrier (parser
, pragma_tok
);
38785 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38786 "used in compound statements", "omp barrier");
38793 case PRAGMA_OMP_FLUSH
:
38796 case pragma_compound
:
38797 cp_parser_omp_flush (parser
, pragma_tok
);
38800 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38801 "used in compound statements", "omp flush");
38808 case PRAGMA_OMP_TASKWAIT
:
38811 case pragma_compound
:
38812 cp_parser_omp_taskwait (parser
, pragma_tok
);
38815 error_at (pragma_tok
->location
,
38816 "%<#pragma %s%> may only be used in compound statements",
38824 case PRAGMA_OMP_TASKYIELD
:
38827 case pragma_compound
:
38828 cp_parser_omp_taskyield (parser
, pragma_tok
);
38831 error_at (pragma_tok
->location
,
38832 "%<#pragma %s%> may only be used in compound statements",
38840 case PRAGMA_OMP_CANCEL
:
38843 case pragma_compound
:
38844 cp_parser_omp_cancel (parser
, pragma_tok
);
38847 error_at (pragma_tok
->location
,
38848 "%<#pragma %s%> may only be used in compound statements",
38856 case PRAGMA_OMP_CANCELLATION_POINT
:
38857 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38860 case PRAGMA_OMP_THREADPRIVATE
:
38861 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38864 case PRAGMA_OMP_DECLARE
:
38865 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38867 case PRAGMA_OACC_DECLARE
:
38868 cp_parser_oacc_declare (parser
, pragma_tok
);
38871 case PRAGMA_OACC_ENTER_DATA
:
38872 if (context
== pragma_stmt
)
38874 error_at (pragma_tok
->location
,
38875 "%<#pragma %s%> may only be used in compound statements",
38879 else if (context
!= pragma_compound
)
38881 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38884 case PRAGMA_OACC_EXIT_DATA
:
38885 if (context
== pragma_stmt
)
38887 error_at (pragma_tok
->location
,
38888 "%<#pragma %s%> may only be used in compound statements",
38892 else if (context
!= pragma_compound
)
38894 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38897 case PRAGMA_OACC_ROUTINE
:
38898 if (context
!= pragma_external
)
38900 error_at (pragma_tok
->location
,
38901 "%<#pragma acc routine%> must be at file scope");
38904 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38907 case PRAGMA_OACC_UPDATE
:
38908 if (context
== pragma_stmt
)
38910 error_at (pragma_tok
->location
,
38911 "%<#pragma %s%> may only be used in compound statements",
38915 else if (context
!= pragma_compound
)
38917 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38920 case PRAGMA_OACC_WAIT
:
38921 if (context
== pragma_stmt
)
38923 error_at (pragma_tok
->location
,
38924 "%<#pragma %s%> may only be used in compound statements",
38928 else if (context
!= pragma_compound
)
38930 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38933 case PRAGMA_OACC_ATOMIC
:
38934 case PRAGMA_OACC_CACHE
:
38935 case PRAGMA_OACC_DATA
:
38936 case PRAGMA_OACC_HOST_DATA
:
38937 case PRAGMA_OACC_KERNELS
:
38938 case PRAGMA_OACC_PARALLEL
:
38939 case PRAGMA_OACC_LOOP
:
38940 case PRAGMA_OMP_ATOMIC
:
38941 case PRAGMA_OMP_CRITICAL
:
38942 case PRAGMA_OMP_DISTRIBUTE
:
38943 case PRAGMA_OMP_FOR
:
38944 case PRAGMA_OMP_MASTER
:
38945 case PRAGMA_OMP_PARALLEL
:
38946 case PRAGMA_OMP_SECTIONS
:
38947 case PRAGMA_OMP_SIMD
:
38948 case PRAGMA_OMP_SINGLE
:
38949 case PRAGMA_OMP_TASK
:
38950 case PRAGMA_OMP_TASKGROUP
:
38951 case PRAGMA_OMP_TASKLOOP
:
38952 case PRAGMA_OMP_TEAMS
:
38953 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38955 stmt
= push_omp_privatization_clauses (false);
38956 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38957 pop_omp_privatization_clauses (stmt
);
38960 case PRAGMA_OMP_ORDERED
:
38961 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38963 stmt
= push_omp_privatization_clauses (false);
38964 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38965 pop_omp_privatization_clauses (stmt
);
38968 case PRAGMA_OMP_TARGET
:
38969 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38971 stmt
= push_omp_privatization_clauses (false);
38972 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38973 pop_omp_privatization_clauses (stmt
);
38976 case PRAGMA_OMP_END_DECLARE_TARGET
:
38977 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38980 case PRAGMA_OMP_SECTION
:
38981 error_at (pragma_tok
->location
,
38982 "%<#pragma omp section%> may only be used in "
38983 "%<#pragma omp sections%> construct");
38988 if (context
== pragma_external
)
38990 error_at (pragma_tok
->location
,
38991 "%<#pragma GCC ivdep%> must be inside a function");
38994 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38996 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38997 if (tok
->type
!= CPP_KEYWORD
38998 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38999 && tok
->keyword
!= RID_DO
))
39001 cp_parser_error (parser
, "for, while or do statement expected");
39004 cp_parser_iteration_statement (parser
, if_p
, true);
39008 case PRAGMA_CILK_SIMD
:
39009 if (context
== pragma_external
)
39011 error_at (pragma_tok
->location
,
39012 "%<#pragma simd%> must be inside a function");
39015 stmt
= push_omp_privatization_clauses (false);
39016 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
39017 pop_omp_privatization_clauses (stmt
);
39020 case PRAGMA_CILK_GRAINSIZE
:
39021 if (context
== pragma_external
)
39023 error_at (pragma_tok
->location
,
39024 "%<#pragma cilk grainsize%> must be inside a function");
39028 /* Ignore the pragma if Cilk Plus is not enabled. */
39031 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
39036 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
39037 "%<#pragma cilk grainsize%>");
39042 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39043 c_invoke_pragma_handler (id
);
39047 cp_parser_error (parser
, "expected declaration specifiers");
39051 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39055 /* The interface the pragma parsers have to the lexer. */
39058 pragma_lex (tree
*value
, location_t
*loc
)
39060 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39061 enum cpp_ttype ret
= tok
->type
;
39063 *value
= tok
->u
.value
;
39065 *loc
= tok
->location
;
39067 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39069 else if (ret
== CPP_STRING
)
39070 *value
= cp_parser_string_literal (the_parser
, false, false);
39073 if (ret
== CPP_KEYWORD
)
39075 cp_lexer_consume_token (the_parser
->lexer
);
39082 /* External interface. */
39084 /* Parse one entire translation unit. */
39087 c_parse_file (void)
39089 static bool already_called
= false;
39091 if (already_called
)
39092 fatal_error (input_location
,
39093 "inter-module optimizations not implemented for C++");
39094 already_called
= true;
39096 the_parser
= cp_parser_new ();
39097 push_deferring_access_checks (flag_access_control
39098 ? dk_no_deferred
: dk_no_check
);
39099 cp_parser_translation_unit (the_parser
);
39103 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
39104 vectorlength clause:
39106 vectorlength ( constant-expression ) */
39109 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
39112 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39114 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
39115 safelen clause. Thus, vectorlength is represented as OMP 4.0
39116 safelen. For SIMD-enabled function it is represented by OMP 4.0
39119 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
39122 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
39125 matching_parens parens
;
39126 if (!parens
.require_open (parser
))
39127 return error_mark_node
;
39129 expr
= cp_parser_constant_expression (parser
);
39130 expr
= maybe_constant_value (expr
);
39132 /* If expr == error_mark_node, then don't emit any errors nor
39133 create a clause. if any of the above functions returns
39134 error mark node then they would have emitted an error message. */
39135 if (expr
== error_mark_node
)
39137 else if (!TREE_TYPE (expr
)
39138 || !TREE_CONSTANT (expr
)
39139 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
39140 error_at (loc
, "vectorlength must be an integer constant");
39141 else if (TREE_CONSTANT (expr
)
39142 && !pow2p_hwi (TREE_INT_CST_LOW (expr
)))
39143 error_at (loc
, "vectorlength must be a power of 2");
39149 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
39150 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
39151 OMP_CLAUSE_CHAIN (c
) = clauses
;
39156 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
39157 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
39158 OMP_CLAUSE_CHAIN (c
) = clauses
;
39163 if (!parens
.require_close (parser
))
39164 return error_mark_node
;
39168 /* Handles the Cilk Plus #pragma simd linear clause.
39170 linear ( simd-linear-variable-list )
39172 simd-linear-variable-list:
39173 simd-linear-variable
39174 simd-linear-variable-list , simd-linear-variable
39176 simd-linear-variable:
39178 id-expression : simd-linear-step
39181 conditional-expression */
39184 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
39186 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39188 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39190 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39192 cp_parser_error (parser
, "expected identifier");
39193 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
39194 return error_mark_node
;
39197 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39198 parser
->colon_corrects_to_scope_p
= false;
39201 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
39204 cp_parser_error (parser
, "expected variable-name");
39205 clauses
= error_mark_node
;
39209 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
39211 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
39213 if (decl
== error_mark_node
)
39215 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
39217 clauses
= error_mark_node
;
39221 tree e
= NULL_TREE
;
39222 tree step_size
= integer_one_node
;
39224 /* If present, parse the linear step. Otherwise, assume the default
39226 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
39228 cp_lexer_consume_token (parser
->lexer
);
39230 e
= cp_parser_assignment_expression (parser
);
39231 e
= maybe_constant_value (e
);
39233 if (e
== error_mark_node
)
39235 /* If an error has occurred, then the whole pragma is
39236 considered ill-formed. Thus, no reason to keep
39238 clauses
= error_mark_node
;
39241 else if (type_dependent_expression_p (e
)
39242 || value_dependent_expression_p (e
)
39244 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
39245 && (TREE_CONSTANT (e
)
39249 cp_parser_error (parser
,
39250 "step size must be an integer constant "
39251 "expression or an integer variable");
39254 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39255 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
39256 OMP_CLAUSE_DECL (l
) = decl
;
39257 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
39258 OMP_CLAUSE_CHAIN (l
) = clauses
;
39261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39262 cp_lexer_consume_token (parser
->lexer
);
39263 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
39267 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39268 "expected %<,%> or %<)%> after %qE", decl
);
39269 clauses
= error_mark_node
;
39273 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39274 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
39278 /* Returns the name of the next clause. If the clause is not
39279 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39280 token is not consumed. Otherwise, the appropriate enum from the
39281 pragma_simd_clause is returned and the token is consumed. */
39283 static pragma_omp_clause
39284 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
39286 pragma_omp_clause clause_type
;
39287 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39289 if (token
->keyword
== RID_PRIVATE
)
39290 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
39291 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
39292 return PRAGMA_CILK_CLAUSE_NONE
;
39293 else if (id_equal (token
->u
.value
, "vectorlength"))
39294 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
39295 else if (id_equal (token
->u
.value
, "linear"))
39296 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
39297 else if (id_equal (token
->u
.value
, "firstprivate"))
39298 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
39299 else if (id_equal (token
->u
.value
, "lastprivate"))
39300 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
39301 else if (id_equal (token
->u
.value
, "reduction"))
39302 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
39304 return PRAGMA_CILK_CLAUSE_NONE
;
39306 cp_lexer_consume_token (parser
->lexer
);
39307 return clause_type
;
39310 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39313 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
39315 tree clauses
= NULL_TREE
;
39317 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39318 && clauses
!= error_mark_node
)
39320 pragma_omp_clause c_kind
;
39321 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
39322 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
39323 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
39324 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
39325 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
39326 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
39327 /* Use the OpenMP 4.0 equivalent function. */
39328 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
39329 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
39330 /* Use the OpenMP 4.0 equivalent function. */
39331 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
39333 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
39334 /* Use the OMP 4.0 equivalent function. */
39335 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
39337 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
39338 /* Use the OMP 4.0 equivalent function. */
39339 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
39342 clauses
= error_mark_node
;
39343 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
39348 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
39350 if (clauses
== error_mark_node
)
39351 return error_mark_node
;
39353 return finish_omp_clauses (clauses
, C_ORT_CILK
);
39356 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39359 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
39361 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
39363 if (clauses
== error_mark_node
)
39366 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
39368 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39369 "for statement expected");
39373 tree sb
= begin_omp_structured_block ();
39374 int save
= cp_parser_begin_omp_structured_block (parser
);
39375 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
39377 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
39378 cp_parser_end_omp_structured_block (parser
, save
);
39379 add_stmt (finish_omp_structured_block (sb
));
39382 /* Main entry-point for parsing Cilk Plus _Cilk_for
39383 loops. The return value is error_mark_node
39384 when errors happen and CILK_FOR tree on success. */
39387 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
39389 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
39390 gcc_unreachable ();
39392 tree sb
= begin_omp_structured_block ();
39393 int save
= cp_parser_begin_omp_structured_block (parser
);
39395 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
39396 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
39397 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
39398 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
39400 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
39402 cpp_validate_cilk_plus_loop (ret
);
39404 ret
= error_mark_node
;
39406 cp_parser_end_omp_structured_block (parser
, save
);
39407 add_stmt (finish_omp_structured_block (sb
));
39411 /* Create an identifier for a generic parameter type (a synthesized
39412 template parameter implied by `auto' or a concept identifier). */
39414 static GTY(()) int generic_parm_count
;
39416 make_generic_type_name ()
39419 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39420 return get_identifier (buf
);
39423 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39424 (creating a new template parameter list if necessary). Returns the newly
39425 created template type parm. */
39428 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39430 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39432 /* Before committing to modifying any scope, if we're in an
39433 implicit template scope, and we're trying to synthesize a
39434 constrained parameter, try to find a previous parameter with
39435 the same name. This is the same-type rule for abbreviated
39436 function templates.
39438 NOTE: We can generate implicit parameters when tentatively
39439 parsing a nested name specifier, only to reject that parse
39440 later. However, matching the same template-id as part of a
39441 direct-declarator should generate an identical template
39442 parameter, so this rule will merge them. */
39443 if (parser
->implicit_template_scope
&& constr
)
39445 tree t
= parser
->implicit_template_parms
;
39448 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39450 tree d
= TREE_VALUE (t
);
39451 if (TREE_CODE (d
) == PARM_DECL
)
39452 /* Return the TEMPLATE_PARM_INDEX. */
39453 d
= DECL_INITIAL (d
);
39456 t
= TREE_CHAIN (t
);
39460 /* We are either continuing a function template that already contains implicit
39461 template parameters, creating a new fully-implicit function template, or
39462 extending an existing explicit function template with implicit template
39465 cp_binding_level
*const entry_scope
= current_binding_level
;
39467 bool become_template
= false;
39468 cp_binding_level
*parent_scope
= 0;
39470 if (parser
->implicit_template_scope
)
39472 gcc_assert (parser
->implicit_template_parms
);
39474 current_binding_level
= parser
->implicit_template_scope
;
39478 /* Roll back to the existing template parameter scope (in the case of
39479 extending an explicit function template) or introduce a new template
39480 parameter scope ahead of the function parameter scope (or class scope
39481 in the case of out-of-line member definitions). The function scope is
39482 added back after template parameter synthesis below. */
39484 cp_binding_level
*scope
= entry_scope
;
39486 while (scope
->kind
== sk_function_parms
)
39488 parent_scope
= scope
;
39489 scope
= scope
->level_chain
;
39491 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39493 /* If not defining a class, then any class scope is a scope level in
39494 an out-of-line member definition. In this case simply wind back
39495 beyond the first such scope to inject the template parameter list.
39496 Otherwise wind back to the class being defined. The latter can
39497 occur in class member friend declarations such as:
39503 friend void A::foo (auto);
39506 The template parameter list synthesized for the friend declaration
39507 must be injected in the scope of 'B'. This can also occur in
39508 erroneous cases such as:
39514 void B::foo (auto) {}
39517 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39518 but, nevertheless, the template parameter list synthesized for the
39519 declarator should be injected into the scope of 'A' as if the
39520 ill-formed template was specified explicitly. */
39522 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39524 parent_scope
= scope
;
39525 scope
= scope
->level_chain
;
39529 current_binding_level
= scope
;
39531 if (scope
->kind
!= sk_template_parms
39532 || !function_being_declared_is_template_p (parser
))
39534 /* Introduce a new template parameter list for implicit template
39537 become_template
= true;
39539 parser
->implicit_template_scope
39540 = begin_scope (sk_template_parms
, NULL
);
39542 ++processing_template_decl
;
39544 parser
->fully_implicit_function_template_p
= true;
39545 ++parser
->num_template_parameter_lists
;
39549 /* Synthesize implicit template parameters at the end of the explicit
39550 template parameter list. */
39552 gcc_assert (current_template_parms
);
39554 parser
->implicit_template_scope
= scope
;
39556 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39557 parser
->implicit_template_parms
39558 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39562 /* Synthesize a new template parameter and track the current template
39563 parameter chain with implicit_template_parms. */
39565 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39566 tree synth_id
= make_generic_type_name ();
39567 tree synth_tmpl_parm
;
39568 bool non_type
= false;
39570 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39572 = finish_template_type_parm (class_type_node
, synth_id
);
39573 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39575 = finish_constrained_template_template_parm (proto
, synth_id
);
39578 synth_tmpl_parm
= copy_decl (proto
);
39579 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39583 // Attach the constraint to the parm before processing.
39584 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39585 TREE_TYPE (node
) = constr
;
39587 = process_template_parm (parser
->implicit_template_parms
,
39590 /*non_type=*/non_type
,
39591 /*param_pack=*/false);
39593 // Chain the new parameter to the list of implicit parameters.
39594 if (parser
->implicit_template_parms
)
39595 parser
->implicit_template_parms
39596 = TREE_CHAIN (parser
->implicit_template_parms
);
39598 parser
->implicit_template_parms
= new_parm
;
39600 tree new_decl
= get_local_decls ();
39602 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39603 new_decl
= DECL_INITIAL (new_decl
);
39605 /* If creating a fully implicit function template, start the new implicit
39606 template parameter list with this synthesized type, otherwise grow the
39607 current template parameter list. */
39609 if (become_template
)
39611 parent_scope
->level_chain
= current_binding_level
;
39613 tree new_parms
= make_tree_vec (1);
39614 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39615 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39616 new_parms
, current_template_parms
);
39620 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39621 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39622 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39623 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39626 // If the new parameter was constrained, we need to add that to the
39627 // constraints in the template parameter list.
39628 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39630 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39631 reqs
= conjoin_constraints (reqs
, req
);
39632 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39635 current_binding_level
= entry_scope
;
39640 /* Finish the declaration of a fully implicit function template. Such a
39641 template has no explicit template parameter list so has not been through the
39642 normal template head and tail processing. synthesize_implicit_template_parm
39643 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39644 provided if the declaration is a class member such that its template
39645 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39646 form is returned. Otherwise NULL_TREE is returned. */
39649 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39651 gcc_assert (parser
->fully_implicit_function_template_p
);
39653 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39654 && DECL_VIRTUAL_P (member_decl_opt
))
39656 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39657 "implicit templates may not be %<virtual%>");
39658 DECL_VIRTUAL_P (member_decl_opt
) = false;
39661 if (member_decl_opt
)
39662 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39663 end_template_decl ();
39665 parser
->fully_implicit_function_template_p
= false;
39666 --parser
->num_template_parameter_lists
;
39668 return member_decl_opt
;
39671 /* Helper function for diagnostics that have complained about things
39672 being used with 'extern "C"' linkage.
39674 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39677 maybe_show_extern_c_location (void)
39679 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39680 inform (the_parser
->innermost_linkage_specification_location
,
39681 "%<extern \"C\"%> linkage started here");
39684 #include "gt-cp-parser.h"