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/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "stringpool.h"
29 #include "print-tree.h"
31 #include "trans-mem.h"
34 #include "c-family/c-objc.h"
36 #include "tree-pretty-print.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token
=
55 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant
{
61 /* floating-point literal */
65 /* %<__FUNCTION__%> */
67 /* %<__PRETTY_FUNCTION__%> */
75 /* %<typeid%> operator */
77 /* non-constant compound literals */
85 /* an array reference */
91 /* the address of a label */
105 /* calls to overloaded operators */
109 /* a comma operator */
111 /* a call to a constructor */
113 /* a transaction expression */
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error
{
123 /* is not a class or namespace */
125 /* is not a class, namespace, or enumeration */
129 /* The various kinds of required token */
130 enum required_token
{
132 RT_SEMICOLON
, /* ';' */
133 RT_OPEN_PAREN
, /* '(' */
134 RT_CLOSE_BRACE
, /* '}' */
135 RT_OPEN_BRACE
, /* '{' */
136 RT_CLOSE_SQUARE
, /* ']' */
137 RT_OPEN_SQUARE
, /* '[' */
141 RT_GREATER
, /* '>' */
143 RT_ELLIPSIS
, /* '...' */
147 RT_COLON_SCOPE
, /* ':' or '::' */
148 RT_CLOSE_PAREN
, /* ')' */
149 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
150 RT_PRAGMA_EOL
, /* end of line */
151 RT_NAME
, /* identifier */
153 /* The type is CPP_KEYWORD */
155 RT_DELETE
, /* delete */
156 RT_RETURN
, /* return */
157 RT_WHILE
, /* while */
158 RT_EXTERN
, /* extern */
159 RT_STATIC_ASSERT
, /* static_assert */
160 RT_DECLTYPE
, /* decltype */
161 RT_OPERATOR
, /* operator */
162 RT_CLASS
, /* class */
163 RT_TEMPLATE
, /* template */
164 RT_NAMESPACE
, /* namespace */
165 RT_USING
, /* using */
168 RT_CATCH
, /* catch */
169 RT_THROW
, /* throw */
170 RT_LABEL
, /* __label__ */
171 RT_AT_TRY
, /* @try */
172 RT_AT_SYNCHRONIZED
, /* @synchronized */
173 RT_AT_THROW
, /* @throw */
175 RT_SELECT
, /* selection-statement */
176 RT_ITERATION
, /* iteration-statement */
177 RT_JUMP
, /* jump-statement */
178 RT_CLASS_KEY
, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
193 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
195 saved (parser
->in_type_id_in_expr_p
)
196 { parser
->in_type_id_in_expr_p
= set
; }
197 ~type_id_in_expr_sentinel ()
198 { parser
->in_type_id_in_expr_p
= saved
; }
203 static cp_lexer
*cp_lexer_new_main
205 static cp_lexer
*cp_lexer_new_from_tokens
206 (cp_token_cache
*tokens
);
207 static void cp_lexer_destroy
209 static int cp_lexer_saving_tokens
211 static cp_token
*cp_lexer_token_at
212 (cp_lexer
*, cp_token_position
);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer
*, cp_token
*);
215 static inline cp_token
*cp_lexer_peek_token
217 static cp_token
*cp_lexer_peek_nth_token
218 (cp_lexer
*, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer
*, enum cpp_ttype
);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer
*, enum rid
);
225 static cp_token
*cp_lexer_consume_token
227 static void cp_lexer_purge_token
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer
*, cp_token_position
);
231 static void cp_lexer_save_tokens
233 static void cp_lexer_commit_tokens
235 static void cp_lexer_rollback_tokens
237 static void cp_lexer_print_token
238 (FILE *, cp_token
*);
239 static inline bool cp_lexer_debugging_p
241 static void cp_lexer_start_debugging
242 (cp_lexer
*) ATTRIBUTE_UNUSED
;
243 static void cp_lexer_stop_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
246 static cp_token_cache
*cp_token_cache_new
247 (cp_token
*, cp_token
*);
249 static void cp_parser_initial_pragma
252 static void cp_parser_cilk_simd
253 (cp_parser
*, cp_token
*, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser
*, tree
, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser
*, tree
, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser
*, tree
, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream
;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand
;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
283 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
284 cp_token
*start_token
, unsigned num
,
285 cp_token
*curr_token
)
287 unsigned i
, nprinted
;
291 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
297 num
= buffer
->length ();
299 if (start_token
== NULL
)
300 start_token
= buffer
->address ();
302 if (start_token
> buffer
->address ())
304 cp_lexer_print_token (file
, &(*buffer
)[0]);
305 fprintf (file
, " ... ");
310 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
312 if (token
== start_token
)
319 if (token
== curr_token
)
320 fprintf (file
, "[[");
322 cp_lexer_print_token (file
, token
);
324 if (token
== curr_token
)
325 fprintf (file
, "]]");
331 case CPP_CLOSE_BRACE
:
341 if (i
== num
&& i
< buffer
->length ())
343 fprintf (file
, " ... ");
344 cp_lexer_print_token (file
, &buffer
->last ());
347 fprintf (file
, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
354 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
356 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
360 debug (vec
<cp_token
, va_gc
> &ref
)
362 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
366 debug (vec
<cp_token
, va_gc
> *ptr
)
371 fprintf (stderr
, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
379 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
383 fprintf (file
, "%s: ", desc
);
384 print_node_brief (file
, "", t
, 0);
389 /* Dump parser context C to FILE. */
392 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
394 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
396 print_node_brief (file
, "", c
->object_type
, 0);
397 fprintf (file
, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
407 cp_parser_context
*c
;
409 fprintf (file
, "Parsing context stack:\n");
410 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
412 fprintf (file
, "\t#%u: ", i
);
413 cp_debug_print_context (file
, c
);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
424 fprintf (file
, "%s: true\n", desc
);
428 /* Print an unparsed function entry UF to FILE. */
431 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
434 cp_default_arg_entry
*default_arg_fn
;
437 fprintf (file
, "\tFunctions with default args:\n");
439 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
442 fprintf (file
, "\t\tClass type: ");
443 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
444 fprintf (file
, "\t\tDeclaration: ");
445 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
446 fprintf (file
, "\n");
449 fprintf (file
, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
453 print_node_brief (file
, "", fn
, 0);
456 fprintf (file
, "\n");
458 fprintf (file
, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
462 print_node_brief (file
, "", fn
, 0);
465 fprintf (file
, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
472 cp_debug_print_unparsed_queues (FILE *file
,
473 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
476 cp_unparsed_functions_entry
*uf
;
478 fprintf (file
, "Unparsed functions\n");
479 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
481 fprintf (file
, "#%u:\n", i
);
482 cp_debug_print_unparsed_function (file
, uf
);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
493 cp_token
*next_token
, *first_token
, *start_token
;
498 next_token
= parser
->lexer
->next_token
;
499 first_token
= parser
->lexer
->buffer
->address ();
500 start_token
= (next_token
> first_token
+ window_size
/ 2)
501 ? next_token
- window_size
/ 2
503 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
512 cp_debug_parser (FILE *file
, cp_parser
*parser
)
514 const size_t window_size
= 20;
516 expanded_location eloc
;
521 fprintf (file
, "Parser state\n\n");
522 fprintf (file
, "Number of tokens: %u\n",
523 vec_safe_length (parser
->lexer
->buffer
));
524 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
525 cp_debug_print_tree_if_set (file
, "Object scope",
526 parser
->object_scope
);
527 cp_debug_print_tree_if_set (file
, "Qualifying scope",
528 parser
->qualifying_scope
);
529 cp_debug_print_context_stack (file
, parser
->context
);
530 cp_debug_print_flag (file
, "Allow GNU extensions",
531 parser
->allow_gnu_extensions_p
);
532 cp_debug_print_flag (file
, "'>' token is greater-than",
533 parser
->greater_than_is_operator_p
);
534 cp_debug_print_flag (file
, "Default args allowed in current "
535 "parameter list", parser
->default_arg_ok_p
);
536 cp_debug_print_flag (file
, "Parsing integral constant-expression",
537 parser
->integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Allow non-constant expression in current "
539 "constant-expression",
540 parser
->allow_non_integral_constant_expression_p
);
541 cp_debug_print_flag (file
, "Seen non-constant expression",
542 parser
->non_integral_constant_expression_p
);
543 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
545 parser
->local_variables_forbidden_p
);
546 cp_debug_print_flag (file
, "In unbraced linkage specification",
547 parser
->in_unbraced_linkage_specification_p
);
548 cp_debug_print_flag (file
, "Parsing a declarator",
549 parser
->in_declarator_p
);
550 cp_debug_print_flag (file
, "In template argument list",
551 parser
->in_template_argument_list_p
);
552 cp_debug_print_flag (file
, "Parsing an iteration statement",
553 parser
->in_statement
& IN_ITERATION_STMT
);
554 cp_debug_print_flag (file
, "Parsing a switch statement",
555 parser
->in_statement
& IN_SWITCH_STMT
);
556 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
557 parser
->in_statement
& IN_OMP_BLOCK
);
558 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
559 parser
->in_statement
& IN_CILK_SIMD_FOR
);
560 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
561 parser
->in_statement
& IN_OMP_FOR
);
562 cp_debug_print_flag (file
, "Parsing an if statement",
563 parser
->in_statement
& IN_IF_STMT
);
564 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
565 "context", parser
->in_type_id_in_expr_p
);
566 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
567 parser
->implicit_extern_c
);
568 cp_debug_print_flag (file
, "String expressions should be translated "
569 "to execution character set",
570 parser
->translate_strings_p
);
571 cp_debug_print_flag (file
, "Parsing function body outside of a "
572 "local class", parser
->in_function_body
);
573 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
574 parser
->colon_corrects_to_scope_p
);
575 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
576 parser
->colon_doesnt_start_class_def_p
);
577 if (parser
->type_definition_forbidden_message
)
578 fprintf (file
, "Error message for forbidden type definitions: %s\n",
579 parser
->type_definition_forbidden_message
);
580 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
581 fprintf (file
, "Number of class definitions in progress: %u\n",
582 parser
->num_classes_being_defined
);
583 fprintf (file
, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser
->num_template_parameter_lists
);
585 cp_debug_parser_tokens (file
, parser
, window_size
);
586 token
= parser
->lexer
->next_token
;
587 fprintf (file
, "Next token to parse:\n");
588 fprintf (file
, "\tToken: ");
589 cp_lexer_print_token (file
, token
);
590 eloc
= expand_location (token
->location
);
591 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
592 fprintf (file
, "\tLine: %d\n", eloc
.line
);
593 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
597 debug (cp_parser
&ref
)
599 cp_debug_parser (stderr
, &ref
);
603 debug (cp_parser
*ptr
)
608 fprintf (stderr
, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
614 cp_lexer_alloc (void)
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
623 /* Initially we are not debugging. */
624 lexer
->debugging_p
= false;
626 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
628 /* Create the buffer. */
629 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 cp_lexer_new_main (void)
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token
);
649 lexer
= cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer
->buffer
->quick_push (token
);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token
.type
!= CPP_EOF
)
657 cp_lexer_get_preprocessor_token (lexer
, &token
);
658 vec_safe_push (lexer
->buffer
, token
);
661 lexer
->last_token
= lexer
->buffer
->address ()
662 + lexer
->buffer
->length ()
664 lexer
->next_token
= lexer
->buffer
->length ()
665 ? lexer
->buffer
->address ()
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
672 gcc_assert (!lexer
->next_token
->purged_p
);
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
680 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
682 cp_token
*first
= cache
->first
;
683 cp_token
*last
= cache
->last
;
684 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
686 /* We do not own the buffer. */
687 lexer
->buffer
= NULL
;
688 lexer
->next_token
= first
== last
? &eof_token
: first
;
689 lexer
->last_token
= last
;
691 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
693 /* Initially we are not debugging. */
694 lexer
->debugging_p
= false;
696 gcc_assert (!lexer
->next_token
->purged_p
);
700 /* Frees all resources associated with LEXER. */
703 cp_lexer_destroy (cp_lexer
*lexer
)
705 vec_free (lexer
->buffer
);
706 lexer
->saved_tokens
.release ();
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
720 cp_lexer_debugging_p (cp_lexer
*lexer
)
722 if (!LEXER_DEBUGGING_ENABLED_P
)
725 return lexer
->debugging_p
;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
732 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
734 return lexer
->next_token
- previous_p
;
737 static inline cp_token
*
738 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
744 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
746 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer
*lexer
)
752 if (lexer
->next_token
== &eof_token
)
753 return lexer
->last_token
- 1;
755 return cp_lexer_token_position (lexer
, true);
758 static inline cp_token
*
759 cp_lexer_previous_token (cp_lexer
*lexer
)
761 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
763 /* Skip past purged tokens. */
766 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
770 return cp_lexer_token_at (lexer
, tp
);
773 /* nonzero if we are presently saving tokens. */
776 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
778 return lexer
->saved_tokens
.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
787 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
789 static int is_extern_c
= 0;
791 /* Get a new token from the preprocessor. */
793 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
794 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
795 token
->keyword
= RID_MAX
;
796 token
->purged_p
= false;
797 token
->error_reported
= false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c
+= pending_lang_change
;
803 pending_lang_change
= 0;
804 token
->implicit_extern_c
= is_extern_c
> 0;
806 /* Check to see if this token is a keyword. */
807 if (token
->type
== CPP_NAME
)
809 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
811 /* Mark this token as a keyword. */
812 token
->type
= CPP_KEYWORD
;
813 /* Record which keyword. */
814 token
->keyword
= C_RID_CODE (token
->u
.value
);
818 if (warn_cxx11_compat
819 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
820 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
822 /* Warn about the C++0x keyword (but still treat it as
824 warning (OPT_Wc__11_compat
,
825 "identifier %qE is a keyword in C++11",
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
833 token
->keyword
= RID_MAX
;
836 else if (token
->type
== CPP_AT_NAME
)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token
->type
= CPP_KEYWORD
;
840 switch (C_RID_CODE (token
->u
.value
))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
850 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
851 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
852 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
853 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
854 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
855 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
856 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
857 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
858 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
863 /* Update the globals input_location and the input file stack from TOKEN. */
865 cp_lexer_set_source_position_from_token (cp_token
*token
)
867 if (token
->type
!= CPP_EOF
)
869 input_location
= token
->location
;
873 /* Update the globals input_location and the input file stack from LEXER. */
875 cp_lexer_set_source_position (cp_lexer
*lexer
)
877 cp_token
*token
= cp_lexer_peek_token (lexer
);
878 cp_lexer_set_source_position_from_token (token
);
881 /* Return a pointer to the next token in the token stream, but do not
884 static inline cp_token
*
885 cp_lexer_peek_token (cp_lexer
*lexer
)
887 if (cp_lexer_debugging_p (lexer
))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
890 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
891 putc ('\n', cp_lexer_debug_stream
);
893 return lexer
->next_token
;
896 /* Return true if the next token has the indicated TYPE. */
899 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
901 return cp_lexer_peek_token (lexer
)->type
== type
;
904 /* Return true if the next token does not have the indicated TYPE. */
907 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
909 return !cp_lexer_next_token_is (lexer
, type
);
912 /* Return true if the next token is the indicated KEYWORD. */
915 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
917 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
921 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
923 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
927 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
929 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
932 /* Return true if the next token is not the indicated KEYWORD. */
935 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
937 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
940 /* Return true if KEYWORD can start a decl-specifier. */
943 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
950 /* Storage classes. */
956 /* Elaborated type specifiers. */
962 /* Simple type specifiers. */
976 /* GNU extensions. */
979 /* C++0x extensions. */
981 case RID_UNDERLYING_TYPE
:
986 if (keyword
>= RID_FIRST_INT_N
987 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
988 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
994 /* Return true if the next token is a keyword for a decl-specifier. */
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1001 token
= cp_lexer_peek_token (lexer
);
1002 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1008 token_is_decltype (cp_token
*t
)
1010 return (t
->keyword
== RID_DECLTYPE
1011 || t
->type
== CPP_DECLTYPE
);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1017 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1019 cp_token
*t
= cp_lexer_peek_token (lexer
);
1020 return token_is_decltype (t
);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1027 saved_checks_value (struct tree_check
*check_value
)
1029 /* Perform any access checks that were deferred. */
1030 vec
<deferred_access_check
, va_gc
> *checks
;
1031 deferred_access_check
*chk
;
1032 checks
= check_value
->checks
;
1036 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1037 perform_or_defer_access_check (chk
->binfo
,
1039 chk
->diag_decl
, tf_warning_or_error
);
1041 /* Return the stored value. */
1042 return check_value
->value
;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1052 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1056 /* N is 1-based, not zero-based. */
1059 if (cp_lexer_debugging_p (lexer
))
1060 fprintf (cp_lexer_debug_stream
,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1064 token
= lexer
->next_token
;
1065 gcc_assert (!n
|| token
!= &eof_token
);
1069 if (token
== lexer
->last_token
)
1075 if (!token
->purged_p
)
1079 if (cp_lexer_debugging_p (lexer
))
1081 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1082 putc ('\n', cp_lexer_debug_stream
);
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1092 cp_lexer_consume_token (cp_lexer
* lexer
)
1094 cp_token
*token
= lexer
->next_token
;
1096 gcc_assert (token
!= &eof_token
);
1097 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1101 lexer
->next_token
++;
1102 if (lexer
->next_token
== lexer
->last_token
)
1104 lexer
->next_token
= &eof_token
;
1109 while (lexer
->next_token
->purged_p
);
1111 cp_lexer_set_source_position_from_token (token
);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer
))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1117 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1118 putc ('\n', cp_lexer_debug_stream
);
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1129 cp_lexer_purge_token (cp_lexer
*lexer
)
1131 cp_token
*tok
= lexer
->next_token
;
1133 gcc_assert (tok
!= &eof_token
);
1134 tok
->purged_p
= true;
1135 tok
->location
= UNKNOWN_LOCATION
;
1136 tok
->u
.value
= NULL_TREE
;
1137 tok
->keyword
= RID_MAX
;
1142 if (tok
== lexer
->last_token
)
1148 while (tok
->purged_p
);
1149 lexer
->next_token
= tok
;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1157 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1159 cp_token
*peek
= lexer
->next_token
;
1161 if (peek
== &eof_token
)
1162 peek
= lexer
->last_token
;
1164 gcc_assert (tok
< peek
);
1166 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1168 tok
->purged_p
= true;
1169 tok
->location
= UNKNOWN_LOCATION
;
1170 tok
->u
.value
= NULL_TREE
;
1171 tok
->keyword
= RID_MAX
;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1179 cp_lexer_save_tokens (cp_lexer
* lexer
)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer
))
1183 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1185 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1188 /* Commit to the portion of the token stream most recently saved. */
1191 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer
))
1195 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1197 lexer
->saved_tokens
.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1204 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer
))
1208 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1210 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1218 struct saved_token_sentinel
1223 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1225 len
= lexer
->saved_tokens
.length ();
1226 cp_lexer_save_tokens (lexer
);
1230 cp_lexer_rollback_tokens (lexer
);
1233 ~saved_token_sentinel()
1236 cp_lexer_commit_tokens (lexer
);
1237 gcc_assert (lexer
->saved_tokens
.length () == len
);
1241 /* Print a representation of the TOKEN on the STREAM. */
1244 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names
[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1255 /* C++ parser token types - see "Manifest constants", above. */
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token
->type
)
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token
->u
.value
))
1271 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1278 case CPP_UTF8STRING
:
1279 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1283 print_generic_expr (stream
, token
->u
.value
);
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token
->type
< ARRAY_SIZE(token_names
))
1290 fputs (token_names
[token
->type
], stream
);
1292 fprintf (stream
, "[%d]", token
->type
);
1298 debug (cp_token
&ref
)
1300 cp_lexer_print_token (stderr
, &ref
);
1301 fprintf (stderr
, "\n");
1305 debug (cp_token
*ptr
)
1310 fprintf (stderr
, "<nil>\n");
1314 /* Start emitting debugging information. */
1317 cp_lexer_start_debugging (cp_lexer
* lexer
)
1319 if (!LEXER_DEBUGGING_ENABLED_P
)
1320 fatal_error (input_location
,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer
->debugging_p
= true;
1324 cp_lexer_debug_stream
= stderr
;
1327 /* Stop emitting debugging information. */
1330 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1332 if (!LEXER_DEBUGGING_ENABLED_P
)
1333 fatal_error (input_location
,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer
->debugging_p
= false;
1337 cp_lexer_debug_stream
= NULL
;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache
*
1343 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1345 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1346 cache
->first
= first
;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1355 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1357 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser
->omp_declare_simd
= NULL
;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1369 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1371 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1373 if (fndecl
== error_mark_node
)
1375 parser
->omp_declare_simd
= NULL
;
1378 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1380 cp_ensure_no_omp_declare_simd (parser
);
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1390 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1392 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1394 error_at (parser
->oacc_routine
->loc
,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser
->oacc_routine
= NULL
;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1406 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1408 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator
*make_call_declarator
1419 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1420 static cp_declarator
*make_array_declarator
1421 (cp_declarator
*, tree
);
1422 static cp_declarator
*make_pointer_declarator
1423 (cp_cv_quals
, cp_declarator
*, tree
);
1424 static cp_declarator
*make_reference_declarator
1425 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1426 static cp_declarator
*make_ptrmem_declarator
1427 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1429 /* An erroneous declarator. */
1430 static cp_declarator
*cp_error_declarator
;
1432 /* The obstack on which declarators and related data structures are
1434 static struct obstack declarator_obstack
;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes
)
1441 return obstack_alloc (&declarator_obstack
, bytes
);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator
*
1448 make_declarator (cp_declarator_kind kind
)
1450 cp_declarator
*declarator
;
1452 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1453 declarator
->kind
= kind
;
1454 declarator
->attributes
= NULL_TREE
;
1455 declarator
->std_attributes
= NULL_TREE
;
1456 declarator
->declarator
= NULL
;
1457 declarator
->parameter_pack_p
= false;
1458 declarator
->id_loc
= UNKNOWN_LOCATION
;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1469 static cp_declarator
*
1470 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1471 special_function_kind sfk
)
1473 cp_declarator
*declarator
;
1475 /* It is valid to write:
1477 class C { void f(); };
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1485 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1486 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1488 gcc_assert (identifier_p (unqualified_name
)
1489 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1492 declarator
= make_declarator (cdk_id
);
1493 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1494 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1495 declarator
->u
.id
.sfk
= sfk
;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1509 cp_declarator
*declarator
;
1511 declarator
= make_declarator (cdk_pointer
);
1512 declarator
->declarator
= target
;
1513 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1514 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1517 declarator
->id_loc
= target
->id_loc
;
1518 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1519 target
->parameter_pack_p
= false;
1522 declarator
->parameter_pack_p
= false;
1524 declarator
->std_attributes
= attributes
;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1534 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1535 bool rvalue_ref
, tree attributes
)
1537 cp_declarator
*declarator
;
1539 declarator
= make_declarator (cdk_reference
);
1540 declarator
->declarator
= target
;
1541 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1542 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1545 declarator
->id_loc
= target
->id_loc
;
1546 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1547 target
->parameter_pack_p
= false;
1550 declarator
->parameter_pack_p
= false;
1552 declarator
->std_attributes
= attributes
;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1563 cp_declarator
*pointee
,
1566 cp_declarator
*declarator
;
1568 declarator
= make_declarator (cdk_ptrmem
);
1569 declarator
->declarator
= pointee
;
1570 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1571 declarator
->u
.pointer
.class_type
= class_type
;
1575 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1576 pointee
->parameter_pack_p
= false;
1579 declarator
->parameter_pack_p
= false;
1581 declarator
->std_attributes
= attributes
;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1592 make_call_declarator (cp_declarator
*target
,
1594 cp_cv_quals cv_qualifiers
,
1595 cp_virt_specifiers virt_specifiers
,
1596 cp_ref_qualifier ref_qualifier
,
1598 tree exception_specification
,
1599 tree late_return_type
,
1600 tree requires_clause
)
1602 cp_declarator
*declarator
;
1604 declarator
= make_declarator (cdk_function
);
1605 declarator
->declarator
= target
;
1606 declarator
->u
.function
.parameters
= parms
;
1607 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1608 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1609 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1610 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1611 declarator
->u
.function
.exception_specification
= exception_specification
;
1612 declarator
->u
.function
.late_return_type
= late_return_type
;
1613 declarator
->u
.function
.requires_clause
= requires_clause
;
1616 declarator
->id_loc
= target
->id_loc
;
1617 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1618 target
->parameter_pack_p
= false;
1621 declarator
->parameter_pack_p
= false;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1630 make_array_declarator (cp_declarator
*element
, tree bounds
)
1632 cp_declarator
*declarator
;
1634 declarator
= make_declarator (cdk_array
);
1635 declarator
->declarator
= element
;
1636 declarator
->u
.array
.bounds
= bounds
;
1639 declarator
->id_loc
= element
->id_loc
;
1640 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1641 element
->parameter_pack_p
= false;
1644 declarator
->parameter_pack_p
= false;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1652 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1654 if (declarator
&& declarator
->parameter_pack_p
)
1655 /* We already saw an ellipsis. */
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1663 while (declarator
&& !found
)
1665 switch ((int)declarator
->kind
)
1677 declarator
= declarator
->declarator
;
1685 cp_parameter_declarator
*no_parameters
;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator
*
1691 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1692 cp_declarator
*declarator
,
1693 tree default_argument
,
1694 bool template_parameter_pack_p
= false)
1696 cp_parameter_declarator
*parameter
;
1698 parameter
= ((cp_parameter_declarator
*)
1699 alloc_declarator (sizeof (cp_parameter_declarator
)));
1700 parameter
->next
= NULL
;
1701 if (decl_specifiers
)
1702 parameter
->decl_specifiers
= *decl_specifiers
;
1704 clear_decl_specs (¶meter
->decl_specifiers
);
1705 parameter
->declarator
= declarator
;
1706 parameter
->default_argument
= default_argument
;
1707 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1715 function_declarator_p (const cp_declarator
*declarator
)
1719 if (declarator
->kind
== cdk_function
1720 && declarator
->declarator
->kind
== cdk_id
)
1722 if (declarator
->kind
== cdk_id
1723 || declarator
->kind
== cdk_decomp
1724 || declarator
->kind
== cdk_error
)
1726 declarator
= declarator
->declarator
;
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1792 CP_PARSER_FLAGS_NONE
= 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags
;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT
,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED
,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1832 PREC_LOGICAL_OR_EXPRESSION
,
1833 PREC_LOGICAL_AND_EXPRESSION
,
1834 PREC_INCLUSIVE_OR_EXPRESSION
,
1835 PREC_EXCLUSIVE_OR_EXPRESSION
,
1836 PREC_AND_EXPRESSION
,
1837 PREC_EQUALITY_EXPRESSION
,
1838 PREC_RELATIONAL_EXPRESSION
,
1839 PREC_SHIFT_EXPRESSION
,
1840 PREC_ADDITIVE_EXPRESSION
,
1841 PREC_MULTIPLICATIVE_EXPRESSION
,
1843 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type
;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type
;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec
;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type
;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type
;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec
;
1871 /* Location of the binary operation we are parsing. */
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1883 /* Constructors and destructors. */
1885 static cp_parser_context
*cp_parser_context_new
1886 (cp_parser_context
*);
1888 /* Class variables. */
1890 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1896 static const cp_parser_binary_operations_map_node binops
[] = {
1897 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1898 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1900 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1901 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1902 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1904 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1905 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1907 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1908 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1910 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1912 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1913 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1915 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1916 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1918 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1920 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1922 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1924 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1926 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1932 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context
*
1940 cp_parser_context_new (cp_parser_context
* next
)
1942 cp_parser_context
*context
;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list
!= NULL
)
1947 /* Pull the first entry from the free list. */
1948 context
= cp_parser_context_free_list
;
1949 cp_parser_context_free_list
= context
->next
;
1950 memset (context
, 0, sizeof (*context
));
1953 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1955 /* No errors have occurred yet in this context. */
1956 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context
->object_type
= next
->object_type
;
1964 /* Thread the stack. */
1965 context
->next
= next
;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1983 push_unparsed_function_queues (cp_parser
*parser
)
1985 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1986 vec_safe_push (parser
->unparsed_queues
, e
);
1990 pop_unparsed_function_queues (cp_parser
*parser
)
1992 release_tree_vector (unparsed_funs_with_definitions
);
1993 parser
->unparsed_queues
->pop ();
1998 /* Constructors and destructors. */
2000 static cp_parser
*cp_parser_new
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser
*, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2023 static tree cp_parser_userdef_string_literal
2025 static cp_expr cp_parser_userdef_numeric_literal
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser
*, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser
*, bool, bool, bool, bool, bool = false);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser
*, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser
*, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser
*, tree
, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2053 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2054 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser
*, tree
, tree
*, tree
*);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2063 static tree cp_parser_new_expression
2065 static vec
<tree
, va_gc
> *cp_parser_new_placement
2067 static tree cp_parser_new_type_id
2068 (cp_parser
*, tree
*);
2069 static cp_declarator
*cp_parser_new_declarator_opt
2071 static cp_declarator
*cp_parser_direct_new_declarator
2073 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2075 static tree cp_parser_delete_expression
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser
*, cp_expr
);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2087 static cp_expr cp_parser_expression
2088 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser
*, bool = false, bool * = NULL
);
2091 static cp_expr cp_parser_builtin_offsetof
2093 static cp_expr cp_parser_lambda_expression
2095 static void cp_parser_lambda_introducer
2096 (cp_parser
*, tree
);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser
*, tree
);
2099 static void cp_parser_lambda_body
2100 (cp_parser
*, tree
);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser
*, tree
);
2108 static tree cp_parser_expression_statement
2109 (cp_parser
*, tree
);
2110 static tree cp_parser_compound_statement
2111 (cp_parser
*, tree
, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser
*, tree
);
2114 static tree cp_parser_selection_statement
2115 (cp_parser
*, bool *, vec
<tree
> *);
2116 static tree cp_parser_condition
2118 static tree cp_parser_iteration_statement
2119 (cp_parser
*, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser
*, tree
*decl
);
2122 static tree cp_parser_for
2123 (cp_parser
*, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser
*, tree
, tree
, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser
*, tree
, tree
, tree
, bool);
2128 static void do_range_for_auto_deduction
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree
, tree
*, tree
*);
2132 static tree cp_parser_range_for_member_function
2134 static tree cp_parser_jump_statement
2136 static void cp_parser_declaration_statement
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser
*, bool *, const token_indent_info
&);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2148 static void cp_parser_declaration
2150 static void cp_parser_block_declaration
2151 (cp_parser
*, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser
*, bool, tree
*);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser
*, cp_decl_specifier_seq
*);
2160 static tree cp_parser_type_specifier
2161 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2165 static tree cp_parser_type_name
2166 (cp_parser
*, bool);
2167 static tree cp_parser_type_name
2169 static tree cp_parser_nonclass_name
2170 (cp_parser
* parser
);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser
*, bool, bool);
2173 static tree cp_parser_enum_specifier
2175 static void cp_parser_enumerator_list
2176 (cp_parser
*, tree
);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser
*, tree
);
2179 static tree cp_parser_namespace_name
2181 static void cp_parser_namespace_definition
2183 static void cp_parser_namespace_body
2185 static tree cp_parser_qualified_namespace_specifier
2187 static void cp_parser_namespace_alias_definition
2189 static bool cp_parser_using_declaration
2190 (cp_parser
*, bool);
2191 static void cp_parser_using_directive
2193 static tree cp_parser_alias_declaration
2195 static void cp_parser_asm_definition
2197 static void cp_parser_linkage_specification
2199 static void cp_parser_static_assert
2200 (cp_parser
*, bool);
2201 static tree cp_parser_decltype
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2210 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2211 static cp_declarator
*cp_parser_declarator
2212 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2213 static cp_declarator
*cp_parser_direct_declarator
2214 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2223 static tree cp_parser_tx_qualifier_opt
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2227 static tree cp_parser_declarator_id
2228 (cp_parser
*, bool);
2229 static tree cp_parser_type_id
2231 static tree cp_parser_template_type_arg
2233 static tree
cp_parser_trailing_type_id (cp_parser
*);
2234 static tree cp_parser_type_id_1
2235 (cp_parser
*, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2238 static tree cp_parser_parameter_declaration_clause
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser
*, bool *);
2242 static cp_parameter_declarator
*cp_parser_parameter_declaration
2243 (cp_parser
*, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser
*, bool);
2246 static void cp_parser_function_body
2247 (cp_parser
*, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser
*, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser
*, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser
*, bool*);
2254 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2255 (cp_parser
*, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser
*, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser
*, tree
);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser
*, tree
);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser
*, tree
);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser
*, tree
);
2271 static tree finish_fully_implicit_template
2272 (cp_parser
*, tree
);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2280 static tree cp_parser_class_head
2281 (cp_parser
*, bool *);
2282 static enum tag_types cp_parser_class_key
2284 static void cp_parser_type_parameter_key
2285 (cp_parser
* parser
);
2286 static void cp_parser_member_specification_opt
2288 static void cp_parser_member_declaration
2290 static tree cp_parser_pure_specifier
2292 static tree cp_parser_constant_initializer
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2299 static tree cp_parser_base_specifier
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2306 static tree cp_parser_conversion_type_id
2308 static cp_declarator
*cp_parser_conversion_declarator_opt
2310 static bool cp_parser_ctor_initializer_opt
2312 static void cp_parser_mem_initializer_list
2314 static tree cp_parser_mem_initializer
2316 static tree cp_parser_mem_initializer_id
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2323 static cp_expr cp_parser_operator
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser
*, bool);
2330 static tree cp_parser_template_parameter_list
2332 static tree cp_parser_template_parameter
2333 (cp_parser
*, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser
*, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser
*, bool, bool, enum tag_types
, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2340 static tree cp_parser_template_argument_list
2342 static tree cp_parser_template_argument
2344 static void cp_parser_explicit_instantiation
2346 static void cp_parser_explicit_specialization
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2353 static bool cp_parser_function_try_block
2355 static void cp_parser_handler_seq
2357 static void cp_parser_handler
2359 static tree cp_parser_exception_declaration
2361 static tree cp_parser_throw_expression
2363 static tree cp_parser_exception_specification_opt
2365 static tree cp_parser_type_id_list
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2372 static tree cp_parser_asm_operand_list
2374 static tree cp_parser_asm_clobber_list
2376 static tree cp_parser_asm_label_list
2378 static bool cp_next_tokens_can_be_attribute_p
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2382 static bool cp_next_tokens_can_be_std_attribute_p
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser
*, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser
*, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser
*, size_t);
2390 static tree cp_parser_attributes_opt
2392 static tree cp_parser_gnu_attributes_opt
2394 static tree cp_parser_gnu_attribute_list
2396 static tree cp_parser_std_attribute
2397 (cp_parser
*, tree
);
2398 static tree cp_parser_std_attribute_spec
2400 static tree cp_parser_std_attribute_spec_seq
2402 static bool cp_parser_extension_opt
2403 (cp_parser
*, int *);
2404 static void cp_parser_label_declaration
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2411 static tree cp_parser_requires_clause_opt
2413 static tree cp_parser_requires_expression
2415 static tree cp_parser_requirement_parameter_list
2417 static tree cp_parser_requirement_body
2419 static tree cp_parser_requirement_list
2421 static tree cp_parser_requirement
2423 static tree cp_parser_simple_requirement
2425 static tree cp_parser_compound_requirement
2427 static tree cp_parser_type_requirement
2429 static tree cp_parser_nested_requirement
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser
*, cp_token
*);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser
*, enum rid
);
2438 static bool cp_parser_function_transaction
2439 (cp_parser
*, enum rid
);
2440 static tree cp_parser_transaction_cancel
2443 enum pragma_context
{
2450 static bool cp_parser_pragma
2451 (cp_parser
*, enum pragma_context
, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2457 static tree cp_parser_objc_message_args
2459 static tree cp_parser_objc_message_expression
2461 static cp_expr cp_parser_objc_encode_expression
2463 static tree cp_parser_objc_defs_expression
2465 static tree cp_parser_objc_protocol_expression
2467 static tree cp_parser_objc_selector_expression
2469 static cp_expr cp_parser_objc_expression
2471 static bool cp_parser_objc_selector_p
2473 static tree cp_parser_objc_selector
2475 static tree cp_parser_objc_protocol_refs_opt
2477 static void cp_parser_objc_declaration
2478 (cp_parser
*, tree
);
2479 static tree cp_parser_objc_statement
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser
*, tree
*);
2483 static void cp_parser_objc_at_property_declaration
2485 static void cp_parser_objc_at_synthesize_declaration
2487 static void cp_parser_objc_at_dynamic_declaration
2489 static tree cp_parser_objc_struct_declaration
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser
*, tree
, location_t
);
2498 static tree cp_parser_maybe_treat_template_as_class
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser
*, cp_declarator
*, location_t
);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2504 static cp_expr cp_parser_simple_cast_expression
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser
*, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser
*, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser
*, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser
*, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec
<deferred_access_check
, va_gc
> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser
*, tree
);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2524 static tree cp_parser_save_nsdmi
2526 static tree cp_parser_enclosed_template_argument_list
2528 static void cp_parser_save_default_args
2529 (cp_parser
*, tree
);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser
*, tree
);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser
*, tree
, tree
, tree
);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser
*, tree
);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser
*, tree
);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser
*, enum rid
);
2540 static tree cp_parser_trait_expr
2541 (cp_parser
*, enum rid
);
2542 static bool cp_parser_declares_only_class_p
2544 static void cp_parser_set_storage_class
2545 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq
*decl_specs
,
2550 cp_decl_spec ds
, cp_token
*);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq
*);
2553 static void cp_parser_required_error
2554 (cp_parser
*, required_token
, bool, location_t
);
2555 static cp_token
*cp_parser_require
2556 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2557 static cp_token
*cp_parser_require_keyword
2558 (cp_parser
*, enum rid
, required_token
);
2559 static bool cp_parser_token_starts_function_definition_p
2561 static bool cp_parser_next_token_starts_class_definition_p
2563 static bool cp_parser_next_token_ends_template_argument_p
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser
*, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2571 static void cp_parser_check_class_key
2572 (enum tag_types
, tree type
);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type
, location_t location
);
2575 static bool cp_parser_optional_template_keyword
2577 static void cp_parser_pre_parsed_nested_name_specifier
2579 static bool cp_parser_cache_group
2580 (cp_parser
*, enum cpp_ttype
, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser
*parser
, bool nsdmi
);
2583 static void cp_parser_parse_tentatively
2585 static void cp_parser_commit_to_tentative_parse
2587 static void cp_parser_commit_to_topmost_tentative_parse
2589 static void cp_parser_abort_tentative_parse
2591 static bool cp_parser_parse_definitely
2593 static inline bool cp_parser_parsing_tentatively
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2597 static void cp_parser_error
2598 (cp_parser
*, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2601 static bool cp_parser_simulate_error
2603 static bool cp_parser_check_type_definition
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator
*, tree
, location_t type_location
);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser
*, non_integral_constant
);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser
*, tree
, location_t
);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser
*, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2621 static void cp_parser_skip_to_end_of_block_or_statement
2623 static bool cp_parser_skip_to_closing_brace
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser
*, cp_token
*);
2629 static bool cp_parser_error_occurred
2631 static bool cp_parser_allow_gnu_extensions_p
2633 static bool cp_parser_is_pure_string_literal
2635 static bool cp_parser_is_string_literal
2637 static bool cp_parser_is_keyword
2638 (cp_token
*, enum rid
);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser
*, tree
, location_t location
);
2641 static cp_declarator
* cp_parser_make_indirect_declarator
2642 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2643 static bool cp_parser_compound_literal_p
2645 static bool cp_parser_array_designator_p
2647 static bool cp_parser_init_statement_p
2649 static bool cp_parser_skip_to_closing_square_bracket
2652 /* Concept-related syntactic transformations */
2654 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2655 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand
;
2664 ++c_inhibit_evaluation_warnings
;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings
;
2670 --cp_unevaluated_operand
;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2679 cp_parser_parsing_tentatively (cp_parser
* parser
)
2681 return parser
->context
->next
!= NULL
;
2684 /* Returns nonzero if TOKEN is a string literal. */
2687 cp_parser_is_pure_string_literal (cp_token
* token
)
2689 return (token
->type
== CPP_STRING
||
2690 token
->type
== CPP_STRING16
||
2691 token
->type
== CPP_STRING32
||
2692 token
->type
== CPP_WSTRING
||
2693 token
->type
== CPP_UTF8STRING
);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2700 cp_parser_is_string_literal (cp_token
* token
)
2702 return (cp_parser_is_pure_string_literal (token
) ||
2703 token
->type
== CPP_STRING_USERDEF
||
2704 token
->type
== CPP_STRING16_USERDEF
||
2705 token
->type
== CPP_STRING32_USERDEF
||
2706 token
->type
== CPP_WSTRING_USERDEF
||
2707 token
->type
== CPP_UTF8STRING_USERDEF
);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2713 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2715 return token
->keyword
== keyword
;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token
*token
)
2724 if (token
->type
!= CPP_PRAGMA
)
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2742 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2743 location_t
*out_loc
)
2745 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2746 if (token2
->type
!= tok1_kind
)
2748 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2749 if (token3
->type
!= tok1_kind
)
2751 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2752 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2755 /* It must be at the start of the line. */
2756 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2757 if (LOCATION_COLUMN (start_loc
) != 1)
2760 /* We have a conflict marker. Construct a location of the form:
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc
= get_finish (token4
->location
);
2765 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2777 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2779 if (!cp_parser_simulate_error (parser
))
2781 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token
);
2786 if (token
->type
== CPP_PRAGMA
)
2788 error_at (token
->location
,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser
, token
);
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token
->type
== CPP_LSHIFT
2796 || token
->type
== CPP_RSHIFT
2797 || token
->type
== CPP_EQ_EQ
)
2800 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2802 error_at (loc
, "version control conflict marker in file");
2807 rich_location
richloc (line_table
, input_location
);
2808 c_parse_error (gmsgid
,
2809 /* Because c_parser_error does not understand
2810 CPP_KEYWORD, keywords are treated like
2812 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2813 token
->u
.value
, token
->flags
, &richloc
);
2817 /* Issue an error about name-lookup failing. NAME is the
2818 IDENTIFIER_NODE DECL is the result of
2819 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2820 the thing that we hoped to find. */
2823 cp_parser_name_lookup_error (cp_parser
* parser
,
2826 name_lookup_error desired
,
2827 location_t location
)
2829 /* If name lookup completely failed, tell the user that NAME was not
2831 if (decl
== error_mark_node
)
2833 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2834 error_at (location
, "%<%E::%E%> has not been declared",
2835 parser
->scope
, name
);
2836 else if (parser
->scope
== global_namespace
)
2837 error_at (location
, "%<::%E%> has not been declared", name
);
2838 else if (parser
->object_scope
2839 && !CLASS_TYPE_P (parser
->object_scope
))
2840 error_at (location
, "request for member %qE in non-class type %qT",
2841 name
, parser
->object_scope
);
2842 else if (parser
->object_scope
)
2843 error_at (location
, "%<%T::%E%> has not been declared",
2844 parser
->object_scope
, name
);
2846 error_at (location
, "%qE has not been declared", name
);
2848 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2853 error_at (location
, "%<%E::%E%> is not a type",
2854 parser
->scope
, name
);
2857 error_at (location
, "%<%E::%E%> is not a class or namespace",
2858 parser
->scope
, name
);
2862 "%<%E::%E%> is not a class, namespace, or enumeration",
2863 parser
->scope
, name
);
2870 else if (parser
->scope
== global_namespace
)
2875 error_at (location
, "%<::%E%> is not a type", name
);
2878 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2882 "%<::%E%> is not a class, namespace, or enumeration",
2894 error_at (location
, "%qE is not a type", name
);
2897 error_at (location
, "%qE is not a class or namespace", name
);
2901 "%qE is not a class, namespace, or enumeration", name
);
2909 /* If we are parsing tentatively, remember that an error has occurred
2910 during this tentative parse. Returns true if the error was
2911 simulated; false if a message should be issued by the caller. */
2914 cp_parser_simulate_error (cp_parser
* parser
)
2916 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2918 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2924 /* This function is called when a type is defined. If type
2925 definitions are forbidden at this point, an error message is
2929 cp_parser_check_type_definition (cp_parser
* parser
)
2931 /* If types are forbidden here, issue a message. */
2932 if (parser
->type_definition_forbidden_message
)
2934 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2935 in the message need to be interpreted. */
2936 error (parser
->type_definition_forbidden_message
);
2942 /* This function is called when the DECLARATOR is processed. The TYPE
2943 was a type defined in the decl-specifiers. If it is invalid to
2944 define a type in the decl-specifiers for DECLARATOR, an error is
2945 issued. TYPE_LOCATION is the location of TYPE and is used
2946 for error reporting. */
2949 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2950 tree type
, location_t type_location
)
2952 /* [dcl.fct] forbids type definitions in return types.
2953 Unfortunately, it's not easy to know whether or not we are
2954 processing a return type until after the fact. */
2956 && (declarator
->kind
== cdk_pointer
2957 || declarator
->kind
== cdk_reference
2958 || declarator
->kind
== cdk_ptrmem
))
2959 declarator
= declarator
->declarator
;
2961 && declarator
->kind
== cdk_function
)
2963 error_at (type_location
,
2964 "new types may not be defined in a return type");
2965 inform (type_location
,
2966 "(perhaps a semicolon is missing after the definition of %qT)",
2971 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2972 "<" in any valid C++ program. If the next token is indeed "<",
2973 issue a message warning the user about what appears to be an
2974 invalid attempt to form a template-id. LOCATION is the location
2975 of the type-specifier (TYPE) */
2978 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2980 enum tag_types tag_type
,
2981 location_t location
)
2983 cp_token_position start
= 0;
2985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2987 if (TREE_CODE (type
) == TYPE_DECL
)
2988 type
= TREE_TYPE (type
);
2989 if (TYPE_P (type
) && !template_placeholder_p (type
))
2990 error_at (location
, "%qT is not a template", type
);
2991 else if (identifier_p (type
))
2993 if (tag_type
!= none_type
)
2994 error_at (location
, "%qE is not a class template", type
);
2996 error_at (location
, "%qE is not a template", type
);
2999 error_at (location
, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3002 start
= cp_lexer_token_position (parser
->lexer
, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser
->lexer
);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser
);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3010 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3020 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3021 non_integral_constant thing
)
3023 parser
->non_integral_constant_expression_p
= true;
3024 if (parser
->integral_constant_expression_p
)
3026 if (!parser
->allow_non_integral_constant_expression_p
)
3028 const char *msg
= NULL
;
3032 pedwarn (input_location
, OPT_Wpedantic
,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3065 case NIC_ADDR_LABEL
:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3069 case NIC_OVERLOADED
:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3073 case NIC_ASSIGNMENT
:
3074 error ("an assignment cannot appear in a constant-expression");
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3080 case NIC_CONSTRUCTOR
:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3084 case NIC_TRANSACTION
:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3092 msg
= "__FUNCTION__";
3094 case NIC_PRETTY_FUNC
:
3095 msg
= "__PRETTY_FUNCTION__";
3115 case NIC_PREINCREMENT
:
3118 case NIC_PREDECREMENT
:
3131 error ("%qs cannot appear in a constant-expression", msg
);
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3144 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3145 location_t location
)
3147 tree decl
, ambiguous_decls
;
3148 cp_parser_commit_to_tentative_parse (parser
);
3149 /* Try to lookup the identifier. */
3150 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls
, location
);
3155 if (ambiguous_decls
)
3156 /* If the lookup was ambiguous, an error will already have
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl
))
3164 "invalid use of template-name %qE without an argument list",
3166 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3167 inform (location
, "class template argument deduction is only available "
3168 "with -std=c++17 or -std=gnu++17");
3169 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3171 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3172 error_at (location
, "invalid use of destructor %qD as a type", id
);
3173 else if (TREE_CODE (decl
) == TYPE_DECL
)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location
, "invalid combination of multiple type-specifiers");
3176 else if (!parser
->scope
)
3178 /* Issue an error message. */
3179 const char *suggestion
= NULL
;
3180 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3181 suggestion
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
);
3184 gcc_rich_location
richloc (location
);
3185 richloc
.add_fixit_replace (suggestion
);
3186 error_at_rich_loc (&richloc
,
3187 "%qE does not name a type; did you mean %qs?",
3191 error_at (location
, "%qE does not name a type", id
);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3200 inform (location
, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3203 inform (location
, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect
< cxx11
3206 && TREE_CODE (id
) == IDENTIFIER_NODE
3207 && id_equal (id
, "thread_local"))
3208 inform (location
, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3211 inform (location
, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl
&& current_class_type
3213 && TYPE_BINFO (current_class_type
))
3217 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3221 tree base_type
= BINFO_TYPE (b
);
3222 if (CLASS_TYPE_P (base_type
)
3223 && dependent_type_p (base_type
))
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3230 for (field
= TYPE_FIELDS (base_type
);
3232 field
= DECL_CHAIN (field
))
3233 if (TREE_CODE (field
) == TYPE_DECL
3234 && DECL_NAME (field
) == id
)
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b
), id
);
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser
->scope
!= error_mark_node
)
3251 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3254 error_at (location_of (id
),
3255 "%qE in namespace %qE does not name a template type",
3257 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3258 error_at (location_of (id
),
3259 "%qE in namespace %qE does not name a template type",
3260 TREE_OPERAND (id
, 0), parser
->scope
);
3262 error_at (location_of (id
),
3263 "%qE in namespace %qE does not name a type",
3266 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3268 else if (CLASS_TYPE_P (parser
->scope
)
3269 && constructor_name_p (id
, parser
->scope
))
3272 error_at (location
, "%<%T::%E%> names the constructor, not"
3273 " the type", parser
->scope
, id
);
3274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3275 error_at (location
, "and %qT has no template constructors",
3278 else if (TYPE_P (parser
->scope
)
3279 && dependent_scope_p (parser
->scope
))
3281 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3283 "need %<typename%> before %<%T::%D::%E%> because "
3284 "%<%T::%D%> is a dependent scope",
3285 TYPE_CONTEXT (parser
->scope
),
3286 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3288 TYPE_CONTEXT (parser
->scope
),
3289 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3291 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3292 "%qT is a dependent scope",
3293 parser
->scope
, id
, parser
->scope
);
3295 else if (TYPE_P (parser
->scope
))
3297 if (!COMPLETE_TYPE_P (parser
->scope
))
3298 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3300 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3301 error_at (location_of (id
),
3302 "%qE in %q#T does not name a template type",
3304 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3305 error_at (location_of (id
),
3306 "%qE in %q#T does not name a template type",
3307 TREE_OPERAND (id
, 0), parser
->scope
);
3309 error_at (location_of (id
),
3310 "%qE in %q#T does not name a type",
3313 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3320 /* Check for a common situation where a type-name should be present,
3321 but is not, and issue a sensible error message. Returns true if an
3322 invalid type-name was detected.
3324 The situation handled by this function are variable declarations of the
3325 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3326 Usually, `ID' should name a type, but if we got here it means that it
3327 does not. We try to emit the best possible error message depending on
3328 how exactly the id-expression looks like. */
3331 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3334 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3336 /* Avoid duplicate error about ambiguous lookup. */
3337 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3339 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3340 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3344 cp_parser_parse_tentatively (parser
);
3345 id
= cp_parser_id_expression (parser
,
3346 /*template_keyword_p=*/false,
3347 /*check_dependency_p=*/true,
3348 /*template_p=*/NULL
,
3349 /*declarator_p=*/true,
3350 /*optional_p=*/false);
3351 /* If the next token is a (, this is a function with no explicit return
3352 type, i.e. constructor, destructor or conversion op. */
3353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3354 || TREE_CODE (id
) == TYPE_DECL
)
3356 cp_parser_abort_tentative_parse (parser
);
3359 if (!cp_parser_parse_definitely (parser
))
3362 /* Emit a diagnostic for the invalid type. */
3363 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3365 /* If we aren't in the middle of a declarator (i.e. in a
3366 parameter-declaration-clause), skip to the end of the declaration;
3367 there's no point in trying to process it. */
3368 if (!parser
->in_declarator_p
)
3369 cp_parser_skip_to_end_of_block_or_statement (parser
);
3373 /* Consume tokens up to, and including, the next non-nested closing `)'.
3374 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3375 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3376 found an unnested token of that type. */
3379 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3384 unsigned paren_depth
= 0;
3385 unsigned brace_depth
= 0;
3386 unsigned square_depth
= 0;
3388 if (recovering
&& or_ttype
== CPP_EOF
3389 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3394 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3396 /* Have we found what we're looking for before the closing paren? */
3397 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3398 && !brace_depth
&& !paren_depth
&& !square_depth
)
3401 switch (token
->type
)
3404 case CPP_PRAGMA_EOL
:
3405 /* If we've run out of tokens, then there is no closing `)'. */
3408 /* This is good for lambda expression capture-lists. */
3409 case CPP_OPEN_SQUARE
:
3412 case CPP_CLOSE_SQUARE
:
3413 if (!square_depth
--)
3418 /* This matches the processing in skip_to_end_of_statement. */
3423 case CPP_OPEN_BRACE
:
3426 case CPP_CLOSE_BRACE
:
3431 case CPP_OPEN_PAREN
:
3436 case CPP_CLOSE_PAREN
:
3437 if (!brace_depth
&& !paren_depth
--)
3440 cp_lexer_consume_token (parser
->lexer
);
3449 /* Consume the token. */
3450 cp_lexer_consume_token (parser
->lexer
);
3454 /* Consume tokens up to, and including, the next non-nested closing `)'.
3455 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3456 are doing error recovery. Returns -1 if OR_COMMA is true and we
3457 found an unnested token of that type. */
3460 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3465 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3466 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3467 ttype
, consume_paren
);
3470 /* Consume tokens until we reach the end of the current statement.
3471 Normally, that will be just before consuming a `;'. However, if a
3472 non-nested `}' comes first, then we stop before consuming that. */
3475 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3477 unsigned nesting_depth
= 0;
3479 /* Unwind generic function template scope if necessary. */
3480 if (parser
->fully_implicit_function_template_p
)
3481 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3485 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3487 switch (token
->type
)
3490 case CPP_PRAGMA_EOL
:
3491 /* If we've run out of tokens, stop. */
3495 /* If the next token is a `;', we have reached the end of the
3501 case CPP_CLOSE_BRACE
:
3502 /* If this is a non-nested '}', stop before consuming it.
3503 That way, when confronted with something like:
3507 we stop before consuming the closing '}', even though we
3508 have not yet reached a `;'. */
3509 if (nesting_depth
== 0)
3512 /* If it is the closing '}' for a block that we have
3513 scanned, stop -- but only after consuming the token.
3519 we will stop after the body of the erroneously declared
3520 function, but before consuming the following `typedef'
3522 if (--nesting_depth
== 0)
3524 cp_lexer_consume_token (parser
->lexer
);
3529 case CPP_OPEN_BRACE
:
3537 /* Consume the token. */
3538 cp_lexer_consume_token (parser
->lexer
);
3542 /* This function is called at the end of a statement or declaration.
3543 If the next token is a semicolon, it is consumed; otherwise, error
3544 recovery is attempted. */
3547 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3549 /* Look for the trailing `;'. */
3550 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3552 /* If there is additional (erroneous) input, skip to the end of
3554 cp_parser_skip_to_end_of_statement (parser
);
3555 /* If the next token is now a `;', consume it. */
3556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3557 cp_lexer_consume_token (parser
->lexer
);
3561 /* Skip tokens until we have consumed an entire block, or until we
3562 have consumed a non-nested `;'. */
3565 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3567 int nesting_depth
= 0;
3569 /* Unwind generic function template scope if necessary. */
3570 if (parser
->fully_implicit_function_template_p
)
3571 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3573 while (nesting_depth
>= 0)
3575 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3577 switch (token
->type
)
3580 case CPP_PRAGMA_EOL
:
3581 /* If we've run out of tokens, stop. */
3585 /* Stop if this is an unnested ';'. */
3590 case CPP_CLOSE_BRACE
:
3591 /* Stop if this is an unnested '}', or closes the outermost
3594 if (nesting_depth
< 0)
3600 case CPP_OPEN_BRACE
:
3609 /* Consume the token. */
3610 cp_lexer_consume_token (parser
->lexer
);
3614 /* Skip tokens until a non-nested closing curly brace is the next
3615 token, or there are no more tokens. Return true in the first case,
3619 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3621 unsigned nesting_depth
= 0;
3625 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3627 switch (token
->type
)
3630 case CPP_PRAGMA_EOL
:
3631 /* If we've run out of tokens, stop. */
3634 case CPP_CLOSE_BRACE
:
3635 /* If the next token is a non-nested `}', then we have reached
3636 the end of the current block. */
3637 if (nesting_depth
-- == 0)
3641 case CPP_OPEN_BRACE
:
3642 /* If it the next token is a `{', then we are entering a new
3643 block. Consume the entire block. */
3651 /* Consume the token. */
3652 cp_lexer_consume_token (parser
->lexer
);
3656 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3657 parameter is the PRAGMA token, allowing us to purge the entire pragma
3661 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3665 parser
->lexer
->in_pragma
= false;
3668 token
= cp_lexer_consume_token (parser
->lexer
);
3669 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3671 /* Ensure that the pragma is not parsed again. */
3672 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3675 /* Require pragma end of line, resyncing with it as necessary. The
3676 arguments are as for cp_parser_skip_to_pragma_eol. */
3679 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3681 parser
->lexer
->in_pragma
= false;
3682 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3683 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3686 /* This is a simple wrapper around make_typename_type. When the id is
3687 an unresolved identifier node, we can provide a superior diagnostic
3688 using cp_parser_diagnose_invalid_type_name. */
3691 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3692 location_t id_location
)
3695 if (identifier_p (id
))
3697 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3698 /*complain=*/tf_none
);
3699 if (result
== error_mark_node
)
3700 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3703 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3706 /* This is a wrapper around the
3707 make_{pointer,ptrmem,reference}_declarator functions that decides
3708 which one to call based on the CODE and CLASS_TYPE arguments. The
3709 CODE argument should be one of the values returned by
3710 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3711 appertain to the pointer or reference. */
3713 static cp_declarator
*
3714 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3715 cp_cv_quals cv_qualifiers
,
3716 cp_declarator
*target
,
3719 if (code
== ERROR_MARK
)
3720 return cp_error_declarator
;
3722 if (code
== INDIRECT_REF
)
3723 if (class_type
== NULL_TREE
)
3724 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3726 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3727 target
, attributes
);
3728 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3729 return make_reference_declarator (cv_qualifiers
, target
,
3731 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3732 return make_reference_declarator (cv_qualifiers
, target
,
3737 /* Create a new C++ parser. */
3740 cp_parser_new (void)
3746 /* cp_lexer_new_main is called before doing GC allocation because
3747 cp_lexer_new_main might load a PCH file. */
3748 lexer
= cp_lexer_new_main ();
3750 /* Initialize the binops_by_token so that we can get the tree
3751 directly from the token. */
3752 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3753 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3755 parser
= ggc_cleared_alloc
<cp_parser
> ();
3756 parser
->lexer
= lexer
;
3757 parser
->context
= cp_parser_context_new (NULL
);
3759 /* For now, we always accept GNU extensions. */
3760 parser
->allow_gnu_extensions_p
= 1;
3762 /* The `>' token is a greater-than operator, not the end of a
3764 parser
->greater_than_is_operator_p
= true;
3766 parser
->default_arg_ok_p
= true;
3768 /* We are not parsing a constant-expression. */
3769 parser
->integral_constant_expression_p
= false;
3770 parser
->allow_non_integral_constant_expression_p
= false;
3771 parser
->non_integral_constant_expression_p
= false;
3773 /* Local variable names are not forbidden. */
3774 parser
->local_variables_forbidden_p
= false;
3776 /* We are not processing an `extern "C"' declaration. */
3777 parser
->in_unbraced_linkage_specification_p
= false;
3779 /* We are not processing a declarator. */
3780 parser
->in_declarator_p
= false;
3782 /* We are not processing a template-argument-list. */
3783 parser
->in_template_argument_list_p
= false;
3785 /* We are not in an iteration statement. */
3786 parser
->in_statement
= 0;
3788 /* We are not in a switch statement. */
3789 parser
->in_switch_statement_p
= false;
3791 /* We are not parsing a type-id inside an expression. */
3792 parser
->in_type_id_in_expr_p
= false;
3794 /* Declarations aren't implicitly extern "C". */
3795 parser
->implicit_extern_c
= false;
3797 /* String literals should be translated to the execution character set. */
3798 parser
->translate_strings_p
= true;
3800 /* We are not parsing a function body. */
3801 parser
->in_function_body
= false;
3803 /* We can correct until told otherwise. */
3804 parser
->colon_corrects_to_scope_p
= true;
3806 /* The unparsed function queue is empty. */
3807 push_unparsed_function_queues (parser
);
3809 /* There are no classes being defined. */
3810 parser
->num_classes_being_defined
= 0;
3812 /* No template parameters apply. */
3813 parser
->num_template_parameter_lists
= 0;
3815 /* Special parsing data structures. */
3816 parser
->omp_declare_simd
= NULL
;
3817 parser
->cilk_simd_fn_info
= NULL
;
3818 parser
->oacc_routine
= NULL
;
3820 /* Not declaring an implicit function template. */
3821 parser
->auto_is_implicit_function_template_parm_p
= false;
3822 parser
->fully_implicit_function_template_p
= false;
3823 parser
->implicit_template_parms
= 0;
3824 parser
->implicit_template_scope
= 0;
3826 /* Allow constrained-type-specifiers. */
3827 parser
->prevent_constrained_type_specifiers
= 0;
3832 /* Create a cp_lexer structure which will emit the tokens in CACHE
3833 and push it onto the parser's lexer stack. This is used for delayed
3834 parsing of in-class method bodies and default arguments, and should
3835 not be confused with tentative parsing. */
3837 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3839 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3840 lexer
->next
= parser
->lexer
;
3841 parser
->lexer
= lexer
;
3843 /* Move the current source position to that of the first token in the
3845 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3848 /* Pop the top lexer off the parser stack. This is never used for the
3849 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3851 cp_parser_pop_lexer (cp_parser
*parser
)
3853 cp_lexer
*lexer
= parser
->lexer
;
3854 parser
->lexer
= lexer
->next
;
3855 cp_lexer_destroy (lexer
);
3857 /* Put the current source position back where it was before this
3858 lexer was pushed. */
3859 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3862 /* Lexical conventions [gram.lex] */
3864 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3868 cp_parser_identifier (cp_parser
* parser
)
3872 /* Look for the identifier. */
3873 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3874 /* Return the value. */
3876 return cp_expr (token
->u
.value
, token
->location
);
3878 return error_mark_node
;
3881 /* Parse a sequence of adjacent string constants. Returns a
3882 TREE_STRING representing the combined, nul-terminated string
3883 constant. If TRANSLATE is true, translate the string to the
3884 execution character set. If WIDE_OK is true, a wide string is
3887 C++98 [lex.string] says that if a narrow string literal token is
3888 adjacent to a wide string literal token, the behavior is undefined.
3889 However, C99 6.4.5p4 says that this results in a wide string literal.
3890 We follow C99 here, for consistency with the C front end.
3892 This code is largely lifted from lex_string() in c-lex.c.
3894 FUTURE: ObjC++ will need to handle @-strings here. */
3896 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3897 bool lookup_udlit
= true)
3901 struct obstack str_ob
;
3902 cpp_string str
, istr
, *strs
;
3904 enum cpp_ttype type
, curr_type
;
3905 int have_suffix_p
= 0;
3907 tree suffix_id
= NULL_TREE
;
3908 bool curr_tok_is_userdef_p
= false;
3910 tok
= cp_lexer_peek_token (parser
->lexer
);
3911 if (!cp_parser_is_string_literal (tok
))
3913 cp_parser_error (parser
, "expected string-literal");
3914 return error_mark_node
;
3917 location_t loc
= tok
->location
;
3919 if (cpp_userdef_string_p (tok
->type
))
3921 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3922 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3923 curr_tok_is_userdef_p
= true;
3927 string_tree
= tok
->u
.value
;
3928 curr_type
= tok
->type
;
3932 /* Try to avoid the overhead of creating and destroying an obstack
3933 for the common case of just one string. */
3934 if (!cp_parser_is_string_literal
3935 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3937 cp_lexer_consume_token (parser
->lexer
);
3939 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3940 str
.len
= TREE_STRING_LENGTH (string_tree
);
3943 if (curr_tok_is_userdef_p
)
3945 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3947 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3950 curr_type
= tok
->type
;
3956 location_t last_tok_loc
= tok
->location
;
3957 gcc_obstack_init (&str_ob
);
3962 cp_lexer_consume_token (parser
->lexer
);
3964 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3965 str
.len
= TREE_STRING_LENGTH (string_tree
);
3967 if (curr_tok_is_userdef_p
)
3969 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3970 if (have_suffix_p
== 0)
3972 suffix_id
= curr_suffix_id
;
3975 else if (have_suffix_p
== 1
3976 && curr_suffix_id
!= suffix_id
)
3978 error ("inconsistent user-defined literal suffixes"
3979 " %qD and %qD in string literal",
3980 suffix_id
, curr_suffix_id
);
3983 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3986 curr_type
= tok
->type
;
3988 if (type
!= curr_type
)
3990 if (type
== CPP_STRING
)
3992 else if (curr_type
!= CPP_STRING
)
3994 rich_location
rich_loc (line_table
, tok
->location
);
3995 rich_loc
.add_range (last_tok_loc
, false);
3996 error_at_rich_loc (&rich_loc
,
3997 "unsupported non-standard concatenation "
3998 "of string literals");
4002 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4004 last_tok_loc
= tok
->location
;
4006 tok
= cp_lexer_peek_token (parser
->lexer
);
4007 if (cpp_userdef_string_p (tok
->type
))
4009 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4010 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4011 curr_tok_is_userdef_p
= true;
4015 string_tree
= tok
->u
.value
;
4016 curr_type
= tok
->type
;
4017 curr_tok_is_userdef_p
= false;
4020 while (cp_parser_is_string_literal (tok
));
4022 /* A string literal built by concatenation has its caret=start at
4023 the start of the initial string, and its finish at the finish of
4024 the final string literal. */
4025 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4027 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4030 if (type
!= CPP_STRING
&& !wide_ok
)
4032 cp_parser_error (parser
, "a wide string is invalid in this context");
4036 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4037 (parse_in
, strs
, count
, &istr
, type
))
4039 value
= build_string (istr
.len
, (const char *)istr
.text
);
4040 free (CONST_CAST (unsigned char *, istr
.text
));
4046 case CPP_UTF8STRING
:
4047 TREE_TYPE (value
) = char_array_type_node
;
4050 TREE_TYPE (value
) = char16_array_type_node
;
4053 TREE_TYPE (value
) = char32_array_type_node
;
4056 TREE_TYPE (value
) = wchar_array_type_node
;
4060 value
= fix_string_type (value
);
4064 tree literal
= build_userdef_literal (suffix_id
, value
,
4065 OT_NONE
, NULL_TREE
);
4067 value
= cp_parser_userdef_string_literal (literal
);
4073 /* cpp_interpret_string has issued an error. */
4074 value
= error_mark_node
;
4077 obstack_free (&str_ob
, 0);
4079 return cp_expr (value
, loc
);
4082 /* Look up a literal operator with the name and the exact arguments. */
4085 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4088 decl
= lookup_name (name
);
4089 if (!decl
|| !is_overloaded_fn (decl
))
4090 return error_mark_node
;
4092 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4097 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4098 if (parmtypes
!= NULL_TREE
)
4100 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4101 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4103 tree tparm
= TREE_VALUE (parmtypes
);
4104 tree targ
= TREE_TYPE ((*args
)[ix
]);
4105 bool ptr
= TYPE_PTR_P (tparm
);
4106 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4107 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4109 || !same_type_p (TREE_TYPE (tparm
),
4114 && ix
== vec_safe_length (args
)
4115 /* May be this should be sufficient_parms_p instead,
4116 depending on how exactly should user-defined literals
4117 work in presence of default arguments on the literal
4118 operator parameters. */
4119 && parmtypes
== void_list_node
)
4124 return error_mark_node
;
4127 /* Parse a user-defined char constant. Returns a call to a user-defined
4128 literal operator taking the character as an argument. */
4131 cp_parser_userdef_char_literal (cp_parser
*parser
)
4133 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4134 tree literal
= token
->u
.value
;
4135 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4136 tree value
= USERDEF_LITERAL_VALUE (literal
);
4137 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4140 /* Build up a call to the user-defined operator */
4141 /* Lookup the name we got back from the id-expression. */
4142 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4143 vec_safe_push (args
, value
);
4144 decl
= lookup_literal_operator (name
, args
);
4145 if (!decl
|| decl
== error_mark_node
)
4147 error ("unable to find character literal operator %qD with %qT argument",
4148 name
, TREE_TYPE (value
));
4149 release_tree_vector (args
);
4150 return error_mark_node
;
4152 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4153 release_tree_vector (args
);
4157 /* A subroutine of cp_parser_userdef_numeric_literal to
4158 create a char... template parameter pack from a string node. */
4161 make_char_string_pack (tree value
)
4164 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4165 const char *str
= TREE_STRING_POINTER (value
);
4166 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4167 tree argvec
= make_tree_vec (1);
4169 /* Fill in CHARVEC with all of the parameters. */
4170 charvec
= make_tree_vec (len
);
4171 for (i
= 0; i
< len
; ++i
)
4172 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4174 /* Build the argument packs. */
4175 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4177 TREE_VEC_ELT (argvec
, 0) = argpack
;
4182 /* A subroutine of cp_parser_userdef_numeric_literal to
4183 create a char... template parameter pack from a string node. */
4186 make_string_pack (tree value
)
4189 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4190 const unsigned char *str
4191 = (const unsigned char *) TREE_STRING_POINTER (value
);
4192 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4193 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4194 tree argvec
= make_tree_vec (2);
4196 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4197 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4199 /* First template parm is character type. */
4200 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4202 /* Fill in CHARVEC with all of the parameters. */
4203 charvec
= make_tree_vec (len
);
4204 for (int i
= 0; i
< len
; ++i
)
4205 TREE_VEC_ELT (charvec
, i
)
4206 = double_int_to_tree (str_char_type_node
,
4207 double_int::from_buffer (str
+ i
* sz
, sz
));
4209 /* Build the argument packs. */
4210 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4212 TREE_VEC_ELT (argvec
, 1) = argpack
;
4217 /* Parse a user-defined numeric constant. returns a call to a user-defined
4218 literal operator. */
4221 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4223 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4224 tree literal
= token
->u
.value
;
4225 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4226 tree value
= USERDEF_LITERAL_VALUE (literal
);
4227 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4228 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4229 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4231 vec
<tree
, va_gc
> *args
;
4233 /* Look for a literal operator taking the exact type of numeric argument
4234 as the literal value. */
4235 args
= make_tree_vector ();
4236 vec_safe_push (args
, value
);
4237 decl
= lookup_literal_operator (name
, args
);
4238 if (decl
&& decl
!= error_mark_node
)
4240 result
= finish_call_expr (decl
, &args
, false, true,
4241 tf_warning_or_error
);
4243 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4245 warning_at (token
->location
, OPT_Woverflow
,
4246 "integer literal exceeds range of %qT type",
4247 long_long_unsigned_type_node
);
4252 warning_at (token
->location
, OPT_Woverflow
,
4253 "floating literal exceeds range of %qT type",
4254 long_double_type_node
);
4255 else if (overflow
< 0)
4256 warning_at (token
->location
, OPT_Woverflow
,
4257 "floating literal truncated to zero");
4260 release_tree_vector (args
);
4263 release_tree_vector (args
);
4265 /* If the numeric argument didn't work, look for a raw literal
4266 operator taking a const char* argument consisting of the number
4267 in string format. */
4268 args
= make_tree_vector ();
4269 vec_safe_push (args
, num_string
);
4270 decl
= lookup_literal_operator (name
, args
);
4271 if (decl
&& decl
!= error_mark_node
)
4273 result
= finish_call_expr (decl
, &args
, false, true,
4274 tf_warning_or_error
);
4275 release_tree_vector (args
);
4278 release_tree_vector (args
);
4280 /* If the raw literal didn't work, look for a non-type template
4281 function with parameter pack char.... Call the function with
4282 template parameter characters representing the number. */
4283 args
= make_tree_vector ();
4284 decl
= lookup_literal_operator (name
, args
);
4285 if (decl
&& decl
!= error_mark_node
)
4287 tree tmpl_args
= make_char_string_pack (num_string
);
4288 decl
= lookup_template_function (decl
, tmpl_args
);
4289 result
= finish_call_expr (decl
, &args
, false, true,
4290 tf_warning_or_error
);
4291 release_tree_vector (args
);
4295 release_tree_vector (args
);
4297 error ("unable to find numeric literal operator %qD", name
);
4298 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4299 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4300 "to enable more built-in suffixes");
4301 return error_mark_node
;
4304 /* Parse a user-defined string constant. Returns a call to a user-defined
4305 literal operator taking a character pointer and the length of the string
4309 cp_parser_userdef_string_literal (tree literal
)
4311 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4312 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4313 tree value
= USERDEF_LITERAL_VALUE (literal
);
4314 int len
= TREE_STRING_LENGTH (value
)
4315 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4317 vec
<tree
, va_gc
> *args
;
4319 /* Build up a call to the user-defined operator. */
4320 /* Lookup the name we got back from the id-expression. */
4321 args
= make_tree_vector ();
4322 vec_safe_push (args
, value
);
4323 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4324 decl
= lookup_literal_operator (name
, args
);
4326 if (decl
&& decl
!= error_mark_node
)
4328 result
= finish_call_expr (decl
, &args
, false, true,
4329 tf_warning_or_error
);
4330 release_tree_vector (args
);
4333 release_tree_vector (args
);
4335 /* Look for a template function with typename parameter CharT
4336 and parameter pack CharT... Call the function with
4337 template parameter characters representing the string. */
4338 args
= make_tree_vector ();
4339 decl
= lookup_literal_operator (name
, args
);
4340 if (decl
&& decl
!= error_mark_node
)
4342 tree tmpl_args
= make_string_pack (value
);
4343 decl
= lookup_template_function (decl
, tmpl_args
);
4344 result
= finish_call_expr (decl
, &args
, false, true,
4345 tf_warning_or_error
);
4346 release_tree_vector (args
);
4349 release_tree_vector (args
);
4351 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4352 name
, TREE_TYPE (value
), size_type_node
);
4353 return error_mark_node
;
4357 /* Basic concepts [gram.basic] */
4359 /* Parse a translation-unit.
4362 declaration-seq [opt]
4364 Returns TRUE if all went well. */
4367 cp_parser_translation_unit (cp_parser
* parser
)
4369 /* The address of the first non-permanent object on the declarator
4371 static void *declarator_obstack_base
;
4375 /* Create the declarator obstack, if necessary. */
4376 if (!cp_error_declarator
)
4378 gcc_obstack_init (&declarator_obstack
);
4379 /* Create the error declarator. */
4380 cp_error_declarator
= make_declarator (cdk_error
);
4381 /* Create the empty parameter list. */
4382 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4383 /* Remember where the base of the declarator obstack lies. */
4384 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4387 cp_parser_declaration_seq_opt (parser
);
4389 /* If there are no tokens left then all went well. */
4390 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4392 /* Get rid of the token array; we don't need it any more. */
4393 cp_lexer_destroy (parser
->lexer
);
4394 parser
->lexer
= NULL
;
4396 /* This file might have been a context that's implicitly extern
4397 "C". If so, pop the lang context. (Only relevant for PCH.) */
4398 if (parser
->implicit_extern_c
)
4400 pop_lang_context ();
4401 parser
->implicit_extern_c
= false;
4405 finish_translation_unit ();
4411 cp_parser_error (parser
, "expected declaration");
4415 /* Make sure the declarator obstack was fully cleaned up. */
4416 gcc_assert (obstack_next_free (&declarator_obstack
)
4417 == declarator_obstack_base
);
4419 /* All went well. */
4423 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4424 decltype context. */
4426 static inline tsubst_flags_t
4427 complain_flags (bool decltype_p
)
4429 tsubst_flags_t complain
= tf_warning_or_error
;
4431 complain
|= tf_decltype
;
4435 /* We're about to parse a collection of statements. If we're currently
4436 parsing tentatively, set up a firewall so that any nested
4437 cp_parser_commit_to_tentative_parse won't affect the current context. */
4439 static cp_token_position
4440 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4445 cp_parser_parse_tentatively (parser
);
4446 cp_parser_commit_to_topmost_tentative_parse (parser
);
4447 return cp_lexer_token_position (parser
->lexer
, false);
4450 /* We've finished parsing the collection of statements. Wrap up the
4451 firewall and replace the relevant tokens with the parsed form. */
4454 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4460 /* Finish the firewall level. */
4461 cp_parser_parse_definitely (parser
);
4462 /* And remember the result of the parse for when we try again. */
4463 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4464 token
->type
= CPP_PREPARSED_EXPR
;
4465 token
->u
.value
= expr
;
4466 token
->keyword
= RID_MAX
;
4467 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4470 /* Like the above functions, but let the user modify the tokens. Used by
4471 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4472 later parses, so it makes sense to localize the effects of
4473 cp_parser_commit_to_tentative_parse. */
4475 struct tentative_firewall
4480 tentative_firewall (cp_parser
*p
): parser(p
)
4482 /* If we're currently parsing tentatively, start a committed level as a
4483 firewall and then an inner tentative parse. */
4484 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4486 cp_parser_parse_tentatively (parser
);
4487 cp_parser_commit_to_topmost_tentative_parse (parser
);
4488 cp_parser_parse_tentatively (parser
);
4492 ~tentative_firewall()
4496 /* Finish the inner tentative parse and the firewall, propagating any
4497 uncommitted error state to the outer tentative parse. */
4498 bool err
= cp_parser_error_occurred (parser
);
4499 cp_parser_parse_definitely (parser
);
4500 cp_parser_parse_definitely (parser
);
4502 cp_parser_simulate_error (parser
);
4507 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4508 This class is for tracking such a matching pair of symbols.
4509 In particular, it tracks the location of the first token,
4510 so that if the second token is missing, we can highlight the
4511 location of the first token when notifying the user about the
4514 template <typename traits_t
>
4518 /* token_pair's ctor. */
4519 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4521 /* If the next token is the opening symbol for this pair, consume it and
4523 Otherwise, issue an error and return false.
4524 In either case, record the location of the opening token. */
4526 bool require_open (cp_parser
*parser
)
4528 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4529 return cp_parser_require (parser
, traits_t::open_token_type
,
4530 traits_t::required_token_open
);
4533 /* Consume the next token from PARSER, recording its location as
4534 that of the opening token within the pair. */
4536 cp_token
* consume_open (cp_parser
*parser
)
4538 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4539 gcc_assert (tok
->type
== traits_t::open_token_type
);
4540 m_open_loc
= tok
->location
;
4544 /* If the next token is the closing symbol for this pair, consume it
4546 Otherwise, issue an error, highlighting the location of the
4547 corresponding opening token, and return NULL. */
4549 cp_token
*require_close (cp_parser
*parser
) const
4551 return cp_parser_require (parser
, traits_t::close_token_type
,
4552 traits_t::required_token_close
,
4557 location_t m_open_loc
;
4560 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4562 struct matching_paren_traits
4564 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4565 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4566 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4567 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4570 /* "matching_parens" is a token_pair<T> class for tracking matching
4571 pairs of parentheses. */
4573 typedef token_pair
<matching_paren_traits
> matching_parens
;
4575 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4577 struct matching_brace_traits
4579 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4580 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4581 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4582 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4585 /* "matching_braces" is a token_pair<T> class for tracking matching
4588 typedef token_pair
<matching_brace_traits
> matching_braces
;
4591 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4592 enclosing parentheses. */
4595 cp_parser_statement_expr (cp_parser
*parser
)
4597 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4599 /* Consume the '('. */
4600 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4601 matching_parens parens
;
4602 parens
.consume_open (parser
);
4603 /* Start the statement-expression. */
4604 tree expr
= begin_stmt_expr ();
4605 /* Parse the compound-statement. */
4606 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4608 expr
= finish_stmt_expr (expr
, false);
4609 /* Consume the ')'. */
4610 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4611 if (!parens
.require_close (parser
))
4612 cp_parser_skip_to_end_of_statement (parser
);
4614 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4615 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4616 return cp_expr (expr
, combined_loc
);
4619 /* Expressions [gram.expr] */
4621 /* Parse a fold-operator.
4624 - * / % ^ & | = < > << >>
4625 = -= *= /= %= ^= &= |= <<= >>=
4626 == != <= >= && || , .* ->*
4628 This returns the tree code corresponding to the matched operator
4629 as an int. When the current token matches a compound assignment
4630 opertor, the resulting tree code is the negative value of the
4631 non-assignment operator. */
4634 cp_parser_fold_operator (cp_token
*token
)
4636 switch (token
->type
)
4638 case CPP_PLUS
: return PLUS_EXPR
;
4639 case CPP_MINUS
: return MINUS_EXPR
;
4640 case CPP_MULT
: return MULT_EXPR
;
4641 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4642 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4643 case CPP_XOR
: return BIT_XOR_EXPR
;
4644 case CPP_AND
: return BIT_AND_EXPR
;
4645 case CPP_OR
: return BIT_IOR_EXPR
;
4646 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4647 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4649 case CPP_EQ
: return -NOP_EXPR
;
4650 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4651 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4652 case CPP_MULT_EQ
: return -MULT_EXPR
;
4653 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4654 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4655 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4656 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4657 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4658 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4659 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4661 case CPP_EQ_EQ
: return EQ_EXPR
;
4662 case CPP_NOT_EQ
: return NE_EXPR
;
4663 case CPP_LESS
: return LT_EXPR
;
4664 case CPP_GREATER
: return GT_EXPR
;
4665 case CPP_LESS_EQ
: return LE_EXPR
;
4666 case CPP_GREATER_EQ
: return GE_EXPR
;
4668 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4669 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4671 case CPP_COMMA
: return COMPOUND_EXPR
;
4673 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4674 case CPP_DEREF_STAR
: return MEMBER_REF
;
4676 default: return ERROR_MARK
;
4680 /* Returns true if CODE indicates a binary expression, which is not allowed in
4681 the LHS of a fold-expression. More codes will need to be added to use this
4682 function in other contexts. */
4685 is_binary_op (tree_code code
)
4690 case POINTER_PLUS_EXPR
:
4693 case TRUNC_DIV_EXPR
:
4694 case TRUNC_MOD_EXPR
:
4710 case TRUTH_ANDIF_EXPR
:
4711 case TRUTH_ORIF_EXPR
:
4724 /* If the next token is a suitable fold operator, consume it and return as
4725 the function above. */
4728 cp_parser_fold_operator (cp_parser
*parser
)
4730 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4731 int code
= cp_parser_fold_operator (token
);
4732 if (code
!= ERROR_MARK
)
4733 cp_lexer_consume_token (parser
->lexer
);
4737 /* Parse a fold-expression.
4740 ( ... folding-operator cast-expression)
4741 ( cast-expression folding-operator ... )
4742 ( cast-expression folding operator ... folding-operator cast-expression)
4744 Note that the '(' and ')' are matched in primary expression. */
4747 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4754 cp_lexer_consume_token (parser
->lexer
);
4755 int op
= cp_parser_fold_operator (parser
);
4756 if (op
== ERROR_MARK
)
4758 cp_parser_error (parser
, "expected binary operator");
4759 return error_mark_node
;
4762 tree expr
= cp_parser_cast_expression (parser
, false, false,
4764 if (expr
== error_mark_node
)
4765 return error_mark_node
;
4766 return finish_left_unary_fold_expr (expr
, op
);
4769 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4770 int op
= cp_parser_fold_operator (parser
);
4771 if (op
== ERROR_MARK
)
4773 cp_parser_error (parser
, "expected binary operator");
4774 return error_mark_node
;
4777 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4779 cp_parser_error (parser
, "expected ...");
4780 return error_mark_node
;
4782 cp_lexer_consume_token (parser
->lexer
);
4784 /* The operands of a fold-expression are cast-expressions, so binary or
4785 conditional expressions are not allowed. We check this here to avoid
4786 tentative parsing. */
4787 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4788 /* OK, the expression was parenthesized. */;
4789 else if (is_binary_op (TREE_CODE (expr1
)))
4790 error_at (location_of (expr1
),
4791 "binary expression in operand of fold-expression");
4792 else if (TREE_CODE (expr1
) == COND_EXPR
)
4793 error_at (location_of (expr1
),
4794 "conditional expression in operand of fold-expression");
4797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4798 return finish_right_unary_fold_expr (expr1
, op
);
4800 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4802 cp_parser_error (parser
, "mismatched operator in fold-expression");
4803 return error_mark_node
;
4805 cp_lexer_consume_token (parser
->lexer
);
4807 // Binary left or right fold.
4808 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4809 if (expr2
== error_mark_node
)
4810 return error_mark_node
;
4811 return finish_binary_fold_expr (expr1
, expr2
, op
);
4814 /* Parse a primary-expression.
4821 lambda-expression (C++11)
4826 ( compound-statement )
4827 __builtin_va_arg ( assignment-expression , type-id )
4828 __builtin_offsetof ( type-id , offsetof-expression )
4831 __has_nothrow_assign ( type-id )
4832 __has_nothrow_constructor ( type-id )
4833 __has_nothrow_copy ( type-id )
4834 __has_trivial_assign ( type-id )
4835 __has_trivial_constructor ( type-id )
4836 __has_trivial_copy ( type-id )
4837 __has_trivial_destructor ( type-id )
4838 __has_virtual_destructor ( type-id )
4839 __is_abstract ( type-id )
4840 __is_base_of ( type-id , type-id )
4841 __is_class ( type-id )
4842 __is_empty ( type-id )
4843 __is_enum ( type-id )
4844 __is_final ( type-id )
4845 __is_literal_type ( type-id )
4846 __is_pod ( type-id )
4847 __is_polymorphic ( type-id )
4848 __is_std_layout ( type-id )
4849 __is_trivial ( type-id )
4850 __is_union ( type-id )
4852 Objective-C++ Extension:
4860 ADDRESS_P is true iff this expression was immediately preceded by
4861 "&" and therefore might denote a pointer-to-member. CAST_P is true
4862 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4863 true iff this expression is a template argument.
4865 Returns a representation of the expression. Upon return, *IDK
4866 indicates what kind of id-expression (if any) was present. */
4869 cp_parser_primary_expression (cp_parser
*parser
,
4872 bool template_arg_p
,
4876 cp_token
*token
= NULL
;
4878 /* Assume the primary expression is not an id-expression. */
4879 *idk
= CP_ID_KIND_NONE
;
4881 /* Peek at the next token. */
4882 token
= cp_lexer_peek_token (parser
->lexer
);
4883 switch ((int) token
->type
)
4892 user-defined-literal */
4899 case CPP_PREPARSED_EXPR
:
4900 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4901 return cp_parser_userdef_numeric_literal (parser
);
4902 token
= cp_lexer_consume_token (parser
->lexer
);
4903 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4905 error_at (token
->location
,
4906 "fixed-point types not supported in C++");
4907 return error_mark_node
;
4909 /* Floating-point literals are only allowed in an integral
4910 constant expression if they are cast to an integral or
4911 enumeration type. */
4912 if (TREE_CODE (token
->u
.value
) == REAL_CST
4913 && parser
->integral_constant_expression_p
4916 /* CAST_P will be set even in invalid code like "int(2.7 +
4917 ...)". Therefore, we have to check that the next token
4918 is sure to end the cast. */
4921 cp_token
*next_token
;
4923 next_token
= cp_lexer_peek_token (parser
->lexer
);
4924 if (/* The comma at the end of an
4925 enumerator-definition. */
4926 next_token
->type
!= CPP_COMMA
4927 /* The curly brace at the end of an enum-specifier. */
4928 && next_token
->type
!= CPP_CLOSE_BRACE
4929 /* The end of a statement. */
4930 && next_token
->type
!= CPP_SEMICOLON
4931 /* The end of the cast-expression. */
4932 && next_token
->type
!= CPP_CLOSE_PAREN
4933 /* The end of an array bound. */
4934 && next_token
->type
!= CPP_CLOSE_SQUARE
4935 /* The closing ">" in a template-argument-list. */
4936 && (next_token
->type
!= CPP_GREATER
4937 || parser
->greater_than_is_operator_p
)
4938 /* C++0x only: A ">>" treated like two ">" tokens,
4939 in a template-argument-list. */
4940 && (next_token
->type
!= CPP_RSHIFT
4941 || (cxx_dialect
== cxx98
)
4942 || parser
->greater_than_is_operator_p
))
4946 /* If we are within a cast, then the constraint that the
4947 cast is to an integral or enumeration type will be
4948 checked at that point. If we are not within a cast, then
4949 this code is invalid. */
4951 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4953 return cp_expr (token
->u
.value
, token
->location
);
4955 case CPP_CHAR_USERDEF
:
4956 case CPP_CHAR16_USERDEF
:
4957 case CPP_CHAR32_USERDEF
:
4958 case CPP_WCHAR_USERDEF
:
4959 case CPP_UTF8CHAR_USERDEF
:
4960 return cp_parser_userdef_char_literal (parser
);
4966 case CPP_UTF8STRING
:
4967 case CPP_STRING_USERDEF
:
4968 case CPP_STRING16_USERDEF
:
4969 case CPP_STRING32_USERDEF
:
4970 case CPP_WSTRING_USERDEF
:
4971 case CPP_UTF8STRING_USERDEF
:
4972 /* ??? Should wide strings be allowed when parser->translate_strings_p
4973 is false (i.e. in attributes)? If not, we can kill the third
4974 argument to cp_parser_string_literal. */
4975 return cp_parser_string_literal (parser
,
4976 parser
->translate_strings_p
,
4979 case CPP_OPEN_PAREN
:
4980 /* If we see `( { ' then we are looking at the beginning of
4981 a GNU statement-expression. */
4982 if (cp_parser_allow_gnu_extensions_p (parser
)
4983 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4985 /* Statement-expressions are not allowed by the standard. */
4986 pedwarn (token
->location
, OPT_Wpedantic
,
4987 "ISO C++ forbids braced-groups within expressions");
4989 /* And they're not allowed outside of a function-body; you
4990 cannot, for example, write:
4992 int i = ({ int j = 3; j + 1; });
4994 at class or namespace scope. */
4995 if (!parser
->in_function_body
4996 || parser
->in_template_argument_list_p
)
4998 error_at (token
->location
,
4999 "statement-expressions are not allowed outside "
5000 "functions nor in template-argument lists");
5001 cp_parser_skip_to_end_of_block_or_statement (parser
);
5002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5003 cp_lexer_consume_token (parser
->lexer
);
5004 return error_mark_node
;
5007 return cp_parser_statement_expr (parser
);
5009 /* Otherwise it's a normal parenthesized expression. */
5012 bool saved_greater_than_is_operator_p
;
5014 location_t open_paren_loc
= token
->location
;
5016 /* Consume the `('. */
5017 matching_parens parens
;
5018 parens
.consume_open (parser
);
5019 /* Within a parenthesized expression, a `>' token is always
5020 the greater-than operator. */
5021 saved_greater_than_is_operator_p
5022 = parser
->greater_than_is_operator_p
;
5023 parser
->greater_than_is_operator_p
= true;
5025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5026 /* Left fold expression. */
5029 /* Parse the parenthesized expression. */
5030 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5032 token
= cp_lexer_peek_token (parser
->lexer
);
5033 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5035 expr
= cp_parser_fold_expression (parser
, expr
);
5036 if (expr
!= error_mark_node
5037 && cxx_dialect
< cxx17
5038 && !in_system_header_at (input_location
))
5039 pedwarn (input_location
, 0, "fold-expressions only available "
5040 "with -std=c++17 or -std=gnu++17");
5043 /* Let the front end know that this expression was
5044 enclosed in parentheses. This matters in case, for
5045 example, the expression is of the form `A::B', since
5046 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5048 expr
= finish_parenthesized_expr (expr
);
5050 /* DR 705: Wrapping an unqualified name in parentheses
5051 suppresses arg-dependent lookup. We want to pass back
5052 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5053 (c++/37862), but none of the others. */
5054 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5055 *idk
= CP_ID_KIND_NONE
;
5057 /* The `>' token might be the end of a template-id or
5058 template-parameter-list now. */
5059 parser
->greater_than_is_operator_p
5060 = saved_greater_than_is_operator_p
;
5062 /* Consume the `)'. */
5063 token
= cp_lexer_peek_token (parser
->lexer
);
5064 location_t close_paren_loc
= token
->location
;
5065 expr
.set_range (open_paren_loc
, close_paren_loc
);
5066 if (!parens
.require_close (parser
)
5067 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5068 cp_parser_skip_to_end_of_statement (parser
);
5073 case CPP_OPEN_SQUARE
:
5075 if (c_dialect_objc ())
5077 /* We might have an Objective-C++ message. */
5078 cp_parser_parse_tentatively (parser
);
5079 tree msg
= cp_parser_objc_message_expression (parser
);
5080 /* If that works out, we're done ... */
5081 if (cp_parser_parse_definitely (parser
))
5083 /* ... else, fall though to see if it's a lambda. */
5085 cp_expr lam
= cp_parser_lambda_expression (parser
);
5086 /* Don't warn about a failed tentative parse. */
5087 if (cp_parser_error_occurred (parser
))
5088 return error_mark_node
;
5089 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5093 case CPP_OBJC_STRING
:
5094 if (c_dialect_objc ())
5095 /* We have an Objective-C++ string literal. */
5096 return cp_parser_objc_expression (parser
);
5097 cp_parser_error (parser
, "expected primary-expression");
5098 return error_mark_node
;
5101 switch (token
->keyword
)
5103 /* These two are the boolean literals. */
5105 cp_lexer_consume_token (parser
->lexer
);
5106 return cp_expr (boolean_true_node
, token
->location
);
5108 cp_lexer_consume_token (parser
->lexer
);
5109 return cp_expr (boolean_false_node
, token
->location
);
5111 /* The `__null' literal. */
5113 cp_lexer_consume_token (parser
->lexer
);
5114 return cp_expr (null_node
, token
->location
);
5116 /* The `nullptr' literal. */
5118 cp_lexer_consume_token (parser
->lexer
);
5119 return cp_expr (nullptr_node
, token
->location
);
5121 /* Recognize the `this' keyword. */
5123 cp_lexer_consume_token (parser
->lexer
);
5124 if (parser
->local_variables_forbidden_p
)
5126 error_at (token
->location
,
5127 "%<this%> may not be used in this context");
5128 return error_mark_node
;
5130 /* Pointers cannot appear in constant-expressions. */
5131 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5132 return error_mark_node
;
5133 return cp_expr (finish_this_expr (), token
->location
);
5135 /* The `operator' keyword can be the beginning of an
5140 case RID_FUNCTION_NAME
:
5141 case RID_PRETTY_FUNCTION_NAME
:
5142 case RID_C99_FUNCTION_NAME
:
5144 non_integral_constant name
;
5146 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5147 __func__ are the names of variables -- but they are
5148 treated specially. Therefore, they are handled here,
5149 rather than relying on the generic id-expression logic
5150 below. Grammatically, these names are id-expressions.
5152 Consume the token. */
5153 token
= cp_lexer_consume_token (parser
->lexer
);
5155 switch (token
->keyword
)
5157 case RID_FUNCTION_NAME
:
5158 name
= NIC_FUNC_NAME
;
5160 case RID_PRETTY_FUNCTION_NAME
:
5161 name
= NIC_PRETTY_FUNC
;
5163 case RID_C99_FUNCTION_NAME
:
5164 name
= NIC_C99_FUNC
;
5170 if (cp_parser_non_integral_constant_expression (parser
, name
))
5171 return error_mark_node
;
5173 /* Look up the name. */
5174 return finish_fname (token
->u
.value
);
5181 source_location type_location
;
5182 location_t start_loc
5183 = cp_lexer_peek_token (parser
->lexer
)->location
;
5184 /* The `__builtin_va_arg' construct is used to handle
5185 `va_arg'. Consume the `__builtin_va_arg' token. */
5186 cp_lexer_consume_token (parser
->lexer
);
5187 /* Look for the opening `('. */
5188 matching_parens parens
;
5189 parens
.require_open (parser
);
5190 /* Now, parse the assignment-expression. */
5191 expression
= cp_parser_assignment_expression (parser
);
5192 /* Look for the `,'. */
5193 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5194 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5195 /* Parse the type-id. */
5197 type_id_in_expr_sentinel
s (parser
);
5198 type
= cp_parser_type_id (parser
);
5200 /* Look for the closing `)'. */
5201 location_t finish_loc
5202 = cp_lexer_peek_token (parser
->lexer
)->location
;
5203 parens
.require_close (parser
);
5204 /* Using `va_arg' in a constant-expression is not
5206 if (cp_parser_non_integral_constant_expression (parser
,
5208 return error_mark_node
;
5209 /* Construct a location of the form:
5210 __builtin_va_arg (v, int)
5211 ~~~~~~~~~~~~~~~~~~~~~^~~~
5212 with the caret at the type, ranging from the start of the
5213 "__builtin_va_arg" token to the close paren. */
5214 location_t combined_loc
5215 = make_location (type_location
, start_loc
, finish_loc
);
5216 return build_x_va_arg (combined_loc
, expression
, type
);
5220 return cp_parser_builtin_offsetof (parser
);
5222 case RID_HAS_NOTHROW_ASSIGN
:
5223 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5224 case RID_HAS_NOTHROW_COPY
:
5225 case RID_HAS_TRIVIAL_ASSIGN
:
5226 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5227 case RID_HAS_TRIVIAL_COPY
:
5228 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5229 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5230 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5231 case RID_IS_ABSTRACT
:
5232 case RID_IS_AGGREGATE
:
5233 case RID_IS_BASE_OF
:
5238 case RID_IS_LITERAL_TYPE
:
5240 case RID_IS_POLYMORPHIC
:
5241 case RID_IS_SAME_AS
:
5242 case RID_IS_STD_LAYOUT
:
5243 case RID_IS_TRIVIAL
:
5244 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5245 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5246 case RID_IS_TRIVIALLY_COPYABLE
:
5248 case RID_IS_ASSIGNABLE
:
5249 case RID_IS_CONSTRUCTIBLE
:
5250 return cp_parser_trait_expr (parser
, token
->keyword
);
5254 return cp_parser_requires_expression (parser
);
5256 /* Objective-C++ expressions. */
5258 case RID_AT_PROTOCOL
:
5259 case RID_AT_SELECTOR
:
5260 return cp_parser_objc_expression (parser
);
5263 if (parser
->in_function_body
5264 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5267 error_at (token
->location
,
5268 "a template declaration cannot appear at block scope");
5269 cp_parser_skip_to_end_of_block_or_statement (parser
);
5270 return error_mark_node
;
5274 cp_parser_error (parser
, "expected primary-expression");
5275 return error_mark_node
;
5278 /* An id-expression can start with either an identifier, a
5279 `::' as the beginning of a qualified-id, or the "operator"
5283 case CPP_TEMPLATE_ID
:
5284 case CPP_NESTED_NAME_SPECIFIER
:
5287 cp_expr id_expression
;
5289 const char *error_msg
;
5292 cp_token
*id_expr_token
;
5294 /* Parse the id-expression. */
5296 = cp_parser_id_expression (parser
,
5297 /*template_keyword_p=*/false,
5298 /*check_dependency_p=*/true,
5300 /*declarator_p=*/false,
5301 /*optional_p=*/false);
5302 if (id_expression
== error_mark_node
)
5303 return error_mark_node
;
5304 id_expr_token
= token
;
5305 token
= cp_lexer_peek_token (parser
->lexer
);
5306 done
= (token
->type
!= CPP_OPEN_SQUARE
5307 && token
->type
!= CPP_OPEN_PAREN
5308 && token
->type
!= CPP_DOT
5309 && token
->type
!= CPP_DEREF
5310 && token
->type
!= CPP_PLUS_PLUS
5311 && token
->type
!= CPP_MINUS_MINUS
);
5312 /* If we have a template-id, then no further lookup is
5313 required. If the template-id was for a template-class, we
5314 will sometimes have a TYPE_DECL at this point. */
5315 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5316 || TREE_CODE (id_expression
) == TYPE_DECL
)
5317 decl
= id_expression
;
5318 /* Look up the name. */
5321 tree ambiguous_decls
;
5323 /* If we already know that this lookup is ambiguous, then
5324 we've already issued an error message; there's no reason
5326 if (id_expr_token
->type
== CPP_NAME
5327 && id_expr_token
->error_reported
)
5329 cp_parser_simulate_error (parser
);
5330 return error_mark_node
;
5333 decl
= cp_parser_lookup_name (parser
, id_expression
,
5336 /*is_namespace=*/false,
5337 /*check_dependency=*/true,
5339 id_expr_token
->location
);
5340 /* If the lookup was ambiguous, an error will already have
5342 if (ambiguous_decls
)
5343 return error_mark_node
;
5345 /* In Objective-C++, we may have an Objective-C 2.0
5346 dot-syntax for classes here. */
5347 if (c_dialect_objc ()
5348 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5349 && TREE_CODE (decl
) == TYPE_DECL
5350 && objc_is_class_name (decl
))
5353 cp_lexer_consume_token (parser
->lexer
);
5354 component
= cp_parser_identifier (parser
);
5355 if (component
== error_mark_node
)
5356 return error_mark_node
;
5358 tree result
= objc_build_class_component_ref (id_expression
,
5360 /* Build a location of the form:
5363 with caret at the start of the component name (at
5364 input_location), ranging from the start of the id_expression
5365 to the end of the component name. */
5366 location_t combined_loc
5367 = make_location (input_location
, id_expression
.get_start (),
5368 get_finish (input_location
));
5369 protected_set_expr_location (result
, combined_loc
);
5373 /* In Objective-C++, an instance variable (ivar) may be preferred
5374 to whatever cp_parser_lookup_name() found.
5375 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5376 rest of c-family, we have to do a little extra work to preserve
5377 any location information in cp_expr "decl". Given that
5378 objc_lookup_ivar is implemented in "c-family" and "objc", we
5379 have a trip through the pure "tree" type, rather than cp_expr.
5380 Naively copying it back to "decl" would implicitly give the
5381 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5382 store an EXPR_LOCATION. Hence we only update "decl" (and
5383 hence its location_t) if we get back a different tree node. */
5384 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5386 if (decl_tree
!= decl
.get_value ())
5387 decl
= cp_expr (decl_tree
);
5389 /* If name lookup gives us a SCOPE_REF, then the
5390 qualifying scope was dependent. */
5391 if (TREE_CODE (decl
) == SCOPE_REF
)
5393 /* At this point, we do not know if DECL is a valid
5394 integral constant expression. We assume that it is
5395 in fact such an expression, so that code like:
5397 template <int N> struct A {
5401 is accepted. At template-instantiation time, we
5402 will check that B<N>::i is actually a constant. */
5405 /* Check to see if DECL is a local variable in a context
5406 where that is forbidden. */
5407 if (parser
->local_variables_forbidden_p
5408 && local_variable_p (decl
))
5410 /* It might be that we only found DECL because we are
5411 trying to be generous with pre-ISO scoping rules.
5412 For example, consider:
5416 for (int i = 0; i < 10; ++i) {}
5417 extern void f(int j = i);
5420 Here, name look up will originally find the out
5421 of scope `i'. We need to issue a warning message,
5422 but then use the global `i'. */
5423 decl
= check_for_out_of_scope_variable (decl
);
5424 if (local_variable_p (decl
))
5426 error_at (id_expr_token
->location
,
5427 "local variable %qD may not appear in this context",
5429 return error_mark_node
;
5434 decl
= (finish_id_expression
5435 (id_expression
, decl
, parser
->scope
,
5437 parser
->integral_constant_expression_p
,
5438 parser
->allow_non_integral_constant_expression_p
,
5439 &parser
->non_integral_constant_expression_p
,
5440 template_p
, done
, address_p
,
5443 id_expression
.get_location ()));
5445 cp_parser_error (parser
, error_msg
);
5446 decl
.set_location (id_expr_token
->location
);
5450 /* Anything else is an error. */
5452 cp_parser_error (parser
, "expected primary-expression");
5453 return error_mark_node
;
5457 static inline cp_expr
5458 cp_parser_primary_expression (cp_parser
*parser
,
5461 bool template_arg_p
,
5464 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5465 /*decltype*/false, idk
);
5468 /* Parse an id-expression.
5475 :: [opt] nested-name-specifier template [opt] unqualified-id
5477 :: operator-function-id
5480 Return a representation of the unqualified portion of the
5481 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5482 a `::' or nested-name-specifier.
5484 Often, if the id-expression was a qualified-id, the caller will
5485 want to make a SCOPE_REF to represent the qualified-id. This
5486 function does not do this in order to avoid wastefully creating
5487 SCOPE_REFs when they are not required.
5489 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5492 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5493 uninstantiated templates.
5495 If *TEMPLATE_P is non-NULL, it is set to true iff the
5496 `template' keyword is used to explicitly indicate that the entity
5497 named is a template.
5499 If DECLARATOR_P is true, the id-expression is appearing as part of
5500 a declarator, rather than as part of an expression. */
5503 cp_parser_id_expression (cp_parser
*parser
,
5504 bool template_keyword_p
,
5505 bool check_dependency_p
,
5510 bool global_scope_p
;
5511 bool nested_name_specifier_p
;
5513 /* Assume the `template' keyword was not used. */
5515 *template_p
= template_keyword_p
;
5517 /* Look for the optional `::' operator. */
5519 = (!template_keyword_p
5520 && (cp_parser_global_scope_opt (parser
,
5521 /*current_scope_valid_p=*/false)
5524 /* Look for the optional nested-name-specifier. */
5525 nested_name_specifier_p
5526 = (cp_parser_nested_name_specifier_opt (parser
,
5527 /*typename_keyword_p=*/false,
5534 /* If there is a nested-name-specifier, then we are looking at
5535 the first qualified-id production. */
5536 if (nested_name_specifier_p
)
5539 tree saved_object_scope
;
5540 tree saved_qualifying_scope
;
5541 cp_expr unqualified_id
;
5544 /* See if the next token is the `template' keyword. */
5546 template_p
= &is_template
;
5547 *template_p
= cp_parser_optional_template_keyword (parser
);
5548 /* Name lookup we do during the processing of the
5549 unqualified-id might obliterate SCOPE. */
5550 saved_scope
= parser
->scope
;
5551 saved_object_scope
= parser
->object_scope
;
5552 saved_qualifying_scope
= parser
->qualifying_scope
;
5553 /* Process the final unqualified-id. */
5554 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5557 /*optional_p=*/false);
5558 /* Restore the SAVED_SCOPE for our caller. */
5559 parser
->scope
= saved_scope
;
5560 parser
->object_scope
= saved_object_scope
;
5561 parser
->qualifying_scope
= saved_qualifying_scope
;
5563 return unqualified_id
;
5565 /* Otherwise, if we are in global scope, then we are looking at one
5566 of the other qualified-id productions. */
5567 else if (global_scope_p
)
5572 /* Peek at the next token. */
5573 token
= cp_lexer_peek_token (parser
->lexer
);
5575 /* If it's an identifier, and the next token is not a "<", then
5576 we can avoid the template-id case. This is an optimization
5577 for this common case. */
5578 if (token
->type
== CPP_NAME
5579 && !cp_parser_nth_token_starts_template_argument_list_p
5581 return cp_parser_identifier (parser
);
5583 cp_parser_parse_tentatively (parser
);
5584 /* Try a template-id. */
5585 id
= cp_parser_template_id (parser
,
5586 /*template_keyword_p=*/false,
5587 /*check_dependency_p=*/true,
5590 /* If that worked, we're done. */
5591 if (cp_parser_parse_definitely (parser
))
5594 /* Peek at the next token. (Changes in the token buffer may
5595 have invalidated the pointer obtained above.) */
5596 token
= cp_lexer_peek_token (parser
->lexer
);
5598 switch (token
->type
)
5601 return cp_parser_identifier (parser
);
5604 if (token
->keyword
== RID_OPERATOR
)
5605 return cp_parser_operator_function_id (parser
);
5609 cp_parser_error (parser
, "expected id-expression");
5610 return error_mark_node
;
5614 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5615 /*check_dependency_p=*/true,
5620 /* Parse an unqualified-id.
5624 operator-function-id
5625 conversion-function-id
5629 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5630 keyword, in a construct like `A::template ...'.
5632 Returns a representation of unqualified-id. For the `identifier'
5633 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5634 production a BIT_NOT_EXPR is returned; the operand of the
5635 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5636 other productions, see the documentation accompanying the
5637 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5638 names are looked up in uninstantiated templates. If DECLARATOR_P
5639 is true, the unqualified-id is appearing as part of a declarator,
5640 rather than as part of an expression. */
5643 cp_parser_unqualified_id (cp_parser
* parser
,
5644 bool template_keyword_p
,
5645 bool check_dependency_p
,
5651 /* Peek at the next token. */
5652 token
= cp_lexer_peek_token (parser
->lexer
);
5654 switch ((int) token
->type
)
5660 /* We don't know yet whether or not this will be a
5662 cp_parser_parse_tentatively (parser
);
5663 /* Try a template-id. */
5664 id
= cp_parser_template_id (parser
, template_keyword_p
,
5668 /* If it worked, we're done. */
5669 if (cp_parser_parse_definitely (parser
))
5671 /* Otherwise, it's an ordinary identifier. */
5672 return cp_parser_identifier (parser
);
5675 case CPP_TEMPLATE_ID
:
5676 return cp_parser_template_id (parser
, template_keyword_p
,
5684 tree qualifying_scope
;
5689 /* Consume the `~' token. */
5690 cp_lexer_consume_token (parser
->lexer
);
5691 /* Parse the class-name. The standard, as written, seems to
5694 template <typename T> struct S { ~S (); };
5695 template <typename T> S<T>::~S() {}
5697 is invalid, since `~' must be followed by a class-name, but
5698 `S<T>' is dependent, and so not known to be a class.
5699 That's not right; we need to look in uninstantiated
5700 templates. A further complication arises from:
5702 template <typename T> void f(T t) {
5706 Here, it is not possible to look up `T' in the scope of `T'
5707 itself. We must look in both the current scope, and the
5708 scope of the containing complete expression.
5710 Yet another issue is:
5719 The standard does not seem to say that the `S' in `~S'
5720 should refer to the type `S' and not the data member
5723 /* DR 244 says that we look up the name after the "~" in the
5724 same scope as we looked up the qualifying name. That idea
5725 isn't fully worked out; it's more complicated than that. */
5726 scope
= parser
->scope
;
5727 object_scope
= parser
->object_scope
;
5728 qualifying_scope
= parser
->qualifying_scope
;
5730 /* Check for invalid scopes. */
5731 if (scope
== error_mark_node
)
5733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5734 cp_lexer_consume_token (parser
->lexer
);
5735 return error_mark_node
;
5737 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5740 error_at (token
->location
,
5741 "scope %qT before %<~%> is not a class-name",
5743 cp_parser_simulate_error (parser
);
5744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5745 cp_lexer_consume_token (parser
->lexer
);
5746 return error_mark_node
;
5748 gcc_assert (!scope
|| TYPE_P (scope
));
5750 /* If the name is of the form "X::~X" it's OK even if X is a
5752 token
= cp_lexer_peek_token (parser
->lexer
);
5754 && token
->type
== CPP_NAME
5755 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5757 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5758 || (CLASS_TYPE_P (scope
)
5759 && constructor_name_p (token
->u
.value
, scope
))))
5761 cp_lexer_consume_token (parser
->lexer
);
5762 return build_nt (BIT_NOT_EXPR
, scope
);
5765 /* ~auto means the destructor of whatever the object is. */
5766 if (cp_parser_is_keyword (token
, RID_AUTO
))
5768 if (cxx_dialect
< cxx14
)
5769 pedwarn (input_location
, 0,
5770 "%<~auto%> only available with "
5771 "-std=c++14 or -std=gnu++14");
5772 cp_lexer_consume_token (parser
->lexer
);
5773 return build_nt (BIT_NOT_EXPR
, make_auto ());
5776 /* If there was an explicit qualification (S::~T), first look
5777 in the scope given by the qualification (i.e., S).
5779 Note: in the calls to cp_parser_class_name below we pass
5780 typename_type so that lookup finds the injected-class-name
5781 rather than the constructor. */
5783 type_decl
= NULL_TREE
;
5786 cp_parser_parse_tentatively (parser
);
5787 type_decl
= cp_parser_class_name (parser
,
5788 /*typename_keyword_p=*/false,
5789 /*template_keyword_p=*/false,
5791 /*check_dependency=*/false,
5792 /*class_head_p=*/false,
5794 if (cp_parser_parse_definitely (parser
))
5797 /* In "N::S::~S", look in "N" as well. */
5798 if (!done
&& scope
&& qualifying_scope
)
5800 cp_parser_parse_tentatively (parser
);
5801 parser
->scope
= qualifying_scope
;
5802 parser
->object_scope
= NULL_TREE
;
5803 parser
->qualifying_scope
= NULL_TREE
;
5805 = cp_parser_class_name (parser
,
5806 /*typename_keyword_p=*/false,
5807 /*template_keyword_p=*/false,
5809 /*check_dependency=*/false,
5810 /*class_head_p=*/false,
5812 if (cp_parser_parse_definitely (parser
))
5815 /* In "p->S::~T", look in the scope given by "*p" as well. */
5816 else if (!done
&& object_scope
)
5818 cp_parser_parse_tentatively (parser
);
5819 parser
->scope
= object_scope
;
5820 parser
->object_scope
= NULL_TREE
;
5821 parser
->qualifying_scope
= NULL_TREE
;
5823 = cp_parser_class_name (parser
,
5824 /*typename_keyword_p=*/false,
5825 /*template_keyword_p=*/false,
5827 /*check_dependency=*/false,
5828 /*class_head_p=*/false,
5830 if (cp_parser_parse_definitely (parser
))
5833 /* Look in the surrounding context. */
5836 parser
->scope
= NULL_TREE
;
5837 parser
->object_scope
= NULL_TREE
;
5838 parser
->qualifying_scope
= NULL_TREE
;
5839 if (processing_template_decl
)
5840 cp_parser_parse_tentatively (parser
);
5842 = cp_parser_class_name (parser
,
5843 /*typename_keyword_p=*/false,
5844 /*template_keyword_p=*/false,
5846 /*check_dependency=*/false,
5847 /*class_head_p=*/false,
5849 if (processing_template_decl
5850 && ! cp_parser_parse_definitely (parser
))
5852 /* We couldn't find a type with this name. If we're parsing
5853 tentatively, fail and try something else. */
5854 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5856 cp_parser_simulate_error (parser
);
5857 return error_mark_node
;
5859 /* Otherwise, accept it and check for a match at instantiation
5861 type_decl
= cp_parser_identifier (parser
);
5862 if (type_decl
!= error_mark_node
)
5863 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5867 /* If an error occurred, assume that the name of the
5868 destructor is the same as the name of the qualifying
5869 class. That allows us to keep parsing after running
5870 into ill-formed destructor names. */
5871 if (type_decl
== error_mark_node
&& scope
)
5872 return build_nt (BIT_NOT_EXPR
, scope
);
5873 else if (type_decl
== error_mark_node
)
5874 return error_mark_node
;
5876 /* Check that destructor name and scope match. */
5877 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5880 error_at (token
->location
,
5881 "declaration of %<~%T%> as member of %qT",
5883 cp_parser_simulate_error (parser
);
5884 return error_mark_node
;
5889 A typedef-name that names a class shall not be used as the
5890 identifier in the declarator for a destructor declaration. */
5892 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5893 && !DECL_SELF_REFERENCE_P (type_decl
)
5894 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5895 error_at (token
->location
,
5896 "typedef-name %qD used as destructor declarator",
5899 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5903 if (token
->keyword
== RID_OPERATOR
)
5907 /* This could be a template-id, so we try that first. */
5908 cp_parser_parse_tentatively (parser
);
5909 /* Try a template-id. */
5910 id
= cp_parser_template_id (parser
, template_keyword_p
,
5911 /*check_dependency_p=*/true,
5914 /* If that worked, we're done. */
5915 if (cp_parser_parse_definitely (parser
))
5917 /* We still don't know whether we're looking at an
5918 operator-function-id or a conversion-function-id. */
5919 cp_parser_parse_tentatively (parser
);
5920 /* Try an operator-function-id. */
5921 id
= cp_parser_operator_function_id (parser
);
5922 /* If that didn't work, try a conversion-function-id. */
5923 if (!cp_parser_parse_definitely (parser
))
5924 id
= cp_parser_conversion_function_id (parser
);
5925 else if (UDLIT_OPER_P (id
))
5928 const char *name
= UDLIT_OP_SUFFIX (id
);
5929 if (name
[0] != '_' && !in_system_header_at (input_location
)
5931 warning (OPT_Wliteral_suffix
,
5932 "literal operator suffixes not preceded by %<_%>"
5933 " are reserved for future standardization");
5943 cp_parser_error (parser
, "expected unqualified-id");
5944 return error_mark_node
;
5948 /* Parse an (optional) nested-name-specifier.
5950 nested-name-specifier: [C++98]
5951 class-or-namespace-name :: nested-name-specifier [opt]
5952 class-or-namespace-name :: template nested-name-specifier [opt]
5954 nested-name-specifier: [C++0x]
5957 nested-name-specifier identifier ::
5958 nested-name-specifier template [opt] simple-template-id ::
5960 PARSER->SCOPE should be set appropriately before this function is
5961 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5962 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5965 Sets PARSER->SCOPE to the class (TYPE) or namespace
5966 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5967 it unchanged if there is no nested-name-specifier. Returns the new
5968 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5970 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5971 part of a declaration and/or decl-specifier. */
5974 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5975 bool typename_keyword_p
,
5976 bool check_dependency_p
,
5978 bool is_declaration
,
5979 bool template_keyword_p
/* = false */)
5981 bool success
= false;
5982 cp_token_position start
= 0;
5985 /* Remember where the nested-name-specifier starts. */
5986 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5988 start
= cp_lexer_token_position (parser
->lexer
, false);
5989 push_deferring_access_checks (dk_deferred
);
5996 tree saved_qualifying_scope
;
5998 /* Spot cases that cannot be the beginning of a
5999 nested-name-specifier. */
6000 token
= cp_lexer_peek_token (parser
->lexer
);
6002 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6003 the already parsed nested-name-specifier. */
6004 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6006 /* Grab the nested-name-specifier and continue the loop. */
6007 cp_parser_pre_parsed_nested_name_specifier (parser
);
6008 /* If we originally encountered this nested-name-specifier
6009 with IS_DECLARATION set to false, we will not have
6010 resolved TYPENAME_TYPEs, so we must do so here. */
6012 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6014 new_scope
= resolve_typename_type (parser
->scope
,
6015 /*only_current_p=*/false);
6016 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6017 parser
->scope
= new_scope
;
6023 /* Spot cases that cannot be the beginning of a
6024 nested-name-specifier. On the second and subsequent times
6025 through the loop, we look for the `template' keyword. */
6026 if (success
&& token
->keyword
== RID_TEMPLATE
)
6028 /* A template-id can start a nested-name-specifier. */
6029 else if (token
->type
== CPP_TEMPLATE_ID
)
6031 /* DR 743: decltype can be used in a nested-name-specifier. */
6032 else if (token_is_decltype (token
))
6036 /* If the next token is not an identifier, then it is
6037 definitely not a type-name or namespace-name. */
6038 if (token
->type
!= CPP_NAME
)
6040 /* If the following token is neither a `<' (to begin a
6041 template-id), nor a `::', then we are not looking at a
6042 nested-name-specifier. */
6043 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6045 if (token
->type
== CPP_COLON
6046 && parser
->colon_corrects_to_scope_p
6047 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6049 gcc_rich_location
richloc (token
->location
);
6050 richloc
.add_fixit_replace ("::");
6051 error_at_rich_loc (&richloc
,
6052 "found %<:%> in nested-name-specifier, "
6054 token
->type
= CPP_SCOPE
;
6057 if (token
->type
!= CPP_SCOPE
6058 && !cp_parser_nth_token_starts_template_argument_list_p
6063 /* The nested-name-specifier is optional, so we parse
6065 cp_parser_parse_tentatively (parser
);
6067 /* Look for the optional `template' keyword, if this isn't the
6068 first time through the loop. */
6070 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6072 /* Save the old scope since the name lookup we are about to do
6073 might destroy it. */
6074 old_scope
= parser
->scope
;
6075 saved_qualifying_scope
= parser
->qualifying_scope
;
6076 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6077 look up names in "X<T>::I" in order to determine that "Y" is
6078 a template. So, if we have a typename at this point, we make
6079 an effort to look through it. */
6081 && !typename_keyword_p
6083 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6084 parser
->scope
= resolve_typename_type (parser
->scope
,
6085 /*only_current_p=*/false);
6086 /* Parse the qualifying entity. */
6088 = cp_parser_qualifying_entity (parser
,
6094 /* Look for the `::' token. */
6095 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6097 /* If we found what we wanted, we keep going; otherwise, we're
6099 if (!cp_parser_parse_definitely (parser
))
6101 bool error_p
= false;
6103 /* Restore the OLD_SCOPE since it was valid before the
6104 failed attempt at finding the last
6105 class-or-namespace-name. */
6106 parser
->scope
= old_scope
;
6107 parser
->qualifying_scope
= saved_qualifying_scope
;
6109 /* If the next token is a decltype, and the one after that is a
6110 `::', then the decltype has failed to resolve to a class or
6111 enumeration type. Give this error even when parsing
6112 tentatively since it can't possibly be valid--and we're going
6113 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6114 won't get another chance.*/
6115 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6116 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6119 token
= cp_lexer_consume_token (parser
->lexer
);
6120 error_at (token
->location
, "decltype evaluates to %qT, "
6121 "which is not a class or enumeration type",
6122 token
->u
.tree_check_value
->value
);
6123 parser
->scope
= error_mark_node
;
6127 cp_lexer_consume_token (parser
->lexer
);
6130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6131 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6133 /* If we have a non-type template-id followed by ::, it can't
6134 possibly be valid. */
6135 token
= cp_lexer_peek_token (parser
->lexer
);
6136 tree tid
= token
->u
.tree_check_value
->value
;
6137 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6138 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6140 tree tmpl
= NULL_TREE
;
6141 if (is_overloaded_fn (tid
))
6143 tree fns
= get_fns (tid
);
6144 if (OVL_SINGLE_P (fns
))
6145 tmpl
= OVL_FIRST (fns
);
6146 error_at (token
->location
, "function template-id %qD "
6147 "in nested-name-specifier", tid
);
6151 /* Variable template. */
6152 tmpl
= TREE_OPERAND (tid
, 0);
6153 gcc_assert (variable_template_p (tmpl
));
6154 error_at (token
->location
, "variable template-id %qD "
6155 "in nested-name-specifier", tid
);
6158 inform (DECL_SOURCE_LOCATION (tmpl
),
6159 "%qD declared here", tmpl
);
6161 parser
->scope
= error_mark_node
;
6165 cp_lexer_consume_token (parser
->lexer
);
6166 cp_lexer_consume_token (parser
->lexer
);
6170 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6172 /* If the next token is an identifier, and the one after
6173 that is a `::', then any valid interpretation would have
6174 found a class-or-namespace-name. */
6175 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6176 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6178 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6181 token
= cp_lexer_consume_token (parser
->lexer
);
6184 if (!token
->error_reported
)
6187 tree ambiguous_decls
;
6189 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6191 /*is_template=*/false,
6192 /*is_namespace=*/false,
6193 /*check_dependency=*/true,
6196 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6197 error_at (token
->location
,
6198 "%qD used without template parameters",
6200 else if (ambiguous_decls
)
6202 // cp_parser_lookup_name has the same diagnostic,
6203 // thus make sure to emit it at most once.
6204 if (cp_parser_uncommitted_to_tentative_parse_p
6207 error_at (token
->location
,
6208 "reference to %qD is ambiguous",
6210 print_candidates (ambiguous_decls
);
6212 decl
= error_mark_node
;
6216 if (cxx_dialect
!= cxx98
)
6217 cp_parser_name_lookup_error
6218 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6221 cp_parser_name_lookup_error
6222 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6226 parser
->scope
= error_mark_node
;
6228 /* Treat this as a successful nested-name-specifier
6233 If the name found is not a class-name (clause
6234 _class_) or namespace-name (_namespace.def_), the
6235 program is ill-formed. */
6238 cp_lexer_consume_token (parser
->lexer
);
6242 /* We've found one valid nested-name-specifier. */
6244 /* Name lookup always gives us a DECL. */
6245 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6246 new_scope
= TREE_TYPE (new_scope
);
6247 /* Uses of "template" must be followed by actual templates. */
6248 if (template_keyword_p
6249 && !(CLASS_TYPE_P (new_scope
)
6250 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6251 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6252 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6253 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6254 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6255 == TEMPLATE_ID_EXPR
)))
6256 permerror (input_location
, TYPE_P (new_scope
)
6257 ? G_("%qT is not a template")
6258 : G_("%qD is not a template"),
6260 /* If it is a class scope, try to complete it; we are about to
6261 be looking up names inside the class. */
6262 if (TYPE_P (new_scope
)
6263 /* Since checking types for dependency can be expensive,
6264 avoid doing it if the type is already complete. */
6265 && !COMPLETE_TYPE_P (new_scope
)
6266 /* Do not try to complete dependent types. */
6267 && !dependent_type_p (new_scope
))
6269 new_scope
= complete_type (new_scope
);
6270 /* If it is a typedef to current class, use the current
6271 class instead, as the typedef won't have any names inside
6273 if (!COMPLETE_TYPE_P (new_scope
)
6274 && currently_open_class (new_scope
))
6275 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6277 /* Make sure we look in the right scope the next time through
6279 parser
->scope
= new_scope
;
6282 /* If parsing tentatively, replace the sequence of tokens that makes
6283 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6284 token. That way, should we re-parse the token stream, we will
6285 not have to repeat the effort required to do the parse, nor will
6286 we issue duplicate error messages. */
6287 if (success
&& start
)
6291 token
= cp_lexer_token_at (parser
->lexer
, start
);
6292 /* Reset the contents of the START token. */
6293 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6294 /* Retrieve any deferred checks. Do not pop this access checks yet
6295 so the memory will not be reclaimed during token replacing below. */
6296 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6297 token
->u
.tree_check_value
->value
= parser
->scope
;
6298 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6299 token
->u
.tree_check_value
->qualifying_scope
=
6300 parser
->qualifying_scope
;
6301 token
->keyword
= RID_MAX
;
6303 /* Purge all subsequent tokens. */
6304 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6308 pop_to_parent_deferring_access_checks ();
6310 return success
? parser
->scope
: NULL_TREE
;
6313 /* Parse a nested-name-specifier. See
6314 cp_parser_nested_name_specifier_opt for details. This function
6315 behaves identically, except that it will an issue an error if no
6316 nested-name-specifier is present. */
6319 cp_parser_nested_name_specifier (cp_parser
*parser
,
6320 bool typename_keyword_p
,
6321 bool check_dependency_p
,
6323 bool is_declaration
)
6327 /* Look for the nested-name-specifier. */
6328 scope
= cp_parser_nested_name_specifier_opt (parser
,
6333 /* If it was not present, issue an error message. */
6336 cp_parser_error (parser
, "expected nested-name-specifier");
6337 parser
->scope
= NULL_TREE
;
6343 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6344 this is either a class-name or a namespace-name (which corresponds
6345 to the class-or-namespace-name production in the grammar). For
6346 C++0x, it can also be a type-name that refers to an enumeration
6347 type or a simple-template-id.
6349 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6350 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6351 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6352 TYPE_P is TRUE iff the next name should be taken as a class-name,
6353 even the same name is declared to be another entity in the same
6356 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6357 specified by the class-or-namespace-name. If neither is found the
6358 ERROR_MARK_NODE is returned. */
6361 cp_parser_qualifying_entity (cp_parser
*parser
,
6362 bool typename_keyword_p
,
6363 bool template_keyword_p
,
6364 bool check_dependency_p
,
6366 bool is_declaration
)
6369 tree saved_qualifying_scope
;
6370 tree saved_object_scope
;
6373 bool successful_parse_p
;
6375 /* DR 743: decltype can appear in a nested-name-specifier. */
6376 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6378 scope
= cp_parser_decltype (parser
);
6379 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6380 && !MAYBE_CLASS_TYPE_P (scope
))
6382 cp_parser_simulate_error (parser
);
6383 return error_mark_node
;
6385 if (TYPE_NAME (scope
))
6386 scope
= TYPE_NAME (scope
);
6390 /* Before we try to parse the class-name, we must save away the
6391 current PARSER->SCOPE since cp_parser_class_name will destroy
6393 saved_scope
= parser
->scope
;
6394 saved_qualifying_scope
= parser
->qualifying_scope
;
6395 saved_object_scope
= parser
->object_scope
;
6396 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6397 there is no need to look for a namespace-name. */
6398 only_class_p
= template_keyword_p
6399 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6401 cp_parser_parse_tentatively (parser
);
6402 scope
= cp_parser_class_name (parser
,
6405 type_p
? class_type
: none_type
,
6407 /*class_head_p=*/false,
6409 /*enum_ok=*/cxx_dialect
> cxx98
);
6410 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6411 /* If that didn't work, try for a namespace-name. */
6412 if (!only_class_p
&& !successful_parse_p
)
6414 /* Restore the saved scope. */
6415 parser
->scope
= saved_scope
;
6416 parser
->qualifying_scope
= saved_qualifying_scope
;
6417 parser
->object_scope
= saved_object_scope
;
6418 /* If we are not looking at an identifier followed by the scope
6419 resolution operator, then this is not part of a
6420 nested-name-specifier. (Note that this function is only used
6421 to parse the components of a nested-name-specifier.) */
6422 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6423 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6424 return error_mark_node
;
6425 scope
= cp_parser_namespace_name (parser
);
6431 /* Return true if we are looking at a compound-literal, false otherwise. */
6434 cp_parser_compound_literal_p (cp_parser
*parser
)
6436 cp_lexer_save_tokens (parser
->lexer
);
6438 /* Skip tokens until the next token is a closing parenthesis.
6439 If we find the closing `)', and the next token is a `{', then
6440 we are looking at a compound-literal. */
6441 bool compound_literal_p
6442 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6443 /*consume_paren=*/true)
6444 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6446 /* Roll back the tokens we skipped. */
6447 cp_lexer_rollback_tokens (parser
->lexer
);
6449 return compound_literal_p
;
6452 /* Parse a postfix-expression.
6456 postfix-expression [ expression ]
6457 postfix-expression ( expression-list [opt] )
6458 simple-type-specifier ( expression-list [opt] )
6459 typename :: [opt] nested-name-specifier identifier
6460 ( expression-list [opt] )
6461 typename :: [opt] nested-name-specifier template [opt] template-id
6462 ( expression-list [opt] )
6463 postfix-expression . template [opt] id-expression
6464 postfix-expression -> template [opt] id-expression
6465 postfix-expression . pseudo-destructor-name
6466 postfix-expression -> pseudo-destructor-name
6467 postfix-expression ++
6468 postfix-expression --
6469 dynamic_cast < type-id > ( expression )
6470 static_cast < type-id > ( expression )
6471 reinterpret_cast < type-id > ( expression )
6472 const_cast < type-id > ( expression )
6473 typeid ( expression )
6479 ( type-id ) { initializer-list , [opt] }
6481 This extension is a GNU version of the C99 compound-literal
6482 construct. (The C99 grammar uses `type-name' instead of `type-id',
6483 but they are essentially the same concept.)
6485 If ADDRESS_P is true, the postfix expression is the operand of the
6486 `&' operator. CAST_P is true if this expression is the target of a
6489 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6490 class member access expressions [expr.ref].
6492 Returns a representation of the expression. */
6495 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6496 bool member_access_only_p
, bool decltype_p
,
6497 cp_id_kind
* pidk_return
)
6502 cp_id_kind idk
= CP_ID_KIND_NONE
;
6503 cp_expr postfix_expression
= NULL_TREE
;
6504 bool is_member_access
= false;
6505 int saved_in_statement
= -1;
6507 /* Peek at the next token. */
6508 token
= cp_lexer_peek_token (parser
->lexer
);
6509 loc
= token
->location
;
6510 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6512 /* Some of the productions are determined by keywords. */
6513 keyword
= token
->keyword
;
6523 const char *saved_message
;
6524 bool saved_in_type_id_in_expr_p
;
6526 /* All of these can be handled in the same way from the point
6527 of view of parsing. Begin by consuming the token
6528 identifying the cast. */
6529 cp_lexer_consume_token (parser
->lexer
);
6531 /* New types cannot be defined in the cast. */
6532 saved_message
= parser
->type_definition_forbidden_message
;
6533 parser
->type_definition_forbidden_message
6534 = G_("types may not be defined in casts");
6536 /* Look for the opening `<'. */
6537 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6538 /* Parse the type to which we are casting. */
6539 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6540 parser
->in_type_id_in_expr_p
= true;
6541 type
= cp_parser_type_id (parser
);
6542 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6543 /* Look for the closing `>'. */
6544 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6545 /* Restore the old message. */
6546 parser
->type_definition_forbidden_message
= saved_message
;
6548 bool saved_greater_than_is_operator_p
6549 = parser
->greater_than_is_operator_p
;
6550 parser
->greater_than_is_operator_p
= true;
6552 /* And the expression which is being cast. */
6553 matching_parens parens
;
6554 parens
.require_open (parser
);
6555 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6556 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6558 location_t end_loc
= close_paren
?
6559 close_paren
->location
: UNKNOWN_LOCATION
;
6561 parser
->greater_than_is_operator_p
6562 = saved_greater_than_is_operator_p
;
6564 /* Only type conversions to integral or enumeration types
6565 can be used in constant-expressions. */
6566 if (!cast_valid_in_integral_constant_expression_p (type
)
6567 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6569 postfix_expression
= error_mark_node
;
6577 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6581 = build_static_cast (type
, expression
, tf_warning_or_error
);
6585 = build_reinterpret_cast (type
, expression
,
6586 tf_warning_or_error
);
6590 = build_const_cast (type
, expression
, tf_warning_or_error
);
6596 /* Construct a location e.g. :
6597 reinterpret_cast <int *> (expr)
6598 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6599 ranging from the start of the "*_cast" token to the final closing
6600 paren, with the caret at the start. */
6601 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6602 postfix_expression
.set_location (cp_cast_loc
);
6609 const char *saved_message
;
6610 bool saved_in_type_id_in_expr_p
;
6612 /* Consume the `typeid' token. */
6613 cp_lexer_consume_token (parser
->lexer
);
6614 /* Look for the `(' token. */
6615 matching_parens parens
;
6616 parens
.require_open (parser
);
6617 /* Types cannot be defined in a `typeid' expression. */
6618 saved_message
= parser
->type_definition_forbidden_message
;
6619 parser
->type_definition_forbidden_message
6620 = G_("types may not be defined in a %<typeid%> expression");
6621 /* We can't be sure yet whether we're looking at a type-id or an
6623 cp_parser_parse_tentatively (parser
);
6624 /* Try a type-id first. */
6625 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6626 parser
->in_type_id_in_expr_p
= true;
6627 type
= cp_parser_type_id (parser
);
6628 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6629 /* Look for the `)' token. Otherwise, we can't be sure that
6630 we're not looking at an expression: consider `typeid (int
6631 (3))', for example. */
6632 cp_token
*close_paren
= parens
.require_close (parser
);
6633 /* If all went well, simply lookup the type-id. */
6634 if (cp_parser_parse_definitely (parser
))
6635 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6636 /* Otherwise, fall back to the expression variant. */
6641 /* Look for an expression. */
6642 expression
= cp_parser_expression (parser
, & idk
);
6643 /* Compute its typeid. */
6644 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6645 /* Look for the `)' token. */
6646 close_paren
= parens
.require_close (parser
);
6648 /* Restore the saved message. */
6649 parser
->type_definition_forbidden_message
= saved_message
;
6650 /* `typeid' may not appear in an integral constant expression. */
6651 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6652 postfix_expression
= error_mark_node
;
6654 /* Construct a location e.g. :
6657 ranging from the start of the "typeid" token to the final closing
6658 paren, with the caret at the start. */
6661 location_t typeid_loc
6662 = make_location (start_loc
, start_loc
, close_paren
->location
);
6663 postfix_expression
.set_location (typeid_loc
);
6671 /* The syntax permitted here is the same permitted for an
6672 elaborated-type-specifier. */
6673 ++parser
->prevent_constrained_type_specifiers
;
6674 type
= cp_parser_elaborated_type_specifier (parser
,
6675 /*is_friend=*/false,
6676 /*is_declaration=*/false);
6677 --parser
->prevent_constrained_type_specifiers
;
6678 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6682 case RID_CILK_SPAWN
:
6684 location_t cilk_spawn_loc
6685 = cp_lexer_peek_token (parser
->lexer
)->location
;
6686 cp_lexer_consume_token (parser
->lexer
);
6687 token
= cp_lexer_peek_token (parser
->lexer
);
6688 if (token
->type
== CPP_SEMICOLON
)
6690 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6692 postfix_expression
= error_mark_node
;
6695 else if (!current_function_decl
)
6697 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6698 "inside a function");
6699 postfix_expression
= error_mark_node
;
6704 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6705 saved_in_statement
= parser
->in_statement
;
6706 parser
->in_statement
|= IN_CILK_SPAWN
;
6708 cfun
->calls_cilk_spawn
= 1;
6709 postfix_expression
=
6710 cp_parser_postfix_expression (parser
, false, false,
6711 false, false, &idk
);
6714 error_at (token
->location
, "-fcilkplus must be enabled to use"
6715 " %<_Cilk_spawn%>");
6716 cfun
->calls_cilk_spawn
= 0;
6718 else if (saved_in_statement
& IN_CILK_SPAWN
)
6720 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6721 "are not permitted");
6722 postfix_expression
= error_mark_node
;
6723 cfun
->calls_cilk_spawn
= 0;
6727 location_t loc
= postfix_expression
.get_location ();
6728 postfix_expression
= build_cilk_spawn (token
->location
,
6729 postfix_expression
);
6730 /* Build a location of the form:
6733 with caret at the expr, ranging from the start of the
6734 _Cilk_spawn token to the end of the expression. */
6735 location_t combined_loc
=
6736 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6737 postfix_expression
.set_location (combined_loc
);
6738 if (postfix_expression
!= error_mark_node
)
6739 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6740 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6746 case RID_BUILTIN_SHUFFLE
:
6747 case RID_BUILTIN_LAUNDER
:
6749 vec
<tree
, va_gc
> *vec
;
6753 cp_lexer_consume_token (parser
->lexer
);
6754 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6755 /*cast_p=*/false, /*allow_expansion_p=*/true,
6756 /*non_constant_p=*/NULL
);
6759 postfix_expression
= error_mark_node
;
6763 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6769 if (vec
->length () == 1)
6771 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6774 error_at (loc
, "wrong number of arguments to "
6775 "%<__builtin_addressof%>");
6776 postfix_expression
= error_mark_node
;
6780 case RID_BUILTIN_LAUNDER
:
6781 if (vec
->length () == 1)
6782 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6783 tf_warning_or_error
);
6786 error_at (loc
, "wrong number of arguments to "
6787 "%<__builtin_launder%>");
6788 postfix_expression
= error_mark_node
;
6792 case RID_BUILTIN_SHUFFLE
:
6793 if (vec
->length () == 2)
6795 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6796 (*vec
)[1], tf_warning_or_error
);
6797 else if (vec
->length () == 3)
6799 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6800 (*vec
)[2], tf_warning_or_error
);
6803 error_at (loc
, "wrong number of arguments to "
6804 "%<__builtin_shuffle%>");
6805 postfix_expression
= error_mark_node
;
6819 /* If the next thing is a simple-type-specifier, we may be
6820 looking at a functional cast. We could also be looking at
6821 an id-expression. So, we try the functional cast, and if
6822 that doesn't work we fall back to the primary-expression. */
6823 cp_parser_parse_tentatively (parser
);
6824 /* Look for the simple-type-specifier. */
6825 ++parser
->prevent_constrained_type_specifiers
;
6826 type
= cp_parser_simple_type_specifier (parser
,
6827 /*decl_specs=*/NULL
,
6828 CP_PARSER_FLAGS_NONE
);
6829 --parser
->prevent_constrained_type_specifiers
;
6830 /* Parse the cast itself. */
6831 if (!cp_parser_error_occurred (parser
))
6833 = cp_parser_functional_cast (parser
, type
);
6834 /* If that worked, we're done. */
6835 if (cp_parser_parse_definitely (parser
))
6838 /* If the functional-cast didn't work out, try a
6839 compound-literal. */
6840 if (cp_parser_allow_gnu_extensions_p (parser
)
6841 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6843 cp_expr initializer
= NULL_TREE
;
6845 cp_parser_parse_tentatively (parser
);
6847 matching_parens parens
;
6848 parens
.consume_open (parser
);
6850 /* Avoid calling cp_parser_type_id pointlessly, see comment
6851 in cp_parser_cast_expression about c++/29234. */
6852 if (!cp_parser_compound_literal_p (parser
))
6853 cp_parser_simulate_error (parser
);
6856 /* Parse the type. */
6857 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6858 parser
->in_type_id_in_expr_p
= true;
6859 type
= cp_parser_type_id (parser
);
6860 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6861 parens
.require_close (parser
);
6864 /* If things aren't going well, there's no need to
6866 if (!cp_parser_error_occurred (parser
))
6868 bool non_constant_p
;
6869 /* Parse the brace-enclosed initializer list. */
6870 initializer
= cp_parser_braced_list (parser
,
6873 /* If that worked, we're definitely looking at a
6874 compound-literal expression. */
6875 if (cp_parser_parse_definitely (parser
))
6877 /* Warn the user that a compound literal is not
6878 allowed in standard C++. */
6879 pedwarn (input_location
, OPT_Wpedantic
,
6880 "ISO C++ forbids compound-literals");
6881 /* For simplicity, we disallow compound literals in
6882 constant-expressions. We could
6883 allow compound literals of integer type, whose
6884 initializer was a constant, in constant
6885 expressions. Permitting that usage, as a further
6886 extension, would not change the meaning of any
6887 currently accepted programs. (Of course, as
6888 compound literals are not part of ISO C++, the
6889 standard has nothing to say.) */
6890 if (cp_parser_non_integral_constant_expression (parser
,
6893 postfix_expression
= error_mark_node
;
6896 /* Form the representation of the compound-literal. */
6898 = finish_compound_literal (type
, initializer
,
6899 tf_warning_or_error
, fcl_c99
);
6900 postfix_expression
.set_location (initializer
.get_location ());
6905 /* It must be a primary-expression. */
6907 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6908 /*template_arg_p=*/false,
6915 /* Note that we don't need to worry about calling build_cplus_new on a
6916 class-valued CALL_EXPR in decltype when it isn't the end of the
6917 postfix-expression; unary_complex_lvalue will take care of that for
6920 /* Keep looping until the postfix-expression is complete. */
6923 if (idk
== CP_ID_KIND_UNQUALIFIED
6924 && identifier_p (postfix_expression
)
6925 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6926 /* It is not a Koenig lookup function call. */
6928 = unqualified_name_lookup_error (postfix_expression
);
6930 /* Peek at the next token. */
6931 token
= cp_lexer_peek_token (parser
->lexer
);
6933 switch (token
->type
)
6935 case CPP_OPEN_SQUARE
:
6936 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6938 cp_parser_error (parser
,
6939 "two consecutive %<[%> shall "
6940 "only introduce an attribute");
6941 return error_mark_node
;
6944 = cp_parser_postfix_open_square_expression (parser
,
6948 postfix_expression
.set_range (start_loc
,
6949 postfix_expression
.get_location ());
6951 idk
= CP_ID_KIND_NONE
;
6952 is_member_access
= false;
6955 case CPP_OPEN_PAREN
:
6956 /* postfix-expression ( expression-list [opt] ) */
6959 bool is_builtin_constant_p
;
6960 bool saved_integral_constant_expression_p
= false;
6961 bool saved_non_integral_constant_expression_p
= false;
6962 tsubst_flags_t complain
= complain_flags (decltype_p
);
6963 vec
<tree
, va_gc
> *args
;
6964 location_t close_paren_loc
= UNKNOWN_LOCATION
;
6966 is_member_access
= false;
6968 is_builtin_constant_p
6969 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6970 if (is_builtin_constant_p
)
6972 /* The whole point of __builtin_constant_p is to allow
6973 non-constant expressions to appear as arguments. */
6974 saved_integral_constant_expression_p
6975 = parser
->integral_constant_expression_p
;
6976 saved_non_integral_constant_expression_p
6977 = parser
->non_integral_constant_expression_p
;
6978 parser
->integral_constant_expression_p
= false;
6980 args
= (cp_parser_parenthesized_expression_list
6982 /*cast_p=*/false, /*allow_expansion_p=*/true,
6983 /*non_constant_p=*/NULL
,
6984 /*close_paren_loc=*/&close_paren_loc
));
6985 if (is_builtin_constant_p
)
6987 parser
->integral_constant_expression_p
6988 = saved_integral_constant_expression_p
;
6989 parser
->non_integral_constant_expression_p
6990 = saved_non_integral_constant_expression_p
;
6995 postfix_expression
= error_mark_node
;
6999 /* Function calls are not permitted in
7000 constant-expressions. */
7001 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7002 && cp_parser_non_integral_constant_expression (parser
,
7005 postfix_expression
= error_mark_node
;
7006 release_tree_vector (args
);
7011 if (idk
== CP_ID_KIND_UNQUALIFIED
7012 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7014 if (identifier_p (postfix_expression
))
7016 if (!args
->is_empty ())
7019 if (!any_type_dependent_arguments_p (args
))
7021 = perform_koenig_lookup (postfix_expression
, args
,
7026 = unqualified_fn_lookup_error (postfix_expression
);
7028 /* We do not perform argument-dependent lookup if
7029 normal lookup finds a non-function, in accordance
7030 with the expected resolution of DR 218. */
7031 else if (!args
->is_empty ()
7032 && is_overloaded_fn (postfix_expression
))
7034 tree fn
= get_first_fn (postfix_expression
);
7035 fn
= STRIP_TEMPLATE (fn
);
7037 /* Do not do argument dependent lookup if regular
7038 lookup finds a member function or a block-scope
7039 function declaration. [basic.lookup.argdep]/3 */
7040 if (!DECL_FUNCTION_MEMBER_P (fn
)
7041 && !DECL_LOCAL_FUNCTION_P (fn
))
7044 if (!any_type_dependent_arguments_p (args
))
7046 = perform_koenig_lookup (postfix_expression
, args
,
7052 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7053 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7054 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7055 && vec_safe_length (args
) == 3)
7057 tree arg0
= (*args
)[0];
7058 tree arg1
= (*args
)[1];
7059 tree arg2
= (*args
)[2];
7060 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
7061 | (!!integer_zerop (arg2
) << 2));
7062 if (TREE_CODE (arg2
) == CONST_DECL
)
7063 arg2
= DECL_INITIAL (arg2
);
7064 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7067 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7069 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7070 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7072 if (processing_template_decl
7073 && (type_dependent_object_expression_p (instance
)
7074 || (!BASELINK_P (fn
)
7075 && TREE_CODE (fn
) != FIELD_DECL
)
7076 || type_dependent_expression_p (fn
)
7077 || any_type_dependent_arguments_p (args
)))
7079 maybe_generic_this_capture (instance
, fn
);
7081 = build_min_nt_call_vec (postfix_expression
, args
);
7082 release_tree_vector (args
);
7086 if (BASELINK_P (fn
))
7089 = (build_new_method_call
7090 (instance
, fn
, &args
, NULL_TREE
,
7091 (idk
== CP_ID_KIND_QUALIFIED
7092 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7099 = finish_call_expr (postfix_expression
, &args
,
7100 /*disallow_virtual=*/false,
7104 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7105 || TREE_CODE (postfix_expression
) == MEMBER_REF
7106 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7107 postfix_expression
= (build_offset_ref_call_from_tree
7108 (postfix_expression
, &args
,
7110 else if (idk
== CP_ID_KIND_QUALIFIED
)
7111 /* A call to a static class member, or a namespace-scope
7114 = finish_call_expr (postfix_expression
, &args
,
7115 /*disallow_virtual=*/true,
7119 /* All other function calls. */
7121 = finish_call_expr (postfix_expression
, &args
,
7122 /*disallow_virtual=*/false,
7126 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7128 location_t combined_loc
= make_location (token
->location
,
7131 postfix_expression
.set_location (combined_loc
);
7134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7135 idk
= CP_ID_KIND_NONE
;
7137 release_tree_vector (args
);
7143 /* postfix-expression . template [opt] id-expression
7144 postfix-expression . pseudo-destructor-name
7145 postfix-expression -> template [opt] id-expression
7146 postfix-expression -> pseudo-destructor-name */
7148 /* Consume the `.' or `->' operator. */
7149 cp_lexer_consume_token (parser
->lexer
);
7152 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7156 is_member_access
= true;
7160 /* postfix-expression ++ */
7161 /* Consume the `++' token. */
7162 cp_lexer_consume_token (parser
->lexer
);
7163 /* Generate a representation for the complete expression. */
7165 = finish_increment_expr (postfix_expression
,
7166 POSTINCREMENT_EXPR
);
7167 /* Increments may not appear in constant-expressions. */
7168 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7169 postfix_expression
= error_mark_node
;
7170 idk
= CP_ID_KIND_NONE
;
7171 is_member_access
= false;
7174 case CPP_MINUS_MINUS
:
7175 /* postfix-expression -- */
7176 /* Consume the `--' token. */
7177 cp_lexer_consume_token (parser
->lexer
);
7178 /* Generate a representation for the complete expression. */
7180 = finish_increment_expr (postfix_expression
,
7181 POSTDECREMENT_EXPR
);
7182 /* Decrements may not appear in constant-expressions. */
7183 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7184 postfix_expression
= error_mark_node
;
7185 idk
= CP_ID_KIND_NONE
;
7186 is_member_access
= false;
7190 if (pidk_return
!= NULL
)
7191 * pidk_return
= idk
;
7192 if (member_access_only_p
)
7193 return is_member_access
7194 ? postfix_expression
7195 : cp_expr (error_mark_node
);
7197 return postfix_expression
;
7201 /* We should never get here. */
7203 return error_mark_node
;
7206 /* This function parses Cilk Plus array notations. If a normal array expr. is
7207 parsed then the array index is passed back to the caller through *INIT_INDEX
7208 and the function returns a NULL_TREE. If array notation expr. is parsed,
7209 then *INIT_INDEX is ignored by the caller and the function returns
7210 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7214 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
7217 cp_token
*token
= NULL
;
7218 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
7219 if (!array_value
|| array_value
== error_mark_node
)
7221 cp_parser_skip_to_end_of_statement (parser
);
7222 return error_mark_node
;
7225 array_type
= TREE_TYPE (array_value
);
7227 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7228 parser
->colon_corrects_to_scope_p
= false;
7229 token
= cp_lexer_peek_token (parser
->lexer
);
7233 cp_parser_error (parser
, "expected %<:%> or numeral");
7234 return error_mark_node
;
7236 else if (token
->type
== CPP_COLON
)
7238 /* Consume the ':'. */
7239 cp_lexer_consume_token (parser
->lexer
);
7241 /* If we are here, then we have a case like this A[:]. */
7242 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7244 cp_parser_error (parser
, "expected %<]%>");
7245 cp_parser_skip_to_end_of_statement (parser
);
7246 return error_mark_node
;
7248 *init_index
= NULL_TREE
;
7250 length_index
= NULL_TREE
;
7254 /* If we are here, then there are three valid possibilities:
7256 2. ARRAY [ EXP : EXP ]
7257 3. ARRAY [ EXP : EXP : EXP ] */
7259 *init_index
= cp_parser_expression (parser
);
7260 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7262 /* This indicates that we have a normal array expression. */
7263 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7267 /* Consume the ':'. */
7268 cp_lexer_consume_token (parser
->lexer
);
7269 length_index
= cp_parser_expression (parser
);
7270 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7272 cp_lexer_consume_token (parser
->lexer
);
7273 stride
= cp_parser_expression (parser
);
7276 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7278 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7279 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7281 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7282 cp_lexer_consume_token (parser
->lexer
);
7283 return error_mark_node
;
7285 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7287 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7288 length_index
, stride
, array_type
);
7292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7293 by cp_parser_builtin_offsetof. We're looking for
7295 postfix-expression [ expression ]
7296 postfix-expression [ braced-init-list ] (C++11)
7298 FOR_OFFSETOF is set if we're being called in that context, which
7299 changes how we deal with integer constant expressions. */
7302 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7303 tree postfix_expression
,
7307 tree index
= NULL_TREE
;
7308 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7309 bool saved_greater_than_is_operator_p
;
7311 /* Consume the `[' token. */
7312 cp_lexer_consume_token (parser
->lexer
);
7314 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7315 parser
->greater_than_is_operator_p
= true;
7317 /* Parse the index expression. */
7318 /* ??? For offsetof, there is a question of what to allow here. If
7319 offsetof is not being used in an integral constant expression context,
7320 then we *could* get the right answer by computing the value at runtime.
7321 If we are in an integral constant expression context, then we might
7322 could accept any constant expression; hard to say without analysis.
7323 Rather than open the barn door too wide right away, allow only integer
7324 constant expressions here. */
7326 index
= cp_parser_constant_expression (parser
);
7329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7331 bool expr_nonconst_p
;
7332 cp_lexer_set_source_position (parser
->lexer
);
7333 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7334 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7336 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7338 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7339 "braced list index is not allowed with array "
7341 cp_parser_skip_to_end_of_statement (parser
);
7342 return error_mark_node
;
7345 else if (flag_cilkplus
)
7347 /* Here are have these two options:
7348 ARRAY[EXP : EXP] - Array notation expr with default
7350 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7352 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7353 postfix_expression
);
7358 index
= cp_parser_expression (parser
);
7361 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7373 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7374 postfix_expression
= error_mark_node
;
7376 return postfix_expression
;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7393 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7394 enum cpp_ttype token_type
,
7395 cp_expr postfix_expression
,
7396 bool for_offsetof
, cp_id_kind
*idk
,
7397 location_t location
)
7401 bool pseudo_destructor_p
;
7402 tree scope
= NULL_TREE
;
7403 location_t start_loc
= postfix_expression
.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type
== CPP_DEREF
)
7407 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7408 tf_warning_or_error
);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser
->scope
= NULL_TREE
;
7414 parser
->qualifying_scope
= NULL_TREE
;
7415 parser
->object_scope
= NULL_TREE
;
7416 *idk
= CP_ID_KIND_NONE
;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7422 scope
= TREE_TYPE (postfix_expression
);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope
= non_reference (scope
);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression
!= current_class_ref
7435 && scope
!= error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope
, current_class_type
))))
7441 scope
= complete_type (scope
);
7442 if (!COMPLETE_TYPE_P (scope
)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression
))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind
= (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope
)
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression
),
7454 postfix_expression
, scope
, kind
);
7455 if (!MAYBE_CLASS_TYPE_P (scope
))
7456 return error_mark_node
;
7457 if (processing_template_decl
)
7460 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser
->context
->object_type
= scope
;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7474 scope
= error_mark_node
;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7478 if (scope
== error_mark_node
)
7479 postfix_expression
= error_mark_node
;
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7486 parser
->context
->object_type
= unknown_type_node
;
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p
= false;
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7500 cp_parser_parse_tentatively (parser
);
7501 /* Parse the pseudo-destructor-name. */
7503 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7506 && (cp_parser_error_occurred (parser
)
7507 || !SCALAR_TYPE_P (type
)))
7508 cp_parser_abort_tentative_parse (parser
);
7509 else if (cp_parser_parse_definitely (parser
))
7511 pseudo_destructor_p
= true;
7513 = finish_pseudo_destructor_expr (postfix_expression
,
7518 if (!pseudo_destructor_p
)
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7524 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7525 /* Parse the id-expression. */
7526 name
= (cp_parser_id_expression
7528 cp_parser_optional_template_keyword (parser
),
7529 /*check_dependency_p=*/true,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7546 *idk
= CP_ID_KIND_QUALIFIED
;
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name
) == TYPE_DECL
)
7552 error_at (token
->location
, "invalid use of %qD", name
);
7553 postfix_expression
= error_mark_node
;
7557 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7559 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7561 error_at (token
->location
, "%<%D::%D%> is not a class member",
7562 parser
->scope
, name
);
7563 postfix_expression
= error_mark_node
;
7566 name
= build_qualified_name (/*type=*/NULL_TREE
,
7570 parser
->scope
= NULL_TREE
;
7571 parser
->qualifying_scope
= NULL_TREE
;
7572 parser
->object_scope
= NULL_TREE
;
7574 if (parser
->scope
&& name
&& BASELINK_P (name
))
7575 adjust_result_of_qualified_name_lookup
7576 (name
, parser
->scope
, scope
);
7578 = finish_class_member_access_expr (postfix_expression
, name
,
7580 tf_warning_or_error
);
7581 /* Build a location e.g.:
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7587 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7588 location_t combined_loc
7589 = make_location (input_location
, start_loc
, end_loc
);
7590 protected_set_expr_location (postfix_expression
, combined_loc
);
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser
->context
->object_type
= NULL_TREE
;
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7601 && (cp_parser_non_integral_constant_expression
7602 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7603 postfix_expression
= error_mark_node
;
7605 return postfix_expression
;
7608 /* Parse a parenthesized expression-list.
7611 assignment-expression
7612 expression-list, assignment-expression
7617 identifier, expression-list
7619 CAST_P is true if this expression is the target of a cast.
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7637 static vec
<tree
, va_gc
> *
7638 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7639 int is_attribute_list
,
7641 bool allow_expansion_p
,
7642 bool *non_constant_p
,
7643 location_t
*close_paren_loc
)
7645 vec
<tree
, va_gc
> *expression_list
;
7646 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7647 tree identifier
= NULL_TREE
;
7648 bool saved_greater_than_is_operator_p
;
7650 /* Assume all the expressions will be constant. */
7652 *non_constant_p
= false;
7654 matching_parens parens
;
7655 if (!parens
.require_open (parser
))
7658 expression_list
= make_tree_vector ();
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser
->greater_than_is_operator_p
;
7664 parser
->greater_than_is_operator_p
= true;
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list
== id_attr
7675 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7679 /* Consume the identifier. */
7680 token
= cp_lexer_consume_token (parser
->lexer
);
7681 /* Save the identifier. */
7682 identifier
= token
->u
.value
;
7686 bool expr_non_constant_p
;
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser
->lexer
);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7694 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7695 if (non_constant_p
&& expr_non_constant_p
)
7696 *non_constant_p
= true;
7698 else if (non_constant_p
)
7700 expr
= (cp_parser_constant_expression
7701 (parser
, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p
));
7703 if (expr_non_constant_p
)
7704 *non_constant_p
= true;
7707 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7711 expr
= instantiate_non_dependent_expr (expr
);
7713 /* If we have an ellipsis, then this is an expression
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser
->lexer
);
7721 /* Build the argument pack. */
7722 expr
= make_pack_expansion (expr
);
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list
, expr
);
7731 if (expr
== error_mark_node
)
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list
= non_attr
;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser
->lexer
);
7748 if (close_paren_loc
)
7749 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7751 if (!parens
.require_close (parser
))
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7759 /*recovering=*/true,
7761 /*consume_paren=*/true);
7766 parser
->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p
;
7772 parser
->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p
;
7776 vec_safe_insert (expression_list
, 0, identifier
);
7778 return expression_list
;
7781 /* Parse a pseudo-destructor-name.
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7794 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7799 bool nested_name_specifier_p
;
7802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7803 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7804 && !type_dependent_expression_p (object
))
7806 if (cxx_dialect
< cxx14
)
7807 pedwarn (input_location
, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser
->lexer
);
7811 cp_lexer_consume_token (parser
->lexer
);
7813 *type
= TREE_TYPE (object
);
7817 /* Assume that things will not work out. */
7818 *type
= error_mark_node
;
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser
,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7828 /*is_declaration=*/false)
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser
->lexer
);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser
,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7857 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7860 cp_parser_error (parser
, "non-scalar type");
7864 /* Look for the type-name. */
7865 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7866 if (*scope
== error_mark_node
)
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7875 /* Look for the `~'. */
7876 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7880 cp_parser_commit_to_topmost_tentative_parse (parser
);
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7887 /* Parse a unary-expression.
7893 unary-operator cast-expression
7894 sizeof unary-expression
7896 alignof ( type-id ) [C++0x]
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7918 Returns a representation of the expression. */
7921 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7922 bool address_p
, bool cast_p
, bool decltype_p
)
7925 enum tree_code unary_operator
;
7927 /* Peek at the next token. */
7928 token
= cp_lexer_peek_token (parser
->lexer
);
7929 /* Some keywords give away the kind of expression. */
7930 if (token
->type
== CPP_KEYWORD
)
7932 enum rid keyword
= token
->keyword
;
7941 location_t start_loc
= token
->location
;
7943 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser
->lexer
);
7946 /* Parse the operand. */
7947 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7949 if (TYPE_P (operand
))
7950 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token
->u
.value
, "alignof"))
7957 pedwarn (token
->location
, OPT_Wpedantic
,
7958 "ISO C++ does not allow %<alignof%> "
7961 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7967 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7969 if (!processing_template_decl
&& TYPE_P (operand
))
7971 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7972 build1 (NOP_EXPR
, operand
,
7974 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7977 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7978 TREE_SIDE_EFFECTS (ret
) = 0;
7979 TREE_READONLY (ret
) = 1;
7983 /* Construct a location e.g. :
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser
->lexer
)->location
;
7990 location_t compound_loc
7991 = make_location (start_loc
, start_loc
, finish_loc
);
7993 cp_expr
ret_expr (ret
);
7994 ret_expr
.set_location (compound_loc
);
7999 return cp_parser_new_expression (parser
);
8002 return cp_parser_delete_expression (parser
);
8006 /* The saved value of the PEDANTIC flag. */
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser
, &saved_pedantic
);
8012 /* Parse the cast-expression. */
8013 expr
= cp_parser_simple_cast_expression (parser
);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic
= saved_pedantic
;
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser
->lexer
);
8027 /* Parse the cast-expression. */
8028 expression
= cp_parser_simple_cast_expression (parser
);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token
->location
,
8031 (keyword
== RID_REALPART
8032 ? REALPART_EXPR
: IMAGPART_EXPR
),
8034 tf_warning_or_error
);
8038 case RID_TRANSACTION_ATOMIC
:
8039 case RID_TRANSACTION_RELAXED
:
8040 return cp_parser_transaction_expression (parser
, keyword
);
8045 const char *saved_message
;
8046 bool saved_integral_constant_expression_p
;
8047 bool saved_non_integral_constant_expression_p
;
8048 bool saved_greater_than_is_operator_p
;
8050 cp_lexer_consume_token (parser
->lexer
);
8051 matching_parens parens
;
8052 parens
.require_open (parser
);
8054 saved_message
= parser
->type_definition_forbidden_message
;
8055 parser
->type_definition_forbidden_message
8056 = G_("types may not be defined in %<noexcept%> expressions");
8058 saved_integral_constant_expression_p
8059 = parser
->integral_constant_expression_p
;
8060 saved_non_integral_constant_expression_p
8061 = parser
->non_integral_constant_expression_p
;
8062 parser
->integral_constant_expression_p
= false;
8064 saved_greater_than_is_operator_p
8065 = parser
->greater_than_is_operator_p
;
8066 parser
->greater_than_is_operator_p
= true;
8068 ++cp_unevaluated_operand
;
8069 ++c_inhibit_evaluation_warnings
;
8070 ++cp_noexcept_operand
;
8071 expr
= cp_parser_expression (parser
);
8072 --cp_noexcept_operand
;
8073 --c_inhibit_evaluation_warnings
;
8074 --cp_unevaluated_operand
;
8076 parser
->greater_than_is_operator_p
8077 = saved_greater_than_is_operator_p
;
8079 parser
->integral_constant_expression_p
8080 = saved_integral_constant_expression_p
;
8081 parser
->non_integral_constant_expression_p
8082 = saved_non_integral_constant_expression_p
;
8084 parser
->type_definition_forbidden_message
= saved_message
;
8086 parens
.require_close (parser
);
8087 return finish_noexcept_expr (expr
, tf_warning_or_error
);
8095 /* Look for the `:: new' and `:: delete', which also signal the
8096 beginning of a new-expression, or delete-expression,
8097 respectively. If the next token is `::', then it might be one of
8099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8103 /* See if the token after the `::' is one of the keywords in
8104 which we're interested. */
8105 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8106 /* If it's `new', we have a new-expression. */
8107 if (keyword
== RID_NEW
)
8108 return cp_parser_new_expression (parser
);
8109 /* Similarly, for `delete'. */
8110 else if (keyword
== RID_DELETE
)
8111 return cp_parser_delete_expression (parser
);
8114 /* Look for a unary operator. */
8115 unary_operator
= cp_parser_unary_operator (token
);
8116 /* The `++' and `--' operators can be handled similarly, even though
8117 they are not technically unary-operators in the grammar. */
8118 if (unary_operator
== ERROR_MARK
)
8120 if (token
->type
== CPP_PLUS_PLUS
)
8121 unary_operator
= PREINCREMENT_EXPR
;
8122 else if (token
->type
== CPP_MINUS_MINUS
)
8123 unary_operator
= PREDECREMENT_EXPR
;
8124 /* Handle the GNU address-of-label extension. */
8125 else if (cp_parser_allow_gnu_extensions_p (parser
)
8126 && token
->type
== CPP_AND_AND
)
8130 location_t start_loc
= token
->location
;
8132 /* Consume the '&&' token. */
8133 cp_lexer_consume_token (parser
->lexer
);
8134 /* Look for the identifier. */
8135 location_t finish_loc
8136 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8137 identifier
= cp_parser_identifier (parser
);
8138 /* Construct a location of the form:
8141 with caret==start at the "&&", finish at the end of the label. */
8142 location_t combined_loc
8143 = make_location (start_loc
, start_loc
, finish_loc
);
8144 /* Create an expression representing the address. */
8145 expression
= finish_label_address_expr (identifier
, combined_loc
);
8146 if (cp_parser_non_integral_constant_expression (parser
,
8148 expression
= error_mark_node
;
8152 if (unary_operator
!= ERROR_MARK
)
8154 cp_expr cast_expression
;
8155 cp_expr expression
= error_mark_node
;
8156 non_integral_constant non_constant_p
= NIC_NONE
;
8157 location_t loc
= token
->location
;
8158 tsubst_flags_t complain
= complain_flags (decltype_p
);
8160 /* Consume the operator token. */
8161 token
= cp_lexer_consume_token (parser
->lexer
);
8162 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8164 /* Parse the cast-expression. */
8166 = cp_parser_cast_expression (parser
,
8167 unary_operator
== ADDR_EXPR
,
8173 OP_TOKEN CAST_EXPRESSION
8174 ^~~~~~~~~~~~~~~~~~~~~~~~~
8175 with start==caret at the operator token, and
8176 extending to the end of the cast_expression. */
8177 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8179 /* Now, build an appropriate representation. */
8180 switch (unary_operator
)
8183 non_constant_p
= NIC_STAR
;
8184 expression
= build_x_indirect_ref (loc
, cast_expression
,
8187 /* TODO: build_x_indirect_ref does not always honor the
8188 location, so ensure it is set. */
8189 expression
.set_location (loc
);
8193 non_constant_p
= NIC_ADDR
;
8196 expression
= build_x_unary_op (loc
, unary_operator
,
8199 /* TODO: build_x_unary_op does not always honor the location,
8200 so ensure it is set. */
8201 expression
.set_location (loc
);
8204 case PREINCREMENT_EXPR
:
8205 case PREDECREMENT_EXPR
:
8206 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8207 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8210 /* Immediately fold negation of a constant, unless the constant is 0
8211 (since -0 == 0) or it would overflow. */
8212 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8213 && CONSTANT_CLASS_P (cast_expression
)
8214 && !integer_zerop (cast_expression
)
8215 && !TREE_OVERFLOW (cast_expression
))
8217 tree folded
= fold_build1 (unary_operator
,
8218 TREE_TYPE (cast_expression
),
8220 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8222 expression
= cp_expr (folded
, loc
);
8227 case UNARY_PLUS_EXPR
:
8228 case TRUTH_NOT_EXPR
:
8229 expression
= finish_unary_op_expr (loc
, unary_operator
,
8230 cast_expression
, complain
);
8237 if (non_constant_p
!= NIC_NONE
8238 && cp_parser_non_integral_constant_expression (parser
,
8240 expression
= error_mark_node
;
8245 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8246 /*member_access_only_p=*/false,
8251 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8252 unary-operator, the corresponding tree code is returned. */
8254 static enum tree_code
8255 cp_parser_unary_operator (cp_token
* token
)
8257 switch (token
->type
)
8260 return INDIRECT_REF
;
8266 return UNARY_PLUS_EXPR
;
8272 return TRUTH_NOT_EXPR
;
8275 return BIT_NOT_EXPR
;
8282 /* Parse a new-expression.
8285 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8286 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8288 Returns a representation of the expression. */
8291 cp_parser_new_expression (cp_parser
* parser
)
8293 bool global_scope_p
;
8294 vec
<tree
, va_gc
> *placement
;
8296 vec
<tree
, va_gc
> *initializer
;
8297 tree nelts
= NULL_TREE
;
8300 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8302 /* Look for the optional `::' operator. */
8304 = (cp_parser_global_scope_opt (parser
,
8305 /*current_scope_valid_p=*/false)
8307 /* Look for the `new' operator. */
8308 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8309 /* There's no easy way to tell a new-placement from the
8310 `( type-id )' construct. */
8311 cp_parser_parse_tentatively (parser
);
8312 /* Look for a new-placement. */
8313 placement
= cp_parser_new_placement (parser
);
8314 /* If that didn't work out, there's no new-placement. */
8315 if (!cp_parser_parse_definitely (parser
))
8317 if (placement
!= NULL
)
8318 release_tree_vector (placement
);
8322 /* If the next token is a `(', then we have a parenthesized
8324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8327 const char *saved_message
= parser
->type_definition_forbidden_message
;
8329 /* Consume the `('. */
8330 matching_parens parens
;
8331 parens
.consume_open (parser
);
8333 /* Parse the type-id. */
8334 parser
->type_definition_forbidden_message
8335 = G_("types may not be defined in a new-expression");
8337 type_id_in_expr_sentinel
s (parser
);
8338 type
= cp_parser_type_id (parser
);
8340 parser
->type_definition_forbidden_message
= saved_message
;
8342 /* Look for the closing `)'. */
8343 parens
.require_close (parser
);
8344 token
= cp_lexer_peek_token (parser
->lexer
);
8345 /* There should not be a direct-new-declarator in this production,
8346 but GCC used to allowed this, so we check and emit a sensible error
8347 message for this case. */
8348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8350 error_at (token
->location
,
8351 "array bound forbidden after parenthesized type-id");
8352 inform (token
->location
,
8353 "try removing the parentheses around the type-id");
8354 cp_parser_direct_new_declarator (parser
);
8357 /* Otherwise, there must be a new-type-id. */
8359 type
= cp_parser_new_type_id (parser
, &nelts
);
8361 /* If the next token is a `(' or '{', then we have a new-initializer. */
8362 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8363 if (token
->type
== CPP_OPEN_PAREN
8364 || token
->type
== CPP_OPEN_BRACE
)
8365 initializer
= cp_parser_new_initializer (parser
);
8369 /* A new-expression may not appear in an integral constant
8371 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8372 ret
= error_mark_node
;
8373 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8374 of a new-type-id or type-id of a new-expression, the new-expression shall
8375 contain a new-initializer of the form ( assignment-expression )".
8376 Additionally, consistently with the spirit of DR 1467, we want to accept
8377 'new auto { 2 }' too. */
8378 else if ((ret
= type_uses_auto (type
))
8379 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8380 && (vec_safe_length (initializer
) != 1
8381 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8382 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8384 error_at (token
->location
,
8385 "initialization of new-expression for type %<auto%> "
8386 "requires exactly one element");
8387 ret
= error_mark_node
;
8391 /* Construct a location e.g.:
8394 with caret == start at the start of the "new" token, and the end
8395 at the end of the final token we consumed. */
8396 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8397 location_t end_loc
= get_finish (end_tok
->location
);
8398 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8400 /* Create a representation of the new-expression. */
8401 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8402 tf_warning_or_error
);
8403 protected_set_expr_location (ret
, combined_loc
);
8406 if (placement
!= NULL
)
8407 release_tree_vector (placement
);
8408 if (initializer
!= NULL
)
8409 release_tree_vector (initializer
);
8414 /* Parse a new-placement.
8419 Returns the same representation as for an expression-list. */
8421 static vec
<tree
, va_gc
> *
8422 cp_parser_new_placement (cp_parser
* parser
)
8424 vec
<tree
, va_gc
> *expression_list
;
8426 /* Parse the expression-list. */
8427 expression_list
= (cp_parser_parenthesized_expression_list
8428 (parser
, non_attr
, /*cast_p=*/false,
8429 /*allow_expansion_p=*/true,
8430 /*non_constant_p=*/NULL
));
8432 if (expression_list
&& expression_list
->is_empty ())
8433 error ("expected expression-list or type-id");
8435 return expression_list
;
8438 /* Parse a new-type-id.
8441 type-specifier-seq new-declarator [opt]
8443 Returns the TYPE allocated. If the new-type-id indicates an array
8444 type, *NELTS is set to the number of elements in the last array
8445 bound; the TYPE will not include the last array bound. */
8448 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8450 cp_decl_specifier_seq type_specifier_seq
;
8451 cp_declarator
*new_declarator
;
8452 cp_declarator
*declarator
;
8453 cp_declarator
*outer_declarator
;
8454 const char *saved_message
;
8456 /* The type-specifier sequence must not contain type definitions.
8457 (It cannot contain declarations of new types either, but if they
8458 are not definitions we will catch that because they are not
8460 saved_message
= parser
->type_definition_forbidden_message
;
8461 parser
->type_definition_forbidden_message
8462 = G_("types may not be defined in a new-type-id");
8463 /* Parse the type-specifier-seq. */
8464 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8465 /*is_trailing_return=*/false,
8466 &type_specifier_seq
);
8467 /* Restore the old message. */
8468 parser
->type_definition_forbidden_message
= saved_message
;
8470 if (type_specifier_seq
.type
== error_mark_node
)
8471 return error_mark_node
;
8473 /* Parse the new-declarator. */
8474 new_declarator
= cp_parser_new_declarator_opt (parser
);
8476 /* Determine the number of elements in the last array dimension, if
8479 /* Skip down to the last array dimension. */
8480 declarator
= new_declarator
;
8481 outer_declarator
= NULL
;
8482 while (declarator
&& (declarator
->kind
== cdk_pointer
8483 || declarator
->kind
== cdk_ptrmem
))
8485 outer_declarator
= declarator
;
8486 declarator
= declarator
->declarator
;
8489 && declarator
->kind
== cdk_array
8490 && declarator
->declarator
8491 && declarator
->declarator
->kind
== cdk_array
)
8493 outer_declarator
= declarator
;
8494 declarator
= declarator
->declarator
;
8497 if (declarator
&& declarator
->kind
== cdk_array
)
8499 *nelts
= declarator
->u
.array
.bounds
;
8500 if (*nelts
== error_mark_node
)
8501 *nelts
= integer_one_node
;
8503 if (outer_declarator
)
8504 outer_declarator
->declarator
= declarator
->declarator
;
8506 new_declarator
= NULL
;
8509 return groktypename (&type_specifier_seq
, new_declarator
, false);
8512 /* Parse an (optional) new-declarator.
8515 ptr-operator new-declarator [opt]
8516 direct-new-declarator
8518 Returns the declarator. */
8520 static cp_declarator
*
8521 cp_parser_new_declarator_opt (cp_parser
* parser
)
8523 enum tree_code code
;
8524 tree type
, std_attributes
= NULL_TREE
;
8525 cp_cv_quals cv_quals
;
8527 /* We don't know if there's a ptr-operator next, or not. */
8528 cp_parser_parse_tentatively (parser
);
8529 /* Look for a ptr-operator. */
8530 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8531 /* If that worked, look for more new-declarators. */
8532 if (cp_parser_parse_definitely (parser
))
8534 cp_declarator
*declarator
;
8536 /* Parse another optional declarator. */
8537 declarator
= cp_parser_new_declarator_opt (parser
);
8539 declarator
= cp_parser_make_indirect_declarator
8540 (code
, type
, cv_quals
, declarator
, std_attributes
);
8545 /* If the next token is a `[', there is a direct-new-declarator. */
8546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8547 return cp_parser_direct_new_declarator (parser
);
8552 /* Parse a direct-new-declarator.
8554 direct-new-declarator:
8556 direct-new-declarator [constant-expression]
8560 static cp_declarator
*
8561 cp_parser_direct_new_declarator (cp_parser
* parser
)
8563 cp_declarator
*declarator
= NULL
;
8570 /* Look for the opening `['. */
8571 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8573 token
= cp_lexer_peek_token (parser
->lexer
);
8574 expression
= cp_parser_expression (parser
);
8575 /* The standard requires that the expression have integral
8576 type. DR 74 adds enumeration types. We believe that the
8577 real intent is that these expressions be handled like the
8578 expression in a `switch' condition, which also allows
8579 classes with a single conversion to integral or
8580 enumeration type. */
8581 if (!processing_template_decl
)
8584 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8589 error_at (token
->location
,
8590 "expression in new-declarator must have integral "
8591 "or enumeration type");
8592 expression
= error_mark_node
;
8596 /* Look for the closing `]'. */
8597 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8599 /* Add this bound to the declarator. */
8600 declarator
= make_array_declarator (declarator
, expression
);
8602 /* If the next token is not a `[', then there are no more
8604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8611 /* Parse a new-initializer.
8614 ( expression-list [opt] )
8617 Returns a representation of the expression-list. */
8619 static vec
<tree
, va_gc
> *
8620 cp_parser_new_initializer (cp_parser
* parser
)
8622 vec
<tree
, va_gc
> *expression_list
;
8624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8627 bool expr_non_constant_p
;
8628 cp_lexer_set_source_position (parser
->lexer
);
8629 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8630 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8631 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8632 expression_list
= make_tree_vector_single (t
);
8635 expression_list
= (cp_parser_parenthesized_expression_list
8636 (parser
, non_attr
, /*cast_p=*/false,
8637 /*allow_expansion_p=*/true,
8638 /*non_constant_p=*/NULL
));
8640 return expression_list
;
8643 /* Parse a delete-expression.
8646 :: [opt] delete cast-expression
8647 :: [opt] delete [ ] cast-expression
8649 Returns a representation of the expression. */
8652 cp_parser_delete_expression (cp_parser
* parser
)
8654 bool global_scope_p
;
8658 /* Look for the optional `::' operator. */
8660 = (cp_parser_global_scope_opt (parser
,
8661 /*current_scope_valid_p=*/false)
8663 /* Look for the `delete' keyword. */
8664 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8665 /* See if the array syntax is in use. */
8666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8668 /* Consume the `[' token. */
8669 cp_lexer_consume_token (parser
->lexer
);
8670 /* Look for the `]' token. */
8671 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8672 /* Remember that this is the `[]' construct. */
8678 /* Parse the cast-expression. */
8679 expression
= cp_parser_simple_cast_expression (parser
);
8681 /* A delete-expression may not appear in an integral constant
8683 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8684 return error_mark_node
;
8686 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8687 tf_warning_or_error
);
8690 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8691 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8695 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8698 switch (token
->type
)
8704 case CPP_CLOSE_SQUARE
:
8705 case CPP_CLOSE_PAREN
:
8706 case CPP_CLOSE_BRACE
:
8707 case CPP_OPEN_BRACE
:
8711 case CPP_DEREF_STAR
:
8719 case CPP_GREATER_EQ
:
8740 case CPP_OPEN_PAREN
:
8741 /* In ((type ()) () the last () isn't a valid cast-expression,
8742 so the whole must be parsed as postfix-expression. */
8743 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8746 case CPP_OPEN_SQUARE
:
8747 /* '[' may start a primary-expression in obj-c++ and in C++11,
8748 as a lambda-expression, eg, '(void)[]{}'. */
8749 if (cxx_dialect
>= cxx11
)
8751 return c_dialect_objc ();
8754 case CPP_MINUS_MINUS
:
8755 /* '++' and '--' may or may not start a cast-expression:
8757 struct T { void operator++(int); };
8758 void f() { (T())++; }
8771 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8772 in the order: const_cast, static_cast, reinterpret_cast.
8774 Don't suggest dynamic_cast.
8776 Return the first legal cast kind found, or NULL otherwise. */
8779 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8783 /* Reuse the parser logic by attempting to build the various kinds of
8784 cast, with "complain" disabled.
8785 Identify the first such cast that is valid. */
8787 /* Don't attempt to run such logic within template processing. */
8788 if (processing_template_decl
)
8791 /* First try const_cast. */
8792 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8793 if (trial
!= error_mark_node
)
8794 return "const_cast";
8796 /* If that fails, try static_cast. */
8797 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8798 if (trial
!= error_mark_node
)
8799 return "static_cast";
8801 /* Finally, try reinterpret_cast. */
8802 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8803 if (trial
!= error_mark_node
)
8804 return "reinterpret_cast";
8806 /* No such cast possible. */
8810 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8811 suggesting how to convert a C-style cast of the form:
8815 to a C++-style cast.
8817 The primary range of RICHLOC is asssumed to be that of the original
8818 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8819 of the parens in the C-style cast. */
8822 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8823 location_t close_paren_loc
, tree orig_expr
,
8826 /* This function is non-trivial, so bail out now if the warning isn't
8827 going to be emitted. */
8828 if (!warn_old_style_cast
)
8831 /* Try to find a legal C++ cast, trying them in order:
8832 const_cast, static_cast, reinterpret_cast. */
8833 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8834 if (!cast_suggestion
)
8837 /* Replace the open paren with "CAST_SUGGESTION<". */
8839 pp_printf (&pp
, "%s<", cast_suggestion
);
8840 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8842 /* Replace the close paren with "> (". */
8843 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8845 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8846 rich_loc
->add_fixit_insert_after (")");
8850 /* Parse a cast-expression.
8854 ( type-id ) cast-expression
8856 ADDRESS_P is true iff the unary-expression is appearing as the
8857 operand of the `&' operator. CAST_P is true if this expression is
8858 the target of a cast.
8860 Returns a representation of the expression. */
8863 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8864 bool decltype_p
, cp_id_kind
* pidk
)
8866 /* If it's a `(', then we might be looking at a cast. */
8867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8869 tree type
= NULL_TREE
;
8870 cp_expr
expr (NULL_TREE
);
8871 int cast_expression
= 0;
8872 const char *saved_message
;
8874 /* There's no way to know yet whether or not this is a cast.
8875 For example, `(int (3))' is a unary-expression, while `(int)
8876 3' is a cast. So, we resort to parsing tentatively. */
8877 cp_parser_parse_tentatively (parser
);
8878 /* Types may not be defined in a cast. */
8879 saved_message
= parser
->type_definition_forbidden_message
;
8880 parser
->type_definition_forbidden_message
8881 = G_("types may not be defined in casts");
8882 /* Consume the `('. */
8883 matching_parens parens
;
8884 cp_token
*open_paren
= parens
.consume_open (parser
);
8885 location_t open_paren_loc
= open_paren
->location
;
8886 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8888 /* A very tricky bit is that `(struct S) { 3 }' is a
8889 compound-literal (which we permit in C++ as an extension).
8890 But, that construct is not a cast-expression -- it is a
8891 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8892 is legal; if the compound-literal were a cast-expression,
8893 you'd need an extra set of parentheses.) But, if we parse
8894 the type-id, and it happens to be a class-specifier, then we
8895 will commit to the parse at that point, because we cannot
8896 undo the action that is done when creating a new class. So,
8897 then we cannot back up and do a postfix-expression.
8899 Another tricky case is the following (c++/29234):
8901 struct S { void operator () (); };
8908 As a type-id we parse the parenthesized S()() as a function
8909 returning a function, groktypename complains and we cannot
8910 back up in this case either.
8912 Therefore, we scan ahead to the closing `)', and check to see
8913 if the tokens after the `)' can start a cast-expression. Otherwise
8914 we are dealing with an unary-expression, a postfix-expression
8917 Yet another tricky case, in C++11, is the following (c++/54891):
8921 The issue is that usually, besides the case of lambda-expressions,
8922 the parenthesized type-id cannot be followed by '[', and, eg, we
8923 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8924 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8925 we don't commit, we try a cast-expression, then an unary-expression.
8927 Save tokens so that we can put them back. */
8928 cp_lexer_save_tokens (parser
->lexer
);
8930 /* We may be looking at a cast-expression. */
8931 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8932 /*consume_paren=*/true))
8934 = cp_parser_tokens_start_cast_expression (parser
);
8936 /* Roll back the tokens we skipped. */
8937 cp_lexer_rollback_tokens (parser
->lexer
);
8938 /* If we aren't looking at a cast-expression, simulate an error so
8939 that the call to cp_parser_error_occurred below returns true. */
8940 if (!cast_expression
)
8941 cp_parser_simulate_error (parser
);
8944 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8945 parser
->in_type_id_in_expr_p
= true;
8946 /* Look for the type-id. */
8947 type
= cp_parser_type_id (parser
);
8948 /* Look for the closing `)'. */
8949 cp_token
*close_paren
= parens
.require_close (parser
);
8951 close_paren_loc
= close_paren
->location
;
8952 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8955 /* Restore the saved message. */
8956 parser
->type_definition_forbidden_message
= saved_message
;
8958 /* At this point this can only be either a cast or a
8959 parenthesized ctor such as `(T ())' that looks like a cast to
8960 function returning T. */
8961 if (!cp_parser_error_occurred (parser
))
8963 /* Only commit if the cast-expression doesn't start with
8964 '++', '--', or '[' in C++11. */
8965 if (cast_expression
> 0)
8966 cp_parser_commit_to_topmost_tentative_parse (parser
);
8968 expr
= cp_parser_cast_expression (parser
,
8969 /*address_p=*/false,
8971 /*decltype_p=*/false,
8974 if (cp_parser_parse_definitely (parser
))
8976 /* Warn about old-style casts, if so requested. */
8977 if (warn_old_style_cast
8978 && !in_system_header_at (input_location
)
8979 && !VOID_TYPE_P (type
)
8980 && current_lang_name
!= lang_name_c
)
8982 gcc_rich_location
rich_loc (input_location
);
8983 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
8985 warning_at_rich_loc (&rich_loc
, OPT_Wold_style_cast
,
8986 "use of old-style cast to %q#T", type
);
8989 /* Only type conversions to integral or enumeration types
8990 can be used in constant-expressions. */
8991 if (!cast_valid_in_integral_constant_expression_p (type
)
8992 && cp_parser_non_integral_constant_expression (parser
,
8994 return error_mark_node
;
8996 /* Perform the cast. */
9000 with start==caret at the open paren, extending to the
9002 location_t cast_loc
= make_location (open_paren_loc
,
9004 expr
.get_finish ());
9005 expr
= build_c_cast (cast_loc
, type
, expr
);
9010 cp_parser_abort_tentative_parse (parser
);
9013 /* If we get here, then it's not a cast, so it must be a
9014 unary-expression. */
9015 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9016 cast_p
, decltype_p
);
9019 /* Parse a binary expression of the general form:
9023 pm-expression .* cast-expression
9024 pm-expression ->* cast-expression
9026 multiplicative-expression:
9028 multiplicative-expression * pm-expression
9029 multiplicative-expression / pm-expression
9030 multiplicative-expression % pm-expression
9032 additive-expression:
9033 multiplicative-expression
9034 additive-expression + multiplicative-expression
9035 additive-expression - multiplicative-expression
9039 shift-expression << additive-expression
9040 shift-expression >> additive-expression
9042 relational-expression:
9044 relational-expression < shift-expression
9045 relational-expression > shift-expression
9046 relational-expression <= shift-expression
9047 relational-expression >= shift-expression
9051 relational-expression:
9052 relational-expression <? shift-expression
9053 relational-expression >? shift-expression
9055 equality-expression:
9056 relational-expression
9057 equality-expression == relational-expression
9058 equality-expression != relational-expression
9062 and-expression & equality-expression
9064 exclusive-or-expression:
9066 exclusive-or-expression ^ and-expression
9068 inclusive-or-expression:
9069 exclusive-or-expression
9070 inclusive-or-expression | exclusive-or-expression
9072 logical-and-expression:
9073 inclusive-or-expression
9074 logical-and-expression && inclusive-or-expression
9076 logical-or-expression:
9077 logical-and-expression
9078 logical-or-expression || logical-and-expression
9080 All these are implemented with a single function like:
9083 simple-cast-expression
9084 binary-expression <token> binary-expression
9086 CAST_P is true if this expression is the target of a cast.
9088 The binops_by_token map is used to get the tree codes for each <token> type.
9089 binary-expressions are associated according to a precedence table. */
9091 #define TOKEN_PRECEDENCE(token) \
9092 (((token->type == CPP_GREATER \
9093 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9094 && !parser->greater_than_is_operator_p) \
9095 ? PREC_NOT_OPERATOR \
9096 : binops_by_token[token->type].prec)
9099 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9100 bool no_toplevel_fold_p
,
9102 enum cp_parser_prec prec
,
9105 cp_parser_expression_stack stack
;
9106 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9107 cp_parser_expression_stack_entry current
;
9110 enum tree_code rhs_type
;
9111 enum cp_parser_prec new_prec
, lookahead_prec
;
9114 /* Parse the first expression. */
9115 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9116 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9117 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9118 cast_p
, decltype_p
, pidk
);
9119 current
.prec
= prec
;
9121 if (cp_parser_error_occurred (parser
))
9122 return error_mark_node
;
9126 /* Get an operator token. */
9127 token
= cp_lexer_peek_token (parser
->lexer
);
9129 if (warn_cxx11_compat
9130 && token
->type
== CPP_RSHIFT
9131 && !parser
->greater_than_is_operator_p
)
9133 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9134 "%<>>%> operator is treated"
9135 " as two right angle brackets in C++11"))
9136 inform (token
->location
,
9137 "suggest parentheses around %<>>%> expression");
9140 new_prec
= TOKEN_PRECEDENCE (token
);
9141 if (new_prec
!= PREC_NOT_OPERATOR
9142 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9143 /* This is a fold-expression; handle it later. */
9144 new_prec
= PREC_NOT_OPERATOR
;
9146 /* Popping an entry off the stack means we completed a subexpression:
9147 - either we found a token which is not an operator (`>' where it is not
9148 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9149 will happen repeatedly;
9150 - or, we found an operator which has lower priority. This is the case
9151 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9153 if (new_prec
<= current
.prec
)
9162 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9163 current
.loc
= token
->location
;
9165 /* We used the operator token. */
9166 cp_lexer_consume_token (parser
->lexer
);
9168 /* For "false && x" or "true || x", x will never be executed;
9169 disable warnings while evaluating it. */
9170 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9171 c_inhibit_evaluation_warnings
+=
9172 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9173 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9174 c_inhibit_evaluation_warnings
+=
9175 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9177 /* Extract another operand. It may be the RHS of this expression
9178 or the LHS of a new, higher priority expression. */
9179 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9180 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9181 rhs
= cp_parser_simple_cast_expression (parser
);
9183 /* Get another operator token. Look up its precedence to avoid
9184 building a useless (immediately popped) stack entry for common
9185 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9186 token
= cp_lexer_peek_token (parser
->lexer
);
9187 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9188 if (lookahead_prec
!= PREC_NOT_OPERATOR
9189 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9190 lookahead_prec
= PREC_NOT_OPERATOR
;
9191 if (lookahead_prec
> new_prec
)
9193 /* ... and prepare to parse the RHS of the new, higher priority
9194 expression. Since precedence levels on the stack are
9195 monotonically increasing, we do not have to care about
9200 current
.lhs_type
= rhs_type
;
9201 current
.prec
= new_prec
;
9202 new_prec
= lookahead_prec
;
9206 lookahead_prec
= new_prec
;
9207 /* If the stack is not empty, we have parsed into LHS the right side
9208 (`4' in the example above) of an expression we had suspended.
9209 We can use the information on the stack to recover the LHS (`3')
9210 from the stack together with the tree code (`MULT_EXPR'), and
9211 the precedence of the higher level subexpression
9212 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9213 which will be used to actually build the additive expression. */
9215 rhs_type
= current
.lhs_type
;
9220 /* Undo the disabling of warnings done above. */
9221 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9222 c_inhibit_evaluation_warnings
-=
9223 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9224 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9225 c_inhibit_evaluation_warnings
-=
9226 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9228 if (warn_logical_not_paren
9229 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9230 && current
.lhs_type
== TRUTH_NOT_EXPR
9231 /* Avoid warning for !!x == y. */
9232 && (TREE_CODE (current
.lhs
) != NE_EXPR
9233 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9234 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9235 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9236 /* Avoid warning for !b == y where b is boolean. */
9237 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9238 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9240 /* Avoid warning for !!b == y where b is boolean. */
9241 && (!DECL_P (current
.lhs
)
9242 || TREE_TYPE (current
.lhs
) == NULL_TREE
9243 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9244 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9245 current
.lhs
, maybe_constant_value (rhs
));
9249 location_t combined_loc
= make_location (current
.loc
,
9250 current
.lhs
.get_start (),
9253 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9254 ERROR_MARK for everything that is not a binary expression.
9255 This makes warn_about_parentheses miss some warnings that
9256 involve unary operators. For unary expressions we should
9257 pass the correct tree_code unless the unary expression was
9258 surrounded by parentheses.
9260 if (no_toplevel_fold_p
9261 && lookahead_prec
<= current
.prec
9263 current
.lhs
= build2_loc (combined_loc
,
9265 TREE_CODE_CLASS (current
.tree_type
)
9267 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9271 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9272 current
.lhs
, current
.lhs_type
,
9273 rhs
, rhs_type
, &overload
,
9274 complain_flags (decltype_p
));
9275 /* TODO: build_x_binary_op doesn't always honor the location. */
9276 current
.lhs
.set_location (combined_loc
);
9278 current
.lhs_type
= current
.tree_type
;
9280 /* If the binary operator required the use of an overloaded operator,
9281 then this expression cannot be an integral constant-expression.
9282 An overloaded operator can be used even if both operands are
9283 otherwise permissible in an integral constant-expression if at
9284 least one of the operands is of enumeration type. */
9287 && cp_parser_non_integral_constant_expression (parser
,
9289 return error_mark_node
;
9296 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9297 bool no_toplevel_fold_p
,
9298 enum cp_parser_prec prec
,
9301 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9302 /*decltype*/false, prec
, pidk
);
9305 /* Parse the `? expression : assignment-expression' part of a
9306 conditional-expression. The LOGICAL_OR_EXPR is the
9307 logical-or-expression that started the conditional-expression.
9308 Returns a representation of the entire conditional-expression.
9310 This routine is used by cp_parser_assignment_expression.
9312 ? expression : assignment-expression
9316 ? : assignment-expression */
9319 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9321 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9322 cp_expr assignment_expr
;
9323 struct cp_token
*token
;
9324 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9326 /* Consume the `?' token. */
9327 cp_lexer_consume_token (parser
->lexer
);
9328 token
= cp_lexer_peek_token (parser
->lexer
);
9329 if (cp_parser_allow_gnu_extensions_p (parser
)
9330 && token
->type
== CPP_COLON
)
9332 pedwarn (token
->location
, OPT_Wpedantic
,
9333 "ISO C++ does not allow ?: with omitted middle operand");
9334 /* Implicit true clause. */
9336 c_inhibit_evaluation_warnings
+=
9337 folded_logical_or_expr
== truthvalue_true_node
;
9338 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9342 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9343 parser
->colon_corrects_to_scope_p
= false;
9344 /* Parse the expression. */
9345 c_inhibit_evaluation_warnings
+=
9346 folded_logical_or_expr
== truthvalue_false_node
;
9347 expr
= cp_parser_expression (parser
);
9348 c_inhibit_evaluation_warnings
+=
9349 ((folded_logical_or_expr
== truthvalue_true_node
)
9350 - (folded_logical_or_expr
== truthvalue_false_node
));
9351 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9354 /* The next token should be a `:'. */
9355 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9356 /* Parse the assignment-expression. */
9357 assignment_expr
= cp_parser_assignment_expression (parser
);
9358 c_inhibit_evaluation_warnings
-=
9359 folded_logical_or_expr
== truthvalue_true_node
;
9362 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9363 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9364 with the caret at the "?", ranging from the start of
9365 the logical_or_expr to the end of the assignment_expr. */
9366 loc
= make_location (loc
,
9367 logical_or_expr
.get_start (),
9368 assignment_expr
.get_finish ());
9370 /* Build the conditional-expression. */
9371 return build_x_conditional_expr (loc
, logical_or_expr
,
9374 tf_warning_or_error
);
9377 /* Parse an assignment-expression.
9379 assignment-expression:
9380 conditional-expression
9381 logical-or-expression assignment-operator assignment_expression
9384 CAST_P is true if this expression is the target of a cast.
9385 DECLTYPE_P is true if this expression is the operand of decltype.
9387 Returns a representation for the expression. */
9390 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9391 bool cast_p
, bool decltype_p
)
9395 /* If the next token is the `throw' keyword, then we're looking at
9396 a throw-expression. */
9397 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9398 expr
= cp_parser_throw_expression (parser
);
9399 /* Otherwise, it must be that we are looking at a
9400 logical-or-expression. */
9403 /* Parse the binary expressions (logical-or-expression). */
9404 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9406 PREC_NOT_OPERATOR
, pidk
);
9407 /* If the next token is a `?' then we're actually looking at a
9408 conditional-expression. */
9409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9410 return cp_parser_question_colon_clause (parser
, expr
);
9413 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9415 /* If it's an assignment-operator, we're using the second
9417 enum tree_code assignment_operator
9418 = cp_parser_assignment_operator_opt (parser
);
9419 if (assignment_operator
!= ERROR_MARK
)
9421 bool non_constant_p
;
9423 /* Parse the right-hand side of the assignment. */
9424 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9427 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9430 /* An assignment may not appear in a
9431 constant-expression. */
9432 if (cp_parser_non_integral_constant_expression (parser
,
9434 return error_mark_node
;
9435 /* Build the assignment expression. Its default
9439 is the location of the '=' token as the
9440 caret, ranging from the start of the lhs to the
9442 loc
= make_location (loc
,
9445 expr
= build_x_modify_expr (loc
, expr
,
9446 assignment_operator
,
9448 complain_flags (decltype_p
));
9449 /* TODO: build_x_modify_expr doesn't honor the location,
9450 so we must set it here. */
9451 expr
.set_location (loc
);
9459 /* Parse an (optional) assignment-operator.
9461 assignment-operator: one of
9462 = *= /= %= += -= >>= <<= &= ^= |=
9466 assignment-operator: one of
9469 If the next token is an assignment operator, the corresponding tree
9470 code is returned, and the token is consumed. For example, for
9471 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9472 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9473 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9474 operator, ERROR_MARK is returned. */
9476 static enum tree_code
9477 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9482 /* Peek at the next token. */
9483 token
= cp_lexer_peek_token (parser
->lexer
);
9485 switch (token
->type
)
9496 op
= TRUNC_DIV_EXPR
;
9500 op
= TRUNC_MOD_EXPR
;
9532 /* Nothing else is an assignment operator. */
9536 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9537 if (op
!= ERROR_MARK
9538 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9541 /* If it was an assignment operator, consume it. */
9542 if (op
!= ERROR_MARK
)
9543 cp_lexer_consume_token (parser
->lexer
);
9548 /* Parse an expression.
9551 assignment-expression
9552 expression , assignment-expression
9554 CAST_P is true if this expression is the target of a cast.
9555 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9556 except possibly parenthesized or on the RHS of a comma (N3276).
9558 Returns a representation of the expression. */
9561 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9562 bool cast_p
, bool decltype_p
)
9564 cp_expr expression
= NULL_TREE
;
9565 location_t loc
= UNKNOWN_LOCATION
;
9569 cp_expr assignment_expression
;
9571 /* Parse the next assignment-expression. */
9572 assignment_expression
9573 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9575 /* We don't create a temporary for a call that is the immediate operand
9576 of decltype or on the RHS of a comma. But when we see a comma, we
9577 need to create a temporary for a call on the LHS. */
9578 if (decltype_p
&& !processing_template_decl
9579 && TREE_CODE (assignment_expression
) == CALL_EXPR
9580 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9581 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9582 assignment_expression
9583 = build_cplus_new (TREE_TYPE (assignment_expression
),
9584 assignment_expression
, tf_warning_or_error
);
9586 /* If this is the first assignment-expression, we can just
9589 expression
= assignment_expression
;
9592 /* Create a location with caret at the comma, ranging
9593 from the start of the LHS to the end of the RHS. */
9594 loc
= make_location (loc
,
9595 expression
.get_start (),
9596 assignment_expression
.get_finish ());
9597 expression
= build_x_compound_expr (loc
, expression
,
9598 assignment_expression
,
9599 complain_flags (decltype_p
));
9600 expression
.set_location (loc
);
9602 /* If the next token is not a comma, or we're in a fold-expression, then
9603 we are done with the expression. */
9604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9605 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9607 /* Consume the `,'. */
9608 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9609 cp_lexer_consume_token (parser
->lexer
);
9610 /* A comma operator cannot appear in a constant-expression. */
9611 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9612 expression
= error_mark_node
;
9618 /* Parse a constant-expression.
9620 constant-expression:
9621 conditional-expression
9623 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9624 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9625 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9626 is false, NON_CONSTANT_P should be NULL. */
9629 cp_parser_constant_expression (cp_parser
* parser
,
9630 bool allow_non_constant_p
,
9631 bool *non_constant_p
)
9633 bool saved_integral_constant_expression_p
;
9634 bool saved_allow_non_integral_constant_expression_p
;
9635 bool saved_non_integral_constant_expression_p
;
9638 /* It might seem that we could simply parse the
9639 conditional-expression, and then check to see if it were
9640 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9641 one that the compiler can figure out is constant, possibly after
9642 doing some simplifications or optimizations. The standard has a
9643 precise definition of constant-expression, and we must honor
9644 that, even though it is somewhat more restrictive.
9650 is not a legal declaration, because `(2, 3)' is not a
9651 constant-expression. The `,' operator is forbidden in a
9652 constant-expression. However, GCC's constant-folding machinery
9653 will fold this operation to an INTEGER_CST for `3'. */
9655 /* Save the old settings. */
9656 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9657 saved_allow_non_integral_constant_expression_p
9658 = parser
->allow_non_integral_constant_expression_p
;
9659 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9660 /* We are now parsing a constant-expression. */
9661 parser
->integral_constant_expression_p
= true;
9662 parser
->allow_non_integral_constant_expression_p
9663 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9664 parser
->non_integral_constant_expression_p
= false;
9665 /* Although the grammar says "conditional-expression", we parse an
9666 "assignment-expression", which also permits "throw-expression"
9667 and the use of assignment operators. In the case that
9668 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9669 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9670 actually essential that we look for an assignment-expression.
9671 For example, cp_parser_initializer_clauses uses this function to
9672 determine whether a particular assignment-expression is in fact
9674 expression
= cp_parser_assignment_expression (parser
);
9675 /* Restore the old settings. */
9676 parser
->integral_constant_expression_p
9677 = saved_integral_constant_expression_p
;
9678 parser
->allow_non_integral_constant_expression_p
9679 = saved_allow_non_integral_constant_expression_p
;
9680 if (cxx_dialect
>= cxx11
)
9682 /* Require an rvalue constant expression here; that's what our
9683 callers expect. Reference constant expressions are handled
9684 separately in e.g. cp_parser_template_argument. */
9685 tree decay
= expression
;
9686 if (TREE_TYPE (expression
)
9687 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9688 decay
= build_address (expression
);
9689 bool is_const
= potential_rvalue_constant_expression (decay
);
9690 parser
->non_integral_constant_expression_p
= !is_const
;
9691 if (!is_const
&& !allow_non_constant_p
)
9692 require_potential_rvalue_constant_expression (decay
);
9694 if (allow_non_constant_p
)
9695 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9696 parser
->non_integral_constant_expression_p
9697 = saved_non_integral_constant_expression_p
;
9702 /* Parse __builtin_offsetof.
9704 offsetof-expression:
9705 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9707 offsetof-member-designator:
9709 | offsetof-member-designator "." id-expression
9710 | offsetof-member-designator "[" expression "]"
9711 | offsetof-member-designator "->" id-expression */
9714 cp_parser_builtin_offsetof (cp_parser
*parser
)
9716 int save_ice_p
, save_non_ice_p
;
9721 location_t finish_loc
;
9723 /* We're about to accept non-integral-constant things, but will
9724 definitely yield an integral constant expression. Save and
9725 restore these values around our local parsing. */
9726 save_ice_p
= parser
->integral_constant_expression_p
;
9727 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9729 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9731 /* Consume the "__builtin_offsetof" token. */
9732 cp_lexer_consume_token (parser
->lexer
);
9733 /* Consume the opening `('. */
9734 matching_parens parens
;
9735 parens
.require_open (parser
);
9736 /* Parse the type-id. */
9737 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9738 type
= cp_parser_type_id (parser
);
9739 /* Look for the `,'. */
9740 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9741 token
= cp_lexer_peek_token (parser
->lexer
);
9743 /* Build the (type *)null that begins the traditional offsetof macro. */
9745 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9746 tf_warning_or_error
);
9748 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9749 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9750 true, &dummy
, token
->location
);
9753 token
= cp_lexer_peek_token (parser
->lexer
);
9754 switch (token
->type
)
9756 case CPP_OPEN_SQUARE
:
9757 /* offsetof-member-designator "[" expression "]" */
9758 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9763 /* offsetof-member-designator "->" identifier */
9764 expr
= grok_array_decl (token
->location
, expr
,
9765 integer_zero_node
, false);
9769 /* offsetof-member-designator "." identifier */
9770 cp_lexer_consume_token (parser
->lexer
);
9771 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9776 case CPP_CLOSE_PAREN
:
9777 /* Consume the ")" token. */
9778 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9779 cp_lexer_consume_token (parser
->lexer
);
9783 /* Error. We know the following require will fail, but
9784 that gives the proper error message. */
9785 parens
.require_close (parser
);
9786 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9787 expr
= error_mark_node
;
9793 /* Make a location of the form:
9794 __builtin_offsetof (struct s, f)
9795 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9796 with caret at the type-id, ranging from the start of the
9797 "_builtin_offsetof" token to the close paren. */
9798 loc
= make_location (loc
, start_loc
, finish_loc
);
9799 /* The result will be an INTEGER_CST, so we need to explicitly
9800 preserve the location. */
9801 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9804 parser
->integral_constant_expression_p
= save_ice_p
;
9805 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9810 /* Parse a trait expression.
9812 Returns a representation of the expression, the underlying type
9813 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9816 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9819 tree type1
, type2
= NULL_TREE
;
9820 bool binary
= false;
9821 bool variadic
= false;
9825 case RID_HAS_NOTHROW_ASSIGN
:
9826 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9828 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9829 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9831 case RID_HAS_NOTHROW_COPY
:
9832 kind
= CPTK_HAS_NOTHROW_COPY
;
9834 case RID_HAS_TRIVIAL_ASSIGN
:
9835 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9837 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9838 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9840 case RID_HAS_TRIVIAL_COPY
:
9841 kind
= CPTK_HAS_TRIVIAL_COPY
;
9843 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9844 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9846 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9847 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9849 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9850 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9852 case RID_IS_ABSTRACT
:
9853 kind
= CPTK_IS_ABSTRACT
;
9855 case RID_IS_AGGREGATE
:
9856 kind
= CPTK_IS_AGGREGATE
;
9858 case RID_IS_BASE_OF
:
9859 kind
= CPTK_IS_BASE_OF
;
9863 kind
= CPTK_IS_CLASS
;
9866 kind
= CPTK_IS_EMPTY
;
9869 kind
= CPTK_IS_ENUM
;
9872 kind
= CPTK_IS_FINAL
;
9874 case RID_IS_LITERAL_TYPE
:
9875 kind
= CPTK_IS_LITERAL_TYPE
;
9880 case RID_IS_POLYMORPHIC
:
9881 kind
= CPTK_IS_POLYMORPHIC
;
9883 case RID_IS_SAME_AS
:
9884 kind
= CPTK_IS_SAME_AS
;
9887 case RID_IS_STD_LAYOUT
:
9888 kind
= CPTK_IS_STD_LAYOUT
;
9890 case RID_IS_TRIVIAL
:
9891 kind
= CPTK_IS_TRIVIAL
;
9893 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9894 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9897 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9898 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9901 case RID_IS_TRIVIALLY_COPYABLE
:
9902 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9905 kind
= CPTK_IS_UNION
;
9907 case RID_UNDERLYING_TYPE
:
9908 kind
= CPTK_UNDERLYING_TYPE
;
9913 case RID_DIRECT_BASES
:
9914 kind
= CPTK_DIRECT_BASES
;
9916 case RID_IS_ASSIGNABLE
:
9917 kind
= CPTK_IS_ASSIGNABLE
;
9920 case RID_IS_CONSTRUCTIBLE
:
9921 kind
= CPTK_IS_CONSTRUCTIBLE
;
9928 /* Consume the token. */
9929 cp_lexer_consume_token (parser
->lexer
);
9931 matching_parens parens
;
9932 parens
.require_open (parser
);
9935 type_id_in_expr_sentinel
s (parser
);
9936 type1
= cp_parser_type_id (parser
);
9939 if (type1
== error_mark_node
)
9940 return error_mark_node
;
9944 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9947 type_id_in_expr_sentinel
s (parser
);
9948 type2
= cp_parser_type_id (parser
);
9951 if (type2
== error_mark_node
)
9952 return error_mark_node
;
9956 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9958 cp_lexer_consume_token (parser
->lexer
);
9959 tree elt
= cp_parser_type_id (parser
);
9960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9962 cp_lexer_consume_token (parser
->lexer
);
9963 elt
= make_pack_expansion (elt
);
9965 if (elt
== error_mark_node
)
9966 return error_mark_node
;
9967 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9971 parens
.require_close (parser
);
9973 /* Complete the trait expression, which may mean either processing
9974 the trait expr now or saving it for template instantiation. */
9977 case CPTK_UNDERLYING_TYPE
:
9978 return finish_underlying_type (type1
);
9980 return finish_bases (type1
, false);
9981 case CPTK_DIRECT_BASES
:
9982 return finish_bases (type1
, true);
9984 return finish_trait_expr (kind
, type1
, type2
);
9988 /* Parse a lambda expression.
9991 lambda-introducer lambda-declarator [opt] compound-statement
9993 Returns a representation of the expression. */
9996 cp_parser_lambda_expression (cp_parser
* parser
)
9998 tree lambda_expr
= build_lambda_expr ();
10001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10002 cp_token_position start
= 0;
10004 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10006 if (cp_unevaluated_operand
)
10008 if (!token
->error_reported
)
10010 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10011 "lambda-expression in unevaluated context");
10012 token
->error_reported
= true;
10016 else if (parser
->in_template_argument_list_p
)
10018 if (!token
->error_reported
)
10020 error_at (token
->location
, "lambda-expression in template-argument");
10021 token
->error_reported
= true;
10026 /* We may be in the middle of deferred access check. Disable
10028 push_deferring_access_checks (dk_no_deferred
);
10030 cp_parser_lambda_introducer (parser
, lambda_expr
);
10032 type
= begin_lambda_type (lambda_expr
);
10033 if (type
== error_mark_node
)
10034 return error_mark_node
;
10036 record_lambda_scope (lambda_expr
);
10038 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10039 determine_visibility (TYPE_NAME (type
));
10041 /* Now that we've started the type, add the capture fields for any
10042 explicit captures. */
10043 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10046 /* Inside the class, surrounding template-parameter-lists do not apply. */
10047 unsigned int saved_num_template_parameter_lists
10048 = parser
->num_template_parameter_lists
;
10049 unsigned char in_statement
= parser
->in_statement
;
10050 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10051 bool fully_implicit_function_template_p
10052 = parser
->fully_implicit_function_template_p
;
10053 tree implicit_template_parms
= parser
->implicit_template_parms
;
10054 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10055 bool auto_is_implicit_function_template_parm_p
10056 = parser
->auto_is_implicit_function_template_parm_p
;
10058 parser
->num_template_parameter_lists
= 0;
10059 parser
->in_statement
= 0;
10060 parser
->in_switch_statement_p
= false;
10061 parser
->fully_implicit_function_template_p
= false;
10062 parser
->implicit_template_parms
= 0;
10063 parser
->implicit_template_scope
= 0;
10064 parser
->auto_is_implicit_function_template_parm_p
= false;
10066 /* By virtue of defining a local class, a lambda expression has access to
10067 the private variables of enclosing classes. */
10069 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10071 if (ok
&& cp_parser_error_occurred (parser
))
10076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10077 && cp_parser_start_tentative_firewall (parser
))
10079 cp_parser_lambda_body (parser
, lambda_expr
);
10081 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10083 if (cp_parser_skip_to_closing_brace (parser
))
10084 cp_lexer_consume_token (parser
->lexer
);
10087 /* The capture list was built up in reverse order; fix that now. */
10088 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10089 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10092 maybe_add_lambda_conv_op (type
);
10094 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10096 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10097 parser
->in_statement
= in_statement
;
10098 parser
->in_switch_statement_p
= in_switch_statement_p
;
10099 parser
->fully_implicit_function_template_p
10100 = fully_implicit_function_template_p
;
10101 parser
->implicit_template_parms
= implicit_template_parms
;
10102 parser
->implicit_template_scope
= implicit_template_scope
;
10103 parser
->auto_is_implicit_function_template_parm_p
10104 = auto_is_implicit_function_template_parm_p
;
10107 /* This field is only used during parsing of the lambda. */
10108 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10110 /* This lambda shouldn't have any proxies left at this point. */
10111 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10112 /* And now that we're done, push proxies for an enclosing lambda. */
10113 insert_pending_capture_proxies ();
10116 lambda_expr
= build_lambda_object (lambda_expr
);
10118 lambda_expr
= error_mark_node
;
10120 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10122 pop_deferring_access_checks ();
10124 return lambda_expr
;
10127 /* Parse the beginning of a lambda expression.
10130 [ lambda-capture [opt] ]
10132 LAMBDA_EXPR is the current representation of the lambda expression. */
10135 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10137 /* Need commas after the first capture. */
10140 /* Eat the leading `['. */
10141 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10143 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10145 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10146 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10147 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10148 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10150 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10152 cp_lexer_consume_token (parser
->lexer
);
10156 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10158 cp_token
* capture_token
;
10160 tree capture_init_expr
;
10161 cp_id_kind idk
= CP_ID_KIND_NONE
;
10162 bool explicit_init_p
= false;
10164 enum capture_kind_type
10169 enum capture_kind_type capture_kind
= BY_COPY
;
10171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10173 error ("expected end of capture-list");
10180 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10182 /* Possibly capture `this'. */
10183 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10185 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10186 if (cxx_dialect
< cxx2a
10187 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10188 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10189 "with by-copy capture default");
10190 cp_lexer_consume_token (parser
->lexer
);
10191 add_capture (lambda_expr
,
10192 /*id=*/this_identifier
,
10193 /*initializer=*/finish_this_expr (),
10194 /*by_reference_p=*/true,
10199 /* Possibly capture `*this'. */
10200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10201 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10203 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10204 if (cxx_dialect
< cxx17
)
10205 pedwarn (loc
, 0, "%<*this%> capture only available with "
10206 "-std=c++17 or -std=gnu++17");
10207 cp_lexer_consume_token (parser
->lexer
);
10208 cp_lexer_consume_token (parser
->lexer
);
10209 add_capture (lambda_expr
,
10210 /*id=*/this_identifier
,
10211 /*initializer=*/finish_this_expr (),
10212 /*by_reference_p=*/false,
10217 /* Remember whether we want to capture as a reference or not. */
10218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10220 capture_kind
= BY_REFERENCE
;
10221 cp_lexer_consume_token (parser
->lexer
);
10224 /* Get the identifier. */
10225 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10226 capture_id
= cp_parser_identifier (parser
);
10228 if (capture_id
== error_mark_node
)
10229 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10230 delimiters, but I modified this to stop on unnested ']' as well. It
10231 was already changed to stop on unnested '}', so the
10232 "closing_parenthesis" name is no more misleading with my change. */
10234 cp_parser_skip_to_closing_parenthesis (parser
,
10235 /*recovering=*/true,
10237 /*consume_paren=*/true);
10241 /* Find the initializer for this capture. */
10242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10243 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10244 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10246 bool direct
, non_constant
;
10247 /* An explicit initializer exists. */
10248 if (cxx_dialect
< cxx14
)
10249 pedwarn (input_location
, 0,
10250 "lambda capture initializers "
10251 "only available with -std=c++14 or -std=gnu++14");
10252 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10254 explicit_init_p
= true;
10255 if (capture_init_expr
== NULL_TREE
)
10257 error ("empty initializer for lambda init-capture");
10258 capture_init_expr
= error_mark_node
;
10263 const char* error_msg
;
10265 /* Turn the identifier into an id-expression. */
10267 = cp_parser_lookup_name_simple (parser
, capture_id
,
10268 capture_token
->location
);
10270 if (capture_init_expr
== error_mark_node
)
10272 unqualified_name_lookup_error (capture_id
);
10275 else if (DECL_P (capture_init_expr
)
10276 && (!VAR_P (capture_init_expr
)
10277 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10279 error_at (capture_token
->location
,
10280 "capture of non-variable %qD ",
10281 capture_init_expr
);
10282 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10283 "%q#D declared here", capture_init_expr
);
10286 if (VAR_P (capture_init_expr
)
10287 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10289 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10290 "%qD with non-automatic storage duration",
10291 capture_init_expr
))
10292 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10293 "%q#D declared here", capture_init_expr
);
10298 = finish_id_expression
10303 /*integral_constant_expression_p=*/false,
10304 /*allow_non_integral_constant_expression_p=*/false,
10305 /*non_integral_constant_expression_p=*/NULL
,
10306 /*template_p=*/false,
10308 /*address_p=*/false,
10309 /*template_arg_p=*/false,
10311 capture_token
->location
);
10313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10315 cp_lexer_consume_token (parser
->lexer
);
10316 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10319 check_for_bare_parameter_packs (capture_init_expr
);
10322 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10323 && !explicit_init_p
)
10325 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10326 && capture_kind
== BY_COPY
)
10327 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10328 "of %qD redundant with by-copy capture default",
10330 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10331 && capture_kind
== BY_REFERENCE
)
10332 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10333 "capture of %qD redundant with by-reference capture "
10334 "default", capture_id
);
10337 add_capture (lambda_expr
,
10340 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10344 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10347 /* Parse the (optional) middle of a lambda expression.
10350 < template-parameter-list [opt] >
10351 ( parameter-declaration-clause [opt] )
10352 attribute-specifier [opt]
10353 decl-specifier-seq [opt]
10354 exception-specification [opt]
10355 lambda-return-type-clause [opt]
10357 LAMBDA_EXPR is the current representation of the lambda expression. */
10360 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10362 /* 5.1.1.4 of the standard says:
10363 If a lambda-expression does not include a lambda-declarator, it is as if
10364 the lambda-declarator were ().
10365 This means an empty parameter list, no attributes, and no exception
10367 tree param_list
= void_list_node
;
10368 tree attributes
= NULL_TREE
;
10369 tree exception_spec
= NULL_TREE
;
10370 tree template_param_list
= NULL_TREE
;
10371 tree tx_qual
= NULL_TREE
;
10372 tree return_type
= NULL_TREE
;
10373 cp_decl_specifier_seq lambda_specs
;
10374 clear_decl_specs (&lambda_specs
);
10376 /* The template-parameter-list is optional, but must begin with
10377 an opening angle if present. */
10378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10380 if (cxx_dialect
< cxx14
)
10381 pedwarn (parser
->lexer
->next_token
->location
, 0,
10382 "lambda templates are only available with "
10383 "-std=c++14 or -std=gnu++14");
10385 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10386 "ISO C++ does not support lambda templates");
10388 cp_lexer_consume_token (parser
->lexer
);
10390 template_param_list
= cp_parser_template_parameter_list (parser
);
10392 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10394 /* We just processed one more parameter list. */
10395 ++parser
->num_template_parameter_lists
;
10398 /* The parameter-declaration-clause is optional (unless
10399 template-parameter-list was given), but must begin with an
10400 opening parenthesis if present. */
10401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10403 matching_parens parens
;
10404 parens
.consume_open (parser
);
10406 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10408 /* Parse parameters. */
10409 param_list
= cp_parser_parameter_declaration_clause (parser
);
10411 /* Default arguments shall not be specified in the
10412 parameter-declaration-clause of a lambda-declarator. */
10413 if (cxx_dialect
< cxx14
)
10414 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10415 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10416 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10417 "default argument specified for lambda parameter");
10419 parens
.require_close (parser
);
10421 attributes
= cp_parser_attributes_opt (parser
);
10423 /* In the decl-specifier-seq of the lambda-declarator, each
10424 decl-specifier shall either be mutable or constexpr. */
10425 int declares_class_or_enum
;
10426 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10427 cp_parser_decl_specifier_seq (parser
,
10428 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10429 &lambda_specs
, &declares_class_or_enum
);
10430 if (lambda_specs
.storage_class
== sc_mutable
)
10432 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10433 if (lambda_specs
.conflicting_specifiers_p
)
10434 error_at (lambda_specs
.locations
[ds_storage_class
],
10435 "duplicate %<mutable%>");
10438 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10440 /* Parse optional exception specification. */
10441 exception_spec
= cp_parser_exception_specification_opt (parser
);
10443 /* Parse optional trailing return type. */
10444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10446 cp_lexer_consume_token (parser
->lexer
);
10447 return_type
= cp_parser_trailing_type_id (parser
);
10450 /* The function parameters must be in scope all the way until after the
10451 trailing-return-type in case of decltype. */
10452 pop_bindings_and_leave_scope ();
10454 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10455 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10457 /* Create the function call operator.
10459 Messing with declarators like this is no uglier than building up the
10460 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10463 cp_decl_specifier_seq return_type_specs
;
10464 cp_declarator
* declarator
;
10469 clear_decl_specs (&return_type_specs
);
10471 return_type_specs
.type
= return_type
;
10473 /* Maybe we will deduce the return type later. */
10474 return_type_specs
.type
= make_auto ();
10476 if (lambda_specs
.locations
[ds_constexpr
])
10478 if (cxx_dialect
>= cxx17
)
10479 return_type_specs
.locations
[ds_constexpr
]
10480 = lambda_specs
.locations
[ds_constexpr
];
10482 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10483 "lambda only available with -std=c++17 or -std=gnu++17");
10486 p
= obstack_alloc (&declarator_obstack
, 0);
10488 declarator
= make_id_declarator (NULL_TREE
, cp_operator_id (CALL_EXPR
),
10491 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10492 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10493 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10494 VIRT_SPEC_UNSPECIFIED
,
10498 /*late_return_type=*/NULL_TREE
,
10499 /*requires_clause*/NULL_TREE
);
10500 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10502 fco
= grokmethod (&return_type_specs
,
10505 if (fco
!= error_mark_node
)
10507 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10508 DECL_ARTIFICIAL (fco
) = 1;
10509 /* Give the object parameter a different name. */
10510 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10512 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10514 if (template_param_list
)
10516 fco
= finish_member_template_decl (fco
);
10517 finish_template_decl (template_param_list
);
10518 --parser
->num_template_parameter_lists
;
10520 else if (parser
->fully_implicit_function_template_p
)
10521 fco
= finish_fully_implicit_template (parser
, fco
);
10523 finish_member_declaration (fco
);
10525 obstack_free (&declarator_obstack
, p
);
10527 return (fco
!= error_mark_node
);
10531 /* Parse the body of a lambda expression, which is simply
10535 but which requires special handling.
10536 LAMBDA_EXPR is the current representation of the lambda expression. */
10539 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10541 bool nested
= (current_function_decl
!= NULL_TREE
);
10542 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10544 push_function_context ();
10546 /* Still increment function_depth so that we don't GC in the
10547 middle of an expression. */
10549 vec
<tree
> omp_privatization_save
;
10550 save_omp_privatization_clauses (omp_privatization_save
);
10551 /* Clear this in case we're in the middle of a default argument. */
10552 parser
->local_variables_forbidden_p
= false;
10554 /* Finish the function call operator
10556 + late_parsing_for_member
10557 + function_definition_after_declarator
10558 + ctor_initializer_opt_and_function_body */
10560 tree fco
= lambda_function (lambda_expr
);
10561 tree body
= start_lambda_function (fco
, lambda_expr
);
10563 tree compound_stmt
;
10565 matching_braces braces
;
10566 if (!braces
.require_open (parser
))
10569 compound_stmt
= begin_compound_stmt (0);
10571 /* 5.1.1.4 of the standard says:
10572 If a lambda-expression does not include a trailing-return-type, it
10573 is as if the trailing-return-type denotes the following type:
10574 * if the compound-statement is of the form
10575 { return attribute-specifier [opt] expression ; }
10576 the type of the returned expression after lvalue-to-rvalue
10577 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10578 (_conv.array_ 4.2), and function-to-pointer conversion
10580 * otherwise, void. */
10582 /* In a lambda that has neither a lambda-return-type-clause
10583 nor a deducible form, errors should be reported for return statements
10584 in the body. Since we used void as the placeholder return type, parsing
10585 the body as usual will give such desired behavior. */
10586 if (is_auto (TREE_TYPE (TREE_TYPE (fco
)))
10587 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10588 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10590 tree expr
= NULL_TREE
;
10591 cp_id_kind idk
= CP_ID_KIND_NONE
;
10593 /* Parse tentatively in case there's more after the initial return
10595 cp_parser_parse_tentatively (parser
);
10597 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10599 expr
= cp_parser_expression (parser
, &idk
);
10601 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10602 braces
.require_close (parser
);
10604 if (cp_parser_parse_definitely (parser
))
10606 if (!processing_template_decl
)
10608 tree type
= lambda_return_type (expr
);
10609 apply_deduced_return_type (fco
, type
);
10610 if (type
== error_mark_node
)
10611 expr
= error_mark_node
;
10614 /* Will get error here if type not deduced yet. */
10615 finish_return_stmt (expr
);
10623 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10624 cp_parser_label_declaration (parser
);
10625 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10626 braces
.require_close (parser
);
10629 finish_compound_stmt (compound_stmt
);
10632 finish_lambda_function (body
);
10635 restore_omp_privatization_clauses (omp_privatization_save
);
10636 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10638 pop_function_context();
10643 /* Statements [gram.stmt.stmt] */
10645 /* Parse a statement.
10649 expression-statement
10651 selection-statement
10652 iteration-statement
10654 declaration-statement
10661 attribute-specifier-seq (opt) expression-statement
10662 attribute-specifier-seq (opt) compound-statement
10663 attribute-specifier-seq (opt) selection-statement
10664 attribute-specifier-seq (opt) iteration-statement
10665 attribute-specifier-seq (opt) jump-statement
10666 declaration-statement
10667 attribute-specifier-seq (opt) try-block
10670 expression-statement
10678 IN_COMPOUND is true when the statement is nested inside a
10679 cp_parser_compound_statement; this matters for certain pragmas.
10681 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10682 is a (possibly labeled) if statement which is not enclosed in braces
10683 and has an else clause. This is used to implement -Wparentheses.
10685 CHAIN is a vector of if-else-if conditions. */
10688 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10689 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10690 location_t
*loc_after_labels
)
10692 tree statement
, std_attrs
= NULL_TREE
;
10694 location_t statement_location
, attrs_location
;
10699 /* There is no statement yet. */
10700 statement
= NULL_TREE
;
10702 saved_token_sentinel
saved_tokens (parser
->lexer
);
10703 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10704 if (c_dialect_objc ())
10705 /* In obj-c++, seeing '[[' might be the either the beginning of
10706 c++11 attributes, or a nested objc-message-expression. So
10707 let's parse the c++11 attributes tentatively. */
10708 cp_parser_parse_tentatively (parser
);
10709 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10710 if (c_dialect_objc ())
10712 if (!cp_parser_parse_definitely (parser
))
10713 std_attrs
= NULL_TREE
;
10716 /* Peek at the next token. */
10717 token
= cp_lexer_peek_token (parser
->lexer
);
10718 /* Remember the location of the first token in the statement. */
10719 statement_location
= token
->location
;
10720 /* If this is a keyword, then that will often determine what kind of
10721 statement we have. */
10722 if (token
->type
== CPP_KEYWORD
)
10724 enum rid keyword
= token
->keyword
;
10730 /* Looks like a labeled-statement with a case label.
10731 Parse the label, and then use tail recursion to parse
10733 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10734 in_compound
= false;
10739 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10745 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10749 if (!flag_cilkplus
)
10751 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10752 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10753 cp_lexer_consume_token (parser
->lexer
);
10754 statement
= error_mark_node
;
10757 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10764 statement
= cp_parser_jump_statement (parser
);
10767 case RID_CILK_SYNC
:
10768 cp_lexer_consume_token (parser
->lexer
);
10771 tree sync_expr
= build_cilk_sync ();
10772 SET_EXPR_LOCATION (sync_expr
,
10774 statement
= finish_expr_stmt (sync_expr
);
10778 error_at (token
->location
, "-fcilkplus must be enabled to use"
10779 " %<_Cilk_sync%>");
10780 statement
= error_mark_node
;
10782 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10785 /* Objective-C++ exception-handling constructs. */
10788 case RID_AT_FINALLY
:
10789 case RID_AT_SYNCHRONIZED
:
10791 statement
= cp_parser_objc_statement (parser
);
10795 statement
= cp_parser_try_block (parser
);
10798 case RID_NAMESPACE
:
10799 /* This must be a namespace alias definition. */
10800 cp_parser_declaration_statement (parser
);
10803 case RID_TRANSACTION_ATOMIC
:
10804 case RID_TRANSACTION_RELAXED
:
10805 case RID_SYNCHRONIZED
:
10806 case RID_ATOMIC_NOEXCEPT
:
10807 case RID_ATOMIC_CANCEL
:
10808 statement
= cp_parser_transaction (parser
, token
);
10810 case RID_TRANSACTION_CANCEL
:
10811 statement
= cp_parser_transaction_cancel (parser
);
10815 /* It might be a keyword like `int' that can start a
10816 declaration-statement. */
10820 else if (token
->type
== CPP_NAME
)
10822 /* If the next token is a `:', then we are looking at a
10823 labeled-statement. */
10824 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10825 if (token
->type
== CPP_COLON
)
10827 /* Looks like a labeled-statement with an ordinary label.
10828 Parse the label, and then use tail recursion to parse
10831 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10832 in_compound
= false;
10836 /* Anything that starts with a `{' must be a compound-statement. */
10837 else if (token
->type
== CPP_OPEN_BRACE
)
10838 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10839 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10840 a statement all its own. */
10841 else if (token
->type
== CPP_PRAGMA
)
10843 /* Only certain OpenMP pragmas are attached to statements, and thus
10844 are considered statements themselves. All others are not. In
10845 the context of a compound, accept the pragma as a "statement" and
10846 return so that we can check for a close brace. Otherwise we
10847 require a real statement and must go back and read one. */
10849 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10850 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10854 else if (token
->type
== CPP_EOF
)
10856 cp_parser_error (parser
, "expected statement");
10860 /* Everything else must be a declaration-statement or an
10861 expression-statement. Try for the declaration-statement
10862 first, unless we are looking at a `;', in which case we know that
10863 we have an expression-statement. */
10866 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10868 if (std_attrs
!= NULL_TREE
)
10870 /* Attributes should be parsed as part of the the
10871 declaration, so let's un-parse them. */
10872 saved_tokens
.rollback();
10873 std_attrs
= NULL_TREE
;
10876 cp_parser_parse_tentatively (parser
);
10877 /* Try to parse the declaration-statement. */
10878 cp_parser_declaration_statement (parser
);
10879 /* If that worked, we're done. */
10880 if (cp_parser_parse_definitely (parser
))
10883 /* All preceding labels have been parsed at this point. */
10884 if (loc_after_labels
!= NULL
)
10885 *loc_after_labels
= statement_location
;
10887 /* Look for an expression-statement instead. */
10888 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10890 /* Handle [[fallthrough]];. */
10891 if (attribute_fallthrough_p (std_attrs
))
10893 /* The next token after the fallthrough attribute is ';'. */
10894 if (statement
== NULL_TREE
)
10896 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10897 statement
= build_call_expr_internal_loc (statement_location
,
10899 void_type_node
, 0);
10900 finish_expr_stmt (statement
);
10903 warning_at (statement_location
, OPT_Wattributes
,
10904 "%<fallthrough%> attribute not followed by %<;%>");
10905 std_attrs
= NULL_TREE
;
10909 /* Set the line number for the statement. */
10910 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10911 SET_EXPR_LOCATION (statement
, statement_location
);
10913 /* Allow "[[fallthrough]];", but warn otherwise. */
10914 if (std_attrs
!= NULL_TREE
)
10915 warning_at (attrs_location
,
10917 "attributes at the beginning of statement are ignored");
10920 /* Parse the label for a labeled-statement, i.e.
10923 case constant-expression :
10927 case constant-expression ... constant-expression : statement
10929 When a label is parsed without errors, the label is added to the
10930 parse tree by the finish_* functions, so this function doesn't
10931 have to return the label. */
10934 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10937 tree label
= NULL_TREE
;
10938 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10940 /* The next token should be an identifier. */
10941 token
= cp_lexer_peek_token (parser
->lexer
);
10942 if (token
->type
!= CPP_NAME
10943 && token
->type
!= CPP_KEYWORD
)
10945 cp_parser_error (parser
, "expected labeled-statement");
10949 /* Remember whether this case or a user-defined label is allowed to fall
10951 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10953 parser
->colon_corrects_to_scope_p
= false;
10954 switch (token
->keyword
)
10958 tree expr
, expr_hi
;
10959 cp_token
*ellipsis
;
10961 /* Consume the `case' token. */
10962 cp_lexer_consume_token (parser
->lexer
);
10963 /* Parse the constant-expression. */
10964 expr
= cp_parser_constant_expression (parser
);
10965 if (check_for_bare_parameter_packs (expr
))
10966 expr
= error_mark_node
;
10968 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10969 if (ellipsis
->type
== CPP_ELLIPSIS
)
10971 /* Consume the `...' token. */
10972 cp_lexer_consume_token (parser
->lexer
);
10973 expr_hi
= cp_parser_constant_expression (parser
);
10974 if (check_for_bare_parameter_packs (expr_hi
))
10975 expr_hi
= error_mark_node
;
10977 /* We don't need to emit warnings here, as the common code
10978 will do this for us. */
10981 expr_hi
= NULL_TREE
;
10983 if (parser
->in_switch_statement_p
)
10985 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10986 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10987 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10990 error_at (token
->location
,
10991 "case label %qE not within a switch statement",
10997 /* Consume the `default' token. */
10998 cp_lexer_consume_token (parser
->lexer
);
11000 if (parser
->in_switch_statement_p
)
11002 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11003 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11004 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11007 error_at (token
->location
, "case label not within a switch statement");
11011 /* Anything else must be an ordinary label. */
11012 label
= finish_label_stmt (cp_parser_identifier (parser
));
11013 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11014 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11018 /* Require the `:' token. */
11019 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11021 /* An ordinary label may optionally be followed by attributes.
11022 However, this is only permitted if the attributes are then
11023 followed by a semicolon. This is because, for backward
11024 compatibility, when parsing
11025 lab: __attribute__ ((unused)) int i;
11026 we want the attribute to attach to "i", not "lab". */
11027 if (label
!= NULL_TREE
11028 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11031 cp_parser_parse_tentatively (parser
);
11032 attrs
= cp_parser_gnu_attributes_opt (parser
);
11033 if (attrs
== NULL_TREE
11034 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11035 cp_parser_abort_tentative_parse (parser
);
11036 else if (!cp_parser_parse_definitely (parser
))
11039 attributes
= chainon (attributes
, attrs
);
11042 if (attributes
!= NULL_TREE
)
11043 cplus_decl_attributes (&label
, attributes
, 0);
11045 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11048 /* Parse an expression-statement.
11050 expression-statement:
11053 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11054 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11055 indicates whether this expression-statement is part of an
11056 expression statement. */
11059 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11061 tree statement
= NULL_TREE
;
11062 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11063 location_t loc
= token
->location
;
11065 /* There might be attribute fallthrough. */
11066 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11068 /* If the next token is a ';', then there is no expression
11070 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11072 statement
= cp_parser_expression (parser
);
11073 if (statement
== error_mark_node
11074 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11076 cp_parser_skip_to_end_of_block_or_statement (parser
);
11077 return error_mark_node
;
11081 /* Handle [[fallthrough]];. */
11082 if (attribute_fallthrough_p (attr
))
11084 /* The next token after the fallthrough attribute is ';'. */
11085 if (statement
== NULL_TREE
)
11086 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11087 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11088 void_type_node
, 0);
11090 warning_at (loc
, OPT_Wattributes
,
11091 "%<fallthrough%> attribute not followed by %<;%>");
11095 /* Allow "[[fallthrough]];", but warn otherwise. */
11096 if (attr
!= NULL_TREE
)
11097 warning_at (loc
, OPT_Wattributes
,
11098 "attributes at the beginning of statement are ignored");
11100 /* Give a helpful message for "A<T>::type t;" and the like. */
11101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11102 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11104 if (TREE_CODE (statement
) == SCOPE_REF
)
11105 error_at (token
->location
, "need %<typename%> before %qE because "
11106 "%qT is a dependent scope",
11107 statement
, TREE_OPERAND (statement
, 0));
11108 else if (is_overloaded_fn (statement
)
11109 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11112 tree fn
= get_first_fn (statement
);
11113 error_at (token
->location
,
11114 "%<%T::%D%> names the constructor, not the type",
11115 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11119 /* Consume the final `;'. */
11120 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11122 if (in_statement_expr
11123 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11124 /* This is the final expression statement of a statement
11126 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11127 else if (statement
)
11128 statement
= finish_expr_stmt (statement
);
11133 /* Parse a compound-statement.
11135 compound-statement:
11136 { statement-seq [opt] }
11140 compound-statement:
11141 { label-declaration-seq [opt] statement-seq [opt] }
11143 label-declaration-seq:
11145 label-declaration-seq label-declaration
11147 Returns a tree representing the statement. */
11150 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11151 int bcs_flags
, bool function_body
)
11153 tree compound_stmt
;
11154 matching_braces braces
;
11156 /* Consume the `{'. */
11157 if (!braces
.require_open (parser
))
11158 return error_mark_node
;
11159 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11160 && !function_body
&& cxx_dialect
< cxx14
)
11161 pedwarn (input_location
, OPT_Wpedantic
,
11162 "compound-statement in constexpr function");
11163 /* Begin the compound-statement. */
11164 compound_stmt
= begin_compound_stmt (bcs_flags
);
11165 /* If the next keyword is `__label__' we have a label declaration. */
11166 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11167 cp_parser_label_declaration (parser
);
11168 /* Parse an (optional) statement-seq. */
11169 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11170 /* Finish the compound-statement. */
11171 finish_compound_stmt (compound_stmt
);
11172 /* Consume the `}'. */
11173 braces
.require_close (parser
);
11175 return compound_stmt
;
11178 /* Parse an (optional) statement-seq.
11182 statement-seq [opt] statement */
11185 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11187 /* Scan statements until there aren't any more. */
11190 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11192 /* If we are looking at a `}', then we have run out of
11193 statements; the same is true if we have reached the end
11194 of file, or have stumbled upon a stray '@end'. */
11195 if (token
->type
== CPP_CLOSE_BRACE
11196 || token
->type
== CPP_EOF
11197 || token
->type
== CPP_PRAGMA_EOL
11198 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11201 /* If we are in a compound statement and find 'else' then
11202 something went wrong. */
11203 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11205 if (parser
->in_statement
& IN_IF_STMT
)
11209 token
= cp_lexer_consume_token (parser
->lexer
);
11210 error_at (token
->location
, "%<else%> without a previous %<if%>");
11214 /* Parse the statement. */
11215 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11219 /* Return true if we're looking at (init; cond), false otherwise. */
11222 cp_parser_init_statement_p (cp_parser
*parser
)
11224 /* Save tokens so that we can put them back. */
11225 cp_lexer_save_tokens (parser
->lexer
);
11227 /* Look for ';' that is not nested in () or {}. */
11228 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11229 /*recovering=*/false,
11231 /*consume_paren=*/false);
11233 /* Roll back the tokens we skipped. */
11234 cp_lexer_rollback_tokens (parser
->lexer
);
11239 /* Parse a selection-statement.
11241 selection-statement:
11242 if ( init-statement [opt] condition ) statement
11243 if ( init-statement [opt] condition ) statement else statement
11244 switch ( init-statement [opt] condition ) statement
11246 Returns the new IF_STMT or SWITCH_STMT.
11248 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11249 is a (possibly labeled) if statement which is not enclosed in
11250 braces and has an else clause. This is used to implement
11253 CHAIN is a vector of if-else-if conditions. This is used to implement
11254 -Wduplicated-cond. */
11257 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11262 token_indent_info guard_tinfo
;
11267 /* Peek at the next token. */
11268 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11269 guard_tinfo
= get_token_indent_info (token
);
11271 /* See what kind of keyword it is. */
11272 keyword
= token
->keyword
;
11282 if (keyword
== RID_IF
11283 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11287 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11288 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11289 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11290 "with -std=c++17 or -std=gnu++17");
11293 /* Look for the `('. */
11294 matching_parens parens
;
11295 if (!parens
.require_open (parser
))
11297 cp_parser_skip_to_end_of_statement (parser
);
11298 return error_mark_node
;
11301 /* Begin the selection-statement. */
11302 if (keyword
== RID_IF
)
11304 statement
= begin_if_stmt ();
11305 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11308 statement
= begin_switch_stmt ();
11310 /* Parse the optional init-statement. */
11311 if (cp_parser_init_statement_p (parser
))
11314 if (cxx_dialect
< cxx17
)
11315 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11316 "init-statement in selection statements only available "
11317 "with -std=c++17 or -std=gnu++17");
11318 cp_parser_init_statement (parser
, &decl
);
11321 /* Parse the condition. */
11322 condition
= cp_parser_condition (parser
);
11323 /* Look for the `)'. */
11324 if (!parens
.require_close (parser
))
11325 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11326 /*consume_paren=*/true);
11328 if (keyword
== RID_IF
)
11331 unsigned char in_statement
;
11333 /* Add the condition. */
11334 condition
= finish_if_stmt_cond (condition
, statement
);
11336 if (warn_duplicated_cond
)
11337 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11340 /* Parse the then-clause. */
11341 in_statement
= parser
->in_statement
;
11342 parser
->in_statement
|= IN_IF_STMT
;
11344 /* Outside a template, the non-selected branch of a constexpr
11345 if is a 'discarded statement', i.e. unevaluated. */
11346 bool was_discarded
= in_discarded_stmt
;
11347 bool discard_then
= (cx
&& !processing_template_decl
11348 && integer_zerop (condition
));
11351 in_discarded_stmt
= true;
11352 ++c_inhibit_evaluation_warnings
;
11355 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11358 parser
->in_statement
= in_statement
;
11360 finish_then_clause (statement
);
11364 THEN_CLAUSE (statement
) = NULL_TREE
;
11365 in_discarded_stmt
= was_discarded
;
11366 --c_inhibit_evaluation_warnings
;
11369 /* If the next token is `else', parse the else-clause. */
11370 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11373 bool discard_else
= (cx
&& !processing_template_decl
11374 && integer_nonzerop (condition
));
11377 in_discarded_stmt
= true;
11378 ++c_inhibit_evaluation_warnings
;
11382 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11383 /* Consume the `else' keyword. */
11384 cp_lexer_consume_token (parser
->lexer
);
11385 if (warn_duplicated_cond
)
11387 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11391 /* We've got "if (COND) else if (COND2)". Start
11392 the condition chain and add COND as the first
11394 chain
= new vec
<tree
> ();
11395 if (!CONSTANT_CLASS_P (condition
)
11396 && !TREE_SIDE_EFFECTS (condition
))
11398 /* Wrap it in a NOP_EXPR so that we can set the
11399 location of the condition. */
11400 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11402 SET_EXPR_LOCATION (e
, token
->location
);
11403 chain
->safe_push (e
);
11406 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11409 /* This is if-else without subsequent if. Zap the
11410 condition chain; we would have already warned at
11416 begin_else_clause (statement
);
11417 /* Parse the else-clause. */
11418 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11419 guard_tinfo
, chain
);
11421 finish_else_clause (statement
);
11423 /* If we are currently parsing a then-clause, then
11424 IF_P will not be NULL. We set it to true to
11425 indicate that this if statement has an else clause.
11426 This may trigger the Wparentheses warning below
11427 when we get back up to the parent if statement. */
11433 ELSE_CLAUSE (statement
) = NULL_TREE
;
11434 in_discarded_stmt
= was_discarded
;
11435 --c_inhibit_evaluation_warnings
;
11440 /* This if statement does not have an else clause. If
11441 NESTED_IF is true, then the then-clause has an if
11442 statement which does have an else clause. We warn
11443 about the potential ambiguity. */
11445 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11446 "suggest explicit braces to avoid ambiguous"
11448 if (warn_duplicated_cond
)
11450 /* We don't need the condition chain anymore. */
11456 /* Now we're all done with the if-statement. */
11457 finish_if_stmt (statement
);
11461 bool in_switch_statement_p
;
11462 unsigned char in_statement
;
11464 /* Add the condition. */
11465 finish_switch_cond (condition
, statement
);
11467 /* Parse the body of the switch-statement. */
11468 in_switch_statement_p
= parser
->in_switch_statement_p
;
11469 in_statement
= parser
->in_statement
;
11470 parser
->in_switch_statement_p
= true;
11471 parser
->in_statement
|= IN_SWITCH_STMT
;
11472 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11474 parser
->in_switch_statement_p
= in_switch_statement_p
;
11475 parser
->in_statement
= in_statement
;
11477 /* Now we're all done with the switch-statement. */
11478 finish_switch_stmt (statement
);
11486 cp_parser_error (parser
, "expected selection-statement");
11487 return error_mark_node
;
11491 /* Parse a condition.
11495 type-specifier-seq declarator = initializer-clause
11496 type-specifier-seq declarator braced-init-list
11501 type-specifier-seq declarator asm-specification [opt]
11502 attributes [opt] = assignment-expression
11504 Returns the expression that should be tested. */
11507 cp_parser_condition (cp_parser
* parser
)
11509 cp_decl_specifier_seq type_specifiers
;
11510 const char *saved_message
;
11511 int declares_class_or_enum
;
11513 /* Try the declaration first. */
11514 cp_parser_parse_tentatively (parser
);
11515 /* New types are not allowed in the type-specifier-seq for a
11517 saved_message
= parser
->type_definition_forbidden_message
;
11518 parser
->type_definition_forbidden_message
11519 = G_("types may not be defined in conditions");
11520 /* Parse the type-specifier-seq. */
11521 cp_parser_decl_specifier_seq (parser
,
11522 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11524 &declares_class_or_enum
);
11525 /* Restore the saved message. */
11526 parser
->type_definition_forbidden_message
= saved_message
;
11527 /* If all is well, we might be looking at a declaration. */
11528 if (!cp_parser_error_occurred (parser
))
11531 tree asm_specification
;
11533 cp_declarator
*declarator
;
11534 tree initializer
= NULL_TREE
;
11536 /* Parse the declarator. */
11537 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11538 /*ctor_dtor_or_conv_p=*/NULL
,
11539 /*parenthesized_p=*/NULL
,
11540 /*member_p=*/false,
11541 /*friend_p=*/false);
11542 /* Parse the attributes. */
11543 attributes
= cp_parser_attributes_opt (parser
);
11544 /* Parse the asm-specification. */
11545 asm_specification
= cp_parser_asm_specification_opt (parser
);
11546 /* If the next token is not an `=' or '{', then we might still be
11547 looking at an expression. For example:
11551 looks like a decl-specifier-seq and a declarator -- but then
11552 there is no `=', so this is an expression. */
11553 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11554 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11555 cp_parser_simulate_error (parser
);
11557 /* If we did see an `=' or '{', then we are looking at a declaration
11559 if (cp_parser_parse_definitely (parser
))
11562 bool non_constant_p
;
11563 int flags
= LOOKUP_ONLYCONVERTING
;
11565 /* Create the declaration. */
11566 decl
= start_decl (declarator
, &type_specifiers
,
11567 /*initialized_p=*/true,
11568 attributes
, /*prefix_attributes=*/NULL_TREE
,
11571 /* Parse the initializer. */
11572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11574 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11575 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11580 /* Consume the `='. */
11581 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11582 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11584 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11585 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11587 /* Process the initializer. */
11588 cp_finish_decl (decl
,
11589 initializer
, !non_constant_p
,
11594 pop_scope (pushed_scope
);
11596 return convert_from_reference (decl
);
11599 /* If we didn't even get past the declarator successfully, we are
11600 definitely not looking at a declaration. */
11602 cp_parser_abort_tentative_parse (parser
);
11604 /* Otherwise, we are looking at an expression. */
11605 return cp_parser_expression (parser
);
11608 /* Parses a for-statement or range-for-statement until the closing ')',
11612 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11614 tree init
, scope
, decl
;
11617 /* Begin the for-statement. */
11618 scope
= begin_for_scope (&init
);
11620 /* Parse the initialization. */
11621 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11624 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11626 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11630 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11632 /* Normal for loop */
11633 tree condition
= NULL_TREE
;
11634 tree expression
= NULL_TREE
;
11637 stmt
= begin_for_stmt (scope
, init
);
11638 /* The init-statement has already been parsed in
11639 cp_parser_init_statement, so no work is needed here. */
11640 finish_init_stmt (stmt
);
11642 /* If there's a condition, process it. */
11643 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11644 condition
= cp_parser_condition (parser
);
11647 cp_parser_error (parser
, "missing loop condition in loop with "
11648 "%<GCC ivdep%> pragma");
11649 condition
= error_mark_node
;
11651 finish_for_cond (condition
, stmt
, ivdep
);
11652 /* Look for the `;'. */
11653 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11655 /* If there's an expression, process it. */
11656 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11657 expression
= cp_parser_expression (parser
);
11658 finish_for_expr (expression
, stmt
);
11663 /* Tries to parse a range-based for-statement:
11666 decl-specifier-seq declarator : expression
11668 The decl-specifier-seq declarator and the `:' are already parsed by
11669 cp_parser_init_statement. If processing_template_decl it returns a
11670 newly created RANGE_FOR_STMT; if not, it is converted to a
11671 regular FOR_STMT. */
11674 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11677 tree stmt
, range_expr
;
11678 auto_vec
<cxx_binding
*, 16> bindings
;
11679 auto_vec
<tree
, 16> names
;
11680 tree decomp_first_name
= NULL_TREE
;
11681 unsigned int decomp_cnt
= 0;
11683 /* Get the range declaration momentarily out of the way so that
11684 the range expression doesn't clash with it. */
11685 if (range_decl
!= error_mark_node
)
11687 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11689 tree v
= DECL_VALUE_EXPR (range_decl
);
11690 /* For decomposition declaration get all of the corresponding
11691 declarations out of the way. */
11692 if (TREE_CODE (v
) == ARRAY_REF
11693 && VAR_P (TREE_OPERAND (v
, 0))
11694 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11696 tree d
= range_decl
;
11697 range_decl
= TREE_OPERAND (v
, 0);
11698 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11699 decomp_first_name
= d
;
11700 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11702 tree name
= DECL_NAME (d
);
11703 names
.safe_push (name
);
11704 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11705 IDENTIFIER_BINDING (name
)
11706 = IDENTIFIER_BINDING (name
)->previous
;
11710 if (names
.is_empty ())
11712 tree name
= DECL_NAME (range_decl
);
11713 names
.safe_push (name
);
11714 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11715 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11721 bool expr_non_constant_p
;
11722 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11725 range_expr
= cp_parser_expression (parser
);
11727 /* Put the range declaration(s) back into scope. */
11728 for (unsigned int i
= 0; i
< names
.length (); i
++)
11730 cxx_binding
*binding
= bindings
[i
];
11731 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11732 IDENTIFIER_BINDING (names
[i
]) = binding
;
11735 /* If in template, STMT is converted to a normal for-statement
11736 at instantiation. If not, it is done just ahead. */
11737 if (processing_template_decl
)
11739 if (check_for_bare_parameter_packs (range_expr
))
11740 range_expr
= error_mark_node
;
11741 stmt
= begin_range_for_stmt (scope
, init
);
11743 RANGE_FOR_IVDEP (stmt
) = 1;
11744 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11745 if (!type_dependent_expression_p (range_expr
)
11746 /* do_auto_deduction doesn't mess with template init-lists. */
11747 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11748 do_range_for_auto_deduction (range_decl
, range_expr
);
11752 stmt
= begin_for_stmt (scope
, init
);
11753 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11754 decomp_first_name
, decomp_cnt
, ivdep
);
11759 /* Subroutine of cp_convert_range_for: given the initializer expression,
11760 builds up the range temporary. */
11763 build_range_temp (tree range_expr
)
11765 tree range_type
, range_temp
;
11767 /* Find out the type deduced by the declaration
11768 `auto &&__range = range_expr'. */
11769 range_type
= cp_build_reference_type (make_auto (), true);
11770 range_type
= do_auto_deduction (range_type
, range_expr
,
11771 type_uses_auto (range_type
));
11773 /* Create the __range variable. */
11774 range_temp
= build_decl (input_location
, VAR_DECL
,
11775 get_identifier ("__for_range"), range_type
);
11776 TREE_USED (range_temp
) = 1;
11777 DECL_ARTIFICIAL (range_temp
) = 1;
11782 /* Used by cp_parser_range_for in template context: we aren't going to
11783 do a full conversion yet, but we still need to resolve auto in the
11784 type of the for-range-declaration if present. This is basically
11785 a shortcut version of cp_convert_range_for. */
11788 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11790 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11793 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11794 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11795 iter_type
= (cp_parser_perform_range_for_lookup
11796 (range_temp
, &begin_dummy
, &end_dummy
));
11799 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11801 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
11802 tf_warning_or_error
);
11803 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11804 iter_decl
, auto_node
);
11809 /* Converts a range-based for-statement into a normal
11810 for-statement, as per the definition.
11812 for (RANGE_DECL : RANGE_EXPR)
11815 should be equivalent to:
11818 auto &&__range = RANGE_EXPR;
11819 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11823 RANGE_DECL = *__begin;
11828 If RANGE_EXPR is an array:
11829 BEGIN_EXPR = __range
11830 END_EXPR = __range + ARRAY_SIZE(__range)
11831 Else if RANGE_EXPR has a member 'begin' or 'end':
11832 BEGIN_EXPR = __range.begin()
11833 END_EXPR = __range.end()
11835 BEGIN_EXPR = begin(__range)
11836 END_EXPR = end(__range);
11838 If __range has a member 'begin' but not 'end', or vice versa, we must
11839 still use the second alternative (it will surely fail, however).
11840 When calling begin()/end() in the third alternative we must use
11841 argument dependent lookup, but always considering 'std' as an associated
11845 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11846 tree decomp_first_name
, unsigned int decomp_cnt
,
11850 tree iter_type
, begin_expr
, end_expr
;
11851 tree condition
, expression
;
11853 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11854 /* If an error happened previously do nothing or else a lot of
11855 unhelpful errors would be issued. */
11856 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11861 if (VAR_P (range_expr
)
11862 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11863 /* Can't bind a reference to an array of runtime bound. */
11864 range_temp
= range_expr
;
11867 range_temp
= build_range_temp (range_expr
);
11868 pushdecl (range_temp
);
11869 cp_finish_decl (range_temp
, range_expr
,
11870 /*is_constant_init*/false, NULL_TREE
,
11871 LOOKUP_ONLYCONVERTING
);
11872 range_temp
= convert_from_reference (range_temp
);
11874 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11875 &begin_expr
, &end_expr
);
11878 /* The new for initialization statement. */
11879 begin
= build_decl (input_location
, VAR_DECL
,
11880 get_identifier ("__for_begin"), iter_type
);
11881 TREE_USED (begin
) = 1;
11882 DECL_ARTIFICIAL (begin
) = 1;
11884 cp_finish_decl (begin
, begin_expr
,
11885 /*is_constant_init*/false, NULL_TREE
,
11886 LOOKUP_ONLYCONVERTING
);
11888 if (cxx_dialect
>= cxx17
)
11889 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11890 end
= build_decl (input_location
, VAR_DECL
,
11891 get_identifier ("__for_end"), iter_type
);
11892 TREE_USED (end
) = 1;
11893 DECL_ARTIFICIAL (end
) = 1;
11895 cp_finish_decl (end
, end_expr
,
11896 /*is_constant_init*/false, NULL_TREE
,
11897 LOOKUP_ONLYCONVERTING
);
11899 finish_init_stmt (statement
);
11901 /* The new for condition. */
11902 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11905 NULL
, tf_warning_or_error
);
11906 finish_for_cond (condition
, statement
, ivdep
);
11908 /* The new increment expression. */
11909 expression
= finish_unary_op_expr (input_location
,
11910 PREINCREMENT_EXPR
, begin
,
11911 tf_warning_or_error
);
11912 finish_for_expr (expression
, statement
);
11914 /* The declaration is initialized with *__begin inside the loop body. */
11915 cp_finish_decl (range_decl
,
11916 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11917 tf_warning_or_error
),
11918 /*is_constant_init*/false, NULL_TREE
,
11919 LOOKUP_ONLYCONVERTING
);
11920 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11921 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11926 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11927 We need to solve both at the same time because the method used
11928 depends on the existence of members begin or end.
11929 Returns the type deduced for the iterator expression. */
11932 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11934 if (error_operand_p (range
))
11936 *begin
= *end
= error_mark_node
;
11937 return error_mark_node
;
11940 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11942 error ("range-based %<for%> expression of type %qT "
11943 "has incomplete type", TREE_TYPE (range
));
11944 *begin
= *end
= error_mark_node
;
11945 return error_mark_node
;
11947 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11949 /* If RANGE is an array, we will use pointer arithmetic. */
11950 *begin
= decay_conversion (range
, tf_warning_or_error
);
11951 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11953 array_type_nelts_top (TREE_TYPE (range
)),
11955 return TREE_TYPE (*begin
);
11959 /* If it is not an array, we must do a bit of magic. */
11960 tree id_begin
, id_end
;
11961 tree member_begin
, member_end
;
11963 *begin
= *end
= error_mark_node
;
11965 id_begin
= get_identifier ("begin");
11966 id_end
= get_identifier ("end");
11967 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11968 /*protect=*/2, /*want_type=*/false,
11969 tf_warning_or_error
);
11970 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11971 /*protect=*/2, /*want_type=*/false,
11972 tf_warning_or_error
);
11974 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11976 /* Use the member functions. */
11977 if (member_begin
!= NULL_TREE
)
11978 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11980 error ("range-based %<for%> expression of type %qT has an "
11981 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11983 if (member_end
!= NULL_TREE
)
11984 *end
= cp_parser_range_for_member_function (range
, id_end
);
11986 error ("range-based %<for%> expression of type %qT has a "
11987 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11991 /* Use global functions with ADL. */
11992 vec
<tree
, va_gc
> *vec
;
11993 vec
= make_tree_vector ();
11995 vec_safe_push (vec
, range
);
11997 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11998 tf_warning_or_error
);
11999 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12000 tf_warning_or_error
);
12001 member_end
= perform_koenig_lookup (id_end
, vec
,
12002 tf_warning_or_error
);
12003 *end
= finish_call_expr (member_end
, &vec
, false, true,
12004 tf_warning_or_error
);
12006 release_tree_vector (vec
);
12009 /* Last common checks. */
12010 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12012 /* If one of the expressions is an error do no more checks. */
12013 *begin
= *end
= error_mark_node
;
12014 return error_mark_node
;
12016 else if (type_dependent_expression_p (*begin
)
12017 || type_dependent_expression_p (*end
))
12018 /* Can happen, when, eg, in a template context, Koenig lookup
12019 can't resolve begin/end (c++/58503). */
12023 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12024 /* The unqualified type of the __begin and __end temporaries should
12025 be the same, as required by the multiple auto declaration. */
12026 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12028 if (cxx_dialect
>= cxx17
12029 && (build_x_binary_op (input_location
, NE_EXPR
,
12030 *begin
, ERROR_MARK
,
12033 != error_mark_node
))
12034 /* P0184R0 allows __begin and __end to have different types,
12035 but make sure they are comparable so we can give a better
12038 error ("inconsistent begin/end types in range-based %<for%> "
12039 "statement: %qT and %qT",
12040 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12047 /* Helper function for cp_parser_perform_range_for_lookup.
12048 Builds a tree for RANGE.IDENTIFIER(). */
12051 cp_parser_range_for_member_function (tree range
, tree identifier
)
12054 vec
<tree
, va_gc
> *vec
;
12056 member
= finish_class_member_access_expr (range
, identifier
,
12057 false, tf_warning_or_error
);
12058 if (member
== error_mark_node
)
12059 return error_mark_node
;
12061 vec
= make_tree_vector ();
12062 res
= finish_call_expr (member
, &vec
,
12063 /*disallow_virtual=*/false,
12064 /*koenig_p=*/false,
12065 tf_warning_or_error
);
12066 release_tree_vector (vec
);
12070 /* Parse an iteration-statement.
12072 iteration-statement:
12073 while ( condition ) statement
12074 do statement while ( expression ) ;
12075 for ( init-statement condition [opt] ; expression [opt] )
12078 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12081 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
12086 unsigned char in_statement
;
12087 token_indent_info guard_tinfo
;
12089 /* Peek at the next token. */
12090 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12092 return error_mark_node
;
12094 guard_tinfo
= get_token_indent_info (token
);
12096 /* Remember whether or not we are already within an iteration
12098 in_statement
= parser
->in_statement
;
12100 /* See what kind of keyword it is. */
12101 keyword
= token
->keyword
;
12108 /* Begin the while-statement. */
12109 statement
= begin_while_stmt ();
12110 /* Look for the `('. */
12111 matching_parens parens
;
12112 parens
.require_open (parser
);
12113 /* Parse the condition. */
12114 condition
= cp_parser_condition (parser
);
12115 finish_while_stmt_cond (condition
, statement
, ivdep
);
12116 /* Look for the `)'. */
12117 parens
.require_close (parser
);
12118 /* Parse the dependent statement. */
12119 parser
->in_statement
= IN_ITERATION_STMT
;
12120 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12121 parser
->in_statement
= in_statement
;
12122 /* We're done with the while-statement. */
12123 finish_while_stmt (statement
);
12131 /* Begin the do-statement. */
12132 statement
= begin_do_stmt ();
12133 /* Parse the body of the do-statement. */
12134 parser
->in_statement
= IN_ITERATION_STMT
;
12135 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12136 parser
->in_statement
= in_statement
;
12137 finish_do_body (statement
);
12138 /* Look for the `while' keyword. */
12139 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12140 /* Look for the `('. */
12141 matching_parens parens
;
12142 parens
.require_open (parser
);
12143 /* Parse the expression. */
12144 expression
= cp_parser_expression (parser
);
12145 /* We're done with the do-statement. */
12146 finish_do_stmt (expression
, statement
, ivdep
);
12147 /* Look for the `)'. */
12148 parens
.require_close (parser
);
12149 /* Look for the `;'. */
12150 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12156 /* Look for the `('. */
12157 matching_parens parens
;
12158 parens
.require_open (parser
);
12160 statement
= cp_parser_for (parser
, ivdep
);
12162 /* Look for the `)'. */
12163 parens
.require_close (parser
);
12165 /* Parse the body of the for-statement. */
12166 parser
->in_statement
= IN_ITERATION_STMT
;
12167 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12168 parser
->in_statement
= in_statement
;
12170 /* We're done with the for-statement. */
12171 finish_for_stmt (statement
);
12176 cp_parser_error (parser
, "expected iteration-statement");
12177 statement
= error_mark_node
;
12184 /* Parse a init-statement or the declarator of a range-based-for.
12185 Returns true if a range-based-for declaration is seen.
12188 expression-statement
12189 simple-declaration */
12192 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12194 /* If the next token is a `;', then we have an empty
12195 expression-statement. Grammatically, this is also a
12196 simple-declaration, but an invalid one, because it does not
12197 declare anything. Therefore, if we did not handle this case
12198 specially, we would issue an error message about an invalid
12200 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12202 bool is_range_for
= false;
12203 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12205 /* A colon is used in range-based for. */
12206 parser
->colon_corrects_to_scope_p
= false;
12208 /* We're going to speculatively look for a declaration, falling back
12209 to an expression, if necessary. */
12210 cp_parser_parse_tentatively (parser
);
12211 /* Parse the declaration. */
12212 cp_parser_simple_declaration (parser
,
12213 /*function_definition_allowed_p=*/false,
12215 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12218 /* It is a range-for, consume the ':' */
12219 cp_lexer_consume_token (parser
->lexer
);
12220 is_range_for
= true;
12221 if (cxx_dialect
< cxx11
)
12223 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12224 "range-based %<for%> loops only available with "
12225 "-std=c++11 or -std=gnu++11");
12226 *decl
= error_mark_node
;
12230 /* The ';' is not consumed yet because we told
12231 cp_parser_simple_declaration not to. */
12232 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12234 if (cp_parser_parse_definitely (parser
))
12235 return is_range_for
;
12236 /* If the tentative parse failed, then we shall need to look for an
12237 expression-statement. */
12239 /* If we are here, it is an expression-statement. */
12240 cp_parser_expression_statement (parser
, NULL_TREE
);
12244 /* Parse a jump-statement.
12249 return expression [opt] ;
12250 return braced-init-list ;
12256 goto * expression ;
12258 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12261 cp_parser_jump_statement (cp_parser
* parser
)
12263 tree statement
= error_mark_node
;
12266 unsigned char in_statement
;
12268 /* Peek at the next token. */
12269 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12271 return error_mark_node
;
12273 /* See what kind of keyword it is. */
12274 keyword
= token
->keyword
;
12278 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12279 switch (in_statement
)
12282 error_at (token
->location
, "break statement not within loop or switch");
12285 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12286 || in_statement
== IN_ITERATION_STMT
);
12287 statement
= finish_break_stmt ();
12288 if (in_statement
== IN_ITERATION_STMT
)
12289 break_maybe_infinite_loop ();
12292 error_at (token
->location
, "invalid exit from OpenMP structured block");
12295 error_at (token
->location
, "break statement used with OpenMP for loop");
12297 case IN_CILK_SIMD_FOR
:
12298 error_at (token
->location
, "break statement used with Cilk Plus for loop");
12301 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12305 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12308 error_at (token
->location
, "continue statement not within a loop");
12310 case IN_CILK_SIMD_FOR
:
12311 error_at (token
->location
,
12312 "continue statement within %<#pragma simd%> loop body");
12313 /* Fall through. */
12314 case IN_ITERATION_STMT
:
12316 statement
= finish_continue_stmt ();
12319 error_at (token
->location
, "invalid exit from OpenMP structured block");
12322 gcc_unreachable ();
12324 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12330 bool expr_non_constant_p
;
12332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12334 cp_lexer_set_source_position (parser
->lexer
);
12335 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12336 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12338 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12339 expr
= cp_parser_expression (parser
);
12341 /* If the next token is a `;', then there is no
12344 /* Build the return-statement. */
12345 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12346 /* Don't deduce from a discarded return statement. */;
12348 statement
= finish_return_stmt (expr
);
12349 /* Look for the final `;'. */
12350 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12355 if (parser
->in_function_body
12356 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12358 error ("%<goto%> in %<constexpr%> function");
12359 cp_function_chain
->invalid_constexpr
= true;
12362 /* Create the goto-statement. */
12363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12365 /* Issue a warning about this use of a GNU extension. */
12366 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12367 /* Consume the '*' token. */
12368 cp_lexer_consume_token (parser
->lexer
);
12369 /* Parse the dependent expression. */
12370 finish_goto_stmt (cp_parser_expression (parser
));
12373 finish_goto_stmt (cp_parser_identifier (parser
));
12374 /* Look for the final `;'. */
12375 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12379 cp_parser_error (parser
, "expected jump-statement");
12386 /* Parse a declaration-statement.
12388 declaration-statement:
12389 block-declaration */
12392 cp_parser_declaration_statement (cp_parser
* parser
)
12396 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12397 p
= obstack_alloc (&declarator_obstack
, 0);
12399 /* Parse the block-declaration. */
12400 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12402 /* Free any declarators allocated. */
12403 obstack_free (&declarator_obstack
, p
);
12406 /* Some dependent statements (like `if (cond) statement'), are
12407 implicitly in their own scope. In other words, if the statement is
12408 a single statement (as opposed to a compound-statement), it is
12409 none-the-less treated as if it were enclosed in braces. Any
12410 declarations appearing in the dependent statement are out of scope
12411 after control passes that point. This function parses a statement,
12412 but ensures that is in its own scope, even if it is not a
12413 compound-statement.
12415 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12416 is a (possibly labeled) if statement which is not enclosed in
12417 braces and has an else clause. This is used to implement
12420 CHAIN is a vector of if-else-if conditions. This is used to implement
12423 Returns the new statement. */
12426 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12427 const token_indent_info
&guard_tinfo
,
12431 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12432 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12433 token_indent_info body_tinfo
12434 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12439 /* Mark if () ; with a special NOP_EXPR. */
12440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12442 cp_lexer_consume_token (parser
->lexer
);
12443 statement
= add_stmt (build_empty_stmt (body_loc
));
12445 if (guard_tinfo
.keyword
== RID_IF
12446 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12447 warning_at (body_loc
, OPT_Wempty_body
,
12448 "suggest braces around empty body in an %<if%> statement");
12449 else if (guard_tinfo
.keyword
== RID_ELSE
)
12450 warning_at (body_loc
, OPT_Wempty_body
,
12451 "suggest braces around empty body in an %<else%> statement");
12453 /* if a compound is opened, we simply parse the statement directly. */
12454 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12455 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12456 /* If the token is not a `{', then we must take special action. */
12459 /* Create a compound-statement. */
12460 statement
= begin_compound_stmt (0);
12461 /* Parse the dependent-statement. */
12462 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12463 &body_loc_after_labels
);
12464 /* Finish the dummy compound-statement. */
12465 finish_compound_stmt (statement
);
12468 token_indent_info next_tinfo
12469 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12470 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12472 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12473 && next_tinfo
.type
!= CPP_SEMICOLON
)
12474 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12475 guard_tinfo
.location
, guard_tinfo
.keyword
);
12477 /* Return the statement. */
12481 /* For some dependent statements (like `while (cond) statement'), we
12482 have already created a scope. Therefore, even if the dependent
12483 statement is a compound-statement, we do not want to create another
12487 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12488 const token_indent_info
&guard_tinfo
)
12490 /* If the token is a `{', then we must take special action. */
12491 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12493 token_indent_info body_tinfo
12494 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12495 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12497 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12498 &loc_after_labels
);
12499 token_indent_info next_tinfo
12500 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12501 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12503 if (loc_after_labels
!= UNKNOWN_LOCATION
12504 && next_tinfo
.type
!= CPP_SEMICOLON
)
12505 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12506 guard_tinfo
.location
,
12507 guard_tinfo
.keyword
);
12511 /* Avoid calling cp_parser_compound_statement, so that we
12512 don't create a new scope. Do everything else by hand. */
12513 matching_braces braces
;
12514 braces
.require_open (parser
);
12515 /* If the next keyword is `__label__' we have a label declaration. */
12516 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12517 cp_parser_label_declaration (parser
);
12518 /* Parse an (optional) statement-seq. */
12519 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12520 braces
.require_close (parser
);
12524 /* Declarations [gram.dcl.dcl] */
12526 /* Parse an optional declaration-sequence.
12530 declaration-seq declaration */
12533 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12539 token
= cp_lexer_peek_token (parser
->lexer
);
12541 if (token
->type
== CPP_CLOSE_BRACE
12542 || token
->type
== CPP_EOF
12543 || token
->type
== CPP_PRAGMA_EOL
)
12546 if (token
->type
== CPP_SEMICOLON
)
12548 /* A declaration consisting of a single semicolon is
12549 invalid. Allow it unless we're being pedantic. */
12550 cp_lexer_consume_token (parser
->lexer
);
12551 if (!in_system_header_at (input_location
))
12552 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12556 /* If we're entering or exiting a region that's implicitly
12557 extern "C", modify the lang context appropriately. */
12558 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12560 push_lang_context (lang_name_c
);
12561 parser
->implicit_extern_c
= true;
12563 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12565 pop_lang_context ();
12566 parser
->implicit_extern_c
= false;
12569 if (token
->type
== CPP_PRAGMA
)
12571 /* A top-level declaration can consist solely of a #pragma.
12572 A nested declaration cannot, so this is done here and not
12573 in cp_parser_declaration. (A #pragma at block scope is
12574 handled in cp_parser_statement.) */
12575 cp_parser_pragma (parser
, pragma_external
, NULL
);
12579 /* Parse the declaration itself. */
12580 cp_parser_declaration (parser
);
12584 /* Parse a declaration.
12588 function-definition
12589 template-declaration
12590 explicit-instantiation
12591 explicit-specialization
12592 linkage-specification
12593 namespace-definition
12601 __extension__ declaration */
12604 cp_parser_declaration (cp_parser
* parser
)
12608 int saved_pedantic
;
12610 tree attributes
= NULL_TREE
;
12612 /* Check for the `__extension__' keyword. */
12613 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12615 /* Parse the qualified declaration. */
12616 cp_parser_declaration (parser
);
12617 /* Restore the PEDANTIC flag. */
12618 pedantic
= saved_pedantic
;
12623 /* Try to figure out what kind of declaration is present. */
12624 token1
= *cp_lexer_peek_token (parser
->lexer
);
12626 if (token1
.type
!= CPP_EOF
)
12627 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12630 token2
.type
= CPP_EOF
;
12631 token2
.keyword
= RID_MAX
;
12634 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12635 p
= obstack_alloc (&declarator_obstack
, 0);
12637 /* If the next token is `extern' and the following token is a string
12638 literal, then we have a linkage specification. */
12639 if (token1
.keyword
== RID_EXTERN
12640 && cp_parser_is_pure_string_literal (&token2
))
12641 cp_parser_linkage_specification (parser
);
12642 /* If the next token is `template', then we have either a template
12643 declaration, an explicit instantiation, or an explicit
12645 else if (token1
.keyword
== RID_TEMPLATE
)
12647 /* `template <>' indicates a template specialization. */
12648 if (token2
.type
== CPP_LESS
12649 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12650 cp_parser_explicit_specialization (parser
);
12651 /* `template <' indicates a template declaration. */
12652 else if (token2
.type
== CPP_LESS
)
12653 cp_parser_template_declaration (parser
, /*member_p=*/false);
12654 /* Anything else must be an explicit instantiation. */
12656 cp_parser_explicit_instantiation (parser
);
12658 /* If the next token is `export', then we have a template
12660 else if (token1
.keyword
== RID_EXPORT
)
12661 cp_parser_template_declaration (parser
, /*member_p=*/false);
12662 /* If the next token is `extern', 'static' or 'inline' and the one
12663 after that is `template', we have a GNU extended explicit
12664 instantiation directive. */
12665 else if (cp_parser_allow_gnu_extensions_p (parser
)
12666 && (token1
.keyword
== RID_EXTERN
12667 || token1
.keyword
== RID_STATIC
12668 || token1
.keyword
== RID_INLINE
)
12669 && token2
.keyword
== RID_TEMPLATE
)
12670 cp_parser_explicit_instantiation (parser
);
12671 /* If the next token is `namespace', check for a named or unnamed
12672 namespace definition. */
12673 else if (token1
.keyword
== RID_NAMESPACE
12674 && (/* A named namespace definition. */
12675 (token2
.type
== CPP_NAME
12676 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12678 || (token2
.type
== CPP_OPEN_SQUARE
12679 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12680 == CPP_OPEN_SQUARE
)
12681 /* An unnamed namespace definition. */
12682 || token2
.type
== CPP_OPEN_BRACE
12683 || token2
.keyword
== RID_ATTRIBUTE
))
12684 cp_parser_namespace_definition (parser
);
12685 /* An inline (associated) namespace definition. */
12686 else if (token1
.keyword
== RID_INLINE
12687 && token2
.keyword
== RID_NAMESPACE
)
12688 cp_parser_namespace_definition (parser
);
12689 /* Objective-C++ declaration/definition. */
12690 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12691 cp_parser_objc_declaration (parser
, NULL_TREE
);
12692 else if (c_dialect_objc ()
12693 && token1
.keyword
== RID_ATTRIBUTE
12694 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12695 cp_parser_objc_declaration (parser
, attributes
);
12696 /* At this point we may have a template declared by a concept
12698 else if (flag_concepts
12699 && cp_parser_template_declaration_after_export (parser
,
12700 /*member_p=*/false))
12703 /* Try to parse a block-declaration, or a function-definition. */
12704 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12706 /* Free any declarators allocated. */
12707 obstack_free (&declarator_obstack
, p
);
12710 /* Parse a block-declaration.
12715 namespace-alias-definition
12722 __extension__ block-declaration
12727 static_assert-declaration
12729 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12730 part of a declaration-statement. */
12733 cp_parser_block_declaration (cp_parser
*parser
,
12737 int saved_pedantic
;
12739 /* Check for the `__extension__' keyword. */
12740 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12742 /* Parse the qualified declaration. */
12743 cp_parser_block_declaration (parser
, statement_p
);
12744 /* Restore the PEDANTIC flag. */
12745 pedantic
= saved_pedantic
;
12750 /* Peek at the next token to figure out which kind of declaration is
12752 token1
= cp_lexer_peek_token (parser
->lexer
);
12754 /* If the next keyword is `asm', we have an asm-definition. */
12755 if (token1
->keyword
== RID_ASM
)
12758 cp_parser_commit_to_tentative_parse (parser
);
12759 cp_parser_asm_definition (parser
);
12761 /* If the next keyword is `namespace', we have a
12762 namespace-alias-definition. */
12763 else if (token1
->keyword
== RID_NAMESPACE
)
12764 cp_parser_namespace_alias_definition (parser
);
12765 /* If the next keyword is `using', we have a
12766 using-declaration, a using-directive, or an alias-declaration. */
12767 else if (token1
->keyword
== RID_USING
)
12772 cp_parser_commit_to_tentative_parse (parser
);
12773 /* If the token after `using' is `namespace', then we have a
12774 using-directive. */
12775 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12776 if (token2
->keyword
== RID_NAMESPACE
)
12777 cp_parser_using_directive (parser
);
12778 /* If the second token after 'using' is '=', then we have an
12779 alias-declaration. */
12780 else if (cxx_dialect
>= cxx11
12781 && token2
->type
== CPP_NAME
12782 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12783 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12784 cp_parser_alias_declaration (parser
);
12785 /* Otherwise, it's a using-declaration. */
12787 cp_parser_using_declaration (parser
,
12788 /*access_declaration_p=*/false);
12790 /* If the next keyword is `__label__' we have a misplaced label
12792 else if (token1
->keyword
== RID_LABEL
)
12794 cp_lexer_consume_token (parser
->lexer
);
12795 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12796 cp_parser_skip_to_end_of_statement (parser
);
12797 /* If the next token is now a `;', consume it. */
12798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12799 cp_lexer_consume_token (parser
->lexer
);
12801 /* If the next token is `static_assert' we have a static assertion. */
12802 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12803 cp_parser_static_assert (parser
, /*member_p=*/false);
12804 /* Anything else must be a simple-declaration. */
12806 cp_parser_simple_declaration (parser
, !statement_p
,
12807 /*maybe_range_for_decl*/NULL
);
12810 /* Parse a simple-declaration.
12812 simple-declaration:
12813 decl-specifier-seq [opt] init-declarator-list [opt] ;
12814 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12815 brace-or-equal-initializer ;
12817 init-declarator-list:
12819 init-declarator-list , init-declarator
12821 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12822 function-definition as a simple-declaration.
12824 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12825 parsed declaration if it is an uninitialized single declarator not followed
12826 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12827 if present, will not be consumed. */
12830 cp_parser_simple_declaration (cp_parser
* parser
,
12831 bool function_definition_allowed_p
,
12832 tree
*maybe_range_for_decl
)
12834 cp_decl_specifier_seq decl_specifiers
;
12835 int declares_class_or_enum
;
12836 bool saw_declarator
;
12837 location_t comma_loc
= UNKNOWN_LOCATION
;
12838 location_t init_loc
= UNKNOWN_LOCATION
;
12840 if (maybe_range_for_decl
)
12841 *maybe_range_for_decl
= NULL_TREE
;
12843 /* Defer access checks until we know what is being declared; the
12844 checks for names appearing in the decl-specifier-seq should be
12845 done as if we were in the scope of the thing being declared. */
12846 push_deferring_access_checks (dk_deferred
);
12848 /* Parse the decl-specifier-seq. We have to keep track of whether
12849 or not the decl-specifier-seq declares a named class or
12850 enumeration type, since that is the only case in which the
12851 init-declarator-list is allowed to be empty.
12855 In a simple-declaration, the optional init-declarator-list can be
12856 omitted only when declaring a class or enumeration, that is when
12857 the decl-specifier-seq contains either a class-specifier, an
12858 elaborated-type-specifier, or an enum-specifier. */
12859 cp_parser_decl_specifier_seq (parser
,
12860 CP_PARSER_FLAGS_OPTIONAL
,
12862 &declares_class_or_enum
);
12863 /* We no longer need to defer access checks. */
12864 stop_deferring_access_checks ();
12866 /* In a block scope, a valid declaration must always have a
12867 decl-specifier-seq. By not trying to parse declarators, we can
12868 resolve the declaration/expression ambiguity more quickly. */
12869 if (!function_definition_allowed_p
12870 && !decl_specifiers
.any_specifiers_p
)
12872 cp_parser_error (parser
, "expected declaration");
12876 /* If the next two tokens are both identifiers, the code is
12877 erroneous. The usual cause of this situation is code like:
12881 where "T" should name a type -- but does not. */
12882 if (!decl_specifiers
.any_type_specifiers_p
12883 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12885 /* If parsing tentatively, we should commit; we really are
12886 looking at a declaration. */
12887 cp_parser_commit_to_tentative_parse (parser
);
12892 /* If we have seen at least one decl-specifier, and the next token
12893 is not a parenthesis, then we must be looking at a declaration.
12894 (After "int (" we might be looking at a functional cast.) */
12895 if (decl_specifiers
.any_specifiers_p
12896 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12897 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12898 && !cp_parser_error_occurred (parser
))
12899 cp_parser_commit_to_tentative_parse (parser
);
12901 /* Look for C++17 decomposition declaration. */
12902 for (size_t n
= 1; ; n
++)
12903 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12904 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12906 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12907 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12908 && decl_specifiers
.any_specifiers_p
)
12911 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12912 maybe_range_for_decl
,
12915 /* The next token should be either a `,' or a `;'. */
12916 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12917 /* If it's a `;', we are done. */
12918 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12920 /* Anything else is an error. */
12923 /* If we have already issued an error message we don't need
12924 to issue another one. */
12925 if ((decl
!= error_mark_node
12926 && DECL_INITIAL (decl
) != error_mark_node
)
12927 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12928 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12929 /* Skip tokens until we reach the end of the statement. */
12930 cp_parser_skip_to_end_of_statement (parser
);
12931 /* If the next token is now a `;', consume it. */
12932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12933 cp_lexer_consume_token (parser
->lexer
);
12941 bool auto_specifier_p
;
12942 /* NULL_TREE if both variable and function declaration are allowed,
12943 error_mark_node if function declaration are not allowed and
12944 a FUNCTION_DECL that should be diagnosed if it is followed by
12945 variable declarations. */
12946 tree auto_function_declaration
;
12948 last_type
= NULL_TREE
;
12950 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12951 auto_function_declaration
= NULL_TREE
;
12953 /* Keep going until we hit the `;' at the end of the simple
12955 saw_declarator
= false;
12956 while (cp_lexer_next_token_is_not (parser
->lexer
,
12960 bool function_definition_p
;
12962 tree auto_result
= NULL_TREE
;
12964 if (saw_declarator
)
12966 /* If we are processing next declarator, comma is expected */
12967 token
= cp_lexer_peek_token (parser
->lexer
);
12968 gcc_assert (token
->type
== CPP_COMMA
);
12969 cp_lexer_consume_token (parser
->lexer
);
12970 if (maybe_range_for_decl
)
12972 *maybe_range_for_decl
= error_mark_node
;
12973 if (comma_loc
== UNKNOWN_LOCATION
)
12974 comma_loc
= token
->location
;
12978 saw_declarator
= true;
12980 /* Parse the init-declarator. */
12981 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12983 function_definition_allowed_p
,
12984 /*member_p=*/false,
12985 declares_class_or_enum
,
12986 &function_definition_p
,
12987 maybe_range_for_decl
,
12990 /* If an error occurred while parsing tentatively, exit quickly.
12991 (That usually happens when in the body of a function; each
12992 statement is treated as a declaration-statement until proven
12994 if (cp_parser_error_occurred (parser
))
12997 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
12999 /* If the init-declarator-list contains more than one
13000 init-declarator, they shall all form declarations of
13002 if (auto_function_declaration
== NULL_TREE
)
13003 auto_function_declaration
13004 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13005 else if (TREE_CODE (decl
) == FUNCTION_DECL
13006 || auto_function_declaration
!= error_mark_node
)
13008 error_at (decl_specifiers
.locations
[ds_type_spec
],
13009 "non-variable %qD in declaration with more than one "
13010 "declarator with placeholder type",
13011 TREE_CODE (decl
) == FUNCTION_DECL
13012 ? decl
: auto_function_declaration
);
13013 auto_function_declaration
= error_mark_node
;
13018 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13021 && last_type
!= error_mark_node
13022 && !same_type_p (auto_result
, last_type
))
13024 /* If the list of declarators contains more than one declarator,
13025 the type of each declared variable is determined as described
13026 above. If the type deduced for the template parameter U is not
13027 the same in each deduction, the program is ill-formed. */
13028 error_at (decl_specifiers
.locations
[ds_type_spec
],
13029 "inconsistent deduction for %qT: %qT and then %qT",
13030 decl_specifiers
.type
, last_type
, auto_result
);
13031 last_type
= error_mark_node
;
13034 last_type
= auto_result
;
13037 /* Handle function definitions specially. */
13038 if (function_definition_p
)
13040 /* If the next token is a `,', then we are probably
13041 processing something like:
13045 which is erroneous. */
13046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13048 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13049 error_at (token
->location
,
13051 " declarations and function-definitions is forbidden");
13053 /* Otherwise, we're done with the list of declarators. */
13056 pop_deferring_access_checks ();
13060 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13061 *maybe_range_for_decl
= decl
;
13062 /* The next token should be either a `,' or a `;'. */
13063 token
= cp_lexer_peek_token (parser
->lexer
);
13064 /* If it's a `,', there are more declarators to come. */
13065 if (token
->type
== CPP_COMMA
)
13066 /* will be consumed next time around */;
13067 /* If it's a `;', we are done. */
13068 else if (token
->type
== CPP_SEMICOLON
)
13070 else if (maybe_range_for_decl
)
13072 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13073 permerror (decl_specifiers
.locations
[ds_type_spec
],
13074 "types may not be defined in a for-range-declaration");
13077 /* Anything else is an error. */
13080 /* If we have already issued an error message we don't need
13081 to issue another one. */
13082 if ((decl
!= error_mark_node
13083 && DECL_INITIAL (decl
) != error_mark_node
)
13084 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13085 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13086 /* Skip tokens until we reach the end of the statement. */
13087 cp_parser_skip_to_end_of_statement (parser
);
13088 /* If the next token is now a `;', consume it. */
13089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13090 cp_lexer_consume_token (parser
->lexer
);
13093 /* After the first time around, a function-definition is not
13094 allowed -- even if it was OK at first. For example:
13099 function_definition_allowed_p
= false;
13102 /* Issue an error message if no declarators are present, and the
13103 decl-specifier-seq does not itself declare a class or
13104 enumeration: [dcl.dcl]/3. */
13105 if (!saw_declarator
)
13107 if (cp_parser_declares_only_class_p (parser
))
13109 if (!declares_class_or_enum
13110 && decl_specifiers
.type
13111 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13112 /* Ensure an error is issued anyway when finish_decltype_type,
13113 called via cp_parser_decl_specifier_seq, returns a class or
13114 an enumeration (c++/51786). */
13115 decl_specifiers
.type
= NULL_TREE
;
13116 shadow_tag (&decl_specifiers
);
13118 /* Perform any deferred access checks. */
13119 perform_deferred_access_checks (tf_warning_or_error
);
13122 /* Consume the `;'. */
13124 if (!maybe_range_for_decl
)
13125 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13126 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13128 if (init_loc
!= UNKNOWN_LOCATION
)
13129 error_at (init_loc
, "initializer in range-based %<for%> loop");
13130 if (comma_loc
!= UNKNOWN_LOCATION
)
13131 error_at (comma_loc
,
13132 "multiple declarations in range-based %<for%> loop");
13136 pop_deferring_access_checks ();
13139 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13140 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13144 cp_parser_decomposition_declaration (cp_parser
*parser
,
13145 cp_decl_specifier_seq
*decl_specifiers
,
13146 tree
*maybe_range_for_decl
,
13147 location_t
*init_loc
)
13149 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13150 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13151 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13153 /* Parse the identifier-list. */
13154 auto_vec
<cp_expr
, 10> v
;
13155 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13158 cp_expr e
= cp_parser_identifier (parser
);
13159 if (e
.get_value () == error_mark_node
)
13162 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13164 cp_lexer_consume_token (parser
->lexer
);
13167 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13168 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13170 end_loc
= UNKNOWN_LOCATION
;
13171 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13174 cp_lexer_consume_token (parser
->lexer
);
13177 cp_parser_skip_to_end_of_statement (parser
);
13178 return error_mark_node
;
13182 if (cxx_dialect
< cxx17
)
13183 pedwarn (loc
, 0, "structured bindings only available with "
13184 "-std=c++17 or -std=gnu++17");
13187 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13188 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13189 declarator
->id_loc
= loc
;
13190 if (ref_qual
!= REF_QUAL_NONE
)
13191 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13192 ref_qual
== REF_QUAL_RVALUE
,
13194 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13195 NULL_TREE
, decl_specifiers
->attributes
,
13197 tree orig_decl
= decl
;
13201 cp_decl_specifier_seq decl_specs
;
13202 clear_decl_specs (&decl_specs
);
13203 decl_specs
.type
= make_auto ();
13205 FOR_EACH_VEC_ELT (v
, i
, e
)
13208 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13210 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13211 declarator
->id_loc
= e
.get_location ();
13212 tree elt_pushed_scope
;
13213 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13214 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13215 if (decl2
== error_mark_node
)
13216 decl
= error_mark_node
;
13217 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13219 /* Ensure we've diagnosed redeclaration if we aren't creating
13221 gcc_assert (errorcount
);
13222 decl
= error_mark_node
;
13226 if (elt_pushed_scope
)
13227 pop_scope (elt_pushed_scope
);
13232 error_at (loc
, "empty structured binding declaration");
13233 decl
= error_mark_node
;
13236 if (maybe_range_for_decl
== NULL
13237 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13239 bool non_constant_p
= false, is_direct_init
= false;
13240 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13241 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13243 if (initializer
== NULL_TREE
13244 || (TREE_CODE (initializer
) == TREE_LIST
13245 && TREE_CHAIN (initializer
))
13246 || (TREE_CODE (initializer
) == CONSTRUCTOR
13247 && CONSTRUCTOR_NELTS (initializer
) != 1))
13249 error_at (loc
, "invalid initializer for structured binding "
13251 initializer
= error_mark_node
;
13254 if (decl
!= error_mark_node
)
13256 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13257 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13258 cp_finish_decomp (decl
, prev
, v
.length ());
13261 else if (decl
!= error_mark_node
)
13263 *maybe_range_for_decl
= prev
;
13264 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13265 the underlying DECL. */
13266 cp_finish_decomp (decl
, prev
, v
.length ());
13270 pop_scope (pushed_scope
);
13272 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13274 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13275 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13281 /* Parse a decl-specifier-seq.
13283 decl-specifier-seq:
13284 decl-specifier-seq [opt] decl-specifier
13285 decl-specifier attribute-specifier-seq [opt] (C++11)
13288 storage-class-specifier
13299 Concepts Extension:
13304 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13306 The parser flags FLAGS is used to control type-specifier parsing.
13308 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13311 1: one of the decl-specifiers is an elaborated-type-specifier
13312 (i.e., a type declaration)
13313 2: one of the decl-specifiers is an enum-specifier or a
13314 class-specifier (i.e., a type definition)
13319 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13320 cp_parser_flags flags
,
13321 cp_decl_specifier_seq
*decl_specs
,
13322 int* declares_class_or_enum
)
13324 bool constructor_possible_p
= !parser
->in_declarator_p
;
13325 bool found_decl_spec
= false;
13326 cp_token
*start_token
= NULL
;
13329 /* Clear DECL_SPECS. */
13330 clear_decl_specs (decl_specs
);
13332 /* Assume no class or enumeration type is declared. */
13333 *declares_class_or_enum
= 0;
13335 /* Keep reading specifiers until there are no more to read. */
13338 bool constructor_p
;
13342 /* Peek at the next token. */
13343 token
= cp_lexer_peek_token (parser
->lexer
);
13345 /* Save the first token of the decl spec list for error
13348 start_token
= token
;
13349 /* Handle attributes. */
13350 if (cp_next_tokens_can_be_attribute_p (parser
))
13352 /* Parse the attributes. */
13353 tree attrs
= cp_parser_attributes_opt (parser
);
13355 /* In a sequence of declaration specifiers, c++11 attributes
13356 appertain to the type that precede them. In that case
13359 The attribute-specifier-seq affects the type only for
13360 the declaration it appears in, not other declarations
13361 involving the same type.
13363 But for now let's force the user to position the
13364 attribute either at the beginning of the declaration or
13365 after the declarator-id, which would clearly mean that it
13366 applies to the declarator. */
13367 if (cxx11_attribute_p (attrs
))
13369 if (!found_decl_spec
)
13370 /* The c++11 attribute is at the beginning of the
13371 declaration. It appertains to the entity being
13375 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13377 /* This is an attribute following a
13378 class-specifier. */
13379 if (decl_specs
->type_definition_p
)
13380 warn_misplaced_attr_for_class_type (token
->location
,
13386 decl_specs
->std_attributes
13387 = chainon (decl_specs
->std_attributes
,
13389 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13390 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13396 decl_specs
->attributes
13397 = chainon (decl_specs
->attributes
,
13399 if (decl_specs
->locations
[ds_attribute
] == 0)
13400 decl_specs
->locations
[ds_attribute
] = token
->location
;
13403 /* Assume we will find a decl-specifier keyword. */
13404 found_decl_spec
= true;
13405 /* If the next token is an appropriate keyword, we can simply
13406 add it to the list. */
13407 switch (token
->keyword
)
13413 if (!at_class_scope_p ())
13415 gcc_rich_location
richloc (token
->location
);
13416 richloc
.add_fixit_remove ();
13417 error_at_rich_loc (&richloc
, "%<friend%> used outside of class");
13418 cp_lexer_purge_token (parser
->lexer
);
13423 /* Consume the token. */
13424 cp_lexer_consume_token (parser
->lexer
);
13428 case RID_CONSTEXPR
:
13430 cp_lexer_consume_token (parser
->lexer
);
13435 cp_lexer_consume_token (parser
->lexer
);
13438 /* function-specifier:
13445 cp_parser_function_specifier_opt (parser
, decl_specs
);
13452 /* Consume the token. */
13453 cp_lexer_consume_token (parser
->lexer
);
13454 /* A constructor declarator cannot appear in a typedef. */
13455 constructor_possible_p
= false;
13456 /* The "typedef" keyword can only occur in a declaration; we
13457 may as well commit at this point. */
13458 cp_parser_commit_to_tentative_parse (parser
);
13460 if (decl_specs
->storage_class
!= sc_none
)
13461 decl_specs
->conflicting_specifiers_p
= true;
13464 /* storage-class-specifier:
13474 if (cxx_dialect
== cxx98
)
13476 /* Consume the token. */
13477 cp_lexer_consume_token (parser
->lexer
);
13479 /* Complain about `auto' as a storage specifier, if
13480 we're complaining about C++0x compatibility. */
13481 gcc_rich_location
richloc (token
->location
);
13482 richloc
.add_fixit_remove ();
13483 warning_at_rich_loc (&richloc
, OPT_Wc__11_compat
,
13484 "%<auto%> changes meaning in C++11; "
13485 "please remove it");
13487 /* Set the storage class anyway. */
13488 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13492 /* C++0x auto type-specifier. */
13493 found_decl_spec
= false;
13500 /* Consume the token. */
13501 cp_lexer_consume_token (parser
->lexer
);
13502 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13506 /* Consume the token. */
13508 cp_lexer_consume_token (parser
->lexer
);
13512 /* We did not yet find a decl-specifier yet. */
13513 found_decl_spec
= false;
13517 if (found_decl_spec
13518 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13519 && token
->keyword
!= RID_CONSTEXPR
)
13520 error ("decl-specifier invalid in condition");
13522 if (found_decl_spec
13523 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13524 && token
->keyword
!= RID_MUTABLE
13525 && token
->keyword
!= RID_CONSTEXPR
)
13526 error_at (token
->location
, "%qD invalid in lambda",
13527 ridpointers
[token
->keyword
]);
13530 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13532 /* Constructors are a special case. The `S' in `S()' is not a
13533 decl-specifier; it is the beginning of the declarator. */
13535 = (!found_decl_spec
13536 && constructor_possible_p
13537 && (cp_parser_constructor_declarator_p
13538 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13540 /* If we don't have a DECL_SPEC yet, then we must be looking at
13541 a type-specifier. */
13542 if (!found_decl_spec
&& !constructor_p
)
13544 int decl_spec_declares_class_or_enum
;
13545 bool is_cv_qualifier
;
13549 = cp_parser_type_specifier (parser
, flags
,
13551 /*is_declaration=*/true,
13552 &decl_spec_declares_class_or_enum
,
13554 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13556 /* If this type-specifier referenced a user-defined type
13557 (a typedef, class-name, etc.), then we can't allow any
13558 more such type-specifiers henceforth.
13562 The longest sequence of decl-specifiers that could
13563 possibly be a type name is taken as the
13564 decl-specifier-seq of a declaration. The sequence shall
13565 be self-consistent as described below.
13569 As a general rule, at most one type-specifier is allowed
13570 in the complete decl-specifier-seq of a declaration. The
13571 only exceptions are the following:
13573 -- const or volatile can be combined with any other
13576 -- signed or unsigned can be combined with char, long,
13584 void g (const int Pc);
13586 Here, Pc is *not* part of the decl-specifier seq; it's
13587 the declarator. Therefore, once we see a type-specifier
13588 (other than a cv-qualifier), we forbid any additional
13589 user-defined types. We *do* still allow things like `int
13590 int' to be considered a decl-specifier-seq, and issue the
13591 error message later. */
13592 if (type_spec
&& !is_cv_qualifier
)
13593 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13594 /* A constructor declarator cannot follow a type-specifier. */
13597 constructor_possible_p
= false;
13598 found_decl_spec
= true;
13599 if (!is_cv_qualifier
)
13600 decl_specs
->any_type_specifiers_p
= true;
13604 /* If we still do not have a DECL_SPEC, then there are no more
13605 decl-specifiers. */
13606 if (!found_decl_spec
)
13609 decl_specs
->any_specifiers_p
= true;
13610 /* After we see one decl-specifier, further decl-specifiers are
13611 always optional. */
13612 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13615 /* Don't allow a friend specifier with a class definition. */
13616 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13617 && (*declares_class_or_enum
& 2))
13618 error_at (decl_specs
->locations
[ds_friend
],
13619 "class definition may not be declared a friend");
13622 /* Parse an (optional) storage-class-specifier.
13624 storage-class-specifier:
13633 storage-class-specifier:
13636 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13639 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13641 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13644 if (cxx_dialect
!= cxx98
)
13646 /* Fall through for C++98. */
13647 gcc_fallthrough ();
13654 /* Consume the token. */
13655 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13662 /* Parse an (optional) function-specifier.
13664 function-specifier:
13669 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13670 Updates DECL_SPECS, if it is non-NULL. */
13673 cp_parser_function_specifier_opt (cp_parser
* parser
,
13674 cp_decl_specifier_seq
*decl_specs
)
13676 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13677 switch (token
->keyword
)
13680 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13684 /* 14.5.2.3 [temp.mem]
13686 A member function template shall not be virtual. */
13687 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13688 && current_class_type
)
13689 error_at (token
->location
, "templates may not be %<virtual%>");
13691 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13695 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13702 /* Consume the token. */
13703 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13706 /* Parse a linkage-specification.
13708 linkage-specification:
13709 extern string-literal { declaration-seq [opt] }
13710 extern string-literal declaration */
13713 cp_parser_linkage_specification (cp_parser
* parser
)
13717 /* Look for the `extern' keyword. */
13718 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13720 /* Look for the string-literal. */
13721 linkage
= cp_parser_string_literal (parser
, false, false);
13723 /* Transform the literal into an identifier. If the literal is a
13724 wide-character string, or contains embedded NULs, then we can't
13725 handle it as the user wants. */
13726 if (strlen (TREE_STRING_POINTER (linkage
))
13727 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13729 cp_parser_error (parser
, "invalid linkage-specification");
13730 /* Assume C++ linkage. */
13731 linkage
= lang_name_cplusplus
;
13734 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13736 /* We're now using the new linkage. */
13737 push_lang_context (linkage
);
13739 /* If the next token is a `{', then we're using the first
13741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13743 cp_ensure_no_omp_declare_simd (parser
);
13744 cp_ensure_no_oacc_routine (parser
);
13746 /* Consume the `{' token. */
13747 matching_braces braces
;
13748 braces
.consume_open (parser
)->location
;
13749 /* Parse the declarations. */
13750 cp_parser_declaration_seq_opt (parser
);
13751 /* Look for the closing `}'. */
13752 braces
.require_close (parser
);
13754 /* Otherwise, there's just one declaration. */
13757 bool saved_in_unbraced_linkage_specification_p
;
13759 saved_in_unbraced_linkage_specification_p
13760 = parser
->in_unbraced_linkage_specification_p
;
13761 parser
->in_unbraced_linkage_specification_p
= true;
13762 cp_parser_declaration (parser
);
13763 parser
->in_unbraced_linkage_specification_p
13764 = saved_in_unbraced_linkage_specification_p
;
13767 /* We're done with the linkage-specification. */
13768 pop_lang_context ();
13771 /* Parse a static_assert-declaration.
13773 static_assert-declaration:
13774 static_assert ( constant-expression , string-literal ) ;
13775 static_assert ( constant-expression ) ; (C++17)
13777 If MEMBER_P, this static_assert is a class member. */
13780 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13785 location_t saved_loc
;
13788 /* Peek at the `static_assert' token so we can keep track of exactly
13789 where the static assertion started. */
13790 token
= cp_lexer_peek_token (parser
->lexer
);
13791 saved_loc
= token
->location
;
13793 /* Look for the `static_assert' keyword. */
13794 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13798 /* We know we are in a static assertion; commit to any tentative
13800 if (cp_parser_parsing_tentatively (parser
))
13801 cp_parser_commit_to_tentative_parse (parser
);
13803 /* Parse the `(' starting the static assertion condition. */
13804 matching_parens parens
;
13805 parens
.require_open (parser
);
13807 /* Parse the constant-expression. Allow a non-constant expression
13808 here in order to give better diagnostics in finish_static_assert. */
13810 cp_parser_constant_expression (parser
,
13811 /*allow_non_constant_p=*/true,
13812 /*non_constant_p=*/&dummy
);
13814 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13816 if (cxx_dialect
< cxx17
)
13817 pedwarn (input_location
, OPT_Wpedantic
,
13818 "static_assert without a message "
13819 "only available with -std=c++17 or -std=gnu++17");
13821 cp_lexer_consume_token (parser
->lexer
);
13822 message
= build_string (1, "");
13823 TREE_TYPE (message
) = char_array_type_node
;
13824 fix_string_type (message
);
13828 /* Parse the separating `,'. */
13829 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13831 /* Parse the string-literal message. */
13832 message
= cp_parser_string_literal (parser
,
13833 /*translate=*/false,
13836 /* A `)' completes the static assertion. */
13837 if (!parens
.require_close (parser
))
13838 cp_parser_skip_to_closing_parenthesis (parser
,
13839 /*recovering=*/true,
13840 /*or_comma=*/false,
13841 /*consume_paren=*/true);
13844 /* A semicolon terminates the declaration. */
13845 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13847 /* Complete the static assertion, which may mean either processing
13848 the static assert now or saving it for template instantiation. */
13849 finish_static_assert (condition
, message
, saved_loc
, member_p
);
13852 /* Parse the expression in decltype ( expression ). */
13855 cp_parser_decltype_expr (cp_parser
*parser
,
13856 bool &id_expression_or_member_access_p
)
13858 cp_token
*id_expr_start_token
;
13861 /* Since we're going to preserve any side-effects from this parse, set up a
13862 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13863 in the expression. */
13864 tentative_firewall
firewall (parser
);
13866 /* First, try parsing an id-expression. */
13867 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13868 cp_parser_parse_tentatively (parser
);
13869 expr
= cp_parser_id_expression (parser
,
13870 /*template_keyword_p=*/false,
13871 /*check_dependency_p=*/true,
13872 /*template_p=*/NULL
,
13873 /*declarator_p=*/false,
13874 /*optional_p=*/false);
13876 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13878 bool non_integral_constant_expression_p
= false;
13879 tree id_expression
= expr
;
13881 const char *error_msg
;
13883 if (identifier_p (expr
))
13884 /* Lookup the name we got back from the id-expression. */
13885 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13886 id_expr_start_token
->location
);
13889 && expr
!= error_mark_node
13890 && TREE_CODE (expr
) != TYPE_DECL
13891 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13892 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13893 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13895 /* Complete lookup of the id-expression. */
13896 expr
= (finish_id_expression
13897 (id_expression
, expr
, parser
->scope
, &idk
,
13898 /*integral_constant_expression_p=*/false,
13899 /*allow_non_integral_constant_expression_p=*/true,
13900 &non_integral_constant_expression_p
,
13901 /*template_p=*/false,
13903 /*address_p=*/false,
13904 /*template_arg_p=*/false,
13906 id_expr_start_token
->location
));
13908 if (expr
== error_mark_node
)
13909 /* We found an id-expression, but it was something that we
13910 should not have found. This is an error, not something
13911 we can recover from, so note that we found an
13912 id-expression and we'll recover as gracefully as
13914 id_expression_or_member_access_p
= true;
13918 && expr
!= error_mark_node
13919 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13920 /* We have an id-expression. */
13921 id_expression_or_member_access_p
= true;
13924 if (!id_expression_or_member_access_p
)
13926 /* Abort the id-expression parse. */
13927 cp_parser_abort_tentative_parse (parser
);
13929 /* Parsing tentatively, again. */
13930 cp_parser_parse_tentatively (parser
);
13932 /* Parse a class member access. */
13933 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13934 /*cast_p=*/false, /*decltype*/true,
13935 /*member_access_only_p=*/true, NULL
);
13938 && expr
!= error_mark_node
13939 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13940 /* We have an id-expression. */
13941 id_expression_or_member_access_p
= true;
13944 if (id_expression_or_member_access_p
)
13945 /* We have parsed the complete id-expression or member access. */
13946 cp_parser_parse_definitely (parser
);
13949 /* Abort our attempt to parse an id-expression or member access
13951 cp_parser_abort_tentative_parse (parser
);
13953 /* Parse a full expression. */
13954 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13955 /*decltype_p=*/true);
13961 /* Parse a `decltype' type. Returns the type.
13963 simple-type-specifier:
13964 decltype ( expression )
13966 decltype ( auto ) */
13969 cp_parser_decltype (cp_parser
*parser
)
13972 bool id_expression_or_member_access_p
= false;
13973 const char *saved_message
;
13974 bool saved_integral_constant_expression_p
;
13975 bool saved_non_integral_constant_expression_p
;
13976 bool saved_greater_than_is_operator_p
;
13977 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13979 if (start_token
->type
== CPP_DECLTYPE
)
13981 /* Already parsed. */
13982 cp_lexer_consume_token (parser
->lexer
);
13983 return saved_checks_value (start_token
->u
.tree_check_value
);
13986 /* Look for the `decltype' token. */
13987 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
13988 return error_mark_node
;
13990 /* Parse the opening `('. */
13991 matching_parens parens
;
13992 if (!parens
.require_open (parser
))
13993 return error_mark_node
;
13995 /* decltype (auto) */
13996 if (cxx_dialect
>= cxx14
13997 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
13999 cp_lexer_consume_token (parser
->lexer
);
14000 if (!parens
.require_close (parser
))
14001 return error_mark_node
;
14002 expr
= make_decltype_auto ();
14003 AUTO_IS_DECLTYPE (expr
) = true;
14007 /* Types cannot be defined in a `decltype' expression. Save away the
14009 saved_message
= parser
->type_definition_forbidden_message
;
14011 /* And create the new one. */
14012 parser
->type_definition_forbidden_message
14013 = G_("types may not be defined in %<decltype%> expressions");
14015 /* The restrictions on constant-expressions do not apply inside
14016 decltype expressions. */
14017 saved_integral_constant_expression_p
14018 = parser
->integral_constant_expression_p
;
14019 saved_non_integral_constant_expression_p
14020 = parser
->non_integral_constant_expression_p
;
14021 parser
->integral_constant_expression_p
= false;
14023 /* Within a parenthesized expression, a `>' token is always
14024 the greater-than operator. */
14025 saved_greater_than_is_operator_p
14026 = parser
->greater_than_is_operator_p
;
14027 parser
->greater_than_is_operator_p
= true;
14029 /* Do not actually evaluate the expression. */
14030 ++cp_unevaluated_operand
;
14032 /* Do not warn about problems with the expression. */
14033 ++c_inhibit_evaluation_warnings
;
14035 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14037 /* Go back to evaluating expressions. */
14038 --cp_unevaluated_operand
;
14039 --c_inhibit_evaluation_warnings
;
14041 /* The `>' token might be the end of a template-id or
14042 template-parameter-list now. */
14043 parser
->greater_than_is_operator_p
14044 = saved_greater_than_is_operator_p
;
14046 /* Restore the old message and the integral constant expression
14048 parser
->type_definition_forbidden_message
= saved_message
;
14049 parser
->integral_constant_expression_p
14050 = saved_integral_constant_expression_p
;
14051 parser
->non_integral_constant_expression_p
14052 = saved_non_integral_constant_expression_p
;
14054 /* Parse to the closing `)'. */
14055 if (!parens
.require_close (parser
))
14057 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14058 /*consume_paren=*/true);
14059 return error_mark_node
;
14062 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14063 tf_warning_or_error
);
14066 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14068 start_token
->type
= CPP_DECLTYPE
;
14069 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14070 start_token
->u
.tree_check_value
->value
= expr
;
14071 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14072 start_token
->keyword
= RID_MAX
;
14073 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14078 /* Special member functions [gram.special] */
14080 /* Parse a conversion-function-id.
14082 conversion-function-id:
14083 operator conversion-type-id
14085 Returns an IDENTIFIER_NODE representing the operator. */
14088 cp_parser_conversion_function_id (cp_parser
* parser
)
14092 tree saved_qualifying_scope
;
14093 tree saved_object_scope
;
14094 tree pushed_scope
= NULL_TREE
;
14096 /* Look for the `operator' token. */
14097 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14098 return error_mark_node
;
14099 /* When we parse the conversion-type-id, the current scope will be
14100 reset. However, we need that information in able to look up the
14101 conversion function later, so we save it here. */
14102 saved_scope
= parser
->scope
;
14103 saved_qualifying_scope
= parser
->qualifying_scope
;
14104 saved_object_scope
= parser
->object_scope
;
14105 /* We must enter the scope of the class so that the names of
14106 entities declared within the class are available in the
14107 conversion-type-id. For example, consider:
14114 S::operator I() { ... }
14116 In order to see that `I' is a type-name in the definition, we
14117 must be in the scope of `S'. */
14119 pushed_scope
= push_scope (saved_scope
);
14120 /* Parse the conversion-type-id. */
14121 type
= cp_parser_conversion_type_id (parser
);
14122 /* Leave the scope of the class, if any. */
14124 pop_scope (pushed_scope
);
14125 /* Restore the saved scope. */
14126 parser
->scope
= saved_scope
;
14127 parser
->qualifying_scope
= saved_qualifying_scope
;
14128 parser
->object_scope
= saved_object_scope
;
14129 /* If the TYPE is invalid, indicate failure. */
14130 if (type
== error_mark_node
)
14131 return error_mark_node
;
14132 return make_conv_op_name (type
);
14135 /* Parse a conversion-type-id:
14137 conversion-type-id:
14138 type-specifier-seq conversion-declarator [opt]
14140 Returns the TYPE specified. */
14143 cp_parser_conversion_type_id (cp_parser
* parser
)
14146 cp_decl_specifier_seq type_specifiers
;
14147 cp_declarator
*declarator
;
14148 tree type_specified
;
14149 const char *saved_message
;
14151 /* Parse the attributes. */
14152 attributes
= cp_parser_attributes_opt (parser
);
14154 saved_message
= parser
->type_definition_forbidden_message
;
14155 parser
->type_definition_forbidden_message
14156 = G_("types may not be defined in a conversion-type-id");
14158 /* Parse the type-specifiers. */
14159 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14160 /*is_trailing_return=*/false,
14163 parser
->type_definition_forbidden_message
= saved_message
;
14165 /* If that didn't work, stop. */
14166 if (type_specifiers
.type
== error_mark_node
)
14167 return error_mark_node
;
14168 /* Parse the conversion-declarator. */
14169 declarator
= cp_parser_conversion_declarator_opt (parser
);
14171 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14172 /*initialized=*/0, &attributes
);
14174 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14176 /* Don't give this error when parsing tentatively. This happens to
14177 work because we always parse this definitively once. */
14178 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14179 && type_uses_auto (type_specified
))
14181 if (cxx_dialect
< cxx14
)
14183 error ("invalid use of %<auto%> in conversion operator");
14184 return error_mark_node
;
14186 else if (template_parm_scope_p ())
14187 warning (0, "use of %<auto%> in member template "
14188 "conversion operator can never be deduced");
14191 return type_specified
;
14194 /* Parse an (optional) conversion-declarator.
14196 conversion-declarator:
14197 ptr-operator conversion-declarator [opt]
14201 static cp_declarator
*
14202 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14204 enum tree_code code
;
14205 tree class_type
, std_attributes
= NULL_TREE
;
14206 cp_cv_quals cv_quals
;
14208 /* We don't know if there's a ptr-operator next, or not. */
14209 cp_parser_parse_tentatively (parser
);
14210 /* Try the ptr-operator. */
14211 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14213 /* If it worked, look for more conversion-declarators. */
14214 if (cp_parser_parse_definitely (parser
))
14216 cp_declarator
*declarator
;
14218 /* Parse another optional declarator. */
14219 declarator
= cp_parser_conversion_declarator_opt (parser
);
14221 declarator
= cp_parser_make_indirect_declarator
14222 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14230 /* Parse an (optional) ctor-initializer.
14233 : mem-initializer-list
14235 Returns TRUE iff the ctor-initializer was actually present. */
14238 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14240 /* If the next token is not a `:', then there is no
14241 ctor-initializer. */
14242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14244 /* Do default initialization of any bases and members. */
14245 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14246 finish_mem_initializers (NULL_TREE
);
14251 /* Consume the `:' token. */
14252 cp_lexer_consume_token (parser
->lexer
);
14253 /* And the mem-initializer-list. */
14254 cp_parser_mem_initializer_list (parser
);
14259 /* Parse a mem-initializer-list.
14261 mem-initializer-list:
14262 mem-initializer ... [opt]
14263 mem-initializer ... [opt] , mem-initializer-list */
14266 cp_parser_mem_initializer_list (cp_parser
* parser
)
14268 tree mem_initializer_list
= NULL_TREE
;
14269 tree target_ctor
= error_mark_node
;
14270 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14272 /* Let the semantic analysis code know that we are starting the
14273 mem-initializer-list. */
14274 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14275 error_at (token
->location
,
14276 "only constructors take member initializers");
14278 /* Loop through the list. */
14281 tree mem_initializer
;
14283 token
= cp_lexer_peek_token (parser
->lexer
);
14284 /* Parse the mem-initializer. */
14285 mem_initializer
= cp_parser_mem_initializer (parser
);
14286 /* If the next token is a `...', we're expanding member initializers. */
14287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14289 /* Consume the `...'. */
14290 cp_lexer_consume_token (parser
->lexer
);
14292 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14293 can be expanded but members cannot. */
14294 if (mem_initializer
!= error_mark_node
14295 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14297 error_at (token
->location
,
14298 "cannot expand initializer for member %qD",
14299 TREE_PURPOSE (mem_initializer
));
14300 mem_initializer
= error_mark_node
;
14303 /* Construct the pack expansion type. */
14304 if (mem_initializer
!= error_mark_node
)
14305 mem_initializer
= make_pack_expansion (mem_initializer
);
14307 if (target_ctor
!= error_mark_node
14308 && mem_initializer
!= error_mark_node
)
14310 error ("mem-initializer for %qD follows constructor delegation",
14311 TREE_PURPOSE (mem_initializer
));
14312 mem_initializer
= error_mark_node
;
14314 /* Look for a target constructor. */
14315 if (mem_initializer
!= error_mark_node
14316 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14317 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14319 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14320 if (mem_initializer_list
)
14322 error ("constructor delegation follows mem-initializer for %qD",
14323 TREE_PURPOSE (mem_initializer_list
));
14324 mem_initializer
= error_mark_node
;
14326 target_ctor
= mem_initializer
;
14328 /* Add it to the list, unless it was erroneous. */
14329 if (mem_initializer
!= error_mark_node
)
14331 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14332 mem_initializer_list
= mem_initializer
;
14334 /* If the next token is not a `,', we're done. */
14335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14337 /* Consume the `,' token. */
14338 cp_lexer_consume_token (parser
->lexer
);
14341 /* Perform semantic analysis. */
14342 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14343 finish_mem_initializers (mem_initializer_list
);
14346 /* Parse a mem-initializer.
14349 mem-initializer-id ( expression-list [opt] )
14350 mem-initializer-id braced-init-list
14355 ( expression-list [opt] )
14357 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14358 class) or FIELD_DECL (for a non-static data member) to initialize;
14359 the TREE_VALUE is the expression-list. An empty initialization
14360 list is represented by void_list_node. */
14363 cp_parser_mem_initializer (cp_parser
* parser
)
14365 tree mem_initializer_id
;
14366 tree expression_list
;
14368 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14370 /* Find out what is being initialized. */
14371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14373 permerror (token
->location
,
14374 "anachronistic old-style base class initializer");
14375 mem_initializer_id
= NULL_TREE
;
14379 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14380 if (mem_initializer_id
== error_mark_node
)
14381 return mem_initializer_id
;
14383 member
= expand_member_init (mem_initializer_id
);
14384 if (member
&& !DECL_P (member
))
14385 in_base_initializer
= 1;
14387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14389 bool expr_non_constant_p
;
14390 cp_lexer_set_source_position (parser
->lexer
);
14391 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14392 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14393 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14394 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14398 vec
<tree
, va_gc
> *vec
;
14399 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14401 /*allow_expansion_p=*/true,
14402 /*non_constant_p=*/NULL
);
14404 return error_mark_node
;
14405 expression_list
= build_tree_list_vec (vec
);
14406 release_tree_vector (vec
);
14409 if (expression_list
== error_mark_node
)
14410 return error_mark_node
;
14411 if (!expression_list
)
14412 expression_list
= void_type_node
;
14414 in_base_initializer
= 0;
14416 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14419 /* Parse a mem-initializer-id.
14421 mem-initializer-id:
14422 :: [opt] nested-name-specifier [opt] class-name
14423 decltype-specifier (C++11)
14426 Returns a TYPE indicating the class to be initialized for the first
14427 production (and the second in C++11). Returns an IDENTIFIER_NODE
14428 indicating the data member to be initialized for the last production. */
14431 cp_parser_mem_initializer_id (cp_parser
* parser
)
14433 bool global_scope_p
;
14434 bool nested_name_specifier_p
;
14435 bool template_p
= false;
14438 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14440 /* `typename' is not allowed in this context ([temp.res]). */
14441 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14443 error_at (token
->location
,
14444 "keyword %<typename%> not allowed in this context (a qualified "
14445 "member initializer is implicitly a type)");
14446 cp_lexer_consume_token (parser
->lexer
);
14448 /* Look for the optional `::' operator. */
14450 = (cp_parser_global_scope_opt (parser
,
14451 /*current_scope_valid_p=*/false)
14453 /* Look for the optional nested-name-specifier. The simplest way to
14458 The keyword `typename' is not permitted in a base-specifier or
14459 mem-initializer; in these contexts a qualified name that
14460 depends on a template-parameter is implicitly assumed to be a
14463 is to assume that we have seen the `typename' keyword at this
14465 nested_name_specifier_p
14466 = (cp_parser_nested_name_specifier_opt (parser
,
14467 /*typename_keyword_p=*/true,
14468 /*check_dependency_p=*/true,
14470 /*is_declaration=*/true)
14472 if (nested_name_specifier_p
)
14473 template_p
= cp_parser_optional_template_keyword (parser
);
14474 /* If there is a `::' operator or a nested-name-specifier, then we
14475 are definitely looking for a class-name. */
14476 if (global_scope_p
|| nested_name_specifier_p
)
14477 return cp_parser_class_name (parser
,
14478 /*typename_keyword_p=*/true,
14479 /*template_keyword_p=*/template_p
,
14481 /*check_dependency_p=*/true,
14482 /*class_head_p=*/false,
14483 /*is_declaration=*/true);
14484 /* Otherwise, we could also be looking for an ordinary identifier. */
14485 cp_parser_parse_tentatively (parser
);
14486 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14487 /* Try a decltype-specifier. */
14488 id
= cp_parser_decltype (parser
);
14490 /* Otherwise, try a class-name. */
14491 id
= cp_parser_class_name (parser
,
14492 /*typename_keyword_p=*/true,
14493 /*template_keyword_p=*/false,
14495 /*check_dependency_p=*/true,
14496 /*class_head_p=*/false,
14497 /*is_declaration=*/true);
14498 /* If we found one, we're done. */
14499 if (cp_parser_parse_definitely (parser
))
14501 /* Otherwise, look for an ordinary identifier. */
14502 return cp_parser_identifier (parser
);
14505 /* Overloading [gram.over] */
14507 /* Parse an operator-function-id.
14509 operator-function-id:
14512 Returns an IDENTIFIER_NODE for the operator which is a
14513 human-readable spelling of the identifier, e.g., `operator +'. */
14516 cp_parser_operator_function_id (cp_parser
* parser
)
14518 /* Look for the `operator' keyword. */
14519 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14520 return error_mark_node
;
14521 /* And then the name of the operator itself. */
14522 return cp_parser_operator (parser
);
14525 /* Return an identifier node for a user-defined literal operator.
14526 The suffix identifier is chained to the operator name identifier. */
14529 cp_literal_operator_id (const char* name
)
14532 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14533 + strlen (name
) + 10);
14534 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14535 identifier
= get_identifier (buffer
);
14540 /* Parse an operator.
14543 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14544 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14545 || ++ -- , ->* -> () []
14552 Returns an IDENTIFIER_NODE for the operator which is a
14553 human-readable spelling of the identifier, e.g., `operator +'. */
14556 cp_parser_operator (cp_parser
* parser
)
14558 tree id
= NULL_TREE
;
14562 /* Peek at the next token. */
14563 token
= cp_lexer_peek_token (parser
->lexer
);
14565 location_t start_loc
= token
->location
;
14567 /* Figure out which operator we have. */
14568 switch (token
->type
)
14574 /* The keyword should be either `new' or `delete'. */
14575 if (token
->keyword
== RID_NEW
)
14577 else if (token
->keyword
== RID_DELETE
)
14582 /* Consume the `new' or `delete' token. */
14583 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14585 /* Peek at the next token. */
14586 token
= cp_lexer_peek_token (parser
->lexer
);
14587 /* If it's a `[' token then this is the array variant of the
14589 if (token
->type
== CPP_OPEN_SQUARE
)
14591 /* Consume the `[' token. */
14592 cp_lexer_consume_token (parser
->lexer
);
14593 /* Look for the `]' token. */
14594 if (cp_token
*close_token
14595 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14596 end_loc
= close_token
->location
;
14597 id
= cp_operator_id (op
== NEW_EXPR
14598 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
14600 /* Otherwise, we have the non-array variant. */
14602 id
= cp_operator_id (op
);
14604 location_t loc
= make_location (start_loc
, start_loc
, end_loc
);
14606 return cp_expr (id
, loc
);
14610 id
= cp_operator_id (PLUS_EXPR
);
14614 id
= cp_operator_id (MINUS_EXPR
);
14618 id
= cp_operator_id (MULT_EXPR
);
14622 id
= cp_operator_id (TRUNC_DIV_EXPR
);
14626 id
= cp_operator_id (TRUNC_MOD_EXPR
);
14630 id
= cp_operator_id (BIT_XOR_EXPR
);
14634 id
= cp_operator_id (BIT_AND_EXPR
);
14638 id
= cp_operator_id (BIT_IOR_EXPR
);
14642 id
= cp_operator_id (BIT_NOT_EXPR
);
14646 id
= cp_operator_id (TRUTH_NOT_EXPR
);
14650 id
= cp_assignment_operator_id (NOP_EXPR
);
14654 id
= cp_operator_id (LT_EXPR
);
14658 id
= cp_operator_id (GT_EXPR
);
14662 id
= cp_assignment_operator_id (PLUS_EXPR
);
14666 id
= cp_assignment_operator_id (MINUS_EXPR
);
14670 id
= cp_assignment_operator_id (MULT_EXPR
);
14674 id
= cp_assignment_operator_id (TRUNC_DIV_EXPR
);
14678 id
= cp_assignment_operator_id (TRUNC_MOD_EXPR
);
14682 id
= cp_assignment_operator_id (BIT_XOR_EXPR
);
14686 id
= cp_assignment_operator_id (BIT_AND_EXPR
);
14690 id
= cp_assignment_operator_id (BIT_IOR_EXPR
);
14694 id
= cp_operator_id (LSHIFT_EXPR
);
14698 id
= cp_operator_id (RSHIFT_EXPR
);
14701 case CPP_LSHIFT_EQ
:
14702 id
= cp_assignment_operator_id (LSHIFT_EXPR
);
14705 case CPP_RSHIFT_EQ
:
14706 id
= cp_assignment_operator_id (RSHIFT_EXPR
);
14710 id
= cp_operator_id (EQ_EXPR
);
14714 id
= cp_operator_id (NE_EXPR
);
14718 id
= cp_operator_id (LE_EXPR
);
14721 case CPP_GREATER_EQ
:
14722 id
= cp_operator_id (GE_EXPR
);
14726 id
= cp_operator_id (TRUTH_ANDIF_EXPR
);
14730 id
= cp_operator_id (TRUTH_ORIF_EXPR
);
14733 case CPP_PLUS_PLUS
:
14734 id
= cp_operator_id (POSTINCREMENT_EXPR
);
14737 case CPP_MINUS_MINUS
:
14738 id
= cp_operator_id (PREDECREMENT_EXPR
);
14742 id
= cp_operator_id (COMPOUND_EXPR
);
14745 case CPP_DEREF_STAR
:
14746 id
= cp_operator_id (MEMBER_REF
);
14750 id
= cp_operator_id (COMPONENT_REF
);
14753 case CPP_OPEN_PAREN
:
14755 /* Consume the `('. */
14756 matching_parens parens
;
14757 parens
.consume_open (parser
);
14758 /* Look for the matching `)'. */
14759 parens
.require_close (parser
);
14760 return cp_operator_id (CALL_EXPR
);
14763 case CPP_OPEN_SQUARE
:
14764 /* Consume the `['. */
14765 cp_lexer_consume_token (parser
->lexer
);
14766 /* Look for the matching `]'. */
14767 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14768 return cp_operator_id (ARRAY_REF
);
14770 case CPP_UTF8STRING
:
14771 case CPP_UTF8STRING_USERDEF
:
14778 case CPP_STRING_USERDEF
:
14779 case CPP_WSTRING_USERDEF
:
14780 case CPP_STRING16_USERDEF
:
14781 case CPP_STRING32_USERDEF
:
14783 tree str
, string_tree
;
14786 if (cxx_dialect
== cxx98
)
14787 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14789 /* Consume the string. */
14790 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14791 /*wide_ok=*/true, /*lookup_udlit=*/false);
14792 if (str
== error_mark_node
)
14793 return error_mark_node
;
14794 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14796 string_tree
= USERDEF_LITERAL_VALUE (str
);
14797 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14802 /* Look for the suffix identifier. */
14803 token
= cp_lexer_peek_token (parser
->lexer
);
14804 if (token
->type
== CPP_NAME
)
14805 id
= cp_parser_identifier (parser
);
14806 else if (token
->type
== CPP_KEYWORD
)
14808 error ("unexpected keyword;"
14809 " remove space between quotes and suffix identifier");
14810 return error_mark_node
;
14814 error ("expected suffix identifier");
14815 return error_mark_node
;
14818 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14819 (TREE_TYPE (TREE_TYPE (string_tree
))));
14820 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14823 error ("expected empty string after %<operator%> keyword");
14824 return error_mark_node
;
14826 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14829 error ("invalid encoding prefix in literal operator");
14830 return error_mark_node
;
14832 if (id
!= error_mark_node
)
14834 const char *name
= IDENTIFIER_POINTER (id
);
14835 id
= cp_literal_operator_id (name
);
14841 /* Anything else is an error. */
14845 /* If we have selected an identifier, we need to consume the
14848 cp_lexer_consume_token (parser
->lexer
);
14849 /* Otherwise, no valid operator name was present. */
14852 cp_parser_error (parser
, "expected operator");
14853 id
= error_mark_node
;
14856 return cp_expr (id
, start_loc
);
14859 /* Parse a template-declaration.
14861 template-declaration:
14862 export [opt] template < template-parameter-list > declaration
14864 If MEMBER_P is TRUE, this template-declaration occurs within a
14867 The grammar rule given by the standard isn't correct. What
14868 is really meant is:
14870 template-declaration:
14871 export [opt] template-parameter-list-seq
14872 decl-specifier-seq [opt] init-declarator [opt] ;
14873 export [opt] template-parameter-list-seq
14874 function-definition
14876 template-parameter-list-seq:
14877 template-parameter-list-seq [opt]
14878 template < template-parameter-list >
14880 Concept Extensions:
14882 template-parameter-list-seq:
14883 template < template-parameter-list > requires-clause [opt]
14886 requires logical-or-expression */
14889 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14891 /* Check for `export'. */
14892 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14894 /* Consume the `export' token. */
14895 cp_lexer_consume_token (parser
->lexer
);
14896 /* Warn that we do not support `export'. */
14897 warning (0, "keyword %<export%> not implemented, and will be ignored");
14900 cp_parser_template_declaration_after_export (parser
, member_p
);
14903 /* Parse a template-parameter-list.
14905 template-parameter-list:
14907 template-parameter-list , template-parameter
14909 Returns a TREE_LIST. Each node represents a template parameter.
14910 The nodes are connected via their TREE_CHAINs. */
14913 cp_parser_template_parameter_list (cp_parser
* parser
)
14915 tree parameter_list
= NULL_TREE
;
14917 begin_template_parm_list ();
14919 /* The loop below parses the template parms. We first need to know
14920 the total number of template parms to be able to compute proper
14921 canonical types of each dependent type. So after the loop, when
14922 we know the total number of template parms,
14923 end_template_parm_list computes the proper canonical types and
14924 fixes up the dependent types accordingly. */
14929 bool is_parameter_pack
;
14930 location_t parm_loc
;
14932 /* Parse the template-parameter. */
14933 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14934 parameter
= cp_parser_template_parameter (parser
,
14936 &is_parameter_pack
);
14937 /* Add it to the list. */
14938 if (parameter
!= error_mark_node
)
14939 parameter_list
= process_template_parm (parameter_list
,
14943 is_parameter_pack
);
14946 tree err_parm
= build_tree_list (parameter
, parameter
);
14947 parameter_list
= chainon (parameter_list
, err_parm
);
14950 /* If the next token is not a `,', we're done. */
14951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14953 /* Otherwise, consume the `,' token. */
14954 cp_lexer_consume_token (parser
->lexer
);
14957 return end_template_parm_list (parameter_list
);
14960 /* Parse a introduction-list.
14963 introduced-parameter
14964 introduction-list , introduced-parameter
14966 introduced-parameter:
14967 ...[opt] identifier
14969 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14970 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14971 WILDCARD_DECL will also have DECL_NAME set and token location in
14972 DECL_SOURCE_LOCATION. */
14975 cp_parser_introduction_list (cp_parser
*parser
)
14977 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
14981 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14983 cp_lexer_consume_token (parser
->lexer
);
14985 /* Build placeholder. */
14986 tree parm
= build_nt (WILDCARD_DECL
);
14987 DECL_SOURCE_LOCATION (parm
)
14988 = cp_lexer_peek_token (parser
->lexer
)->location
;
14989 DECL_NAME (parm
) = cp_parser_identifier (parser
);
14990 WILDCARD_PACK_P (parm
) = is_pack
;
14991 vec_safe_push (introduction_vec
, parm
);
14993 /* If the next token is not a `,', we're done. */
14994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14996 /* Otherwise, consume the `,' token. */
14997 cp_lexer_consume_token (parser
->lexer
);
15000 /* Convert the vec into a TREE_VEC. */
15001 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15004 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15005 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15007 release_tree_vector (introduction_vec
);
15008 return introduction_list
;
15011 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15012 is an abstract declarator. */
15014 static inline cp_declarator
*
15015 get_id_declarator (cp_declarator
*declarator
)
15017 cp_declarator
*d
= declarator
;
15018 while (d
&& d
->kind
!= cdk_id
)
15023 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15024 is an abstract declarator. */
15027 get_unqualified_id (cp_declarator
*declarator
)
15029 declarator
= get_id_declarator (declarator
);
15031 return declarator
->u
.id
.unqualified_name
;
15036 /* Returns true if DECL represents a constrained-parameter. */
15039 is_constrained_parameter (tree decl
)
15042 && TREE_CODE (decl
) == TYPE_DECL
15043 && CONSTRAINED_PARM_CONCEPT (decl
)
15044 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15047 /* Returns true if PARM declares a constrained-parameter. */
15050 is_constrained_parameter (cp_parameter_declarator
*parm
)
15052 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15055 /* Check that the type parameter is only a declarator-id, and that its
15056 type is not cv-qualified. */
15059 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15060 cp_parameter_declarator
*parm
)
15062 if (!parm
->declarator
)
15065 if (parm
->declarator
->kind
!= cdk_id
)
15067 cp_parser_error (parser
, "invalid constrained type parameter");
15071 /* Don't allow cv-qualified type parameters. */
15072 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15073 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15075 cp_parser_error (parser
, "cv-qualified type parameter");
15082 /* Finish parsing/processing a template type parameter and checking
15083 various restrictions. */
15086 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15088 cp_parameter_declarator
* parmdecl
)
15090 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15091 return finish_template_type_parm (class_type_node
, id
);
15093 return error_mark_node
;
15097 finish_constrained_template_template_parm (tree proto
, tree id
)
15099 /* FIXME: This should probably be copied, and we may need to adjust
15100 the template parameter depths. */
15101 tree saved_parms
= current_template_parms
;
15102 begin_template_parm_list ();
15103 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15104 end_template_parm_list ();
15106 tree parm
= finish_template_template_parm (class_type_node
, id
);
15107 current_template_parms
= saved_parms
;
15112 /* Finish parsing/processing a template template parameter by borrowing
15113 the template parameter list from the prototype parameter. */
15116 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15119 cp_parameter_declarator
*parmdecl
)
15121 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15122 return error_mark_node
;
15123 return finish_constrained_template_template_parm (proto
, id
);
15126 /* Create a new non-type template parameter from the given PARM
15130 constrained_non_type_template_parm (bool *is_non_type
,
15131 cp_parameter_declarator
*parm
)
15133 *is_non_type
= true;
15134 cp_declarator
*decl
= parm
->declarator
;
15135 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15136 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15137 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15140 /* Build a constrained template parameter based on the PARMDECL
15141 declarator. The type of PARMDECL is the constrained type, which
15142 refers to the prototype template parameter that ultimately
15143 specifies the type of the declared parameter. */
15146 finish_constrained_parameter (cp_parser
*parser
,
15147 cp_parameter_declarator
*parmdecl
,
15149 bool *is_parameter_pack
)
15151 tree decl
= parmdecl
->decl_specifiers
.type
;
15152 tree id
= get_unqualified_id (parmdecl
->declarator
);
15153 tree def
= parmdecl
->default_argument
;
15154 tree proto
= DECL_INITIAL (decl
);
15156 /* A template parameter constrained by a variadic concept shall also
15157 be declared as a template parameter pack. */
15158 bool is_variadic
= template_parameter_pack_p (proto
);
15159 if (is_variadic
&& !*is_parameter_pack
)
15160 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15162 /* Build the parameter. Return an error if the declarator was invalid. */
15164 if (TREE_CODE (proto
) == TYPE_DECL
)
15165 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15166 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15167 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15170 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15171 if (parm
== error_mark_node
)
15172 return error_mark_node
;
15174 /* Finish the parameter decl and create a node attaching the
15175 default argument and constraint. */
15176 parm
= build_tree_list (def
, parm
);
15177 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15182 /* Returns true if the parsed type actually represents the declaration
15183 of a type template-parameter. */
15186 declares_constrained_type_template_parameter (tree type
)
15188 return (is_constrained_parameter (type
)
15189 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15193 /* Returns true if the parsed type actually represents the declaration of
15194 a template template-parameter. */
15197 declares_constrained_template_template_parameter (tree type
)
15199 return (is_constrained_parameter (type
)
15200 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15203 /* Parse a default argument for a type template-parameter.
15204 Note that diagnostics are handled in cp_parser_template_parameter. */
15207 cp_parser_default_type_template_argument (cp_parser
*parser
)
15209 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15211 /* Consume the `=' token. */
15212 cp_lexer_consume_token (parser
->lexer
);
15214 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15216 /* Parse the default-argument. */
15217 push_deferring_access_checks (dk_no_deferred
);
15218 tree default_argument
= cp_parser_type_id (parser
);
15219 pop_deferring_access_checks ();
15221 if (flag_concepts
&& type_uses_auto (default_argument
))
15223 error_at (token
->location
,
15224 "invalid use of %<auto%> in default template argument");
15225 return error_mark_node
;
15228 return default_argument
;
15231 /* Parse a default argument for a template template-parameter. */
15234 cp_parser_default_template_template_argument (cp_parser
*parser
)
15236 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15240 /* Consume the `='. */
15241 cp_lexer_consume_token (parser
->lexer
);
15242 /* Parse the id-expression. */
15243 push_deferring_access_checks (dk_no_deferred
);
15244 /* save token before parsing the id-expression, for error
15246 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15247 tree default_argument
15248 = cp_parser_id_expression (parser
,
15249 /*template_keyword_p=*/false,
15250 /*check_dependency_p=*/true,
15251 /*template_p=*/&is_template
,
15252 /*declarator_p=*/false,
15253 /*optional_p=*/false);
15254 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15255 /* If the id-expression was a template-id that refers to
15256 a template-class, we already have the declaration here,
15257 so no further lookup is needed. */
15260 /* Look up the name. */
15262 = cp_parser_lookup_name (parser
, default_argument
,
15264 /*is_template=*/is_template
,
15265 /*is_namespace=*/false,
15266 /*check_dependency=*/true,
15267 /*ambiguous_decls=*/NULL
,
15269 /* See if the default argument is valid. */
15270 default_argument
= check_template_template_default_arg (default_argument
);
15271 pop_deferring_access_checks ();
15272 return default_argument
;
15275 /* Parse a template-parameter.
15277 template-parameter:
15279 parameter-declaration
15281 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15282 the parameter. The TREE_PURPOSE is the default value, if any.
15283 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15284 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15285 set to true iff this parameter is a parameter pack. */
15288 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15289 bool *is_parameter_pack
)
15292 cp_parameter_declarator
*parameter_declarator
;
15295 /* Assume it is a type parameter or a template parameter. */
15296 *is_non_type
= false;
15297 /* Assume it not a parameter pack. */
15298 *is_parameter_pack
= false;
15299 /* Peek at the next token. */
15300 token
= cp_lexer_peek_token (parser
->lexer
);
15301 /* If it is `template', we have a type-parameter. */
15302 if (token
->keyword
== RID_TEMPLATE
)
15303 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15304 /* If it is `class' or `typename' we do not know yet whether it is a
15305 type parameter or a non-type parameter. Consider:
15307 template <typename T, typename T::X X> ...
15311 template <class C, class D*> ...
15313 Here, the first parameter is a type parameter, and the second is
15314 a non-type parameter. We can tell by looking at the token after
15315 the identifier -- if it is a `,', `=', or `>' then we have a type
15317 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15319 /* Peek at the token after `class' or `typename'. */
15320 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15321 /* If it's an ellipsis, we have a template type parameter
15323 if (token
->type
== CPP_ELLIPSIS
)
15324 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15325 /* If it's an identifier, skip it. */
15326 if (token
->type
== CPP_NAME
)
15327 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15328 /* Now, see if the token looks like the end of a template
15330 if (token
->type
== CPP_COMMA
15331 || token
->type
== CPP_EQ
15332 || token
->type
== CPP_GREATER
)
15333 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15336 /* Otherwise, it is a non-type parameter or a constrained parameter.
15340 When parsing a default template-argument for a non-type
15341 template-parameter, the first non-nested `>' is taken as the end
15342 of the template parameter-list rather than a greater-than
15344 parameter_declarator
15345 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15346 /*parenthesized_p=*/NULL
);
15348 if (!parameter_declarator
)
15349 return error_mark_node
;
15351 /* If the parameter declaration is marked as a parameter pack, set
15352 *IS_PARAMETER_PACK to notify the caller. */
15353 if (parameter_declarator
->template_parameter_pack_p
)
15354 *is_parameter_pack
= true;
15356 if (parameter_declarator
->default_argument
)
15358 /* Can happen in some cases of erroneous input (c++/34892). */
15359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15360 /* Consume the `...' for better error recovery. */
15361 cp_lexer_consume_token (parser
->lexer
);
15364 // The parameter may have been constrained.
15365 if (is_constrained_parameter (parameter_declarator
))
15366 return finish_constrained_parameter (parser
,
15367 parameter_declarator
,
15369 is_parameter_pack
);
15371 // Now we're sure that the parameter is a non-type parameter.
15372 *is_non_type
= true;
15374 parm
= grokdeclarator (parameter_declarator
->declarator
,
15375 ¶meter_declarator
->decl_specifiers
,
15376 TPARM
, /*initialized=*/0,
15377 /*attrlist=*/NULL
);
15378 if (parm
== error_mark_node
)
15379 return error_mark_node
;
15381 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15384 /* Parse a type-parameter.
15387 class identifier [opt]
15388 class identifier [opt] = type-id
15389 typename identifier [opt]
15390 typename identifier [opt] = type-id
15391 template < template-parameter-list > class identifier [opt]
15392 template < template-parameter-list > class identifier [opt]
15395 GNU Extension (variadic templates):
15398 class ... identifier [opt]
15399 typename ... identifier [opt]
15401 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15402 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15403 the declaration of the parameter.
15405 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15408 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15413 /* Look for a keyword to tell us what kind of parameter this is. */
15414 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15416 return error_mark_node
;
15418 switch (token
->keyword
)
15424 tree default_argument
;
15426 /* If the next token is an ellipsis, we have a template
15428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15430 /* Consume the `...' token. */
15431 cp_lexer_consume_token (parser
->lexer
);
15432 maybe_warn_variadic_templates ();
15434 *is_parameter_pack
= true;
15437 /* If the next token is an identifier, then it names the
15439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15440 identifier
= cp_parser_identifier (parser
);
15442 identifier
= NULL_TREE
;
15444 /* Create the parameter. */
15445 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15447 /* If the next token is an `=', we have a default argument. */
15448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15451 = cp_parser_default_type_template_argument (parser
);
15453 /* Template parameter packs cannot have default
15455 if (*is_parameter_pack
)
15458 error_at (token
->location
,
15459 "template parameter pack %qD cannot have a "
15460 "default argument", identifier
);
15462 error_at (token
->location
,
15463 "template parameter packs cannot have "
15464 "default arguments");
15465 default_argument
= NULL_TREE
;
15467 else if (check_for_bare_parameter_packs (default_argument
))
15468 default_argument
= error_mark_node
;
15471 default_argument
= NULL_TREE
;
15473 /* Create the combined representation of the parameter and the
15474 default argument. */
15475 parameter
= build_tree_list (default_argument
, parameter
);
15482 tree default_argument
;
15484 /* Look for the `<'. */
15485 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15486 /* Parse the template-parameter-list. */
15487 cp_parser_template_parameter_list (parser
);
15488 /* Look for the `>'. */
15489 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15491 // If template requirements are present, parse them.
15494 tree reqs
= get_shorthand_constraints (current_template_parms
);
15495 if (tree r
= cp_parser_requires_clause_opt (parser
))
15496 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15497 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15500 /* Look for the `class' or 'typename' keywords. */
15501 cp_parser_type_parameter_key (parser
);
15502 /* If the next token is an ellipsis, we have a template
15504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15506 /* Consume the `...' token. */
15507 cp_lexer_consume_token (parser
->lexer
);
15508 maybe_warn_variadic_templates ();
15510 *is_parameter_pack
= true;
15512 /* If the next token is an `=', then there is a
15513 default-argument. If the next token is a `>', we are at
15514 the end of the parameter-list. If the next token is a `,',
15515 then we are at the end of this parameter. */
15516 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15517 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15518 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15520 identifier
= cp_parser_identifier (parser
);
15521 /* Treat invalid names as if the parameter were nameless. */
15522 if (identifier
== error_mark_node
)
15523 identifier
= NULL_TREE
;
15526 identifier
= NULL_TREE
;
15528 /* Create the template parameter. */
15529 parameter
= finish_template_template_parm (class_type_node
,
15532 /* If the next token is an `=', then there is a
15533 default-argument. */
15534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15537 = cp_parser_default_template_template_argument (parser
);
15539 /* Template parameter packs cannot have default
15541 if (*is_parameter_pack
)
15544 error_at (token
->location
,
15545 "template parameter pack %qD cannot "
15546 "have a default argument",
15549 error_at (token
->location
, "template parameter packs cannot "
15550 "have default arguments");
15551 default_argument
= NULL_TREE
;
15555 default_argument
= NULL_TREE
;
15557 /* Create the combined representation of the parameter and the
15558 default argument. */
15559 parameter
= build_tree_list (default_argument
, parameter
);
15564 gcc_unreachable ();
15571 /* Parse a template-id.
15574 template-name < template-argument-list [opt] >
15576 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15577 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15578 returned. Otherwise, if the template-name names a function, or set
15579 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15580 names a class, returns a TYPE_DECL for the specialization.
15582 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15583 uninstantiated templates. */
15586 cp_parser_template_id (cp_parser
*parser
,
15587 bool template_keyword_p
,
15588 bool check_dependency_p
,
15589 enum tag_types tag_type
,
15590 bool is_declaration
)
15595 cp_token_position start_of_id
= 0;
15596 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15597 bool is_identifier
;
15599 /* If the next token corresponds to a template-id, there is no need
15601 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15602 if (token
->type
== CPP_TEMPLATE_ID
)
15604 cp_lexer_consume_token (parser
->lexer
);
15605 return saved_checks_value (token
->u
.tree_check_value
);
15608 /* Avoid performing name lookup if there is no possibility of
15609 finding a template-id. */
15610 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15611 || (token
->type
== CPP_NAME
15612 && !cp_parser_nth_token_starts_template_argument_list_p
15615 cp_parser_error (parser
, "expected template-id");
15616 return error_mark_node
;
15619 /* Remember where the template-id starts. */
15620 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15621 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15623 push_deferring_access_checks (dk_deferred
);
15625 /* Parse the template-name. */
15626 is_identifier
= false;
15627 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15628 check_dependency_p
,
15632 if (templ
== error_mark_node
|| is_identifier
)
15634 pop_deferring_access_checks ();
15638 /* Since we're going to preserve any side-effects from this parse, set up a
15639 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15640 in the template arguments. */
15641 tentative_firewall
firewall (parser
);
15643 /* If we find the sequence `[:' after a template-name, it's probably
15644 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15645 parse correctly the argument list. */
15646 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15647 == CPP_OPEN_SQUARE
)
15648 && next_token
->flags
& DIGRAPH
15649 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15651 && !(next_token_2
->flags
& PREV_WHITE
))
15653 cp_parser_parse_tentatively (parser
);
15654 /* Change `:' into `::'. */
15655 next_token_2
->type
= CPP_SCOPE
;
15656 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15658 cp_lexer_consume_token (parser
->lexer
);
15660 /* Parse the arguments. */
15661 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15662 if (!cp_parser_parse_definitely (parser
))
15664 /* If we couldn't parse an argument list, then we revert our changes
15665 and return simply an error. Maybe this is not a template-id
15667 next_token_2
->type
= CPP_COLON
;
15668 cp_parser_error (parser
, "expected %<<%>");
15669 pop_deferring_access_checks ();
15670 return error_mark_node
;
15672 /* Otherwise, emit an error about the invalid digraph, but continue
15673 parsing because we got our argument list. */
15674 if (permerror (next_token
->location
,
15675 "%<<::%> cannot begin a template-argument list"))
15677 static bool hint
= false;
15678 inform (next_token
->location
,
15679 "%<<:%> is an alternate spelling for %<[%>."
15680 " Insert whitespace between %<<%> and %<::%>");
15681 if (!hint
&& !flag_permissive
)
15683 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15684 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15685 "accept your code)");
15692 /* Look for the `<' that starts the template-argument-list. */
15693 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15695 pop_deferring_access_checks ();
15696 return error_mark_node
;
15698 /* Parse the arguments. */
15699 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15702 /* Set the location to be of the form:
15703 template-name < template-argument-list [opt] >
15704 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15705 with caret == start at the start of the template-name,
15706 ranging until the closing '>'. */
15707 location_t finish_loc
15708 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15709 location_t combined_loc
15710 = make_location (token
->location
, token
->location
, finish_loc
);
15712 /* Build a representation of the specialization. */
15713 if (identifier_p (templ
))
15714 template_id
= build_min_nt_loc (combined_loc
,
15717 else if (DECL_TYPE_TEMPLATE_P (templ
)
15718 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15720 bool entering_scope
;
15721 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15722 template (rather than some instantiation thereof) only if
15723 is not nested within some other construct. For example, in
15724 "template <typename T> void f(T) { A<T>::", A<T> is just an
15725 instantiation of A. */
15726 entering_scope
= (template_parm_scope_p ()
15727 && cp_lexer_next_token_is (parser
->lexer
,
15730 = finish_template_type (templ
, arguments
, entering_scope
);
15732 /* A template-like identifier may be a partial concept id. */
15733 else if (flag_concepts
15734 && (template_id
= (cp_parser_maybe_partial_concept_id
15735 (parser
, templ
, arguments
))))
15736 return template_id
;
15737 else if (variable_template_p (templ
))
15739 template_id
= lookup_template_variable (templ
, arguments
);
15740 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15741 SET_EXPR_LOCATION (template_id
, combined_loc
);
15745 /* If it's not a class-template or a template-template, it should be
15746 a function-template. */
15747 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15748 || TREE_CODE (templ
) == OVERLOAD
15749 || BASELINK_P (templ
)));
15751 template_id
= lookup_template_function (templ
, arguments
);
15752 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15753 SET_EXPR_LOCATION (template_id
, combined_loc
);
15756 /* If parsing tentatively, replace the sequence of tokens that makes
15757 up the template-id with a CPP_TEMPLATE_ID token. That way,
15758 should we re-parse the token stream, we will not have to repeat
15759 the effort required to do the parse, nor will we issue duplicate
15760 error messages about problems during instantiation of the
15763 /* Don't do this if we had a parse error in a declarator; re-parsing
15764 might succeed if a name changes meaning (60361). */
15765 && !(cp_parser_error_occurred (parser
)
15766 && cp_parser_parsing_tentatively (parser
)
15767 && parser
->in_declarator_p
))
15769 /* Reset the contents of the START_OF_ID token. */
15770 token
->type
= CPP_TEMPLATE_ID
;
15771 token
->location
= combined_loc
;
15773 /* We must mark the lookup as kept, so we don't throw it away on
15774 the first parse. */
15775 if (is_overloaded_fn (template_id
))
15776 lookup_keep (get_fns (template_id
), true);
15778 /* Retrieve any deferred checks. Do not pop this access checks yet
15779 so the memory will not be reclaimed during token replacing below. */
15780 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15781 token
->u
.tree_check_value
->value
= template_id
;
15782 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15783 token
->keyword
= RID_MAX
;
15785 /* Purge all subsequent tokens. */
15786 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15788 /* ??? Can we actually assume that, if template_id ==
15789 error_mark_node, we will have issued a diagnostic to the
15790 user, as opposed to simply marking the tentative parse as
15792 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15793 error_at (token
->location
, "parse error in template argument list");
15796 pop_to_parent_deferring_access_checks ();
15797 return template_id
;
15800 /* Parse a template-name.
15805 The standard should actually say:
15809 operator-function-id
15811 A defect report has been filed about this issue.
15813 A conversion-function-id cannot be a template name because they cannot
15814 be part of a template-id. In fact, looking at this code:
15816 a.operator K<int>()
15818 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15819 It is impossible to call a templated conversion-function-id with an
15820 explicit argument list, since the only allowed template parameter is
15821 the type to which it is converting.
15823 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15824 `template' keyword, in a construction like:
15828 In that case `f' is taken to be a template-name, even though there
15829 is no way of knowing for sure.
15831 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15832 name refers to a set of overloaded functions, at least one of which
15833 is a template, or an IDENTIFIER_NODE with the name of the template,
15834 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15835 names are looked up inside uninstantiated templates. */
15838 cp_parser_template_name (cp_parser
* parser
,
15839 bool template_keyword_p
,
15840 bool check_dependency_p
,
15841 bool is_declaration
,
15842 enum tag_types tag_type
,
15843 bool *is_identifier
)
15847 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15849 /* If the next token is `operator', then we have either an
15850 operator-function-id or a conversion-function-id. */
15851 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15853 /* We don't know whether we're looking at an
15854 operator-function-id or a conversion-function-id. */
15855 cp_parser_parse_tentatively (parser
);
15856 /* Try an operator-function-id. */
15857 identifier
= cp_parser_operator_function_id (parser
);
15858 /* If that didn't work, try a conversion-function-id. */
15859 if (!cp_parser_parse_definitely (parser
))
15861 cp_parser_error (parser
, "expected template-name");
15862 return error_mark_node
;
15865 /* Look for the identifier. */
15867 identifier
= cp_parser_identifier (parser
);
15869 /* If we didn't find an identifier, we don't have a template-id. */
15870 if (identifier
== error_mark_node
)
15871 return error_mark_node
;
15873 /* If the name immediately followed the `template' keyword, then it
15874 is a template-name. However, if the next token is not `<', then
15875 we do not treat it as a template-name, since it is not being used
15876 as part of a template-id. This enables us to handle constructs
15879 template <typename T> struct S { S(); };
15880 template <typename T> S<T>::S();
15882 correctly. We would treat `S' as a template -- if it were `S<T>'
15883 -- but we do not if there is no `<'. */
15885 if (processing_template_decl
15886 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15888 /* In a declaration, in a dependent context, we pretend that the
15889 "template" keyword was present in order to improve error
15890 recovery. For example, given:
15892 template <typename T> void f(T::X<int>);
15894 we want to treat "X<int>" as a template-id. */
15896 && !template_keyword_p
15897 && parser
->scope
&& TYPE_P (parser
->scope
)
15898 && check_dependency_p
15899 && dependent_scope_p (parser
->scope
)
15900 /* Do not do this for dtors (or ctors), since they never
15901 need the template keyword before their name. */
15902 && !constructor_name_p (identifier
, parser
->scope
))
15904 cp_token_position start
= 0;
15906 /* Explain what went wrong. */
15907 error_at (token
->location
, "non-template %qD used as template",
15909 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15910 parser
->scope
, identifier
);
15911 /* If parsing tentatively, find the location of the "<" token. */
15912 if (cp_parser_simulate_error (parser
))
15913 start
= cp_lexer_token_position (parser
->lexer
, true);
15914 /* Parse the template arguments so that we can issue error
15915 messages about them. */
15916 cp_lexer_consume_token (parser
->lexer
);
15917 cp_parser_enclosed_template_argument_list (parser
);
15918 /* Skip tokens until we find a good place from which to
15919 continue parsing. */
15920 cp_parser_skip_to_closing_parenthesis (parser
,
15921 /*recovering=*/true,
15923 /*consume_paren=*/false);
15924 /* If parsing tentatively, permanently remove the
15925 template argument list. That will prevent duplicate
15926 error messages from being issued about the missing
15927 "template" keyword. */
15929 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15931 *is_identifier
= true;
15932 parser
->context
->object_type
= NULL_TREE
;
15936 /* If the "template" keyword is present, then there is generally
15937 no point in doing name-lookup, so we just return IDENTIFIER.
15938 But, if the qualifying scope is non-dependent then we can
15939 (and must) do name-lookup normally. */
15940 if (template_keyword_p
)
15942 tree scope
= (parser
->scope
? parser
->scope
15943 : parser
->context
->object_type
);
15944 if (scope
&& TYPE_P (scope
)
15945 && (!CLASS_TYPE_P (scope
)
15946 || (check_dependency_p
&& dependent_type_p (scope
))))
15948 /* We're optimizing away the call to cp_parser_lookup_name, but
15949 we still need to do this. */
15950 parser
->context
->object_type
= NULL_TREE
;
15956 /* Look up the name. */
15957 decl
= cp_parser_lookup_name (parser
, identifier
,
15959 /*is_template=*/true,
15960 /*is_namespace=*/false,
15961 check_dependency_p
,
15962 /*ambiguous_decls=*/NULL
,
15965 decl
= strip_using_decl (decl
);
15967 /* If DECL is a template, then the name was a template-name. */
15968 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
15970 if (TREE_DEPRECATED (decl
)
15971 && deprecated_state
!= DEPRECATED_SUPPRESS
)
15972 warn_deprecated_use (decl
, NULL_TREE
);
15976 /* The standard does not explicitly indicate whether a name that
15977 names a set of overloaded declarations, some of which are
15978 templates, is a template-name. However, such a name should
15979 be a template-name; otherwise, there is no way to form a
15980 template-id for the overloaded templates. */
15981 bool found
= false;
15983 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
15984 !found
&& iter
; ++iter
)
15985 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
15990 /* The name does not name a template. */
15991 cp_parser_error (parser
, "expected template-name");
15992 return error_mark_node
;
15996 /* If DECL is dependent, and refers to a function, then just return
15997 its name; we will look it up again during template instantiation. */
15998 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16000 tree scope
= ovl_scope (decl
);
16001 if (TYPE_P (scope
) && dependent_type_p (scope
))
16008 /* Parse a template-argument-list.
16010 template-argument-list:
16011 template-argument ... [opt]
16012 template-argument-list , template-argument ... [opt]
16014 Returns a TREE_VEC containing the arguments. */
16017 cp_parser_template_argument_list (cp_parser
* parser
)
16019 tree fixed_args
[10];
16020 unsigned n_args
= 0;
16021 unsigned alloced
= 10;
16022 tree
*arg_ary
= fixed_args
;
16024 bool saved_in_template_argument_list_p
;
16026 bool saved_non_ice_p
;
16028 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16029 parser
->in_template_argument_list_p
= true;
16030 /* Even if the template-id appears in an integral
16031 constant-expression, the contents of the argument list do
16033 saved_ice_p
= parser
->integral_constant_expression_p
;
16034 parser
->integral_constant_expression_p
= false;
16035 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16036 parser
->non_integral_constant_expression_p
= false;
16038 /* Parse the arguments. */
16044 /* Consume the comma. */
16045 cp_lexer_consume_token (parser
->lexer
);
16047 /* Parse the template-argument. */
16048 argument
= cp_parser_template_argument (parser
);
16050 /* If the next token is an ellipsis, we're expanding a template
16052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16054 if (argument
== error_mark_node
)
16056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16057 error_at (token
->location
,
16058 "expected parameter pack before %<...%>");
16060 /* Consume the `...' token. */
16061 cp_lexer_consume_token (parser
->lexer
);
16063 /* Make the argument into a TYPE_PACK_EXPANSION or
16064 EXPR_PACK_EXPANSION. */
16065 argument
= make_pack_expansion (argument
);
16068 if (n_args
== alloced
)
16072 if (arg_ary
== fixed_args
)
16074 arg_ary
= XNEWVEC (tree
, alloced
);
16075 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16078 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16080 arg_ary
[n_args
++] = argument
;
16082 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16084 vec
= make_tree_vec (n_args
);
16087 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16089 if (arg_ary
!= fixed_args
)
16091 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16092 parser
->integral_constant_expression_p
= saved_ice_p
;
16093 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16095 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16099 /* Parse a template-argument.
16102 assignment-expression
16106 The representation is that of an assignment-expression, type-id, or
16107 id-expression -- except that the qualified id-expression is
16108 evaluated, so that the value returned is either a DECL or an
16111 Although the standard says "assignment-expression", it forbids
16112 throw-expressions or assignments in the template argument.
16113 Therefore, we use "conditional-expression" instead. */
16116 cp_parser_template_argument (cp_parser
* parser
)
16121 bool maybe_type_id
= false;
16122 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16123 location_t loc
= 0;
16126 /* There's really no way to know what we're looking at, so we just
16127 try each alternative in order.
16131 In a template-argument, an ambiguity between a type-id and an
16132 expression is resolved to a type-id, regardless of the form of
16133 the corresponding template-parameter.
16135 Therefore, we try a type-id first. */
16136 cp_parser_parse_tentatively (parser
);
16137 argument
= cp_parser_template_type_arg (parser
);
16138 /* If there was no error parsing the type-id but the next token is a
16139 '>>', our behavior depends on which dialect of C++ we're
16140 parsing. In C++98, we probably found a typo for '> >'. But there
16141 are type-id which are also valid expressions. For instance:
16143 struct X { int operator >> (int); };
16144 template <int V> struct Foo {};
16147 Here 'X()' is a valid type-id of a function type, but the user just
16148 wanted to write the expression "X() >> 5". Thus, we remember that we
16149 found a valid type-id, but we still try to parse the argument as an
16150 expression to see what happens.
16152 In C++0x, the '>>' will be considered two separate '>'
16154 if (!cp_parser_error_occurred (parser
)
16155 && cxx_dialect
== cxx98
16156 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16158 maybe_type_id
= true;
16159 cp_parser_abort_tentative_parse (parser
);
16163 /* If the next token isn't a `,' or a `>', then this argument wasn't
16164 really finished. This means that the argument is not a valid
16166 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16167 cp_parser_error (parser
, "expected template-argument");
16168 /* If that worked, we're done. */
16169 if (cp_parser_parse_definitely (parser
))
16172 /* We're still not sure what the argument will be. */
16173 cp_parser_parse_tentatively (parser
);
16174 /* Try a template. */
16175 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16176 argument
= cp_parser_id_expression (parser
,
16177 /*template_keyword_p=*/false,
16178 /*check_dependency_p=*/true,
16180 /*declarator_p=*/false,
16181 /*optional_p=*/false);
16182 /* If the next token isn't a `,' or a `>', then this argument wasn't
16183 really finished. */
16184 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16185 cp_parser_error (parser
, "expected template-argument");
16186 if (!cp_parser_error_occurred (parser
))
16188 /* Figure out what is being referred to. If the id-expression
16189 was for a class template specialization, then we will have a
16190 TYPE_DECL at this point. There is no need to do name lookup
16191 at this point in that case. */
16192 if (TREE_CODE (argument
) != TYPE_DECL
)
16193 argument
= cp_parser_lookup_name (parser
, argument
,
16195 /*is_template=*/template_p
,
16196 /*is_namespace=*/false,
16197 /*check_dependency=*/true,
16198 /*ambiguous_decls=*/NULL
,
16199 argument_start_token
->location
);
16200 /* Handle a constrained-type-specifier for a non-type template
16202 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16204 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16205 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16206 cp_parser_error (parser
, "expected template-name");
16208 if (cp_parser_parse_definitely (parser
))
16210 if (TREE_DEPRECATED (argument
))
16211 warn_deprecated_use (argument
, NULL_TREE
);
16214 /* It must be a non-type argument. In C++17 any constant-expression is
16216 if (cxx_dialect
> cxx14
)
16219 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16221 -- an integral constant-expression of integral or enumeration
16224 -- the name of a non-type template-parameter; or
16226 -- the name of an object or function with external linkage...
16228 -- the address of an object or function with external linkage...
16230 -- a pointer to member... */
16231 /* Look for a non-type template parameter. */
16232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16234 cp_parser_parse_tentatively (parser
);
16235 argument
= cp_parser_primary_expression (parser
,
16236 /*address_p=*/false,
16238 /*template_arg_p=*/true,
16240 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16241 || !cp_parser_next_token_ends_template_argument_p (parser
))
16242 cp_parser_simulate_error (parser
);
16243 if (cp_parser_parse_definitely (parser
))
16247 /* If the next token is "&", the argument must be the address of an
16248 object or function with external linkage. */
16249 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16252 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16253 cp_lexer_consume_token (parser
->lexer
);
16255 /* See if we might have an id-expression. */
16256 token
= cp_lexer_peek_token (parser
->lexer
);
16257 if (token
->type
== CPP_NAME
16258 || token
->keyword
== RID_OPERATOR
16259 || token
->type
== CPP_SCOPE
16260 || token
->type
== CPP_TEMPLATE_ID
16261 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16263 cp_parser_parse_tentatively (parser
);
16264 argument
= cp_parser_primary_expression (parser
,
16267 /*template_arg_p=*/true,
16269 if (cp_parser_error_occurred (parser
)
16270 || !cp_parser_next_token_ends_template_argument_p (parser
))
16271 cp_parser_abort_tentative_parse (parser
);
16276 if (INDIRECT_REF_P (argument
))
16278 /* Strip the dereference temporarily. */
16279 gcc_assert (REFERENCE_REF_P (argument
));
16280 argument
= TREE_OPERAND (argument
, 0);
16283 /* If we're in a template, we represent a qualified-id referring
16284 to a static data member as a SCOPE_REF even if the scope isn't
16285 dependent so that we can check access control later. */
16287 if (TREE_CODE (probe
) == SCOPE_REF
)
16288 probe
= TREE_OPERAND (probe
, 1);
16291 /* A variable without external linkage might still be a
16292 valid constant-expression, so no error is issued here
16293 if the external-linkage check fails. */
16294 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16295 cp_parser_simulate_error (parser
);
16297 else if (is_overloaded_fn (argument
))
16298 /* All overloaded functions are allowed; if the external
16299 linkage test does not pass, an error will be issued
16303 && (TREE_CODE (argument
) == OFFSET_REF
16304 || TREE_CODE (argument
) == SCOPE_REF
))
16305 /* A pointer-to-member. */
16307 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16310 cp_parser_simulate_error (parser
);
16312 if (cp_parser_parse_definitely (parser
))
16315 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16316 tf_warning_or_error
);
16318 argument
= convert_from_reference (argument
);
16323 /* If the argument started with "&", there are no other valid
16324 alternatives at this point. */
16327 cp_parser_error (parser
, "invalid non-type template argument");
16328 return error_mark_node
;
16332 /* If the argument wasn't successfully parsed as a type-id followed
16333 by '>>', the argument can only be a constant expression now.
16334 Otherwise, we try parsing the constant-expression tentatively,
16335 because the argument could really be a type-id. */
16337 cp_parser_parse_tentatively (parser
);
16339 if (cxx_dialect
<= cxx14
)
16340 argument
= cp_parser_constant_expression (parser
);
16343 /* With C++17 generalized non-type template arguments we need to handle
16344 lvalue constant expressions, too. */
16345 argument
= cp_parser_assignment_expression (parser
);
16346 require_potential_constant_expression (argument
);
16349 if (!maybe_type_id
)
16351 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16352 cp_parser_error (parser
, "expected template-argument");
16353 if (cp_parser_parse_definitely (parser
))
16355 /* We did our best to parse the argument as a non type-id, but that
16356 was the only alternative that matched (albeit with a '>' after
16357 it). We can assume it's just a typo from the user, and a
16358 diagnostic will then be issued. */
16359 return cp_parser_template_type_arg (parser
);
16362 /* Parse an explicit-instantiation.
16364 explicit-instantiation:
16365 template declaration
16367 Although the standard says `declaration', what it really means is:
16369 explicit-instantiation:
16370 template decl-specifier-seq [opt] declarator [opt] ;
16372 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16373 supposed to be allowed. A defect report has been filed about this
16378 explicit-instantiation:
16379 storage-class-specifier template
16380 decl-specifier-seq [opt] declarator [opt] ;
16381 function-specifier template
16382 decl-specifier-seq [opt] declarator [opt] ; */
16385 cp_parser_explicit_instantiation (cp_parser
* parser
)
16387 int declares_class_or_enum
;
16388 cp_decl_specifier_seq decl_specifiers
;
16389 tree extension_specifier
= NULL_TREE
;
16391 timevar_push (TV_TEMPLATE_INST
);
16393 /* Look for an (optional) storage-class-specifier or
16394 function-specifier. */
16395 if (cp_parser_allow_gnu_extensions_p (parser
))
16397 extension_specifier
16398 = cp_parser_storage_class_specifier_opt (parser
);
16399 if (!extension_specifier
)
16400 extension_specifier
16401 = cp_parser_function_specifier_opt (parser
,
16402 /*decl_specs=*/NULL
);
16405 /* Look for the `template' keyword. */
16406 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16407 /* Let the front end know that we are processing an explicit
16409 begin_explicit_instantiation ();
16410 /* [temp.explicit] says that we are supposed to ignore access
16411 control while processing explicit instantiation directives. */
16412 push_deferring_access_checks (dk_no_check
);
16413 /* Parse a decl-specifier-seq. */
16414 cp_parser_decl_specifier_seq (parser
,
16415 CP_PARSER_FLAGS_OPTIONAL
,
16417 &declares_class_or_enum
);
16418 /* If there was exactly one decl-specifier, and it declared a class,
16419 and there's no declarator, then we have an explicit type
16421 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16425 type
= check_tag_decl (&decl_specifiers
,
16426 /*explicit_type_instantiation_p=*/true);
16427 /* Turn access control back on for names used during
16428 template instantiation. */
16429 pop_deferring_access_checks ();
16431 do_type_instantiation (type
, extension_specifier
,
16432 /*complain=*/tf_error
);
16436 cp_declarator
*declarator
;
16439 /* Parse the declarator. */
16441 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16442 /*ctor_dtor_or_conv_p=*/NULL
,
16443 /*parenthesized_p=*/NULL
,
16444 /*member_p=*/false,
16445 /*friend_p=*/false);
16446 if (declares_class_or_enum
& 2)
16447 cp_parser_check_for_definition_in_return_type (declarator
,
16448 decl_specifiers
.type
,
16449 decl_specifiers
.locations
[ds_type_spec
]);
16450 if (declarator
!= cp_error_declarator
)
16452 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16453 permerror (decl_specifiers
.locations
[ds_inline
],
16454 "explicit instantiation shall not use"
16455 " %<inline%> specifier");
16456 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16457 permerror (decl_specifiers
.locations
[ds_constexpr
],
16458 "explicit instantiation shall not use"
16459 " %<constexpr%> specifier");
16461 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16462 NORMAL
, 0, &decl_specifiers
.attributes
);
16463 /* Turn access control back on for names used during
16464 template instantiation. */
16465 pop_deferring_access_checks ();
16466 /* Do the explicit instantiation. */
16467 do_decl_instantiation (decl
, extension_specifier
);
16471 pop_deferring_access_checks ();
16472 /* Skip the body of the explicit instantiation. */
16473 cp_parser_skip_to_end_of_statement (parser
);
16476 /* We're done with the instantiation. */
16477 end_explicit_instantiation ();
16479 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16481 timevar_pop (TV_TEMPLATE_INST
);
16484 /* Parse an explicit-specialization.
16486 explicit-specialization:
16487 template < > declaration
16489 Although the standard says `declaration', what it really means is:
16491 explicit-specialization:
16492 template <> decl-specifier [opt] init-declarator [opt] ;
16493 template <> function-definition
16494 template <> explicit-specialization
16495 template <> template-declaration */
16498 cp_parser_explicit_specialization (cp_parser
* parser
)
16500 bool need_lang_pop
;
16501 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16503 /* Look for the `template' keyword. */
16504 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16505 /* Look for the `<'. */
16506 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16507 /* Look for the `>'. */
16508 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16509 /* We have processed another parameter list. */
16510 ++parser
->num_template_parameter_lists
;
16513 A template ... explicit specialization ... shall not have C
16515 if (current_lang_name
== lang_name_c
)
16517 error_at (token
->location
, "template specialization with C linkage");
16518 /* Give it C++ linkage to avoid confusing other parts of the
16520 push_lang_context (lang_name_cplusplus
);
16521 need_lang_pop
= true;
16524 need_lang_pop
= false;
16525 /* Let the front end know that we are beginning a specialization. */
16526 if (!begin_specialization ())
16528 end_specialization ();
16532 /* If the next keyword is `template', we need to figure out whether
16533 or not we're looking a template-declaration. */
16534 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16536 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16537 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16538 cp_parser_template_declaration_after_export (parser
,
16539 /*member_p=*/false);
16541 cp_parser_explicit_specialization (parser
);
16544 /* Parse the dependent declaration. */
16545 cp_parser_single_declaration (parser
,
16547 /*member_p=*/false,
16548 /*explicit_specialization_p=*/true,
16549 /*friend_p=*/NULL
);
16550 /* We're done with the specialization. */
16551 end_specialization ();
16552 /* For the erroneous case of a template with C linkage, we pushed an
16553 implicit C++ linkage scope; exit that scope now. */
16555 pop_lang_context ();
16556 /* We're done with this parameter list. */
16557 --parser
->num_template_parameter_lists
;
16560 /* Parse a type-specifier.
16563 simple-type-specifier
16566 elaborated-type-specifier
16574 Returns a representation of the type-specifier. For a
16575 class-specifier, enum-specifier, or elaborated-type-specifier, a
16576 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16578 The parser flags FLAGS is used to control type-specifier parsing.
16580 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16581 in a decl-specifier-seq.
16583 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16584 class-specifier, enum-specifier, or elaborated-type-specifier, then
16585 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16586 if a type is declared; 2 if it is defined. Otherwise, it is set to
16589 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16590 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16591 is set to FALSE. */
16594 cp_parser_type_specifier (cp_parser
* parser
,
16595 cp_parser_flags flags
,
16596 cp_decl_specifier_seq
*decl_specs
,
16597 bool is_declaration
,
16598 int* declares_class_or_enum
,
16599 bool* is_cv_qualifier
)
16601 tree type_spec
= NULL_TREE
;
16604 cp_decl_spec ds
= ds_last
;
16606 /* Assume this type-specifier does not declare a new type. */
16607 if (declares_class_or_enum
)
16608 *declares_class_or_enum
= 0;
16609 /* And that it does not specify a cv-qualifier. */
16610 if (is_cv_qualifier
)
16611 *is_cv_qualifier
= false;
16612 /* Peek at the next token. */
16613 token
= cp_lexer_peek_token (parser
->lexer
);
16615 /* If we're looking at a keyword, we can use that to guide the
16616 production we choose. */
16617 keyword
= token
->keyword
;
16621 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16622 goto elaborated_type_specifier
;
16624 /* Look for the enum-specifier. */
16625 type_spec
= cp_parser_enum_specifier (parser
);
16626 /* If that worked, we're done. */
16629 if (declares_class_or_enum
)
16630 *declares_class_or_enum
= 2;
16632 cp_parser_set_decl_spec_type (decl_specs
,
16635 /*type_definition_p=*/true);
16639 goto elaborated_type_specifier
;
16641 /* Any of these indicate either a class-specifier, or an
16642 elaborated-type-specifier. */
16646 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16647 goto elaborated_type_specifier
;
16649 /* Parse tentatively so that we can back up if we don't find a
16650 class-specifier. */
16651 cp_parser_parse_tentatively (parser
);
16652 /* Look for the class-specifier. */
16653 type_spec
= cp_parser_class_specifier (parser
);
16654 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16655 /* If that worked, we're done. */
16656 if (cp_parser_parse_definitely (parser
))
16658 if (declares_class_or_enum
)
16659 *declares_class_or_enum
= 2;
16661 cp_parser_set_decl_spec_type (decl_specs
,
16664 /*type_definition_p=*/true);
16668 /* Fall through. */
16669 elaborated_type_specifier
:
16670 /* We're declaring (not defining) a class or enum. */
16671 if (declares_class_or_enum
)
16672 *declares_class_or_enum
= 1;
16674 /* Fall through. */
16676 /* Look for an elaborated-type-specifier. */
16678 = (cp_parser_elaborated_type_specifier
16680 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16683 cp_parser_set_decl_spec_type (decl_specs
,
16686 /*type_definition_p=*/false);
16691 if (is_cv_qualifier
)
16692 *is_cv_qualifier
= true;
16697 if (is_cv_qualifier
)
16698 *is_cv_qualifier
= true;
16703 if (is_cv_qualifier
)
16704 *is_cv_qualifier
= true;
16708 /* The `__complex__' keyword is a GNU extension. */
16716 /* Handle simple keywords. */
16721 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16722 decl_specs
->any_specifiers_p
= true;
16724 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16727 /* If we do not already have a type-specifier, assume we are looking
16728 at a simple-type-specifier. */
16729 type_spec
= cp_parser_simple_type_specifier (parser
,
16733 /* If we didn't find a type-specifier, and a type-specifier was not
16734 optional in this context, issue an error message. */
16735 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16737 cp_parser_error (parser
, "expected type specifier");
16738 return error_mark_node
;
16744 /* Parse a simple-type-specifier.
16746 simple-type-specifier:
16747 :: [opt] nested-name-specifier [opt] type-name
16748 :: [opt] nested-name-specifier template template-id
16763 simple-type-specifier:
16765 decltype ( expression )
16768 __underlying_type ( type-id )
16772 nested-name-specifier(opt) template-name
16776 simple-type-specifier:
16778 __typeof__ unary-expression
16779 __typeof__ ( type-id )
16780 __typeof__ ( type-id ) { initializer-list , [opt] }
16782 Concepts Extension:
16784 simple-type-specifier:
16785 constrained-type-specifier
16787 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16788 appropriately updated. */
16791 cp_parser_simple_type_specifier (cp_parser
* parser
,
16792 cp_decl_specifier_seq
*decl_specs
,
16793 cp_parser_flags flags
)
16795 tree type
= NULL_TREE
;
16799 /* Peek at the next token. */
16800 token
= cp_lexer_peek_token (parser
->lexer
);
16802 /* If we're looking at a keyword, things are easy. */
16803 switch (token
->keyword
)
16807 decl_specs
->explicit_char_p
= true;
16808 type
= char_type_node
;
16811 type
= char16_type_node
;
16814 type
= char32_type_node
;
16817 type
= wchar_type_node
;
16820 type
= boolean_type_node
;
16823 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16824 type
= short_integer_type_node
;
16828 decl_specs
->explicit_int_p
= true;
16829 type
= integer_type_node
;
16835 idx
= token
->keyword
- RID_INT_N_0
;
16836 if (! int_n_enabled_p
[idx
])
16840 decl_specs
->explicit_intN_p
= true;
16841 decl_specs
->int_n_idx
= idx
;
16843 type
= int_n_trees
[idx
].signed_type
;
16847 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16848 type
= long_integer_type_node
;
16851 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16852 type
= integer_type_node
;
16855 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16856 type
= unsigned_type_node
;
16859 type
= float_type_node
;
16862 type
= double_type_node
;
16865 type
= void_type_node
;
16869 maybe_warn_cpp0x (CPP0X_AUTO
);
16870 if (parser
->auto_is_implicit_function_template_parm_p
)
16872 /* The 'auto' might be the placeholder return type for a function decl
16873 with trailing return type. */
16874 bool have_trailing_return_fn_decl
= false;
16876 cp_parser_parse_tentatively (parser
);
16877 cp_lexer_consume_token (parser
->lexer
);
16878 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16879 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16880 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16881 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16885 cp_lexer_consume_token (parser
->lexer
);
16886 cp_parser_skip_to_closing_parenthesis (parser
,
16887 /*recovering*/false,
16889 /*consume_paren*/true);
16893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16895 have_trailing_return_fn_decl
= true;
16899 cp_lexer_consume_token (parser
->lexer
);
16901 cp_parser_abort_tentative_parse (parser
);
16903 if (have_trailing_return_fn_decl
)
16905 type
= make_auto ();
16909 if (cxx_dialect
>= cxx14
)
16911 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16912 type
= TREE_TYPE (type
);
16915 type
= error_mark_node
;
16917 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16919 if (cxx_dialect
< cxx14
)
16920 error_at (token
->location
,
16921 "use of %<auto%> in lambda parameter declaration "
16922 "only available with "
16923 "-std=c++14 or -std=gnu++14");
16925 else if (cxx_dialect
< cxx14
)
16926 error_at (token
->location
,
16927 "use of %<auto%> in parameter declaration "
16928 "only available with "
16929 "-std=c++14 or -std=gnu++14");
16930 else if (!flag_concepts
)
16931 pedwarn (token
->location
, OPT_Wpedantic
,
16932 "ISO C++ forbids use of %<auto%> in parameter "
16936 type
= make_auto ();
16940 /* Since DR 743, decltype can either be a simple-type-specifier by
16941 itself or begin a nested-name-specifier. Parsing it will replace
16942 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16943 handling below decide what to do. */
16944 cp_parser_decltype (parser
);
16945 cp_lexer_set_token_position (parser
->lexer
, token
);
16949 /* Consume the `typeof' token. */
16950 cp_lexer_consume_token (parser
->lexer
);
16951 /* Parse the operand to `typeof'. */
16952 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16953 /* If it is not already a TYPE, take its type. */
16954 if (!TYPE_P (type
))
16955 type
= finish_typeof (type
);
16958 cp_parser_set_decl_spec_type (decl_specs
, type
,
16960 /*type_definition_p=*/false);
16964 case RID_UNDERLYING_TYPE
:
16965 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
16967 cp_parser_set_decl_spec_type (decl_specs
, type
,
16969 /*type_definition_p=*/false);
16974 case RID_DIRECT_BASES
:
16975 type
= cp_parser_trait_expr (parser
, token
->keyword
);
16977 cp_parser_set_decl_spec_type (decl_specs
, type
,
16979 /*type_definition_p=*/false);
16985 /* If token is an already-parsed decltype not followed by ::,
16986 it's a simple-type-specifier. */
16987 if (token
->type
== CPP_DECLTYPE
16988 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
16990 type
= saved_checks_value (token
->u
.tree_check_value
);
16993 cp_parser_set_decl_spec_type (decl_specs
, type
,
16995 /*type_definition_p=*/false);
16996 /* Remember that we are handling a decltype in order to
16997 implement the resolution of DR 1510 when the argument
16998 isn't instantiation dependent. */
16999 decl_specs
->decltype_p
= true;
17001 cp_lexer_consume_token (parser
->lexer
);
17005 /* If the type-specifier was for a built-in type, we're done. */
17008 /* Record the type. */
17010 && (token
->keyword
!= RID_SIGNED
17011 && token
->keyword
!= RID_UNSIGNED
17012 && token
->keyword
!= RID_SHORT
17013 && token
->keyword
!= RID_LONG
))
17014 cp_parser_set_decl_spec_type (decl_specs
,
17017 /*type_definition_p=*/false);
17019 decl_specs
->any_specifiers_p
= true;
17021 /* Consume the token. */
17022 cp_lexer_consume_token (parser
->lexer
);
17024 if (type
== error_mark_node
)
17025 return error_mark_node
;
17027 /* There is no valid C++ program where a non-template type is
17028 followed by a "<". That usually indicates that the user thought
17029 that the type was a template. */
17030 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17033 return TYPE_NAME (type
);
17036 /* The type-specifier must be a user-defined type. */
17037 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17042 /* Don't gobble tokens or issue error messages if this is an
17043 optional type-specifier. */
17044 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17045 cp_parser_parse_tentatively (parser
);
17047 token
= cp_lexer_peek_token (parser
->lexer
);
17049 /* Look for the optional `::' operator. */
17051 = (cp_parser_global_scope_opt (parser
,
17052 /*current_scope_valid_p=*/false)
17054 /* Look for the nested-name specifier. */
17056 = (cp_parser_nested_name_specifier_opt (parser
,
17057 /*typename_keyword_p=*/false,
17058 /*check_dependency_p=*/true,
17060 /*is_declaration=*/false)
17062 /* If we have seen a nested-name-specifier, and the next token
17063 is `template', then we are using the template-id production. */
17065 && cp_parser_optional_template_keyword (parser
))
17067 /* Look for the template-id. */
17068 type
= cp_parser_template_id (parser
,
17069 /*template_keyword_p=*/true,
17070 /*check_dependency_p=*/true,
17072 /*is_declaration=*/false);
17073 /* If the template-id did not name a type, we are out of
17075 if (TREE_CODE (type
) != TYPE_DECL
)
17077 cp_parser_error (parser
, "expected template-id for type");
17081 /* Otherwise, look for a type-name. */
17083 type
= cp_parser_type_name (parser
);
17084 /* Keep track of all name-lookups performed in class scopes. */
17088 && TREE_CODE (type
) == TYPE_DECL
17089 && identifier_p (DECL_NAME (type
)))
17090 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17091 /* If it didn't work out, we don't have a TYPE. */
17092 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17093 && !cp_parser_parse_definitely (parser
))
17095 if (!type
&& cxx_dialect
>= cxx17
)
17097 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17098 cp_parser_parse_tentatively (parser
);
17100 cp_parser_global_scope_opt (parser
,
17101 /*current_scope_valid_p=*/false);
17102 cp_parser_nested_name_specifier_opt (parser
,
17103 /*typename_keyword_p=*/false,
17104 /*check_dependency_p=*/true,
17106 /*is_declaration=*/false);
17107 tree name
= cp_parser_identifier (parser
);
17108 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17109 && parser
->scope
!= error_mark_node
)
17111 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17113 /*is_template=*/false,
17114 /*is_namespace=*/false,
17115 /*check_dependency=*/true,
17116 /*ambiguous_decls=*/NULL
,
17118 if (tmpl
&& tmpl
!= error_mark_node
17119 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17120 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17121 type
= make_template_placeholder (tmpl
);
17124 type
= error_mark_node
;
17125 if (!cp_parser_simulate_error (parser
))
17126 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17127 NLE_TYPE
, token
->location
);
17131 type
= error_mark_node
;
17133 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17134 && !cp_parser_parse_definitely (parser
))
17137 if (type
&& decl_specs
)
17138 cp_parser_set_decl_spec_type (decl_specs
, type
,
17140 /*type_definition_p=*/false);
17143 /* If we didn't get a type-name, issue an error message. */
17144 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17146 cp_parser_error (parser
, "expected type-name");
17147 return error_mark_node
;
17150 if (type
&& type
!= error_mark_node
)
17152 /* See if TYPE is an Objective-C type, and if so, parse and
17153 accept any protocol references following it. Do this before
17154 the cp_parser_check_for_invalid_template_id() call, because
17155 Objective-C types can be followed by '<...>' which would
17156 enclose protocol names rather than template arguments, and so
17157 everything is fine. */
17158 if (c_dialect_objc () && !parser
->scope
17159 && (objc_is_id (type
) || objc_is_class_name (type
)))
17161 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17162 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17164 /* Clobber the "unqualified" type previously entered into
17165 DECL_SPECS with the new, improved protocol-qualified version. */
17167 decl_specs
->type
= qual_type
;
17172 /* There is no valid C++ program where a non-template type is
17173 followed by a "<". That usually indicates that the user
17174 thought that the type was a template. */
17175 cp_parser_check_for_invalid_template_id (parser
, type
,
17183 /* Parse a type-name.
17189 simple-template-id [in c++0x]
17206 Returns a TYPE_DECL for the type. */
17209 cp_parser_type_name (cp_parser
* parser
)
17211 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17216 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17220 /* We can't know yet whether it is a class-name or not. */
17221 cp_parser_parse_tentatively (parser
);
17222 /* Try a class-name. */
17223 type_decl
= cp_parser_class_name (parser
,
17224 typename_keyword_p
,
17225 /*template_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17228 /*class_head_p=*/false,
17229 /*is_declaration=*/false);
17230 /* If it's not a class-name, keep looking. */
17231 if (!cp_parser_parse_definitely (parser
))
17233 if (cxx_dialect
< cxx11
)
17234 /* It must be a typedef-name or an enum-name. */
17235 return cp_parser_nonclass_name (parser
);
17237 cp_parser_parse_tentatively (parser
);
17238 /* It is either a simple-template-id representing an
17239 instantiation of an alias template... */
17240 type_decl
= cp_parser_template_id (parser
,
17241 /*template_keyword_p=*/false,
17242 /*check_dependency_p=*/true,
17244 /*is_declaration=*/false);
17245 /* Note that this must be an instantiation of an alias template
17246 because [temp.names]/6 says:
17248 A template-id that names an alias template specialization
17251 Whereas [temp.names]/7 says:
17253 A simple-template-id that names a class template
17254 specialization is a class-name.
17256 With concepts, this could also be a partial-concept-id that
17257 declares a non-type template parameter. */
17258 if (type_decl
!= NULL_TREE
17259 && TREE_CODE (type_decl
) == TYPE_DECL
17260 && TYPE_DECL_ALIAS_P (type_decl
))
17261 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17262 else if (is_constrained_parameter (type_decl
))
17263 /* Don't do anything. */ ;
17265 cp_parser_simulate_error (parser
);
17267 if (!cp_parser_parse_definitely (parser
))
17268 /* ... Or a typedef-name or an enum-name. */
17269 return cp_parser_nonclass_name (parser
);
17275 /* Check if DECL and ARGS can form a constrained-type-specifier.
17276 If ARGS is non-null, we try to form a concept check of the
17277 form DECL<?, ARGS> where ? is a wildcard that matches any
17278 kind of template argument. If ARGS is NULL, then we try to
17279 form a concept check of the form DECL<?>. */
17282 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17283 tree decl
, tree args
)
17285 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17287 /* If we a constrained-type-specifier cannot be deduced. */
17288 if (parser
->prevent_constrained_type_specifiers
)
17291 /* A constrained type specifier can only be found in an
17292 overload set or as a reference to a template declaration.
17294 FIXME: This might be masking a bug. It's possible that
17295 that the deduction below is causing template specializations
17296 to be formed with the wildcard as an argument. */
17297 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17300 /* Try to build a call expression that evaluates the
17301 concept. This can fail if the overload set refers
17302 only to non-templates. */
17303 tree placeholder
= build_nt (WILDCARD_DECL
);
17304 tree check
= build_concept_check (decl
, placeholder
, args
);
17305 if (check
== error_mark_node
)
17308 /* Deduce the checked constraint and the prototype parameter.
17310 FIXME: In certain cases, failure to deduce should be a
17311 diagnosable error. */
17314 if (!deduce_constrained_parameter (check
, conc
, proto
))
17317 /* In template parameter scope, this results in a constrained
17318 parameter. Return a descriptor of that parm. */
17319 if (processing_template_parmlist
)
17320 return build_constrained_parameter (conc
, proto
, args
);
17322 /* In a parameter-declaration-clause, constrained-type
17323 specifiers result in invented template parameters. */
17324 if (parser
->auto_is_implicit_function_template_parm_p
)
17326 tree x
= build_constrained_parameter (conc
, proto
, args
);
17327 return synthesize_implicit_template_parm (parser
, x
);
17331 /* Otherwise, we're in a context where the constrained
17332 type name is deduced and the constraint applies
17333 after deduction. */
17334 return make_constrained_auto (conc
, args
);
17340 /* If DECL refers to a concept, return a TYPE_DECL representing
17341 the result of using the constrained type specifier in the
17342 current context. DECL refers to a concept if
17344 - it is an overload set containing a function concept taking a single
17347 - it is a variable concept taking a single type argument. */
17350 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17353 && (TREE_CODE (decl
) == OVERLOAD
17354 || BASELINK_P (decl
)
17355 || variable_concept_p (decl
)))
17356 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17361 /* Check if DECL and ARGS form a partial-concept-id. If so,
17362 assign ID to the resulting constrained placeholder.
17364 Returns true if the partial-concept-id designates a placeholder
17365 and false otherwise. Note that *id is set to NULL_TREE in
17369 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17371 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17374 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17386 Returns a TYPE_DECL for the type. */
17389 cp_parser_nonclass_name (cp_parser
* parser
)
17394 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17395 identifier
= cp_parser_identifier (parser
);
17396 if (identifier
== error_mark_node
)
17397 return error_mark_node
;
17399 /* Look up the type-name. */
17400 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17402 type_decl
= strip_using_decl (type_decl
);
17404 /* If we found an overload set, then it may refer to a concept-name. */
17405 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17408 if (TREE_CODE (type_decl
) != TYPE_DECL
17409 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17411 /* See if this is an Objective-C type. */
17412 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17413 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17415 type_decl
= TYPE_NAME (type
);
17418 /* Issue an error if we did not find a type-name. */
17419 if (TREE_CODE (type_decl
) != TYPE_DECL
17420 /* In Objective-C, we have the complication that class names are
17421 normally type names and start declarations (eg, the
17422 "NSObject" in "NSObject *object;"), but can be used in an
17423 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17424 is an expression. So, a classname followed by a dot is not a
17425 valid type-name. */
17426 || (objc_is_class_name (TREE_TYPE (type_decl
))
17427 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17429 if (!cp_parser_simulate_error (parser
))
17430 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17431 NLE_TYPE
, token
->location
);
17432 return error_mark_node
;
17434 /* Remember that the name was used in the definition of the
17435 current class so that we can check later to see if the
17436 meaning would have been different after the class was
17437 entirely defined. */
17438 else if (type_decl
!= error_mark_node
17440 maybe_note_name_used_in_class (identifier
, type_decl
);
17445 /* Parse an elaborated-type-specifier. Note that the grammar given
17446 here incorporates the resolution to DR68.
17448 elaborated-type-specifier:
17449 class-key :: [opt] nested-name-specifier [opt] identifier
17450 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17451 enum-key :: [opt] nested-name-specifier [opt] identifier
17452 typename :: [opt] nested-name-specifier identifier
17453 typename :: [opt] nested-name-specifier template [opt]
17458 elaborated-type-specifier:
17459 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17460 class-key attributes :: [opt] nested-name-specifier [opt]
17461 template [opt] template-id
17462 enum attributes :: [opt] nested-name-specifier [opt] identifier
17464 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17465 declared `friend'. If IS_DECLARATION is TRUE, then this
17466 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17467 something is being declared.
17469 Returns the TYPE specified. */
17472 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17474 bool is_declaration
)
17476 enum tag_types tag_type
;
17478 tree type
= NULL_TREE
;
17479 tree attributes
= NULL_TREE
;
17481 cp_token
*token
= NULL
;
17483 /* See if we're looking at the `enum' keyword. */
17484 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17486 /* Consume the `enum' token. */
17487 cp_lexer_consume_token (parser
->lexer
);
17488 /* Remember that it's an enumeration type. */
17489 tag_type
= enum_type
;
17490 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17491 enums) is used here. */
17492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17493 if (cp_parser_is_keyword (token
, RID_CLASS
)
17494 || cp_parser_is_keyword (token
, RID_STRUCT
))
17496 gcc_rich_location
richloc (token
->location
);
17497 richloc
.add_range (input_location
, false);
17498 richloc
.add_fixit_remove ();
17499 pedwarn_at_rich_loc (&richloc
, 0, "elaborated-type-specifier for "
17500 "a scoped enum must not use the %qD keyword",
17502 /* Consume the `struct' or `class' and parse it anyway. */
17503 cp_lexer_consume_token (parser
->lexer
);
17505 /* Parse the attributes. */
17506 attributes
= cp_parser_attributes_opt (parser
);
17508 /* Or, it might be `typename'. */
17509 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17512 /* Consume the `typename' token. */
17513 cp_lexer_consume_token (parser
->lexer
);
17514 /* Remember that it's a `typename' type. */
17515 tag_type
= typename_type
;
17517 /* Otherwise it must be a class-key. */
17520 tag_type
= cp_parser_class_key (parser
);
17521 if (tag_type
== none_type
)
17522 return error_mark_node
;
17523 /* Parse the attributes. */
17524 attributes
= cp_parser_attributes_opt (parser
);
17527 /* Look for the `::' operator. */
17528 globalscope
= cp_parser_global_scope_opt (parser
,
17529 /*current_scope_valid_p=*/false);
17530 /* Look for the nested-name-specifier. */
17531 tree nested_name_specifier
;
17532 if (tag_type
== typename_type
&& !globalscope
)
17534 nested_name_specifier
17535 = cp_parser_nested_name_specifier (parser
,
17536 /*typename_keyword_p=*/true,
17537 /*check_dependency_p=*/true,
17540 if (!nested_name_specifier
)
17541 return error_mark_node
;
17544 /* Even though `typename' is not present, the proposed resolution
17545 to Core Issue 180 says that in `class A<T>::B', `B' should be
17546 considered a type-name, even if `A<T>' is dependent. */
17547 nested_name_specifier
17548 = cp_parser_nested_name_specifier_opt (parser
,
17549 /*typename_keyword_p=*/true,
17550 /*check_dependency_p=*/true,
17553 /* For everything but enumeration types, consider a template-id.
17554 For an enumeration type, consider only a plain identifier. */
17555 if (tag_type
!= enum_type
)
17557 bool template_p
= false;
17560 /* Allow the `template' keyword. */
17561 template_p
= cp_parser_optional_template_keyword (parser
);
17562 /* If we didn't see `template', we don't know if there's a
17563 template-id or not. */
17565 cp_parser_parse_tentatively (parser
);
17566 /* Parse the template-id. */
17567 token
= cp_lexer_peek_token (parser
->lexer
);
17568 decl
= cp_parser_template_id (parser
, template_p
,
17569 /*check_dependency_p=*/true,
17572 /* If we didn't find a template-id, look for an ordinary
17574 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17576 /* We can get here when cp_parser_template_id, called by
17577 cp_parser_class_name with tag_type == none_type, succeeds
17578 and caches a BASELINK. Then, when called again here,
17579 instead of failing and returning an error_mark_node
17580 returns it (see template/typename17.C in C++11).
17581 ??? Could we diagnose this earlier? */
17582 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17584 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17585 type
= error_mark_node
;
17587 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17588 in effect, then we must assume that, upon instantiation, the
17589 template will correspond to a class. */
17590 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17591 && tag_type
== typename_type
)
17592 type
= make_typename_type (parser
->scope
, decl
,
17594 /*complain=*/tf_error
);
17595 /* If the `typename' keyword is in effect and DECL is not a type
17596 decl, then type is non existent. */
17597 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17599 else if (TREE_CODE (decl
) == TYPE_DECL
)
17601 type
= check_elaborated_type_specifier (tag_type
, decl
,
17602 /*allow_template_p=*/true);
17604 /* If the next token is a semicolon, this must be a specialization,
17605 instantiation, or friend declaration. Check the scope while we
17606 still know whether or not we had a nested-name-specifier. */
17607 if (type
!= error_mark_node
17608 && !nested_name_specifier
&& !is_friend
17609 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17610 check_unqualified_spec_or_inst (type
, token
->location
);
17612 else if (decl
== error_mark_node
)
17613 type
= error_mark_node
;
17618 token
= cp_lexer_peek_token (parser
->lexer
);
17619 identifier
= cp_parser_identifier (parser
);
17621 if (identifier
== error_mark_node
)
17623 parser
->scope
= NULL_TREE
;
17624 return error_mark_node
;
17627 /* For a `typename', we needn't call xref_tag. */
17628 if (tag_type
== typename_type
17629 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17630 return cp_parser_make_typename_type (parser
, identifier
,
17633 /* Template parameter lists apply only if we are not within a
17634 function parameter list. */
17635 bool template_parm_lists_apply
17636 = parser
->num_template_parameter_lists
;
17637 if (template_parm_lists_apply
)
17638 for (cp_binding_level
*s
= current_binding_level
;
17639 s
&& s
->kind
!= sk_template_parms
;
17640 s
= s
->level_chain
)
17641 if (s
->kind
== sk_function_parms
)
17642 template_parm_lists_apply
= false;
17644 /* Look up a qualified name in the usual way. */
17648 tree ambiguous_decls
;
17650 decl
= cp_parser_lookup_name (parser
, identifier
,
17652 /*is_template=*/false,
17653 /*is_namespace=*/false,
17654 /*check_dependency=*/true,
17658 /* If the lookup was ambiguous, an error will already have been
17660 if (ambiguous_decls
)
17661 return error_mark_node
;
17663 /* If we are parsing friend declaration, DECL may be a
17664 TEMPLATE_DECL tree node here. However, we need to check
17665 whether this TEMPLATE_DECL results in valid code. Consider
17666 the following example:
17669 template <class T> class C {};
17672 template <class T> friend class N::C; // #1, valid code
17674 template <class T> class Y {
17675 friend class N::C; // #2, invalid code
17678 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17679 name lookup of `N::C'. We see that friend declaration must
17680 be template for the code to be valid. Note that
17681 processing_template_decl does not work here since it is
17682 always 1 for the above two cases. */
17684 decl
= (cp_parser_maybe_treat_template_as_class
17685 (decl
, /*tag_name_p=*/is_friend
17686 && template_parm_lists_apply
));
17688 if (TREE_CODE (decl
) != TYPE_DECL
)
17690 cp_parser_diagnose_invalid_type_name (parser
,
17693 return error_mark_node
;
17696 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17698 bool allow_template
= (template_parm_lists_apply
17699 || DECL_SELF_REFERENCE_P (decl
));
17700 type
= check_elaborated_type_specifier (tag_type
, decl
,
17703 if (type
== error_mark_node
)
17704 return error_mark_node
;
17707 /* Forward declarations of nested types, such as
17712 are invalid unless all components preceding the final '::'
17713 are complete. If all enclosing types are complete, these
17714 declarations become merely pointless.
17716 Invalid forward declarations of nested types are errors
17717 caught elsewhere in parsing. Those that are pointless arrive
17720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17721 && !is_friend
&& !processing_explicit_instantiation
)
17722 warning (0, "declaration %qD does not declare anything", decl
);
17724 type
= TREE_TYPE (decl
);
17728 /* An elaborated-type-specifier sometimes introduces a new type and
17729 sometimes names an existing type. Normally, the rule is that it
17730 introduces a new type only if there is not an existing type of
17731 the same name already in scope. For example, given:
17734 void f() { struct S s; }
17736 the `struct S' in the body of `f' is the same `struct S' as in
17737 the global scope; the existing definition is used. However, if
17738 there were no global declaration, this would introduce a new
17739 local class named `S'.
17741 An exception to this rule applies to the following code:
17743 namespace N { struct S; }
17745 Here, the elaborated-type-specifier names a new type
17746 unconditionally; even if there is already an `S' in the
17747 containing scope this declaration names a new type.
17748 This exception only applies if the elaborated-type-specifier
17749 forms the complete declaration:
17753 A declaration consisting solely of `class-key identifier ;' is
17754 either a redeclaration of the name in the current scope or a
17755 forward declaration of the identifier as a class name. It
17756 introduces the name into the current scope.
17758 We are in this situation precisely when the next token is a `;'.
17760 An exception to the exception is that a `friend' declaration does
17761 *not* name a new type; i.e., given:
17763 struct S { friend struct T; };
17765 `T' is not a new type in the scope of `S'.
17767 Also, `new struct S' or `sizeof (struct S)' never results in the
17768 definition of a new type; a new type can only be declared in a
17769 declaration context. */
17775 /* Friends have special name lookup rules. */
17776 ts
= ts_within_enclosing_non_class
;
17777 else if (is_declaration
17778 && cp_lexer_next_token_is (parser
->lexer
,
17780 /* This is a `class-key identifier ;' */
17786 (template_parm_lists_apply
17787 && (cp_parser_next_token_starts_class_definition_p (parser
)
17788 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17789 /* An unqualified name was used to reference this type, so
17790 there were no qualifying templates. */
17791 if (template_parm_lists_apply
17792 && !cp_parser_check_template_parameters (parser
,
17793 /*num_templates=*/0,
17795 /*declarator=*/NULL
))
17796 return error_mark_node
;
17797 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17801 if (type
== error_mark_node
)
17802 return error_mark_node
;
17804 /* Allow attributes on forward declarations of classes. */
17807 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17808 warning (OPT_Wattributes
,
17809 "attributes ignored on uninstantiated type");
17810 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17811 && ! processing_explicit_instantiation
)
17812 warning (OPT_Wattributes
,
17813 "attributes ignored on template instantiation");
17814 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17815 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17817 warning (OPT_Wattributes
,
17818 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17821 if (tag_type
!= enum_type
)
17823 /* Indicate whether this class was declared as a `class' or as a
17825 if (CLASS_TYPE_P (type
))
17826 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17827 cp_parser_check_class_key (tag_type
, type
);
17830 /* A "<" cannot follow an elaborated type specifier. If that
17831 happens, the user was probably trying to form a template-id. */
17832 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17838 /* Parse an enum-specifier.
17841 enum-head { enumerator-list [opt] }
17842 enum-head { enumerator-list , } [C++0x]
17845 enum-key identifier [opt] enum-base [opt]
17846 enum-key nested-name-specifier identifier enum-base [opt]
17851 enum struct [C++0x]
17854 : type-specifier-seq
17856 opaque-enum-specifier:
17857 enum-key identifier enum-base [opt] ;
17860 enum-key attributes[opt] identifier [opt] enum-base [opt]
17861 { enumerator-list [opt] }attributes[opt]
17862 enum-key attributes[opt] identifier [opt] enum-base [opt]
17863 { enumerator-list, }attributes[opt] [C++0x]
17865 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17866 if the token stream isn't an enum-specifier after all. */
17869 cp_parser_enum_specifier (cp_parser
* parser
)
17872 tree type
= NULL_TREE
;
17874 tree nested_name_specifier
= NULL_TREE
;
17876 bool scoped_enum_p
= false;
17877 bool has_underlying_type
= false;
17878 bool nested_being_defined
= false;
17879 bool new_value_list
= false;
17880 bool is_new_type
= false;
17881 bool is_unnamed
= false;
17882 tree underlying_type
= NULL_TREE
;
17883 cp_token
*type_start_token
= NULL
;
17884 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17886 parser
->colon_corrects_to_scope_p
= false;
17888 /* Parse tentatively so that we can back up if we don't find a
17890 cp_parser_parse_tentatively (parser
);
17892 /* Caller guarantees that the current token is 'enum', an identifier
17893 possibly follows, and the token after that is an opening brace.
17894 If we don't have an identifier, fabricate an anonymous name for
17895 the enumeration being defined. */
17896 cp_lexer_consume_token (parser
->lexer
);
17898 /* Parse the "class" or "struct", which indicates a scoped
17899 enumeration type in C++0x. */
17900 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17901 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17903 if (cxx_dialect
< cxx11
)
17904 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17906 /* Consume the `struct' or `class' token. */
17907 cp_lexer_consume_token (parser
->lexer
);
17909 scoped_enum_p
= true;
17912 attributes
= cp_parser_attributes_opt (parser
);
17914 /* Clear the qualification. */
17915 parser
->scope
= NULL_TREE
;
17916 parser
->qualifying_scope
= NULL_TREE
;
17917 parser
->object_scope
= NULL_TREE
;
17919 /* Figure out in what scope the declaration is being placed. */
17920 prev_scope
= current_scope ();
17922 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17924 push_deferring_access_checks (dk_no_check
);
17925 nested_name_specifier
17926 = cp_parser_nested_name_specifier_opt (parser
,
17927 /*typename_keyword_p=*/true,
17928 /*check_dependency_p=*/false,
17930 /*is_declaration=*/false);
17932 if (nested_name_specifier
)
17936 identifier
= cp_parser_identifier (parser
);
17937 name
= cp_parser_lookup_name (parser
, identifier
,
17939 /*is_template=*/false,
17940 /*is_namespace=*/false,
17941 /*check_dependency=*/true,
17942 /*ambiguous_decls=*/NULL
,
17944 if (name
&& name
!= error_mark_node
)
17946 type
= TREE_TYPE (name
);
17947 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17949 /* Are template enums allowed in ISO? */
17950 if (template_parm_scope_p ())
17951 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17952 "%qD is an enumeration template", name
);
17953 /* ignore a typename reference, for it will be solved by name
17958 else if (nested_name_specifier
== error_mark_node
)
17959 /* We already issued an error. */;
17962 error_at (type_start_token
->location
,
17963 "%qD does not name an enumeration in %qT",
17964 identifier
, nested_name_specifier
);
17965 nested_name_specifier
= error_mark_node
;
17970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17971 identifier
= cp_parser_identifier (parser
);
17974 identifier
= make_anon_name ();
17977 error_at (type_start_token
->location
,
17978 "unnamed scoped enum is not allowed");
17981 pop_deferring_access_checks ();
17983 /* Check for the `:' that denotes a specified underlying type in C++0x.
17984 Note that a ':' could also indicate a bitfield width, however. */
17985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17987 cp_decl_specifier_seq type_specifiers
;
17989 /* Consume the `:'. */
17990 cp_lexer_consume_token (parser
->lexer
);
17992 /* Parse the type-specifier-seq. */
17993 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17994 /*is_trailing_return=*/false,
17997 /* At this point this is surely not elaborated type specifier. */
17998 if (!cp_parser_parse_definitely (parser
))
18001 if (cxx_dialect
< cxx11
)
18002 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18004 has_underlying_type
= true;
18006 /* If that didn't work, stop. */
18007 if (type_specifiers
.type
!= error_mark_node
)
18009 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18010 /*initialized=*/0, NULL
);
18011 if (underlying_type
== error_mark_node
18012 || check_for_bare_parameter_packs (underlying_type
))
18013 underlying_type
= NULL_TREE
;
18017 /* Look for the `{' but don't consume it yet. */
18018 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18020 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18022 cp_parser_error (parser
, "expected %<{%>");
18023 if (has_underlying_type
)
18029 /* An opaque-enum-specifier must have a ';' here. */
18030 if ((scoped_enum_p
|| underlying_type
)
18031 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18033 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18034 if (has_underlying_type
)
18042 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18045 if (nested_name_specifier
)
18047 if (CLASS_TYPE_P (nested_name_specifier
))
18049 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18050 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18051 push_scope (nested_name_specifier
);
18053 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18055 push_nested_namespace (nested_name_specifier
);
18059 /* Issue an error message if type-definitions are forbidden here. */
18060 if (!cp_parser_check_type_definition (parser
))
18061 type
= error_mark_node
;
18063 /* Create the new type. We do this before consuming the opening
18064 brace so the enum will be recorded as being on the line of its
18065 tag (or the 'enum' keyword, if there is no tag). */
18066 type
= start_enum (identifier
, type
, underlying_type
,
18067 attributes
, scoped_enum_p
, &is_new_type
);
18069 /* If the next token is not '{' it is an opaque-enum-specifier or an
18070 elaborated-type-specifier. */
18071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18073 timevar_push (TV_PARSE_ENUM
);
18074 if (nested_name_specifier
18075 && nested_name_specifier
!= error_mark_node
)
18077 /* The following catches invalid code such as:
18078 enum class S<int>::E { A, B, C }; */
18079 if (!processing_specialization
18080 && CLASS_TYPE_P (nested_name_specifier
)
18081 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18082 error_at (type_start_token
->location
, "cannot add an enumerator "
18083 "list to a template instantiation");
18085 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18087 error_at (type_start_token
->location
,
18088 "%<%T::%E%> has not been declared",
18089 TYPE_CONTEXT (nested_name_specifier
),
18090 nested_name_specifier
);
18091 type
= error_mark_node
;
18093 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18094 && !CLASS_TYPE_P (nested_name_specifier
))
18096 error_at (type_start_token
->location
, "nested name specifier "
18097 "%qT for enum declaration does not name a class "
18098 "or namespace", nested_name_specifier
);
18099 type
= error_mark_node
;
18101 /* If that scope does not contain the scope in which the
18102 class was originally declared, the program is invalid. */
18103 else if (prev_scope
&& !is_ancestor (prev_scope
,
18104 nested_name_specifier
))
18106 if (at_namespace_scope_p ())
18107 error_at (type_start_token
->location
,
18108 "declaration of %qD in namespace %qD which does not "
18110 type
, prev_scope
, nested_name_specifier
);
18112 error_at (type_start_token
->location
,
18113 "declaration of %qD in %qD which does not "
18115 type
, prev_scope
, nested_name_specifier
);
18116 type
= error_mark_node
;
18118 /* If that scope is the scope where the declaration is being placed
18119 the program is invalid. */
18120 else if (CLASS_TYPE_P (nested_name_specifier
)
18121 && CLASS_TYPE_P (prev_scope
)
18122 && same_type_p (nested_name_specifier
, prev_scope
))
18124 permerror (type_start_token
->location
,
18125 "extra qualification not allowed");
18126 nested_name_specifier
= NULL_TREE
;
18131 begin_scope (sk_scoped_enum
, type
);
18133 /* Consume the opening brace. */
18134 matching_braces braces
;
18135 braces
.consume_open (parser
);
18137 if (type
== error_mark_node
)
18138 ; /* Nothing to add */
18139 else if (OPAQUE_ENUM_P (type
)
18140 || (cxx_dialect
> cxx98
&& processing_specialization
))
18142 new_value_list
= true;
18143 SET_OPAQUE_ENUM_P (type
, false);
18144 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18148 error_at (type_start_token
->location
,
18149 "multiple definition of %q#T", type
);
18150 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18151 "previous definition here");
18152 type
= error_mark_node
;
18155 if (type
== error_mark_node
)
18156 cp_parser_skip_to_end_of_block_or_statement (parser
);
18157 /* If the next token is not '}', then there are some enumerators. */
18158 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18160 if (is_unnamed
&& !scoped_enum_p
)
18161 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18162 "ISO C++ forbids empty unnamed enum");
18165 cp_parser_enumerator_list (parser
, type
);
18167 /* Consume the final '}'. */
18168 braces
.require_close (parser
);
18172 timevar_pop (TV_PARSE_ENUM
);
18176 /* If a ';' follows, then it is an opaque-enum-specifier
18177 and additional restrictions apply. */
18178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18181 error_at (type_start_token
->location
,
18182 "opaque-enum-specifier without name");
18183 else if (nested_name_specifier
)
18184 error_at (type_start_token
->location
,
18185 "opaque-enum-specifier must use a simple identifier");
18189 /* Look for trailing attributes to apply to this enumeration, and
18190 apply them if appropriate. */
18191 if (cp_parser_allow_gnu_extensions_p (parser
))
18193 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18194 cplus_decl_attributes (&type
,
18196 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18199 /* Finish up the enumeration. */
18200 if (type
!= error_mark_node
)
18202 if (new_value_list
)
18203 finish_enum_value_list (type
);
18205 finish_enum (type
);
18208 if (nested_name_specifier
)
18210 if (CLASS_TYPE_P (nested_name_specifier
))
18212 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18213 pop_scope (nested_name_specifier
);
18215 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18217 pop_nested_namespace (nested_name_specifier
);
18221 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18225 /* Parse an enumerator-list. The enumerators all have the indicated
18229 enumerator-definition
18230 enumerator-list , enumerator-definition */
18233 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18237 /* Parse an enumerator-definition. */
18238 cp_parser_enumerator_definition (parser
, type
);
18240 /* If the next token is not a ',', we've reached the end of
18242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18244 /* Otherwise, consume the `,' and keep going. */
18245 cp_lexer_consume_token (parser
->lexer
);
18246 /* If the next token is a `}', there is a trailing comma. */
18247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18249 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18250 pedwarn (input_location
, OPT_Wpedantic
,
18251 "comma at end of enumerator list");
18257 /* Parse an enumerator-definition. The enumerator has the indicated
18260 enumerator-definition:
18262 enumerator = constant-expression
18269 enumerator-definition:
18270 enumerator attributes [opt]
18271 enumerator attributes [opt] = constant-expression */
18274 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18280 /* Save the input location because we are interested in the location
18281 of the identifier and not the location of the explicit value. */
18282 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18284 /* Look for the identifier. */
18285 identifier
= cp_parser_identifier (parser
);
18286 if (identifier
== error_mark_node
)
18289 /* Parse any specified attributes. */
18290 tree attrs
= cp_parser_attributes_opt (parser
);
18292 /* If the next token is an '=', then there is an explicit value. */
18293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18295 /* Consume the `=' token. */
18296 cp_lexer_consume_token (parser
->lexer
);
18297 /* Parse the value. */
18298 value
= cp_parser_constant_expression (parser
);
18303 /* If we are processing a template, make sure the initializer of the
18304 enumerator doesn't contain any bare template parameter pack. */
18305 if (check_for_bare_parameter_packs (value
))
18306 value
= error_mark_node
;
18308 /* Create the enumerator. */
18309 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18312 /* Parse a namespace-name.
18315 original-namespace-name
18318 Returns the NAMESPACE_DECL for the namespace. */
18321 cp_parser_namespace_name (cp_parser
* parser
)
18324 tree namespace_decl
;
18326 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18328 /* Get the name of the namespace. */
18329 identifier
= cp_parser_identifier (parser
);
18330 if (identifier
== error_mark_node
)
18331 return error_mark_node
;
18333 /* Look up the identifier in the currently active scope. Look only
18334 for namespaces, due to:
18336 [basic.lookup.udir]
18338 When looking up a namespace-name in a using-directive or alias
18339 definition, only namespace names are considered.
18343 [basic.lookup.qual]
18345 During the lookup of a name preceding the :: scope resolution
18346 operator, object, function, and enumerator names are ignored.
18348 (Note that cp_parser_qualifying_entity only calls this
18349 function if the token after the name is the scope resolution
18351 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18353 /*is_template=*/false,
18354 /*is_namespace=*/true,
18355 /*check_dependency=*/true,
18356 /*ambiguous_decls=*/NULL
,
18358 /* If it's not a namespace, issue an error. */
18359 if (namespace_decl
== error_mark_node
18360 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18362 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18363 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18364 cp_parser_error (parser
, "expected namespace-name");
18365 namespace_decl
= error_mark_node
;
18368 return namespace_decl
;
18371 /* Parse a namespace-definition.
18373 namespace-definition:
18374 named-namespace-definition
18375 unnamed-namespace-definition
18377 named-namespace-definition:
18378 original-namespace-definition
18379 extension-namespace-definition
18381 original-namespace-definition:
18382 namespace identifier { namespace-body }
18384 extension-namespace-definition:
18385 namespace original-namespace-name { namespace-body }
18387 unnamed-namespace-definition:
18388 namespace { namespace-body } */
18391 cp_parser_namespace_definition (cp_parser
* parser
)
18394 int nested_definition_count
= 0;
18396 cp_ensure_no_omp_declare_simd (parser
);
18397 cp_ensure_no_oacc_routine (parser
);
18399 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18403 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18404 cp_lexer_consume_token (parser
->lexer
);
18407 /* Look for the `namespace' keyword. */
18409 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18411 /* Parse any specified attributes before the identifier. */
18412 tree attribs
= cp_parser_attributes_opt (parser
);
18416 identifier
= NULL_TREE
;
18418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18420 identifier
= cp_parser_identifier (parser
);
18422 /* Parse any attributes specified after the identifier. */
18423 attribs
= chainon (attribs
, cp_parser_attributes_opt (parser
));
18426 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18429 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18430 pedwarn (input_location
, OPT_Wpedantic
,
18431 "nested namespace definitions only available with "
18432 "-std=c++17 or -std=gnu++17");
18434 /* Nested namespace names can create new namespaces (unlike
18435 other qualified-ids). */
18436 if (int count
= identifier
? push_namespace (identifier
) : 0)
18437 nested_definition_count
+= count
;
18439 cp_parser_error (parser
, "nested namespace name required");
18440 cp_lexer_consume_token (parser
->lexer
);
18443 if (nested_definition_count
&& !identifier
)
18444 cp_parser_error (parser
, "namespace name required");
18446 if (nested_definition_count
&& attribs
)
18447 error_at (token
->location
,
18448 "a nested namespace definition cannot have attributes");
18449 if (nested_definition_count
&& is_inline
)
18450 error_at (token
->location
,
18451 "a nested namespace definition cannot be inline");
18453 /* Start the namespace. */
18454 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18456 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18458 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18460 /* Look for the `{' to validate starting the namespace. */
18461 matching_braces braces
;
18462 if (braces
.require_open (parser
))
18464 /* Parse the body of the namespace. */
18465 cp_parser_namespace_body (parser
);
18467 /* Look for the final `}'. */
18468 braces
.require_close (parser
);
18471 if (has_visibility
)
18472 pop_visibility (1);
18474 /* Pop the nested namespace definitions. */
18475 while (nested_definition_count
--)
18479 /* Parse a namespace-body.
18482 declaration-seq [opt] */
18485 cp_parser_namespace_body (cp_parser
* parser
)
18487 cp_parser_declaration_seq_opt (parser
);
18490 /* Parse a namespace-alias-definition.
18492 namespace-alias-definition:
18493 namespace identifier = qualified-namespace-specifier ; */
18496 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18499 tree namespace_specifier
;
18501 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18503 /* Look for the `namespace' keyword. */
18504 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18505 /* Look for the identifier. */
18506 identifier
= cp_parser_identifier (parser
);
18507 if (identifier
== error_mark_node
)
18509 /* Look for the `=' token. */
18510 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18511 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18513 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18514 /* Skip the definition. */
18515 cp_lexer_consume_token (parser
->lexer
);
18516 if (cp_parser_skip_to_closing_brace (parser
))
18517 cp_lexer_consume_token (parser
->lexer
);
18520 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18521 /* Look for the qualified-namespace-specifier. */
18522 namespace_specifier
18523 = cp_parser_qualified_namespace_specifier (parser
);
18524 /* Look for the `;' token. */
18525 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18527 /* Register the alias in the symbol table. */
18528 do_namespace_alias (identifier
, namespace_specifier
);
18531 /* Parse a qualified-namespace-specifier.
18533 qualified-namespace-specifier:
18534 :: [opt] nested-name-specifier [opt] namespace-name
18536 Returns a NAMESPACE_DECL corresponding to the specified
18540 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18542 /* Look for the optional `::'. */
18543 cp_parser_global_scope_opt (parser
,
18544 /*current_scope_valid_p=*/false);
18546 /* Look for the optional nested-name-specifier. */
18547 cp_parser_nested_name_specifier_opt (parser
,
18548 /*typename_keyword_p=*/false,
18549 /*check_dependency_p=*/true,
18551 /*is_declaration=*/true);
18553 return cp_parser_namespace_name (parser
);
18556 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18557 access declaration.
18560 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18561 using :: unqualified-id ;
18563 access-declaration:
18569 cp_parser_using_declaration (cp_parser
* parser
,
18570 bool access_declaration_p
)
18573 bool typename_p
= false;
18574 bool global_scope_p
;
18578 int oldcount
= errorcount
;
18579 cp_token
*diag_token
= NULL
;
18581 if (access_declaration_p
)
18583 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18584 cp_parser_parse_tentatively (parser
);
18588 /* Look for the `using' keyword. */
18589 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18592 /* Peek at the next token. */
18593 token
= cp_lexer_peek_token (parser
->lexer
);
18594 /* See if it's `typename'. */
18595 if (token
->keyword
== RID_TYPENAME
)
18597 /* Remember that we've seen it. */
18599 /* Consume the `typename' token. */
18600 cp_lexer_consume_token (parser
->lexer
);
18604 /* Look for the optional global scope qualification. */
18606 = (cp_parser_global_scope_opt (parser
,
18607 /*current_scope_valid_p=*/false)
18610 /* If we saw `typename', or didn't see `::', then there must be a
18611 nested-name-specifier present. */
18612 if (typename_p
|| !global_scope_p
)
18614 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18615 /*check_dependency_p=*/true,
18617 /*is_declaration=*/true);
18618 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18620 cp_parser_skip_to_end_of_block_or_statement (parser
);
18624 /* Otherwise, we could be in either of the two productions. In that
18625 case, treat the nested-name-specifier as optional. */
18627 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18628 /*typename_keyword_p=*/false,
18629 /*check_dependency_p=*/true,
18631 /*is_declaration=*/true);
18633 qscope
= global_namespace
;
18634 else if (UNSCOPED_ENUM_P (qscope
))
18635 qscope
= CP_TYPE_CONTEXT (qscope
);
18637 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18638 /* Something has already gone wrong; there's no need to parse
18639 further. Since an error has occurred, the return value of
18640 cp_parser_parse_definitely will be false, as required. */
18641 return cp_parser_parse_definitely (parser
);
18643 token
= cp_lexer_peek_token (parser
->lexer
);
18644 /* Parse the unqualified-id. */
18645 identifier
= cp_parser_unqualified_id (parser
,
18646 /*template_keyword_p=*/false,
18647 /*check_dependency_p=*/true,
18648 /*declarator_p=*/true,
18649 /*optional_p=*/false);
18651 if (access_declaration_p
)
18653 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18654 cp_parser_simulate_error (parser
);
18655 if (!cp_parser_parse_definitely (parser
))
18658 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18660 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18661 if (cxx_dialect
< cxx17
18662 && !in_system_header_at (ell
->location
))
18663 pedwarn (ell
->location
, 0,
18664 "pack expansion in using-declaration only available "
18665 "with -std=c++17 or -std=gnu++17");
18666 qscope
= make_pack_expansion (qscope
);
18669 /* The function we call to handle a using-declaration is different
18670 depending on what scope we are in. */
18671 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18673 else if (!identifier_p (identifier
)
18674 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18675 /* [namespace.udecl]
18677 A using declaration shall not name a template-id. */
18678 error_at (token
->location
,
18679 "a template-id may not appear in a using-declaration");
18682 if (at_class_scope_p ())
18684 /* Create the USING_DECL. */
18685 decl
= do_class_using_decl (qscope
, identifier
);
18687 if (decl
&& typename_p
)
18688 USING_DECL_TYPENAME_P (decl
) = 1;
18690 if (check_for_bare_parameter_packs (decl
))
18692 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18696 /* Add it to the list of members in this class. */
18697 finish_member_declaration (decl
);
18701 decl
= cp_parser_lookup_name_simple (parser
,
18704 if (decl
== error_mark_node
)
18705 cp_parser_name_lookup_error (parser
, identifier
,
18708 else if (check_for_bare_parameter_packs (decl
))
18710 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18713 else if (!at_namespace_scope_p ())
18714 finish_local_using_decl (decl
, qscope
, identifier
);
18716 finish_namespace_using_decl (decl
, qscope
, identifier
);
18720 if (!access_declaration_p
18721 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18723 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18724 if (cxx_dialect
< cxx17
)
18725 pedwarn (comma
->location
, 0,
18726 "comma-separated list in using-declaration only available "
18727 "with -std=c++17 or -std=gnu++17");
18731 /* Look for the final `;'. */
18732 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18734 if (access_declaration_p
&& errorcount
== oldcount
)
18735 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18736 "access declarations are deprecated "
18737 "in favour of using-declarations; "
18738 "suggestion: add the %<using%> keyword");
18743 /* Parse an alias-declaration.
18746 using identifier attribute-specifier-seq [opt] = type-id */
18749 cp_parser_alias_declaration (cp_parser
* parser
)
18751 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18752 location_t id_location
;
18753 cp_declarator
*declarator
;
18754 cp_decl_specifier_seq decl_specs
;
18756 const char *saved_message
= NULL
;
18758 /* Look for the `using' keyword. */
18759 cp_token
*using_token
18760 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18761 if (using_token
== NULL
)
18762 return error_mark_node
;
18764 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18765 id
= cp_parser_identifier (parser
);
18766 if (id
== error_mark_node
)
18767 return error_mark_node
;
18769 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18770 attributes
= cp_parser_attributes_opt (parser
);
18771 if (attributes
== error_mark_node
)
18772 return error_mark_node
;
18774 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18776 if (cp_parser_error_occurred (parser
))
18777 return error_mark_node
;
18779 cp_parser_commit_to_tentative_parse (parser
);
18781 /* Now we are going to parse the type-id of the declaration. */
18786 "A type-specifier-seq shall not define a class or enumeration
18787 unless it appears in the type-id of an alias-declaration (7.1.3) that
18788 is not the declaration of a template-declaration."
18790 In other words, if we currently are in an alias template, the
18791 type-id should not define a type.
18793 So let's set parser->type_definition_forbidden_message in that
18794 case; cp_parser_check_type_definition (called by
18795 cp_parser_class_specifier) will then emit an error if a type is
18796 defined in the type-id. */
18797 if (parser
->num_template_parameter_lists
)
18799 saved_message
= parser
->type_definition_forbidden_message
;
18800 parser
->type_definition_forbidden_message
=
18801 G_("types may not be defined in alias template declarations");
18804 type
= cp_parser_type_id (parser
);
18806 /* Restore the error message if need be. */
18807 if (parser
->num_template_parameter_lists
)
18808 parser
->type_definition_forbidden_message
= saved_message
;
18810 if (type
== error_mark_node
18811 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18813 cp_parser_skip_to_end_of_block_or_statement (parser
);
18814 return error_mark_node
;
18817 /* A typedef-name can also be introduced by an alias-declaration. The
18818 identifier following the using keyword becomes a typedef-name. It has
18819 the same semantics as if it were introduced by the typedef
18820 specifier. In particular, it does not define a new type and it shall
18821 not appear in the type-id. */
18823 clear_decl_specs (&decl_specs
);
18824 decl_specs
.type
= type
;
18825 if (attributes
!= NULL_TREE
)
18827 decl_specs
.attributes
= attributes
;
18828 set_and_check_decl_spec_loc (&decl_specs
,
18832 set_and_check_decl_spec_loc (&decl_specs
,
18835 set_and_check_decl_spec_loc (&decl_specs
,
18839 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18840 declarator
->id_loc
= id_location
;
18842 member_p
= at_class_scope_p ();
18844 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18845 NULL_TREE
, attributes
);
18847 decl
= start_decl (declarator
, &decl_specs
, 0,
18848 attributes
, NULL_TREE
, &pushed_scope
);
18849 if (decl
== error_mark_node
)
18852 // Attach constraints to the alias declaration.
18853 if (flag_concepts
&& current_template_parms
)
18855 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18856 tree constr
= build_constraints (reqs
, NULL_TREE
);
18857 set_constraints (decl
, constr
);
18860 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18863 pop_scope (pushed_scope
);
18865 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18866 added into the symbol table; otherwise, return the TYPE_DECL. */
18867 if (DECL_LANG_SPECIFIC (decl
)
18868 && DECL_TEMPLATE_INFO (decl
)
18869 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18871 decl
= DECL_TI_TEMPLATE (decl
);
18873 check_member_template (decl
);
18879 /* Parse a using-directive.
18882 using namespace :: [opt] nested-name-specifier [opt]
18883 namespace-name ; */
18886 cp_parser_using_directive (cp_parser
* parser
)
18888 tree namespace_decl
;
18891 /* Look for the `using' keyword. */
18892 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18893 /* And the `namespace' keyword. */
18894 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18895 /* Look for the optional `::' operator. */
18896 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18897 /* And the optional nested-name-specifier. */
18898 cp_parser_nested_name_specifier_opt (parser
,
18899 /*typename_keyword_p=*/false,
18900 /*check_dependency_p=*/true,
18902 /*is_declaration=*/true);
18903 /* Get the namespace being used. */
18904 namespace_decl
= cp_parser_namespace_name (parser
);
18905 /* And any specified attributes. */
18906 attribs
= cp_parser_attributes_opt (parser
);
18908 /* Update the symbol table. */
18909 if (namespace_bindings_p ())
18910 finish_namespace_using_directive (namespace_decl
, attribs
);
18912 finish_local_using_directive (namespace_decl
, attribs
);
18914 /* Look for the final `;'. */
18915 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18918 /* Parse an asm-definition.
18921 asm ( string-literal ) ;
18926 asm volatile [opt] ( string-literal ) ;
18927 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18928 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18929 : asm-operand-list [opt] ) ;
18930 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18931 : asm-operand-list [opt]
18932 : asm-clobber-list [opt] ) ;
18933 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18934 : asm-clobber-list [opt]
18935 : asm-goto-list ) ; */
18938 cp_parser_asm_definition (cp_parser
* parser
)
18941 tree outputs
= NULL_TREE
;
18942 tree inputs
= NULL_TREE
;
18943 tree clobbers
= NULL_TREE
;
18944 tree labels
= NULL_TREE
;
18946 bool volatile_p
= false;
18947 bool extended_p
= false;
18948 bool invalid_inputs_p
= false;
18949 bool invalid_outputs_p
= false;
18950 bool goto_p
= false;
18951 required_token missing
= RT_NONE
;
18953 /* Look for the `asm' keyword. */
18954 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
18956 if (parser
->in_function_body
18957 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
18959 error ("%<asm%> in %<constexpr%> function");
18960 cp_function_chain
->invalid_constexpr
= true;
18963 /* See if the next token is `volatile'. */
18964 if (cp_parser_allow_gnu_extensions_p (parser
)
18965 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
18967 /* Remember that we saw the `volatile' keyword. */
18969 /* Consume the token. */
18970 cp_lexer_consume_token (parser
->lexer
);
18972 if (cp_parser_allow_gnu_extensions_p (parser
)
18973 && parser
->in_function_body
18974 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
18976 /* Remember that we saw the `goto' keyword. */
18978 /* Consume the token. */
18979 cp_lexer_consume_token (parser
->lexer
);
18981 /* Look for the opening `('. */
18982 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
18984 /* Look for the string. */
18985 string
= cp_parser_string_literal (parser
, false, false);
18986 if (string
== error_mark_node
)
18988 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18989 /*consume_paren=*/true);
18993 /* If we're allowing GNU extensions, check for the extended assembly
18994 syntax. Unfortunately, the `:' tokens need not be separated by
18995 a space in C, and so, for compatibility, we tolerate that here
18996 too. Doing that means that we have to treat the `::' operator as
18998 if (cp_parser_allow_gnu_extensions_p (parser
)
18999 && parser
->in_function_body
19000 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19001 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19003 bool inputs_p
= false;
19004 bool clobbers_p
= false;
19005 bool labels_p
= false;
19007 /* The extended syntax was used. */
19010 /* Look for outputs. */
19011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19013 /* Consume the `:'. */
19014 cp_lexer_consume_token (parser
->lexer
);
19015 /* Parse the output-operands. */
19016 if (cp_lexer_next_token_is_not (parser
->lexer
,
19018 && cp_lexer_next_token_is_not (parser
->lexer
,
19020 && cp_lexer_next_token_is_not (parser
->lexer
,
19024 outputs
= cp_parser_asm_operand_list (parser
);
19025 if (outputs
== error_mark_node
)
19026 invalid_outputs_p
= true;
19029 /* If the next token is `::', there are no outputs, and the
19030 next token is the beginning of the inputs. */
19031 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19032 /* The inputs are coming next. */
19035 /* Look for inputs. */
19037 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19039 /* Consume the `:' or `::'. */
19040 cp_lexer_consume_token (parser
->lexer
);
19041 /* Parse the output-operands. */
19042 if (cp_lexer_next_token_is_not (parser
->lexer
,
19044 && cp_lexer_next_token_is_not (parser
->lexer
,
19046 && cp_lexer_next_token_is_not (parser
->lexer
,
19049 inputs
= cp_parser_asm_operand_list (parser
);
19050 if (inputs
== error_mark_node
)
19051 invalid_inputs_p
= true;
19054 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19055 /* The clobbers are coming next. */
19058 /* Look for clobbers. */
19060 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19063 /* Consume the `:' or `::'. */
19064 cp_lexer_consume_token (parser
->lexer
);
19065 /* Parse the clobbers. */
19066 if (cp_lexer_next_token_is_not (parser
->lexer
,
19068 && cp_lexer_next_token_is_not (parser
->lexer
,
19070 clobbers
= cp_parser_asm_clobber_list (parser
);
19073 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19074 /* The labels are coming next. */
19077 /* Look for labels. */
19079 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19082 /* Consume the `:' or `::'. */
19083 cp_lexer_consume_token (parser
->lexer
);
19084 /* Parse the labels. */
19085 labels
= cp_parser_asm_label_list (parser
);
19088 if (goto_p
&& !labels_p
)
19089 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19092 missing
= RT_COLON_SCOPE
;
19094 /* Look for the closing `)'. */
19095 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19096 missing
? missing
: RT_CLOSE_PAREN
))
19097 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19098 /*consume_paren=*/true);
19099 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19101 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19103 /* Create the ASM_EXPR. */
19104 if (parser
->in_function_body
)
19106 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19107 inputs
, clobbers
, labels
);
19108 /* If the extended syntax was not used, mark the ASM_EXPR. */
19111 tree temp
= asm_stmt
;
19112 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19113 temp
= TREE_OPERAND (temp
, 0);
19115 ASM_INPUT_P (temp
) = 1;
19119 symtab
->finalize_toplevel_asm (string
);
19123 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19124 type that comes from the decl-specifier-seq. */
19127 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19129 for (cp_declarator
*d
= declarator
; d
;)
19139 if (TYPE_PTRMEMFUNC_P (type
))
19140 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19141 type
= TREE_TYPE (type
);
19149 /* Declarators [gram.dcl.decl] */
19151 /* Parse an init-declarator.
19154 declarator initializer [opt]
19159 declarator asm-specification [opt] attributes [opt] initializer [opt]
19161 function-definition:
19162 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19164 decl-specifier-seq [opt] declarator function-try-block
19168 function-definition:
19169 __extension__ function-definition
19173 function-definition:
19174 decl-specifier-seq [opt] declarator function-transaction-block
19176 The DECL_SPECIFIERS apply to this declarator. Returns a
19177 representation of the entity declared. If MEMBER_P is TRUE, then
19178 this declarator appears in a class scope. The new DECL created by
19179 this declarator is returned.
19181 The CHECKS are access checks that should be performed once we know
19182 what entity is being declared (and, therefore, what classes have
19185 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19186 for a function-definition here as well. If the declarator is a
19187 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19188 be TRUE upon return. By that point, the function-definition will
19189 have been completely parsed.
19191 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19194 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19195 parsed declaration if it is an uninitialized single declarator not followed
19196 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19197 if present, will not be consumed. If returned, this declarator will be
19198 created with SD_INITIALIZED but will not call cp_finish_decl.
19200 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19201 and there is an initializer, the pointed location_t is set to the
19202 location of the '=' or `(', or '{' in C++11 token introducing the
19206 cp_parser_init_declarator (cp_parser
* parser
,
19207 cp_decl_specifier_seq
*decl_specifiers
,
19208 vec
<deferred_access_check
, va_gc
> *checks
,
19209 bool function_definition_allowed_p
,
19211 int declares_class_or_enum
,
19212 bool* function_definition_p
,
19213 tree
* maybe_range_for_decl
,
19214 location_t
* init_loc
,
19217 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19218 *attributes_start_token
= NULL
;
19219 cp_declarator
*declarator
;
19220 tree prefix_attributes
;
19221 tree attributes
= NULL
;
19222 tree asm_specification
;
19224 tree decl
= NULL_TREE
;
19226 int is_initialized
;
19227 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19228 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19230 enum cpp_ttype initialization_kind
;
19231 bool is_direct_init
= false;
19232 bool is_non_constant_init
;
19233 int ctor_dtor_or_conv_p
;
19234 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19235 tree pushed_scope
= NULL_TREE
;
19236 bool range_for_decl_p
= false;
19237 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19238 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19240 /* Gather the attributes that were provided with the
19241 decl-specifiers. */
19242 prefix_attributes
= decl_specifiers
->attributes
;
19244 /* Assume that this is not the declarator for a function
19246 if (function_definition_p
)
19247 *function_definition_p
= false;
19249 /* Default arguments are only permitted for function parameters. */
19250 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19251 parser
->default_arg_ok_p
= false;
19253 /* Defer access checks while parsing the declarator; we cannot know
19254 what names are accessible until we know what is being
19256 resume_deferring_access_checks ();
19258 token
= cp_lexer_peek_token (parser
->lexer
);
19260 /* Parse the declarator. */
19262 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19263 &ctor_dtor_or_conv_p
,
19264 /*parenthesized_p=*/NULL
,
19265 member_p
, friend_p
);
19266 /* Gather up the deferred checks. */
19267 stop_deferring_access_checks ();
19269 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19271 /* If the DECLARATOR was erroneous, there's no need to go
19273 if (declarator
== cp_error_declarator
)
19274 return error_mark_node
;
19276 /* Check that the number of template-parameter-lists is OK. */
19277 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19279 return error_mark_node
;
19281 if (declares_class_or_enum
& 2)
19282 cp_parser_check_for_definition_in_return_type (declarator
,
19283 decl_specifiers
->type
,
19284 decl_specifiers
->locations
[ds_type_spec
]);
19286 /* Figure out what scope the entity declared by the DECLARATOR is
19287 located in. `grokdeclarator' sometimes changes the scope, so
19288 we compute it now. */
19289 scope
= get_scope_of_declarator (declarator
);
19291 /* Perform any lookups in the declared type which were thought to be
19292 dependent, but are not in the scope of the declarator. */
19293 decl_specifiers
->type
19294 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19296 /* If we're allowing GNU extensions, look for an
19297 asm-specification. */
19298 if (cp_parser_allow_gnu_extensions_p (parser
))
19300 /* Look for an asm-specification. */
19301 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19302 asm_specification
= cp_parser_asm_specification_opt (parser
);
19305 asm_specification
= NULL_TREE
;
19307 /* Look for attributes. */
19308 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19309 attributes
= cp_parser_attributes_opt (parser
);
19311 /* Peek at the next token. */
19312 token
= cp_lexer_peek_token (parser
->lexer
);
19314 bool bogus_implicit_tmpl
= false;
19316 if (function_declarator_p (declarator
))
19318 /* Handle C++17 deduction guides. */
19319 if (!decl_specifiers
->type
19320 && ctor_dtor_or_conv_p
<= 0
19321 && cxx_dialect
>= cxx17
)
19323 cp_declarator
*id
= get_id_declarator (declarator
);
19324 tree name
= id
->u
.id
.unqualified_name
;
19325 parser
->scope
= id
->u
.id
.qualifying_scope
;
19326 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19328 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19329 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19331 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19332 id
->u
.id
.sfk
= sfk_deduction_guide
;
19333 ctor_dtor_or_conv_p
= 1;
19337 /* Check to see if the token indicates the start of a
19338 function-definition. */
19339 if (cp_parser_token_starts_function_definition_p (token
))
19341 if (!function_definition_allowed_p
)
19343 /* If a function-definition should not appear here, issue an
19345 cp_parser_error (parser
,
19346 "a function-definition is not allowed here");
19347 return error_mark_node
;
19350 location_t func_brace_location
19351 = cp_lexer_peek_token (parser
->lexer
)->location
;
19353 /* Neither attributes nor an asm-specification are allowed
19354 on a function-definition. */
19355 if (asm_specification
)
19356 error_at (asm_spec_start_token
->location
,
19357 "an asm-specification is not allowed "
19358 "on a function-definition");
19360 error_at (attributes_start_token
->location
,
19361 "attributes are not allowed "
19362 "on a function-definition");
19363 /* This is a function-definition. */
19364 *function_definition_p
= true;
19366 /* Parse the function definition. */
19368 decl
= cp_parser_save_member_function_body (parser
,
19371 prefix_attributes
);
19374 (cp_parser_function_definition_from_specifiers_and_declarator
19375 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19377 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19379 /* This is where the prologue starts... */
19380 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19381 = func_brace_location
;
19387 else if (parser
->fully_implicit_function_template_p
)
19389 /* A non-template declaration involving a function parameter list
19390 containing an implicit template parameter will be made into a
19391 template. If the resulting declaration is not going to be an
19392 actual function then finish the template scope here to prevent it.
19393 An error message will be issued once we have a decl to talk about.
19395 FIXME probably we should do type deduction rather than create an
19396 implicit template, but the standard currently doesn't allow it. */
19397 bogus_implicit_tmpl
= true;
19398 finish_fully_implicit_template (parser
, NULL_TREE
);
19403 Only in function declarations for constructors, destructors, type
19404 conversions, and deduction guides can the decl-specifier-seq be omitted.
19406 We explicitly postpone this check past the point where we handle
19407 function-definitions because we tolerate function-definitions
19408 that are missing their return types in some modes. */
19409 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19411 cp_parser_error (parser
,
19412 "expected constructor, destructor, or type conversion");
19413 return error_mark_node
;
19416 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19417 if (token
->type
== CPP_EQ
19418 || token
->type
== CPP_OPEN_PAREN
19419 || token
->type
== CPP_OPEN_BRACE
)
19421 is_initialized
= SD_INITIALIZED
;
19422 initialization_kind
= token
->type
;
19423 if (maybe_range_for_decl
)
19424 *maybe_range_for_decl
= error_mark_node
;
19425 tmp_init_loc
= token
->location
;
19426 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19427 *init_loc
= tmp_init_loc
;
19429 if (token
->type
== CPP_EQ
19430 && function_declarator_p (declarator
))
19432 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19433 if (t2
->keyword
== RID_DEFAULT
)
19434 is_initialized
= SD_DEFAULTED
;
19435 else if (t2
->keyword
== RID_DELETE
)
19436 is_initialized
= SD_DELETED
;
19441 /* If the init-declarator isn't initialized and isn't followed by a
19442 `,' or `;', it's not a valid init-declarator. */
19443 if (token
->type
!= CPP_COMMA
19444 && token
->type
!= CPP_SEMICOLON
)
19446 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19447 range_for_decl_p
= true;
19450 if (!maybe_range_for_decl
)
19451 cp_parser_error (parser
, "expected initializer");
19452 return error_mark_node
;
19455 is_initialized
= SD_UNINITIALIZED
;
19456 initialization_kind
= CPP_EOF
;
19459 /* Because start_decl has side-effects, we should only call it if we
19460 know we're going ahead. By this point, we know that we cannot
19461 possibly be looking at any other construct. */
19462 cp_parser_commit_to_tentative_parse (parser
);
19464 /* Enter the newly declared entry in the symbol table. If we're
19465 processing a declaration in a class-specifier, we wait until
19466 after processing the initializer. */
19469 if (parser
->in_unbraced_linkage_specification_p
)
19470 decl_specifiers
->storage_class
= sc_extern
;
19471 decl
= start_decl (declarator
, decl_specifiers
,
19472 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19473 attributes
, prefix_attributes
, &pushed_scope
);
19474 cp_finalize_omp_declare_simd (parser
, decl
);
19475 cp_finalize_oacc_routine (parser
, decl
, false);
19476 /* Adjust location of decl if declarator->id_loc is more appropriate:
19477 set, and decl wasn't merged with another decl, in which case its
19478 location would be different from input_location, and more accurate. */
19480 && declarator
->id_loc
!= UNKNOWN_LOCATION
19481 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19482 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19485 /* Enter the SCOPE. That way unqualified names appearing in the
19486 initializer will be looked up in SCOPE. */
19487 pushed_scope
= push_scope (scope
);
19489 /* Perform deferred access control checks, now that we know in which
19490 SCOPE the declared entity resides. */
19491 if (!member_p
&& decl
)
19493 tree saved_current_function_decl
= NULL_TREE
;
19495 /* If the entity being declared is a function, pretend that we
19496 are in its scope. If it is a `friend', it may have access to
19497 things that would not otherwise be accessible. */
19498 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19500 saved_current_function_decl
= current_function_decl
;
19501 current_function_decl
= decl
;
19504 /* Perform access checks for template parameters. */
19505 cp_parser_perform_template_parameter_access_checks (checks
);
19507 /* Perform the access control checks for the declarator and the
19508 decl-specifiers. */
19509 perform_deferred_access_checks (tf_warning_or_error
);
19511 /* Restore the saved value. */
19512 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19513 current_function_decl
= saved_current_function_decl
;
19516 /* Parse the initializer. */
19517 initializer
= NULL_TREE
;
19518 is_direct_init
= false;
19519 is_non_constant_init
= true;
19520 if (is_initialized
)
19522 if (function_declarator_p (declarator
))
19524 if (initialization_kind
== CPP_EQ
)
19525 initializer
= cp_parser_pure_specifier (parser
);
19528 /* If the declaration was erroneous, we don't really
19529 know what the user intended, so just silently
19530 consume the initializer. */
19531 if (decl
!= error_mark_node
)
19532 error_at (tmp_init_loc
, "initializer provided for function");
19533 cp_parser_skip_to_closing_parenthesis (parser
,
19534 /*recovering=*/true,
19535 /*or_comma=*/false,
19536 /*consume_paren=*/true);
19541 /* We want to record the extra mangling scope for in-class
19542 initializers of class members and initializers of static data
19543 member templates. The former involves deferring
19544 parsing of the initializer until end of class as with default
19545 arguments. So right here we only handle the latter. */
19546 if (!member_p
&& processing_template_decl
)
19547 start_lambda_scope (decl
);
19548 initializer
= cp_parser_initializer (parser
,
19550 &is_non_constant_init
);
19551 if (!member_p
&& processing_template_decl
)
19552 finish_lambda_scope ();
19553 if (initializer
== error_mark_node
)
19554 cp_parser_skip_to_end_of_statement (parser
);
19558 /* The old parser allows attributes to appear after a parenthesized
19559 initializer. Mark Mitchell proposed removing this functionality
19560 on the GCC mailing lists on 2002-08-13. This parser accepts the
19561 attributes -- but ignores them. */
19562 if (cp_parser_allow_gnu_extensions_p (parser
)
19563 && initialization_kind
== CPP_OPEN_PAREN
)
19564 if (cp_parser_attributes_opt (parser
))
19565 warning (OPT_Wattributes
,
19566 "attributes after parenthesized initializer ignored");
19568 /* And now complain about a non-function implicit template. */
19569 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19570 error_at (DECL_SOURCE_LOCATION (decl
),
19571 "non-function %qD declared as implicit template", decl
);
19573 /* For an in-class declaration, use `grokfield' to create the
19579 pop_scope (pushed_scope
);
19580 pushed_scope
= NULL_TREE
;
19582 decl
= grokfield (declarator
, decl_specifiers
,
19583 initializer
, !is_non_constant_init
,
19584 /*asmspec=*/NULL_TREE
,
19585 chainon (attributes
, prefix_attributes
));
19586 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19587 cp_parser_save_default_args (parser
, decl
);
19588 cp_finalize_omp_declare_simd (parser
, decl
);
19589 cp_finalize_oacc_routine (parser
, decl
, false);
19592 /* Finish processing the declaration. But, skip member
19594 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19596 cp_finish_decl (decl
,
19597 initializer
, !is_non_constant_init
,
19599 /* If the initializer is in parentheses, then this is
19600 a direct-initialization, which means that an
19601 `explicit' constructor is OK. Otherwise, an
19602 `explicit' constructor cannot be used. */
19603 ((is_direct_init
|| !is_initialized
)
19604 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19606 else if ((cxx_dialect
!= cxx98
) && friend_p
19607 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19608 /* Core issue #226 (C++0x only): A default template-argument
19609 shall not be specified in a friend class template
19611 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19612 /*is_partial=*/false, /*is_friend_decl=*/1);
19614 if (!friend_p
&& pushed_scope
)
19615 pop_scope (pushed_scope
);
19617 if (function_declarator_p (declarator
)
19618 && parser
->fully_implicit_function_template_p
)
19621 decl
= finish_fully_implicit_template (parser
, decl
);
19623 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19626 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19627 && type_uses_auto (decl_specifiers
->type
))
19628 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19633 /* Parse a declarator.
19637 ptr-operator declarator
19639 abstract-declarator:
19640 ptr-operator abstract-declarator [opt]
19641 direct-abstract-declarator
19646 attributes [opt] direct-declarator
19647 attributes [opt] ptr-operator declarator
19649 abstract-declarator:
19650 attributes [opt] ptr-operator abstract-declarator [opt]
19651 attributes [opt] direct-abstract-declarator
19653 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19654 detect constructors, destructors, deduction guides, or conversion operators.
19655 It is set to -1 if the declarator is a name, and +1 if it is a
19656 function. Otherwise it is set to zero. Usually you just want to
19657 test for >0, but internally the negative value is used.
19659 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19660 a decl-specifier-seq unless it declares a constructor, destructor,
19661 or conversion. It might seem that we could check this condition in
19662 semantic analysis, rather than parsing, but that makes it difficult
19663 to handle something like `f()'. We want to notice that there are
19664 no decl-specifiers, and therefore realize that this is an
19665 expression, not a declaration.)
19667 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19668 the declarator is a direct-declarator of the form "(...)".
19670 MEMBER_P is true iff this declarator is a member-declarator.
19672 FRIEND_P is true iff this declarator is a friend. */
19674 static cp_declarator
*
19675 cp_parser_declarator (cp_parser
* parser
,
19676 cp_parser_declarator_kind dcl_kind
,
19677 int* ctor_dtor_or_conv_p
,
19678 bool* parenthesized_p
,
19679 bool member_p
, bool friend_p
)
19681 cp_declarator
*declarator
;
19682 enum tree_code code
;
19683 cp_cv_quals cv_quals
;
19685 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19687 /* Assume this is not a constructor, destructor, or type-conversion
19689 if (ctor_dtor_or_conv_p
)
19690 *ctor_dtor_or_conv_p
= 0;
19692 if (cp_parser_allow_gnu_extensions_p (parser
))
19693 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19695 /* Check for the ptr-operator production. */
19696 cp_parser_parse_tentatively (parser
);
19697 /* Parse the ptr-operator. */
19698 code
= cp_parser_ptr_operator (parser
,
19703 /* If that worked, then we have a ptr-operator. */
19704 if (cp_parser_parse_definitely (parser
))
19706 /* If a ptr-operator was found, then this declarator was not
19708 if (parenthesized_p
)
19709 *parenthesized_p
= true;
19710 /* The dependent declarator is optional if we are parsing an
19711 abstract-declarator. */
19712 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19713 cp_parser_parse_tentatively (parser
);
19715 /* Parse the dependent declarator. */
19716 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19717 /*ctor_dtor_or_conv_p=*/NULL
,
19718 /*parenthesized_p=*/NULL
,
19719 /*member_p=*/false,
19722 /* If we are parsing an abstract-declarator, we must handle the
19723 case where the dependent declarator is absent. */
19724 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19725 && !cp_parser_parse_definitely (parser
))
19728 declarator
= cp_parser_make_indirect_declarator
19729 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19731 /* Everything else is a direct-declarator. */
19734 if (parenthesized_p
)
19735 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19737 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19738 ctor_dtor_or_conv_p
,
19739 member_p
, friend_p
);
19742 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19743 declarator
->attributes
= gnu_attributes
;
19747 /* Parse a direct-declarator or direct-abstract-declarator.
19751 direct-declarator ( parameter-declaration-clause )
19752 cv-qualifier-seq [opt]
19753 ref-qualifier [opt]
19754 exception-specification [opt]
19755 direct-declarator [ constant-expression [opt] ]
19758 direct-abstract-declarator:
19759 direct-abstract-declarator [opt]
19760 ( parameter-declaration-clause )
19761 cv-qualifier-seq [opt]
19762 ref-qualifier [opt]
19763 exception-specification [opt]
19764 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19765 ( abstract-declarator )
19767 Returns a representation of the declarator. DCL_KIND is
19768 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19769 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19770 we are parsing a direct-declarator. It is
19771 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19772 of ambiguity we prefer an abstract declarator, as per
19773 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19774 as for cp_parser_declarator. */
19776 static cp_declarator
*
19777 cp_parser_direct_declarator (cp_parser
* parser
,
19778 cp_parser_declarator_kind dcl_kind
,
19779 int* ctor_dtor_or_conv_p
,
19780 bool member_p
, bool friend_p
)
19783 cp_declarator
*declarator
= NULL
;
19784 tree scope
= NULL_TREE
;
19785 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19786 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19788 tree pushed_scope
= NULL_TREE
;
19792 /* Peek at the next token. */
19793 token
= cp_lexer_peek_token (parser
->lexer
);
19794 if (token
->type
== CPP_OPEN_PAREN
)
19796 /* This is either a parameter-declaration-clause, or a
19797 parenthesized declarator. When we know we are parsing a
19798 named declarator, it must be a parenthesized declarator
19799 if FIRST is true. For instance, `(int)' is a
19800 parameter-declaration-clause, with an omitted
19801 direct-abstract-declarator. But `((*))', is a
19802 parenthesized abstract declarator. Finally, when T is a
19803 template parameter `(T)' is a
19804 parameter-declaration-clause, and not a parenthesized
19807 We first try and parse a parameter-declaration-clause,
19808 and then try a nested declarator (if FIRST is true).
19810 It is not an error for it not to be a
19811 parameter-declaration-clause, even when FIRST is
19817 The first is the declaration of a function while the
19818 second is the definition of a variable, including its
19821 Having seen only the parenthesis, we cannot know which of
19822 these two alternatives should be selected. Even more
19823 complex are examples like:
19828 The former is a function-declaration; the latter is a
19829 variable initialization.
19831 Thus again, we try a parameter-declaration-clause, and if
19832 that fails, we back out and return. */
19834 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19837 bool is_declarator
= false;
19839 /* In a member-declarator, the only valid interpretation
19840 of a parenthesis is the start of a
19841 parameter-declaration-clause. (It is invalid to
19842 initialize a static data member with a parenthesized
19843 initializer; only the "=" form of initialization is
19846 cp_parser_parse_tentatively (parser
);
19848 /* Consume the `('. */
19849 matching_parens parens
;
19850 parens
.consume_open (parser
);
19853 /* If this is going to be an abstract declarator, we're
19854 in a declarator and we can't have default args. */
19855 parser
->default_arg_ok_p
= false;
19856 parser
->in_declarator_p
= true;
19859 begin_scope (sk_function_parms
, NULL_TREE
);
19861 /* Parse the parameter-declaration-clause. */
19862 params
= cp_parser_parameter_declaration_clause (parser
);
19864 /* Consume the `)'. */
19865 parens
.require_close (parser
);
19867 /* If all went well, parse the cv-qualifier-seq,
19868 ref-qualifier and the exception-specification. */
19869 if (member_p
|| cp_parser_parse_definitely (parser
))
19871 cp_cv_quals cv_quals
;
19872 cp_virt_specifiers virt_specifiers
;
19873 cp_ref_qualifier ref_qual
;
19874 tree exception_specification
;
19877 bool memfn
= (member_p
|| (pushed_scope
19878 && CLASS_TYPE_P (pushed_scope
)));
19880 is_declarator
= true;
19882 if (ctor_dtor_or_conv_p
)
19883 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19886 /* Parse the cv-qualifier-seq. */
19887 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19888 /* Parse the ref-qualifier. */
19889 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19890 /* Parse the tx-qualifier. */
19891 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19892 /* And the exception-specification. */
19893 exception_specification
19894 = cp_parser_exception_specification_opt (parser
);
19896 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19898 /* In here, we handle cases where attribute is used after
19899 the function declaration. For example:
19900 void func (int x) __attribute__((vector(..))); */
19901 tree gnu_attrs
= NULL_TREE
;
19903 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
19905 cp_parser_parse_tentatively (parser
);
19906 tree attr
= cp_parser_gnu_attributes_opt (parser
);
19907 if (cp_lexer_next_token_is_not (parser
->lexer
,
19909 && cp_lexer_next_token_is_not (parser
->lexer
,
19911 cp_parser_abort_tentative_parse (parser
);
19912 else if (!cp_parser_parse_definitely (parser
))
19917 tree requires_clause
= NULL_TREE
;
19918 late_return
= (cp_parser_late_return_type_opt
19919 (parser
, declarator
, requires_clause
,
19920 memfn
? cv_quals
: -1));
19922 /* Parse the virt-specifier-seq. */
19923 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19925 /* Create the function-declarator. */
19926 declarator
= make_call_declarator (declarator
,
19932 exception_specification
,
19935 declarator
->std_attributes
= attrs
;
19936 declarator
->attributes
= gnu_attrs
;
19937 /* Any subsequent parameter lists are to do with
19938 return type, so are not those of the declared
19940 parser
->default_arg_ok_p
= false;
19943 /* Remove the function parms from scope. */
19944 pop_bindings_and_leave_scope ();
19947 /* Repeat the main loop. */
19951 /* If this is the first, we can try a parenthesized
19955 bool saved_in_type_id_in_expr_p
;
19957 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19958 parser
->in_declarator_p
= saved_in_declarator_p
;
19960 /* Consume the `('. */
19961 matching_parens parens
;
19962 parens
.consume_open (parser
);
19963 /* Parse the nested declarator. */
19964 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19965 parser
->in_type_id_in_expr_p
= true;
19967 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
19968 /*parenthesized_p=*/NULL
,
19969 member_p
, friend_p
);
19970 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19972 /* Expect a `)'. */
19973 if (!parens
.require_close (parser
))
19974 declarator
= cp_error_declarator
;
19975 if (declarator
== cp_error_declarator
)
19978 goto handle_declarator
;
19980 /* Otherwise, we must be done. */
19984 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19985 && token
->type
== CPP_OPEN_SQUARE
19986 && !cp_next_tokens_can_be_attribute_p (parser
))
19988 /* Parse an array-declarator. */
19989 tree bounds
, attrs
;
19991 if (ctor_dtor_or_conv_p
)
19992 *ctor_dtor_or_conv_p
= 0;
19995 parser
->default_arg_ok_p
= false;
19996 parser
->in_declarator_p
= true;
19997 /* Consume the `['. */
19998 cp_lexer_consume_token (parser
->lexer
);
19999 /* Peek at the next token. */
20000 token
= cp_lexer_peek_token (parser
->lexer
);
20001 /* If the next token is `]', then there is no
20002 constant-expression. */
20003 if (token
->type
!= CPP_CLOSE_SQUARE
)
20005 bool non_constant_p
;
20007 = cp_parser_constant_expression (parser
,
20008 /*allow_non_constant=*/true,
20010 if (!non_constant_p
)
20012 else if (error_operand_p (bounds
))
20013 /* Already gave an error. */;
20014 else if (!parser
->in_function_body
20015 || current_binding_level
->kind
== sk_function_parms
)
20017 /* Normally, the array bound must be an integral constant
20018 expression. However, as an extension, we allow VLAs
20019 in function scopes as long as they aren't part of a
20020 parameter declaration. */
20021 cp_parser_error (parser
,
20022 "array bound is not an integer constant");
20023 bounds
= error_mark_node
;
20025 else if (processing_template_decl
20026 && !type_dependent_expression_p (bounds
))
20028 /* Remember this wasn't a constant-expression. */
20029 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20030 TREE_SIDE_EFFECTS (bounds
) = 1;
20034 bounds
= NULL_TREE
;
20035 /* Look for the closing `]'. */
20036 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20038 declarator
= cp_error_declarator
;
20042 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20043 declarator
= make_array_declarator (declarator
, bounds
);
20044 declarator
->std_attributes
= attrs
;
20046 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20049 tree qualifying_scope
;
20050 tree unqualified_name
;
20052 special_function_kind sfk
;
20054 bool pack_expansion_p
= false;
20055 cp_token
*declarator_id_start_token
;
20057 /* Parse a declarator-id */
20058 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20061 cp_parser_parse_tentatively (parser
);
20063 /* If we see an ellipsis, we should be looking at a
20065 if (token
->type
== CPP_ELLIPSIS
)
20067 /* Consume the `...' */
20068 cp_lexer_consume_token (parser
->lexer
);
20070 pack_expansion_p
= true;
20074 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20076 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20077 qualifying_scope
= parser
->scope
;
20082 if (!unqualified_name
&& pack_expansion_p
)
20084 /* Check whether an error occurred. */
20085 okay
= !cp_parser_error_occurred (parser
);
20087 /* We already consumed the ellipsis to mark a
20088 parameter pack, but we have no way to report it,
20089 so abort the tentative parse. We will be exiting
20090 immediately anyway. */
20091 cp_parser_abort_tentative_parse (parser
);
20094 okay
= cp_parser_parse_definitely (parser
);
20097 unqualified_name
= error_mark_node
;
20098 else if (unqualified_name
20099 && (qualifying_scope
20100 || (!identifier_p (unqualified_name
))))
20102 cp_parser_error (parser
, "expected unqualified-id");
20103 unqualified_name
= error_mark_node
;
20107 if (!unqualified_name
)
20109 if (unqualified_name
== error_mark_node
)
20111 declarator
= cp_error_declarator
;
20112 pack_expansion_p
= false;
20113 declarator
->parameter_pack_p
= false;
20117 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20119 if (qualifying_scope
&& at_namespace_scope_p ()
20120 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20122 /* In the declaration of a member of a template class
20123 outside of the class itself, the SCOPE will sometimes
20124 be a TYPENAME_TYPE. For example, given:
20126 template <typename T>
20127 int S<T>::R::i = 3;
20129 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20130 this context, we must resolve S<T>::R to an ordinary
20131 type, rather than a typename type.
20133 The reason we normally avoid resolving TYPENAME_TYPEs
20134 is that a specialization of `S' might render
20135 `S<T>::R' not a type. However, if `S' is
20136 specialized, then this `i' will not be used, so there
20137 is no harm in resolving the types here. */
20140 /* Resolve the TYPENAME_TYPE. */
20141 type
= resolve_typename_type (qualifying_scope
,
20142 /*only_current_p=*/false);
20143 /* If that failed, the declarator is invalid. */
20144 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20146 if (typedef_variant_p (type
))
20147 error_at (declarator_id_start_token
->location
,
20148 "cannot define member of dependent typedef "
20151 error_at (declarator_id_start_token
->location
,
20152 "%<%T::%E%> is not a type",
20153 TYPE_CONTEXT (qualifying_scope
),
20154 TYPE_IDENTIFIER (qualifying_scope
));
20156 qualifying_scope
= type
;
20161 if (unqualified_name
)
20165 if (qualifying_scope
20166 && CLASS_TYPE_P (qualifying_scope
))
20167 class_type
= qualifying_scope
;
20169 class_type
= current_class_type
;
20171 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20173 tree name_type
= TREE_TYPE (unqualified_name
);
20175 if (!class_type
|| !same_type_p (name_type
, class_type
))
20177 /* We do not attempt to print the declarator
20178 here because we do not have enough
20179 information about its original syntactic
20181 cp_parser_error (parser
, "invalid declarator");
20182 declarator
= cp_error_declarator
;
20185 else if (qualifying_scope
20186 && CLASSTYPE_USE_TEMPLATE (name_type
))
20188 error_at (declarator_id_start_token
->location
,
20189 "invalid use of constructor as a template");
20190 inform (declarator_id_start_token
->location
,
20191 "use %<%T::%D%> instead of %<%T::%D%> to "
20192 "name the constructor in a qualified name",
20194 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20195 class_type
, name_type
);
20196 declarator
= cp_error_declarator
;
20199 unqualified_name
= constructor_name (class_type
);
20204 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20205 sfk
= sfk_destructor
;
20206 else if (identifier_p (unqualified_name
)
20207 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20208 sfk
= sfk_conversion
;
20209 else if (/* There's no way to declare a constructor
20210 for an unnamed type, even if the type
20211 got a name for linkage purposes. */
20212 !TYPE_WAS_UNNAMED (class_type
)
20213 /* Handle correctly (c++/19200):
20227 friend void N::S();
20229 && (!friend_p
|| class_type
== qualifying_scope
)
20230 && constructor_name_p (unqualified_name
,
20232 sfk
= sfk_constructor
;
20233 else if (is_overloaded_fn (unqualified_name
)
20234 && DECL_CONSTRUCTOR_P (get_first_fn
20235 (unqualified_name
)))
20236 sfk
= sfk_constructor
;
20238 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20239 *ctor_dtor_or_conv_p
= -1;
20242 declarator
= make_id_declarator (qualifying_scope
,
20245 declarator
->std_attributes
= attrs
;
20246 declarator
->id_loc
= token
->location
;
20247 declarator
->parameter_pack_p
= pack_expansion_p
;
20249 if (pack_expansion_p
)
20250 maybe_warn_variadic_templates ();
20253 handle_declarator
:;
20254 scope
= get_scope_of_declarator (declarator
);
20257 /* Any names that appear after the declarator-id for a
20258 member are looked up in the containing scope. */
20259 if (at_function_scope_p ())
20261 /* But declarations with qualified-ids can't appear in a
20263 cp_parser_error (parser
, "qualified-id in declaration");
20264 declarator
= cp_error_declarator
;
20267 pushed_scope
= push_scope (scope
);
20269 parser
->in_declarator_p
= true;
20270 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20271 || (declarator
&& declarator
->kind
== cdk_id
))
20272 /* Default args are only allowed on function
20274 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20276 parser
->default_arg_ok_p
= false;
20285 /* For an abstract declarator, we might wind up with nothing at this
20286 point. That's an error; the declarator is not optional. */
20288 cp_parser_error (parser
, "expected declarator");
20290 /* If we entered a scope, we must exit it now. */
20292 pop_scope (pushed_scope
);
20294 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20295 parser
->in_declarator_p
= saved_in_declarator_p
;
20300 /* Parse a ptr-operator.
20303 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20304 * cv-qualifier-seq [opt]
20306 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20307 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20312 & cv-qualifier-seq [opt]
20314 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20315 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20316 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20317 filled in with the TYPE containing the member. *CV_QUALS is
20318 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20319 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20320 Note that the tree codes returned by this function have nothing
20321 to do with the types of trees that will be eventually be created
20322 to represent the pointer or reference type being parsed. They are
20323 just constants with suggestive names. */
20324 static enum tree_code
20325 cp_parser_ptr_operator (cp_parser
* parser
,
20327 cp_cv_quals
*cv_quals
,
20330 enum tree_code code
= ERROR_MARK
;
20332 tree attrs
= NULL_TREE
;
20334 /* Assume that it's not a pointer-to-member. */
20336 /* And that there are no cv-qualifiers. */
20337 *cv_quals
= TYPE_UNQUALIFIED
;
20339 /* Peek at the next token. */
20340 token
= cp_lexer_peek_token (parser
->lexer
);
20342 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20343 if (token
->type
== CPP_MULT
)
20344 code
= INDIRECT_REF
;
20345 else if (token
->type
== CPP_AND
)
20347 else if ((cxx_dialect
!= cxx98
) &&
20348 token
->type
== CPP_AND_AND
) /* C++0x only */
20349 code
= NON_LVALUE_EXPR
;
20351 if (code
!= ERROR_MARK
)
20353 /* Consume the `*', `&' or `&&'. */
20354 cp_lexer_consume_token (parser
->lexer
);
20356 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20357 `&', if we are allowing GNU extensions. (The only qualifier
20358 that can legally appear after `&' is `restrict', but that is
20359 enforced during semantic analysis. */
20360 if (code
== INDIRECT_REF
20361 || cp_parser_allow_gnu_extensions_p (parser
))
20362 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20364 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20365 if (attributes
!= NULL
)
20366 *attributes
= attrs
;
20370 /* Try the pointer-to-member case. */
20371 cp_parser_parse_tentatively (parser
);
20372 /* Look for the optional `::' operator. */
20373 cp_parser_global_scope_opt (parser
,
20374 /*current_scope_valid_p=*/false);
20375 /* Look for the nested-name specifier. */
20376 token
= cp_lexer_peek_token (parser
->lexer
);
20377 cp_parser_nested_name_specifier (parser
,
20378 /*typename_keyword_p=*/false,
20379 /*check_dependency_p=*/true,
20381 /*is_declaration=*/false);
20382 /* If we found it, and the next token is a `*', then we are
20383 indeed looking at a pointer-to-member operator. */
20384 if (!cp_parser_error_occurred (parser
)
20385 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20387 /* Indicate that the `*' operator was used. */
20388 code
= INDIRECT_REF
;
20390 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20391 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20392 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20393 error_at (token
->location
, "cannot form pointer to member of "
20394 "non-class %q#T", parser
->scope
);
20397 /* The type of which the member is a member is given by the
20399 *type
= parser
->scope
;
20400 /* The next name will not be qualified. */
20401 parser
->scope
= NULL_TREE
;
20402 parser
->qualifying_scope
= NULL_TREE
;
20403 parser
->object_scope
= NULL_TREE
;
20404 /* Look for optional c++11 attributes. */
20405 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20406 if (attributes
!= NULL
)
20407 *attributes
= attrs
;
20408 /* Look for the optional cv-qualifier-seq. */
20409 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20412 /* If that didn't work we don't have a ptr-operator. */
20413 if (!cp_parser_parse_definitely (parser
))
20414 cp_parser_error (parser
, "expected ptr-operator");
20420 /* Parse an (optional) cv-qualifier-seq.
20423 cv-qualifier cv-qualifier-seq [opt]
20434 Returns a bitmask representing the cv-qualifiers. */
20437 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20439 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20444 cp_cv_quals cv_qualifier
;
20446 /* Peek at the next token. */
20447 token
= cp_lexer_peek_token (parser
->lexer
);
20448 /* See if it's a cv-qualifier. */
20449 switch (token
->keyword
)
20452 cv_qualifier
= TYPE_QUAL_CONST
;
20456 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20460 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20464 cv_qualifier
= TYPE_UNQUALIFIED
;
20471 if (cv_quals
& cv_qualifier
)
20473 gcc_rich_location
richloc (token
->location
);
20474 richloc
.add_fixit_remove ();
20475 error_at_rich_loc (&richloc
, "duplicate cv-qualifier");
20476 cp_lexer_purge_token (parser
->lexer
);
20480 cp_lexer_consume_token (parser
->lexer
);
20481 cv_quals
|= cv_qualifier
;
20488 /* Parse an (optional) ref-qualifier
20494 Returns cp_ref_qualifier representing ref-qualifier. */
20496 static cp_ref_qualifier
20497 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20499 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20501 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20502 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20507 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20508 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20510 switch (token
->type
)
20513 curr_ref_qual
= REF_QUAL_LVALUE
;
20517 curr_ref_qual
= REF_QUAL_RVALUE
;
20521 curr_ref_qual
= REF_QUAL_NONE
;
20525 if (!curr_ref_qual
)
20529 error_at (token
->location
, "multiple ref-qualifiers");
20530 cp_lexer_purge_token (parser
->lexer
);
20534 ref_qual
= curr_ref_qual
;
20535 cp_lexer_consume_token (parser
->lexer
);
20542 /* Parse an optional tx-qualifier.
20546 transaction_safe_dynamic */
20549 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20552 if (token
->type
== CPP_NAME
)
20554 tree name
= token
->u
.value
;
20555 const char *p
= IDENTIFIER_POINTER (name
);
20556 const int len
= strlen ("transaction_safe");
20557 if (!strncmp (p
, "transaction_safe", len
))
20561 || !strcmp (p
, "_dynamic"))
20563 cp_lexer_consume_token (parser
->lexer
);
20566 error ("%qE requires %<-fgnu-tm%>", name
);
20577 /* Parse an (optional) virt-specifier-seq.
20579 virt-specifier-seq:
20580 virt-specifier virt-specifier-seq [opt]
20586 Returns a bitmask representing the virt-specifiers. */
20588 static cp_virt_specifiers
20589 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20591 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20596 cp_virt_specifiers virt_specifier
;
20598 /* Peek at the next token. */
20599 token
= cp_lexer_peek_token (parser
->lexer
);
20600 /* See if it's a virt-specifier-qualifier. */
20601 if (token
->type
!= CPP_NAME
)
20603 if (id_equal (token
->u
.value
, "override"))
20605 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20606 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20608 else if (id_equal (token
->u
.value
, "final"))
20610 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20611 virt_specifier
= VIRT_SPEC_FINAL
;
20613 else if (id_equal (token
->u
.value
, "__final"))
20615 virt_specifier
= VIRT_SPEC_FINAL
;
20620 if (virt_specifiers
& virt_specifier
)
20622 gcc_rich_location
richloc (token
->location
);
20623 richloc
.add_fixit_remove ();
20624 error_at_rich_loc (&richloc
, "duplicate virt-specifier");
20625 cp_lexer_purge_token (parser
->lexer
);
20629 cp_lexer_consume_token (parser
->lexer
);
20630 virt_specifiers
|= virt_specifier
;
20633 return virt_specifiers
;
20636 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20637 is in scope even though it isn't real. */
20640 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20644 if (current_class_ptr
)
20646 /* We don't clear this between NSDMIs. Is it already what we want? */
20647 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20648 if (DECL_P (current_class_ptr
)
20649 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20650 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20651 && cp_type_quals (type
) == quals
)
20655 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20656 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20657 current_class_ptr
= NULL_TREE
;
20659 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
20660 current_class_ptr
= this_parm
;
20663 /* Return true iff our current scope is a non-static data member
20667 parsing_nsdmi (void)
20669 /* We recognize NSDMI context by the context-less 'this' pointer set up
20670 by the function above. */
20671 if (current_class_ptr
20672 && TREE_CODE (current_class_ptr
) == PARM_DECL
20673 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20678 /* Parse a late-specified return type, if any. This is not a separate
20679 non-terminal, but part of a function declarator, which looks like
20681 -> trailing-type-specifier-seq abstract-declarator(opt)
20683 Returns the type indicated by the type-id.
20685 In addition to this, parse any queued up #pragma omp declare simd
20686 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20687 #pragma acc routine clauses.
20689 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20693 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20694 tree
& requires_clause
, cp_cv_quals quals
)
20697 tree type
= NULL_TREE
;
20698 bool declare_simd_p
= (parser
->omp_declare_simd
20700 && declarator
->kind
== cdk_id
);
20702 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
20703 && declarator
&& declarator
->kind
== cdk_id
);
20705 bool oacc_routine_p
= (parser
->oacc_routine
20707 && declarator
->kind
== cdk_id
);
20709 /* Peek at the next token. */
20710 token
= cp_lexer_peek_token (parser
->lexer
);
20711 /* A late-specified return type is indicated by an initial '->'. */
20712 if (token
->type
!= CPP_DEREF
20713 && token
->keyword
!= RID_REQUIRES
20714 && !(token
->type
== CPP_NAME
20715 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20716 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
20719 tree save_ccp
= current_class_ptr
;
20720 tree save_ccr
= current_class_ref
;
20723 /* DR 1207: 'this' is in scope in the trailing return type. */
20724 inject_this_parameter (current_class_type
, quals
);
20727 if (token
->type
== CPP_DEREF
)
20729 /* Consume the ->. */
20730 cp_lexer_consume_token (parser
->lexer
);
20732 type
= cp_parser_trailing_type_id (parser
);
20735 /* Function declarations may be followed by a trailing
20736 requires-clause. */
20737 requires_clause
= cp_parser_requires_clause_opt (parser
);
20739 if (cilk_simd_fn_vector_p
)
20740 declarator
->attributes
20741 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
20742 declarator
->attributes
);
20743 if (declare_simd_p
)
20744 declarator
->attributes
20745 = cp_parser_late_parsing_omp_declare_simd (parser
,
20746 declarator
->attributes
);
20747 if (oacc_routine_p
)
20748 declarator
->attributes
20749 = cp_parser_late_parsing_oacc_routine (parser
,
20750 declarator
->attributes
);
20754 current_class_ptr
= save_ccp
;
20755 current_class_ref
= save_ccr
;
20761 /* Parse a declarator-id.
20765 :: [opt] nested-name-specifier [opt] type-name
20767 In the `id-expression' case, the value returned is as for
20768 cp_parser_id_expression if the id-expression was an unqualified-id.
20769 If the id-expression was a qualified-id, then a SCOPE_REF is
20770 returned. The first operand is the scope (either a NAMESPACE_DECL
20771 or TREE_TYPE), but the second is still just a representation of an
20775 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20778 /* The expression must be an id-expression. Assume that qualified
20779 names are the names of types so that:
20782 int S<T>::R::i = 3;
20784 will work; we must treat `S<T>::R' as the name of a type.
20785 Similarly, assume that qualified names are templates, where
20789 int S<T>::R<T>::i = 3;
20792 id
= cp_parser_id_expression (parser
,
20793 /*template_keyword_p=*/false,
20794 /*check_dependency_p=*/false,
20795 /*template_p=*/NULL
,
20796 /*declarator_p=*/true,
20798 if (id
&& BASELINK_P (id
))
20799 id
= BASELINK_FUNCTIONS (id
);
20803 /* Parse a type-id.
20806 type-specifier-seq abstract-declarator [opt]
20808 Returns the TYPE specified. */
20811 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20812 bool is_trailing_return
)
20814 cp_decl_specifier_seq type_specifier_seq
;
20815 cp_declarator
*abstract_declarator
;
20817 /* Parse the type-specifier-seq. */
20818 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20819 is_trailing_return
,
20820 &type_specifier_seq
);
20821 if (is_template_arg
&& type_specifier_seq
.type
20822 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20823 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20824 /* A bare template name as a template argument is a template template
20825 argument, not a placeholder, so fail parsing it as a type argument. */
20827 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20828 cp_parser_simulate_error (parser
);
20829 return error_mark_node
;
20831 if (type_specifier_seq
.type
== error_mark_node
)
20832 return error_mark_node
;
20834 /* There might or might not be an abstract declarator. */
20835 cp_parser_parse_tentatively (parser
);
20836 /* Look for the declarator. */
20837 abstract_declarator
20838 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20839 /*parenthesized_p=*/NULL
,
20840 /*member_p=*/false,
20841 /*friend_p=*/false);
20842 /* Check to see if there really was a declarator. */
20843 if (!cp_parser_parse_definitely (parser
))
20844 abstract_declarator
= NULL
;
20846 if (type_specifier_seq
.type
20847 /* The concepts TS allows 'auto' as a type-id. */
20848 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20849 /* None of the valid uses of 'auto' in C++14 involve the type-id
20850 nonterminal, but it is valid in a trailing-return-type. */
20851 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20852 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20854 /* A type-id with type 'auto' is only ok if the abstract declarator
20855 is a function declarator with a late-specified return type.
20857 A type-id with 'auto' is also valid in a trailing-return-type
20858 in a compound-requirement. */
20859 if (abstract_declarator
20860 && abstract_declarator
->kind
== cdk_function
20861 && abstract_declarator
->u
.function
.late_return_type
)
20863 else if (parser
->in_result_type_constraint_p
)
20867 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20868 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20870 error_at (loc
, "missing template arguments after %qT",
20872 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20876 error_at (loc
, "invalid use of %qT", auto_node
);
20877 return error_mark_node
;
20881 return groktypename (&type_specifier_seq
, abstract_declarator
,
20886 cp_parser_type_id (cp_parser
*parser
)
20888 return cp_parser_type_id_1 (parser
, false, false);
20892 cp_parser_template_type_arg (cp_parser
*parser
)
20895 const char *saved_message
= parser
->type_definition_forbidden_message
;
20896 parser
->type_definition_forbidden_message
20897 = G_("types may not be defined in template arguments");
20898 r
= cp_parser_type_id_1 (parser
, true, false);
20899 parser
->type_definition_forbidden_message
= saved_message
;
20900 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20902 error ("invalid use of %<auto%> in template argument");
20903 r
= error_mark_node
;
20909 cp_parser_trailing_type_id (cp_parser
*parser
)
20911 return cp_parser_type_id_1 (parser
, false, true);
20914 /* Parse a type-specifier-seq.
20916 type-specifier-seq:
20917 type-specifier type-specifier-seq [opt]
20921 type-specifier-seq:
20922 attributes type-specifier-seq [opt]
20924 If IS_DECLARATION is true, we are at the start of a "condition" or
20925 exception-declaration, so we might be followed by a declarator-id.
20927 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20928 i.e. we've just seen "->".
20930 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20933 cp_parser_type_specifier_seq (cp_parser
* parser
,
20934 bool is_declaration
,
20935 bool is_trailing_return
,
20936 cp_decl_specifier_seq
*type_specifier_seq
)
20938 bool seen_type_specifier
= false;
20939 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20940 cp_token
*start_token
= NULL
;
20942 /* Clear the TYPE_SPECIFIER_SEQ. */
20943 clear_decl_specs (type_specifier_seq
);
20945 /* In the context of a trailing return type, enum E { } is an
20946 elaborated-type-specifier followed by a function-body, not an
20948 if (is_trailing_return
)
20949 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
20951 /* Parse the type-specifiers and attributes. */
20954 tree type_specifier
;
20955 bool is_cv_qualifier
;
20957 /* Check for attributes first. */
20958 if (cp_next_tokens_can_be_attribute_p (parser
))
20960 type_specifier_seq
->attributes
=
20961 chainon (type_specifier_seq
->attributes
,
20962 cp_parser_attributes_opt (parser
));
20966 /* record the token of the beginning of the type specifier seq,
20967 for error reporting purposes*/
20969 start_token
= cp_lexer_peek_token (parser
->lexer
);
20971 /* Look for the type-specifier. */
20972 type_specifier
= cp_parser_type_specifier (parser
,
20974 type_specifier_seq
,
20975 /*is_declaration=*/false,
20978 if (!type_specifier
)
20980 /* If the first type-specifier could not be found, this is not a
20981 type-specifier-seq at all. */
20982 if (!seen_type_specifier
)
20984 /* Set in_declarator_p to avoid skipping to the semicolon. */
20985 int in_decl
= parser
->in_declarator_p
;
20986 parser
->in_declarator_p
= true;
20988 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
20989 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20990 cp_parser_error (parser
, "expected type-specifier");
20992 parser
->in_declarator_p
= in_decl
;
20994 type_specifier_seq
->type
= error_mark_node
;
20997 /* If subsequent type-specifiers could not be found, the
20998 type-specifier-seq is complete. */
21002 seen_type_specifier
= true;
21003 /* The standard says that a condition can be:
21005 type-specifier-seq declarator = assignment-expression
21012 we should treat the "S" as a declarator, not as a
21013 type-specifier. The standard doesn't say that explicitly for
21014 type-specifier-seq, but it does say that for
21015 decl-specifier-seq in an ordinary declaration. Perhaps it
21016 would be clearer just to allow a decl-specifier-seq here, and
21017 then add a semantic restriction that if any decl-specifiers
21018 that are not type-specifiers appear, the program is invalid. */
21019 if (is_declaration
&& !is_cv_qualifier
)
21020 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21024 /* Return whether the function currently being declared has an associated
21025 template parameter list. */
21028 function_being_declared_is_template_p (cp_parser
* parser
)
21030 if (!current_template_parms
|| processing_template_parmlist
)
21033 if (parser
->implicit_template_scope
)
21036 if (at_class_scope_p ()
21037 && TYPE_BEING_DEFINED (current_class_type
))
21038 return parser
->num_template_parameter_lists
!= 0;
21040 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21041 (current_class_type
));
21044 /* Parse a parameter-declaration-clause.
21046 parameter-declaration-clause:
21047 parameter-declaration-list [opt] ... [opt]
21048 parameter-declaration-list , ...
21050 Returns a representation for the parameter declarations. A return
21051 value of NULL indicates a parameter-declaration-clause consisting
21052 only of an ellipsis. */
21055 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21064 int auto_is_implicit_function_template_parm_p
;
21066 parser
->auto_is_implicit_function_template_parm_p
21067 = auto_is_implicit_function_template_parm_p
;
21069 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21073 if (!processing_specialization
21074 && !processing_template_parmlist
21075 && !processing_explicit_instantiation
)
21076 if (!current_function_decl
21077 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21078 parser
->auto_is_implicit_function_template_parm_p
= true;
21080 /* Peek at the next token. */
21081 token
= cp_lexer_peek_token (parser
->lexer
);
21082 /* Check for trivial parameter-declaration-clauses. */
21083 if (token
->type
== CPP_ELLIPSIS
)
21085 /* Consume the `...' token. */
21086 cp_lexer_consume_token (parser
->lexer
);
21089 else if (token
->type
== CPP_CLOSE_PAREN
)
21090 /* There are no parameters. */
21092 #ifndef NO_IMPLICIT_EXTERN_C
21093 if (in_system_header_at (input_location
)
21094 && current_class_type
== NULL
21095 && current_lang_name
== lang_name_c
)
21099 return void_list_node
;
21101 /* Check for `(void)', too, which is a special case. */
21102 else if (token
->keyword
== RID_VOID
21103 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21104 == CPP_CLOSE_PAREN
))
21106 /* Consume the `void' token. */
21107 cp_lexer_consume_token (parser
->lexer
);
21108 /* There are no parameters. */
21109 return void_list_node
;
21112 /* Parse the parameter-declaration-list. */
21113 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21114 /* If a parse error occurred while parsing the
21115 parameter-declaration-list, then the entire
21116 parameter-declaration-clause is erroneous. */
21120 /* Peek at the next token. */
21121 token
= cp_lexer_peek_token (parser
->lexer
);
21122 /* If it's a `,', the clause should terminate with an ellipsis. */
21123 if (token
->type
== CPP_COMMA
)
21125 /* Consume the `,'. */
21126 cp_lexer_consume_token (parser
->lexer
);
21127 /* Expect an ellipsis. */
21129 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21131 /* It might also be `...' if the optional trailing `,' was
21133 else if (token
->type
== CPP_ELLIPSIS
)
21135 /* Consume the `...' token. */
21136 cp_lexer_consume_token (parser
->lexer
);
21137 /* And remember that we saw it. */
21141 ellipsis_p
= false;
21143 /* Finish the parameter list. */
21145 parameters
= chainon (parameters
, void_list_node
);
21150 /* Parse a parameter-declaration-list.
21152 parameter-declaration-list:
21153 parameter-declaration
21154 parameter-declaration-list , parameter-declaration
21156 Returns a representation of the parameter-declaration-list, as for
21157 cp_parser_parameter_declaration_clause. However, the
21158 `void_list_node' is never appended to the list. Upon return,
21159 *IS_ERROR will be true iff an error occurred. */
21162 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21164 tree parameters
= NULL_TREE
;
21165 tree
*tail
= ¶meters
;
21166 bool saved_in_unbraced_linkage_specification_p
;
21169 /* Assume all will go well. */
21171 /* The special considerations that apply to a function within an
21172 unbraced linkage specifications do not apply to the parameters
21173 to the function. */
21174 saved_in_unbraced_linkage_specification_p
21175 = parser
->in_unbraced_linkage_specification_p
;
21176 parser
->in_unbraced_linkage_specification_p
= false;
21178 /* Look for more parameters. */
21181 cp_parameter_declarator
*parameter
;
21182 tree decl
= error_mark_node
;
21183 bool parenthesized_p
= false;
21184 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21185 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21186 (current_template_parms
)) : 0);
21188 /* Parse the parameter. */
21190 = cp_parser_parameter_declaration (parser
,
21191 /*template_parm_p=*/false,
21194 /* We don't know yet if the enclosing context is deprecated, so wait
21195 and warn in grokparms if appropriate. */
21196 deprecated_state
= DEPRECATED_SUPPRESS
;
21200 /* If a function parameter pack was specified and an implicit template
21201 parameter was introduced during cp_parser_parameter_declaration,
21202 change any implicit parameters introduced into packs. */
21203 if (parser
->implicit_template_parms
21204 && parameter
->declarator
21205 && parameter
->declarator
->parameter_pack_p
)
21207 int latest_template_parm_idx
= TREE_VEC_LENGTH
21208 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21210 if (latest_template_parm_idx
!= template_parm_idx
)
21211 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21212 (parameter
->decl_specifiers
.type
,
21213 template_parm_idx
, latest_template_parm_idx
);
21216 decl
= grokdeclarator (parameter
->declarator
,
21217 ¶meter
->decl_specifiers
,
21219 parameter
->default_argument
!= NULL_TREE
,
21220 ¶meter
->decl_specifiers
.attributes
);
21223 deprecated_state
= DEPRECATED_NORMAL
;
21225 /* If a parse error occurred parsing the parameter declaration,
21226 then the entire parameter-declaration-list is erroneous. */
21227 if (decl
== error_mark_node
)
21230 parameters
= error_mark_node
;
21234 if (parameter
->decl_specifiers
.attributes
)
21235 cplus_decl_attributes (&decl
,
21236 parameter
->decl_specifiers
.attributes
,
21238 if (DECL_NAME (decl
))
21239 decl
= pushdecl (decl
);
21241 if (decl
!= error_mark_node
)
21243 retrofit_lang_decl (decl
);
21244 DECL_PARM_INDEX (decl
) = ++index
;
21245 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21248 /* Add the new parameter to the list. */
21249 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21250 tail
= &TREE_CHAIN (*tail
);
21252 /* Peek at the next token. */
21253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21254 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21255 /* These are for Objective-C++ */
21256 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21257 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21258 /* The parameter-declaration-list is complete. */
21260 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21264 /* Peek at the next token. */
21265 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21266 /* If it's an ellipsis, then the list is complete. */
21267 if (token
->type
== CPP_ELLIPSIS
)
21269 /* Otherwise, there must be more parameters. Consume the
21271 cp_lexer_consume_token (parser
->lexer
);
21272 /* When parsing something like:
21274 int i(float f, double d)
21276 we can tell after seeing the declaration for "f" that we
21277 are not looking at an initialization of a variable "i",
21278 but rather at the declaration of a function "i".
21280 Due to the fact that the parsing of template arguments
21281 (as specified to a template-id) requires backtracking we
21282 cannot use this technique when inside a template argument
21284 if (!parser
->in_template_argument_list_p
21285 && !parser
->in_type_id_in_expr_p
21286 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21287 /* However, a parameter-declaration of the form
21288 "float(f)" (which is a valid declaration of a
21289 parameter "f") can also be interpreted as an
21290 expression (the conversion of "f" to "float"). */
21291 && !parenthesized_p
)
21292 cp_parser_commit_to_tentative_parse (parser
);
21296 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21297 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21298 cp_parser_skip_to_closing_parenthesis (parser
,
21299 /*recovering=*/true,
21300 /*or_comma=*/false,
21301 /*consume_paren=*/false);
21306 parser
->in_unbraced_linkage_specification_p
21307 = saved_in_unbraced_linkage_specification_p
;
21309 /* Reset implicit_template_scope if we are about to leave the function
21310 parameter list that introduced it. Note that for out-of-line member
21311 definitions, there will be one or more class scopes before we get to
21312 the template parameter scope. */
21314 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21315 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21317 while (maybe_its
->kind
== sk_class
)
21318 maybe_its
= maybe_its
->level_chain
;
21319 if (maybe_its
== its
)
21321 parser
->implicit_template_parms
= 0;
21322 parser
->implicit_template_scope
= 0;
21329 /* Parse a parameter declaration.
21331 parameter-declaration:
21332 decl-specifier-seq ... [opt] declarator
21333 decl-specifier-seq declarator = assignment-expression
21334 decl-specifier-seq ... [opt] abstract-declarator [opt]
21335 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21337 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21338 declares a template parameter. (In that case, a non-nested `>'
21339 token encountered during the parsing of the assignment-expression
21340 is not interpreted as a greater-than operator.)
21342 Returns a representation of the parameter, or NULL if an error
21343 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21344 true iff the declarator is of the form "(p)". */
21346 static cp_parameter_declarator
*
21347 cp_parser_parameter_declaration (cp_parser
*parser
,
21348 bool template_parm_p
,
21349 bool *parenthesized_p
)
21351 int declares_class_or_enum
;
21352 cp_decl_specifier_seq decl_specifiers
;
21353 cp_declarator
*declarator
;
21354 tree default_argument
;
21355 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21356 const char *saved_message
;
21357 bool template_parameter_pack_p
= false;
21359 /* In a template parameter, `>' is not an operator.
21363 When parsing a default template-argument for a non-type
21364 template-parameter, the first non-nested `>' is taken as the end
21365 of the template parameter-list rather than a greater-than
21368 /* Type definitions may not appear in parameter types. */
21369 saved_message
= parser
->type_definition_forbidden_message
;
21370 parser
->type_definition_forbidden_message
21371 = G_("types may not be defined in parameter types");
21373 /* Parse the declaration-specifiers. */
21374 cp_parser_decl_specifier_seq (parser
,
21375 CP_PARSER_FLAGS_NONE
,
21377 &declares_class_or_enum
);
21379 /* Complain about missing 'typename' or other invalid type names. */
21380 if (!decl_specifiers
.any_type_specifiers_p
21381 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21382 decl_specifiers
.type
= error_mark_node
;
21384 /* If an error occurred, there's no reason to attempt to parse the
21385 rest of the declaration. */
21386 if (cp_parser_error_occurred (parser
))
21388 parser
->type_definition_forbidden_message
= saved_message
;
21392 /* Peek at the next token. */
21393 token
= cp_lexer_peek_token (parser
->lexer
);
21395 /* If the next token is a `)', `,', `=', `>', or `...', then there
21396 is no declarator. However, when variadic templates are enabled,
21397 there may be a declarator following `...'. */
21398 if (token
->type
== CPP_CLOSE_PAREN
21399 || token
->type
== CPP_COMMA
21400 || token
->type
== CPP_EQ
21401 || token
->type
== CPP_GREATER
)
21404 if (parenthesized_p
)
21405 *parenthesized_p
= false;
21407 /* Otherwise, there should be a declarator. */
21410 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21411 parser
->default_arg_ok_p
= false;
21413 /* After seeing a decl-specifier-seq, if the next token is not a
21414 "(", there is no possibility that the code is a valid
21415 expression. Therefore, if parsing tentatively, we commit at
21417 if (!parser
->in_template_argument_list_p
21418 /* In an expression context, having seen:
21422 we cannot be sure whether we are looking at a
21423 function-type (taking a "char" as a parameter) or a cast
21424 of some object of type "char" to "int". */
21425 && !parser
->in_type_id_in_expr_p
21426 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21427 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21428 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21429 cp_parser_commit_to_tentative_parse (parser
);
21430 /* Parse the declarator. */
21431 declarator_token_start
= token
;
21432 declarator
= cp_parser_declarator (parser
,
21433 CP_PARSER_DECLARATOR_EITHER
,
21434 /*ctor_dtor_or_conv_p=*/NULL
,
21436 /*member_p=*/false,
21437 /*friend_p=*/false);
21438 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21439 /* After the declarator, allow more attributes. */
21440 decl_specifiers
.attributes
21441 = chainon (decl_specifiers
.attributes
,
21442 cp_parser_attributes_opt (parser
));
21444 /* If the declarator is a template parameter pack, remember that and
21445 clear the flag in the declarator itself so we don't get errors
21446 from grokdeclarator. */
21447 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21449 declarator
->parameter_pack_p
= false;
21450 template_parameter_pack_p
= true;
21454 /* If the next token is an ellipsis, and we have not seen a declarator
21455 name, and if either the type of the declarator contains parameter
21456 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21457 for, eg, abbreviated integral type names), then we actually have a
21458 parameter pack expansion expression. Otherwise, leave the ellipsis
21459 for a C-style variadic function. */
21460 token
= cp_lexer_peek_token (parser
->lexer
);
21461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21463 tree type
= decl_specifiers
.type
;
21465 if (type
&& DECL_P (type
))
21466 type
= TREE_TYPE (type
);
21469 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21470 && (template_parm_p
|| uses_parameter_packs (type
)))
21471 || (!type
&& template_parm_p
))
21472 && declarator_can_be_parameter_pack (declarator
))
21474 /* Consume the `...'. */
21475 cp_lexer_consume_token (parser
->lexer
);
21476 maybe_warn_variadic_templates ();
21478 /* Build a pack expansion type */
21479 if (template_parm_p
)
21480 template_parameter_pack_p
= true;
21481 else if (declarator
)
21482 declarator
->parameter_pack_p
= true;
21484 decl_specifiers
.type
= make_pack_expansion (type
);
21488 /* The restriction on defining new types applies only to the type
21489 of the parameter, not to the default argument. */
21490 parser
->type_definition_forbidden_message
= saved_message
;
21492 /* If the next token is `=', then process a default argument. */
21493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21495 tree type
= decl_specifiers
.type
;
21496 token
= cp_lexer_peek_token (parser
->lexer
);
21497 /* If we are defining a class, then the tokens that make up the
21498 default argument must be saved and processed later. */
21499 if (!template_parm_p
&& at_class_scope_p ()
21500 && TYPE_BEING_DEFINED (current_class_type
)
21501 && !LAMBDA_TYPE_P (current_class_type
))
21502 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21504 // A constrained-type-specifier may declare a type template-parameter.
21505 else if (declares_constrained_type_template_parameter (type
))
21507 = cp_parser_default_type_template_argument (parser
);
21509 // A constrained-type-specifier may declare a template-template-parameter.
21510 else if (declares_constrained_template_template_parameter (type
))
21512 = cp_parser_default_template_template_argument (parser
);
21514 /* Outside of a class definition, we can just parse the
21515 assignment-expression. */
21518 = cp_parser_default_argument (parser
, template_parm_p
);
21520 if (!parser
->default_arg_ok_p
)
21522 permerror (token
->location
,
21523 "default arguments are only "
21524 "permitted for function parameters");
21526 else if ((declarator
&& declarator
->parameter_pack_p
)
21527 || template_parameter_pack_p
21528 || (decl_specifiers
.type
21529 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21531 /* Find the name of the parameter pack. */
21532 cp_declarator
*id_declarator
= declarator
;
21533 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21534 id_declarator
= id_declarator
->declarator
;
21536 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21537 error_at (declarator_token_start
->location
,
21539 ? G_("template parameter pack %qD "
21540 "cannot have a default argument")
21541 : G_("parameter pack %qD cannot have "
21542 "a default argument"),
21543 id_declarator
->u
.id
.unqualified_name
);
21545 error_at (declarator_token_start
->location
,
21547 ? G_("template parameter pack cannot have "
21548 "a default argument")
21549 : G_("parameter pack cannot have a "
21550 "default argument"));
21552 default_argument
= NULL_TREE
;
21556 default_argument
= NULL_TREE
;
21558 return make_parameter_declarator (&decl_specifiers
,
21561 template_parameter_pack_p
);
21564 /* Parse a default argument and return it.
21566 TEMPLATE_PARM_P is true if this is a default argument for a
21567 non-type template parameter. */
21569 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21571 tree default_argument
= NULL_TREE
;
21572 bool saved_greater_than_is_operator_p
;
21573 bool saved_local_variables_forbidden_p
;
21574 bool non_constant_p
, is_direct_init
;
21576 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21578 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21579 parser
->greater_than_is_operator_p
= !template_parm_p
;
21580 /* Local variable names (and the `this' keyword) may not
21581 appear in a default argument. */
21582 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21583 parser
->local_variables_forbidden_p
= true;
21584 /* Parse the assignment-expression. */
21585 if (template_parm_p
)
21586 push_deferring_access_checks (dk_no_deferred
);
21587 tree saved_class_ptr
= NULL_TREE
;
21588 tree saved_class_ref
= NULL_TREE
;
21589 /* The "this" pointer is not valid in a default argument. */
21592 saved_class_ptr
= current_class_ptr
;
21593 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21594 saved_class_ref
= current_class_ref
;
21595 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21598 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21599 /* Restore the "this" pointer. */
21602 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21603 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21605 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21606 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21607 if (template_parm_p
)
21608 pop_deferring_access_checks ();
21609 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21610 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21612 return default_argument
;
21615 /* Parse a function-body.
21618 compound_statement */
21621 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21623 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21624 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21628 /* Parse a ctor-initializer-opt followed by a function-body. Return
21629 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21630 is true we are parsing a function-try-block. */
21633 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21634 bool in_function_try_block
)
21637 bool ctor_initializer_p
;
21638 const bool check_body_p
=
21639 DECL_CONSTRUCTOR_P (current_function_decl
)
21640 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21643 /* Begin the function body. */
21644 body
= begin_function_body ();
21645 /* Parse the optional ctor-initializer. */
21646 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
21648 /* If we're parsing a constexpr constructor definition, we need
21649 to check that the constructor body is indeed empty. However,
21650 before we get to cp_parser_function_body lot of junk has been
21651 generated, so we can't just check that we have an empty block.
21652 Rather we take a snapshot of the outermost block, and check whether
21653 cp_parser_function_body changed its state. */
21656 list
= cur_stmt_list
;
21657 if (STATEMENT_LIST_TAIL (list
))
21658 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21660 /* Parse the function-body. */
21661 cp_parser_function_body (parser
, in_function_try_block
);
21663 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21664 /* Finish the function body. */
21665 finish_function_body (body
);
21667 return ctor_initializer_p
;
21670 /* Parse an initializer.
21673 = initializer-clause
21674 ( expression-list )
21676 Returns an expression representing the initializer. If no
21677 initializer is present, NULL_TREE is returned.
21679 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21680 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21681 set to TRUE if there is no initializer present. If there is an
21682 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21683 is set to true; otherwise it is set to false. */
21686 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21687 bool* non_constant_p
)
21692 /* Peek at the next token. */
21693 token
= cp_lexer_peek_token (parser
->lexer
);
21695 /* Let our caller know whether or not this initializer was
21697 *is_direct_init
= (token
->type
!= CPP_EQ
);
21698 /* Assume that the initializer is constant. */
21699 *non_constant_p
= false;
21701 if (token
->type
== CPP_EQ
)
21703 /* Consume the `='. */
21704 cp_lexer_consume_token (parser
->lexer
);
21705 /* Parse the initializer-clause. */
21706 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21708 else if (token
->type
== CPP_OPEN_PAREN
)
21710 vec
<tree
, va_gc
> *vec
;
21711 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21713 /*allow_expansion_p=*/true,
21716 return error_mark_node
;
21717 init
= build_tree_list_vec (vec
);
21718 release_tree_vector (vec
);
21720 else if (token
->type
== CPP_OPEN_BRACE
)
21722 cp_lexer_set_source_position (parser
->lexer
);
21723 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21724 init
= cp_parser_braced_list (parser
, non_constant_p
);
21725 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21729 /* Anything else is an error. */
21730 cp_parser_error (parser
, "expected initializer");
21731 init
= error_mark_node
;
21734 if (check_for_bare_parameter_packs (init
))
21735 init
= error_mark_node
;
21740 /* Parse an initializer-clause.
21742 initializer-clause:
21743 assignment-expression
21746 Returns an expression representing the initializer.
21748 If the `assignment-expression' production is used the value
21749 returned is simply a representation for the expression.
21751 Otherwise, calls cp_parser_braced_list. */
21754 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21756 cp_expr initializer
;
21758 /* Assume the expression is constant. */
21759 *non_constant_p
= false;
21761 /* If it is not a `{', then we are looking at an
21762 assignment-expression. */
21763 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21766 = cp_parser_constant_expression (parser
,
21767 /*allow_non_constant_p=*/true,
21771 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21773 return initializer
;
21776 /* Parse a brace-enclosed initializer list.
21779 { initializer-list , [opt] }
21782 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21783 the elements of the initializer-list (or NULL, if the last
21784 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21785 NULL_TREE. There is no way to detect whether or not the optional
21786 trailing `,' was provided. NON_CONSTANT_P is as for
21787 cp_parser_initializer. */
21790 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21793 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21795 /* Consume the `{' token. */
21796 matching_braces braces
;
21797 braces
.consume_open (parser
);
21798 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21799 initializer
= make_node (CONSTRUCTOR
);
21800 /* If it's not a `}', then there is a non-trivial initializer. */
21801 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21803 /* Parse the initializer list. */
21804 CONSTRUCTOR_ELTS (initializer
)
21805 = cp_parser_initializer_list (parser
, non_constant_p
);
21806 /* A trailing `,' token is allowed. */
21807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21808 cp_lexer_consume_token (parser
->lexer
);
21811 *non_constant_p
= false;
21812 /* Now, there should be a trailing `}'. */
21813 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21814 braces
.require_close (parser
);
21815 TREE_TYPE (initializer
) = init_list_type_node
;
21817 cp_expr
result (initializer
);
21818 /* Build a location of the form:
21821 with caret==start at the open brace, finish at the close brace. */
21822 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21823 result
.set_location (combined_loc
);
21827 /* Consume tokens up to, and including, the next non-nested closing `]'.
21828 Returns true iff we found a closing `]'. */
21831 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21833 unsigned square_depth
= 0;
21837 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21839 switch (token
->type
)
21842 case CPP_PRAGMA_EOL
:
21843 /* If we've run out of tokens, then there is no closing `]'. */
21846 case CPP_OPEN_SQUARE
:
21850 case CPP_CLOSE_SQUARE
:
21851 if (!square_depth
--)
21853 cp_lexer_consume_token (parser
->lexer
);
21862 /* Consume the token. */
21863 cp_lexer_consume_token (parser
->lexer
);
21867 /* Return true if we are looking at an array-designator, false otherwise. */
21870 cp_parser_array_designator_p (cp_parser
*parser
)
21872 /* Consume the `['. */
21873 cp_lexer_consume_token (parser
->lexer
);
21875 cp_lexer_save_tokens (parser
->lexer
);
21877 /* Skip tokens until the next token is a closing square bracket.
21878 If we find the closing `]', and the next token is a `=', then
21879 we are looking at an array designator. */
21880 bool array_designator_p
21881 = (cp_parser_skip_to_closing_square_bracket (parser
)
21882 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21884 /* Roll back the tokens we skipped. */
21885 cp_lexer_rollback_tokens (parser
->lexer
);
21887 return array_designator_p
;
21890 /* Parse an initializer-list.
21893 initializer-clause ... [opt]
21894 initializer-list , initializer-clause ... [opt]
21899 designation initializer-clause ...[opt]
21900 initializer-list , designation initializer-clause ...[opt]
21905 [ constant-expression ] =
21907 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21908 for the initializer. If the INDEX of the elt is non-NULL, it is the
21909 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21910 as for cp_parser_initializer. */
21912 static vec
<constructor_elt
, va_gc
> *
21913 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
21915 vec
<constructor_elt
, va_gc
> *v
= NULL
;
21917 /* Assume all of the expressions are constant. */
21918 *non_constant_p
= false;
21920 /* Parse the rest of the list. */
21926 bool clause_non_constant_p
;
21928 /* If the next token is an identifier and the following one is a
21929 colon, we are looking at the GNU designated-initializer
21931 if (cp_parser_allow_gnu_extensions_p (parser
)
21932 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
21933 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
21935 /* Warn the user that they are using an extension. */
21936 pedwarn (input_location
, OPT_Wpedantic
,
21937 "ISO C++ does not allow designated initializers");
21938 /* Consume the identifier. */
21939 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21940 /* Consume the `:'. */
21941 cp_lexer_consume_token (parser
->lexer
);
21943 /* Also handle the C99 syntax, '. id ='. */
21944 else if (cp_parser_allow_gnu_extensions_p (parser
)
21945 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
21946 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
21947 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
21949 /* Warn the user that they are using an extension. */
21950 pedwarn (input_location
, OPT_Wpedantic
,
21951 "ISO C++ does not allow C99 designated initializers");
21952 /* Consume the `.'. */
21953 cp_lexer_consume_token (parser
->lexer
);
21954 /* Consume the identifier. */
21955 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21956 /* Consume the `='. */
21957 cp_lexer_consume_token (parser
->lexer
);
21959 /* Also handle C99 array designators, '[ const ] ='. */
21960 else if (cp_parser_allow_gnu_extensions_p (parser
)
21961 && !c_dialect_objc ()
21962 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21964 /* In C++11, [ could start a lambda-introducer. */
21965 bool non_const
= false;
21967 cp_parser_parse_tentatively (parser
);
21969 if (!cp_parser_array_designator_p (parser
))
21971 cp_parser_simulate_error (parser
);
21972 designator
= NULL_TREE
;
21976 designator
= cp_parser_constant_expression (parser
, true,
21978 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21979 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21982 if (!cp_parser_parse_definitely (parser
))
21983 designator
= NULL_TREE
;
21984 else if (non_const
)
21985 require_potential_rvalue_constant_expression (designator
);
21988 designator
= NULL_TREE
;
21990 /* Parse the initializer. */
21991 initializer
= cp_parser_initializer_clause (parser
,
21992 &clause_non_constant_p
);
21993 /* If any clause is non-constant, so is the entire initializer. */
21994 if (clause_non_constant_p
)
21995 *non_constant_p
= true;
21997 /* If we have an ellipsis, this is an initializer pack
21999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22001 /* Consume the `...'. */
22002 cp_lexer_consume_token (parser
->lexer
);
22004 /* Turn the initializer into an initializer expansion. */
22005 initializer
= make_pack_expansion (initializer
);
22008 /* Add it to the vector. */
22009 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22011 /* If the next token is not a comma, we have reached the end of
22013 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22016 /* Peek at the next token. */
22017 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22018 /* If the next token is a `}', then we're still done. An
22019 initializer-clause can have a trailing `,' after the
22020 initializer-list and before the closing `}'. */
22021 if (token
->type
== CPP_CLOSE_BRACE
)
22024 /* Consume the `,' token. */
22025 cp_lexer_consume_token (parser
->lexer
);
22031 /* Classes [gram.class] */
22033 /* Parse a class-name.
22039 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22040 to indicate that names looked up in dependent types should be
22041 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22042 keyword has been used to indicate that the name that appears next
22043 is a template. TAG_TYPE indicates the explicit tag given before
22044 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22045 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22046 is the class being defined in a class-head. If ENUM_OK is TRUE,
22047 enum-names are also accepted.
22049 Returns the TYPE_DECL representing the class. */
22052 cp_parser_class_name (cp_parser
*parser
,
22053 bool typename_keyword_p
,
22054 bool template_keyword_p
,
22055 enum tag_types tag_type
,
22056 bool check_dependency_p
,
22058 bool is_declaration
,
22065 tree identifier
= NULL_TREE
;
22067 /* All class-names start with an identifier. */
22068 token
= cp_lexer_peek_token (parser
->lexer
);
22069 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22071 cp_parser_error (parser
, "expected class-name");
22072 return error_mark_node
;
22075 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22076 to a template-id, so we save it here. */
22077 scope
= parser
->scope
;
22078 if (scope
== error_mark_node
)
22079 return error_mark_node
;
22081 /* Any name names a type if we're following the `typename' keyword
22082 in a qualified name where the enclosing scope is type-dependent. */
22083 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22084 && dependent_type_p (scope
));
22085 /* Handle the common case (an identifier, but not a template-id)
22087 if (token
->type
== CPP_NAME
22088 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22090 cp_token
*identifier_token
;
22093 /* Look for the identifier. */
22094 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22095 ambiguous_p
= identifier_token
->error_reported
;
22096 identifier
= cp_parser_identifier (parser
);
22097 /* If the next token isn't an identifier, we are certainly not
22098 looking at a class-name. */
22099 if (identifier
== error_mark_node
)
22100 decl
= error_mark_node
;
22101 /* If we know this is a type-name, there's no need to look it
22103 else if (typename_p
)
22107 tree ambiguous_decls
;
22108 /* If we already know that this lookup is ambiguous, then
22109 we've already issued an error message; there's no reason
22113 cp_parser_simulate_error (parser
);
22114 return error_mark_node
;
22116 /* If the next token is a `::', then the name must be a type
22119 [basic.lookup.qual]
22121 During the lookup for a name preceding the :: scope
22122 resolution operator, object, function, and enumerator
22123 names are ignored. */
22124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22125 tag_type
= scope_type
;
22126 /* Look up the name. */
22127 decl
= cp_parser_lookup_name (parser
, identifier
,
22129 /*is_template=*/false,
22130 /*is_namespace=*/false,
22131 check_dependency_p
,
22133 identifier_token
->location
);
22134 if (ambiguous_decls
)
22136 if (cp_parser_parsing_tentatively (parser
))
22137 cp_parser_simulate_error (parser
);
22138 return error_mark_node
;
22144 /* Try a template-id. */
22145 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22146 check_dependency_p
,
22149 if (decl
== error_mark_node
)
22150 return error_mark_node
;
22153 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22155 /* If this is a typename, create a TYPENAME_TYPE. */
22156 if (typename_p
&& decl
!= error_mark_node
)
22158 decl
= make_typename_type (scope
, decl
, typename_type
,
22159 /*complain=*/tf_error
);
22160 if (decl
!= error_mark_node
)
22161 decl
= TYPE_NAME (decl
);
22164 decl
= strip_using_decl (decl
);
22166 /* Check to see that it is really the name of a class. */
22167 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22168 && identifier_p (TREE_OPERAND (decl
, 0))
22169 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22170 /* Situations like this:
22172 template <typename T> struct A {
22173 typename T::template X<int>::I i;
22176 are problematic. Is `T::template X<int>' a class-name? The
22177 standard does not seem to be definitive, but there is no other
22178 valid interpretation of the following `::'. Therefore, those
22179 names are considered class-names. */
22181 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22182 if (decl
!= error_mark_node
)
22183 decl
= TYPE_NAME (decl
);
22185 else if (TREE_CODE (decl
) != TYPE_DECL
22186 || TREE_TYPE (decl
) == error_mark_node
22187 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22188 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22189 /* In Objective-C 2.0, a classname followed by '.' starts a
22190 dot-syntax expression, and it's not a type-name. */
22191 || (c_dialect_objc ()
22192 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22193 && objc_is_class_name (decl
)))
22194 decl
= error_mark_node
;
22196 if (decl
== error_mark_node
)
22197 cp_parser_error (parser
, "expected class-name");
22198 else if (identifier
&& !parser
->scope
)
22199 maybe_note_name_used_in_class (identifier
, decl
);
22204 /* Parse a class-specifier.
22207 class-head { member-specification [opt] }
22209 Returns the TREE_TYPE representing the class. */
22212 cp_parser_class_specifier_1 (cp_parser
* parser
)
22215 tree attributes
= NULL_TREE
;
22216 bool nested_name_specifier_p
;
22217 unsigned saved_num_template_parameter_lists
;
22218 bool saved_in_function_body
;
22219 unsigned char in_statement
;
22220 bool in_switch_statement_p
;
22221 bool saved_in_unbraced_linkage_specification_p
;
22222 tree old_scope
= NULL_TREE
;
22223 tree scope
= NULL_TREE
;
22224 cp_token
*closing_brace
;
22226 push_deferring_access_checks (dk_no_deferred
);
22228 /* Parse the class-head. */
22229 type
= cp_parser_class_head (parser
,
22230 &nested_name_specifier_p
);
22231 /* If the class-head was a semantic disaster, skip the entire body
22235 cp_parser_skip_to_end_of_block_or_statement (parser
);
22236 pop_deferring_access_checks ();
22237 return error_mark_node
;
22240 /* Look for the `{'. */
22241 matching_braces braces
;
22242 if (!braces
.require_open (parser
))
22244 pop_deferring_access_checks ();
22245 return error_mark_node
;
22248 cp_ensure_no_omp_declare_simd (parser
);
22249 cp_ensure_no_oacc_routine (parser
);
22251 /* Issue an error message if type-definitions are forbidden here. */
22252 cp_parser_check_type_definition (parser
);
22253 /* Remember that we are defining one more class. */
22254 ++parser
->num_classes_being_defined
;
22255 /* Inside the class, surrounding template-parameter-lists do not
22257 saved_num_template_parameter_lists
22258 = parser
->num_template_parameter_lists
;
22259 parser
->num_template_parameter_lists
= 0;
22260 /* We are not in a function body. */
22261 saved_in_function_body
= parser
->in_function_body
;
22262 parser
->in_function_body
= false;
22263 /* Or in a loop. */
22264 in_statement
= parser
->in_statement
;
22265 parser
->in_statement
= 0;
22266 /* Or in a switch. */
22267 in_switch_statement_p
= parser
->in_switch_statement_p
;
22268 parser
->in_switch_statement_p
= false;
22269 /* We are not immediately inside an extern "lang" block. */
22270 saved_in_unbraced_linkage_specification_p
22271 = parser
->in_unbraced_linkage_specification_p
;
22272 parser
->in_unbraced_linkage_specification_p
= false;
22274 // Associate constraints with the type.
22276 type
= associate_classtype_constraints (type
);
22278 /* Start the class. */
22279 if (nested_name_specifier_p
)
22281 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22282 old_scope
= push_inner_scope (scope
);
22284 type
= begin_class_definition (type
);
22286 if (type
== error_mark_node
)
22287 /* If the type is erroneous, skip the entire body of the class. */
22288 cp_parser_skip_to_closing_brace (parser
);
22290 /* Parse the member-specification. */
22291 cp_parser_member_specification_opt (parser
);
22293 /* Look for the trailing `}'. */
22294 closing_brace
= braces
.require_close (parser
);
22295 /* Look for trailing attributes to apply to this class. */
22296 if (cp_parser_allow_gnu_extensions_p (parser
))
22297 attributes
= cp_parser_gnu_attributes_opt (parser
);
22298 if (type
!= error_mark_node
)
22299 type
= finish_struct (type
, attributes
);
22300 if (nested_name_specifier_p
)
22301 pop_inner_scope (old_scope
, scope
);
22303 /* We've finished a type definition. Check for the common syntax
22304 error of forgetting a semicolon after the definition. We need to
22305 be careful, as we can't just check for not-a-semicolon and be done
22306 with it; the user might have typed:
22308 class X { } c = ...;
22309 class X { } *p = ...;
22311 and so forth. Instead, enumerate all the possible tokens that
22312 might follow this production; if we don't see one of them, then
22313 complain and silently insert the semicolon. */
22315 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22316 bool want_semicolon
= true;
22318 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22319 /* Don't try to parse c++11 attributes here. As per the
22320 grammar, that should be a task for
22321 cp_parser_decl_specifier_seq. */
22322 want_semicolon
= false;
22324 switch (token
->type
)
22327 case CPP_SEMICOLON
:
22330 case CPP_OPEN_PAREN
:
22331 case CPP_CLOSE_PAREN
:
22333 want_semicolon
= false;
22336 /* While it's legal for type qualifiers and storage class
22337 specifiers to follow type definitions in the grammar, only
22338 compiler testsuites contain code like that. Assume that if
22339 we see such code, then what we're really seeing is a case
22343 const <type> var = ...;
22348 static <type> func (...) ...
22350 i.e. the qualifier or specifier applies to the next
22351 declaration. To do so, however, we need to look ahead one
22352 more token to see if *that* token is a type specifier.
22354 This code could be improved to handle:
22357 static const <type> var = ...; */
22359 if (keyword_is_decl_specifier (token
->keyword
))
22361 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22363 /* Handling user-defined types here would be nice, but very
22366 = (lookahead
->type
== CPP_KEYWORD
22367 && keyword_begins_type_specifier (lookahead
->keyword
));
22374 /* If we don't have a type, then something is very wrong and we
22375 shouldn't try to do anything clever. Likewise for not seeing the
22377 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22379 /* Locate the closing brace. */
22380 cp_token_position prev
22381 = cp_lexer_previous_token_position (parser
->lexer
);
22382 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22383 location_t loc
= prev_token
->location
;
22385 /* We want to suggest insertion of a ';' immediately *after* the
22386 closing brace, so, if we can, offset the location by 1 column. */
22387 location_t next_loc
= loc
;
22388 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22389 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22391 rich_location
richloc (line_table
, next_loc
);
22393 /* If we successfully offset the location, suggest the fix-it. */
22394 if (next_loc
!= loc
)
22395 richloc
.add_fixit_insert_before (next_loc
, ";");
22397 if (CLASSTYPE_DECLARED_CLASS (type
))
22398 error_at_rich_loc (&richloc
,
22399 "expected %<;%> after class definition");
22400 else if (TREE_CODE (type
) == RECORD_TYPE
)
22401 error_at_rich_loc (&richloc
,
22402 "expected %<;%> after struct definition");
22403 else if (TREE_CODE (type
) == UNION_TYPE
)
22404 error_at_rich_loc (&richloc
,
22405 "expected %<;%> after union definition");
22407 gcc_unreachable ();
22409 /* Unget one token and smash it to look as though we encountered
22410 a semicolon in the input stream. */
22411 cp_lexer_set_token_position (parser
->lexer
, prev
);
22412 token
= cp_lexer_peek_token (parser
->lexer
);
22413 token
->type
= CPP_SEMICOLON
;
22414 token
->keyword
= RID_MAX
;
22418 /* If this class is not itself within the scope of another class,
22419 then we need to parse the bodies of all of the queued function
22420 definitions. Note that the queued functions defined in a class
22421 are not always processed immediately following the
22422 class-specifier for that class. Consider:
22425 struct B { void f() { sizeof (A); } };
22428 If `f' were processed before the processing of `A' were
22429 completed, there would be no way to compute the size of `A'.
22430 Note that the nesting we are interested in here is lexical --
22431 not the semantic nesting given by TYPE_CONTEXT. In particular,
22434 struct A { struct B; };
22435 struct A::B { void f() { } };
22437 there is no need to delay the parsing of `A::B::f'. */
22438 if (--parser
->num_classes_being_defined
== 0)
22441 tree class_type
= NULL_TREE
;
22442 tree pushed_scope
= NULL_TREE
;
22444 cp_default_arg_entry
*e
;
22445 tree save_ccp
, save_ccr
;
22447 /* In a first pass, parse default arguments to the functions.
22448 Then, in a second pass, parse the bodies of the functions.
22449 This two-phased approach handles cases like:
22457 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22460 /* If there are default arguments that have not yet been processed,
22461 take care of them now. */
22462 if (class_type
!= e
->class_type
)
22465 pop_scope (pushed_scope
);
22466 class_type
= e
->class_type
;
22467 pushed_scope
= push_scope (class_type
);
22469 /* Make sure that any template parameters are in scope. */
22470 maybe_begin_member_template_processing (decl
);
22471 /* Parse the default argument expressions. */
22472 cp_parser_late_parsing_default_args (parser
, decl
);
22473 /* Remove any template parameters from the symbol table. */
22474 maybe_end_member_template_processing ();
22476 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22477 /* Now parse any NSDMIs. */
22478 save_ccp
= current_class_ptr
;
22479 save_ccr
= current_class_ref
;
22480 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22482 if (class_type
!= DECL_CONTEXT (decl
))
22485 pop_scope (pushed_scope
);
22486 class_type
= DECL_CONTEXT (decl
);
22487 pushed_scope
= push_scope (class_type
);
22489 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22490 cp_parser_late_parsing_nsdmi (parser
, decl
);
22492 vec_safe_truncate (unparsed_nsdmis
, 0);
22493 current_class_ptr
= save_ccp
;
22494 current_class_ref
= save_ccr
;
22496 pop_scope (pushed_scope
);
22498 /* Now do some post-NSDMI bookkeeping. */
22499 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22500 after_nsdmi_defaulted_late_checks (class_type
);
22501 vec_safe_truncate (unparsed_classes
, 0);
22502 after_nsdmi_defaulted_late_checks (type
);
22504 /* Now parse the body of the functions. */
22507 /* OpenMP UDRs need to be parsed before all other functions. */
22508 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22509 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22510 cp_parser_late_parsing_for_member (parser
, decl
);
22511 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22512 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22513 cp_parser_late_parsing_for_member (parser
, decl
);
22516 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22517 cp_parser_late_parsing_for_member (parser
, decl
);
22518 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22521 vec_safe_push (unparsed_classes
, type
);
22523 /* Put back any saved access checks. */
22524 pop_deferring_access_checks ();
22526 /* Restore saved state. */
22527 parser
->in_switch_statement_p
= in_switch_statement_p
;
22528 parser
->in_statement
= in_statement
;
22529 parser
->in_function_body
= saved_in_function_body
;
22530 parser
->num_template_parameter_lists
22531 = saved_num_template_parameter_lists
;
22532 parser
->in_unbraced_linkage_specification_p
22533 = saved_in_unbraced_linkage_specification_p
;
22539 cp_parser_class_specifier (cp_parser
* parser
)
22542 timevar_push (TV_PARSE_STRUCT
);
22543 ret
= cp_parser_class_specifier_1 (parser
);
22544 timevar_pop (TV_PARSE_STRUCT
);
22548 /* Parse a class-head.
22551 class-key identifier [opt] base-clause [opt]
22552 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22553 class-key nested-name-specifier [opt] template-id
22556 class-virt-specifier:
22560 class-key attributes identifier [opt] base-clause [opt]
22561 class-key attributes nested-name-specifier identifier base-clause [opt]
22562 class-key attributes nested-name-specifier [opt] template-id
22565 Upon return BASES is initialized to the list of base classes (or
22566 NULL, if there are none) in the same form returned by
22567 cp_parser_base_clause.
22569 Returns the TYPE of the indicated class. Sets
22570 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22571 involving a nested-name-specifier was used, and FALSE otherwise.
22573 Returns error_mark_node if this is not a class-head.
22575 Returns NULL_TREE if the class-head is syntactically valid, but
22576 semantically invalid in a way that means we should skip the entire
22577 body of the class. */
22580 cp_parser_class_head (cp_parser
* parser
,
22581 bool* nested_name_specifier_p
)
22583 tree nested_name_specifier
;
22584 enum tag_types class_key
;
22585 tree id
= NULL_TREE
;
22586 tree type
= NULL_TREE
;
22589 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22590 bool template_id_p
= false;
22591 bool qualified_p
= false;
22592 bool invalid_nested_name_p
= false;
22593 bool invalid_explicit_specialization_p
= false;
22594 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22595 tree pushed_scope
= NULL_TREE
;
22596 unsigned num_templates
;
22597 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22598 /* Assume no nested-name-specifier will be present. */
22599 *nested_name_specifier_p
= false;
22600 /* Assume no template parameter lists will be used in defining the
22603 parser
->colon_corrects_to_scope_p
= false;
22605 /* Look for the class-key. */
22606 class_key
= cp_parser_class_key (parser
);
22607 if (class_key
== none_type
)
22608 return error_mark_node
;
22610 location_t class_head_start_location
= input_location
;
22612 /* Parse the attributes. */
22613 attributes
= cp_parser_attributes_opt (parser
);
22615 /* If the next token is `::', that is invalid -- but sometimes
22616 people do try to write:
22620 Handle this gracefully by accepting the extra qualifier, and then
22621 issuing an error about it later if this really is a
22622 class-head. If it turns out just to be an elaborated type
22623 specifier, remain silent. */
22624 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22625 qualified_p
= true;
22627 push_deferring_access_checks (dk_no_check
);
22629 /* Determine the name of the class. Begin by looking for an
22630 optional nested-name-specifier. */
22631 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22632 nested_name_specifier
22633 = cp_parser_nested_name_specifier_opt (parser
,
22634 /*typename_keyword_p=*/false,
22635 /*check_dependency_p=*/false,
22637 /*is_declaration=*/false);
22638 /* If there was a nested-name-specifier, then there *must* be an
22641 cp_token
*bad_template_keyword
= NULL
;
22643 if (nested_name_specifier
)
22645 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22646 /* Although the grammar says `identifier', it really means
22647 `class-name' or `template-name'. You are only allowed to
22648 define a class that has already been declared with this
22651 The proposed resolution for Core Issue 180 says that wherever
22652 you see `class T::X' you should treat `X' as a type-name.
22654 It is OK to define an inaccessible class; for example:
22656 class A { class B; };
22659 We do not know if we will see a class-name, or a
22660 template-name. We look for a class-name first, in case the
22661 class-name is a template-id; if we looked for the
22662 template-name first we would stop after the template-name. */
22663 cp_parser_parse_tentatively (parser
);
22664 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22665 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22666 type
= cp_parser_class_name (parser
,
22667 /*typename_keyword_p=*/false,
22668 /*template_keyword_p=*/false,
22670 /*check_dependency_p=*/false,
22671 /*class_head_p=*/true,
22672 /*is_declaration=*/false);
22673 /* If that didn't work, ignore the nested-name-specifier. */
22674 if (!cp_parser_parse_definitely (parser
))
22676 invalid_nested_name_p
= true;
22677 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22678 id
= cp_parser_identifier (parser
);
22679 if (id
== error_mark_node
)
22682 /* If we could not find a corresponding TYPE, treat this
22683 declaration like an unqualified declaration. */
22684 if (type
== error_mark_node
)
22685 nested_name_specifier
= NULL_TREE
;
22686 /* Otherwise, count the number of templates used in TYPE and its
22687 containing scopes. */
22692 for (scope
= TREE_TYPE (type
);
22693 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22694 scope
= get_containing_scope (scope
))
22696 && CLASS_TYPE_P (scope
)
22697 && CLASSTYPE_TEMPLATE_INFO (scope
)
22698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22699 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22700 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22704 /* Otherwise, the identifier is optional. */
22707 /* We don't know whether what comes next is a template-id,
22708 an identifier, or nothing at all. */
22709 cp_parser_parse_tentatively (parser
);
22710 /* Check for a template-id. */
22711 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22712 id
= cp_parser_template_id (parser
,
22713 /*template_keyword_p=*/false,
22714 /*check_dependency_p=*/true,
22716 /*is_declaration=*/true);
22717 /* If that didn't work, it could still be an identifier. */
22718 if (!cp_parser_parse_definitely (parser
))
22720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22722 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22723 id
= cp_parser_identifier (parser
);
22730 template_id_p
= true;
22735 pop_deferring_access_checks ();
22739 cp_parser_check_for_invalid_template_id (parser
, id
,
22741 type_start_token
->location
);
22743 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22745 /* If it's not a `:' or a `{' then we can't really be looking at a
22746 class-head, since a class-head only appears as part of a
22747 class-specifier. We have to detect this situation before calling
22748 xref_tag, since that has irreversible side-effects. */
22749 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22751 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22752 type
= error_mark_node
;
22756 /* At this point, we're going ahead with the class-specifier, even
22757 if some other problem occurs. */
22758 cp_parser_commit_to_tentative_parse (parser
);
22759 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22761 cp_parser_error (parser
,
22762 "cannot specify %<override%> for a class");
22763 type
= error_mark_node
;
22766 /* Issue the error about the overly-qualified name now. */
22769 cp_parser_error (parser
,
22770 "global qualification of class name is invalid");
22771 type
= error_mark_node
;
22774 else if (invalid_nested_name_p
)
22776 cp_parser_error (parser
,
22777 "qualified name does not name a class");
22778 type
= error_mark_node
;
22781 else if (nested_name_specifier
)
22785 if (bad_template_keyword
)
22786 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22787 keyword template shall not appear at the top level. */
22788 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
22789 "keyword %<template%> not allowed in class-head-name");
22791 /* Reject typedef-names in class heads. */
22792 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22794 error_at (type_start_token
->location
,
22795 "invalid class name in declaration of %qD",
22801 /* Figure out in what scope the declaration is being placed. */
22802 scope
= current_scope ();
22803 /* If that scope does not contain the scope in which the
22804 class was originally declared, the program is invalid. */
22805 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22807 if (at_namespace_scope_p ())
22808 error_at (type_start_token
->location
,
22809 "declaration of %qD in namespace %qD which does not "
22811 type
, scope
, nested_name_specifier
);
22813 error_at (type_start_token
->location
,
22814 "declaration of %qD in %qD which does not enclose %qD",
22815 type
, scope
, nested_name_specifier
);
22821 A declarator-id shall not be qualified except for the
22822 definition of a ... nested class outside of its class
22823 ... [or] the definition or explicit instantiation of a
22824 class member of a namespace outside of its namespace. */
22825 if (scope
== nested_name_specifier
)
22827 permerror (nested_name_specifier_token_start
->location
,
22828 "extra qualification not allowed");
22829 nested_name_specifier
= NULL_TREE
;
22833 /* An explicit-specialization must be preceded by "template <>". If
22834 it is not, try to recover gracefully. */
22835 if (at_namespace_scope_p ()
22836 && parser
->num_template_parameter_lists
== 0
22837 && !processing_template_parmlist
22840 /* Build a location of this form:
22841 struct typename <ARGS>
22842 ^~~~~~~~~~~~~~~~~~~~~~
22843 with caret==start at the start token, and
22844 finishing at the end of the type. */
22845 location_t reported_loc
22846 = make_location (class_head_start_location
,
22847 class_head_start_location
,
22848 get_finish (type_start_token
->location
));
22849 rich_location
richloc (line_table
, reported_loc
);
22850 richloc
.add_fixit_insert_before (class_head_start_location
,
22854 "an explicit specialization must be preceded by %<template <>%>");
22855 invalid_explicit_specialization_p
= true;
22856 /* Take the same action that would have been taken by
22857 cp_parser_explicit_specialization. */
22858 ++parser
->num_template_parameter_lists
;
22859 begin_specialization ();
22861 /* There must be no "return" statements between this point and the
22862 end of this function; set "type "to the correct return value and
22863 use "goto done;" to return. */
22864 /* Make sure that the right number of template parameters were
22866 if (!cp_parser_check_template_parameters (parser
, num_templates
,
22867 type_start_token
->location
,
22868 /*declarator=*/NULL
))
22870 /* If something went wrong, there is no point in even trying to
22871 process the class-definition. */
22876 /* Look up the type. */
22879 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
22880 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
22881 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
22883 error_at (type_start_token
->location
,
22884 "function template %qD redeclared as a class template", id
);
22885 type
= error_mark_node
;
22889 type
= TREE_TYPE (id
);
22890 type
= maybe_process_partial_specialization (type
);
22892 /* Check the scope while we still know whether or not we had a
22893 nested-name-specifier. */
22894 if (type
!= error_mark_node
)
22895 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
22897 if (nested_name_specifier
)
22898 pushed_scope
= push_scope (nested_name_specifier
);
22900 else if (nested_name_specifier
)
22906 template <typename T> struct S { struct T };
22907 template <typename T> struct S<T>::T { };
22909 we will get a TYPENAME_TYPE when processing the definition of
22910 `S::T'. We need to resolve it to the actual type before we
22911 try to define it. */
22912 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
22914 class_type
= resolve_typename_type (TREE_TYPE (type
),
22915 /*only_current_p=*/false);
22916 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
22917 type
= TYPE_NAME (class_type
);
22920 cp_parser_error (parser
, "could not resolve typename type");
22921 type
= error_mark_node
;
22925 if (maybe_process_partial_specialization (TREE_TYPE (type
))
22926 == error_mark_node
)
22932 class_type
= current_class_type
;
22933 /* Enter the scope indicated by the nested-name-specifier. */
22934 pushed_scope
= push_scope (nested_name_specifier
);
22935 /* Get the canonical version of this type. */
22936 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
22937 /* Call push_template_decl if it seems like we should be defining a
22938 template either from the template headers or the type we're
22939 defining, so that we diagnose both extra and missing headers. */
22940 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22941 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
22942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
22944 type
= push_template_decl (type
);
22945 if (type
== error_mark_node
)
22952 type
= TREE_TYPE (type
);
22953 *nested_name_specifier_p
= true;
22955 else /* The name is not a nested name. */
22957 /* If the class was unnamed, create a dummy name. */
22959 id
= make_anon_name ();
22960 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
22961 ? ts_within_enclosing_non_class
22963 type
= xref_tag (class_key
, id
, tag_scope
,
22964 parser
->num_template_parameter_lists
);
22967 /* Indicate whether this class was declared as a `class' or as a
22969 if (TREE_CODE (type
) == RECORD_TYPE
)
22970 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
22971 cp_parser_check_class_key (class_key
, type
);
22973 /* If this type was already complete, and we see another definition,
22974 that's an error. */
22975 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
22977 error_at (type_start_token
->location
, "redefinition of %q#T",
22979 inform (location_of (type
), "previous definition of %q#T",
22984 else if (type
== error_mark_node
)
22989 /* Apply attributes now, before any use of the class as a template
22990 argument in its base list. */
22991 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
22992 fixup_attribute_variants (type
);
22995 /* We will have entered the scope containing the class; the names of
22996 base classes should be looked up in that context. For example:
22998 struct A { struct B {}; struct C; };
22999 struct A::C : B {};
23003 /* Get the list of base-classes, if there is one. */
23004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23006 /* PR59482: enter the class scope so that base-specifiers are looked
23010 bases
= cp_parser_base_clause (parser
);
23011 /* PR59482: get out of the previously pushed class scope so that the
23012 subsequent pops pop the right thing. */
23019 /* If we're really defining a class, process the base classes.
23020 If they're invalid, fail. */
23021 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23022 xref_basetypes (type
, bases
);
23025 /* Leave the scope given by the nested-name-specifier. We will
23026 enter the class scope itself while processing the members. */
23028 pop_scope (pushed_scope
);
23030 if (invalid_explicit_specialization_p
)
23032 end_specialization ();
23033 --parser
->num_template_parameter_lists
;
23037 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23038 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23039 CLASSTYPE_FINAL (type
) = 1;
23041 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23045 /* Parse a class-key.
23052 Returns the kind of class-key specified, or none_type to indicate
23055 static enum tag_types
23056 cp_parser_class_key (cp_parser
* parser
)
23059 enum tag_types tag_type
;
23061 /* Look for the class-key. */
23062 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23066 /* Check to see if the TOKEN is a class-key. */
23067 tag_type
= cp_parser_token_is_class_key (token
);
23069 cp_parser_error (parser
, "expected class-key");
23073 /* Parse a type-parameter-key.
23075 type-parameter-key:
23081 cp_parser_type_parameter_key (cp_parser
* parser
)
23083 /* Look for the type-parameter-key. */
23084 enum tag_types tag_type
= none_type
;
23085 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23086 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23088 cp_lexer_consume_token (parser
->lexer
);
23089 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23090 /* typename is not allowed in a template template parameter
23091 by the standard until C++17. */
23092 pedwarn (token
->location
, OPT_Wpedantic
,
23093 "ISO C++ forbids typename key in template template parameter;"
23094 " use -std=c++17 or -std=gnu++17");
23097 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23102 /* Parse an (optional) member-specification.
23104 member-specification:
23105 member-declaration member-specification [opt]
23106 access-specifier : member-specification [opt] */
23109 cp_parser_member_specification_opt (cp_parser
* parser
)
23116 /* Peek at the next token. */
23117 token
= cp_lexer_peek_token (parser
->lexer
);
23118 /* If it's a `}', or EOF then we've seen all the members. */
23119 if (token
->type
== CPP_CLOSE_BRACE
23120 || token
->type
== CPP_EOF
23121 || token
->type
== CPP_PRAGMA_EOL
)
23124 /* See if this token is a keyword. */
23125 keyword
= token
->keyword
;
23129 case RID_PROTECTED
:
23131 /* Consume the access-specifier. */
23132 cp_lexer_consume_token (parser
->lexer
);
23133 /* Remember which access-specifier is active. */
23134 current_access_specifier
= token
->u
.value
;
23135 /* Look for the `:'. */
23136 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23140 /* Accept #pragmas at class scope. */
23141 if (token
->type
== CPP_PRAGMA
)
23143 cp_parser_pragma (parser
, pragma_member
, NULL
);
23147 /* Otherwise, the next construction must be a
23148 member-declaration. */
23149 cp_parser_member_declaration (parser
);
23154 /* Parse a member-declaration.
23156 member-declaration:
23157 decl-specifier-seq [opt] member-declarator-list [opt] ;
23158 function-definition ; [opt]
23159 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23161 template-declaration
23164 member-declarator-list:
23166 member-declarator-list , member-declarator
23169 declarator pure-specifier [opt]
23170 declarator constant-initializer [opt]
23171 identifier [opt] : constant-expression
23175 member-declaration:
23176 __extension__ member-declaration
23179 declarator attributes [opt] pure-specifier [opt]
23180 declarator attributes [opt] constant-initializer [opt]
23181 identifier [opt] attributes [opt] : constant-expression
23185 member-declaration:
23186 static_assert-declaration */
23189 cp_parser_member_declaration (cp_parser
* parser
)
23191 cp_decl_specifier_seq decl_specifiers
;
23192 tree prefix_attributes
;
23194 int declares_class_or_enum
;
23196 cp_token
*token
= NULL
;
23197 cp_token
*decl_spec_token_start
= NULL
;
23198 cp_token
*initializer_token_start
= NULL
;
23199 int saved_pedantic
;
23200 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23202 /* Check for the `__extension__' keyword. */
23203 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23206 cp_parser_member_declaration (parser
);
23207 /* Restore the old value of the PEDANTIC flag. */
23208 pedantic
= saved_pedantic
;
23213 /* Check for a template-declaration. */
23214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23216 /* An explicit specialization here is an error condition, and we
23217 expect the specialization handler to detect and report this. */
23218 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23219 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23220 cp_parser_explicit_specialization (parser
);
23222 cp_parser_template_declaration (parser
, /*member_p=*/true);
23226 /* Check for a template introduction. */
23227 else if (cp_parser_template_declaration_after_export (parser
, true))
23230 /* Check for a using-declaration. */
23231 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23233 if (cxx_dialect
< cxx11
)
23235 /* Parse the using-declaration. */
23236 cp_parser_using_declaration (parser
,
23237 /*access_declaration_p=*/false);
23243 bool alias_decl_expected
;
23244 cp_parser_parse_tentatively (parser
);
23245 decl
= cp_parser_alias_declaration (parser
);
23246 /* Note that if we actually see the '=' token after the
23247 identifier, cp_parser_alias_declaration commits the
23248 tentative parse. In that case, we really expect an
23249 alias-declaration. Otherwise, we expect a using
23251 alias_decl_expected
=
23252 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23253 cp_parser_parse_definitely (parser
);
23255 if (alias_decl_expected
)
23256 finish_member_declaration (decl
);
23258 cp_parser_using_declaration (parser
,
23259 /*access_declaration_p=*/false);
23264 /* Check for @defs. */
23265 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23268 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23269 ivar
= ivar_chains
;
23273 ivar
= TREE_CHAIN (member
);
23274 TREE_CHAIN (member
) = NULL_TREE
;
23275 finish_member_declaration (member
);
23280 /* If the next token is `static_assert' we have a static assertion. */
23281 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23283 cp_parser_static_assert (parser
, /*member_p=*/true);
23287 parser
->colon_corrects_to_scope_p
= false;
23289 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23292 /* Parse the decl-specifier-seq. */
23293 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23294 cp_parser_decl_specifier_seq (parser
,
23295 CP_PARSER_FLAGS_OPTIONAL
,
23297 &declares_class_or_enum
);
23298 /* Check for an invalid type-name. */
23299 if (!decl_specifiers
.any_type_specifiers_p
23300 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23302 /* If there is no declarator, then the decl-specifier-seq should
23304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23306 /* If there was no decl-specifier-seq, and the next token is a
23307 `;', then we have something like:
23313 Each member-declaration shall declare at least one member
23314 name of the class. */
23315 if (!decl_specifiers
.any_specifiers_p
)
23317 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23318 if (!in_system_header_at (token
->location
))
23320 gcc_rich_location
richloc (token
->location
);
23321 richloc
.add_fixit_remove ();
23322 pedwarn_at_rich_loc (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23329 /* See if this declaration is a friend. */
23330 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23331 /* If there were decl-specifiers, check to see if there was
23332 a class-declaration. */
23333 type
= check_tag_decl (&decl_specifiers
,
23334 /*explicit_type_instantiation_p=*/false);
23335 /* Nested classes have already been added to the class, but
23336 a `friend' needs to be explicitly registered. */
23339 /* If the `friend' keyword was present, the friend must
23340 be introduced with a class-key. */
23341 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23342 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23343 "in C++03 a class-key must be used "
23344 "when declaring a friend");
23347 template <typename T> struct A {
23348 friend struct A<T>::B;
23351 A<T>::B will be represented by a TYPENAME_TYPE, and
23352 therefore not recognized by check_tag_decl. */
23355 type
= decl_specifiers
.type
;
23356 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23357 type
= TREE_TYPE (type
);
23359 if (!type
|| !TYPE_P (type
))
23360 error_at (decl_spec_token_start
->location
,
23361 "friend declaration does not name a class or "
23364 make_friend_class (current_class_type
, type
,
23365 /*complain=*/true);
23367 /* If there is no TYPE, an error message will already have
23369 else if (!type
|| type
== error_mark_node
)
23371 /* An anonymous aggregate has to be handled specially; such
23372 a declaration really declares a data member (with a
23373 particular type), as opposed to a nested class. */
23374 else if (ANON_AGGR_TYPE_P (type
))
23377 if (decl_specifiers
.storage_class
!= sc_none
)
23378 error_at (decl_spec_token_start
->location
,
23379 "a storage class on an anonymous aggregate "
23380 "in class scope is not allowed");
23382 /* Remove constructors and such from TYPE, now that we
23383 know it is an anonymous aggregate. */
23384 fixup_anonymous_aggr (type
);
23385 /* And make the corresponding data member. */
23386 decl
= build_decl (decl_spec_token_start
->location
,
23387 FIELD_DECL
, NULL_TREE
, type
);
23388 /* Add it to the class. */
23389 finish_member_declaration (decl
);
23392 cp_parser_check_access_in_redeclaration
23394 decl_spec_token_start
->location
);
23399 bool assume_semicolon
= false;
23401 /* Clear attributes from the decl_specifiers but keep them
23402 around as prefix attributes that apply them to the entity
23404 prefix_attributes
= decl_specifiers
.attributes
;
23405 decl_specifiers
.attributes
= NULL_TREE
;
23407 /* See if these declarations will be friends. */
23408 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23410 /* Keep going until we hit the `;' at the end of the
23412 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23414 tree attributes
= NULL_TREE
;
23415 tree first_attribute
;
23417 /* Peek at the next token. */
23418 token
= cp_lexer_peek_token (parser
->lexer
);
23420 /* Check for a bitfield declaration. */
23421 if (token
->type
== CPP_COLON
23422 || (token
->type
== CPP_NAME
23423 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23429 /* Get the name of the bitfield. Note that we cannot just
23430 check TOKEN here because it may have been invalidated by
23431 the call to cp_lexer_peek_nth_token above. */
23432 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
23433 identifier
= cp_parser_identifier (parser
);
23435 identifier
= NULL_TREE
;
23437 /* Consume the `:' token. */
23438 cp_lexer_consume_token (parser
->lexer
);
23439 /* Get the width of the bitfield. */
23441 = cp_parser_constant_expression (parser
);
23443 /* Look for attributes that apply to the bitfield. */
23444 attributes
= cp_parser_attributes_opt (parser
);
23445 /* Remember which attributes are prefix attributes and
23447 first_attribute
= attributes
;
23448 /* Combine the attributes. */
23449 attributes
= chainon (prefix_attributes
, attributes
);
23451 /* Create the bitfield declaration. */
23452 decl
= grokbitfield (identifier
23453 ? make_id_declarator (NULL_TREE
,
23463 cp_declarator
*declarator
;
23465 tree asm_specification
;
23466 int ctor_dtor_or_conv_p
;
23468 /* Parse the declarator. */
23470 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23471 &ctor_dtor_or_conv_p
,
23472 /*parenthesized_p=*/NULL
,
23476 /* If something went wrong parsing the declarator, make sure
23477 that we at least consume some tokens. */
23478 if (declarator
== cp_error_declarator
)
23480 /* Skip to the end of the statement. */
23481 cp_parser_skip_to_end_of_statement (parser
);
23482 /* If the next token is not a semicolon, that is
23483 probably because we just skipped over the body of
23484 a function. So, we consume a semicolon if
23485 present, but do not issue an error message if it
23487 if (cp_lexer_next_token_is (parser
->lexer
,
23489 cp_lexer_consume_token (parser
->lexer
);
23493 if (declares_class_or_enum
& 2)
23494 cp_parser_check_for_definition_in_return_type
23495 (declarator
, decl_specifiers
.type
,
23496 decl_specifiers
.locations
[ds_type_spec
]);
23498 /* Look for an asm-specification. */
23499 asm_specification
= cp_parser_asm_specification_opt (parser
);
23500 /* Look for attributes that apply to the declaration. */
23501 attributes
= cp_parser_attributes_opt (parser
);
23502 /* Remember which attributes are prefix attributes and
23504 first_attribute
= attributes
;
23505 /* Combine the attributes. */
23506 attributes
= chainon (prefix_attributes
, attributes
);
23508 /* If it's an `=', then we have a constant-initializer or a
23509 pure-specifier. It is not correct to parse the
23510 initializer before registering the member declaration
23511 since the member declaration should be in scope while
23512 its initializer is processed. However, the rest of the
23513 front end does not yet provide an interface that allows
23514 us to handle this correctly. */
23515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23519 A pure-specifier shall be used only in the declaration of
23520 a virtual function.
23522 A member-declarator can contain a constant-initializer
23523 only if it declares a static member of integral or
23526 Therefore, if the DECLARATOR is for a function, we look
23527 for a pure-specifier; otherwise, we look for a
23528 constant-initializer. When we call `grokfield', it will
23529 perform more stringent semantics checks. */
23530 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23531 if (function_declarator_p (declarator
)
23532 || (decl_specifiers
.type
23533 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23534 && declarator
->kind
== cdk_id
23535 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23536 == FUNCTION_TYPE
)))
23537 initializer
= cp_parser_pure_specifier (parser
);
23538 else if (decl_specifiers
.storage_class
!= sc_static
)
23539 initializer
= cp_parser_save_nsdmi (parser
);
23540 else if (cxx_dialect
>= cxx11
)
23543 /* Don't require a constant rvalue in C++11, since we
23544 might want a reference constant. We'll enforce
23545 constancy later. */
23546 cp_lexer_consume_token (parser
->lexer
);
23547 /* Parse the initializer. */
23548 initializer
= cp_parser_initializer_clause (parser
,
23552 /* Parse the initializer. */
23553 initializer
= cp_parser_constant_initializer (parser
);
23555 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23556 && !function_declarator_p (declarator
))
23559 if (decl_specifiers
.storage_class
!= sc_static
)
23560 initializer
= cp_parser_save_nsdmi (parser
);
23562 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23564 /* Otherwise, there is no initializer. */
23566 initializer
= NULL_TREE
;
23568 /* See if we are probably looking at a function
23569 definition. We are certainly not looking at a
23570 member-declarator. Calling `grokfield' has
23571 side-effects, so we must not do it unless we are sure
23572 that we are looking at a member-declarator. */
23573 if (cp_parser_token_starts_function_definition_p
23574 (cp_lexer_peek_token (parser
->lexer
)))
23576 /* The grammar does not allow a pure-specifier to be
23577 used when a member function is defined. (It is
23578 possible that this fact is an oversight in the
23579 standard, since a pure function may be defined
23580 outside of the class-specifier. */
23581 if (initializer
&& initializer_token_start
)
23582 error_at (initializer_token_start
->location
,
23583 "pure-specifier on function-definition");
23584 decl
= cp_parser_save_member_function_body (parser
,
23588 if (parser
->fully_implicit_function_template_p
)
23589 decl
= finish_fully_implicit_template (parser
, decl
);
23590 /* If the member was not a friend, declare it here. */
23592 finish_member_declaration (decl
);
23593 /* Peek at the next token. */
23594 token
= cp_lexer_peek_token (parser
->lexer
);
23595 /* If the next token is a semicolon, consume it. */
23596 if (token
->type
== CPP_SEMICOLON
)
23598 location_t semicolon_loc
23599 = cp_lexer_consume_token (parser
->lexer
)->location
;
23600 gcc_rich_location
richloc (semicolon_loc
);
23601 richloc
.add_fixit_remove ();
23602 warning_at_rich_loc (&richloc
, OPT_Wextra_semi
,
23603 "extra %<;%> after in-class "
23604 "function definition");
23609 if (declarator
->kind
== cdk_function
)
23610 declarator
->id_loc
= token
->location
;
23611 /* Create the declaration. */
23612 decl
= grokfield (declarator
, &decl_specifiers
,
23613 initializer
, /*init_const_expr_p=*/true,
23614 asm_specification
, attributes
);
23615 if (parser
->fully_implicit_function_template_p
)
23618 finish_fully_implicit_template (parser
, 0);
23620 decl
= finish_fully_implicit_template (parser
, decl
);
23624 cp_finalize_omp_declare_simd (parser
, decl
);
23625 cp_finalize_oacc_routine (parser
, decl
, false);
23627 /* Reset PREFIX_ATTRIBUTES. */
23628 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23629 attributes
= TREE_CHAIN (attributes
);
23631 TREE_CHAIN (attributes
) = NULL_TREE
;
23633 /* If there is any qualification still in effect, clear it
23634 now; we will be starting fresh with the next declarator. */
23635 parser
->scope
= NULL_TREE
;
23636 parser
->qualifying_scope
= NULL_TREE
;
23637 parser
->object_scope
= NULL_TREE
;
23638 /* If it's a `,', then there are more declarators. */
23639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23641 cp_lexer_consume_token (parser
->lexer
);
23642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23644 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23645 gcc_rich_location
richloc (token
->location
);
23646 richloc
.add_fixit_remove ();
23647 error_at_rich_loc (&richloc
, "stray %<,%> at end of "
23648 "member declaration");
23651 /* If the next token isn't a `;', then we have a parse error. */
23652 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23655 /* The next token might be a ways away from where the
23656 actual semicolon is missing. Find the previous token
23657 and use that for our error position. */
23658 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23659 gcc_rich_location
richloc (token
->location
);
23660 richloc
.add_fixit_insert_after (";");
23661 error_at_rich_loc (&richloc
, "expected %<;%> at end of "
23662 "member declaration");
23664 /* Assume that the user meant to provide a semicolon. If
23665 we were to cp_parser_skip_to_end_of_statement, we might
23666 skip to a semicolon inside a member function definition
23667 and issue nonsensical error messages. */
23668 assume_semicolon
= true;
23673 /* Add DECL to the list of members. */
23675 /* Explicitly include, eg, NSDMIs, for better error
23676 recovery (c++/58650). */
23677 || !DECL_DECLARES_FUNCTION_P (decl
))
23678 finish_member_declaration (decl
);
23680 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23681 cp_parser_save_default_args (parser
, decl
);
23682 else if (TREE_CODE (decl
) == FIELD_DECL
23683 && !DECL_C_BIT_FIELD (decl
)
23684 && DECL_INITIAL (decl
))
23685 /* Add DECL to the queue of NSDMI to be parsed later. */
23686 vec_safe_push (unparsed_nsdmis
, decl
);
23689 if (assume_semicolon
)
23694 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23696 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23699 /* Parse a pure-specifier.
23704 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23705 Otherwise, ERROR_MARK_NODE is returned. */
23708 cp_parser_pure_specifier (cp_parser
* parser
)
23712 /* Look for the `=' token. */
23713 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23714 return error_mark_node
;
23715 /* Look for the `0' token. */
23716 token
= cp_lexer_peek_token (parser
->lexer
);
23718 if (token
->type
== CPP_EOF
23719 || token
->type
== CPP_PRAGMA_EOL
)
23720 return error_mark_node
;
23722 cp_lexer_consume_token (parser
->lexer
);
23724 /* Accept = default or = delete in c++0x mode. */
23725 if (token
->keyword
== RID_DEFAULT
23726 || token
->keyword
== RID_DELETE
)
23728 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23729 return token
->u
.value
;
23732 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23733 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23735 cp_parser_error (parser
,
23736 "invalid pure specifier (only %<= 0%> is allowed)");
23737 cp_parser_skip_to_end_of_statement (parser
);
23738 return error_mark_node
;
23740 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23742 error_at (token
->location
, "templates may not be %<virtual%>");
23743 return error_mark_node
;
23746 return integer_zero_node
;
23749 /* Parse a constant-initializer.
23751 constant-initializer:
23752 = constant-expression
23754 Returns a representation of the constant-expression. */
23757 cp_parser_constant_initializer (cp_parser
* parser
)
23759 /* Look for the `=' token. */
23760 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23761 return error_mark_node
;
23763 /* It is invalid to write:
23765 struct S { static const int i = { 7 }; };
23768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23770 cp_parser_error (parser
,
23771 "a brace-enclosed initializer is not allowed here");
23772 /* Consume the opening brace. */
23773 matching_braces braces
;
23774 braces
.consume_open (parser
);
23775 /* Skip the initializer. */
23776 cp_parser_skip_to_closing_brace (parser
);
23777 /* Look for the trailing `}'. */
23778 braces
.require_close (parser
);
23780 return error_mark_node
;
23783 return cp_parser_constant_expression (parser
);
23786 /* Derived classes [gram.class.derived] */
23788 /* Parse a base-clause.
23791 : base-specifier-list
23793 base-specifier-list:
23794 base-specifier ... [opt]
23795 base-specifier-list , base-specifier ... [opt]
23797 Returns a TREE_LIST representing the base-classes, in the order in
23798 which they were declared. The representation of each node is as
23799 described by cp_parser_base_specifier.
23801 In the case that no bases are specified, this function will return
23802 NULL_TREE, not ERROR_MARK_NODE. */
23805 cp_parser_base_clause (cp_parser
* parser
)
23807 tree bases
= NULL_TREE
;
23809 /* Look for the `:' that begins the list. */
23810 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23812 /* Scan the base-specifier-list. */
23817 bool pack_expansion_p
= false;
23819 /* Look for the base-specifier. */
23820 base
= cp_parser_base_specifier (parser
);
23821 /* Look for the (optional) ellipsis. */
23822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23824 /* Consume the `...'. */
23825 cp_lexer_consume_token (parser
->lexer
);
23827 pack_expansion_p
= true;
23830 /* Add BASE to the front of the list. */
23831 if (base
&& base
!= error_mark_node
)
23833 if (pack_expansion_p
)
23834 /* Make this a pack expansion type. */
23835 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
23837 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
23839 TREE_CHAIN (base
) = bases
;
23843 /* Peek at the next token. */
23844 token
= cp_lexer_peek_token (parser
->lexer
);
23845 /* If it's not a comma, then the list is complete. */
23846 if (token
->type
!= CPP_COMMA
)
23848 /* Consume the `,'. */
23849 cp_lexer_consume_token (parser
->lexer
);
23852 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23853 base class had a qualified name. However, the next name that
23854 appears is certainly not qualified. */
23855 parser
->scope
= NULL_TREE
;
23856 parser
->qualifying_scope
= NULL_TREE
;
23857 parser
->object_scope
= NULL_TREE
;
23859 return nreverse (bases
);
23862 /* Parse a base-specifier.
23865 :: [opt] nested-name-specifier [opt] class-name
23866 virtual access-specifier [opt] :: [opt] nested-name-specifier
23868 access-specifier virtual [opt] :: [opt] nested-name-specifier
23871 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23872 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23873 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23874 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23877 cp_parser_base_specifier (cp_parser
* parser
)
23881 bool virtual_p
= false;
23882 bool duplicate_virtual_error_issued_p
= false;
23883 bool duplicate_access_error_issued_p
= false;
23884 bool class_scope_p
, template_p
;
23885 tree access
= access_default_node
;
23888 /* Process the optional `virtual' and `access-specifier'. */
23891 /* Peek at the next token. */
23892 token
= cp_lexer_peek_token (parser
->lexer
);
23893 /* Process `virtual'. */
23894 switch (token
->keyword
)
23897 /* If `virtual' appears more than once, issue an error. */
23898 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
23900 cp_parser_error (parser
,
23901 "%<virtual%> specified more than once in base-specifier");
23902 duplicate_virtual_error_issued_p
= true;
23907 /* Consume the `virtual' token. */
23908 cp_lexer_consume_token (parser
->lexer
);
23913 case RID_PROTECTED
:
23915 /* If more than one access specifier appears, issue an
23917 if (access
!= access_default_node
23918 && !duplicate_access_error_issued_p
)
23920 cp_parser_error (parser
,
23921 "more than one access specifier in base-specifier");
23922 duplicate_access_error_issued_p
= true;
23925 access
= ridpointers
[(int) token
->keyword
];
23927 /* Consume the access-specifier. */
23928 cp_lexer_consume_token (parser
->lexer
);
23937 /* It is not uncommon to see programs mechanically, erroneously, use
23938 the 'typename' keyword to denote (dependent) qualified types
23939 as base classes. */
23940 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23942 token
= cp_lexer_peek_token (parser
->lexer
);
23943 if (!processing_template_decl
)
23944 error_at (token
->location
,
23945 "keyword %<typename%> not allowed outside of templates");
23947 error_at (token
->location
,
23948 "keyword %<typename%> not allowed in this context "
23949 "(the base class is implicitly a type)");
23950 cp_lexer_consume_token (parser
->lexer
);
23953 /* Look for the optional `::' operator. */
23954 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
23955 /* Look for the nested-name-specifier. The simplest way to
23960 The keyword `typename' is not permitted in a base-specifier or
23961 mem-initializer; in these contexts a qualified name that
23962 depends on a template-parameter is implicitly assumed to be a
23965 is to pretend that we have seen the `typename' keyword at this
23967 cp_parser_nested_name_specifier_opt (parser
,
23968 /*typename_keyword_p=*/true,
23969 /*check_dependency_p=*/true,
23971 /*is_declaration=*/true);
23972 /* If the base class is given by a qualified name, assume that names
23973 we see are type names or templates, as appropriate. */
23974 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
23975 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
23978 && cp_lexer_next_token_is_decltype (parser
->lexer
))
23979 /* DR 950 allows decltype as a base-specifier. */
23980 type
= cp_parser_decltype (parser
);
23983 /* Otherwise, look for the class-name. */
23984 type
= cp_parser_class_name (parser
,
23988 /*check_dependency_p=*/true,
23989 /*class_head_p=*/false,
23990 /*is_declaration=*/true);
23991 type
= TREE_TYPE (type
);
23994 if (type
== error_mark_node
)
23995 return error_mark_node
;
23997 return finish_base_specifier (type
, access
, virtual_p
);
24000 /* Exception handling [gram.exception] */
24002 /* Parse an (optional) noexcept-specification.
24004 noexcept-specification:
24005 noexcept ( constant-expression ) [opt]
24007 If no noexcept-specification is present, returns NULL_TREE.
24008 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24009 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24010 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24011 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24012 in which case a boolean condition is returned instead. */
24015 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24016 bool require_constexpr
,
24017 bool* consumed_expr
,
24021 const char *saved_message
;
24023 /* Peek at the next token. */
24024 token
= cp_lexer_peek_token (parser
->lexer
);
24026 /* Is it a noexcept-specification? */
24027 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24030 cp_lexer_consume_token (parser
->lexer
);
24032 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24034 matching_parens parens
;
24035 parens
.consume_open (parser
);
24037 if (require_constexpr
)
24039 /* Types may not be defined in an exception-specification. */
24040 saved_message
= parser
->type_definition_forbidden_message
;
24041 parser
->type_definition_forbidden_message
24042 = G_("types may not be defined in an exception-specification");
24044 expr
= cp_parser_constant_expression (parser
);
24046 /* Restore the saved message. */
24047 parser
->type_definition_forbidden_message
= saved_message
;
24051 expr
= cp_parser_expression (parser
);
24052 *consumed_expr
= true;
24055 parens
.require_close (parser
);
24059 expr
= boolean_true_node
;
24060 if (!require_constexpr
)
24061 *consumed_expr
= false;
24064 /* We cannot build a noexcept-spec right away because this will check
24065 that expr is a constexpr. */
24067 return build_noexcept_spec (expr
, tf_warning_or_error
);
24075 /* Parse an (optional) exception-specification.
24077 exception-specification:
24078 throw ( type-id-list [opt] )
24080 Returns a TREE_LIST representing the exception-specification. The
24081 TREE_VALUE of each node is a type. */
24084 cp_parser_exception_specification_opt (cp_parser
* parser
)
24088 const char *saved_message
;
24090 /* Peek at the next token. */
24091 token
= cp_lexer_peek_token (parser
->lexer
);
24093 /* Is it a noexcept-specification? */
24094 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24096 if (type_id_list
!= NULL_TREE
)
24097 return type_id_list
;
24099 /* If it's not `throw', then there's no exception-specification. */
24100 if (!cp_parser_is_keyword (token
, RID_THROW
))
24103 location_t loc
= token
->location
;
24105 /* Consume the `throw'. */
24106 cp_lexer_consume_token (parser
->lexer
);
24108 /* Look for the `('. */
24109 matching_parens parens
;
24110 parens
.require_open (parser
);
24112 /* Peek at the next token. */
24113 token
= cp_lexer_peek_token (parser
->lexer
);
24114 /* If it's not a `)', then there is a type-id-list. */
24115 if (token
->type
!= CPP_CLOSE_PAREN
)
24117 /* Types may not be defined in an exception-specification. */
24118 saved_message
= parser
->type_definition_forbidden_message
;
24119 parser
->type_definition_forbidden_message
24120 = G_("types may not be defined in an exception-specification");
24121 /* Parse the type-id-list. */
24122 type_id_list
= cp_parser_type_id_list (parser
);
24123 /* Restore the saved message. */
24124 parser
->type_definition_forbidden_message
= saved_message
;
24126 if (cxx_dialect
>= cxx17
)
24128 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24130 type_id_list
= NULL_TREE
;
24132 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24133 warning_at (loc
, OPT_Wdeprecated
,
24134 "dynamic exception specifications are deprecated in "
24137 /* In C++17, throw() is equivalent to noexcept (true). throw()
24138 is deprecated in C++11 and above as well, but is still widely used,
24139 so don't warn about it yet. */
24140 else if (cxx_dialect
>= cxx17
)
24141 type_id_list
= noexcept_true_spec
;
24143 type_id_list
= empty_except_spec
;
24145 /* Look for the `)'. */
24146 parens
.require_close (parser
);
24148 return type_id_list
;
24151 /* Parse an (optional) type-id-list.
24155 type-id-list , type-id ... [opt]
24157 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24158 in the order that the types were presented. */
24161 cp_parser_type_id_list (cp_parser
* parser
)
24163 tree types
= NULL_TREE
;
24170 token
= cp_lexer_peek_token (parser
->lexer
);
24172 /* Get the next type-id. */
24173 type
= cp_parser_type_id (parser
);
24174 /* Check for invalid 'auto'. */
24175 if (flag_concepts
&& type_uses_auto (type
))
24177 error_at (token
->location
,
24178 "invalid use of %<auto%> in exception-specification");
24179 type
= error_mark_node
;
24181 /* Parse the optional ellipsis. */
24182 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24184 /* Consume the `...'. */
24185 cp_lexer_consume_token (parser
->lexer
);
24187 /* Turn the type into a pack expansion expression. */
24188 type
= make_pack_expansion (type
);
24190 /* Add it to the list. */
24191 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24192 /* Peek at the next token. */
24193 token
= cp_lexer_peek_token (parser
->lexer
);
24194 /* If it is not a `,', we are done. */
24195 if (token
->type
!= CPP_COMMA
)
24197 /* Consume the `,'. */
24198 cp_lexer_consume_token (parser
->lexer
);
24201 return nreverse (types
);
24204 /* Parse a try-block.
24207 try compound-statement handler-seq */
24210 cp_parser_try_block (cp_parser
* parser
)
24214 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24215 if (parser
->in_function_body
24216 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24217 error ("%<try%> in %<constexpr%> function");
24219 try_block
= begin_try_block ();
24220 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24221 finish_try_block (try_block
);
24222 cp_parser_handler_seq (parser
);
24223 finish_handler_sequence (try_block
);
24228 /* Parse a function-try-block.
24230 function-try-block:
24231 try ctor-initializer [opt] function-body handler-seq */
24234 cp_parser_function_try_block (cp_parser
* parser
)
24236 tree compound_stmt
;
24238 bool ctor_initializer_p
;
24240 /* Look for the `try' keyword. */
24241 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24243 /* Let the rest of the front end know where we are. */
24244 try_block
= begin_function_try_block (&compound_stmt
);
24245 /* Parse the function-body. */
24246 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
24247 (parser
, /*in_function_try_block=*/true);
24248 /* We're done with the `try' part. */
24249 finish_function_try_block (try_block
);
24250 /* Parse the handlers. */
24251 cp_parser_handler_seq (parser
);
24252 /* We're done with the handlers. */
24253 finish_function_handler_sequence (try_block
, compound_stmt
);
24255 return ctor_initializer_p
;
24258 /* Parse a handler-seq.
24261 handler handler-seq [opt] */
24264 cp_parser_handler_seq (cp_parser
* parser
)
24270 /* Parse the handler. */
24271 cp_parser_handler (parser
);
24272 /* Peek at the next token. */
24273 token
= cp_lexer_peek_token (parser
->lexer
);
24274 /* If it's not `catch' then there are no more handlers. */
24275 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24280 /* Parse a handler.
24283 catch ( exception-declaration ) compound-statement */
24286 cp_parser_handler (cp_parser
* parser
)
24291 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24292 handler
= begin_handler ();
24293 matching_parens parens
;
24294 parens
.require_open (parser
);
24295 declaration
= cp_parser_exception_declaration (parser
);
24296 finish_handler_parms (declaration
, handler
);
24297 parens
.require_close (parser
);
24298 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24299 finish_handler (handler
);
24302 /* Parse an exception-declaration.
24304 exception-declaration:
24305 type-specifier-seq declarator
24306 type-specifier-seq abstract-declarator
24310 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24311 ellipsis variant is used. */
24314 cp_parser_exception_declaration (cp_parser
* parser
)
24316 cp_decl_specifier_seq type_specifiers
;
24317 cp_declarator
*declarator
;
24318 const char *saved_message
;
24320 /* If it's an ellipsis, it's easy to handle. */
24321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24323 /* Consume the `...' token. */
24324 cp_lexer_consume_token (parser
->lexer
);
24328 /* Types may not be defined in exception-declarations. */
24329 saved_message
= parser
->type_definition_forbidden_message
;
24330 parser
->type_definition_forbidden_message
24331 = G_("types may not be defined in exception-declarations");
24333 /* Parse the type-specifier-seq. */
24334 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24335 /*is_trailing_return=*/false,
24337 /* If it's a `)', then there is no declarator. */
24338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24341 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24342 /*ctor_dtor_or_conv_p=*/NULL
,
24343 /*parenthesized_p=*/NULL
,
24344 /*member_p=*/false,
24345 /*friend_p=*/false);
24347 /* Restore the saved message. */
24348 parser
->type_definition_forbidden_message
= saved_message
;
24350 if (!type_specifiers
.any_specifiers_p
)
24351 return error_mark_node
;
24353 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24356 /* Parse a throw-expression.
24359 throw assignment-expression [opt]
24361 Returns a THROW_EXPR representing the throw-expression. */
24364 cp_parser_throw_expression (cp_parser
* parser
)
24369 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24370 token
= cp_lexer_peek_token (parser
->lexer
);
24371 /* Figure out whether or not there is an assignment-expression
24372 following the "throw" keyword. */
24373 if (token
->type
== CPP_COMMA
24374 || token
->type
== CPP_SEMICOLON
24375 || token
->type
== CPP_CLOSE_PAREN
24376 || token
->type
== CPP_CLOSE_SQUARE
24377 || token
->type
== CPP_CLOSE_BRACE
24378 || token
->type
== CPP_COLON
)
24379 expression
= NULL_TREE
;
24381 expression
= cp_parser_assignment_expression (parser
);
24383 return build_throw (expression
);
24386 /* GNU Extensions */
24388 /* Parse an (optional) asm-specification.
24391 asm ( string-literal )
24393 If the asm-specification is present, returns a STRING_CST
24394 corresponding to the string-literal. Otherwise, returns
24398 cp_parser_asm_specification_opt (cp_parser
* parser
)
24401 tree asm_specification
;
24403 /* Peek at the next token. */
24404 token
= cp_lexer_peek_token (parser
->lexer
);
24405 /* If the next token isn't the `asm' keyword, then there's no
24406 asm-specification. */
24407 if (!cp_parser_is_keyword (token
, RID_ASM
))
24410 /* Consume the `asm' token. */
24411 cp_lexer_consume_token (parser
->lexer
);
24412 /* Look for the `('. */
24413 matching_parens parens
;
24414 parens
.require_open (parser
);
24416 /* Look for the string-literal. */
24417 asm_specification
= cp_parser_string_literal (parser
, false, false);
24419 /* Look for the `)'. */
24420 parens
.require_close (parser
);
24422 return asm_specification
;
24425 /* Parse an asm-operand-list.
24429 asm-operand-list , asm-operand
24432 string-literal ( expression )
24433 [ string-literal ] string-literal ( expression )
24435 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24436 each node is the expression. The TREE_PURPOSE is itself a
24437 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24438 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24439 is a STRING_CST for the string literal before the parenthesis. Returns
24440 ERROR_MARK_NODE if any of the operands are invalid. */
24443 cp_parser_asm_operand_list (cp_parser
* parser
)
24445 tree asm_operands
= NULL_TREE
;
24446 bool invalid_operands
= false;
24450 tree string_literal
;
24454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24456 /* Consume the `[' token. */
24457 cp_lexer_consume_token (parser
->lexer
);
24458 /* Read the operand name. */
24459 name
= cp_parser_identifier (parser
);
24460 if (name
!= error_mark_node
)
24461 name
= build_string (IDENTIFIER_LENGTH (name
),
24462 IDENTIFIER_POINTER (name
));
24463 /* Look for the closing `]'. */
24464 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24468 /* Look for the string-literal. */
24469 string_literal
= cp_parser_string_literal (parser
, false, false);
24471 /* Look for the `('. */
24472 matching_parens parens
;
24473 parens
.require_open (parser
);
24474 /* Parse the expression. */
24475 expression
= cp_parser_expression (parser
);
24476 /* Look for the `)'. */
24477 parens
.require_close (parser
);
24479 if (name
== error_mark_node
24480 || string_literal
== error_mark_node
24481 || expression
== error_mark_node
)
24482 invalid_operands
= true;
24484 /* Add this operand to the list. */
24485 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24488 /* If the next token is not a `,', there are no more
24490 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24492 /* Consume the `,'. */
24493 cp_lexer_consume_token (parser
->lexer
);
24496 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24499 /* Parse an asm-clobber-list.
24503 asm-clobber-list , string-literal
24505 Returns a TREE_LIST, indicating the clobbers in the order that they
24506 appeared. The TREE_VALUE of each node is a STRING_CST. */
24509 cp_parser_asm_clobber_list (cp_parser
* parser
)
24511 tree clobbers
= NULL_TREE
;
24515 tree string_literal
;
24517 /* Look for the string literal. */
24518 string_literal
= cp_parser_string_literal (parser
, false, false);
24519 /* Add it to the list. */
24520 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24521 /* If the next token is not a `,', then the list is
24523 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24525 /* Consume the `,' token. */
24526 cp_lexer_consume_token (parser
->lexer
);
24532 /* Parse an asm-label-list.
24536 asm-label-list , identifier
24538 Returns a TREE_LIST, indicating the labels in the order that they
24539 appeared. The TREE_VALUE of each node is a label. */
24542 cp_parser_asm_label_list (cp_parser
* parser
)
24544 tree labels
= NULL_TREE
;
24548 tree identifier
, label
, name
;
24550 /* Look for the identifier. */
24551 identifier
= cp_parser_identifier (parser
);
24552 if (!error_operand_p (identifier
))
24554 label
= lookup_label (identifier
);
24555 if (TREE_CODE (label
) == LABEL_DECL
)
24557 TREE_USED (label
) = 1;
24558 check_goto (label
);
24559 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24560 IDENTIFIER_POINTER (identifier
));
24561 labels
= tree_cons (name
, label
, labels
);
24564 /* If the next token is not a `,', then the list is
24566 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24568 /* Consume the `,' token. */
24569 cp_lexer_consume_token (parser
->lexer
);
24572 return nreverse (labels
);
24575 /* Return TRUE iff the next tokens in the stream are possibly the
24576 beginning of a GNU extension attribute. */
24579 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24581 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24584 /* Return TRUE iff the next tokens in the stream are possibly the
24585 beginning of a standard C++-11 attribute specifier. */
24588 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24590 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24593 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24594 beginning of a standard C++-11 attribute specifier. */
24597 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24599 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24601 return (cxx_dialect
>= cxx11
24602 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24603 || (token
->type
== CPP_OPEN_SQUARE
24604 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24605 && token
->type
== CPP_OPEN_SQUARE
)));
24608 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24609 beginning of a GNU extension attribute. */
24612 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24614 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24616 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24619 /* Return true iff the next tokens can be the beginning of either a
24620 GNU attribute list, or a standard C++11 attribute sequence. */
24623 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24625 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24626 || cp_next_tokens_can_be_std_attribute_p (parser
));
24629 /* Return true iff the next Nth tokens can be the beginning of either
24630 a GNU attribute list, or a standard C++11 attribute sequence. */
24633 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24635 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24636 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24639 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24640 of GNU attributes, or return NULL. */
24643 cp_parser_attributes_opt (cp_parser
*parser
)
24645 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24646 return cp_parser_gnu_attributes_opt (parser
);
24647 return cp_parser_std_attribute_spec_seq (parser
);
24650 #define CILK_SIMD_FN_CLAUSE_MASK \
24651 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24652 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24653 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24654 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24655 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24657 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24658 vector [(<clauses>)] */
24661 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
24663 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
24664 cp_token
*token
= v_token
;
24667 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
24668 parser
->cilk_simd_fn_info
->error_seen
= false;
24669 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
24670 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
24671 parser
->cilk_simd_fn_info
->clauses
= NULL_TREE
;
24673 int paren_scope
= 0;
24674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24676 cp_lexer_consume_token (parser
->lexer
);
24677 v_token
= cp_lexer_peek_token (parser
->lexer
);
24680 while (paren_scope
> 0)
24682 token
= cp_lexer_peek_token (parser
->lexer
);
24683 if (token
->type
== CPP_OPEN_PAREN
)
24685 else if (token
->type
== CPP_CLOSE_PAREN
)
24687 /* Do not push the last ')' */
24688 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
24689 cp_lexer_consume_token (parser
->lexer
);
24692 token
->type
= CPP_PRAGMA_EOL
;
24693 parser
->lexer
->next_token
= token
;
24694 cp_lexer_consume_token (parser
->lexer
);
24696 struct cp_token_cache
*cp
24697 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
24698 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
24701 /* Parse an (optional) series of attributes.
24704 attributes attribute
24707 __attribute__ (( attribute-list [opt] ))
24709 The return value is as for cp_parser_gnu_attribute_list. */
24712 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24714 tree attributes
= NULL_TREE
;
24719 tree attribute_list
;
24722 /* Peek at the next token. */
24723 token
= cp_lexer_peek_token (parser
->lexer
);
24724 /* If it's not `__attribute__', then we're done. */
24725 if (token
->keyword
!= RID_ATTRIBUTE
)
24728 /* Consume the `__attribute__' keyword. */
24729 cp_lexer_consume_token (parser
->lexer
);
24730 /* Look for the two `(' tokens. */
24731 matching_parens outer_parens
;
24732 outer_parens
.require_open (parser
);
24733 matching_parens inner_parens
;
24734 inner_parens
.require_open (parser
);
24736 /* Peek at the next token. */
24737 token
= cp_lexer_peek_token (parser
->lexer
);
24738 if (token
->type
!= CPP_CLOSE_PAREN
)
24739 /* Parse the attribute-list. */
24740 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24742 /* If the next token is a `)', then there is no attribute
24744 attribute_list
= NULL
;
24746 /* Look for the two `)' tokens. */
24747 if (!inner_parens
.require_close (parser
))
24749 if (!outer_parens
.require_close (parser
))
24752 cp_parser_skip_to_end_of_statement (parser
);
24754 /* Add these new attributes to the list. */
24755 attributes
= chainon (attributes
, attribute_list
);
24761 /* Parse a GNU attribute-list.
24765 attribute-list , attribute
24769 identifier ( identifier )
24770 identifier ( identifier , expression-list )
24771 identifier ( expression-list )
24773 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24774 to an attribute. The TREE_PURPOSE of each node is the identifier
24775 indicating which attribute is in use. The TREE_VALUE represents
24776 the arguments, if any. */
24779 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24781 tree attribute_list
= NULL_TREE
;
24782 bool save_translate_strings_p
= parser
->translate_strings_p
;
24784 parser
->translate_strings_p
= false;
24791 /* Look for the identifier. We also allow keywords here; for
24792 example `__attribute__ ((const))' is legal. */
24793 token
= cp_lexer_peek_token (parser
->lexer
);
24794 if (token
->type
== CPP_NAME
24795 || token
->type
== CPP_KEYWORD
)
24797 tree arguments
= NULL_TREE
;
24799 /* Consume the token, but save it since we need it for the
24800 SIMD enabled function parsing. */
24801 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24803 /* Save away the identifier that indicates which attribute
24805 identifier
= (token
->type
== CPP_KEYWORD
)
24806 /* For keywords, use the canonical spelling, not the
24807 parsed identifier. */
24808 ? ridpointers
[(int) token
->keyword
]
24809 : id_token
->u
.value
;
24811 identifier
= canonicalize_attr_name (identifier
);
24812 attribute
= build_tree_list (identifier
, NULL_TREE
);
24814 /* Peek at the next token. */
24815 token
= cp_lexer_peek_token (parser
->lexer
);
24816 /* If it's an `(', then parse the attribute arguments. */
24817 if (token
->type
== CPP_OPEN_PAREN
)
24819 vec
<tree
, va_gc
> *vec
;
24820 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24821 ? id_attr
: normal_attr
);
24822 if (is_cilkplus_vector_p (identifier
))
24824 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24828 vec
= cp_parser_parenthesized_expression_list
24829 (parser
, attr_flag
, /*cast_p=*/false,
24830 /*allow_expansion_p=*/false,
24831 /*non_constant_p=*/NULL
);
24833 arguments
= error_mark_node
;
24836 arguments
= build_tree_list_vec (vec
);
24837 release_tree_vector (vec
);
24839 /* Save the arguments away. */
24840 TREE_VALUE (attribute
) = arguments
;
24842 else if (is_cilkplus_vector_p (identifier
))
24844 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24848 if (arguments
!= error_mark_node
)
24850 /* Add this attribute to the list. */
24851 TREE_CHAIN (attribute
) = attribute_list
;
24852 attribute_list
= attribute
;
24855 token
= cp_lexer_peek_token (parser
->lexer
);
24857 /* Now, look for more attributes. If the next token isn't a
24858 `,', we're done. */
24859 if (token
->type
!= CPP_COMMA
)
24862 /* Consume the comma and keep going. */
24863 cp_lexer_consume_token (parser
->lexer
);
24865 parser
->translate_strings_p
= save_translate_strings_p
;
24867 /* We built up the list in reverse order. */
24868 return nreverse (attribute_list
);
24871 /* Parse a standard C++11 attribute.
24873 The returned representation is a TREE_LIST which TREE_PURPOSE is
24874 the scoped name of the attribute, and the TREE_VALUE is its
24877 Note that the scoped name of the attribute is itself a TREE_LIST
24878 which TREE_PURPOSE is the namespace of the attribute, and
24879 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24880 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24881 and which TREE_PURPOSE is directly the attribute name.
24883 Clients of the attribute code should use get_attribute_namespace
24884 and get_attribute_name to get the actual namespace and name of
24885 attributes, regardless of their being GNU or C++11 attributes.
24888 attribute-token attribute-argument-clause [opt]
24892 attribute-scoped-token
24894 attribute-scoped-token:
24895 attribute-namespace :: identifier
24897 attribute-namespace:
24900 attribute-argument-clause:
24901 ( balanced-token-seq )
24903 balanced-token-seq:
24904 balanced-token [opt]
24905 balanced-token-seq balanced-token
24908 ( balanced-token-seq )
24909 [ balanced-token-seq ]
24910 { balanced-token-seq }. */
24913 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
24915 tree attribute
, attr_id
= NULL_TREE
, arguments
;
24918 /* First, parse name of the attribute, a.k.a attribute-token. */
24920 token
= cp_lexer_peek_token (parser
->lexer
);
24921 if (token
->type
== CPP_NAME
)
24922 attr_id
= token
->u
.value
;
24923 else if (token
->type
== CPP_KEYWORD
)
24924 attr_id
= ridpointers
[(int) token
->keyword
];
24925 else if (token
->flags
& NAMED_OP
)
24926 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24928 if (attr_id
== NULL_TREE
)
24931 cp_lexer_consume_token (parser
->lexer
);
24933 token
= cp_lexer_peek_token (parser
->lexer
);
24934 if (token
->type
== CPP_SCOPE
)
24936 /* We are seeing a scoped attribute token. */
24938 cp_lexer_consume_token (parser
->lexer
);
24940 error_at (token
->location
, "attribute using prefix used together "
24941 "with scoped attribute token");
24944 token
= cp_lexer_consume_token (parser
->lexer
);
24945 if (token
->type
== CPP_NAME
)
24946 attr_id
= token
->u
.value
;
24947 else if (token
->type
== CPP_KEYWORD
)
24948 attr_id
= ridpointers
[(int) token
->keyword
];
24949 else if (token
->flags
& NAMED_OP
)
24950 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24953 error_at (token
->location
,
24954 "expected an identifier for the attribute name");
24955 return error_mark_node
;
24958 attr_id
= canonicalize_attr_name (attr_id
);
24959 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24961 token
= cp_lexer_peek_token (parser
->lexer
);
24964 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24968 attr_id
= canonicalize_attr_name (attr_id
);
24969 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
24971 /* C++11 noreturn attribute is equivalent to GNU's. */
24972 if (is_attribute_p ("noreturn", attr_id
))
24973 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24974 /* C++14 deprecated attribute is equivalent to GNU's. */
24975 else if (is_attribute_p ("deprecated", attr_id
))
24976 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24977 /* C++17 fallthrough attribute is equivalent to GNU's. */
24978 else if (is_attribute_p ("fallthrough", attr_id
))
24979 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24980 /* Transactional Memory TS optimize_for_synchronized attribute is
24981 equivalent to GNU transaction_callable. */
24982 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
24983 TREE_PURPOSE (attribute
)
24984 = get_identifier ("transaction_callable");
24985 /* Transactional Memory attributes are GNU attributes. */
24986 else if (tm_attr_to_mask (attr_id
))
24987 TREE_PURPOSE (attribute
) = attr_id
;
24990 /* Now parse the optional argument clause of the attribute. */
24992 if (token
->type
!= CPP_OPEN_PAREN
)
24996 vec
<tree
, va_gc
> *vec
;
24997 int attr_flag
= normal_attr
;
24999 if (attr_ns
== get_identifier ("gnu")
25000 && attribute_takes_identifier_p (attr_id
))
25001 /* A GNU attribute that takes an identifier in parameter. */
25002 attr_flag
= id_attr
;
25004 vec
= cp_parser_parenthesized_expression_list
25005 (parser
, attr_flag
, /*cast_p=*/false,
25006 /*allow_expansion_p=*/true,
25007 /*non_constant_p=*/NULL
);
25009 arguments
= error_mark_node
;
25012 arguments
= build_tree_list_vec (vec
);
25013 release_tree_vector (vec
);
25016 if (arguments
== error_mark_node
)
25017 attribute
= error_mark_node
;
25019 TREE_VALUE (attribute
) = arguments
;
25025 /* Check that the attribute ATTRIBUTE appears at most once in the
25026 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25027 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25028 isn't implemented yet in GCC. */
25031 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25035 tree name
= get_attribute_name (attribute
);
25036 if (is_attribute_p ("noreturn", name
)
25037 && lookup_attribute ("noreturn", attributes
))
25038 error ("attribute %<noreturn%> can appear at most once "
25039 "in an attribute-list");
25040 else if (is_attribute_p ("deprecated", name
)
25041 && lookup_attribute ("deprecated", attributes
))
25042 error ("attribute %<deprecated%> can appear at most once "
25043 "in an attribute-list");
25047 /* Parse a list of standard C++-11 attributes.
25051 attribute-list , attribute[opt]
25053 attribute-list , attribute ...
25057 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25059 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25060 cp_token
*token
= NULL
;
25064 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25065 if (attribute
== error_mark_node
)
25067 if (attribute
!= NULL_TREE
)
25069 cp_parser_check_std_attribute (attributes
, attribute
);
25070 TREE_CHAIN (attribute
) = attributes
;
25071 attributes
= attribute
;
25073 token
= cp_lexer_peek_token (parser
->lexer
);
25074 if (token
->type
== CPP_ELLIPSIS
)
25076 cp_lexer_consume_token (parser
->lexer
);
25077 if (attribute
== NULL_TREE
)
25078 error_at (token
->location
,
25079 "expected attribute before %<...%>");
25082 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25083 if (pack
== error_mark_node
)
25084 return error_mark_node
;
25085 TREE_VALUE (attribute
) = pack
;
25087 token
= cp_lexer_peek_token (parser
->lexer
);
25089 if (token
->type
!= CPP_COMMA
)
25091 cp_lexer_consume_token (parser
->lexer
);
25093 attributes
= nreverse (attributes
);
25097 /* Parse a standard C++-11 attribute specifier.
25099 attribute-specifier:
25100 [ [ attribute-using-prefix [opt] attribute-list ] ]
25101 alignment-specifier
25103 attribute-using-prefix:
25104 using attribute-namespace :
25106 alignment-specifier:
25107 alignas ( type-id ... [opt] )
25108 alignas ( alignment-expression ... [opt] ). */
25111 cp_parser_std_attribute_spec (cp_parser
*parser
)
25113 tree attributes
= NULL_TREE
;
25114 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25116 if (token
->type
== CPP_OPEN_SQUARE
25117 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25119 tree attr_ns
= NULL_TREE
;
25121 cp_lexer_consume_token (parser
->lexer
);
25122 cp_lexer_consume_token (parser
->lexer
);
25124 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25126 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25127 if (token
->type
== CPP_NAME
)
25128 attr_ns
= token
->u
.value
;
25129 else if (token
->type
== CPP_KEYWORD
)
25130 attr_ns
= ridpointers
[(int) token
->keyword
];
25131 else if (token
->flags
& NAMED_OP
)
25132 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25135 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25137 if (cxx_dialect
< cxx17
25138 && !in_system_header_at (input_location
))
25139 pedwarn (input_location
, 0,
25140 "attribute using prefix only available "
25141 "with -std=c++17 or -std=gnu++17");
25143 cp_lexer_consume_token (parser
->lexer
);
25144 cp_lexer_consume_token (parser
->lexer
);
25145 cp_lexer_consume_token (parser
->lexer
);
25148 attr_ns
= NULL_TREE
;
25151 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25153 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25154 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25155 cp_parser_skip_to_end_of_statement (parser
);
25157 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25158 when we are sure that we have actually parsed them. */
25159 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25165 /* Look for an alignment-specifier. */
25167 token
= cp_lexer_peek_token (parser
->lexer
);
25169 if (token
->type
!= CPP_KEYWORD
25170 || token
->keyword
!= RID_ALIGNAS
)
25173 cp_lexer_consume_token (parser
->lexer
);
25174 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25176 matching_parens parens
;
25177 if (!parens
.require_open (parser
))
25179 cp_parser_error (parser
, "expected %<(%>");
25180 return error_mark_node
;
25183 cp_parser_parse_tentatively (parser
);
25184 alignas_expr
= cp_parser_type_id (parser
);
25186 if (!cp_parser_parse_definitely (parser
))
25188 alignas_expr
= cp_parser_assignment_expression (parser
);
25189 if (alignas_expr
== error_mark_node
)
25190 cp_parser_skip_to_end_of_statement (parser
);
25191 if (alignas_expr
== NULL_TREE
25192 || alignas_expr
== error_mark_node
)
25193 return alignas_expr
;
25196 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25197 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25199 /* Handle alignas (pack...). */
25200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25202 cp_lexer_consume_token (parser
->lexer
);
25203 alignas_expr
= make_pack_expansion (alignas_expr
);
25206 /* Something went wrong, so don't build the attribute. */
25207 if (alignas_expr
== error_mark_node
)
25208 return error_mark_node
;
25210 if (!parens
.require_close (parser
))
25212 cp_parser_error (parser
, "expected %<)%>");
25213 return error_mark_node
;
25216 /* Build the C++-11 representation of an 'aligned'
25219 build_tree_list (build_tree_list (get_identifier ("gnu"),
25220 get_identifier ("aligned")),
25227 /* Parse a standard C++-11 attribute-specifier-seq.
25229 attribute-specifier-seq:
25230 attribute-specifier-seq [opt] attribute-specifier
25234 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25236 tree attr_specs
= NULL_TREE
;
25237 tree attr_last
= NULL_TREE
;
25241 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25242 if (attr_spec
== NULL_TREE
)
25244 if (attr_spec
== error_mark_node
)
25245 return error_mark_node
;
25248 TREE_CHAIN (attr_last
) = attr_spec
;
25250 attr_specs
= attr_last
= attr_spec
;
25251 attr_last
= tree_last (attr_last
);
25257 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25258 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25259 current value of the PEDANTIC flag, regardless of whether or not
25260 the `__extension__' keyword is present. The caller is responsible
25261 for restoring the value of the PEDANTIC flag. */
25264 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25266 /* Save the old value of the PEDANTIC flag. */
25267 *saved_pedantic
= pedantic
;
25269 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25271 /* Consume the `__extension__' token. */
25272 cp_lexer_consume_token (parser
->lexer
);
25273 /* We're not being pedantic while the `__extension__' keyword is
25283 /* Parse a label declaration.
25286 __label__ label-declarator-seq ;
25288 label-declarator-seq:
25289 identifier , label-declarator-seq
25293 cp_parser_label_declaration (cp_parser
* parser
)
25295 /* Look for the `__label__' keyword. */
25296 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25302 /* Look for an identifier. */
25303 identifier
= cp_parser_identifier (parser
);
25304 /* If we failed, stop. */
25305 if (identifier
== error_mark_node
)
25307 /* Declare it as a label. */
25308 finish_label_decl (identifier
);
25309 /* If the next token is a `;', stop. */
25310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25312 /* Look for the `,' separating the label declarations. */
25313 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25316 /* Look for the final `;'. */
25317 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25320 // -------------------------------------------------------------------------- //
25323 // Parse a requires clause.
25325 // requires-clause:
25326 // 'requires' logical-or-expression
25328 // The required logical-or-expression must be a constant expression. Note
25329 // that we don't check that the expression is constepxr here. We defer until
25330 // we analyze constraints and then, we only check atomic constraints.
25332 cp_parser_requires_clause (cp_parser
*parser
)
25334 // Parse the requires clause so that it is not automatically folded.
25335 ++processing_template_decl
;
25336 tree expr
= cp_parser_binary_expression (parser
, false, false,
25337 PREC_NOT_OPERATOR
, NULL
);
25338 if (check_for_bare_parameter_packs (expr
))
25339 expr
= error_mark_node
;
25340 --processing_template_decl
;
25344 // Optionally parse a requires clause:
25346 cp_parser_requires_clause_opt (cp_parser
*parser
)
25348 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25349 if (tok
->keyword
!= RID_REQUIRES
)
25351 if (!flag_concepts
&& tok
->type
== CPP_NAME
25352 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25354 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25355 "%<requires%> only available with -fconcepts");
25356 /* Parse and discard the requires-clause. */
25357 cp_lexer_consume_token (parser
->lexer
);
25358 cp_parser_requires_clause (parser
);
25362 cp_lexer_consume_token (parser
->lexer
);
25363 return cp_parser_requires_clause (parser
);
25367 /*---------------------------------------------------------------------------
25368 Requires expressions
25369 ---------------------------------------------------------------------------*/
25371 /* Parse a requires expression
25373 requirement-expression:
25374 'requires' requirement-parameter-list [opt] requirement-body */
25376 cp_parser_requires_expression (cp_parser
*parser
)
25378 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25379 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25381 /* A requires-expression shall appear only within a concept
25382 definition or a requires-clause.
25384 TODO: Implement this diagnostic correctly. */
25385 if (!processing_template_decl
)
25387 error_at (loc
, "a requires expression cannot appear outside a template");
25388 cp_parser_skip_to_end_of_statement (parser
);
25389 return error_mark_node
;
25394 /* Local parameters are delared as variables within the scope
25395 of the expression. They are not visible past the end of
25396 the expression. Expressions within the requires-expression
25397 are unevaluated. */
25398 struct scope_sentinel
25402 ++cp_unevaluated_operand
;
25403 begin_scope (sk_block
, NULL_TREE
);
25408 pop_bindings_and_leave_scope ();
25409 --cp_unevaluated_operand
;
25413 /* Parse the optional parameter list. */
25414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25416 parms
= cp_parser_requirement_parameter_list (parser
);
25417 if (parms
== error_mark_node
)
25418 return error_mark_node
;
25423 /* Parse the requirement body. */
25424 reqs
= cp_parser_requirement_body (parser
);
25425 if (reqs
== error_mark_node
)
25426 return error_mark_node
;
25429 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25431 grokparms (parms
, &parms
);
25432 return finish_requires_expr (parms
, reqs
);
25435 /* Parse a parameterized requirement.
25437 requirement-parameter-list:
25438 '(' parameter-declaration-clause ')' */
25440 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25442 matching_parens parens
;
25443 if (!parens
.require_open (parser
))
25444 return error_mark_node
;
25446 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25448 if (!parens
.require_close (parser
))
25449 return error_mark_node
;
25454 /* Parse the body of a requirement.
25457 '{' requirement-list '}' */
25459 cp_parser_requirement_body (cp_parser
*parser
)
25461 matching_braces braces
;
25462 if (!braces
.require_open (parser
))
25463 return error_mark_node
;
25465 tree reqs
= cp_parser_requirement_list (parser
);
25467 if (!braces
.require_close (parser
))
25468 return error_mark_node
;
25473 /* Parse a list of requirements.
25477 requirement-list ';' requirement[opt] */
25479 cp_parser_requirement_list (cp_parser
*parser
)
25481 tree result
= NULL_TREE
;
25484 tree req
= cp_parser_requirement (parser
);
25485 if (req
== error_mark_node
)
25486 return error_mark_node
;
25488 result
= tree_cons (NULL_TREE
, req
, result
);
25490 /* If we see a semi-colon, consume it. */
25491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25492 cp_lexer_consume_token (parser
->lexer
);
25494 /* Stop processing at the end of the list. */
25495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25499 /* Reverse the order of requirements so they are analyzed in
25500 declaration order. */
25501 return nreverse (result
);
25504 /* Parse a syntactic requirement or type requirement.
25508 compound-requirement
25510 nested-requirement */
25512 cp_parser_requirement (cp_parser
*parser
)
25514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25515 return cp_parser_compound_requirement (parser
);
25516 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25517 return cp_parser_type_requirement (parser
);
25518 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25519 return cp_parser_nested_requirement (parser
);
25521 return cp_parser_simple_requirement (parser
);
25524 /* Parse a simple requirement.
25526 simple-requirement:
25529 cp_parser_simple_requirement (cp_parser
*parser
)
25531 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25532 if (!expr
|| expr
== error_mark_node
)
25533 return error_mark_node
;
25535 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25536 return error_mark_node
;
25538 return finish_simple_requirement (expr
);
25541 /* Parse a type requirement
25544 nested-name-specifier [opt] required-type-name ';'
25546 required-type-name:
25548 'template' [opt] simple-template-id */
25550 cp_parser_type_requirement (cp_parser
*parser
)
25552 cp_lexer_consume_token (parser
->lexer
);
25554 // Save the scope before parsing name specifiers.
25555 tree saved_scope
= parser
->scope
;
25556 tree saved_object_scope
= parser
->object_scope
;
25557 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25558 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25559 cp_parser_nested_name_specifier_opt (parser
,
25560 /*typename_keyword_p=*/true,
25561 /*check_dependency_p=*/false,
25563 /*is_declaration=*/false);
25566 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25568 cp_lexer_consume_token (parser
->lexer
);
25569 type
= cp_parser_template_id (parser
,
25570 /*template_keyword_p=*/true,
25571 /*check_dependency=*/false,
25572 /*tag_type=*/none_type
,
25573 /*is_declaration=*/false);
25574 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25575 /*complain=*/tf_error
);
25578 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25580 if (TREE_CODE (type
) == TYPE_DECL
)
25581 type
= TREE_TYPE (type
);
25583 parser
->scope
= saved_scope
;
25584 parser
->object_scope
= saved_object_scope
;
25585 parser
->qualifying_scope
= saved_qualifying_scope
;
25587 if (type
== error_mark_node
)
25588 cp_parser_skip_to_end_of_statement (parser
);
25590 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25591 return error_mark_node
;
25592 if (type
== error_mark_node
)
25593 return error_mark_node
;
25595 return finish_type_requirement (type
);
25598 /* Parse a compound requirement
25600 compound-requirement:
25601 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25603 cp_parser_compound_requirement (cp_parser
*parser
)
25605 /* Parse an expression enclosed in '{ }'s. */
25606 matching_braces braces
;
25607 if (!braces
.require_open (parser
))
25608 return error_mark_node
;
25610 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25611 if (!expr
|| expr
== error_mark_node
)
25612 return error_mark_node
;
25614 if (!braces
.require_close (parser
))
25615 return error_mark_node
;
25617 /* Parse the optional noexcept. */
25618 bool noexcept_p
= false;
25619 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25621 cp_lexer_consume_token (parser
->lexer
);
25625 /* Parse the optional trailing return type. */
25626 tree type
= NULL_TREE
;
25627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25629 cp_lexer_consume_token (parser
->lexer
);
25630 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25631 parser
->in_result_type_constraint_p
= true;
25632 type
= cp_parser_trailing_type_id (parser
);
25633 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25634 if (type
== error_mark_node
)
25635 return error_mark_node
;
25638 return finish_compound_requirement (expr
, type
, noexcept_p
);
25641 /* Parse a nested requirement. This is the same as a requires clause.
25643 nested-requirement:
25646 cp_parser_nested_requirement (cp_parser
*parser
)
25648 cp_lexer_consume_token (parser
->lexer
);
25649 tree req
= cp_parser_requires_clause (parser
);
25650 if (req
== error_mark_node
)
25651 return error_mark_node
;
25652 return finish_nested_requirement (req
);
25655 /* Support Functions */
25657 /* Return the appropriate prefer_type argument for lookup_name_real based on
25658 tag_type and template_mem_access. */
25661 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25663 /* DR 141: When looking in the current enclosing context for a template-name
25664 after -> or ., only consider class templates. */
25665 if (template_mem_access
)
25669 case none_type
: return 0; // No preference.
25670 case scope_type
: return 1; // Type or namespace.
25671 default: return 2; // Type only.
25675 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25676 NAME should have one of the representations used for an
25677 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25678 is returned. If PARSER->SCOPE is a dependent type, then a
25679 SCOPE_REF is returned.
25681 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25682 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25683 was formed. Abstractly, such entities should not be passed to this
25684 function, because they do not need to be looked up, but it is
25685 simpler to check for this special case here, rather than at the
25688 In cases not explicitly covered above, this function returns a
25689 DECL, OVERLOAD, or baselink representing the result of the lookup.
25690 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25693 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25694 (e.g., "struct") that was used. In that case bindings that do not
25695 refer to types are ignored.
25697 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25700 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25703 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25706 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25707 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25708 NULL_TREE otherwise. */
25711 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25712 enum tag_types tag_type
,
25715 bool check_dependency
,
25716 tree
*ambiguous_decls
,
25717 location_t name_location
)
25720 tree object_type
= parser
->context
->object_type
;
25722 /* Assume that the lookup will be unambiguous. */
25723 if (ambiguous_decls
)
25724 *ambiguous_decls
= NULL_TREE
;
25726 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25727 no longer valid. Note that if we are parsing tentatively, and
25728 the parse fails, OBJECT_TYPE will be automatically restored. */
25729 parser
->context
->object_type
= NULL_TREE
;
25731 if (name
== error_mark_node
)
25732 return error_mark_node
;
25734 /* A template-id has already been resolved; there is no lookup to
25736 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25738 if (BASELINK_P (name
))
25740 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25741 == TEMPLATE_ID_EXPR
);
25745 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25746 it should already have been checked to make sure that the name
25747 used matches the type being destroyed. */
25748 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25752 /* Figure out to which type this destructor applies. */
25754 type
= parser
->scope
;
25755 else if (object_type
)
25756 type
= object_type
;
25758 type
= current_class_type
;
25759 /* If that's not a class type, there is no destructor. */
25760 if (!type
|| !CLASS_TYPE_P (type
))
25761 return error_mark_node
;
25763 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25764 lazily_declare_fn (sfk_destructor
, type
);
25766 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
25769 return error_mark_node
;
25772 /* By this point, the NAME should be an ordinary identifier. If
25773 the id-expression was a qualified name, the qualifying scope is
25774 stored in PARSER->SCOPE at this point. */
25775 gcc_assert (identifier_p (name
));
25777 /* Perform the lookup. */
25782 if (parser
->scope
== error_mark_node
)
25783 return error_mark_node
;
25785 /* If the SCOPE is dependent, the lookup must be deferred until
25786 the template is instantiated -- unless we are explicitly
25787 looking up names in uninstantiated templates. Even then, we
25788 cannot look up the name if the scope is not a class type; it
25789 might, for example, be a template type parameter. */
25790 dependent_p
= (TYPE_P (parser
->scope
)
25791 && dependent_scope_p (parser
->scope
));
25792 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25794 /* Defer lookup. */
25795 decl
= error_mark_node
;
25798 tree pushed_scope
= NULL_TREE
;
25800 /* If PARSER->SCOPE is a dependent type, then it must be a
25801 class type, and we must not be checking dependencies;
25802 otherwise, we would have processed this lookup above. So
25803 that PARSER->SCOPE is not considered a dependent base by
25804 lookup_member, we must enter the scope here. */
25806 pushed_scope
= push_scope (parser
->scope
);
25808 /* If the PARSER->SCOPE is a template specialization, it
25809 may be instantiated during name lookup. In that case,
25810 errors may be issued. Even if we rollback the current
25811 tentative parse, those errors are valid. */
25812 decl
= lookup_qualified_name (parser
->scope
, name
,
25813 prefer_type_arg (tag_type
),
25814 /*complain=*/true);
25816 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25817 lookup result and the nested-name-specifier nominates a class C:
25818 * if the name specified after the nested-name-specifier, when
25819 looked up in C, is the injected-class-name of C (Clause 9), or
25820 * if the name specified after the nested-name-specifier is the
25821 same as the identifier or the simple-template-id's template-
25822 name in the last component of the nested-name-specifier,
25823 the name is instead considered to name the constructor of
25824 class C. [ Note: for example, the constructor is not an
25825 acceptable lookup result in an elaborated-type-specifier so
25826 the constructor would not be used in place of the
25827 injected-class-name. --end note ] Such a constructor name
25828 shall be used only in the declarator-id of a declaration that
25829 names a constructor or in a using-declaration. */
25830 if (tag_type
== none_type
25831 && DECL_SELF_REFERENCE_P (decl
)
25832 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25833 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25834 prefer_type_arg (tag_type
),
25835 /*complain=*/true);
25837 /* If we have a single function from a using decl, pull it out. */
25838 if (TREE_CODE (decl
) == OVERLOAD
25839 && !really_overloaded_fn (decl
))
25840 decl
= OVL_FUNCTION (decl
);
25843 pop_scope (pushed_scope
);
25846 /* If the scope is a dependent type and either we deferred lookup or
25847 we did lookup but didn't find the name, rememeber the name. */
25848 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25849 && dependent_type_p (parser
->scope
))
25855 /* The resolution to Core Issue 180 says that `struct
25856 A::B' should be considered a type-name, even if `A'
25858 type
= make_typename_type (parser
->scope
, name
, tag_type
,
25859 /*complain=*/tf_error
);
25860 if (type
!= error_mark_node
)
25861 decl
= TYPE_NAME (type
);
25863 else if (is_template
25864 && (cp_parser_next_token_ends_template_argument_p (parser
)
25865 || cp_lexer_next_token_is (parser
->lexer
,
25867 decl
= make_unbound_class_template (parser
->scope
,
25869 /*complain=*/tf_error
);
25871 decl
= build_qualified_name (/*type=*/NULL_TREE
,
25872 parser
->scope
, name
,
25875 parser
->qualifying_scope
= parser
->scope
;
25876 parser
->object_scope
= NULL_TREE
;
25878 else if (object_type
)
25880 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25881 OBJECT_TYPE is not a class. */
25882 if (CLASS_TYPE_P (object_type
))
25883 /* If the OBJECT_TYPE is a template specialization, it may
25884 be instantiated during name lookup. In that case, errors
25885 may be issued. Even if we rollback the current tentative
25886 parse, those errors are valid. */
25887 decl
= lookup_member (object_type
,
25890 prefer_type_arg (tag_type
),
25891 tf_warning_or_error
);
25896 /* Look it up in the enclosing context. DR 141: When looking for a
25897 template-name after -> or ., only consider class templates. */
25898 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
25900 /*block_p=*/true, is_namespace
, 0);
25901 if (object_type
== unknown_type_node
)
25902 /* The object is type-dependent, so we can't look anything up; we used
25903 this to get the DR 141 behavior. */
25904 object_type
= NULL_TREE
;
25905 parser
->object_scope
= object_type
;
25906 parser
->qualifying_scope
= NULL_TREE
;
25910 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
25912 /*block_p=*/true, is_namespace
, 0);
25913 parser
->qualifying_scope
= NULL_TREE
;
25914 parser
->object_scope
= NULL_TREE
;
25917 /* If the lookup failed, let our caller know. */
25918 if (!decl
|| decl
== error_mark_node
)
25919 return error_mark_node
;
25921 /* Pull out the template from an injected-class-name (or multiple). */
25923 decl
= maybe_get_template_decl_from_type_decl (decl
);
25925 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25926 if (TREE_CODE (decl
) == TREE_LIST
)
25928 if (ambiguous_decls
)
25929 *ambiguous_decls
= decl
;
25930 /* The error message we have to print is too complicated for
25931 cp_parser_error, so we incorporate its actions directly. */
25932 if (!cp_parser_simulate_error (parser
))
25934 error_at (name_location
, "reference to %qD is ambiguous",
25936 print_candidates (decl
);
25938 return error_mark_node
;
25941 gcc_assert (DECL_P (decl
)
25942 || TREE_CODE (decl
) == OVERLOAD
25943 || TREE_CODE (decl
) == SCOPE_REF
25944 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
25945 || BASELINK_P (decl
));
25947 /* If we have resolved the name of a member declaration, check to
25948 see if the declaration is accessible. When the name resolves to
25949 set of overloaded functions, accessibility is checked when
25950 overload resolution is done.
25952 During an explicit instantiation, access is not checked at all,
25953 as per [temp.explicit]. */
25955 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
25957 maybe_record_typedef_use (decl
);
25959 return cp_expr (decl
, name_location
);
25962 /* Like cp_parser_lookup_name, but for use in the typical case where
25963 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25964 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25967 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
25969 return cp_parser_lookup_name (parser
, name
,
25971 /*is_template=*/false,
25972 /*is_namespace=*/false,
25973 /*check_dependency=*/true,
25974 /*ambiguous_decls=*/NULL
,
25978 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25979 the current context, return the TYPE_DECL. If TAG_NAME_P is
25980 true, the DECL indicates the class being defined in a class-head,
25981 or declared in an elaborated-type-specifier.
25983 Otherwise, return DECL. */
25986 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
25988 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25989 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25992 template <typename T> struct B;
25995 template <typename T> struct A::B {};
25997 Similarly, in an elaborated-type-specifier:
25999 namespace N { struct X{}; }
26002 template <typename T> friend struct N::X;
26005 However, if the DECL refers to a class type, and we are in
26006 the scope of the class, then the name lookup automatically
26007 finds the TYPE_DECL created by build_self_reference rather
26008 than a TEMPLATE_DECL. For example, in:
26010 template <class T> struct S {
26014 there is no need to handle such case. */
26016 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26017 return DECL_TEMPLATE_RESULT (decl
);
26022 /* If too many, or too few, template-parameter lists apply to the
26023 declarator, issue an error message. Returns TRUE if all went well,
26024 and FALSE otherwise. */
26027 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26028 cp_declarator
*declarator
,
26029 location_t declarator_location
)
26031 switch (declarator
->kind
)
26035 unsigned num_templates
= 0;
26036 tree scope
= declarator
->u
.id
.qualifying_scope
;
26039 num_templates
= num_template_headers_for_class (scope
);
26040 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26041 == TEMPLATE_ID_EXPR
)
26042 /* If the DECLARATOR has the form `X<y>' then it uses one
26043 additional level of template parameters. */
26046 return cp_parser_check_template_parameters
26047 (parser
, num_templates
, declarator_location
, declarator
);
26053 case cdk_reference
:
26055 return (cp_parser_check_declarator_template_parameters
26056 (parser
, declarator
->declarator
, declarator_location
));
26063 gcc_unreachable ();
26068 /* NUM_TEMPLATES were used in the current declaration. If that is
26069 invalid, return FALSE and issue an error messages. Otherwise,
26070 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26071 declarator and we can print more accurate diagnostics. */
26074 cp_parser_check_template_parameters (cp_parser
* parser
,
26075 unsigned num_templates
,
26076 location_t location
,
26077 cp_declarator
*declarator
)
26079 /* If there are the same number of template classes and parameter
26080 lists, that's OK. */
26081 if (parser
->num_template_parameter_lists
== num_templates
)
26083 /* If there are more, but only one more, then we are referring to a
26084 member template. That's OK too. */
26085 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26087 /* If there are more template classes than parameter lists, we have
26090 template <class T> void S<T>::R<T>::f (); */
26091 if (parser
->num_template_parameter_lists
< num_templates
)
26093 if (declarator
&& !current_function_decl
)
26094 error_at (location
, "specializing member %<%T::%E%> "
26095 "requires %<template<>%> syntax",
26096 declarator
->u
.id
.qualifying_scope
,
26097 declarator
->u
.id
.unqualified_name
);
26098 else if (declarator
)
26099 error_at (location
, "invalid declaration of %<%T::%E%>",
26100 declarator
->u
.id
.qualifying_scope
,
26101 declarator
->u
.id
.unqualified_name
);
26103 error_at (location
, "too few template-parameter-lists");
26106 /* Otherwise, there are too many template parameter lists. We have
26109 template <class T> template <class U> void S::f(); */
26110 error_at (location
, "too many template-parameter-lists");
26114 /* Parse an optional `::' token indicating that the following name is
26115 from the global namespace. If so, PARSER->SCOPE is set to the
26116 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26117 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26118 Returns the new value of PARSER->SCOPE, if the `::' token is
26119 present, and NULL_TREE otherwise. */
26122 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26126 /* Peek at the next token. */
26127 token
= cp_lexer_peek_token (parser
->lexer
);
26128 /* If we're looking at a `::' token then we're starting from the
26129 global namespace, not our current location. */
26130 if (token
->type
== CPP_SCOPE
)
26132 /* Consume the `::' token. */
26133 cp_lexer_consume_token (parser
->lexer
);
26134 /* Set the SCOPE so that we know where to start the lookup. */
26135 parser
->scope
= global_namespace
;
26136 parser
->qualifying_scope
= global_namespace
;
26137 parser
->object_scope
= NULL_TREE
;
26139 return parser
->scope
;
26141 else if (!current_scope_valid_p
)
26143 parser
->scope
= NULL_TREE
;
26144 parser
->qualifying_scope
= NULL_TREE
;
26145 parser
->object_scope
= NULL_TREE
;
26151 /* Returns TRUE if the upcoming token sequence is the start of a
26152 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26153 declarator is preceded by the `friend' specifier. */
26156 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26158 bool constructor_p
;
26159 bool outside_class_specifier_p
;
26160 tree nested_name_specifier
;
26161 cp_token
*next_token
;
26163 /* The common case is that this is not a constructor declarator, so
26164 try to avoid doing lots of work if at all possible. It's not
26165 valid declare a constructor at function scope. */
26166 if (parser
->in_function_body
)
26168 /* And only certain tokens can begin a constructor declarator. */
26169 next_token
= cp_lexer_peek_token (parser
->lexer
);
26170 if (next_token
->type
!= CPP_NAME
26171 && next_token
->type
!= CPP_SCOPE
26172 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26173 && next_token
->type
!= CPP_TEMPLATE_ID
)
26176 /* Parse tentatively; we are going to roll back all of the tokens
26178 cp_parser_parse_tentatively (parser
);
26179 /* Assume that we are looking at a constructor declarator. */
26180 constructor_p
= true;
26182 /* Look for the optional `::' operator. */
26183 cp_parser_global_scope_opt (parser
,
26184 /*current_scope_valid_p=*/false);
26185 /* Look for the nested-name-specifier. */
26186 nested_name_specifier
26187 = (cp_parser_nested_name_specifier_opt (parser
,
26188 /*typename_keyword_p=*/false,
26189 /*check_dependency_p=*/false,
26191 /*is_declaration=*/false));
26193 outside_class_specifier_p
= (!at_class_scope_p ()
26194 || !TYPE_BEING_DEFINED (current_class_type
)
26197 /* Outside of a class-specifier, there must be a
26198 nested-name-specifier. Except in C++17 mode, where we
26199 might be declaring a guiding declaration. */
26200 if (!nested_name_specifier
&& outside_class_specifier_p
26201 && cxx_dialect
< cxx17
)
26202 constructor_p
= false;
26203 else if (nested_name_specifier
== error_mark_node
)
26204 constructor_p
= false;
26206 /* If we have a class scope, this is easy; DR 147 says that S::S always
26207 names the constructor, and no other qualified name could. */
26208 if (constructor_p
&& nested_name_specifier
26209 && CLASS_TYPE_P (nested_name_specifier
))
26211 tree id
= cp_parser_unqualified_id (parser
,
26212 /*template_keyword_p=*/false,
26213 /*check_dependency_p=*/false,
26214 /*declarator_p=*/true,
26215 /*optional_p=*/false);
26216 if (is_overloaded_fn (id
))
26217 id
= DECL_NAME (get_first_fn (id
));
26218 if (!constructor_name_p (id
, nested_name_specifier
))
26219 constructor_p
= false;
26221 /* If we still think that this might be a constructor-declarator,
26222 look for a class-name. */
26223 else if (constructor_p
)
26227 template <typename T> struct S {
26231 we must recognize that the nested `S' names a class. */
26232 if (cxx_dialect
>= cxx17
)
26233 cp_parser_parse_tentatively (parser
);
26236 type_decl
= cp_parser_class_name (parser
,
26237 /*typename_keyword_p=*/false,
26238 /*template_keyword_p=*/false,
26240 /*check_dependency_p=*/false,
26241 /*class_head_p=*/false,
26242 /*is_declaration=*/false);
26244 if (cxx_dialect
>= cxx17
26245 && !cp_parser_parse_definitely (parser
))
26247 type_decl
= NULL_TREE
;
26248 tree tmpl
= cp_parser_template_name (parser
,
26249 /*template_keyword*/false,
26250 /*check_dependency_p*/false,
26251 /*is_declaration*/false,
26253 /*is_identifier*/NULL
);
26254 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26255 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26256 /* It's a deduction guide, return true. */;
26258 cp_parser_simulate_error (parser
);
26261 /* If there was no class-name, then this is not a constructor.
26262 Otherwise, if we are in a class-specifier and we aren't
26263 handling a friend declaration, check that its type matches
26264 current_class_type (c++/38313). Note: error_mark_node
26265 is left alone for error recovery purposes. */
26266 constructor_p
= (!cp_parser_error_occurred (parser
)
26267 && (outside_class_specifier_p
26268 || type_decl
== NULL_TREE
26269 || type_decl
== error_mark_node
26270 || same_type_p (current_class_type
,
26271 TREE_TYPE (type_decl
))));
26273 /* If we're still considering a constructor, we have to see a `(',
26274 to begin the parameter-declaration-clause, followed by either a
26275 `)', an `...', or a decl-specifier. We need to check for a
26276 type-specifier to avoid being fooled into thinking that:
26280 is a constructor. (It is actually a function named `f' that
26281 takes one parameter (of type `int') and returns a value of type
26284 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26285 constructor_p
= false;
26288 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26289 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26290 /* A parameter declaration begins with a decl-specifier,
26291 which is either the "attribute" keyword, a storage class
26292 specifier, or (usually) a type-specifier. */
26293 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26296 tree pushed_scope
= NULL_TREE
;
26297 unsigned saved_num_template_parameter_lists
;
26299 /* Names appearing in the type-specifier should be looked up
26300 in the scope of the class. */
26301 if (current_class_type
)
26303 else if (type_decl
)
26305 type
= TREE_TYPE (type_decl
);
26306 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26308 type
= resolve_typename_type (type
,
26309 /*only_current_p=*/false);
26310 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26312 cp_parser_abort_tentative_parse (parser
);
26316 pushed_scope
= push_scope (type
);
26319 /* Inside the constructor parameter list, surrounding
26320 template-parameter-lists do not apply. */
26321 saved_num_template_parameter_lists
26322 = parser
->num_template_parameter_lists
;
26323 parser
->num_template_parameter_lists
= 0;
26325 /* Look for the type-specifier. */
26326 cp_parser_type_specifier (parser
,
26327 CP_PARSER_FLAGS_NONE
,
26328 /*decl_specs=*/NULL
,
26329 /*is_declarator=*/true,
26330 /*declares_class_or_enum=*/NULL
,
26331 /*is_cv_qualifier=*/NULL
);
26333 parser
->num_template_parameter_lists
26334 = saved_num_template_parameter_lists
;
26336 /* Leave the scope of the class. */
26338 pop_scope (pushed_scope
);
26340 constructor_p
= !cp_parser_error_occurred (parser
);
26344 /* We did not really want to consume any tokens. */
26345 cp_parser_abort_tentative_parse (parser
);
26347 return constructor_p
;
26350 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26351 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26352 they must be performed once we are in the scope of the function.
26354 Returns the function defined. */
26357 cp_parser_function_definition_from_specifiers_and_declarator
26358 (cp_parser
* parser
,
26359 cp_decl_specifier_seq
*decl_specifiers
,
26361 const cp_declarator
*declarator
)
26366 /* Begin the function-definition. */
26367 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26369 /* The things we're about to see are not directly qualified by any
26370 template headers we've seen thus far. */
26371 reset_specialization ();
26373 /* If there were names looked up in the decl-specifier-seq that we
26374 did not check, check them now. We must wait until we are in the
26375 scope of the function to perform the checks, since the function
26376 might be a friend. */
26377 perform_deferred_access_checks (tf_warning_or_error
);
26381 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26382 parser
->omp_declare_simd
= NULL
;
26383 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26384 parser
->oacc_routine
= NULL
;
26389 /* Skip the entire function. */
26390 cp_parser_skip_to_end_of_block_or_statement (parser
);
26391 fn
= error_mark_node
;
26393 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26395 /* Seen already, skip it. An error message has already been output. */
26396 cp_parser_skip_to_end_of_block_or_statement (parser
);
26397 fn
= current_function_decl
;
26398 current_function_decl
= NULL_TREE
;
26399 /* If this is a function from a class, pop the nested class. */
26400 if (current_class_name
)
26401 pop_nested_class ();
26406 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26407 tv
= TV_PARSE_INLINE
;
26409 tv
= TV_PARSE_FUNC
;
26411 fn
= cp_parser_function_definition_after_declarator (parser
,
26412 /*inline_p=*/false);
26419 /* Parse the part of a function-definition that follows the
26420 declarator. INLINE_P is TRUE iff this function is an inline
26421 function defined within a class-specifier.
26423 Returns the function defined. */
26426 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26430 bool ctor_initializer_p
= false;
26431 bool saved_in_unbraced_linkage_specification_p
;
26432 bool saved_in_function_body
;
26433 unsigned saved_num_template_parameter_lists
;
26435 bool fully_implicit_function_template_p
26436 = parser
->fully_implicit_function_template_p
;
26437 parser
->fully_implicit_function_template_p
= false;
26438 tree implicit_template_parms
26439 = parser
->implicit_template_parms
;
26440 parser
->implicit_template_parms
= 0;
26441 cp_binding_level
* implicit_template_scope
26442 = parser
->implicit_template_scope
;
26443 parser
->implicit_template_scope
= 0;
26445 saved_in_function_body
= parser
->in_function_body
;
26446 parser
->in_function_body
= true;
26447 /* If the next token is `return', then the code may be trying to
26448 make use of the "named return value" extension that G++ used to
26450 token
= cp_lexer_peek_token (parser
->lexer
);
26451 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26453 /* Consume the `return' keyword. */
26454 cp_lexer_consume_token (parser
->lexer
);
26455 /* Look for the identifier that indicates what value is to be
26457 cp_parser_identifier (parser
);
26458 /* Issue an error message. */
26459 error_at (token
->location
,
26460 "named return values are no longer supported");
26461 /* Skip tokens until we reach the start of the function body. */
26464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26465 if (token
->type
== CPP_OPEN_BRACE
26466 || token
->type
== CPP_EOF
26467 || token
->type
== CPP_PRAGMA_EOL
)
26469 cp_lexer_consume_token (parser
->lexer
);
26472 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26473 anything declared inside `f'. */
26474 saved_in_unbraced_linkage_specification_p
26475 = parser
->in_unbraced_linkage_specification_p
;
26476 parser
->in_unbraced_linkage_specification_p
= false;
26477 /* Inside the function, surrounding template-parameter-lists do not
26479 saved_num_template_parameter_lists
26480 = parser
->num_template_parameter_lists
;
26481 parser
->num_template_parameter_lists
= 0;
26483 /* If the next token is `try', `__transaction_atomic', or
26484 `__transaction_relaxed`, then we are looking at either function-try-block
26485 or function-transaction-block. Note that all of these include the
26487 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26488 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26489 RID_TRANSACTION_ATOMIC
);
26490 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26491 RID_TRANSACTION_RELAXED
))
26492 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26493 RID_TRANSACTION_RELAXED
);
26494 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26495 ctor_initializer_p
= cp_parser_function_try_block (parser
);
26497 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
26498 (parser
, /*in_function_try_block=*/false);
26500 /* Finish the function. */
26501 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
26502 (inline_p
? 2 : 0));
26503 /* Generate code for it, if necessary. */
26504 expand_or_defer_fn (fn
);
26505 /* Restore the saved values. */
26506 parser
->in_unbraced_linkage_specification_p
26507 = saved_in_unbraced_linkage_specification_p
;
26508 parser
->num_template_parameter_lists
26509 = saved_num_template_parameter_lists
;
26510 parser
->in_function_body
= saved_in_function_body
;
26512 parser
->fully_implicit_function_template_p
26513 = fully_implicit_function_template_p
;
26514 parser
->implicit_template_parms
26515 = implicit_template_parms
;
26516 parser
->implicit_template_scope
26517 = implicit_template_scope
;
26519 if (parser
->fully_implicit_function_template_p
)
26520 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26525 /* Parse a template-declaration body (following argument list). */
26528 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26529 tree parameter_list
,
26532 tree decl
= NULL_TREE
;
26533 bool friend_p
= false;
26535 /* We just processed one more parameter list. */
26536 ++parser
->num_template_parameter_lists
;
26538 /* Get the deferred access checks from the parameter list. These
26539 will be checked once we know what is being declared, as for a
26540 member template the checks must be performed in the scope of the
26541 class containing the member. */
26542 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26544 /* Tentatively parse for a new template parameter list, which can either be
26545 the template keyword or a template introduction. */
26546 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26548 else if (cxx_dialect
>= cxx11
26549 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26550 decl
= cp_parser_alias_declaration (parser
);
26553 /* There are no access checks when parsing a template, as we do not
26554 know if a specialization will be a friend. */
26555 push_deferring_access_checks (dk_no_check
);
26556 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26557 decl
= cp_parser_single_declaration (parser
,
26560 /*explicit_specialization_p=*/false,
26562 pop_deferring_access_checks ();
26564 /* If this is a member template declaration, let the front
26566 if (member_p
&& !friend_p
&& decl
)
26568 if (TREE_CODE (decl
) == TYPE_DECL
)
26569 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26571 decl
= finish_member_template_decl (decl
);
26573 else if (friend_p
&& decl
26574 && DECL_DECLARES_TYPE_P (decl
))
26575 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26576 /*complain=*/true);
26578 /* We are done with the current parameter list. */
26579 --parser
->num_template_parameter_lists
;
26581 pop_deferring_access_checks ();
26584 finish_template_decl (parameter_list
);
26586 /* Check the template arguments for a literal operator template. */
26588 && DECL_DECLARES_FUNCTION_P (decl
)
26589 && UDLIT_OPER_P (DECL_NAME (decl
)))
26592 if (parameter_list
== NULL_TREE
)
26596 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26597 if (num_parms
== 1)
26599 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26600 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26601 if (TREE_TYPE (parm
) != char_type_node
26602 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26605 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26607 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26608 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26609 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26610 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26611 if (parm
== error_mark_node
26612 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26613 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26621 if (cxx_dialect
>= cxx14
)
26622 error ("literal operator template %qD has invalid parameter list."
26623 " Expected non-type template argument pack <char...>"
26624 " or <typename CharT, CharT...>",
26627 error ("literal operator template %qD has invalid parameter list."
26628 " Expected non-type template argument pack <char...>",
26633 /* Register member declarations. */
26634 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26635 finish_member_declaration (decl
);
26636 /* If DECL is a function template, we must return to parse it later.
26637 (Even though there is no definition, there might be default
26638 arguments that need handling.) */
26639 if (member_p
&& decl
26640 && DECL_DECLARES_FUNCTION_P (decl
))
26641 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26644 /* Parse a template introduction header for a template-declaration. Returns
26645 false if tentative parse fails. */
26648 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26650 cp_parser_parse_tentatively (parser
);
26652 tree saved_scope
= parser
->scope
;
26653 tree saved_object_scope
= parser
->object_scope
;
26654 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26656 /* Look for the optional `::' operator. */
26657 cp_parser_global_scope_opt (parser
,
26658 /*current_scope_valid_p=*/false);
26659 /* Look for the nested-name-specifier. */
26660 cp_parser_nested_name_specifier_opt (parser
,
26661 /*typename_keyword_p=*/false,
26662 /*check_dependency_p=*/true,
26664 /*is_declaration=*/false);
26666 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26667 tree concept_name
= cp_parser_identifier (parser
);
26669 /* Look up the concept for which we will be matching
26670 template parameters. */
26671 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26673 parser
->scope
= saved_scope
;
26674 parser
->object_scope
= saved_object_scope
;
26675 parser
->qualifying_scope
= saved_qualifying_scope
;
26677 if (concept_name
== error_mark_node
)
26678 cp_parser_simulate_error (parser
);
26680 /* Look for opening brace for introduction. */
26681 matching_braces braces
;
26682 braces
.require_open (parser
);
26684 if (!cp_parser_parse_definitely (parser
))
26687 push_deferring_access_checks (dk_deferred
);
26689 /* Build vector of placeholder parameters and grab
26690 matching identifiers. */
26691 tree introduction_list
= cp_parser_introduction_list (parser
);
26693 /* The introduction-list shall not be empty. */
26694 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26697 error ("empty introduction-list");
26701 /* Look for closing brace for introduction. */
26702 if (!braces
.require_close (parser
))
26705 if (tmpl_decl
== error_mark_node
)
26707 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26712 /* Build and associate the constraint. */
26713 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26714 if (parms
&& parms
!= error_mark_node
)
26716 cp_parser_template_declaration_after_parameters (parser
, parms
,
26721 error_at (token
->location
, "no matching concept for template-introduction");
26725 /* Parse a normal template-declaration following the template keyword. */
26728 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26730 tree parameter_list
;
26731 bool need_lang_pop
;
26732 location_t location
= input_location
;
26734 /* Look for the `<' token. */
26735 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26737 if (at_class_scope_p () && current_function_decl
)
26739 /* 14.5.2.2 [temp.mem]
26741 A local class shall not have member templates. */
26742 error_at (location
,
26743 "invalid declaration of member template in local class");
26744 cp_parser_skip_to_end_of_block_or_statement (parser
);
26749 A template ... shall not have C linkage. */
26750 if (current_lang_name
== lang_name_c
)
26752 error_at (location
, "template with C linkage");
26753 /* Give it C++ linkage to avoid confusing other parts of the
26755 push_lang_context (lang_name_cplusplus
);
26756 need_lang_pop
= true;
26759 need_lang_pop
= false;
26761 /* We cannot perform access checks on the template parameter
26762 declarations until we know what is being declared, just as we
26763 cannot check the decl-specifier list. */
26764 push_deferring_access_checks (dk_deferred
);
26766 /* If the next token is `>', then we have an invalid
26767 specialization. Rather than complain about an invalid template
26768 parameter, issue an error message here. */
26769 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26771 cp_parser_error (parser
, "invalid explicit specialization");
26772 begin_specialization ();
26773 parameter_list
= NULL_TREE
;
26777 /* Parse the template parameters. */
26778 parameter_list
= cp_parser_template_parameter_list (parser
);
26781 /* Look for the `>'. */
26782 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26784 /* Manage template requirements */
26787 tree reqs
= get_shorthand_constraints (current_template_parms
);
26788 if (tree r
= cp_parser_requires_clause_opt (parser
))
26789 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26790 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26793 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26796 /* For the erroneous case of a template with C linkage, we pushed an
26797 implicit C++ linkage scope; exit that scope now. */
26799 pop_lang_context ();
26802 /* Parse a template-declaration, assuming that the `export' (and
26803 `extern') keywords, if present, has already been scanned. MEMBER_P
26804 is as for cp_parser_template_declaration. */
26807 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26809 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26811 cp_lexer_consume_token (parser
->lexer
);
26812 cp_parser_explicit_template_declaration (parser
, member_p
);
26815 else if (flag_concepts
)
26816 return cp_parser_template_introduction (parser
, member_p
);
26821 /* Perform the deferred access checks from a template-parameter-list.
26822 CHECKS is a TREE_LIST of access checks, as returned by
26823 get_deferred_access_checks. */
26826 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26828 ++processing_template_parmlist
;
26829 perform_access_checks (checks
, tf_warning_or_error
);
26830 --processing_template_parmlist
;
26833 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26834 `function-definition' sequence that follows a template header.
26835 If MEMBER_P is true, this declaration appears in a class scope.
26837 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26838 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26841 cp_parser_single_declaration (cp_parser
* parser
,
26842 vec
<deferred_access_check
, va_gc
> *checks
,
26844 bool explicit_specialization_p
,
26847 int declares_class_or_enum
;
26848 tree decl
= NULL_TREE
;
26849 cp_decl_specifier_seq decl_specifiers
;
26850 bool function_definition_p
= false;
26851 cp_token
*decl_spec_token_start
;
26853 /* This function is only used when processing a template
26855 gcc_assert (innermost_scope_kind () == sk_template_parms
26856 || innermost_scope_kind () == sk_template_spec
);
26858 /* Defer access checks until we know what is being declared. */
26859 push_deferring_access_checks (dk_deferred
);
26861 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26863 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
26864 cp_parser_decl_specifier_seq (parser
,
26865 CP_PARSER_FLAGS_OPTIONAL
,
26867 &declares_class_or_enum
);
26869 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
26871 /* There are no template typedefs. */
26872 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
26874 error_at (decl_spec_token_start
->location
,
26875 "template declaration of %<typedef%>");
26876 decl
= error_mark_node
;
26879 /* Gather up the access checks that occurred the
26880 decl-specifier-seq. */
26881 stop_deferring_access_checks ();
26883 /* Check for the declaration of a template class. */
26884 if (declares_class_or_enum
)
26886 if (cp_parser_declares_only_class_p (parser
)
26887 || (declares_class_or_enum
& 2))
26889 // If this is a declaration, but not a definition, associate
26890 // any constraints with the type declaration. Constraints
26891 // are associated with definitions in cp_parser_class_specifier.
26892 if (declares_class_or_enum
== 1)
26893 associate_classtype_constraints (decl_specifiers
.type
);
26895 decl
= shadow_tag (&decl_specifiers
);
26900 friend template <typename T> struct A<T>::B;
26903 A<T>::B will be represented by a TYPENAME_TYPE, and
26904 therefore not recognized by shadow_tag. */
26905 if (friend_p
&& *friend_p
26907 && decl_specifiers
.type
26908 && TYPE_P (decl_specifiers
.type
))
26909 decl
= decl_specifiers
.type
;
26911 if (decl
&& decl
!= error_mark_node
)
26912 decl
= TYPE_NAME (decl
);
26914 decl
= error_mark_node
;
26916 /* Perform access checks for template parameters. */
26917 cp_parser_perform_template_parameter_access_checks (checks
);
26919 /* Give a helpful diagnostic for
26920 template <class T> struct A { } a;
26921 if we aren't already recovering from an error. */
26922 if (!cp_parser_declares_only_class_p (parser
)
26925 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26926 "a class template declaration must not declare "
26928 cp_parser_skip_to_end_of_block_or_statement (parser
);
26934 /* Complain about missing 'typename' or other invalid type names. */
26935 if (!decl_specifiers
.any_type_specifiers_p
26936 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
26938 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26939 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26940 the rest of this declaration. */
26941 decl
= error_mark_node
;
26945 /* If it's not a template class, try for a template function. If
26946 the next token is a `;', then this declaration does not declare
26947 anything. But, if there were errors in the decl-specifiers, then
26948 the error might well have come from an attempted class-specifier.
26949 In that case, there's no need to warn about a missing declarator. */
26951 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
26952 || decl_specifiers
.type
!= error_mark_node
))
26954 decl
= cp_parser_init_declarator (parser
,
26957 /*function_definition_allowed_p=*/true,
26959 declares_class_or_enum
,
26960 &function_definition_p
,
26963 /* 7.1.1-1 [dcl.stc]
26965 A storage-class-specifier shall not be specified in an explicit
26966 specialization... */
26968 && explicit_specialization_p
26969 && decl_specifiers
.storage_class
!= sc_none
)
26971 error_at (decl_spec_token_start
->location
,
26972 "explicit template specialization cannot have a storage class");
26973 decl
= error_mark_node
;
26976 if (decl
&& VAR_P (decl
))
26977 check_template_variable (decl
);
26980 /* Look for a trailing `;' after the declaration. */
26981 if (!function_definition_p
26982 && (decl
== error_mark_node
26983 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
26984 cp_parser_skip_to_end_of_block_or_statement (parser
);
26987 pop_deferring_access_checks ();
26989 /* Clear any current qualification; whatever comes next is the start
26990 of something new. */
26991 parser
->scope
= NULL_TREE
;
26992 parser
->qualifying_scope
= NULL_TREE
;
26993 parser
->object_scope
= NULL_TREE
;
26998 /* Parse a cast-expression that is not the operand of a unary "&". */
27001 cp_parser_simple_cast_expression (cp_parser
*parser
)
27003 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27004 /*cast_p=*/false, /*decltype*/false, NULL
);
27007 /* Parse a functional cast to TYPE. Returns an expression
27008 representing the cast. */
27011 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27013 vec
<tree
, va_gc
> *vec
;
27014 tree expression_list
;
27018 location_t start_loc
= input_location
;
27021 type
= error_mark_node
;
27023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27025 cp_lexer_set_source_position (parser
->lexer
);
27026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27027 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27028 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27029 if (TREE_CODE (type
) == TYPE_DECL
)
27030 type
= TREE_TYPE (type
);
27032 cast
= finish_compound_literal (type
, expression_list
,
27033 tf_warning_or_error
, fcl_functional
);
27034 /* Create a location of the form:
27037 with caret == start at the start of the type name,
27038 finishing at the closing brace. */
27039 location_t finish_loc
27040 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27041 location_t combined_loc
= make_location (start_loc
, start_loc
,
27043 cast
.set_location (combined_loc
);
27048 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27050 /*allow_expansion_p=*/true,
27051 /*non_constant_p=*/NULL
);
27053 expression_list
= error_mark_node
;
27056 expression_list
= build_tree_list_vec (vec
);
27057 release_tree_vector (vec
);
27060 cast
= build_functional_cast (type
, expression_list
,
27061 tf_warning_or_error
);
27062 /* [expr.const]/1: In an integral constant expression "only type
27063 conversions to integral or enumeration type can be used". */
27064 if (TREE_CODE (type
) == TYPE_DECL
)
27065 type
= TREE_TYPE (type
);
27066 if (cast
!= error_mark_node
27067 && !cast_valid_in_integral_constant_expression_p (type
)
27068 && cp_parser_non_integral_constant_expression (parser
,
27070 return error_mark_node
;
27072 /* Create a location of the form:
27075 with caret == start at the start of the type name,
27076 finishing at the closing paren. */
27077 location_t finish_loc
27078 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27079 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27080 cast
.set_location (combined_loc
);
27084 /* Save the tokens that make up the body of a member function defined
27085 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27086 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27087 specifiers applied to the declaration. Returns the FUNCTION_DECL
27088 for the member function. */
27091 cp_parser_save_member_function_body (cp_parser
* parser
,
27092 cp_decl_specifier_seq
*decl_specifiers
,
27093 cp_declarator
*declarator
,
27099 bool function_try_block
= false;
27101 /* Create the FUNCTION_DECL. */
27102 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27103 cp_finalize_omp_declare_simd (parser
, fn
);
27104 cp_finalize_oacc_routine (parser
, fn
, true);
27105 /* If something went badly wrong, bail out now. */
27106 if (fn
== error_mark_node
)
27108 /* If there's a function-body, skip it. */
27109 if (cp_parser_token_starts_function_definition_p
27110 (cp_lexer_peek_token (parser
->lexer
)))
27111 cp_parser_skip_to_end_of_block_or_statement (parser
);
27112 return error_mark_node
;
27115 /* Remember it, if there default args to post process. */
27116 cp_parser_save_default_args (parser
, fn
);
27118 /* Save away the tokens that make up the body of the
27120 first
= parser
->lexer
->next_token
;
27122 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27123 cp_lexer_consume_token (parser
->lexer
);
27124 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27125 RID_TRANSACTION_ATOMIC
))
27127 cp_lexer_consume_token (parser
->lexer
);
27128 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27130 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27131 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27132 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27133 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27134 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27136 cp_lexer_consume_token (parser
->lexer
);
27137 cp_lexer_consume_token (parser
->lexer
);
27138 cp_lexer_consume_token (parser
->lexer
);
27139 cp_lexer_consume_token (parser
->lexer
);
27140 cp_lexer_consume_token (parser
->lexer
);
27143 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27144 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27146 cp_lexer_consume_token (parser
->lexer
);
27147 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27152 /* Handle function try blocks. */
27153 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27155 cp_lexer_consume_token (parser
->lexer
);
27156 function_try_block
= true;
27158 /* We can have braced-init-list mem-initializers before the fn body. */
27159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27161 cp_lexer_consume_token (parser
->lexer
);
27162 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27164 /* cache_group will stop after an un-nested { } pair, too. */
27165 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27168 /* variadic mem-inits have ... after the ')'. */
27169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27170 cp_lexer_consume_token (parser
->lexer
);
27173 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27174 /* Handle function try blocks. */
27175 if (function_try_block
)
27176 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27177 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27178 last
= parser
->lexer
->next_token
;
27180 /* Save away the inline definition; we will process it when the
27181 class is complete. */
27182 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27183 DECL_PENDING_INLINE_P (fn
) = 1;
27185 /* We need to know that this was defined in the class, so that
27186 friend templates are handled correctly. */
27187 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27189 /* Add FN to the queue of functions to be parsed later. */
27190 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27195 /* Save the tokens that make up the in-class initializer for a non-static
27196 data member. Returns a DEFAULT_ARG. */
27199 cp_parser_save_nsdmi (cp_parser
* parser
)
27201 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27204 /* Parse a template-argument-list, as well as the trailing ">" (but
27205 not the opening "<"). See cp_parser_template_argument_list for the
27209 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27213 tree saved_qualifying_scope
;
27214 tree saved_object_scope
;
27215 bool saved_greater_than_is_operator_p
;
27216 int saved_unevaluated_operand
;
27217 int saved_inhibit_evaluation_warnings
;
27221 When parsing a template-id, the first non-nested `>' is taken as
27222 the end of the template-argument-list rather than a greater-than
27224 saved_greater_than_is_operator_p
27225 = parser
->greater_than_is_operator_p
;
27226 parser
->greater_than_is_operator_p
= false;
27227 /* Parsing the argument list may modify SCOPE, so we save it
27229 saved_scope
= parser
->scope
;
27230 saved_qualifying_scope
= parser
->qualifying_scope
;
27231 saved_object_scope
= parser
->object_scope
;
27232 /* We need to evaluate the template arguments, even though this
27233 template-id may be nested within a "sizeof". */
27234 saved_unevaluated_operand
= cp_unevaluated_operand
;
27235 cp_unevaluated_operand
= 0;
27236 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27237 c_inhibit_evaluation_warnings
= 0;
27238 /* Parse the template-argument-list itself. */
27239 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27240 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27241 arguments
= NULL_TREE
;
27243 arguments
= cp_parser_template_argument_list (parser
);
27244 /* Look for the `>' that ends the template-argument-list. If we find
27245 a '>>' instead, it's probably just a typo. */
27246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27248 if (cxx_dialect
!= cxx98
)
27250 /* In C++0x, a `>>' in a template argument list or cast
27251 expression is considered to be two separate `>'
27252 tokens. So, change the current token to a `>', but don't
27253 consume it: it will be consumed later when the outer
27254 template argument list (or cast expression) is parsed.
27255 Note that this replacement of `>' for `>>' is necessary
27256 even if we are parsing tentatively: in the tentative
27257 case, after calling
27258 cp_parser_enclosed_template_argument_list we will always
27259 throw away all of the template arguments and the first
27260 closing `>', either because the template argument list
27261 was erroneous or because we are replacing those tokens
27262 with a CPP_TEMPLATE_ID token. The second `>' (which will
27263 not have been thrown away) is needed either to close an
27264 outer template argument list or to complete a new-style
27266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27267 token
->type
= CPP_GREATER
;
27269 else if (!saved_greater_than_is_operator_p
)
27271 /* If we're in a nested template argument list, the '>>' has
27272 to be a typo for '> >'. We emit the error message, but we
27273 continue parsing and we push a '>' as next token, so that
27274 the argument list will be parsed correctly. Note that the
27275 global source location is still on the token before the
27276 '>>', so we need to say explicitly where we want it. */
27277 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27278 gcc_rich_location
richloc (token
->location
);
27279 richloc
.add_fixit_replace ("> >");
27280 error_at_rich_loc (&richloc
, "%<>>%> should be %<> >%> "
27281 "within a nested template argument list");
27283 token
->type
= CPP_GREATER
;
27287 /* If this is not a nested template argument list, the '>>'
27288 is a typo for '>'. Emit an error message and continue.
27289 Same deal about the token location, but here we can get it
27290 right by consuming the '>>' before issuing the diagnostic. */
27291 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27292 error_at (token
->location
,
27293 "spurious %<>>%>, use %<>%> to terminate "
27294 "a template argument list");
27298 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27299 /* The `>' token might be a greater-than operator again now. */
27300 parser
->greater_than_is_operator_p
27301 = saved_greater_than_is_operator_p
;
27302 /* Restore the SAVED_SCOPE. */
27303 parser
->scope
= saved_scope
;
27304 parser
->qualifying_scope
= saved_qualifying_scope
;
27305 parser
->object_scope
= saved_object_scope
;
27306 cp_unevaluated_operand
= saved_unevaluated_operand
;
27307 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27312 /* MEMBER_FUNCTION is a member function, or a friend. If default
27313 arguments, or the body of the function have not yet been parsed,
27317 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27319 timevar_push (TV_PARSE_INMETH
);
27320 /* If this member is a template, get the underlying
27322 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27323 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27325 /* There should not be any class definitions in progress at this
27326 point; the bodies of members are only parsed outside of all class
27328 gcc_assert (parser
->num_classes_being_defined
== 0);
27329 /* While we're parsing the member functions we might encounter more
27330 classes. We want to handle them right away, but we don't want
27331 them getting mixed up with functions that are currently in the
27333 push_unparsed_function_queues (parser
);
27335 /* Make sure that any template parameters are in scope. */
27336 maybe_begin_member_template_processing (member_function
);
27338 /* If the body of the function has not yet been parsed, parse it
27340 if (DECL_PENDING_INLINE_P (member_function
))
27342 tree function_scope
;
27343 cp_token_cache
*tokens
;
27345 /* The function is no longer pending; we are processing it. */
27346 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27347 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27348 DECL_PENDING_INLINE_P (member_function
) = 0;
27350 /* If this is a local class, enter the scope of the containing
27352 function_scope
= current_function_decl
;
27353 if (function_scope
)
27354 push_function_context ();
27356 /* Push the body of the function onto the lexer stack. */
27357 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27359 /* Let the front end know that we going to be defining this
27361 start_preparsed_function (member_function
, NULL_TREE
,
27362 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27364 /* Don't do access checking if it is a templated function. */
27365 if (processing_template_decl
)
27366 push_deferring_access_checks (dk_no_check
);
27368 /* #pragma omp declare reduction needs special parsing. */
27369 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27371 parser
->lexer
->in_pragma
= true;
27372 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27373 finish_function (/*inline*/2);
27374 cp_check_omp_declare_reduction (member_function
);
27377 /* Now, parse the body of the function. */
27378 cp_parser_function_definition_after_declarator (parser
,
27379 /*inline_p=*/true);
27381 if (processing_template_decl
)
27382 pop_deferring_access_checks ();
27384 /* Leave the scope of the containing function. */
27385 if (function_scope
)
27386 pop_function_context ();
27387 cp_parser_pop_lexer (parser
);
27390 /* Remove any template parameters from the symbol table. */
27391 maybe_end_member_template_processing ();
27393 /* Restore the queue. */
27394 pop_unparsed_function_queues (parser
);
27395 timevar_pop (TV_PARSE_INMETH
);
27398 /* If DECL contains any default args, remember it on the unparsed
27399 functions queue. */
27402 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27406 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27408 probe
= TREE_CHAIN (probe
))
27409 if (TREE_PURPOSE (probe
))
27411 cp_default_arg_entry entry
= {current_class_type
, decl
};
27412 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27417 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27418 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27419 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27420 from the parameter-type-list. */
27423 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27424 tree default_arg
, tree parmtype
)
27426 cp_token_cache
*tokens
;
27430 if (default_arg
== error_mark_node
)
27431 return error_mark_node
;
27433 /* Push the saved tokens for the default argument onto the parser's
27435 tokens
= DEFARG_TOKENS (default_arg
);
27436 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27438 start_lambda_scope (decl
);
27440 /* Parse the default argument. */
27441 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27442 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27443 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27445 finish_lambda_scope ();
27447 if (parsed_arg
== error_mark_node
)
27448 cp_parser_skip_to_end_of_statement (parser
);
27450 if (!processing_template_decl
)
27452 /* In a non-template class, check conversions now. In a template,
27453 we'll wait and instantiate these as needed. */
27454 if (TREE_CODE (decl
) == PARM_DECL
)
27455 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27456 tf_warning_or_error
);
27457 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27458 parsed_arg
= error_mark_node
;
27460 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27463 /* If the token stream has not been completely used up, then
27464 there was extra junk after the end of the default
27466 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27468 if (TREE_CODE (decl
) == PARM_DECL
)
27469 cp_parser_error (parser
, "expected %<,%>");
27471 cp_parser_error (parser
, "expected %<;%>");
27474 /* Revert to the main lexer. */
27475 cp_parser_pop_lexer (parser
);
27480 /* FIELD is a non-static data member with an initializer which we saved for
27481 later; parse it now. */
27484 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27488 maybe_begin_member_template_processing (field
);
27490 push_unparsed_function_queues (parser
);
27491 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27492 DECL_INITIAL (field
),
27494 pop_unparsed_function_queues (parser
);
27496 maybe_end_member_template_processing ();
27498 DECL_INITIAL (field
) = def
;
27501 /* FN is a FUNCTION_DECL which may contains a parameter with an
27502 unparsed DEFAULT_ARG. Parse the default args now. This function
27503 assumes that the current scope is the scope in which the default
27504 argument should be processed. */
27507 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27509 bool saved_local_variables_forbidden_p
;
27510 tree parm
, parmdecl
;
27512 /* While we're parsing the default args, we might (due to the
27513 statement expression extension) encounter more classes. We want
27514 to handle them right away, but we don't want them getting mixed
27515 up with default args that are currently in the queue. */
27516 push_unparsed_function_queues (parser
);
27518 /* Local variable names (and the `this' keyword) may not appear
27519 in a default argument. */
27520 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27521 parser
->local_variables_forbidden_p
= true;
27523 push_defarg_context (fn
);
27525 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27526 parmdecl
= DECL_ARGUMENTS (fn
);
27527 parm
&& parm
!= void_list_node
;
27528 parm
= TREE_CHAIN (parm
),
27529 parmdecl
= DECL_CHAIN (parmdecl
))
27531 tree default_arg
= TREE_PURPOSE (parm
);
27533 vec
<tree
, va_gc
> *insts
;
27540 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27541 /* This can happen for a friend declaration for a function
27542 already declared with default arguments. */
27546 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27548 TREE_VALUE (parm
));
27549 TREE_PURPOSE (parm
) = parsed_arg
;
27551 /* Update any instantiations we've already created. */
27552 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27553 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27554 TREE_PURPOSE (copy
) = parsed_arg
;
27557 pop_defarg_context ();
27559 /* Make sure no default arg is missing. */
27560 check_default_args (fn
);
27562 /* Restore the state of local_variables_forbidden_p. */
27563 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27565 /* Restore the queue. */
27566 pop_unparsed_function_queues (parser
);
27569 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27571 sizeof ... ( identifier )
27573 where the 'sizeof' token has already been consumed. */
27576 cp_parser_sizeof_pack (cp_parser
*parser
)
27578 /* Consume the `...'. */
27579 cp_lexer_consume_token (parser
->lexer
);
27580 maybe_warn_variadic_templates ();
27582 matching_parens parens
;
27583 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27585 parens
.consume_open (parser
);
27587 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27588 "%<sizeof...%> argument must be surrounded by parentheses");
27590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27591 tree name
= cp_parser_identifier (parser
);
27592 if (name
== error_mark_node
)
27593 return error_mark_node
;
27594 /* The name is not qualified. */
27595 parser
->scope
= NULL_TREE
;
27596 parser
->qualifying_scope
= NULL_TREE
;
27597 parser
->object_scope
= NULL_TREE
;
27598 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27599 if (expr
== error_mark_node
)
27600 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27602 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27603 expr
= TREE_TYPE (expr
);
27604 else if (TREE_CODE (expr
) == CONST_DECL
)
27605 expr
= DECL_INITIAL (expr
);
27606 expr
= make_pack_expansion (expr
);
27607 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27610 parens
.require_close (parser
);
27615 /* Parse the operand of `sizeof' (or a similar operator). Returns
27616 either a TYPE or an expression, depending on the form of the
27617 input. The KEYWORD indicates which kind of expression we have
27621 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27623 tree expr
= NULL_TREE
;
27624 const char *saved_message
;
27626 bool saved_integral_constant_expression_p
;
27627 bool saved_non_integral_constant_expression_p
;
27629 /* If it's a `...', then we are computing the length of a parameter
27631 if (keyword
== RID_SIZEOF
27632 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27633 return cp_parser_sizeof_pack (parser
);
27635 /* Types cannot be defined in a `sizeof' expression. Save away the
27637 saved_message
= parser
->type_definition_forbidden_message
;
27638 /* And create the new one. */
27639 tmp
= concat ("types may not be defined in %<",
27640 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27641 "%> expressions", NULL
);
27642 parser
->type_definition_forbidden_message
= tmp
;
27644 /* The restrictions on constant-expressions do not apply inside
27645 sizeof expressions. */
27646 saved_integral_constant_expression_p
27647 = parser
->integral_constant_expression_p
;
27648 saved_non_integral_constant_expression_p
27649 = parser
->non_integral_constant_expression_p
;
27650 parser
->integral_constant_expression_p
= false;
27652 /* Do not actually evaluate the expression. */
27653 ++cp_unevaluated_operand
;
27654 ++c_inhibit_evaluation_warnings
;
27655 /* If it's a `(', then we might be looking at the type-id
27657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27659 tree type
= NULL_TREE
;
27661 /* We can't be sure yet whether we're looking at a type-id or an
27663 cp_parser_parse_tentatively (parser
);
27665 matching_parens parens
;
27666 parens
.consume_open (parser
);
27668 /* Note: as a GNU Extension, compound literals are considered
27669 postfix-expressions as they are in C99, so they are valid
27670 arguments to sizeof. See comment in cp_parser_cast_expression
27672 if (cp_parser_compound_literal_p (parser
))
27673 cp_parser_simulate_error (parser
);
27676 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27677 parser
->in_type_id_in_expr_p
= true;
27678 /* Look for the type-id. */
27679 type
= cp_parser_type_id (parser
);
27680 /* Look for the closing `)'. */
27681 parens
.require_close (parser
);
27682 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27685 /* If all went well, then we're done. */
27686 if (cp_parser_parse_definitely (parser
))
27688 cp_decl_specifier_seq decl_specs
;
27690 /* Build a trivial decl-specifier-seq. */
27691 clear_decl_specs (&decl_specs
);
27692 decl_specs
.type
= type
;
27694 /* Call grokdeclarator to figure out what type this is. */
27695 expr
= grokdeclarator (NULL
,
27699 /*attrlist=*/NULL
);
27703 /* If the type-id production did not work out, then we must be
27704 looking at the unary-expression production. */
27706 expr
= cp_parser_unary_expression (parser
);
27708 /* Go back to evaluating expressions. */
27709 --cp_unevaluated_operand
;
27710 --c_inhibit_evaluation_warnings
;
27712 /* Free the message we created. */
27714 /* And restore the old one. */
27715 parser
->type_definition_forbidden_message
= saved_message
;
27716 parser
->integral_constant_expression_p
27717 = saved_integral_constant_expression_p
;
27718 parser
->non_integral_constant_expression_p
27719 = saved_non_integral_constant_expression_p
;
27724 /* If the current declaration has no declarator, return true. */
27727 cp_parser_declares_only_class_p (cp_parser
*parser
)
27729 /* If the next token is a `;' or a `,' then there is no
27731 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27732 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27735 /* Update the DECL_SPECS to reflect the storage class indicated by
27739 cp_parser_set_storage_class (cp_parser
*parser
,
27740 cp_decl_specifier_seq
*decl_specs
,
27744 cp_storage_class storage_class
;
27746 if (parser
->in_unbraced_linkage_specification_p
)
27748 error_at (token
->location
, "invalid use of %qD in linkage specification",
27749 ridpointers
[keyword
]);
27752 else if (decl_specs
->storage_class
!= sc_none
)
27754 decl_specs
->conflicting_specifiers_p
= true;
27758 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27759 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27760 && decl_specs
->gnu_thread_keyword_p
)
27762 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27763 "%<__thread%> before %qD", ridpointers
[keyword
]);
27769 storage_class
= sc_auto
;
27772 storage_class
= sc_register
;
27775 storage_class
= sc_static
;
27778 storage_class
= sc_extern
;
27781 storage_class
= sc_mutable
;
27784 gcc_unreachable ();
27786 decl_specs
->storage_class
= storage_class
;
27787 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27789 /* A storage class specifier cannot be applied alongside a typedef
27790 specifier. If there is a typedef specifier present then set
27791 conflicting_specifiers_p which will trigger an error later
27792 on in grokdeclarator. */
27793 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27794 decl_specs
->conflicting_specifiers_p
= true;
27797 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27798 is true, the type is a class or enum definition. */
27801 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27804 bool type_definition_p
)
27806 decl_specs
->any_specifiers_p
= true;
27808 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27809 (with, for example, in "typedef int wchar_t;") we remember that
27810 this is what happened. In system headers, we ignore these
27811 declarations so that G++ can work with system headers that are not
27813 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27814 && !type_definition_p
27815 && (type_spec
== boolean_type_node
27816 || type_spec
== char16_type_node
27817 || type_spec
== char32_type_node
27818 || type_spec
== wchar_type_node
)
27819 && (decl_specs
->type
27820 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27821 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27822 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27823 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27825 decl_specs
->redefined_builtin_type
= type_spec
;
27826 set_and_check_decl_spec_loc (decl_specs
,
27827 ds_redefined_builtin_type_spec
,
27829 if (!decl_specs
->type
)
27831 decl_specs
->type
= type_spec
;
27832 decl_specs
->type_definition_p
= false;
27833 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27836 else if (decl_specs
->type
)
27837 decl_specs
->multiple_types_p
= true;
27840 decl_specs
->type
= type_spec
;
27841 decl_specs
->type_definition_p
= type_definition_p
;
27842 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27843 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27847 /* True iff TOKEN is the GNU keyword __thread. */
27850 token_is__thread (cp_token
*token
)
27852 gcc_assert (token
->keyword
== RID_THREAD
);
27853 return id_equal (token
->u
.value
, "__thread");
27856 /* Set the location for a declarator specifier and check if it is
27859 DECL_SPECS is the sequence of declarator specifiers onto which to
27862 DS is the single declarator specifier to set which location is to
27863 be set onto the existing sequence of declarators.
27865 LOCATION is the location for the declarator specifier to
27869 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
27870 cp_decl_spec ds
, cp_token
*token
)
27872 gcc_assert (ds
< ds_last
);
27874 if (decl_specs
== NULL
)
27877 source_location location
= token
->location
;
27879 if (decl_specs
->locations
[ds
] == 0)
27881 decl_specs
->locations
[ds
] = location
;
27882 if (ds
== ds_thread
)
27883 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
27889 if (decl_specs
->locations
[ds_long_long
] != 0)
27890 error_at (location
,
27891 "%<long long long%> is too long for GCC");
27894 decl_specs
->locations
[ds_long_long
] = location
;
27895 pedwarn_cxx98 (location
,
27897 "ISO C++ 1998 does not support %<long long%>");
27900 else if (ds
== ds_thread
)
27902 bool gnu
= token_is__thread (token
);
27903 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
27904 error_at (location
,
27905 "both %<__thread%> and %<thread_local%> specified");
27908 gcc_rich_location
richloc (location
);
27909 richloc
.add_fixit_remove ();
27910 error_at_rich_loc (&richloc
, "duplicate %qD", token
->u
.value
);
27915 static const char *const decl_spec_names
[] = {
27932 gcc_rich_location
richloc (location
);
27933 richloc
.add_fixit_remove ();
27934 error_at_rich_loc (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
27939 /* Return true iff the declarator specifier DS is present in the
27940 sequence of declarator specifiers DECL_SPECS. */
27943 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
27946 gcc_assert (ds
< ds_last
);
27948 if (decl_specs
== NULL
)
27951 return decl_specs
->locations
[ds
] != 0;
27954 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27955 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27958 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
27960 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
27963 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
27966 static const char *
27967 get_matching_symbol (required_token token_desc
)
27969 switch (token_desc
)
27972 gcc_unreachable ();
27974 case RT_CLOSE_BRACE
:
27976 case RT_CLOSE_PAREN
:
27981 /* Issue an error message indicating that TOKEN_DESC was expected.
27982 If KEYWORD is true, it indicated this function is called by
27983 cp_parser_require_keword and the required token can only be
27984 a indicated keyword.
27986 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
27987 within any error as the location of an "opening" token matching
27988 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
27989 RT_CLOSE_PAREN). */
27992 cp_parser_required_error (cp_parser
*parser
,
27993 required_token token_desc
,
27995 location_t matching_location
)
27997 if (cp_parser_simulate_error (parser
))
28000 const char *gmsgid
= NULL
;
28001 switch (token_desc
)
28004 gmsgid
= G_("expected %<new%>");
28007 gmsgid
= G_("expected %<delete%>");
28010 gmsgid
= G_("expected %<return%>");
28013 gmsgid
= G_("expected %<while%>");
28016 gmsgid
= G_("expected %<extern%>");
28018 case RT_STATIC_ASSERT
:
28019 gmsgid
= G_("expected %<static_assert%>");
28022 gmsgid
= G_("expected %<decltype%>");
28025 gmsgid
= G_("expected %<operator%>");
28028 gmsgid
= G_("expected %<class%>");
28031 gmsgid
= G_("expected %<template%>");
28034 gmsgid
= G_("expected %<namespace%>");
28037 gmsgid
= G_("expected %<using%>");
28040 gmsgid
= G_("expected %<asm%>");
28043 gmsgid
= G_("expected %<try%>");
28046 gmsgid
= G_("expected %<catch%>");
28049 gmsgid
= G_("expected %<throw%>");
28052 gmsgid
= G_("expected %<__label__%>");
28055 gmsgid
= G_("expected %<@try%>");
28057 case RT_AT_SYNCHRONIZED
:
28058 gmsgid
= G_("expected %<@synchronized%>");
28061 gmsgid
= G_("expected %<@throw%>");
28063 case RT_TRANSACTION_ATOMIC
:
28064 gmsgid
= G_("expected %<__transaction_atomic%>");
28066 case RT_TRANSACTION_RELAXED
:
28067 gmsgid
= G_("expected %<__transaction_relaxed%>");
28073 if (!gmsgid
&& !keyword
)
28075 switch (token_desc
)
28078 gmsgid
= G_("expected %<;%>");
28080 case RT_OPEN_PAREN
:
28081 gmsgid
= G_("expected %<(%>");
28083 case RT_CLOSE_BRACE
:
28084 gmsgid
= G_("expected %<}%>");
28086 case RT_OPEN_BRACE
:
28087 gmsgid
= G_("expected %<{%>");
28089 case RT_CLOSE_SQUARE
:
28090 gmsgid
= G_("expected %<]%>");
28092 case RT_OPEN_SQUARE
:
28093 gmsgid
= G_("expected %<[%>");
28096 gmsgid
= G_("expected %<,%>");
28099 gmsgid
= G_("expected %<::%>");
28102 gmsgid
= G_("expected %<<%>");
28105 gmsgid
= G_("expected %<>%>");
28108 gmsgid
= G_("expected %<=%>");
28111 gmsgid
= G_("expected %<...%>");
28114 gmsgid
= G_("expected %<*%>");
28117 gmsgid
= G_("expected %<~%>");
28120 gmsgid
= G_("expected %<:%>");
28122 case RT_COLON_SCOPE
:
28123 gmsgid
= G_("expected %<:%> or %<::%>");
28125 case RT_CLOSE_PAREN
:
28126 gmsgid
= G_("expected %<)%>");
28128 case RT_COMMA_CLOSE_PAREN
:
28129 gmsgid
= G_("expected %<,%> or %<)%>");
28131 case RT_PRAGMA_EOL
:
28132 gmsgid
= G_("expected end of line");
28135 gmsgid
= G_("expected identifier");
28138 gmsgid
= G_("expected selection-statement");
28141 gmsgid
= G_("expected iteration-statement");
28144 gmsgid
= G_("expected jump-statement");
28147 gmsgid
= G_("expected class-key");
28149 case RT_CLASS_TYPENAME_TEMPLATE
:
28150 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28153 gcc_unreachable ();
28159 /* Emulate rest of cp_parser_error. */
28160 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28161 cp_lexer_set_source_position_from_token (token
);
28163 gcc_rich_location
richloc (input_location
);
28165 /* If matching_location != UNKNOWN_LOCATION, highlight it.
28166 Attempt to consolidate diagnostics by printing it as a
28167 secondary range within the main diagnostic. */
28168 bool added_matching_location
= false;
28169 if (matching_location
!= UNKNOWN_LOCATION
)
28170 added_matching_location
28171 = richloc
.add_location_if_nearby (matching_location
);
28173 c_parse_error (gmsgid
,
28174 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
28175 token
->u
.value
, token
->flags
, &richloc
);
28177 /* If we weren't able to consolidate matching_location, then
28178 print it as a secondary diagnostic. */
28179 if (matching_location
!= UNKNOWN_LOCATION
&& !added_matching_location
)
28180 inform (matching_location
, "to match this %qs",
28181 get_matching_symbol (token_desc
));
28186 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28187 issue an error message indicating that TOKEN_DESC was expected.
28189 Returns the token consumed, if the token had the appropriate type.
28190 Otherwise, returns NULL.
28192 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28193 within any error as the location of an "opening" token matching
28194 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28195 RT_CLOSE_PAREN). */
28198 cp_parser_require (cp_parser
* parser
,
28199 enum cpp_ttype type
,
28200 required_token token_desc
,
28201 location_t matching_location
)
28203 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28204 return cp_lexer_consume_token (parser
->lexer
);
28207 /* Output the MESSAGE -- unless we're parsing tentatively. */
28208 if (!cp_parser_simulate_error (parser
))
28209 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28210 matching_location
);
28215 /* An error message is produced if the next token is not '>'.
28216 All further tokens are skipped until the desired token is
28217 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28220 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28222 /* Current level of '< ... >'. */
28223 unsigned level
= 0;
28224 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28225 unsigned nesting_depth
= 0;
28227 /* Are we ready, yet? If not, issue error message. */
28228 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28231 /* Skip tokens until the desired token is found. */
28234 /* Peek at the next token. */
28235 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28238 if (!nesting_depth
)
28243 if (cxx_dialect
== cxx98
)
28244 /* C++0x views the `>>' operator as two `>' tokens, but
28247 else if (!nesting_depth
&& level
-- == 0)
28249 /* We've hit a `>>' where the first `>' closes the
28250 template argument list, and the second `>' is
28251 spurious. Just consume the `>>' and stop; we've
28252 already produced at least one error. */
28253 cp_lexer_consume_token (parser
->lexer
);
28256 /* Fall through for C++0x, so we handle the second `>' in
28258 gcc_fallthrough ();
28261 if (!nesting_depth
&& level
-- == 0)
28263 /* We've reached the token we want, consume it and stop. */
28264 cp_lexer_consume_token (parser
->lexer
);
28269 case CPP_OPEN_PAREN
:
28270 case CPP_OPEN_SQUARE
:
28274 case CPP_CLOSE_PAREN
:
28275 case CPP_CLOSE_SQUARE
:
28276 if (nesting_depth
-- == 0)
28281 case CPP_PRAGMA_EOL
:
28282 case CPP_SEMICOLON
:
28283 case CPP_OPEN_BRACE
:
28284 case CPP_CLOSE_BRACE
:
28285 /* The '>' was probably forgotten, don't look further. */
28292 /* Consume this token. */
28293 cp_lexer_consume_token (parser
->lexer
);
28297 /* If the next token is the indicated keyword, consume it. Otherwise,
28298 issue an error message indicating that TOKEN_DESC was expected.
28300 Returns the token consumed, if the token had the appropriate type.
28301 Otherwise, returns NULL. */
28304 cp_parser_require_keyword (cp_parser
* parser
,
28306 required_token token_desc
)
28308 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28310 if (token
&& token
->keyword
!= keyword
)
28312 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28320 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28321 function-definition. */
28324 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28326 return (/* An ordinary function-body begins with an `{'. */
28327 token
->type
== CPP_OPEN_BRACE
28328 /* A ctor-initializer begins with a `:'. */
28329 || token
->type
== CPP_COLON
28330 /* A function-try-block begins with `try'. */
28331 || token
->keyword
== RID_TRY
28332 /* A function-transaction-block begins with `__transaction_atomic'
28333 or `__transaction_relaxed'. */
28334 || token
->keyword
== RID_TRANSACTION_ATOMIC
28335 || token
->keyword
== RID_TRANSACTION_RELAXED
28336 /* The named return value extension begins with `return'. */
28337 || token
->keyword
== RID_RETURN
);
28340 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28344 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28348 token
= cp_lexer_peek_token (parser
->lexer
);
28349 return (token
->type
== CPP_OPEN_BRACE
28350 || (token
->type
== CPP_COLON
28351 && !parser
->colon_doesnt_start_class_def_p
));
28354 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28355 C++0x) ending a template-argument. */
28358 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28362 token
= cp_lexer_peek_token (parser
->lexer
);
28363 return (token
->type
== CPP_COMMA
28364 || token
->type
== CPP_GREATER
28365 || token
->type
== CPP_ELLIPSIS
28366 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28369 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28370 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28373 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28378 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28379 if (token
->type
== CPP_LESS
)
28381 /* Check for the sequence `<::' in the original code. It would be lexed as
28382 `[:', where `[' is a digraph, and there is no whitespace before
28384 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28387 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28388 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28394 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28395 or none_type otherwise. */
28397 static enum tag_types
28398 cp_parser_token_is_class_key (cp_token
* token
)
28400 switch (token
->keyword
)
28405 return record_type
;
28414 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28415 or none_type otherwise or if the token is null. */
28417 static enum tag_types
28418 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28423 switch (token
->keyword
)
28428 return typename_type
;
28435 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28438 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28440 if (type
== error_mark_node
)
28442 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28444 if (permerror (input_location
, "%qs tag used in naming %q#T",
28445 class_key
== union_type
? "union"
28446 : class_key
== record_type
? "struct" : "class",
28448 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28449 "%q#T was previously declared here", type
);
28453 /* Issue an error message if DECL is redeclared with different
28454 access than its original declaration [class.access.spec/3].
28455 This applies to nested classes, nested class templates and
28456 enumerations [class.mem/1]. */
28459 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28462 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28463 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28466 if ((TREE_PRIVATE (decl
)
28467 != (current_access_specifier
== access_private_node
))
28468 || (TREE_PROTECTED (decl
)
28469 != (current_access_specifier
== access_protected_node
)))
28470 error_at (location
, "%qD redeclared with different access", decl
);
28473 /* Look for the `template' keyword, as a syntactic disambiguator.
28474 Return TRUE iff it is present, in which case it will be
28478 cp_parser_optional_template_keyword (cp_parser
*parser
)
28480 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28482 /* In C++98 the `template' keyword can only be used within templates;
28483 outside templates the parser can always figure out what is a
28484 template and what is not. In C++11, per the resolution of DR 468,
28485 `template' is allowed in cases where it is not strictly necessary. */
28486 if (!processing_template_decl
28487 && pedantic
&& cxx_dialect
== cxx98
)
28489 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28490 pedwarn (token
->location
, OPT_Wpedantic
,
28491 "in C++98 %<template%> (as a disambiguator) is only "
28492 "allowed within templates");
28493 /* If this part of the token stream is rescanned, the same
28494 error message would be generated. So, we purge the token
28495 from the stream. */
28496 cp_lexer_purge_token (parser
->lexer
);
28501 /* Consume the `template' keyword. */
28502 cp_lexer_consume_token (parser
->lexer
);
28509 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28510 set PARSER->SCOPE, and perform other related actions. */
28513 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28515 struct tree_check
*check_value
;
28517 /* Get the stored value. */
28518 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28519 /* Set the scope from the stored value. */
28520 parser
->scope
= saved_checks_value (check_value
);
28521 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28522 parser
->object_scope
= NULL_TREE
;
28525 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28526 encounter the end of a block before what we were looking for. */
28529 cp_parser_cache_group (cp_parser
*parser
,
28530 enum cpp_ttype end
,
28535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28537 /* Abort a parenthesized expression if we encounter a semicolon. */
28538 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28539 && token
->type
== CPP_SEMICOLON
)
28541 /* If we've reached the end of the file, stop. */
28542 if (token
->type
== CPP_EOF
28543 || (end
!= CPP_PRAGMA_EOL
28544 && token
->type
== CPP_PRAGMA_EOL
))
28546 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28547 /* We've hit the end of an enclosing block, so there's been some
28548 kind of syntax error. */
28551 /* Consume the token. */
28552 cp_lexer_consume_token (parser
->lexer
);
28553 /* See if it starts a new group. */
28554 if (token
->type
== CPP_OPEN_BRACE
)
28556 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28557 /* In theory this should probably check end == '}', but
28558 cp_parser_save_member_function_body needs it to exit
28559 after either '}' or ')' when called with ')'. */
28563 else if (token
->type
== CPP_OPEN_PAREN
)
28565 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28566 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28569 else if (token
->type
== CPP_PRAGMA
)
28570 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28571 else if (token
->type
== end
)
28576 /* Like above, for caching a default argument or NSDMI. Both of these are
28577 terminated by a non-nested comma, but it can be unclear whether or not a
28578 comma is nested in a template argument list unless we do more parsing.
28579 In order to handle this ambiguity, when we encounter a ',' after a '<'
28580 we try to parse what follows as a parameter-declaration-list (in the
28581 case of a default argument) or a member-declarator (in the case of an
28582 NSDMI). If that succeeds, then we stop caching. */
28585 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28587 unsigned depth
= 0;
28588 int maybe_template_id
= 0;
28589 cp_token
*first_token
;
28591 tree default_argument
;
28593 /* Add tokens until we have processed the entire default
28594 argument. We add the range [first_token, token). */
28595 first_token
= cp_lexer_peek_token (parser
->lexer
);
28596 if (first_token
->type
== CPP_OPEN_BRACE
)
28598 /* For list-initialization, this is straightforward. */
28599 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28600 token
= cp_lexer_peek_token (parser
->lexer
);
28606 /* Peek at the next token. */
28607 token
= cp_lexer_peek_token (parser
->lexer
);
28608 /* What we do depends on what token we have. */
28609 switch (token
->type
)
28611 /* In valid code, a default argument must be
28612 immediately followed by a `,' `)', or `...'. */
28614 if (depth
== 0 && maybe_template_id
)
28616 /* If we've seen a '<', we might be in a
28617 template-argument-list. Until Core issue 325 is
28618 resolved, we don't know how this situation ought
28619 to be handled, so try to DTRT. We check whether
28620 what comes after the comma is a valid parameter
28621 declaration list. If it is, then the comma ends
28622 the default argument; otherwise the default
28623 argument continues. */
28624 bool error
= false;
28627 /* Set ITALP so cp_parser_parameter_declaration_list
28628 doesn't decide to commit to this parse. */
28629 bool saved_italp
= parser
->in_template_argument_list_p
;
28630 parser
->in_template_argument_list_p
= true;
28632 cp_parser_parse_tentatively (parser
);
28636 /* Parse declarators until we reach a non-comma or
28637 somthing that cannot be an initializer.
28638 Just checking whether we're looking at a single
28639 declarator is insufficient. Consider:
28640 int var = tuple<T,U>::x;
28641 The template parameter 'U' looks exactly like a
28645 int ctor_dtor_or_conv_p
;
28646 cp_lexer_consume_token (parser
->lexer
);
28647 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28648 &ctor_dtor_or_conv_p
,
28649 /*parenthesized_p=*/NULL
,
28651 /*friend_p=*/false);
28652 peek
= cp_lexer_peek_token (parser
->lexer
);
28653 if (cp_parser_error_occurred (parser
))
28656 while (peek
->type
== CPP_COMMA
);
28657 /* If we met an '=' or ';' then the original comma
28658 was the end of the NSDMI. Otherwise assume
28659 we're still in the NSDMI. */
28660 error
= (peek
->type
!= CPP_EQ
28661 && peek
->type
!= CPP_SEMICOLON
);
28665 cp_lexer_consume_token (parser
->lexer
);
28666 begin_scope (sk_function_parms
, NULL_TREE
);
28667 cp_parser_parameter_declaration_list (parser
, &error
);
28668 pop_bindings_and_leave_scope ();
28670 if (!cp_parser_error_occurred (parser
) && !error
)
28672 cp_parser_abort_tentative_parse (parser
);
28674 parser
->in_template_argument_list_p
= saved_italp
;
28678 case CPP_CLOSE_PAREN
:
28680 /* If we run into a non-nested `;', `}', or `]',
28681 then the code is invalid -- but the default
28682 argument is certainly over. */
28683 case CPP_SEMICOLON
:
28684 case CPP_CLOSE_BRACE
:
28685 case CPP_CLOSE_SQUARE
:
28687 /* Handle correctly int n = sizeof ... ( p ); */
28688 && token
->type
!= CPP_ELLIPSIS
)
28690 /* Update DEPTH, if necessary. */
28691 else if (token
->type
== CPP_CLOSE_PAREN
28692 || token
->type
== CPP_CLOSE_BRACE
28693 || token
->type
== CPP_CLOSE_SQUARE
)
28697 case CPP_OPEN_PAREN
:
28698 case CPP_OPEN_SQUARE
:
28699 case CPP_OPEN_BRACE
:
28705 /* This might be the comparison operator, or it might
28706 start a template argument list. */
28707 ++maybe_template_id
;
28711 if (cxx_dialect
== cxx98
)
28713 /* Fall through for C++0x, which treats the `>>'
28714 operator like two `>' tokens in certain
28716 gcc_fallthrough ();
28721 /* This might be an operator, or it might close a
28722 template argument list. But if a previous '<'
28723 started a template argument list, this will have
28724 closed it, so we can't be in one anymore. */
28725 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28726 if (maybe_template_id
< 0)
28727 maybe_template_id
= 0;
28731 /* If we run out of tokens, issue an error message. */
28733 case CPP_PRAGMA_EOL
:
28734 error_at (token
->location
, "file ends in default argument");
28735 return error_mark_node
;
28739 /* In these cases, we should look for template-ids.
28740 For example, if the default argument is
28741 `X<int, double>()', we need to do name lookup to
28742 figure out whether or not `X' is a template; if
28743 so, the `,' does not end the default argument.
28745 That is not yet done. */
28752 /* If we've reached the end, stop. */
28756 /* Add the token to the token block. */
28757 token
= cp_lexer_consume_token (parser
->lexer
);
28760 /* Create a DEFAULT_ARG to represent the unparsed default
28762 default_argument
= make_node (DEFAULT_ARG
);
28763 DEFARG_TOKENS (default_argument
)
28764 = cp_token_cache_new (first_token
, token
);
28765 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28767 return default_argument
;
28770 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28773 defarg_location (tree default_argument
)
28775 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
28776 location_t start
= tokens
->first
->location
;
28777 location_t end
= tokens
->last
->location
;
28778 return make_location (start
, start
, end
);
28781 /* Begin parsing tentatively. We always save tokens while parsing
28782 tentatively so that if the tentative parsing fails we can restore the
28786 cp_parser_parse_tentatively (cp_parser
* parser
)
28788 /* Enter a new parsing context. */
28789 parser
->context
= cp_parser_context_new (parser
->context
);
28790 /* Begin saving tokens. */
28791 cp_lexer_save_tokens (parser
->lexer
);
28792 /* In order to avoid repetitive access control error messages,
28793 access checks are queued up until we are no longer parsing
28795 push_deferring_access_checks (dk_deferred
);
28798 /* Commit to the currently active tentative parse. */
28801 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28803 cp_parser_context
*context
;
28806 /* Mark all of the levels as committed. */
28807 lexer
= parser
->lexer
;
28808 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28810 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28812 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28813 while (!cp_lexer_saving_tokens (lexer
))
28814 lexer
= lexer
->next
;
28815 cp_lexer_commit_tokens (lexer
);
28819 /* Commit to the topmost currently active tentative parse.
28821 Note that this function shouldn't be called when there are
28822 irreversible side-effects while in a tentative state. For
28823 example, we shouldn't create a permanent entry in the symbol
28824 table, or issue an error message that might not apply if the
28825 tentative parse is aborted. */
28828 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28830 cp_parser_context
*context
= parser
->context
;
28831 cp_lexer
*lexer
= parser
->lexer
;
28835 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28837 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28839 while (!cp_lexer_saving_tokens (lexer
))
28840 lexer
= lexer
->next
;
28841 cp_lexer_commit_tokens (lexer
);
28845 /* Abort the currently active tentative parse. All consumed tokens
28846 will be rolled back, and no diagnostics will be issued. */
28849 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28851 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28852 || errorcount
> 0);
28853 cp_parser_simulate_error (parser
);
28854 /* Now, pretend that we want to see if the construct was
28855 successfully parsed. */
28856 cp_parser_parse_definitely (parser
);
28859 /* Stop parsing tentatively. If a parse error has occurred, restore the
28860 token stream. Otherwise, commit to the tokens we have consumed.
28861 Returns true if no error occurred; false otherwise. */
28864 cp_parser_parse_definitely (cp_parser
* parser
)
28866 bool error_occurred
;
28867 cp_parser_context
*context
;
28869 /* Remember whether or not an error occurred, since we are about to
28870 destroy that information. */
28871 error_occurred
= cp_parser_error_occurred (parser
);
28872 /* Remove the topmost context from the stack. */
28873 context
= parser
->context
;
28874 parser
->context
= context
->next
;
28875 /* If no parse errors occurred, commit to the tentative parse. */
28876 if (!error_occurred
)
28878 /* Commit to the tokens read tentatively, unless that was
28880 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28881 cp_lexer_commit_tokens (parser
->lexer
);
28883 pop_to_parent_deferring_access_checks ();
28885 /* Otherwise, if errors occurred, roll back our state so that things
28886 are just as they were before we began the tentative parse. */
28889 cp_lexer_rollback_tokens (parser
->lexer
);
28890 pop_deferring_access_checks ();
28892 /* Add the context to the front of the free list. */
28893 context
->next
= cp_parser_context_free_list
;
28894 cp_parser_context_free_list
= context
;
28896 return !error_occurred
;
28899 /* Returns true if we are parsing tentatively and are not committed to
28900 this tentative parse. */
28903 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
28905 return (cp_parser_parsing_tentatively (parser
)
28906 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
28909 /* Returns nonzero iff an error has occurred during the most recent
28910 tentative parse. */
28913 cp_parser_error_occurred (cp_parser
* parser
)
28915 return (cp_parser_parsing_tentatively (parser
)
28916 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
28919 /* Returns nonzero if GNU extensions are allowed. */
28922 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
28924 return parser
->allow_gnu_extensions_p
;
28927 /* Objective-C++ Productions */
28930 /* Parse an Objective-C expression, which feeds into a primary-expression
28934 objc-message-expression
28935 objc-string-literal
28936 objc-encode-expression
28937 objc-protocol-expression
28938 objc-selector-expression
28940 Returns a tree representation of the expression. */
28943 cp_parser_objc_expression (cp_parser
* parser
)
28945 /* Try to figure out what kind of declaration is present. */
28946 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28950 case CPP_OPEN_SQUARE
:
28951 return cp_parser_objc_message_expression (parser
);
28953 case CPP_OBJC_STRING
:
28954 kwd
= cp_lexer_consume_token (parser
->lexer
);
28955 return objc_build_string_object (kwd
->u
.value
);
28958 switch (kwd
->keyword
)
28960 case RID_AT_ENCODE
:
28961 return cp_parser_objc_encode_expression (parser
);
28963 case RID_AT_PROTOCOL
:
28964 return cp_parser_objc_protocol_expression (parser
);
28966 case RID_AT_SELECTOR
:
28967 return cp_parser_objc_selector_expression (parser
);
28973 error_at (kwd
->location
,
28974 "misplaced %<@%D%> Objective-C++ construct",
28976 cp_parser_skip_to_end_of_block_or_statement (parser
);
28979 return error_mark_node
;
28982 /* Parse an Objective-C message expression.
28984 objc-message-expression:
28985 [ objc-message-receiver objc-message-args ]
28987 Returns a representation of an Objective-C message. */
28990 cp_parser_objc_message_expression (cp_parser
* parser
)
28992 tree receiver
, messageargs
;
28994 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28995 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
28996 receiver
= cp_parser_objc_message_receiver (parser
);
28997 messageargs
= cp_parser_objc_message_args (parser
);
28998 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28999 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29001 tree result
= objc_build_message_expr (receiver
, messageargs
);
29003 /* Construct a location e.g.
29006 ranging from the '[' to the ']', with the caret at the start. */
29007 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29008 protected_set_expr_location (result
, combined_loc
);
29013 /* Parse an objc-message-receiver.
29015 objc-message-receiver:
29017 simple-type-specifier
29019 Returns a representation of the type or expression. */
29022 cp_parser_objc_message_receiver (cp_parser
* parser
)
29026 /* An Objective-C message receiver may be either (1) a type
29027 or (2) an expression. */
29028 cp_parser_parse_tentatively (parser
);
29029 rcv
= cp_parser_expression (parser
);
29031 /* If that worked out, fine. */
29032 if (cp_parser_parse_definitely (parser
))
29035 cp_parser_parse_tentatively (parser
);
29036 rcv
= cp_parser_simple_type_specifier (parser
,
29037 /*decl_specs=*/NULL
,
29038 CP_PARSER_FLAGS_NONE
);
29040 if (cp_parser_parse_definitely (parser
))
29041 return objc_get_class_reference (rcv
);
29043 cp_parser_error (parser
, "objective-c++ message receiver expected");
29044 return error_mark_node
;
29047 /* Parse the arguments and selectors comprising an Objective-C message.
29052 objc-selector-args , objc-comma-args
29054 objc-selector-args:
29055 objc-selector [opt] : assignment-expression
29056 objc-selector-args objc-selector [opt] : assignment-expression
29059 assignment-expression
29060 objc-comma-args , assignment-expression
29062 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29063 selector arguments and TREE_VALUE containing a list of comma
29067 cp_parser_objc_message_args (cp_parser
* parser
)
29069 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29070 bool maybe_unary_selector_p
= true;
29071 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29073 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29075 tree selector
= NULL_TREE
, arg
;
29077 if (token
->type
!= CPP_COLON
)
29078 selector
= cp_parser_objc_selector (parser
);
29080 /* Detect if we have a unary selector. */
29081 if (maybe_unary_selector_p
29082 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29083 return build_tree_list (selector
, NULL_TREE
);
29085 maybe_unary_selector_p
= false;
29086 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29087 arg
= cp_parser_assignment_expression (parser
);
29090 = chainon (sel_args
,
29091 build_tree_list (selector
, arg
));
29093 token
= cp_lexer_peek_token (parser
->lexer
);
29096 /* Handle non-selector arguments, if any. */
29097 while (token
->type
== CPP_COMMA
)
29101 cp_lexer_consume_token (parser
->lexer
);
29102 arg
= cp_parser_assignment_expression (parser
);
29105 = chainon (addl_args
,
29106 build_tree_list (NULL_TREE
, arg
));
29108 token
= cp_lexer_peek_token (parser
->lexer
);
29111 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29113 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29114 return build_tree_list (error_mark_node
, error_mark_node
);
29117 return build_tree_list (sel_args
, addl_args
);
29120 /* Parse an Objective-C encode expression.
29122 objc-encode-expression:
29123 @encode objc-typename
29125 Returns an encoded representation of the type argument. */
29128 cp_parser_objc_encode_expression (cp_parser
* parser
)
29132 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29134 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29135 matching_parens parens
;
29136 parens
.require_open (parser
);
29137 token
= cp_lexer_peek_token (parser
->lexer
);
29138 type
= complete_type (cp_parser_type_id (parser
));
29139 parens
.require_close (parser
);
29143 error_at (token
->location
,
29144 "%<@encode%> must specify a type as an argument");
29145 return error_mark_node
;
29148 /* This happens if we find @encode(T) (where T is a template
29149 typename or something dependent on a template typename) when
29150 parsing a template. In that case, we can't compile it
29151 immediately, but we rather create an AT_ENCODE_EXPR which will
29152 need to be instantiated when the template is used.
29154 if (dependent_type_p (type
))
29156 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29157 TREE_READONLY (value
) = 1;
29162 /* Build a location of the form:
29165 with caret==start at the @ token, finishing at the close paren. */
29166 location_t combined_loc
29167 = make_location (start_loc
, start_loc
,
29168 cp_lexer_previous_token (parser
->lexer
)->location
);
29170 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29173 /* Parse an Objective-C @defs expression. */
29176 cp_parser_objc_defs_expression (cp_parser
*parser
)
29180 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29181 matching_parens parens
;
29182 parens
.require_open (parser
);
29183 name
= cp_parser_identifier (parser
);
29184 parens
.require_close (parser
);
29186 return objc_get_class_ivars (name
);
29189 /* Parse an Objective-C protocol expression.
29191 objc-protocol-expression:
29192 @protocol ( identifier )
29194 Returns a representation of the protocol expression. */
29197 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29200 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29202 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29203 matching_parens parens
;
29204 parens
.require_open (parser
);
29205 proto
= cp_parser_identifier (parser
);
29206 parens
.require_close (parser
);
29208 /* Build a location of the form:
29211 with caret==start at the @ token, finishing at the close paren. */
29212 location_t combined_loc
29213 = make_location (start_loc
, start_loc
,
29214 cp_lexer_previous_token (parser
->lexer
)->location
);
29215 tree result
= objc_build_protocol_expr (proto
);
29216 protected_set_expr_location (result
, combined_loc
);
29220 /* Parse an Objective-C selector expression.
29222 objc-selector-expression:
29223 @selector ( objc-method-signature )
29225 objc-method-signature:
29231 objc-selector-seq objc-selector :
29233 Returns a representation of the method selector. */
29236 cp_parser_objc_selector_expression (cp_parser
* parser
)
29238 tree sel_seq
= NULL_TREE
;
29239 bool maybe_unary_selector_p
= true;
29241 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29243 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29244 matching_parens parens
;
29245 parens
.require_open (parser
);
29246 token
= cp_lexer_peek_token (parser
->lexer
);
29248 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29249 || token
->type
== CPP_SCOPE
)
29251 tree selector
= NULL_TREE
;
29253 if (token
->type
!= CPP_COLON
29254 || token
->type
== CPP_SCOPE
)
29255 selector
= cp_parser_objc_selector (parser
);
29257 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29258 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29260 /* Detect if we have a unary selector. */
29261 if (maybe_unary_selector_p
)
29263 sel_seq
= selector
;
29264 goto finish_selector
;
29268 cp_parser_error (parser
, "expected %<:%>");
29271 maybe_unary_selector_p
= false;
29272 token
= cp_lexer_consume_token (parser
->lexer
);
29274 if (token
->type
== CPP_SCOPE
)
29277 = chainon (sel_seq
,
29278 build_tree_list (selector
, NULL_TREE
));
29280 = chainon (sel_seq
,
29281 build_tree_list (NULL_TREE
, NULL_TREE
));
29285 = chainon (sel_seq
,
29286 build_tree_list (selector
, NULL_TREE
));
29288 token
= cp_lexer_peek_token (parser
->lexer
);
29292 parens
.require_close (parser
);
29295 /* Build a location of the form:
29298 with caret==start at the @ token, finishing at the close paren. */
29299 location_t combined_loc
29300 = make_location (loc
, loc
,
29301 cp_lexer_previous_token (parser
->lexer
)->location
);
29302 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29303 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29304 protected_set_expr_location (result
, combined_loc
);
29308 /* Parse a list of identifiers.
29310 objc-identifier-list:
29312 objc-identifier-list , identifier
29314 Returns a TREE_LIST of identifier nodes. */
29317 cp_parser_objc_identifier_list (cp_parser
* parser
)
29323 identifier
= cp_parser_identifier (parser
);
29324 if (identifier
== error_mark_node
)
29325 return error_mark_node
;
29327 list
= build_tree_list (NULL_TREE
, identifier
);
29328 sep
= cp_lexer_peek_token (parser
->lexer
);
29330 while (sep
->type
== CPP_COMMA
)
29332 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29333 identifier
= cp_parser_identifier (parser
);
29334 if (identifier
== error_mark_node
)
29337 list
= chainon (list
, build_tree_list (NULL_TREE
,
29339 sep
= cp_lexer_peek_token (parser
->lexer
);
29345 /* Parse an Objective-C alias declaration.
29347 objc-alias-declaration:
29348 @compatibility_alias identifier identifier ;
29350 This function registers the alias mapping with the Objective-C front end.
29351 It returns nothing. */
29354 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29358 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29359 alias
= cp_parser_identifier (parser
);
29360 orig
= cp_parser_identifier (parser
);
29361 objc_declare_alias (alias
, orig
);
29362 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29365 /* Parse an Objective-C class forward-declaration.
29367 objc-class-declaration:
29368 @class objc-identifier-list ;
29370 The function registers the forward declarations with the Objective-C
29371 front end. It returns nothing. */
29374 cp_parser_objc_class_declaration (cp_parser
* parser
)
29376 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29381 id
= cp_parser_identifier (parser
);
29382 if (id
== error_mark_node
)
29385 objc_declare_class (id
);
29387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29388 cp_lexer_consume_token (parser
->lexer
);
29392 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29395 /* Parse a list of Objective-C protocol references.
29397 objc-protocol-refs-opt:
29398 objc-protocol-refs [opt]
29400 objc-protocol-refs:
29401 < objc-identifier-list >
29403 Returns a TREE_LIST of identifiers, if any. */
29406 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29408 tree protorefs
= NULL_TREE
;
29410 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29412 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29413 protorefs
= cp_parser_objc_identifier_list (parser
);
29414 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29420 /* Parse a Objective-C visibility specification. */
29423 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29425 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29427 switch (vis
->keyword
)
29429 case RID_AT_PRIVATE
:
29430 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29432 case RID_AT_PROTECTED
:
29433 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29435 case RID_AT_PUBLIC
:
29436 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29438 case RID_AT_PACKAGE
:
29439 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29445 /* Eat '@private'/'@protected'/'@public'. */
29446 cp_lexer_consume_token (parser
->lexer
);
29449 /* Parse an Objective-C method type. Return 'true' if it is a class
29450 (+) method, and 'false' if it is an instance (-) method. */
29453 cp_parser_objc_method_type (cp_parser
* parser
)
29455 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29461 /* Parse an Objective-C protocol qualifier. */
29464 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29466 tree quals
= NULL_TREE
, node
;
29467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29469 node
= token
->u
.value
;
29471 while (node
&& identifier_p (node
)
29472 && (node
== ridpointers
[(int) RID_IN
]
29473 || node
== ridpointers
[(int) RID_OUT
]
29474 || node
== ridpointers
[(int) RID_INOUT
]
29475 || node
== ridpointers
[(int) RID_BYCOPY
]
29476 || node
== ridpointers
[(int) RID_BYREF
]
29477 || node
== ridpointers
[(int) RID_ONEWAY
]))
29479 quals
= tree_cons (NULL_TREE
, node
, quals
);
29480 cp_lexer_consume_token (parser
->lexer
);
29481 token
= cp_lexer_peek_token (parser
->lexer
);
29482 node
= token
->u
.value
;
29488 /* Parse an Objective-C typename. */
29491 cp_parser_objc_typename (cp_parser
* parser
)
29493 tree type_name
= NULL_TREE
;
29495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29497 tree proto_quals
, cp_type
= NULL_TREE
;
29499 matching_parens parens
;
29500 parens
.consume_open (parser
); /* Eat '('. */
29501 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29503 /* An ObjC type name may consist of just protocol qualifiers, in which
29504 case the type shall default to 'id'. */
29505 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29507 cp_type
= cp_parser_type_id (parser
);
29509 /* If the type could not be parsed, an error has already
29510 been produced. For error recovery, behave as if it had
29511 not been specified, which will use the default type
29513 if (cp_type
== error_mark_node
)
29515 cp_type
= NULL_TREE
;
29516 /* We need to skip to the closing parenthesis as
29517 cp_parser_type_id() does not seem to do it for
29519 cp_parser_skip_to_closing_parenthesis (parser
,
29520 /*recovering=*/true,
29521 /*or_comma=*/false,
29522 /*consume_paren=*/false);
29526 parens
.require_close (parser
);
29527 type_name
= build_tree_list (proto_quals
, cp_type
);
29533 /* Check to see if TYPE refers to an Objective-C selector name. */
29536 cp_parser_objc_selector_p (enum cpp_ttype type
)
29538 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29539 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29540 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29541 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29542 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29545 /* Parse an Objective-C selector. */
29548 cp_parser_objc_selector (cp_parser
* parser
)
29550 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29552 if (!cp_parser_objc_selector_p (token
->type
))
29554 error_at (token
->location
, "invalid Objective-C++ selector name");
29555 return error_mark_node
;
29558 /* C++ operator names are allowed to appear in ObjC selectors. */
29559 switch (token
->type
)
29561 case CPP_AND_AND
: return get_identifier ("and");
29562 case CPP_AND_EQ
: return get_identifier ("and_eq");
29563 case CPP_AND
: return get_identifier ("bitand");
29564 case CPP_OR
: return get_identifier ("bitor");
29565 case CPP_COMPL
: return get_identifier ("compl");
29566 case CPP_NOT
: return get_identifier ("not");
29567 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29568 case CPP_OR_OR
: return get_identifier ("or");
29569 case CPP_OR_EQ
: return get_identifier ("or_eq");
29570 case CPP_XOR
: return get_identifier ("xor");
29571 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29572 default: return token
->u
.value
;
29576 /* Parse an Objective-C params list. */
29579 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29581 tree params
= NULL_TREE
;
29582 bool maybe_unary_selector_p
= true;
29583 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29585 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29587 tree selector
= NULL_TREE
, type_name
, identifier
;
29588 tree parm_attr
= NULL_TREE
;
29590 if (token
->keyword
== RID_ATTRIBUTE
)
29593 if (token
->type
!= CPP_COLON
)
29594 selector
= cp_parser_objc_selector (parser
);
29596 /* Detect if we have a unary selector. */
29597 if (maybe_unary_selector_p
29598 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29600 params
= selector
; /* Might be followed by attributes. */
29604 maybe_unary_selector_p
= false;
29605 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29607 /* Something went quite wrong. There should be a colon
29608 here, but there is not. Stop parsing parameters. */
29611 type_name
= cp_parser_objc_typename (parser
);
29612 /* New ObjC allows attributes on parameters too. */
29613 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29614 parm_attr
= cp_parser_attributes_opt (parser
);
29615 identifier
= cp_parser_identifier (parser
);
29619 objc_build_keyword_decl (selector
,
29624 token
= cp_lexer_peek_token (parser
->lexer
);
29627 if (params
== NULL_TREE
)
29629 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29630 return error_mark_node
;
29633 /* We allow tail attributes for the method. */
29634 if (token
->keyword
== RID_ATTRIBUTE
)
29636 *attributes
= cp_parser_attributes_opt (parser
);
29637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29638 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29640 cp_parser_error (parser
,
29641 "method attributes must be specified at the end");
29642 return error_mark_node
;
29645 if (params
== NULL_TREE
)
29647 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29648 return error_mark_node
;
29653 /* Parse the non-keyword Objective-C params. */
29656 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29659 tree params
= make_node (TREE_LIST
);
29660 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29661 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29663 while (token
->type
== CPP_COMMA
)
29665 cp_parameter_declarator
*parmdecl
;
29668 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29669 token
= cp_lexer_peek_token (parser
->lexer
);
29671 if (token
->type
== CPP_ELLIPSIS
)
29673 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29675 token
= cp_lexer_peek_token (parser
->lexer
);
29679 /* TODO: parse attributes for tail parameters. */
29680 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29681 parm
= grokdeclarator (parmdecl
->declarator
,
29682 &parmdecl
->decl_specifiers
,
29683 PARM
, /*initialized=*/0,
29684 /*attrlist=*/NULL
);
29686 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29687 token
= cp_lexer_peek_token (parser
->lexer
);
29690 /* We allow tail attributes for the method. */
29691 if (token
->keyword
== RID_ATTRIBUTE
)
29693 if (*attributes
== NULL_TREE
)
29695 *attributes
= cp_parser_attributes_opt (parser
);
29696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29697 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29701 /* We have an error, but parse the attributes, so that we can
29703 *attributes
= cp_parser_attributes_opt (parser
);
29705 cp_parser_error (parser
,
29706 "method attributes must be specified at the end");
29707 return error_mark_node
;
29713 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29716 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29718 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29720 /* If the next token is `extern' and the following token is a string
29721 literal, then we have a linkage specification. */
29722 if (token
->keyword
== RID_EXTERN
29723 && cp_parser_is_pure_string_literal
29724 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29725 cp_parser_linkage_specification (parser
);
29726 /* Handle #pragma, if any. */
29727 else if (token
->type
== CPP_PRAGMA
)
29728 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29729 /* Allow stray semicolons. */
29730 else if (token
->type
== CPP_SEMICOLON
)
29731 cp_lexer_consume_token (parser
->lexer
);
29732 /* Mark methods as optional or required, when building protocols. */
29733 else if (token
->keyword
== RID_AT_OPTIONAL
)
29735 cp_lexer_consume_token (parser
->lexer
);
29736 objc_set_method_opt (true);
29738 else if (token
->keyword
== RID_AT_REQUIRED
)
29740 cp_lexer_consume_token (parser
->lexer
);
29741 objc_set_method_opt (false);
29743 else if (token
->keyword
== RID_NAMESPACE
)
29744 cp_parser_namespace_definition (parser
);
29745 /* Other stray characters must generate errors. */
29746 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29748 cp_lexer_consume_token (parser
->lexer
);
29749 error ("stray %qs between Objective-C++ methods",
29750 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29752 /* Finally, try to parse a block-declaration, or a function-definition. */
29754 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29757 /* Parse a method signature. */
29760 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29762 tree rettype
, kwdparms
, optparms
;
29763 bool ellipsis
= false;
29764 bool is_class_method
;
29766 is_class_method
= cp_parser_objc_method_type (parser
);
29767 rettype
= cp_parser_objc_typename (parser
);
29768 *attributes
= NULL_TREE
;
29769 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29770 if (kwdparms
== error_mark_node
)
29771 return error_mark_node
;
29772 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29773 if (optparms
== error_mark_node
)
29774 return error_mark_node
;
29776 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29780 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29783 cp_lexer_save_tokens (parser
->lexer
);
29784 tattr
= cp_parser_attributes_opt (parser
);
29785 gcc_assert (tattr
) ;
29787 /* If the attributes are followed by a method introducer, this is not allowed.
29788 Dump the attributes and flag the situation. */
29789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29790 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29793 /* Otherwise, the attributes introduce some interstitial code, possibly so
29794 rewind to allow that check. */
29795 cp_lexer_rollback_tokens (parser
->lexer
);
29799 /* Parse an Objective-C method prototype list. */
29802 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29804 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29806 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29808 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29810 tree attributes
, sig
;
29811 bool is_class_method
;
29812 if (token
->type
== CPP_PLUS
)
29813 is_class_method
= true;
29815 is_class_method
= false;
29816 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29817 if (sig
== error_mark_node
)
29819 cp_parser_skip_to_end_of_block_or_statement (parser
);
29820 token
= cp_lexer_peek_token (parser
->lexer
);
29823 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29824 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29826 else if (token
->keyword
== RID_AT_PROPERTY
)
29827 cp_parser_objc_at_property_declaration (parser
);
29828 else if (token
->keyword
== RID_ATTRIBUTE
29829 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29830 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29832 "prefix attributes are ignored for methods");
29834 /* Allow for interspersed non-ObjC++ code. */
29835 cp_parser_objc_interstitial_code (parser
);
29837 token
= cp_lexer_peek_token (parser
->lexer
);
29840 if (token
->type
!= CPP_EOF
)
29841 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29843 cp_parser_error (parser
, "expected %<@end%>");
29845 objc_finish_interface ();
29848 /* Parse an Objective-C method definition list. */
29851 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29855 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29859 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29862 tree sig
, attribute
;
29863 bool is_class_method
;
29864 if (token
->type
== CPP_PLUS
)
29865 is_class_method
= true;
29867 is_class_method
= false;
29868 push_deferring_access_checks (dk_deferred
);
29869 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29870 if (sig
== error_mark_node
)
29872 cp_parser_skip_to_end_of_block_or_statement (parser
);
29873 token
= cp_lexer_peek_token (parser
->lexer
);
29876 objc_start_method_definition (is_class_method
, sig
, attribute
,
29879 /* For historical reasons, we accept an optional semicolon. */
29880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29881 cp_lexer_consume_token (parser
->lexer
);
29883 ptk
= cp_lexer_peek_token (parser
->lexer
);
29884 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29885 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29887 perform_deferred_access_checks (tf_warning_or_error
);
29888 stop_deferring_access_checks ();
29889 meth
= cp_parser_function_definition_after_declarator (parser
,
29891 pop_deferring_access_checks ();
29892 objc_finish_method_definition (meth
);
29895 /* The following case will be removed once @synthesize is
29896 completely implemented. */
29897 else if (token
->keyword
== RID_AT_PROPERTY
)
29898 cp_parser_objc_at_property_declaration (parser
);
29899 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
29900 cp_parser_objc_at_synthesize_declaration (parser
);
29901 else if (token
->keyword
== RID_AT_DYNAMIC
)
29902 cp_parser_objc_at_dynamic_declaration (parser
);
29903 else if (token
->keyword
== RID_ATTRIBUTE
29904 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29905 warning_at (token
->location
, OPT_Wattributes
,
29906 "prefix attributes are ignored for methods");
29908 /* Allow for interspersed non-ObjC++ code. */
29909 cp_parser_objc_interstitial_code (parser
);
29911 token
= cp_lexer_peek_token (parser
->lexer
);
29914 if (token
->type
!= CPP_EOF
)
29915 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29917 cp_parser_error (parser
, "expected %<@end%>");
29919 objc_finish_implementation ();
29922 /* Parse Objective-C ivars. */
29925 cp_parser_objc_class_ivars (cp_parser
* parser
)
29927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29929 if (token
->type
!= CPP_OPEN_BRACE
)
29930 return; /* No ivars specified. */
29932 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
29933 token
= cp_lexer_peek_token (parser
->lexer
);
29935 while (token
->type
!= CPP_CLOSE_BRACE
29936 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29938 cp_decl_specifier_seq declspecs
;
29939 int decl_class_or_enum_p
;
29940 tree prefix_attributes
;
29942 cp_parser_objc_visibility_spec (parser
);
29944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29947 cp_parser_decl_specifier_seq (parser
,
29948 CP_PARSER_FLAGS_OPTIONAL
,
29950 &decl_class_or_enum_p
);
29952 /* auto, register, static, extern, mutable. */
29953 if (declspecs
.storage_class
!= sc_none
)
29955 cp_parser_error (parser
, "invalid type for instance variable");
29956 declspecs
.storage_class
= sc_none
;
29959 /* thread_local. */
29960 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
29962 cp_parser_error (parser
, "invalid type for instance variable");
29963 declspecs
.locations
[ds_thread
] = 0;
29967 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
29969 cp_parser_error (parser
, "invalid type for instance variable");
29970 declspecs
.locations
[ds_typedef
] = 0;
29973 prefix_attributes
= declspecs
.attributes
;
29974 declspecs
.attributes
= NULL_TREE
;
29976 /* Keep going until we hit the `;' at the end of the
29978 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29980 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
29981 cp_declarator
*declarator
= NULL
;
29982 int ctor_dtor_or_conv_p
;
29984 /* Check for a (possibly unnamed) bitfield declaration. */
29985 token
= cp_lexer_peek_token (parser
->lexer
);
29986 if (token
->type
== CPP_COLON
)
29989 if (token
->type
== CPP_NAME
29990 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
29993 /* Get the name of the bitfield. */
29994 declarator
= make_id_declarator (NULL_TREE
,
29995 cp_parser_identifier (parser
),
29999 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30000 /* Get the width of the bitfield. */
30002 = cp_parser_constant_expression (parser
);
30006 /* Parse the declarator. */
30008 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30009 &ctor_dtor_or_conv_p
,
30010 /*parenthesized_p=*/NULL
,
30011 /*member_p=*/false,
30012 /*friend_p=*/false);
30015 /* Look for attributes that apply to the ivar. */
30016 attributes
= cp_parser_attributes_opt (parser
);
30017 /* Remember which attributes are prefix attributes and
30019 first_attribute
= attributes
;
30020 /* Combine the attributes. */
30021 attributes
= chainon (prefix_attributes
, attributes
);
30024 /* Create the bitfield declaration. */
30025 decl
= grokbitfield (declarator
, &declspecs
,
30029 decl
= grokfield (declarator
, &declspecs
,
30030 NULL_TREE
, /*init_const_expr_p=*/false,
30031 NULL_TREE
, attributes
);
30033 /* Add the instance variable. */
30034 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30035 objc_add_instance_variable (decl
);
30037 /* Reset PREFIX_ATTRIBUTES. */
30038 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30039 attributes
= TREE_CHAIN (attributes
);
30041 TREE_CHAIN (attributes
) = NULL_TREE
;
30043 token
= cp_lexer_peek_token (parser
->lexer
);
30045 if (token
->type
== CPP_COMMA
)
30047 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30053 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30054 token
= cp_lexer_peek_token (parser
->lexer
);
30057 if (token
->keyword
== RID_AT_END
)
30058 cp_parser_error (parser
, "expected %<}%>");
30060 /* Do not consume the RID_AT_END, so it will be read again as terminating
30061 the @interface of @implementation. */
30062 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30063 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30065 /* For historical reasons, we accept an optional semicolon. */
30066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30067 cp_lexer_consume_token (parser
->lexer
);
30070 /* Parse an Objective-C protocol declaration. */
30073 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30075 tree proto
, protorefs
;
30078 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30079 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30081 tok
= cp_lexer_peek_token (parser
->lexer
);
30082 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30083 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30087 /* See if we have a forward declaration or a definition. */
30088 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30090 /* Try a forward declaration first. */
30091 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30097 id
= cp_parser_identifier (parser
);
30098 if (id
== error_mark_node
)
30101 objc_declare_protocol (id
, attributes
);
30103 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30104 cp_lexer_consume_token (parser
->lexer
);
30108 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30111 /* Ok, we got a full-fledged definition (or at least should). */
30114 proto
= cp_parser_identifier (parser
);
30115 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30116 objc_start_protocol (proto
, protorefs
, attributes
);
30117 cp_parser_objc_method_prototype_list (parser
);
30121 /* Parse an Objective-C superclass or category. */
30124 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30127 tree
*categ
, bool *is_class_extension
)
30129 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30131 *super
= *categ
= NULL_TREE
;
30132 *is_class_extension
= false;
30133 if (next
->type
== CPP_COLON
)
30135 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30136 *super
= cp_parser_identifier (parser
);
30138 else if (next
->type
== CPP_OPEN_PAREN
)
30140 matching_parens parens
;
30141 parens
.consume_open (parser
); /* Eat '('. */
30143 /* If there is no category name, and this is an @interface, we
30144 have a class extension. */
30145 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30147 *categ
= NULL_TREE
;
30148 *is_class_extension
= true;
30151 *categ
= cp_parser_identifier (parser
);
30153 parens
.require_close (parser
);
30157 /* Parse an Objective-C class interface. */
30160 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30162 tree name
, super
, categ
, protos
;
30163 bool is_class_extension
;
30165 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30166 name
= cp_parser_identifier (parser
);
30167 if (name
== error_mark_node
)
30169 /* It's hard to recover because even if valid @interface stuff
30170 is to follow, we can't compile it (or validate it) if we
30171 don't even know which class it refers to. Let's assume this
30172 was a stray '@interface' token in the stream and skip it.
30176 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30177 &is_class_extension
);
30178 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30180 /* We have either a class or a category on our hands. */
30181 if (categ
|| is_class_extension
)
30182 objc_start_category_interface (name
, categ
, protos
, attributes
);
30185 objc_start_class_interface (name
, super
, protos
, attributes
);
30186 /* Handle instance variable declarations, if any. */
30187 cp_parser_objc_class_ivars (parser
);
30188 objc_continue_interface ();
30191 cp_parser_objc_method_prototype_list (parser
);
30194 /* Parse an Objective-C class implementation. */
30197 cp_parser_objc_class_implementation (cp_parser
* parser
)
30199 tree name
, super
, categ
;
30200 bool is_class_extension
;
30202 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30203 name
= cp_parser_identifier (parser
);
30204 if (name
== error_mark_node
)
30206 /* It's hard to recover because even if valid @implementation
30207 stuff is to follow, we can't compile it (or validate it) if
30208 we don't even know which class it refers to. Let's assume
30209 this was a stray '@implementation' token in the stream and
30214 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30215 &is_class_extension
);
30217 /* We have either a class or a category on our hands. */
30219 objc_start_category_implementation (name
, categ
);
30222 objc_start_class_implementation (name
, super
);
30223 /* Handle instance variable declarations, if any. */
30224 cp_parser_objc_class_ivars (parser
);
30225 objc_continue_implementation ();
30228 cp_parser_objc_method_definition_list (parser
);
30231 /* Consume the @end token and finish off the implementation. */
30234 cp_parser_objc_end_implementation (cp_parser
* parser
)
30236 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30237 objc_finish_implementation ();
30240 /* Parse an Objective-C declaration. */
30243 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30245 /* Try to figure out what kind of declaration is present. */
30246 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30249 switch (kwd
->keyword
)
30254 error_at (kwd
->location
, "attributes may not be specified before"
30255 " the %<@%D%> Objective-C++ keyword",
30259 case RID_AT_IMPLEMENTATION
:
30260 warning_at (kwd
->location
, OPT_Wattributes
,
30261 "prefix attributes are ignored before %<@%D%>",
30268 switch (kwd
->keyword
)
30271 cp_parser_objc_alias_declaration (parser
);
30274 cp_parser_objc_class_declaration (parser
);
30276 case RID_AT_PROTOCOL
:
30277 cp_parser_objc_protocol_declaration (parser
, attributes
);
30279 case RID_AT_INTERFACE
:
30280 cp_parser_objc_class_interface (parser
, attributes
);
30282 case RID_AT_IMPLEMENTATION
:
30283 cp_parser_objc_class_implementation (parser
);
30286 cp_parser_objc_end_implementation (parser
);
30289 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30291 cp_parser_skip_to_end_of_block_or_statement (parser
);
30295 /* Parse an Objective-C try-catch-finally statement.
30297 objc-try-catch-finally-stmt:
30298 @try compound-statement objc-catch-clause-seq [opt]
30299 objc-finally-clause [opt]
30301 objc-catch-clause-seq:
30302 objc-catch-clause objc-catch-clause-seq [opt]
30305 @catch ( objc-exception-declaration ) compound-statement
30307 objc-finally-clause:
30308 @finally compound-statement
30310 objc-exception-declaration:
30311 parameter-declaration
30314 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30318 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30319 for C. Keep them in sync. */
30322 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30324 location_t location
;
30327 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30328 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30329 objc_maybe_warn_exceptions (location
);
30330 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30331 node, lest it get absorbed into the surrounding block. */
30332 stmt
= push_stmt_list ();
30333 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30334 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30336 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30338 cp_parameter_declarator
*parm
;
30339 tree parameter_declaration
= error_mark_node
;
30340 bool seen_open_paren
= false;
30341 matching_parens parens
;
30343 cp_lexer_consume_token (parser
->lexer
);
30344 if (parens
.require_open (parser
))
30345 seen_open_paren
= true;
30346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30348 /* We have "@catch (...)" (where the '...' are literally
30349 what is in the code). Skip the '...'.
30350 parameter_declaration is set to NULL_TREE, and
30351 objc_being_catch_clauses() knows that that means
30353 cp_lexer_consume_token (parser
->lexer
);
30354 parameter_declaration
= NULL_TREE
;
30358 /* We have "@catch (NSException *exception)" or something
30359 like that. Parse the parameter declaration. */
30360 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30362 parameter_declaration
= error_mark_node
;
30364 parameter_declaration
= grokdeclarator (parm
->declarator
,
30365 &parm
->decl_specifiers
,
30366 PARM
, /*initialized=*/0,
30367 /*attrlist=*/NULL
);
30369 if (seen_open_paren
)
30370 parens
.require_close (parser
);
30373 /* If there was no open parenthesis, we are recovering from
30374 an error, and we are trying to figure out what mistake
30375 the user has made. */
30377 /* If there is an immediate closing parenthesis, the user
30378 probably forgot the opening one (ie, they typed "@catch
30379 NSException *e)". Parse the closing parenthesis and keep
30381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30382 cp_lexer_consume_token (parser
->lexer
);
30384 /* If these is no immediate closing parenthesis, the user
30385 probably doesn't know that parenthesis are required at
30386 all (ie, they typed "@catch NSException *e"). So, just
30387 forget about the closing parenthesis and keep going. */
30389 objc_begin_catch_clause (parameter_declaration
);
30390 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30391 objc_finish_catch_clause ();
30393 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30395 cp_lexer_consume_token (parser
->lexer
);
30396 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30397 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30398 node, lest it get absorbed into the surrounding block. */
30399 stmt
= push_stmt_list ();
30400 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30401 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30404 return objc_finish_try_stmt ();
30407 /* Parse an Objective-C synchronized statement.
30409 objc-synchronized-stmt:
30410 @synchronized ( expression ) compound-statement
30412 Returns NULL_TREE. */
30415 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30417 location_t location
;
30420 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30422 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30423 objc_maybe_warn_exceptions (location
);
30424 matching_parens parens
;
30425 parens
.require_open (parser
);
30426 lock
= cp_parser_expression (parser
);
30427 parens
.require_close (parser
);
30429 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30430 node, lest it get absorbed into the surrounding block. */
30431 stmt
= push_stmt_list ();
30432 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30434 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30437 /* Parse an Objective-C throw statement.
30440 @throw assignment-expression [opt] ;
30442 Returns a constructed '@throw' statement. */
30445 cp_parser_objc_throw_statement (cp_parser
*parser
)
30447 tree expr
= NULL_TREE
;
30448 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30450 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30452 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30453 expr
= cp_parser_expression (parser
);
30455 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30457 return objc_build_throw_stmt (loc
, expr
);
30460 /* Parse an Objective-C statement. */
30463 cp_parser_objc_statement (cp_parser
* parser
)
30465 /* Try to figure out what kind of declaration is present. */
30466 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30468 switch (kwd
->keyword
)
30471 return cp_parser_objc_try_catch_finally_statement (parser
);
30472 case RID_AT_SYNCHRONIZED
:
30473 return cp_parser_objc_synchronized_statement (parser
);
30475 return cp_parser_objc_throw_statement (parser
);
30477 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30479 cp_parser_skip_to_end_of_block_or_statement (parser
);
30482 return error_mark_node
;
30485 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30486 look ahead to see if an objc keyword follows the attributes. This
30487 is to detect the use of prefix attributes on ObjC @interface and
30491 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30493 cp_lexer_save_tokens (parser
->lexer
);
30494 *attrib
= cp_parser_attributes_opt (parser
);
30495 gcc_assert (*attrib
);
30496 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30498 cp_lexer_commit_tokens (parser
->lexer
);
30501 cp_lexer_rollback_tokens (parser
->lexer
);
30505 /* This routine is a minimal replacement for
30506 c_parser_struct_declaration () used when parsing the list of
30507 types/names or ObjC++ properties. For example, when parsing the
30510 @property (readonly) int a, b, c;
30512 this function is responsible for parsing "int a, int b, int c" and
30513 returning the declarations as CHAIN of DECLs.
30515 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30516 similar parsing. */
30518 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30520 tree decls
= NULL_TREE
;
30521 cp_decl_specifier_seq declspecs
;
30522 int decl_class_or_enum_p
;
30523 tree prefix_attributes
;
30525 cp_parser_decl_specifier_seq (parser
,
30526 CP_PARSER_FLAGS_NONE
,
30528 &decl_class_or_enum_p
);
30530 if (declspecs
.type
== error_mark_node
)
30531 return error_mark_node
;
30533 /* auto, register, static, extern, mutable. */
30534 if (declspecs
.storage_class
!= sc_none
)
30536 cp_parser_error (parser
, "invalid type for property");
30537 declspecs
.storage_class
= sc_none
;
30540 /* thread_local. */
30541 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30543 cp_parser_error (parser
, "invalid type for property");
30544 declspecs
.locations
[ds_thread
] = 0;
30548 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30550 cp_parser_error (parser
, "invalid type for property");
30551 declspecs
.locations
[ds_typedef
] = 0;
30554 prefix_attributes
= declspecs
.attributes
;
30555 declspecs
.attributes
= NULL_TREE
;
30557 /* Keep going until we hit the `;' at the end of the declaration. */
30558 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30560 tree attributes
, first_attribute
, decl
;
30561 cp_declarator
*declarator
;
30564 /* Parse the declarator. */
30565 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30566 NULL
, NULL
, false, false);
30568 /* Look for attributes that apply to the ivar. */
30569 attributes
= cp_parser_attributes_opt (parser
);
30570 /* Remember which attributes are prefix attributes and
30572 first_attribute
= attributes
;
30573 /* Combine the attributes. */
30574 attributes
= chainon (prefix_attributes
, attributes
);
30576 decl
= grokfield (declarator
, &declspecs
,
30577 NULL_TREE
, /*init_const_expr_p=*/false,
30578 NULL_TREE
, attributes
);
30580 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30581 return error_mark_node
;
30583 /* Reset PREFIX_ATTRIBUTES. */
30584 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30585 attributes
= TREE_CHAIN (attributes
);
30587 TREE_CHAIN (attributes
) = NULL_TREE
;
30589 DECL_CHAIN (decl
) = decls
;
30592 token
= cp_lexer_peek_token (parser
->lexer
);
30593 if (token
->type
== CPP_COMMA
)
30595 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30604 /* Parse an Objective-C @property declaration. The syntax is:
30606 objc-property-declaration:
30607 '@property' objc-property-attributes[opt] struct-declaration ;
30609 objc-property-attributes:
30610 '(' objc-property-attribute-list ')'
30612 objc-property-attribute-list:
30613 objc-property-attribute
30614 objc-property-attribute-list, objc-property-attribute
30616 objc-property-attribute
30617 'getter' = identifier
30618 'setter' = identifier
30627 @property NSString *name;
30628 @property (readonly) id object;
30629 @property (retain, nonatomic, getter=getTheName) id name;
30630 @property int a, b, c;
30632 PS: This function is identical to
30633 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30635 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30637 /* The following variables hold the attributes of the properties as
30638 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30639 seen. When we see an attribute, we set them to 'true' (if they
30640 are boolean properties) or to the identifier (if they have an
30641 argument, ie, for getter and setter). Note that here we only
30642 parse the list of attributes, check the syntax and accumulate the
30643 attributes that we find. objc_add_property_declaration() will
30644 then process the information. */
30645 bool property_assign
= false;
30646 bool property_copy
= false;
30647 tree property_getter_ident
= NULL_TREE
;
30648 bool property_nonatomic
= false;
30649 bool property_readonly
= false;
30650 bool property_readwrite
= false;
30651 bool property_retain
= false;
30652 tree property_setter_ident
= NULL_TREE
;
30654 /* 'properties' is the list of properties that we read. Usually a
30655 single one, but maybe more (eg, in "@property int a, b, c;" there
30660 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30662 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30664 /* Parse the optional attribute list... */
30665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30668 matching_parens parens
;
30669 parens
.consume_open (parser
);
30673 bool syntax_error
= false;
30674 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30677 if (token
->type
!= CPP_NAME
)
30679 cp_parser_error (parser
, "expected identifier");
30682 keyword
= C_RID_CODE (token
->u
.value
);
30683 cp_lexer_consume_token (parser
->lexer
);
30686 case RID_ASSIGN
: property_assign
= true; break;
30687 case RID_COPY
: property_copy
= true; break;
30688 case RID_NONATOMIC
: property_nonatomic
= true; break;
30689 case RID_READONLY
: property_readonly
= true; break;
30690 case RID_READWRITE
: property_readwrite
= true; break;
30691 case RID_RETAIN
: property_retain
= true; break;
30695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30697 if (keyword
== RID_GETTER
)
30698 cp_parser_error (parser
,
30699 "missing %<=%> (after %<getter%> attribute)");
30701 cp_parser_error (parser
,
30702 "missing %<=%> (after %<setter%> attribute)");
30703 syntax_error
= true;
30706 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30707 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30709 cp_parser_error (parser
, "expected identifier");
30710 syntax_error
= true;
30713 if (keyword
== RID_SETTER
)
30715 if (property_setter_ident
!= NULL_TREE
)
30717 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30718 cp_lexer_consume_token (parser
->lexer
);
30721 property_setter_ident
= cp_parser_objc_selector (parser
);
30722 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30723 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30725 cp_lexer_consume_token (parser
->lexer
);
30729 if (property_getter_ident
!= NULL_TREE
)
30731 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30732 cp_lexer_consume_token (parser
->lexer
);
30735 property_getter_ident
= cp_parser_objc_selector (parser
);
30739 cp_parser_error (parser
, "unknown property attribute");
30740 syntax_error
= true;
30747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30748 cp_lexer_consume_token (parser
->lexer
);
30753 /* FIXME: "@property (setter, assign);" will generate a spurious
30754 "error: expected ‘)’ before ‘,’ token". This is because
30755 cp_parser_require, unlike the C counterpart, will produce an
30756 error even if we are in error recovery. */
30757 if (!parens
.require_close (parser
))
30759 cp_parser_skip_to_closing_parenthesis (parser
,
30760 /*recovering=*/true,
30761 /*or_comma=*/false,
30762 /*consume_paren=*/true);
30766 /* ... and the property declaration(s). */
30767 properties
= cp_parser_objc_struct_declaration (parser
);
30769 if (properties
== error_mark_node
)
30771 cp_parser_skip_to_end_of_statement (parser
);
30772 /* If the next token is now a `;', consume it. */
30773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30774 cp_lexer_consume_token (parser
->lexer
);
30778 if (properties
== NULL_TREE
)
30779 cp_parser_error (parser
, "expected identifier");
30782 /* Comma-separated properties are chained together in
30783 reverse order; add them one by one. */
30784 properties
= nreverse (properties
);
30786 for (; properties
; properties
= TREE_CHAIN (properties
))
30787 objc_add_property_declaration (loc
, copy_node (properties
),
30788 property_readonly
, property_readwrite
,
30789 property_assign
, property_retain
,
30790 property_copy
, property_nonatomic
,
30791 property_getter_ident
, property_setter_ident
);
30794 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30797 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30799 objc-synthesize-declaration:
30800 @synthesize objc-synthesize-identifier-list ;
30802 objc-synthesize-identifier-list:
30803 objc-synthesize-identifier
30804 objc-synthesize-identifier-list, objc-synthesize-identifier
30806 objc-synthesize-identifier
30808 identifier = identifier
30811 @synthesize MyProperty;
30812 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30814 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30815 for C. Keep them in sync.
30818 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30820 tree list
= NULL_TREE
;
30822 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30824 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30827 tree property
, ivar
;
30828 property
= cp_parser_identifier (parser
);
30829 if (property
== error_mark_node
)
30831 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30836 cp_lexer_consume_token (parser
->lexer
);
30837 ivar
= cp_parser_identifier (parser
);
30838 if (ivar
== error_mark_node
)
30840 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30846 list
= chainon (list
, build_tree_list (ivar
, property
));
30847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30848 cp_lexer_consume_token (parser
->lexer
);
30852 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30853 objc_add_synthesize_declaration (loc
, list
);
30856 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30858 objc-dynamic-declaration:
30859 @dynamic identifier-list ;
30862 @dynamic MyProperty;
30863 @dynamic MyProperty, AnotherProperty;
30865 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30866 for C. Keep them in sync.
30869 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30871 tree list
= NULL_TREE
;
30873 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30875 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30879 property
= cp_parser_identifier (parser
);
30880 if (property
== error_mark_node
)
30882 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30885 list
= chainon (list
, build_tree_list (NULL
, property
));
30886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30887 cp_lexer_consume_token (parser
->lexer
);
30891 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30892 objc_add_dynamic_declaration (loc
, list
);
30896 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30898 /* Returns name of the next clause.
30899 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30900 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30901 returned and the token is consumed. */
30903 static pragma_omp_clause
30904 cp_parser_omp_clause_name (cp_parser
*parser
)
30906 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
30908 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30909 result
= PRAGMA_OACC_CLAUSE_AUTO
;
30910 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
30911 result
= PRAGMA_OMP_CLAUSE_IF
;
30912 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
30913 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
30914 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
30915 result
= PRAGMA_OACC_CLAUSE_DELETE
;
30916 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
30917 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
30918 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30919 result
= PRAGMA_OMP_CLAUSE_FOR
;
30920 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30922 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30923 const char *p
= IDENTIFIER_POINTER (id
);
30928 if (!strcmp ("aligned", p
))
30929 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
30930 else if (!strcmp ("async", p
))
30931 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
30934 if (!strcmp ("collapse", p
))
30935 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
30936 else if (!strcmp ("copy", p
))
30937 result
= PRAGMA_OACC_CLAUSE_COPY
;
30938 else if (!strcmp ("copyin", p
))
30939 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
30940 else if (!strcmp ("copyout", p
))
30941 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
30942 else if (!strcmp ("copyprivate", p
))
30943 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
30944 else if (!strcmp ("create", p
))
30945 result
= PRAGMA_OACC_CLAUSE_CREATE
;
30948 if (!strcmp ("defaultmap", p
))
30949 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
30950 else if (!strcmp ("depend", p
))
30951 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
30952 else if (!strcmp ("device", p
))
30953 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
30954 else if (!strcmp ("deviceptr", p
))
30955 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
30956 else if (!strcmp ("device_resident", p
))
30957 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
30958 else if (!strcmp ("dist_schedule", p
))
30959 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
30962 if (!strcmp ("final", p
))
30963 result
= PRAGMA_OMP_CLAUSE_FINAL
;
30964 else if (!strcmp ("firstprivate", p
))
30965 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
30966 else if (!strcmp ("from", p
))
30967 result
= PRAGMA_OMP_CLAUSE_FROM
;
30970 if (!strcmp ("gang", p
))
30971 result
= PRAGMA_OACC_CLAUSE_GANG
;
30972 else if (!strcmp ("grainsize", p
))
30973 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
30976 if (!strcmp ("hint", p
))
30977 result
= PRAGMA_OMP_CLAUSE_HINT
;
30978 else if (!strcmp ("host", p
))
30979 result
= PRAGMA_OACC_CLAUSE_HOST
;
30982 if (!strcmp ("inbranch", p
))
30983 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
30984 else if (!strcmp ("independent", p
))
30985 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
30986 else if (!strcmp ("is_device_ptr", p
))
30987 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
30990 if (!strcmp ("lastprivate", p
))
30991 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
30992 else if (!strcmp ("linear", p
))
30993 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
30994 else if (!strcmp ("link", p
))
30995 result
= PRAGMA_OMP_CLAUSE_LINK
;
30998 if (!strcmp ("map", p
))
30999 result
= PRAGMA_OMP_CLAUSE_MAP
;
31000 else if (!strcmp ("mergeable", p
))
31001 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31002 else if (flag_cilkplus
&& !strcmp ("mask", p
))
31003 result
= PRAGMA_CILK_CLAUSE_MASK
;
31006 if (!strcmp ("nogroup", p
))
31007 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31008 else if (!strcmp ("notinbranch", p
))
31009 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31010 else if (!strcmp ("nowait", p
))
31011 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31012 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
31013 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
31014 else if (!strcmp ("num_gangs", p
))
31015 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31016 else if (!strcmp ("num_tasks", p
))
31017 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31018 else if (!strcmp ("num_teams", p
))
31019 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31020 else if (!strcmp ("num_threads", p
))
31021 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31022 else if (!strcmp ("num_workers", p
))
31023 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31026 if (!strcmp ("ordered", p
))
31027 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31030 if (!strcmp ("parallel", p
))
31031 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31032 else if (!strcmp ("present", p
))
31033 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31034 else if (!strcmp ("present_or_copy", p
)
31035 || !strcmp ("pcopy", p
))
31036 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31037 else if (!strcmp ("present_or_copyin", p
)
31038 || !strcmp ("pcopyin", p
))
31039 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31040 else if (!strcmp ("present_or_copyout", p
)
31041 || !strcmp ("pcopyout", p
))
31042 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31043 else if (!strcmp ("present_or_create", p
)
31044 || !strcmp ("pcreate", p
))
31045 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31046 else if (!strcmp ("priority", p
))
31047 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31048 else if (!strcmp ("proc_bind", p
))
31049 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31052 if (!strcmp ("reduction", p
))
31053 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31056 if (!strcmp ("safelen", p
))
31057 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31058 else if (!strcmp ("schedule", p
))
31059 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31060 else if (!strcmp ("sections", p
))
31061 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31062 else if (!strcmp ("self", p
))
31063 result
= PRAGMA_OACC_CLAUSE_SELF
;
31064 else if (!strcmp ("seq", p
))
31065 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31066 else if (!strcmp ("shared", p
))
31067 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31068 else if (!strcmp ("simd", p
))
31069 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31070 else if (!strcmp ("simdlen", p
))
31071 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31074 if (!strcmp ("taskgroup", p
))
31075 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31076 else if (!strcmp ("thread_limit", p
))
31077 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31078 else if (!strcmp ("threads", p
))
31079 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31080 else if (!strcmp ("tile", p
))
31081 result
= PRAGMA_OACC_CLAUSE_TILE
;
31082 else if (!strcmp ("to", p
))
31083 result
= PRAGMA_OMP_CLAUSE_TO
;
31086 if (!strcmp ("uniform", p
))
31087 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31088 else if (!strcmp ("untied", p
))
31089 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31090 else if (!strcmp ("use_device", p
))
31091 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31092 else if (!strcmp ("use_device_ptr", p
))
31093 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31096 if (!strcmp ("vector", p
))
31097 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31098 else if (!strcmp ("vector_length", p
))
31099 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31100 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
31101 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
31104 if (!strcmp ("wait", p
))
31105 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31106 else if (!strcmp ("worker", p
))
31107 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31112 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31113 cp_lexer_consume_token (parser
->lexer
);
31118 /* Validate that a clause of the given type does not already exist. */
31121 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31122 const char *name
, location_t location
)
31126 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31127 if (OMP_CLAUSE_CODE (c
) == code
)
31129 error_at (location
, "too many %qs clauses", name
);
31137 variable-list , identifier
31139 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31140 colon). An opening parenthesis will have been consumed by the caller.
31142 If KIND is nonzero, create the appropriate node and install the decl
31143 in OMP_CLAUSE_DECL and add the node to the head of the list.
31145 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31146 return the list created.
31148 COLON can be NULL if only closing parenthesis should end the list,
31149 or pointer to bool which will receive false if the list is terminated
31150 by closing parenthesis or true if the list is terminated by colon. */
31153 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31154 tree list
, bool *colon
)
31157 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31160 parser
->colon_corrects_to_scope_p
= false;
31167 token
= cp_lexer_peek_token (parser
->lexer
);
31169 && current_class_ptr
31170 && cp_parser_is_keyword (token
, RID_THIS
))
31172 decl
= finish_this_expr ();
31173 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31174 || CONVERT_EXPR_P (decl
))
31175 decl
= TREE_OPERAND (decl
, 0);
31176 cp_lexer_consume_token (parser
->lexer
);
31180 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31181 /*check_dependency_p=*/true,
31182 /*template_p=*/NULL
,
31183 /*declarator_p=*/false,
31184 /*optional_p=*/false);
31185 if (name
== error_mark_node
)
31188 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31189 if (decl
== error_mark_node
)
31190 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31193 if (decl
== error_mark_node
)
31195 else if (kind
!= 0)
31199 case OMP_CLAUSE__CACHE_
:
31200 /* The OpenACC cache directive explicitly only allows "array
31201 elements or subarrays". */
31202 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31204 error_at (token
->location
, "expected %<[%>");
31205 decl
= error_mark_node
;
31209 case OMP_CLAUSE_MAP
:
31210 case OMP_CLAUSE_FROM
:
31211 case OMP_CLAUSE_TO
:
31212 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31215 = cp_lexer_peek_token (parser
->lexer
)->location
;
31216 cp_id_kind idk
= CP_ID_KIND_NONE
;
31217 cp_lexer_consume_token (parser
->lexer
);
31218 decl
= convert_from_reference (decl
);
31220 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31225 case OMP_CLAUSE_DEPEND
:
31226 case OMP_CLAUSE_REDUCTION
:
31227 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31229 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31231 parser
->colon_corrects_to_scope_p
= false;
31232 cp_lexer_consume_token (parser
->lexer
);
31233 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31234 low_bound
= cp_parser_expression (parser
);
31236 parser
->colon_corrects_to_scope_p
31237 = saved_colon_corrects_to_scope_p
;
31238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31239 length
= integer_one_node
;
31242 /* Look for `:'. */
31243 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31245 if (!cp_lexer_next_token_is (parser
->lexer
,
31247 length
= cp_parser_expression (parser
);
31249 /* Look for the closing `]'. */
31250 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31254 decl
= tree_cons (low_bound
, length
, decl
);
31261 tree u
= build_omp_clause (token
->location
, kind
);
31262 OMP_CLAUSE_DECL (u
) = decl
;
31263 OMP_CLAUSE_CHAIN (u
) = list
;
31267 list
= tree_cons (decl
, NULL_TREE
, list
);
31270 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31272 cp_lexer_consume_token (parser
->lexer
);
31276 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31278 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31281 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31285 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31289 /* Try to resync to an unnested comma. Copied from
31290 cp_parser_parenthesized_expression_list. */
31293 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31294 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31295 /*recovering=*/true,
31297 /*consume_paren=*/true);
31305 /* Similarly, but expect leading and trailing parenthesis. This is a very
31306 common case for omp clauses. */
31309 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31311 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31312 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31317 copy ( variable-list )
31318 copyin ( variable-list )
31319 copyout ( variable-list )
31320 create ( variable-list )
31321 delete ( variable-list )
31322 present ( variable-list )
31323 present_or_copy ( variable-list )
31324 pcopy ( variable-list )
31325 present_or_copyin ( variable-list )
31326 pcopyin ( variable-list )
31327 present_or_copyout ( variable-list )
31328 pcopyout ( variable-list )
31329 present_or_create ( variable-list )
31330 pcreate ( variable-list ) */
31333 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31336 enum gomp_map_kind kind
;
31339 case PRAGMA_OACC_CLAUSE_COPY
:
31340 kind
= GOMP_MAP_FORCE_TOFROM
;
31342 case PRAGMA_OACC_CLAUSE_COPYIN
:
31343 kind
= GOMP_MAP_FORCE_TO
;
31345 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31346 kind
= GOMP_MAP_FORCE_FROM
;
31348 case PRAGMA_OACC_CLAUSE_CREATE
:
31349 kind
= GOMP_MAP_FORCE_ALLOC
;
31351 case PRAGMA_OACC_CLAUSE_DELETE
:
31352 kind
= GOMP_MAP_DELETE
;
31354 case PRAGMA_OACC_CLAUSE_DEVICE
:
31355 kind
= GOMP_MAP_FORCE_TO
;
31357 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31358 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31360 case PRAGMA_OACC_CLAUSE_HOST
:
31361 case PRAGMA_OACC_CLAUSE_SELF
:
31362 kind
= GOMP_MAP_FORCE_FROM
;
31364 case PRAGMA_OACC_CLAUSE_LINK
:
31365 kind
= GOMP_MAP_LINK
;
31367 case PRAGMA_OACC_CLAUSE_PRESENT
:
31368 kind
= GOMP_MAP_FORCE_PRESENT
;
31370 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31371 kind
= GOMP_MAP_TOFROM
;
31373 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31374 kind
= GOMP_MAP_TO
;
31376 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31377 kind
= GOMP_MAP_FROM
;
31379 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31380 kind
= GOMP_MAP_ALLOC
;
31383 gcc_unreachable ();
31386 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31388 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31389 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31395 deviceptr ( variable-list ) */
31398 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31400 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31403 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31404 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31405 variable-list must only allow for pointer variables. */
31406 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31407 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31409 tree v
= TREE_PURPOSE (t
);
31410 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31411 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31412 OMP_CLAUSE_DECL (u
) = v
;
31413 OMP_CLAUSE_CHAIN (u
) = list
;
31427 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31428 enum omp_clause_code code
,
31429 tree list
, location_t location
)
31431 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31432 tree c
= build_omp_clause (location
, code
);
31433 OMP_CLAUSE_CHAIN (c
) = list
;
31438 num_gangs ( expression )
31439 num_workers ( expression )
31440 vector_length ( expression ) */
31443 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31444 const char *str
, tree list
)
31446 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31448 matching_parens parens
;
31449 if (!parens
.require_open (parser
))
31452 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31454 if (t
== error_mark_node
31455 || !parens
.require_close (parser
))
31457 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31458 /*or_comma=*/false,
31459 /*consume_paren=*/true);
31463 check_no_duplicate_clause (list
, code
, str
, loc
);
31465 tree c
= build_omp_clause (loc
, code
);
31466 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31467 OMP_CLAUSE_CHAIN (c
) = list
;
31473 gang [( gang-arg-list )]
31474 worker [( [num:] int-expr )]
31475 vector [( [length:] int-expr )]
31477 where gang-arg is one of:
31482 and size-expr may be:
31489 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31490 const char *str
, tree list
)
31492 const char *id
= "num";
31493 cp_lexer
*lexer
= parser
->lexer
;
31494 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31495 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31497 if (kind
== OMP_CLAUSE_VECTOR
)
31500 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31502 matching_parens parens
;
31503 parens
.consume_open (parser
);
31507 cp_token
*next
= cp_lexer_peek_token (lexer
);
31510 /* Gang static argument. */
31511 if (kind
== OMP_CLAUSE_GANG
31512 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31514 cp_lexer_consume_token (lexer
);
31516 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31517 goto cleanup_error
;
31520 if (ops
[idx
] != NULL
)
31522 cp_parser_error (parser
, "too many %<static%> arguments");
31523 goto cleanup_error
;
31526 /* Check for the '*' argument. */
31527 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31528 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31529 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31532 cp_lexer_consume_token (lexer
);
31533 ops
[idx
] = integer_minus_one_node
;
31535 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31537 cp_lexer_consume_token (lexer
);
31543 /* Worker num: argument and vector length: arguments. */
31544 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31545 && id_equal (next
->u
.value
, id
)
31546 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31548 cp_lexer_consume_token (lexer
); /* id */
31549 cp_lexer_consume_token (lexer
); /* ':' */
31552 /* Now collect the actual argument. */
31553 if (ops
[idx
] != NULL_TREE
)
31555 cp_parser_error (parser
, "unexpected argument");
31556 goto cleanup_error
;
31559 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31561 if (expr
== error_mark_node
)
31562 goto cleanup_error
;
31564 mark_exp_read (expr
);
31567 if (kind
== OMP_CLAUSE_GANG
31568 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31570 cp_lexer_consume_token (lexer
);
31577 if (!parens
.require_close (parser
))
31578 goto cleanup_error
;
31581 check_no_duplicate_clause (list
, kind
, str
, loc
);
31583 c
= build_omp_clause (loc
, kind
);
31586 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31588 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31589 OMP_CLAUSE_CHAIN (c
) = list
;
31594 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31599 tile ( size-expr-list ) */
31602 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31604 tree c
, expr
= error_mark_node
;
31605 tree tile
= NULL_TREE
;
31607 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31608 so, but the spec authors never considered such a case and have
31609 differing opinions on what it might mean, including 'not
31611 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31612 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31615 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31620 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31624 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31625 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31627 cp_lexer_consume_token (parser
->lexer
);
31628 expr
= integer_zero_node
;
31631 expr
= cp_parser_constant_expression (parser
);
31633 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31635 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31637 /* Consume the trailing ')'. */
31638 cp_lexer_consume_token (parser
->lexer
);
31640 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31641 tile
= nreverse (tile
);
31642 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31643 OMP_CLAUSE_CHAIN (c
) = list
;
31648 Parse wait clause or directive parameters. */
31651 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31653 vec
<tree
, va_gc
> *args
;
31656 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31658 /*allow_expansion_p=*/true,
31659 /*non_constant_p=*/NULL
);
31661 if (args
== NULL
|| args
->length () == 0)
31663 cp_parser_error (parser
, "expected integer expression before ')'");
31665 release_tree_vector (args
);
31669 args_tree
= build_tree_list_vec (args
);
31671 release_tree_vector (args
);
31673 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31675 tree targ
= TREE_VALUE (t
);
31677 if (targ
!= error_mark_node
)
31679 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31680 error ("%<wait%> expression must be integral");
31683 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31685 mark_rvalue_use (targ
);
31686 OMP_CLAUSE_DECL (c
) = targ
;
31687 OMP_CLAUSE_CHAIN (c
) = list
;
31697 wait ( int-expr-list ) */
31700 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31702 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31704 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31707 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31713 collapse ( constant-expression ) */
31716 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31722 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31723 matching_parens parens
;
31724 if (!parens
.require_open (parser
))
31727 num
= cp_parser_constant_expression (parser
);
31729 if (!parens
.require_close (parser
))
31730 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31731 /*or_comma=*/false,
31732 /*consume_paren=*/true);
31734 if (num
== error_mark_node
)
31736 num
= fold_non_dependent_expr (num
);
31737 if (!tree_fits_shwi_p (num
)
31738 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31739 || (n
= tree_to_shwi (num
)) <= 0
31742 error_at (loc
, "collapse argument needs positive constant integer expression");
31746 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31747 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31748 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31749 OMP_CLAUSE_CHAIN (c
) = list
;
31750 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31756 default ( none | shared )
31759 default ( none | present ) */
31762 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31763 location_t location
, bool is_oacc
)
31765 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31768 matching_parens parens
;
31769 if (!parens
.require_open (parser
))
31771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31773 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31774 const char *p
= IDENTIFIER_POINTER (id
);
31779 if (strcmp ("none", p
) != 0)
31781 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31785 if (strcmp ("present", p
) != 0 || !is_oacc
)
31787 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
31791 if (strcmp ("shared", p
) != 0 || is_oacc
)
31793 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31800 cp_lexer_consume_token (parser
->lexer
);
31806 cp_parser_error (parser
, "expected %<none%> or %<present%>");
31808 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31811 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
31812 || !parens
.require_close (parser
))
31813 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31814 /*or_comma=*/false,
31815 /*consume_paren=*/true);
31817 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31820 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31821 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31822 OMP_CLAUSE_CHAIN (c
) = list
;
31823 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31829 final ( expression ) */
31832 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31836 matching_parens parens
;
31837 if (!parens
.require_open (parser
))
31840 t
= cp_parser_condition (parser
);
31842 if (t
== error_mark_node
31843 || !parens
.require_close (parser
))
31844 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31845 /*or_comma=*/false,
31846 /*consume_paren=*/true);
31848 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31850 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31851 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31852 OMP_CLAUSE_CHAIN (c
) = list
;
31861 if ( directive-name-modifier : expression )
31863 directive-name-modifier:
31864 parallel | task | taskloop | target data | target | target update
31865 | target enter data | target exit data */
31868 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31872 enum tree_code if_modifier
= ERROR_MARK
;
31874 matching_parens parens
;
31875 if (!parens
.require_open (parser
))
31878 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31880 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31881 const char *p
= IDENTIFIER_POINTER (id
);
31884 if (strcmp ("parallel", p
) == 0)
31885 if_modifier
= OMP_PARALLEL
;
31886 else if (strcmp ("task", p
) == 0)
31887 if_modifier
= OMP_TASK
;
31888 else if (strcmp ("taskloop", p
) == 0)
31889 if_modifier
= OMP_TASKLOOP
;
31890 else if (strcmp ("target", p
) == 0)
31892 if_modifier
= OMP_TARGET
;
31893 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31895 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31896 p
= IDENTIFIER_POINTER (id
);
31897 if (strcmp ("data", p
) == 0)
31898 if_modifier
= OMP_TARGET_DATA
;
31899 else if (strcmp ("update", p
) == 0)
31900 if_modifier
= OMP_TARGET_UPDATE
;
31901 else if (strcmp ("enter", p
) == 0)
31902 if_modifier
= OMP_TARGET_ENTER_DATA
;
31903 else if (strcmp ("exit", p
) == 0)
31904 if_modifier
= OMP_TARGET_EXIT_DATA
;
31905 if (if_modifier
!= OMP_TARGET
)
31910 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
31911 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
31913 if_modifier
= ERROR_MARK
;
31915 if (if_modifier
== OMP_TARGET_ENTER_DATA
31916 || if_modifier
== OMP_TARGET_EXIT_DATA
)
31918 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
31920 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
31921 p
= IDENTIFIER_POINTER (id
);
31922 if (strcmp ("data", p
) == 0)
31928 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
31929 error_at (loc
, "expected %<data%>");
31930 if_modifier
= ERROR_MARK
;
31935 if (if_modifier
!= ERROR_MARK
)
31937 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
31940 cp_lexer_consume_token (parser
->lexer
);
31947 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
31948 error_at (loc
, "expected %<:%>");
31950 if_modifier
= ERROR_MARK
;
31955 t
= cp_parser_condition (parser
);
31957 if (t
== error_mark_node
31958 || !parens
.require_close (parser
))
31959 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31960 /*or_comma=*/false,
31961 /*consume_paren=*/true);
31963 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31964 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
31966 if (if_modifier
!= ERROR_MARK
31967 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31969 const char *p
= NULL
;
31970 switch (if_modifier
)
31972 case OMP_PARALLEL
: p
= "parallel"; break;
31973 case OMP_TASK
: p
= "task"; break;
31974 case OMP_TASKLOOP
: p
= "taskloop"; break;
31975 case OMP_TARGET_DATA
: p
= "target data"; break;
31976 case OMP_TARGET
: p
= "target"; break;
31977 case OMP_TARGET_UPDATE
: p
= "target update"; break;
31978 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
31979 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
31980 default: gcc_unreachable ();
31982 error_at (location
, "too many %<if%> clauses with %qs modifier",
31986 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31989 error_at (location
, "too many %<if%> clauses");
31991 error_at (location
, "too many %<if%> clauses without modifier");
31994 else if (if_modifier
== ERROR_MARK
31995 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
31997 error_at (location
, "if any %<if%> clause has modifier, then all "
31998 "%<if%> clauses have to use modifier");
32003 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32004 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32005 OMP_CLAUSE_IF_EXPR (c
) = t
;
32006 OMP_CLAUSE_CHAIN (c
) = list
;
32015 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32016 tree list
, location_t location
)
32020 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32023 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32024 OMP_CLAUSE_CHAIN (c
) = list
;
32032 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32033 tree list
, location_t location
)
32037 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32039 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32040 OMP_CLAUSE_CHAIN (c
) = list
;
32045 num_threads ( expression ) */
32048 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32049 location_t location
)
32053 matching_parens parens
;
32054 if (!parens
.require_open (parser
))
32057 t
= cp_parser_expression (parser
);
32059 if (t
== error_mark_node
32060 || !parens
.require_close (parser
))
32061 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32062 /*or_comma=*/false,
32063 /*consume_paren=*/true);
32065 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32066 "num_threads", location
);
32068 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32069 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32070 OMP_CLAUSE_CHAIN (c
) = list
;
32076 num_tasks ( expression ) */
32079 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32080 location_t location
)
32084 matching_parens parens
;
32085 if (!parens
.require_open (parser
))
32088 t
= cp_parser_expression (parser
);
32090 if (t
== error_mark_node
32091 || !parens
.require_close (parser
))
32092 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32093 /*or_comma=*/false,
32094 /*consume_paren=*/true);
32096 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32097 "num_tasks", location
);
32099 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32100 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32101 OMP_CLAUSE_CHAIN (c
) = list
;
32107 grainsize ( expression ) */
32110 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32111 location_t location
)
32115 matching_parens parens
;
32116 if (!parens
.require_open (parser
))
32119 t
= cp_parser_expression (parser
);
32121 if (t
== error_mark_node
32122 || !parens
.require_close (parser
))
32123 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32128 "grainsize", location
);
32130 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32131 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32132 OMP_CLAUSE_CHAIN (c
) = list
;
32138 priority ( expression ) */
32141 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32142 location_t location
)
32146 matching_parens parens
;
32147 if (!parens
.require_open (parser
))
32150 t
= cp_parser_expression (parser
);
32152 if (t
== error_mark_node
32153 || !parens
.require_close (parser
))
32154 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32155 /*or_comma=*/false,
32156 /*consume_paren=*/true);
32158 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32159 "priority", location
);
32161 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32162 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32163 OMP_CLAUSE_CHAIN (c
) = list
;
32169 hint ( expression ) */
32172 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32173 location_t location
)
32177 matching_parens parens
;
32178 if (!parens
.require_open (parser
))
32181 t
= cp_parser_expression (parser
);
32183 if (t
== error_mark_node
32184 || !parens
.require_close (parser
))
32185 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32186 /*or_comma=*/false,
32187 /*consume_paren=*/true);
32189 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32191 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32192 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32193 OMP_CLAUSE_CHAIN (c
) = list
;
32199 defaultmap ( tofrom : scalar ) */
32202 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32203 location_t location
)
32208 matching_parens parens
;
32209 if (!parens
.require_open (parser
))
32212 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32214 cp_parser_error (parser
, "expected %<tofrom%>");
32217 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32218 p
= IDENTIFIER_POINTER (id
);
32219 if (strcmp (p
, "tofrom") != 0)
32221 cp_parser_error (parser
, "expected %<tofrom%>");
32224 cp_lexer_consume_token (parser
->lexer
);
32225 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32228 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32230 cp_parser_error (parser
, "expected %<scalar%>");
32233 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32234 p
= IDENTIFIER_POINTER (id
);
32235 if (strcmp (p
, "scalar") != 0)
32237 cp_parser_error (parser
, "expected %<scalar%>");
32240 cp_lexer_consume_token (parser
->lexer
);
32241 if (!parens
.require_close (parser
))
32244 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32247 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32248 OMP_CLAUSE_CHAIN (c
) = list
;
32252 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32253 /*or_comma=*/false,
32254 /*consume_paren=*/true);
32262 ordered ( constant-expression ) */
32265 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32266 tree list
, location_t location
)
32268 tree c
, num
= NULL_TREE
;
32271 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32272 "ordered", location
);
32274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32276 matching_parens parens
;
32277 parens
.consume_open (parser
);
32279 num
= cp_parser_constant_expression (parser
);
32281 if (!parens
.require_close (parser
))
32282 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32283 /*or_comma=*/false,
32284 /*consume_paren=*/true);
32286 if (num
== error_mark_node
)
32288 num
= fold_non_dependent_expr (num
);
32289 if (!tree_fits_shwi_p (num
)
32290 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32291 || (n
= tree_to_shwi (num
)) <= 0
32294 error_at (location
,
32295 "ordered argument needs positive constant integer "
32301 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32302 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32303 OMP_CLAUSE_CHAIN (c
) = list
;
32308 reduction ( reduction-operator : variable-list )
32310 reduction-operator:
32311 One of: + * - & ^ | && ||
32315 reduction-operator:
32316 One of: + * - & ^ | && || min max
32320 reduction-operator:
32321 One of: + * - & ^ | && ||
32325 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32327 enum tree_code code
= ERROR_MARK
;
32328 tree nlist
, c
, id
= NULL_TREE
;
32330 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32333 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32335 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32336 case CPP_MULT
: code
= MULT_EXPR
; break;
32337 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32338 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32339 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32340 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32341 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32342 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32346 if (code
!= ERROR_MARK
)
32347 cp_lexer_consume_token (parser
->lexer
);
32350 bool saved_colon_corrects_to_scope_p
;
32351 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32352 parser
->colon_corrects_to_scope_p
= false;
32353 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32354 /*check_dependency_p=*/true,
32355 /*template_p=*/NULL
,
32356 /*declarator_p=*/false,
32357 /*optional_p=*/false);
32358 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32359 if (identifier_p (id
))
32361 const char *p
= IDENTIFIER_POINTER (id
);
32363 if (strcmp (p
, "min") == 0)
32365 else if (strcmp (p
, "max") == 0)
32367 else if (id
== cp_operator_id (PLUS_EXPR
))
32369 else if (id
== cp_operator_id (MULT_EXPR
))
32371 else if (id
== cp_operator_id (MINUS_EXPR
))
32373 else if (id
== cp_operator_id (BIT_AND_EXPR
))
32374 code
= BIT_AND_EXPR
;
32375 else if (id
== cp_operator_id (BIT_IOR_EXPR
))
32376 code
= BIT_IOR_EXPR
;
32377 else if (id
== cp_operator_id (BIT_XOR_EXPR
))
32378 code
= BIT_XOR_EXPR
;
32379 else if (id
== cp_operator_id (TRUTH_ANDIF_EXPR
))
32380 code
= TRUTH_ANDIF_EXPR
;
32381 else if (id
== cp_operator_id (TRUTH_ORIF_EXPR
))
32382 code
= TRUTH_ORIF_EXPR
;
32383 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32384 tree scope
= parser
->scope
;
32386 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32387 parser
->scope
= NULL_TREE
;
32388 parser
->qualifying_scope
= NULL_TREE
;
32389 parser
->object_scope
= NULL_TREE
;
32393 error ("invalid reduction-identifier");
32395 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32402 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32405 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32407 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32409 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32410 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32417 schedule ( schedule-kind )
32418 schedule ( schedule-kind , expression )
32421 static | dynamic | guided | runtime | auto
32424 schedule ( schedule-modifier : schedule-kind )
32425 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32433 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32436 int modifiers
= 0, nmodifiers
= 0;
32438 matching_parens parens
;
32439 if (!parens
.require_open (parser
))
32442 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32444 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32446 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32447 const char *p
= IDENTIFIER_POINTER (id
);
32448 if (strcmp ("simd", p
) == 0)
32449 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32450 else if (strcmp ("monotonic", p
) == 0)
32451 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32452 else if (strcmp ("nonmonotonic", p
) == 0)
32453 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32456 cp_lexer_consume_token (parser
->lexer
);
32457 if (nmodifiers
++ == 0
32458 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32459 cp_lexer_consume_token (parser
->lexer
);
32462 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32469 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32470 const char *p
= IDENTIFIER_POINTER (id
);
32475 if (strcmp ("dynamic", p
) != 0)
32477 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32481 if (strcmp ("guided", p
) != 0)
32483 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32487 if (strcmp ("runtime", p
) != 0)
32489 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32496 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32497 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32498 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32499 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32502 cp_lexer_consume_token (parser
->lexer
);
32504 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32505 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32506 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32507 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32509 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32517 cp_lexer_consume_token (parser
->lexer
);
32519 token
= cp_lexer_peek_token (parser
->lexer
);
32520 t
= cp_parser_assignment_expression (parser
);
32522 if (t
== error_mark_node
)
32524 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32525 error_at (token
->location
, "schedule %<runtime%> does not take "
32526 "a %<chunk_size%> parameter");
32527 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32528 error_at (token
->location
, "schedule %<auto%> does not take "
32529 "a %<chunk_size%> parameter");
32531 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32533 if (!parens
.require_close (parser
))
32536 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32539 OMP_CLAUSE_SCHEDULE_KIND (c
)
32540 = (enum omp_clause_schedule_kind
)
32541 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32543 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32544 OMP_CLAUSE_CHAIN (c
) = list
;
32548 cp_parser_error (parser
, "invalid schedule kind");
32550 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32551 /*or_comma=*/false,
32552 /*consume_paren=*/true);
32560 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32561 tree list
, location_t location
)
32565 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32567 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32568 OMP_CLAUSE_CHAIN (c
) = list
;
32577 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32578 tree list
, location_t location
)
32580 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32581 tree c
= build_omp_clause (location
, code
);
32582 OMP_CLAUSE_CHAIN (c
) = list
;
32593 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32594 enum omp_clause_code code
,
32595 tree list
, location_t location
)
32597 tree c
= build_omp_clause (location
, code
);
32598 OMP_CLAUSE_CHAIN (c
) = list
;
32606 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32607 tree list
, location_t location
)
32609 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32610 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32611 OMP_CLAUSE_CHAIN (c
) = list
;
32620 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32621 enum omp_clause_code code
,
32622 tree list
, location_t location
)
32624 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32625 tree c
= build_omp_clause (location
, code
);
32626 OMP_CLAUSE_CHAIN (c
) = list
;
32631 num_teams ( expression ) */
32634 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32635 location_t location
)
32639 matching_parens parens
;
32640 if (!parens
.require_open (parser
))
32643 t
= cp_parser_expression (parser
);
32645 if (t
== error_mark_node
32646 || !parens
.require_close (parser
))
32647 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32648 /*or_comma=*/false,
32649 /*consume_paren=*/true);
32651 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32652 "num_teams", location
);
32654 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32655 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32656 OMP_CLAUSE_CHAIN (c
) = list
;
32662 thread_limit ( expression ) */
32665 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32666 location_t location
)
32670 matching_parens parens
;
32671 if (!parens
.require_open (parser
))
32674 t
= cp_parser_expression (parser
);
32676 if (t
== error_mark_node
32677 || !parens
.require_close (parser
))
32678 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32679 /*or_comma=*/false,
32680 /*consume_paren=*/true);
32682 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32683 "thread_limit", location
);
32685 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32686 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32687 OMP_CLAUSE_CHAIN (c
) = list
;
32693 aligned ( variable-list )
32694 aligned ( variable-list : constant-expression ) */
32697 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32699 tree nlist
, c
, alignment
= NULL_TREE
;
32702 matching_parens parens
;
32703 if (!parens
.require_open (parser
))
32706 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32711 alignment
= cp_parser_constant_expression (parser
);
32713 if (!parens
.require_close (parser
))
32714 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32715 /*or_comma=*/false,
32716 /*consume_paren=*/true);
32718 if (alignment
== error_mark_node
)
32719 alignment
= NULL_TREE
;
32722 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32723 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32729 linear ( variable-list )
32730 linear ( variable-list : expression )
32733 linear ( modifier ( variable-list ) )
32734 linear ( modifier ( variable-list ) : expression ) */
32737 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32738 bool is_cilk_simd_fn
, bool declare_simd
)
32740 tree nlist
, c
, step
= integer_one_node
;
32742 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32744 matching_parens parens
;
32745 if (!parens
.require_open (parser
))
32748 if (!is_cilk_simd_fn
32749 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32751 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32752 const char *p
= IDENTIFIER_POINTER (id
);
32754 if (strcmp ("ref", p
) == 0)
32755 kind
= OMP_CLAUSE_LINEAR_REF
;
32756 else if (strcmp ("val", p
) == 0)
32757 kind
= OMP_CLAUSE_LINEAR_VAL
;
32758 else if (strcmp ("uval", p
) == 0)
32759 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32760 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32761 cp_lexer_consume_token (parser
->lexer
);
32763 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32766 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32767 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32771 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32772 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32774 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32775 else if (!parens
.require_close (parser
))
32776 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32777 /*or_comma=*/false,
32778 /*consume_paren=*/true);
32785 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32786 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32788 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32789 cp_parser_parse_tentatively (parser
);
32790 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32791 /*check_dependency_p=*/true,
32792 /*template_p=*/NULL
,
32793 /*declarator_p=*/false,
32794 /*optional_p=*/false);
32795 if (step
!= error_mark_node
)
32796 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32797 if (step
== error_mark_node
)
32800 cp_parser_abort_tentative_parse (parser
);
32802 else if (!cp_parser_parse_definitely (parser
))
32806 step
= cp_parser_expression (parser
);
32808 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
32810 sorry ("using parameters for %<linear%> step is not supported yet");
32811 step
= integer_one_node
;
32813 if (!parens
.require_close (parser
))
32814 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32815 /*or_comma=*/false,
32816 /*consume_paren=*/true);
32818 if (step
== error_mark_node
)
32822 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32824 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32825 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32832 safelen ( constant-expression ) */
32835 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32836 location_t location
)
32840 matching_parens parens
;
32841 if (!parens
.require_open (parser
))
32844 t
= cp_parser_constant_expression (parser
);
32846 if (t
== error_mark_node
32847 || !parens
.require_close (parser
))
32848 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32849 /*or_comma=*/false,
32850 /*consume_paren=*/true);
32852 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32854 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32855 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32856 OMP_CLAUSE_CHAIN (c
) = list
;
32862 simdlen ( constant-expression ) */
32865 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32866 location_t location
)
32870 matching_parens parens
;
32871 if (!parens
.require_open (parser
))
32874 t
= cp_parser_constant_expression (parser
);
32876 if (t
== error_mark_node
32877 || !parens
.require_close (parser
))
32878 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32879 /*or_comma=*/false,
32880 /*consume_paren=*/true);
32882 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32884 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32885 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32886 OMP_CLAUSE_CHAIN (c
) = list
;
32893 identifier [+/- integer]
32894 vec , identifier [+/- integer]
32898 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32903 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32905 cp_parser_error (parser
, "expected identifier");
32909 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32911 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32912 tree t
, identifier
= cp_parser_identifier (parser
);
32913 tree addend
= NULL
;
32915 if (identifier
== error_mark_node
)
32916 t
= error_mark_node
;
32919 t
= cp_parser_lookup_name_simple
32920 (parser
, identifier
,
32921 cp_lexer_peek_token (parser
->lexer
)->location
);
32922 if (t
== error_mark_node
)
32923 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
32928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
32930 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
32932 addend
= integer_zero_node
;
32933 goto add_to_vector
;
32935 cp_lexer_consume_token (parser
->lexer
);
32937 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
32939 cp_parser_error (parser
, "expected integer");
32943 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32944 if (TREE_CODE (addend
) != INTEGER_CST
)
32946 cp_parser_error (parser
, "expected integer");
32949 cp_lexer_consume_token (parser
->lexer
);
32952 if (t
!= error_mark_node
)
32954 vec
= tree_cons (addend
, t
, vec
);
32956 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
32959 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32962 cp_lexer_consume_token (parser
->lexer
);
32965 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
32967 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
32968 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
32969 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
32970 OMP_CLAUSE_CHAIN (u
) = list
;
32977 depend ( depend-kind : variable-list )
32985 depend ( sink : vec ) */
32988 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
32991 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
32993 matching_parens parens
;
32994 if (!parens
.require_open (parser
))
32997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32999 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33000 const char *p
= IDENTIFIER_POINTER (id
);
33002 if (strcmp ("in", p
) == 0)
33003 kind
= OMP_CLAUSE_DEPEND_IN
;
33004 else if (strcmp ("inout", p
) == 0)
33005 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33006 else if (strcmp ("out", p
) == 0)
33007 kind
= OMP_CLAUSE_DEPEND_OUT
;
33008 else if (strcmp ("source", p
) == 0)
33009 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33010 else if (strcmp ("sink", p
) == 0)
33011 kind
= OMP_CLAUSE_DEPEND_SINK
;
33018 cp_lexer_consume_token (parser
->lexer
);
33020 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33022 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33023 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33024 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33025 OMP_CLAUSE_CHAIN (c
) = list
;
33026 if (!parens
.require_close (parser
))
33027 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33028 /*or_comma=*/false,
33029 /*consume_paren=*/true);
33033 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33036 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33037 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33040 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33043 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33044 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33049 cp_parser_error (parser
, "invalid depend kind");
33051 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33052 /*or_comma=*/false,
33053 /*consume_paren=*/true);
33058 map ( map-kind : variable-list )
33059 map ( variable-list )
33062 alloc | to | from | tofrom
33066 alloc | to | from | tofrom | release | delete
33068 map ( always [,] map-kind: variable-list ) */
33071 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33074 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33075 bool always
= false;
33077 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33082 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33083 const char *p
= IDENTIFIER_POINTER (id
);
33085 if (strcmp ("always", p
) == 0)
33088 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33090 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33091 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33093 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33097 cp_lexer_consume_token (parser
->lexer
);
33099 cp_lexer_consume_token (parser
->lexer
);
33104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33105 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33107 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33108 const char *p
= IDENTIFIER_POINTER (id
);
33110 if (strcmp ("alloc", p
) == 0)
33111 kind
= GOMP_MAP_ALLOC
;
33112 else if (strcmp ("to", p
) == 0)
33113 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33114 else if (strcmp ("from", p
) == 0)
33115 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33116 else if (strcmp ("tofrom", p
) == 0)
33117 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33118 else if (strcmp ("release", p
) == 0)
33119 kind
= GOMP_MAP_RELEASE
;
33122 cp_parser_error (parser
, "invalid map kind");
33123 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33124 /*or_comma=*/false,
33125 /*consume_paren=*/true);
33128 cp_lexer_consume_token (parser
->lexer
);
33129 cp_lexer_consume_token (parser
->lexer
);
33131 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33132 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33134 kind
= GOMP_MAP_DELETE
;
33135 cp_lexer_consume_token (parser
->lexer
);
33136 cp_lexer_consume_token (parser
->lexer
);
33139 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33142 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33143 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33149 device ( expression ) */
33152 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33153 location_t location
)
33157 matching_parens parens
;
33158 if (!parens
.require_open (parser
))
33161 t
= cp_parser_expression (parser
);
33163 if (t
== error_mark_node
33164 || !parens
.require_close (parser
))
33165 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33166 /*or_comma=*/false,
33167 /*consume_paren=*/true);
33169 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33170 "device", location
);
33172 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33173 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33174 OMP_CLAUSE_CHAIN (c
) = list
;
33180 dist_schedule ( static )
33181 dist_schedule ( static , expression ) */
33184 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33185 location_t location
)
33189 matching_parens parens
;
33190 if (!parens
.require_open (parser
))
33193 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33195 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33197 cp_lexer_consume_token (parser
->lexer
);
33199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33201 cp_lexer_consume_token (parser
->lexer
);
33203 t
= cp_parser_assignment_expression (parser
);
33205 if (t
== error_mark_node
)
33207 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33209 if (!parens
.require_close (parser
))
33212 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33215 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33217 OMP_CLAUSE_CHAIN (c
) = list
;
33221 cp_parser_error (parser
, "invalid dist_schedule kind");
33223 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33224 /*or_comma=*/false,
33225 /*consume_paren=*/true);
33230 proc_bind ( proc-bind-kind )
33233 master | close | spread */
33236 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33237 location_t location
)
33240 enum omp_clause_proc_bind_kind kind
;
33242 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33247 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33248 const char *p
= IDENTIFIER_POINTER (id
);
33250 if (strcmp ("master", p
) == 0)
33251 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33252 else if (strcmp ("close", p
) == 0)
33253 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33254 else if (strcmp ("spread", p
) == 0)
33255 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33262 cp_lexer_consume_token (parser
->lexer
);
33263 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33266 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33267 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33269 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33270 OMP_CLAUSE_CHAIN (c
) = list
;
33274 cp_parser_error (parser
, "invalid depend kind");
33276 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33283 async [( int-expr )] */
33286 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33289 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33291 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33293 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33295 matching_parens parens
;
33296 parens
.consume_open (parser
);
33298 t
= cp_parser_expression (parser
);
33299 if (t
== error_mark_node
33300 || !parens
.require_close (parser
))
33301 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33302 /*or_comma=*/false,
33303 /*consume_paren=*/true);
33306 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33308 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33309 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33310 OMP_CLAUSE_CHAIN (c
) = list
;
33316 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33317 is a bitmask in MASK. Return the list of clauses found. */
33320 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33321 const char *where
, cp_token
*pragma_tok
,
33322 bool finish_p
= true)
33324 tree clauses
= NULL
;
33327 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33330 pragma_omp_clause c_kind
;
33331 omp_clause_code code
;
33332 const char *c_name
;
33333 tree prev
= clauses
;
33335 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33336 cp_lexer_consume_token (parser
->lexer
);
33338 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33339 c_kind
= cp_parser_omp_clause_name (parser
);
33343 case PRAGMA_OACC_CLAUSE_ASYNC
:
33344 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33347 case PRAGMA_OACC_CLAUSE_AUTO
:
33348 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33352 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33353 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33354 c_name
= "collapse";
33356 case PRAGMA_OACC_CLAUSE_COPY
:
33357 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33360 case PRAGMA_OACC_CLAUSE_COPYIN
:
33361 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33364 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33365 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33366 c_name
= "copyout";
33368 case PRAGMA_OACC_CLAUSE_CREATE
:
33369 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33372 case PRAGMA_OACC_CLAUSE_DELETE
:
33373 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33376 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33377 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33378 c_name
= "default";
33380 case PRAGMA_OACC_CLAUSE_DEVICE
:
33381 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33384 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33385 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33386 c_name
= "deviceptr";
33388 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33389 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33390 c_name
= "device_resident";
33392 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33393 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33395 c_name
= "firstprivate";
33397 case PRAGMA_OACC_CLAUSE_GANG
:
33399 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33402 case PRAGMA_OACC_CLAUSE_HOST
:
33403 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33406 case PRAGMA_OACC_CLAUSE_IF
:
33407 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33410 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33411 clauses
= cp_parser_oacc_simple_clause (parser
,
33412 OMP_CLAUSE_INDEPENDENT
,
33414 c_name
= "independent";
33416 case PRAGMA_OACC_CLAUSE_LINK
:
33417 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33420 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33421 code
= OMP_CLAUSE_NUM_GANGS
;
33422 c_name
= "num_gangs";
33423 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33426 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33427 c_name
= "num_workers";
33428 code
= OMP_CLAUSE_NUM_WORKERS
;
33429 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33432 case PRAGMA_OACC_CLAUSE_PRESENT
:
33433 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33434 c_name
= "present";
33436 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33437 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33438 c_name
= "present_or_copy";
33440 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33441 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33442 c_name
= "present_or_copyin";
33444 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33445 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33446 c_name
= "present_or_copyout";
33448 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33449 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33450 c_name
= "present_or_create";
33452 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33453 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33455 c_name
= "private";
33457 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33458 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33459 c_name
= "reduction";
33461 case PRAGMA_OACC_CLAUSE_SELF
:
33462 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33465 case PRAGMA_OACC_CLAUSE_SEQ
:
33466 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33470 case PRAGMA_OACC_CLAUSE_TILE
:
33471 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33474 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33475 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33477 c_name
= "use_device";
33479 case PRAGMA_OACC_CLAUSE_VECTOR
:
33481 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33484 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33485 c_name
= "vector_length";
33486 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33487 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33490 case PRAGMA_OACC_CLAUSE_WAIT
:
33491 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33494 case PRAGMA_OACC_CLAUSE_WORKER
:
33496 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33500 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33506 if (((mask
>> c_kind
) & 1) == 0)
33508 /* Remove the invalid clause(s) from the list to avoid
33509 confusing the rest of the compiler. */
33511 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33516 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33519 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33524 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33525 is a bitmask in MASK. Return the list of clauses found; the result
33526 of clause default goes in *pdefault. */
33529 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33530 const char *where
, cp_token
*pragma_tok
,
33531 bool finish_p
= true)
33533 tree clauses
= NULL
;
33535 cp_token
*token
= NULL
;
33537 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33539 pragma_omp_clause c_kind
;
33540 const char *c_name
;
33541 tree prev
= clauses
;
33543 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33544 cp_lexer_consume_token (parser
->lexer
);
33546 token
= cp_lexer_peek_token (parser
->lexer
);
33547 c_kind
= cp_parser_omp_clause_name (parser
);
33551 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33552 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33554 c_name
= "collapse";
33556 case PRAGMA_OMP_CLAUSE_COPYIN
:
33557 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33560 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33561 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33563 c_name
= "copyprivate";
33565 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33566 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33567 token
->location
, false);
33568 c_name
= "default";
33570 case PRAGMA_OMP_CLAUSE_FINAL
:
33571 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33574 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33575 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33577 c_name
= "firstprivate";
33579 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33580 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33582 c_name
= "grainsize";
33584 case PRAGMA_OMP_CLAUSE_HINT
:
33585 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33589 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33590 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33592 c_name
= "defaultmap";
33594 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33595 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33597 c_name
= "use_device_ptr";
33599 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33600 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33602 c_name
= "is_device_ptr";
33604 case PRAGMA_OMP_CLAUSE_IF
:
33605 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33609 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33610 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33612 c_name
= "lastprivate";
33614 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33615 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33617 c_name
= "mergeable";
33619 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33620 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33623 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33624 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33626 c_name
= "num_tasks";
33628 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33629 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33631 c_name
= "num_threads";
33633 case PRAGMA_OMP_CLAUSE_ORDERED
:
33634 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33636 c_name
= "ordered";
33638 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33639 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33641 c_name
= "priority";
33643 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33644 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33646 c_name
= "private";
33648 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33649 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33650 c_name
= "reduction";
33652 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33653 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33655 c_name
= "schedule";
33657 case PRAGMA_OMP_CLAUSE_SHARED
:
33658 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33662 case PRAGMA_OMP_CLAUSE_UNTIED
:
33663 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33667 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33668 case PRAGMA_CILK_CLAUSE_MASK
:
33669 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33670 clauses
, token
->location
);
33671 c_name
= "inbranch";
33673 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33674 case PRAGMA_CILK_CLAUSE_NOMASK
:
33675 clauses
= cp_parser_omp_clause_branch (parser
,
33676 OMP_CLAUSE_NOTINBRANCH
,
33677 clauses
, token
->location
);
33678 c_name
= "notinbranch";
33680 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33681 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33682 clauses
, token
->location
);
33683 c_name
= "parallel";
33687 error_at (token
->location
, "%qs must be the first clause of %qs",
33692 case PRAGMA_OMP_CLAUSE_FOR
:
33693 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33694 clauses
, token
->location
);
33697 goto clause_not_first
;
33699 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33700 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33701 clauses
, token
->location
);
33702 c_name
= "sections";
33704 goto clause_not_first
;
33706 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33707 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33708 clauses
, token
->location
);
33709 c_name
= "taskgroup";
33711 goto clause_not_first
;
33713 case PRAGMA_OMP_CLAUSE_LINK
:
33714 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33717 case PRAGMA_OMP_CLAUSE_TO
:
33718 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33719 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33722 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33725 case PRAGMA_OMP_CLAUSE_FROM
:
33726 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33729 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33730 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33732 c_name
= "uniform";
33734 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33735 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33737 c_name
= "num_teams";
33739 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33740 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33742 c_name
= "thread_limit";
33744 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33745 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33746 c_name
= "aligned";
33748 case PRAGMA_OMP_CLAUSE_LINEAR
:
33750 bool cilk_simd_fn
= false, declare_simd
= false;
33751 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
33752 cilk_simd_fn
= true;
33753 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33754 declare_simd
= true;
33755 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
33756 cilk_simd_fn
, declare_simd
);
33760 case PRAGMA_OMP_CLAUSE_DEPEND
:
33761 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33765 case PRAGMA_OMP_CLAUSE_MAP
:
33766 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33769 case PRAGMA_OMP_CLAUSE_DEVICE
:
33770 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33774 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33775 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33777 c_name
= "dist_schedule";
33779 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33780 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33782 c_name
= "proc_bind";
33784 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33785 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33787 c_name
= "safelen";
33789 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33790 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33792 c_name
= "simdlen";
33794 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33795 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33797 c_name
= "nogroup";
33799 case PRAGMA_OMP_CLAUSE_THREADS
:
33801 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33802 clauses
, token
->location
);
33803 c_name
= "threads";
33805 case PRAGMA_OMP_CLAUSE_SIMD
:
33807 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33808 clauses
, token
->location
);
33811 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
33812 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
33813 c_name
= "simdlen";
33816 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33822 if (((mask
>> c_kind
) & 1) == 0)
33824 /* Remove the invalid clause(s) from the list to avoid
33825 confusing the rest of the compiler. */
33827 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33831 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33832 no reason to skip to the end. */
33833 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
33834 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33837 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33838 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33840 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33849 In practice, we're also interested in adding the statement to an
33850 outer node. So it is convenient if we work around the fact that
33851 cp_parser_statement calls add_stmt. */
33854 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33856 unsigned save
= parser
->in_statement
;
33858 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33859 This preserves the "not within loop or switch" style error messages
33860 for nonsense cases like
33866 if (parser
->in_statement
)
33867 parser
->in_statement
= IN_OMP_BLOCK
;
33873 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33875 parser
->in_statement
= save
;
33879 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33881 tree stmt
= begin_omp_structured_block ();
33882 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33884 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33886 cp_parser_end_omp_structured_block (parser
, save
);
33887 return finish_omp_structured_block (stmt
);
33891 # pragma omp atomic new-line
33895 x binop= expr | x++ | ++x | x-- | --x
33897 +, *, -, /, &, ^, |, <<, >>
33899 where x is an lvalue expression with scalar type.
33902 # pragma omp atomic new-line
33905 # pragma omp atomic read new-line
33908 # pragma omp atomic write new-line
33911 # pragma omp atomic update new-line
33914 # pragma omp atomic capture new-line
33917 # pragma omp atomic capture new-line
33925 expression-stmt | x = x binop expr
33927 v = expression-stmt
33929 { v = x; update-stmt; } | { update-stmt; v = x; }
33933 expression-stmt | x = x binop expr | x = expr binop x
33937 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33939 where x and v are lvalue expressions with scalar type. */
33942 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
33944 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
33945 tree rhs1
= NULL_TREE
, orig_lhs
;
33946 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
33947 bool structured_block
= false;
33948 bool seq_cst
= false;
33950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33952 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33953 const char *p
= IDENTIFIER_POINTER (id
);
33955 if (!strcmp (p
, "seq_cst"))
33958 cp_lexer_consume_token (parser
->lexer
);
33959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33960 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33961 cp_lexer_consume_token (parser
->lexer
);
33964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33966 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33967 const char *p
= IDENTIFIER_POINTER (id
);
33969 if (!strcmp (p
, "read"))
33970 code
= OMP_ATOMIC_READ
;
33971 else if (!strcmp (p
, "write"))
33973 else if (!strcmp (p
, "update"))
33975 else if (!strcmp (p
, "capture"))
33976 code
= OMP_ATOMIC_CAPTURE_NEW
;
33980 cp_lexer_consume_token (parser
->lexer
);
33984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33985 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33986 cp_lexer_consume_token (parser
->lexer
);
33988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33990 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33991 const char *p
= IDENTIFIER_POINTER (id
);
33993 if (!strcmp (p
, "seq_cst"))
33996 cp_lexer_consume_token (parser
->lexer
);
34000 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34004 case OMP_ATOMIC_READ
:
34005 case NOP_EXPR
: /* atomic write */
34006 v
= cp_parser_unary_expression (parser
);
34007 if (v
== error_mark_node
)
34009 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34011 if (code
== NOP_EXPR
)
34012 lhs
= cp_parser_expression (parser
);
34014 lhs
= cp_parser_unary_expression (parser
);
34015 if (lhs
== error_mark_node
)
34017 if (code
== NOP_EXPR
)
34019 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34027 case OMP_ATOMIC_CAPTURE_NEW
:
34028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34030 cp_lexer_consume_token (parser
->lexer
);
34031 structured_block
= true;
34035 v
= cp_parser_unary_expression (parser
);
34036 if (v
== error_mark_node
)
34038 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34046 lhs
= cp_parser_unary_expression (parser
);
34048 switch (TREE_CODE (lhs
))
34053 case POSTINCREMENT_EXPR
:
34054 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34055 code
= OMP_ATOMIC_CAPTURE_OLD
;
34057 case PREINCREMENT_EXPR
:
34058 lhs
= TREE_OPERAND (lhs
, 0);
34059 opcode
= PLUS_EXPR
;
34060 rhs
= integer_one_node
;
34063 case POSTDECREMENT_EXPR
:
34064 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34065 code
= OMP_ATOMIC_CAPTURE_OLD
;
34067 case PREDECREMENT_EXPR
:
34068 lhs
= TREE_OPERAND (lhs
, 0);
34069 opcode
= MINUS_EXPR
;
34070 rhs
= integer_one_node
;
34073 case COMPOUND_EXPR
:
34074 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34075 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34076 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34077 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34078 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34079 (TREE_OPERAND (lhs
, 1), 0), 0)))
34081 /* Undo effects of boolean_increment for post {in,de}crement. */
34082 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34085 if (TREE_CODE (lhs
) == MODIFY_EXPR
34086 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34088 /* Undo effects of boolean_increment. */
34089 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34091 /* This is pre or post increment. */
34092 rhs
= TREE_OPERAND (lhs
, 1);
34093 lhs
= TREE_OPERAND (lhs
, 0);
34095 if (code
== OMP_ATOMIC_CAPTURE_NEW
34096 && !structured_block
34097 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34098 code
= OMP_ATOMIC_CAPTURE_OLD
;
34104 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34107 opcode
= MULT_EXPR
;
34110 opcode
= TRUNC_DIV_EXPR
;
34113 opcode
= PLUS_EXPR
;
34116 opcode
= MINUS_EXPR
;
34118 case CPP_LSHIFT_EQ
:
34119 opcode
= LSHIFT_EXPR
;
34121 case CPP_RSHIFT_EQ
:
34122 opcode
= RSHIFT_EXPR
;
34125 opcode
= BIT_AND_EXPR
;
34128 opcode
= BIT_IOR_EXPR
;
34131 opcode
= BIT_XOR_EXPR
;
34134 enum cp_parser_prec oprec
;
34136 cp_lexer_consume_token (parser
->lexer
);
34137 cp_parser_parse_tentatively (parser
);
34138 rhs1
= cp_parser_simple_cast_expression (parser
);
34139 if (rhs1
== error_mark_node
)
34141 cp_parser_abort_tentative_parse (parser
);
34142 cp_parser_simple_cast_expression (parser
);
34145 token
= cp_lexer_peek_token (parser
->lexer
);
34146 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34148 cp_parser_abort_tentative_parse (parser
);
34149 cp_parser_parse_tentatively (parser
);
34150 rhs
= cp_parser_binary_expression (parser
, false, true,
34151 PREC_NOT_OPERATOR
, NULL
);
34152 if (rhs
== error_mark_node
)
34154 cp_parser_abort_tentative_parse (parser
);
34155 cp_parser_binary_expression (parser
, false, true,
34156 PREC_NOT_OPERATOR
, NULL
);
34159 switch (TREE_CODE (rhs
))
34162 case TRUNC_DIV_EXPR
:
34171 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34173 if (cp_parser_parse_definitely (parser
))
34175 opcode
= TREE_CODE (rhs
);
34176 rhs1
= TREE_OPERAND (rhs
, 0);
34177 rhs
= TREE_OPERAND (rhs
, 1);
34187 cp_parser_abort_tentative_parse (parser
);
34188 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34190 rhs
= cp_parser_expression (parser
);
34191 if (rhs
== error_mark_node
)
34197 cp_parser_error (parser
,
34198 "invalid form of %<#pragma omp atomic%>");
34201 if (!cp_parser_parse_definitely (parser
))
34203 switch (token
->type
)
34205 case CPP_SEMICOLON
:
34206 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34208 code
= OMP_ATOMIC_CAPTURE_OLD
;
34213 cp_lexer_consume_token (parser
->lexer
);
34216 else if (structured_block
)
34223 cp_parser_error (parser
,
34224 "invalid form of %<#pragma omp atomic%>");
34227 opcode
= MULT_EXPR
;
34230 opcode
= TRUNC_DIV_EXPR
;
34233 opcode
= PLUS_EXPR
;
34236 opcode
= MINUS_EXPR
;
34239 opcode
= LSHIFT_EXPR
;
34242 opcode
= RSHIFT_EXPR
;
34245 opcode
= BIT_AND_EXPR
;
34248 opcode
= BIT_IOR_EXPR
;
34251 opcode
= BIT_XOR_EXPR
;
34254 cp_parser_error (parser
,
34255 "invalid operator for %<#pragma omp atomic%>");
34258 oprec
= TOKEN_PRECEDENCE (token
);
34259 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34260 if (commutative_tree_code (opcode
))
34261 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34262 cp_lexer_consume_token (parser
->lexer
);
34263 rhs
= cp_parser_binary_expression (parser
, false, false,
34265 if (rhs
== error_mark_node
)
34270 cp_parser_error (parser
,
34271 "invalid operator for %<#pragma omp atomic%>");
34274 cp_lexer_consume_token (parser
->lexer
);
34276 rhs
= cp_parser_expression (parser
);
34277 if (rhs
== error_mark_node
)
34282 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34284 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34286 v
= cp_parser_unary_expression (parser
);
34287 if (v
== error_mark_node
)
34289 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34291 lhs1
= cp_parser_unary_expression (parser
);
34292 if (lhs1
== error_mark_node
)
34295 if (structured_block
)
34297 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34298 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34301 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34302 if (!structured_block
)
34303 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34307 cp_parser_skip_to_end_of_block_or_statement (parser
);
34308 if (structured_block
)
34310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34311 cp_lexer_consume_token (parser
->lexer
);
34312 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34314 cp_parser_skip_to_end_of_block_or_statement (parser
);
34315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34316 cp_lexer_consume_token (parser
->lexer
);
34323 # pragma omp barrier new-line */
34326 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34328 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34329 finish_omp_barrier ();
34333 # pragma omp critical [(name)] new-line
34337 # pragma omp critical [(name) [hint(expression)]] new-line
34338 structured-block */
34340 #define OMP_CRITICAL_CLAUSE_MASK \
34341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34344 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34346 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34350 matching_parens parens
;
34351 parens
.consume_open (parser
);
34353 name
= cp_parser_identifier (parser
);
34355 if (name
== error_mark_node
34356 || !parens
.require_close (parser
))
34357 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34358 /*or_comma=*/false,
34359 /*consume_paren=*/true);
34360 if (name
== error_mark_node
)
34363 clauses
= cp_parser_omp_all_clauses (parser
,
34364 OMP_CRITICAL_CLAUSE_MASK
,
34365 "#pragma omp critical", pragma_tok
);
34368 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34370 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34371 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34375 # pragma omp flush flush-vars[opt] new-line
34378 ( variable-list ) */
34381 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34384 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34385 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34387 finish_omp_flush ();
34390 /* Helper function, to parse omp for increment expression. */
34393 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
34395 tree cond
= cp_parser_binary_expression (parser
, false, true,
34396 PREC_NOT_OPERATOR
, NULL
);
34397 if (cond
== error_mark_node
34398 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34400 cp_parser_skip_to_end_of_statement (parser
);
34401 return error_mark_node
;
34404 switch (TREE_CODE (cond
))
34412 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34414 /* Fall through: OpenMP disallows NE_EXPR. */
34415 gcc_fallthrough ();
34417 return error_mark_node
;
34420 /* If decl is an iterator, preserve LHS and RHS of the relational
34421 expr until finish_omp_for. */
34423 && (type_dependent_expression_p (decl
)
34424 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34427 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34429 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34430 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34431 /*overload=*/NULL
, tf_warning_or_error
);
34434 /* Helper function, to parse omp for increment expression. */
34437 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34445 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34447 op
= (token
->type
== CPP_PLUS_PLUS
34448 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34449 cp_lexer_consume_token (parser
->lexer
);
34450 lhs
= cp_parser_simple_cast_expression (parser
);
34452 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34453 return error_mark_node
;
34454 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34457 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34459 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34460 return error_mark_node
;
34462 token
= cp_lexer_peek_token (parser
->lexer
);
34463 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34465 op
= (token
->type
== CPP_PLUS_PLUS
34466 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34467 cp_lexer_consume_token (parser
->lexer
);
34468 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34471 op
= cp_parser_assignment_operator_opt (parser
);
34472 if (op
== ERROR_MARK
)
34473 return error_mark_node
;
34475 if (op
!= NOP_EXPR
)
34477 rhs
= cp_parser_assignment_expression (parser
);
34478 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34479 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34482 lhs
= cp_parser_binary_expression (parser
, false, false,
34483 PREC_ADDITIVE_EXPRESSION
, NULL
);
34484 token
= cp_lexer_peek_token (parser
->lexer
);
34485 decl_first
= (lhs
== decl
34486 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34489 if (token
->type
!= CPP_PLUS
34490 && token
->type
!= CPP_MINUS
)
34491 return error_mark_node
;
34495 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34496 cp_lexer_consume_token (parser
->lexer
);
34497 rhs
= cp_parser_binary_expression (parser
, false, false,
34498 PREC_ADDITIVE_EXPRESSION
, NULL
);
34499 token
= cp_lexer_peek_token (parser
->lexer
);
34500 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34502 if (lhs
== NULL_TREE
)
34504 if (op
== PLUS_EXPR
)
34507 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34508 tf_warning_or_error
);
34511 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34512 ERROR_MARK
, NULL
, tf_warning_or_error
);
34515 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34520 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34521 || op
== MINUS_EXPR
)
34522 return error_mark_node
;
34523 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34526 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34528 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34531 /* Parse the initialization statement of either an OpenMP for loop or
34532 a Cilk Plus for loop.
34534 Return true if the resulting construct should have an
34535 OMP_CLAUSE_PRIVATE added to it. */
34538 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34539 enum tree_code code
,
34540 tree
&this_pre_body
,
34541 vec
<tree
, va_gc
> *for_block
,
34547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34550 tree add_private_clause
= NULL_TREE
;
34552 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34556 integer-type var = lb
34557 random-access-iterator-type var = lb
34558 pointer-type var = lb
34560 cp_decl_specifier_seq type_specifiers
;
34562 /* First, try to parse as an initialized declaration. See
34563 cp_parser_condition, from whence the bulk of this is copied. */
34565 cp_parser_parse_tentatively (parser
);
34566 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34567 /*is_trailing_return=*/false,
34569 if (cp_parser_parse_definitely (parser
))
34571 /* If parsing a type specifier seq succeeded, then this
34572 MUST be a initialized declaration. */
34573 tree asm_specification
, attributes
;
34574 cp_declarator
*declarator
;
34576 declarator
= cp_parser_declarator (parser
,
34577 CP_PARSER_DECLARATOR_NAMED
,
34578 /*ctor_dtor_or_conv_p=*/NULL
,
34579 /*parenthesized_p=*/NULL
,
34580 /*member_p=*/false,
34581 /*friend_p=*/false);
34582 attributes
= cp_parser_attributes_opt (parser
);
34583 asm_specification
= cp_parser_asm_specification_opt (parser
);
34585 if (declarator
== cp_error_declarator
)
34586 cp_parser_skip_to_end_of_statement (parser
);
34590 tree pushed_scope
, auto_node
;
34592 decl
= start_decl (declarator
, &type_specifiers
,
34593 SD_INITIALIZED
, attributes
,
34594 /*prefix_attributes=*/NULL_TREE
,
34597 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34598 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34600 if (cp_lexer_next_token_is (parser
->lexer
,
34603 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
34604 error ("parenthesized initialization is not allowed in "
34605 "OpenMP %<for%> loop");
34607 error ("parenthesized initialization is "
34608 "not allowed in for-loop");
34611 /* Trigger an error. */
34612 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34614 init
= error_mark_node
;
34615 cp_parser_skip_to_end_of_statement (parser
);
34617 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34618 || type_dependent_expression_p (decl
)
34621 bool is_direct_init
, is_non_constant_init
;
34623 init
= cp_parser_initializer (parser
,
34625 &is_non_constant_init
);
34630 = do_auto_deduction (TREE_TYPE (decl
), init
,
34633 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34634 && !type_dependent_expression_p (decl
))
34638 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34640 LOOKUP_ONLYCONVERTING
);
34642 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34644 vec_safe_push (for_block
, this_pre_body
);
34649 init
= pop_stmt_list (this_pre_body
);
34650 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34652 tree_stmt_iterator i
= tsi_start (init
);
34653 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34654 while (!tsi_end_p (i
))
34656 tree t
= tsi_stmt (i
);
34657 if (TREE_CODE (t
) == DECL_EXPR
34658 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34661 vec_safe_push (for_block
, t
);
34666 if (tsi_one_before_end_p (i
))
34668 tree t
= tsi_stmt (i
);
34670 free_stmt_list (init
);
34675 this_pre_body
= NULL_TREE
;
34680 cp_lexer_consume_token (parser
->lexer
);
34681 init
= cp_parser_assignment_expression (parser
);
34684 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34685 init
= error_mark_node
;
34687 cp_finish_decl (decl
, NULL_TREE
,
34688 /*init_const_expr_p=*/false,
34690 LOOKUP_ONLYCONVERTING
);
34694 pop_scope (pushed_scope
);
34700 /* If parsing a type specifier sequence failed, then
34701 this MUST be a simple expression. */
34702 if (code
== CILK_FOR
)
34703 error ("%<_Cilk_for%> allows expression instead of declaration only "
34704 "in C, not in C++");
34705 cp_parser_parse_tentatively (parser
);
34706 decl
= cp_parser_primary_expression (parser
, false, false,
34708 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34709 if (!cp_parser_error_occurred (parser
)
34711 && (TREE_CODE (decl
) == COMPONENT_REF
34712 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34714 cp_parser_abort_tentative_parse (parser
);
34715 cp_parser_parse_tentatively (parser
);
34716 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34717 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34718 /*check_dependency_p=*/true,
34719 /*template_p=*/NULL
,
34720 /*declarator_p=*/false,
34721 /*optional_p=*/false);
34722 if (name
!= error_mark_node
34723 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34725 decl
= cp_parser_lookup_name_simple (parser
, name
,
34727 if (TREE_CODE (decl
) == FIELD_DECL
)
34728 add_private_clause
= omp_privatize_field (decl
, false);
34730 cp_parser_abort_tentative_parse (parser
);
34731 cp_parser_parse_tentatively (parser
);
34732 decl
= cp_parser_primary_expression (parser
, false, false,
34735 if (!cp_parser_error_occurred (parser
)
34738 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34742 cp_parser_parse_definitely (parser
);
34743 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34744 rhs
= cp_parser_assignment_expression (parser
);
34746 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34749 tf_warning_or_error
));
34750 if (!add_private_clause
)
34751 add_private_clause
= decl
;
34756 cp_parser_abort_tentative_parse (parser
);
34757 init
= cp_parser_expression (parser
);
34760 if (TREE_CODE (init
) == MODIFY_EXPR
34761 || TREE_CODE (init
) == MODOP_EXPR
)
34762 real_decl
= TREE_OPERAND (init
, 0);
34766 return add_private_clause
;
34769 /* Parse the restricted form of the for statement allowed by OpenMP. */
34772 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34773 tree
*cclauses
, bool *if_p
)
34775 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34776 tree real_decl
, initv
, condv
, incrv
, declv
;
34777 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34778 location_t loc_first
;
34779 bool collapse_err
= false;
34780 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34781 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34782 auto_vec
<tree
, 4> orig_inits
;
34783 bool tiling
= false;
34785 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34786 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34787 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34788 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34791 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34793 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34794 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34797 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34800 if (ordered
&& ordered
< collapse
)
34802 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34803 "%<ordered%> clause parameter is less than %<collapse%>");
34804 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34805 = build_int_cst (NULL_TREE
, collapse
);
34806 ordered
= collapse
;
34810 for (tree
*pc
= &clauses
; *pc
; )
34811 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34813 error_at (OMP_CLAUSE_LOCATION (*pc
),
34814 "%<linear%> clause may not be specified together "
34815 "with %<ordered%> clause with a parameter");
34816 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34819 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34822 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34823 count
= ordered
? ordered
: collapse
;
34825 declv
= make_tree_vec (count
);
34826 initv
= make_tree_vec (count
);
34827 condv
= make_tree_vec (count
);
34828 incrv
= make_tree_vec (count
);
34830 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34832 for (i
= 0; i
< count
; i
++)
34834 int bracecount
= 0;
34835 tree add_private_clause
= NULL_TREE
;
34838 if (code
!= CILK_FOR
34839 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34842 cp_parser_error (parser
, "for statement expected");
34845 if (code
== CILK_FOR
34846 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
34849 cp_parser_error (parser
, "_Cilk_for statement expected");
34852 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34854 matching_parens parens
;
34855 if (!parens
.require_open (parser
))
34858 init
= orig_init
= decl
= real_decl
= NULL
;
34859 this_pre_body
= push_stmt_list ();
34862 = cp_parser_omp_for_loop_init (parser
, code
,
34863 this_pre_body
, for_block
,
34864 init
, orig_init
, decl
, real_decl
);
34866 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34869 this_pre_body
= pop_stmt_list (this_pre_body
);
34873 pre_body
= push_stmt_list ();
34875 add_stmt (this_pre_body
);
34876 pre_body
= pop_stmt_list (pre_body
);
34879 pre_body
= this_pre_body
;
34884 if (cclauses
!= NULL
34885 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34886 && real_decl
!= NULL_TREE
)
34889 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34890 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34891 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34893 error_at (loc
, "iteration variable %qD"
34894 " should not be firstprivate", real_decl
);
34895 *c
= OMP_CLAUSE_CHAIN (*c
);
34897 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34898 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34900 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34902 *c
= OMP_CLAUSE_CHAIN (*c
);
34903 if (code
== OMP_SIMD
)
34905 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34906 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34910 OMP_CLAUSE_CHAIN (l
) = clauses
;
34913 add_private_clause
= NULL_TREE
;
34917 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34918 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34919 add_private_clause
= NULL_TREE
;
34920 c
= &OMP_CLAUSE_CHAIN (*c
);
34924 if (add_private_clause
)
34927 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34929 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34930 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34931 && OMP_CLAUSE_DECL (c
) == decl
)
34933 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34934 && OMP_CLAUSE_DECL (c
) == decl
)
34935 error_at (loc
, "iteration variable %qD "
34936 "should not be firstprivate",
34938 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34939 && OMP_CLAUSE_DECL (c
) == decl
)
34940 error_at (loc
, "iteration variable %qD should not be reduction",
34945 if (code
!= OMP_SIMD
)
34946 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
34947 else if (collapse
== 1)
34948 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
34950 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
34951 OMP_CLAUSE_DECL (c
) = add_private_clause
;
34952 c
= finish_omp_clauses (c
, C_ORT_OMP
);
34955 OMP_CLAUSE_CHAIN (c
) = clauses
;
34957 /* For linear, signal that we need to fill up
34958 the so far unknown linear step. */
34959 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
34960 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
34966 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34967 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
34968 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34971 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
34973 /* If decl is an iterator, preserve the operator on decl
34974 until finish_omp_for. */
34976 && ((processing_template_decl
34977 && (TREE_TYPE (real_decl
) == NULL_TREE
34978 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
34979 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
34980 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
34982 incr
= cp_parser_expression (parser
);
34983 if (!EXPR_HAS_LOCATION (incr
))
34984 protected_set_expr_location (incr
, input_location
);
34987 if (!parens
.require_close (parser
))
34988 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34989 /*or_comma=*/false,
34990 /*consume_paren=*/true);
34992 TREE_VEC_ELT (declv
, i
) = decl
;
34993 TREE_VEC_ELT (initv
, i
) = init
;
34994 TREE_VEC_ELT (condv
, i
) = cond
;
34995 TREE_VEC_ELT (incrv
, i
) = incr
;
34998 orig_inits
.safe_grow_cleared (i
+ 1);
34999 orig_inits
[i
] = orig_init
;
35002 if (i
== count
- 1)
35005 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35006 in between the collapsed for loops to be still considered perfectly
35007 nested. Hopefully the final version clarifies this.
35008 For now handle (multiple) {'s and empty statements. */
35009 cp_parser_parse_tentatively (parser
);
35012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35014 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35016 cp_lexer_consume_token (parser
->lexer
);
35019 else if (bracecount
35020 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35021 cp_lexer_consume_token (parser
->lexer
);
35024 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35025 error_at (loc
, "not enough for loops to collapse");
35026 collapse_err
= true;
35027 cp_parser_abort_tentative_parse (parser
);
35035 cp_parser_parse_definitely (parser
);
35036 nbraces
+= bracecount
;
35043 /* Note that we saved the original contents of this flag when we entered
35044 the structured block, and so we don't need to re-save it here. */
35045 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
35046 parser
->in_statement
= IN_CILK_SIMD_FOR
;
35048 parser
->in_statement
= IN_OMP_FOR
;
35050 /* Note that the grammar doesn't call for a structured block here,
35051 though the loop as a whole is a structured block. */
35052 body
= push_stmt_list ();
35053 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35054 body
= pop_stmt_list (body
);
35056 if (declv
== NULL_TREE
)
35059 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35060 body
, pre_body
, &orig_inits
, clauses
);
35064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35066 cp_lexer_consume_token (parser
->lexer
);
35069 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35070 cp_lexer_consume_token (parser
->lexer
);
35075 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35076 "collapsed loops not perfectly nested");
35078 collapse_err
= true;
35079 cp_parser_statement_seq_opt (parser
, NULL
);
35080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35085 while (!for_block
->is_empty ())
35087 tree t
= for_block
->pop ();
35088 if (TREE_CODE (t
) == STATEMENT_LIST
)
35089 add_stmt (pop_stmt_list (t
));
35093 release_tree_vector (for_block
);
35098 /* Helper function for OpenMP parsing, split clauses and call
35099 finish_omp_clauses on each of the set of clauses afterwards. */
35102 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35103 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35106 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35107 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35109 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35113 #pragma omp simd simd-clause[optseq] new-line
35116 #define OMP_SIMD_CLAUSE_MASK \
35117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35127 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35128 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35131 tree clauses
, sb
, ret
;
35133 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35135 strcat (p_name
, " simd");
35136 mask
|= OMP_SIMD_CLAUSE_MASK
;
35138 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35142 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35143 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35144 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35145 OMP_CLAUSE_ORDERED
);
35146 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35148 error_at (OMP_CLAUSE_LOCATION (c
),
35149 "%<ordered%> clause with parameter may not be specified "
35150 "on %qs construct", p_name
);
35151 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35155 sb
= begin_omp_structured_block ();
35156 save
= cp_parser_begin_omp_structured_block (parser
);
35158 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35160 cp_parser_end_omp_structured_block (parser
, save
);
35161 add_stmt (finish_omp_structured_block (sb
));
35167 #pragma omp for for-clause[optseq] new-line
35171 #pragma omp for simd for-simd-clause[optseq] new-line
35174 #define OMP_FOR_CLAUSE_MASK \
35175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35186 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35187 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35190 tree clauses
, sb
, ret
;
35192 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35194 strcat (p_name
, " for");
35195 mask
|= OMP_FOR_CLAUSE_MASK
;
35196 /* parallel for{, simd} disallows nowait clause, but for
35197 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35198 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35199 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35200 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35201 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35202 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35206 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35207 const char *p
= IDENTIFIER_POINTER (id
);
35209 if (strcmp (p
, "simd") == 0)
35211 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35212 if (cclauses
== NULL
)
35213 cclauses
= cclauses_buf
;
35215 cp_lexer_consume_token (parser
->lexer
);
35216 if (!flag_openmp
) /* flag_openmp_simd */
35217 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35219 sb
= begin_omp_structured_block ();
35220 save
= cp_parser_begin_omp_structured_block (parser
);
35221 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35223 cp_parser_end_omp_structured_block (parser
, save
);
35224 tree body
= finish_omp_structured_block (sb
);
35227 ret
= make_node (OMP_FOR
);
35228 TREE_TYPE (ret
) = void_type_node
;
35229 OMP_FOR_BODY (ret
) = body
;
35230 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35231 SET_EXPR_LOCATION (ret
, loc
);
35236 if (!flag_openmp
) /* flag_openmp_simd */
35238 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35242 /* Composite distribute parallel for disallows linear clause. */
35243 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35244 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35246 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35250 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35251 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35254 sb
= begin_omp_structured_block ();
35255 save
= cp_parser_begin_omp_structured_block (parser
);
35257 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35259 cp_parser_end_omp_structured_block (parser
, save
);
35260 add_stmt (finish_omp_structured_block (sb
));
35266 # pragma omp master new-line
35267 structured-block */
35270 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35272 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35273 return c_finish_omp_master (input_location
,
35274 cp_parser_omp_structured_block (parser
, if_p
));
35278 # pragma omp ordered new-line
35282 # pragma omp ordered ordered-clauses new-line
35283 structured-block */
35285 #define OMP_ORDERED_CLAUSE_MASK \
35286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35289 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35290 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35293 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35294 enum pragma_context context
, bool *if_p
)
35296 location_t loc
= pragma_tok
->location
;
35298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35300 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35301 const char *p
= IDENTIFIER_POINTER (id
);
35303 if (strcmp (p
, "depend") == 0)
35305 if (!flag_openmp
) /* flag_openmp_simd */
35307 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35310 if (context
== pragma_stmt
)
35312 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35313 "%<depend%> clause may only be used in compound "
35315 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35319 = cp_parser_omp_all_clauses (parser
,
35320 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35321 "#pragma omp ordered", pragma_tok
);
35322 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35328 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35329 "#pragma omp ordered", pragma_tok
);
35331 if (!flag_openmp
/* flag_openmp_simd */
35332 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35335 c_finish_omp_ordered (loc
, clauses
,
35336 cp_parser_omp_structured_block (parser
, if_p
));
35343 { section-sequence }
35346 section-directive[opt] structured-block
35347 section-sequence section-directive structured-block */
35350 cp_parser_omp_sections_scope (cp_parser
*parser
)
35352 tree stmt
, substmt
;
35353 bool error_suppress
= false;
35356 matching_braces braces
;
35357 if (!braces
.require_open (parser
))
35360 stmt
= push_stmt_list ();
35362 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35363 != PRAGMA_OMP_SECTION
)
35365 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35366 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35367 add_stmt (substmt
);
35372 tok
= cp_lexer_peek_token (parser
->lexer
);
35373 if (tok
->type
== CPP_CLOSE_BRACE
)
35375 if (tok
->type
== CPP_EOF
)
35378 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35380 cp_lexer_consume_token (parser
->lexer
);
35381 cp_parser_require_pragma_eol (parser
, tok
);
35382 error_suppress
= false;
35384 else if (!error_suppress
)
35386 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35387 error_suppress
= true;
35390 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35391 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35392 add_stmt (substmt
);
35394 braces
.require_close (parser
);
35396 substmt
= pop_stmt_list (stmt
);
35398 stmt
= make_node (OMP_SECTIONS
);
35399 TREE_TYPE (stmt
) = void_type_node
;
35400 OMP_SECTIONS_BODY (stmt
) = substmt
;
35407 # pragma omp sections sections-clause[optseq] newline
35410 #define OMP_SECTIONS_CLAUSE_MASK \
35411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35418 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35419 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35422 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35424 strcat (p_name
, " sections");
35425 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35427 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35429 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35433 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35434 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35437 ret
= cp_parser_omp_sections_scope (parser
);
35439 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35445 # pragma omp parallel parallel-clause[optseq] new-line
35447 # pragma omp parallel for parallel-for-clause[optseq] new-line
35449 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35453 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35454 structured-block */
35456 #define OMP_PARALLEL_CLAUSE_MASK \
35457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35468 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35469 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35472 tree stmt
, clauses
, block
;
35474 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35476 strcat (p_name
, " parallel");
35477 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35478 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35479 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35480 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35481 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35483 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35485 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35486 if (cclauses
== NULL
)
35487 cclauses
= cclauses_buf
;
35489 cp_lexer_consume_token (parser
->lexer
);
35490 if (!flag_openmp
) /* flag_openmp_simd */
35491 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35493 block
= begin_omp_parallel ();
35494 save
= cp_parser_begin_omp_structured_block (parser
);
35495 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35497 cp_parser_end_omp_structured_block (parser
, save
);
35498 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35500 if (ret
== NULL_TREE
)
35502 OMP_PARALLEL_COMBINED (stmt
) = 1;
35505 /* When combined with distribute, parallel has to be followed by for.
35506 #pragma omp target parallel is allowed though. */
35508 && (mask
& (OMP_CLAUSE_MASK_1
35509 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35511 error_at (loc
, "expected %<for%> after %qs", p_name
);
35512 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35515 else if (!flag_openmp
) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35520 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35522 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35523 const char *p
= IDENTIFIER_POINTER (id
);
35524 if (strcmp (p
, "sections") == 0)
35526 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35527 cclauses
= cclauses_buf
;
35529 cp_lexer_consume_token (parser
->lexer
);
35530 block
= begin_omp_parallel ();
35531 save
= cp_parser_begin_omp_structured_block (parser
);
35532 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35533 cp_parser_end_omp_structured_block (parser
, save
);
35534 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35536 OMP_PARALLEL_COMBINED (stmt
) = 1;
35541 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35545 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35546 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35549 block
= begin_omp_parallel ();
35550 save
= cp_parser_begin_omp_structured_block (parser
);
35551 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35552 cp_parser_end_omp_structured_block (parser
, save
);
35553 stmt
= finish_omp_parallel (clauses
, block
);
35558 # pragma omp single single-clause[optseq] new-line
35559 structured-block */
35561 #define OMP_SINGLE_CLAUSE_MASK \
35562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35568 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35570 tree stmt
= make_node (OMP_SINGLE
);
35571 TREE_TYPE (stmt
) = void_type_node
;
35573 OMP_SINGLE_CLAUSES (stmt
)
35574 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35575 "#pragma omp single", pragma_tok
);
35576 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35578 return add_stmt (stmt
);
35582 # pragma omp task task-clause[optseq] new-line
35583 structured-block */
35585 #define OMP_TASK_CLAUSE_MASK \
35586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35598 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35600 tree clauses
, block
;
35603 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35604 "#pragma omp task", pragma_tok
);
35605 block
= begin_omp_task ();
35606 save
= cp_parser_begin_omp_structured_block (parser
);
35607 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35608 cp_parser_end_omp_structured_block (parser
, save
);
35609 return finish_omp_task (clauses
, block
);
35613 # pragma omp taskwait new-line */
35616 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35618 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35619 finish_omp_taskwait ();
35623 # pragma omp taskyield new-line */
35626 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35628 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35629 finish_omp_taskyield ();
35633 # pragma omp taskgroup new-line
35634 structured-block */
35637 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35639 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35640 return c_finish_omp_taskgroup (input_location
,
35641 cp_parser_omp_structured_block (parser
,
35647 # pragma omp threadprivate (variable-list) */
35650 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35654 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35655 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35657 finish_omp_threadprivate (vars
);
35661 # pragma omp cancel cancel-clause[optseq] new-line */
35663 #define OMP_CANCEL_CLAUSE_MASK \
35664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35671 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35673 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35674 "#pragma omp cancel", pragma_tok
);
35675 finish_omp_cancel (clauses
);
35679 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35681 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35688 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35689 enum pragma_context context
)
35692 bool point_seen
= false;
35694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35696 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35697 const char *p
= IDENTIFIER_POINTER (id
);
35699 if (strcmp (p
, "point") == 0)
35701 cp_lexer_consume_token (parser
->lexer
);
35707 cp_parser_error (parser
, "expected %<point%>");
35708 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35712 if (context
!= pragma_compound
)
35714 if (context
== pragma_stmt
)
35715 error_at (pragma_tok
->location
,
35716 "%<#pragma %s%> may only be used in compound statements",
35717 "omp cancellation point");
35719 cp_parser_error (parser
, "expected declaration specifiers");
35720 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35724 clauses
= cp_parser_omp_all_clauses (parser
,
35725 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35726 "#pragma omp cancellation point",
35728 finish_omp_cancellation_point (clauses
);
35732 #pragma omp distribute distribute-clause[optseq] new-line
35735 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35743 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35744 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35747 tree clauses
, sb
, ret
;
35749 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35751 strcat (p_name
, " distribute");
35752 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35756 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35757 const char *p
= IDENTIFIER_POINTER (id
);
35759 bool parallel
= false;
35761 if (strcmp (p
, "simd") == 0)
35764 parallel
= strcmp (p
, "parallel") == 0;
35765 if (parallel
|| simd
)
35767 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35768 if (cclauses
== NULL
)
35769 cclauses
= cclauses_buf
;
35770 cp_lexer_consume_token (parser
->lexer
);
35771 if (!flag_openmp
) /* flag_openmp_simd */
35774 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35777 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35780 sb
= begin_omp_structured_block ();
35781 save
= cp_parser_begin_omp_structured_block (parser
);
35783 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35786 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35788 cp_parser_end_omp_structured_block (parser
, save
);
35789 tree body
= finish_omp_structured_block (sb
);
35792 ret
= make_node (OMP_DISTRIBUTE
);
35793 TREE_TYPE (ret
) = void_type_node
;
35794 OMP_FOR_BODY (ret
) = body
;
35795 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35796 SET_EXPR_LOCATION (ret
, loc
);
35801 if (!flag_openmp
) /* flag_openmp_simd */
35803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35807 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35811 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35812 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35815 sb
= begin_omp_structured_block ();
35816 save
= cp_parser_begin_omp_structured_block (parser
);
35818 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35820 cp_parser_end_omp_structured_block (parser
, save
);
35821 add_stmt (finish_omp_structured_block (sb
));
35827 # pragma omp teams teams-clause[optseq] new-line
35828 structured-block */
35830 #define OMP_TEAMS_CLAUSE_MASK \
35831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35840 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35841 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35844 tree clauses
, sb
, ret
;
35846 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35848 strcat (p_name
, " teams");
35849 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35853 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35854 const char *p
= IDENTIFIER_POINTER (id
);
35855 if (strcmp (p
, "distribute") == 0)
35857 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35858 if (cclauses
== NULL
)
35859 cclauses
= cclauses_buf
;
35861 cp_lexer_consume_token (parser
->lexer
);
35862 if (!flag_openmp
) /* flag_openmp_simd */
35863 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35865 sb
= begin_omp_structured_block ();
35866 save
= cp_parser_begin_omp_structured_block (parser
);
35867 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35869 cp_parser_end_omp_structured_block (parser
, save
);
35870 tree body
= finish_omp_structured_block (sb
);
35873 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35874 ret
= make_node (OMP_TEAMS
);
35875 TREE_TYPE (ret
) = void_type_node
;
35876 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35877 OMP_TEAMS_BODY (ret
) = body
;
35878 OMP_TEAMS_COMBINED (ret
) = 1;
35879 SET_EXPR_LOCATION (ret
, loc
);
35880 return add_stmt (ret
);
35883 if (!flag_openmp
) /* flag_openmp_simd */
35885 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35889 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35893 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35894 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35897 tree stmt
= make_node (OMP_TEAMS
);
35898 TREE_TYPE (stmt
) = void_type_node
;
35899 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35900 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35901 SET_EXPR_LOCATION (stmt
, loc
);
35903 return add_stmt (stmt
);
35907 # pragma omp target data target-data-clause[optseq] new-line
35908 structured-block */
35910 #define OMP_TARGET_DATA_CLAUSE_MASK \
35911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35917 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35920 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35921 "#pragma omp target data", pragma_tok
);
35923 for (tree
*pc
= &clauses
; *pc
;)
35925 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35926 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35929 case GOMP_MAP_ALWAYS_TO
:
35930 case GOMP_MAP_FROM
:
35931 case GOMP_MAP_ALWAYS_FROM
:
35932 case GOMP_MAP_TOFROM
:
35933 case GOMP_MAP_ALWAYS_TOFROM
:
35934 case GOMP_MAP_ALLOC
:
35937 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35938 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35939 case GOMP_MAP_ALWAYS_POINTER
:
35943 error_at (OMP_CLAUSE_LOCATION (*pc
),
35944 "%<#pragma omp target data%> with map-type other "
35945 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35946 "on %<map%> clause");
35947 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35950 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35956 error_at (pragma_tok
->location
,
35957 "%<#pragma omp target data%> must contain at least "
35958 "one %<map%> clause");
35962 tree stmt
= make_node (OMP_TARGET_DATA
);
35963 TREE_TYPE (stmt
) = void_type_node
;
35964 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
35966 keep_next_level (true);
35967 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35969 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35970 return add_stmt (stmt
);
35974 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35975 structured-block */
35977 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35985 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35986 enum pragma_context context
)
35988 bool data_seen
= false;
35989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35991 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35992 const char *p
= IDENTIFIER_POINTER (id
);
35994 if (strcmp (p
, "data") == 0)
35996 cp_lexer_consume_token (parser
->lexer
);
36002 cp_parser_error (parser
, "expected %<data%>");
36003 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36007 if (context
== pragma_stmt
)
36009 error_at (pragma_tok
->location
,
36010 "%<#pragma %s%> may only be used in compound statements",
36011 "omp target enter data");
36012 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36017 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36018 "#pragma omp target enter data", pragma_tok
);
36020 for (tree
*pc
= &clauses
; *pc
;)
36022 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36023 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36026 case GOMP_MAP_ALWAYS_TO
:
36027 case GOMP_MAP_ALLOC
:
36030 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36031 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36032 case GOMP_MAP_ALWAYS_POINTER
:
36036 error_at (OMP_CLAUSE_LOCATION (*pc
),
36037 "%<#pragma omp target enter data%> with map-type other "
36038 "than %<to%> or %<alloc%> on %<map%> clause");
36039 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36042 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36048 error_at (pragma_tok
->location
,
36049 "%<#pragma omp target enter data%> must contain at least "
36050 "one %<map%> clause");
36054 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36055 TREE_TYPE (stmt
) = void_type_node
;
36056 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36057 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36058 return add_stmt (stmt
);
36062 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36063 structured-block */
36065 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36073 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36074 enum pragma_context context
)
36076 bool data_seen
= false;
36077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36079 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36080 const char *p
= IDENTIFIER_POINTER (id
);
36082 if (strcmp (p
, "data") == 0)
36084 cp_lexer_consume_token (parser
->lexer
);
36090 cp_parser_error (parser
, "expected %<data%>");
36091 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36095 if (context
== pragma_stmt
)
36097 error_at (pragma_tok
->location
,
36098 "%<#pragma %s%> may only be used in compound statements",
36099 "omp target exit data");
36100 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36105 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36106 "#pragma omp target exit data", pragma_tok
);
36108 for (tree
*pc
= &clauses
; *pc
;)
36110 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36111 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36113 case GOMP_MAP_FROM
:
36114 case GOMP_MAP_ALWAYS_FROM
:
36115 case GOMP_MAP_RELEASE
:
36116 case GOMP_MAP_DELETE
:
36119 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36120 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36121 case GOMP_MAP_ALWAYS_POINTER
:
36125 error_at (OMP_CLAUSE_LOCATION (*pc
),
36126 "%<#pragma omp target exit data%> with map-type other "
36127 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36129 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36132 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36138 error_at (pragma_tok
->location
,
36139 "%<#pragma omp target exit data%> must contain at least "
36140 "one %<map%> clause");
36144 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36145 TREE_TYPE (stmt
) = void_type_node
;
36146 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36147 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36148 return add_stmt (stmt
);
36152 # pragma omp target update target-update-clause[optseq] new-line */
36154 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36163 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36164 enum pragma_context context
)
36166 if (context
== pragma_stmt
)
36168 error_at (pragma_tok
->location
,
36169 "%<#pragma %s%> may only be used in compound statements",
36170 "omp target update");
36171 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36176 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36177 "#pragma omp target update", pragma_tok
);
36178 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36179 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36181 error_at (pragma_tok
->location
,
36182 "%<#pragma omp target update%> must contain at least one "
36183 "%<from%> or %<to%> clauses");
36187 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36188 TREE_TYPE (stmt
) = void_type_node
;
36189 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36190 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36196 # pragma omp target target-clause[optseq] new-line
36197 structured-block */
36199 #define OMP_TARGET_CLAUSE_MASK \
36200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36211 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36212 enum pragma_context context
, bool *if_p
)
36214 tree
*pc
= NULL
, stmt
;
36216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36218 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36219 const char *p
= IDENTIFIER_POINTER (id
);
36220 enum tree_code ccode
= ERROR_MARK
;
36222 if (strcmp (p
, "teams") == 0)
36224 else if (strcmp (p
, "parallel") == 0)
36225 ccode
= OMP_PARALLEL
;
36226 else if (strcmp (p
, "simd") == 0)
36228 if (ccode
!= ERROR_MARK
)
36230 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36231 char p_name
[sizeof ("#pragma omp target teams distribute "
36232 "parallel for simd")];
36234 cp_lexer_consume_token (parser
->lexer
);
36235 strcpy (p_name
, "#pragma omp target");
36236 if (!flag_openmp
) /* flag_openmp_simd */
36242 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36243 OMP_TARGET_CLAUSE_MASK
,
36247 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36248 OMP_TARGET_CLAUSE_MASK
,
36252 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36253 OMP_TARGET_CLAUSE_MASK
,
36257 gcc_unreachable ();
36259 return stmt
!= NULL_TREE
;
36261 keep_next_level (true);
36262 tree sb
= begin_omp_structured_block (), ret
;
36263 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36267 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36268 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36272 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36273 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36277 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36278 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36282 gcc_unreachable ();
36284 cp_parser_end_omp_structured_block (parser
, save
);
36285 tree body
= finish_omp_structured_block (sb
);
36286 if (ret
== NULL_TREE
)
36288 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36290 /* For combined target teams, ensure the num_teams and
36291 thread_limit clause expressions are evaluated on the host,
36292 before entering the target construct. */
36294 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36295 c
; c
= OMP_CLAUSE_CHAIN (c
))
36296 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36297 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36298 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36300 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36301 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36302 if (expr
== error_mark_node
)
36304 tree tmp
= TARGET_EXPR_SLOT (expr
);
36306 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36307 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36308 OMP_CLAUSE_FIRSTPRIVATE
);
36309 OMP_CLAUSE_DECL (tc
) = tmp
;
36310 OMP_CLAUSE_CHAIN (tc
)
36311 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36312 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36315 tree stmt
= make_node (OMP_TARGET
);
36316 TREE_TYPE (stmt
) = void_type_node
;
36317 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36318 OMP_TARGET_BODY (stmt
) = body
;
36319 OMP_TARGET_COMBINED (stmt
) = 1;
36320 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36322 pc
= &OMP_TARGET_CLAUSES (stmt
);
36323 goto check_clauses
;
36325 else if (!flag_openmp
) /* flag_openmp_simd */
36327 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36330 else if (strcmp (p
, "data") == 0)
36332 cp_lexer_consume_token (parser
->lexer
);
36333 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36336 else if (strcmp (p
, "enter") == 0)
36338 cp_lexer_consume_token (parser
->lexer
);
36339 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36342 else if (strcmp (p
, "exit") == 0)
36344 cp_lexer_consume_token (parser
->lexer
);
36345 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36348 else if (strcmp (p
, "update") == 0)
36350 cp_lexer_consume_token (parser
->lexer
);
36351 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36354 if (!flag_openmp
) /* flag_openmp_simd */
36356 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36360 stmt
= make_node (OMP_TARGET
);
36361 TREE_TYPE (stmt
) = void_type_node
;
36363 OMP_TARGET_CLAUSES (stmt
)
36364 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36365 "#pragma omp target", pragma_tok
);
36366 pc
= &OMP_TARGET_CLAUSES (stmt
);
36367 keep_next_level (true);
36368 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36370 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36376 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36377 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36380 case GOMP_MAP_ALWAYS_TO
:
36381 case GOMP_MAP_FROM
:
36382 case GOMP_MAP_ALWAYS_FROM
:
36383 case GOMP_MAP_TOFROM
:
36384 case GOMP_MAP_ALWAYS_TOFROM
:
36385 case GOMP_MAP_ALLOC
:
36386 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36387 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36388 case GOMP_MAP_ALWAYS_POINTER
:
36391 error_at (OMP_CLAUSE_LOCATION (*pc
),
36392 "%<#pragma omp target%> with map-type other "
36393 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36394 "on %<map%> clause");
36395 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36398 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36404 # pragma acc cache (variable-list) new-line
36408 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36410 tree stmt
, clauses
;
36412 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36413 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36415 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36417 stmt
= make_node (OACC_CACHE
);
36418 TREE_TYPE (stmt
) = void_type_node
;
36419 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36420 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36427 # pragma acc data oacc-data-clause[optseq] new-line
36428 structured-block */
36430 #define OACC_DATA_CLAUSE_MASK \
36431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36444 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36446 tree stmt
, clauses
, block
;
36449 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36450 "#pragma acc data", pragma_tok
);
36452 block
= begin_omp_parallel ();
36453 save
= cp_parser_begin_omp_structured_block (parser
);
36454 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36455 cp_parser_end_omp_structured_block (parser
, save
);
36456 stmt
= finish_oacc_data (clauses
, block
);
36461 # pragma acc host_data <clauses> new-line
36462 structured-block */
36464 #define OACC_HOST_DATA_CLAUSE_MASK \
36465 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36468 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36470 tree stmt
, clauses
, block
;
36473 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36474 "#pragma acc host_data", pragma_tok
);
36476 block
= begin_omp_parallel ();
36477 save
= cp_parser_begin_omp_structured_block (parser
);
36478 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36479 cp_parser_end_omp_structured_block (parser
, save
);
36480 stmt
= finish_oacc_host_data (clauses
, block
);
36485 # pragma acc declare oacc-data-clause[optseq] new-line
36488 #define OACC_DECLARE_CLAUSE_MASK \
36489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36503 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36505 tree clauses
, stmt
;
36506 bool error
= false;
36508 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36509 "#pragma acc declare", pragma_tok
, true);
36512 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36514 error_at (pragma_tok
->location
,
36515 "no valid clauses specified in %<#pragma acc declare%>");
36519 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36521 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36522 tree decl
= OMP_CLAUSE_DECL (t
);
36523 if (!DECL_P (decl
))
36525 error_at (loc
, "array section in %<#pragma acc declare%>");
36529 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36530 switch (OMP_CLAUSE_MAP_KIND (t
))
36532 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36533 case GOMP_MAP_FORCE_ALLOC
:
36534 case GOMP_MAP_FORCE_TO
:
36535 case GOMP_MAP_FORCE_DEVICEPTR
:
36536 case GOMP_MAP_DEVICE_RESIDENT
:
36539 case GOMP_MAP_LINK
:
36540 if (!global_bindings_p ()
36541 && (TREE_STATIC (decl
)
36542 || !DECL_EXTERNAL (decl
)))
36545 "%qD must be a global variable in "
36546 "%<#pragma acc declare link%>",
36554 if (global_bindings_p ())
36556 error_at (loc
, "invalid OpenACC clause at file scope");
36560 if (DECL_EXTERNAL (decl
))
36563 "invalid use of %<extern%> variable %qD "
36564 "in %<#pragma acc declare%>", decl
);
36568 else if (TREE_PUBLIC (decl
))
36571 "invalid use of %<global%> variable %qD "
36572 "in %<#pragma acc declare%>", decl
);
36579 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36580 || lookup_attribute ("omp declare target link",
36581 DECL_ATTRIBUTES (decl
)))
36583 error_at (loc
, "variable %qD used more than once with "
36584 "%<#pragma acc declare%>", decl
);
36593 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36594 id
= get_identifier ("omp declare target link");
36596 id
= get_identifier ("omp declare target");
36598 DECL_ATTRIBUTES (decl
)
36599 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36600 if (global_bindings_p ())
36602 symtab_node
*node
= symtab_node::get (decl
);
36605 node
->offloadable
= 1;
36606 if (ENABLE_OFFLOADING
)
36608 g
->have_offload
= true;
36609 if (is_a
<varpool_node
*> (node
))
36610 vec_safe_push (offload_vars
, decl
);
36617 if (error
|| global_bindings_p ())
36620 stmt
= make_node (OACC_DECLARE
);
36621 TREE_TYPE (stmt
) = void_type_node
;
36622 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36623 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36631 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36635 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36637 LOC is the location of the #pragma token.
36640 #define OACC_ENTER_DATA_CLAUSE_MASK \
36641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36649 #define OACC_EXIT_DATA_CLAUSE_MASK \
36650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36657 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36660 location_t loc
= pragma_tok
->location
;
36661 tree stmt
, clauses
;
36662 const char *p
= "";
36664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36665 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36667 if (strcmp (p
, "data") != 0)
36669 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36670 enter
? "enter" : "exit");
36671 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36675 cp_lexer_consume_token (parser
->lexer
);
36678 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36679 "#pragma acc enter data", pragma_tok
);
36681 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36682 "#pragma acc exit data", pragma_tok
);
36684 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36686 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36687 enter
? "enter" : "exit");
36691 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36692 TREE_TYPE (stmt
) = void_type_node
;
36693 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36694 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36700 # pragma acc loop oacc-loop-clause[optseq] new-line
36701 structured-block */
36703 #define OACC_LOOP_CLAUSE_MASK \
36704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36716 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36717 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36719 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36721 strcat (p_name
, " loop");
36722 mask
|= OACC_LOOP_CLAUSE_MASK
;
36724 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36728 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36730 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36732 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36735 tree block
= begin_omp_structured_block ();
36736 int save
= cp_parser_begin_omp_structured_block (parser
);
36737 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36738 cp_parser_end_omp_structured_block (parser
, save
);
36739 add_stmt (finish_omp_structured_block (block
));
36745 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36750 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36754 #define OACC_KERNELS_CLAUSE_MASK \
36755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36773 #define OACC_PARALLEL_CLAUSE_MASK \
36774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36796 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36797 char *p_name
, bool *if_p
)
36799 omp_clause_mask mask
;
36800 enum tree_code code
;
36801 switch (cp_parser_pragma_kind (pragma_tok
))
36803 case PRAGMA_OACC_KERNELS
:
36804 strcat (p_name
, " kernels");
36805 mask
= OACC_KERNELS_CLAUSE_MASK
;
36806 code
= OACC_KERNELS
;
36808 case PRAGMA_OACC_PARALLEL
:
36809 strcat (p_name
, " parallel");
36810 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36811 code
= OACC_PARALLEL
;
36814 gcc_unreachable ();
36817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36820 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36821 if (strcmp (p
, "loop") == 0)
36823 cp_lexer_consume_token (parser
->lexer
);
36824 tree block
= begin_omp_parallel ();
36826 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36828 return finish_omp_construct (code
, block
, clauses
);
36832 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36834 tree block
= begin_omp_parallel ();
36835 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36836 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36837 cp_parser_end_omp_structured_block (parser
, save
);
36838 return finish_omp_construct (code
, block
, clauses
);
36842 # pragma acc update oacc-update-clause[optseq] new-line
36845 #define OACC_UPDATE_CLAUSE_MASK \
36846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36854 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36856 tree stmt
, clauses
;
36858 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36859 "#pragma acc update", pragma_tok
);
36861 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36863 error_at (pragma_tok
->location
,
36864 "%<#pragma acc update%> must contain at least one "
36865 "%<device%> or %<host%> or %<self%> clause");
36869 stmt
= make_node (OACC_UPDATE
);
36870 TREE_TYPE (stmt
) = void_type_node
;
36871 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36872 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36878 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36880 LOC is the location of the #pragma token.
36883 #define OACC_WAIT_CLAUSE_MASK \
36884 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36887 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36889 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36890 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36892 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36893 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36895 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36896 "#pragma acc wait", pragma_tok
);
36898 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36899 stmt
= finish_expr_stmt (stmt
);
36905 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36907 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36916 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36917 enum pragma_context context
)
36919 bool first_p
= parser
->omp_declare_simd
== NULL
;
36920 cp_omp_declare_simd_data data
;
36923 data
.error_seen
= false;
36924 data
.fndecl_seen
= false;
36925 data
.tokens
= vNULL
;
36926 data
.clauses
= NULL_TREE
;
36927 /* It is safe to take the address of a local variable; it will only be
36928 used while this scope is live. */
36929 parser
->omp_declare_simd
= &data
;
36932 /* Store away all pragma tokens. */
36933 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36934 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36935 cp_lexer_consume_token (parser
->lexer
);
36936 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36937 parser
->omp_declare_simd
->error_seen
= true;
36938 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36939 struct cp_token_cache
*cp
36940 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36941 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36945 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36946 cp_parser_pragma (parser
, context
, NULL
);
36949 case pragma_external
:
36950 cp_parser_declaration (parser
);
36952 case pragma_member
:
36953 cp_parser_member_declaration (parser
);
36955 case pragma_objc_icode
:
36956 cp_parser_block_declaration (parser
, /*statement_p=*/false);
36959 cp_parser_declaration_statement (parser
);
36962 if (parser
->omp_declare_simd
36963 && !parser
->omp_declare_simd
->error_seen
36964 && !parser
->omp_declare_simd
->fndecl_seen
)
36965 error_at (pragma_tok
->location
,
36966 "%<#pragma omp declare simd%> not immediately followed by "
36967 "function declaration or definition");
36968 data
.tokens
.release ();
36969 parser
->omp_declare_simd
= NULL
;
36973 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36974 This function is modelled similar to the late parsing of omp declare
36978 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
36980 struct cp_token_cache
*ce
;
36981 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
36984 if (parser
->omp_declare_simd
!= NULL
36985 || lookup_attribute ("simd", attrs
))
36987 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36988 "used in the same function marked as a Cilk Plus SIMD-enabled "
36990 parser
->cilk_simd_fn_info
->tokens
.release ();
36991 XDELETE (parser
->cilk_simd_fn_info
);
36992 parser
->cilk_simd_fn_info
= NULL
;
36995 if (!info
->error_seen
&& info
->fndecl_seen
)
36997 error ("vector attribute not immediately followed by a single function"
36998 " declaration or definition");
36999 info
->error_seen
= true;
37001 if (info
->error_seen
)
37004 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
37008 cp_parser_push_lexer_for_tokens (parser
, ce
);
37009 parser
->lexer
->in_pragma
= true;
37010 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
37011 "SIMD-enabled functions attribute",
37013 cp_parser_pop_lexer (parser
);
37015 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37017 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
37018 TREE_CHAIN (c
) = attrs
;
37021 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37022 TREE_CHAIN (c
) = attrs
;
37023 if (processing_template_decl
)
37024 ATTR_IS_DEPENDENT (c
) = 1;
37027 info
->fndecl_seen
= true;
37028 parser
->cilk_simd_fn_info
->tokens
.release ();
37029 XDELETE (parser
->cilk_simd_fn_info
);
37030 parser
->cilk_simd_fn_info
= NULL
;
37034 /* Finalize #pragma omp declare simd clauses after direct declarator has
37035 been parsed, and put that into "omp declare simd" attribute. */
37038 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37040 struct cp_token_cache
*ce
;
37041 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37044 if (!data
->error_seen
&& data
->fndecl_seen
)
37046 error ("%<#pragma omp declare simd%> not immediately followed by "
37047 "a single function declaration or definition");
37048 data
->error_seen
= true;
37050 if (data
->error_seen
)
37053 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37057 cp_parser_push_lexer_for_tokens (parser
, ce
);
37058 parser
->lexer
->in_pragma
= true;
37059 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37060 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37061 cp_lexer_consume_token (parser
->lexer
);
37062 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37063 "#pragma omp declare simd", pragma_tok
);
37064 cp_parser_pop_lexer (parser
);
37066 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37067 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37068 TREE_CHAIN (c
) = attrs
;
37069 if (processing_template_decl
)
37070 ATTR_IS_DEPENDENT (c
) = 1;
37074 data
->fndecl_seen
= true;
37080 # pragma omp declare target new-line
37081 declarations and definitions
37082 # pragma omp end declare target new-line
37085 # pragma omp declare target ( extended-list ) new-line
37087 # pragma omp declare target declare-target-clauses[seq] new-line */
37089 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37094 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37096 tree clauses
= NULL_TREE
;
37097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37099 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37100 "#pragma omp declare target", pragma_tok
);
37101 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37103 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37105 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37106 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37110 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37111 scope_chain
->omp_declare_target_attribute
++;
37114 if (scope_chain
->omp_declare_target_attribute
)
37115 error_at (pragma_tok
->location
,
37116 "%<#pragma omp declare target%> with clauses in between "
37117 "%<#pragma omp declare target%> without clauses and "
37118 "%<#pragma omp end declare target%>");
37119 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37121 tree t
= OMP_CLAUSE_DECL (c
), id
;
37122 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37123 tree at2
= lookup_attribute ("omp declare target link",
37124 DECL_ATTRIBUTES (t
));
37125 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37127 id
= get_identifier ("omp declare target link");
37128 std::swap (at1
, at2
);
37131 id
= get_identifier ("omp declare target");
37134 error_at (OMP_CLAUSE_LOCATION (c
),
37135 "%qD specified both in declare target %<link%> and %<to%>"
37141 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37142 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37145 symtab_node
*node
= symtab_node::get (t
);
37148 node
->offloadable
= 1;
37149 if (ENABLE_OFFLOADING
)
37151 g
->have_offload
= true;
37152 if (is_a
<varpool_node
*> (node
))
37153 vec_safe_push (offload_vars
, t
);
37161 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37163 const char *p
= "";
37164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37166 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37167 p
= IDENTIFIER_POINTER (id
);
37169 if (strcmp (p
, "declare") == 0)
37171 cp_lexer_consume_token (parser
->lexer
);
37173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37175 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37176 p
= IDENTIFIER_POINTER (id
);
37178 if (strcmp (p
, "target") == 0)
37179 cp_lexer_consume_token (parser
->lexer
);
37182 cp_parser_error (parser
, "expected %<target%>");
37183 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37189 cp_parser_error (parser
, "expected %<declare%>");
37190 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37193 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37194 if (!scope_chain
->omp_declare_target_attribute
)
37195 error_at (pragma_tok
->location
,
37196 "%<#pragma omp end declare target%> without corresponding "
37197 "%<#pragma omp declare target%>");
37199 scope_chain
->omp_declare_target_attribute
--;
37202 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37203 expression and optional initializer clause of
37204 #pragma omp declare reduction. We store the expression(s) as
37205 either 3, 6 or 7 special statements inside of the artificial function's
37206 body. The first two statements are DECL_EXPRs for the artificial
37207 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37208 expression that uses those variables.
37209 If there was any INITIALIZER clause, this is followed by further statements,
37210 the fourth and fifth statements are DECL_EXPRs for the artificial
37211 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37212 constructor variant (first token after open paren is not omp_priv),
37213 then the sixth statement is a statement with the function call expression
37214 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37215 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37216 to initialize the OMP_PRIV artificial variable and there is seventh
37217 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37220 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37222 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37223 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37224 type
= TREE_TYPE (type
);
37225 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37226 DECL_ARTIFICIAL (omp_out
) = 1;
37227 pushdecl (omp_out
);
37228 add_decl_expr (omp_out
);
37229 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37230 DECL_ARTIFICIAL (omp_in
) = 1;
37232 add_decl_expr (omp_in
);
37234 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37236 keep_next_level (true);
37237 tree block
= begin_omp_structured_block ();
37238 combiner
= cp_parser_expression (parser
);
37239 finish_expr_stmt (combiner
);
37240 block
= finish_omp_structured_block (block
);
37243 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37246 const char *p
= "";
37247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37249 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37250 p
= IDENTIFIER_POINTER (id
);
37253 if (strcmp (p
, "initializer") == 0)
37255 cp_lexer_consume_token (parser
->lexer
);
37256 matching_parens parens
;
37257 if (!parens
.require_open (parser
))
37261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37263 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37264 p
= IDENTIFIER_POINTER (id
);
37267 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37268 DECL_ARTIFICIAL (omp_priv
) = 1;
37269 pushdecl (omp_priv
);
37270 add_decl_expr (omp_priv
);
37271 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37272 DECL_ARTIFICIAL (omp_orig
) = 1;
37273 pushdecl (omp_orig
);
37274 add_decl_expr (omp_orig
);
37276 keep_next_level (true);
37277 block
= begin_omp_structured_block ();
37280 if (strcmp (p
, "omp_priv") == 0)
37282 bool is_direct_init
, is_non_constant_init
;
37284 cp_lexer_consume_token (parser
->lexer
);
37285 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37287 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37288 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37290 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37291 == CPP_CLOSE_PAREN
))
37293 finish_omp_structured_block (block
);
37294 error ("invalid initializer clause");
37297 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37298 &is_non_constant_init
);
37299 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37300 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37304 cp_parser_parse_tentatively (parser
);
37305 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37306 /*check_dependency_p=*/true,
37307 /*template_p=*/NULL
,
37308 /*declarator_p=*/false,
37309 /*optional_p=*/false);
37310 vec
<tree
, va_gc
> *args
;
37311 if (fn_name
== error_mark_node
37312 || cp_parser_error_occurred (parser
)
37313 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37314 || ((args
= cp_parser_parenthesized_expression_list
37315 (parser
, non_attr
, /*cast_p=*/false,
37316 /*allow_expansion_p=*/true,
37317 /*non_constant_p=*/NULL
)),
37318 cp_parser_error_occurred (parser
)))
37320 finish_omp_structured_block (block
);
37321 cp_parser_abort_tentative_parse (parser
);
37322 cp_parser_error (parser
, "expected id-expression (arguments)");
37327 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37328 if (arg
== omp_priv
37329 || (TREE_CODE (arg
) == ADDR_EXPR
37330 && TREE_OPERAND (arg
, 0) == omp_priv
))
37332 cp_parser_abort_tentative_parse (parser
);
37333 if (arg
== NULL_TREE
)
37334 error ("one of the initializer call arguments should be %<omp_priv%>"
37335 " or %<&omp_priv%>");
37336 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37338 finish_expr_stmt (initializer
);
37341 block
= finish_omp_structured_block (block
);
37342 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37346 add_decl_expr (omp_orig
);
37348 if (!parens
.require_close (parser
))
37352 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37353 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37360 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37361 initializer-clause[opt] new-line
37363 initializer-clause:
37364 initializer (omp_priv initializer)
37365 initializer (function-name (argument-list)) */
37368 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37369 enum pragma_context
)
37371 auto_vec
<tree
> types
;
37372 enum tree_code reduc_code
= ERROR_MARK
;
37373 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37375 cp_token
*first_token
;
37376 cp_token_cache
*cp
;
37380 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37381 p
= obstack_alloc (&declarator_obstack
, 0);
37383 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37386 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37389 reduc_code
= PLUS_EXPR
;
37392 reduc_code
= MULT_EXPR
;
37395 reduc_code
= MINUS_EXPR
;
37398 reduc_code
= BIT_AND_EXPR
;
37401 reduc_code
= BIT_XOR_EXPR
;
37404 reduc_code
= BIT_IOR_EXPR
;
37407 reduc_code
= TRUTH_ANDIF_EXPR
;
37410 reduc_code
= TRUTH_ORIF_EXPR
;
37413 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37416 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37417 "%<|%>, %<&&%>, %<||%> or identifier");
37421 if (reduc_code
!= ERROR_MARK
)
37422 cp_lexer_consume_token (parser
->lexer
);
37424 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37425 if (reduc_id
== error_mark_node
)
37428 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37431 /* Types may not be defined in declare reduction type list. */
37432 const char *saved_message
;
37433 saved_message
= parser
->type_definition_forbidden_message
;
37434 parser
->type_definition_forbidden_message
37435 = G_("types may not be defined in declare reduction type list");
37436 bool saved_colon_corrects_to_scope_p
;
37437 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37438 parser
->colon_corrects_to_scope_p
= false;
37439 bool saved_colon_doesnt_start_class_def_p
;
37440 saved_colon_doesnt_start_class_def_p
37441 = parser
->colon_doesnt_start_class_def_p
;
37442 parser
->colon_doesnt_start_class_def_p
= true;
37446 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37447 type
= cp_parser_type_id (parser
);
37448 if (type
== error_mark_node
)
37450 else if (ARITHMETIC_TYPE_P (type
)
37451 && (orig_reduc_id
== NULL_TREE
37452 || (TREE_CODE (type
) != COMPLEX_TYPE
37453 && (id_equal (orig_reduc_id
, "min")
37454 || id_equal (orig_reduc_id
, "max")))))
37455 error_at (loc
, "predeclared arithmetic type %qT in "
37456 "%<#pragma omp declare reduction%>", type
);
37457 else if (TREE_CODE (type
) == FUNCTION_TYPE
37458 || TREE_CODE (type
) == METHOD_TYPE
37459 || TREE_CODE (type
) == ARRAY_TYPE
)
37460 error_at (loc
, "function or array type %qT in "
37461 "%<#pragma omp declare reduction%>", type
);
37462 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37463 error_at (loc
, "reference type %qT in "
37464 "%<#pragma omp declare reduction%>", type
);
37465 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37466 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37467 "%<#pragma omp declare reduction%>", type
);
37469 types
.safe_push (type
);
37471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37472 cp_lexer_consume_token (parser
->lexer
);
37477 /* Restore the saved message. */
37478 parser
->type_definition_forbidden_message
= saved_message
;
37479 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37480 parser
->colon_doesnt_start_class_def_p
37481 = saved_colon_doesnt_start_class_def_p
;
37483 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37484 || types
.is_empty ())
37487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37491 first_token
= cp_lexer_peek_token (parser
->lexer
);
37494 FOR_EACH_VEC_ELT (types
, i
, type
)
37497 = build_function_type_list (void_type_node
,
37498 cp_build_reference_type (type
, false),
37500 tree this_reduc_id
= reduc_id
;
37501 if (!dependent_type_p (type
))
37502 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37503 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37504 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37505 DECL_ARTIFICIAL (fndecl
) = 1;
37506 DECL_EXTERNAL (fndecl
) = 1;
37507 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37508 DECL_IGNORED_P (fndecl
) = 1;
37509 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37510 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37511 DECL_ATTRIBUTES (fndecl
)
37512 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37513 DECL_ATTRIBUTES (fndecl
));
37514 if (processing_template_decl
)
37515 fndecl
= push_template_decl (fndecl
);
37516 bool block_scope
= false;
37517 tree block
= NULL_TREE
;
37518 if (current_function_decl
)
37520 block_scope
= true;
37521 DECL_CONTEXT (fndecl
) = global_namespace
;
37522 if (!processing_template_decl
)
37525 else if (current_class_type
)
37529 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37530 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37531 cp_lexer_consume_token (parser
->lexer
);
37532 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37534 cp
= cp_token_cache_new (first_token
,
37535 cp_lexer_peek_nth_token (parser
->lexer
,
37538 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37539 finish_member_declaration (fndecl
);
37540 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37541 DECL_PENDING_INLINE_P (fndecl
) = 1;
37542 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37547 DECL_CONTEXT (fndecl
) = current_namespace
;
37551 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37553 block
= begin_omp_structured_block ();
37556 cp_parser_push_lexer_for_tokens (parser
, cp
);
37557 parser
->lexer
->in_pragma
= true;
37559 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37562 finish_function (0);
37564 DECL_CONTEXT (fndecl
) = current_function_decl
;
37566 cp_parser_pop_lexer (parser
);
37570 cp_parser_pop_lexer (parser
);
37572 finish_function (0);
37575 DECL_CONTEXT (fndecl
) = current_function_decl
;
37576 block
= finish_omp_structured_block (block
);
37577 if (TREE_CODE (block
) == BIND_EXPR
)
37578 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37579 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37580 DECL_SAVED_TREE (fndecl
) = block
;
37581 if (processing_template_decl
)
37582 add_decl_expr (fndecl
);
37584 cp_check_omp_declare_reduction (fndecl
);
37585 if (cp
== NULL
&& types
.length () > 1)
37586 cp
= cp_token_cache_new (first_token
,
37587 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37588 if (errs
!= errorcount
)
37592 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37595 /* Free any declarators allocated. */
37596 obstack_free (&declarator_obstack
, p
);
37600 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37601 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37602 initializer-clause[opt] new-line
37603 #pragma omp declare target new-line */
37606 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37607 enum pragma_context context
)
37609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37611 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37612 const char *p
= IDENTIFIER_POINTER (id
);
37614 if (strcmp (p
, "simd") == 0)
37616 cp_lexer_consume_token (parser
->lexer
);
37617 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37621 cp_ensure_no_omp_declare_simd (parser
);
37622 if (strcmp (p
, "reduction") == 0)
37624 cp_lexer_consume_token (parser
->lexer
);
37625 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37629 if (!flag_openmp
) /* flag_openmp_simd */
37631 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37634 if (strcmp (p
, "target") == 0)
37636 cp_lexer_consume_token (parser
->lexer
);
37637 cp_parser_omp_declare_target (parser
, pragma_tok
);
37641 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37643 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37647 #pragma omp taskloop taskloop-clause[optseq] new-line
37650 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37653 #define OMP_TASKLOOP_CLAUSE_MASK \
37654 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37670 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37671 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37674 tree clauses
, sb
, ret
;
37676 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37678 strcat (p_name
, " taskloop");
37679 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37683 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37684 const char *p
= IDENTIFIER_POINTER (id
);
37686 if (strcmp (p
, "simd") == 0)
37688 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37689 if (cclauses
== NULL
)
37690 cclauses
= cclauses_buf
;
37692 cp_lexer_consume_token (parser
->lexer
);
37693 if (!flag_openmp
) /* flag_openmp_simd */
37694 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37696 sb
= begin_omp_structured_block ();
37697 save
= cp_parser_begin_omp_structured_block (parser
);
37698 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37700 cp_parser_end_omp_structured_block (parser
, save
);
37701 tree body
= finish_omp_structured_block (sb
);
37704 ret
= make_node (OMP_TASKLOOP
);
37705 TREE_TYPE (ret
) = void_type_node
;
37706 OMP_FOR_BODY (ret
) = body
;
37707 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37708 SET_EXPR_LOCATION (ret
, loc
);
37713 if (!flag_openmp
) /* flag_openmp_simd */
37715 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37719 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37723 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37724 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37727 sb
= begin_omp_structured_block ();
37728 save
= cp_parser_begin_omp_structured_block (parser
);
37730 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37733 cp_parser_end_omp_structured_block (parser
, save
);
37734 add_stmt (finish_omp_structured_block (sb
));
37741 # pragma acc routine oacc-routine-clause[optseq] new-line
37742 function-definition
37744 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37747 #define OACC_ROUTINE_CLAUSE_MASK \
37748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37754 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37755 component, which must resolve to a declared namespace-scope
37756 function. The clauses are either processed directly (for a named
37757 function), or defered until the immediatley following declaration
37761 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37762 enum pragma_context context
)
37764 gcc_checking_assert (context
== pragma_external
);
37765 /* The checking for "another pragma following this one" in the "no optional
37766 '( name )'" case makes sure that we dont re-enter. */
37767 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37769 cp_oacc_routine_data data
;
37770 data
.error_seen
= false;
37771 data
.fndecl_seen
= false;
37772 data
.tokens
= vNULL
;
37773 data
.clauses
= NULL_TREE
;
37774 data
.loc
= pragma_tok
->location
;
37775 /* It is safe to take the address of a local variable; it will only be
37776 used while this scope is live. */
37777 parser
->oacc_routine
= &data
;
37779 /* Look for optional '( name )'. */
37780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37782 matching_parens parens
;
37783 parens
.consume_open (parser
); /* '(' */
37785 /* We parse the name as an id-expression. If it resolves to
37786 anything other than a non-overloaded function at namespace
37787 scope, it's an error. */
37788 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37789 tree name
= cp_parser_id_expression (parser
,
37790 /*template_keyword_p=*/false,
37791 /*check_dependency_p=*/false,
37792 /*template_p=*/NULL
,
37793 /*declarator_p=*/false,
37794 /*optional_p=*/false);
37795 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37796 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37797 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37799 if (decl
== error_mark_node
37800 || !parens
.require_close (parser
))
37802 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37803 parser
->oacc_routine
= NULL
;
37808 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37809 "#pragma acc routine",
37810 cp_lexer_peek_token (parser
->lexer
));
37812 if (decl
&& is_overloaded_fn (decl
)
37813 && (TREE_CODE (decl
) != FUNCTION_DECL
37814 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37816 error_at (name_loc
,
37817 "%<#pragma acc routine%> names a set of overloads");
37818 parser
->oacc_routine
= NULL
;
37822 /* Perhaps we should use the same rule as declarations in different
37824 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37826 error_at (name_loc
,
37827 "%qD does not refer to a namespace scope function", decl
);
37828 parser
->oacc_routine
= NULL
;
37832 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37834 error_at (name_loc
, "%qD does not refer to a function", decl
);
37835 parser
->oacc_routine
= NULL
;
37839 cp_finalize_oacc_routine (parser
, decl
, false);
37840 parser
->oacc_routine
= NULL
;
37842 else /* No optional '( name )'. */
37844 /* Store away all pragma tokens. */
37845 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37846 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37847 cp_lexer_consume_token (parser
->lexer
);
37848 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37849 parser
->oacc_routine
->error_seen
= true;
37850 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37851 struct cp_token_cache
*cp
37852 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37853 parser
->oacc_routine
->tokens
.safe_push (cp
);
37855 /* Emit a helpful diagnostic if there's another pragma following this
37857 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37859 cp_ensure_no_oacc_routine (parser
);
37860 data
.tokens
.release ();
37861 /* ..., and then just keep going. */
37865 /* We only have to consider the pragma_external case here. */
37866 cp_parser_declaration (parser
);
37867 if (parser
->oacc_routine
37868 && !parser
->oacc_routine
->fndecl_seen
)
37869 cp_ensure_no_oacc_routine (parser
);
37871 parser
->oacc_routine
= NULL
;
37872 data
.tokens
.release ();
37876 /* Finalize #pragma acc routine clauses after direct declarator has
37880 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37882 struct cp_token_cache
*ce
;
37883 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37885 if (!data
->error_seen
&& data
->fndecl_seen
)
37887 error_at (data
->loc
,
37888 "%<#pragma acc routine%> not immediately followed by "
37889 "a single function declaration or definition");
37890 data
->error_seen
= true;
37892 if (data
->error_seen
)
37895 gcc_checking_assert (data
->tokens
.length () == 1);
37896 ce
= data
->tokens
[0];
37898 cp_parser_push_lexer_for_tokens (parser
, ce
);
37899 parser
->lexer
->in_pragma
= true;
37900 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37902 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37903 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37904 parser
->oacc_routine
->clauses
37905 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37906 "#pragma acc routine", pragma_tok
);
37907 cp_parser_pop_lexer (parser
);
37908 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37914 /* Apply any saved OpenACC routine clauses to a just-parsed
37918 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37920 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37922 /* Keep going if we're in error reporting mode. */
37923 if (parser
->oacc_routine
->error_seen
37924 || fndecl
== error_mark_node
)
37927 if (parser
->oacc_routine
->fndecl_seen
)
37929 error_at (parser
->oacc_routine
->loc
,
37930 "%<#pragma acc routine%> not immediately followed by"
37931 " a single function declaration or definition");
37932 parser
->oacc_routine
= NULL
;
37935 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37937 cp_ensure_no_oacc_routine (parser
);
37941 if (oacc_get_fn_attrib (fndecl
))
37943 error_at (parser
->oacc_routine
->loc
,
37944 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37945 parser
->oacc_routine
= NULL
;
37949 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37951 error_at (parser
->oacc_routine
->loc
,
37953 ? G_("%<#pragma acc routine%> must be applied before use")
37954 : G_("%<#pragma acc routine%> must be applied before "
37956 parser
->oacc_routine
= NULL
;
37960 /* Process the routine's dimension clauses. */
37961 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37962 oacc_replace_fn_attrib (fndecl
, dims
);
37964 /* Add an "omp declare target" attribute. */
37965 DECL_ATTRIBUTES (fndecl
)
37966 = tree_cons (get_identifier ("omp declare target"),
37967 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37969 /* Don't unset parser->oacc_routine here: we may still need it to
37970 diagnose wrong usage. But, remember that we've used this "#pragma acc
37972 parser
->oacc_routine
->fndecl_seen
= true;
37976 /* Main entry point to OpenMP statement pragmas. */
37979 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37982 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37983 omp_clause_mask
mask (0);
37985 switch (cp_parser_pragma_kind (pragma_tok
))
37987 case PRAGMA_OACC_ATOMIC
:
37988 cp_parser_omp_atomic (parser
, pragma_tok
);
37990 case PRAGMA_OACC_CACHE
:
37991 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37993 case PRAGMA_OACC_DATA
:
37994 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37996 case PRAGMA_OACC_ENTER_DATA
:
37997 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37999 case PRAGMA_OACC_EXIT_DATA
:
38000 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38002 case PRAGMA_OACC_HOST_DATA
:
38003 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38005 case PRAGMA_OACC_KERNELS
:
38006 case PRAGMA_OACC_PARALLEL
:
38007 strcpy (p_name
, "#pragma acc");
38008 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38011 case PRAGMA_OACC_LOOP
:
38012 strcpy (p_name
, "#pragma acc");
38013 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38016 case PRAGMA_OACC_UPDATE
:
38017 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38019 case PRAGMA_OACC_WAIT
:
38020 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38022 case PRAGMA_OMP_ATOMIC
:
38023 cp_parser_omp_atomic (parser
, pragma_tok
);
38025 case PRAGMA_OMP_CRITICAL
:
38026 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38028 case PRAGMA_OMP_DISTRIBUTE
:
38029 strcpy (p_name
, "#pragma omp");
38030 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38033 case PRAGMA_OMP_FOR
:
38034 strcpy (p_name
, "#pragma omp");
38035 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38038 case PRAGMA_OMP_MASTER
:
38039 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38041 case PRAGMA_OMP_PARALLEL
:
38042 strcpy (p_name
, "#pragma omp");
38043 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38046 case PRAGMA_OMP_SECTIONS
:
38047 strcpy (p_name
, "#pragma omp");
38048 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38050 case PRAGMA_OMP_SIMD
:
38051 strcpy (p_name
, "#pragma omp");
38052 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38055 case PRAGMA_OMP_SINGLE
:
38056 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38058 case PRAGMA_OMP_TASK
:
38059 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38061 case PRAGMA_OMP_TASKGROUP
:
38062 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38064 case PRAGMA_OMP_TASKLOOP
:
38065 strcpy (p_name
, "#pragma omp");
38066 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38069 case PRAGMA_OMP_TEAMS
:
38070 strcpy (p_name
, "#pragma omp");
38071 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38075 gcc_unreachable ();
38078 protected_set_expr_location (stmt
, pragma_tok
->location
);
38081 /* Transactional Memory parsing routines. */
38083 /* Parse a transaction attribute.
38089 We use this instead of cp_parser_attributes_opt for transactions to avoid
38090 the pedwarn in C++98 mode. */
38093 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38096 tree attr_name
, attr
= NULL
;
38098 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38099 return cp_parser_attributes_opt (parser
);
38101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38103 cp_lexer_consume_token (parser
->lexer
);
38104 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38107 token
= cp_lexer_peek_token (parser
->lexer
);
38108 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38110 token
= cp_lexer_consume_token (parser
->lexer
);
38112 attr_name
= (token
->type
== CPP_KEYWORD
38113 /* For keywords, use the canonical spelling,
38114 not the parsed identifier. */
38115 ? ridpointers
[(int) token
->keyword
]
38117 attr
= build_tree_list (attr_name
, NULL_TREE
);
38120 cp_parser_error (parser
, "expected identifier");
38122 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38124 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38128 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38130 transaction-statement:
38131 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38133 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38137 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38139 unsigned char old_in
= parser
->in_transaction
;
38140 unsigned char this_in
= 1, new_in
;
38141 enum rid keyword
= token
->keyword
;
38142 tree stmt
, attrs
, noex
;
38144 cp_lexer_consume_token (parser
->lexer
);
38146 if (keyword
== RID_TRANSACTION_RELAXED
38147 || keyword
== RID_SYNCHRONIZED
)
38148 this_in
|= TM_STMT_ATTR_RELAXED
;
38151 attrs
= cp_parser_txn_attribute_opt (parser
);
38153 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38156 /* Parse a noexcept specification. */
38157 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38158 noex
= boolean_true_node
;
38159 else if (keyword
== RID_ATOMIC_CANCEL
)
38161 /* cancel-and-throw is unimplemented. */
38162 sorry ("atomic_cancel");
38166 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38168 /* Keep track if we're in the lexical scope of an outer transaction. */
38169 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38171 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38173 parser
->in_transaction
= new_in
;
38174 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38175 parser
->in_transaction
= old_in
;
38177 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38182 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38184 transaction-expression:
38185 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38186 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38190 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38192 unsigned char old_in
= parser
->in_transaction
;
38193 unsigned char this_in
= 1;
38197 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38199 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38200 || keyword
== RID_TRANSACTION_RELAXED
);
38204 keyword
== RID_TRANSACTION_RELAXED
38205 ? G_("%<__transaction_relaxed%> without transactional memory "
38207 : G_("%<__transaction_atomic%> without transactional memory "
38208 "support enabled"));
38210 token
= cp_parser_require_keyword (parser
, keyword
,
38211 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38212 : RT_TRANSACTION_RELAXED
));
38213 gcc_assert (token
!= NULL
);
38215 if (keyword
== RID_TRANSACTION_RELAXED
)
38216 this_in
|= TM_STMT_ATTR_RELAXED
;
38218 /* Set this early. This might mean that we allow transaction_cancel in
38219 an expression that we find out later actually has to be a constexpr.
38220 However, we expect that cxx_constant_value will be able to deal with
38221 this; also, if the noexcept has no constexpr, then what we parse next
38222 really is a transaction's body. */
38223 parser
->in_transaction
= this_in
;
38225 /* Parse a noexcept specification. */
38226 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38229 if (!noex
|| !noex_expr
38230 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38232 matching_parens parens
;
38233 parens
.require_open (parser
);
38235 expr
= cp_parser_expression (parser
);
38236 expr
= finish_parenthesized_expr (expr
);
38238 parens
.require_close (parser
);
38242 /* The only expression that is available got parsed for the noexcept
38243 already. noexcept is true then. */
38245 noex
= boolean_true_node
;
38248 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38249 parser
->in_transaction
= old_in
;
38251 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38252 return error_mark_node
;
38254 return (flag_tm
? expr
: error_mark_node
);
38257 /* Parse a function-transaction-block.
38259 function-transaction-block:
38260 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38262 __transaction_atomic txn-attribute[opt] function-try-block
38263 __transaction_relaxed ctor-initializer[opt] function-body
38264 __transaction_relaxed function-try-block
38268 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38270 unsigned char old_in
= parser
->in_transaction
;
38271 unsigned char new_in
= 1;
38272 tree compound_stmt
, stmt
, attrs
;
38273 bool ctor_initializer_p
;
38276 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38277 || keyword
== RID_TRANSACTION_RELAXED
);
38278 token
= cp_parser_require_keyword (parser
, keyword
,
38279 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38280 : RT_TRANSACTION_RELAXED
));
38281 gcc_assert (token
!= NULL
);
38283 if (keyword
== RID_TRANSACTION_RELAXED
)
38284 new_in
|= TM_STMT_ATTR_RELAXED
;
38287 attrs
= cp_parser_txn_attribute_opt (parser
);
38289 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38292 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38294 parser
->in_transaction
= new_in
;
38296 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38297 ctor_initializer_p
= cp_parser_function_try_block (parser
);
38299 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
38300 (parser
, /*in_function_try_block=*/false);
38302 parser
->in_transaction
= old_in
;
38304 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38306 return ctor_initializer_p
;
38309 /* Parse a __transaction_cancel statement.
38312 __transaction_cancel txn-attribute[opt] ;
38313 __transaction_cancel txn-attribute[opt] throw-expression ;
38315 ??? Cancel and throw is not yet implemented. */
38318 cp_parser_transaction_cancel (cp_parser
*parser
)
38321 bool is_outer
= false;
38324 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38325 RT_TRANSACTION_CANCEL
);
38326 gcc_assert (token
!= NULL
);
38328 attrs
= cp_parser_txn_attribute_opt (parser
);
38330 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38332 /* ??? Parse cancel-and-throw here. */
38334 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38338 error_at (token
->location
, "%<__transaction_cancel%> without "
38339 "transactional memory support enabled");
38340 return error_mark_node
;
38342 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38344 error_at (token
->location
, "%<__transaction_cancel%> within a "
38345 "%<__transaction_relaxed%>");
38346 return error_mark_node
;
38350 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38351 && !is_tm_may_cancel_outer (current_function_decl
))
38353 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38354 "within outer %<__transaction_atomic%>");
38355 error_at (token
->location
,
38356 " or a %<transaction_may_cancel_outer%> function");
38357 return error_mark_node
;
38360 else if (parser
->in_transaction
== 0)
38362 error_at (token
->location
, "%<__transaction_cancel%> not within "
38363 "%<__transaction_atomic%>");
38364 return error_mark_node
;
38367 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38375 static GTY (()) cp_parser
*the_parser
;
38378 /* Special handling for the first token or line in the file. The first
38379 thing in the file might be #pragma GCC pch_preprocess, which loads a
38380 PCH file, which is a GC collection point. So we need to handle this
38381 first pragma without benefit of an existing lexer structure.
38383 Always returns one token to the caller in *FIRST_TOKEN. This is
38384 either the true first token of the file, or the first token after
38385 the initial pragma. */
38388 cp_parser_initial_pragma (cp_token
*first_token
)
38392 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38393 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38396 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38397 if (first_token
->type
== CPP_STRING
)
38399 name
= first_token
->u
.value
;
38401 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38402 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38403 error_at (first_token
->location
,
38404 "junk at end of %<#pragma GCC pch_preprocess%>");
38407 error_at (first_token
->location
, "expected string literal");
38409 /* Skip to the end of the pragma. */
38410 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38411 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38413 /* Now actually load the PCH file. */
38415 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38417 /* Read one more token to return to our caller. We have to do this
38418 after reading the PCH file in, since its pointers have to be
38420 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38423 /* Parses the grainsize pragma for the _Cilk_for statement.
38425 #pragma cilk grainsize = <VALUE>. */
38428 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38430 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38432 tree exp
= cp_parser_binary_expression (parser
, false, false,
38433 PREC_NOT_OPERATOR
, NULL
);
38434 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38435 if (!exp
|| exp
== error_mark_node
)
38437 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
38441 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38442 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
38443 cp_parser_cilk_for (parser
, exp
, if_p
);
38445 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
38446 "%<#pragma cilk grainsize%> is not followed by "
38450 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38453 /* Normal parsing of a pragma token. Here we can (and must) use the
38457 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38459 cp_token
*pragma_tok
;
38464 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38465 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38466 parser
->lexer
->in_pragma
= true;
38468 id
= cp_parser_pragma_kind (pragma_tok
);
38469 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38470 cp_ensure_no_omp_declare_simd (parser
);
38473 case PRAGMA_GCC_PCH_PREPROCESS
:
38474 error_at (pragma_tok
->location
,
38475 "%<#pragma GCC pch_preprocess%> must be first");
38478 case PRAGMA_OMP_BARRIER
:
38481 case pragma_compound
:
38482 cp_parser_omp_barrier (parser
, pragma_tok
);
38485 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38486 "used in compound statements", "omp barrier");
38493 case PRAGMA_OMP_FLUSH
:
38496 case pragma_compound
:
38497 cp_parser_omp_flush (parser
, pragma_tok
);
38500 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38501 "used in compound statements", "omp flush");
38508 case PRAGMA_OMP_TASKWAIT
:
38511 case pragma_compound
:
38512 cp_parser_omp_taskwait (parser
, pragma_tok
);
38515 error_at (pragma_tok
->location
,
38516 "%<#pragma %s%> may only be used in compound statements",
38524 case PRAGMA_OMP_TASKYIELD
:
38527 case pragma_compound
:
38528 cp_parser_omp_taskyield (parser
, pragma_tok
);
38531 error_at (pragma_tok
->location
,
38532 "%<#pragma %s%> may only be used in compound statements",
38540 case PRAGMA_OMP_CANCEL
:
38543 case pragma_compound
:
38544 cp_parser_omp_cancel (parser
, pragma_tok
);
38547 error_at (pragma_tok
->location
,
38548 "%<#pragma %s%> may only be used in compound statements",
38556 case PRAGMA_OMP_CANCELLATION_POINT
:
38557 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38560 case PRAGMA_OMP_THREADPRIVATE
:
38561 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38564 case PRAGMA_OMP_DECLARE
:
38565 cp_parser_omp_declare (parser
, pragma_tok
, context
);
38568 case PRAGMA_OACC_DECLARE
:
38569 cp_parser_oacc_declare (parser
, pragma_tok
);
38572 case PRAGMA_OACC_ENTER_DATA
:
38573 if (context
== pragma_stmt
)
38575 error_at (pragma_tok
->location
,
38576 "%<#pragma %s%> may only be used in compound statements",
38580 else if (context
!= pragma_compound
)
38582 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38585 case PRAGMA_OACC_EXIT_DATA
:
38586 if (context
== pragma_stmt
)
38588 error_at (pragma_tok
->location
,
38589 "%<#pragma %s%> may only be used in compound statements",
38593 else if (context
!= pragma_compound
)
38595 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38598 case PRAGMA_OACC_ROUTINE
:
38599 if (context
!= pragma_external
)
38601 error_at (pragma_tok
->location
,
38602 "%<#pragma acc routine%> must be at file scope");
38605 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38608 case PRAGMA_OACC_UPDATE
:
38609 if (context
== pragma_stmt
)
38611 error_at (pragma_tok
->location
,
38612 "%<#pragma %s%> may only be used in compound statements",
38616 else if (context
!= pragma_compound
)
38618 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38621 case PRAGMA_OACC_WAIT
:
38622 if (context
== pragma_stmt
)
38624 error_at (pragma_tok
->location
,
38625 "%<#pragma %s%> may only be used in compound statements",
38629 else if (context
!= pragma_compound
)
38631 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38634 case PRAGMA_OACC_ATOMIC
:
38635 case PRAGMA_OACC_CACHE
:
38636 case PRAGMA_OACC_DATA
:
38637 case PRAGMA_OACC_HOST_DATA
:
38638 case PRAGMA_OACC_KERNELS
:
38639 case PRAGMA_OACC_PARALLEL
:
38640 case PRAGMA_OACC_LOOP
:
38641 case PRAGMA_OMP_ATOMIC
:
38642 case PRAGMA_OMP_CRITICAL
:
38643 case PRAGMA_OMP_DISTRIBUTE
:
38644 case PRAGMA_OMP_FOR
:
38645 case PRAGMA_OMP_MASTER
:
38646 case PRAGMA_OMP_PARALLEL
:
38647 case PRAGMA_OMP_SECTIONS
:
38648 case PRAGMA_OMP_SIMD
:
38649 case PRAGMA_OMP_SINGLE
:
38650 case PRAGMA_OMP_TASK
:
38651 case PRAGMA_OMP_TASKGROUP
:
38652 case PRAGMA_OMP_TASKLOOP
:
38653 case PRAGMA_OMP_TEAMS
:
38654 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38656 stmt
= push_omp_privatization_clauses (false);
38657 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38658 pop_omp_privatization_clauses (stmt
);
38661 case PRAGMA_OMP_ORDERED
:
38662 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38664 stmt
= push_omp_privatization_clauses (false);
38665 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38666 pop_omp_privatization_clauses (stmt
);
38669 case PRAGMA_OMP_TARGET
:
38670 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38672 stmt
= push_omp_privatization_clauses (false);
38673 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38674 pop_omp_privatization_clauses (stmt
);
38677 case PRAGMA_OMP_END_DECLARE_TARGET
:
38678 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38681 case PRAGMA_OMP_SECTION
:
38682 error_at (pragma_tok
->location
,
38683 "%<#pragma omp section%> may only be used in "
38684 "%<#pragma omp sections%> construct");
38689 if (context
== pragma_external
)
38691 error_at (pragma_tok
->location
,
38692 "%<#pragma GCC ivdep%> must be inside a function");
38695 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38697 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38698 if (tok
->type
!= CPP_KEYWORD
38699 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38700 && tok
->keyword
!= RID_DO
))
38702 cp_parser_error (parser
, "for, while or do statement expected");
38705 cp_parser_iteration_statement (parser
, if_p
, true);
38709 case PRAGMA_CILK_SIMD
:
38710 if (context
== pragma_external
)
38712 error_at (pragma_tok
->location
,
38713 "%<#pragma simd%> must be inside a function");
38716 stmt
= push_omp_privatization_clauses (false);
38717 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
38718 pop_omp_privatization_clauses (stmt
);
38721 case PRAGMA_CILK_GRAINSIZE
:
38722 if (context
== pragma_external
)
38724 error_at (pragma_tok
->location
,
38725 "%<#pragma cilk grainsize%> must be inside a function");
38729 /* Ignore the pragma if Cilk Plus is not enabled. */
38732 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
38737 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
38738 "%<#pragma cilk grainsize%>");
38743 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38744 c_invoke_pragma_handler (id
);
38748 cp_parser_error (parser
, "expected declaration specifiers");
38752 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38756 /* The interface the pragma parsers have to the lexer. */
38759 pragma_lex (tree
*value
, location_t
*loc
)
38761 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38762 enum cpp_ttype ret
= tok
->type
;
38764 *value
= tok
->u
.value
;
38766 *loc
= tok
->location
;
38768 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38770 else if (ret
== CPP_STRING
)
38771 *value
= cp_parser_string_literal (the_parser
, false, false);
38774 if (ret
== CPP_KEYWORD
)
38776 cp_lexer_consume_token (the_parser
->lexer
);
38783 /* External interface. */
38785 /* Parse one entire translation unit. */
38788 c_parse_file (void)
38790 static bool already_called
= false;
38792 if (already_called
)
38793 fatal_error (input_location
,
38794 "inter-module optimizations not implemented for C++");
38795 already_called
= true;
38797 the_parser
= cp_parser_new ();
38798 push_deferring_access_checks (flag_access_control
38799 ? dk_no_deferred
: dk_no_check
);
38800 cp_parser_translation_unit (the_parser
);
38804 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38805 vectorlength clause:
38807 vectorlength ( constant-expression ) */
38810 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
38813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38815 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38816 safelen clause. Thus, vectorlength is represented as OMP 4.0
38817 safelen. For SIMD-enabled function it is represented by OMP 4.0
38820 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
38823 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
38826 matching_parens parens
;
38827 if (!parens
.require_open (parser
))
38828 return error_mark_node
;
38830 expr
= cp_parser_constant_expression (parser
);
38831 expr
= maybe_constant_value (expr
);
38833 /* If expr == error_mark_node, then don't emit any errors nor
38834 create a clause. if any of the above functions returns
38835 error mark node then they would have emitted an error message. */
38836 if (expr
== error_mark_node
)
38838 else if (!TREE_TYPE (expr
)
38839 || !TREE_CONSTANT (expr
)
38840 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
38841 error_at (loc
, "vectorlength must be an integer constant");
38842 else if (TREE_CONSTANT (expr
)
38843 && !pow2p_hwi (TREE_INT_CST_LOW (expr
)))
38844 error_at (loc
, "vectorlength must be a power of 2");
38850 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
38851 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
38852 OMP_CLAUSE_CHAIN (c
) = clauses
;
38857 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
38858 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
38859 OMP_CLAUSE_CHAIN (c
) = clauses
;
38864 if (!parens
.require_close (parser
))
38865 return error_mark_node
;
38869 /* Handles the Cilk Plus #pragma simd linear clause.
38871 linear ( simd-linear-variable-list )
38873 simd-linear-variable-list:
38874 simd-linear-variable
38875 simd-linear-variable-list , simd-linear-variable
38877 simd-linear-variable:
38879 id-expression : simd-linear-step
38882 conditional-expression */
38885 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
38887 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38889 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38891 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38893 cp_parser_error (parser
, "expected identifier");
38894 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38895 return error_mark_node
;
38898 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38899 parser
->colon_corrects_to_scope_p
= false;
38902 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38903 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38905 cp_parser_error (parser
, "expected variable-name");
38906 clauses
= error_mark_node
;
38910 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
38912 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
38914 if (decl
== error_mark_node
)
38916 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
38918 clauses
= error_mark_node
;
38922 tree e
= NULL_TREE
;
38923 tree step_size
= integer_one_node
;
38925 /* If present, parse the linear step. Otherwise, assume the default
38927 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
38929 cp_lexer_consume_token (parser
->lexer
);
38931 e
= cp_parser_assignment_expression (parser
);
38932 e
= maybe_constant_value (e
);
38934 if (e
== error_mark_node
)
38936 /* If an error has occurred, then the whole pragma is
38937 considered ill-formed. Thus, no reason to keep
38939 clauses
= error_mark_node
;
38942 else if (type_dependent_expression_p (e
)
38943 || value_dependent_expression_p (e
)
38945 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
38946 && (TREE_CONSTANT (e
)
38950 cp_parser_error (parser
,
38951 "step size must be an integer constant "
38952 "expression or an integer variable");
38955 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38956 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
38957 OMP_CLAUSE_DECL (l
) = decl
;
38958 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
38959 OMP_CLAUSE_CHAIN (l
) = clauses
;
38962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38963 cp_lexer_consume_token (parser
->lexer
);
38964 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38968 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38969 "expected %<,%> or %<)%> after %qE", decl
);
38970 clauses
= error_mark_node
;
38974 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38975 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38979 /* Returns the name of the next clause. If the clause is not
38980 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38981 token is not consumed. Otherwise, the appropriate enum from the
38982 pragma_simd_clause is returned and the token is consumed. */
38984 static pragma_omp_clause
38985 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
38987 pragma_omp_clause clause_type
;
38988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38990 if (token
->keyword
== RID_PRIVATE
)
38991 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
38992 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
38993 return PRAGMA_CILK_CLAUSE_NONE
;
38994 else if (id_equal (token
->u
.value
, "vectorlength"))
38995 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
38996 else if (id_equal (token
->u
.value
, "linear"))
38997 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
38998 else if (id_equal (token
->u
.value
, "firstprivate"))
38999 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
39000 else if (id_equal (token
->u
.value
, "lastprivate"))
39001 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
39002 else if (id_equal (token
->u
.value
, "reduction"))
39003 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
39005 return PRAGMA_CILK_CLAUSE_NONE
;
39007 cp_lexer_consume_token (parser
->lexer
);
39008 return clause_type
;
39011 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39014 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
39016 tree clauses
= NULL_TREE
;
39018 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39019 && clauses
!= error_mark_node
)
39021 pragma_omp_clause c_kind
;
39022 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
39023 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
39024 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
39025 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
39026 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
39027 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
39028 /* Use the OpenMP 4.0 equivalent function. */
39029 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
39030 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
39031 /* Use the OpenMP 4.0 equivalent function. */
39032 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
39034 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
39035 /* Use the OMP 4.0 equivalent function. */
39036 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
39038 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
39039 /* Use the OMP 4.0 equivalent function. */
39040 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
39043 clauses
= error_mark_node
;
39044 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
39049 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
39051 if (clauses
== error_mark_node
)
39052 return error_mark_node
;
39054 return finish_omp_clauses (clauses
, C_ORT_CILK
);
39057 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39060 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
39062 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
39064 if (clauses
== error_mark_node
)
39067 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
39069 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39070 "for statement expected");
39074 tree sb
= begin_omp_structured_block ();
39075 int save
= cp_parser_begin_omp_structured_block (parser
);
39076 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
39078 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
39079 cp_parser_end_omp_structured_block (parser
, save
);
39080 add_stmt (finish_omp_structured_block (sb
));
39083 /* Main entry-point for parsing Cilk Plus _Cilk_for
39084 loops. The return value is error_mark_node
39085 when errors happen and CILK_FOR tree on success. */
39088 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
39090 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
39091 gcc_unreachable ();
39093 tree sb
= begin_omp_structured_block ();
39094 int save
= cp_parser_begin_omp_structured_block (parser
);
39096 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
39097 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
39098 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
39099 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
39101 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
39103 cpp_validate_cilk_plus_loop (ret
);
39105 ret
= error_mark_node
;
39107 cp_parser_end_omp_structured_block (parser
, save
);
39108 add_stmt (finish_omp_structured_block (sb
));
39112 /* Create an identifier for a generic parameter type (a synthesized
39113 template parameter implied by `auto' or a concept identifier). */
39115 static GTY(()) int generic_parm_count
;
39117 make_generic_type_name ()
39120 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39121 return get_identifier (buf
);
39124 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39125 (creating a new template parameter list if necessary). Returns the newly
39126 created template type parm. */
39129 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39131 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39133 /* Before committing to modifying any scope, if we're in an
39134 implicit template scope, and we're trying to synthesize a
39135 constrained parameter, try to find a previous parameter with
39136 the same name. This is the same-type rule for abbreviated
39137 function templates.
39139 NOTE: We can generate implicit parameters when tentatively
39140 parsing a nested name specifier, only to reject that parse
39141 later. However, matching the same template-id as part of a
39142 direct-declarator should generate an identical template
39143 parameter, so this rule will merge them. */
39144 if (parser
->implicit_template_scope
&& constr
)
39146 tree t
= parser
->implicit_template_parms
;
39149 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39151 tree d
= TREE_VALUE (t
);
39152 if (TREE_CODE (d
) == PARM_DECL
)
39153 /* Return the TEMPLATE_PARM_INDEX. */
39154 d
= DECL_INITIAL (d
);
39157 t
= TREE_CHAIN (t
);
39161 /* We are either continuing a function template that already contains implicit
39162 template parameters, creating a new fully-implicit function template, or
39163 extending an existing explicit function template with implicit template
39166 cp_binding_level
*const entry_scope
= current_binding_level
;
39168 bool become_template
= false;
39169 cp_binding_level
*parent_scope
= 0;
39171 if (parser
->implicit_template_scope
)
39173 gcc_assert (parser
->implicit_template_parms
);
39175 current_binding_level
= parser
->implicit_template_scope
;
39179 /* Roll back to the existing template parameter scope (in the case of
39180 extending an explicit function template) or introduce a new template
39181 parameter scope ahead of the function parameter scope (or class scope
39182 in the case of out-of-line member definitions). The function scope is
39183 added back after template parameter synthesis below. */
39185 cp_binding_level
*scope
= entry_scope
;
39187 while (scope
->kind
== sk_function_parms
)
39189 parent_scope
= scope
;
39190 scope
= scope
->level_chain
;
39192 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39194 /* If not defining a class, then any class scope is a scope level in
39195 an out-of-line member definition. In this case simply wind back
39196 beyond the first such scope to inject the template parameter list.
39197 Otherwise wind back to the class being defined. The latter can
39198 occur in class member friend declarations such as:
39204 friend void A::foo (auto);
39207 The template parameter list synthesized for the friend declaration
39208 must be injected in the scope of 'B'. This can also occur in
39209 erroneous cases such as:
39215 void B::foo (auto) {}
39218 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39219 but, nevertheless, the template parameter list synthesized for the
39220 declarator should be injected into the scope of 'A' as if the
39221 ill-formed template was specified explicitly. */
39223 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39225 parent_scope
= scope
;
39226 scope
= scope
->level_chain
;
39230 current_binding_level
= scope
;
39232 if (scope
->kind
!= sk_template_parms
39233 || !function_being_declared_is_template_p (parser
))
39235 /* Introduce a new template parameter list for implicit template
39238 become_template
= true;
39240 parser
->implicit_template_scope
39241 = begin_scope (sk_template_parms
, NULL
);
39243 ++processing_template_decl
;
39245 parser
->fully_implicit_function_template_p
= true;
39246 ++parser
->num_template_parameter_lists
;
39250 /* Synthesize implicit template parameters at the end of the explicit
39251 template parameter list. */
39253 gcc_assert (current_template_parms
);
39255 parser
->implicit_template_scope
= scope
;
39257 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39258 parser
->implicit_template_parms
39259 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39263 /* Synthesize a new template parameter and track the current template
39264 parameter chain with implicit_template_parms. */
39266 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39267 tree synth_id
= make_generic_type_name ();
39268 tree synth_tmpl_parm
;
39269 bool non_type
= false;
39271 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39273 = finish_template_type_parm (class_type_node
, synth_id
);
39274 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39276 = finish_constrained_template_template_parm (proto
, synth_id
);
39279 synth_tmpl_parm
= copy_decl (proto
);
39280 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39284 // Attach the constraint to the parm before processing.
39285 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39286 TREE_TYPE (node
) = constr
;
39288 = process_template_parm (parser
->implicit_template_parms
,
39291 /*non_type=*/non_type
,
39292 /*param_pack=*/false);
39294 // Chain the new parameter to the list of implicit parameters.
39295 if (parser
->implicit_template_parms
)
39296 parser
->implicit_template_parms
39297 = TREE_CHAIN (parser
->implicit_template_parms
);
39299 parser
->implicit_template_parms
= new_parm
;
39301 tree new_decl
= get_local_decls ();
39303 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39304 new_decl
= DECL_INITIAL (new_decl
);
39306 /* If creating a fully implicit function template, start the new implicit
39307 template parameter list with this synthesized type, otherwise grow the
39308 current template parameter list. */
39310 if (become_template
)
39312 parent_scope
->level_chain
= current_binding_level
;
39314 tree new_parms
= make_tree_vec (1);
39315 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39316 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39317 new_parms
, current_template_parms
);
39321 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39322 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39323 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39324 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39327 // If the new parameter was constrained, we need to add that to the
39328 // constraints in the template parameter list.
39329 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39331 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39332 reqs
= conjoin_constraints (reqs
, req
);
39333 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39336 current_binding_level
= entry_scope
;
39341 /* Finish the declaration of a fully implicit function template. Such a
39342 template has no explicit template parameter list so has not been through the
39343 normal template head and tail processing. synthesize_implicit_template_parm
39344 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39345 provided if the declaration is a class member such that its template
39346 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39347 form is returned. Otherwise NULL_TREE is returned. */
39350 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39352 gcc_assert (parser
->fully_implicit_function_template_p
);
39354 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39355 && DECL_VIRTUAL_P (member_decl_opt
))
39357 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39358 "implicit templates may not be %<virtual%>");
39359 DECL_VIRTUAL_P (member_decl_opt
) = false;
39362 if (member_decl_opt
)
39363 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39364 end_template_decl ();
39366 parser
->fully_implicit_function_template_p
= false;
39367 --parser
->num_template_parameter_lists
;
39369 return member_decl_opt
;
39372 #include "gt-cp-parser.h"