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_INTERATION
, /* 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 (C_IS_RESERVED_WORD (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
, 0);
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 aply 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);
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
);
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);
2555 static cp_token
*cp_parser_require
2556 (cp_parser
*, enum cpp_ttype
, required_token
);
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 c_parse_error (gmsgid
,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2811 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2812 token
->u
.value
, token
->flags
);
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2822 cp_parser_name_lookup_error (cp_parser
* parser
,
2825 name_lookup_error desired
,
2826 location_t location
)
2828 /* If name lookup completely failed, tell the user that NAME was not
2830 if (decl
== error_mark_node
)
2832 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2833 error_at (location
, "%<%E::%E%> has not been declared",
2834 parser
->scope
, name
);
2835 else if (parser
->scope
== global_namespace
)
2836 error_at (location
, "%<::%E%> has not been declared", name
);
2837 else if (parser
->object_scope
2838 && !CLASS_TYPE_P (parser
->object_scope
))
2839 error_at (location
, "request for member %qE in non-class type %qT",
2840 name
, parser
->object_scope
);
2841 else if (parser
->object_scope
)
2842 error_at (location
, "%<%T::%E%> has not been declared",
2843 parser
->object_scope
, name
);
2845 error_at (location
, "%qE has not been declared", name
);
2847 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2852 error_at (location
, "%<%E::%E%> is not a type",
2853 parser
->scope
, name
);
2856 error_at (location
, "%<%E::%E%> is not a class or namespace",
2857 parser
->scope
, name
);
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser
->scope
, name
);
2869 else if (parser
->scope
== global_namespace
)
2874 error_at (location
, "%<::%E%> is not a type", name
);
2877 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2881 "%<::%E%> is not a class, namespace, or enumeration",
2893 error_at (location
, "%qE is not a type", name
);
2896 error_at (location
, "%qE is not a class or namespace", name
);
2900 "%qE is not a class, namespace, or enumeration", name
);
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2913 cp_parser_simulate_error (cp_parser
* parser
)
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2917 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2928 cp_parser_check_type_definition (cp_parser
* parser
)
2930 /* If types are forbidden here, issue a message. */
2931 if (parser
->type_definition_forbidden_message
)
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser
->type_definition_forbidden_message
);
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2948 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2949 tree type
, location_t type_location
)
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2955 && (declarator
->kind
== cdk_pointer
2956 || declarator
->kind
== cdk_reference
2957 || declarator
->kind
== cdk_ptrmem
))
2958 declarator
= declarator
->declarator
;
2960 && declarator
->kind
== cdk_function
)
2962 error_at (type_location
,
2963 "new types may not be defined in a return type");
2964 inform (type_location
,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2977 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2979 enum tag_types tag_type
,
2980 location_t location
)
2982 cp_token_position start
= 0;
2984 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2987 error_at (location
, "%qT is not a template", type
);
2988 else if (identifier_p (type
))
2990 if (tag_type
!= none_type
)
2991 error_at (location
, "%qE is not a class template", type
);
2993 error_at (location
, "%qE is not a template", type
);
2996 error_at (location
, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2999 start
= cp_lexer_token_position (parser
->lexer
, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser
->lexer
);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser
);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3007 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3017 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3018 non_integral_constant thing
)
3020 parser
->non_integral_constant_expression_p
= true;
3021 if (parser
->integral_constant_expression_p
)
3023 if (!parser
->allow_non_integral_constant_expression_p
)
3025 const char *msg
= NULL
;
3029 pedwarn (input_location
, OPT_Wpedantic
,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3062 case NIC_ADDR_LABEL
:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3066 case NIC_OVERLOADED
:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3070 case NIC_ASSIGNMENT
:
3071 error ("an assignment cannot appear in a constant-expression");
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3077 case NIC_CONSTRUCTOR
:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3081 case NIC_TRANSACTION
:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3089 msg
= "__FUNCTION__";
3091 case NIC_PRETTY_FUNC
:
3092 msg
= "__PRETTY_FUNCTION__";
3112 case NIC_PREINCREMENT
:
3115 case NIC_PREDECREMENT
:
3128 error ("%qs cannot appear in a constant-expression", msg
);
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3141 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3142 location_t location
)
3144 tree decl
, ambiguous_decls
;
3145 cp_parser_commit_to_tentative_parse (parser
);
3146 /* Try to lookup the identifier. */
3147 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls
, location
);
3152 if (ambiguous_decls
)
3153 /* If the lookup was ambiguous, an error will already have
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl
))
3161 "invalid use of template-name %qE without an argument list",
3163 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx1z
)
3164 inform (location
, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3168 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3169 error_at (location
, "invalid use of destructor %qD as a type", id
);
3170 else if (TREE_CODE (decl
) == TYPE_DECL
)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location
, "invalid combination of multiple type-specifiers");
3173 else if (!parser
->scope
)
3175 /* Issue an error message. */
3176 const char *suggestion
= NULL
;
3177 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3178 suggestion
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
);
3181 gcc_rich_location
richloc (location
);
3182 richloc
.add_fixit_replace (suggestion
);
3183 error_at_rich_loc (&richloc
,
3184 "%qE does not name a type; did you mean %qs?",
3188 error_at (location
, "%qE does not name a type", id
);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3197 inform (location
, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3200 inform (location
, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect
< cxx11
3203 && TREE_CODE (id
) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
3205 inform (location
, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3208 inform (location
, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl
&& current_class_type
3210 && TYPE_BINFO (current_class_type
))
3214 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3218 tree base_type
= BINFO_TYPE (b
);
3219 if (CLASS_TYPE_P (base_type
)
3220 && dependent_type_p (base_type
))
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3227 for (field
= TYPE_FIELDS (base_type
);
3229 field
= DECL_CHAIN (field
))
3230 if (TREE_CODE (field
) == TYPE_DECL
3231 && DECL_NAME (field
) == id
)
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b
), id
);
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser
->scope
!= error_mark_node
)
3248 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3251 error_at (location_of (id
),
3252 "%qE in namespace %qE does not name a template type",
3255 error_at (location_of (id
),
3256 "%qE in namespace %qE does not name a type",
3259 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3261 else if (CLASS_TYPE_P (parser
->scope
)
3262 && constructor_name_p (id
, parser
->scope
))
3265 error_at (location
, "%<%T::%E%> names the constructor, not"
3266 " the type", parser
->scope
, id
);
3267 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3268 error_at (location
, "and %qT has no template constructors",
3271 else if (TYPE_P (parser
->scope
)
3272 && dependent_scope_p (parser
->scope
))
3273 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser
->scope
, id
, parser
->scope
);
3276 else if (TYPE_P (parser
->scope
))
3278 if (!COMPLETE_TYPE_P (parser
->scope
))
3279 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3281 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3282 error_at (location_of (id
),
3283 "%qE in %q#T does not name a template type",
3286 error_at (location_of (id
),
3287 "%qE in %q#T does not name a type",
3290 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3311 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3316 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3317 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3321 cp_parser_parse_tentatively (parser
);
3322 id
= cp_parser_id_expression (parser
,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL
,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3331 || TREE_CODE (id
) == TYPE_DECL
)
3333 cp_parser_abort_tentative_parse (parser
);
3336 if (!cp_parser_parse_definitely (parser
))
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser
->in_declarator_p
)
3346 cp_parser_skip_to_end_of_block_or_statement (parser
);
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3361 unsigned paren_depth
= 0;
3362 unsigned brace_depth
= 0;
3363 unsigned square_depth
= 0;
3365 if (recovering
&& or_ttype
== CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3371 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3375 && !brace_depth
&& !paren_depth
&& !square_depth
)
3378 switch (token
->type
)
3381 case CPP_PRAGMA_EOL
:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE
:
3389 case CPP_CLOSE_SQUARE
:
3390 if (!square_depth
--)
3395 /* This matches the processing in skip_to_end_of_statement. */
3400 case CPP_OPEN_BRACE
:
3403 case CPP_CLOSE_BRACE
:
3408 case CPP_OPEN_PAREN
:
3413 case CPP_CLOSE_PAREN
:
3414 if (!brace_depth
&& !paren_depth
--)
3417 cp_lexer_consume_token (parser
->lexer
);
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser
->lexer
);
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3437 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3442 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3444 ttype
, consume_paren
);
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3452 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3454 unsigned nesting_depth
= 0;
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser
->fully_implicit_function_template_p
)
3458 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3462 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3464 switch (token
->type
)
3467 case CPP_PRAGMA_EOL
:
3468 /* If we've run out of tokens, stop. */
3472 /* If the next token is a `;', we have reached the end of the
3478 case CPP_CLOSE_BRACE
:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth
== 0)
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3499 if (--nesting_depth
== 0)
3501 cp_lexer_consume_token (parser
->lexer
);
3506 case CPP_OPEN_BRACE
:
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser
->lexer
);
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3529 /* If there is additional (erroneous) input, skip to the end of
3531 cp_parser_skip_to_end_of_statement (parser
);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3534 cp_lexer_consume_token (parser
->lexer
);
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3544 int nesting_depth
= 0;
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser
->fully_implicit_function_template_p
)
3548 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3550 while (nesting_depth
>= 0)
3552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3554 switch (token
->type
)
3557 case CPP_PRAGMA_EOL
:
3558 /* If we've run out of tokens, stop. */
3562 /* Stop if this is an unnested ';'. */
3567 case CPP_CLOSE_BRACE
:
3568 /* Stop if this is an unnested '}', or closes the outermost
3571 if (nesting_depth
< 0)
3577 case CPP_OPEN_BRACE
:
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser
->lexer
);
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3596 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3598 unsigned nesting_depth
= 0;
3602 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3604 switch (token
->type
)
3607 case CPP_PRAGMA_EOL
:
3608 /* If we've run out of tokens, stop. */
3611 case CPP_CLOSE_BRACE
:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth
-- == 0)
3618 case CPP_OPEN_BRACE
:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser
->lexer
);
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3638 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3642 parser
->lexer
->in_pragma
= false;
3645 token
= cp_lexer_consume_token (parser
->lexer
);
3646 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3656 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3658 parser
->lexer
->in_pragma
= false;
3659 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3660 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3668 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3669 location_t id_location
)
3672 if (identifier_p (id
))
3674 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3675 /*complain=*/tf_none
);
3676 if (result
== error_mark_node
)
3677 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3680 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3690 static cp_declarator
*
3691 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3692 cp_cv_quals cv_qualifiers
,
3693 cp_declarator
*target
,
3696 if (code
== ERROR_MARK
)
3697 return cp_error_declarator
;
3699 if (code
== INDIRECT_REF
)
3700 if (class_type
== NULL_TREE
)
3701 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3703 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3704 target
, attributes
);
3705 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3706 return make_reference_declarator (cv_qualifiers
, target
,
3708 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3709 return make_reference_declarator (cv_qualifiers
, target
,
3714 /* Create a new C++ parser. */
3717 cp_parser_new (void)
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer
= cp_lexer_new_main ();
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3730 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3732 parser
= ggc_cleared_alloc
<cp_parser
> ();
3733 parser
->lexer
= lexer
;
3734 parser
->context
= cp_parser_context_new (NULL
);
3736 /* For now, we always accept GNU extensions. */
3737 parser
->allow_gnu_extensions_p
= 1;
3739 /* The `>' token is a greater-than operator, not the end of a
3741 parser
->greater_than_is_operator_p
= true;
3743 parser
->default_arg_ok_p
= true;
3745 /* We are not parsing a constant-expression. */
3746 parser
->integral_constant_expression_p
= false;
3747 parser
->allow_non_integral_constant_expression_p
= false;
3748 parser
->non_integral_constant_expression_p
= false;
3750 /* Local variable names are not forbidden. */
3751 parser
->local_variables_forbidden_p
= false;
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser
->in_unbraced_linkage_specification_p
= false;
3756 /* We are not processing a declarator. */
3757 parser
->in_declarator_p
= false;
3759 /* We are not processing a template-argument-list. */
3760 parser
->in_template_argument_list_p
= false;
3762 /* We are not in an iteration statement. */
3763 parser
->in_statement
= 0;
3765 /* We are not in a switch statement. */
3766 parser
->in_switch_statement_p
= false;
3768 /* We are not parsing a type-id inside an expression. */
3769 parser
->in_type_id_in_expr_p
= false;
3771 /* Declarations aren't implicitly extern "C". */
3772 parser
->implicit_extern_c
= false;
3774 /* String literals should be translated to the execution character set. */
3775 parser
->translate_strings_p
= true;
3777 /* We are not parsing a function body. */
3778 parser
->in_function_body
= false;
3780 /* We can correct until told otherwise. */
3781 parser
->colon_corrects_to_scope_p
= true;
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser
);
3786 /* There are no classes being defined. */
3787 parser
->num_classes_being_defined
= 0;
3789 /* No template parameters apply. */
3790 parser
->num_template_parameter_lists
= 0;
3792 /* Special parsing data structures. */
3793 parser
->omp_declare_simd
= NULL
;
3794 parser
->cilk_simd_fn_info
= NULL
;
3795 parser
->oacc_routine
= NULL
;
3797 /* Not declaring an implicit function template. */
3798 parser
->auto_is_implicit_function_template_parm_p
= false;
3799 parser
->fully_implicit_function_template_p
= false;
3800 parser
->implicit_template_parms
= 0;
3801 parser
->implicit_template_scope
= 0;
3803 /* Allow constrained-type-specifiers. */
3804 parser
->prevent_constrained_type_specifiers
= 0;
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3814 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3816 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3817 lexer
->next
= parser
->lexer
;
3818 parser
->lexer
= lexer
;
3820 /* Move the current source position to that of the first token in the
3822 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3828 cp_parser_pop_lexer (cp_parser
*parser
)
3830 cp_lexer
*lexer
= parser
->lexer
;
3831 parser
->lexer
= lexer
->next
;
3832 cp_lexer_destroy (lexer
);
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3839 /* Lexical conventions [gram.lex] */
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3845 cp_parser_identifier (cp_parser
* parser
)
3849 /* Look for the identifier. */
3850 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3851 /* Return the value. */
3853 return cp_expr (token
->u
.value
, token
->location
);
3855 return error_mark_node
;
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3869 This code is largely lifted from lex_string() in c-lex.c.
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3873 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3874 bool lookup_udlit
= true)
3878 struct obstack str_ob
;
3879 cpp_string str
, istr
, *strs
;
3881 enum cpp_ttype type
, curr_type
;
3882 int have_suffix_p
= 0;
3884 tree suffix_id
= NULL_TREE
;
3885 bool curr_tok_is_userdef_p
= false;
3887 tok
= cp_lexer_peek_token (parser
->lexer
);
3888 if (!cp_parser_is_string_literal (tok
))
3890 cp_parser_error (parser
, "expected string-literal");
3891 return error_mark_node
;
3894 location_t loc
= tok
->location
;
3896 if (cpp_userdef_string_p (tok
->type
))
3898 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3899 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3900 curr_tok_is_userdef_p
= true;
3904 string_tree
= tok
->u
.value
;
3905 curr_type
= tok
->type
;
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3914 cp_lexer_consume_token (parser
->lexer
);
3916 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3917 str
.len
= TREE_STRING_LENGTH (string_tree
);
3920 if (curr_tok_is_userdef_p
)
3922 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3924 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3927 curr_type
= tok
->type
;
3933 location_t last_tok_loc
= tok
->location
;
3934 gcc_obstack_init (&str_ob
);
3939 cp_lexer_consume_token (parser
->lexer
);
3941 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3942 str
.len
= TREE_STRING_LENGTH (string_tree
);
3944 if (curr_tok_is_userdef_p
)
3946 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3947 if (have_suffix_p
== 0)
3949 suffix_id
= curr_suffix_id
;
3952 else if (have_suffix_p
== 1
3953 && curr_suffix_id
!= suffix_id
)
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id
, curr_suffix_id
);
3960 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3963 curr_type
= tok
->type
;
3965 if (type
!= curr_type
)
3967 if (type
== CPP_STRING
)
3969 else if (curr_type
!= CPP_STRING
)
3971 rich_location
rich_loc (line_table
, tok
->location
);
3972 rich_loc
.add_range (last_tok_loc
, false);
3973 error_at_rich_loc (&rich_loc
,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3979 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3981 last_tok_loc
= tok
->location
;
3983 tok
= cp_lexer_peek_token (parser
->lexer
);
3984 if (cpp_userdef_string_p (tok
->type
))
3986 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3987 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3988 curr_tok_is_userdef_p
= true;
3992 string_tree
= tok
->u
.value
;
3993 curr_type
= tok
->type
;
3994 curr_tok_is_userdef_p
= false;
3997 while (cp_parser_is_string_literal (tok
));
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4004 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4007 if (type
!= CPP_STRING
&& !wide_ok
)
4009 cp_parser_error (parser
, "a wide string is invalid in this context");
4013 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4014 (parse_in
, strs
, count
, &istr
, type
))
4016 value
= build_string (istr
.len
, (const char *)istr
.text
);
4017 free (CONST_CAST (unsigned char *, istr
.text
));
4023 case CPP_UTF8STRING
:
4024 TREE_TYPE (value
) = char_array_type_node
;
4027 TREE_TYPE (value
) = char16_array_type_node
;
4030 TREE_TYPE (value
) = char32_array_type_node
;
4033 TREE_TYPE (value
) = wchar_array_type_node
;
4037 value
= fix_string_type (value
);
4041 tree literal
= build_userdef_literal (suffix_id
, value
,
4042 OT_NONE
, NULL_TREE
);
4044 value
= cp_parser_userdef_string_literal (literal
);
4050 /* cpp_interpret_string has issued an error. */
4051 value
= error_mark_node
;
4054 obstack_free (&str_ob
, 0);
4056 return cp_expr (value
, loc
);
4059 /* Look up a literal operator with the name and the exact arguments. */
4062 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4065 decl
= lookup_name (name
);
4066 if (!decl
|| !is_overloaded_fn (decl
))
4067 return error_mark_node
;
4069 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
4073 tree fn
= OVL_CURRENT (fns
);
4074 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4075 if (parmtypes
!= NULL_TREE
)
4077 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4078 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4080 tree tparm
= TREE_VALUE (parmtypes
);
4081 tree targ
= TREE_TYPE ((*args
)[ix
]);
4082 bool ptr
= TYPE_PTR_P (tparm
);
4083 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4084 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4086 || !same_type_p (TREE_TYPE (tparm
),
4091 && ix
== vec_safe_length (args
)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes
== void_list_node
)
4101 return error_mark_node
;
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4108 cp_parser_userdef_char_literal (cp_parser
*parser
)
4110 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4111 tree literal
= token
->u
.value
;
4112 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4113 tree value
= USERDEF_LITERAL_VALUE (literal
);
4114 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4120 vec_safe_push (args
, value
);
4121 decl
= lookup_literal_operator (name
, args
);
4122 if (!decl
|| decl
== error_mark_node
)
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name
, TREE_TYPE (value
));
4126 release_tree_vector (args
);
4127 return error_mark_node
;
4129 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4130 release_tree_vector (args
);
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4138 make_char_string_pack (tree value
)
4141 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4142 const char *str
= TREE_STRING_POINTER (value
);
4143 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4144 tree argvec
= make_tree_vec (1);
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec
= make_tree_vec (len
);
4148 for (i
= 0; i
< len
; ++i
)
4149 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4153 TREE_TYPE (argpack
) = char_type_node
;
4155 TREE_VEC_ELT (argvec
, 0) = argpack
;
4160 /* A subroutine of cp_parser_userdef_numeric_literal to
4161 create a char... template parameter pack from a string node. */
4164 make_string_pack (tree value
)
4167 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4168 const unsigned char *str
4169 = (const unsigned char *) TREE_STRING_POINTER (value
);
4170 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4171 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4172 tree argvec
= make_tree_vec (2);
4174 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4175 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4177 /* First template parm is character type. */
4178 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4180 /* Fill in CHARVEC with all of the parameters. */
4181 charvec
= make_tree_vec (len
);
4182 for (int i
= 0; i
< len
; ++i
)
4183 TREE_VEC_ELT (charvec
, i
)
4184 = double_int_to_tree (str_char_type_node
,
4185 double_int::from_buffer (str
+ i
* sz
, sz
));
4187 /* Build the argument packs. */
4188 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4189 TREE_TYPE (argpack
) = str_char_type_node
;
4191 TREE_VEC_ELT (argvec
, 1) = argpack
;
4196 /* Parse a user-defined numeric constant. returns a call to a user-defined
4197 literal operator. */
4200 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4202 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4203 tree literal
= token
->u
.value
;
4204 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4205 tree value
= USERDEF_LITERAL_VALUE (literal
);
4206 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4207 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4208 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4210 vec
<tree
, va_gc
> *args
;
4212 /* Look for a literal operator taking the exact type of numeric argument
4213 as the literal value. */
4214 args
= make_tree_vector ();
4215 vec_safe_push (args
, value
);
4216 decl
= lookup_literal_operator (name
, args
);
4217 if (decl
&& decl
!= error_mark_node
)
4219 result
= finish_call_expr (decl
, &args
, false, true,
4220 tf_warning_or_error
);
4222 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4224 warning_at (token
->location
, OPT_Woverflow
,
4225 "integer literal exceeds range of %qT type",
4226 long_long_unsigned_type_node
);
4231 warning_at (token
->location
, OPT_Woverflow
,
4232 "floating literal exceeds range of %qT type",
4233 long_double_type_node
);
4234 else if (overflow
< 0)
4235 warning_at (token
->location
, OPT_Woverflow
,
4236 "floating literal truncated to zero");
4239 release_tree_vector (args
);
4242 release_tree_vector (args
);
4244 /* If the numeric argument didn't work, look for a raw literal
4245 operator taking a const char* argument consisting of the number
4246 in string format. */
4247 args
= make_tree_vector ();
4248 vec_safe_push (args
, num_string
);
4249 decl
= lookup_literal_operator (name
, args
);
4250 if (decl
&& decl
!= error_mark_node
)
4252 result
= finish_call_expr (decl
, &args
, false, true,
4253 tf_warning_or_error
);
4254 release_tree_vector (args
);
4257 release_tree_vector (args
);
4259 /* If the raw literal didn't work, look for a non-type template
4260 function with parameter pack char.... Call the function with
4261 template parameter characters representing the number. */
4262 args
= make_tree_vector ();
4263 decl
= lookup_literal_operator (name
, args
);
4264 if (decl
&& decl
!= error_mark_node
)
4266 tree tmpl_args
= make_char_string_pack (num_string
);
4267 decl
= lookup_template_function (decl
, tmpl_args
);
4268 result
= finish_call_expr (decl
, &args
, false, true,
4269 tf_warning_or_error
);
4270 release_tree_vector (args
);
4274 release_tree_vector (args
);
4276 error ("unable to find numeric literal operator %qD", name
);
4277 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4278 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4279 "to enable more built-in suffixes");
4280 return error_mark_node
;
4283 /* Parse a user-defined string constant. Returns a call to a user-defined
4284 literal operator taking a character pointer and the length of the string
4288 cp_parser_userdef_string_literal (tree literal
)
4290 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4291 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4292 tree value
= USERDEF_LITERAL_VALUE (literal
);
4293 int len
= TREE_STRING_LENGTH (value
)
4294 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4296 vec
<tree
, va_gc
> *args
;
4298 /* Build up a call to the user-defined operator. */
4299 /* Lookup the name we got back from the id-expression. */
4300 args
= make_tree_vector ();
4301 vec_safe_push (args
, value
);
4302 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4303 decl
= lookup_literal_operator (name
, args
);
4305 if (decl
&& decl
!= error_mark_node
)
4307 result
= finish_call_expr (decl
, &args
, false, true,
4308 tf_warning_or_error
);
4309 release_tree_vector (args
);
4312 release_tree_vector (args
);
4314 /* Look for a template function with typename parameter CharT
4315 and parameter pack CharT... Call the function with
4316 template parameter characters representing the string. */
4317 args
= make_tree_vector ();
4318 decl
= lookup_literal_operator (name
, args
);
4319 if (decl
&& decl
!= error_mark_node
)
4321 tree tmpl_args
= make_string_pack (value
);
4322 decl
= lookup_template_function (decl
, tmpl_args
);
4323 result
= finish_call_expr (decl
, &args
, false, true,
4324 tf_warning_or_error
);
4325 release_tree_vector (args
);
4328 release_tree_vector (args
);
4330 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4331 name
, TREE_TYPE (value
), size_type_node
);
4332 return error_mark_node
;
4336 /* Basic concepts [gram.basic] */
4338 /* Parse a translation-unit.
4341 declaration-seq [opt]
4343 Returns TRUE if all went well. */
4346 cp_parser_translation_unit (cp_parser
* parser
)
4348 /* The address of the first non-permanent object on the declarator
4350 static void *declarator_obstack_base
;
4354 /* Create the declarator obstack, if necessary. */
4355 if (!cp_error_declarator
)
4357 gcc_obstack_init (&declarator_obstack
);
4358 /* Create the error declarator. */
4359 cp_error_declarator
= make_declarator (cdk_error
);
4360 /* Create the empty parameter list. */
4361 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4362 /* Remember where the base of the declarator obstack lies. */
4363 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4366 cp_parser_declaration_seq_opt (parser
);
4368 /* If there are no tokens left then all went well. */
4369 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4371 /* Get rid of the token array; we don't need it any more. */
4372 cp_lexer_destroy (parser
->lexer
);
4373 parser
->lexer
= NULL
;
4375 /* This file might have been a context that's implicitly extern
4376 "C". If so, pop the lang context. (Only relevant for PCH.) */
4377 if (parser
->implicit_extern_c
)
4379 pop_lang_context ();
4380 parser
->implicit_extern_c
= false;
4384 finish_translation_unit ();
4390 cp_parser_error (parser
, "expected declaration");
4394 /* Make sure the declarator obstack was fully cleaned up. */
4395 gcc_assert (obstack_next_free (&declarator_obstack
)
4396 == declarator_obstack_base
);
4398 /* All went well. */
4402 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4403 decltype context. */
4405 static inline tsubst_flags_t
4406 complain_flags (bool decltype_p
)
4408 tsubst_flags_t complain
= tf_warning_or_error
;
4410 complain
|= tf_decltype
;
4414 /* We're about to parse a collection of statements. If we're currently
4415 parsing tentatively, set up a firewall so that any nested
4416 cp_parser_commit_to_tentative_parse won't affect the current context. */
4418 static cp_token_position
4419 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4424 cp_parser_parse_tentatively (parser
);
4425 cp_parser_commit_to_topmost_tentative_parse (parser
);
4426 return cp_lexer_token_position (parser
->lexer
, false);
4429 /* We've finished parsing the collection of statements. Wrap up the
4430 firewall and replace the relevant tokens with the parsed form. */
4433 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4439 /* Finish the firewall level. */
4440 cp_parser_parse_definitely (parser
);
4441 /* And remember the result of the parse for when we try again. */
4442 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4443 token
->type
= CPP_PREPARSED_EXPR
;
4444 token
->u
.value
= expr
;
4445 token
->keyword
= RID_MAX
;
4446 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4449 /* Like the above functions, but let the user modify the tokens. Used by
4450 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4451 later parses, so it makes sense to localize the effects of
4452 cp_parser_commit_to_tentative_parse. */
4454 struct tentative_firewall
4459 tentative_firewall (cp_parser
*p
): parser(p
)
4461 /* If we're currently parsing tentatively, start a committed level as a
4462 firewall and then an inner tentative parse. */
4463 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4465 cp_parser_parse_tentatively (parser
);
4466 cp_parser_commit_to_topmost_tentative_parse (parser
);
4467 cp_parser_parse_tentatively (parser
);
4471 ~tentative_firewall()
4475 /* Finish the inner tentative parse and the firewall, propagating any
4476 uncommitted error state to the outer tentative parse. */
4477 bool err
= cp_parser_error_occurred (parser
);
4478 cp_parser_parse_definitely (parser
);
4479 cp_parser_parse_definitely (parser
);
4481 cp_parser_simulate_error (parser
);
4486 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4487 enclosing parentheses. */
4490 cp_parser_statement_expr (cp_parser
*parser
)
4492 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4494 /* Consume the '('. */
4495 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4496 cp_lexer_consume_token (parser
->lexer
);
4497 /* Start the statement-expression. */
4498 tree expr
= begin_stmt_expr ();
4499 /* Parse the compound-statement. */
4500 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4502 expr
= finish_stmt_expr (expr
, false);
4503 /* Consume the ')'. */
4504 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4505 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4506 cp_parser_skip_to_end_of_statement (parser
);
4508 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4509 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4510 return cp_expr (expr
, combined_loc
);
4513 /* Expressions [gram.expr] */
4515 /* Parse a fold-operator.
4518 - * / % ^ & | = < > << >>
4519 = -= *= /= %= ^= &= |= <<= >>=
4520 == != <= >= && || , .* ->*
4522 This returns the tree code corresponding to the matched operator
4523 as an int. When the current token matches a compound assignment
4524 opertor, the resulting tree code is the negative value of the
4525 non-assignment operator. */
4528 cp_parser_fold_operator (cp_token
*token
)
4530 switch (token
->type
)
4532 case CPP_PLUS
: return PLUS_EXPR
;
4533 case CPP_MINUS
: return MINUS_EXPR
;
4534 case CPP_MULT
: return MULT_EXPR
;
4535 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4536 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4537 case CPP_XOR
: return BIT_XOR_EXPR
;
4538 case CPP_AND
: return BIT_AND_EXPR
;
4539 case CPP_OR
: return BIT_IOR_EXPR
;
4540 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4541 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4543 case CPP_EQ
: return -NOP_EXPR
;
4544 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4545 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4546 case CPP_MULT_EQ
: return -MULT_EXPR
;
4547 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4548 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4549 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4550 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4551 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4552 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4553 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4555 case CPP_EQ_EQ
: return EQ_EXPR
;
4556 case CPP_NOT_EQ
: return NE_EXPR
;
4557 case CPP_LESS
: return LT_EXPR
;
4558 case CPP_GREATER
: return GT_EXPR
;
4559 case CPP_LESS_EQ
: return LE_EXPR
;
4560 case CPP_GREATER_EQ
: return GE_EXPR
;
4562 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4563 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4565 case CPP_COMMA
: return COMPOUND_EXPR
;
4567 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4568 case CPP_DEREF_STAR
: return MEMBER_REF
;
4570 default: return ERROR_MARK
;
4574 /* Returns true if CODE indicates a binary expression, which is not allowed in
4575 the LHS of a fold-expression. More codes will need to be added to use this
4576 function in other contexts. */
4579 is_binary_op (tree_code code
)
4584 case POINTER_PLUS_EXPR
:
4587 case TRUNC_DIV_EXPR
:
4588 case TRUNC_MOD_EXPR
:
4604 case TRUTH_ANDIF_EXPR
:
4605 case TRUTH_ORIF_EXPR
:
4618 /* If the next token is a suitable fold operator, consume it and return as
4619 the function above. */
4622 cp_parser_fold_operator (cp_parser
*parser
)
4624 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4625 int code
= cp_parser_fold_operator (token
);
4626 if (code
!= ERROR_MARK
)
4627 cp_lexer_consume_token (parser
->lexer
);
4631 /* Parse a fold-expression.
4634 ( ... folding-operator cast-expression)
4635 ( cast-expression folding-operator ... )
4636 ( cast-expression folding operator ... folding-operator cast-expression)
4638 Note that the '(' and ')' are matched in primary expression. */
4641 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4648 cp_lexer_consume_token (parser
->lexer
);
4649 int op
= cp_parser_fold_operator (parser
);
4650 if (op
== ERROR_MARK
)
4652 cp_parser_error (parser
, "expected binary operator");
4653 return error_mark_node
;
4656 tree expr
= cp_parser_cast_expression (parser
, false, false,
4658 if (expr
== error_mark_node
)
4659 return error_mark_node
;
4660 return finish_left_unary_fold_expr (expr
, op
);
4663 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4664 int op
= cp_parser_fold_operator (parser
);
4665 if (op
== ERROR_MARK
)
4667 cp_parser_error (parser
, "expected binary operator");
4668 return error_mark_node
;
4671 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4673 cp_parser_error (parser
, "expected ...");
4674 return error_mark_node
;
4676 cp_lexer_consume_token (parser
->lexer
);
4678 /* The operands of a fold-expression are cast-expressions, so binary or
4679 conditional expressions are not allowed. We check this here to avoid
4680 tentative parsing. */
4681 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4682 /* OK, the expression was parenthesized. */;
4683 else if (is_binary_op (TREE_CODE (expr1
)))
4684 error_at (location_of (expr1
),
4685 "binary expression in operand of fold-expression");
4686 else if (TREE_CODE (expr1
) == COND_EXPR
)
4687 error_at (location_of (expr1
),
4688 "conditional expression in operand of fold-expression");
4691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4692 return finish_right_unary_fold_expr (expr1
, op
);
4694 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4696 cp_parser_error (parser
, "mismatched operator in fold-expression");
4697 return error_mark_node
;
4699 cp_lexer_consume_token (parser
->lexer
);
4701 // Binary left or right fold.
4702 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4703 if (expr2
== error_mark_node
)
4704 return error_mark_node
;
4705 return finish_binary_fold_expr (expr1
, expr2
, op
);
4708 /* Parse a primary-expression.
4715 lambda-expression (C++11)
4720 ( compound-statement )
4721 __builtin_va_arg ( assignment-expression , type-id )
4722 __builtin_offsetof ( type-id , offsetof-expression )
4725 __has_nothrow_assign ( type-id )
4726 __has_nothrow_constructor ( type-id )
4727 __has_nothrow_copy ( type-id )
4728 __has_trivial_assign ( type-id )
4729 __has_trivial_constructor ( type-id )
4730 __has_trivial_copy ( type-id )
4731 __has_trivial_destructor ( type-id )
4732 __has_virtual_destructor ( type-id )
4733 __is_abstract ( type-id )
4734 __is_base_of ( type-id , type-id )
4735 __is_class ( type-id )
4736 __is_empty ( type-id )
4737 __is_enum ( type-id )
4738 __is_final ( type-id )
4739 __is_literal_type ( type-id )
4740 __is_pod ( type-id )
4741 __is_polymorphic ( type-id )
4742 __is_std_layout ( type-id )
4743 __is_trivial ( type-id )
4744 __is_union ( type-id )
4746 Objective-C++ Extension:
4754 ADDRESS_P is true iff this expression was immediately preceded by
4755 "&" and therefore might denote a pointer-to-member. CAST_P is true
4756 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4757 true iff this expression is a template argument.
4759 Returns a representation of the expression. Upon return, *IDK
4760 indicates what kind of id-expression (if any) was present. */
4763 cp_parser_primary_expression (cp_parser
*parser
,
4766 bool template_arg_p
,
4770 cp_token
*token
= NULL
;
4772 /* Assume the primary expression is not an id-expression. */
4773 *idk
= CP_ID_KIND_NONE
;
4775 /* Peek at the next token. */
4776 token
= cp_lexer_peek_token (parser
->lexer
);
4777 switch ((int) token
->type
)
4786 user-defined-literal */
4793 case CPP_PREPARSED_EXPR
:
4794 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4795 return cp_parser_userdef_numeric_literal (parser
);
4796 token
= cp_lexer_consume_token (parser
->lexer
);
4797 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4799 error_at (token
->location
,
4800 "fixed-point types not supported in C++");
4801 return error_mark_node
;
4803 /* Floating-point literals are only allowed in an integral
4804 constant expression if they are cast to an integral or
4805 enumeration type. */
4806 if (TREE_CODE (token
->u
.value
) == REAL_CST
4807 && parser
->integral_constant_expression_p
4810 /* CAST_P will be set even in invalid code like "int(2.7 +
4811 ...)". Therefore, we have to check that the next token
4812 is sure to end the cast. */
4815 cp_token
*next_token
;
4817 next_token
= cp_lexer_peek_token (parser
->lexer
);
4818 if (/* The comma at the end of an
4819 enumerator-definition. */
4820 next_token
->type
!= CPP_COMMA
4821 /* The curly brace at the end of an enum-specifier. */
4822 && next_token
->type
!= CPP_CLOSE_BRACE
4823 /* The end of a statement. */
4824 && next_token
->type
!= CPP_SEMICOLON
4825 /* The end of the cast-expression. */
4826 && next_token
->type
!= CPP_CLOSE_PAREN
4827 /* The end of an array bound. */
4828 && next_token
->type
!= CPP_CLOSE_SQUARE
4829 /* The closing ">" in a template-argument-list. */
4830 && (next_token
->type
!= CPP_GREATER
4831 || parser
->greater_than_is_operator_p
)
4832 /* C++0x only: A ">>" treated like two ">" tokens,
4833 in a template-argument-list. */
4834 && (next_token
->type
!= CPP_RSHIFT
4835 || (cxx_dialect
== cxx98
)
4836 || parser
->greater_than_is_operator_p
))
4840 /* If we are within a cast, then the constraint that the
4841 cast is to an integral or enumeration type will be
4842 checked at that point. If we are not within a cast, then
4843 this code is invalid. */
4845 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4847 return cp_expr (token
->u
.value
, token
->location
);
4849 case CPP_CHAR_USERDEF
:
4850 case CPP_CHAR16_USERDEF
:
4851 case CPP_CHAR32_USERDEF
:
4852 case CPP_WCHAR_USERDEF
:
4853 case CPP_UTF8CHAR_USERDEF
:
4854 return cp_parser_userdef_char_literal (parser
);
4860 case CPP_UTF8STRING
:
4861 case CPP_STRING_USERDEF
:
4862 case CPP_STRING16_USERDEF
:
4863 case CPP_STRING32_USERDEF
:
4864 case CPP_WSTRING_USERDEF
:
4865 case CPP_UTF8STRING_USERDEF
:
4866 /* ??? Should wide strings be allowed when parser->translate_strings_p
4867 is false (i.e. in attributes)? If not, we can kill the third
4868 argument to cp_parser_string_literal. */
4869 return cp_parser_string_literal (parser
,
4870 parser
->translate_strings_p
,
4873 case CPP_OPEN_PAREN
:
4874 /* If we see `( { ' then we are looking at the beginning of
4875 a GNU statement-expression. */
4876 if (cp_parser_allow_gnu_extensions_p (parser
)
4877 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4879 /* Statement-expressions are not allowed by the standard. */
4880 pedwarn (token
->location
, OPT_Wpedantic
,
4881 "ISO C++ forbids braced-groups within expressions");
4883 /* And they're not allowed outside of a function-body; you
4884 cannot, for example, write:
4886 int i = ({ int j = 3; j + 1; });
4888 at class or namespace scope. */
4889 if (!parser
->in_function_body
4890 || parser
->in_template_argument_list_p
)
4892 error_at (token
->location
,
4893 "statement-expressions are not allowed outside "
4894 "functions nor in template-argument lists");
4895 cp_parser_skip_to_end_of_block_or_statement (parser
);
4896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4897 cp_lexer_consume_token (parser
->lexer
);
4898 return error_mark_node
;
4901 return cp_parser_statement_expr (parser
);
4903 /* Otherwise it's a normal parenthesized expression. */
4906 bool saved_greater_than_is_operator_p
;
4908 location_t open_paren_loc
= token
->location
;
4910 /* Consume the `('. */
4911 cp_lexer_consume_token (parser
->lexer
);
4912 /* Within a parenthesized expression, a `>' token is always
4913 the greater-than operator. */
4914 saved_greater_than_is_operator_p
4915 = parser
->greater_than_is_operator_p
;
4916 parser
->greater_than_is_operator_p
= true;
4918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4919 /* Left fold expression. */
4922 /* Parse the parenthesized expression. */
4923 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4925 token
= cp_lexer_peek_token (parser
->lexer
);
4926 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
4928 expr
= cp_parser_fold_expression (parser
, expr
);
4929 if (expr
!= error_mark_node
4930 && cxx_dialect
< cxx1z
4931 && !in_system_header_at (input_location
))
4932 pedwarn (input_location
, 0, "fold-expressions only available "
4933 "with -std=c++1z or -std=gnu++1z");
4936 /* Let the front end know that this expression was
4937 enclosed in parentheses. This matters in case, for
4938 example, the expression is of the form `A::B', since
4939 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4941 expr
= finish_parenthesized_expr (expr
);
4943 /* DR 705: Wrapping an unqualified name in parentheses
4944 suppresses arg-dependent lookup. We want to pass back
4945 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4946 (c++/37862), but none of the others. */
4947 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4948 *idk
= CP_ID_KIND_NONE
;
4950 /* The `>' token might be the end of a template-id or
4951 template-parameter-list now. */
4952 parser
->greater_than_is_operator_p
4953 = saved_greater_than_is_operator_p
;
4955 /* Consume the `)'. */
4956 token
= cp_lexer_peek_token (parser
->lexer
);
4957 location_t close_paren_loc
= token
->location
;
4958 expr
.set_range (open_paren_loc
, close_paren_loc
);
4959 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
)
4960 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4961 cp_parser_skip_to_end_of_statement (parser
);
4966 case CPP_OPEN_SQUARE
:
4968 if (c_dialect_objc ())
4970 /* We might have an Objective-C++ message. */
4971 cp_parser_parse_tentatively (parser
);
4972 tree msg
= cp_parser_objc_message_expression (parser
);
4973 /* If that works out, we're done ... */
4974 if (cp_parser_parse_definitely (parser
))
4976 /* ... else, fall though to see if it's a lambda. */
4978 cp_expr lam
= cp_parser_lambda_expression (parser
);
4979 /* Don't warn about a failed tentative parse. */
4980 if (cp_parser_error_occurred (parser
))
4981 return error_mark_node
;
4982 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4986 case CPP_OBJC_STRING
:
4987 if (c_dialect_objc ())
4988 /* We have an Objective-C++ string literal. */
4989 return cp_parser_objc_expression (parser
);
4990 cp_parser_error (parser
, "expected primary-expression");
4991 return error_mark_node
;
4994 switch (token
->keyword
)
4996 /* These two are the boolean literals. */
4998 cp_lexer_consume_token (parser
->lexer
);
4999 return cp_expr (boolean_true_node
, token
->location
);
5001 cp_lexer_consume_token (parser
->lexer
);
5002 return cp_expr (boolean_false_node
, token
->location
);
5004 /* The `__null' literal. */
5006 cp_lexer_consume_token (parser
->lexer
);
5007 return cp_expr (null_node
, token
->location
);
5009 /* The `nullptr' literal. */
5011 cp_lexer_consume_token (parser
->lexer
);
5012 return cp_expr (nullptr_node
, token
->location
);
5014 /* Recognize the `this' keyword. */
5016 cp_lexer_consume_token (parser
->lexer
);
5017 if (parser
->local_variables_forbidden_p
)
5019 error_at (token
->location
,
5020 "%<this%> may not be used in this context");
5021 return error_mark_node
;
5023 /* Pointers cannot appear in constant-expressions. */
5024 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5025 return error_mark_node
;
5026 return cp_expr (finish_this_expr (), token
->location
);
5028 /* The `operator' keyword can be the beginning of an
5033 case RID_FUNCTION_NAME
:
5034 case RID_PRETTY_FUNCTION_NAME
:
5035 case RID_C99_FUNCTION_NAME
:
5037 non_integral_constant name
;
5039 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5040 __func__ are the names of variables -- but they are
5041 treated specially. Therefore, they are handled here,
5042 rather than relying on the generic id-expression logic
5043 below. Grammatically, these names are id-expressions.
5045 Consume the token. */
5046 token
= cp_lexer_consume_token (parser
->lexer
);
5048 switch (token
->keyword
)
5050 case RID_FUNCTION_NAME
:
5051 name
= NIC_FUNC_NAME
;
5053 case RID_PRETTY_FUNCTION_NAME
:
5054 name
= NIC_PRETTY_FUNC
;
5056 case RID_C99_FUNCTION_NAME
:
5057 name
= NIC_C99_FUNC
;
5063 if (cp_parser_non_integral_constant_expression (parser
, name
))
5064 return error_mark_node
;
5066 /* Look up the name. */
5067 return finish_fname (token
->u
.value
);
5074 source_location type_location
;
5075 location_t start_loc
5076 = cp_lexer_peek_token (parser
->lexer
)->location
;
5077 /* The `__builtin_va_arg' construct is used to handle
5078 `va_arg'. Consume the `__builtin_va_arg' token. */
5079 cp_lexer_consume_token (parser
->lexer
);
5080 /* Look for the opening `('. */
5081 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5082 /* Now, parse the assignment-expression. */
5083 expression
= cp_parser_assignment_expression (parser
);
5084 /* Look for the `,'. */
5085 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5086 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5087 /* Parse the type-id. */
5089 type_id_in_expr_sentinel
s (parser
);
5090 type
= cp_parser_type_id (parser
);
5092 /* Look for the closing `)'. */
5093 location_t finish_loc
5094 = cp_lexer_peek_token (parser
->lexer
)->location
;
5095 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5096 /* Using `va_arg' in a constant-expression is not
5098 if (cp_parser_non_integral_constant_expression (parser
,
5100 return error_mark_node
;
5101 /* Construct a location of the form:
5102 __builtin_va_arg (v, int)
5103 ~~~~~~~~~~~~~~~~~~~~~^~~~
5104 with the caret at the type, ranging from the start of the
5105 "__builtin_va_arg" token to the close paren. */
5106 location_t combined_loc
5107 = make_location (type_location
, start_loc
, finish_loc
);
5108 return build_x_va_arg (combined_loc
, expression
, type
);
5112 return cp_parser_builtin_offsetof (parser
);
5114 case RID_HAS_NOTHROW_ASSIGN
:
5115 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5116 case RID_HAS_NOTHROW_COPY
:
5117 case RID_HAS_TRIVIAL_ASSIGN
:
5118 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5119 case RID_HAS_TRIVIAL_COPY
:
5120 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5121 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5122 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5123 case RID_IS_ABSTRACT
:
5124 case RID_IS_BASE_OF
:
5129 case RID_IS_LITERAL_TYPE
:
5131 case RID_IS_POLYMORPHIC
:
5132 case RID_IS_SAME_AS
:
5133 case RID_IS_STD_LAYOUT
:
5134 case RID_IS_TRIVIAL
:
5135 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5136 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5137 case RID_IS_TRIVIALLY_COPYABLE
:
5139 return cp_parser_trait_expr (parser
, token
->keyword
);
5143 return cp_parser_requires_expression (parser
);
5145 /* Objective-C++ expressions. */
5147 case RID_AT_PROTOCOL
:
5148 case RID_AT_SELECTOR
:
5149 return cp_parser_objc_expression (parser
);
5152 if (parser
->in_function_body
5153 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5156 error_at (token
->location
,
5157 "a template declaration cannot appear at block scope");
5158 cp_parser_skip_to_end_of_block_or_statement (parser
);
5159 return error_mark_node
;
5163 cp_parser_error (parser
, "expected primary-expression");
5164 return error_mark_node
;
5167 /* An id-expression can start with either an identifier, a
5168 `::' as the beginning of a qualified-id, or the "operator"
5172 case CPP_TEMPLATE_ID
:
5173 case CPP_NESTED_NAME_SPECIFIER
:
5176 cp_expr id_expression
;
5178 const char *error_msg
;
5181 cp_token
*id_expr_token
;
5183 /* Parse the id-expression. */
5185 = cp_parser_id_expression (parser
,
5186 /*template_keyword_p=*/false,
5187 /*check_dependency_p=*/true,
5189 /*declarator_p=*/false,
5190 /*optional_p=*/false);
5191 if (id_expression
== error_mark_node
)
5192 return error_mark_node
;
5193 id_expr_token
= token
;
5194 token
= cp_lexer_peek_token (parser
->lexer
);
5195 done
= (token
->type
!= CPP_OPEN_SQUARE
5196 && token
->type
!= CPP_OPEN_PAREN
5197 && token
->type
!= CPP_DOT
5198 && token
->type
!= CPP_DEREF
5199 && token
->type
!= CPP_PLUS_PLUS
5200 && token
->type
!= CPP_MINUS_MINUS
);
5201 /* If we have a template-id, then no further lookup is
5202 required. If the template-id was for a template-class, we
5203 will sometimes have a TYPE_DECL at this point. */
5204 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5205 || TREE_CODE (id_expression
) == TYPE_DECL
)
5206 decl
= id_expression
;
5207 /* Look up the name. */
5210 tree ambiguous_decls
;
5212 /* If we already know that this lookup is ambiguous, then
5213 we've already issued an error message; there's no reason
5215 if (id_expr_token
->type
== CPP_NAME
5216 && id_expr_token
->error_reported
)
5218 cp_parser_simulate_error (parser
);
5219 return error_mark_node
;
5222 decl
= cp_parser_lookup_name (parser
, id_expression
,
5225 /*is_namespace=*/false,
5226 /*check_dependency=*/true,
5228 id_expr_token
->location
);
5229 /* If the lookup was ambiguous, an error will already have
5231 if (ambiguous_decls
)
5232 return error_mark_node
;
5234 /* In Objective-C++, we may have an Objective-C 2.0
5235 dot-syntax for classes here. */
5236 if (c_dialect_objc ()
5237 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5238 && TREE_CODE (decl
) == TYPE_DECL
5239 && objc_is_class_name (decl
))
5242 cp_lexer_consume_token (parser
->lexer
);
5243 component
= cp_parser_identifier (parser
);
5244 if (component
== error_mark_node
)
5245 return error_mark_node
;
5247 tree result
= objc_build_class_component_ref (id_expression
,
5249 /* Build a location of the form:
5252 with caret at the start of the component name (at
5253 input_location), ranging from the start of the id_expression
5254 to the end of the component name. */
5255 location_t combined_loc
5256 = make_location (input_location
, id_expression
.get_start (),
5257 get_finish (input_location
));
5258 protected_set_expr_location (result
, combined_loc
);
5262 /* In Objective-C++, an instance variable (ivar) may be preferred
5263 to whatever cp_parser_lookup_name() found.
5264 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5265 rest of c-family, we have to do a little extra work to preserve
5266 any location information in cp_expr "decl". Given that
5267 objc_lookup_ivar is implemented in "c-family" and "objc", we
5268 have a trip through the pure "tree" type, rather than cp_expr.
5269 Naively copying it back to "decl" would implicitly give the
5270 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5271 store an EXPR_LOCATION. Hence we only update "decl" (and
5272 hence its location_t) if we get back a different tree node. */
5273 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5275 if (decl_tree
!= decl
.get_value ())
5276 decl
= cp_expr (decl_tree
);
5278 /* If name lookup gives us a SCOPE_REF, then the
5279 qualifying scope was dependent. */
5280 if (TREE_CODE (decl
) == SCOPE_REF
)
5282 /* At this point, we do not know if DECL is a valid
5283 integral constant expression. We assume that it is
5284 in fact such an expression, so that code like:
5286 template <int N> struct A {
5290 is accepted. At template-instantiation time, we
5291 will check that B<N>::i is actually a constant. */
5294 /* Check to see if DECL is a local variable in a context
5295 where that is forbidden. */
5296 if (parser
->local_variables_forbidden_p
5297 && local_variable_p (decl
))
5299 /* It might be that we only found DECL because we are
5300 trying to be generous with pre-ISO scoping rules.
5301 For example, consider:
5305 for (int i = 0; i < 10; ++i) {}
5306 extern void f(int j = i);
5309 Here, name look up will originally find the out
5310 of scope `i'. We need to issue a warning message,
5311 but then use the global `i'. */
5312 decl
= check_for_out_of_scope_variable (decl
);
5313 if (local_variable_p (decl
))
5315 error_at (id_expr_token
->location
,
5316 "local variable %qD may not appear in this context",
5318 return error_mark_node
;
5323 decl
= (finish_id_expression
5324 (id_expression
, decl
, parser
->scope
,
5326 parser
->integral_constant_expression_p
,
5327 parser
->allow_non_integral_constant_expression_p
,
5328 &parser
->non_integral_constant_expression_p
,
5329 template_p
, done
, address_p
,
5332 id_expression
.get_location ()));
5334 cp_parser_error (parser
, error_msg
);
5335 decl
.set_location (id_expr_token
->location
);
5339 /* Anything else is an error. */
5341 cp_parser_error (parser
, "expected primary-expression");
5342 return error_mark_node
;
5346 static inline cp_expr
5347 cp_parser_primary_expression (cp_parser
*parser
,
5350 bool template_arg_p
,
5353 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5354 /*decltype*/false, idk
);
5357 /* Parse an id-expression.
5364 :: [opt] nested-name-specifier template [opt] unqualified-id
5366 :: operator-function-id
5369 Return a representation of the unqualified portion of the
5370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5371 a `::' or nested-name-specifier.
5373 Often, if the id-expression was a qualified-id, the caller will
5374 want to make a SCOPE_REF to represent the qualified-id. This
5375 function does not do this in order to avoid wastefully creating
5376 SCOPE_REFs when they are not required.
5378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5382 uninstantiated templates.
5384 If *TEMPLATE_P is non-NULL, it is set to true iff the
5385 `template' keyword is used to explicitly indicate that the entity
5386 named is a template.
5388 If DECLARATOR_P is true, the id-expression is appearing as part of
5389 a declarator, rather than as part of an expression. */
5392 cp_parser_id_expression (cp_parser
*parser
,
5393 bool template_keyword_p
,
5394 bool check_dependency_p
,
5399 bool global_scope_p
;
5400 bool nested_name_specifier_p
;
5402 /* Assume the `template' keyword was not used. */
5404 *template_p
= template_keyword_p
;
5406 /* Look for the optional `::' operator. */
5408 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5410 /* Look for the optional nested-name-specifier. */
5411 nested_name_specifier_p
5412 = (cp_parser_nested_name_specifier_opt (parser
,
5413 /*typename_keyword_p=*/false,
5418 /* If there is a nested-name-specifier, then we are looking at
5419 the first qualified-id production. */
5420 if (nested_name_specifier_p
)
5423 tree saved_object_scope
;
5424 tree saved_qualifying_scope
;
5425 cp_expr unqualified_id
;
5428 /* See if the next token is the `template' keyword. */
5430 template_p
= &is_template
;
5431 *template_p
= cp_parser_optional_template_keyword (parser
);
5432 /* Name lookup we do during the processing of the
5433 unqualified-id might obliterate SCOPE. */
5434 saved_scope
= parser
->scope
;
5435 saved_object_scope
= parser
->object_scope
;
5436 saved_qualifying_scope
= parser
->qualifying_scope
;
5437 /* Process the final unqualified-id. */
5438 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5441 /*optional_p=*/false);
5442 /* Restore the SAVED_SCOPE for our caller. */
5443 parser
->scope
= saved_scope
;
5444 parser
->object_scope
= saved_object_scope
;
5445 parser
->qualifying_scope
= saved_qualifying_scope
;
5447 return unqualified_id
;
5449 /* Otherwise, if we are in global scope, then we are looking at one
5450 of the other qualified-id productions. */
5451 else if (global_scope_p
)
5456 /* Peek at the next token. */
5457 token
= cp_lexer_peek_token (parser
->lexer
);
5459 /* If it's an identifier, and the next token is not a "<", then
5460 we can avoid the template-id case. This is an optimization
5461 for this common case. */
5462 if (token
->type
== CPP_NAME
5463 && !cp_parser_nth_token_starts_template_argument_list_p
5465 return cp_parser_identifier (parser
);
5467 cp_parser_parse_tentatively (parser
);
5468 /* Try a template-id. */
5469 id
= cp_parser_template_id (parser
,
5470 /*template_keyword_p=*/false,
5471 /*check_dependency_p=*/true,
5474 /* If that worked, we're done. */
5475 if (cp_parser_parse_definitely (parser
))
5478 /* Peek at the next token. (Changes in the token buffer may
5479 have invalidated the pointer obtained above.) */
5480 token
= cp_lexer_peek_token (parser
->lexer
);
5482 switch (token
->type
)
5485 return cp_parser_identifier (parser
);
5488 if (token
->keyword
== RID_OPERATOR
)
5489 return cp_parser_operator_function_id (parser
);
5493 cp_parser_error (parser
, "expected id-expression");
5494 return error_mark_node
;
5498 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5499 /*check_dependency_p=*/true,
5504 /* Parse an unqualified-id.
5508 operator-function-id
5509 conversion-function-id
5513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5514 keyword, in a construct like `A::template ...'.
5516 Returns a representation of unqualified-id. For the `identifier'
5517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5518 production a BIT_NOT_EXPR is returned; the operand of the
5519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5520 other productions, see the documentation accompanying the
5521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5522 names are looked up in uninstantiated templates. If DECLARATOR_P
5523 is true, the unqualified-id is appearing as part of a declarator,
5524 rather than as part of an expression. */
5527 cp_parser_unqualified_id (cp_parser
* parser
,
5528 bool template_keyword_p
,
5529 bool check_dependency_p
,
5535 /* Peek at the next token. */
5536 token
= cp_lexer_peek_token (parser
->lexer
);
5538 switch ((int) token
->type
)
5544 /* We don't know yet whether or not this will be a
5546 cp_parser_parse_tentatively (parser
);
5547 /* Try a template-id. */
5548 id
= cp_parser_template_id (parser
, template_keyword_p
,
5552 /* If it worked, we're done. */
5553 if (cp_parser_parse_definitely (parser
))
5555 /* Otherwise, it's an ordinary identifier. */
5556 return cp_parser_identifier (parser
);
5559 case CPP_TEMPLATE_ID
:
5560 return cp_parser_template_id (parser
, template_keyword_p
,
5568 tree qualifying_scope
;
5573 /* Consume the `~' token. */
5574 cp_lexer_consume_token (parser
->lexer
);
5575 /* Parse the class-name. The standard, as written, seems to
5578 template <typename T> struct S { ~S (); };
5579 template <typename T> S<T>::~S() {}
5581 is invalid, since `~' must be followed by a class-name, but
5582 `S<T>' is dependent, and so not known to be a class.
5583 That's not right; we need to look in uninstantiated
5584 templates. A further complication arises from:
5586 template <typename T> void f(T t) {
5590 Here, it is not possible to look up `T' in the scope of `T'
5591 itself. We must look in both the current scope, and the
5592 scope of the containing complete expression.
5594 Yet another issue is:
5603 The standard does not seem to say that the `S' in `~S'
5604 should refer to the type `S' and not the data member
5607 /* DR 244 says that we look up the name after the "~" in the
5608 same scope as we looked up the qualifying name. That idea
5609 isn't fully worked out; it's more complicated than that. */
5610 scope
= parser
->scope
;
5611 object_scope
= parser
->object_scope
;
5612 qualifying_scope
= parser
->qualifying_scope
;
5614 /* Check for invalid scopes. */
5615 if (scope
== error_mark_node
)
5617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5618 cp_lexer_consume_token (parser
->lexer
);
5619 return error_mark_node
;
5621 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5624 error_at (token
->location
,
5625 "scope %qT before %<~%> is not a class-name",
5627 cp_parser_simulate_error (parser
);
5628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5629 cp_lexer_consume_token (parser
->lexer
);
5630 return error_mark_node
;
5632 gcc_assert (!scope
|| TYPE_P (scope
));
5634 /* If the name is of the form "X::~X" it's OK even if X is a
5636 token
= cp_lexer_peek_token (parser
->lexer
);
5638 && token
->type
== CPP_NAME
5639 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5641 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5642 || (CLASS_TYPE_P (scope
)
5643 && constructor_name_p (token
->u
.value
, scope
))))
5645 cp_lexer_consume_token (parser
->lexer
);
5646 return build_nt (BIT_NOT_EXPR
, scope
);
5649 /* ~auto means the destructor of whatever the object is. */
5650 if (cp_parser_is_keyword (token
, RID_AUTO
))
5652 if (cxx_dialect
< cxx14
)
5653 pedwarn (input_location
, 0,
5654 "%<~auto%> only available with "
5655 "-std=c++14 or -std=gnu++14");
5656 cp_lexer_consume_token (parser
->lexer
);
5657 return build_nt (BIT_NOT_EXPR
, make_auto ());
5660 /* If there was an explicit qualification (S::~T), first look
5661 in the scope given by the qualification (i.e., S).
5663 Note: in the calls to cp_parser_class_name below we pass
5664 typename_type so that lookup finds the injected-class-name
5665 rather than the constructor. */
5667 type_decl
= NULL_TREE
;
5670 cp_parser_parse_tentatively (parser
);
5671 type_decl
= cp_parser_class_name (parser
,
5672 /*typename_keyword_p=*/false,
5673 /*template_keyword_p=*/false,
5675 /*check_dependency=*/false,
5676 /*class_head_p=*/false,
5678 if (cp_parser_parse_definitely (parser
))
5681 /* In "N::S::~S", look in "N" as well. */
5682 if (!done
&& scope
&& qualifying_scope
)
5684 cp_parser_parse_tentatively (parser
);
5685 parser
->scope
= qualifying_scope
;
5686 parser
->object_scope
= NULL_TREE
;
5687 parser
->qualifying_scope
= NULL_TREE
;
5689 = cp_parser_class_name (parser
,
5690 /*typename_keyword_p=*/false,
5691 /*template_keyword_p=*/false,
5693 /*check_dependency=*/false,
5694 /*class_head_p=*/false,
5696 if (cp_parser_parse_definitely (parser
))
5699 /* In "p->S::~T", look in the scope given by "*p" as well. */
5700 else if (!done
&& object_scope
)
5702 cp_parser_parse_tentatively (parser
);
5703 parser
->scope
= object_scope
;
5704 parser
->object_scope
= NULL_TREE
;
5705 parser
->qualifying_scope
= NULL_TREE
;
5707 = cp_parser_class_name (parser
,
5708 /*typename_keyword_p=*/false,
5709 /*template_keyword_p=*/false,
5711 /*check_dependency=*/false,
5712 /*class_head_p=*/false,
5714 if (cp_parser_parse_definitely (parser
))
5717 /* Look in the surrounding context. */
5720 parser
->scope
= NULL_TREE
;
5721 parser
->object_scope
= NULL_TREE
;
5722 parser
->qualifying_scope
= NULL_TREE
;
5723 if (processing_template_decl
)
5724 cp_parser_parse_tentatively (parser
);
5726 = cp_parser_class_name (parser
,
5727 /*typename_keyword_p=*/false,
5728 /*template_keyword_p=*/false,
5730 /*check_dependency=*/false,
5731 /*class_head_p=*/false,
5733 if (processing_template_decl
5734 && ! cp_parser_parse_definitely (parser
))
5736 /* We couldn't find a type with this name. If we're parsing
5737 tentatively, fail and try something else. */
5738 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5740 cp_parser_simulate_error (parser
);
5741 return error_mark_node
;
5743 /* Otherwise, accept it and check for a match at instantiation
5745 type_decl
= cp_parser_identifier (parser
);
5746 if (type_decl
!= error_mark_node
)
5747 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5751 /* If an error occurred, assume that the name of the
5752 destructor is the same as the name of the qualifying
5753 class. That allows us to keep parsing after running
5754 into ill-formed destructor names. */
5755 if (type_decl
== error_mark_node
&& scope
)
5756 return build_nt (BIT_NOT_EXPR
, scope
);
5757 else if (type_decl
== error_mark_node
)
5758 return error_mark_node
;
5760 /* Check that destructor name and scope match. */
5761 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5763 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5764 error_at (token
->location
,
5765 "declaration of %<~%T%> as member of %qT",
5767 cp_parser_simulate_error (parser
);
5768 return error_mark_node
;
5773 A typedef-name that names a class shall not be used as the
5774 identifier in the declarator for a destructor declaration. */
5776 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5777 && !DECL_SELF_REFERENCE_P (type_decl
)
5778 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5779 error_at (token
->location
,
5780 "typedef-name %qD used as destructor declarator",
5783 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5787 if (token
->keyword
== RID_OPERATOR
)
5791 /* This could be a template-id, so we try that first. */
5792 cp_parser_parse_tentatively (parser
);
5793 /* Try a template-id. */
5794 id
= cp_parser_template_id (parser
, template_keyword_p
,
5795 /*check_dependency_p=*/true,
5798 /* If that worked, we're done. */
5799 if (cp_parser_parse_definitely (parser
))
5801 /* We still don't know whether we're looking at an
5802 operator-function-id or a conversion-function-id. */
5803 cp_parser_parse_tentatively (parser
);
5804 /* Try an operator-function-id. */
5805 id
= cp_parser_operator_function_id (parser
);
5806 /* If that didn't work, try a conversion-function-id. */
5807 if (!cp_parser_parse_definitely (parser
))
5808 id
= cp_parser_conversion_function_id (parser
);
5809 else if (UDLIT_OPER_P (id
))
5812 const char *name
= UDLIT_OP_SUFFIX (id
);
5813 if (name
[0] != '_' && !in_system_header_at (input_location
)
5815 warning (OPT_Wliteral_suffix
,
5816 "literal operator suffixes not preceded by %<_%>"
5817 " are reserved for future standardization");
5827 cp_parser_error (parser
, "expected unqualified-id");
5828 return error_mark_node
;
5832 /* Parse an (optional) nested-name-specifier.
5834 nested-name-specifier: [C++98]
5835 class-or-namespace-name :: nested-name-specifier [opt]
5836 class-or-namespace-name :: template nested-name-specifier [opt]
5838 nested-name-specifier: [C++0x]
5841 nested-name-specifier identifier ::
5842 nested-name-specifier template [opt] simple-template-id ::
5844 PARSER->SCOPE should be set appropriately before this function is
5845 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5846 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5849 Sets PARSER->SCOPE to the class (TYPE) or namespace
5850 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5851 it unchanged if there is no nested-name-specifier. Returns the new
5852 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5854 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5855 part of a declaration and/or decl-specifier. */
5858 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5859 bool typename_keyword_p
,
5860 bool check_dependency_p
,
5862 bool is_declaration
)
5864 bool success
= false;
5865 cp_token_position start
= 0;
5868 /* Remember where the nested-name-specifier starts. */
5869 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5871 start
= cp_lexer_token_position (parser
->lexer
, false);
5872 push_deferring_access_checks (dk_deferred
);
5879 tree saved_qualifying_scope
;
5880 bool template_keyword_p
;
5882 /* Spot cases that cannot be the beginning of a
5883 nested-name-specifier. */
5884 token
= cp_lexer_peek_token (parser
->lexer
);
5886 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5887 the already parsed nested-name-specifier. */
5888 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5890 /* Grab the nested-name-specifier and continue the loop. */
5891 cp_parser_pre_parsed_nested_name_specifier (parser
);
5892 /* If we originally encountered this nested-name-specifier
5893 with IS_DECLARATION set to false, we will not have
5894 resolved TYPENAME_TYPEs, so we must do so here. */
5896 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5898 new_scope
= resolve_typename_type (parser
->scope
,
5899 /*only_current_p=*/false);
5900 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5901 parser
->scope
= new_scope
;
5907 /* Spot cases that cannot be the beginning of a
5908 nested-name-specifier. On the second and subsequent times
5909 through the loop, we look for the `template' keyword. */
5910 if (success
&& token
->keyword
== RID_TEMPLATE
)
5912 /* A template-id can start a nested-name-specifier. */
5913 else if (token
->type
== CPP_TEMPLATE_ID
)
5915 /* DR 743: decltype can be used in a nested-name-specifier. */
5916 else if (token_is_decltype (token
))
5920 /* If the next token is not an identifier, then it is
5921 definitely not a type-name or namespace-name. */
5922 if (token
->type
!= CPP_NAME
)
5924 /* If the following token is neither a `<' (to begin a
5925 template-id), nor a `::', then we are not looking at a
5926 nested-name-specifier. */
5927 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5929 if (token
->type
== CPP_COLON
5930 && parser
->colon_corrects_to_scope_p
5931 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5933 error_at (token
->location
,
5934 "found %<:%> in nested-name-specifier, expected %<::%>");
5935 token
->type
= CPP_SCOPE
;
5938 if (token
->type
!= CPP_SCOPE
5939 && !cp_parser_nth_token_starts_template_argument_list_p
5944 /* The nested-name-specifier is optional, so we parse
5946 cp_parser_parse_tentatively (parser
);
5948 /* Look for the optional `template' keyword, if this isn't the
5949 first time through the loop. */
5951 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5953 template_keyword_p
= false;
5955 /* Save the old scope since the name lookup we are about to do
5956 might destroy it. */
5957 old_scope
= parser
->scope
;
5958 saved_qualifying_scope
= parser
->qualifying_scope
;
5959 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5960 look up names in "X<T>::I" in order to determine that "Y" is
5961 a template. So, if we have a typename at this point, we make
5962 an effort to look through it. */
5964 && !typename_keyword_p
5966 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5967 parser
->scope
= resolve_typename_type (parser
->scope
,
5968 /*only_current_p=*/false);
5969 /* Parse the qualifying entity. */
5971 = cp_parser_qualifying_entity (parser
,
5977 /* Look for the `::' token. */
5978 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5980 /* If we found what we wanted, we keep going; otherwise, we're
5982 if (!cp_parser_parse_definitely (parser
))
5984 bool error_p
= false;
5986 /* Restore the OLD_SCOPE since it was valid before the
5987 failed attempt at finding the last
5988 class-or-namespace-name. */
5989 parser
->scope
= old_scope
;
5990 parser
->qualifying_scope
= saved_qualifying_scope
;
5992 /* If the next token is a decltype, and the one after that is a
5993 `::', then the decltype has failed to resolve to a class or
5994 enumeration type. Give this error even when parsing
5995 tentatively since it can't possibly be valid--and we're going
5996 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5997 won't get another chance.*/
5998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5999 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6002 token
= cp_lexer_consume_token (parser
->lexer
);
6003 error_at (token
->location
, "decltype evaluates to %qT, "
6004 "which is not a class or enumeration type",
6005 token
->u
.tree_check_value
->value
);
6006 parser
->scope
= error_mark_node
;
6010 cp_lexer_consume_token (parser
->lexer
);
6013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6014 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6016 /* If we have a non-type template-id followed by ::, it can't
6017 possibly be valid. */
6018 token
= cp_lexer_peek_token (parser
->lexer
);
6019 tree tid
= token
->u
.tree_check_value
->value
;
6020 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6021 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6023 tree tmpl
= NULL_TREE
;
6024 if (is_overloaded_fn (tid
))
6026 tree fns
= get_fns (tid
);
6027 if (!OVL_CHAIN (fns
))
6028 tmpl
= OVL_CURRENT (fns
);
6029 error_at (token
->location
, "function template-id %qD "
6030 "in nested-name-specifier", tid
);
6034 /* Variable template. */
6035 tmpl
= TREE_OPERAND (tid
, 0);
6036 gcc_assert (variable_template_p (tmpl
));
6037 error_at (token
->location
, "variable template-id %qD "
6038 "in nested-name-specifier", tid
);
6041 inform (DECL_SOURCE_LOCATION (tmpl
),
6042 "%qD declared here", tmpl
);
6044 parser
->scope
= error_mark_node
;
6048 cp_lexer_consume_token (parser
->lexer
);
6049 cp_lexer_consume_token (parser
->lexer
);
6053 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6055 /* If the next token is an identifier, and the one after
6056 that is a `::', then any valid interpretation would have
6057 found a class-or-namespace-name. */
6058 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6059 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6061 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6064 token
= cp_lexer_consume_token (parser
->lexer
);
6067 if (!token
->error_reported
)
6070 tree ambiguous_decls
;
6072 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6074 /*is_template=*/false,
6075 /*is_namespace=*/false,
6076 /*check_dependency=*/true,
6079 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6080 error_at (token
->location
,
6081 "%qD used without template parameters",
6083 else if (ambiguous_decls
)
6085 // cp_parser_lookup_name has the same diagnostic,
6086 // thus make sure to emit it at most once.
6087 if (cp_parser_uncommitted_to_tentative_parse_p
6090 error_at (token
->location
,
6091 "reference to %qD is ambiguous",
6093 print_candidates (ambiguous_decls
);
6095 decl
= error_mark_node
;
6099 if (cxx_dialect
!= cxx98
)
6100 cp_parser_name_lookup_error
6101 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6104 cp_parser_name_lookup_error
6105 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6109 parser
->scope
= error_mark_node
;
6111 /* Treat this as a successful nested-name-specifier
6116 If the name found is not a class-name (clause
6117 _class_) or namespace-name (_namespace.def_), the
6118 program is ill-formed. */
6121 cp_lexer_consume_token (parser
->lexer
);
6125 /* We've found one valid nested-name-specifier. */
6127 /* Name lookup always gives us a DECL. */
6128 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6129 new_scope
= TREE_TYPE (new_scope
);
6130 /* Uses of "template" must be followed by actual templates. */
6131 if (template_keyword_p
6132 && !(CLASS_TYPE_P (new_scope
)
6133 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6134 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6135 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6136 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6137 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6138 == TEMPLATE_ID_EXPR
)))
6139 permerror (input_location
, TYPE_P (new_scope
)
6140 ? G_("%qT is not a template")
6141 : G_("%qD is not a template"),
6143 /* If it is a class scope, try to complete it; we are about to
6144 be looking up names inside the class. */
6145 if (TYPE_P (new_scope
)
6146 /* Since checking types for dependency can be expensive,
6147 avoid doing it if the type is already complete. */
6148 && !COMPLETE_TYPE_P (new_scope
)
6149 /* Do not try to complete dependent types. */
6150 && !dependent_type_p (new_scope
))
6152 new_scope
= complete_type (new_scope
);
6153 /* If it is a typedef to current class, use the current
6154 class instead, as the typedef won't have any names inside
6156 if (!COMPLETE_TYPE_P (new_scope
)
6157 && currently_open_class (new_scope
))
6158 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6160 /* Make sure we look in the right scope the next time through
6162 parser
->scope
= new_scope
;
6165 /* If parsing tentatively, replace the sequence of tokens that makes
6166 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6167 token. That way, should we re-parse the token stream, we will
6168 not have to repeat the effort required to do the parse, nor will
6169 we issue duplicate error messages. */
6170 if (success
&& start
)
6174 token
= cp_lexer_token_at (parser
->lexer
, start
);
6175 /* Reset the contents of the START token. */
6176 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6177 /* Retrieve any deferred checks. Do not pop this access checks yet
6178 so the memory will not be reclaimed during token replacing below. */
6179 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6180 token
->u
.tree_check_value
->value
= parser
->scope
;
6181 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6182 token
->u
.tree_check_value
->qualifying_scope
=
6183 parser
->qualifying_scope
;
6184 token
->keyword
= RID_MAX
;
6186 /* Purge all subsequent tokens. */
6187 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6191 pop_to_parent_deferring_access_checks ();
6193 return success
? parser
->scope
: NULL_TREE
;
6196 /* Parse a nested-name-specifier. See
6197 cp_parser_nested_name_specifier_opt for details. This function
6198 behaves identically, except that it will an issue an error if no
6199 nested-name-specifier is present. */
6202 cp_parser_nested_name_specifier (cp_parser
*parser
,
6203 bool typename_keyword_p
,
6204 bool check_dependency_p
,
6206 bool is_declaration
)
6210 /* Look for the nested-name-specifier. */
6211 scope
= cp_parser_nested_name_specifier_opt (parser
,
6216 /* If it was not present, issue an error message. */
6219 cp_parser_error (parser
, "expected nested-name-specifier");
6220 parser
->scope
= NULL_TREE
;
6226 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6227 this is either a class-name or a namespace-name (which corresponds
6228 to the class-or-namespace-name production in the grammar). For
6229 C++0x, it can also be a type-name that refers to an enumeration
6230 type or a simple-template-id.
6232 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6233 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6234 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6235 TYPE_P is TRUE iff the next name should be taken as a class-name,
6236 even the same name is declared to be another entity in the same
6239 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6240 specified by the class-or-namespace-name. If neither is found the
6241 ERROR_MARK_NODE is returned. */
6244 cp_parser_qualifying_entity (cp_parser
*parser
,
6245 bool typename_keyword_p
,
6246 bool template_keyword_p
,
6247 bool check_dependency_p
,
6249 bool is_declaration
)
6252 tree saved_qualifying_scope
;
6253 tree saved_object_scope
;
6256 bool successful_parse_p
;
6258 /* DR 743: decltype can appear in a nested-name-specifier. */
6259 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6261 scope
= cp_parser_decltype (parser
);
6262 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6263 && !MAYBE_CLASS_TYPE_P (scope
))
6265 cp_parser_simulate_error (parser
);
6266 return error_mark_node
;
6268 if (TYPE_NAME (scope
))
6269 scope
= TYPE_NAME (scope
);
6273 /* Before we try to parse the class-name, we must save away the
6274 current PARSER->SCOPE since cp_parser_class_name will destroy
6276 saved_scope
= parser
->scope
;
6277 saved_qualifying_scope
= parser
->qualifying_scope
;
6278 saved_object_scope
= parser
->object_scope
;
6279 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6280 there is no need to look for a namespace-name. */
6281 only_class_p
= template_keyword_p
6282 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6284 cp_parser_parse_tentatively (parser
);
6285 scope
= cp_parser_class_name (parser
,
6288 type_p
? class_type
: none_type
,
6290 /*class_head_p=*/false,
6292 /*enum_ok=*/cxx_dialect
> cxx98
);
6293 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6294 /* If that didn't work, try for a namespace-name. */
6295 if (!only_class_p
&& !successful_parse_p
)
6297 /* Restore the saved scope. */
6298 parser
->scope
= saved_scope
;
6299 parser
->qualifying_scope
= saved_qualifying_scope
;
6300 parser
->object_scope
= saved_object_scope
;
6301 /* If we are not looking at an identifier followed by the scope
6302 resolution operator, then this is not part of a
6303 nested-name-specifier. (Note that this function is only used
6304 to parse the components of a nested-name-specifier.) */
6305 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6306 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6307 return error_mark_node
;
6308 scope
= cp_parser_namespace_name (parser
);
6314 /* Return true if we are looking at a compound-literal, false otherwise. */
6317 cp_parser_compound_literal_p (cp_parser
*parser
)
6319 /* Consume the `('. */
6320 cp_lexer_consume_token (parser
->lexer
);
6322 cp_lexer_save_tokens (parser
->lexer
);
6324 /* Skip tokens until the next token is a closing parenthesis.
6325 If we find the closing `)', and the next token is a `{', then
6326 we are looking at a compound-literal. */
6327 bool compound_literal_p
6328 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6329 /*consume_paren=*/true)
6330 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6332 /* Roll back the tokens we skipped. */
6333 cp_lexer_rollback_tokens (parser
->lexer
);
6335 return compound_literal_p
;
6338 /* Parse a postfix-expression.
6342 postfix-expression [ expression ]
6343 postfix-expression ( expression-list [opt] )
6344 simple-type-specifier ( expression-list [opt] )
6345 typename :: [opt] nested-name-specifier identifier
6346 ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier template [opt] template-id
6348 ( expression-list [opt] )
6349 postfix-expression . template [opt] id-expression
6350 postfix-expression -> template [opt] id-expression
6351 postfix-expression . pseudo-destructor-name
6352 postfix-expression -> pseudo-destructor-name
6353 postfix-expression ++
6354 postfix-expression --
6355 dynamic_cast < type-id > ( expression )
6356 static_cast < type-id > ( expression )
6357 reinterpret_cast < type-id > ( expression )
6358 const_cast < type-id > ( expression )
6359 typeid ( expression )
6365 ( type-id ) { initializer-list , [opt] }
6367 This extension is a GNU version of the C99 compound-literal
6368 construct. (The C99 grammar uses `type-name' instead of `type-id',
6369 but they are essentially the same concept.)
6371 If ADDRESS_P is true, the postfix expression is the operand of the
6372 `&' operator. CAST_P is true if this expression is the target of a
6375 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6376 class member access expressions [expr.ref].
6378 Returns a representation of the expression. */
6381 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6382 bool member_access_only_p
, bool decltype_p
,
6383 cp_id_kind
* pidk_return
)
6388 cp_id_kind idk
= CP_ID_KIND_NONE
;
6389 cp_expr postfix_expression
= NULL_TREE
;
6390 bool is_member_access
= false;
6391 int saved_in_statement
= -1;
6393 /* Peek at the next token. */
6394 token
= cp_lexer_peek_token (parser
->lexer
);
6395 loc
= token
->location
;
6396 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6398 /* Some of the productions are determined by keywords. */
6399 keyword
= token
->keyword
;
6409 const char *saved_message
;
6410 bool saved_in_type_id_in_expr_p
;
6412 /* All of these can be handled in the same way from the point
6413 of view of parsing. Begin by consuming the token
6414 identifying the cast. */
6415 cp_lexer_consume_token (parser
->lexer
);
6417 /* New types cannot be defined in the cast. */
6418 saved_message
= parser
->type_definition_forbidden_message
;
6419 parser
->type_definition_forbidden_message
6420 = G_("types may not be defined in casts");
6422 /* Look for the opening `<'. */
6423 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6424 /* Parse the type to which we are casting. */
6425 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6426 parser
->in_type_id_in_expr_p
= true;
6427 type
= cp_parser_type_id (parser
);
6428 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6429 /* Look for the closing `>'. */
6430 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6431 /* Restore the old message. */
6432 parser
->type_definition_forbidden_message
= saved_message
;
6434 bool saved_greater_than_is_operator_p
6435 = parser
->greater_than_is_operator_p
;
6436 parser
->greater_than_is_operator_p
= true;
6438 /* And the expression which is being cast. */
6439 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6440 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6441 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6443 location_t end_loc
= close_paren
?
6444 close_paren
->location
: UNKNOWN_LOCATION
;
6446 parser
->greater_than_is_operator_p
6447 = saved_greater_than_is_operator_p
;
6449 /* Only type conversions to integral or enumeration types
6450 can be used in constant-expressions. */
6451 if (!cast_valid_in_integral_constant_expression_p (type
)
6452 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6454 postfix_expression
= error_mark_node
;
6462 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6466 = build_static_cast (type
, expression
, tf_warning_or_error
);
6470 = build_reinterpret_cast (type
, expression
,
6471 tf_warning_or_error
);
6475 = build_const_cast (type
, expression
, tf_warning_or_error
);
6481 /* Construct a location e.g. :
6482 reinterpret_cast <int *> (expr)
6483 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6484 ranging from the start of the "*_cast" token to the final closing
6485 paren, with the caret at the start. */
6486 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6487 postfix_expression
.set_location (cp_cast_loc
);
6494 const char *saved_message
;
6495 bool saved_in_type_id_in_expr_p
;
6497 /* Consume the `typeid' token. */
6498 cp_lexer_consume_token (parser
->lexer
);
6499 /* Look for the `(' token. */
6500 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6501 /* Types cannot be defined in a `typeid' expression. */
6502 saved_message
= parser
->type_definition_forbidden_message
;
6503 parser
->type_definition_forbidden_message
6504 = G_("types may not be defined in a %<typeid%> expression");
6505 /* We can't be sure yet whether we're looking at a type-id or an
6507 cp_parser_parse_tentatively (parser
);
6508 /* Try a type-id first. */
6509 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6510 parser
->in_type_id_in_expr_p
= true;
6511 type
= cp_parser_type_id (parser
);
6512 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6513 /* Look for the `)' token. Otherwise, we can't be sure that
6514 we're not looking at an expression: consider `typeid (int
6515 (3))', for example. */
6516 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6517 /* If all went well, simply lookup the type-id. */
6518 if (cp_parser_parse_definitely (parser
))
6519 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6520 /* Otherwise, fall back to the expression variant. */
6525 /* Look for an expression. */
6526 expression
= cp_parser_expression (parser
, & idk
);
6527 /* Compute its typeid. */
6528 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6529 /* Look for the `)' token. */
6530 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6532 /* Restore the saved message. */
6533 parser
->type_definition_forbidden_message
= saved_message
;
6534 /* `typeid' may not appear in an integral constant expression. */
6535 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6536 postfix_expression
= error_mark_node
;
6543 /* The syntax permitted here is the same permitted for an
6544 elaborated-type-specifier. */
6545 ++parser
->prevent_constrained_type_specifiers
;
6546 type
= cp_parser_elaborated_type_specifier (parser
,
6547 /*is_friend=*/false,
6548 /*is_declaration=*/false);
6549 --parser
->prevent_constrained_type_specifiers
;
6550 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6554 case RID_CILK_SPAWN
:
6556 location_t cilk_spawn_loc
6557 = cp_lexer_peek_token (parser
->lexer
)->location
;
6558 cp_lexer_consume_token (parser
->lexer
);
6559 token
= cp_lexer_peek_token (parser
->lexer
);
6560 if (token
->type
== CPP_SEMICOLON
)
6562 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6564 postfix_expression
= error_mark_node
;
6567 else if (!current_function_decl
)
6569 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6570 "inside a function");
6571 postfix_expression
= error_mark_node
;
6576 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6577 saved_in_statement
= parser
->in_statement
;
6578 parser
->in_statement
|= IN_CILK_SPAWN
;
6580 cfun
->calls_cilk_spawn
= 1;
6581 postfix_expression
=
6582 cp_parser_postfix_expression (parser
, false, false,
6583 false, false, &idk
);
6586 error_at (token
->location
, "-fcilkplus must be enabled to use"
6587 " %<_Cilk_spawn%>");
6588 cfun
->calls_cilk_spawn
= 0;
6590 else if (saved_in_statement
& IN_CILK_SPAWN
)
6592 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6593 "are not permitted");
6594 postfix_expression
= error_mark_node
;
6595 cfun
->calls_cilk_spawn
= 0;
6599 location_t loc
= postfix_expression
.get_location ();
6600 postfix_expression
= build_cilk_spawn (token
->location
,
6601 postfix_expression
);
6602 /* Build a location of the form:
6605 with caret at the expr, ranging from the start of the
6606 _Cilk_spawn token to the end of the expression. */
6607 location_t combined_loc
=
6608 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6609 postfix_expression
.set_location (combined_loc
);
6610 if (postfix_expression
!= error_mark_node
)
6611 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6612 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6618 case RID_BUILTIN_SHUFFLE
:
6619 case RID_BUILTIN_LAUNDER
:
6621 vec
<tree
, va_gc
> *vec
;
6625 cp_lexer_consume_token (parser
->lexer
);
6626 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6627 /*cast_p=*/false, /*allow_expansion_p=*/true,
6628 /*non_constant_p=*/NULL
);
6631 postfix_expression
= error_mark_node
;
6635 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6641 if (vec
->length () == 1)
6643 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6646 error_at (loc
, "wrong number of arguments to "
6647 "%<__builtin_addressof%>");
6648 postfix_expression
= error_mark_node
;
6652 case RID_BUILTIN_LAUNDER
:
6653 if (vec
->length () == 1)
6654 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6655 tf_warning_or_error
);
6658 error_at (loc
, "wrong number of arguments to "
6659 "%<__builtin_launder%>");
6660 postfix_expression
= error_mark_node
;
6664 case RID_BUILTIN_SHUFFLE
:
6665 if (vec
->length () == 2)
6667 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6668 (*vec
)[1], tf_warning_or_error
);
6669 else if (vec
->length () == 3)
6671 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6672 (*vec
)[2], tf_warning_or_error
);
6675 error_at (loc
, "wrong number of arguments to "
6676 "%<__builtin_shuffle%>");
6677 postfix_expression
= error_mark_node
;
6691 /* If the next thing is a simple-type-specifier, we may be
6692 looking at a functional cast. We could also be looking at
6693 an id-expression. So, we try the functional cast, and if
6694 that doesn't work we fall back to the primary-expression. */
6695 cp_parser_parse_tentatively (parser
);
6696 /* Look for the simple-type-specifier. */
6697 ++parser
->prevent_constrained_type_specifiers
;
6698 type
= cp_parser_simple_type_specifier (parser
,
6699 /*decl_specs=*/NULL
,
6700 CP_PARSER_FLAGS_NONE
);
6701 --parser
->prevent_constrained_type_specifiers
;
6702 /* Parse the cast itself. */
6703 if (!cp_parser_error_occurred (parser
))
6705 = cp_parser_functional_cast (parser
, type
);
6706 /* If that worked, we're done. */
6707 if (cp_parser_parse_definitely (parser
))
6710 /* If the functional-cast didn't work out, try a
6711 compound-literal. */
6712 if (cp_parser_allow_gnu_extensions_p (parser
)
6713 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6715 cp_expr initializer
= NULL_TREE
;
6717 cp_parser_parse_tentatively (parser
);
6719 /* Avoid calling cp_parser_type_id pointlessly, see comment
6720 in cp_parser_cast_expression about c++/29234. */
6721 if (!cp_parser_compound_literal_p (parser
))
6722 cp_parser_simulate_error (parser
);
6725 /* Parse the type. */
6726 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6727 parser
->in_type_id_in_expr_p
= true;
6728 type
= cp_parser_type_id (parser
);
6729 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6730 /* Look for the `)'. */
6731 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6734 /* If things aren't going well, there's no need to
6736 if (!cp_parser_error_occurred (parser
))
6738 bool non_constant_p
;
6739 /* Parse the brace-enclosed initializer list. */
6740 initializer
= cp_parser_braced_list (parser
,
6743 /* If that worked, we're definitely looking at a
6744 compound-literal expression. */
6745 if (cp_parser_parse_definitely (parser
))
6747 /* Warn the user that a compound literal is not
6748 allowed in standard C++. */
6749 pedwarn (input_location
, OPT_Wpedantic
,
6750 "ISO C++ forbids compound-literals");
6751 /* For simplicity, we disallow compound literals in
6752 constant-expressions. We could
6753 allow compound literals of integer type, whose
6754 initializer was a constant, in constant
6755 expressions. Permitting that usage, as a further
6756 extension, would not change the meaning of any
6757 currently accepted programs. (Of course, as
6758 compound literals are not part of ISO C++, the
6759 standard has nothing to say.) */
6760 if (cp_parser_non_integral_constant_expression (parser
,
6763 postfix_expression
= error_mark_node
;
6766 /* Form the representation of the compound-literal. */
6768 = finish_compound_literal (type
, initializer
,
6769 tf_warning_or_error
);
6770 postfix_expression
.set_location (initializer
.get_location ());
6775 /* It must be a primary-expression. */
6777 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6778 /*template_arg_p=*/false,
6785 /* Note that we don't need to worry about calling build_cplus_new on a
6786 class-valued CALL_EXPR in decltype when it isn't the end of the
6787 postfix-expression; unary_complex_lvalue will take care of that for
6790 /* Keep looping until the postfix-expression is complete. */
6793 if (idk
== CP_ID_KIND_UNQUALIFIED
6794 && identifier_p (postfix_expression
)
6795 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6796 /* It is not a Koenig lookup function call. */
6798 = unqualified_name_lookup_error (postfix_expression
);
6800 /* Peek at the next token. */
6801 token
= cp_lexer_peek_token (parser
->lexer
);
6803 switch (token
->type
)
6805 case CPP_OPEN_SQUARE
:
6806 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6808 cp_parser_error (parser
,
6809 "two consecutive %<[%> shall "
6810 "only introduce an attribute");
6811 return error_mark_node
;
6814 = cp_parser_postfix_open_square_expression (parser
,
6818 postfix_expression
.set_range (start_loc
,
6819 postfix_expression
.get_location ());
6821 idk
= CP_ID_KIND_NONE
;
6822 is_member_access
= false;
6825 case CPP_OPEN_PAREN
:
6826 /* postfix-expression ( expression-list [opt] ) */
6829 bool is_builtin_constant_p
;
6830 bool saved_integral_constant_expression_p
= false;
6831 bool saved_non_integral_constant_expression_p
= false;
6832 tsubst_flags_t complain
= complain_flags (decltype_p
);
6833 vec
<tree
, va_gc
> *args
;
6834 location_t close_paren_loc
= UNKNOWN_LOCATION
;
6836 is_member_access
= false;
6838 is_builtin_constant_p
6839 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6840 if (is_builtin_constant_p
)
6842 /* The whole point of __builtin_constant_p is to allow
6843 non-constant expressions to appear as arguments. */
6844 saved_integral_constant_expression_p
6845 = parser
->integral_constant_expression_p
;
6846 saved_non_integral_constant_expression_p
6847 = parser
->non_integral_constant_expression_p
;
6848 parser
->integral_constant_expression_p
= false;
6850 args
= (cp_parser_parenthesized_expression_list
6852 /*cast_p=*/false, /*allow_expansion_p=*/true,
6853 /*non_constant_p=*/NULL
,
6854 /*close_paren_loc=*/&close_paren_loc
));
6855 if (is_builtin_constant_p
)
6857 parser
->integral_constant_expression_p
6858 = saved_integral_constant_expression_p
;
6859 parser
->non_integral_constant_expression_p
6860 = saved_non_integral_constant_expression_p
;
6865 postfix_expression
= error_mark_node
;
6869 /* Function calls are not permitted in
6870 constant-expressions. */
6871 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6872 && cp_parser_non_integral_constant_expression (parser
,
6875 postfix_expression
= error_mark_node
;
6876 release_tree_vector (args
);
6881 if (idk
== CP_ID_KIND_UNQUALIFIED
6882 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6884 if (identifier_p (postfix_expression
))
6886 if (!args
->is_empty ())
6889 if (!any_type_dependent_arguments_p (args
))
6891 = perform_koenig_lookup (postfix_expression
, args
,
6896 = unqualified_fn_lookup_error (postfix_expression
);
6898 /* We do not perform argument-dependent lookup if
6899 normal lookup finds a non-function, in accordance
6900 with the expected resolution of DR 218. */
6901 else if (!args
->is_empty ()
6902 && is_overloaded_fn (postfix_expression
))
6904 tree fn
= get_first_fn (postfix_expression
);
6905 fn
= STRIP_TEMPLATE (fn
);
6907 /* Do not do argument dependent lookup if regular
6908 lookup finds a member function or a block-scope
6909 function declaration. [basic.lookup.argdep]/3 */
6910 if (!DECL_FUNCTION_MEMBER_P (fn
)
6911 && !DECL_LOCAL_FUNCTION_P (fn
))
6914 if (!any_type_dependent_arguments_p (args
))
6916 = perform_koenig_lookup (postfix_expression
, args
,
6922 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6923 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6924 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6925 && vec_safe_length (args
) == 3)
6927 tree arg0
= (*args
)[0];
6928 tree arg1
= (*args
)[1];
6929 tree arg2
= (*args
)[2];
6930 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
6931 | (!!integer_zerop (arg2
) << 2));
6932 if (TREE_CODE (arg2
) == CONST_DECL
)
6933 arg2
= DECL_INITIAL (arg2
);
6934 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
6937 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6939 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6940 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6942 if (processing_template_decl
6943 && (type_dependent_object_expression_p (instance
)
6944 || (!BASELINK_P (fn
)
6945 && TREE_CODE (fn
) != FIELD_DECL
)
6946 || type_dependent_expression_p (fn
)
6947 || any_type_dependent_arguments_p (args
)))
6949 maybe_generic_this_capture (instance
, fn
);
6951 = build_nt_call_vec (postfix_expression
, args
);
6952 release_tree_vector (args
);
6956 if (BASELINK_P (fn
))
6959 = (build_new_method_call
6960 (instance
, fn
, &args
, NULL_TREE
,
6961 (idk
== CP_ID_KIND_QUALIFIED
6962 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6969 = finish_call_expr (postfix_expression
, &args
,
6970 /*disallow_virtual=*/false,
6974 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6975 || TREE_CODE (postfix_expression
) == MEMBER_REF
6976 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6977 postfix_expression
= (build_offset_ref_call_from_tree
6978 (postfix_expression
, &args
,
6980 else if (idk
== CP_ID_KIND_QUALIFIED
)
6981 /* A call to a static class member, or a namespace-scope
6984 = finish_call_expr (postfix_expression
, &args
,
6985 /*disallow_virtual=*/true,
6989 /* All other function calls. */
6991 = finish_call_expr (postfix_expression
, &args
,
6992 /*disallow_virtual=*/false,
6996 if (close_paren_loc
!= UNKNOWN_LOCATION
)
6998 location_t combined_loc
= make_location (token
->location
,
7001 postfix_expression
.set_location (combined_loc
);
7004 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7005 idk
= CP_ID_KIND_NONE
;
7007 release_tree_vector (args
);
7013 /* postfix-expression . template [opt] id-expression
7014 postfix-expression . pseudo-destructor-name
7015 postfix-expression -> template [opt] id-expression
7016 postfix-expression -> pseudo-destructor-name */
7018 /* Consume the `.' or `->' operator. */
7019 cp_lexer_consume_token (parser
->lexer
);
7022 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7026 is_member_access
= true;
7030 /* postfix-expression ++ */
7031 /* Consume the `++' token. */
7032 cp_lexer_consume_token (parser
->lexer
);
7033 /* Generate a representation for the complete expression. */
7035 = finish_increment_expr (postfix_expression
,
7036 POSTINCREMENT_EXPR
);
7037 /* Increments may not appear in constant-expressions. */
7038 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7039 postfix_expression
= error_mark_node
;
7040 idk
= CP_ID_KIND_NONE
;
7041 is_member_access
= false;
7044 case CPP_MINUS_MINUS
:
7045 /* postfix-expression -- */
7046 /* Consume the `--' token. */
7047 cp_lexer_consume_token (parser
->lexer
);
7048 /* Generate a representation for the complete expression. */
7050 = finish_increment_expr (postfix_expression
,
7051 POSTDECREMENT_EXPR
);
7052 /* Decrements may not appear in constant-expressions. */
7053 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7054 postfix_expression
= error_mark_node
;
7055 idk
= CP_ID_KIND_NONE
;
7056 is_member_access
= false;
7060 if (pidk_return
!= NULL
)
7061 * pidk_return
= idk
;
7062 if (member_access_only_p
)
7063 return is_member_access
7064 ? postfix_expression
7065 : cp_expr (error_mark_node
);
7067 return postfix_expression
;
7071 /* We should never get here. */
7073 return error_mark_node
;
7076 /* This function parses Cilk Plus array notations. If a normal array expr. is
7077 parsed then the array index is passed back to the caller through *INIT_INDEX
7078 and the function returns a NULL_TREE. If array notation expr. is parsed,
7079 then *INIT_INDEX is ignored by the caller and the function returns
7080 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7084 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
7087 cp_token
*token
= NULL
;
7088 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
7089 if (!array_value
|| array_value
== error_mark_node
)
7091 cp_parser_skip_to_end_of_statement (parser
);
7092 return error_mark_node
;
7095 array_type
= TREE_TYPE (array_value
);
7097 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7098 parser
->colon_corrects_to_scope_p
= false;
7099 token
= cp_lexer_peek_token (parser
->lexer
);
7103 cp_parser_error (parser
, "expected %<:%> or numeral");
7104 return error_mark_node
;
7106 else if (token
->type
== CPP_COLON
)
7108 /* Consume the ':'. */
7109 cp_lexer_consume_token (parser
->lexer
);
7111 /* If we are here, then we have a case like this A[:]. */
7112 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7114 cp_parser_error (parser
, "expected %<]%>");
7115 cp_parser_skip_to_end_of_statement (parser
);
7116 return error_mark_node
;
7118 *init_index
= NULL_TREE
;
7120 length_index
= NULL_TREE
;
7124 /* If we are here, then there are three valid possibilities:
7126 2. ARRAY [ EXP : EXP ]
7127 3. ARRAY [ EXP : EXP : EXP ] */
7129 *init_index
= cp_parser_expression (parser
);
7130 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7132 /* This indicates that we have a normal array expression. */
7133 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7137 /* Consume the ':'. */
7138 cp_lexer_consume_token (parser
->lexer
);
7139 length_index
= cp_parser_expression (parser
);
7140 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7142 cp_lexer_consume_token (parser
->lexer
);
7143 stride
= cp_parser_expression (parser
);
7146 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7148 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7149 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7151 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7152 cp_lexer_consume_token (parser
->lexer
);
7153 return error_mark_node
;
7155 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7157 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7158 length_index
, stride
, array_type
);
7162 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7163 by cp_parser_builtin_offsetof. We're looking for
7165 postfix-expression [ expression ]
7166 postfix-expression [ braced-init-list ] (C++11)
7168 FOR_OFFSETOF is set if we're being called in that context, which
7169 changes how we deal with integer constant expressions. */
7172 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7173 tree postfix_expression
,
7177 tree index
= NULL_TREE
;
7178 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7179 bool saved_greater_than_is_operator_p
;
7181 /* Consume the `[' token. */
7182 cp_lexer_consume_token (parser
->lexer
);
7184 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7185 parser
->greater_than_is_operator_p
= true;
7187 /* Parse the index expression. */
7188 /* ??? For offsetof, there is a question of what to allow here. If
7189 offsetof is not being used in an integral constant expression context,
7190 then we *could* get the right answer by computing the value at runtime.
7191 If we are in an integral constant expression context, then we might
7192 could accept any constant expression; hard to say without analysis.
7193 Rather than open the barn door too wide right away, allow only integer
7194 constant expressions here. */
7196 index
= cp_parser_constant_expression (parser
);
7199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7201 bool expr_nonconst_p
;
7202 cp_lexer_set_source_position (parser
->lexer
);
7203 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7204 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7206 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7208 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7209 "braced list index is not allowed with array "
7211 cp_parser_skip_to_end_of_statement (parser
);
7212 return error_mark_node
;
7215 else if (flag_cilkplus
)
7217 /* Here are have these two options:
7218 ARRAY[EXP : EXP] - Array notation expr with default
7220 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7222 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7223 postfix_expression
);
7228 index
= cp_parser_expression (parser
);
7231 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7233 /* Look for the closing `]'. */
7234 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7236 /* Build the ARRAY_REF. */
7237 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7240 /* When not doing offsetof, array references are not permitted in
7241 constant-expressions. */
7243 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7244 postfix_expression
= error_mark_node
;
7246 return postfix_expression
;
7249 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7250 by cp_parser_builtin_offsetof. We're looking for
7252 postfix-expression . template [opt] id-expression
7253 postfix-expression . pseudo-destructor-name
7254 postfix-expression -> template [opt] id-expression
7255 postfix-expression -> pseudo-destructor-name
7257 FOR_OFFSETOF is set if we're being called in that context. That sorta
7258 limits what of the above we'll actually accept, but nevermind.
7259 TOKEN_TYPE is the "." or "->" token, which will already have been
7260 removed from the stream. */
7263 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7264 enum cpp_ttype token_type
,
7265 cp_expr postfix_expression
,
7266 bool for_offsetof
, cp_id_kind
*idk
,
7267 location_t location
)
7271 bool pseudo_destructor_p
;
7272 tree scope
= NULL_TREE
;
7273 location_t start_loc
= postfix_expression
.get_start ();
7275 /* If this is a `->' operator, dereference the pointer. */
7276 if (token_type
== CPP_DEREF
)
7277 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7278 tf_warning_or_error
);
7279 /* Check to see whether or not the expression is type-dependent and
7280 not the current instantiation. */
7281 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7282 /* The identifier following the `->' or `.' is not qualified. */
7283 parser
->scope
= NULL_TREE
;
7284 parser
->qualifying_scope
= NULL_TREE
;
7285 parser
->object_scope
= NULL_TREE
;
7286 *idk
= CP_ID_KIND_NONE
;
7288 /* Enter the scope corresponding to the type of the object
7289 given by the POSTFIX_EXPRESSION. */
7292 scope
= TREE_TYPE (postfix_expression
);
7293 /* According to the standard, no expression should ever have
7294 reference type. Unfortunately, we do not currently match
7295 the standard in this respect in that our internal representation
7296 of an expression may have reference type even when the standard
7297 says it does not. Therefore, we have to manually obtain the
7298 underlying type here. */
7299 scope
= non_reference (scope
);
7300 /* The type of the POSTFIX_EXPRESSION must be complete. */
7301 /* Unlike the object expression in other contexts, *this is not
7302 required to be of complete type for purposes of class member
7303 access (5.2.5) outside the member function body. */
7304 if (postfix_expression
!= current_class_ref
7305 && scope
!= error_mark_node
7306 && !(processing_template_decl
7307 && current_class_type
7308 && (same_type_ignoring_top_level_qualifiers_p
7309 (scope
, current_class_type
))))
7311 scope
= complete_type (scope
);
7312 if (!COMPLETE_TYPE_P (scope
)
7313 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7314 && EXPR_P (postfix_expression
))
7316 /* In a template, be permissive by treating an object expression
7317 of incomplete type as dependent (after a pedwarn). */
7318 diagnostic_t kind
= (processing_template_decl
7321 cxx_incomplete_type_diagnostic
7322 (location_of (postfix_expression
),
7323 postfix_expression
, scope
, kind
);
7324 if (processing_template_decl
)
7327 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7334 /* Let the name lookup machinery know that we are processing a
7335 class member access expression. */
7336 parser
->context
->object_type
= scope
;
7337 /* If something went wrong, we want to be able to discern that case,
7338 as opposed to the case where there was no SCOPE due to the type
7339 of expression being dependent. */
7341 scope
= error_mark_node
;
7342 /* If the SCOPE was erroneous, make the various semantic analysis
7343 functions exit quickly -- and without issuing additional error
7345 if (scope
== error_mark_node
)
7346 postfix_expression
= error_mark_node
;
7351 /* Tell cp_parser_lookup_name that there was an object, even though it's
7353 parser
->context
->object_type
= unknown_type_node
;
7355 /* Assume this expression is not a pseudo-destructor access. */
7356 pseudo_destructor_p
= false;
7358 /* If the SCOPE is a scalar type, then, if this is a valid program,
7359 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7360 is type dependent, it can be pseudo-destructor-name or something else.
7361 Try to parse it as pseudo-destructor-name first. */
7362 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7367 cp_parser_parse_tentatively (parser
);
7368 /* Parse the pseudo-destructor-name. */
7370 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7373 && (cp_parser_error_occurred (parser
)
7374 || !SCALAR_TYPE_P (type
)))
7375 cp_parser_abort_tentative_parse (parser
);
7376 else if (cp_parser_parse_definitely (parser
))
7378 pseudo_destructor_p
= true;
7380 = finish_pseudo_destructor_expr (postfix_expression
,
7385 if (!pseudo_destructor_p
)
7387 /* If the SCOPE is not a scalar type, we are looking at an
7388 ordinary class member access expression, rather than a
7389 pseudo-destructor-name. */
7391 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7392 /* Parse the id-expression. */
7393 name
= (cp_parser_id_expression
7395 cp_parser_optional_template_keyword (parser
),
7396 /*check_dependency_p=*/true,
7398 /*declarator_p=*/false,
7399 /*optional_p=*/false));
7400 /* In general, build a SCOPE_REF if the member name is qualified.
7401 However, if the name was not dependent and has already been
7402 resolved; there is no need to build the SCOPE_REF. For example;
7404 struct X { void f(); };
7405 template <typename T> void f(T* t) { t->X::f(); }
7407 Even though "t" is dependent, "X::f" is not and has been resolved
7408 to a BASELINK; there is no need to include scope information. */
7410 /* But we do need to remember that there was an explicit scope for
7411 virtual function calls. */
7413 *idk
= CP_ID_KIND_QUALIFIED
;
7415 /* If the name is a template-id that names a type, we will get a
7416 TYPE_DECL here. That is invalid code. */
7417 if (TREE_CODE (name
) == TYPE_DECL
)
7419 error_at (token
->location
, "invalid use of %qD", name
);
7420 postfix_expression
= error_mark_node
;
7424 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7426 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7428 error_at (token
->location
, "%<%D::%D%> is not a class member",
7429 parser
->scope
, name
);
7430 postfix_expression
= error_mark_node
;
7433 name
= build_qualified_name (/*type=*/NULL_TREE
,
7437 parser
->scope
= NULL_TREE
;
7438 parser
->qualifying_scope
= NULL_TREE
;
7439 parser
->object_scope
= NULL_TREE
;
7441 if (parser
->scope
&& name
&& BASELINK_P (name
))
7442 adjust_result_of_qualified_name_lookup
7443 (name
, parser
->scope
, scope
);
7445 = finish_class_member_access_expr (postfix_expression
, name
,
7447 tf_warning_or_error
);
7448 /* Build a location e.g.:
7451 where the caret is at the deref token, ranging from
7452 the start of postfix_expression to the end of the access expr. */
7454 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7455 location_t combined_loc
7456 = make_location (input_location
, start_loc
, end_loc
);
7457 protected_set_expr_location (postfix_expression
, combined_loc
);
7461 /* We no longer need to look up names in the scope of the object on
7462 the left-hand side of the `.' or `->' operator. */
7463 parser
->context
->object_type
= NULL_TREE
;
7465 /* Outside of offsetof, these operators may not appear in
7466 constant-expressions. */
7468 && (cp_parser_non_integral_constant_expression
7469 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7470 postfix_expression
= error_mark_node
;
7472 return postfix_expression
;
7475 /* Parse a parenthesized expression-list.
7478 assignment-expression
7479 expression-list, assignment-expression
7484 identifier, expression-list
7486 CAST_P is true if this expression is the target of a cast.
7488 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7491 Returns a vector of trees. Each element is a representation of an
7492 assignment-expression. NULL is returned if the ( and or ) are
7493 missing. An empty, but allocated, vector is returned on no
7494 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7495 if we are parsing an attribute list for an attribute that wants a
7496 plain identifier argument, normal_attr for an attribute that wants
7497 an expression, or non_attr if we aren't parsing an attribute list. If
7498 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7499 not all of the expressions in the list were constant.
7500 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7501 will be written to with the location of the closing parenthesis. If
7502 an error occurs, it may or may not be written to. */
7504 static vec
<tree
, va_gc
> *
7505 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7506 int is_attribute_list
,
7508 bool allow_expansion_p
,
7509 bool *non_constant_p
,
7510 location_t
*close_paren_loc
)
7512 vec
<tree
, va_gc
> *expression_list
;
7513 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7514 tree identifier
= NULL_TREE
;
7515 bool saved_greater_than_is_operator_p
;
7517 /* Assume all the expressions will be constant. */
7519 *non_constant_p
= false;
7521 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7524 expression_list
= make_tree_vector ();
7526 /* Within a parenthesized expression, a `>' token is always
7527 the greater-than operator. */
7528 saved_greater_than_is_operator_p
7529 = parser
->greater_than_is_operator_p
;
7530 parser
->greater_than_is_operator_p
= true;
7532 /* Consume expressions until there are no more. */
7533 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7538 /* At the beginning of attribute lists, check to see if the
7539 next token is an identifier. */
7540 if (is_attribute_list
== id_attr
7541 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7545 /* Consume the identifier. */
7546 token
= cp_lexer_consume_token (parser
->lexer
);
7547 /* Save the identifier. */
7548 identifier
= token
->u
.value
;
7552 bool expr_non_constant_p
;
7554 /* Parse the next assignment-expression. */
7555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7557 /* A braced-init-list. */
7558 cp_lexer_set_source_position (parser
->lexer
);
7559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7560 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7561 if (non_constant_p
&& expr_non_constant_p
)
7562 *non_constant_p
= true;
7564 else if (non_constant_p
)
7566 expr
= (cp_parser_constant_expression
7567 (parser
, /*allow_non_constant_p=*/true,
7568 &expr_non_constant_p
));
7569 if (expr_non_constant_p
)
7570 *non_constant_p
= true;
7573 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7577 expr
= instantiate_non_dependent_expr (expr
);
7579 /* If we have an ellipsis, then this is an expression
7581 if (allow_expansion_p
7582 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7584 /* Consume the `...'. */
7585 cp_lexer_consume_token (parser
->lexer
);
7587 /* Build the argument pack. */
7588 expr
= make_pack_expansion (expr
);
7591 /* Add it to the list. We add error_mark_node
7592 expressions to the list, so that we can still tell if
7593 the correct form for a parenthesized expression-list
7594 is found. That gives better errors. */
7595 vec_safe_push (expression_list
, expr
);
7597 if (expr
== error_mark_node
)
7601 /* After the first item, attribute lists look the same as
7602 expression lists. */
7603 is_attribute_list
= non_attr
;
7606 /* If the next token isn't a `,', then we are done. */
7607 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7610 /* Otherwise, consume the `,' and keep going. */
7611 cp_lexer_consume_token (parser
->lexer
);
7614 if (close_paren_loc
)
7615 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7617 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7622 /* We try and resync to an unnested comma, as that will give the
7623 user better diagnostics. */
7624 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7625 /*recovering=*/true,
7627 /*consume_paren=*/true);
7632 parser
->greater_than_is_operator_p
7633 = saved_greater_than_is_operator_p
;
7638 parser
->greater_than_is_operator_p
7639 = saved_greater_than_is_operator_p
;
7642 vec_safe_insert (expression_list
, 0, identifier
);
7644 return expression_list
;
7647 /* Parse a pseudo-destructor-name.
7649 pseudo-destructor-name:
7650 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7651 :: [opt] nested-name-specifier template template-id :: ~ type-name
7652 :: [opt] nested-name-specifier [opt] ~ type-name
7654 If either of the first two productions is used, sets *SCOPE to the
7655 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7656 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7657 or ERROR_MARK_NODE if the parse fails. */
7660 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7665 bool nested_name_specifier_p
;
7668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7669 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7670 && !type_dependent_expression_p (object
))
7672 if (cxx_dialect
< cxx14
)
7673 pedwarn (input_location
, 0,
7674 "%<~auto%> only available with "
7675 "-std=c++14 or -std=gnu++14");
7676 cp_lexer_consume_token (parser
->lexer
);
7677 cp_lexer_consume_token (parser
->lexer
);
7679 *type
= TREE_TYPE (object
);
7683 /* Assume that things will not work out. */
7684 *type
= error_mark_node
;
7686 /* Look for the optional `::' operator. */
7687 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7688 /* Look for the optional nested-name-specifier. */
7689 nested_name_specifier_p
7690 = (cp_parser_nested_name_specifier_opt (parser
,
7691 /*typename_keyword_p=*/false,
7692 /*check_dependency_p=*/true,
7694 /*is_declaration=*/false)
7696 /* Now, if we saw a nested-name-specifier, we might be doing the
7697 second production. */
7698 if (nested_name_specifier_p
7699 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7701 /* Consume the `template' keyword. */
7702 cp_lexer_consume_token (parser
->lexer
);
7703 /* Parse the template-id. */
7704 cp_parser_template_id (parser
,
7705 /*template_keyword_p=*/true,
7706 /*check_dependency_p=*/false,
7708 /*is_declaration=*/true);
7709 /* Look for the `::' token. */
7710 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7712 /* If the next token is not a `~', then there might be some
7713 additional qualification. */
7714 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7716 /* At this point, we're looking for "type-name :: ~". The type-name
7717 must not be a class-name, since this is a pseudo-destructor. So,
7718 it must be either an enum-name, or a typedef-name -- both of which
7719 are just identifiers. So, we peek ahead to check that the "::"
7720 and "~" tokens are present; if they are not, then we can avoid
7721 calling type_name. */
7722 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7723 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7724 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7726 cp_parser_error (parser
, "non-scalar type");
7730 /* Look for the type-name. */
7731 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7732 if (*scope
== error_mark_node
)
7735 /* Look for the `::' token. */
7736 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7741 /* Look for the `~'. */
7742 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7744 /* Once we see the ~, this has to be a pseudo-destructor. */
7745 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7746 cp_parser_commit_to_topmost_tentative_parse (parser
);
7748 /* Look for the type-name again. We are not responsible for
7749 checking that it matches the first type-name. */
7750 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7753 /* Parse a unary-expression.
7759 unary-operator cast-expression
7760 sizeof unary-expression
7762 alignof ( type-id ) [C++0x]
7769 __extension__ cast-expression
7770 __alignof__ unary-expression
7771 __alignof__ ( type-id )
7772 alignof unary-expression [C++0x]
7773 __real__ cast-expression
7774 __imag__ cast-expression
7776 sizeof ( type-id ) { initializer-list , [opt] }
7777 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7778 __alignof__ ( type-id ) { initializer-list , [opt] }
7780 ADDRESS_P is true iff the unary-expression is appearing as the
7781 operand of the `&' operator. CAST_P is true if this expression is
7782 the target of a cast.
7784 Returns a representation of the expression. */
7787 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7788 bool address_p
, bool cast_p
, bool decltype_p
)
7791 enum tree_code unary_operator
;
7793 /* Peek at the next token. */
7794 token
= cp_lexer_peek_token (parser
->lexer
);
7795 /* Some keywords give away the kind of expression. */
7796 if (token
->type
== CPP_KEYWORD
)
7798 enum rid keyword
= token
->keyword
;
7807 location_t first_loc
;
7809 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7810 /* Consume the token. */
7811 cp_lexer_consume_token (parser
->lexer
);
7812 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7813 /* Parse the operand. */
7814 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7816 if (TYPE_P (operand
))
7817 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7820 /* ISO C++ defines alignof only with types, not with
7821 expressions. So pedwarn if alignof is used with a non-
7822 type expression. However, __alignof__ is ok. */
7823 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7824 pedwarn (token
->location
, OPT_Wpedantic
,
7825 "ISO C++ does not allow %<alignof%> "
7828 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7830 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7831 SIZEOF_EXPR with the original operand. */
7832 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7834 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7836 if (!processing_template_decl
&& TYPE_P (operand
))
7838 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7839 build1 (NOP_EXPR
, operand
,
7841 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7844 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7845 TREE_SIDE_EFFECTS (ret
) = 0;
7846 TREE_READONLY (ret
) = 1;
7848 SET_EXPR_LOCATION (ret
, first_loc
);
7854 return cp_parser_new_expression (parser
);
7857 return cp_parser_delete_expression (parser
);
7861 /* The saved value of the PEDANTIC flag. */
7865 /* Save away the PEDANTIC flag. */
7866 cp_parser_extension_opt (parser
, &saved_pedantic
);
7867 /* Parse the cast-expression. */
7868 expr
= cp_parser_simple_cast_expression (parser
);
7869 /* Restore the PEDANTIC flag. */
7870 pedantic
= saved_pedantic
;
7880 /* Consume the `__real__' or `__imag__' token. */
7881 cp_lexer_consume_token (parser
->lexer
);
7882 /* Parse the cast-expression. */
7883 expression
= cp_parser_simple_cast_expression (parser
);
7884 /* Create the complete representation. */
7885 return build_x_unary_op (token
->location
,
7886 (keyword
== RID_REALPART
7887 ? REALPART_EXPR
: IMAGPART_EXPR
),
7889 tf_warning_or_error
);
7893 case RID_TRANSACTION_ATOMIC
:
7894 case RID_TRANSACTION_RELAXED
:
7895 return cp_parser_transaction_expression (parser
, keyword
);
7900 const char *saved_message
;
7901 bool saved_integral_constant_expression_p
;
7902 bool saved_non_integral_constant_expression_p
;
7903 bool saved_greater_than_is_operator_p
;
7905 cp_lexer_consume_token (parser
->lexer
);
7906 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7908 saved_message
= parser
->type_definition_forbidden_message
;
7909 parser
->type_definition_forbidden_message
7910 = G_("types may not be defined in %<noexcept%> expressions");
7912 saved_integral_constant_expression_p
7913 = parser
->integral_constant_expression_p
;
7914 saved_non_integral_constant_expression_p
7915 = parser
->non_integral_constant_expression_p
;
7916 parser
->integral_constant_expression_p
= false;
7918 saved_greater_than_is_operator_p
7919 = parser
->greater_than_is_operator_p
;
7920 parser
->greater_than_is_operator_p
= true;
7922 ++cp_unevaluated_operand
;
7923 ++c_inhibit_evaluation_warnings
;
7924 ++cp_noexcept_operand
;
7925 expr
= cp_parser_expression (parser
);
7926 --cp_noexcept_operand
;
7927 --c_inhibit_evaluation_warnings
;
7928 --cp_unevaluated_operand
;
7930 parser
->greater_than_is_operator_p
7931 = saved_greater_than_is_operator_p
;
7933 parser
->integral_constant_expression_p
7934 = saved_integral_constant_expression_p
;
7935 parser
->non_integral_constant_expression_p
7936 = saved_non_integral_constant_expression_p
;
7938 parser
->type_definition_forbidden_message
= saved_message
;
7940 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7941 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7949 /* Look for the `:: new' and `:: delete', which also signal the
7950 beginning of a new-expression, or delete-expression,
7951 respectively. If the next token is `::', then it might be one of
7953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7957 /* See if the token after the `::' is one of the keywords in
7958 which we're interested. */
7959 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7960 /* If it's `new', we have a new-expression. */
7961 if (keyword
== RID_NEW
)
7962 return cp_parser_new_expression (parser
);
7963 /* Similarly, for `delete'. */
7964 else if (keyword
== RID_DELETE
)
7965 return cp_parser_delete_expression (parser
);
7968 /* Look for a unary operator. */
7969 unary_operator
= cp_parser_unary_operator (token
);
7970 /* The `++' and `--' operators can be handled similarly, even though
7971 they are not technically unary-operators in the grammar. */
7972 if (unary_operator
== ERROR_MARK
)
7974 if (token
->type
== CPP_PLUS_PLUS
)
7975 unary_operator
= PREINCREMENT_EXPR
;
7976 else if (token
->type
== CPP_MINUS_MINUS
)
7977 unary_operator
= PREDECREMENT_EXPR
;
7978 /* Handle the GNU address-of-label extension. */
7979 else if (cp_parser_allow_gnu_extensions_p (parser
)
7980 && token
->type
== CPP_AND_AND
)
7984 location_t start_loc
= token
->location
;
7986 /* Consume the '&&' token. */
7987 cp_lexer_consume_token (parser
->lexer
);
7988 /* Look for the identifier. */
7989 location_t finish_loc
7990 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
7991 identifier
= cp_parser_identifier (parser
);
7992 /* Construct a location of the form:
7995 with caret==start at the "&&", finish at the end of the label. */
7996 location_t combined_loc
7997 = make_location (start_loc
, start_loc
, finish_loc
);
7998 /* Create an expression representing the address. */
7999 expression
= finish_label_address_expr (identifier
, combined_loc
);
8000 if (cp_parser_non_integral_constant_expression (parser
,
8002 expression
= error_mark_node
;
8006 if (unary_operator
!= ERROR_MARK
)
8008 cp_expr cast_expression
;
8009 cp_expr expression
= error_mark_node
;
8010 non_integral_constant non_constant_p
= NIC_NONE
;
8011 location_t loc
= token
->location
;
8012 tsubst_flags_t complain
= complain_flags (decltype_p
);
8014 /* Consume the operator token. */
8015 token
= cp_lexer_consume_token (parser
->lexer
);
8016 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8018 /* Parse the cast-expression. */
8020 = cp_parser_cast_expression (parser
,
8021 unary_operator
== ADDR_EXPR
,
8027 OP_TOKEN CAST_EXPRESSION
8028 ^~~~~~~~~~~~~~~~~~~~~~~~~
8029 with start==caret at the operator token, and
8030 extending to the end of the cast_expression. */
8031 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8033 /* Now, build an appropriate representation. */
8034 switch (unary_operator
)
8037 non_constant_p
= NIC_STAR
;
8038 expression
= build_x_indirect_ref (loc
, cast_expression
,
8041 /* TODO: build_x_indirect_ref does not always honor the
8042 location, so ensure it is set. */
8043 expression
.set_location (loc
);
8047 non_constant_p
= NIC_ADDR
;
8050 expression
= build_x_unary_op (loc
, unary_operator
,
8053 /* TODO: build_x_unary_op does not always honor the location,
8054 so ensure it is set. */
8055 expression
.set_location (loc
);
8058 case PREINCREMENT_EXPR
:
8059 case PREDECREMENT_EXPR
:
8060 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8061 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8064 /* Immediately fold negation of a constant, unless the constant is 0
8065 (since -0 == 0) or it would overflow. */
8066 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8067 && CONSTANT_CLASS_P (cast_expression
)
8068 && !integer_zerop (cast_expression
)
8069 && !TREE_OVERFLOW (cast_expression
))
8071 tree folded
= fold_build1 (unary_operator
,
8072 TREE_TYPE (cast_expression
),
8074 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8076 expression
= cp_expr (folded
, loc
);
8081 case UNARY_PLUS_EXPR
:
8082 case TRUTH_NOT_EXPR
:
8083 expression
= finish_unary_op_expr (loc
, unary_operator
,
8084 cast_expression
, complain
);
8091 if (non_constant_p
!= NIC_NONE
8092 && cp_parser_non_integral_constant_expression (parser
,
8094 expression
= error_mark_node
;
8099 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8100 /*member_access_only_p=*/false,
8105 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8106 unary-operator, the corresponding tree code is returned. */
8108 static enum tree_code
8109 cp_parser_unary_operator (cp_token
* token
)
8111 switch (token
->type
)
8114 return INDIRECT_REF
;
8120 return UNARY_PLUS_EXPR
;
8126 return TRUTH_NOT_EXPR
;
8129 return BIT_NOT_EXPR
;
8136 /* Parse a new-expression.
8139 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8140 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8142 Returns a representation of the expression. */
8145 cp_parser_new_expression (cp_parser
* parser
)
8147 bool global_scope_p
;
8148 vec
<tree
, va_gc
> *placement
;
8150 vec
<tree
, va_gc
> *initializer
;
8151 tree nelts
= NULL_TREE
;
8154 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8156 /* Look for the optional `::' operator. */
8158 = (cp_parser_global_scope_opt (parser
,
8159 /*current_scope_valid_p=*/false)
8161 /* Look for the `new' operator. */
8162 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8163 /* There's no easy way to tell a new-placement from the
8164 `( type-id )' construct. */
8165 cp_parser_parse_tentatively (parser
);
8166 /* Look for a new-placement. */
8167 placement
= cp_parser_new_placement (parser
);
8168 /* If that didn't work out, there's no new-placement. */
8169 if (!cp_parser_parse_definitely (parser
))
8171 if (placement
!= NULL
)
8172 release_tree_vector (placement
);
8176 /* If the next token is a `(', then we have a parenthesized
8178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8181 const char *saved_message
= parser
->type_definition_forbidden_message
;
8183 /* Consume the `('. */
8184 cp_lexer_consume_token (parser
->lexer
);
8186 /* Parse the type-id. */
8187 parser
->type_definition_forbidden_message
8188 = G_("types may not be defined in a new-expression");
8190 type_id_in_expr_sentinel
s (parser
);
8191 type
= cp_parser_type_id (parser
);
8193 parser
->type_definition_forbidden_message
= saved_message
;
8195 /* Look for the closing `)'. */
8196 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8197 token
= cp_lexer_peek_token (parser
->lexer
);
8198 /* There should not be a direct-new-declarator in this production,
8199 but GCC used to allowed this, so we check and emit a sensible error
8200 message for this case. */
8201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8203 error_at (token
->location
,
8204 "array bound forbidden after parenthesized type-id");
8205 inform (token
->location
,
8206 "try removing the parentheses around the type-id");
8207 cp_parser_direct_new_declarator (parser
);
8210 /* Otherwise, there must be a new-type-id. */
8212 type
= cp_parser_new_type_id (parser
, &nelts
);
8214 /* If the next token is a `(' or '{', then we have a new-initializer. */
8215 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8216 if (token
->type
== CPP_OPEN_PAREN
8217 || token
->type
== CPP_OPEN_BRACE
)
8218 initializer
= cp_parser_new_initializer (parser
);
8222 /* A new-expression may not appear in an integral constant
8224 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8225 ret
= error_mark_node
;
8226 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8227 of a new-type-id or type-id of a new-expression, the new-expression shall
8228 contain a new-initializer of the form ( assignment-expression )".
8229 Additionally, consistently with the spirit of DR 1467, we want to accept
8230 'new auto { 2 }' too. */
8231 else if (type_uses_auto (type
)
8232 && (vec_safe_length (initializer
) != 1
8233 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8234 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8236 error_at (token
->location
,
8237 "initialization of new-expression for type %<auto%> "
8238 "requires exactly one element");
8239 ret
= error_mark_node
;
8243 /* Construct a location e.g.:
8246 with caret == start at the start of the "new" token, and the end
8247 at the end of the final token we consumed. */
8248 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8249 location_t end_loc
= get_finish (end_tok
->location
);
8250 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8252 /* Create a representation of the new-expression. */
8253 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8254 tf_warning_or_error
);
8255 protected_set_expr_location (ret
, combined_loc
);
8258 if (placement
!= NULL
)
8259 release_tree_vector (placement
);
8260 if (initializer
!= NULL
)
8261 release_tree_vector (initializer
);
8266 /* Parse a new-placement.
8271 Returns the same representation as for an expression-list. */
8273 static vec
<tree
, va_gc
> *
8274 cp_parser_new_placement (cp_parser
* parser
)
8276 vec
<tree
, va_gc
> *expression_list
;
8278 /* Parse the expression-list. */
8279 expression_list
= (cp_parser_parenthesized_expression_list
8280 (parser
, non_attr
, /*cast_p=*/false,
8281 /*allow_expansion_p=*/true,
8282 /*non_constant_p=*/NULL
));
8284 if (expression_list
&& expression_list
->is_empty ())
8285 error ("expected expression-list or type-id");
8287 return expression_list
;
8290 /* Parse a new-type-id.
8293 type-specifier-seq new-declarator [opt]
8295 Returns the TYPE allocated. If the new-type-id indicates an array
8296 type, *NELTS is set to the number of elements in the last array
8297 bound; the TYPE will not include the last array bound. */
8300 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8302 cp_decl_specifier_seq type_specifier_seq
;
8303 cp_declarator
*new_declarator
;
8304 cp_declarator
*declarator
;
8305 cp_declarator
*outer_declarator
;
8306 const char *saved_message
;
8308 /* The type-specifier sequence must not contain type definitions.
8309 (It cannot contain declarations of new types either, but if they
8310 are not definitions we will catch that because they are not
8312 saved_message
= parser
->type_definition_forbidden_message
;
8313 parser
->type_definition_forbidden_message
8314 = G_("types may not be defined in a new-type-id");
8315 /* Parse the type-specifier-seq. */
8316 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8317 /*is_trailing_return=*/false,
8318 &type_specifier_seq
);
8319 /* Restore the old message. */
8320 parser
->type_definition_forbidden_message
= saved_message
;
8322 if (type_specifier_seq
.type
== error_mark_node
)
8323 return error_mark_node
;
8325 /* Parse the new-declarator. */
8326 new_declarator
= cp_parser_new_declarator_opt (parser
);
8328 /* Determine the number of elements in the last array dimension, if
8331 /* Skip down to the last array dimension. */
8332 declarator
= new_declarator
;
8333 outer_declarator
= NULL
;
8334 while (declarator
&& (declarator
->kind
== cdk_pointer
8335 || declarator
->kind
== cdk_ptrmem
))
8337 outer_declarator
= declarator
;
8338 declarator
= declarator
->declarator
;
8341 && declarator
->kind
== cdk_array
8342 && declarator
->declarator
8343 && declarator
->declarator
->kind
== cdk_array
)
8345 outer_declarator
= declarator
;
8346 declarator
= declarator
->declarator
;
8349 if (declarator
&& declarator
->kind
== cdk_array
)
8351 *nelts
= declarator
->u
.array
.bounds
;
8352 if (*nelts
== error_mark_node
)
8353 *nelts
= integer_one_node
;
8355 if (outer_declarator
)
8356 outer_declarator
->declarator
= declarator
->declarator
;
8358 new_declarator
= NULL
;
8361 return groktypename (&type_specifier_seq
, new_declarator
, false);
8364 /* Parse an (optional) new-declarator.
8367 ptr-operator new-declarator [opt]
8368 direct-new-declarator
8370 Returns the declarator. */
8372 static cp_declarator
*
8373 cp_parser_new_declarator_opt (cp_parser
* parser
)
8375 enum tree_code code
;
8376 tree type
, std_attributes
= NULL_TREE
;
8377 cp_cv_quals cv_quals
;
8379 /* We don't know if there's a ptr-operator next, or not. */
8380 cp_parser_parse_tentatively (parser
);
8381 /* Look for a ptr-operator. */
8382 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8383 /* If that worked, look for more new-declarators. */
8384 if (cp_parser_parse_definitely (parser
))
8386 cp_declarator
*declarator
;
8388 /* Parse another optional declarator. */
8389 declarator
= cp_parser_new_declarator_opt (parser
);
8391 declarator
= cp_parser_make_indirect_declarator
8392 (code
, type
, cv_quals
, declarator
, std_attributes
);
8397 /* If the next token is a `[', there is a direct-new-declarator. */
8398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8399 return cp_parser_direct_new_declarator (parser
);
8404 /* Parse a direct-new-declarator.
8406 direct-new-declarator:
8408 direct-new-declarator [constant-expression]
8412 static cp_declarator
*
8413 cp_parser_direct_new_declarator (cp_parser
* parser
)
8415 cp_declarator
*declarator
= NULL
;
8422 /* Look for the opening `['. */
8423 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8425 token
= cp_lexer_peek_token (parser
->lexer
);
8426 expression
= cp_parser_expression (parser
);
8427 /* The standard requires that the expression have integral
8428 type. DR 74 adds enumeration types. We believe that the
8429 real intent is that these expressions be handled like the
8430 expression in a `switch' condition, which also allows
8431 classes with a single conversion to integral or
8432 enumeration type. */
8433 if (!processing_template_decl
)
8436 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8441 error_at (token
->location
,
8442 "expression in new-declarator must have integral "
8443 "or enumeration type");
8444 expression
= error_mark_node
;
8448 /* Look for the closing `]'. */
8449 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8451 /* Add this bound to the declarator. */
8452 declarator
= make_array_declarator (declarator
, expression
);
8454 /* If the next token is not a `[', then there are no more
8456 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8463 /* Parse a new-initializer.
8466 ( expression-list [opt] )
8469 Returns a representation of the expression-list. */
8471 static vec
<tree
, va_gc
> *
8472 cp_parser_new_initializer (cp_parser
* parser
)
8474 vec
<tree
, va_gc
> *expression_list
;
8476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8479 bool expr_non_constant_p
;
8480 cp_lexer_set_source_position (parser
->lexer
);
8481 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8482 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8483 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8484 expression_list
= make_tree_vector_single (t
);
8487 expression_list
= (cp_parser_parenthesized_expression_list
8488 (parser
, non_attr
, /*cast_p=*/false,
8489 /*allow_expansion_p=*/true,
8490 /*non_constant_p=*/NULL
));
8492 return expression_list
;
8495 /* Parse a delete-expression.
8498 :: [opt] delete cast-expression
8499 :: [opt] delete [ ] cast-expression
8501 Returns a representation of the expression. */
8504 cp_parser_delete_expression (cp_parser
* parser
)
8506 bool global_scope_p
;
8510 /* Look for the optional `::' operator. */
8512 = (cp_parser_global_scope_opt (parser
,
8513 /*current_scope_valid_p=*/false)
8515 /* Look for the `delete' keyword. */
8516 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8517 /* See if the array syntax is in use. */
8518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8520 /* Consume the `[' token. */
8521 cp_lexer_consume_token (parser
->lexer
);
8522 /* Look for the `]' token. */
8523 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8524 /* Remember that this is the `[]' construct. */
8530 /* Parse the cast-expression. */
8531 expression
= cp_parser_simple_cast_expression (parser
);
8533 /* A delete-expression may not appear in an integral constant
8535 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8536 return error_mark_node
;
8538 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8539 tf_warning_or_error
);
8542 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8543 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8547 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8549 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8550 switch (token
->type
)
8556 case CPP_CLOSE_SQUARE
:
8557 case CPP_CLOSE_PAREN
:
8558 case CPP_CLOSE_BRACE
:
8559 case CPP_OPEN_BRACE
:
8563 case CPP_DEREF_STAR
:
8571 case CPP_GREATER_EQ
:
8592 case CPP_OPEN_PAREN
:
8593 /* In ((type ()) () the last () isn't a valid cast-expression,
8594 so the whole must be parsed as postfix-expression. */
8595 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8598 case CPP_OPEN_SQUARE
:
8599 /* '[' may start a primary-expression in obj-c++ and in C++11,
8600 as a lambda-expression, eg, '(void)[]{}'. */
8601 if (cxx_dialect
>= cxx11
)
8603 return c_dialect_objc ();
8606 case CPP_MINUS_MINUS
:
8607 /* '++' and '--' may or may not start a cast-expression:
8609 struct T { void operator++(int); };
8610 void f() { (T())++; }
8623 /* Parse a cast-expression.
8627 ( type-id ) cast-expression
8629 ADDRESS_P is true iff the unary-expression is appearing as the
8630 operand of the `&' operator. CAST_P is true if this expression is
8631 the target of a cast.
8633 Returns a representation of the expression. */
8636 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8637 bool decltype_p
, cp_id_kind
* pidk
)
8639 /* If it's a `(', then we might be looking at a cast. */
8640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8642 tree type
= NULL_TREE
;
8643 cp_expr
expr (NULL_TREE
);
8644 int cast_expression
= 0;
8645 const char *saved_message
;
8647 /* There's no way to know yet whether or not this is a cast.
8648 For example, `(int (3))' is a unary-expression, while `(int)
8649 3' is a cast. So, we resort to parsing tentatively. */
8650 cp_parser_parse_tentatively (parser
);
8651 /* Types may not be defined in a cast. */
8652 saved_message
= parser
->type_definition_forbidden_message
;
8653 parser
->type_definition_forbidden_message
8654 = G_("types may not be defined in casts");
8655 /* Consume the `('. */
8656 cp_token
*open_paren
= cp_lexer_consume_token (parser
->lexer
);
8657 location_t open_paren_loc
= open_paren
->location
;
8659 /* A very tricky bit is that `(struct S) { 3 }' is a
8660 compound-literal (which we permit in C++ as an extension).
8661 But, that construct is not a cast-expression -- it is a
8662 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8663 is legal; if the compound-literal were a cast-expression,
8664 you'd need an extra set of parentheses.) But, if we parse
8665 the type-id, and it happens to be a class-specifier, then we
8666 will commit to the parse at that point, because we cannot
8667 undo the action that is done when creating a new class. So,
8668 then we cannot back up and do a postfix-expression.
8670 Another tricky case is the following (c++/29234):
8672 struct S { void operator () (); };
8679 As a type-id we parse the parenthesized S()() as a function
8680 returning a function, groktypename complains and we cannot
8681 back up in this case either.
8683 Therefore, we scan ahead to the closing `)', and check to see
8684 if the tokens after the `)' can start a cast-expression. Otherwise
8685 we are dealing with an unary-expression, a postfix-expression
8688 Yet another tricky case, in C++11, is the following (c++/54891):
8692 The issue is that usually, besides the case of lambda-expressions,
8693 the parenthesized type-id cannot be followed by '[', and, eg, we
8694 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8695 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8696 we don't commit, we try a cast-expression, then an unary-expression.
8698 Save tokens so that we can put them back. */
8699 cp_lexer_save_tokens (parser
->lexer
);
8701 /* We may be looking at a cast-expression. */
8702 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8703 /*consume_paren=*/true))
8705 = cp_parser_tokens_start_cast_expression (parser
);
8707 /* Roll back the tokens we skipped. */
8708 cp_lexer_rollback_tokens (parser
->lexer
);
8709 /* If we aren't looking at a cast-expression, simulate an error so
8710 that the call to cp_parser_error_occurred below returns true. */
8711 if (!cast_expression
)
8712 cp_parser_simulate_error (parser
);
8715 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8716 parser
->in_type_id_in_expr_p
= true;
8717 /* Look for the type-id. */
8718 type
= cp_parser_type_id (parser
);
8719 /* Look for the closing `)'. */
8720 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8721 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8724 /* Restore the saved message. */
8725 parser
->type_definition_forbidden_message
= saved_message
;
8727 /* At this point this can only be either a cast or a
8728 parenthesized ctor such as `(T ())' that looks like a cast to
8729 function returning T. */
8730 if (!cp_parser_error_occurred (parser
))
8732 /* Only commit if the cast-expression doesn't start with
8733 '++', '--', or '[' in C++11. */
8734 if (cast_expression
> 0)
8735 cp_parser_commit_to_topmost_tentative_parse (parser
);
8737 expr
= cp_parser_cast_expression (parser
,
8738 /*address_p=*/false,
8740 /*decltype_p=*/false,
8743 if (cp_parser_parse_definitely (parser
))
8745 /* Warn about old-style casts, if so requested. */
8746 if (warn_old_style_cast
8747 && !in_system_header_at (input_location
)
8748 && !VOID_TYPE_P (type
)
8749 && current_lang_name
!= lang_name_c
)
8750 warning (OPT_Wold_style_cast
, "use of old-style cast");
8752 /* Only type conversions to integral or enumeration types
8753 can be used in constant-expressions. */
8754 if (!cast_valid_in_integral_constant_expression_p (type
)
8755 && cp_parser_non_integral_constant_expression (parser
,
8757 return error_mark_node
;
8759 /* Perform the cast. */
8763 with start==caret at the open paren, extending to the
8765 location_t cast_loc
= make_location (open_paren_loc
,
8767 expr
.get_finish ());
8768 expr
= build_c_cast (cast_loc
, type
, expr
);
8773 cp_parser_abort_tentative_parse (parser
);
8776 /* If we get here, then it's not a cast, so it must be a
8777 unary-expression. */
8778 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8779 cast_p
, decltype_p
);
8782 /* Parse a binary expression of the general form:
8786 pm-expression .* cast-expression
8787 pm-expression ->* cast-expression
8789 multiplicative-expression:
8791 multiplicative-expression * pm-expression
8792 multiplicative-expression / pm-expression
8793 multiplicative-expression % pm-expression
8795 additive-expression:
8796 multiplicative-expression
8797 additive-expression + multiplicative-expression
8798 additive-expression - multiplicative-expression
8802 shift-expression << additive-expression
8803 shift-expression >> additive-expression
8805 relational-expression:
8807 relational-expression < shift-expression
8808 relational-expression > shift-expression
8809 relational-expression <= shift-expression
8810 relational-expression >= shift-expression
8814 relational-expression:
8815 relational-expression <? shift-expression
8816 relational-expression >? shift-expression
8818 equality-expression:
8819 relational-expression
8820 equality-expression == relational-expression
8821 equality-expression != relational-expression
8825 and-expression & equality-expression
8827 exclusive-or-expression:
8829 exclusive-or-expression ^ and-expression
8831 inclusive-or-expression:
8832 exclusive-or-expression
8833 inclusive-or-expression | exclusive-or-expression
8835 logical-and-expression:
8836 inclusive-or-expression
8837 logical-and-expression && inclusive-or-expression
8839 logical-or-expression:
8840 logical-and-expression
8841 logical-or-expression || logical-and-expression
8843 All these are implemented with a single function like:
8846 simple-cast-expression
8847 binary-expression <token> binary-expression
8849 CAST_P is true if this expression is the target of a cast.
8851 The binops_by_token map is used to get the tree codes for each <token> type.
8852 binary-expressions are associated according to a precedence table. */
8854 #define TOKEN_PRECEDENCE(token) \
8855 (((token->type == CPP_GREATER \
8856 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8857 && !parser->greater_than_is_operator_p) \
8858 ? PREC_NOT_OPERATOR \
8859 : binops_by_token[token->type].prec)
8862 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8863 bool no_toplevel_fold_p
,
8865 enum cp_parser_prec prec
,
8868 cp_parser_expression_stack stack
;
8869 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8870 cp_parser_expression_stack_entry current
;
8873 enum tree_code rhs_type
;
8874 enum cp_parser_prec new_prec
, lookahead_prec
;
8877 /* Parse the first expression. */
8878 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8879 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8880 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8881 cast_p
, decltype_p
, pidk
);
8882 current
.prec
= prec
;
8884 if (cp_parser_error_occurred (parser
))
8885 return error_mark_node
;
8889 /* Get an operator token. */
8890 token
= cp_lexer_peek_token (parser
->lexer
);
8892 if (warn_cxx11_compat
8893 && token
->type
== CPP_RSHIFT
8894 && !parser
->greater_than_is_operator_p
)
8896 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8897 "%<>>%> operator is treated"
8898 " as two right angle brackets in C++11"))
8899 inform (token
->location
,
8900 "suggest parentheses around %<>>%> expression");
8903 new_prec
= TOKEN_PRECEDENCE (token
);
8904 if (new_prec
!= PREC_NOT_OPERATOR
8905 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8906 /* This is a fold-expression; handle it later. */
8907 new_prec
= PREC_NOT_OPERATOR
;
8909 /* Popping an entry off the stack means we completed a subexpression:
8910 - either we found a token which is not an operator (`>' where it is not
8911 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8912 will happen repeatedly;
8913 - or, we found an operator which has lower priority. This is the case
8914 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8916 if (new_prec
<= current
.prec
)
8925 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8926 current
.loc
= token
->location
;
8928 /* We used the operator token. */
8929 cp_lexer_consume_token (parser
->lexer
);
8931 /* For "false && x" or "true || x", x will never be executed;
8932 disable warnings while evaluating it. */
8933 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8934 c_inhibit_evaluation_warnings
+=
8935 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8936 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8937 c_inhibit_evaluation_warnings
+=
8938 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8940 /* Extract another operand. It may be the RHS of this expression
8941 or the LHS of a new, higher priority expression. */
8942 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8943 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8944 rhs
= cp_parser_simple_cast_expression (parser
);
8946 /* Get another operator token. Look up its precedence to avoid
8947 building a useless (immediately popped) stack entry for common
8948 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8949 token
= cp_lexer_peek_token (parser
->lexer
);
8950 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8951 if (lookahead_prec
!= PREC_NOT_OPERATOR
8952 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8953 lookahead_prec
= PREC_NOT_OPERATOR
;
8954 if (lookahead_prec
> new_prec
)
8956 /* ... and prepare to parse the RHS of the new, higher priority
8957 expression. Since precedence levels on the stack are
8958 monotonically increasing, we do not have to care about
8963 current
.lhs_type
= rhs_type
;
8964 current
.prec
= new_prec
;
8965 new_prec
= lookahead_prec
;
8969 lookahead_prec
= new_prec
;
8970 /* If the stack is not empty, we have parsed into LHS the right side
8971 (`4' in the example above) of an expression we had suspended.
8972 We can use the information on the stack to recover the LHS (`3')
8973 from the stack together with the tree code (`MULT_EXPR'), and
8974 the precedence of the higher level subexpression
8975 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8976 which will be used to actually build the additive expression. */
8978 rhs_type
= current
.lhs_type
;
8983 /* Undo the disabling of warnings done above. */
8984 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8985 c_inhibit_evaluation_warnings
-=
8986 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8987 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8988 c_inhibit_evaluation_warnings
-=
8989 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8991 if (warn_logical_not_paren
8992 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8993 && current
.lhs_type
== TRUTH_NOT_EXPR
8994 /* Avoid warning for !!x == y. */
8995 && (TREE_CODE (current
.lhs
) != NE_EXPR
8996 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8997 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8998 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8999 /* Avoid warning for !b == y where b is boolean. */
9000 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9001 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9003 /* Avoid warning for !!b == y where b is boolean. */
9004 && (!DECL_P (current
.lhs
)
9005 || TREE_TYPE (current
.lhs
) == NULL_TREE
9006 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9007 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9008 current
.lhs
, maybe_constant_value (rhs
));
9012 location_t combined_loc
= make_location (current
.loc
,
9013 current
.lhs
.get_start (),
9016 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9017 ERROR_MARK for everything that is not a binary expression.
9018 This makes warn_about_parentheses miss some warnings that
9019 involve unary operators. For unary expressions we should
9020 pass the correct tree_code unless the unary expression was
9021 surrounded by parentheses.
9023 if (no_toplevel_fold_p
9024 && lookahead_prec
<= current
.prec
9026 current
.lhs
= build2_loc (combined_loc
,
9028 TREE_CODE_CLASS (current
.tree_type
)
9030 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9034 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9035 current
.lhs
, current
.lhs_type
,
9036 rhs
, rhs_type
, &overload
,
9037 complain_flags (decltype_p
));
9038 /* TODO: build_x_binary_op doesn't always honor the location. */
9039 current
.lhs
.set_location (combined_loc
);
9041 current
.lhs_type
= current
.tree_type
;
9043 /* If the binary operator required the use of an overloaded operator,
9044 then this expression cannot be an integral constant-expression.
9045 An overloaded operator can be used even if both operands are
9046 otherwise permissible in an integral constant-expression if at
9047 least one of the operands is of enumeration type. */
9050 && cp_parser_non_integral_constant_expression (parser
,
9052 return error_mark_node
;
9059 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9060 bool no_toplevel_fold_p
,
9061 enum cp_parser_prec prec
,
9064 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9065 /*decltype*/false, prec
, pidk
);
9068 /* Parse the `? expression : assignment-expression' part of a
9069 conditional-expression. The LOGICAL_OR_EXPR is the
9070 logical-or-expression that started the conditional-expression.
9071 Returns a representation of the entire conditional-expression.
9073 This routine is used by cp_parser_assignment_expression.
9075 ? expression : assignment-expression
9079 ? : assignment-expression */
9082 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9084 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9085 cp_expr assignment_expr
;
9086 struct cp_token
*token
;
9087 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9089 /* Consume the `?' token. */
9090 cp_lexer_consume_token (parser
->lexer
);
9091 token
= cp_lexer_peek_token (parser
->lexer
);
9092 if (cp_parser_allow_gnu_extensions_p (parser
)
9093 && token
->type
== CPP_COLON
)
9095 pedwarn (token
->location
, OPT_Wpedantic
,
9096 "ISO C++ does not allow ?: with omitted middle operand");
9097 /* Implicit true clause. */
9099 c_inhibit_evaluation_warnings
+=
9100 folded_logical_or_expr
== truthvalue_true_node
;
9101 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9105 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9106 parser
->colon_corrects_to_scope_p
= false;
9107 /* Parse the expression. */
9108 c_inhibit_evaluation_warnings
+=
9109 folded_logical_or_expr
== truthvalue_false_node
;
9110 expr
= cp_parser_expression (parser
);
9111 c_inhibit_evaluation_warnings
+=
9112 ((folded_logical_or_expr
== truthvalue_true_node
)
9113 - (folded_logical_or_expr
== truthvalue_false_node
));
9114 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9117 /* The next token should be a `:'. */
9118 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9119 /* Parse the assignment-expression. */
9120 assignment_expr
= cp_parser_assignment_expression (parser
);
9121 c_inhibit_evaluation_warnings
-=
9122 folded_logical_or_expr
== truthvalue_true_node
;
9125 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9126 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9127 with the caret at the "?", ranging from the start of
9128 the logical_or_expr to the end of the assignment_expr. */
9129 loc
= make_location (loc
,
9130 logical_or_expr
.get_start (),
9131 assignment_expr
.get_finish ());
9133 /* Build the conditional-expression. */
9134 return build_x_conditional_expr (loc
, logical_or_expr
,
9137 tf_warning_or_error
);
9140 /* Parse an assignment-expression.
9142 assignment-expression:
9143 conditional-expression
9144 logical-or-expression assignment-operator assignment_expression
9147 CAST_P is true if this expression is the target of a cast.
9148 DECLTYPE_P is true if this expression is the operand of decltype.
9150 Returns a representation for the expression. */
9153 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9154 bool cast_p
, bool decltype_p
)
9158 /* If the next token is the `throw' keyword, then we're looking at
9159 a throw-expression. */
9160 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9161 expr
= cp_parser_throw_expression (parser
);
9162 /* Otherwise, it must be that we are looking at a
9163 logical-or-expression. */
9166 /* Parse the binary expressions (logical-or-expression). */
9167 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9169 PREC_NOT_OPERATOR
, pidk
);
9170 /* If the next token is a `?' then we're actually looking at a
9171 conditional-expression. */
9172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9173 return cp_parser_question_colon_clause (parser
, expr
);
9176 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9178 /* If it's an assignment-operator, we're using the second
9180 enum tree_code assignment_operator
9181 = cp_parser_assignment_operator_opt (parser
);
9182 if (assignment_operator
!= ERROR_MARK
)
9184 bool non_constant_p
;
9186 /* Parse the right-hand side of the assignment. */
9187 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9190 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9191 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9193 /* An assignment may not appear in a
9194 constant-expression. */
9195 if (cp_parser_non_integral_constant_expression (parser
,
9197 return error_mark_node
;
9198 /* Build the assignment expression. Its default
9202 is the location of the '=' token as the
9203 caret, ranging from the start of the lhs to the
9205 loc
= make_location (loc
,
9208 expr
= build_x_modify_expr (loc
, expr
,
9209 assignment_operator
,
9211 complain_flags (decltype_p
));
9212 /* TODO: build_x_modify_expr doesn't honor the location,
9213 so we must set it here. */
9214 expr
.set_location (loc
);
9222 /* Parse an (optional) assignment-operator.
9224 assignment-operator: one of
9225 = *= /= %= += -= >>= <<= &= ^= |=
9229 assignment-operator: one of
9232 If the next token is an assignment operator, the corresponding tree
9233 code is returned, and the token is consumed. For example, for
9234 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9235 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9236 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9237 operator, ERROR_MARK is returned. */
9239 static enum tree_code
9240 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9245 /* Peek at the next token. */
9246 token
= cp_lexer_peek_token (parser
->lexer
);
9248 switch (token
->type
)
9259 op
= TRUNC_DIV_EXPR
;
9263 op
= TRUNC_MOD_EXPR
;
9295 /* Nothing else is an assignment operator. */
9299 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9300 if (op
!= ERROR_MARK
9301 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9304 /* If it was an assignment operator, consume it. */
9305 if (op
!= ERROR_MARK
)
9306 cp_lexer_consume_token (parser
->lexer
);
9311 /* Parse an expression.
9314 assignment-expression
9315 expression , assignment-expression
9317 CAST_P is true if this expression is the target of a cast.
9318 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9319 except possibly parenthesized or on the RHS of a comma (N3276).
9321 Returns a representation of the expression. */
9324 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9325 bool cast_p
, bool decltype_p
)
9327 cp_expr expression
= NULL_TREE
;
9328 location_t loc
= UNKNOWN_LOCATION
;
9332 cp_expr assignment_expression
;
9334 /* Parse the next assignment-expression. */
9335 assignment_expression
9336 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9338 /* We don't create a temporary for a call that is the immediate operand
9339 of decltype or on the RHS of a comma. But when we see a comma, we
9340 need to create a temporary for a call on the LHS. */
9341 if (decltype_p
&& !processing_template_decl
9342 && TREE_CODE (assignment_expression
) == CALL_EXPR
9343 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9344 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9345 assignment_expression
9346 = build_cplus_new (TREE_TYPE (assignment_expression
),
9347 assignment_expression
, tf_warning_or_error
);
9349 /* If this is the first assignment-expression, we can just
9352 expression
= assignment_expression
;
9355 /* Create a location with caret at the comma, ranging
9356 from the start of the LHS to the end of the RHS. */
9357 loc
= make_location (loc
,
9358 expression
.get_start (),
9359 assignment_expression
.get_finish ());
9360 expression
= build_x_compound_expr (loc
, expression
,
9361 assignment_expression
,
9362 complain_flags (decltype_p
));
9363 expression
.set_location (loc
);
9365 /* If the next token is not a comma, or we're in a fold-expression, then
9366 we are done with the expression. */
9367 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9368 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9370 /* Consume the `,'. */
9371 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9372 cp_lexer_consume_token (parser
->lexer
);
9373 /* A comma operator cannot appear in a constant-expression. */
9374 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9375 expression
= error_mark_node
;
9381 /* Parse a constant-expression.
9383 constant-expression:
9384 conditional-expression
9386 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9387 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9388 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9389 is false, NON_CONSTANT_P should be NULL. */
9392 cp_parser_constant_expression (cp_parser
* parser
,
9393 bool allow_non_constant_p
,
9394 bool *non_constant_p
)
9396 bool saved_integral_constant_expression_p
;
9397 bool saved_allow_non_integral_constant_expression_p
;
9398 bool saved_non_integral_constant_expression_p
;
9401 /* It might seem that we could simply parse the
9402 conditional-expression, and then check to see if it were
9403 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9404 one that the compiler can figure out is constant, possibly after
9405 doing some simplifications or optimizations. The standard has a
9406 precise definition of constant-expression, and we must honor
9407 that, even though it is somewhat more restrictive.
9413 is not a legal declaration, because `(2, 3)' is not a
9414 constant-expression. The `,' operator is forbidden in a
9415 constant-expression. However, GCC's constant-folding machinery
9416 will fold this operation to an INTEGER_CST for `3'. */
9418 /* Save the old settings. */
9419 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9420 saved_allow_non_integral_constant_expression_p
9421 = parser
->allow_non_integral_constant_expression_p
;
9422 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9423 /* We are now parsing a constant-expression. */
9424 parser
->integral_constant_expression_p
= true;
9425 parser
->allow_non_integral_constant_expression_p
9426 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9427 parser
->non_integral_constant_expression_p
= false;
9428 /* Although the grammar says "conditional-expression", we parse an
9429 "assignment-expression", which also permits "throw-expression"
9430 and the use of assignment operators. In the case that
9431 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9432 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9433 actually essential that we look for an assignment-expression.
9434 For example, cp_parser_initializer_clauses uses this function to
9435 determine whether a particular assignment-expression is in fact
9437 expression
= cp_parser_assignment_expression (parser
);
9438 /* Restore the old settings. */
9439 parser
->integral_constant_expression_p
9440 = saved_integral_constant_expression_p
;
9441 parser
->allow_non_integral_constant_expression_p
9442 = saved_allow_non_integral_constant_expression_p
;
9443 if (cxx_dialect
>= cxx11
)
9445 /* Require an rvalue constant expression here; that's what our
9446 callers expect. Reference constant expressions are handled
9447 separately in e.g. cp_parser_template_argument. */
9448 bool is_const
= potential_rvalue_constant_expression (expression
);
9449 parser
->non_integral_constant_expression_p
= !is_const
;
9450 if (!is_const
&& !allow_non_constant_p
)
9451 require_potential_rvalue_constant_expression (expression
);
9453 if (allow_non_constant_p
)
9454 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9455 parser
->non_integral_constant_expression_p
9456 = saved_non_integral_constant_expression_p
;
9461 /* Parse __builtin_offsetof.
9463 offsetof-expression:
9464 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9466 offsetof-member-designator:
9468 | offsetof-member-designator "." id-expression
9469 | offsetof-member-designator "[" expression "]"
9470 | offsetof-member-designator "->" id-expression */
9473 cp_parser_builtin_offsetof (cp_parser
*parser
)
9475 int save_ice_p
, save_non_ice_p
;
9480 location_t finish_loc
;
9482 /* We're about to accept non-integral-constant things, but will
9483 definitely yield an integral constant expression. Save and
9484 restore these values around our local parsing. */
9485 save_ice_p
= parser
->integral_constant_expression_p
;
9486 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9488 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9490 /* Consume the "__builtin_offsetof" token. */
9491 cp_lexer_consume_token (parser
->lexer
);
9492 /* Consume the opening `('. */
9493 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9494 /* Parse the type-id. */
9495 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9496 type
= cp_parser_type_id (parser
);
9497 /* Look for the `,'. */
9498 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9499 token
= cp_lexer_peek_token (parser
->lexer
);
9501 /* Build the (type *)null that begins the traditional offsetof macro. */
9503 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9504 tf_warning_or_error
);
9506 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9507 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9508 true, &dummy
, token
->location
);
9511 token
= cp_lexer_peek_token (parser
->lexer
);
9512 switch (token
->type
)
9514 case CPP_OPEN_SQUARE
:
9515 /* offsetof-member-designator "[" expression "]" */
9516 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9521 /* offsetof-member-designator "->" identifier */
9522 expr
= grok_array_decl (token
->location
, expr
,
9523 integer_zero_node
, false);
9527 /* offsetof-member-designator "." identifier */
9528 cp_lexer_consume_token (parser
->lexer
);
9529 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9534 case CPP_CLOSE_PAREN
:
9535 /* Consume the ")" token. */
9536 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9537 cp_lexer_consume_token (parser
->lexer
);
9541 /* Error. We know the following require will fail, but
9542 that gives the proper error message. */
9543 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9544 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9545 expr
= error_mark_node
;
9551 /* Make a location of the form:
9552 __builtin_offsetof (struct s, f)
9553 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9554 with caret at the type-id, ranging from the start of the
9555 "_builtin_offsetof" token to the close paren. */
9556 loc
= make_location (loc
, start_loc
, finish_loc
);
9557 /* The result will be an INTEGER_CST, so we need to explicitly
9558 preserve the location. */
9559 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9562 parser
->integral_constant_expression_p
= save_ice_p
;
9563 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9568 /* Parse a trait expression.
9570 Returns a representation of the expression, the underlying type
9571 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9574 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9577 tree type1
, type2
= NULL_TREE
;
9578 bool binary
= false;
9579 bool variadic
= false;
9583 case RID_HAS_NOTHROW_ASSIGN
:
9584 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9586 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9587 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9589 case RID_HAS_NOTHROW_COPY
:
9590 kind
= CPTK_HAS_NOTHROW_COPY
;
9592 case RID_HAS_TRIVIAL_ASSIGN
:
9593 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9595 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9596 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9598 case RID_HAS_TRIVIAL_COPY
:
9599 kind
= CPTK_HAS_TRIVIAL_COPY
;
9601 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9602 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9604 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9605 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9607 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9608 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9610 case RID_IS_ABSTRACT
:
9611 kind
= CPTK_IS_ABSTRACT
;
9613 case RID_IS_BASE_OF
:
9614 kind
= CPTK_IS_BASE_OF
;
9618 kind
= CPTK_IS_CLASS
;
9621 kind
= CPTK_IS_EMPTY
;
9624 kind
= CPTK_IS_ENUM
;
9627 kind
= CPTK_IS_FINAL
;
9629 case RID_IS_LITERAL_TYPE
:
9630 kind
= CPTK_IS_LITERAL_TYPE
;
9635 case RID_IS_POLYMORPHIC
:
9636 kind
= CPTK_IS_POLYMORPHIC
;
9638 case RID_IS_SAME_AS
:
9639 kind
= CPTK_IS_SAME_AS
;
9642 case RID_IS_STD_LAYOUT
:
9643 kind
= CPTK_IS_STD_LAYOUT
;
9645 case RID_IS_TRIVIAL
:
9646 kind
= CPTK_IS_TRIVIAL
;
9648 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9649 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9652 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9653 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9656 case RID_IS_TRIVIALLY_COPYABLE
:
9657 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9660 kind
= CPTK_IS_UNION
;
9662 case RID_UNDERLYING_TYPE
:
9663 kind
= CPTK_UNDERLYING_TYPE
;
9668 case RID_DIRECT_BASES
:
9669 kind
= CPTK_DIRECT_BASES
;
9675 /* Consume the token. */
9676 cp_lexer_consume_token (parser
->lexer
);
9678 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9681 type_id_in_expr_sentinel
s (parser
);
9682 type1
= cp_parser_type_id (parser
);
9685 if (type1
== error_mark_node
)
9686 return error_mark_node
;
9690 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9693 type_id_in_expr_sentinel
s (parser
);
9694 type2
= cp_parser_type_id (parser
);
9697 if (type2
== error_mark_node
)
9698 return error_mark_node
;
9702 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9704 cp_lexer_consume_token (parser
->lexer
);
9705 tree elt
= cp_parser_type_id (parser
);
9706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9708 cp_lexer_consume_token (parser
->lexer
);
9709 elt
= make_pack_expansion (elt
);
9711 if (elt
== error_mark_node
)
9712 return error_mark_node
;
9713 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9717 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9719 /* Complete the trait expression, which may mean either processing
9720 the trait expr now or saving it for template instantiation. */
9723 case CPTK_UNDERLYING_TYPE
:
9724 return finish_underlying_type (type1
);
9726 return finish_bases (type1
, false);
9727 case CPTK_DIRECT_BASES
:
9728 return finish_bases (type1
, true);
9730 return finish_trait_expr (kind
, type1
, type2
);
9734 /* Lambdas that appear in variable initializer or default argument scope
9735 get that in their mangling, so we need to record it. We might as well
9736 use the count for function and namespace scopes as well. */
9737 static GTY(()) tree lambda_scope
;
9738 static GTY(()) int lambda_count
;
9739 struct GTY(()) tree_int
9744 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9747 start_lambda_scope (tree decl
)
9751 /* Once we're inside a function, we ignore other scopes and just push
9752 the function again so that popping works properly. */
9753 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9754 decl
= current_function_decl
;
9755 ti
.t
= lambda_scope
;
9756 ti
.i
= lambda_count
;
9757 vec_safe_push (lambda_scope_stack
, ti
);
9758 if (lambda_scope
!= decl
)
9760 /* Don't reset the count if we're still in the same function. */
9761 lambda_scope
= decl
;
9767 record_lambda_scope (tree lambda
)
9769 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9770 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9774 finish_lambda_scope (void)
9776 tree_int
*p
= &lambda_scope_stack
->last ();
9777 if (lambda_scope
!= p
->t
)
9779 lambda_scope
= p
->t
;
9780 lambda_count
= p
->i
;
9782 lambda_scope_stack
->pop ();
9785 /* Parse a lambda expression.
9788 lambda-introducer lambda-declarator [opt] compound-statement
9790 Returns a representation of the expression. */
9793 cp_parser_lambda_expression (cp_parser
* parser
)
9795 tree lambda_expr
= build_lambda_expr ();
9798 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9799 cp_token_position start
= 0;
9801 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9803 if (cp_unevaluated_operand
)
9805 if (!token
->error_reported
)
9807 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9808 "lambda-expression in unevaluated context");
9809 token
->error_reported
= true;
9813 else if (parser
->in_template_argument_list_p
)
9815 if (!token
->error_reported
)
9817 error_at (token
->location
, "lambda-expression in template-argument");
9818 token
->error_reported
= true;
9823 /* We may be in the middle of deferred access check. Disable
9825 push_deferring_access_checks (dk_no_deferred
);
9827 cp_parser_lambda_introducer (parser
, lambda_expr
);
9829 type
= begin_lambda_type (lambda_expr
);
9830 if (type
== error_mark_node
)
9831 return error_mark_node
;
9833 record_lambda_scope (lambda_expr
);
9835 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9836 determine_visibility (TYPE_NAME (type
));
9838 /* Now that we've started the type, add the capture fields for any
9839 explicit captures. */
9840 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9843 /* Inside the class, surrounding template-parameter-lists do not apply. */
9844 unsigned int saved_num_template_parameter_lists
9845 = parser
->num_template_parameter_lists
;
9846 unsigned char in_statement
= parser
->in_statement
;
9847 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9848 bool fully_implicit_function_template_p
9849 = parser
->fully_implicit_function_template_p
;
9850 tree implicit_template_parms
= parser
->implicit_template_parms
;
9851 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9852 bool auto_is_implicit_function_template_parm_p
9853 = parser
->auto_is_implicit_function_template_parm_p
;
9855 parser
->num_template_parameter_lists
= 0;
9856 parser
->in_statement
= 0;
9857 parser
->in_switch_statement_p
= false;
9858 parser
->fully_implicit_function_template_p
= false;
9859 parser
->implicit_template_parms
= 0;
9860 parser
->implicit_template_scope
= 0;
9861 parser
->auto_is_implicit_function_template_parm_p
= false;
9863 /* By virtue of defining a local class, a lambda expression has access to
9864 the private variables of enclosing classes. */
9866 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9868 if (ok
&& cp_parser_error_occurred (parser
))
9873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9874 && cp_parser_start_tentative_firewall (parser
))
9876 cp_parser_lambda_body (parser
, lambda_expr
);
9878 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9880 if (cp_parser_skip_to_closing_brace (parser
))
9881 cp_lexer_consume_token (parser
->lexer
);
9884 /* The capture list was built up in reverse order; fix that now. */
9885 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9886 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9889 maybe_add_lambda_conv_op (type
);
9891 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9893 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9894 parser
->in_statement
= in_statement
;
9895 parser
->in_switch_statement_p
= in_switch_statement_p
;
9896 parser
->fully_implicit_function_template_p
9897 = fully_implicit_function_template_p
;
9898 parser
->implicit_template_parms
= implicit_template_parms
;
9899 parser
->implicit_template_scope
= implicit_template_scope
;
9900 parser
->auto_is_implicit_function_template_parm_p
9901 = auto_is_implicit_function_template_parm_p
;
9904 /* This field is only used during parsing of the lambda. */
9905 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9907 /* This lambda shouldn't have any proxies left at this point. */
9908 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9909 /* And now that we're done, push proxies for an enclosing lambda. */
9910 insert_pending_capture_proxies ();
9913 lambda_expr
= build_lambda_object (lambda_expr
);
9915 lambda_expr
= error_mark_node
;
9917 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9919 pop_deferring_access_checks ();
9924 /* Parse the beginning of a lambda expression.
9927 [ lambda-capture [opt] ]
9929 LAMBDA_EXPR is the current representation of the lambda expression. */
9932 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9934 /* Need commas after the first capture. */
9937 /* Eat the leading `['. */
9938 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9940 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9942 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9944 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9945 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9947 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9949 cp_lexer_consume_token (parser
->lexer
);
9953 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9955 cp_token
* capture_token
;
9957 tree capture_init_expr
;
9958 cp_id_kind idk
= CP_ID_KIND_NONE
;
9959 bool explicit_init_p
= false;
9961 enum capture_kind_type
9966 enum capture_kind_type capture_kind
= BY_COPY
;
9968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9970 error ("expected end of capture-list");
9977 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9979 /* Possibly capture `this'. */
9980 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9982 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9984 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9985 "with by-copy capture default");
9986 cp_lexer_consume_token (parser
->lexer
);
9987 add_capture (lambda_expr
,
9988 /*id=*/this_identifier
,
9989 /*initializer=*/finish_this_expr (),
9990 /*by_reference_p=*/true,
9995 /* Possibly capture `*this'. */
9996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
9997 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
9999 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10000 if (cxx_dialect
< cxx1z
)
10001 pedwarn (loc
, 0, "%<*this%> capture only available with "
10002 "-std=c++1z or -std=gnu++1z");
10003 cp_lexer_consume_token (parser
->lexer
);
10004 cp_lexer_consume_token (parser
->lexer
);
10005 add_capture (lambda_expr
,
10006 /*id=*/this_identifier
,
10007 /*initializer=*/finish_this_expr (),
10008 /*by_reference_p=*/false,
10013 /* Remember whether we want to capture as a reference or not. */
10014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10016 capture_kind
= BY_REFERENCE
;
10017 cp_lexer_consume_token (parser
->lexer
);
10020 /* Get the identifier. */
10021 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10022 capture_id
= cp_parser_identifier (parser
);
10024 if (capture_id
== error_mark_node
)
10025 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10026 delimiters, but I modified this to stop on unnested ']' as well. It
10027 was already changed to stop on unnested '}', so the
10028 "closing_parenthesis" name is no more misleading with my change. */
10030 cp_parser_skip_to_closing_parenthesis (parser
,
10031 /*recovering=*/true,
10033 /*consume_paren=*/true);
10037 /* Find the initializer for this capture. */
10038 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10039 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10040 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10042 bool direct
, non_constant
;
10043 /* An explicit initializer exists. */
10044 if (cxx_dialect
< cxx14
)
10045 pedwarn (input_location
, 0,
10046 "lambda capture initializers "
10047 "only available with -std=c++14 or -std=gnu++14");
10048 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10050 explicit_init_p
= true;
10051 if (capture_init_expr
== NULL_TREE
)
10053 error ("empty initializer for lambda init-capture");
10054 capture_init_expr
= error_mark_node
;
10059 const char* error_msg
;
10061 /* Turn the identifier into an id-expression. */
10063 = cp_parser_lookup_name_simple (parser
, capture_id
,
10064 capture_token
->location
);
10066 if (capture_init_expr
== error_mark_node
)
10068 unqualified_name_lookup_error (capture_id
);
10071 else if (DECL_P (capture_init_expr
)
10072 && (!VAR_P (capture_init_expr
)
10073 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10075 error_at (capture_token
->location
,
10076 "capture of non-variable %qD ",
10077 capture_init_expr
);
10078 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10079 "%q#D declared here", capture_init_expr
);
10082 if (VAR_P (capture_init_expr
)
10083 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10085 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10086 "%qD with non-automatic storage duration",
10087 capture_init_expr
))
10088 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10089 "%q#D declared here", capture_init_expr
);
10094 = finish_id_expression
10099 /*integral_constant_expression_p=*/false,
10100 /*allow_non_integral_constant_expression_p=*/false,
10101 /*non_integral_constant_expression_p=*/NULL
,
10102 /*template_p=*/false,
10104 /*address_p=*/false,
10105 /*template_arg_p=*/false,
10107 capture_token
->location
);
10109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10111 cp_lexer_consume_token (parser
->lexer
);
10112 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10115 check_for_bare_parameter_packs (capture_init_expr
);
10118 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10119 && !explicit_init_p
)
10121 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10122 && capture_kind
== BY_COPY
)
10123 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10124 "of %qD redundant with by-copy capture default",
10126 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10127 && capture_kind
== BY_REFERENCE
)
10128 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10129 "capture of %qD redundant with by-reference capture "
10130 "default", capture_id
);
10133 add_capture (lambda_expr
,
10136 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10140 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10143 /* Parse the (optional) middle of a lambda expression.
10146 < template-parameter-list [opt] >
10147 ( parameter-declaration-clause [opt] )
10148 attribute-specifier [opt]
10149 decl-specifier-seq [opt]
10150 exception-specification [opt]
10151 lambda-return-type-clause [opt]
10153 LAMBDA_EXPR is the current representation of the lambda expression. */
10156 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10158 /* 5.1.1.4 of the standard says:
10159 If a lambda-expression does not include a lambda-declarator, it is as if
10160 the lambda-declarator were ().
10161 This means an empty parameter list, no attributes, and no exception
10163 tree param_list
= void_list_node
;
10164 tree attributes
= NULL_TREE
;
10165 tree exception_spec
= NULL_TREE
;
10166 tree template_param_list
= NULL_TREE
;
10167 tree tx_qual
= NULL_TREE
;
10168 cp_decl_specifier_seq lambda_specs
;
10169 clear_decl_specs (&lambda_specs
);
10171 /* The template-parameter-list is optional, but must begin with
10172 an opening angle if present. */
10173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10175 if (cxx_dialect
< cxx14
)
10176 pedwarn (parser
->lexer
->next_token
->location
, 0,
10177 "lambda templates are only available with "
10178 "-std=c++14 or -std=gnu++14");
10180 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10181 "ISO C++ does not support lambda templates");
10183 cp_lexer_consume_token (parser
->lexer
);
10185 template_param_list
= cp_parser_template_parameter_list (parser
);
10187 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10189 /* We just processed one more parameter list. */
10190 ++parser
->num_template_parameter_lists
;
10193 /* The parameter-declaration-clause is optional (unless
10194 template-parameter-list was given), but must begin with an
10195 opening parenthesis if present. */
10196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10198 cp_lexer_consume_token (parser
->lexer
);
10200 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10202 /* Parse parameters. */
10203 param_list
= cp_parser_parameter_declaration_clause (parser
);
10205 /* Default arguments shall not be specified in the
10206 parameter-declaration-clause of a lambda-declarator. */
10207 if (cxx_dialect
< cxx14
)
10208 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10209 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10210 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10211 "default argument specified for lambda parameter");
10213 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10215 attributes
= cp_parser_attributes_opt (parser
);
10217 /* In the decl-specifier-seq of the lambda-declarator, each
10218 decl-specifier shall either be mutable or constexpr. */
10219 int declares_class_or_enum
;
10220 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10221 cp_parser_decl_specifier_seq (parser
,
10222 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10223 &lambda_specs
, &declares_class_or_enum
);
10224 if (lambda_specs
.storage_class
== sc_mutable
)
10226 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10227 if (lambda_specs
.conflicting_specifiers_p
)
10228 error_at (lambda_specs
.locations
[ds_storage_class
],
10229 "duplicate %<mutable%>");
10232 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10234 /* Parse optional exception specification. */
10235 exception_spec
= cp_parser_exception_specification_opt (parser
);
10237 /* Parse optional trailing return type. */
10238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10240 cp_lexer_consume_token (parser
->lexer
);
10241 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10242 = cp_parser_trailing_type_id (parser
);
10245 /* The function parameters must be in scope all the way until after the
10246 trailing-return-type in case of decltype. */
10247 pop_bindings_and_leave_scope ();
10249 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10250 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10252 /* Create the function call operator.
10254 Messing with declarators like this is no uglier than building up the
10255 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10258 cp_decl_specifier_seq return_type_specs
;
10259 cp_declarator
* declarator
;
10264 clear_decl_specs (&return_type_specs
);
10265 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10266 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
10268 /* Maybe we will deduce the return type later. */
10269 return_type_specs
.type
= make_auto ();
10271 if (lambda_specs
.locations
[ds_constexpr
])
10273 if (cxx_dialect
>= cxx1z
)
10274 return_type_specs
.locations
[ds_constexpr
]
10275 = lambda_specs
.locations
[ds_constexpr
];
10277 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10278 "lambda only available with -std=c++1z or -std=gnu++1z");
10281 p
= obstack_alloc (&declarator_obstack
, 0);
10283 declarator
= make_id_declarator (NULL_TREE
, cp_operator_id (CALL_EXPR
),
10286 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10287 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10288 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10289 VIRT_SPEC_UNSPECIFIED
,
10293 /*late_return_type=*/NULL_TREE
,
10294 /*requires_clause*/NULL_TREE
);
10295 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10297 fco
= grokmethod (&return_type_specs
,
10300 if (fco
!= error_mark_node
)
10302 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10303 DECL_ARTIFICIAL (fco
) = 1;
10304 /* Give the object parameter a different name. */
10305 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10306 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10307 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10309 if (template_param_list
)
10311 fco
= finish_member_template_decl (fco
);
10312 finish_template_decl (template_param_list
);
10313 --parser
->num_template_parameter_lists
;
10315 else if (parser
->fully_implicit_function_template_p
)
10316 fco
= finish_fully_implicit_template (parser
, fco
);
10318 finish_member_declaration (fco
);
10320 obstack_free (&declarator_obstack
, p
);
10322 return (fco
!= error_mark_node
);
10326 /* Parse the body of a lambda expression, which is simply
10330 but which requires special handling.
10331 LAMBDA_EXPR is the current representation of the lambda expression. */
10334 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10336 bool nested
= (current_function_decl
!= NULL_TREE
);
10337 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10339 push_function_context ();
10341 /* Still increment function_depth so that we don't GC in the
10342 middle of an expression. */
10344 vec
<tree
> omp_privatization_save
;
10345 save_omp_privatization_clauses (omp_privatization_save
);
10346 /* Clear this in case we're in the middle of a default argument. */
10347 parser
->local_variables_forbidden_p
= false;
10349 /* Finish the function call operator
10351 + late_parsing_for_member
10352 + function_definition_after_declarator
10353 + ctor_initializer_opt_and_function_body */
10355 tree fco
= lambda_function (lambda_expr
);
10358 tree compound_stmt
;
10361 /* Let the front end know that we are going to be defining this
10363 start_preparsed_function (fco
,
10365 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
10367 start_lambda_scope (fco
);
10368 body
= begin_function_body ();
10370 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10373 /* Push the proxies for any explicit captures. */
10374 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
10375 cap
= TREE_CHAIN (cap
))
10376 build_capture_proxy (TREE_PURPOSE (cap
));
10378 compound_stmt
= begin_compound_stmt (0);
10380 /* 5.1.1.4 of the standard says:
10381 If a lambda-expression does not include a trailing-return-type, it
10382 is as if the trailing-return-type denotes the following type:
10383 * if the compound-statement is of the form
10384 { return attribute-specifier [opt] expression ; }
10385 the type of the returned expression after lvalue-to-rvalue
10386 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10387 (_conv.array_ 4.2), and function-to-pointer conversion
10389 * otherwise, void. */
10391 /* In a lambda that has neither a lambda-return-type-clause
10392 nor a deducible form, errors should be reported for return statements
10393 in the body. Since we used void as the placeholder return type, parsing
10394 the body as usual will give such desired behavior. */
10395 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10396 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10397 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10399 tree expr
= NULL_TREE
;
10400 cp_id_kind idk
= CP_ID_KIND_NONE
;
10402 /* Parse tentatively in case there's more after the initial return
10404 cp_parser_parse_tentatively (parser
);
10406 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10408 expr
= cp_parser_expression (parser
, &idk
);
10410 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10411 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10413 if (cp_parser_parse_definitely (parser
))
10415 if (!processing_template_decl
)
10417 tree type
= lambda_return_type (expr
);
10418 apply_deduced_return_type (fco
, type
);
10419 if (type
== error_mark_node
)
10420 expr
= error_mark_node
;
10423 /* Will get error here if type not deduced yet. */
10424 finish_return_stmt (expr
);
10432 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10433 cp_parser_label_declaration (parser
);
10434 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10435 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10438 finish_compound_stmt (compound_stmt
);
10441 finish_function_body (body
);
10442 finish_lambda_scope ();
10444 /* Finish the function and generate code for it if necessary. */
10445 tree fn
= finish_function (/*inline*/2);
10447 /* Only expand if the call op is not a template. */
10448 if (!DECL_TEMPLATE_INFO (fco
))
10449 expand_or_defer_fn (fn
);
10452 restore_omp_privatization_clauses (omp_privatization_save
);
10453 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10455 pop_function_context();
10460 /* Statements [gram.stmt.stmt] */
10462 /* Parse a statement.
10466 expression-statement
10468 selection-statement
10469 iteration-statement
10471 declaration-statement
10478 attribute-specifier-seq (opt) expression-statement
10479 attribute-specifier-seq (opt) compound-statement
10480 attribute-specifier-seq (opt) selection-statement
10481 attribute-specifier-seq (opt) iteration-statement
10482 attribute-specifier-seq (opt) jump-statement
10483 declaration-statement
10484 attribute-specifier-seq (opt) try-block
10487 expression-statement
10495 IN_COMPOUND is true when the statement is nested inside a
10496 cp_parser_compound_statement; this matters for certain pragmas.
10498 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10499 is a (possibly labeled) if statement which is not enclosed in braces
10500 and has an else clause. This is used to implement -Wparentheses.
10502 CHAIN is a vector of if-else-if conditions. */
10505 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10506 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
10508 tree statement
, std_attrs
= NULL_TREE
;
10510 location_t statement_location
, attrs_location
;
10515 /* There is no statement yet. */
10516 statement
= NULL_TREE
;
10518 saved_token_sentinel
saved_tokens (parser
->lexer
);
10519 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10520 if (c_dialect_objc ())
10521 /* In obj-c++, seeing '[[' might be the either the beginning of
10522 c++11 attributes, or a nested objc-message-expression. So
10523 let's parse the c++11 attributes tentatively. */
10524 cp_parser_parse_tentatively (parser
);
10525 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10526 if (c_dialect_objc ())
10528 if (!cp_parser_parse_definitely (parser
))
10529 std_attrs
= NULL_TREE
;
10532 /* Peek at the next token. */
10533 token
= cp_lexer_peek_token (parser
->lexer
);
10534 /* Remember the location of the first token in the statement. */
10535 statement_location
= token
->location
;
10536 /* If this is a keyword, then that will often determine what kind of
10537 statement we have. */
10538 if (token
->type
== CPP_KEYWORD
)
10540 enum rid keyword
= token
->keyword
;
10546 /* Looks like a labeled-statement with a case label.
10547 Parse the label, and then use tail recursion to parse
10549 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10550 in_compound
= false;
10555 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10561 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10565 if (!flag_cilkplus
)
10567 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10568 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10569 cp_lexer_consume_token (parser
->lexer
);
10570 statement
= error_mark_node
;
10573 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10580 statement
= cp_parser_jump_statement (parser
);
10583 case RID_CILK_SYNC
:
10584 cp_lexer_consume_token (parser
->lexer
);
10587 tree sync_expr
= build_cilk_sync ();
10588 SET_EXPR_LOCATION (sync_expr
,
10590 statement
= finish_expr_stmt (sync_expr
);
10594 error_at (token
->location
, "-fcilkplus must be enabled to use"
10595 " %<_Cilk_sync%>");
10596 statement
= error_mark_node
;
10598 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10601 /* Objective-C++ exception-handling constructs. */
10604 case RID_AT_FINALLY
:
10605 case RID_AT_SYNCHRONIZED
:
10607 statement
= cp_parser_objc_statement (parser
);
10611 statement
= cp_parser_try_block (parser
);
10614 case RID_NAMESPACE
:
10615 /* This must be a namespace alias definition. */
10616 cp_parser_declaration_statement (parser
);
10619 case RID_TRANSACTION_ATOMIC
:
10620 case RID_TRANSACTION_RELAXED
:
10621 case RID_SYNCHRONIZED
:
10622 case RID_ATOMIC_NOEXCEPT
:
10623 case RID_ATOMIC_CANCEL
:
10624 statement
= cp_parser_transaction (parser
, token
);
10626 case RID_TRANSACTION_CANCEL
:
10627 statement
= cp_parser_transaction_cancel (parser
);
10631 /* It might be a keyword like `int' that can start a
10632 declaration-statement. */
10636 else if (token
->type
== CPP_NAME
)
10638 /* If the next token is a `:', then we are looking at a
10639 labeled-statement. */
10640 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10641 if (token
->type
== CPP_COLON
)
10643 /* Looks like a labeled-statement with an ordinary label.
10644 Parse the label, and then use tail recursion to parse
10647 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10648 in_compound
= false;
10652 /* Anything that starts with a `{' must be a compound-statement. */
10653 else if (token
->type
== CPP_OPEN_BRACE
)
10654 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10655 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10656 a statement all its own. */
10657 else if (token
->type
== CPP_PRAGMA
)
10659 /* Only certain OpenMP pragmas are attached to statements, and thus
10660 are considered statements themselves. All others are not. In
10661 the context of a compound, accept the pragma as a "statement" and
10662 return so that we can check for a close brace. Otherwise we
10663 require a real statement and must go back and read one. */
10665 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10666 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10670 else if (token
->type
== CPP_EOF
)
10672 cp_parser_error (parser
, "expected statement");
10676 /* Everything else must be a declaration-statement or an
10677 expression-statement. Try for the declaration-statement
10678 first, unless we are looking at a `;', in which case we know that
10679 we have an expression-statement. */
10682 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10684 if (std_attrs
!= NULL_TREE
)
10686 /* Attributes should be parsed as part of the the
10687 declaration, so let's un-parse them. */
10688 saved_tokens
.rollback();
10689 std_attrs
= NULL_TREE
;
10692 cp_parser_parse_tentatively (parser
);
10693 /* Try to parse the declaration-statement. */
10694 cp_parser_declaration_statement (parser
);
10695 /* If that worked, we're done. */
10696 if (cp_parser_parse_definitely (parser
))
10699 /* Look for an expression-statement instead. */
10700 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10702 /* Handle [[fallthrough]];. */
10703 if (attribute_fallthrough_p (std_attrs
))
10705 /* The next token after the fallthrough attribute is ';'. */
10706 if (statement
== NULL_TREE
)
10708 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10709 statement
= build_call_expr_internal_loc (statement_location
,
10711 void_type_node
, 0);
10712 finish_expr_stmt (statement
);
10715 warning_at (statement_location
, OPT_Wattributes
,
10716 "%<fallthrough%> attribute not followed by %<;%>");
10717 std_attrs
= NULL_TREE
;
10721 /* Set the line number for the statement. */
10722 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10723 SET_EXPR_LOCATION (statement
, statement_location
);
10725 /* Allow "[[fallthrough]];", but warn otherwise. */
10726 if (std_attrs
!= NULL_TREE
)
10727 warning_at (attrs_location
,
10729 "attributes at the beginning of statement are ignored");
10732 /* Parse the label for a labeled-statement, i.e.
10735 case constant-expression :
10739 case constant-expression ... constant-expression : statement
10741 When a label is parsed without errors, the label is added to the
10742 parse tree by the finish_* functions, so this function doesn't
10743 have to return the label. */
10746 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10749 tree label
= NULL_TREE
;
10750 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10752 /* The next token should be an identifier. */
10753 token
= cp_lexer_peek_token (parser
->lexer
);
10754 if (token
->type
!= CPP_NAME
10755 && token
->type
!= CPP_KEYWORD
)
10757 cp_parser_error (parser
, "expected labeled-statement");
10761 /* Remember whether this case or a user-defined label is allowed to fall
10763 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10765 parser
->colon_corrects_to_scope_p
= false;
10766 switch (token
->keyword
)
10770 tree expr
, expr_hi
;
10771 cp_token
*ellipsis
;
10773 /* Consume the `case' token. */
10774 cp_lexer_consume_token (parser
->lexer
);
10775 /* Parse the constant-expression. */
10776 expr
= cp_parser_constant_expression (parser
);
10777 if (check_for_bare_parameter_packs (expr
))
10778 expr
= error_mark_node
;
10780 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10781 if (ellipsis
->type
== CPP_ELLIPSIS
)
10783 /* Consume the `...' token. */
10784 cp_lexer_consume_token (parser
->lexer
);
10785 expr_hi
= cp_parser_constant_expression (parser
);
10786 if (check_for_bare_parameter_packs (expr_hi
))
10787 expr_hi
= error_mark_node
;
10789 /* We don't need to emit warnings here, as the common code
10790 will do this for us. */
10793 expr_hi
= NULL_TREE
;
10795 if (parser
->in_switch_statement_p
)
10797 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10798 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10799 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10802 error_at (token
->location
,
10803 "case label %qE not within a switch statement",
10809 /* Consume the `default' token. */
10810 cp_lexer_consume_token (parser
->lexer
);
10812 if (parser
->in_switch_statement_p
)
10814 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10815 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10816 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10819 error_at (token
->location
, "case label not within a switch statement");
10823 /* Anything else must be an ordinary label. */
10824 label
= finish_label_stmt (cp_parser_identifier (parser
));
10825 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
10826 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
10830 /* Require the `:' token. */
10831 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10833 /* An ordinary label may optionally be followed by attributes.
10834 However, this is only permitted if the attributes are then
10835 followed by a semicolon. This is because, for backward
10836 compatibility, when parsing
10837 lab: __attribute__ ((unused)) int i;
10838 we want the attribute to attach to "i", not "lab". */
10839 if (label
!= NULL_TREE
10840 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10843 cp_parser_parse_tentatively (parser
);
10844 attrs
= cp_parser_gnu_attributes_opt (parser
);
10845 if (attrs
== NULL_TREE
10846 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10847 cp_parser_abort_tentative_parse (parser
);
10848 else if (!cp_parser_parse_definitely (parser
))
10851 attributes
= chainon (attributes
, attrs
);
10854 if (attributes
!= NULL_TREE
)
10855 cplus_decl_attributes (&label
, attributes
, 0);
10857 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10860 /* Parse an expression-statement.
10862 expression-statement:
10865 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10866 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10867 indicates whether this expression-statement is part of an
10868 expression statement. */
10871 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10873 tree statement
= NULL_TREE
;
10874 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10875 location_t loc
= token
->location
;
10877 /* There might be attribute fallthrough. */
10878 tree attr
= cp_parser_gnu_attributes_opt (parser
);
10880 /* If the next token is a ';', then there is no expression
10882 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10884 statement
= cp_parser_expression (parser
);
10885 if (statement
== error_mark_node
10886 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10888 cp_parser_skip_to_end_of_block_or_statement (parser
);
10889 return error_mark_node
;
10893 /* Handle [[fallthrough]];. */
10894 if (attribute_fallthrough_p (attr
))
10896 /* The next token after the fallthrough attribute is ';'. */
10897 if (statement
== NULL_TREE
)
10898 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10899 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
10900 void_type_node
, 0);
10902 warning_at (loc
, OPT_Wattributes
,
10903 "%<fallthrough%> attribute not followed by %<;%>");
10907 /* Allow "[[fallthrough]];", but warn otherwise. */
10908 if (attr
!= NULL_TREE
)
10909 warning_at (loc
, OPT_Wattributes
,
10910 "attributes at the beginning of statement are ignored");
10912 /* Give a helpful message for "A<T>::type t;" and the like. */
10913 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10914 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10916 if (TREE_CODE (statement
) == SCOPE_REF
)
10917 error_at (token
->location
, "need %<typename%> before %qE because "
10918 "%qT is a dependent scope",
10919 statement
, TREE_OPERAND (statement
, 0));
10920 else if (is_overloaded_fn (statement
)
10921 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10924 tree fn
= get_first_fn (statement
);
10925 error_at (token
->location
,
10926 "%<%T::%D%> names the constructor, not the type",
10927 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10931 /* Consume the final `;'. */
10932 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10934 if (in_statement_expr
10935 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10936 /* This is the final expression statement of a statement
10938 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10939 else if (statement
)
10940 statement
= finish_expr_stmt (statement
);
10945 /* Parse a compound-statement.
10947 compound-statement:
10948 { statement-seq [opt] }
10952 compound-statement:
10953 { label-declaration-seq [opt] statement-seq [opt] }
10955 label-declaration-seq:
10957 label-declaration-seq label-declaration
10959 Returns a tree representing the statement. */
10962 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10963 int bcs_flags
, bool function_body
)
10965 tree compound_stmt
;
10967 /* Consume the `{'. */
10968 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10969 return error_mark_node
;
10970 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10971 && !function_body
&& cxx_dialect
< cxx14
)
10972 pedwarn (input_location
, OPT_Wpedantic
,
10973 "compound-statement in constexpr function");
10974 /* Begin the compound-statement. */
10975 compound_stmt
= begin_compound_stmt (bcs_flags
);
10976 /* If the next keyword is `__label__' we have a label declaration. */
10977 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10978 cp_parser_label_declaration (parser
);
10979 /* Parse an (optional) statement-seq. */
10980 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10981 /* Finish the compound-statement. */
10982 finish_compound_stmt (compound_stmt
);
10983 /* Consume the `}'. */
10984 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10986 return compound_stmt
;
10989 /* Parse an (optional) statement-seq.
10993 statement-seq [opt] statement */
10996 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10998 /* Scan statements until there aren't any more. */
11001 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11003 /* If we are looking at a `}', then we have run out of
11004 statements; the same is true if we have reached the end
11005 of file, or have stumbled upon a stray '@end'. */
11006 if (token
->type
== CPP_CLOSE_BRACE
11007 || token
->type
== CPP_EOF
11008 || token
->type
== CPP_PRAGMA_EOL
11009 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11012 /* If we are in a compound statement and find 'else' then
11013 something went wrong. */
11014 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11016 if (parser
->in_statement
& IN_IF_STMT
)
11020 token
= cp_lexer_consume_token (parser
->lexer
);
11021 error_at (token
->location
, "%<else%> without a previous %<if%>");
11025 /* Parse the statement. */
11026 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11030 /* Return true if we're looking at (init; cond), false otherwise. */
11033 cp_parser_init_statement_p (cp_parser
*parser
)
11035 /* Save tokens so that we can put them back. */
11036 cp_lexer_save_tokens (parser
->lexer
);
11038 /* Look for ';' that is not nested in () or {}. */
11039 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11040 /*recovering=*/false,
11042 /*consume_paren=*/false);
11044 /* Roll back the tokens we skipped. */
11045 cp_lexer_rollback_tokens (parser
->lexer
);
11050 /* Parse a selection-statement.
11052 selection-statement:
11053 if ( init-statement [opt] condition ) statement
11054 if ( init-statement [opt] condition ) statement else statement
11055 switch ( init-statement [opt] condition ) statement
11057 Returns the new IF_STMT or SWITCH_STMT.
11059 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11060 is a (possibly labeled) if statement which is not enclosed in
11061 braces and has an else clause. This is used to implement
11064 CHAIN is a vector of if-else-if conditions. This is used to implement
11065 -Wduplicated-cond. */
11068 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11073 token_indent_info guard_tinfo
;
11078 /* Peek at the next token. */
11079 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11080 guard_tinfo
= get_token_indent_info (token
);
11082 /* See what kind of keyword it is. */
11083 keyword
= token
->keyword
;
11093 if (keyword
== RID_IF
11094 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11098 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11099 if (cxx_dialect
< cxx1z
&& !in_system_header_at (tok
->location
))
11100 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11101 "with -std=c++1z or -std=gnu++1z");
11104 /* Look for the `('. */
11105 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11107 cp_parser_skip_to_end_of_statement (parser
);
11108 return error_mark_node
;
11111 /* Begin the selection-statement. */
11112 if (keyword
== RID_IF
)
11114 statement
= begin_if_stmt ();
11115 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11118 statement
= begin_switch_stmt ();
11120 /* Parse the optional init-statement. */
11121 if (cp_parser_init_statement_p (parser
))
11124 if (cxx_dialect
< cxx1z
)
11125 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11126 "init-statement in selection statements only available "
11127 "with -std=c++1z or -std=gnu++1z");
11128 cp_parser_init_statement (parser
, &decl
);
11131 /* Parse the condition. */
11132 condition
= cp_parser_condition (parser
);
11133 /* Look for the `)'. */
11134 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11135 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11136 /*consume_paren=*/true);
11138 if (keyword
== RID_IF
)
11141 unsigned char in_statement
;
11143 /* Add the condition. */
11144 condition
= finish_if_stmt_cond (condition
, statement
);
11146 if (warn_duplicated_cond
)
11147 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11150 /* Parse the then-clause. */
11151 in_statement
= parser
->in_statement
;
11152 parser
->in_statement
|= IN_IF_STMT
;
11154 /* Outside a template, the non-selected branch of a constexpr
11155 if is a 'discarded statement', i.e. unevaluated. */
11156 bool was_discarded
= in_discarded_stmt
;
11157 bool discard_then
= (cx
&& !processing_template_decl
11158 && integer_zerop (condition
));
11161 in_discarded_stmt
= true;
11162 ++c_inhibit_evaluation_warnings
;
11165 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11168 parser
->in_statement
= in_statement
;
11170 finish_then_clause (statement
);
11174 THEN_CLAUSE (statement
) = NULL_TREE
;
11175 in_discarded_stmt
= was_discarded
;
11176 --c_inhibit_evaluation_warnings
;
11179 /* If the next token is `else', parse the else-clause. */
11180 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11183 bool discard_else
= (cx
&& !processing_template_decl
11184 && integer_nonzerop (condition
));
11187 in_discarded_stmt
= true;
11188 ++c_inhibit_evaluation_warnings
;
11192 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11193 /* Consume the `else' keyword. */
11194 cp_lexer_consume_token (parser
->lexer
);
11195 if (warn_duplicated_cond
)
11197 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11201 /* We've got "if (COND) else if (COND2)". Start
11202 the condition chain and add COND as the first
11204 chain
= new vec
<tree
> ();
11205 if (!CONSTANT_CLASS_P (condition
)
11206 && !TREE_SIDE_EFFECTS (condition
))
11208 /* Wrap it in a NOP_EXPR so that we can set the
11209 location of the condition. */
11210 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11212 SET_EXPR_LOCATION (e
, token
->location
);
11213 chain
->safe_push (e
);
11216 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11219 /* This is if-else without subsequent if. Zap the
11220 condition chain; we would have already warned at
11226 begin_else_clause (statement
);
11227 /* Parse the else-clause. */
11228 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11229 guard_tinfo
, chain
);
11231 finish_else_clause (statement
);
11233 /* If we are currently parsing a then-clause, then
11234 IF_P will not be NULL. We set it to true to
11235 indicate that this if statement has an else clause.
11236 This may trigger the Wparentheses warning below
11237 when we get back up to the parent if statement. */
11243 ELSE_CLAUSE (statement
) = NULL_TREE
;
11244 in_discarded_stmt
= was_discarded
;
11245 --c_inhibit_evaluation_warnings
;
11250 /* This if statement does not have an else clause. If
11251 NESTED_IF is true, then the then-clause has an if
11252 statement which does have an else clause. We warn
11253 about the potential ambiguity. */
11255 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11256 "suggest explicit braces to avoid ambiguous"
11258 if (warn_duplicated_cond
)
11260 /* We don't need the condition chain anymore. */
11266 /* Now we're all done with the if-statement. */
11267 finish_if_stmt (statement
);
11271 bool in_switch_statement_p
;
11272 unsigned char in_statement
;
11274 /* Add the condition. */
11275 finish_switch_cond (condition
, statement
);
11277 /* Parse the body of the switch-statement. */
11278 in_switch_statement_p
= parser
->in_switch_statement_p
;
11279 in_statement
= parser
->in_statement
;
11280 parser
->in_switch_statement_p
= true;
11281 parser
->in_statement
|= IN_SWITCH_STMT
;
11282 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11284 parser
->in_switch_statement_p
= in_switch_statement_p
;
11285 parser
->in_statement
= in_statement
;
11287 /* Now we're all done with the switch-statement. */
11288 finish_switch_stmt (statement
);
11296 cp_parser_error (parser
, "expected selection-statement");
11297 return error_mark_node
;
11301 /* Parse a condition.
11305 type-specifier-seq declarator = initializer-clause
11306 type-specifier-seq declarator braced-init-list
11311 type-specifier-seq declarator asm-specification [opt]
11312 attributes [opt] = assignment-expression
11314 Returns the expression that should be tested. */
11317 cp_parser_condition (cp_parser
* parser
)
11319 cp_decl_specifier_seq type_specifiers
;
11320 const char *saved_message
;
11321 int declares_class_or_enum
;
11323 /* Try the declaration first. */
11324 cp_parser_parse_tentatively (parser
);
11325 /* New types are not allowed in the type-specifier-seq for a
11327 saved_message
= parser
->type_definition_forbidden_message
;
11328 parser
->type_definition_forbidden_message
11329 = G_("types may not be defined in conditions");
11330 /* Parse the type-specifier-seq. */
11331 cp_parser_decl_specifier_seq (parser
,
11332 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11334 &declares_class_or_enum
);
11335 /* Restore the saved message. */
11336 parser
->type_definition_forbidden_message
= saved_message
;
11337 /* If all is well, we might be looking at a declaration. */
11338 if (!cp_parser_error_occurred (parser
))
11341 tree asm_specification
;
11343 cp_declarator
*declarator
;
11344 tree initializer
= NULL_TREE
;
11346 /* Parse the declarator. */
11347 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11348 /*ctor_dtor_or_conv_p=*/NULL
,
11349 /*parenthesized_p=*/NULL
,
11350 /*member_p=*/false,
11351 /*friend_p=*/false);
11352 /* Parse the attributes. */
11353 attributes
= cp_parser_attributes_opt (parser
);
11354 /* Parse the asm-specification. */
11355 asm_specification
= cp_parser_asm_specification_opt (parser
);
11356 /* If the next token is not an `=' or '{', then we might still be
11357 looking at an expression. For example:
11361 looks like a decl-specifier-seq and a declarator -- but then
11362 there is no `=', so this is an expression. */
11363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11364 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11365 cp_parser_simulate_error (parser
);
11367 /* If we did see an `=' or '{', then we are looking at a declaration
11369 if (cp_parser_parse_definitely (parser
))
11372 bool non_constant_p
;
11373 int flags
= LOOKUP_ONLYCONVERTING
;
11375 /* Create the declaration. */
11376 decl
= start_decl (declarator
, &type_specifiers
,
11377 /*initialized_p=*/true,
11378 attributes
, /*prefix_attributes=*/NULL_TREE
,
11381 /* Parse the initializer. */
11382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11384 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11385 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11390 /* Consume the `='. */
11391 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11392 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11394 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11395 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11397 /* Process the initializer. */
11398 cp_finish_decl (decl
,
11399 initializer
, !non_constant_p
,
11404 pop_scope (pushed_scope
);
11406 return convert_from_reference (decl
);
11409 /* If we didn't even get past the declarator successfully, we are
11410 definitely not looking at a declaration. */
11412 cp_parser_abort_tentative_parse (parser
);
11414 /* Otherwise, we are looking at an expression. */
11415 return cp_parser_expression (parser
);
11418 /* Parses a for-statement or range-for-statement until the closing ')',
11422 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11424 tree init
, scope
, decl
;
11427 /* Begin the for-statement. */
11428 scope
= begin_for_scope (&init
);
11430 /* Parse the initialization. */
11431 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11434 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11436 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11440 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11442 /* Normal for loop */
11443 tree condition
= NULL_TREE
;
11444 tree expression
= NULL_TREE
;
11447 stmt
= begin_for_stmt (scope
, init
);
11448 /* The init-statement has already been parsed in
11449 cp_parser_init_statement, so no work is needed here. */
11450 finish_init_stmt (stmt
);
11452 /* If there's a condition, process it. */
11453 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11454 condition
= cp_parser_condition (parser
);
11457 cp_parser_error (parser
, "missing loop condition in loop with "
11458 "%<GCC ivdep%> pragma");
11459 condition
= error_mark_node
;
11461 finish_for_cond (condition
, stmt
, ivdep
);
11462 /* Look for the `;'. */
11463 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11465 /* If there's an expression, process it. */
11466 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11467 expression
= cp_parser_expression (parser
);
11468 finish_for_expr (expression
, stmt
);
11473 /* Tries to parse a range-based for-statement:
11476 decl-specifier-seq declarator : expression
11478 The decl-specifier-seq declarator and the `:' are already parsed by
11479 cp_parser_init_statement. If processing_template_decl it returns a
11480 newly created RANGE_FOR_STMT; if not, it is converted to a
11481 regular FOR_STMT. */
11484 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11487 tree stmt
, range_expr
;
11488 auto_vec
<cxx_binding
*, 16> bindings
;
11489 auto_vec
<tree
, 16> names
;
11490 tree decomp_first_name
= NULL_TREE
;
11491 unsigned int decomp_cnt
= 0;
11493 /* Get the range declaration momentarily out of the way so that
11494 the range expression doesn't clash with it. */
11495 if (range_decl
!= error_mark_node
)
11497 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11499 tree v
= DECL_VALUE_EXPR (range_decl
);
11500 /* For decomposition declaration get all of the corresponding
11501 declarations out of the way. */
11502 if (TREE_CODE (v
) == ARRAY_REF
11503 && VAR_P (TREE_OPERAND (v
, 0))
11504 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11506 tree d
= range_decl
;
11507 range_decl
= TREE_OPERAND (v
, 0);
11508 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11509 decomp_first_name
= d
;
11510 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11512 tree name
= DECL_NAME (d
);
11513 names
.safe_push (name
);
11514 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11515 IDENTIFIER_BINDING (name
)
11516 = IDENTIFIER_BINDING (name
)->previous
;
11520 if (names
.is_empty ())
11522 tree name
= DECL_NAME (range_decl
);
11523 names
.safe_push (name
);
11524 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11525 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11531 bool expr_non_constant_p
;
11532 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11535 range_expr
= cp_parser_expression (parser
);
11537 /* Put the range declaration(s) back into scope. */
11538 for (unsigned int i
= 0; i
< names
.length (); i
++)
11540 cxx_binding
*binding
= bindings
[i
];
11541 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11542 IDENTIFIER_BINDING (names
[i
]) = binding
;
11545 /* If in template, STMT is converted to a normal for-statement
11546 at instantiation. If not, it is done just ahead. */
11547 if (processing_template_decl
)
11549 if (check_for_bare_parameter_packs (range_expr
))
11550 range_expr
= error_mark_node
;
11551 stmt
= begin_range_for_stmt (scope
, init
);
11553 RANGE_FOR_IVDEP (stmt
) = 1;
11554 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11555 if (!type_dependent_expression_p (range_expr
)
11556 /* do_auto_deduction doesn't mess with template init-lists. */
11557 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11558 do_range_for_auto_deduction (range_decl
, range_expr
);
11562 stmt
= begin_for_stmt (scope
, init
);
11563 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11564 decomp_first_name
, decomp_cnt
, ivdep
);
11569 /* Subroutine of cp_convert_range_for: given the initializer expression,
11570 builds up the range temporary. */
11573 build_range_temp (tree range_expr
)
11575 tree range_type
, range_temp
;
11577 /* Find out the type deduced by the declaration
11578 `auto &&__range = range_expr'. */
11579 range_type
= cp_build_reference_type (make_auto (), true);
11580 range_type
= do_auto_deduction (range_type
, range_expr
,
11581 type_uses_auto (range_type
));
11583 /* Create the __range variable. */
11584 range_temp
= build_decl (input_location
, VAR_DECL
,
11585 get_identifier ("__for_range"), range_type
);
11586 TREE_USED (range_temp
) = 1;
11587 DECL_ARTIFICIAL (range_temp
) = 1;
11592 /* Used by cp_parser_range_for in template context: we aren't going to
11593 do a full conversion yet, but we still need to resolve auto in the
11594 type of the for-range-declaration if present. This is basically
11595 a shortcut version of cp_convert_range_for. */
11598 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11600 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11603 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11604 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11605 iter_type
= (cp_parser_perform_range_for_lookup
11606 (range_temp
, &begin_dummy
, &end_dummy
));
11609 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11611 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
11612 tf_warning_or_error
);
11613 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11614 iter_decl
, auto_node
);
11619 /* Converts a range-based for-statement into a normal
11620 for-statement, as per the definition.
11622 for (RANGE_DECL : RANGE_EXPR)
11625 should be equivalent to:
11628 auto &&__range = RANGE_EXPR;
11629 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11633 RANGE_DECL = *__begin;
11638 If RANGE_EXPR is an array:
11639 BEGIN_EXPR = __range
11640 END_EXPR = __range + ARRAY_SIZE(__range)
11641 Else if RANGE_EXPR has a member 'begin' or 'end':
11642 BEGIN_EXPR = __range.begin()
11643 END_EXPR = __range.end()
11645 BEGIN_EXPR = begin(__range)
11646 END_EXPR = end(__range);
11648 If __range has a member 'begin' but not 'end', or vice versa, we must
11649 still use the second alternative (it will surely fail, however).
11650 When calling begin()/end() in the third alternative we must use
11651 argument dependent lookup, but always considering 'std' as an associated
11655 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11656 tree decomp_first_name
, unsigned int decomp_cnt
,
11660 tree iter_type
, begin_expr
, end_expr
;
11661 tree condition
, expression
;
11663 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11664 /* If an error happened previously do nothing or else a lot of
11665 unhelpful errors would be issued. */
11666 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11671 if (VAR_P (range_expr
)
11672 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11673 /* Can't bind a reference to an array of runtime bound. */
11674 range_temp
= range_expr
;
11677 range_temp
= build_range_temp (range_expr
);
11678 pushdecl (range_temp
);
11679 cp_finish_decl (range_temp
, range_expr
,
11680 /*is_constant_init*/false, NULL_TREE
,
11681 LOOKUP_ONLYCONVERTING
);
11682 range_temp
= convert_from_reference (range_temp
);
11684 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11685 &begin_expr
, &end_expr
);
11688 /* The new for initialization statement. */
11689 begin
= build_decl (input_location
, VAR_DECL
,
11690 get_identifier ("__for_begin"), iter_type
);
11691 TREE_USED (begin
) = 1;
11692 DECL_ARTIFICIAL (begin
) = 1;
11694 cp_finish_decl (begin
, begin_expr
,
11695 /*is_constant_init*/false, NULL_TREE
,
11696 LOOKUP_ONLYCONVERTING
);
11698 if (cxx_dialect
>= cxx1z
)
11699 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11700 end
= build_decl (input_location
, VAR_DECL
,
11701 get_identifier ("__for_end"), iter_type
);
11702 TREE_USED (end
) = 1;
11703 DECL_ARTIFICIAL (end
) = 1;
11705 cp_finish_decl (end
, end_expr
,
11706 /*is_constant_init*/false, NULL_TREE
,
11707 LOOKUP_ONLYCONVERTING
);
11709 finish_init_stmt (statement
);
11711 /* The new for condition. */
11712 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11715 NULL
, tf_warning_or_error
);
11716 finish_for_cond (condition
, statement
, ivdep
);
11718 /* The new increment expression. */
11719 expression
= finish_unary_op_expr (input_location
,
11720 PREINCREMENT_EXPR
, begin
,
11721 tf_warning_or_error
);
11722 finish_for_expr (expression
, statement
);
11724 /* The declaration is initialized with *__begin inside the loop body. */
11725 cp_finish_decl (range_decl
,
11726 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11727 tf_warning_or_error
),
11728 /*is_constant_init*/false, NULL_TREE
,
11729 LOOKUP_ONLYCONVERTING
);
11730 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11731 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11736 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11737 We need to solve both at the same time because the method used
11738 depends on the existence of members begin or end.
11739 Returns the type deduced for the iterator expression. */
11742 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11744 if (error_operand_p (range
))
11746 *begin
= *end
= error_mark_node
;
11747 return error_mark_node
;
11750 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11752 error ("range-based %<for%> expression of type %qT "
11753 "has incomplete type", TREE_TYPE (range
));
11754 *begin
= *end
= error_mark_node
;
11755 return error_mark_node
;
11757 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11759 /* If RANGE is an array, we will use pointer arithmetic. */
11760 *begin
= decay_conversion (range
, tf_warning_or_error
);
11761 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11763 array_type_nelts_top (TREE_TYPE (range
)),
11765 return TREE_TYPE (*begin
);
11769 /* If it is not an array, we must do a bit of magic. */
11770 tree id_begin
, id_end
;
11771 tree member_begin
, member_end
;
11773 *begin
= *end
= error_mark_node
;
11775 id_begin
= get_identifier ("begin");
11776 id_end
= get_identifier ("end");
11777 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11778 /*protect=*/2, /*want_type=*/false,
11779 tf_warning_or_error
);
11780 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11781 /*protect=*/2, /*want_type=*/false,
11782 tf_warning_or_error
);
11784 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11786 /* Use the member functions. */
11787 if (member_begin
!= NULL_TREE
)
11788 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11790 error ("range-based %<for%> expression of type %qT has an "
11791 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11793 if (member_end
!= NULL_TREE
)
11794 *end
= cp_parser_range_for_member_function (range
, id_end
);
11796 error ("range-based %<for%> expression of type %qT has a "
11797 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11801 /* Use global functions with ADL. */
11802 vec
<tree
, va_gc
> *vec
;
11803 vec
= make_tree_vector ();
11805 vec_safe_push (vec
, range
);
11807 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11808 tf_warning_or_error
);
11809 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11810 tf_warning_or_error
);
11811 member_end
= perform_koenig_lookup (id_end
, vec
,
11812 tf_warning_or_error
);
11813 *end
= finish_call_expr (member_end
, &vec
, false, true,
11814 tf_warning_or_error
);
11816 release_tree_vector (vec
);
11819 /* Last common checks. */
11820 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11822 /* If one of the expressions is an error do no more checks. */
11823 *begin
= *end
= error_mark_node
;
11824 return error_mark_node
;
11826 else if (type_dependent_expression_p (*begin
)
11827 || type_dependent_expression_p (*end
))
11828 /* Can happen, when, eg, in a template context, Koenig lookup
11829 can't resolve begin/end (c++/58503). */
11833 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11834 /* The unqualified type of the __begin and __end temporaries should
11835 be the same, as required by the multiple auto declaration. */
11836 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11838 if (cxx_dialect
>= cxx1z
11839 && (build_x_binary_op (input_location
, NE_EXPR
,
11840 *begin
, ERROR_MARK
,
11843 != error_mark_node
))
11844 /* P0184R0 allows __begin and __end to have different types,
11845 but make sure they are comparable so we can give a better
11848 error ("inconsistent begin/end types in range-based %<for%> "
11849 "statement: %qT and %qT",
11850 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11857 /* Helper function for cp_parser_perform_range_for_lookup.
11858 Builds a tree for RANGE.IDENTIFIER(). */
11861 cp_parser_range_for_member_function (tree range
, tree identifier
)
11864 vec
<tree
, va_gc
> *vec
;
11866 member
= finish_class_member_access_expr (range
, identifier
,
11867 false, tf_warning_or_error
);
11868 if (member
== error_mark_node
)
11869 return error_mark_node
;
11871 vec
= make_tree_vector ();
11872 res
= finish_call_expr (member
, &vec
,
11873 /*disallow_virtual=*/false,
11874 /*koenig_p=*/false,
11875 tf_warning_or_error
);
11876 release_tree_vector (vec
);
11880 /* Parse an iteration-statement.
11882 iteration-statement:
11883 while ( condition ) statement
11884 do statement while ( expression ) ;
11885 for ( init-statement condition [opt] ; expression [opt] )
11888 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11891 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
11896 unsigned char in_statement
;
11897 token_indent_info guard_tinfo
;
11899 /* Peek at the next token. */
11900 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11902 return error_mark_node
;
11904 guard_tinfo
= get_token_indent_info (token
);
11906 /* Remember whether or not we are already within an iteration
11908 in_statement
= parser
->in_statement
;
11910 /* See what kind of keyword it is. */
11911 keyword
= token
->keyword
;
11918 /* Begin the while-statement. */
11919 statement
= begin_while_stmt ();
11920 /* Look for the `('. */
11921 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11922 /* Parse the condition. */
11923 condition
= cp_parser_condition (parser
);
11924 finish_while_stmt_cond (condition
, statement
, ivdep
);
11925 /* Look for the `)'. */
11926 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11927 /* Parse the dependent statement. */
11928 parser
->in_statement
= IN_ITERATION_STMT
;
11929 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11930 parser
->in_statement
= in_statement
;
11931 /* We're done with the while-statement. */
11932 finish_while_stmt (statement
);
11940 /* Begin the do-statement. */
11941 statement
= begin_do_stmt ();
11942 /* Parse the body of the do-statement. */
11943 parser
->in_statement
= IN_ITERATION_STMT
;
11944 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11945 parser
->in_statement
= in_statement
;
11946 finish_do_body (statement
);
11947 /* Look for the `while' keyword. */
11948 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11949 /* Look for the `('. */
11950 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11951 /* Parse the expression. */
11952 expression
= cp_parser_expression (parser
);
11953 /* We're done with the do-statement. */
11954 finish_do_stmt (expression
, statement
, ivdep
);
11955 /* Look for the `)'. */
11956 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11957 /* Look for the `;'. */
11958 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11964 /* Look for the `('. */
11965 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11967 statement
= cp_parser_for (parser
, ivdep
);
11969 /* Look for the `)'. */
11970 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11972 /* Parse the body of the for-statement. */
11973 parser
->in_statement
= IN_ITERATION_STMT
;
11974 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11975 parser
->in_statement
= in_statement
;
11977 /* We're done with the for-statement. */
11978 finish_for_stmt (statement
);
11983 cp_parser_error (parser
, "expected iteration-statement");
11984 statement
= error_mark_node
;
11991 /* Parse a init-statement or the declarator of a range-based-for.
11992 Returns true if a range-based-for declaration is seen.
11995 expression-statement
11996 simple-declaration */
11999 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12001 /* If the next token is a `;', then we have an empty
12002 expression-statement. Grammatically, this is also a
12003 simple-declaration, but an invalid one, because it does not
12004 declare anything. Therefore, if we did not handle this case
12005 specially, we would issue an error message about an invalid
12007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12009 bool is_range_for
= false;
12010 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12012 /* A colon is used in range-based for. */
12013 parser
->colon_corrects_to_scope_p
= false;
12015 /* We're going to speculatively look for a declaration, falling back
12016 to an expression, if necessary. */
12017 cp_parser_parse_tentatively (parser
);
12018 /* Parse the declaration. */
12019 cp_parser_simple_declaration (parser
,
12020 /*function_definition_allowed_p=*/false,
12022 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12025 /* It is a range-for, consume the ':' */
12026 cp_lexer_consume_token (parser
->lexer
);
12027 is_range_for
= true;
12028 if (cxx_dialect
< cxx11
)
12030 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12031 "range-based %<for%> loops only available with "
12032 "-std=c++11 or -std=gnu++11");
12033 *decl
= error_mark_node
;
12037 /* The ';' is not consumed yet because we told
12038 cp_parser_simple_declaration not to. */
12039 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12041 if (cp_parser_parse_definitely (parser
))
12042 return is_range_for
;
12043 /* If the tentative parse failed, then we shall need to look for an
12044 expression-statement. */
12046 /* If we are here, it is an expression-statement. */
12047 cp_parser_expression_statement (parser
, NULL_TREE
);
12051 /* Parse a jump-statement.
12056 return expression [opt] ;
12057 return braced-init-list ;
12063 goto * expression ;
12065 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12068 cp_parser_jump_statement (cp_parser
* parser
)
12070 tree statement
= error_mark_node
;
12073 unsigned char in_statement
;
12075 /* Peek at the next token. */
12076 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12078 return error_mark_node
;
12080 /* See what kind of keyword it is. */
12081 keyword
= token
->keyword
;
12085 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12086 switch (in_statement
)
12089 error_at (token
->location
, "break statement not within loop or switch");
12092 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12093 || in_statement
== IN_ITERATION_STMT
);
12094 statement
= finish_break_stmt ();
12095 if (in_statement
== IN_ITERATION_STMT
)
12096 break_maybe_infinite_loop ();
12099 error_at (token
->location
, "invalid exit from OpenMP structured block");
12102 error_at (token
->location
, "break statement used with OpenMP for loop");
12104 case IN_CILK_SIMD_FOR
:
12105 error_at (token
->location
, "break statement used with Cilk Plus for loop");
12108 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12112 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12115 error_at (token
->location
, "continue statement not within a loop");
12117 case IN_CILK_SIMD_FOR
:
12118 error_at (token
->location
,
12119 "continue statement within %<#pragma simd%> loop body");
12120 /* Fall through. */
12121 case IN_ITERATION_STMT
:
12123 statement
= finish_continue_stmt ();
12126 error_at (token
->location
, "invalid exit from OpenMP structured block");
12129 gcc_unreachable ();
12131 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12137 bool expr_non_constant_p
;
12139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12141 cp_lexer_set_source_position (parser
->lexer
);
12142 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12143 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12145 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12146 expr
= cp_parser_expression (parser
);
12148 /* If the next token is a `;', then there is no
12151 /* Build the return-statement. */
12152 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12153 /* Don't deduce from a discarded return statement. */;
12155 statement
= finish_return_stmt (expr
);
12156 /* Look for the final `;'. */
12157 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12162 if (parser
->in_function_body
12163 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12165 error ("%<goto%> in %<constexpr%> function");
12166 cp_function_chain
->invalid_constexpr
= true;
12169 /* Create the goto-statement. */
12170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12172 /* Issue a warning about this use of a GNU extension. */
12173 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12174 /* Consume the '*' token. */
12175 cp_lexer_consume_token (parser
->lexer
);
12176 /* Parse the dependent expression. */
12177 finish_goto_stmt (cp_parser_expression (parser
));
12180 finish_goto_stmt (cp_parser_identifier (parser
));
12181 /* Look for the final `;'. */
12182 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12186 cp_parser_error (parser
, "expected jump-statement");
12193 /* Parse a declaration-statement.
12195 declaration-statement:
12196 block-declaration */
12199 cp_parser_declaration_statement (cp_parser
* parser
)
12203 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12204 p
= obstack_alloc (&declarator_obstack
, 0);
12206 /* Parse the block-declaration. */
12207 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12209 /* Free any declarators allocated. */
12210 obstack_free (&declarator_obstack
, p
);
12213 /* Some dependent statements (like `if (cond) statement'), are
12214 implicitly in their own scope. In other words, if the statement is
12215 a single statement (as opposed to a compound-statement), it is
12216 none-the-less treated as if it were enclosed in braces. Any
12217 declarations appearing in the dependent statement are out of scope
12218 after control passes that point. This function parses a statement,
12219 but ensures that is in its own scope, even if it is not a
12220 compound-statement.
12222 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12223 is a (possibly labeled) if statement which is not enclosed in
12224 braces and has an else clause. This is used to implement
12227 CHAIN is a vector of if-else-if conditions. This is used to implement
12230 Returns the new statement. */
12233 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12234 const token_indent_info
&guard_tinfo
,
12238 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12239 token_indent_info body_tinfo
12240 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12245 /* Mark if () ; with a special NOP_EXPR. */
12246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12248 cp_lexer_consume_token (parser
->lexer
);
12249 statement
= add_stmt (build_empty_stmt (body_loc
));
12251 if (guard_tinfo
.keyword
== RID_IF
12252 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12253 warning_at (body_loc
, OPT_Wempty_body
,
12254 "suggest braces around empty body in an %<if%> statement");
12255 else if (guard_tinfo
.keyword
== RID_ELSE
)
12256 warning_at (body_loc
, OPT_Wempty_body
,
12257 "suggest braces around empty body in an %<else%> statement");
12259 /* if a compound is opened, we simply parse the statement directly. */
12260 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12261 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12262 /* If the token is not a `{', then we must take special action. */
12265 /* Create a compound-statement. */
12266 statement
= begin_compound_stmt (0);
12267 /* Parse the dependent-statement. */
12268 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
12269 /* Finish the dummy compound-statement. */
12270 finish_compound_stmt (statement
);
12273 token_indent_info next_tinfo
12274 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12275 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12277 /* Return the statement. */
12281 /* For some dependent statements (like `while (cond) statement'), we
12282 have already created a scope. Therefore, even if the dependent
12283 statement is a compound-statement, we do not want to create another
12287 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12288 const token_indent_info
&guard_tinfo
)
12290 /* If the token is a `{', then we must take special action. */
12291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12293 token_indent_info body_tinfo
12294 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12296 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
12297 token_indent_info next_tinfo
12298 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12299 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12303 /* Avoid calling cp_parser_compound_statement, so that we
12304 don't create a new scope. Do everything else by hand. */
12305 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
12306 /* If the next keyword is `__label__' we have a label declaration. */
12307 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12308 cp_parser_label_declaration (parser
);
12309 /* Parse an (optional) statement-seq. */
12310 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12311 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12315 /* Declarations [gram.dcl.dcl] */
12317 /* Parse an optional declaration-sequence.
12321 declaration-seq declaration */
12324 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12330 token
= cp_lexer_peek_token (parser
->lexer
);
12332 if (token
->type
== CPP_CLOSE_BRACE
12333 || token
->type
== CPP_EOF
12334 || token
->type
== CPP_PRAGMA_EOL
)
12337 if (token
->type
== CPP_SEMICOLON
)
12339 /* A declaration consisting of a single semicolon is
12340 invalid. Allow it unless we're being pedantic. */
12341 cp_lexer_consume_token (parser
->lexer
);
12342 if (!in_system_header_at (input_location
))
12343 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12347 /* If we're entering or exiting a region that's implicitly
12348 extern "C", modify the lang context appropriately. */
12349 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12351 push_lang_context (lang_name_c
);
12352 parser
->implicit_extern_c
= true;
12354 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12356 pop_lang_context ();
12357 parser
->implicit_extern_c
= false;
12360 if (token
->type
== CPP_PRAGMA
)
12362 /* A top-level declaration can consist solely of a #pragma.
12363 A nested declaration cannot, so this is done here and not
12364 in cp_parser_declaration. (A #pragma at block scope is
12365 handled in cp_parser_statement.) */
12366 cp_parser_pragma (parser
, pragma_external
, NULL
);
12370 /* Parse the declaration itself. */
12371 cp_parser_declaration (parser
);
12375 /* Parse a declaration.
12379 function-definition
12380 template-declaration
12381 explicit-instantiation
12382 explicit-specialization
12383 linkage-specification
12384 namespace-definition
12392 __extension__ declaration */
12395 cp_parser_declaration (cp_parser
* parser
)
12399 int saved_pedantic
;
12401 tree attributes
= NULL_TREE
;
12403 /* Check for the `__extension__' keyword. */
12404 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12406 /* Parse the qualified declaration. */
12407 cp_parser_declaration (parser
);
12408 /* Restore the PEDANTIC flag. */
12409 pedantic
= saved_pedantic
;
12414 /* Try to figure out what kind of declaration is present. */
12415 token1
= *cp_lexer_peek_token (parser
->lexer
);
12417 if (token1
.type
!= CPP_EOF
)
12418 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12421 token2
.type
= CPP_EOF
;
12422 token2
.keyword
= RID_MAX
;
12425 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12426 p
= obstack_alloc (&declarator_obstack
, 0);
12428 /* If the next token is `extern' and the following token is a string
12429 literal, then we have a linkage specification. */
12430 if (token1
.keyword
== RID_EXTERN
12431 && cp_parser_is_pure_string_literal (&token2
))
12432 cp_parser_linkage_specification (parser
);
12433 /* If the next token is `template', then we have either a template
12434 declaration, an explicit instantiation, or an explicit
12436 else if (token1
.keyword
== RID_TEMPLATE
)
12438 /* `template <>' indicates a template specialization. */
12439 if (token2
.type
== CPP_LESS
12440 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12441 cp_parser_explicit_specialization (parser
);
12442 /* `template <' indicates a template declaration. */
12443 else if (token2
.type
== CPP_LESS
)
12444 cp_parser_template_declaration (parser
, /*member_p=*/false);
12445 /* Anything else must be an explicit instantiation. */
12447 cp_parser_explicit_instantiation (parser
);
12449 /* If the next token is `export', then we have a template
12451 else if (token1
.keyword
== RID_EXPORT
)
12452 cp_parser_template_declaration (parser
, /*member_p=*/false);
12453 /* If the next token is `extern', 'static' or 'inline' and the one
12454 after that is `template', we have a GNU extended explicit
12455 instantiation directive. */
12456 else if (cp_parser_allow_gnu_extensions_p (parser
)
12457 && (token1
.keyword
== RID_EXTERN
12458 || token1
.keyword
== RID_STATIC
12459 || token1
.keyword
== RID_INLINE
)
12460 && token2
.keyword
== RID_TEMPLATE
)
12461 cp_parser_explicit_instantiation (parser
);
12462 /* If the next token is `namespace', check for a named or unnamed
12463 namespace definition. */
12464 else if (token1
.keyword
== RID_NAMESPACE
12465 && (/* A named namespace definition. */
12466 (token2
.type
== CPP_NAME
12467 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12469 || (token2
.type
== CPP_OPEN_SQUARE
12470 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12471 == CPP_OPEN_SQUARE
)
12472 /* An unnamed namespace definition. */
12473 || token2
.type
== CPP_OPEN_BRACE
12474 || token2
.keyword
== RID_ATTRIBUTE
))
12475 cp_parser_namespace_definition (parser
);
12476 /* An inline (associated) namespace definition. */
12477 else if (token1
.keyword
== RID_INLINE
12478 && token2
.keyword
== RID_NAMESPACE
)
12479 cp_parser_namespace_definition (parser
);
12480 /* Objective-C++ declaration/definition. */
12481 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12482 cp_parser_objc_declaration (parser
, NULL_TREE
);
12483 else if (c_dialect_objc ()
12484 && token1
.keyword
== RID_ATTRIBUTE
12485 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12486 cp_parser_objc_declaration (parser
, attributes
);
12487 /* At this point we may have a template declared by a concept
12489 else if (flag_concepts
12490 && cp_parser_template_declaration_after_export (parser
,
12491 /*member_p=*/false))
12494 /* Try to parse a block-declaration, or a function-definition. */
12495 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12497 /* Free any declarators allocated. */
12498 obstack_free (&declarator_obstack
, p
);
12501 /* Parse a block-declaration.
12506 namespace-alias-definition
12513 __extension__ block-declaration
12518 static_assert-declaration
12520 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12521 part of a declaration-statement. */
12524 cp_parser_block_declaration (cp_parser
*parser
,
12528 int saved_pedantic
;
12530 /* Check for the `__extension__' keyword. */
12531 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12533 /* Parse the qualified declaration. */
12534 cp_parser_block_declaration (parser
, statement_p
);
12535 /* Restore the PEDANTIC flag. */
12536 pedantic
= saved_pedantic
;
12541 /* Peek at the next token to figure out which kind of declaration is
12543 token1
= cp_lexer_peek_token (parser
->lexer
);
12545 /* If the next keyword is `asm', we have an asm-definition. */
12546 if (token1
->keyword
== RID_ASM
)
12549 cp_parser_commit_to_tentative_parse (parser
);
12550 cp_parser_asm_definition (parser
);
12552 /* If the next keyword is `namespace', we have a
12553 namespace-alias-definition. */
12554 else if (token1
->keyword
== RID_NAMESPACE
)
12555 cp_parser_namespace_alias_definition (parser
);
12556 /* If the next keyword is `using', we have a
12557 using-declaration, a using-directive, or an alias-declaration. */
12558 else if (token1
->keyword
== RID_USING
)
12563 cp_parser_commit_to_tentative_parse (parser
);
12564 /* If the token after `using' is `namespace', then we have a
12565 using-directive. */
12566 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12567 if (token2
->keyword
== RID_NAMESPACE
)
12568 cp_parser_using_directive (parser
);
12569 /* If the second token after 'using' is '=', then we have an
12570 alias-declaration. */
12571 else if (cxx_dialect
>= cxx11
12572 && token2
->type
== CPP_NAME
12573 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12574 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12575 cp_parser_alias_declaration (parser
);
12576 /* Otherwise, it's a using-declaration. */
12578 cp_parser_using_declaration (parser
,
12579 /*access_declaration_p=*/false);
12581 /* If the next keyword is `__label__' we have a misplaced label
12583 else if (token1
->keyword
== RID_LABEL
)
12585 cp_lexer_consume_token (parser
->lexer
);
12586 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12587 cp_parser_skip_to_end_of_statement (parser
);
12588 /* If the next token is now a `;', consume it. */
12589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12590 cp_lexer_consume_token (parser
->lexer
);
12592 /* If the next token is `static_assert' we have a static assertion. */
12593 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12594 cp_parser_static_assert (parser
, /*member_p=*/false);
12595 /* Anything else must be a simple-declaration. */
12597 cp_parser_simple_declaration (parser
, !statement_p
,
12598 /*maybe_range_for_decl*/NULL
);
12601 /* Parse a simple-declaration.
12603 simple-declaration:
12604 decl-specifier-seq [opt] init-declarator-list [opt] ;
12605 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12606 brace-or-equal-initializer ;
12608 init-declarator-list:
12610 init-declarator-list , init-declarator
12612 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12613 function-definition as a simple-declaration.
12615 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12616 parsed declaration if it is an uninitialized single declarator not followed
12617 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12618 if present, will not be consumed. */
12621 cp_parser_simple_declaration (cp_parser
* parser
,
12622 bool function_definition_allowed_p
,
12623 tree
*maybe_range_for_decl
)
12625 cp_decl_specifier_seq decl_specifiers
;
12626 int declares_class_or_enum
;
12627 bool saw_declarator
;
12628 location_t comma_loc
= UNKNOWN_LOCATION
;
12629 location_t init_loc
= UNKNOWN_LOCATION
;
12631 if (maybe_range_for_decl
)
12632 *maybe_range_for_decl
= NULL_TREE
;
12634 /* Defer access checks until we know what is being declared; the
12635 checks for names appearing in the decl-specifier-seq should be
12636 done as if we were in the scope of the thing being declared. */
12637 push_deferring_access_checks (dk_deferred
);
12639 /* Parse the decl-specifier-seq. We have to keep track of whether
12640 or not the decl-specifier-seq declares a named class or
12641 enumeration type, since that is the only case in which the
12642 init-declarator-list is allowed to be empty.
12646 In a simple-declaration, the optional init-declarator-list can be
12647 omitted only when declaring a class or enumeration, that is when
12648 the decl-specifier-seq contains either a class-specifier, an
12649 elaborated-type-specifier, or an enum-specifier. */
12650 cp_parser_decl_specifier_seq (parser
,
12651 CP_PARSER_FLAGS_OPTIONAL
,
12653 &declares_class_or_enum
);
12654 /* We no longer need to defer access checks. */
12655 stop_deferring_access_checks ();
12657 /* In a block scope, a valid declaration must always have a
12658 decl-specifier-seq. By not trying to parse declarators, we can
12659 resolve the declaration/expression ambiguity more quickly. */
12660 if (!function_definition_allowed_p
12661 && !decl_specifiers
.any_specifiers_p
)
12663 cp_parser_error (parser
, "expected declaration");
12667 /* If the next two tokens are both identifiers, the code is
12668 erroneous. The usual cause of this situation is code like:
12672 where "T" should name a type -- but does not. */
12673 if (!decl_specifiers
.any_type_specifiers_p
12674 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12676 /* If parsing tentatively, we should commit; we really are
12677 looking at a declaration. */
12678 cp_parser_commit_to_tentative_parse (parser
);
12683 /* If we have seen at least one decl-specifier, and the next token
12684 is not a parenthesis, then we must be looking at a declaration.
12685 (After "int (" we might be looking at a functional cast.) */
12686 if (decl_specifiers
.any_specifiers_p
12687 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12688 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12689 && !cp_parser_error_occurred (parser
))
12690 cp_parser_commit_to_tentative_parse (parser
);
12692 /* Look for C++17 decomposition declaration. */
12693 for (size_t n
= 1; ; n
++)
12694 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12695 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12697 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12698 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12699 && decl_specifiers
.any_specifiers_p
)
12702 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12703 maybe_range_for_decl
,
12706 /* The next token should be either a `,' or a `;'. */
12707 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12708 /* If it's a `;', we are done. */
12709 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12711 /* Anything else is an error. */
12714 /* If we have already issued an error message we don't need
12715 to issue another one. */
12716 if ((decl
!= error_mark_node
12717 && DECL_INITIAL (decl
) != error_mark_node
)
12718 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12719 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12720 /* Skip tokens until we reach the end of the statement. */
12721 cp_parser_skip_to_end_of_statement (parser
);
12722 /* If the next token is now a `;', consume it. */
12723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12724 cp_lexer_consume_token (parser
->lexer
);
12732 bool auto_specifier_p
;
12733 /* NULL_TREE if both variable and function declaration are allowed,
12734 error_mark_node if function declaration are not allowed and
12735 a FUNCTION_DECL that should be diagnosed if it is followed by
12736 variable declarations. */
12737 tree auto_function_declaration
;
12739 last_type
= NULL_TREE
;
12741 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12742 auto_function_declaration
= NULL_TREE
;
12744 /* Keep going until we hit the `;' at the end of the simple
12746 saw_declarator
= false;
12747 while (cp_lexer_next_token_is_not (parser
->lexer
,
12751 bool function_definition_p
;
12753 tree auto_result
= NULL_TREE
;
12755 if (saw_declarator
)
12757 /* If we are processing next declarator, comma is expected */
12758 token
= cp_lexer_peek_token (parser
->lexer
);
12759 gcc_assert (token
->type
== CPP_COMMA
);
12760 cp_lexer_consume_token (parser
->lexer
);
12761 if (maybe_range_for_decl
)
12763 *maybe_range_for_decl
= error_mark_node
;
12764 if (comma_loc
== UNKNOWN_LOCATION
)
12765 comma_loc
= token
->location
;
12769 saw_declarator
= true;
12771 /* Parse the init-declarator. */
12772 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12774 function_definition_allowed_p
,
12775 /*member_p=*/false,
12776 declares_class_or_enum
,
12777 &function_definition_p
,
12778 maybe_range_for_decl
,
12781 /* If an error occurred while parsing tentatively, exit quickly.
12782 (That usually happens when in the body of a function; each
12783 statement is treated as a declaration-statement until proven
12785 if (cp_parser_error_occurred (parser
))
12788 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
12790 /* If the init-declarator-list contains more than one
12791 init-declarator, they shall all form declarations of
12793 if (auto_function_declaration
== NULL_TREE
)
12794 auto_function_declaration
12795 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
12796 else if (TREE_CODE (decl
) == FUNCTION_DECL
12797 || auto_function_declaration
!= error_mark_node
)
12799 error_at (decl_specifiers
.locations
[ds_type_spec
],
12800 "non-variable %qD in declaration with more than one "
12801 "declarator with placeholder type",
12802 TREE_CODE (decl
) == FUNCTION_DECL
12803 ? decl
: auto_function_declaration
);
12804 auto_function_declaration
= error_mark_node
;
12809 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
12812 && last_type
!= error_mark_node
12813 && !same_type_p (auto_result
, last_type
))
12815 /* If the list of declarators contains more than one declarator,
12816 the type of each declared variable is determined as described
12817 above. If the type deduced for the template parameter U is not
12818 the same in each deduction, the program is ill-formed. */
12819 error_at (decl_specifiers
.locations
[ds_type_spec
],
12820 "inconsistent deduction for %qT: %qT and then %qT",
12821 decl_specifiers
.type
, last_type
, auto_result
);
12822 last_type
= error_mark_node
;
12825 last_type
= auto_result
;
12828 /* Handle function definitions specially. */
12829 if (function_definition_p
)
12831 /* If the next token is a `,', then we are probably
12832 processing something like:
12836 which is erroneous. */
12837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12839 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12840 error_at (token
->location
,
12842 " declarations and function-definitions is forbidden");
12844 /* Otherwise, we're done with the list of declarators. */
12847 pop_deferring_access_checks ();
12851 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12852 *maybe_range_for_decl
= decl
;
12853 /* The next token should be either a `,' or a `;'. */
12854 token
= cp_lexer_peek_token (parser
->lexer
);
12855 /* If it's a `,', there are more declarators to come. */
12856 if (token
->type
== CPP_COMMA
)
12857 /* will be consumed next time around */;
12858 /* If it's a `;', we are done. */
12859 else if (token
->type
== CPP_SEMICOLON
)
12861 else if (maybe_range_for_decl
)
12863 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
12864 permerror (decl_specifiers
.locations
[ds_type_spec
],
12865 "types may not be defined in a for-range-declaration");
12868 /* Anything else is an error. */
12871 /* If we have already issued an error message we don't need
12872 to issue another one. */
12873 if ((decl
!= error_mark_node
12874 && DECL_INITIAL (decl
) != error_mark_node
)
12875 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12876 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12877 /* Skip tokens until we reach the end of the statement. */
12878 cp_parser_skip_to_end_of_statement (parser
);
12879 /* If the next token is now a `;', consume it. */
12880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12881 cp_lexer_consume_token (parser
->lexer
);
12884 /* After the first time around, a function-definition is not
12885 allowed -- even if it was OK at first. For example:
12890 function_definition_allowed_p
= false;
12893 /* Issue an error message if no declarators are present, and the
12894 decl-specifier-seq does not itself declare a class or
12895 enumeration: [dcl.dcl]/3. */
12896 if (!saw_declarator
)
12898 if (cp_parser_declares_only_class_p (parser
))
12900 if (!declares_class_or_enum
12901 && decl_specifiers
.type
12902 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12903 /* Ensure an error is issued anyway when finish_decltype_type,
12904 called via cp_parser_decl_specifier_seq, returns a class or
12905 an enumeration (c++/51786). */
12906 decl_specifiers
.type
= NULL_TREE
;
12907 shadow_tag (&decl_specifiers
);
12909 /* Perform any deferred access checks. */
12910 perform_deferred_access_checks (tf_warning_or_error
);
12913 /* Consume the `;'. */
12915 if (!maybe_range_for_decl
)
12916 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12917 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12919 if (init_loc
!= UNKNOWN_LOCATION
)
12920 error_at (init_loc
, "initializer in range-based %<for%> loop");
12921 if (comma_loc
!= UNKNOWN_LOCATION
)
12922 error_at (comma_loc
,
12923 "multiple declarations in range-based %<for%> loop");
12927 pop_deferring_access_checks ();
12930 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12931 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12935 cp_parser_decomposition_declaration (cp_parser
*parser
,
12936 cp_decl_specifier_seq
*decl_specifiers
,
12937 tree
*maybe_range_for_decl
,
12938 location_t
*init_loc
)
12940 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
12941 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12942 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
12944 /* Parse the identifier-list. */
12945 auto_vec
<cp_expr
, 10> v
;
12946 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12949 cp_expr e
= cp_parser_identifier (parser
);
12950 if (e
.get_value () == error_mark_node
)
12953 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12955 cp_lexer_consume_token (parser
->lexer
);
12958 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12959 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
12961 end_loc
= UNKNOWN_LOCATION
;
12962 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
12964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12965 cp_lexer_consume_token (parser
->lexer
);
12968 cp_parser_skip_to_end_of_statement (parser
);
12969 return error_mark_node
;
12973 if (cxx_dialect
< cxx1z
)
12974 pedwarn (loc
, 0, "decomposition declaration only available with "
12975 "-std=c++1z or -std=gnu++1z");
12978 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
12979 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
12980 declarator
->id_loc
= loc
;
12981 if (ref_qual
!= REF_QUAL_NONE
)
12982 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
12983 ref_qual
== REF_QUAL_RVALUE
,
12985 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
12986 NULL_TREE
, decl_specifiers
->attributes
,
12988 tree orig_decl
= decl
;
12992 cp_decl_specifier_seq decl_specs
;
12993 clear_decl_specs (&decl_specs
);
12994 decl_specs
.type
= make_auto ();
12996 FOR_EACH_VEC_ELT (v
, i
, e
)
12999 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13001 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13002 declarator
->id_loc
= e
.get_location ();
13003 tree elt_pushed_scope
;
13004 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13005 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13006 if (decl2
== error_mark_node
)
13007 decl
= error_mark_node
;
13008 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13010 /* Ensure we've diagnosed redeclaration if we aren't creating
13012 gcc_assert (errorcount
);
13013 decl
= error_mark_node
;
13017 if (elt_pushed_scope
)
13018 pop_scope (elt_pushed_scope
);
13023 error_at (loc
, "empty decomposition declaration");
13024 decl
= error_mark_node
;
13027 if (maybe_range_for_decl
== NULL
13028 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13030 bool non_constant_p
= false, is_direct_init
= false;
13031 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13032 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13035 if (decl
!= error_mark_node
)
13037 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13038 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13039 cp_finish_decomp (decl
, prev
, v
.length ());
13042 else if (decl
!= error_mark_node
)
13044 *maybe_range_for_decl
= prev
;
13045 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13046 the underlying DECL. */
13047 cp_finish_decomp (decl
, prev
, v
.length ());
13051 pop_scope (pushed_scope
);
13053 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13055 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13056 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13062 /* Parse a decl-specifier-seq.
13064 decl-specifier-seq:
13065 decl-specifier-seq [opt] decl-specifier
13066 decl-specifier attribute-specifier-seq [opt] (C++11)
13069 storage-class-specifier
13080 Concepts Extension:
13085 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13087 The parser flags FLAGS is used to control type-specifier parsing.
13089 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13092 1: one of the decl-specifiers is an elaborated-type-specifier
13093 (i.e., a type declaration)
13094 2: one of the decl-specifiers is an enum-specifier or a
13095 class-specifier (i.e., a type definition)
13100 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13101 cp_parser_flags flags
,
13102 cp_decl_specifier_seq
*decl_specs
,
13103 int* declares_class_or_enum
)
13105 bool constructor_possible_p
= !parser
->in_declarator_p
;
13106 bool found_decl_spec
= false;
13107 cp_token
*start_token
= NULL
;
13110 /* Clear DECL_SPECS. */
13111 clear_decl_specs (decl_specs
);
13113 /* Assume no class or enumeration type is declared. */
13114 *declares_class_or_enum
= 0;
13116 /* Keep reading specifiers until there are no more to read. */
13119 bool constructor_p
;
13123 /* Peek at the next token. */
13124 token
= cp_lexer_peek_token (parser
->lexer
);
13126 /* Save the first token of the decl spec list for error
13129 start_token
= token
;
13130 /* Handle attributes. */
13131 if (cp_next_tokens_can_be_attribute_p (parser
))
13133 /* Parse the attributes. */
13134 tree attrs
= cp_parser_attributes_opt (parser
);
13136 /* In a sequence of declaration specifiers, c++11 attributes
13137 appertain to the type that precede them. In that case
13140 The attribute-specifier-seq affects the type only for
13141 the declaration it appears in, not other declarations
13142 involving the same type.
13144 But for now let's force the user to position the
13145 attribute either at the beginning of the declaration or
13146 after the declarator-id, which would clearly mean that it
13147 applies to the declarator. */
13148 if (cxx11_attribute_p (attrs
))
13150 if (!found_decl_spec
)
13151 /* The c++11 attribute is at the beginning of the
13152 declaration. It appertains to the entity being
13156 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13158 /* This is an attribute following a
13159 class-specifier. */
13160 if (decl_specs
->type_definition_p
)
13161 warn_misplaced_attr_for_class_type (token
->location
,
13167 decl_specs
->std_attributes
13168 = chainon (decl_specs
->std_attributes
,
13170 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13171 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13177 decl_specs
->attributes
13178 = chainon (decl_specs
->attributes
,
13180 if (decl_specs
->locations
[ds_attribute
] == 0)
13181 decl_specs
->locations
[ds_attribute
] = token
->location
;
13184 /* Assume we will find a decl-specifier keyword. */
13185 found_decl_spec
= true;
13186 /* If the next token is an appropriate keyword, we can simply
13187 add it to the list. */
13188 switch (token
->keyword
)
13194 if (!at_class_scope_p ())
13196 error_at (token
->location
, "%<friend%> used outside of class");
13197 cp_lexer_purge_token (parser
->lexer
);
13202 /* Consume the token. */
13203 cp_lexer_consume_token (parser
->lexer
);
13207 case RID_CONSTEXPR
:
13209 cp_lexer_consume_token (parser
->lexer
);
13214 cp_lexer_consume_token (parser
->lexer
);
13217 /* function-specifier:
13224 cp_parser_function_specifier_opt (parser
, decl_specs
);
13231 /* Consume the token. */
13232 cp_lexer_consume_token (parser
->lexer
);
13233 /* A constructor declarator cannot appear in a typedef. */
13234 constructor_possible_p
= false;
13235 /* The "typedef" keyword can only occur in a declaration; we
13236 may as well commit at this point. */
13237 cp_parser_commit_to_tentative_parse (parser
);
13239 if (decl_specs
->storage_class
!= sc_none
)
13240 decl_specs
->conflicting_specifiers_p
= true;
13243 /* storage-class-specifier:
13253 if (cxx_dialect
== cxx98
)
13255 /* Consume the token. */
13256 cp_lexer_consume_token (parser
->lexer
);
13258 /* Complain about `auto' as a storage specifier, if
13259 we're complaining about C++0x compatibility. */
13260 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
13261 " changes meaning in C++11; please remove it");
13263 /* Set the storage class anyway. */
13264 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13268 /* C++0x auto type-specifier. */
13269 found_decl_spec
= false;
13276 /* Consume the token. */
13277 cp_lexer_consume_token (parser
->lexer
);
13278 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13282 /* Consume the token. */
13284 cp_lexer_consume_token (parser
->lexer
);
13288 /* We did not yet find a decl-specifier yet. */
13289 found_decl_spec
= false;
13293 if (found_decl_spec
13294 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13295 && token
->keyword
!= RID_CONSTEXPR
)
13296 error ("decl-specifier invalid in condition");
13298 if (found_decl_spec
13299 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13300 && token
->keyword
!= RID_MUTABLE
13301 && token
->keyword
!= RID_CONSTEXPR
)
13302 error_at (token
->location
, "%qD invalid in lambda",
13303 ridpointers
[token
->keyword
]);
13306 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13308 /* Constructors are a special case. The `S' in `S()' is not a
13309 decl-specifier; it is the beginning of the declarator. */
13311 = (!found_decl_spec
13312 && constructor_possible_p
13313 && (cp_parser_constructor_declarator_p
13314 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13316 /* If we don't have a DECL_SPEC yet, then we must be looking at
13317 a type-specifier. */
13318 if (!found_decl_spec
&& !constructor_p
)
13320 int decl_spec_declares_class_or_enum
;
13321 bool is_cv_qualifier
;
13325 = cp_parser_type_specifier (parser
, flags
,
13327 /*is_declaration=*/true,
13328 &decl_spec_declares_class_or_enum
,
13330 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13332 /* If this type-specifier referenced a user-defined type
13333 (a typedef, class-name, etc.), then we can't allow any
13334 more such type-specifiers henceforth.
13338 The longest sequence of decl-specifiers that could
13339 possibly be a type name is taken as the
13340 decl-specifier-seq of a declaration. The sequence shall
13341 be self-consistent as described below.
13345 As a general rule, at most one type-specifier is allowed
13346 in the complete decl-specifier-seq of a declaration. The
13347 only exceptions are the following:
13349 -- const or volatile can be combined with any other
13352 -- signed or unsigned can be combined with char, long,
13360 void g (const int Pc);
13362 Here, Pc is *not* part of the decl-specifier seq; it's
13363 the declarator. Therefore, once we see a type-specifier
13364 (other than a cv-qualifier), we forbid any additional
13365 user-defined types. We *do* still allow things like `int
13366 int' to be considered a decl-specifier-seq, and issue the
13367 error message later. */
13368 if (type_spec
&& !is_cv_qualifier
)
13369 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13370 /* A constructor declarator cannot follow a type-specifier. */
13373 constructor_possible_p
= false;
13374 found_decl_spec
= true;
13375 if (!is_cv_qualifier
)
13376 decl_specs
->any_type_specifiers_p
= true;
13380 /* If we still do not have a DECL_SPEC, then there are no more
13381 decl-specifiers. */
13382 if (!found_decl_spec
)
13385 decl_specs
->any_specifiers_p
= true;
13386 /* After we see one decl-specifier, further decl-specifiers are
13387 always optional. */
13388 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13391 /* Don't allow a friend specifier with a class definition. */
13392 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13393 && (*declares_class_or_enum
& 2))
13394 error_at (decl_specs
->locations
[ds_friend
],
13395 "class definition may not be declared a friend");
13398 /* Parse an (optional) storage-class-specifier.
13400 storage-class-specifier:
13409 storage-class-specifier:
13412 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13415 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13417 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13420 if (cxx_dialect
!= cxx98
)
13422 /* Fall through for C++98. */
13423 gcc_fallthrough ();
13430 /* Consume the token. */
13431 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13438 /* Parse an (optional) function-specifier.
13440 function-specifier:
13445 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13446 Updates DECL_SPECS, if it is non-NULL. */
13449 cp_parser_function_specifier_opt (cp_parser
* parser
,
13450 cp_decl_specifier_seq
*decl_specs
)
13452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13453 switch (token
->keyword
)
13456 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13460 /* 14.5.2.3 [temp.mem]
13462 A member function template shall not be virtual. */
13463 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13464 && current_class_type
)
13465 error_at (token
->location
, "templates may not be %<virtual%>");
13467 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13471 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13478 /* Consume the token. */
13479 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13482 /* Parse a linkage-specification.
13484 linkage-specification:
13485 extern string-literal { declaration-seq [opt] }
13486 extern string-literal declaration */
13489 cp_parser_linkage_specification (cp_parser
* parser
)
13493 /* Look for the `extern' keyword. */
13494 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13496 /* Look for the string-literal. */
13497 linkage
= cp_parser_string_literal (parser
, false, false);
13499 /* Transform the literal into an identifier. If the literal is a
13500 wide-character string, or contains embedded NULs, then we can't
13501 handle it as the user wants. */
13502 if (strlen (TREE_STRING_POINTER (linkage
))
13503 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13505 cp_parser_error (parser
, "invalid linkage-specification");
13506 /* Assume C++ linkage. */
13507 linkage
= lang_name_cplusplus
;
13510 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13512 /* We're now using the new linkage. */
13513 push_lang_context (linkage
);
13515 /* If the next token is a `{', then we're using the first
13517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13519 cp_ensure_no_omp_declare_simd (parser
);
13520 cp_ensure_no_oacc_routine (parser
);
13522 /* Consume the `{' token. */
13523 cp_lexer_consume_token (parser
->lexer
);
13524 /* Parse the declarations. */
13525 cp_parser_declaration_seq_opt (parser
);
13526 /* Look for the closing `}'. */
13527 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13529 /* Otherwise, there's just one declaration. */
13532 bool saved_in_unbraced_linkage_specification_p
;
13534 saved_in_unbraced_linkage_specification_p
13535 = parser
->in_unbraced_linkage_specification_p
;
13536 parser
->in_unbraced_linkage_specification_p
= true;
13537 cp_parser_declaration (parser
);
13538 parser
->in_unbraced_linkage_specification_p
13539 = saved_in_unbraced_linkage_specification_p
;
13542 /* We're done with the linkage-specification. */
13543 pop_lang_context ();
13546 /* Parse a static_assert-declaration.
13548 static_assert-declaration:
13549 static_assert ( constant-expression , string-literal ) ;
13550 static_assert ( constant-expression ) ; (C++1Z)
13552 If MEMBER_P, this static_assert is a class member. */
13555 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13560 location_t saved_loc
;
13563 /* Peek at the `static_assert' token so we can keep track of exactly
13564 where the static assertion started. */
13565 token
= cp_lexer_peek_token (parser
->lexer
);
13566 saved_loc
= token
->location
;
13568 /* Look for the `static_assert' keyword. */
13569 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13573 /* We know we are in a static assertion; commit to any tentative
13575 if (cp_parser_parsing_tentatively (parser
))
13576 cp_parser_commit_to_tentative_parse (parser
);
13578 /* Parse the `(' starting the static assertion condition. */
13579 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
13581 /* Parse the constant-expression. Allow a non-constant expression
13582 here in order to give better diagnostics in finish_static_assert. */
13584 cp_parser_constant_expression (parser
,
13585 /*allow_non_constant_p=*/true,
13586 /*non_constant_p=*/&dummy
);
13588 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13590 if (cxx_dialect
< cxx1z
)
13591 pedwarn (input_location
, OPT_Wpedantic
,
13592 "static_assert without a message "
13593 "only available with -std=c++1z or -std=gnu++1z");
13595 cp_lexer_consume_token (parser
->lexer
);
13596 message
= build_string (1, "");
13597 TREE_TYPE (message
) = char_array_type_node
;
13598 fix_string_type (message
);
13602 /* Parse the separating `,'. */
13603 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13605 /* Parse the string-literal message. */
13606 message
= cp_parser_string_literal (parser
,
13607 /*translate=*/false,
13610 /* A `)' completes the static assertion. */
13611 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13612 cp_parser_skip_to_closing_parenthesis (parser
,
13613 /*recovering=*/true,
13614 /*or_comma=*/false,
13615 /*consume_paren=*/true);
13618 /* A semicolon terminates the declaration. */
13619 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13621 /* Complete the static assertion, which may mean either processing
13622 the static assert now or saving it for template instantiation. */
13623 finish_static_assert (condition
, message
, saved_loc
, member_p
);
13626 /* Parse the expression in decltype ( expression ). */
13629 cp_parser_decltype_expr (cp_parser
*parser
,
13630 bool &id_expression_or_member_access_p
)
13632 cp_token
*id_expr_start_token
;
13635 /* Since we're going to preserve any side-effects from this parse, set up a
13636 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13637 in the expression. */
13638 tentative_firewall
firewall (parser
);
13640 /* First, try parsing an id-expression. */
13641 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13642 cp_parser_parse_tentatively (parser
);
13643 expr
= cp_parser_id_expression (parser
,
13644 /*template_keyword_p=*/false,
13645 /*check_dependency_p=*/true,
13646 /*template_p=*/NULL
,
13647 /*declarator_p=*/false,
13648 /*optional_p=*/false);
13650 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13652 bool non_integral_constant_expression_p
= false;
13653 tree id_expression
= expr
;
13655 const char *error_msg
;
13657 if (identifier_p (expr
))
13658 /* Lookup the name we got back from the id-expression. */
13659 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13660 id_expr_start_token
->location
);
13663 && expr
!= error_mark_node
13664 && TREE_CODE (expr
) != TYPE_DECL
13665 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13666 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13667 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13669 /* Complete lookup of the id-expression. */
13670 expr
= (finish_id_expression
13671 (id_expression
, expr
, parser
->scope
, &idk
,
13672 /*integral_constant_expression_p=*/false,
13673 /*allow_non_integral_constant_expression_p=*/true,
13674 &non_integral_constant_expression_p
,
13675 /*template_p=*/false,
13677 /*address_p=*/false,
13678 /*template_arg_p=*/false,
13680 id_expr_start_token
->location
));
13682 if (expr
== error_mark_node
)
13683 /* We found an id-expression, but it was something that we
13684 should not have found. This is an error, not something
13685 we can recover from, so note that we found an
13686 id-expression and we'll recover as gracefully as
13688 id_expression_or_member_access_p
= true;
13692 && expr
!= error_mark_node
13693 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13694 /* We have an id-expression. */
13695 id_expression_or_member_access_p
= true;
13698 if (!id_expression_or_member_access_p
)
13700 /* Abort the id-expression parse. */
13701 cp_parser_abort_tentative_parse (parser
);
13703 /* Parsing tentatively, again. */
13704 cp_parser_parse_tentatively (parser
);
13706 /* Parse a class member access. */
13707 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13708 /*cast_p=*/false, /*decltype*/true,
13709 /*member_access_only_p=*/true, NULL
);
13712 && expr
!= error_mark_node
13713 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13714 /* We have an id-expression. */
13715 id_expression_or_member_access_p
= true;
13718 if (id_expression_or_member_access_p
)
13719 /* We have parsed the complete id-expression or member access. */
13720 cp_parser_parse_definitely (parser
);
13723 /* Abort our attempt to parse an id-expression or member access
13725 cp_parser_abort_tentative_parse (parser
);
13727 /* Parse a full expression. */
13728 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13729 /*decltype_p=*/true);
13735 /* Parse a `decltype' type. Returns the type.
13737 simple-type-specifier:
13738 decltype ( expression )
13740 decltype ( auto ) */
13743 cp_parser_decltype (cp_parser
*parser
)
13746 bool id_expression_or_member_access_p
= false;
13747 const char *saved_message
;
13748 bool saved_integral_constant_expression_p
;
13749 bool saved_non_integral_constant_expression_p
;
13750 bool saved_greater_than_is_operator_p
;
13751 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13753 if (start_token
->type
== CPP_DECLTYPE
)
13755 /* Already parsed. */
13756 cp_lexer_consume_token (parser
->lexer
);
13757 return saved_checks_value (start_token
->u
.tree_check_value
);
13760 /* Look for the `decltype' token. */
13761 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
13762 return error_mark_node
;
13764 /* Parse the opening `('. */
13765 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13766 return error_mark_node
;
13768 /* decltype (auto) */
13769 if (cxx_dialect
>= cxx14
13770 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
13772 cp_lexer_consume_token (parser
->lexer
);
13773 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13774 return error_mark_node
;
13775 expr
= make_decltype_auto ();
13776 AUTO_IS_DECLTYPE (expr
) = true;
13780 /* Types cannot be defined in a `decltype' expression. Save away the
13782 saved_message
= parser
->type_definition_forbidden_message
;
13784 /* And create the new one. */
13785 parser
->type_definition_forbidden_message
13786 = G_("types may not be defined in %<decltype%> expressions");
13788 /* The restrictions on constant-expressions do not apply inside
13789 decltype expressions. */
13790 saved_integral_constant_expression_p
13791 = parser
->integral_constant_expression_p
;
13792 saved_non_integral_constant_expression_p
13793 = parser
->non_integral_constant_expression_p
;
13794 parser
->integral_constant_expression_p
= false;
13796 /* Within a parenthesized expression, a `>' token is always
13797 the greater-than operator. */
13798 saved_greater_than_is_operator_p
13799 = parser
->greater_than_is_operator_p
;
13800 parser
->greater_than_is_operator_p
= true;
13802 /* Do not actually evaluate the expression. */
13803 ++cp_unevaluated_operand
;
13805 /* Do not warn about problems with the expression. */
13806 ++c_inhibit_evaluation_warnings
;
13808 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
13810 /* Go back to evaluating expressions. */
13811 --cp_unevaluated_operand
;
13812 --c_inhibit_evaluation_warnings
;
13814 /* The `>' token might be the end of a template-id or
13815 template-parameter-list now. */
13816 parser
->greater_than_is_operator_p
13817 = saved_greater_than_is_operator_p
;
13819 /* Restore the old message and the integral constant expression
13821 parser
->type_definition_forbidden_message
= saved_message
;
13822 parser
->integral_constant_expression_p
13823 = saved_integral_constant_expression_p
;
13824 parser
->non_integral_constant_expression_p
13825 = saved_non_integral_constant_expression_p
;
13827 /* Parse to the closing `)'. */
13828 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13830 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13831 /*consume_paren=*/true);
13832 return error_mark_node
;
13835 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
13836 tf_warning_or_error
);
13839 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13841 start_token
->type
= CPP_DECLTYPE
;
13842 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13843 start_token
->u
.tree_check_value
->value
= expr
;
13844 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13845 start_token
->keyword
= RID_MAX
;
13846 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
13851 /* Special member functions [gram.special] */
13853 /* Parse a conversion-function-id.
13855 conversion-function-id:
13856 operator conversion-type-id
13858 Returns an IDENTIFIER_NODE representing the operator. */
13861 cp_parser_conversion_function_id (cp_parser
* parser
)
13865 tree saved_qualifying_scope
;
13866 tree saved_object_scope
;
13867 tree pushed_scope
= NULL_TREE
;
13869 /* Look for the `operator' token. */
13870 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13871 return error_mark_node
;
13872 /* When we parse the conversion-type-id, the current scope will be
13873 reset. However, we need that information in able to look up the
13874 conversion function later, so we save it here. */
13875 saved_scope
= parser
->scope
;
13876 saved_qualifying_scope
= parser
->qualifying_scope
;
13877 saved_object_scope
= parser
->object_scope
;
13878 /* We must enter the scope of the class so that the names of
13879 entities declared within the class are available in the
13880 conversion-type-id. For example, consider:
13887 S::operator I() { ... }
13889 In order to see that `I' is a type-name in the definition, we
13890 must be in the scope of `S'. */
13892 pushed_scope
= push_scope (saved_scope
);
13893 /* Parse the conversion-type-id. */
13894 type
= cp_parser_conversion_type_id (parser
);
13895 /* Leave the scope of the class, if any. */
13897 pop_scope (pushed_scope
);
13898 /* Restore the saved scope. */
13899 parser
->scope
= saved_scope
;
13900 parser
->qualifying_scope
= saved_qualifying_scope
;
13901 parser
->object_scope
= saved_object_scope
;
13902 /* If the TYPE is invalid, indicate failure. */
13903 if (type
== error_mark_node
)
13904 return error_mark_node
;
13905 return mangle_conv_op_name_for_type (type
);
13908 /* Parse a conversion-type-id:
13910 conversion-type-id:
13911 type-specifier-seq conversion-declarator [opt]
13913 Returns the TYPE specified. */
13916 cp_parser_conversion_type_id (cp_parser
* parser
)
13919 cp_decl_specifier_seq type_specifiers
;
13920 cp_declarator
*declarator
;
13921 tree type_specified
;
13922 const char *saved_message
;
13924 /* Parse the attributes. */
13925 attributes
= cp_parser_attributes_opt (parser
);
13927 saved_message
= parser
->type_definition_forbidden_message
;
13928 parser
->type_definition_forbidden_message
13929 = G_("types may not be defined in a conversion-type-id");
13931 /* Parse the type-specifiers. */
13932 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13933 /*is_trailing_return=*/false,
13936 parser
->type_definition_forbidden_message
= saved_message
;
13938 /* If that didn't work, stop. */
13939 if (type_specifiers
.type
== error_mark_node
)
13940 return error_mark_node
;
13941 /* Parse the conversion-declarator. */
13942 declarator
= cp_parser_conversion_declarator_opt (parser
);
13944 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
13945 /*initialized=*/0, &attributes
);
13947 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
13949 /* Don't give this error when parsing tentatively. This happens to
13950 work because we always parse this definitively once. */
13951 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
13952 && type_uses_auto (type_specified
))
13954 if (cxx_dialect
< cxx14
)
13956 error ("invalid use of %<auto%> in conversion operator");
13957 return error_mark_node
;
13959 else if (template_parm_scope_p ())
13960 warning (0, "use of %<auto%> in member template "
13961 "conversion operator can never be deduced");
13964 return type_specified
;
13967 /* Parse an (optional) conversion-declarator.
13969 conversion-declarator:
13970 ptr-operator conversion-declarator [opt]
13974 static cp_declarator
*
13975 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
13977 enum tree_code code
;
13978 tree class_type
, std_attributes
= NULL_TREE
;
13979 cp_cv_quals cv_quals
;
13981 /* We don't know if there's a ptr-operator next, or not. */
13982 cp_parser_parse_tentatively (parser
);
13983 /* Try the ptr-operator. */
13984 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
13986 /* If it worked, look for more conversion-declarators. */
13987 if (cp_parser_parse_definitely (parser
))
13989 cp_declarator
*declarator
;
13991 /* Parse another optional declarator. */
13992 declarator
= cp_parser_conversion_declarator_opt (parser
);
13994 declarator
= cp_parser_make_indirect_declarator
13995 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14003 /* Parse an (optional) ctor-initializer.
14006 : mem-initializer-list
14008 Returns TRUE iff the ctor-initializer was actually present. */
14011 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14013 /* If the next token is not a `:', then there is no
14014 ctor-initializer. */
14015 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14017 /* Do default initialization of any bases and members. */
14018 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14019 finish_mem_initializers (NULL_TREE
);
14024 /* Consume the `:' token. */
14025 cp_lexer_consume_token (parser
->lexer
);
14026 /* And the mem-initializer-list. */
14027 cp_parser_mem_initializer_list (parser
);
14032 /* Parse a mem-initializer-list.
14034 mem-initializer-list:
14035 mem-initializer ... [opt]
14036 mem-initializer ... [opt] , mem-initializer-list */
14039 cp_parser_mem_initializer_list (cp_parser
* parser
)
14041 tree mem_initializer_list
= NULL_TREE
;
14042 tree target_ctor
= error_mark_node
;
14043 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14045 /* Let the semantic analysis code know that we are starting the
14046 mem-initializer-list. */
14047 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14048 error_at (token
->location
,
14049 "only constructors take member initializers");
14051 /* Loop through the list. */
14054 tree mem_initializer
;
14056 token
= cp_lexer_peek_token (parser
->lexer
);
14057 /* Parse the mem-initializer. */
14058 mem_initializer
= cp_parser_mem_initializer (parser
);
14059 /* If the next token is a `...', we're expanding member initializers. */
14060 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14062 /* Consume the `...'. */
14063 cp_lexer_consume_token (parser
->lexer
);
14065 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14066 can be expanded but members cannot. */
14067 if (mem_initializer
!= error_mark_node
14068 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14070 error_at (token
->location
,
14071 "cannot expand initializer for member %<%D%>",
14072 TREE_PURPOSE (mem_initializer
));
14073 mem_initializer
= error_mark_node
;
14076 /* Construct the pack expansion type. */
14077 if (mem_initializer
!= error_mark_node
)
14078 mem_initializer
= make_pack_expansion (mem_initializer
);
14080 if (target_ctor
!= error_mark_node
14081 && mem_initializer
!= error_mark_node
)
14083 error ("mem-initializer for %qD follows constructor delegation",
14084 TREE_PURPOSE (mem_initializer
));
14085 mem_initializer
= error_mark_node
;
14087 /* Look for a target constructor. */
14088 if (mem_initializer
!= error_mark_node
14089 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14090 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14092 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14093 if (mem_initializer_list
)
14095 error ("constructor delegation follows mem-initializer for %qD",
14096 TREE_PURPOSE (mem_initializer_list
));
14097 mem_initializer
= error_mark_node
;
14099 target_ctor
= mem_initializer
;
14101 /* Add it to the list, unless it was erroneous. */
14102 if (mem_initializer
!= error_mark_node
)
14104 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14105 mem_initializer_list
= mem_initializer
;
14107 /* If the next token is not a `,', we're done. */
14108 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14110 /* Consume the `,' token. */
14111 cp_lexer_consume_token (parser
->lexer
);
14114 /* Perform semantic analysis. */
14115 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14116 finish_mem_initializers (mem_initializer_list
);
14119 /* Parse a mem-initializer.
14122 mem-initializer-id ( expression-list [opt] )
14123 mem-initializer-id braced-init-list
14128 ( expression-list [opt] )
14130 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14131 class) or FIELD_DECL (for a non-static data member) to initialize;
14132 the TREE_VALUE is the expression-list. An empty initialization
14133 list is represented by void_list_node. */
14136 cp_parser_mem_initializer (cp_parser
* parser
)
14138 tree mem_initializer_id
;
14139 tree expression_list
;
14141 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14143 /* Find out what is being initialized. */
14144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14146 permerror (token
->location
,
14147 "anachronistic old-style base class initializer");
14148 mem_initializer_id
= NULL_TREE
;
14152 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14153 if (mem_initializer_id
== error_mark_node
)
14154 return mem_initializer_id
;
14156 member
= expand_member_init (mem_initializer_id
);
14157 if (member
&& !DECL_P (member
))
14158 in_base_initializer
= 1;
14160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14162 bool expr_non_constant_p
;
14163 cp_lexer_set_source_position (parser
->lexer
);
14164 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14165 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14166 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14167 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14171 vec
<tree
, va_gc
> *vec
;
14172 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14174 /*allow_expansion_p=*/true,
14175 /*non_constant_p=*/NULL
);
14177 return error_mark_node
;
14178 expression_list
= build_tree_list_vec (vec
);
14179 release_tree_vector (vec
);
14182 if (expression_list
== error_mark_node
)
14183 return error_mark_node
;
14184 if (!expression_list
)
14185 expression_list
= void_type_node
;
14187 in_base_initializer
= 0;
14189 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14192 /* Parse a mem-initializer-id.
14194 mem-initializer-id:
14195 :: [opt] nested-name-specifier [opt] class-name
14196 decltype-specifier (C++11)
14199 Returns a TYPE indicating the class to be initialized for the first
14200 production (and the second in C++11). Returns an IDENTIFIER_NODE
14201 indicating the data member to be initialized for the last production. */
14204 cp_parser_mem_initializer_id (cp_parser
* parser
)
14206 bool global_scope_p
;
14207 bool nested_name_specifier_p
;
14208 bool template_p
= false;
14211 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14213 /* `typename' is not allowed in this context ([temp.res]). */
14214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14216 error_at (token
->location
,
14217 "keyword %<typename%> not allowed in this context (a qualified "
14218 "member initializer is implicitly a type)");
14219 cp_lexer_consume_token (parser
->lexer
);
14221 /* Look for the optional `::' operator. */
14223 = (cp_parser_global_scope_opt (parser
,
14224 /*current_scope_valid_p=*/false)
14226 /* Look for the optional nested-name-specifier. The simplest way to
14231 The keyword `typename' is not permitted in a base-specifier or
14232 mem-initializer; in these contexts a qualified name that
14233 depends on a template-parameter is implicitly assumed to be a
14236 is to assume that we have seen the `typename' keyword at this
14238 nested_name_specifier_p
14239 = (cp_parser_nested_name_specifier_opt (parser
,
14240 /*typename_keyword_p=*/true,
14241 /*check_dependency_p=*/true,
14243 /*is_declaration=*/true)
14245 if (nested_name_specifier_p
)
14246 template_p
= cp_parser_optional_template_keyword (parser
);
14247 /* If there is a `::' operator or a nested-name-specifier, then we
14248 are definitely looking for a class-name. */
14249 if (global_scope_p
|| nested_name_specifier_p
)
14250 return cp_parser_class_name (parser
,
14251 /*typename_keyword_p=*/true,
14252 /*template_keyword_p=*/template_p
,
14254 /*check_dependency_p=*/true,
14255 /*class_head_p=*/false,
14256 /*is_declaration=*/true);
14257 /* Otherwise, we could also be looking for an ordinary identifier. */
14258 cp_parser_parse_tentatively (parser
);
14259 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14260 /* Try a decltype-specifier. */
14261 id
= cp_parser_decltype (parser
);
14263 /* Otherwise, try a class-name. */
14264 id
= cp_parser_class_name (parser
,
14265 /*typename_keyword_p=*/true,
14266 /*template_keyword_p=*/false,
14268 /*check_dependency_p=*/true,
14269 /*class_head_p=*/false,
14270 /*is_declaration=*/true);
14271 /* If we found one, we're done. */
14272 if (cp_parser_parse_definitely (parser
))
14274 /* Otherwise, look for an ordinary identifier. */
14275 return cp_parser_identifier (parser
);
14278 /* Overloading [gram.over] */
14280 /* Parse an operator-function-id.
14282 operator-function-id:
14285 Returns an IDENTIFIER_NODE for the operator which is a
14286 human-readable spelling of the identifier, e.g., `operator +'. */
14289 cp_parser_operator_function_id (cp_parser
* parser
)
14291 /* Look for the `operator' keyword. */
14292 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14293 return error_mark_node
;
14294 /* And then the name of the operator itself. */
14295 return cp_parser_operator (parser
);
14298 /* Return an identifier node for a user-defined literal operator.
14299 The suffix identifier is chained to the operator name identifier. */
14302 cp_literal_operator_id (const char* name
)
14305 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14306 + strlen (name
) + 10);
14307 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14308 identifier
= get_identifier (buffer
);
14313 /* Parse an operator.
14316 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14317 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14318 || ++ -- , ->* -> () []
14325 Returns an IDENTIFIER_NODE for the operator which is a
14326 human-readable spelling of the identifier, e.g., `operator +'. */
14329 cp_parser_operator (cp_parser
* parser
)
14331 tree id
= NULL_TREE
;
14335 /* Peek at the next token. */
14336 token
= cp_lexer_peek_token (parser
->lexer
);
14338 location_t start_loc
= token
->location
;
14340 /* Figure out which operator we have. */
14341 switch (token
->type
)
14347 /* The keyword should be either `new' or `delete'. */
14348 if (token
->keyword
== RID_NEW
)
14350 else if (token
->keyword
== RID_DELETE
)
14355 /* Consume the `new' or `delete' token. */
14356 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14358 /* Peek at the next token. */
14359 token
= cp_lexer_peek_token (parser
->lexer
);
14360 /* If it's a `[' token then this is the array variant of the
14362 if (token
->type
== CPP_OPEN_SQUARE
)
14364 /* Consume the `[' token. */
14365 cp_lexer_consume_token (parser
->lexer
);
14366 /* Look for the `]' token. */
14367 if (cp_token
*close_token
14368 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14369 end_loc
= close_token
->location
;
14370 id
= cp_operator_id (op
== NEW_EXPR
14371 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
14373 /* Otherwise, we have the non-array variant. */
14375 id
= cp_operator_id (op
);
14377 location_t loc
= make_location (start_loc
, start_loc
, end_loc
);
14379 return cp_expr (id
, loc
);
14383 id
= cp_operator_id (PLUS_EXPR
);
14387 id
= cp_operator_id (MINUS_EXPR
);
14391 id
= cp_operator_id (MULT_EXPR
);
14395 id
= cp_operator_id (TRUNC_DIV_EXPR
);
14399 id
= cp_operator_id (TRUNC_MOD_EXPR
);
14403 id
= cp_operator_id (BIT_XOR_EXPR
);
14407 id
= cp_operator_id (BIT_AND_EXPR
);
14411 id
= cp_operator_id (BIT_IOR_EXPR
);
14415 id
= cp_operator_id (BIT_NOT_EXPR
);
14419 id
= cp_operator_id (TRUTH_NOT_EXPR
);
14423 id
= cp_assignment_operator_id (NOP_EXPR
);
14427 id
= cp_operator_id (LT_EXPR
);
14431 id
= cp_operator_id (GT_EXPR
);
14435 id
= cp_assignment_operator_id (PLUS_EXPR
);
14439 id
= cp_assignment_operator_id (MINUS_EXPR
);
14443 id
= cp_assignment_operator_id (MULT_EXPR
);
14447 id
= cp_assignment_operator_id (TRUNC_DIV_EXPR
);
14451 id
= cp_assignment_operator_id (TRUNC_MOD_EXPR
);
14455 id
= cp_assignment_operator_id (BIT_XOR_EXPR
);
14459 id
= cp_assignment_operator_id (BIT_AND_EXPR
);
14463 id
= cp_assignment_operator_id (BIT_IOR_EXPR
);
14467 id
= cp_operator_id (LSHIFT_EXPR
);
14471 id
= cp_operator_id (RSHIFT_EXPR
);
14474 case CPP_LSHIFT_EQ
:
14475 id
= cp_assignment_operator_id (LSHIFT_EXPR
);
14478 case CPP_RSHIFT_EQ
:
14479 id
= cp_assignment_operator_id (RSHIFT_EXPR
);
14483 id
= cp_operator_id (EQ_EXPR
);
14487 id
= cp_operator_id (NE_EXPR
);
14491 id
= cp_operator_id (LE_EXPR
);
14494 case CPP_GREATER_EQ
:
14495 id
= cp_operator_id (GE_EXPR
);
14499 id
= cp_operator_id (TRUTH_ANDIF_EXPR
);
14503 id
= cp_operator_id (TRUTH_ORIF_EXPR
);
14506 case CPP_PLUS_PLUS
:
14507 id
= cp_operator_id (POSTINCREMENT_EXPR
);
14510 case CPP_MINUS_MINUS
:
14511 id
= cp_operator_id (PREDECREMENT_EXPR
);
14515 id
= cp_operator_id (COMPOUND_EXPR
);
14518 case CPP_DEREF_STAR
:
14519 id
= cp_operator_id (MEMBER_REF
);
14523 id
= cp_operator_id (COMPONENT_REF
);
14526 case CPP_OPEN_PAREN
:
14527 /* Consume the `('. */
14528 cp_lexer_consume_token (parser
->lexer
);
14529 /* Look for the matching `)'. */
14530 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14531 return cp_operator_id (CALL_EXPR
);
14533 case CPP_OPEN_SQUARE
:
14534 /* Consume the `['. */
14535 cp_lexer_consume_token (parser
->lexer
);
14536 /* Look for the matching `]'. */
14537 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14538 return cp_operator_id (ARRAY_REF
);
14540 case CPP_UTF8STRING
:
14541 case CPP_UTF8STRING_USERDEF
:
14548 case CPP_STRING_USERDEF
:
14549 case CPP_WSTRING_USERDEF
:
14550 case CPP_STRING16_USERDEF
:
14551 case CPP_STRING32_USERDEF
:
14553 tree str
, string_tree
;
14556 if (cxx_dialect
== cxx98
)
14557 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14559 /* Consume the string. */
14560 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14561 /*wide_ok=*/true, /*lookup_udlit=*/false);
14562 if (str
== error_mark_node
)
14563 return error_mark_node
;
14564 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14566 string_tree
= USERDEF_LITERAL_VALUE (str
);
14567 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14572 /* Look for the suffix identifier. */
14573 token
= cp_lexer_peek_token (parser
->lexer
);
14574 if (token
->type
== CPP_NAME
)
14575 id
= cp_parser_identifier (parser
);
14576 else if (token
->type
== CPP_KEYWORD
)
14578 error ("unexpected keyword;"
14579 " remove space between quotes and suffix identifier");
14580 return error_mark_node
;
14584 error ("expected suffix identifier");
14585 return error_mark_node
;
14588 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14589 (TREE_TYPE (TREE_TYPE (string_tree
))));
14590 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14593 error ("expected empty string after %<operator%> keyword");
14594 return error_mark_node
;
14596 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14599 error ("invalid encoding prefix in literal operator");
14600 return error_mark_node
;
14602 if (id
!= error_mark_node
)
14604 const char *name
= IDENTIFIER_POINTER (id
);
14605 id
= cp_literal_operator_id (name
);
14611 /* Anything else is an error. */
14615 /* If we have selected an identifier, we need to consume the
14618 cp_lexer_consume_token (parser
->lexer
);
14619 /* Otherwise, no valid operator name was present. */
14622 cp_parser_error (parser
, "expected operator");
14623 id
= error_mark_node
;
14626 return cp_expr (id
, start_loc
);
14629 /* Parse a template-declaration.
14631 template-declaration:
14632 export [opt] template < template-parameter-list > declaration
14634 If MEMBER_P is TRUE, this template-declaration occurs within a
14637 The grammar rule given by the standard isn't correct. What
14638 is really meant is:
14640 template-declaration:
14641 export [opt] template-parameter-list-seq
14642 decl-specifier-seq [opt] init-declarator [opt] ;
14643 export [opt] template-parameter-list-seq
14644 function-definition
14646 template-parameter-list-seq:
14647 template-parameter-list-seq [opt]
14648 template < template-parameter-list >
14650 Concept Extensions:
14652 template-parameter-list-seq:
14653 template < template-parameter-list > requires-clause [opt]
14656 requires logical-or-expression */
14659 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14661 /* Check for `export'. */
14662 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14664 /* Consume the `export' token. */
14665 cp_lexer_consume_token (parser
->lexer
);
14666 /* Warn that we do not support `export'. */
14667 warning (0, "keyword %<export%> not implemented, and will be ignored");
14670 cp_parser_template_declaration_after_export (parser
, member_p
);
14673 /* Parse a template-parameter-list.
14675 template-parameter-list:
14677 template-parameter-list , template-parameter
14679 Returns a TREE_LIST. Each node represents a template parameter.
14680 The nodes are connected via their TREE_CHAINs. */
14683 cp_parser_template_parameter_list (cp_parser
* parser
)
14685 tree parameter_list
= NULL_TREE
;
14687 begin_template_parm_list ();
14689 /* The loop below parses the template parms. We first need to know
14690 the total number of template parms to be able to compute proper
14691 canonical types of each dependent type. So after the loop, when
14692 we know the total number of template parms,
14693 end_template_parm_list computes the proper canonical types and
14694 fixes up the dependent types accordingly. */
14699 bool is_parameter_pack
;
14700 location_t parm_loc
;
14702 /* Parse the template-parameter. */
14703 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14704 parameter
= cp_parser_template_parameter (parser
,
14706 &is_parameter_pack
);
14707 /* Add it to the list. */
14708 if (parameter
!= error_mark_node
)
14709 parameter_list
= process_template_parm (parameter_list
,
14713 is_parameter_pack
);
14716 tree err_parm
= build_tree_list (parameter
, parameter
);
14717 parameter_list
= chainon (parameter_list
, err_parm
);
14720 /* If the next token is not a `,', we're done. */
14721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14723 /* Otherwise, consume the `,' token. */
14724 cp_lexer_consume_token (parser
->lexer
);
14727 return end_template_parm_list (parameter_list
);
14730 /* Parse a introduction-list.
14733 introduced-parameter
14734 introduction-list , introduced-parameter
14736 introduced-parameter:
14737 ...[opt] identifier
14739 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14740 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14741 WILDCARD_DECL will also have DECL_NAME set and token location in
14742 DECL_SOURCE_LOCATION. */
14745 cp_parser_introduction_list (cp_parser
*parser
)
14747 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
14751 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14753 cp_lexer_consume_token (parser
->lexer
);
14755 /* Build placeholder. */
14756 tree parm
= build_nt (WILDCARD_DECL
);
14757 DECL_SOURCE_LOCATION (parm
)
14758 = cp_lexer_peek_token (parser
->lexer
)->location
;
14759 DECL_NAME (parm
) = cp_parser_identifier (parser
);
14760 WILDCARD_PACK_P (parm
) = is_pack
;
14761 vec_safe_push (introduction_vec
, parm
);
14763 /* If the next token is not a `,', we're done. */
14764 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14766 /* Otherwise, consume the `,' token. */
14767 cp_lexer_consume_token (parser
->lexer
);
14770 /* Convert the vec into a TREE_VEC. */
14771 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
14774 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
14775 TREE_VEC_ELT (introduction_list
, n
) = parm
;
14777 release_tree_vector (introduction_vec
);
14778 return introduction_list
;
14781 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14782 is an abstract declarator. */
14784 static inline cp_declarator
*
14785 get_id_declarator (cp_declarator
*declarator
)
14787 cp_declarator
*d
= declarator
;
14788 while (d
&& d
->kind
!= cdk_id
)
14793 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14794 is an abstract declarator. */
14797 get_unqualified_id (cp_declarator
*declarator
)
14799 declarator
= get_id_declarator (declarator
);
14801 return declarator
->u
.id
.unqualified_name
;
14806 /* Returns true if DECL represents a constrained-parameter. */
14809 is_constrained_parameter (tree decl
)
14812 && TREE_CODE (decl
) == TYPE_DECL
14813 && CONSTRAINED_PARM_CONCEPT (decl
)
14814 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
14817 /* Returns true if PARM declares a constrained-parameter. */
14820 is_constrained_parameter (cp_parameter_declarator
*parm
)
14822 return is_constrained_parameter (parm
->decl_specifiers
.type
);
14825 /* Check that the type parameter is only a declarator-id, and that its
14826 type is not cv-qualified. */
14829 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
14830 cp_parameter_declarator
*parm
)
14832 if (!parm
->declarator
)
14835 if (parm
->declarator
->kind
!= cdk_id
)
14837 cp_parser_error (parser
, "invalid constrained type parameter");
14841 /* Don't allow cv-qualified type parameters. */
14842 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
14843 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
14845 cp_parser_error (parser
, "cv-qualified type parameter");
14852 /* Finish parsing/processing a template type parameter and checking
14853 various restrictions. */
14856 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
14858 cp_parameter_declarator
* parmdecl
)
14860 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14861 return finish_template_type_parm (class_type_node
, id
);
14863 return error_mark_node
;
14867 finish_constrained_template_template_parm (tree proto
, tree id
)
14869 /* FIXME: This should probably be copied, and we may need to adjust
14870 the template parameter depths. */
14871 tree saved_parms
= current_template_parms
;
14872 begin_template_parm_list ();
14873 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
14874 end_template_parm_list ();
14876 tree parm
= finish_template_template_parm (class_type_node
, id
);
14877 current_template_parms
= saved_parms
;
14882 /* Finish parsing/processing a template template parameter by borrowing
14883 the template parameter list from the prototype parameter. */
14886 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
14889 cp_parameter_declarator
*parmdecl
)
14891 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14892 return error_mark_node
;
14893 return finish_constrained_template_template_parm (proto
, id
);
14896 /* Create a new non-type template parameter from the given PARM
14900 constrained_non_type_template_parm (bool *is_non_type
,
14901 cp_parameter_declarator
*parm
)
14903 *is_non_type
= true;
14904 cp_declarator
*decl
= parm
->declarator
;
14905 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
14906 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
14907 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
14910 /* Build a constrained template parameter based on the PARMDECL
14911 declarator. The type of PARMDECL is the constrained type, which
14912 refers to the prototype template parameter that ultimately
14913 specifies the type of the declared parameter. */
14916 finish_constrained_parameter (cp_parser
*parser
,
14917 cp_parameter_declarator
*parmdecl
,
14919 bool *is_parameter_pack
)
14921 tree decl
= parmdecl
->decl_specifiers
.type
;
14922 tree id
= get_unqualified_id (parmdecl
->declarator
);
14923 tree def
= parmdecl
->default_argument
;
14924 tree proto
= DECL_INITIAL (decl
);
14926 /* A template parameter constrained by a variadic concept shall also
14927 be declared as a template parameter pack. */
14928 bool is_variadic
= template_parameter_pack_p (proto
);
14929 if (is_variadic
&& !*is_parameter_pack
)
14930 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
14932 /* Build the parameter. Return an error if the declarator was invalid. */
14934 if (TREE_CODE (proto
) == TYPE_DECL
)
14935 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
14936 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
14937 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
14940 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
14941 if (parm
== error_mark_node
)
14942 return error_mark_node
;
14944 /* Finish the parameter decl and create a node attaching the
14945 default argument and constraint. */
14946 parm
= build_tree_list (def
, parm
);
14947 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
14952 /* Returns true if the parsed type actually represents the declaration
14953 of a type template-parameter. */
14956 declares_constrained_type_template_parameter (tree type
)
14958 return (is_constrained_parameter (type
)
14959 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
14963 /* Returns true if the parsed type actually represents the declaration of
14964 a template template-parameter. */
14967 declares_constrained_template_template_parameter (tree type
)
14969 return (is_constrained_parameter (type
)
14970 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
14973 /* Parse a default argument for a type template-parameter.
14974 Note that diagnostics are handled in cp_parser_template_parameter. */
14977 cp_parser_default_type_template_argument (cp_parser
*parser
)
14979 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14981 /* Consume the `=' token. */
14982 cp_lexer_consume_token (parser
->lexer
);
14984 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14986 /* Parse the default-argument. */
14987 push_deferring_access_checks (dk_no_deferred
);
14988 tree default_argument
= cp_parser_type_id (parser
);
14989 pop_deferring_access_checks ();
14991 if (flag_concepts
&& type_uses_auto (default_argument
))
14993 error_at (token
->location
,
14994 "invalid use of %<auto%> in default template argument");
14995 return error_mark_node
;
14998 return default_argument
;
15001 /* Parse a default argument for a template template-parameter. */
15004 cp_parser_default_template_template_argument (cp_parser
*parser
)
15006 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15010 /* Consume the `='. */
15011 cp_lexer_consume_token (parser
->lexer
);
15012 /* Parse the id-expression. */
15013 push_deferring_access_checks (dk_no_deferred
);
15014 /* save token before parsing the id-expression, for error
15016 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15017 tree default_argument
15018 = cp_parser_id_expression (parser
,
15019 /*template_keyword_p=*/false,
15020 /*check_dependency_p=*/true,
15021 /*template_p=*/&is_template
,
15022 /*declarator_p=*/false,
15023 /*optional_p=*/false);
15024 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15025 /* If the id-expression was a template-id that refers to
15026 a template-class, we already have the declaration here,
15027 so no further lookup is needed. */
15030 /* Look up the name. */
15032 = cp_parser_lookup_name (parser
, default_argument
,
15034 /*is_template=*/is_template
,
15035 /*is_namespace=*/false,
15036 /*check_dependency=*/true,
15037 /*ambiguous_decls=*/NULL
,
15039 /* See if the default argument is valid. */
15040 default_argument
= check_template_template_default_arg (default_argument
);
15041 pop_deferring_access_checks ();
15042 return default_argument
;
15045 /* Parse a template-parameter.
15047 template-parameter:
15049 parameter-declaration
15051 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15052 the parameter. The TREE_PURPOSE is the default value, if any.
15053 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15054 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15055 set to true iff this parameter is a parameter pack. */
15058 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15059 bool *is_parameter_pack
)
15062 cp_parameter_declarator
*parameter_declarator
;
15065 /* Assume it is a type parameter or a template parameter. */
15066 *is_non_type
= false;
15067 /* Assume it not a parameter pack. */
15068 *is_parameter_pack
= false;
15069 /* Peek at the next token. */
15070 token
= cp_lexer_peek_token (parser
->lexer
);
15071 /* If it is `class' or `template', we have a type-parameter. */
15072 if (token
->keyword
== RID_TEMPLATE
)
15073 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15074 /* If it is `class' or `typename' we do not know yet whether it is a
15075 type parameter or a non-type parameter. Consider:
15077 template <typename T, typename T::X X> ...
15081 template <class C, class D*> ...
15083 Here, the first parameter is a type parameter, and the second is
15084 a non-type parameter. We can tell by looking at the token after
15085 the identifier -- if it is a `,', `=', or `>' then we have a type
15087 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15089 /* Peek at the token after `class' or `typename'. */
15090 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15091 /* If it's an ellipsis, we have a template type parameter
15093 if (token
->type
== CPP_ELLIPSIS
)
15094 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15095 /* If it's an identifier, skip it. */
15096 if (token
->type
== CPP_NAME
)
15097 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15098 /* Now, see if the token looks like the end of a template
15100 if (token
->type
== CPP_COMMA
15101 || token
->type
== CPP_EQ
15102 || token
->type
== CPP_GREATER
)
15103 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15106 /* Otherwise, it is a non-type parameter or a constrained parameter.
15110 When parsing a default template-argument for a non-type
15111 template-parameter, the first non-nested `>' is taken as the end
15112 of the template parameter-list rather than a greater-than
15114 parameter_declarator
15115 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15116 /*parenthesized_p=*/NULL
);
15118 if (!parameter_declarator
)
15119 return error_mark_node
;
15121 /* If the parameter declaration is marked as a parameter pack, set
15122 *IS_PARAMETER_PACK to notify the caller. */
15123 if (parameter_declarator
->template_parameter_pack_p
)
15124 *is_parameter_pack
= true;
15126 if (parameter_declarator
->default_argument
)
15128 /* Can happen in some cases of erroneous input (c++/34892). */
15129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15130 /* Consume the `...' for better error recovery. */
15131 cp_lexer_consume_token (parser
->lexer
);
15134 // The parameter may have been constrained.
15135 if (is_constrained_parameter (parameter_declarator
))
15136 return finish_constrained_parameter (parser
,
15137 parameter_declarator
,
15139 is_parameter_pack
);
15141 // Now we're sure that the parameter is a non-type parameter.
15142 *is_non_type
= true;
15144 parm
= grokdeclarator (parameter_declarator
->declarator
,
15145 ¶meter_declarator
->decl_specifiers
,
15146 TPARM
, /*initialized=*/0,
15147 /*attrlist=*/NULL
);
15148 if (parm
== error_mark_node
)
15149 return error_mark_node
;
15151 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15154 /* Parse a type-parameter.
15157 class identifier [opt]
15158 class identifier [opt] = type-id
15159 typename identifier [opt]
15160 typename identifier [opt] = type-id
15161 template < template-parameter-list > class identifier [opt]
15162 template < template-parameter-list > class identifier [opt]
15165 GNU Extension (variadic templates):
15168 class ... identifier [opt]
15169 typename ... identifier [opt]
15171 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15172 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15173 the declaration of the parameter.
15175 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15178 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15183 /* Look for a keyword to tell us what kind of parameter this is. */
15184 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15186 return error_mark_node
;
15188 switch (token
->keyword
)
15194 tree default_argument
;
15196 /* If the next token is an ellipsis, we have a template
15198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15200 /* Consume the `...' token. */
15201 cp_lexer_consume_token (parser
->lexer
);
15202 maybe_warn_variadic_templates ();
15204 *is_parameter_pack
= true;
15207 /* If the next token is an identifier, then it names the
15209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15210 identifier
= cp_parser_identifier (parser
);
15212 identifier
= NULL_TREE
;
15214 /* Create the parameter. */
15215 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15217 /* If the next token is an `=', we have a default argument. */
15218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15221 = cp_parser_default_type_template_argument (parser
);
15223 /* Template parameter packs cannot have default
15225 if (*is_parameter_pack
)
15228 error_at (token
->location
,
15229 "template parameter pack %qD cannot have a "
15230 "default argument", identifier
);
15232 error_at (token
->location
,
15233 "template parameter packs cannot have "
15234 "default arguments");
15235 default_argument
= NULL_TREE
;
15237 else if (check_for_bare_parameter_packs (default_argument
))
15238 default_argument
= error_mark_node
;
15241 default_argument
= NULL_TREE
;
15243 /* Create the combined representation of the parameter and the
15244 default argument. */
15245 parameter
= build_tree_list (default_argument
, parameter
);
15252 tree default_argument
;
15254 /* Look for the `<'. */
15255 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15256 /* Parse the template-parameter-list. */
15257 cp_parser_template_parameter_list (parser
);
15258 /* Look for the `>'. */
15259 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15261 // If template requirements are present, parse them.
15264 tree reqs
= get_shorthand_constraints (current_template_parms
);
15265 if (tree r
= cp_parser_requires_clause_opt (parser
))
15266 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15267 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15270 /* Look for the `class' or 'typename' keywords. */
15271 cp_parser_type_parameter_key (parser
);
15272 /* If the next token is an ellipsis, we have a template
15274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15276 /* Consume the `...' token. */
15277 cp_lexer_consume_token (parser
->lexer
);
15278 maybe_warn_variadic_templates ();
15280 *is_parameter_pack
= true;
15282 /* If the next token is an `=', then there is a
15283 default-argument. If the next token is a `>', we are at
15284 the end of the parameter-list. If the next token is a `,',
15285 then we are at the end of this parameter. */
15286 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15287 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15288 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15290 identifier
= cp_parser_identifier (parser
);
15291 /* Treat invalid names as if the parameter were nameless. */
15292 if (identifier
== error_mark_node
)
15293 identifier
= NULL_TREE
;
15296 identifier
= NULL_TREE
;
15298 /* Create the template parameter. */
15299 parameter
= finish_template_template_parm (class_type_node
,
15302 /* If the next token is an `=', then there is a
15303 default-argument. */
15304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15307 = cp_parser_default_template_template_argument (parser
);
15309 /* Template parameter packs cannot have default
15311 if (*is_parameter_pack
)
15314 error_at (token
->location
,
15315 "template parameter pack %qD cannot "
15316 "have a default argument",
15319 error_at (token
->location
, "template parameter packs cannot "
15320 "have default arguments");
15321 default_argument
= NULL_TREE
;
15325 default_argument
= NULL_TREE
;
15327 /* Create the combined representation of the parameter and the
15328 default argument. */
15329 parameter
= build_tree_list (default_argument
, parameter
);
15334 gcc_unreachable ();
15341 /* Parse a template-id.
15344 template-name < template-argument-list [opt] >
15346 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15347 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15348 returned. Otherwise, if the template-name names a function, or set
15349 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15350 names a class, returns a TYPE_DECL for the specialization.
15352 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15353 uninstantiated templates. */
15356 cp_parser_template_id (cp_parser
*parser
,
15357 bool template_keyword_p
,
15358 bool check_dependency_p
,
15359 enum tag_types tag_type
,
15360 bool is_declaration
)
15365 cp_token_position start_of_id
= 0;
15366 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15367 bool is_identifier
;
15369 /* If the next token corresponds to a template-id, there is no need
15371 next_token
= cp_lexer_peek_token (parser
->lexer
);
15372 if (next_token
->type
== CPP_TEMPLATE_ID
)
15374 cp_lexer_consume_token (parser
->lexer
);
15375 return saved_checks_value (next_token
->u
.tree_check_value
);
15378 /* Avoid performing name lookup if there is no possibility of
15379 finding a template-id. */
15380 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
15381 || (next_token
->type
== CPP_NAME
15382 && !cp_parser_nth_token_starts_template_argument_list_p
15385 cp_parser_error (parser
, "expected template-id");
15386 return error_mark_node
;
15389 /* Remember where the template-id starts. */
15390 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15391 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15393 push_deferring_access_checks (dk_deferred
);
15395 /* Parse the template-name. */
15396 is_identifier
= false;
15397 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15398 check_dependency_p
,
15402 if (templ
== error_mark_node
|| is_identifier
)
15404 pop_deferring_access_checks ();
15408 /* Since we're going to preserve any side-effects from this parse, set up a
15409 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15410 in the template arguments. */
15411 tentative_firewall
firewall (parser
);
15413 /* If we find the sequence `[:' after a template-name, it's probably
15414 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15415 parse correctly the argument list. */
15416 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15417 == CPP_OPEN_SQUARE
)
15418 && next_token
->flags
& DIGRAPH
15419 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15421 && !(next_token_2
->flags
& PREV_WHITE
))
15423 cp_parser_parse_tentatively (parser
);
15424 /* Change `:' into `::'. */
15425 next_token_2
->type
= CPP_SCOPE
;
15426 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15428 cp_lexer_consume_token (parser
->lexer
);
15430 /* Parse the arguments. */
15431 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15432 if (!cp_parser_parse_definitely (parser
))
15434 /* If we couldn't parse an argument list, then we revert our changes
15435 and return simply an error. Maybe this is not a template-id
15437 next_token_2
->type
= CPP_COLON
;
15438 cp_parser_error (parser
, "expected %<<%>");
15439 pop_deferring_access_checks ();
15440 return error_mark_node
;
15442 /* Otherwise, emit an error about the invalid digraph, but continue
15443 parsing because we got our argument list. */
15444 if (permerror (next_token
->location
,
15445 "%<<::%> cannot begin a template-argument list"))
15447 static bool hint
= false;
15448 inform (next_token
->location
,
15449 "%<<:%> is an alternate spelling for %<[%>."
15450 " Insert whitespace between %<<%> and %<::%>");
15451 if (!hint
&& !flag_permissive
)
15453 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15454 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15455 "accept your code)");
15462 /* Look for the `<' that starts the template-argument-list. */
15463 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15465 pop_deferring_access_checks ();
15466 return error_mark_node
;
15468 /* Parse the arguments. */
15469 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15472 /* Build a representation of the specialization. */
15473 if (identifier_p (templ
))
15474 template_id
= build_min_nt_loc (next_token
->location
,
15477 else if (DECL_TYPE_TEMPLATE_P (templ
)
15478 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15480 bool entering_scope
;
15481 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15482 template (rather than some instantiation thereof) only if
15483 is not nested within some other construct. For example, in
15484 "template <typename T> void f(T) { A<T>::", A<T> is just an
15485 instantiation of A. */
15486 entering_scope
= (template_parm_scope_p ()
15487 && cp_lexer_next_token_is (parser
->lexer
,
15490 = finish_template_type (templ
, arguments
, entering_scope
);
15492 /* A template-like identifier may be a partial concept id. */
15493 else if (flag_concepts
15494 && (template_id
= (cp_parser_maybe_partial_concept_id
15495 (parser
, templ
, arguments
))))
15496 return template_id
;
15497 else if (variable_template_p (templ
))
15499 template_id
= lookup_template_variable (templ
, arguments
);
15500 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15501 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15505 /* If it's not a class-template or a template-template, it should be
15506 a function-template. */
15507 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15508 || TREE_CODE (templ
) == OVERLOAD
15509 || BASELINK_P (templ
)));
15511 template_id
= lookup_template_function (templ
, arguments
);
15512 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15513 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15516 /* If parsing tentatively, replace the sequence of tokens that makes
15517 up the template-id with a CPP_TEMPLATE_ID token. That way,
15518 should we re-parse the token stream, we will not have to repeat
15519 the effort required to do the parse, nor will we issue duplicate
15520 error messages about problems during instantiation of the
15523 /* Don't do this if we had a parse error in a declarator; re-parsing
15524 might succeed if a name changes meaning (60361). */
15525 && !(cp_parser_error_occurred (parser
)
15526 && cp_parser_parsing_tentatively (parser
)
15527 && parser
->in_declarator_p
))
15529 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
15531 /* Reset the contents of the START_OF_ID token. */
15532 token
->type
= CPP_TEMPLATE_ID
;
15534 /* Update the location to be of the form:
15535 template-name < template-argument-list [opt] >
15536 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15537 with caret == start at the start of the template-name,
15538 ranging until the closing '>'. */
15539 location_t finish_loc
15540 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15541 location_t combined_loc
15542 = make_location (token
->location
, token
->location
, finish_loc
);
15543 token
->location
= combined_loc
;
15545 /* Retrieve any deferred checks. Do not pop this access checks yet
15546 so the memory will not be reclaimed during token replacing below. */
15547 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15548 token
->u
.tree_check_value
->value
= template_id
;
15549 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15550 token
->keyword
= RID_MAX
;
15552 /* Purge all subsequent tokens. */
15553 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15555 /* ??? Can we actually assume that, if template_id ==
15556 error_mark_node, we will have issued a diagnostic to the
15557 user, as opposed to simply marking the tentative parse as
15559 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15560 error_at (token
->location
, "parse error in template argument list");
15563 pop_to_parent_deferring_access_checks ();
15564 return template_id
;
15567 /* Parse a template-name.
15572 The standard should actually say:
15576 operator-function-id
15578 A defect report has been filed about this issue.
15580 A conversion-function-id cannot be a template name because they cannot
15581 be part of a template-id. In fact, looking at this code:
15583 a.operator K<int>()
15585 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15586 It is impossible to call a templated conversion-function-id with an
15587 explicit argument list, since the only allowed template parameter is
15588 the type to which it is converting.
15590 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15591 `template' keyword, in a construction like:
15595 In that case `f' is taken to be a template-name, even though there
15596 is no way of knowing for sure.
15598 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15599 name refers to a set of overloaded functions, at least one of which
15600 is a template, or an IDENTIFIER_NODE with the name of the template,
15601 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15602 names are looked up inside uninstantiated templates. */
15605 cp_parser_template_name (cp_parser
* parser
,
15606 bool template_keyword_p
,
15607 bool check_dependency_p
,
15608 bool is_declaration
,
15609 enum tag_types tag_type
,
15610 bool *is_identifier
)
15615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15617 /* If the next token is `operator', then we have either an
15618 operator-function-id or a conversion-function-id. */
15619 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15621 /* We don't know whether we're looking at an
15622 operator-function-id or a conversion-function-id. */
15623 cp_parser_parse_tentatively (parser
);
15624 /* Try an operator-function-id. */
15625 identifier
= cp_parser_operator_function_id (parser
);
15626 /* If that didn't work, try a conversion-function-id. */
15627 if (!cp_parser_parse_definitely (parser
))
15629 cp_parser_error (parser
, "expected template-name");
15630 return error_mark_node
;
15633 /* Look for the identifier. */
15635 identifier
= cp_parser_identifier (parser
);
15637 /* If we didn't find an identifier, we don't have a template-id. */
15638 if (identifier
== error_mark_node
)
15639 return error_mark_node
;
15641 /* If the name immediately followed the `template' keyword, then it
15642 is a template-name. However, if the next token is not `<', then
15643 we do not treat it as a template-name, since it is not being used
15644 as part of a template-id. This enables us to handle constructs
15647 template <typename T> struct S { S(); };
15648 template <typename T> S<T>::S();
15650 correctly. We would treat `S' as a template -- if it were `S<T>'
15651 -- but we do not if there is no `<'. */
15653 if (processing_template_decl
15654 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15656 /* In a declaration, in a dependent context, we pretend that the
15657 "template" keyword was present in order to improve error
15658 recovery. For example, given:
15660 template <typename T> void f(T::X<int>);
15662 we want to treat "X<int>" as a template-id. */
15664 && !template_keyword_p
15665 && parser
->scope
&& TYPE_P (parser
->scope
)
15666 && check_dependency_p
15667 && dependent_scope_p (parser
->scope
)
15668 /* Do not do this for dtors (or ctors), since they never
15669 need the template keyword before their name. */
15670 && !constructor_name_p (identifier
, parser
->scope
))
15672 cp_token_position start
= 0;
15674 /* Explain what went wrong. */
15675 error_at (token
->location
, "non-template %qD used as template",
15677 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15678 parser
->scope
, identifier
);
15679 /* If parsing tentatively, find the location of the "<" token. */
15680 if (cp_parser_simulate_error (parser
))
15681 start
= cp_lexer_token_position (parser
->lexer
, true);
15682 /* Parse the template arguments so that we can issue error
15683 messages about them. */
15684 cp_lexer_consume_token (parser
->lexer
);
15685 cp_parser_enclosed_template_argument_list (parser
);
15686 /* Skip tokens until we find a good place from which to
15687 continue parsing. */
15688 cp_parser_skip_to_closing_parenthesis (parser
,
15689 /*recovering=*/true,
15691 /*consume_paren=*/false);
15692 /* If parsing tentatively, permanently remove the
15693 template argument list. That will prevent duplicate
15694 error messages from being issued about the missing
15695 "template" keyword. */
15697 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15699 *is_identifier
= true;
15700 parser
->context
->object_type
= NULL_TREE
;
15704 /* If the "template" keyword is present, then there is generally
15705 no point in doing name-lookup, so we just return IDENTIFIER.
15706 But, if the qualifying scope is non-dependent then we can
15707 (and must) do name-lookup normally. */
15708 if (template_keyword_p
15710 || (TYPE_P (parser
->scope
)
15711 && dependent_type_p (parser
->scope
))))
15713 /* We're optimizing away the call to cp_parser_lookup_name, but we
15714 still need to do this. */
15715 parser
->context
->object_type
= NULL_TREE
;
15720 /* Look up the name. */
15721 decl
= cp_parser_lookup_name (parser
, identifier
,
15723 /*is_template=*/true,
15724 /*is_namespace=*/false,
15725 check_dependency_p
,
15726 /*ambiguous_decls=*/NULL
,
15729 decl
= strip_using_decl (decl
);
15731 /* If DECL is a template, then the name was a template-name. */
15732 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
15734 if (TREE_DEPRECATED (decl
)
15735 && deprecated_state
!= DEPRECATED_SUPPRESS
)
15736 warn_deprecated_use (decl
, NULL_TREE
);
15740 tree fn
= NULL_TREE
;
15742 /* The standard does not explicitly indicate whether a name that
15743 names a set of overloaded declarations, some of which are
15744 templates, is a template-name. However, such a name should
15745 be a template-name; otherwise, there is no way to form a
15746 template-id for the overloaded templates. */
15747 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
15748 if (TREE_CODE (fns
) == OVERLOAD
)
15749 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
15750 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
15755 /* The name does not name a template. */
15756 cp_parser_error (parser
, "expected template-name");
15757 return error_mark_node
;
15761 /* If DECL is dependent, and refers to a function, then just return
15762 its name; we will look it up again during template instantiation. */
15763 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
15765 tree scope
= ovl_scope (decl
);
15766 if (TYPE_P (scope
) && dependent_type_p (scope
))
15773 /* Parse a template-argument-list.
15775 template-argument-list:
15776 template-argument ... [opt]
15777 template-argument-list , template-argument ... [opt]
15779 Returns a TREE_VEC containing the arguments. */
15782 cp_parser_template_argument_list (cp_parser
* parser
)
15784 tree fixed_args
[10];
15785 unsigned n_args
= 0;
15786 unsigned alloced
= 10;
15787 tree
*arg_ary
= fixed_args
;
15789 bool saved_in_template_argument_list_p
;
15791 bool saved_non_ice_p
;
15793 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
15794 parser
->in_template_argument_list_p
= true;
15795 /* Even if the template-id appears in an integral
15796 constant-expression, the contents of the argument list do
15798 saved_ice_p
= parser
->integral_constant_expression_p
;
15799 parser
->integral_constant_expression_p
= false;
15800 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
15801 parser
->non_integral_constant_expression_p
= false;
15803 /* Parse the arguments. */
15809 /* Consume the comma. */
15810 cp_lexer_consume_token (parser
->lexer
);
15812 /* Parse the template-argument. */
15813 argument
= cp_parser_template_argument (parser
);
15815 /* If the next token is an ellipsis, we're expanding a template
15817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15819 if (argument
== error_mark_node
)
15821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15822 error_at (token
->location
,
15823 "expected parameter pack before %<...%>");
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser
->lexer
);
15828 /* Make the argument into a TYPE_PACK_EXPANSION or
15829 EXPR_PACK_EXPANSION. */
15830 argument
= make_pack_expansion (argument
);
15833 if (n_args
== alloced
)
15837 if (arg_ary
== fixed_args
)
15839 arg_ary
= XNEWVEC (tree
, alloced
);
15840 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
15843 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
15845 arg_ary
[n_args
++] = argument
;
15847 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15849 vec
= make_tree_vec (n_args
);
15852 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
15854 if (arg_ary
!= fixed_args
)
15856 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
15857 parser
->integral_constant_expression_p
= saved_ice_p
;
15858 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
15860 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
15864 /* Parse a template-argument.
15867 assignment-expression
15871 The representation is that of an assignment-expression, type-id, or
15872 id-expression -- except that the qualified id-expression is
15873 evaluated, so that the value returned is either a DECL or an
15876 Although the standard says "assignment-expression", it forbids
15877 throw-expressions or assignments in the template argument.
15878 Therefore, we use "conditional-expression" instead. */
15881 cp_parser_template_argument (cp_parser
* parser
)
15886 bool maybe_type_id
= false;
15887 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
15888 location_t loc
= 0;
15891 /* There's really no way to know what we're looking at, so we just
15892 try each alternative in order.
15896 In a template-argument, an ambiguity between a type-id and an
15897 expression is resolved to a type-id, regardless of the form of
15898 the corresponding template-parameter.
15900 Therefore, we try a type-id first. */
15901 cp_parser_parse_tentatively (parser
);
15902 argument
= cp_parser_template_type_arg (parser
);
15903 /* If there was no error parsing the type-id but the next token is a
15904 '>>', our behavior depends on which dialect of C++ we're
15905 parsing. In C++98, we probably found a typo for '> >'. But there
15906 are type-id which are also valid expressions. For instance:
15908 struct X { int operator >> (int); };
15909 template <int V> struct Foo {};
15912 Here 'X()' is a valid type-id of a function type, but the user just
15913 wanted to write the expression "X() >> 5". Thus, we remember that we
15914 found a valid type-id, but we still try to parse the argument as an
15915 expression to see what happens.
15917 In C++0x, the '>>' will be considered two separate '>'
15919 if (!cp_parser_error_occurred (parser
)
15920 && cxx_dialect
== cxx98
15921 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15923 maybe_type_id
= true;
15924 cp_parser_abort_tentative_parse (parser
);
15928 /* If the next token isn't a `,' or a `>', then this argument wasn't
15929 really finished. This means that the argument is not a valid
15931 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15932 cp_parser_error (parser
, "expected template-argument");
15933 /* If that worked, we're done. */
15934 if (cp_parser_parse_definitely (parser
))
15937 /* We're still not sure what the argument will be. */
15938 cp_parser_parse_tentatively (parser
);
15939 /* Try a template. */
15940 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
15941 argument
= cp_parser_id_expression (parser
,
15942 /*template_keyword_p=*/false,
15943 /*check_dependency_p=*/true,
15945 /*declarator_p=*/false,
15946 /*optional_p=*/false);
15947 /* If the next token isn't a `,' or a `>', then this argument wasn't
15948 really finished. */
15949 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15950 cp_parser_error (parser
, "expected template-argument");
15951 if (!cp_parser_error_occurred (parser
))
15953 /* Figure out what is being referred to. If the id-expression
15954 was for a class template specialization, then we will have a
15955 TYPE_DECL at this point. There is no need to do name lookup
15956 at this point in that case. */
15957 if (TREE_CODE (argument
) != TYPE_DECL
)
15958 argument
= cp_parser_lookup_name (parser
, argument
,
15960 /*is_template=*/template_p
,
15961 /*is_namespace=*/false,
15962 /*check_dependency=*/true,
15963 /*ambiguous_decls=*/NULL
,
15964 argument_start_token
->location
);
15965 /* Handle a constrained-type-specifier for a non-type template
15967 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
15969 else if (TREE_CODE (argument
) != TEMPLATE_DECL
15970 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
15971 cp_parser_error (parser
, "expected template-name");
15973 if (cp_parser_parse_definitely (parser
))
15975 if (TREE_DEPRECATED (argument
))
15976 warn_deprecated_use (argument
, NULL_TREE
);
15979 /* It must be a non-type argument. In C++17 any constant-expression is
15981 if (cxx_dialect
> cxx14
)
15984 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15986 -- an integral constant-expression of integral or enumeration
15989 -- the name of a non-type template-parameter; or
15991 -- the name of an object or function with external linkage...
15993 -- the address of an object or function with external linkage...
15995 -- a pointer to member... */
15996 /* Look for a non-type template parameter. */
15997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15999 cp_parser_parse_tentatively (parser
);
16000 argument
= cp_parser_primary_expression (parser
,
16001 /*address_p=*/false,
16003 /*template_arg_p=*/true,
16005 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16006 || !cp_parser_next_token_ends_template_argument_p (parser
))
16007 cp_parser_simulate_error (parser
);
16008 if (cp_parser_parse_definitely (parser
))
16012 /* If the next token is "&", the argument must be the address of an
16013 object or function with external linkage. */
16014 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16017 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16018 cp_lexer_consume_token (parser
->lexer
);
16020 /* See if we might have an id-expression. */
16021 token
= cp_lexer_peek_token (parser
->lexer
);
16022 if (token
->type
== CPP_NAME
16023 || token
->keyword
== RID_OPERATOR
16024 || token
->type
== CPP_SCOPE
16025 || token
->type
== CPP_TEMPLATE_ID
16026 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16028 cp_parser_parse_tentatively (parser
);
16029 argument
= cp_parser_primary_expression (parser
,
16032 /*template_arg_p=*/true,
16034 if (cp_parser_error_occurred (parser
)
16035 || !cp_parser_next_token_ends_template_argument_p (parser
))
16036 cp_parser_abort_tentative_parse (parser
);
16041 if (INDIRECT_REF_P (argument
))
16043 /* Strip the dereference temporarily. */
16044 gcc_assert (REFERENCE_REF_P (argument
));
16045 argument
= TREE_OPERAND (argument
, 0);
16048 /* If we're in a template, we represent a qualified-id referring
16049 to a static data member as a SCOPE_REF even if the scope isn't
16050 dependent so that we can check access control later. */
16052 if (TREE_CODE (probe
) == SCOPE_REF
)
16053 probe
= TREE_OPERAND (probe
, 1);
16056 /* A variable without external linkage might still be a
16057 valid constant-expression, so no error is issued here
16058 if the external-linkage check fails. */
16059 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16060 cp_parser_simulate_error (parser
);
16062 else if (is_overloaded_fn (argument
))
16063 /* All overloaded functions are allowed; if the external
16064 linkage test does not pass, an error will be issued
16068 && (TREE_CODE (argument
) == OFFSET_REF
16069 || TREE_CODE (argument
) == SCOPE_REF
))
16070 /* A pointer-to-member. */
16072 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16075 cp_parser_simulate_error (parser
);
16077 if (cp_parser_parse_definitely (parser
))
16080 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16081 tf_warning_or_error
);
16083 argument
= convert_from_reference (argument
);
16088 /* If the argument started with "&", there are no other valid
16089 alternatives at this point. */
16092 cp_parser_error (parser
, "invalid non-type template argument");
16093 return error_mark_node
;
16097 /* If the argument wasn't successfully parsed as a type-id followed
16098 by '>>', the argument can only be a constant expression now.
16099 Otherwise, we try parsing the constant-expression tentatively,
16100 because the argument could really be a type-id. */
16102 cp_parser_parse_tentatively (parser
);
16104 if (cxx_dialect
<= cxx14
)
16105 argument
= cp_parser_constant_expression (parser
);
16108 /* With C++17 generalized non-type template arguments we need to handle
16109 lvalue constant expressions, too. */
16110 argument
= cp_parser_assignment_expression (parser
);
16111 require_potential_constant_expression (argument
);
16114 if (!maybe_type_id
)
16116 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16117 cp_parser_error (parser
, "expected template-argument");
16118 if (cp_parser_parse_definitely (parser
))
16120 /* We did our best to parse the argument as a non type-id, but that
16121 was the only alternative that matched (albeit with a '>' after
16122 it). We can assume it's just a typo from the user, and a
16123 diagnostic will then be issued. */
16124 return cp_parser_template_type_arg (parser
);
16127 /* Parse an explicit-instantiation.
16129 explicit-instantiation:
16130 template declaration
16132 Although the standard says `declaration', what it really means is:
16134 explicit-instantiation:
16135 template decl-specifier-seq [opt] declarator [opt] ;
16137 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16138 supposed to be allowed. A defect report has been filed about this
16143 explicit-instantiation:
16144 storage-class-specifier template
16145 decl-specifier-seq [opt] declarator [opt] ;
16146 function-specifier template
16147 decl-specifier-seq [opt] declarator [opt] ; */
16150 cp_parser_explicit_instantiation (cp_parser
* parser
)
16152 int declares_class_or_enum
;
16153 cp_decl_specifier_seq decl_specifiers
;
16154 tree extension_specifier
= NULL_TREE
;
16156 timevar_push (TV_TEMPLATE_INST
);
16158 /* Look for an (optional) storage-class-specifier or
16159 function-specifier. */
16160 if (cp_parser_allow_gnu_extensions_p (parser
))
16162 extension_specifier
16163 = cp_parser_storage_class_specifier_opt (parser
);
16164 if (!extension_specifier
)
16165 extension_specifier
16166 = cp_parser_function_specifier_opt (parser
,
16167 /*decl_specs=*/NULL
);
16170 /* Look for the `template' keyword. */
16171 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16172 /* Let the front end know that we are processing an explicit
16174 begin_explicit_instantiation ();
16175 /* [temp.explicit] says that we are supposed to ignore access
16176 control while processing explicit instantiation directives. */
16177 push_deferring_access_checks (dk_no_check
);
16178 /* Parse a decl-specifier-seq. */
16179 cp_parser_decl_specifier_seq (parser
,
16180 CP_PARSER_FLAGS_OPTIONAL
,
16182 &declares_class_or_enum
);
16183 /* If there was exactly one decl-specifier, and it declared a class,
16184 and there's no declarator, then we have an explicit type
16186 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16190 type
= check_tag_decl (&decl_specifiers
,
16191 /*explicit_type_instantiation_p=*/true);
16192 /* Turn access control back on for names used during
16193 template instantiation. */
16194 pop_deferring_access_checks ();
16196 do_type_instantiation (type
, extension_specifier
,
16197 /*complain=*/tf_error
);
16201 cp_declarator
*declarator
;
16204 /* Parse the declarator. */
16206 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16207 /*ctor_dtor_or_conv_p=*/NULL
,
16208 /*parenthesized_p=*/NULL
,
16209 /*member_p=*/false,
16210 /*friend_p=*/false);
16211 if (declares_class_or_enum
& 2)
16212 cp_parser_check_for_definition_in_return_type (declarator
,
16213 decl_specifiers
.type
,
16214 decl_specifiers
.locations
[ds_type_spec
]);
16215 if (declarator
!= cp_error_declarator
)
16217 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16218 permerror (decl_specifiers
.locations
[ds_inline
],
16219 "explicit instantiation shall not use"
16220 " %<inline%> specifier");
16221 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16222 permerror (decl_specifiers
.locations
[ds_constexpr
],
16223 "explicit instantiation shall not use"
16224 " %<constexpr%> specifier");
16226 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16227 NORMAL
, 0, &decl_specifiers
.attributes
);
16228 /* Turn access control back on for names used during
16229 template instantiation. */
16230 pop_deferring_access_checks ();
16231 /* Do the explicit instantiation. */
16232 do_decl_instantiation (decl
, extension_specifier
);
16236 pop_deferring_access_checks ();
16237 /* Skip the body of the explicit instantiation. */
16238 cp_parser_skip_to_end_of_statement (parser
);
16241 /* We're done with the instantiation. */
16242 end_explicit_instantiation ();
16244 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16246 timevar_pop (TV_TEMPLATE_INST
);
16249 /* Parse an explicit-specialization.
16251 explicit-specialization:
16252 template < > declaration
16254 Although the standard says `declaration', what it really means is:
16256 explicit-specialization:
16257 template <> decl-specifier [opt] init-declarator [opt] ;
16258 template <> function-definition
16259 template <> explicit-specialization
16260 template <> template-declaration */
16263 cp_parser_explicit_specialization (cp_parser
* parser
)
16265 bool need_lang_pop
;
16266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16268 /* Look for the `template' keyword. */
16269 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16270 /* Look for the `<'. */
16271 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16272 /* Look for the `>'. */
16273 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16274 /* We have processed another parameter list. */
16275 ++parser
->num_template_parameter_lists
;
16278 A template ... explicit specialization ... shall not have C
16280 if (current_lang_name
== lang_name_c
)
16282 error_at (token
->location
, "template specialization with C linkage");
16283 /* Give it C++ linkage to avoid confusing other parts of the
16285 push_lang_context (lang_name_cplusplus
);
16286 need_lang_pop
= true;
16289 need_lang_pop
= false;
16290 /* Let the front end know that we are beginning a specialization. */
16291 if (!begin_specialization ())
16293 end_specialization ();
16297 /* If the next keyword is `template', we need to figure out whether
16298 or not we're looking a template-declaration. */
16299 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16301 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16302 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16303 cp_parser_template_declaration_after_export (parser
,
16304 /*member_p=*/false);
16306 cp_parser_explicit_specialization (parser
);
16309 /* Parse the dependent declaration. */
16310 cp_parser_single_declaration (parser
,
16312 /*member_p=*/false,
16313 /*explicit_specialization_p=*/true,
16314 /*friend_p=*/NULL
);
16315 /* We're done with the specialization. */
16316 end_specialization ();
16317 /* For the erroneous case of a template with C linkage, we pushed an
16318 implicit C++ linkage scope; exit that scope now. */
16320 pop_lang_context ();
16321 /* We're done with this parameter list. */
16322 --parser
->num_template_parameter_lists
;
16325 /* Parse a type-specifier.
16328 simple-type-specifier
16331 elaborated-type-specifier
16339 Returns a representation of the type-specifier. For a
16340 class-specifier, enum-specifier, or elaborated-type-specifier, a
16341 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16343 The parser flags FLAGS is used to control type-specifier parsing.
16345 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16346 in a decl-specifier-seq.
16348 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16349 class-specifier, enum-specifier, or elaborated-type-specifier, then
16350 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16351 if a type is declared; 2 if it is defined. Otherwise, it is set to
16354 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16355 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16356 is set to FALSE. */
16359 cp_parser_type_specifier (cp_parser
* parser
,
16360 cp_parser_flags flags
,
16361 cp_decl_specifier_seq
*decl_specs
,
16362 bool is_declaration
,
16363 int* declares_class_or_enum
,
16364 bool* is_cv_qualifier
)
16366 tree type_spec
= NULL_TREE
;
16369 cp_decl_spec ds
= ds_last
;
16371 /* Assume this type-specifier does not declare a new type. */
16372 if (declares_class_or_enum
)
16373 *declares_class_or_enum
= 0;
16374 /* And that it does not specify a cv-qualifier. */
16375 if (is_cv_qualifier
)
16376 *is_cv_qualifier
= false;
16377 /* Peek at the next token. */
16378 token
= cp_lexer_peek_token (parser
->lexer
);
16380 /* If we're looking at a keyword, we can use that to guide the
16381 production we choose. */
16382 keyword
= token
->keyword
;
16386 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16387 goto elaborated_type_specifier
;
16389 /* Look for the enum-specifier. */
16390 type_spec
= cp_parser_enum_specifier (parser
);
16391 /* If that worked, we're done. */
16394 if (declares_class_or_enum
)
16395 *declares_class_or_enum
= 2;
16397 cp_parser_set_decl_spec_type (decl_specs
,
16400 /*type_definition_p=*/true);
16404 goto elaborated_type_specifier
;
16406 /* Any of these indicate either a class-specifier, or an
16407 elaborated-type-specifier. */
16411 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16412 goto elaborated_type_specifier
;
16414 /* Parse tentatively so that we can back up if we don't find a
16415 class-specifier. */
16416 cp_parser_parse_tentatively (parser
);
16417 /* Look for the class-specifier. */
16418 type_spec
= cp_parser_class_specifier (parser
);
16419 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16420 /* If that worked, we're done. */
16421 if (cp_parser_parse_definitely (parser
))
16423 if (declares_class_or_enum
)
16424 *declares_class_or_enum
= 2;
16426 cp_parser_set_decl_spec_type (decl_specs
,
16429 /*type_definition_p=*/true);
16433 /* Fall through. */
16434 elaborated_type_specifier
:
16435 /* We're declaring (not defining) a class or enum. */
16436 if (declares_class_or_enum
)
16437 *declares_class_or_enum
= 1;
16439 /* Fall through. */
16441 /* Look for an elaborated-type-specifier. */
16443 = (cp_parser_elaborated_type_specifier
16445 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16448 cp_parser_set_decl_spec_type (decl_specs
,
16451 /*type_definition_p=*/false);
16456 if (is_cv_qualifier
)
16457 *is_cv_qualifier
= true;
16462 if (is_cv_qualifier
)
16463 *is_cv_qualifier
= true;
16468 if (is_cv_qualifier
)
16469 *is_cv_qualifier
= true;
16473 /* The `__complex__' keyword is a GNU extension. */
16481 /* Handle simple keywords. */
16486 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16487 decl_specs
->any_specifiers_p
= true;
16489 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16492 /* If we do not already have a type-specifier, assume we are looking
16493 at a simple-type-specifier. */
16494 type_spec
= cp_parser_simple_type_specifier (parser
,
16498 /* If we didn't find a type-specifier, and a type-specifier was not
16499 optional in this context, issue an error message. */
16500 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16502 cp_parser_error (parser
, "expected type specifier");
16503 return error_mark_node
;
16509 /* Parse a simple-type-specifier.
16511 simple-type-specifier:
16512 :: [opt] nested-name-specifier [opt] type-name
16513 :: [opt] nested-name-specifier template template-id
16528 simple-type-specifier:
16530 decltype ( expression )
16533 __underlying_type ( type-id )
16537 nested-name-specifier(opt) template-name
16541 simple-type-specifier:
16543 __typeof__ unary-expression
16544 __typeof__ ( type-id )
16545 __typeof__ ( type-id ) { initializer-list , [opt] }
16547 Concepts Extension:
16549 simple-type-specifier:
16550 constrained-type-specifier
16552 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16553 appropriately updated. */
16556 cp_parser_simple_type_specifier (cp_parser
* parser
,
16557 cp_decl_specifier_seq
*decl_specs
,
16558 cp_parser_flags flags
)
16560 tree type
= NULL_TREE
;
16564 /* Peek at the next token. */
16565 token
= cp_lexer_peek_token (parser
->lexer
);
16567 /* If we're looking at a keyword, things are easy. */
16568 switch (token
->keyword
)
16572 decl_specs
->explicit_char_p
= true;
16573 type
= char_type_node
;
16576 type
= char16_type_node
;
16579 type
= char32_type_node
;
16582 type
= wchar_type_node
;
16585 type
= boolean_type_node
;
16588 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16589 type
= short_integer_type_node
;
16593 decl_specs
->explicit_int_p
= true;
16594 type
= integer_type_node
;
16600 idx
= token
->keyword
- RID_INT_N_0
;
16601 if (! int_n_enabled_p
[idx
])
16605 decl_specs
->explicit_intN_p
= true;
16606 decl_specs
->int_n_idx
= idx
;
16608 type
= int_n_trees
[idx
].signed_type
;
16612 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16613 type
= long_integer_type_node
;
16616 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16617 type
= integer_type_node
;
16620 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16621 type
= unsigned_type_node
;
16624 type
= float_type_node
;
16627 type
= double_type_node
;
16630 type
= void_type_node
;
16634 maybe_warn_cpp0x (CPP0X_AUTO
);
16635 if (parser
->auto_is_implicit_function_template_parm_p
)
16637 /* The 'auto' might be the placeholder return type for a function decl
16638 with trailing return type. */
16639 bool have_trailing_return_fn_decl
= false;
16641 cp_parser_parse_tentatively (parser
);
16642 cp_lexer_consume_token (parser
->lexer
);
16643 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16644 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16645 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16646 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16650 cp_lexer_consume_token (parser
->lexer
);
16651 cp_parser_skip_to_closing_parenthesis (parser
,
16652 /*recovering*/false,
16654 /*consume_paren*/true);
16658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16660 have_trailing_return_fn_decl
= true;
16664 cp_lexer_consume_token (parser
->lexer
);
16666 cp_parser_abort_tentative_parse (parser
);
16668 if (have_trailing_return_fn_decl
)
16670 type
= make_auto ();
16674 if (cxx_dialect
>= cxx14
)
16676 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16677 type
= TREE_TYPE (type
);
16680 type
= error_mark_node
;
16682 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16684 if (cxx_dialect
< cxx14
)
16685 error_at (token
->location
,
16686 "use of %<auto%> in lambda parameter declaration "
16687 "only available with "
16688 "-std=c++14 or -std=gnu++14");
16690 else if (cxx_dialect
< cxx14
)
16691 error_at (token
->location
,
16692 "use of %<auto%> in parameter declaration "
16693 "only available with "
16694 "-std=c++14 or -std=gnu++14");
16695 else if (!flag_concepts
)
16696 pedwarn (token
->location
, OPT_Wpedantic
,
16697 "ISO C++ forbids use of %<auto%> in parameter "
16701 type
= make_auto ();
16705 /* Since DR 743, decltype can either be a simple-type-specifier by
16706 itself or begin a nested-name-specifier. Parsing it will replace
16707 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16708 handling below decide what to do. */
16709 cp_parser_decltype (parser
);
16710 cp_lexer_set_token_position (parser
->lexer
, token
);
16714 /* Consume the `typeof' token. */
16715 cp_lexer_consume_token (parser
->lexer
);
16716 /* Parse the operand to `typeof'. */
16717 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16718 /* If it is not already a TYPE, take its type. */
16719 if (!TYPE_P (type
))
16720 type
= finish_typeof (type
);
16723 cp_parser_set_decl_spec_type (decl_specs
, type
,
16725 /*type_definition_p=*/false);
16729 case RID_UNDERLYING_TYPE
:
16730 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
16732 cp_parser_set_decl_spec_type (decl_specs
, type
,
16734 /*type_definition_p=*/false);
16739 case RID_DIRECT_BASES
:
16740 type
= cp_parser_trait_expr (parser
, token
->keyword
);
16742 cp_parser_set_decl_spec_type (decl_specs
, type
,
16744 /*type_definition_p=*/false);
16750 /* If token is an already-parsed decltype not followed by ::,
16751 it's a simple-type-specifier. */
16752 if (token
->type
== CPP_DECLTYPE
16753 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
16755 type
= saved_checks_value (token
->u
.tree_check_value
);
16758 cp_parser_set_decl_spec_type (decl_specs
, type
,
16760 /*type_definition_p=*/false);
16761 /* Remember that we are handling a decltype in order to
16762 implement the resolution of DR 1510 when the argument
16763 isn't instantiation dependent. */
16764 decl_specs
->decltype_p
= true;
16766 cp_lexer_consume_token (parser
->lexer
);
16770 /* If the type-specifier was for a built-in type, we're done. */
16773 /* Record the type. */
16775 && (token
->keyword
!= RID_SIGNED
16776 && token
->keyword
!= RID_UNSIGNED
16777 && token
->keyword
!= RID_SHORT
16778 && token
->keyword
!= RID_LONG
))
16779 cp_parser_set_decl_spec_type (decl_specs
,
16782 /*type_definition_p=*/false);
16784 decl_specs
->any_specifiers_p
= true;
16786 /* Consume the token. */
16787 cp_lexer_consume_token (parser
->lexer
);
16789 if (type
== error_mark_node
)
16790 return error_mark_node
;
16792 /* There is no valid C++ program where a non-template type is
16793 followed by a "<". That usually indicates that the user thought
16794 that the type was a template. */
16795 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
16798 return TYPE_NAME (type
);
16801 /* The type-specifier must be a user-defined type. */
16802 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
16807 /* Don't gobble tokens or issue error messages if this is an
16808 optional type-specifier. */
16809 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16810 cp_parser_parse_tentatively (parser
);
16812 token
= cp_lexer_peek_token (parser
->lexer
);
16814 /* Look for the optional `::' operator. */
16816 = (cp_parser_global_scope_opt (parser
,
16817 /*current_scope_valid_p=*/false)
16819 /* Look for the nested-name specifier. */
16821 = (cp_parser_nested_name_specifier_opt (parser
,
16822 /*typename_keyword_p=*/false,
16823 /*check_dependency_p=*/true,
16825 /*is_declaration=*/false)
16827 /* If we have seen a nested-name-specifier, and the next token
16828 is `template', then we are using the template-id production. */
16830 && cp_parser_optional_template_keyword (parser
))
16832 /* Look for the template-id. */
16833 type
= cp_parser_template_id (parser
,
16834 /*template_keyword_p=*/true,
16835 /*check_dependency_p=*/true,
16837 /*is_declaration=*/false);
16838 /* If the template-id did not name a type, we are out of
16840 if (TREE_CODE (type
) != TYPE_DECL
)
16842 cp_parser_error (parser
, "expected template-id for type");
16846 /* Otherwise, look for a type-name. */
16848 type
= cp_parser_type_name (parser
);
16849 /* Keep track of all name-lookups performed in class scopes. */
16853 && TREE_CODE (type
) == TYPE_DECL
16854 && identifier_p (DECL_NAME (type
)))
16855 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
16856 /* If it didn't work out, we don't have a TYPE. */
16857 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16858 && !cp_parser_parse_definitely (parser
))
16860 if (!type
&& cxx_dialect
>= cxx1z
)
16862 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
16863 cp_parser_parse_tentatively (parser
);
16865 cp_parser_global_scope_opt (parser
,
16866 /*current_scope_valid_p=*/false);
16867 cp_parser_nested_name_specifier_opt (parser
,
16868 /*typename_keyword_p=*/false,
16869 /*check_dependency_p=*/true,
16871 /*is_declaration=*/false);
16872 tree name
= cp_parser_identifier (parser
);
16873 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
16874 && parser
->scope
!= error_mark_node
)
16876 tree tmpl
= cp_parser_lookup_name (parser
, name
,
16878 /*is_template=*/false,
16879 /*is_namespace=*/false,
16880 /*check_dependency=*/true,
16881 /*ambiguous_decls=*/NULL
,
16883 if (tmpl
&& tmpl
!= error_mark_node
16884 && (DECL_CLASS_TEMPLATE_P (tmpl
)
16885 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
16886 type
= make_template_placeholder (tmpl
);
16889 type
= error_mark_node
;
16890 if (!cp_parser_simulate_error (parser
))
16891 cp_parser_name_lookup_error (parser
, name
, tmpl
,
16892 NLE_TYPE
, token
->location
);
16896 type
= error_mark_node
;
16898 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
16899 && !cp_parser_parse_definitely (parser
))
16902 if (type
&& decl_specs
)
16903 cp_parser_set_decl_spec_type (decl_specs
, type
,
16905 /*type_definition_p=*/false);
16908 /* If we didn't get a type-name, issue an error message. */
16909 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16911 cp_parser_error (parser
, "expected type-name");
16912 return error_mark_node
;
16915 if (type
&& type
!= error_mark_node
)
16917 /* See if TYPE is an Objective-C type, and if so, parse and
16918 accept any protocol references following it. Do this before
16919 the cp_parser_check_for_invalid_template_id() call, because
16920 Objective-C types can be followed by '<...>' which would
16921 enclose protocol names rather than template arguments, and so
16922 everything is fine. */
16923 if (c_dialect_objc () && !parser
->scope
16924 && (objc_is_id (type
) || objc_is_class_name (type
)))
16926 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16927 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
16929 /* Clobber the "unqualified" type previously entered into
16930 DECL_SPECS with the new, improved protocol-qualified version. */
16932 decl_specs
->type
= qual_type
;
16937 /* There is no valid C++ program where a non-template type is
16938 followed by a "<". That usually indicates that the user
16939 thought that the type was a template. */
16940 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
16948 /* Parse a type-name.
16954 simple-template-id [in c++0x]
16971 Returns a TYPE_DECL for the type. */
16974 cp_parser_type_name (cp_parser
* parser
)
16976 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
16981 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
16985 /* We can't know yet whether it is a class-name or not. */
16986 cp_parser_parse_tentatively (parser
);
16987 /* Try a class-name. */
16988 type_decl
= cp_parser_class_name (parser
,
16989 typename_keyword_p
,
16990 /*template_keyword_p=*/false,
16992 /*check_dependency_p=*/true,
16993 /*class_head_p=*/false,
16994 /*is_declaration=*/false);
16995 /* If it's not a class-name, keep looking. */
16996 if (!cp_parser_parse_definitely (parser
))
16998 if (cxx_dialect
< cxx11
)
16999 /* It must be a typedef-name or an enum-name. */
17000 return cp_parser_nonclass_name (parser
);
17002 cp_parser_parse_tentatively (parser
);
17003 /* It is either a simple-template-id representing an
17004 instantiation of an alias template... */
17005 type_decl
= cp_parser_template_id (parser
,
17006 /*template_keyword_p=*/false,
17007 /*check_dependency_p=*/true,
17009 /*is_declaration=*/false);
17010 /* Note that this must be an instantiation of an alias template
17011 because [temp.names]/6 says:
17013 A template-id that names an alias template specialization
17016 Whereas [temp.names]/7 says:
17018 A simple-template-id that names a class template
17019 specialization is a class-name.
17021 With concepts, this could also be a partial-concept-id that
17022 declares a non-type template parameter. */
17023 if (type_decl
!= NULL_TREE
17024 && TREE_CODE (type_decl
) == TYPE_DECL
17025 && TYPE_DECL_ALIAS_P (type_decl
))
17026 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17027 else if (is_constrained_parameter (type_decl
))
17028 /* Don't do anything. */ ;
17030 cp_parser_simulate_error (parser
);
17032 if (!cp_parser_parse_definitely (parser
))
17033 /* ... Or a typedef-name or an enum-name. */
17034 return cp_parser_nonclass_name (parser
);
17040 /* Check if DECL and ARGS can form a constrained-type-specifier.
17041 If ARGS is non-null, we try to form a concept check of the
17042 form DECL<?, ARGS> where ? is a wildcard that matches any
17043 kind of template argument. If ARGS is NULL, then we try to
17044 form a concept check of the form DECL<?>. */
17047 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17048 tree decl
, tree args
)
17050 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17052 /* If we a constrained-type-specifier cannot be deduced. */
17053 if (parser
->prevent_constrained_type_specifiers
)
17056 /* A constrained type specifier can only be found in an
17057 overload set or as a reference to a template declaration.
17059 FIXME: This might be masking a bug. It's possible that
17060 that the deduction below is causing template specializations
17061 to be formed with the wildcard as an argument. */
17062 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17065 /* Try to build a call expression that evaluates the
17066 concept. This can fail if the overload set refers
17067 only to non-templates. */
17068 tree placeholder
= build_nt (WILDCARD_DECL
);
17069 tree check
= build_concept_check (decl
, placeholder
, args
);
17070 if (check
== error_mark_node
)
17073 /* Deduce the checked constraint and the prototype parameter.
17075 FIXME: In certain cases, failure to deduce should be a
17076 diagnosable error. */
17079 if (!deduce_constrained_parameter (check
, conc
, proto
))
17082 /* In template parameter scope, this results in a constrained
17083 parameter. Return a descriptor of that parm. */
17084 if (processing_template_parmlist
)
17085 return build_constrained_parameter (conc
, proto
, args
);
17087 /* In a parameter-declaration-clause, constrained-type
17088 specifiers result in invented template parameters. */
17089 if (parser
->auto_is_implicit_function_template_parm_p
)
17091 tree x
= build_constrained_parameter (conc
, proto
, args
);
17092 return synthesize_implicit_template_parm (parser
, x
);
17096 /* Otherwise, we're in a context where the constrained
17097 type name is deduced and the constraint applies
17098 after deduction. */
17099 return make_constrained_auto (conc
, args
);
17105 /* If DECL refers to a concept, return a TYPE_DECL representing
17106 the result of using the constrained type specifier in the
17107 current context. DECL refers to a concept if
17109 - it is an overload set containing a function concept taking a single
17112 - it is a variable concept taking a single type argument. */
17115 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17118 && (TREE_CODE (decl
) == OVERLOAD
17119 || BASELINK_P (decl
)
17120 || variable_concept_p (decl
)))
17121 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17126 /* Check if DECL and ARGS form a partial-concept-id. If so,
17127 assign ID to the resulting constrained placeholder.
17129 Returns true if the partial-concept-id designates a placeholder
17130 and false otherwise. Note that *id is set to NULL_TREE in
17134 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17136 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17139 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17151 Returns a TYPE_DECL for the type. */
17154 cp_parser_nonclass_name (cp_parser
* parser
)
17159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17160 identifier
= cp_parser_identifier (parser
);
17161 if (identifier
== error_mark_node
)
17162 return error_mark_node
;
17164 /* Look up the type-name. */
17165 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17167 type_decl
= strip_using_decl (type_decl
);
17169 /* If we found an overload set, then it may refer to a concept-name. */
17170 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17173 if (TREE_CODE (type_decl
) != TYPE_DECL
17174 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17176 /* See if this is an Objective-C type. */
17177 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17178 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17180 type_decl
= TYPE_NAME (type
);
17183 /* Issue an error if we did not find a type-name. */
17184 if (TREE_CODE (type_decl
) != TYPE_DECL
17185 /* In Objective-C, we have the complication that class names are
17186 normally type names and start declarations (eg, the
17187 "NSObject" in "NSObject *object;"), but can be used in an
17188 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17189 is an expression. So, a classname followed by a dot is not a
17190 valid type-name. */
17191 || (objc_is_class_name (TREE_TYPE (type_decl
))
17192 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17194 if (!cp_parser_simulate_error (parser
))
17195 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17196 NLE_TYPE
, token
->location
);
17197 return error_mark_node
;
17199 /* Remember that the name was used in the definition of the
17200 current class so that we can check later to see if the
17201 meaning would have been different after the class was
17202 entirely defined. */
17203 else if (type_decl
!= error_mark_node
17205 maybe_note_name_used_in_class (identifier
, type_decl
);
17210 /* Parse an elaborated-type-specifier. Note that the grammar given
17211 here incorporates the resolution to DR68.
17213 elaborated-type-specifier:
17214 class-key :: [opt] nested-name-specifier [opt] identifier
17215 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17216 enum-key :: [opt] nested-name-specifier [opt] identifier
17217 typename :: [opt] nested-name-specifier identifier
17218 typename :: [opt] nested-name-specifier template [opt]
17223 elaborated-type-specifier:
17224 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17225 class-key attributes :: [opt] nested-name-specifier [opt]
17226 template [opt] template-id
17227 enum attributes :: [opt] nested-name-specifier [opt] identifier
17229 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17230 declared `friend'. If IS_DECLARATION is TRUE, then this
17231 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17232 something is being declared.
17234 Returns the TYPE specified. */
17237 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17239 bool is_declaration
)
17241 enum tag_types tag_type
;
17243 tree type
= NULL_TREE
;
17244 tree attributes
= NULL_TREE
;
17246 cp_token
*token
= NULL
;
17248 /* See if we're looking at the `enum' keyword. */
17249 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17251 /* Consume the `enum' token. */
17252 cp_lexer_consume_token (parser
->lexer
);
17253 /* Remember that it's an enumeration type. */
17254 tag_type
= enum_type
;
17255 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17256 enums) is used here. */
17257 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17258 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17260 pedwarn (input_location
, 0, "elaborated-type-specifier "
17261 "for a scoped enum must not use the %<%D%> keyword",
17262 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
17263 /* Consume the `struct' or `class' and parse it anyway. */
17264 cp_lexer_consume_token (parser
->lexer
);
17266 /* Parse the attributes. */
17267 attributes
= cp_parser_attributes_opt (parser
);
17269 /* Or, it might be `typename'. */
17270 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17273 /* Consume the `typename' token. */
17274 cp_lexer_consume_token (parser
->lexer
);
17275 /* Remember that it's a `typename' type. */
17276 tag_type
= typename_type
;
17278 /* Otherwise it must be a class-key. */
17281 tag_type
= cp_parser_class_key (parser
);
17282 if (tag_type
== none_type
)
17283 return error_mark_node
;
17284 /* Parse the attributes. */
17285 attributes
= cp_parser_attributes_opt (parser
);
17288 /* Look for the `::' operator. */
17289 globalscope
= cp_parser_global_scope_opt (parser
,
17290 /*current_scope_valid_p=*/false);
17291 /* Look for the nested-name-specifier. */
17292 tree nested_name_specifier
;
17293 if (tag_type
== typename_type
&& !globalscope
)
17295 nested_name_specifier
17296 = cp_parser_nested_name_specifier (parser
,
17297 /*typename_keyword_p=*/true,
17298 /*check_dependency_p=*/true,
17301 if (!nested_name_specifier
)
17302 return error_mark_node
;
17305 /* Even though `typename' is not present, the proposed resolution
17306 to Core Issue 180 says that in `class A<T>::B', `B' should be
17307 considered a type-name, even if `A<T>' is dependent. */
17308 nested_name_specifier
17309 = cp_parser_nested_name_specifier_opt (parser
,
17310 /*typename_keyword_p=*/true,
17311 /*check_dependency_p=*/true,
17314 /* For everything but enumeration types, consider a template-id.
17315 For an enumeration type, consider only a plain identifier. */
17316 if (tag_type
!= enum_type
)
17318 bool template_p
= false;
17321 /* Allow the `template' keyword. */
17322 template_p
= cp_parser_optional_template_keyword (parser
);
17323 /* If we didn't see `template', we don't know if there's a
17324 template-id or not. */
17326 cp_parser_parse_tentatively (parser
);
17327 /* Parse the template-id. */
17328 token
= cp_lexer_peek_token (parser
->lexer
);
17329 decl
= cp_parser_template_id (parser
, template_p
,
17330 /*check_dependency_p=*/true,
17333 /* If we didn't find a template-id, look for an ordinary
17335 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17337 /* We can get here when cp_parser_template_id, called by
17338 cp_parser_class_name with tag_type == none_type, succeeds
17339 and caches a BASELINK. Then, when called again here,
17340 instead of failing and returning an error_mark_node
17341 returns it (see template/typename17.C in C++11).
17342 ??? Could we diagnose this earlier? */
17343 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17345 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17346 type
= error_mark_node
;
17348 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17349 in effect, then we must assume that, upon instantiation, the
17350 template will correspond to a class. */
17351 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17352 && tag_type
== typename_type
)
17353 type
= make_typename_type (parser
->scope
, decl
,
17355 /*complain=*/tf_error
);
17356 /* If the `typename' keyword is in effect and DECL is not a type
17357 decl, then type is non existent. */
17358 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17360 else if (TREE_CODE (decl
) == TYPE_DECL
)
17362 type
= check_elaborated_type_specifier (tag_type
, decl
,
17363 /*allow_template_p=*/true);
17365 /* If the next token is a semicolon, this must be a specialization,
17366 instantiation, or friend declaration. Check the scope while we
17367 still know whether or not we had a nested-name-specifier. */
17368 if (type
!= error_mark_node
17369 && !nested_name_specifier
&& !is_friend
17370 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17371 check_unqualified_spec_or_inst (type
, token
->location
);
17373 else if (decl
== error_mark_node
)
17374 type
= error_mark_node
;
17379 token
= cp_lexer_peek_token (parser
->lexer
);
17380 identifier
= cp_parser_identifier (parser
);
17382 if (identifier
== error_mark_node
)
17384 parser
->scope
= NULL_TREE
;
17385 return error_mark_node
;
17388 /* For a `typename', we needn't call xref_tag. */
17389 if (tag_type
== typename_type
17390 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17391 return cp_parser_make_typename_type (parser
, identifier
,
17394 /* Template parameter lists apply only if we are not within a
17395 function parameter list. */
17396 bool template_parm_lists_apply
17397 = parser
->num_template_parameter_lists
;
17398 if (template_parm_lists_apply
)
17399 for (cp_binding_level
*s
= current_binding_level
;
17400 s
&& s
->kind
!= sk_template_parms
;
17401 s
= s
->level_chain
)
17402 if (s
->kind
== sk_function_parms
)
17403 template_parm_lists_apply
= false;
17405 /* Look up a qualified name in the usual way. */
17409 tree ambiguous_decls
;
17411 decl
= cp_parser_lookup_name (parser
, identifier
,
17413 /*is_template=*/false,
17414 /*is_namespace=*/false,
17415 /*check_dependency=*/true,
17419 /* If the lookup was ambiguous, an error will already have been
17421 if (ambiguous_decls
)
17422 return error_mark_node
;
17424 /* If we are parsing friend declaration, DECL may be a
17425 TEMPLATE_DECL tree node here. However, we need to check
17426 whether this TEMPLATE_DECL results in valid code. Consider
17427 the following example:
17430 template <class T> class C {};
17433 template <class T> friend class N::C; // #1, valid code
17435 template <class T> class Y {
17436 friend class N::C; // #2, invalid code
17439 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17440 name lookup of `N::C'. We see that friend declaration must
17441 be template for the code to be valid. Note that
17442 processing_template_decl does not work here since it is
17443 always 1 for the above two cases. */
17445 decl
= (cp_parser_maybe_treat_template_as_class
17446 (decl
, /*tag_name_p=*/is_friend
17447 && template_parm_lists_apply
));
17449 if (TREE_CODE (decl
) != TYPE_DECL
)
17451 cp_parser_diagnose_invalid_type_name (parser
,
17454 return error_mark_node
;
17457 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17459 bool allow_template
= (template_parm_lists_apply
17460 || DECL_SELF_REFERENCE_P (decl
));
17461 type
= check_elaborated_type_specifier (tag_type
, decl
,
17464 if (type
== error_mark_node
)
17465 return error_mark_node
;
17468 /* Forward declarations of nested types, such as
17473 are invalid unless all components preceding the final '::'
17474 are complete. If all enclosing types are complete, these
17475 declarations become merely pointless.
17477 Invalid forward declarations of nested types are errors
17478 caught elsewhere in parsing. Those that are pointless arrive
17481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17482 && !is_friend
&& !processing_explicit_instantiation
)
17483 warning (0, "declaration %qD does not declare anything", decl
);
17485 type
= TREE_TYPE (decl
);
17489 /* An elaborated-type-specifier sometimes introduces a new type and
17490 sometimes names an existing type. Normally, the rule is that it
17491 introduces a new type only if there is not an existing type of
17492 the same name already in scope. For example, given:
17495 void f() { struct S s; }
17497 the `struct S' in the body of `f' is the same `struct S' as in
17498 the global scope; the existing definition is used. However, if
17499 there were no global declaration, this would introduce a new
17500 local class named `S'.
17502 An exception to this rule applies to the following code:
17504 namespace N { struct S; }
17506 Here, the elaborated-type-specifier names a new type
17507 unconditionally; even if there is already an `S' in the
17508 containing scope this declaration names a new type.
17509 This exception only applies if the elaborated-type-specifier
17510 forms the complete declaration:
17514 A declaration consisting solely of `class-key identifier ;' is
17515 either a redeclaration of the name in the current scope or a
17516 forward declaration of the identifier as a class name. It
17517 introduces the name into the current scope.
17519 We are in this situation precisely when the next token is a `;'.
17521 An exception to the exception is that a `friend' declaration does
17522 *not* name a new type; i.e., given:
17524 struct S { friend struct T; };
17526 `T' is not a new type in the scope of `S'.
17528 Also, `new struct S' or `sizeof (struct S)' never results in the
17529 definition of a new type; a new type can only be declared in a
17530 declaration context. */
17536 /* Friends have special name lookup rules. */
17537 ts
= ts_within_enclosing_non_class
;
17538 else if (is_declaration
17539 && cp_lexer_next_token_is (parser
->lexer
,
17541 /* This is a `class-key identifier ;' */
17547 (template_parm_lists_apply
17548 && (cp_parser_next_token_starts_class_definition_p (parser
)
17549 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17550 /* An unqualified name was used to reference this type, so
17551 there were no qualifying templates. */
17552 if (template_parm_lists_apply
17553 && !cp_parser_check_template_parameters (parser
,
17554 /*num_templates=*/0,
17556 /*declarator=*/NULL
))
17557 return error_mark_node
;
17558 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17562 if (type
== error_mark_node
)
17563 return error_mark_node
;
17565 /* Allow attributes on forward declarations of classes. */
17568 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17569 warning (OPT_Wattributes
,
17570 "attributes ignored on uninstantiated type");
17571 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17572 && ! processing_explicit_instantiation
)
17573 warning (OPT_Wattributes
,
17574 "attributes ignored on template instantiation");
17575 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17576 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17578 warning (OPT_Wattributes
,
17579 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17582 if (tag_type
!= enum_type
)
17584 /* Indicate whether this class was declared as a `class' or as a
17586 if (CLASS_TYPE_P (type
))
17587 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17588 cp_parser_check_class_key (tag_type
, type
);
17591 /* A "<" cannot follow an elaborated type specifier. If that
17592 happens, the user was probably trying to form a template-id. */
17593 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17599 /* Parse an enum-specifier.
17602 enum-head { enumerator-list [opt] }
17603 enum-head { enumerator-list , } [C++0x]
17606 enum-key identifier [opt] enum-base [opt]
17607 enum-key nested-name-specifier identifier enum-base [opt]
17612 enum struct [C++0x]
17615 : type-specifier-seq
17617 opaque-enum-specifier:
17618 enum-key identifier enum-base [opt] ;
17621 enum-key attributes[opt] identifier [opt] enum-base [opt]
17622 { enumerator-list [opt] }attributes[opt]
17623 enum-key attributes[opt] identifier [opt] enum-base [opt]
17624 { enumerator-list, }attributes[opt] [C++0x]
17626 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17627 if the token stream isn't an enum-specifier after all. */
17630 cp_parser_enum_specifier (cp_parser
* parser
)
17633 tree type
= NULL_TREE
;
17635 tree nested_name_specifier
= NULL_TREE
;
17637 bool scoped_enum_p
= false;
17638 bool has_underlying_type
= false;
17639 bool nested_being_defined
= false;
17640 bool new_value_list
= false;
17641 bool is_new_type
= false;
17642 bool is_unnamed
= false;
17643 tree underlying_type
= NULL_TREE
;
17644 cp_token
*type_start_token
= NULL
;
17645 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17647 parser
->colon_corrects_to_scope_p
= false;
17649 /* Parse tentatively so that we can back up if we don't find a
17651 cp_parser_parse_tentatively (parser
);
17653 /* Caller guarantees that the current token is 'enum', an identifier
17654 possibly follows, and the token after that is an opening brace.
17655 If we don't have an identifier, fabricate an anonymous name for
17656 the enumeration being defined. */
17657 cp_lexer_consume_token (parser
->lexer
);
17659 /* Parse the "class" or "struct", which indicates a scoped
17660 enumeration type in C++0x. */
17661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17662 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17664 if (cxx_dialect
< cxx11
)
17665 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17667 /* Consume the `struct' or `class' token. */
17668 cp_lexer_consume_token (parser
->lexer
);
17670 scoped_enum_p
= true;
17673 attributes
= cp_parser_attributes_opt (parser
);
17675 /* Clear the qualification. */
17676 parser
->scope
= NULL_TREE
;
17677 parser
->qualifying_scope
= NULL_TREE
;
17678 parser
->object_scope
= NULL_TREE
;
17680 /* Figure out in what scope the declaration is being placed. */
17681 prev_scope
= current_scope ();
17683 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17685 push_deferring_access_checks (dk_no_check
);
17686 nested_name_specifier
17687 = cp_parser_nested_name_specifier_opt (parser
,
17688 /*typename_keyword_p=*/true,
17689 /*check_dependency_p=*/false,
17691 /*is_declaration=*/false);
17693 if (nested_name_specifier
)
17697 identifier
= cp_parser_identifier (parser
);
17698 name
= cp_parser_lookup_name (parser
, identifier
,
17700 /*is_template=*/false,
17701 /*is_namespace=*/false,
17702 /*check_dependency=*/true,
17703 /*ambiguous_decls=*/NULL
,
17705 if (name
&& name
!= error_mark_node
)
17707 type
= TREE_TYPE (name
);
17708 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17710 /* Are template enums allowed in ISO? */
17711 if (template_parm_scope_p ())
17712 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17713 "%qD is an enumeration template", name
);
17714 /* ignore a typename reference, for it will be solved by name
17719 else if (nested_name_specifier
== error_mark_node
)
17720 /* We already issued an error. */;
17723 error_at (type_start_token
->location
,
17724 "%qD does not name an enumeration in %qT",
17725 identifier
, nested_name_specifier
);
17726 nested_name_specifier
= error_mark_node
;
17731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17732 identifier
= cp_parser_identifier (parser
);
17735 identifier
= make_anon_name ();
17738 error_at (type_start_token
->location
,
17739 "unnamed scoped enum is not allowed");
17742 pop_deferring_access_checks ();
17744 /* Check for the `:' that denotes a specified underlying type in C++0x.
17745 Note that a ':' could also indicate a bitfield width, however. */
17746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17748 cp_decl_specifier_seq type_specifiers
;
17750 /* Consume the `:'. */
17751 cp_lexer_consume_token (parser
->lexer
);
17753 /* Parse the type-specifier-seq. */
17754 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17755 /*is_trailing_return=*/false,
17758 /* At this point this is surely not elaborated type specifier. */
17759 if (!cp_parser_parse_definitely (parser
))
17762 if (cxx_dialect
< cxx11
)
17763 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17765 has_underlying_type
= true;
17767 /* If that didn't work, stop. */
17768 if (type_specifiers
.type
!= error_mark_node
)
17770 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
17771 /*initialized=*/0, NULL
);
17772 if (underlying_type
== error_mark_node
17773 || check_for_bare_parameter_packs (underlying_type
))
17774 underlying_type
= NULL_TREE
;
17778 /* Look for the `{' but don't consume it yet. */
17779 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17781 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
17783 cp_parser_error (parser
, "expected %<{%>");
17784 if (has_underlying_type
)
17790 /* An opaque-enum-specifier must have a ';' here. */
17791 if ((scoped_enum_p
|| underlying_type
)
17792 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17794 cp_parser_error (parser
, "expected %<;%> or %<{%>");
17795 if (has_underlying_type
)
17803 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
17806 if (nested_name_specifier
)
17808 if (CLASS_TYPE_P (nested_name_specifier
))
17810 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
17811 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
17812 push_scope (nested_name_specifier
);
17814 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17816 push_nested_namespace (nested_name_specifier
);
17820 /* Issue an error message if type-definitions are forbidden here. */
17821 if (!cp_parser_check_type_definition (parser
))
17822 type
= error_mark_node
;
17824 /* Create the new type. We do this before consuming the opening
17825 brace so the enum will be recorded as being on the line of its
17826 tag (or the 'enum' keyword, if there is no tag). */
17827 type
= start_enum (identifier
, type
, underlying_type
,
17828 attributes
, scoped_enum_p
, &is_new_type
);
17830 /* If the next token is not '{' it is an opaque-enum-specifier or an
17831 elaborated-type-specifier. */
17832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17834 timevar_push (TV_PARSE_ENUM
);
17835 if (nested_name_specifier
17836 && nested_name_specifier
!= error_mark_node
)
17838 /* The following catches invalid code such as:
17839 enum class S<int>::E { A, B, C }; */
17840 if (!processing_specialization
17841 && CLASS_TYPE_P (nested_name_specifier
)
17842 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
17843 error_at (type_start_token
->location
, "cannot add an enumerator "
17844 "list to a template instantiation");
17846 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
17848 error_at (type_start_token
->location
,
17849 "%<%T::%E%> has not been declared",
17850 TYPE_CONTEXT (nested_name_specifier
),
17851 nested_name_specifier
);
17852 type
= error_mark_node
;
17854 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
17855 && !CLASS_TYPE_P (nested_name_specifier
))
17857 error_at (type_start_token
->location
, "nested name specifier "
17858 "%qT for enum declaration does not name a class "
17859 "or namespace", nested_name_specifier
);
17860 type
= error_mark_node
;
17862 /* If that scope does not contain the scope in which the
17863 class was originally declared, the program is invalid. */
17864 else if (prev_scope
&& !is_ancestor (prev_scope
,
17865 nested_name_specifier
))
17867 if (at_namespace_scope_p ())
17868 error_at (type_start_token
->location
,
17869 "declaration of %qD in namespace %qD which does not "
17871 type
, prev_scope
, nested_name_specifier
);
17873 error_at (type_start_token
->location
,
17874 "declaration of %qD in %qD which does not "
17876 type
, prev_scope
, nested_name_specifier
);
17877 type
= error_mark_node
;
17879 /* If that scope is the scope where the declaration is being placed
17880 the program is invalid. */
17881 else if (CLASS_TYPE_P (nested_name_specifier
)
17882 && CLASS_TYPE_P (prev_scope
)
17883 && same_type_p (nested_name_specifier
, prev_scope
))
17885 permerror (type_start_token
->location
,
17886 "extra qualification not allowed");
17887 nested_name_specifier
= NULL_TREE
;
17892 begin_scope (sk_scoped_enum
, type
);
17894 /* Consume the opening brace. */
17895 cp_lexer_consume_token (parser
->lexer
);
17897 if (type
== error_mark_node
)
17898 ; /* Nothing to add */
17899 else if (OPAQUE_ENUM_P (type
)
17900 || (cxx_dialect
> cxx98
&& processing_specialization
))
17902 new_value_list
= true;
17903 SET_OPAQUE_ENUM_P (type
, false);
17904 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17908 error_at (type_start_token
->location
,
17909 "multiple definition of %q#T", type
);
17910 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
17911 "previous definition here");
17912 type
= error_mark_node
;
17915 if (type
== error_mark_node
)
17916 cp_parser_skip_to_end_of_block_or_statement (parser
);
17917 /* If the next token is not '}', then there are some enumerators. */
17918 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17920 if (is_unnamed
&& !scoped_enum_p
)
17921 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17922 "ISO C++ forbids empty unnamed enum");
17925 cp_parser_enumerator_list (parser
, type
);
17927 /* Consume the final '}'. */
17928 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17932 timevar_pop (TV_PARSE_ENUM
);
17936 /* If a ';' follows, then it is an opaque-enum-specifier
17937 and additional restrictions apply. */
17938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17941 error_at (type_start_token
->location
,
17942 "opaque-enum-specifier without name");
17943 else if (nested_name_specifier
)
17944 error_at (type_start_token
->location
,
17945 "opaque-enum-specifier must use a simple identifier");
17949 /* Look for trailing attributes to apply to this enumeration, and
17950 apply them if appropriate. */
17951 if (cp_parser_allow_gnu_extensions_p (parser
))
17953 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
17954 cplus_decl_attributes (&type
,
17956 (int) ATTR_FLAG_TYPE_IN_PLACE
);
17959 /* Finish up the enumeration. */
17960 if (type
!= error_mark_node
)
17962 if (new_value_list
)
17963 finish_enum_value_list (type
);
17965 finish_enum (type
);
17968 if (nested_name_specifier
)
17970 if (CLASS_TYPE_P (nested_name_specifier
))
17972 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
17973 pop_scope (nested_name_specifier
);
17975 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17977 pop_nested_namespace (nested_name_specifier
);
17981 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17985 /* Parse an enumerator-list. The enumerators all have the indicated
17989 enumerator-definition
17990 enumerator-list , enumerator-definition */
17993 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
17997 /* Parse an enumerator-definition. */
17998 cp_parser_enumerator_definition (parser
, type
);
18000 /* If the next token is not a ',', we've reached the end of
18002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18004 /* Otherwise, consume the `,' and keep going. */
18005 cp_lexer_consume_token (parser
->lexer
);
18006 /* If the next token is a `}', there is a trailing comma. */
18007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18009 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18010 pedwarn (input_location
, OPT_Wpedantic
,
18011 "comma at end of enumerator list");
18017 /* Parse an enumerator-definition. The enumerator has the indicated
18020 enumerator-definition:
18022 enumerator = constant-expression
18029 enumerator-definition:
18030 enumerator attributes [opt]
18031 enumerator attributes [opt] = constant-expression */
18034 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18040 /* Save the input location because we are interested in the location
18041 of the identifier and not the location of the explicit value. */
18042 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18044 /* Look for the identifier. */
18045 identifier
= cp_parser_identifier (parser
);
18046 if (identifier
== error_mark_node
)
18049 /* Parse any specified attributes. */
18050 tree attrs
= cp_parser_attributes_opt (parser
);
18052 /* If the next token is an '=', then there is an explicit value. */
18053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18055 /* Consume the `=' token. */
18056 cp_lexer_consume_token (parser
->lexer
);
18057 /* Parse the value. */
18058 value
= cp_parser_constant_expression (parser
);
18063 /* If we are processing a template, make sure the initializer of the
18064 enumerator doesn't contain any bare template parameter pack. */
18065 if (check_for_bare_parameter_packs (value
))
18066 value
= error_mark_node
;
18068 /* Create the enumerator. */
18069 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18072 /* Parse a namespace-name.
18075 original-namespace-name
18078 Returns the NAMESPACE_DECL for the namespace. */
18081 cp_parser_namespace_name (cp_parser
* parser
)
18084 tree namespace_decl
;
18086 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18088 /* Get the name of the namespace. */
18089 identifier
= cp_parser_identifier (parser
);
18090 if (identifier
== error_mark_node
)
18091 return error_mark_node
;
18093 /* Look up the identifier in the currently active scope. Look only
18094 for namespaces, due to:
18096 [basic.lookup.udir]
18098 When looking up a namespace-name in a using-directive or alias
18099 definition, only namespace names are considered.
18103 [basic.lookup.qual]
18105 During the lookup of a name preceding the :: scope resolution
18106 operator, object, function, and enumerator names are ignored.
18108 (Note that cp_parser_qualifying_entity only calls this
18109 function if the token after the name is the scope resolution
18111 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18113 /*is_template=*/false,
18114 /*is_namespace=*/true,
18115 /*check_dependency=*/true,
18116 /*ambiguous_decls=*/NULL
,
18118 /* If it's not a namespace, issue an error. */
18119 if (namespace_decl
== error_mark_node
18120 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18122 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18123 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18124 cp_parser_error (parser
, "expected namespace-name");
18125 namespace_decl
= error_mark_node
;
18128 return namespace_decl
;
18131 /* Parse a namespace-definition.
18133 namespace-definition:
18134 named-namespace-definition
18135 unnamed-namespace-definition
18137 named-namespace-definition:
18138 original-namespace-definition
18139 extension-namespace-definition
18141 original-namespace-definition:
18142 namespace identifier { namespace-body }
18144 extension-namespace-definition:
18145 namespace original-namespace-name { namespace-body }
18147 unnamed-namespace-definition:
18148 namespace { namespace-body } */
18151 cp_parser_namespace_definition (cp_parser
* parser
)
18153 tree identifier
, attribs
;
18154 bool has_visibility
;
18157 int nested_definition_count
= 0;
18159 cp_ensure_no_omp_declare_simd (parser
);
18160 cp_ensure_no_oacc_routine (parser
);
18161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
18163 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18165 cp_lexer_consume_token (parser
->lexer
);
18170 /* Look for the `namespace' keyword. */
18171 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18173 /* Parse any specified attributes before the identifier. */
18174 attribs
= cp_parser_attributes_opt (parser
);
18176 /* Get the name of the namespace. We do not attempt to distinguish
18177 between an original-namespace-definition and an
18178 extension-namespace-definition at this point. The semantic
18179 analysis routines are responsible for that. */
18180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18181 identifier
= cp_parser_identifier (parser
);
18183 identifier
= NULL_TREE
;
18185 /* Parse any specified attributes after the identifier. */
18186 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
18187 if (post_ident_attribs
)
18190 attribs
= chainon (attribs
, post_ident_attribs
);
18192 attribs
= post_ident_attribs
;
18195 /* Start the namespace. */
18196 bool ok
= push_namespace (identifier
);
18198 /* Parse any nested namespace definition. */
18199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18202 error_at (token
->location
, "a nested namespace definition cannot have attributes");
18203 if (cxx_dialect
< cxx1z
)
18204 pedwarn (input_location
, OPT_Wpedantic
,
18205 "nested namespace definitions only available with "
18206 "-std=c++1z or -std=gnu++1z");
18208 error_at (token
->location
, "a nested namespace definition cannot be inline");
18209 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18211 cp_lexer_consume_token (parser
->lexer
);
18212 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18213 identifier
= cp_parser_identifier (parser
);
18216 cp_parser_error (parser
, "nested identifier required");
18219 if (push_namespace (identifier
))
18220 ++nested_definition_count
;
18224 /* Look for the `{' to validate starting the namespace. */
18225 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
18227 /* "inline namespace" is equivalent to a stub namespace definition
18228 followed by a strong using directive. */
18229 if (is_inline
&& ok
)
18231 tree name_space
= current_namespace
;
18232 /* Set up namespace association. */
18233 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
18234 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
18235 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
18236 /* Import the contents of the inline namespace. */
18238 do_using_directive (name_space
);
18239 push_namespace (identifier
);
18242 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18244 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18246 /* Parse the body of the namespace. */
18247 cp_parser_namespace_body (parser
);
18249 if (has_visibility
)
18250 pop_visibility (1);
18252 /* Finish the nested namespace definitions. */
18253 while (nested_definition_count
--)
18256 /* Finish the namespace. */
18259 /* Look for the final `}'. */
18260 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18263 /* Parse a namespace-body.
18266 declaration-seq [opt] */
18269 cp_parser_namespace_body (cp_parser
* parser
)
18271 cp_parser_declaration_seq_opt (parser
);
18274 /* Parse a namespace-alias-definition.
18276 namespace-alias-definition:
18277 namespace identifier = qualified-namespace-specifier ; */
18280 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18283 tree namespace_specifier
;
18285 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18287 /* Look for the `namespace' keyword. */
18288 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18289 /* Look for the identifier. */
18290 identifier
= cp_parser_identifier (parser
);
18291 if (identifier
== error_mark_node
)
18293 /* Look for the `=' token. */
18294 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18295 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18297 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18298 /* Skip the definition. */
18299 cp_lexer_consume_token (parser
->lexer
);
18300 if (cp_parser_skip_to_closing_brace (parser
))
18301 cp_lexer_consume_token (parser
->lexer
);
18304 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18305 /* Look for the qualified-namespace-specifier. */
18306 namespace_specifier
18307 = cp_parser_qualified_namespace_specifier (parser
);
18308 /* Look for the `;' token. */
18309 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18311 /* Register the alias in the symbol table. */
18312 do_namespace_alias (identifier
, namespace_specifier
);
18315 /* Parse a qualified-namespace-specifier.
18317 qualified-namespace-specifier:
18318 :: [opt] nested-name-specifier [opt] namespace-name
18320 Returns a NAMESPACE_DECL corresponding to the specified
18324 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18326 /* Look for the optional `::'. */
18327 cp_parser_global_scope_opt (parser
,
18328 /*current_scope_valid_p=*/false);
18330 /* Look for the optional nested-name-specifier. */
18331 cp_parser_nested_name_specifier_opt (parser
,
18332 /*typename_keyword_p=*/false,
18333 /*check_dependency_p=*/true,
18335 /*is_declaration=*/true);
18337 return cp_parser_namespace_name (parser
);
18340 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18341 access declaration.
18344 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18345 using :: unqualified-id ;
18347 access-declaration:
18353 cp_parser_using_declaration (cp_parser
* parser
,
18354 bool access_declaration_p
)
18357 bool typename_p
= false;
18358 bool global_scope_p
;
18362 int oldcount
= errorcount
;
18363 cp_token
*diag_token
= NULL
;
18365 if (access_declaration_p
)
18367 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18368 cp_parser_parse_tentatively (parser
);
18372 /* Look for the `using' keyword. */
18373 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18376 /* Peek at the next token. */
18377 token
= cp_lexer_peek_token (parser
->lexer
);
18378 /* See if it's `typename'. */
18379 if (token
->keyword
== RID_TYPENAME
)
18381 /* Remember that we've seen it. */
18383 /* Consume the `typename' token. */
18384 cp_lexer_consume_token (parser
->lexer
);
18388 /* Look for the optional global scope qualification. */
18390 = (cp_parser_global_scope_opt (parser
,
18391 /*current_scope_valid_p=*/false)
18394 /* If we saw `typename', or didn't see `::', then there must be a
18395 nested-name-specifier present. */
18396 if (typename_p
|| !global_scope_p
)
18398 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18399 /*check_dependency_p=*/true,
18401 /*is_declaration=*/true);
18402 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18404 cp_parser_skip_to_end_of_block_or_statement (parser
);
18408 /* Otherwise, we could be in either of the two productions. In that
18409 case, treat the nested-name-specifier as optional. */
18411 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18412 /*typename_keyword_p=*/false,
18413 /*check_dependency_p=*/true,
18415 /*is_declaration=*/true);
18417 qscope
= global_namespace
;
18418 else if (UNSCOPED_ENUM_P (qscope
))
18419 qscope
= CP_TYPE_CONTEXT (qscope
);
18421 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18422 /* Something has already gone wrong; there's no need to parse
18423 further. Since an error has occurred, the return value of
18424 cp_parser_parse_definitely will be false, as required. */
18425 return cp_parser_parse_definitely (parser
);
18427 token
= cp_lexer_peek_token (parser
->lexer
);
18428 /* Parse the unqualified-id. */
18429 identifier
= cp_parser_unqualified_id (parser
,
18430 /*template_keyword_p=*/false,
18431 /*check_dependency_p=*/true,
18432 /*declarator_p=*/true,
18433 /*optional_p=*/false);
18435 if (access_declaration_p
)
18437 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18438 cp_parser_simulate_error (parser
);
18439 if (!cp_parser_parse_definitely (parser
))
18442 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18444 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18445 if (cxx_dialect
< cxx1z
18446 && !in_system_header_at (ell
->location
))
18447 pedwarn (ell
->location
, 0,
18448 "pack expansion in using-declaration only available "
18449 "with -std=c++1z or -std=gnu++1z");
18450 qscope
= make_pack_expansion (qscope
);
18453 /* The function we call to handle a using-declaration is different
18454 depending on what scope we are in. */
18455 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18457 else if (!identifier_p (identifier
)
18458 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18459 /* [namespace.udecl]
18461 A using declaration shall not name a template-id. */
18462 error_at (token
->location
,
18463 "a template-id may not appear in a using-declaration");
18466 if (at_class_scope_p ())
18468 /* Create the USING_DECL. */
18469 decl
= do_class_using_decl (qscope
, identifier
);
18471 if (decl
&& typename_p
)
18472 USING_DECL_TYPENAME_P (decl
) = 1;
18474 if (check_for_bare_parameter_packs (decl
))
18476 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18480 /* Add it to the list of members in this class. */
18481 finish_member_declaration (decl
);
18485 decl
= cp_parser_lookup_name_simple (parser
,
18488 if (decl
== error_mark_node
)
18489 cp_parser_name_lookup_error (parser
, identifier
,
18492 else if (check_for_bare_parameter_packs (decl
))
18494 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18497 else if (!at_namespace_scope_p ())
18498 do_local_using_decl (decl
, qscope
, identifier
);
18500 do_toplevel_using_decl (decl
, qscope
, identifier
);
18504 if (!access_declaration_p
18505 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18507 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18508 if (cxx_dialect
< cxx1z
)
18509 pedwarn (comma
->location
, 0,
18510 "comma-separated list in using-declaration only available "
18511 "with -std=c++1z or -std=gnu++1z");
18515 /* Look for the final `;'. */
18516 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18518 if (access_declaration_p
&& errorcount
== oldcount
)
18519 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18520 "access declarations are deprecated "
18521 "in favour of using-declarations; "
18522 "suggestion: add the %<using%> keyword");
18527 /* Parse an alias-declaration.
18530 using identifier attribute-specifier-seq [opt] = type-id */
18533 cp_parser_alias_declaration (cp_parser
* parser
)
18535 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18536 location_t id_location
;
18537 cp_declarator
*declarator
;
18538 cp_decl_specifier_seq decl_specs
;
18540 const char *saved_message
= NULL
;
18542 /* Look for the `using' keyword. */
18543 cp_token
*using_token
18544 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18545 if (using_token
== NULL
)
18546 return error_mark_node
;
18548 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18549 id
= cp_parser_identifier (parser
);
18550 if (id
== error_mark_node
)
18551 return error_mark_node
;
18553 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18554 attributes
= cp_parser_attributes_opt (parser
);
18555 if (attributes
== error_mark_node
)
18556 return error_mark_node
;
18558 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18560 if (cp_parser_error_occurred (parser
))
18561 return error_mark_node
;
18563 cp_parser_commit_to_tentative_parse (parser
);
18565 /* Now we are going to parse the type-id of the declaration. */
18570 "A type-specifier-seq shall not define a class or enumeration
18571 unless it appears in the type-id of an alias-declaration (7.1.3) that
18572 is not the declaration of a template-declaration."
18574 In other words, if we currently are in an alias template, the
18575 type-id should not define a type.
18577 So let's set parser->type_definition_forbidden_message in that
18578 case; cp_parser_check_type_definition (called by
18579 cp_parser_class_specifier) will then emit an error if a type is
18580 defined in the type-id. */
18581 if (parser
->num_template_parameter_lists
)
18583 saved_message
= parser
->type_definition_forbidden_message
;
18584 parser
->type_definition_forbidden_message
=
18585 G_("types may not be defined in alias template declarations");
18588 type
= cp_parser_type_id (parser
);
18590 /* Restore the error message if need be. */
18591 if (parser
->num_template_parameter_lists
)
18592 parser
->type_definition_forbidden_message
= saved_message
;
18594 if (type
== error_mark_node
18595 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18597 cp_parser_skip_to_end_of_block_or_statement (parser
);
18598 return error_mark_node
;
18601 /* A typedef-name can also be introduced by an alias-declaration. The
18602 identifier following the using keyword becomes a typedef-name. It has
18603 the same semantics as if it were introduced by the typedef
18604 specifier. In particular, it does not define a new type and it shall
18605 not appear in the type-id. */
18607 clear_decl_specs (&decl_specs
);
18608 decl_specs
.type
= type
;
18609 if (attributes
!= NULL_TREE
)
18611 decl_specs
.attributes
= attributes
;
18612 set_and_check_decl_spec_loc (&decl_specs
,
18616 set_and_check_decl_spec_loc (&decl_specs
,
18619 set_and_check_decl_spec_loc (&decl_specs
,
18623 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18624 declarator
->id_loc
= id_location
;
18626 member_p
= at_class_scope_p ();
18628 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18629 NULL_TREE
, attributes
);
18631 decl
= start_decl (declarator
, &decl_specs
, 0,
18632 attributes
, NULL_TREE
, &pushed_scope
);
18633 if (decl
== error_mark_node
)
18636 // Attach constraints to the alias declaration.
18637 if (flag_concepts
&& current_template_parms
)
18639 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18640 tree constr
= build_constraints (reqs
, NULL_TREE
);
18641 set_constraints (decl
, constr
);
18644 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18647 pop_scope (pushed_scope
);
18649 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18650 added into the symbol table; otherwise, return the TYPE_DECL. */
18651 if (DECL_LANG_SPECIFIC (decl
)
18652 && DECL_TEMPLATE_INFO (decl
)
18653 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18655 decl
= DECL_TI_TEMPLATE (decl
);
18657 check_member_template (decl
);
18663 /* Parse a using-directive.
18666 using namespace :: [opt] nested-name-specifier [opt]
18667 namespace-name ; */
18670 cp_parser_using_directive (cp_parser
* parser
)
18672 tree namespace_decl
;
18675 /* Look for the `using' keyword. */
18676 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18677 /* And the `namespace' keyword. */
18678 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18679 /* Look for the optional `::' operator. */
18680 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18681 /* And the optional nested-name-specifier. */
18682 cp_parser_nested_name_specifier_opt (parser
,
18683 /*typename_keyword_p=*/false,
18684 /*check_dependency_p=*/true,
18686 /*is_declaration=*/true);
18687 /* Get the namespace being used. */
18688 namespace_decl
= cp_parser_namespace_name (parser
);
18689 /* And any specified attributes. */
18690 attribs
= cp_parser_attributes_opt (parser
);
18691 /* Update the symbol table. */
18692 parse_using_directive (namespace_decl
, attribs
);
18693 /* Look for the final `;'. */
18694 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18697 /* Parse an asm-definition.
18700 asm ( string-literal ) ;
18705 asm volatile [opt] ( string-literal ) ;
18706 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18707 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18708 : asm-operand-list [opt] ) ;
18709 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18710 : asm-operand-list [opt]
18711 : asm-clobber-list [opt] ) ;
18712 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18713 : asm-clobber-list [opt]
18714 : asm-goto-list ) ; */
18717 cp_parser_asm_definition (cp_parser
* parser
)
18720 tree outputs
= NULL_TREE
;
18721 tree inputs
= NULL_TREE
;
18722 tree clobbers
= NULL_TREE
;
18723 tree labels
= NULL_TREE
;
18725 bool volatile_p
= false;
18726 bool extended_p
= false;
18727 bool invalid_inputs_p
= false;
18728 bool invalid_outputs_p
= false;
18729 bool goto_p
= false;
18730 required_token missing
= RT_NONE
;
18732 /* Look for the `asm' keyword. */
18733 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
18735 if (parser
->in_function_body
18736 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
18738 error ("%<asm%> in %<constexpr%> function");
18739 cp_function_chain
->invalid_constexpr
= true;
18742 /* See if the next token is `volatile'. */
18743 if (cp_parser_allow_gnu_extensions_p (parser
)
18744 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
18746 /* Remember that we saw the `volatile' keyword. */
18748 /* Consume the token. */
18749 cp_lexer_consume_token (parser
->lexer
);
18751 if (cp_parser_allow_gnu_extensions_p (parser
)
18752 && parser
->in_function_body
18753 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
18755 /* Remember that we saw the `goto' keyword. */
18757 /* Consume the token. */
18758 cp_lexer_consume_token (parser
->lexer
);
18760 /* Look for the opening `('. */
18761 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
18763 /* Look for the string. */
18764 string
= cp_parser_string_literal (parser
, false, false);
18765 if (string
== error_mark_node
)
18767 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18768 /*consume_paren=*/true);
18772 /* If we're allowing GNU extensions, check for the extended assembly
18773 syntax. Unfortunately, the `:' tokens need not be separated by
18774 a space in C, and so, for compatibility, we tolerate that here
18775 too. Doing that means that we have to treat the `::' operator as
18777 if (cp_parser_allow_gnu_extensions_p (parser
)
18778 && parser
->in_function_body
18779 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
18780 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
18782 bool inputs_p
= false;
18783 bool clobbers_p
= false;
18784 bool labels_p
= false;
18786 /* The extended syntax was used. */
18789 /* Look for outputs. */
18790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18792 /* Consume the `:'. */
18793 cp_lexer_consume_token (parser
->lexer
);
18794 /* Parse the output-operands. */
18795 if (cp_lexer_next_token_is_not (parser
->lexer
,
18797 && cp_lexer_next_token_is_not (parser
->lexer
,
18799 && cp_lexer_next_token_is_not (parser
->lexer
,
18803 outputs
= cp_parser_asm_operand_list (parser
);
18804 if (outputs
== error_mark_node
)
18805 invalid_outputs_p
= true;
18808 /* If the next token is `::', there are no outputs, and the
18809 next token is the beginning of the inputs. */
18810 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18811 /* The inputs are coming next. */
18814 /* Look for inputs. */
18816 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18818 /* Consume the `:' or `::'. */
18819 cp_lexer_consume_token (parser
->lexer
);
18820 /* Parse the output-operands. */
18821 if (cp_lexer_next_token_is_not (parser
->lexer
,
18823 && cp_lexer_next_token_is_not (parser
->lexer
,
18825 && cp_lexer_next_token_is_not (parser
->lexer
,
18828 inputs
= cp_parser_asm_operand_list (parser
);
18829 if (inputs
== error_mark_node
)
18830 invalid_inputs_p
= true;
18833 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18834 /* The clobbers are coming next. */
18837 /* Look for clobbers. */
18839 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18842 /* Consume the `:' or `::'. */
18843 cp_lexer_consume_token (parser
->lexer
);
18844 /* Parse the clobbers. */
18845 if (cp_lexer_next_token_is_not (parser
->lexer
,
18847 && cp_lexer_next_token_is_not (parser
->lexer
,
18849 clobbers
= cp_parser_asm_clobber_list (parser
);
18852 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18853 /* The labels are coming next. */
18856 /* Look for labels. */
18858 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
18861 /* Consume the `:' or `::'. */
18862 cp_lexer_consume_token (parser
->lexer
);
18863 /* Parse the labels. */
18864 labels
= cp_parser_asm_label_list (parser
);
18867 if (goto_p
&& !labels_p
)
18868 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
18871 missing
= RT_COLON_SCOPE
;
18873 /* Look for the closing `)'. */
18874 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
18875 missing
? missing
: RT_CLOSE_PAREN
))
18876 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18877 /*consume_paren=*/true);
18878 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18880 if (!invalid_inputs_p
&& !invalid_outputs_p
)
18882 /* Create the ASM_EXPR. */
18883 if (parser
->in_function_body
)
18885 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
18886 inputs
, clobbers
, labels
);
18887 /* If the extended syntax was not used, mark the ASM_EXPR. */
18890 tree temp
= asm_stmt
;
18891 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
18892 temp
= TREE_OPERAND (temp
, 0);
18894 ASM_INPUT_P (temp
) = 1;
18898 symtab
->finalize_toplevel_asm (string
);
18902 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18903 type that comes from the decl-specifier-seq. */
18906 strip_declarator_types (tree type
, cp_declarator
*declarator
)
18908 for (cp_declarator
*d
= declarator
; d
;)
18918 if (TYPE_PTRMEMFUNC_P (type
))
18919 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
18920 type
= TREE_TYPE (type
);
18928 /* Declarators [gram.dcl.decl] */
18930 /* Parse an init-declarator.
18933 declarator initializer [opt]
18938 declarator asm-specification [opt] attributes [opt] initializer [opt]
18940 function-definition:
18941 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18943 decl-specifier-seq [opt] declarator function-try-block
18947 function-definition:
18948 __extension__ function-definition
18952 function-definition:
18953 decl-specifier-seq [opt] declarator function-transaction-block
18955 The DECL_SPECIFIERS apply to this declarator. Returns a
18956 representation of the entity declared. If MEMBER_P is TRUE, then
18957 this declarator appears in a class scope. The new DECL created by
18958 this declarator is returned.
18960 The CHECKS are access checks that should be performed once we know
18961 what entity is being declared (and, therefore, what classes have
18964 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18965 for a function-definition here as well. If the declarator is a
18966 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18967 be TRUE upon return. By that point, the function-definition will
18968 have been completely parsed.
18970 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18973 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18974 parsed declaration if it is an uninitialized single declarator not followed
18975 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18976 if present, will not be consumed. If returned, this declarator will be
18977 created with SD_INITIALIZED but will not call cp_finish_decl.
18979 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18980 and there is an initializer, the pointed location_t is set to the
18981 location of the '=' or `(', or '{' in C++11 token introducing the
18985 cp_parser_init_declarator (cp_parser
* parser
,
18986 cp_decl_specifier_seq
*decl_specifiers
,
18987 vec
<deferred_access_check
, va_gc
> *checks
,
18988 bool function_definition_allowed_p
,
18990 int declares_class_or_enum
,
18991 bool* function_definition_p
,
18992 tree
* maybe_range_for_decl
,
18993 location_t
* init_loc
,
18996 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
18997 *attributes_start_token
= NULL
;
18998 cp_declarator
*declarator
;
18999 tree prefix_attributes
;
19000 tree attributes
= NULL
;
19001 tree asm_specification
;
19003 tree decl
= NULL_TREE
;
19005 int is_initialized
;
19006 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19007 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19009 enum cpp_ttype initialization_kind
;
19010 bool is_direct_init
= false;
19011 bool is_non_constant_init
;
19012 int ctor_dtor_or_conv_p
;
19013 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19014 tree pushed_scope
= NULL_TREE
;
19015 bool range_for_decl_p
= false;
19016 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19017 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19019 /* Gather the attributes that were provided with the
19020 decl-specifiers. */
19021 prefix_attributes
= decl_specifiers
->attributes
;
19023 /* Assume that this is not the declarator for a function
19025 if (function_definition_p
)
19026 *function_definition_p
= false;
19028 /* Default arguments are only permitted for function parameters. */
19029 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19030 parser
->default_arg_ok_p
= false;
19032 /* Defer access checks while parsing the declarator; we cannot know
19033 what names are accessible until we know what is being
19035 resume_deferring_access_checks ();
19037 token
= cp_lexer_peek_token (parser
->lexer
);
19039 /* Parse the declarator. */
19041 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19042 &ctor_dtor_or_conv_p
,
19043 /*parenthesized_p=*/NULL
,
19044 member_p
, friend_p
);
19045 /* Gather up the deferred checks. */
19046 stop_deferring_access_checks ();
19048 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19050 /* If the DECLARATOR was erroneous, there's no need to go
19052 if (declarator
== cp_error_declarator
)
19053 return error_mark_node
;
19055 /* Check that the number of template-parameter-lists is OK. */
19056 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19058 return error_mark_node
;
19060 if (declares_class_or_enum
& 2)
19061 cp_parser_check_for_definition_in_return_type (declarator
,
19062 decl_specifiers
->type
,
19063 decl_specifiers
->locations
[ds_type_spec
]);
19065 /* Figure out what scope the entity declared by the DECLARATOR is
19066 located in. `grokdeclarator' sometimes changes the scope, so
19067 we compute it now. */
19068 scope
= get_scope_of_declarator (declarator
);
19070 /* Perform any lookups in the declared type which were thought to be
19071 dependent, but are not in the scope of the declarator. */
19072 decl_specifiers
->type
19073 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19075 /* If we're allowing GNU extensions, look for an
19076 asm-specification. */
19077 if (cp_parser_allow_gnu_extensions_p (parser
))
19079 /* Look for an asm-specification. */
19080 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19081 asm_specification
= cp_parser_asm_specification_opt (parser
);
19084 asm_specification
= NULL_TREE
;
19086 /* Look for attributes. */
19087 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19088 attributes
= cp_parser_attributes_opt (parser
);
19090 /* Peek at the next token. */
19091 token
= cp_lexer_peek_token (parser
->lexer
);
19093 bool bogus_implicit_tmpl
= false;
19095 if (function_declarator_p (declarator
))
19097 /* Handle C++17 deduction guides. */
19098 if (!decl_specifiers
->type
19099 && ctor_dtor_or_conv_p
<= 0
19100 && cxx_dialect
>= cxx1z
)
19102 cp_declarator
*id
= get_id_declarator (declarator
);
19103 tree name
= id
->u
.id
.unqualified_name
;
19104 parser
->scope
= id
->u
.id
.qualifying_scope
;
19105 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19107 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19108 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19110 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19111 id
->u
.id
.sfk
= sfk_deduction_guide
;
19112 ctor_dtor_or_conv_p
= 1;
19116 /* Check to see if the token indicates the start of a
19117 function-definition. */
19118 if (cp_parser_token_starts_function_definition_p (token
))
19120 if (!function_definition_allowed_p
)
19122 /* If a function-definition should not appear here, issue an
19124 cp_parser_error (parser
,
19125 "a function-definition is not allowed here");
19126 return error_mark_node
;
19129 location_t func_brace_location
19130 = cp_lexer_peek_token (parser
->lexer
)->location
;
19132 /* Neither attributes nor an asm-specification are allowed
19133 on a function-definition. */
19134 if (asm_specification
)
19135 error_at (asm_spec_start_token
->location
,
19136 "an asm-specification is not allowed "
19137 "on a function-definition");
19139 error_at (attributes_start_token
->location
,
19140 "attributes are not allowed "
19141 "on a function-definition");
19142 /* This is a function-definition. */
19143 *function_definition_p
= true;
19145 /* Parse the function definition. */
19147 decl
= cp_parser_save_member_function_body (parser
,
19150 prefix_attributes
);
19153 (cp_parser_function_definition_from_specifiers_and_declarator
19154 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19156 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19158 /* This is where the prologue starts... */
19159 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19160 = func_brace_location
;
19166 else if (parser
->fully_implicit_function_template_p
)
19168 /* A non-template declaration involving a function parameter list
19169 containing an implicit template parameter will be made into a
19170 template. If the resulting declaration is not going to be an
19171 actual function then finish the template scope here to prevent it.
19172 An error message will be issued once we have a decl to talk about.
19174 FIXME probably we should do type deduction rather than create an
19175 implicit template, but the standard currently doesn't allow it. */
19176 bogus_implicit_tmpl
= true;
19177 finish_fully_implicit_template (parser
, NULL_TREE
);
19182 Only in function declarations for constructors, destructors, type
19183 conversions, and deduction guides can the decl-specifier-seq be omitted.
19185 We explicitly postpone this check past the point where we handle
19186 function-definitions because we tolerate function-definitions
19187 that are missing their return types in some modes. */
19188 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19190 cp_parser_error (parser
,
19191 "expected constructor, destructor, or type conversion");
19192 return error_mark_node
;
19195 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19196 if (token
->type
== CPP_EQ
19197 || token
->type
== CPP_OPEN_PAREN
19198 || token
->type
== CPP_OPEN_BRACE
)
19200 is_initialized
= SD_INITIALIZED
;
19201 initialization_kind
= token
->type
;
19202 if (maybe_range_for_decl
)
19203 *maybe_range_for_decl
= error_mark_node
;
19204 tmp_init_loc
= token
->location
;
19205 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19206 *init_loc
= tmp_init_loc
;
19208 if (token
->type
== CPP_EQ
19209 && function_declarator_p (declarator
))
19211 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19212 if (t2
->keyword
== RID_DEFAULT
)
19213 is_initialized
= SD_DEFAULTED
;
19214 else if (t2
->keyword
== RID_DELETE
)
19215 is_initialized
= SD_DELETED
;
19220 /* If the init-declarator isn't initialized and isn't followed by a
19221 `,' or `;', it's not a valid init-declarator. */
19222 if (token
->type
!= CPP_COMMA
19223 && token
->type
!= CPP_SEMICOLON
)
19225 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19226 range_for_decl_p
= true;
19229 if (!maybe_range_for_decl
)
19230 cp_parser_error (parser
, "expected initializer");
19231 return error_mark_node
;
19234 is_initialized
= SD_UNINITIALIZED
;
19235 initialization_kind
= CPP_EOF
;
19238 /* Because start_decl has side-effects, we should only call it if we
19239 know we're going ahead. By this point, we know that we cannot
19240 possibly be looking at any other construct. */
19241 cp_parser_commit_to_tentative_parse (parser
);
19243 /* Enter the newly declared entry in the symbol table. If we're
19244 processing a declaration in a class-specifier, we wait until
19245 after processing the initializer. */
19248 if (parser
->in_unbraced_linkage_specification_p
)
19249 decl_specifiers
->storage_class
= sc_extern
;
19250 decl
= start_decl (declarator
, decl_specifiers
,
19251 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19252 attributes
, prefix_attributes
, &pushed_scope
);
19253 cp_finalize_omp_declare_simd (parser
, decl
);
19254 cp_finalize_oacc_routine (parser
, decl
, false);
19255 /* Adjust location of decl if declarator->id_loc is more appropriate:
19256 set, and decl wasn't merged with another decl, in which case its
19257 location would be different from input_location, and more accurate. */
19259 && declarator
->id_loc
!= UNKNOWN_LOCATION
19260 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19261 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19264 /* Enter the SCOPE. That way unqualified names appearing in the
19265 initializer will be looked up in SCOPE. */
19266 pushed_scope
= push_scope (scope
);
19268 /* Perform deferred access control checks, now that we know in which
19269 SCOPE the declared entity resides. */
19270 if (!member_p
&& decl
)
19272 tree saved_current_function_decl
= NULL_TREE
;
19274 /* If the entity being declared is a function, pretend that we
19275 are in its scope. If it is a `friend', it may have access to
19276 things that would not otherwise be accessible. */
19277 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19279 saved_current_function_decl
= current_function_decl
;
19280 current_function_decl
= decl
;
19283 /* Perform access checks for template parameters. */
19284 cp_parser_perform_template_parameter_access_checks (checks
);
19286 /* Perform the access control checks for the declarator and the
19287 decl-specifiers. */
19288 perform_deferred_access_checks (tf_warning_or_error
);
19290 /* Restore the saved value. */
19291 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19292 current_function_decl
= saved_current_function_decl
;
19295 /* Parse the initializer. */
19296 initializer
= NULL_TREE
;
19297 is_direct_init
= false;
19298 is_non_constant_init
= true;
19299 if (is_initialized
)
19301 if (function_declarator_p (declarator
))
19303 if (initialization_kind
== CPP_EQ
)
19304 initializer
= cp_parser_pure_specifier (parser
);
19307 /* If the declaration was erroneous, we don't really
19308 know what the user intended, so just silently
19309 consume the initializer. */
19310 if (decl
!= error_mark_node
)
19311 error_at (tmp_init_loc
, "initializer provided for function");
19312 cp_parser_skip_to_closing_parenthesis (parser
,
19313 /*recovering=*/true,
19314 /*or_comma=*/false,
19315 /*consume_paren=*/true);
19320 /* We want to record the extra mangling scope for in-class
19321 initializers of class members and initializers of static data
19322 member templates. The former involves deferring
19323 parsing of the initializer until end of class as with default
19324 arguments. So right here we only handle the latter. */
19325 if (!member_p
&& processing_template_decl
)
19326 start_lambda_scope (decl
);
19327 initializer
= cp_parser_initializer (parser
,
19329 &is_non_constant_init
);
19330 if (!member_p
&& processing_template_decl
)
19331 finish_lambda_scope ();
19332 if (initializer
== error_mark_node
)
19333 cp_parser_skip_to_end_of_statement (parser
);
19337 /* The old parser allows attributes to appear after a parenthesized
19338 initializer. Mark Mitchell proposed removing this functionality
19339 on the GCC mailing lists on 2002-08-13. This parser accepts the
19340 attributes -- but ignores them. */
19341 if (cp_parser_allow_gnu_extensions_p (parser
)
19342 && initialization_kind
== CPP_OPEN_PAREN
)
19343 if (cp_parser_attributes_opt (parser
))
19344 warning (OPT_Wattributes
,
19345 "attributes after parenthesized initializer ignored");
19347 /* And now complain about a non-function implicit template. */
19348 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19349 error_at (DECL_SOURCE_LOCATION (decl
),
19350 "non-function %qD declared as implicit template", decl
);
19352 /* For an in-class declaration, use `grokfield' to create the
19358 pop_scope (pushed_scope
);
19359 pushed_scope
= NULL_TREE
;
19361 decl
= grokfield (declarator
, decl_specifiers
,
19362 initializer
, !is_non_constant_init
,
19363 /*asmspec=*/NULL_TREE
,
19364 chainon (attributes
, prefix_attributes
));
19365 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19366 cp_parser_save_default_args (parser
, decl
);
19367 cp_finalize_omp_declare_simd (parser
, decl
);
19368 cp_finalize_oacc_routine (parser
, decl
, false);
19371 /* Finish processing the declaration. But, skip member
19373 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19375 cp_finish_decl (decl
,
19376 initializer
, !is_non_constant_init
,
19378 /* If the initializer is in parentheses, then this is
19379 a direct-initialization, which means that an
19380 `explicit' constructor is OK. Otherwise, an
19381 `explicit' constructor cannot be used. */
19382 ((is_direct_init
|| !is_initialized
)
19383 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19385 else if ((cxx_dialect
!= cxx98
) && friend_p
19386 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19387 /* Core issue #226 (C++0x only): A default template-argument
19388 shall not be specified in a friend class template
19390 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19391 /*is_partial=*/false, /*is_friend_decl=*/1);
19393 if (!friend_p
&& pushed_scope
)
19394 pop_scope (pushed_scope
);
19396 if (function_declarator_p (declarator
)
19397 && parser
->fully_implicit_function_template_p
)
19400 decl
= finish_fully_implicit_template (parser
, decl
);
19402 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19405 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19406 && type_uses_auto (decl_specifiers
->type
))
19407 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19412 /* Parse a declarator.
19416 ptr-operator declarator
19418 abstract-declarator:
19419 ptr-operator abstract-declarator [opt]
19420 direct-abstract-declarator
19425 attributes [opt] direct-declarator
19426 attributes [opt] ptr-operator declarator
19428 abstract-declarator:
19429 attributes [opt] ptr-operator abstract-declarator [opt]
19430 attributes [opt] direct-abstract-declarator
19432 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19433 detect constructors, destructors, deduction guides, or conversion operators.
19434 It is set to -1 if the declarator is a name, and +1 if it is a
19435 function. Otherwise it is set to zero. Usually you just want to
19436 test for >0, but internally the negative value is used.
19438 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19439 a decl-specifier-seq unless it declares a constructor, destructor,
19440 or conversion. It might seem that we could check this condition in
19441 semantic analysis, rather than parsing, but that makes it difficult
19442 to handle something like `f()'. We want to notice that there are
19443 no decl-specifiers, and therefore realize that this is an
19444 expression, not a declaration.)
19446 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19447 the declarator is a direct-declarator of the form "(...)".
19449 MEMBER_P is true iff this declarator is a member-declarator.
19451 FRIEND_P is true iff this declarator is a friend. */
19453 static cp_declarator
*
19454 cp_parser_declarator (cp_parser
* parser
,
19455 cp_parser_declarator_kind dcl_kind
,
19456 int* ctor_dtor_or_conv_p
,
19457 bool* parenthesized_p
,
19458 bool member_p
, bool friend_p
)
19460 cp_declarator
*declarator
;
19461 enum tree_code code
;
19462 cp_cv_quals cv_quals
;
19464 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19466 /* Assume this is not a constructor, destructor, or type-conversion
19468 if (ctor_dtor_or_conv_p
)
19469 *ctor_dtor_or_conv_p
= 0;
19471 if (cp_parser_allow_gnu_extensions_p (parser
))
19472 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19474 /* Check for the ptr-operator production. */
19475 cp_parser_parse_tentatively (parser
);
19476 /* Parse the ptr-operator. */
19477 code
= cp_parser_ptr_operator (parser
,
19482 /* If that worked, then we have a ptr-operator. */
19483 if (cp_parser_parse_definitely (parser
))
19485 /* If a ptr-operator was found, then this declarator was not
19487 if (parenthesized_p
)
19488 *parenthesized_p
= true;
19489 /* The dependent declarator is optional if we are parsing an
19490 abstract-declarator. */
19491 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19492 cp_parser_parse_tentatively (parser
);
19494 /* Parse the dependent declarator. */
19495 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19496 /*ctor_dtor_or_conv_p=*/NULL
,
19497 /*parenthesized_p=*/NULL
,
19498 /*member_p=*/false,
19501 /* If we are parsing an abstract-declarator, we must handle the
19502 case where the dependent declarator is absent. */
19503 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19504 && !cp_parser_parse_definitely (parser
))
19507 declarator
= cp_parser_make_indirect_declarator
19508 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19510 /* Everything else is a direct-declarator. */
19513 if (parenthesized_p
)
19514 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19516 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19517 ctor_dtor_or_conv_p
,
19518 member_p
, friend_p
);
19521 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19522 declarator
->attributes
= gnu_attributes
;
19526 /* Parse a direct-declarator or direct-abstract-declarator.
19530 direct-declarator ( parameter-declaration-clause )
19531 cv-qualifier-seq [opt]
19532 ref-qualifier [opt]
19533 exception-specification [opt]
19534 direct-declarator [ constant-expression [opt] ]
19537 direct-abstract-declarator:
19538 direct-abstract-declarator [opt]
19539 ( parameter-declaration-clause )
19540 cv-qualifier-seq [opt]
19541 ref-qualifier [opt]
19542 exception-specification [opt]
19543 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19544 ( abstract-declarator )
19546 Returns a representation of the declarator. DCL_KIND is
19547 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19548 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19549 we are parsing a direct-declarator. It is
19550 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19551 of ambiguity we prefer an abstract declarator, as per
19552 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19553 as for cp_parser_declarator. */
19555 static cp_declarator
*
19556 cp_parser_direct_declarator (cp_parser
* parser
,
19557 cp_parser_declarator_kind dcl_kind
,
19558 int* ctor_dtor_or_conv_p
,
19559 bool member_p
, bool friend_p
)
19562 cp_declarator
*declarator
= NULL
;
19563 tree scope
= NULL_TREE
;
19564 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19565 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19567 tree pushed_scope
= NULL_TREE
;
19571 /* Peek at the next token. */
19572 token
= cp_lexer_peek_token (parser
->lexer
);
19573 if (token
->type
== CPP_OPEN_PAREN
)
19575 /* This is either a parameter-declaration-clause, or a
19576 parenthesized declarator. When we know we are parsing a
19577 named declarator, it must be a parenthesized declarator
19578 if FIRST is true. For instance, `(int)' is a
19579 parameter-declaration-clause, with an omitted
19580 direct-abstract-declarator. But `((*))', is a
19581 parenthesized abstract declarator. Finally, when T is a
19582 template parameter `(T)' is a
19583 parameter-declaration-clause, and not a parenthesized
19586 We first try and parse a parameter-declaration-clause,
19587 and then try a nested declarator (if FIRST is true).
19589 It is not an error for it not to be a
19590 parameter-declaration-clause, even when FIRST is
19596 The first is the declaration of a function while the
19597 second is the definition of a variable, including its
19600 Having seen only the parenthesis, we cannot know which of
19601 these two alternatives should be selected. Even more
19602 complex are examples like:
19607 The former is a function-declaration; the latter is a
19608 variable initialization.
19610 Thus again, we try a parameter-declaration-clause, and if
19611 that fails, we back out and return. */
19613 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19616 bool is_declarator
= false;
19618 /* In a member-declarator, the only valid interpretation
19619 of a parenthesis is the start of a
19620 parameter-declaration-clause. (It is invalid to
19621 initialize a static data member with a parenthesized
19622 initializer; only the "=" form of initialization is
19625 cp_parser_parse_tentatively (parser
);
19627 /* Consume the `('. */
19628 cp_lexer_consume_token (parser
->lexer
);
19631 /* If this is going to be an abstract declarator, we're
19632 in a declarator and we can't have default args. */
19633 parser
->default_arg_ok_p
= false;
19634 parser
->in_declarator_p
= true;
19637 begin_scope (sk_function_parms
, NULL_TREE
);
19639 /* Parse the parameter-declaration-clause. */
19640 params
= cp_parser_parameter_declaration_clause (parser
);
19642 /* Consume the `)'. */
19643 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19645 /* If all went well, parse the cv-qualifier-seq,
19646 ref-qualifier and the exception-specification. */
19647 if (member_p
|| cp_parser_parse_definitely (parser
))
19649 cp_cv_quals cv_quals
;
19650 cp_virt_specifiers virt_specifiers
;
19651 cp_ref_qualifier ref_qual
;
19652 tree exception_specification
;
19655 bool memfn
= (member_p
|| (pushed_scope
19656 && CLASS_TYPE_P (pushed_scope
)));
19658 is_declarator
= true;
19660 if (ctor_dtor_or_conv_p
)
19661 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19664 /* Parse the cv-qualifier-seq. */
19665 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19666 /* Parse the ref-qualifier. */
19667 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19668 /* Parse the tx-qualifier. */
19669 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19670 /* And the exception-specification. */
19671 exception_specification
19672 = cp_parser_exception_specification_opt (parser
);
19674 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19676 /* In here, we handle cases where attribute is used after
19677 the function declaration. For example:
19678 void func (int x) __attribute__((vector(..))); */
19679 tree gnu_attrs
= NULL_TREE
;
19681 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
19683 cp_parser_parse_tentatively (parser
);
19684 tree attr
= cp_parser_gnu_attributes_opt (parser
);
19685 if (cp_lexer_next_token_is_not (parser
->lexer
,
19687 && cp_lexer_next_token_is_not (parser
->lexer
,
19689 cp_parser_abort_tentative_parse (parser
);
19690 else if (!cp_parser_parse_definitely (parser
))
19695 tree requires_clause
= NULL_TREE
;
19696 late_return
= (cp_parser_late_return_type_opt
19697 (parser
, declarator
, requires_clause
,
19698 memfn
? cv_quals
: -1));
19700 /* Parse the virt-specifier-seq. */
19701 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19703 /* Create the function-declarator. */
19704 declarator
= make_call_declarator (declarator
,
19710 exception_specification
,
19713 declarator
->std_attributes
= attrs
;
19714 declarator
->attributes
= gnu_attrs
;
19715 /* Any subsequent parameter lists are to do with
19716 return type, so are not those of the declared
19718 parser
->default_arg_ok_p
= false;
19721 /* Remove the function parms from scope. */
19722 pop_bindings_and_leave_scope ();
19725 /* Repeat the main loop. */
19729 /* If this is the first, we can try a parenthesized
19733 bool saved_in_type_id_in_expr_p
;
19735 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19736 parser
->in_declarator_p
= saved_in_declarator_p
;
19738 /* Consume the `('. */
19739 cp_lexer_consume_token (parser
->lexer
);
19740 /* Parse the nested declarator. */
19741 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19742 parser
->in_type_id_in_expr_p
= true;
19744 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
19745 /*parenthesized_p=*/NULL
,
19746 member_p
, friend_p
);
19747 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19749 /* Expect a `)'. */
19750 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
19751 declarator
= cp_error_declarator
;
19752 if (declarator
== cp_error_declarator
)
19755 goto handle_declarator
;
19757 /* Otherwise, we must be done. */
19761 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19762 && token
->type
== CPP_OPEN_SQUARE
19763 && !cp_next_tokens_can_be_attribute_p (parser
))
19765 /* Parse an array-declarator. */
19766 tree bounds
, attrs
;
19768 if (ctor_dtor_or_conv_p
)
19769 *ctor_dtor_or_conv_p
= 0;
19772 parser
->default_arg_ok_p
= false;
19773 parser
->in_declarator_p
= true;
19774 /* Consume the `['. */
19775 cp_lexer_consume_token (parser
->lexer
);
19776 /* Peek at the next token. */
19777 token
= cp_lexer_peek_token (parser
->lexer
);
19778 /* If the next token is `]', then there is no
19779 constant-expression. */
19780 if (token
->type
!= CPP_CLOSE_SQUARE
)
19782 bool non_constant_p
;
19784 = cp_parser_constant_expression (parser
,
19785 /*allow_non_constant=*/true,
19787 if (!non_constant_p
)
19789 else if (error_operand_p (bounds
))
19790 /* Already gave an error. */;
19791 else if (!parser
->in_function_body
19792 || current_binding_level
->kind
== sk_function_parms
)
19794 /* Normally, the array bound must be an integral constant
19795 expression. However, as an extension, we allow VLAs
19796 in function scopes as long as they aren't part of a
19797 parameter declaration. */
19798 cp_parser_error (parser
,
19799 "array bound is not an integer constant");
19800 bounds
= error_mark_node
;
19802 else if (processing_template_decl
19803 && !type_dependent_expression_p (bounds
))
19805 /* Remember this wasn't a constant-expression. */
19806 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
19807 TREE_SIDE_EFFECTS (bounds
) = 1;
19811 bounds
= NULL_TREE
;
19812 /* Look for the closing `]'. */
19813 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
19815 declarator
= cp_error_declarator
;
19819 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19820 declarator
= make_array_declarator (declarator
, bounds
);
19821 declarator
->std_attributes
= attrs
;
19823 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
19826 tree qualifying_scope
;
19827 tree unqualified_name
;
19829 special_function_kind sfk
;
19831 bool pack_expansion_p
= false;
19832 cp_token
*declarator_id_start_token
;
19834 /* Parse a declarator-id */
19835 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
19838 cp_parser_parse_tentatively (parser
);
19840 /* If we see an ellipsis, we should be looking at a
19842 if (token
->type
== CPP_ELLIPSIS
)
19844 /* Consume the `...' */
19845 cp_lexer_consume_token (parser
->lexer
);
19847 pack_expansion_p
= true;
19851 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
19853 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
19854 qualifying_scope
= parser
->scope
;
19859 if (!unqualified_name
&& pack_expansion_p
)
19861 /* Check whether an error occurred. */
19862 okay
= !cp_parser_error_occurred (parser
);
19864 /* We already consumed the ellipsis to mark a
19865 parameter pack, but we have no way to report it,
19866 so abort the tentative parse. We will be exiting
19867 immediately anyway. */
19868 cp_parser_abort_tentative_parse (parser
);
19871 okay
= cp_parser_parse_definitely (parser
);
19874 unqualified_name
= error_mark_node
;
19875 else if (unqualified_name
19876 && (qualifying_scope
19877 || (!identifier_p (unqualified_name
))))
19879 cp_parser_error (parser
, "expected unqualified-id");
19880 unqualified_name
= error_mark_node
;
19884 if (!unqualified_name
)
19886 if (unqualified_name
== error_mark_node
)
19888 declarator
= cp_error_declarator
;
19889 pack_expansion_p
= false;
19890 declarator
->parameter_pack_p
= false;
19894 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19896 if (qualifying_scope
&& at_namespace_scope_p ()
19897 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
19899 /* In the declaration of a member of a template class
19900 outside of the class itself, the SCOPE will sometimes
19901 be a TYPENAME_TYPE. For example, given:
19903 template <typename T>
19904 int S<T>::R::i = 3;
19906 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19907 this context, we must resolve S<T>::R to an ordinary
19908 type, rather than a typename type.
19910 The reason we normally avoid resolving TYPENAME_TYPEs
19911 is that a specialization of `S' might render
19912 `S<T>::R' not a type. However, if `S' is
19913 specialized, then this `i' will not be used, so there
19914 is no harm in resolving the types here. */
19917 /* Resolve the TYPENAME_TYPE. */
19918 type
= resolve_typename_type (qualifying_scope
,
19919 /*only_current_p=*/false);
19920 /* If that failed, the declarator is invalid. */
19921 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19923 if (typedef_variant_p (type
))
19924 error_at (declarator_id_start_token
->location
,
19925 "cannot define member of dependent typedef "
19928 error_at (declarator_id_start_token
->location
,
19929 "%<%T::%E%> is not a type",
19930 TYPE_CONTEXT (qualifying_scope
),
19931 TYPE_IDENTIFIER (qualifying_scope
));
19933 qualifying_scope
= type
;
19938 if (unqualified_name
)
19942 if (qualifying_scope
19943 && CLASS_TYPE_P (qualifying_scope
))
19944 class_type
= qualifying_scope
;
19946 class_type
= current_class_type
;
19948 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
19950 tree name_type
= TREE_TYPE (unqualified_name
);
19951 if (class_type
&& same_type_p (name_type
, class_type
))
19953 if (qualifying_scope
19954 && CLASSTYPE_USE_TEMPLATE (name_type
))
19956 error_at (declarator_id_start_token
->location
,
19957 "invalid use of constructor as a template");
19958 inform (declarator_id_start_token
->location
,
19959 "use %<%T::%D%> instead of %<%T::%D%> to "
19960 "name the constructor in a qualified name",
19962 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
19963 class_type
, name_type
);
19964 declarator
= cp_error_declarator
;
19968 unqualified_name
= constructor_name (class_type
);
19972 /* We do not attempt to print the declarator
19973 here because we do not have enough
19974 information about its original syntactic
19976 cp_parser_error (parser
, "invalid declarator");
19977 declarator
= cp_error_declarator
;
19984 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
19985 sfk
= sfk_destructor
;
19986 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
19987 sfk
= sfk_conversion
;
19988 else if (/* There's no way to declare a constructor
19989 for an unnamed type, even if the type
19990 got a name for linkage purposes. */
19991 !TYPE_WAS_UNNAMED (class_type
)
19992 /* Handle correctly (c++/19200):
20006 friend void N::S();
20009 && class_type
!= qualifying_scope
)
20010 && constructor_name_p (unqualified_name
,
20013 unqualified_name
= constructor_name (class_type
);
20014 sfk
= sfk_constructor
;
20016 else if (is_overloaded_fn (unqualified_name
)
20017 && DECL_CONSTRUCTOR_P (get_first_fn
20018 (unqualified_name
)))
20019 sfk
= sfk_constructor
;
20021 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20022 *ctor_dtor_or_conv_p
= -1;
20025 declarator
= make_id_declarator (qualifying_scope
,
20028 declarator
->std_attributes
= attrs
;
20029 declarator
->id_loc
= token
->location
;
20030 declarator
->parameter_pack_p
= pack_expansion_p
;
20032 if (pack_expansion_p
)
20033 maybe_warn_variadic_templates ();
20036 handle_declarator
:;
20037 scope
= get_scope_of_declarator (declarator
);
20040 /* Any names that appear after the declarator-id for a
20041 member are looked up in the containing scope. */
20042 if (at_function_scope_p ())
20044 /* But declarations with qualified-ids can't appear in a
20046 cp_parser_error (parser
, "qualified-id in declaration");
20047 declarator
= cp_error_declarator
;
20050 pushed_scope
= push_scope (scope
);
20052 parser
->in_declarator_p
= true;
20053 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20054 || (declarator
&& declarator
->kind
== cdk_id
))
20055 /* Default args are only allowed on function
20057 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20059 parser
->default_arg_ok_p
= false;
20068 /* For an abstract declarator, we might wind up with nothing at this
20069 point. That's an error; the declarator is not optional. */
20071 cp_parser_error (parser
, "expected declarator");
20073 /* If we entered a scope, we must exit it now. */
20075 pop_scope (pushed_scope
);
20077 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20078 parser
->in_declarator_p
= saved_in_declarator_p
;
20083 /* Parse a ptr-operator.
20086 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20087 * cv-qualifier-seq [opt]
20089 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20090 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20095 & cv-qualifier-seq [opt]
20097 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20098 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20099 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20100 filled in with the TYPE containing the member. *CV_QUALS is
20101 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20102 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20103 Note that the tree codes returned by this function have nothing
20104 to do with the types of trees that will be eventually be created
20105 to represent the pointer or reference type being parsed. They are
20106 just constants with suggestive names. */
20107 static enum tree_code
20108 cp_parser_ptr_operator (cp_parser
* parser
,
20110 cp_cv_quals
*cv_quals
,
20113 enum tree_code code
= ERROR_MARK
;
20115 tree attrs
= NULL_TREE
;
20117 /* Assume that it's not a pointer-to-member. */
20119 /* And that there are no cv-qualifiers. */
20120 *cv_quals
= TYPE_UNQUALIFIED
;
20122 /* Peek at the next token. */
20123 token
= cp_lexer_peek_token (parser
->lexer
);
20125 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20126 if (token
->type
== CPP_MULT
)
20127 code
= INDIRECT_REF
;
20128 else if (token
->type
== CPP_AND
)
20130 else if ((cxx_dialect
!= cxx98
) &&
20131 token
->type
== CPP_AND_AND
) /* C++0x only */
20132 code
= NON_LVALUE_EXPR
;
20134 if (code
!= ERROR_MARK
)
20136 /* Consume the `*', `&' or `&&'. */
20137 cp_lexer_consume_token (parser
->lexer
);
20139 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20140 `&', if we are allowing GNU extensions. (The only qualifier
20141 that can legally appear after `&' is `restrict', but that is
20142 enforced during semantic analysis. */
20143 if (code
== INDIRECT_REF
20144 || cp_parser_allow_gnu_extensions_p (parser
))
20145 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20147 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20148 if (attributes
!= NULL
)
20149 *attributes
= attrs
;
20153 /* Try the pointer-to-member case. */
20154 cp_parser_parse_tentatively (parser
);
20155 /* Look for the optional `::' operator. */
20156 cp_parser_global_scope_opt (parser
,
20157 /*current_scope_valid_p=*/false);
20158 /* Look for the nested-name specifier. */
20159 token
= cp_lexer_peek_token (parser
->lexer
);
20160 cp_parser_nested_name_specifier (parser
,
20161 /*typename_keyword_p=*/false,
20162 /*check_dependency_p=*/true,
20164 /*is_declaration=*/false);
20165 /* If we found it, and the next token is a `*', then we are
20166 indeed looking at a pointer-to-member operator. */
20167 if (!cp_parser_error_occurred (parser
)
20168 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20170 /* Indicate that the `*' operator was used. */
20171 code
= INDIRECT_REF
;
20173 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20174 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20175 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20176 error_at (token
->location
, "cannot form pointer to member of "
20177 "non-class %q#T", parser
->scope
);
20180 /* The type of which the member is a member is given by the
20182 *type
= parser
->scope
;
20183 /* The next name will not be qualified. */
20184 parser
->scope
= NULL_TREE
;
20185 parser
->qualifying_scope
= NULL_TREE
;
20186 parser
->object_scope
= NULL_TREE
;
20187 /* Look for optional c++11 attributes. */
20188 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20189 if (attributes
!= NULL
)
20190 *attributes
= attrs
;
20191 /* Look for the optional cv-qualifier-seq. */
20192 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20195 /* If that didn't work we don't have a ptr-operator. */
20196 if (!cp_parser_parse_definitely (parser
))
20197 cp_parser_error (parser
, "expected ptr-operator");
20203 /* Parse an (optional) cv-qualifier-seq.
20206 cv-qualifier cv-qualifier-seq [opt]
20217 Returns a bitmask representing the cv-qualifiers. */
20220 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20222 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20227 cp_cv_quals cv_qualifier
;
20229 /* Peek at the next token. */
20230 token
= cp_lexer_peek_token (parser
->lexer
);
20231 /* See if it's a cv-qualifier. */
20232 switch (token
->keyword
)
20235 cv_qualifier
= TYPE_QUAL_CONST
;
20239 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20243 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20247 cv_qualifier
= TYPE_UNQUALIFIED
;
20254 if (cv_quals
& cv_qualifier
)
20256 error_at (token
->location
, "duplicate cv-qualifier");
20257 cp_lexer_purge_token (parser
->lexer
);
20261 cp_lexer_consume_token (parser
->lexer
);
20262 cv_quals
|= cv_qualifier
;
20269 /* Parse an (optional) ref-qualifier
20275 Returns cp_ref_qualifier representing ref-qualifier. */
20277 static cp_ref_qualifier
20278 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20280 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20282 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20283 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20288 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20291 switch (token
->type
)
20294 curr_ref_qual
= REF_QUAL_LVALUE
;
20298 curr_ref_qual
= REF_QUAL_RVALUE
;
20302 curr_ref_qual
= REF_QUAL_NONE
;
20306 if (!curr_ref_qual
)
20310 error_at (token
->location
, "multiple ref-qualifiers");
20311 cp_lexer_purge_token (parser
->lexer
);
20315 ref_qual
= curr_ref_qual
;
20316 cp_lexer_consume_token (parser
->lexer
);
20323 /* Parse an optional tx-qualifier.
20327 transaction_safe_dynamic */
20330 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20332 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20333 if (token
->type
== CPP_NAME
)
20335 tree name
= token
->u
.value
;
20336 const char *p
= IDENTIFIER_POINTER (name
);
20337 const int len
= strlen ("transaction_safe");
20338 if (!strncmp (p
, "transaction_safe", len
))
20342 || !strcmp (p
, "_dynamic"))
20344 cp_lexer_consume_token (parser
->lexer
);
20347 error ("%E requires %<-fgnu-tm%>", name
);
20358 /* Parse an (optional) virt-specifier-seq.
20360 virt-specifier-seq:
20361 virt-specifier virt-specifier-seq [opt]
20367 Returns a bitmask representing the virt-specifiers. */
20369 static cp_virt_specifiers
20370 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20372 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20377 cp_virt_specifiers virt_specifier
;
20379 /* Peek at the next token. */
20380 token
= cp_lexer_peek_token (parser
->lexer
);
20381 /* See if it's a virt-specifier-qualifier. */
20382 if (token
->type
!= CPP_NAME
)
20384 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
20386 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20387 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20389 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
20391 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20392 virt_specifier
= VIRT_SPEC_FINAL
;
20394 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
20396 virt_specifier
= VIRT_SPEC_FINAL
;
20401 if (virt_specifiers
& virt_specifier
)
20403 error_at (token
->location
, "duplicate virt-specifier");
20404 cp_lexer_purge_token (parser
->lexer
);
20408 cp_lexer_consume_token (parser
->lexer
);
20409 virt_specifiers
|= virt_specifier
;
20412 return virt_specifiers
;
20415 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20416 is in scope even though it isn't real. */
20419 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20423 if (current_class_ptr
)
20425 /* We don't clear this between NSDMIs. Is it already what we want? */
20426 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20427 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20428 && cp_type_quals (type
) == quals
)
20432 this_parm
= build_this_parm (ctype
, quals
);
20433 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20434 current_class_ptr
= NULL_TREE
;
20436 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
20437 current_class_ptr
= this_parm
;
20440 /* Return true iff our current scope is a non-static data member
20444 parsing_nsdmi (void)
20446 /* We recognize NSDMI context by the context-less 'this' pointer set up
20447 by the function above. */
20448 if (current_class_ptr
20449 && TREE_CODE (current_class_ptr
) == PARM_DECL
20450 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20455 /* Return true iff our current scope is a default capturing generic lambda
20456 defined within a template. FIXME: This is part of a workaround (see
20457 semantics.c) to handle building lambda closure types correctly in templates
20458 which we ultimately want to defer to instantiation time. */
20461 parsing_default_capturing_generic_lambda_in_template (void)
20463 if (!processing_template_decl
|| !current_class_type
)
20466 tree lam
= CLASSTYPE_LAMBDA_EXPR (current_class_type
);
20467 if (!lam
|| LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) == CPLD_NONE
)
20470 tree callop
= lambda_function (lam
);
20474 return (DECL_TEMPLATE_INFO (callop
)
20475 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop
)) == callop
)
20476 && ((current_nonlambda_class_type ()
20477 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20478 || ((current_nonlambda_function ()
20479 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20482 /* Parse a late-specified return type, if any. This is not a separate
20483 non-terminal, but part of a function declarator, which looks like
20485 -> trailing-type-specifier-seq abstract-declarator(opt)
20487 Returns the type indicated by the type-id.
20489 In addition to this, parse any queued up #pragma omp declare simd
20490 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20491 #pragma acc routine clauses.
20493 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20497 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20498 tree
& requires_clause
, cp_cv_quals quals
)
20501 tree type
= NULL_TREE
;
20502 bool declare_simd_p
= (parser
->omp_declare_simd
20504 && declarator
->kind
== cdk_id
);
20506 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
20507 && declarator
&& declarator
->kind
== cdk_id
);
20509 bool oacc_routine_p
= (parser
->oacc_routine
20511 && declarator
->kind
== cdk_id
);
20513 /* Peek at the next token. */
20514 token
= cp_lexer_peek_token (parser
->lexer
);
20515 /* A late-specified return type is indicated by an initial '->'. */
20516 if (token
->type
!= CPP_DEREF
20517 && token
->keyword
!= RID_REQUIRES
20518 && !(token
->type
== CPP_NAME
20519 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20520 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
20523 tree save_ccp
= current_class_ptr
;
20524 tree save_ccr
= current_class_ref
;
20527 /* DR 1207: 'this' is in scope in the trailing return type. */
20528 inject_this_parameter (current_class_type
, quals
);
20531 if (token
->type
== CPP_DEREF
)
20533 /* Consume the ->. */
20534 cp_lexer_consume_token (parser
->lexer
);
20536 type
= cp_parser_trailing_type_id (parser
);
20539 /* Function declarations may be followed by a trailing
20540 requires-clause. */
20541 requires_clause
= cp_parser_requires_clause_opt (parser
);
20543 if (cilk_simd_fn_vector_p
)
20544 declarator
->attributes
20545 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
20546 declarator
->attributes
);
20547 if (declare_simd_p
)
20548 declarator
->attributes
20549 = cp_parser_late_parsing_omp_declare_simd (parser
,
20550 declarator
->attributes
);
20551 if (oacc_routine_p
)
20552 declarator
->attributes
20553 = cp_parser_late_parsing_oacc_routine (parser
,
20554 declarator
->attributes
);
20558 current_class_ptr
= save_ccp
;
20559 current_class_ref
= save_ccr
;
20565 /* Parse a declarator-id.
20569 :: [opt] nested-name-specifier [opt] type-name
20571 In the `id-expression' case, the value returned is as for
20572 cp_parser_id_expression if the id-expression was an unqualified-id.
20573 If the id-expression was a qualified-id, then a SCOPE_REF is
20574 returned. The first operand is the scope (either a NAMESPACE_DECL
20575 or TREE_TYPE), but the second is still just a representation of an
20579 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20582 /* The expression must be an id-expression. Assume that qualified
20583 names are the names of types so that:
20586 int S<T>::R::i = 3;
20588 will work; we must treat `S<T>::R' as the name of a type.
20589 Similarly, assume that qualified names are templates, where
20593 int S<T>::R<T>::i = 3;
20596 id
= cp_parser_id_expression (parser
,
20597 /*template_keyword_p=*/false,
20598 /*check_dependency_p=*/false,
20599 /*template_p=*/NULL
,
20600 /*declarator_p=*/true,
20602 if (id
&& BASELINK_P (id
))
20603 id
= BASELINK_FUNCTIONS (id
);
20607 /* Parse a type-id.
20610 type-specifier-seq abstract-declarator [opt]
20612 Returns the TYPE specified. */
20615 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20616 bool is_trailing_return
)
20618 cp_decl_specifier_seq type_specifier_seq
;
20619 cp_declarator
*abstract_declarator
;
20621 /* Parse the type-specifier-seq. */
20622 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20623 is_trailing_return
,
20624 &type_specifier_seq
);
20625 if (is_template_arg
&& type_specifier_seq
.type
20626 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20627 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20628 /* A bare template name as a template argument is a template template
20629 argument, not a placeholder, so fail parsing it as a type argument. */
20631 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20632 cp_parser_simulate_error (parser
);
20633 return error_mark_node
;
20635 if (type_specifier_seq
.type
== error_mark_node
)
20636 return error_mark_node
;
20638 /* There might or might not be an abstract declarator. */
20639 cp_parser_parse_tentatively (parser
);
20640 /* Look for the declarator. */
20641 abstract_declarator
20642 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20643 /*parenthesized_p=*/NULL
,
20644 /*member_p=*/false,
20645 /*friend_p=*/false);
20646 /* Check to see if there really was a declarator. */
20647 if (!cp_parser_parse_definitely (parser
))
20648 abstract_declarator
= NULL
;
20650 if (type_specifier_seq
.type
20651 /* The concepts TS allows 'auto' as a type-id. */
20652 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20653 /* None of the valid uses of 'auto' in C++14 involve the type-id
20654 nonterminal, but it is valid in a trailing-return-type. */
20655 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20656 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20658 /* A type-id with type 'auto' is only ok if the abstract declarator
20659 is a function declarator with a late-specified return type.
20661 A type-id with 'auto' is also valid in a trailing-return-type
20662 in a compound-requirement. */
20663 if (abstract_declarator
20664 && abstract_declarator
->kind
== cdk_function
20665 && abstract_declarator
->u
.function
.late_return_type
)
20667 else if (parser
->in_result_type_constraint_p
)
20671 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20672 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20674 error_at (loc
, "missing template arguments after %qT",
20676 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20680 error_at (loc
, "invalid use of %qT", auto_node
);
20681 return error_mark_node
;
20685 return groktypename (&type_specifier_seq
, abstract_declarator
,
20690 cp_parser_type_id (cp_parser
*parser
)
20692 return cp_parser_type_id_1 (parser
, false, false);
20696 cp_parser_template_type_arg (cp_parser
*parser
)
20699 const char *saved_message
= parser
->type_definition_forbidden_message
;
20700 parser
->type_definition_forbidden_message
20701 = G_("types may not be defined in template arguments");
20702 r
= cp_parser_type_id_1 (parser
, true, false);
20703 parser
->type_definition_forbidden_message
= saved_message
;
20704 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20706 error ("invalid use of %<auto%> in template argument");
20707 r
= error_mark_node
;
20713 cp_parser_trailing_type_id (cp_parser
*parser
)
20715 return cp_parser_type_id_1 (parser
, false, true);
20718 /* Parse a type-specifier-seq.
20720 type-specifier-seq:
20721 type-specifier type-specifier-seq [opt]
20725 type-specifier-seq:
20726 attributes type-specifier-seq [opt]
20728 If IS_DECLARATION is true, we are at the start of a "condition" or
20729 exception-declaration, so we might be followed by a declarator-id.
20731 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20732 i.e. we've just seen "->".
20734 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20737 cp_parser_type_specifier_seq (cp_parser
* parser
,
20738 bool is_declaration
,
20739 bool is_trailing_return
,
20740 cp_decl_specifier_seq
*type_specifier_seq
)
20742 bool seen_type_specifier
= false;
20743 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20744 cp_token
*start_token
= NULL
;
20746 /* Clear the TYPE_SPECIFIER_SEQ. */
20747 clear_decl_specs (type_specifier_seq
);
20749 /* In the context of a trailing return type, enum E { } is an
20750 elaborated-type-specifier followed by a function-body, not an
20752 if (is_trailing_return
)
20753 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
20755 /* Parse the type-specifiers and attributes. */
20758 tree type_specifier
;
20759 bool is_cv_qualifier
;
20761 /* Check for attributes first. */
20762 if (cp_next_tokens_can_be_attribute_p (parser
))
20764 type_specifier_seq
->attributes
=
20765 chainon (type_specifier_seq
->attributes
,
20766 cp_parser_attributes_opt (parser
));
20770 /* record the token of the beginning of the type specifier seq,
20771 for error reporting purposes*/
20773 start_token
= cp_lexer_peek_token (parser
->lexer
);
20775 /* Look for the type-specifier. */
20776 type_specifier
= cp_parser_type_specifier (parser
,
20778 type_specifier_seq
,
20779 /*is_declaration=*/false,
20782 if (!type_specifier
)
20784 /* If the first type-specifier could not be found, this is not a
20785 type-specifier-seq at all. */
20786 if (!seen_type_specifier
)
20788 /* Set in_declarator_p to avoid skipping to the semicolon. */
20789 int in_decl
= parser
->in_declarator_p
;
20790 parser
->in_declarator_p
= true;
20792 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
20793 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20794 cp_parser_error (parser
, "expected type-specifier");
20796 parser
->in_declarator_p
= in_decl
;
20798 type_specifier_seq
->type
= error_mark_node
;
20801 /* If subsequent type-specifiers could not be found, the
20802 type-specifier-seq is complete. */
20806 seen_type_specifier
= true;
20807 /* The standard says that a condition can be:
20809 type-specifier-seq declarator = assignment-expression
20816 we should treat the "S" as a declarator, not as a
20817 type-specifier. The standard doesn't say that explicitly for
20818 type-specifier-seq, but it does say that for
20819 decl-specifier-seq in an ordinary declaration. Perhaps it
20820 would be clearer just to allow a decl-specifier-seq here, and
20821 then add a semantic restriction that if any decl-specifiers
20822 that are not type-specifiers appear, the program is invalid. */
20823 if (is_declaration
&& !is_cv_qualifier
)
20824 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
20828 /* Return whether the function currently being declared has an associated
20829 template parameter list. */
20832 function_being_declared_is_template_p (cp_parser
* parser
)
20834 if (!current_template_parms
|| processing_template_parmlist
)
20837 if (parser
->implicit_template_scope
)
20840 if (at_class_scope_p ()
20841 && TYPE_BEING_DEFINED (current_class_type
))
20842 return parser
->num_template_parameter_lists
!= 0;
20844 return ((int) parser
->num_template_parameter_lists
> template_class_depth
20845 (current_class_type
));
20848 /* Parse a parameter-declaration-clause.
20850 parameter-declaration-clause:
20851 parameter-declaration-list [opt] ... [opt]
20852 parameter-declaration-list , ...
20854 Returns a representation for the parameter declarations. A return
20855 value of NULL indicates a parameter-declaration-clause consisting
20856 only of an ellipsis. */
20859 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
20868 int auto_is_implicit_function_template_parm_p
;
20870 parser
->auto_is_implicit_function_template_parm_p
20871 = auto_is_implicit_function_template_parm_p
;
20873 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
20877 if (!processing_specialization
20878 && !processing_template_parmlist
20879 && !processing_explicit_instantiation
)
20880 if (!current_function_decl
20881 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
20882 parser
->auto_is_implicit_function_template_parm_p
= true;
20884 /* Peek at the next token. */
20885 token
= cp_lexer_peek_token (parser
->lexer
);
20886 /* Check for trivial parameter-declaration-clauses. */
20887 if (token
->type
== CPP_ELLIPSIS
)
20889 /* Consume the `...' token. */
20890 cp_lexer_consume_token (parser
->lexer
);
20893 else if (token
->type
== CPP_CLOSE_PAREN
)
20894 /* There are no parameters. */
20896 #ifndef NO_IMPLICIT_EXTERN_C
20897 if (in_system_header_at (input_location
)
20898 && current_class_type
== NULL
20899 && current_lang_name
== lang_name_c
)
20903 return void_list_node
;
20905 /* Check for `(void)', too, which is a special case. */
20906 else if (token
->keyword
== RID_VOID
20907 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20908 == CPP_CLOSE_PAREN
))
20910 /* Consume the `void' token. */
20911 cp_lexer_consume_token (parser
->lexer
);
20912 /* There are no parameters. */
20913 return void_list_node
;
20916 /* Parse the parameter-declaration-list. */
20917 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
20918 /* If a parse error occurred while parsing the
20919 parameter-declaration-list, then the entire
20920 parameter-declaration-clause is erroneous. */
20924 /* Peek at the next token. */
20925 token
= cp_lexer_peek_token (parser
->lexer
);
20926 /* If it's a `,', the clause should terminate with an ellipsis. */
20927 if (token
->type
== CPP_COMMA
)
20929 /* Consume the `,'. */
20930 cp_lexer_consume_token (parser
->lexer
);
20931 /* Expect an ellipsis. */
20933 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
20935 /* It might also be `...' if the optional trailing `,' was
20937 else if (token
->type
== CPP_ELLIPSIS
)
20939 /* Consume the `...' token. */
20940 cp_lexer_consume_token (parser
->lexer
);
20941 /* And remember that we saw it. */
20945 ellipsis_p
= false;
20947 /* Finish the parameter list. */
20949 parameters
= chainon (parameters
, void_list_node
);
20954 /* Parse a parameter-declaration-list.
20956 parameter-declaration-list:
20957 parameter-declaration
20958 parameter-declaration-list , parameter-declaration
20960 Returns a representation of the parameter-declaration-list, as for
20961 cp_parser_parameter_declaration_clause. However, the
20962 `void_list_node' is never appended to the list. Upon return,
20963 *IS_ERROR will be true iff an error occurred. */
20966 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
20968 tree parameters
= NULL_TREE
;
20969 tree
*tail
= ¶meters
;
20970 bool saved_in_unbraced_linkage_specification_p
;
20973 /* Assume all will go well. */
20975 /* The special considerations that apply to a function within an
20976 unbraced linkage specifications do not apply to the parameters
20977 to the function. */
20978 saved_in_unbraced_linkage_specification_p
20979 = parser
->in_unbraced_linkage_specification_p
;
20980 parser
->in_unbraced_linkage_specification_p
= false;
20982 /* Look for more parameters. */
20985 cp_parameter_declarator
*parameter
;
20986 tree decl
= error_mark_node
;
20987 bool parenthesized_p
= false;
20988 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
20989 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20990 (current_template_parms
)) : 0);
20992 /* Parse the parameter. */
20994 = cp_parser_parameter_declaration (parser
,
20995 /*template_parm_p=*/false,
20998 /* We don't know yet if the enclosing context is deprecated, so wait
20999 and warn in grokparms if appropriate. */
21000 deprecated_state
= DEPRECATED_SUPPRESS
;
21004 /* If a function parameter pack was specified and an implicit template
21005 parameter was introduced during cp_parser_parameter_declaration,
21006 change any implicit parameters introduced into packs. */
21007 if (parser
->implicit_template_parms
21008 && parameter
->declarator
21009 && parameter
->declarator
->parameter_pack_p
)
21011 int latest_template_parm_idx
= TREE_VEC_LENGTH
21012 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21014 if (latest_template_parm_idx
!= template_parm_idx
)
21015 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21016 (parameter
->decl_specifiers
.type
,
21017 template_parm_idx
, latest_template_parm_idx
);
21020 decl
= grokdeclarator (parameter
->declarator
,
21021 ¶meter
->decl_specifiers
,
21023 parameter
->default_argument
!= NULL_TREE
,
21024 ¶meter
->decl_specifiers
.attributes
);
21027 deprecated_state
= DEPRECATED_NORMAL
;
21029 /* If a parse error occurred parsing the parameter declaration,
21030 then the entire parameter-declaration-list is erroneous. */
21031 if (decl
== error_mark_node
)
21034 parameters
= error_mark_node
;
21038 if (parameter
->decl_specifiers
.attributes
)
21039 cplus_decl_attributes (&decl
,
21040 parameter
->decl_specifiers
.attributes
,
21042 if (DECL_NAME (decl
))
21043 decl
= pushdecl (decl
);
21045 if (decl
!= error_mark_node
)
21047 retrofit_lang_decl (decl
);
21048 DECL_PARM_INDEX (decl
) = ++index
;
21049 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21052 /* Add the new parameter to the list. */
21053 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21054 tail
= &TREE_CHAIN (*tail
);
21056 /* Peek at the next token. */
21057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21058 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21059 /* These are for Objective-C++ */
21060 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21061 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21062 /* The parameter-declaration-list is complete. */
21064 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21068 /* Peek at the next token. */
21069 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21070 /* If it's an ellipsis, then the list is complete. */
21071 if (token
->type
== CPP_ELLIPSIS
)
21073 /* Otherwise, there must be more parameters. Consume the
21075 cp_lexer_consume_token (parser
->lexer
);
21076 /* When parsing something like:
21078 int i(float f, double d)
21080 we can tell after seeing the declaration for "f" that we
21081 are not looking at an initialization of a variable "i",
21082 but rather at the declaration of a function "i".
21084 Due to the fact that the parsing of template arguments
21085 (as specified to a template-id) requires backtracking we
21086 cannot use this technique when inside a template argument
21088 if (!parser
->in_template_argument_list_p
21089 && !parser
->in_type_id_in_expr_p
21090 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21091 /* However, a parameter-declaration of the form
21092 "float(f)" (which is a valid declaration of a
21093 parameter "f") can also be interpreted as an
21094 expression (the conversion of "f" to "float"). */
21095 && !parenthesized_p
)
21096 cp_parser_commit_to_tentative_parse (parser
);
21100 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21101 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21102 cp_parser_skip_to_closing_parenthesis (parser
,
21103 /*recovering=*/true,
21104 /*or_comma=*/false,
21105 /*consume_paren=*/false);
21110 parser
->in_unbraced_linkage_specification_p
21111 = saved_in_unbraced_linkage_specification_p
;
21113 /* Reset implicit_template_scope if we are about to leave the function
21114 parameter list that introduced it. Note that for out-of-line member
21115 definitions, there will be one or more class scopes before we get to
21116 the template parameter scope. */
21118 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21119 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21121 while (maybe_its
->kind
== sk_class
)
21122 maybe_its
= maybe_its
->level_chain
;
21123 if (maybe_its
== its
)
21125 parser
->implicit_template_parms
= 0;
21126 parser
->implicit_template_scope
= 0;
21133 /* Parse a parameter declaration.
21135 parameter-declaration:
21136 decl-specifier-seq ... [opt] declarator
21137 decl-specifier-seq declarator = assignment-expression
21138 decl-specifier-seq ... [opt] abstract-declarator [opt]
21139 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21141 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21142 declares a template parameter. (In that case, a non-nested `>'
21143 token encountered during the parsing of the assignment-expression
21144 is not interpreted as a greater-than operator.)
21146 Returns a representation of the parameter, or NULL if an error
21147 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21148 true iff the declarator is of the form "(p)". */
21150 static cp_parameter_declarator
*
21151 cp_parser_parameter_declaration (cp_parser
*parser
,
21152 bool template_parm_p
,
21153 bool *parenthesized_p
)
21155 int declares_class_or_enum
;
21156 cp_decl_specifier_seq decl_specifiers
;
21157 cp_declarator
*declarator
;
21158 tree default_argument
;
21159 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21160 const char *saved_message
;
21161 bool template_parameter_pack_p
= false;
21163 /* In a template parameter, `>' is not an operator.
21167 When parsing a default template-argument for a non-type
21168 template-parameter, the first non-nested `>' is taken as the end
21169 of the template parameter-list rather than a greater-than
21172 /* Type definitions may not appear in parameter types. */
21173 saved_message
= parser
->type_definition_forbidden_message
;
21174 parser
->type_definition_forbidden_message
21175 = G_("types may not be defined in parameter types");
21177 /* Parse the declaration-specifiers. */
21178 cp_parser_decl_specifier_seq (parser
,
21179 CP_PARSER_FLAGS_NONE
,
21181 &declares_class_or_enum
);
21183 /* Complain about missing 'typename' or other invalid type names. */
21184 if (!decl_specifiers
.any_type_specifiers_p
21185 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21186 decl_specifiers
.type
= error_mark_node
;
21188 /* If an error occurred, there's no reason to attempt to parse the
21189 rest of the declaration. */
21190 if (cp_parser_error_occurred (parser
))
21192 parser
->type_definition_forbidden_message
= saved_message
;
21196 /* Peek at the next token. */
21197 token
= cp_lexer_peek_token (parser
->lexer
);
21199 /* If the next token is a `)', `,', `=', `>', or `...', then there
21200 is no declarator. However, when variadic templates are enabled,
21201 there may be a declarator following `...'. */
21202 if (token
->type
== CPP_CLOSE_PAREN
21203 || token
->type
== CPP_COMMA
21204 || token
->type
== CPP_EQ
21205 || token
->type
== CPP_GREATER
)
21208 if (parenthesized_p
)
21209 *parenthesized_p
= false;
21211 /* Otherwise, there should be a declarator. */
21214 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21215 parser
->default_arg_ok_p
= false;
21217 /* After seeing a decl-specifier-seq, if the next token is not a
21218 "(", there is no possibility that the code is a valid
21219 expression. Therefore, if parsing tentatively, we commit at
21221 if (!parser
->in_template_argument_list_p
21222 /* In an expression context, having seen:
21226 we cannot be sure whether we are looking at a
21227 function-type (taking a "char" as a parameter) or a cast
21228 of some object of type "char" to "int". */
21229 && !parser
->in_type_id_in_expr_p
21230 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21231 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21232 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21233 cp_parser_commit_to_tentative_parse (parser
);
21234 /* Parse the declarator. */
21235 declarator_token_start
= token
;
21236 declarator
= cp_parser_declarator (parser
,
21237 CP_PARSER_DECLARATOR_EITHER
,
21238 /*ctor_dtor_or_conv_p=*/NULL
,
21240 /*member_p=*/false,
21241 /*friend_p=*/false);
21242 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21243 /* After the declarator, allow more attributes. */
21244 decl_specifiers
.attributes
21245 = chainon (decl_specifiers
.attributes
,
21246 cp_parser_attributes_opt (parser
));
21248 /* If the declarator is a template parameter pack, remember that and
21249 clear the flag in the declarator itself so we don't get errors
21250 from grokdeclarator. */
21251 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21253 declarator
->parameter_pack_p
= false;
21254 template_parameter_pack_p
= true;
21258 /* If the next token is an ellipsis, and we have not seen a declarator
21259 name, and if either the type of the declarator contains parameter
21260 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21261 for, eg, abbreviated integral type names), then we actually have a
21262 parameter pack expansion expression. Otherwise, leave the ellipsis
21263 for a C-style variadic function. */
21264 token
= cp_lexer_peek_token (parser
->lexer
);
21265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21267 tree type
= decl_specifiers
.type
;
21269 if (type
&& DECL_P (type
))
21270 type
= TREE_TYPE (type
);
21273 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21274 && (template_parm_p
|| uses_parameter_packs (type
)))
21275 || (!type
&& template_parm_p
))
21276 && declarator_can_be_parameter_pack (declarator
))
21278 /* Consume the `...'. */
21279 cp_lexer_consume_token (parser
->lexer
);
21280 maybe_warn_variadic_templates ();
21282 /* Build a pack expansion type */
21283 if (template_parm_p
)
21284 template_parameter_pack_p
= true;
21285 else if (declarator
)
21286 declarator
->parameter_pack_p
= true;
21288 decl_specifiers
.type
= make_pack_expansion (type
);
21292 /* The restriction on defining new types applies only to the type
21293 of the parameter, not to the default argument. */
21294 parser
->type_definition_forbidden_message
= saved_message
;
21296 /* If the next token is `=', then process a default argument. */
21297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21299 tree type
= decl_specifiers
.type
;
21300 token
= cp_lexer_peek_token (parser
->lexer
);
21301 /* If we are defining a class, then the tokens that make up the
21302 default argument must be saved and processed later. */
21303 if (!template_parm_p
&& at_class_scope_p ()
21304 && TYPE_BEING_DEFINED (current_class_type
)
21305 && !LAMBDA_TYPE_P (current_class_type
))
21306 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21308 // A constrained-type-specifier may declare a type template-parameter.
21309 else if (declares_constrained_type_template_parameter (type
))
21311 = cp_parser_default_type_template_argument (parser
);
21313 // A constrained-type-specifier may declare a template-template-parameter.
21314 else if (declares_constrained_template_template_parameter (type
))
21316 = cp_parser_default_template_template_argument (parser
);
21318 /* Outside of a class definition, we can just parse the
21319 assignment-expression. */
21322 = cp_parser_default_argument (parser
, template_parm_p
);
21324 if (!parser
->default_arg_ok_p
)
21326 permerror (token
->location
,
21327 "default arguments are only "
21328 "permitted for function parameters");
21330 else if ((declarator
&& declarator
->parameter_pack_p
)
21331 || template_parameter_pack_p
21332 || (decl_specifiers
.type
21333 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21335 /* Find the name of the parameter pack. */
21336 cp_declarator
*id_declarator
= declarator
;
21337 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21338 id_declarator
= id_declarator
->declarator
;
21340 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21341 error_at (declarator_token_start
->location
,
21343 ? G_("template parameter pack %qD "
21344 "cannot have a default argument")
21345 : G_("parameter pack %qD cannot have "
21346 "a default argument"),
21347 id_declarator
->u
.id
.unqualified_name
);
21349 error_at (declarator_token_start
->location
,
21351 ? G_("template parameter pack cannot have "
21352 "a default argument")
21353 : G_("parameter pack cannot have a "
21354 "default argument"));
21356 default_argument
= NULL_TREE
;
21360 default_argument
= NULL_TREE
;
21362 return make_parameter_declarator (&decl_specifiers
,
21365 template_parameter_pack_p
);
21368 /* Parse a default argument and return it.
21370 TEMPLATE_PARM_P is true if this is a default argument for a
21371 non-type template parameter. */
21373 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21375 tree default_argument
= NULL_TREE
;
21376 bool saved_greater_than_is_operator_p
;
21377 bool saved_local_variables_forbidden_p
;
21378 bool non_constant_p
, is_direct_init
;
21380 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21382 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21383 parser
->greater_than_is_operator_p
= !template_parm_p
;
21384 /* Local variable names (and the `this' keyword) may not
21385 appear in a default argument. */
21386 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21387 parser
->local_variables_forbidden_p
= true;
21388 /* Parse the assignment-expression. */
21389 if (template_parm_p
)
21390 push_deferring_access_checks (dk_no_deferred
);
21391 tree saved_class_ptr
= NULL_TREE
;
21392 tree saved_class_ref
= NULL_TREE
;
21393 /* The "this" pointer is not valid in a default argument. */
21396 saved_class_ptr
= current_class_ptr
;
21397 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21398 saved_class_ref
= current_class_ref
;
21399 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21402 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21403 /* Restore the "this" pointer. */
21406 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21407 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21409 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21410 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21411 if (template_parm_p
)
21412 pop_deferring_access_checks ();
21413 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21414 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21416 return default_argument
;
21419 /* Parse a function-body.
21422 compound_statement */
21425 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21427 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21428 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21432 /* Parse a ctor-initializer-opt followed by a function-body. Return
21433 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21434 is true we are parsing a function-try-block. */
21437 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21438 bool in_function_try_block
)
21441 bool ctor_initializer_p
;
21442 const bool check_body_p
=
21443 DECL_CONSTRUCTOR_P (current_function_decl
)
21444 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21447 /* Begin the function body. */
21448 body
= begin_function_body ();
21449 /* Parse the optional ctor-initializer. */
21450 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
21452 /* If we're parsing a constexpr constructor definition, we need
21453 to check that the constructor body is indeed empty. However,
21454 before we get to cp_parser_function_body lot of junk has been
21455 generated, so we can't just check that we have an empty block.
21456 Rather we take a snapshot of the outermost block, and check whether
21457 cp_parser_function_body changed its state. */
21460 list
= cur_stmt_list
;
21461 if (STATEMENT_LIST_TAIL (list
))
21462 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21464 /* Parse the function-body. */
21465 cp_parser_function_body (parser
, in_function_try_block
);
21467 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21468 /* Finish the function body. */
21469 finish_function_body (body
);
21471 return ctor_initializer_p
;
21474 /* Parse an initializer.
21477 = initializer-clause
21478 ( expression-list )
21480 Returns an expression representing the initializer. If no
21481 initializer is present, NULL_TREE is returned.
21483 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21484 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21485 set to TRUE if there is no initializer present. If there is an
21486 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21487 is set to true; otherwise it is set to false. */
21490 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21491 bool* non_constant_p
)
21496 /* Peek at the next token. */
21497 token
= cp_lexer_peek_token (parser
->lexer
);
21499 /* Let our caller know whether or not this initializer was
21501 *is_direct_init
= (token
->type
!= CPP_EQ
);
21502 /* Assume that the initializer is constant. */
21503 *non_constant_p
= false;
21505 if (token
->type
== CPP_EQ
)
21507 /* Consume the `='. */
21508 cp_lexer_consume_token (parser
->lexer
);
21509 /* Parse the initializer-clause. */
21510 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21512 else if (token
->type
== CPP_OPEN_PAREN
)
21514 vec
<tree
, va_gc
> *vec
;
21515 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21517 /*allow_expansion_p=*/true,
21520 return error_mark_node
;
21521 init
= build_tree_list_vec (vec
);
21522 release_tree_vector (vec
);
21524 else if (token
->type
== CPP_OPEN_BRACE
)
21526 cp_lexer_set_source_position (parser
->lexer
);
21527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21528 init
= cp_parser_braced_list (parser
, non_constant_p
);
21529 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21533 /* Anything else is an error. */
21534 cp_parser_error (parser
, "expected initializer");
21535 init
= error_mark_node
;
21538 if (check_for_bare_parameter_packs (init
))
21539 init
= error_mark_node
;
21544 /* Parse an initializer-clause.
21546 initializer-clause:
21547 assignment-expression
21550 Returns an expression representing the initializer.
21552 If the `assignment-expression' production is used the value
21553 returned is simply a representation for the expression.
21555 Otherwise, calls cp_parser_braced_list. */
21558 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21560 cp_expr initializer
;
21562 /* Assume the expression is constant. */
21563 *non_constant_p
= false;
21565 /* If it is not a `{', then we are looking at an
21566 assignment-expression. */
21567 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21570 = cp_parser_constant_expression (parser
,
21571 /*allow_non_constant_p=*/true,
21575 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21577 return initializer
;
21580 /* Parse a brace-enclosed initializer list.
21583 { initializer-list , [opt] }
21586 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21587 the elements of the initializer-list (or NULL, if the last
21588 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21589 NULL_TREE. There is no way to detect whether or not the optional
21590 trailing `,' was provided. NON_CONSTANT_P is as for
21591 cp_parser_initializer. */
21594 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21597 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21599 /* Consume the `{' token. */
21600 cp_lexer_consume_token (parser
->lexer
);
21601 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21602 initializer
= make_node (CONSTRUCTOR
);
21603 /* If it's not a `}', then there is a non-trivial initializer. */
21604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21606 /* Parse the initializer list. */
21607 CONSTRUCTOR_ELTS (initializer
)
21608 = cp_parser_initializer_list (parser
, non_constant_p
);
21609 /* A trailing `,' token is allowed. */
21610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21611 cp_lexer_consume_token (parser
->lexer
);
21614 *non_constant_p
= false;
21615 /* Now, there should be a trailing `}'. */
21616 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21617 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21618 TREE_TYPE (initializer
) = init_list_type_node
;
21620 cp_expr
result (initializer
);
21621 /* Build a location of the form:
21624 with caret==start at the open brace, finish at the close brace. */
21625 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21626 result
.set_location (combined_loc
);
21630 /* Consume tokens up to, and including, the next non-nested closing `]'.
21631 Returns true iff we found a closing `]'. */
21634 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21636 unsigned square_depth
= 0;
21640 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21642 switch (token
->type
)
21645 case CPP_PRAGMA_EOL
:
21646 /* If we've run out of tokens, then there is no closing `]'. */
21649 case CPP_OPEN_SQUARE
:
21653 case CPP_CLOSE_SQUARE
:
21654 if (!square_depth
--)
21656 cp_lexer_consume_token (parser
->lexer
);
21665 /* Consume the token. */
21666 cp_lexer_consume_token (parser
->lexer
);
21670 /* Return true if we are looking at an array-designator, false otherwise. */
21673 cp_parser_array_designator_p (cp_parser
*parser
)
21675 /* Consume the `['. */
21676 cp_lexer_consume_token (parser
->lexer
);
21678 cp_lexer_save_tokens (parser
->lexer
);
21680 /* Skip tokens until the next token is a closing square bracket.
21681 If we find the closing `]', and the next token is a `=', then
21682 we are looking at an array designator. */
21683 bool array_designator_p
21684 = (cp_parser_skip_to_closing_square_bracket (parser
)
21685 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21687 /* Roll back the tokens we skipped. */
21688 cp_lexer_rollback_tokens (parser
->lexer
);
21690 return array_designator_p
;
21693 /* Parse an initializer-list.
21696 initializer-clause ... [opt]
21697 initializer-list , initializer-clause ... [opt]
21702 designation initializer-clause ...[opt]
21703 initializer-list , designation initializer-clause ...[opt]
21708 [ constant-expression ] =
21710 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21711 for the initializer. If the INDEX of the elt is non-NULL, it is the
21712 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21713 as for cp_parser_initializer. */
21715 static vec
<constructor_elt
, va_gc
> *
21716 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
21718 vec
<constructor_elt
, va_gc
> *v
= NULL
;
21720 /* Assume all of the expressions are constant. */
21721 *non_constant_p
= false;
21723 /* Parse the rest of the list. */
21729 bool clause_non_constant_p
;
21731 /* If the next token is an identifier and the following one is a
21732 colon, we are looking at the GNU designated-initializer
21734 if (cp_parser_allow_gnu_extensions_p (parser
)
21735 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
21736 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
21738 /* Warn the user that they are using an extension. */
21739 pedwarn (input_location
, OPT_Wpedantic
,
21740 "ISO C++ does not allow designated initializers");
21741 /* Consume the identifier. */
21742 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21743 /* Consume the `:'. */
21744 cp_lexer_consume_token (parser
->lexer
);
21746 /* Also handle the C99 syntax, '. id ='. */
21747 else if (cp_parser_allow_gnu_extensions_p (parser
)
21748 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
21749 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
21750 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
21752 /* Warn the user that they are using an extension. */
21753 pedwarn (input_location
, OPT_Wpedantic
,
21754 "ISO C++ does not allow C99 designated initializers");
21755 /* Consume the `.'. */
21756 cp_lexer_consume_token (parser
->lexer
);
21757 /* Consume the identifier. */
21758 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21759 /* Consume the `='. */
21760 cp_lexer_consume_token (parser
->lexer
);
21762 /* Also handle C99 array designators, '[ const ] ='. */
21763 else if (cp_parser_allow_gnu_extensions_p (parser
)
21764 && !c_dialect_objc ()
21765 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21767 /* In C++11, [ could start a lambda-introducer. */
21768 bool non_const
= false;
21770 cp_parser_parse_tentatively (parser
);
21772 if (!cp_parser_array_designator_p (parser
))
21774 cp_parser_simulate_error (parser
);
21775 designator
= NULL_TREE
;
21779 designator
= cp_parser_constant_expression (parser
, true,
21781 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21782 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21785 if (!cp_parser_parse_definitely (parser
))
21786 designator
= NULL_TREE
;
21787 else if (non_const
)
21788 require_potential_rvalue_constant_expression (designator
);
21791 designator
= NULL_TREE
;
21793 /* Parse the initializer. */
21794 initializer
= cp_parser_initializer_clause (parser
,
21795 &clause_non_constant_p
);
21796 /* If any clause is non-constant, so is the entire initializer. */
21797 if (clause_non_constant_p
)
21798 *non_constant_p
= true;
21800 /* If we have an ellipsis, this is an initializer pack
21802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21804 /* Consume the `...'. */
21805 cp_lexer_consume_token (parser
->lexer
);
21807 /* Turn the initializer into an initializer expansion. */
21808 initializer
= make_pack_expansion (initializer
);
21811 /* Add it to the vector. */
21812 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
21814 /* If the next token is not a comma, we have reached the end of
21816 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21819 /* Peek at the next token. */
21820 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21821 /* If the next token is a `}', then we're still done. An
21822 initializer-clause can have a trailing `,' after the
21823 initializer-list and before the closing `}'. */
21824 if (token
->type
== CPP_CLOSE_BRACE
)
21827 /* Consume the `,' token. */
21828 cp_lexer_consume_token (parser
->lexer
);
21834 /* Classes [gram.class] */
21836 /* Parse a class-name.
21842 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21843 to indicate that names looked up in dependent types should be
21844 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21845 keyword has been used to indicate that the name that appears next
21846 is a template. TAG_TYPE indicates the explicit tag given before
21847 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21848 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21849 is the class being defined in a class-head. If ENUM_OK is TRUE,
21850 enum-names are also accepted.
21852 Returns the TYPE_DECL representing the class. */
21855 cp_parser_class_name (cp_parser
*parser
,
21856 bool typename_keyword_p
,
21857 bool template_keyword_p
,
21858 enum tag_types tag_type
,
21859 bool check_dependency_p
,
21861 bool is_declaration
,
21868 tree identifier
= NULL_TREE
;
21870 /* All class-names start with an identifier. */
21871 token
= cp_lexer_peek_token (parser
->lexer
);
21872 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
21874 cp_parser_error (parser
, "expected class-name");
21875 return error_mark_node
;
21878 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21879 to a template-id, so we save it here. */
21880 scope
= parser
->scope
;
21881 if (scope
== error_mark_node
)
21882 return error_mark_node
;
21884 /* Any name names a type if we're following the `typename' keyword
21885 in a qualified name where the enclosing scope is type-dependent. */
21886 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
21887 && dependent_type_p (scope
));
21888 /* Handle the common case (an identifier, but not a template-id)
21890 if (token
->type
== CPP_NAME
21891 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
21893 cp_token
*identifier_token
;
21896 /* Look for the identifier. */
21897 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
21898 ambiguous_p
= identifier_token
->error_reported
;
21899 identifier
= cp_parser_identifier (parser
);
21900 /* If the next token isn't an identifier, we are certainly not
21901 looking at a class-name. */
21902 if (identifier
== error_mark_node
)
21903 decl
= error_mark_node
;
21904 /* If we know this is a type-name, there's no need to look it
21906 else if (typename_p
)
21910 tree ambiguous_decls
;
21911 /* If we already know that this lookup is ambiguous, then
21912 we've already issued an error message; there's no reason
21916 cp_parser_simulate_error (parser
);
21917 return error_mark_node
;
21919 /* If the next token is a `::', then the name must be a type
21922 [basic.lookup.qual]
21924 During the lookup for a name preceding the :: scope
21925 resolution operator, object, function, and enumerator
21926 names are ignored. */
21927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21928 tag_type
= scope_type
;
21929 /* Look up the name. */
21930 decl
= cp_parser_lookup_name (parser
, identifier
,
21932 /*is_template=*/false,
21933 /*is_namespace=*/false,
21934 check_dependency_p
,
21936 identifier_token
->location
);
21937 if (ambiguous_decls
)
21939 if (cp_parser_parsing_tentatively (parser
))
21940 cp_parser_simulate_error (parser
);
21941 return error_mark_node
;
21947 /* Try a template-id. */
21948 decl
= cp_parser_template_id (parser
, template_keyword_p
,
21949 check_dependency_p
,
21952 if (decl
== error_mark_node
)
21953 return error_mark_node
;
21956 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
21958 /* If this is a typename, create a TYPENAME_TYPE. */
21959 if (typename_p
&& decl
!= error_mark_node
)
21961 decl
= make_typename_type (scope
, decl
, typename_type
,
21962 /*complain=*/tf_error
);
21963 if (decl
!= error_mark_node
)
21964 decl
= TYPE_NAME (decl
);
21967 decl
= strip_using_decl (decl
);
21969 /* Check to see that it is really the name of a class. */
21970 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
21971 && identifier_p (TREE_OPERAND (decl
, 0))
21972 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21973 /* Situations like this:
21975 template <typename T> struct A {
21976 typename T::template X<int>::I i;
21979 are problematic. Is `T::template X<int>' a class-name? The
21980 standard does not seem to be definitive, but there is no other
21981 valid interpretation of the following `::'. Therefore, those
21982 names are considered class-names. */
21984 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
21985 if (decl
!= error_mark_node
)
21986 decl
= TYPE_NAME (decl
);
21988 else if (TREE_CODE (decl
) != TYPE_DECL
21989 || TREE_TYPE (decl
) == error_mark_node
21990 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
21991 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
21992 /* In Objective-C 2.0, a classname followed by '.' starts a
21993 dot-syntax expression, and it's not a type-name. */
21994 || (c_dialect_objc ()
21995 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
21996 && objc_is_class_name (decl
)))
21997 decl
= error_mark_node
;
21999 if (decl
== error_mark_node
)
22000 cp_parser_error (parser
, "expected class-name");
22001 else if (identifier
&& !parser
->scope
)
22002 maybe_note_name_used_in_class (identifier
, decl
);
22007 /* Parse a class-specifier.
22010 class-head { member-specification [opt] }
22012 Returns the TREE_TYPE representing the class. */
22015 cp_parser_class_specifier_1 (cp_parser
* parser
)
22018 tree attributes
= NULL_TREE
;
22019 bool nested_name_specifier_p
;
22020 unsigned saved_num_template_parameter_lists
;
22021 bool saved_in_function_body
;
22022 unsigned char in_statement
;
22023 bool in_switch_statement_p
;
22024 bool saved_in_unbraced_linkage_specification_p
;
22025 tree old_scope
= NULL_TREE
;
22026 tree scope
= NULL_TREE
;
22027 cp_token
*closing_brace
;
22029 push_deferring_access_checks (dk_no_deferred
);
22031 /* Parse the class-head. */
22032 type
= cp_parser_class_head (parser
,
22033 &nested_name_specifier_p
);
22034 /* If the class-head was a semantic disaster, skip the entire body
22038 cp_parser_skip_to_end_of_block_or_statement (parser
);
22039 pop_deferring_access_checks ();
22040 return error_mark_node
;
22043 /* Look for the `{'. */
22044 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
22046 pop_deferring_access_checks ();
22047 return error_mark_node
;
22050 cp_ensure_no_omp_declare_simd (parser
);
22051 cp_ensure_no_oacc_routine (parser
);
22053 /* Issue an error message if type-definitions are forbidden here. */
22054 cp_parser_check_type_definition (parser
);
22055 /* Remember that we are defining one more class. */
22056 ++parser
->num_classes_being_defined
;
22057 /* Inside the class, surrounding template-parameter-lists do not
22059 saved_num_template_parameter_lists
22060 = parser
->num_template_parameter_lists
;
22061 parser
->num_template_parameter_lists
= 0;
22062 /* We are not in a function body. */
22063 saved_in_function_body
= parser
->in_function_body
;
22064 parser
->in_function_body
= false;
22065 /* Or in a loop. */
22066 in_statement
= parser
->in_statement
;
22067 parser
->in_statement
= 0;
22068 /* Or in a switch. */
22069 in_switch_statement_p
= parser
->in_switch_statement_p
;
22070 parser
->in_switch_statement_p
= false;
22071 /* We are not immediately inside an extern "lang" block. */
22072 saved_in_unbraced_linkage_specification_p
22073 = parser
->in_unbraced_linkage_specification_p
;
22074 parser
->in_unbraced_linkage_specification_p
= false;
22076 // Associate constraints with the type.
22078 type
= associate_classtype_constraints (type
);
22080 /* Start the class. */
22081 if (nested_name_specifier_p
)
22083 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22084 old_scope
= push_inner_scope (scope
);
22086 type
= begin_class_definition (type
);
22088 if (type
== error_mark_node
)
22089 /* If the type is erroneous, skip the entire body of the class. */
22090 cp_parser_skip_to_closing_brace (parser
);
22092 /* Parse the member-specification. */
22093 cp_parser_member_specification_opt (parser
);
22095 /* Look for the trailing `}'. */
22096 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22097 /* Look for trailing attributes to apply to this class. */
22098 if (cp_parser_allow_gnu_extensions_p (parser
))
22099 attributes
= cp_parser_gnu_attributes_opt (parser
);
22100 if (type
!= error_mark_node
)
22101 type
= finish_struct (type
, attributes
);
22102 if (nested_name_specifier_p
)
22103 pop_inner_scope (old_scope
, scope
);
22105 /* We've finished a type definition. Check for the common syntax
22106 error of forgetting a semicolon after the definition. We need to
22107 be careful, as we can't just check for not-a-semicolon and be done
22108 with it; the user might have typed:
22110 class X { } c = ...;
22111 class X { } *p = ...;
22113 and so forth. Instead, enumerate all the possible tokens that
22114 might follow this production; if we don't see one of them, then
22115 complain and silently insert the semicolon. */
22117 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22118 bool want_semicolon
= true;
22120 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22121 /* Don't try to parse c++11 attributes here. As per the
22122 grammar, that should be a task for
22123 cp_parser_decl_specifier_seq. */
22124 want_semicolon
= false;
22126 switch (token
->type
)
22129 case CPP_SEMICOLON
:
22132 case CPP_OPEN_PAREN
:
22133 case CPP_CLOSE_PAREN
:
22135 want_semicolon
= false;
22138 /* While it's legal for type qualifiers and storage class
22139 specifiers to follow type definitions in the grammar, only
22140 compiler testsuites contain code like that. Assume that if
22141 we see such code, then what we're really seeing is a case
22145 const <type> var = ...;
22150 static <type> func (...) ...
22152 i.e. the qualifier or specifier applies to the next
22153 declaration. To do so, however, we need to look ahead one
22154 more token to see if *that* token is a type specifier.
22156 This code could be improved to handle:
22159 static const <type> var = ...; */
22161 if (keyword_is_decl_specifier (token
->keyword
))
22163 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22165 /* Handling user-defined types here would be nice, but very
22168 = (lookahead
->type
== CPP_KEYWORD
22169 && keyword_begins_type_specifier (lookahead
->keyword
));
22176 /* If we don't have a type, then something is very wrong and we
22177 shouldn't try to do anything clever. Likewise for not seeing the
22179 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22181 /* Locate the closing brace. */
22182 cp_token_position prev
22183 = cp_lexer_previous_token_position (parser
->lexer
);
22184 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22185 location_t loc
= prev_token
->location
;
22187 /* We want to suggest insertion of a ';' immediately *after* the
22188 closing brace, so, if we can, offset the location by 1 column. */
22189 location_t next_loc
= loc
;
22190 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22191 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22193 rich_location
richloc (line_table
, next_loc
);
22195 /* If we successfully offset the location, suggest the fix-it. */
22196 if (next_loc
!= loc
)
22197 richloc
.add_fixit_insert_before (next_loc
, ";");
22199 if (CLASSTYPE_DECLARED_CLASS (type
))
22200 error_at_rich_loc (&richloc
,
22201 "expected %<;%> after class definition");
22202 else if (TREE_CODE (type
) == RECORD_TYPE
)
22203 error_at_rich_loc (&richloc
,
22204 "expected %<;%> after struct definition");
22205 else if (TREE_CODE (type
) == UNION_TYPE
)
22206 error_at_rich_loc (&richloc
,
22207 "expected %<;%> after union definition");
22209 gcc_unreachable ();
22211 /* Unget one token and smash it to look as though we encountered
22212 a semicolon in the input stream. */
22213 cp_lexer_set_token_position (parser
->lexer
, prev
);
22214 token
= cp_lexer_peek_token (parser
->lexer
);
22215 token
->type
= CPP_SEMICOLON
;
22216 token
->keyword
= RID_MAX
;
22220 /* If this class is not itself within the scope of another class,
22221 then we need to parse the bodies of all of the queued function
22222 definitions. Note that the queued functions defined in a class
22223 are not always processed immediately following the
22224 class-specifier for that class. Consider:
22227 struct B { void f() { sizeof (A); } };
22230 If `f' were processed before the processing of `A' were
22231 completed, there would be no way to compute the size of `A'.
22232 Note that the nesting we are interested in here is lexical --
22233 not the semantic nesting given by TYPE_CONTEXT. In particular,
22236 struct A { struct B; };
22237 struct A::B { void f() { } };
22239 there is no need to delay the parsing of `A::B::f'. */
22240 if (--parser
->num_classes_being_defined
== 0)
22243 tree class_type
= NULL_TREE
;
22244 tree pushed_scope
= NULL_TREE
;
22246 cp_default_arg_entry
*e
;
22247 tree save_ccp
, save_ccr
;
22249 /* In a first pass, parse default arguments to the functions.
22250 Then, in a second pass, parse the bodies of the functions.
22251 This two-phased approach handles cases like:
22259 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22262 /* If there are default arguments that have not yet been processed,
22263 take care of them now. */
22264 if (class_type
!= e
->class_type
)
22267 pop_scope (pushed_scope
);
22268 class_type
= e
->class_type
;
22269 pushed_scope
= push_scope (class_type
);
22271 /* Make sure that any template parameters are in scope. */
22272 maybe_begin_member_template_processing (decl
);
22273 /* Parse the default argument expressions. */
22274 cp_parser_late_parsing_default_args (parser
, decl
);
22275 /* Remove any template parameters from the symbol table. */
22276 maybe_end_member_template_processing ();
22278 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22279 /* Now parse any NSDMIs. */
22280 save_ccp
= current_class_ptr
;
22281 save_ccr
= current_class_ref
;
22282 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22284 if (class_type
!= DECL_CONTEXT (decl
))
22287 pop_scope (pushed_scope
);
22288 class_type
= DECL_CONTEXT (decl
);
22289 pushed_scope
= push_scope (class_type
);
22291 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22292 cp_parser_late_parsing_nsdmi (parser
, decl
);
22294 vec_safe_truncate (unparsed_nsdmis
, 0);
22295 current_class_ptr
= save_ccp
;
22296 current_class_ref
= save_ccr
;
22298 pop_scope (pushed_scope
);
22300 /* Now do some post-NSDMI bookkeeping. */
22301 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22302 after_nsdmi_defaulted_late_checks (class_type
);
22303 vec_safe_truncate (unparsed_classes
, 0);
22304 after_nsdmi_defaulted_late_checks (type
);
22306 /* Now parse the body of the functions. */
22309 /* OpenMP UDRs need to be parsed before all other functions. */
22310 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22311 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22312 cp_parser_late_parsing_for_member (parser
, decl
);
22313 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22314 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22315 cp_parser_late_parsing_for_member (parser
, decl
);
22318 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22319 cp_parser_late_parsing_for_member (parser
, decl
);
22320 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22323 vec_safe_push (unparsed_classes
, type
);
22325 /* Put back any saved access checks. */
22326 pop_deferring_access_checks ();
22328 /* Restore saved state. */
22329 parser
->in_switch_statement_p
= in_switch_statement_p
;
22330 parser
->in_statement
= in_statement
;
22331 parser
->in_function_body
= saved_in_function_body
;
22332 parser
->num_template_parameter_lists
22333 = saved_num_template_parameter_lists
;
22334 parser
->in_unbraced_linkage_specification_p
22335 = saved_in_unbraced_linkage_specification_p
;
22341 cp_parser_class_specifier (cp_parser
* parser
)
22344 timevar_push (TV_PARSE_STRUCT
);
22345 ret
= cp_parser_class_specifier_1 (parser
);
22346 timevar_pop (TV_PARSE_STRUCT
);
22350 /* Parse a class-head.
22353 class-key identifier [opt] base-clause [opt]
22354 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22355 class-key nested-name-specifier [opt] template-id
22358 class-virt-specifier:
22362 class-key attributes identifier [opt] base-clause [opt]
22363 class-key attributes nested-name-specifier identifier base-clause [opt]
22364 class-key attributes nested-name-specifier [opt] template-id
22367 Upon return BASES is initialized to the list of base classes (or
22368 NULL, if there are none) in the same form returned by
22369 cp_parser_base_clause.
22371 Returns the TYPE of the indicated class. Sets
22372 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22373 involving a nested-name-specifier was used, and FALSE otherwise.
22375 Returns error_mark_node if this is not a class-head.
22377 Returns NULL_TREE if the class-head is syntactically valid, but
22378 semantically invalid in a way that means we should skip the entire
22379 body of the class. */
22382 cp_parser_class_head (cp_parser
* parser
,
22383 bool* nested_name_specifier_p
)
22385 tree nested_name_specifier
;
22386 enum tag_types class_key
;
22387 tree id
= NULL_TREE
;
22388 tree type
= NULL_TREE
;
22391 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22392 bool template_id_p
= false;
22393 bool qualified_p
= false;
22394 bool invalid_nested_name_p
= false;
22395 bool invalid_explicit_specialization_p
= false;
22396 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22397 tree pushed_scope
= NULL_TREE
;
22398 unsigned num_templates
;
22399 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22400 /* Assume no nested-name-specifier will be present. */
22401 *nested_name_specifier_p
= false;
22402 /* Assume no template parameter lists will be used in defining the
22405 parser
->colon_corrects_to_scope_p
= false;
22407 /* Look for the class-key. */
22408 class_key
= cp_parser_class_key (parser
);
22409 if (class_key
== none_type
)
22410 return error_mark_node
;
22412 location_t class_head_start_location
= input_location
;
22414 /* Parse the attributes. */
22415 attributes
= cp_parser_attributes_opt (parser
);
22417 /* If the next token is `::', that is invalid -- but sometimes
22418 people do try to write:
22422 Handle this gracefully by accepting the extra qualifier, and then
22423 issuing an error about it later if this really is a
22424 class-head. If it turns out just to be an elaborated type
22425 specifier, remain silent. */
22426 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22427 qualified_p
= true;
22429 push_deferring_access_checks (dk_no_check
);
22431 /* Determine the name of the class. Begin by looking for an
22432 optional nested-name-specifier. */
22433 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22434 nested_name_specifier
22435 = cp_parser_nested_name_specifier_opt (parser
,
22436 /*typename_keyword_p=*/false,
22437 /*check_dependency_p=*/false,
22439 /*is_declaration=*/false);
22440 /* If there was a nested-name-specifier, then there *must* be an
22442 if (nested_name_specifier
)
22444 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22445 /* Although the grammar says `identifier', it really means
22446 `class-name' or `template-name'. You are only allowed to
22447 define a class that has already been declared with this
22450 The proposed resolution for Core Issue 180 says that wherever
22451 you see `class T::X' you should treat `X' as a type-name.
22453 It is OK to define an inaccessible class; for example:
22455 class A { class B; };
22458 We do not know if we will see a class-name, or a
22459 template-name. We look for a class-name first, in case the
22460 class-name is a template-id; if we looked for the
22461 template-name first we would stop after the template-name. */
22462 cp_parser_parse_tentatively (parser
);
22463 type
= cp_parser_class_name (parser
,
22464 /*typename_keyword_p=*/false,
22465 /*template_keyword_p=*/false,
22467 /*check_dependency_p=*/false,
22468 /*class_head_p=*/true,
22469 /*is_declaration=*/false);
22470 /* If that didn't work, ignore the nested-name-specifier. */
22471 if (!cp_parser_parse_definitely (parser
))
22473 invalid_nested_name_p
= true;
22474 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22475 id
= cp_parser_identifier (parser
);
22476 if (id
== error_mark_node
)
22479 /* If we could not find a corresponding TYPE, treat this
22480 declaration like an unqualified declaration. */
22481 if (type
== error_mark_node
)
22482 nested_name_specifier
= NULL_TREE
;
22483 /* Otherwise, count the number of templates used in TYPE and its
22484 containing scopes. */
22489 for (scope
= TREE_TYPE (type
);
22490 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22491 scope
= get_containing_scope (scope
))
22493 && CLASS_TYPE_P (scope
)
22494 && CLASSTYPE_TEMPLATE_INFO (scope
)
22495 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22496 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22497 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22501 /* Otherwise, the identifier is optional. */
22504 /* We don't know whether what comes next is a template-id,
22505 an identifier, or nothing at all. */
22506 cp_parser_parse_tentatively (parser
);
22507 /* Check for a template-id. */
22508 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22509 id
= cp_parser_template_id (parser
,
22510 /*template_keyword_p=*/false,
22511 /*check_dependency_p=*/true,
22513 /*is_declaration=*/true);
22514 /* If that didn't work, it could still be an identifier. */
22515 if (!cp_parser_parse_definitely (parser
))
22517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22519 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22520 id
= cp_parser_identifier (parser
);
22527 template_id_p
= true;
22532 pop_deferring_access_checks ();
22536 cp_parser_check_for_invalid_template_id (parser
, id
,
22538 type_start_token
->location
);
22540 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22542 /* If it's not a `:' or a `{' then we can't really be looking at a
22543 class-head, since a class-head only appears as part of a
22544 class-specifier. We have to detect this situation before calling
22545 xref_tag, since that has irreversible side-effects. */
22546 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22548 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22549 type
= error_mark_node
;
22553 /* At this point, we're going ahead with the class-specifier, even
22554 if some other problem occurs. */
22555 cp_parser_commit_to_tentative_parse (parser
);
22556 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22558 cp_parser_error (parser
,
22559 "cannot specify %<override%> for a class");
22560 type
= error_mark_node
;
22563 /* Issue the error about the overly-qualified name now. */
22566 cp_parser_error (parser
,
22567 "global qualification of class name is invalid");
22568 type
= error_mark_node
;
22571 else if (invalid_nested_name_p
)
22573 cp_parser_error (parser
,
22574 "qualified name does not name a class");
22575 type
= error_mark_node
;
22578 else if (nested_name_specifier
)
22582 /* Reject typedef-names in class heads. */
22583 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22585 error_at (type_start_token
->location
,
22586 "invalid class name in declaration of %qD",
22592 /* Figure out in what scope the declaration is being placed. */
22593 scope
= current_scope ();
22594 /* If that scope does not contain the scope in which the
22595 class was originally declared, the program is invalid. */
22596 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22598 if (at_namespace_scope_p ())
22599 error_at (type_start_token
->location
,
22600 "declaration of %qD in namespace %qD which does not "
22602 type
, scope
, nested_name_specifier
);
22604 error_at (type_start_token
->location
,
22605 "declaration of %qD in %qD which does not enclose %qD",
22606 type
, scope
, nested_name_specifier
);
22612 A declarator-id shall not be qualified except for the
22613 definition of a ... nested class outside of its class
22614 ... [or] the definition or explicit instantiation of a
22615 class member of a namespace outside of its namespace. */
22616 if (scope
== nested_name_specifier
)
22618 permerror (nested_name_specifier_token_start
->location
,
22619 "extra qualification not allowed");
22620 nested_name_specifier
= NULL_TREE
;
22624 /* An explicit-specialization must be preceded by "template <>". If
22625 it is not, try to recover gracefully. */
22626 if (at_namespace_scope_p ()
22627 && parser
->num_template_parameter_lists
== 0
22628 && !processing_template_parmlist
22631 /* Build a location of this form:
22632 struct typename <ARGS>
22633 ^~~~~~~~~~~~~~~~~~~~~~
22634 with caret==start at the start token, and
22635 finishing at the end of the type. */
22636 location_t reported_loc
22637 = make_location (class_head_start_location
,
22638 class_head_start_location
,
22639 get_finish (type_start_token
->location
));
22640 rich_location
richloc (line_table
, reported_loc
);
22641 richloc
.add_fixit_insert_before (class_head_start_location
,
22645 "an explicit specialization must be preceded by %<template <>%>");
22646 invalid_explicit_specialization_p
= true;
22647 /* Take the same action that would have been taken by
22648 cp_parser_explicit_specialization. */
22649 ++parser
->num_template_parameter_lists
;
22650 begin_specialization ();
22652 /* There must be no "return" statements between this point and the
22653 end of this function; set "type "to the correct return value and
22654 use "goto done;" to return. */
22655 /* Make sure that the right number of template parameters were
22657 if (!cp_parser_check_template_parameters (parser
, num_templates
,
22658 type_start_token
->location
,
22659 /*declarator=*/NULL
))
22661 /* If something went wrong, there is no point in even trying to
22662 process the class-definition. */
22667 /* Look up the type. */
22670 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
22671 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
22672 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
22674 error_at (type_start_token
->location
,
22675 "function template %qD redeclared as a class template", id
);
22676 type
= error_mark_node
;
22680 type
= TREE_TYPE (id
);
22681 type
= maybe_process_partial_specialization (type
);
22683 /* Check the scope while we still know whether or not we had a
22684 nested-name-specifier. */
22685 if (type
!= error_mark_node
)
22686 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
22688 if (nested_name_specifier
)
22689 pushed_scope
= push_scope (nested_name_specifier
);
22691 else if (nested_name_specifier
)
22697 template <typename T> struct S { struct T };
22698 template <typename T> struct S<T>::T { };
22700 we will get a TYPENAME_TYPE when processing the definition of
22701 `S::T'. We need to resolve it to the actual type before we
22702 try to define it. */
22703 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
22705 class_type
= resolve_typename_type (TREE_TYPE (type
),
22706 /*only_current_p=*/false);
22707 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
22708 type
= TYPE_NAME (class_type
);
22711 cp_parser_error (parser
, "could not resolve typename type");
22712 type
= error_mark_node
;
22716 if (maybe_process_partial_specialization (TREE_TYPE (type
))
22717 == error_mark_node
)
22723 class_type
= current_class_type
;
22724 /* Enter the scope indicated by the nested-name-specifier. */
22725 pushed_scope
= push_scope (nested_name_specifier
);
22726 /* Get the canonical version of this type. */
22727 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
22728 /* Call push_template_decl if it seems like we should be defining a
22729 template either from the template headers or the type we're
22730 defining, so that we diagnose both extra and missing headers. */
22731 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22732 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
22733 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
22735 type
= push_template_decl (type
);
22736 if (type
== error_mark_node
)
22743 type
= TREE_TYPE (type
);
22744 *nested_name_specifier_p
= true;
22746 else /* The name is not a nested name. */
22748 /* If the class was unnamed, create a dummy name. */
22750 id
= make_anon_name ();
22751 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
22752 ? ts_within_enclosing_non_class
22754 type
= xref_tag (class_key
, id
, tag_scope
,
22755 parser
->num_template_parameter_lists
);
22758 /* Indicate whether this class was declared as a `class' or as a
22760 if (TREE_CODE (type
) == RECORD_TYPE
)
22761 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
22762 cp_parser_check_class_key (class_key
, type
);
22764 /* If this type was already complete, and we see another definition,
22765 that's an error. */
22766 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
22768 error_at (type_start_token
->location
, "redefinition of %q#T",
22770 inform (location_of (type
), "previous definition of %q#T",
22775 else if (type
== error_mark_node
)
22780 /* Apply attributes now, before any use of the class as a template
22781 argument in its base list. */
22782 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
22783 fixup_attribute_variants (type
);
22786 /* We will have entered the scope containing the class; the names of
22787 base classes should be looked up in that context. For example:
22789 struct A { struct B {}; struct C; };
22790 struct A::C : B {};
22794 /* Get the list of base-classes, if there is one. */
22795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22797 /* PR59482: enter the class scope so that base-specifiers are looked
22801 bases
= cp_parser_base_clause (parser
);
22802 /* PR59482: get out of the previously pushed class scope so that the
22803 subsequent pops pop the right thing. */
22810 /* If we're really defining a class, process the base classes.
22811 If they're invalid, fail. */
22812 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22813 xref_basetypes (type
, bases
);
22816 /* Leave the scope given by the nested-name-specifier. We will
22817 enter the class scope itself while processing the members. */
22819 pop_scope (pushed_scope
);
22821 if (invalid_explicit_specialization_p
)
22823 end_specialization ();
22824 --parser
->num_template_parameter_lists
;
22828 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
22829 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
22830 CLASSTYPE_FINAL (type
) = 1;
22832 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22836 /* Parse a class-key.
22843 Returns the kind of class-key specified, or none_type to indicate
22846 static enum tag_types
22847 cp_parser_class_key (cp_parser
* parser
)
22850 enum tag_types tag_type
;
22852 /* Look for the class-key. */
22853 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
22857 /* Check to see if the TOKEN is a class-key. */
22858 tag_type
= cp_parser_token_is_class_key (token
);
22860 cp_parser_error (parser
, "expected class-key");
22864 /* Parse a type-parameter-key.
22866 type-parameter-key:
22872 cp_parser_type_parameter_key (cp_parser
* parser
)
22874 /* Look for the type-parameter-key. */
22875 enum tag_types tag_type
= none_type
;
22876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22877 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
22879 cp_lexer_consume_token (parser
->lexer
);
22880 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
22881 /* typename is not allowed in a template template parameter
22882 by the standard until C++1Z. */
22883 pedwarn (token
->location
, OPT_Wpedantic
,
22884 "ISO C++ forbids typename key in template template parameter;"
22885 " use -std=c++1z or -std=gnu++1z");
22888 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
22893 /* Parse an (optional) member-specification.
22895 member-specification:
22896 member-declaration member-specification [opt]
22897 access-specifier : member-specification [opt] */
22900 cp_parser_member_specification_opt (cp_parser
* parser
)
22907 /* Peek at the next token. */
22908 token
= cp_lexer_peek_token (parser
->lexer
);
22909 /* If it's a `}', or EOF then we've seen all the members. */
22910 if (token
->type
== CPP_CLOSE_BRACE
22911 || token
->type
== CPP_EOF
22912 || token
->type
== CPP_PRAGMA_EOL
)
22915 /* See if this token is a keyword. */
22916 keyword
= token
->keyword
;
22920 case RID_PROTECTED
:
22922 /* Consume the access-specifier. */
22923 cp_lexer_consume_token (parser
->lexer
);
22924 /* Remember which access-specifier is active. */
22925 current_access_specifier
= token
->u
.value
;
22926 /* Look for the `:'. */
22927 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22931 /* Accept #pragmas at class scope. */
22932 if (token
->type
== CPP_PRAGMA
)
22934 cp_parser_pragma (parser
, pragma_member
, NULL
);
22938 /* Otherwise, the next construction must be a
22939 member-declaration. */
22940 cp_parser_member_declaration (parser
);
22945 /* Parse a member-declaration.
22947 member-declaration:
22948 decl-specifier-seq [opt] member-declarator-list [opt] ;
22949 function-definition ; [opt]
22950 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22952 template-declaration
22955 member-declarator-list:
22957 member-declarator-list , member-declarator
22960 declarator pure-specifier [opt]
22961 declarator constant-initializer [opt]
22962 identifier [opt] : constant-expression
22966 member-declaration:
22967 __extension__ member-declaration
22970 declarator attributes [opt] pure-specifier [opt]
22971 declarator attributes [opt] constant-initializer [opt]
22972 identifier [opt] attributes [opt] : constant-expression
22976 member-declaration:
22977 static_assert-declaration */
22980 cp_parser_member_declaration (cp_parser
* parser
)
22982 cp_decl_specifier_seq decl_specifiers
;
22983 tree prefix_attributes
;
22985 int declares_class_or_enum
;
22987 cp_token
*token
= NULL
;
22988 cp_token
*decl_spec_token_start
= NULL
;
22989 cp_token
*initializer_token_start
= NULL
;
22990 int saved_pedantic
;
22991 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22993 /* Check for the `__extension__' keyword. */
22994 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
22997 cp_parser_member_declaration (parser
);
22998 /* Restore the old value of the PEDANTIC flag. */
22999 pedantic
= saved_pedantic
;
23004 /* Check for a template-declaration. */
23005 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23007 /* An explicit specialization here is an error condition, and we
23008 expect the specialization handler to detect and report this. */
23009 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23010 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23011 cp_parser_explicit_specialization (parser
);
23013 cp_parser_template_declaration (parser
, /*member_p=*/true);
23017 /* Check for a template introduction. */
23018 else if (cp_parser_template_declaration_after_export (parser
, true))
23021 /* Check for a using-declaration. */
23022 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23024 if (cxx_dialect
< cxx11
)
23026 /* Parse the using-declaration. */
23027 cp_parser_using_declaration (parser
,
23028 /*access_declaration_p=*/false);
23034 bool alias_decl_expected
;
23035 cp_parser_parse_tentatively (parser
);
23036 decl
= cp_parser_alias_declaration (parser
);
23037 /* Note that if we actually see the '=' token after the
23038 identifier, cp_parser_alias_declaration commits the
23039 tentative parse. In that case, we really expect an
23040 alias-declaration. Otherwise, we expect a using
23042 alias_decl_expected
=
23043 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23044 cp_parser_parse_definitely (parser
);
23046 if (alias_decl_expected
)
23047 finish_member_declaration (decl
);
23049 cp_parser_using_declaration (parser
,
23050 /*access_declaration_p=*/false);
23055 /* Check for @defs. */
23056 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23059 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23060 ivar
= ivar_chains
;
23064 ivar
= TREE_CHAIN (member
);
23065 TREE_CHAIN (member
) = NULL_TREE
;
23066 finish_member_declaration (member
);
23071 /* If the next token is `static_assert' we have a static assertion. */
23072 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23074 cp_parser_static_assert (parser
, /*member_p=*/true);
23078 parser
->colon_corrects_to_scope_p
= false;
23080 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23083 /* Parse the decl-specifier-seq. */
23084 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23085 cp_parser_decl_specifier_seq (parser
,
23086 CP_PARSER_FLAGS_OPTIONAL
,
23088 &declares_class_or_enum
);
23089 /* Check for an invalid type-name. */
23090 if (!decl_specifiers
.any_type_specifiers_p
23091 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23093 /* If there is no declarator, then the decl-specifier-seq should
23095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23097 /* If there was no decl-specifier-seq, and the next token is a
23098 `;', then we have something like:
23104 Each member-declaration shall declare at least one member
23105 name of the class. */
23106 if (!decl_specifiers
.any_specifiers_p
)
23108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23109 if (!in_system_header_at (token
->location
))
23110 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
23116 /* See if this declaration is a friend. */
23117 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23118 /* If there were decl-specifiers, check to see if there was
23119 a class-declaration. */
23120 type
= check_tag_decl (&decl_specifiers
,
23121 /*explicit_type_instantiation_p=*/false);
23122 /* Nested classes have already been added to the class, but
23123 a `friend' needs to be explicitly registered. */
23126 /* If the `friend' keyword was present, the friend must
23127 be introduced with a class-key. */
23128 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23129 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23130 "in C++03 a class-key must be used "
23131 "when declaring a friend");
23134 template <typename T> struct A {
23135 friend struct A<T>::B;
23138 A<T>::B will be represented by a TYPENAME_TYPE, and
23139 therefore not recognized by check_tag_decl. */
23142 type
= decl_specifiers
.type
;
23143 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23144 type
= TREE_TYPE (type
);
23146 if (!type
|| !TYPE_P (type
))
23147 error_at (decl_spec_token_start
->location
,
23148 "friend declaration does not name a class or "
23151 make_friend_class (current_class_type
, type
,
23152 /*complain=*/true);
23154 /* If there is no TYPE, an error message will already have
23156 else if (!type
|| type
== error_mark_node
)
23158 /* An anonymous aggregate has to be handled specially; such
23159 a declaration really declares a data member (with a
23160 particular type), as opposed to a nested class. */
23161 else if (ANON_AGGR_TYPE_P (type
))
23164 if (decl_specifiers
.storage_class
!= sc_none
)
23165 error_at (decl_spec_token_start
->location
,
23166 "a storage class on an anonymous aggregate "
23167 "in class scope is not allowed");
23169 /* Remove constructors and such from TYPE, now that we
23170 know it is an anonymous aggregate. */
23171 fixup_anonymous_aggr (type
);
23172 /* And make the corresponding data member. */
23173 decl
= build_decl (decl_spec_token_start
->location
,
23174 FIELD_DECL
, NULL_TREE
, type
);
23175 /* Add it to the class. */
23176 finish_member_declaration (decl
);
23179 cp_parser_check_access_in_redeclaration
23181 decl_spec_token_start
->location
);
23186 bool assume_semicolon
= false;
23188 /* Clear attributes from the decl_specifiers but keep them
23189 around as prefix attributes that apply them to the entity
23191 prefix_attributes
= decl_specifiers
.attributes
;
23192 decl_specifiers
.attributes
= NULL_TREE
;
23194 /* See if these declarations will be friends. */
23195 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23197 /* Keep going until we hit the `;' at the end of the
23199 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23201 tree attributes
= NULL_TREE
;
23202 tree first_attribute
;
23204 /* Peek at the next token. */
23205 token
= cp_lexer_peek_token (parser
->lexer
);
23207 /* Check for a bitfield declaration. */
23208 if (token
->type
== CPP_COLON
23209 || (token
->type
== CPP_NAME
23210 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23216 /* Get the name of the bitfield. Note that we cannot just
23217 check TOKEN here because it may have been invalidated by
23218 the call to cp_lexer_peek_nth_token above. */
23219 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
23220 identifier
= cp_parser_identifier (parser
);
23222 identifier
= NULL_TREE
;
23224 /* Consume the `:' token. */
23225 cp_lexer_consume_token (parser
->lexer
);
23226 /* Get the width of the bitfield. */
23228 = cp_parser_constant_expression (parser
);
23230 /* Look for attributes that apply to the bitfield. */
23231 attributes
= cp_parser_attributes_opt (parser
);
23232 /* Remember which attributes are prefix attributes and
23234 first_attribute
= attributes
;
23235 /* Combine the attributes. */
23236 attributes
= chainon (prefix_attributes
, attributes
);
23238 /* Create the bitfield declaration. */
23239 decl
= grokbitfield (identifier
23240 ? make_id_declarator (NULL_TREE
,
23250 cp_declarator
*declarator
;
23252 tree asm_specification
;
23253 int ctor_dtor_or_conv_p
;
23255 /* Parse the declarator. */
23257 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23258 &ctor_dtor_or_conv_p
,
23259 /*parenthesized_p=*/NULL
,
23263 /* If something went wrong parsing the declarator, make sure
23264 that we at least consume some tokens. */
23265 if (declarator
== cp_error_declarator
)
23267 /* Skip to the end of the statement. */
23268 cp_parser_skip_to_end_of_statement (parser
);
23269 /* If the next token is not a semicolon, that is
23270 probably because we just skipped over the body of
23271 a function. So, we consume a semicolon if
23272 present, but do not issue an error message if it
23274 if (cp_lexer_next_token_is (parser
->lexer
,
23276 cp_lexer_consume_token (parser
->lexer
);
23280 if (declares_class_or_enum
& 2)
23281 cp_parser_check_for_definition_in_return_type
23282 (declarator
, decl_specifiers
.type
,
23283 decl_specifiers
.locations
[ds_type_spec
]);
23285 /* Look for an asm-specification. */
23286 asm_specification
= cp_parser_asm_specification_opt (parser
);
23287 /* Look for attributes that apply to the declaration. */
23288 attributes
= cp_parser_attributes_opt (parser
);
23289 /* Remember which attributes are prefix attributes and
23291 first_attribute
= attributes
;
23292 /* Combine the attributes. */
23293 attributes
= chainon (prefix_attributes
, attributes
);
23295 /* If it's an `=', then we have a constant-initializer or a
23296 pure-specifier. It is not correct to parse the
23297 initializer before registering the member declaration
23298 since the member declaration should be in scope while
23299 its initializer is processed. However, the rest of the
23300 front end does not yet provide an interface that allows
23301 us to handle this correctly. */
23302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23306 A pure-specifier shall be used only in the declaration of
23307 a virtual function.
23309 A member-declarator can contain a constant-initializer
23310 only if it declares a static member of integral or
23313 Therefore, if the DECLARATOR is for a function, we look
23314 for a pure-specifier; otherwise, we look for a
23315 constant-initializer. When we call `grokfield', it will
23316 perform more stringent semantics checks. */
23317 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23318 if (function_declarator_p (declarator
)
23319 || (decl_specifiers
.type
23320 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23321 && declarator
->kind
== cdk_id
23322 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23323 == FUNCTION_TYPE
)))
23324 initializer
= cp_parser_pure_specifier (parser
);
23325 else if (decl_specifiers
.storage_class
!= sc_static
)
23326 initializer
= cp_parser_save_nsdmi (parser
);
23327 else if (cxx_dialect
>= cxx11
)
23330 /* Don't require a constant rvalue in C++11, since we
23331 might want a reference constant. We'll enforce
23332 constancy later. */
23333 cp_lexer_consume_token (parser
->lexer
);
23334 /* Parse the initializer. */
23335 initializer
= cp_parser_initializer_clause (parser
,
23339 /* Parse the initializer. */
23340 initializer
= cp_parser_constant_initializer (parser
);
23342 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23343 && !function_declarator_p (declarator
))
23346 if (decl_specifiers
.storage_class
!= sc_static
)
23347 initializer
= cp_parser_save_nsdmi (parser
);
23349 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23351 /* Otherwise, there is no initializer. */
23353 initializer
= NULL_TREE
;
23355 /* See if we are probably looking at a function
23356 definition. We are certainly not looking at a
23357 member-declarator. Calling `grokfield' has
23358 side-effects, so we must not do it unless we are sure
23359 that we are looking at a member-declarator. */
23360 if (cp_parser_token_starts_function_definition_p
23361 (cp_lexer_peek_token (parser
->lexer
)))
23363 /* The grammar does not allow a pure-specifier to be
23364 used when a member function is defined. (It is
23365 possible that this fact is an oversight in the
23366 standard, since a pure function may be defined
23367 outside of the class-specifier. */
23368 if (initializer
&& initializer_token_start
)
23369 error_at (initializer_token_start
->location
,
23370 "pure-specifier on function-definition");
23371 decl
= cp_parser_save_member_function_body (parser
,
23375 if (parser
->fully_implicit_function_template_p
)
23376 decl
= finish_fully_implicit_template (parser
, decl
);
23377 /* If the member was not a friend, declare it here. */
23379 finish_member_declaration (decl
);
23380 /* Peek at the next token. */
23381 token
= cp_lexer_peek_token (parser
->lexer
);
23382 /* If the next token is a semicolon, consume it. */
23383 if (token
->type
== CPP_SEMICOLON
)
23384 cp_lexer_consume_token (parser
->lexer
);
23388 if (declarator
->kind
== cdk_function
)
23389 declarator
->id_loc
= token
->location
;
23390 /* Create the declaration. */
23391 decl
= grokfield (declarator
, &decl_specifiers
,
23392 initializer
, /*init_const_expr_p=*/true,
23393 asm_specification
, attributes
);
23394 if (parser
->fully_implicit_function_template_p
)
23397 finish_fully_implicit_template (parser
, 0);
23399 decl
= finish_fully_implicit_template (parser
, decl
);
23403 cp_finalize_omp_declare_simd (parser
, decl
);
23404 cp_finalize_oacc_routine (parser
, decl
, false);
23406 /* Reset PREFIX_ATTRIBUTES. */
23407 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23408 attributes
= TREE_CHAIN (attributes
);
23410 TREE_CHAIN (attributes
) = NULL_TREE
;
23412 /* If there is any qualification still in effect, clear it
23413 now; we will be starting fresh with the next declarator. */
23414 parser
->scope
= NULL_TREE
;
23415 parser
->qualifying_scope
= NULL_TREE
;
23416 parser
->object_scope
= NULL_TREE
;
23417 /* If it's a `,', then there are more declarators. */
23418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23420 cp_lexer_consume_token (parser
->lexer
);
23421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23423 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23424 error_at (token
->location
,
23425 "stray %<,%> at end of member declaration");
23428 /* If the next token isn't a `;', then we have a parse error. */
23429 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23432 /* The next token might be a ways away from where the
23433 actual semicolon is missing. Find the previous token
23434 and use that for our error position. */
23435 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23436 error_at (token
->location
,
23437 "expected %<;%> at end of member declaration");
23439 /* Assume that the user meant to provide a semicolon. If
23440 we were to cp_parser_skip_to_end_of_statement, we might
23441 skip to a semicolon inside a member function definition
23442 and issue nonsensical error messages. */
23443 assume_semicolon
= true;
23448 /* Add DECL to the list of members. */
23450 /* Explicitly include, eg, NSDMIs, for better error
23451 recovery (c++/58650). */
23452 || !DECL_DECLARES_FUNCTION_P (decl
))
23453 finish_member_declaration (decl
);
23455 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23456 cp_parser_save_default_args (parser
, decl
);
23457 else if (TREE_CODE (decl
) == FIELD_DECL
23458 && !DECL_C_BIT_FIELD (decl
)
23459 && DECL_INITIAL (decl
))
23460 /* Add DECL to the queue of NSDMI to be parsed later. */
23461 vec_safe_push (unparsed_nsdmis
, decl
);
23464 if (assume_semicolon
)
23469 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23471 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23474 /* Parse a pure-specifier.
23479 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23480 Otherwise, ERROR_MARK_NODE is returned. */
23483 cp_parser_pure_specifier (cp_parser
* parser
)
23487 /* Look for the `=' token. */
23488 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23489 return error_mark_node
;
23490 /* Look for the `0' token. */
23491 token
= cp_lexer_peek_token (parser
->lexer
);
23493 if (token
->type
== CPP_EOF
23494 || token
->type
== CPP_PRAGMA_EOL
)
23495 return error_mark_node
;
23497 cp_lexer_consume_token (parser
->lexer
);
23499 /* Accept = default or = delete in c++0x mode. */
23500 if (token
->keyword
== RID_DEFAULT
23501 || token
->keyword
== RID_DELETE
)
23503 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23504 return token
->u
.value
;
23507 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23508 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23510 cp_parser_error (parser
,
23511 "invalid pure specifier (only %<= 0%> is allowed)");
23512 cp_parser_skip_to_end_of_statement (parser
);
23513 return error_mark_node
;
23515 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23517 error_at (token
->location
, "templates may not be %<virtual%>");
23518 return error_mark_node
;
23521 return integer_zero_node
;
23524 /* Parse a constant-initializer.
23526 constant-initializer:
23527 = constant-expression
23529 Returns a representation of the constant-expression. */
23532 cp_parser_constant_initializer (cp_parser
* parser
)
23534 /* Look for the `=' token. */
23535 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23536 return error_mark_node
;
23538 /* It is invalid to write:
23540 struct S { static const int i = { 7 }; };
23543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23545 cp_parser_error (parser
,
23546 "a brace-enclosed initializer is not allowed here");
23547 /* Consume the opening brace. */
23548 cp_lexer_consume_token (parser
->lexer
);
23549 /* Skip the initializer. */
23550 cp_parser_skip_to_closing_brace (parser
);
23551 /* Look for the trailing `}'. */
23552 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
23554 return error_mark_node
;
23557 return cp_parser_constant_expression (parser
);
23560 /* Derived classes [gram.class.derived] */
23562 /* Parse a base-clause.
23565 : base-specifier-list
23567 base-specifier-list:
23568 base-specifier ... [opt]
23569 base-specifier-list , base-specifier ... [opt]
23571 Returns a TREE_LIST representing the base-classes, in the order in
23572 which they were declared. The representation of each node is as
23573 described by cp_parser_base_specifier.
23575 In the case that no bases are specified, this function will return
23576 NULL_TREE, not ERROR_MARK_NODE. */
23579 cp_parser_base_clause (cp_parser
* parser
)
23581 tree bases
= NULL_TREE
;
23583 /* Look for the `:' that begins the list. */
23584 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23586 /* Scan the base-specifier-list. */
23591 bool pack_expansion_p
= false;
23593 /* Look for the base-specifier. */
23594 base
= cp_parser_base_specifier (parser
);
23595 /* Look for the (optional) ellipsis. */
23596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23598 /* Consume the `...'. */
23599 cp_lexer_consume_token (parser
->lexer
);
23601 pack_expansion_p
= true;
23604 /* Add BASE to the front of the list. */
23605 if (base
&& base
!= error_mark_node
)
23607 if (pack_expansion_p
)
23608 /* Make this a pack expansion type. */
23609 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
23611 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
23613 TREE_CHAIN (base
) = bases
;
23617 /* Peek at the next token. */
23618 token
= cp_lexer_peek_token (parser
->lexer
);
23619 /* If it's not a comma, then the list is complete. */
23620 if (token
->type
!= CPP_COMMA
)
23622 /* Consume the `,'. */
23623 cp_lexer_consume_token (parser
->lexer
);
23626 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23627 base class had a qualified name. However, the next name that
23628 appears is certainly not qualified. */
23629 parser
->scope
= NULL_TREE
;
23630 parser
->qualifying_scope
= NULL_TREE
;
23631 parser
->object_scope
= NULL_TREE
;
23633 return nreverse (bases
);
23636 /* Parse a base-specifier.
23639 :: [opt] nested-name-specifier [opt] class-name
23640 virtual access-specifier [opt] :: [opt] nested-name-specifier
23642 access-specifier virtual [opt] :: [opt] nested-name-specifier
23645 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23646 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23647 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23648 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23651 cp_parser_base_specifier (cp_parser
* parser
)
23655 bool virtual_p
= false;
23656 bool duplicate_virtual_error_issued_p
= false;
23657 bool duplicate_access_error_issued_p
= false;
23658 bool class_scope_p
, template_p
;
23659 tree access
= access_default_node
;
23662 /* Process the optional `virtual' and `access-specifier'. */
23665 /* Peek at the next token. */
23666 token
= cp_lexer_peek_token (parser
->lexer
);
23667 /* Process `virtual'. */
23668 switch (token
->keyword
)
23671 /* If `virtual' appears more than once, issue an error. */
23672 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
23674 cp_parser_error (parser
,
23675 "%<virtual%> specified more than once in base-specified");
23676 duplicate_virtual_error_issued_p
= true;
23681 /* Consume the `virtual' token. */
23682 cp_lexer_consume_token (parser
->lexer
);
23687 case RID_PROTECTED
:
23689 /* If more than one access specifier appears, issue an
23691 if (access
!= access_default_node
23692 && !duplicate_access_error_issued_p
)
23694 cp_parser_error (parser
,
23695 "more than one access specifier in base-specified");
23696 duplicate_access_error_issued_p
= true;
23699 access
= ridpointers
[(int) token
->keyword
];
23701 /* Consume the access-specifier. */
23702 cp_lexer_consume_token (parser
->lexer
);
23711 /* It is not uncommon to see programs mechanically, erroneously, use
23712 the 'typename' keyword to denote (dependent) qualified types
23713 as base classes. */
23714 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23716 token
= cp_lexer_peek_token (parser
->lexer
);
23717 if (!processing_template_decl
)
23718 error_at (token
->location
,
23719 "keyword %<typename%> not allowed outside of templates");
23721 error_at (token
->location
,
23722 "keyword %<typename%> not allowed in this context "
23723 "(the base class is implicitly a type)");
23724 cp_lexer_consume_token (parser
->lexer
);
23727 /* Look for the optional `::' operator. */
23728 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
23729 /* Look for the nested-name-specifier. The simplest way to
23734 The keyword `typename' is not permitted in a base-specifier or
23735 mem-initializer; in these contexts a qualified name that
23736 depends on a template-parameter is implicitly assumed to be a
23739 is to pretend that we have seen the `typename' keyword at this
23741 cp_parser_nested_name_specifier_opt (parser
,
23742 /*typename_keyword_p=*/true,
23743 /*check_dependency_p=*/true,
23745 /*is_declaration=*/true);
23746 /* If the base class is given by a qualified name, assume that names
23747 we see are type names or templates, as appropriate. */
23748 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
23749 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
23752 && cp_lexer_next_token_is_decltype (parser
->lexer
))
23753 /* DR 950 allows decltype as a base-specifier. */
23754 type
= cp_parser_decltype (parser
);
23757 /* Otherwise, look for the class-name. */
23758 type
= cp_parser_class_name (parser
,
23762 /*check_dependency_p=*/true,
23763 /*class_head_p=*/false,
23764 /*is_declaration=*/true);
23765 type
= TREE_TYPE (type
);
23768 if (type
== error_mark_node
)
23769 return error_mark_node
;
23771 return finish_base_specifier (type
, access
, virtual_p
);
23774 /* Exception handling [gram.exception] */
23776 /* Parse an (optional) noexcept-specification.
23778 noexcept-specification:
23779 noexcept ( constant-expression ) [opt]
23781 If no noexcept-specification is present, returns NULL_TREE.
23782 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23783 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23784 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23785 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23786 in which case a boolean condition is returned instead. */
23789 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
23790 bool require_constexpr
,
23791 bool* consumed_expr
,
23795 const char *saved_message
;
23797 /* Peek at the next token. */
23798 token
= cp_lexer_peek_token (parser
->lexer
);
23800 /* Is it a noexcept-specification? */
23801 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
23804 cp_lexer_consume_token (parser
->lexer
);
23806 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
23808 cp_lexer_consume_token (parser
->lexer
);
23810 if (require_constexpr
)
23812 /* Types may not be defined in an exception-specification. */
23813 saved_message
= parser
->type_definition_forbidden_message
;
23814 parser
->type_definition_forbidden_message
23815 = G_("types may not be defined in an exception-specification");
23817 expr
= cp_parser_constant_expression (parser
);
23819 /* Restore the saved message. */
23820 parser
->type_definition_forbidden_message
= saved_message
;
23824 expr
= cp_parser_expression (parser
);
23825 *consumed_expr
= true;
23828 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23832 expr
= boolean_true_node
;
23833 if (!require_constexpr
)
23834 *consumed_expr
= false;
23837 /* We cannot build a noexcept-spec right away because this will check
23838 that expr is a constexpr. */
23840 return build_noexcept_spec (expr
, tf_warning_or_error
);
23848 /* Parse an (optional) exception-specification.
23850 exception-specification:
23851 throw ( type-id-list [opt] )
23853 Returns a TREE_LIST representing the exception-specification. The
23854 TREE_VALUE of each node is a type. */
23857 cp_parser_exception_specification_opt (cp_parser
* parser
)
23861 const char *saved_message
;
23863 /* Peek at the next token. */
23864 token
= cp_lexer_peek_token (parser
->lexer
);
23866 /* Is it a noexcept-specification? */
23867 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
23869 if (type_id_list
!= NULL_TREE
)
23870 return type_id_list
;
23872 /* If it's not `throw', then there's no exception-specification. */
23873 if (!cp_parser_is_keyword (token
, RID_THROW
))
23876 location_t loc
= token
->location
;
23878 /* Consume the `throw'. */
23879 cp_lexer_consume_token (parser
->lexer
);
23881 /* Look for the `('. */
23882 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23884 /* Peek at the next token. */
23885 token
= cp_lexer_peek_token (parser
->lexer
);
23886 /* If it's not a `)', then there is a type-id-list. */
23887 if (token
->type
!= CPP_CLOSE_PAREN
)
23889 /* Types may not be defined in an exception-specification. */
23890 saved_message
= parser
->type_definition_forbidden_message
;
23891 parser
->type_definition_forbidden_message
23892 = G_("types may not be defined in an exception-specification");
23893 /* Parse the type-id-list. */
23894 type_id_list
= cp_parser_type_id_list (parser
);
23895 /* Restore the saved message. */
23896 parser
->type_definition_forbidden_message
= saved_message
;
23898 if (cxx_dialect
>= cxx1z
)
23900 error_at (loc
, "ISO C++1z does not allow dynamic exception "
23902 type_id_list
= NULL_TREE
;
23904 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
23905 warning_at (loc
, OPT_Wdeprecated
,
23906 "dynamic exception specifications are deprecated in "
23909 /* In C++17, throw() is equivalent to noexcept (true). throw()
23910 is deprecated in C++11 and above as well, but is still widely used,
23911 so don't warn about it yet. */
23912 else if (cxx_dialect
>= cxx1z
)
23913 type_id_list
= noexcept_true_spec
;
23915 type_id_list
= empty_except_spec
;
23917 /* Look for the `)'. */
23918 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23920 return type_id_list
;
23923 /* Parse an (optional) type-id-list.
23927 type-id-list , type-id ... [opt]
23929 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23930 in the order that the types were presented. */
23933 cp_parser_type_id_list (cp_parser
* parser
)
23935 tree types
= NULL_TREE
;
23942 token
= cp_lexer_peek_token (parser
->lexer
);
23944 /* Get the next type-id. */
23945 type
= cp_parser_type_id (parser
);
23946 /* Check for invalid 'auto'. */
23947 if (flag_concepts
&& type_uses_auto (type
))
23949 error_at (token
->location
,
23950 "invalid use of %<auto%> in exception-specification");
23951 type
= error_mark_node
;
23953 /* Parse the optional ellipsis. */
23954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23956 /* Consume the `...'. */
23957 cp_lexer_consume_token (parser
->lexer
);
23959 /* Turn the type into a pack expansion expression. */
23960 type
= make_pack_expansion (type
);
23962 /* Add it to the list. */
23963 types
= add_exception_specifier (types
, type
, /*complain=*/1);
23964 /* Peek at the next token. */
23965 token
= cp_lexer_peek_token (parser
->lexer
);
23966 /* If it is not a `,', we are done. */
23967 if (token
->type
!= CPP_COMMA
)
23969 /* Consume the `,'. */
23970 cp_lexer_consume_token (parser
->lexer
);
23973 return nreverse (types
);
23976 /* Parse a try-block.
23979 try compound-statement handler-seq */
23982 cp_parser_try_block (cp_parser
* parser
)
23986 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
23987 if (parser
->in_function_body
23988 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
23989 error ("%<try%> in %<constexpr%> function");
23991 try_block
= begin_try_block ();
23992 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
23993 finish_try_block (try_block
);
23994 cp_parser_handler_seq (parser
);
23995 finish_handler_sequence (try_block
);
24000 /* Parse a function-try-block.
24002 function-try-block:
24003 try ctor-initializer [opt] function-body handler-seq */
24006 cp_parser_function_try_block (cp_parser
* parser
)
24008 tree compound_stmt
;
24010 bool ctor_initializer_p
;
24012 /* Look for the `try' keyword. */
24013 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24015 /* Let the rest of the front end know where we are. */
24016 try_block
= begin_function_try_block (&compound_stmt
);
24017 /* Parse the function-body. */
24018 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
24019 (parser
, /*in_function_try_block=*/true);
24020 /* We're done with the `try' part. */
24021 finish_function_try_block (try_block
);
24022 /* Parse the handlers. */
24023 cp_parser_handler_seq (parser
);
24024 /* We're done with the handlers. */
24025 finish_function_handler_sequence (try_block
, compound_stmt
);
24027 return ctor_initializer_p
;
24030 /* Parse a handler-seq.
24033 handler handler-seq [opt] */
24036 cp_parser_handler_seq (cp_parser
* parser
)
24042 /* Parse the handler. */
24043 cp_parser_handler (parser
);
24044 /* Peek at the next token. */
24045 token
= cp_lexer_peek_token (parser
->lexer
);
24046 /* If it's not `catch' then there are no more handlers. */
24047 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24052 /* Parse a handler.
24055 catch ( exception-declaration ) compound-statement */
24058 cp_parser_handler (cp_parser
* parser
)
24063 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24064 handler
= begin_handler ();
24065 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24066 declaration
= cp_parser_exception_declaration (parser
);
24067 finish_handler_parms (declaration
, handler
);
24068 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24069 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24070 finish_handler (handler
);
24073 /* Parse an exception-declaration.
24075 exception-declaration:
24076 type-specifier-seq declarator
24077 type-specifier-seq abstract-declarator
24081 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24082 ellipsis variant is used. */
24085 cp_parser_exception_declaration (cp_parser
* parser
)
24087 cp_decl_specifier_seq type_specifiers
;
24088 cp_declarator
*declarator
;
24089 const char *saved_message
;
24091 /* If it's an ellipsis, it's easy to handle. */
24092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24094 /* Consume the `...' token. */
24095 cp_lexer_consume_token (parser
->lexer
);
24099 /* Types may not be defined in exception-declarations. */
24100 saved_message
= parser
->type_definition_forbidden_message
;
24101 parser
->type_definition_forbidden_message
24102 = G_("types may not be defined in exception-declarations");
24104 /* Parse the type-specifier-seq. */
24105 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24106 /*is_trailing_return=*/false,
24108 /* If it's a `)', then there is no declarator. */
24109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24112 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24113 /*ctor_dtor_or_conv_p=*/NULL
,
24114 /*parenthesized_p=*/NULL
,
24115 /*member_p=*/false,
24116 /*friend_p=*/false);
24118 /* Restore the saved message. */
24119 parser
->type_definition_forbidden_message
= saved_message
;
24121 if (!type_specifiers
.any_specifiers_p
)
24122 return error_mark_node
;
24124 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24127 /* Parse a throw-expression.
24130 throw assignment-expression [opt]
24132 Returns a THROW_EXPR representing the throw-expression. */
24135 cp_parser_throw_expression (cp_parser
* parser
)
24140 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24141 token
= cp_lexer_peek_token (parser
->lexer
);
24142 /* Figure out whether or not there is an assignment-expression
24143 following the "throw" keyword. */
24144 if (token
->type
== CPP_COMMA
24145 || token
->type
== CPP_SEMICOLON
24146 || token
->type
== CPP_CLOSE_PAREN
24147 || token
->type
== CPP_CLOSE_SQUARE
24148 || token
->type
== CPP_CLOSE_BRACE
24149 || token
->type
== CPP_COLON
)
24150 expression
= NULL_TREE
;
24152 expression
= cp_parser_assignment_expression (parser
);
24154 return build_throw (expression
);
24157 /* GNU Extensions */
24159 /* Parse an (optional) asm-specification.
24162 asm ( string-literal )
24164 If the asm-specification is present, returns a STRING_CST
24165 corresponding to the string-literal. Otherwise, returns
24169 cp_parser_asm_specification_opt (cp_parser
* parser
)
24172 tree asm_specification
;
24174 /* Peek at the next token. */
24175 token
= cp_lexer_peek_token (parser
->lexer
);
24176 /* If the next token isn't the `asm' keyword, then there's no
24177 asm-specification. */
24178 if (!cp_parser_is_keyword (token
, RID_ASM
))
24181 /* Consume the `asm' token. */
24182 cp_lexer_consume_token (parser
->lexer
);
24183 /* Look for the `('. */
24184 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24186 /* Look for the string-literal. */
24187 asm_specification
= cp_parser_string_literal (parser
, false, false);
24189 /* Look for the `)'. */
24190 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24192 return asm_specification
;
24195 /* Parse an asm-operand-list.
24199 asm-operand-list , asm-operand
24202 string-literal ( expression )
24203 [ string-literal ] string-literal ( expression )
24205 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24206 each node is the expression. The TREE_PURPOSE is itself a
24207 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24208 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24209 is a STRING_CST for the string literal before the parenthesis. Returns
24210 ERROR_MARK_NODE if any of the operands are invalid. */
24213 cp_parser_asm_operand_list (cp_parser
* parser
)
24215 tree asm_operands
= NULL_TREE
;
24216 bool invalid_operands
= false;
24220 tree string_literal
;
24224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24226 /* Consume the `[' token. */
24227 cp_lexer_consume_token (parser
->lexer
);
24228 /* Read the operand name. */
24229 name
= cp_parser_identifier (parser
);
24230 if (name
!= error_mark_node
)
24231 name
= build_string (IDENTIFIER_LENGTH (name
),
24232 IDENTIFIER_POINTER (name
));
24233 /* Look for the closing `]'. */
24234 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24238 /* Look for the string-literal. */
24239 string_literal
= cp_parser_string_literal (parser
, false, false);
24241 /* Look for the `('. */
24242 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24243 /* Parse the expression. */
24244 expression
= cp_parser_expression (parser
);
24245 /* Look for the `)'. */
24246 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24248 if (name
== error_mark_node
24249 || string_literal
== error_mark_node
24250 || expression
== error_mark_node
)
24251 invalid_operands
= true;
24253 /* Add this operand to the list. */
24254 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24257 /* If the next token is not a `,', there are no more
24259 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24261 /* Consume the `,'. */
24262 cp_lexer_consume_token (parser
->lexer
);
24265 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24268 /* Parse an asm-clobber-list.
24272 asm-clobber-list , string-literal
24274 Returns a TREE_LIST, indicating the clobbers in the order that they
24275 appeared. The TREE_VALUE of each node is a STRING_CST. */
24278 cp_parser_asm_clobber_list (cp_parser
* parser
)
24280 tree clobbers
= NULL_TREE
;
24284 tree string_literal
;
24286 /* Look for the string literal. */
24287 string_literal
= cp_parser_string_literal (parser
, false, false);
24288 /* Add it to the list. */
24289 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24290 /* If the next token is not a `,', then the list is
24292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24294 /* Consume the `,' token. */
24295 cp_lexer_consume_token (parser
->lexer
);
24301 /* Parse an asm-label-list.
24305 asm-label-list , identifier
24307 Returns a TREE_LIST, indicating the labels in the order that they
24308 appeared. The TREE_VALUE of each node is a label. */
24311 cp_parser_asm_label_list (cp_parser
* parser
)
24313 tree labels
= NULL_TREE
;
24317 tree identifier
, label
, name
;
24319 /* Look for the identifier. */
24320 identifier
= cp_parser_identifier (parser
);
24321 if (!error_operand_p (identifier
))
24323 label
= lookup_label (identifier
);
24324 if (TREE_CODE (label
) == LABEL_DECL
)
24326 TREE_USED (label
) = 1;
24327 check_goto (label
);
24328 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24329 IDENTIFIER_POINTER (identifier
));
24330 labels
= tree_cons (name
, label
, labels
);
24333 /* If the next token is not a `,', then the list is
24335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24337 /* Consume the `,' token. */
24338 cp_lexer_consume_token (parser
->lexer
);
24341 return nreverse (labels
);
24344 /* Return TRUE iff the next tokens in the stream are possibly the
24345 beginning of a GNU extension attribute. */
24348 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24350 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24353 /* Return TRUE iff the next tokens in the stream are possibly the
24354 beginning of a standard C++-11 attribute specifier. */
24357 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24359 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24362 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24363 beginning of a standard C++-11 attribute specifier. */
24366 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24368 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24370 return (cxx_dialect
>= cxx11
24371 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24372 || (token
->type
== CPP_OPEN_SQUARE
24373 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24374 && token
->type
== CPP_OPEN_SQUARE
)));
24377 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24378 beginning of a GNU extension attribute. */
24381 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24383 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24385 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24388 /* Return true iff the next tokens can be the beginning of either a
24389 GNU attribute list, or a standard C++11 attribute sequence. */
24392 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24394 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24395 || cp_next_tokens_can_be_std_attribute_p (parser
));
24398 /* Return true iff the next Nth tokens can be the beginning of either
24399 a GNU attribute list, or a standard C++11 attribute sequence. */
24402 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24404 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24405 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24408 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24409 of GNU attributes, or return NULL. */
24412 cp_parser_attributes_opt (cp_parser
*parser
)
24414 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24415 return cp_parser_gnu_attributes_opt (parser
);
24416 return cp_parser_std_attribute_spec_seq (parser
);
24419 #define CILK_SIMD_FN_CLAUSE_MASK \
24420 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24421 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24422 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24423 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24424 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24426 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24427 vector [(<clauses>)] */
24430 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
24432 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
24433 cp_token
*token
= v_token
;
24436 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
24437 parser
->cilk_simd_fn_info
->error_seen
= false;
24438 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
24439 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
24440 parser
->cilk_simd_fn_info
->clauses
= NULL_TREE
;
24442 int paren_scope
= 0;
24443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24445 cp_lexer_consume_token (parser
->lexer
);
24446 v_token
= cp_lexer_peek_token (parser
->lexer
);
24449 while (paren_scope
> 0)
24451 token
= cp_lexer_peek_token (parser
->lexer
);
24452 if (token
->type
== CPP_OPEN_PAREN
)
24454 else if (token
->type
== CPP_CLOSE_PAREN
)
24456 /* Do not push the last ')' */
24457 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
24458 cp_lexer_consume_token (parser
->lexer
);
24461 token
->type
= CPP_PRAGMA_EOL
;
24462 parser
->lexer
->next_token
= token
;
24463 cp_lexer_consume_token (parser
->lexer
);
24465 struct cp_token_cache
*cp
24466 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
24467 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
24470 /* Parse an (optional) series of attributes.
24473 attributes attribute
24476 __attribute__ (( attribute-list [opt] ))
24478 The return value is as for cp_parser_gnu_attribute_list. */
24481 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24483 tree attributes
= NULL_TREE
;
24488 tree attribute_list
;
24491 /* Peek at the next token. */
24492 token
= cp_lexer_peek_token (parser
->lexer
);
24493 /* If it's not `__attribute__', then we're done. */
24494 if (token
->keyword
!= RID_ATTRIBUTE
)
24497 /* Consume the `__attribute__' keyword. */
24498 cp_lexer_consume_token (parser
->lexer
);
24499 /* Look for the two `(' tokens. */
24500 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24501 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24503 /* Peek at the next token. */
24504 token
= cp_lexer_peek_token (parser
->lexer
);
24505 if (token
->type
!= CPP_CLOSE_PAREN
)
24506 /* Parse the attribute-list. */
24507 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24509 /* If the next token is a `)', then there is no attribute
24511 attribute_list
= NULL
;
24513 /* Look for the two `)' tokens. */
24514 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24516 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24519 cp_parser_skip_to_end_of_statement (parser
);
24521 /* Add these new attributes to the list. */
24522 attributes
= chainon (attributes
, attribute_list
);
24528 /* Parse a GNU attribute-list.
24532 attribute-list , attribute
24536 identifier ( identifier )
24537 identifier ( identifier , expression-list )
24538 identifier ( expression-list )
24540 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24541 to an attribute. The TREE_PURPOSE of each node is the identifier
24542 indicating which attribute is in use. The TREE_VALUE represents
24543 the arguments, if any. */
24546 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24548 tree attribute_list
= NULL_TREE
;
24549 bool save_translate_strings_p
= parser
->translate_strings_p
;
24551 parser
->translate_strings_p
= false;
24558 /* Look for the identifier. We also allow keywords here; for
24559 example `__attribute__ ((const))' is legal. */
24560 token
= cp_lexer_peek_token (parser
->lexer
);
24561 if (token
->type
== CPP_NAME
24562 || token
->type
== CPP_KEYWORD
)
24564 tree arguments
= NULL_TREE
;
24566 /* Consume the token, but save it since we need it for the
24567 SIMD enabled function parsing. */
24568 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24570 /* Save away the identifier that indicates which attribute
24572 identifier
= (token
->type
== CPP_KEYWORD
)
24573 /* For keywords, use the canonical spelling, not the
24574 parsed identifier. */
24575 ? ridpointers
[(int) token
->keyword
]
24576 : id_token
->u
.value
;
24578 attribute
= build_tree_list (identifier
, NULL_TREE
);
24580 /* Peek at the next token. */
24581 token
= cp_lexer_peek_token (parser
->lexer
);
24582 /* If it's an `(', then parse the attribute arguments. */
24583 if (token
->type
== CPP_OPEN_PAREN
)
24585 vec
<tree
, va_gc
> *vec
;
24586 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24587 ? id_attr
: normal_attr
);
24588 if (is_cilkplus_vector_p (identifier
))
24590 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24594 vec
= cp_parser_parenthesized_expression_list
24595 (parser
, attr_flag
, /*cast_p=*/false,
24596 /*allow_expansion_p=*/false,
24597 /*non_constant_p=*/NULL
);
24599 arguments
= error_mark_node
;
24602 arguments
= build_tree_list_vec (vec
);
24603 release_tree_vector (vec
);
24605 /* Save the arguments away. */
24606 TREE_VALUE (attribute
) = arguments
;
24608 else if (is_cilkplus_vector_p (identifier
))
24610 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24614 if (arguments
!= error_mark_node
)
24616 /* Add this attribute to the list. */
24617 TREE_CHAIN (attribute
) = attribute_list
;
24618 attribute_list
= attribute
;
24621 token
= cp_lexer_peek_token (parser
->lexer
);
24623 /* Now, look for more attributes. If the next token isn't a
24624 `,', we're done. */
24625 if (token
->type
!= CPP_COMMA
)
24628 /* Consume the comma and keep going. */
24629 cp_lexer_consume_token (parser
->lexer
);
24631 parser
->translate_strings_p
= save_translate_strings_p
;
24633 /* We built up the list in reverse order. */
24634 return nreverse (attribute_list
);
24637 /* Parse a standard C++11 attribute.
24639 The returned representation is a TREE_LIST which TREE_PURPOSE is
24640 the scoped name of the attribute, and the TREE_VALUE is its
24643 Note that the scoped name of the attribute is itself a TREE_LIST
24644 which TREE_PURPOSE is the namespace of the attribute, and
24645 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24646 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24647 and which TREE_PURPOSE is directly the attribute name.
24649 Clients of the attribute code should use get_attribute_namespace
24650 and get_attribute_name to get the actual namespace and name of
24651 attributes, regardless of their being GNU or C++11 attributes.
24654 attribute-token attribute-argument-clause [opt]
24658 attribute-scoped-token
24660 attribute-scoped-token:
24661 attribute-namespace :: identifier
24663 attribute-namespace:
24666 attribute-argument-clause:
24667 ( balanced-token-seq )
24669 balanced-token-seq:
24670 balanced-token [opt]
24671 balanced-token-seq balanced-token
24674 ( balanced-token-seq )
24675 [ balanced-token-seq ]
24676 { balanced-token-seq }. */
24679 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
24681 tree attribute
, attr_id
= NULL_TREE
, arguments
;
24684 /* First, parse name of the attribute, a.k.a attribute-token. */
24686 token
= cp_lexer_peek_token (parser
->lexer
);
24687 if (token
->type
== CPP_NAME
)
24688 attr_id
= token
->u
.value
;
24689 else if (token
->type
== CPP_KEYWORD
)
24690 attr_id
= ridpointers
[(int) token
->keyword
];
24691 else if (token
->flags
& NAMED_OP
)
24692 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24694 if (attr_id
== NULL_TREE
)
24697 cp_lexer_consume_token (parser
->lexer
);
24699 token
= cp_lexer_peek_token (parser
->lexer
);
24700 if (token
->type
== CPP_SCOPE
)
24702 /* We are seeing a scoped attribute token. */
24704 cp_lexer_consume_token (parser
->lexer
);
24706 error_at (token
->location
, "attribute using prefix used together "
24707 "with scoped attribute token");
24710 token
= cp_lexer_consume_token (parser
->lexer
);
24711 if (token
->type
== CPP_NAME
)
24712 attr_id
= token
->u
.value
;
24713 else if (token
->type
== CPP_KEYWORD
)
24714 attr_id
= ridpointers
[(int) token
->keyword
];
24715 else if (token
->flags
& NAMED_OP
)
24716 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24719 error_at (token
->location
,
24720 "expected an identifier for the attribute name");
24721 return error_mark_node
;
24723 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24725 token
= cp_lexer_peek_token (parser
->lexer
);
24728 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24732 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
24734 /* C++11 noreturn attribute is equivalent to GNU's. */
24735 if (is_attribute_p ("noreturn", attr_id
))
24736 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24737 /* C++14 deprecated attribute is equivalent to GNU's. */
24738 else if (is_attribute_p ("deprecated", attr_id
))
24739 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24740 /* C++17 fallthrough attribute is equivalent to GNU's. */
24741 else if (is_attribute_p ("fallthrough", attr_id
))
24742 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24743 /* Transactional Memory TS optimize_for_synchronized attribute is
24744 equivalent to GNU transaction_callable. */
24745 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
24746 TREE_PURPOSE (attribute
)
24747 = get_identifier ("transaction_callable");
24748 /* Transactional Memory attributes are GNU attributes. */
24749 else if (tm_attr_to_mask (attr_id
))
24750 TREE_PURPOSE (attribute
) = attr_id
;
24753 /* Now parse the optional argument clause of the attribute. */
24755 if (token
->type
!= CPP_OPEN_PAREN
)
24759 vec
<tree
, va_gc
> *vec
;
24760 int attr_flag
= normal_attr
;
24762 if (attr_ns
== get_identifier ("gnu")
24763 && attribute_takes_identifier_p (attr_id
))
24764 /* A GNU attribute that takes an identifier in parameter. */
24765 attr_flag
= id_attr
;
24767 vec
= cp_parser_parenthesized_expression_list
24768 (parser
, attr_flag
, /*cast_p=*/false,
24769 /*allow_expansion_p=*/true,
24770 /*non_constant_p=*/NULL
);
24772 arguments
= error_mark_node
;
24775 arguments
= build_tree_list_vec (vec
);
24776 release_tree_vector (vec
);
24779 if (arguments
== error_mark_node
)
24780 attribute
= error_mark_node
;
24782 TREE_VALUE (attribute
) = arguments
;
24788 /* Check that the attribute ATTRIBUTE appears at most once in the
24789 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24790 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24791 isn't implemented yet in GCC. */
24794 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
24798 tree name
= get_attribute_name (attribute
);
24799 if (is_attribute_p ("noreturn", name
)
24800 && lookup_attribute ("noreturn", attributes
))
24801 error ("attribute %<noreturn%> can appear at most once "
24802 "in an attribute-list");
24803 else if (is_attribute_p ("deprecated", name
)
24804 && lookup_attribute ("deprecated", attributes
))
24805 error ("attribute %<deprecated%> can appear at most once "
24806 "in an attribute-list");
24810 /* Parse a list of standard C++-11 attributes.
24814 attribute-list , attribute[opt]
24816 attribute-list , attribute ...
24820 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
24822 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
24823 cp_token
*token
= NULL
;
24827 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
24828 if (attribute
== error_mark_node
)
24830 if (attribute
!= NULL_TREE
)
24832 cp_parser_check_std_attribute (attributes
, attribute
);
24833 TREE_CHAIN (attribute
) = attributes
;
24834 attributes
= attribute
;
24836 token
= cp_lexer_peek_token (parser
->lexer
);
24837 if (token
->type
== CPP_ELLIPSIS
)
24839 cp_lexer_consume_token (parser
->lexer
);
24840 if (attribute
== NULL_TREE
)
24841 error_at (token
->location
,
24842 "expected attribute before %<...%>");
24844 TREE_VALUE (attribute
)
24845 = make_pack_expansion (TREE_VALUE (attribute
));
24846 token
= cp_lexer_peek_token (parser
->lexer
);
24848 if (token
->type
!= CPP_COMMA
)
24850 cp_lexer_consume_token (parser
->lexer
);
24852 attributes
= nreverse (attributes
);
24856 /* Parse a standard C++-11 attribute specifier.
24858 attribute-specifier:
24859 [ [ attribute-using-prefix [opt] attribute-list ] ]
24860 alignment-specifier
24862 attribute-using-prefix:
24863 using attribute-namespace :
24865 alignment-specifier:
24866 alignas ( type-id ... [opt] )
24867 alignas ( alignment-expression ... [opt] ). */
24870 cp_parser_std_attribute_spec (cp_parser
*parser
)
24872 tree attributes
= NULL_TREE
;
24873 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24875 if (token
->type
== CPP_OPEN_SQUARE
24876 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
24878 tree attr_ns
= NULL_TREE
;
24880 cp_lexer_consume_token (parser
->lexer
);
24881 cp_lexer_consume_token (parser
->lexer
);
24883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24885 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24886 if (token
->type
== CPP_NAME
)
24887 attr_ns
= token
->u
.value
;
24888 else if (token
->type
== CPP_KEYWORD
)
24889 attr_ns
= ridpointers
[(int) token
->keyword
];
24890 else if (token
->flags
& NAMED_OP
)
24891 attr_ns
= get_identifier (cpp_type2name (token
->type
,
24894 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
24896 if (cxx_dialect
< cxx1z
24897 && !in_system_header_at (input_location
))
24898 pedwarn (input_location
, 0,
24899 "attribute using prefix only available "
24900 "with -std=c++1z or -std=gnu++1z");
24902 cp_lexer_consume_token (parser
->lexer
);
24903 cp_lexer_consume_token (parser
->lexer
);
24904 cp_lexer_consume_token (parser
->lexer
);
24907 attr_ns
= NULL_TREE
;
24910 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
24912 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
24913 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
24914 cp_parser_skip_to_end_of_statement (parser
);
24916 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24917 when we are sure that we have actually parsed them. */
24918 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24924 /* Look for an alignment-specifier. */
24926 token
= cp_lexer_peek_token (parser
->lexer
);
24928 if (token
->type
!= CPP_KEYWORD
24929 || token
->keyword
!= RID_ALIGNAS
)
24932 cp_lexer_consume_token (parser
->lexer
);
24933 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24935 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
24937 cp_parser_error (parser
, "expected %<(%>");
24938 return error_mark_node
;
24941 cp_parser_parse_tentatively (parser
);
24942 alignas_expr
= cp_parser_type_id (parser
);
24944 if (!cp_parser_parse_definitely (parser
))
24946 alignas_expr
= cp_parser_assignment_expression (parser
);
24947 if (alignas_expr
== error_mark_node
)
24948 cp_parser_skip_to_end_of_statement (parser
);
24949 if (alignas_expr
== NULL_TREE
24950 || alignas_expr
== error_mark_node
)
24951 return alignas_expr
;
24954 alignas_expr
= cxx_alignas_expr (alignas_expr
);
24955 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
24957 /* Handle alignas (pack...). */
24958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24960 cp_lexer_consume_token (parser
->lexer
);
24961 alignas_expr
= make_pack_expansion (alignas_expr
);
24964 /* Something went wrong, so don't build the attribute. */
24965 if (alignas_expr
== error_mark_node
)
24966 return error_mark_node
;
24968 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
24970 cp_parser_error (parser
, "expected %<)%>");
24971 return error_mark_node
;
24974 /* Build the C++-11 representation of an 'aligned'
24977 build_tree_list (build_tree_list (get_identifier ("gnu"),
24978 get_identifier ("aligned")),
24985 /* Parse a standard C++-11 attribute-specifier-seq.
24987 attribute-specifier-seq:
24988 attribute-specifier-seq [opt] attribute-specifier
24992 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
24994 tree attr_specs
= NULL_TREE
;
24995 tree attr_last
= NULL_TREE
;
24999 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25000 if (attr_spec
== NULL_TREE
)
25002 if (attr_spec
== error_mark_node
)
25003 return error_mark_node
;
25006 TREE_CHAIN (attr_last
) = attr_spec
;
25008 attr_specs
= attr_last
= attr_spec
;
25009 attr_last
= tree_last (attr_last
);
25015 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25016 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25017 current value of the PEDANTIC flag, regardless of whether or not
25018 the `__extension__' keyword is present. The caller is responsible
25019 for restoring the value of the PEDANTIC flag. */
25022 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25024 /* Save the old value of the PEDANTIC flag. */
25025 *saved_pedantic
= pedantic
;
25027 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25029 /* Consume the `__extension__' token. */
25030 cp_lexer_consume_token (parser
->lexer
);
25031 /* We're not being pedantic while the `__extension__' keyword is
25041 /* Parse a label declaration.
25044 __label__ label-declarator-seq ;
25046 label-declarator-seq:
25047 identifier , label-declarator-seq
25051 cp_parser_label_declaration (cp_parser
* parser
)
25053 /* Look for the `__label__' keyword. */
25054 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25060 /* Look for an identifier. */
25061 identifier
= cp_parser_identifier (parser
);
25062 /* If we failed, stop. */
25063 if (identifier
== error_mark_node
)
25065 /* Declare it as a label. */
25066 finish_label_decl (identifier
);
25067 /* If the next token is a `;', stop. */
25068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25070 /* Look for the `,' separating the label declarations. */
25071 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25074 /* Look for the final `;'. */
25075 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25078 // -------------------------------------------------------------------------- //
25081 // Parse a requires clause.
25083 // requires-clause:
25084 // 'requires' logical-or-expression
25086 // The required logical-or-expression must be a constant expression. Note
25087 // that we don't check that the expression is constepxr here. We defer until
25088 // we analyze constraints and then, we only check atomic constraints.
25090 cp_parser_requires_clause (cp_parser
*parser
)
25092 // Parse the requires clause so that it is not automatically folded.
25093 ++processing_template_decl
;
25094 tree expr
= cp_parser_binary_expression (parser
, false, false,
25095 PREC_NOT_OPERATOR
, NULL
);
25096 if (check_for_bare_parameter_packs (expr
))
25097 expr
= error_mark_node
;
25098 --processing_template_decl
;
25102 // Optionally parse a requires clause:
25104 cp_parser_requires_clause_opt (cp_parser
*parser
)
25106 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25107 if (tok
->keyword
!= RID_REQUIRES
)
25109 if (!flag_concepts
&& tok
->type
== CPP_NAME
25110 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25112 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25113 "%<requires%> only available with -fconcepts");
25114 /* Parse and discard the requires-clause. */
25115 cp_lexer_consume_token (parser
->lexer
);
25116 cp_parser_requires_clause (parser
);
25120 cp_lexer_consume_token (parser
->lexer
);
25121 return cp_parser_requires_clause (parser
);
25125 /*---------------------------------------------------------------------------
25126 Requires expressions
25127 ---------------------------------------------------------------------------*/
25129 /* Parse a requires expression
25131 requirement-expression:
25132 'requires' requirement-parameter-list [opt] requirement-body */
25134 cp_parser_requires_expression (cp_parser
*parser
)
25136 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25137 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25139 /* A requires-expression shall appear only within a concept
25140 definition or a requires-clause.
25142 TODO: Implement this diagnostic correctly. */
25143 if (!processing_template_decl
)
25145 error_at (loc
, "a requires expression cannot appear outside a template");
25146 cp_parser_skip_to_end_of_statement (parser
);
25147 return error_mark_node
;
25152 /* Local parameters are delared as variables within the scope
25153 of the expression. They are not visible past the end of
25154 the expression. Expressions within the requires-expression
25155 are unevaluated. */
25156 struct scope_sentinel
25160 ++cp_unevaluated_operand
;
25161 begin_scope (sk_block
, NULL_TREE
);
25166 pop_bindings_and_leave_scope ();
25167 --cp_unevaluated_operand
;
25171 /* Parse the optional parameter list. */
25172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25174 parms
= cp_parser_requirement_parameter_list (parser
);
25175 if (parms
== error_mark_node
)
25176 return error_mark_node
;
25181 /* Parse the requirement body. */
25182 reqs
= cp_parser_requirement_body (parser
);
25183 if (reqs
== error_mark_node
)
25184 return error_mark_node
;
25187 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25189 grokparms (parms
, &parms
);
25190 return finish_requires_expr (parms
, reqs
);
25193 /* Parse a parameterized requirement.
25195 requirement-parameter-list:
25196 '(' parameter-declaration-clause ')' */
25198 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25200 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25201 return error_mark_node
;
25203 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25205 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25206 return error_mark_node
;
25211 /* Parse the body of a requirement.
25214 '{' requirement-list '}' */
25216 cp_parser_requirement_body (cp_parser
*parser
)
25218 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25219 return error_mark_node
;
25221 tree reqs
= cp_parser_requirement_list (parser
);
25223 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25224 return error_mark_node
;
25229 /* Parse a list of requirements.
25233 requirement-list ';' requirement[opt] */
25235 cp_parser_requirement_list (cp_parser
*parser
)
25237 tree result
= NULL_TREE
;
25240 tree req
= cp_parser_requirement (parser
);
25241 if (req
== error_mark_node
)
25242 return error_mark_node
;
25244 result
= tree_cons (NULL_TREE
, req
, result
);
25246 /* If we see a semi-colon, consume it. */
25247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25248 cp_lexer_consume_token (parser
->lexer
);
25250 /* Stop processing at the end of the list. */
25251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25255 /* Reverse the order of requirements so they are analyzed in
25256 declaration order. */
25257 return nreverse (result
);
25260 /* Parse a syntactic requirement or type requirement.
25264 compound-requirement
25266 nested-requirement */
25268 cp_parser_requirement (cp_parser
*parser
)
25270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25271 return cp_parser_compound_requirement (parser
);
25272 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25273 return cp_parser_type_requirement (parser
);
25274 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25275 return cp_parser_nested_requirement (parser
);
25277 return cp_parser_simple_requirement (parser
);
25280 /* Parse a simple requirement.
25282 simple-requirement:
25285 cp_parser_simple_requirement (cp_parser
*parser
)
25287 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25288 if (!expr
|| expr
== error_mark_node
)
25289 return error_mark_node
;
25291 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25292 return error_mark_node
;
25294 return finish_simple_requirement (expr
);
25297 /* Parse a type requirement
25300 nested-name-specifier [opt] required-type-name ';'
25302 required-type-name:
25304 'template' [opt] simple-template-id */
25306 cp_parser_type_requirement (cp_parser
*parser
)
25308 cp_lexer_consume_token (parser
->lexer
);
25310 // Save the scope before parsing name specifiers.
25311 tree saved_scope
= parser
->scope
;
25312 tree saved_object_scope
= parser
->object_scope
;
25313 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25314 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25315 cp_parser_nested_name_specifier_opt (parser
,
25316 /*typename_keyword_p=*/true,
25317 /*check_dependency_p=*/false,
25319 /*is_declaration=*/false);
25322 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25324 cp_lexer_consume_token (parser
->lexer
);
25325 type
= cp_parser_template_id (parser
,
25326 /*template_keyword_p=*/true,
25327 /*check_dependency=*/false,
25328 /*tag_type=*/none_type
,
25329 /*is_declaration=*/false);
25330 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25331 /*complain=*/tf_error
);
25334 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25336 if (TREE_CODE (type
) == TYPE_DECL
)
25337 type
= TREE_TYPE (type
);
25339 parser
->scope
= saved_scope
;
25340 parser
->object_scope
= saved_object_scope
;
25341 parser
->qualifying_scope
= saved_qualifying_scope
;
25343 if (type
== error_mark_node
)
25344 cp_parser_skip_to_end_of_statement (parser
);
25346 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25347 return error_mark_node
;
25348 if (type
== error_mark_node
)
25349 return error_mark_node
;
25351 return finish_type_requirement (type
);
25354 /* Parse a compound requirement
25356 compound-requirement:
25357 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25359 cp_parser_compound_requirement (cp_parser
*parser
)
25361 /* Parse an expression enclosed in '{ }'s. */
25362 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25363 return error_mark_node
;
25365 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25366 if (!expr
|| expr
== error_mark_node
)
25367 return error_mark_node
;
25369 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25370 return error_mark_node
;
25372 /* Parse the optional noexcept. */
25373 bool noexcept_p
= false;
25374 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25376 cp_lexer_consume_token (parser
->lexer
);
25380 /* Parse the optional trailing return type. */
25381 tree type
= NULL_TREE
;
25382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25384 cp_lexer_consume_token (parser
->lexer
);
25385 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25386 parser
->in_result_type_constraint_p
= true;
25387 type
= cp_parser_trailing_type_id (parser
);
25388 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25389 if (type
== error_mark_node
)
25390 return error_mark_node
;
25393 return finish_compound_requirement (expr
, type
, noexcept_p
);
25396 /* Parse a nested requirement. This is the same as a requires clause.
25398 nested-requirement:
25401 cp_parser_nested_requirement (cp_parser
*parser
)
25403 cp_lexer_consume_token (parser
->lexer
);
25404 tree req
= cp_parser_requires_clause (parser
);
25405 if (req
== error_mark_node
)
25406 return error_mark_node
;
25407 return finish_nested_requirement (req
);
25410 /* Support Functions */
25412 /* Return the appropriate prefer_type argument for lookup_name_real based on
25413 tag_type and template_mem_access. */
25416 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25418 /* DR 141: When looking in the current enclosing context for a template-name
25419 after -> or ., only consider class templates. */
25420 if (template_mem_access
)
25424 case none_type
: return 0; // No preference.
25425 case scope_type
: return 1; // Type or namespace.
25426 default: return 2; // Type only.
25430 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25431 NAME should have one of the representations used for an
25432 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25433 is returned. If PARSER->SCOPE is a dependent type, then a
25434 SCOPE_REF is returned.
25436 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25437 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25438 was formed. Abstractly, such entities should not be passed to this
25439 function, because they do not need to be looked up, but it is
25440 simpler to check for this special case here, rather than at the
25443 In cases not explicitly covered above, this function returns a
25444 DECL, OVERLOAD, or baselink representing the result of the lookup.
25445 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25448 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25449 (e.g., "struct") that was used. In that case bindings that do not
25450 refer to types are ignored.
25452 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25455 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25458 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25461 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25462 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25463 NULL_TREE otherwise. */
25466 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25467 enum tag_types tag_type
,
25470 bool check_dependency
,
25471 tree
*ambiguous_decls
,
25472 location_t name_location
)
25475 tree object_type
= parser
->context
->object_type
;
25477 /* Assume that the lookup will be unambiguous. */
25478 if (ambiguous_decls
)
25479 *ambiguous_decls
= NULL_TREE
;
25481 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25482 no longer valid. Note that if we are parsing tentatively, and
25483 the parse fails, OBJECT_TYPE will be automatically restored. */
25484 parser
->context
->object_type
= NULL_TREE
;
25486 if (name
== error_mark_node
)
25487 return error_mark_node
;
25489 /* A template-id has already been resolved; there is no lookup to
25491 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25493 if (BASELINK_P (name
))
25495 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25496 == TEMPLATE_ID_EXPR
);
25500 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25501 it should already have been checked to make sure that the name
25502 used matches the type being destroyed. */
25503 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25507 /* Figure out to which type this destructor applies. */
25509 type
= parser
->scope
;
25510 else if (object_type
)
25511 type
= object_type
;
25513 type
= current_class_type
;
25514 /* If that's not a class type, there is no destructor. */
25515 if (!type
|| !CLASS_TYPE_P (type
))
25516 return error_mark_node
;
25517 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25518 lazily_declare_fn (sfk_destructor
, type
);
25519 if (!CLASSTYPE_DESTRUCTORS (type
))
25520 return error_mark_node
;
25521 /* If it was a class type, return the destructor. */
25522 return CLASSTYPE_DESTRUCTORS (type
);
25525 /* By this point, the NAME should be an ordinary identifier. If
25526 the id-expression was a qualified name, the qualifying scope is
25527 stored in PARSER->SCOPE at this point. */
25528 gcc_assert (identifier_p (name
));
25530 /* Perform the lookup. */
25535 if (parser
->scope
== error_mark_node
)
25536 return error_mark_node
;
25538 /* If the SCOPE is dependent, the lookup must be deferred until
25539 the template is instantiated -- unless we are explicitly
25540 looking up names in uninstantiated templates. Even then, we
25541 cannot look up the name if the scope is not a class type; it
25542 might, for example, be a template type parameter. */
25543 dependent_p
= (TYPE_P (parser
->scope
)
25544 && dependent_scope_p (parser
->scope
));
25545 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25547 /* Defer lookup. */
25548 decl
= error_mark_node
;
25551 tree pushed_scope
= NULL_TREE
;
25553 /* If PARSER->SCOPE is a dependent type, then it must be a
25554 class type, and we must not be checking dependencies;
25555 otherwise, we would have processed this lookup above. So
25556 that PARSER->SCOPE is not considered a dependent base by
25557 lookup_member, we must enter the scope here. */
25559 pushed_scope
= push_scope (parser
->scope
);
25561 /* If the PARSER->SCOPE is a template specialization, it
25562 may be instantiated during name lookup. In that case,
25563 errors may be issued. Even if we rollback the current
25564 tentative parse, those errors are valid. */
25565 decl
= lookup_qualified_name (parser
->scope
, name
,
25566 prefer_type_arg (tag_type
),
25567 /*complain=*/true);
25569 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25570 lookup result and the nested-name-specifier nominates a class C:
25571 * if the name specified after the nested-name-specifier, when
25572 looked up in C, is the injected-class-name of C (Clause 9), or
25573 * if the name specified after the nested-name-specifier is the
25574 same as the identifier or the simple-template-id's template-
25575 name in the last component of the nested-name-specifier,
25576 the name is instead considered to name the constructor of
25577 class C. [ Note: for example, the constructor is not an
25578 acceptable lookup result in an elaborated-type-specifier so
25579 the constructor would not be used in place of the
25580 injected-class-name. --end note ] Such a constructor name
25581 shall be used only in the declarator-id of a declaration that
25582 names a constructor or in a using-declaration. */
25583 if (tag_type
== none_type
25584 && DECL_SELF_REFERENCE_P (decl
)
25585 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25586 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25587 prefer_type_arg (tag_type
),
25588 /*complain=*/true);
25590 /* If we have a single function from a using decl, pull it out. */
25591 if (TREE_CODE (decl
) == OVERLOAD
25592 && !really_overloaded_fn (decl
))
25593 decl
= OVL_FUNCTION (decl
);
25596 pop_scope (pushed_scope
);
25599 /* If the scope is a dependent type and either we deferred lookup or
25600 we did lookup but didn't find the name, rememeber the name. */
25601 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25602 && dependent_type_p (parser
->scope
))
25608 /* The resolution to Core Issue 180 says that `struct
25609 A::B' should be considered a type-name, even if `A'
25611 type
= make_typename_type (parser
->scope
, name
, tag_type
,
25612 /*complain=*/tf_error
);
25613 if (type
!= error_mark_node
)
25614 decl
= TYPE_NAME (type
);
25616 else if (is_template
25617 && (cp_parser_next_token_ends_template_argument_p (parser
)
25618 || cp_lexer_next_token_is (parser
->lexer
,
25620 decl
= make_unbound_class_template (parser
->scope
,
25622 /*complain=*/tf_error
);
25624 decl
= build_qualified_name (/*type=*/NULL_TREE
,
25625 parser
->scope
, name
,
25628 parser
->qualifying_scope
= parser
->scope
;
25629 parser
->object_scope
= NULL_TREE
;
25631 else if (object_type
)
25633 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25634 OBJECT_TYPE is not a class. */
25635 if (CLASS_TYPE_P (object_type
))
25636 /* If the OBJECT_TYPE is a template specialization, it may
25637 be instantiated during name lookup. In that case, errors
25638 may be issued. Even if we rollback the current tentative
25639 parse, those errors are valid. */
25640 decl
= lookup_member (object_type
,
25643 prefer_type_arg (tag_type
),
25644 tf_warning_or_error
);
25649 /* Look it up in the enclosing context. DR 141: When looking for a
25650 template-name after -> or ., only consider class templates. */
25651 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
25653 /*block_p=*/true, is_namespace
, 0);
25654 if (object_type
== unknown_type_node
)
25655 /* The object is type-dependent, so we can't look anything up; we used
25656 this to get the DR 141 behavior. */
25657 object_type
= NULL_TREE
;
25658 parser
->object_scope
= object_type
;
25659 parser
->qualifying_scope
= NULL_TREE
;
25663 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
25665 /*block_p=*/true, is_namespace
, 0);
25666 parser
->qualifying_scope
= NULL_TREE
;
25667 parser
->object_scope
= NULL_TREE
;
25670 /* If the lookup failed, let our caller know. */
25671 if (!decl
|| decl
== error_mark_node
)
25672 return error_mark_node
;
25674 /* Pull out the template from an injected-class-name (or multiple). */
25676 decl
= maybe_get_template_decl_from_type_decl (decl
);
25678 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25679 if (TREE_CODE (decl
) == TREE_LIST
)
25681 if (ambiguous_decls
)
25682 *ambiguous_decls
= decl
;
25683 /* The error message we have to print is too complicated for
25684 cp_parser_error, so we incorporate its actions directly. */
25685 if (!cp_parser_simulate_error (parser
))
25687 error_at (name_location
, "reference to %qD is ambiguous",
25689 print_candidates (decl
);
25691 return error_mark_node
;
25694 gcc_assert (DECL_P (decl
)
25695 || TREE_CODE (decl
) == OVERLOAD
25696 || TREE_CODE (decl
) == SCOPE_REF
25697 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
25698 || BASELINK_P (decl
));
25700 /* If we have resolved the name of a member declaration, check to
25701 see if the declaration is accessible. When the name resolves to
25702 set of overloaded functions, accessibility is checked when
25703 overload resolution is done.
25705 During an explicit instantiation, access is not checked at all,
25706 as per [temp.explicit]. */
25708 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
25710 maybe_record_typedef_use (decl
);
25712 return cp_expr (decl
, name_location
);
25715 /* Like cp_parser_lookup_name, but for use in the typical case where
25716 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25717 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25720 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
25722 return cp_parser_lookup_name (parser
, name
,
25724 /*is_template=*/false,
25725 /*is_namespace=*/false,
25726 /*check_dependency=*/true,
25727 /*ambiguous_decls=*/NULL
,
25731 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25732 the current context, return the TYPE_DECL. If TAG_NAME_P is
25733 true, the DECL indicates the class being defined in a class-head,
25734 or declared in an elaborated-type-specifier.
25736 Otherwise, return DECL. */
25739 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
25741 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25742 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25745 template <typename T> struct B;
25748 template <typename T> struct A::B {};
25750 Similarly, in an elaborated-type-specifier:
25752 namespace N { struct X{}; }
25755 template <typename T> friend struct N::X;
25758 However, if the DECL refers to a class type, and we are in
25759 the scope of the class, then the name lookup automatically
25760 finds the TYPE_DECL created by build_self_reference rather
25761 than a TEMPLATE_DECL. For example, in:
25763 template <class T> struct S {
25767 there is no need to handle such case. */
25769 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
25770 return DECL_TEMPLATE_RESULT (decl
);
25775 /* If too many, or too few, template-parameter lists apply to the
25776 declarator, issue an error message. Returns TRUE if all went well,
25777 and FALSE otherwise. */
25780 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
25781 cp_declarator
*declarator
,
25782 location_t declarator_location
)
25784 switch (declarator
->kind
)
25788 unsigned num_templates
= 0;
25789 tree scope
= declarator
->u
.id
.qualifying_scope
;
25792 num_templates
= num_template_headers_for_class (scope
);
25793 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
25794 == TEMPLATE_ID_EXPR
)
25795 /* If the DECLARATOR has the form `X<y>' then it uses one
25796 additional level of template parameters. */
25799 return cp_parser_check_template_parameters
25800 (parser
, num_templates
, declarator_location
, declarator
);
25806 case cdk_reference
:
25808 return (cp_parser_check_declarator_template_parameters
25809 (parser
, declarator
->declarator
, declarator_location
));
25816 gcc_unreachable ();
25821 /* NUM_TEMPLATES were used in the current declaration. If that is
25822 invalid, return FALSE and issue an error messages. Otherwise,
25823 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25824 declarator and we can print more accurate diagnostics. */
25827 cp_parser_check_template_parameters (cp_parser
* parser
,
25828 unsigned num_templates
,
25829 location_t location
,
25830 cp_declarator
*declarator
)
25832 /* If there are the same number of template classes and parameter
25833 lists, that's OK. */
25834 if (parser
->num_template_parameter_lists
== num_templates
)
25836 /* If there are more, but only one more, then we are referring to a
25837 member template. That's OK too. */
25838 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
25840 /* If there are more template classes than parameter lists, we have
25843 template <class T> void S<T>::R<T>::f (); */
25844 if (parser
->num_template_parameter_lists
< num_templates
)
25846 if (declarator
&& !current_function_decl
)
25847 error_at (location
, "specializing member %<%T::%E%> "
25848 "requires %<template<>%> syntax",
25849 declarator
->u
.id
.qualifying_scope
,
25850 declarator
->u
.id
.unqualified_name
);
25851 else if (declarator
)
25852 error_at (location
, "invalid declaration of %<%T::%E%>",
25853 declarator
->u
.id
.qualifying_scope
,
25854 declarator
->u
.id
.unqualified_name
);
25856 error_at (location
, "too few template-parameter-lists");
25859 /* Otherwise, there are too many template parameter lists. We have
25862 template <class T> template <class U> void S::f(); */
25863 error_at (location
, "too many template-parameter-lists");
25867 /* Parse an optional `::' token indicating that the following name is
25868 from the global namespace. If so, PARSER->SCOPE is set to the
25869 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25870 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25871 Returns the new value of PARSER->SCOPE, if the `::' token is
25872 present, and NULL_TREE otherwise. */
25875 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
25879 /* Peek at the next token. */
25880 token
= cp_lexer_peek_token (parser
->lexer
);
25881 /* If we're looking at a `::' token then we're starting from the
25882 global namespace, not our current location. */
25883 if (token
->type
== CPP_SCOPE
)
25885 /* Consume the `::' token. */
25886 cp_lexer_consume_token (parser
->lexer
);
25887 /* Set the SCOPE so that we know where to start the lookup. */
25888 parser
->scope
= global_namespace
;
25889 parser
->qualifying_scope
= global_namespace
;
25890 parser
->object_scope
= NULL_TREE
;
25892 return parser
->scope
;
25894 else if (!current_scope_valid_p
)
25896 parser
->scope
= NULL_TREE
;
25897 parser
->qualifying_scope
= NULL_TREE
;
25898 parser
->object_scope
= NULL_TREE
;
25904 /* Returns TRUE if the upcoming token sequence is the start of a
25905 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25906 declarator is preceded by the `friend' specifier. */
25909 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
25911 bool constructor_p
;
25912 bool outside_class_specifier_p
;
25913 tree nested_name_specifier
;
25914 cp_token
*next_token
;
25916 /* The common case is that this is not a constructor declarator, so
25917 try to avoid doing lots of work if at all possible. It's not
25918 valid declare a constructor at function scope. */
25919 if (parser
->in_function_body
)
25921 /* And only certain tokens can begin a constructor declarator. */
25922 next_token
= cp_lexer_peek_token (parser
->lexer
);
25923 if (next_token
->type
!= CPP_NAME
25924 && next_token
->type
!= CPP_SCOPE
25925 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
25926 && next_token
->type
!= CPP_TEMPLATE_ID
)
25929 /* Parse tentatively; we are going to roll back all of the tokens
25931 cp_parser_parse_tentatively (parser
);
25932 /* Assume that we are looking at a constructor declarator. */
25933 constructor_p
= true;
25935 /* Look for the optional `::' operator. */
25936 cp_parser_global_scope_opt (parser
,
25937 /*current_scope_valid_p=*/false);
25938 /* Look for the nested-name-specifier. */
25939 nested_name_specifier
25940 = (cp_parser_nested_name_specifier_opt (parser
,
25941 /*typename_keyword_p=*/false,
25942 /*check_dependency_p=*/false,
25944 /*is_declaration=*/false));
25946 outside_class_specifier_p
= (!at_class_scope_p ()
25947 || !TYPE_BEING_DEFINED (current_class_type
)
25950 /* Outside of a class-specifier, there must be a
25951 nested-name-specifier. Except in C++17 mode, where we
25952 might be declaring a guiding declaration. */
25953 if (!nested_name_specifier
&& outside_class_specifier_p
25954 && cxx_dialect
< cxx1z
)
25955 constructor_p
= false;
25956 else if (nested_name_specifier
== error_mark_node
)
25957 constructor_p
= false;
25959 /* If we have a class scope, this is easy; DR 147 says that S::S always
25960 names the constructor, and no other qualified name could. */
25961 if (constructor_p
&& nested_name_specifier
25962 && CLASS_TYPE_P (nested_name_specifier
))
25964 tree id
= cp_parser_unqualified_id (parser
,
25965 /*template_keyword_p=*/false,
25966 /*check_dependency_p=*/false,
25967 /*declarator_p=*/true,
25968 /*optional_p=*/false);
25969 if (is_overloaded_fn (id
))
25970 id
= DECL_NAME (get_first_fn (id
));
25971 if (!constructor_name_p (id
, nested_name_specifier
))
25972 constructor_p
= false;
25974 /* If we still think that this might be a constructor-declarator,
25975 look for a class-name. */
25976 else if (constructor_p
)
25980 template <typename T> struct S {
25984 we must recognize that the nested `S' names a class. */
25985 if (cxx_dialect
>= cxx1z
)
25986 cp_parser_parse_tentatively (parser
);
25989 type_decl
= cp_parser_class_name (parser
,
25990 /*typename_keyword_p=*/false,
25991 /*template_keyword_p=*/false,
25993 /*check_dependency_p=*/false,
25994 /*class_head_p=*/false,
25995 /*is_declaration=*/false);
25997 if (cxx_dialect
>= cxx1z
25998 && !cp_parser_parse_definitely (parser
))
26000 type_decl
= NULL_TREE
;
26001 tree tmpl
= cp_parser_template_name (parser
,
26002 /*template_keyword*/false,
26003 /*check_dependency_p*/false,
26004 /*is_declaration*/false,
26006 /*is_identifier*/NULL
);
26007 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26008 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26009 /* It's a deduction guide, return true. */;
26011 cp_parser_simulate_error (parser
);
26014 /* If there was no class-name, then this is not a constructor.
26015 Otherwise, if we are in a class-specifier and we aren't
26016 handling a friend declaration, check that its type matches
26017 current_class_type (c++/38313). Note: error_mark_node
26018 is left alone for error recovery purposes. */
26019 constructor_p
= (!cp_parser_error_occurred (parser
)
26020 && (outside_class_specifier_p
26021 || type_decl
== NULL_TREE
26022 || type_decl
== error_mark_node
26023 || same_type_p (current_class_type
,
26024 TREE_TYPE (type_decl
))));
26026 /* If we're still considering a constructor, we have to see a `(',
26027 to begin the parameter-declaration-clause, followed by either a
26028 `)', an `...', or a decl-specifier. We need to check for a
26029 type-specifier to avoid being fooled into thinking that:
26033 is a constructor. (It is actually a function named `f' that
26034 takes one parameter (of type `int') and returns a value of type
26037 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26038 constructor_p
= false;
26041 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26042 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26043 /* A parameter declaration begins with a decl-specifier,
26044 which is either the "attribute" keyword, a storage class
26045 specifier, or (usually) a type-specifier. */
26046 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26049 tree pushed_scope
= NULL_TREE
;
26050 unsigned saved_num_template_parameter_lists
;
26052 /* Names appearing in the type-specifier should be looked up
26053 in the scope of the class. */
26054 if (current_class_type
)
26056 else if (type_decl
)
26058 type
= TREE_TYPE (type_decl
);
26059 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26061 type
= resolve_typename_type (type
,
26062 /*only_current_p=*/false);
26063 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26065 cp_parser_abort_tentative_parse (parser
);
26069 pushed_scope
= push_scope (type
);
26072 /* Inside the constructor parameter list, surrounding
26073 template-parameter-lists do not apply. */
26074 saved_num_template_parameter_lists
26075 = parser
->num_template_parameter_lists
;
26076 parser
->num_template_parameter_lists
= 0;
26078 /* Look for the type-specifier. */
26079 cp_parser_type_specifier (parser
,
26080 CP_PARSER_FLAGS_NONE
,
26081 /*decl_specs=*/NULL
,
26082 /*is_declarator=*/true,
26083 /*declares_class_or_enum=*/NULL
,
26084 /*is_cv_qualifier=*/NULL
);
26086 parser
->num_template_parameter_lists
26087 = saved_num_template_parameter_lists
;
26089 /* Leave the scope of the class. */
26091 pop_scope (pushed_scope
);
26093 constructor_p
= !cp_parser_error_occurred (parser
);
26097 /* We did not really want to consume any tokens. */
26098 cp_parser_abort_tentative_parse (parser
);
26100 return constructor_p
;
26103 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26104 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26105 they must be performed once we are in the scope of the function.
26107 Returns the function defined. */
26110 cp_parser_function_definition_from_specifiers_and_declarator
26111 (cp_parser
* parser
,
26112 cp_decl_specifier_seq
*decl_specifiers
,
26114 const cp_declarator
*declarator
)
26119 /* Begin the function-definition. */
26120 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26122 /* The things we're about to see are not directly qualified by any
26123 template headers we've seen thus far. */
26124 reset_specialization ();
26126 /* If there were names looked up in the decl-specifier-seq that we
26127 did not check, check them now. We must wait until we are in the
26128 scope of the function to perform the checks, since the function
26129 might be a friend. */
26130 perform_deferred_access_checks (tf_warning_or_error
);
26134 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26135 parser
->omp_declare_simd
= NULL
;
26136 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26137 parser
->oacc_routine
= NULL
;
26142 /* Skip the entire function. */
26143 cp_parser_skip_to_end_of_block_or_statement (parser
);
26144 fn
= error_mark_node
;
26146 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26148 /* Seen already, skip it. An error message has already been output. */
26149 cp_parser_skip_to_end_of_block_or_statement (parser
);
26150 fn
= current_function_decl
;
26151 current_function_decl
= NULL_TREE
;
26152 /* If this is a function from a class, pop the nested class. */
26153 if (current_class_name
)
26154 pop_nested_class ();
26159 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26160 tv
= TV_PARSE_INLINE
;
26162 tv
= TV_PARSE_FUNC
;
26164 fn
= cp_parser_function_definition_after_declarator (parser
,
26165 /*inline_p=*/false);
26172 /* Parse the part of a function-definition that follows the
26173 declarator. INLINE_P is TRUE iff this function is an inline
26174 function defined within a class-specifier.
26176 Returns the function defined. */
26179 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26183 bool ctor_initializer_p
= false;
26184 bool saved_in_unbraced_linkage_specification_p
;
26185 bool saved_in_function_body
;
26186 unsigned saved_num_template_parameter_lists
;
26188 bool fully_implicit_function_template_p
26189 = parser
->fully_implicit_function_template_p
;
26190 parser
->fully_implicit_function_template_p
= false;
26191 tree implicit_template_parms
26192 = parser
->implicit_template_parms
;
26193 parser
->implicit_template_parms
= 0;
26194 cp_binding_level
* implicit_template_scope
26195 = parser
->implicit_template_scope
;
26196 parser
->implicit_template_scope
= 0;
26198 saved_in_function_body
= parser
->in_function_body
;
26199 parser
->in_function_body
= true;
26200 /* If the next token is `return', then the code may be trying to
26201 make use of the "named return value" extension that G++ used to
26203 token
= cp_lexer_peek_token (parser
->lexer
);
26204 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26206 /* Consume the `return' keyword. */
26207 cp_lexer_consume_token (parser
->lexer
);
26208 /* Look for the identifier that indicates what value is to be
26210 cp_parser_identifier (parser
);
26211 /* Issue an error message. */
26212 error_at (token
->location
,
26213 "named return values are no longer supported");
26214 /* Skip tokens until we reach the start of the function body. */
26217 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26218 if (token
->type
== CPP_OPEN_BRACE
26219 || token
->type
== CPP_EOF
26220 || token
->type
== CPP_PRAGMA_EOL
)
26222 cp_lexer_consume_token (parser
->lexer
);
26225 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26226 anything declared inside `f'. */
26227 saved_in_unbraced_linkage_specification_p
26228 = parser
->in_unbraced_linkage_specification_p
;
26229 parser
->in_unbraced_linkage_specification_p
= false;
26230 /* Inside the function, surrounding template-parameter-lists do not
26232 saved_num_template_parameter_lists
26233 = parser
->num_template_parameter_lists
;
26234 parser
->num_template_parameter_lists
= 0;
26236 start_lambda_scope (current_function_decl
);
26238 /* If the next token is `try', `__transaction_atomic', or
26239 `__transaction_relaxed`, then we are looking at either function-try-block
26240 or function-transaction-block. Note that all of these include the
26242 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26243 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26244 RID_TRANSACTION_ATOMIC
);
26245 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26246 RID_TRANSACTION_RELAXED
))
26247 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26248 RID_TRANSACTION_RELAXED
);
26249 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26250 ctor_initializer_p
= cp_parser_function_try_block (parser
);
26252 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
26253 (parser
, /*in_function_try_block=*/false);
26255 finish_lambda_scope ();
26257 /* Finish the function. */
26258 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
26259 (inline_p
? 2 : 0));
26260 /* Generate code for it, if necessary. */
26261 expand_or_defer_fn (fn
);
26262 /* Restore the saved values. */
26263 parser
->in_unbraced_linkage_specification_p
26264 = saved_in_unbraced_linkage_specification_p
;
26265 parser
->num_template_parameter_lists
26266 = saved_num_template_parameter_lists
;
26267 parser
->in_function_body
= saved_in_function_body
;
26269 parser
->fully_implicit_function_template_p
26270 = fully_implicit_function_template_p
;
26271 parser
->implicit_template_parms
26272 = implicit_template_parms
;
26273 parser
->implicit_template_scope
26274 = implicit_template_scope
;
26276 if (parser
->fully_implicit_function_template_p
)
26277 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26282 /* Parse a template-declaration body (following argument list). */
26285 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26286 tree parameter_list
,
26289 tree decl
= NULL_TREE
;
26290 bool friend_p
= false;
26292 /* We just processed one more parameter list. */
26293 ++parser
->num_template_parameter_lists
;
26295 /* Get the deferred access checks from the parameter list. These
26296 will be checked once we know what is being declared, as for a
26297 member template the checks must be performed in the scope of the
26298 class containing the member. */
26299 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26301 /* Tentatively parse for a new template parameter list, which can either be
26302 the template keyword or a template introduction. */
26303 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26305 else if (cxx_dialect
>= cxx11
26306 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26307 decl
= cp_parser_alias_declaration (parser
);
26310 /* There are no access checks when parsing a template, as we do not
26311 know if a specialization will be a friend. */
26312 push_deferring_access_checks (dk_no_check
);
26313 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26314 decl
= cp_parser_single_declaration (parser
,
26317 /*explicit_specialization_p=*/false,
26319 pop_deferring_access_checks ();
26321 /* If this is a member template declaration, let the front
26323 if (member_p
&& !friend_p
&& decl
)
26325 if (TREE_CODE (decl
) == TYPE_DECL
)
26326 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26328 decl
= finish_member_template_decl (decl
);
26330 else if (friend_p
&& decl
26331 && DECL_DECLARES_TYPE_P (decl
))
26332 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26333 /*complain=*/true);
26335 /* We are done with the current parameter list. */
26336 --parser
->num_template_parameter_lists
;
26338 pop_deferring_access_checks ();
26341 finish_template_decl (parameter_list
);
26343 /* Check the template arguments for a literal operator template. */
26345 && DECL_DECLARES_FUNCTION_P (decl
)
26346 && UDLIT_OPER_P (DECL_NAME (decl
)))
26349 if (parameter_list
== NULL_TREE
)
26353 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26354 if (num_parms
== 1)
26356 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26357 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26358 if (TREE_TYPE (parm
) != char_type_node
26359 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26362 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26364 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26365 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26366 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26367 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26368 if (parm
== error_mark_node
26369 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26370 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26378 if (cxx_dialect
>= cxx14
)
26379 error ("literal operator template %qD has invalid parameter list."
26380 " Expected non-type template argument pack <char...>"
26381 " or <typename CharT, CharT...>",
26384 error ("literal operator template %qD has invalid parameter list."
26385 " Expected non-type template argument pack <char...>",
26390 /* Register member declarations. */
26391 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26392 finish_member_declaration (decl
);
26393 /* If DECL is a function template, we must return to parse it later.
26394 (Even though there is no definition, there might be default
26395 arguments that need handling.) */
26396 if (member_p
&& decl
26397 && DECL_DECLARES_FUNCTION_P (decl
))
26398 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26401 /* Parse a template introduction header for a template-declaration. Returns
26402 false if tentative parse fails. */
26405 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26407 cp_parser_parse_tentatively (parser
);
26409 tree saved_scope
= parser
->scope
;
26410 tree saved_object_scope
= parser
->object_scope
;
26411 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26413 /* Look for the optional `::' operator. */
26414 cp_parser_global_scope_opt (parser
,
26415 /*current_scope_valid_p=*/false);
26416 /* Look for the nested-name-specifier. */
26417 cp_parser_nested_name_specifier_opt (parser
,
26418 /*typename_keyword_p=*/false,
26419 /*check_dependency_p=*/true,
26421 /*is_declaration=*/false);
26423 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26424 tree concept_name
= cp_parser_identifier (parser
);
26426 /* Look up the concept for which we will be matching
26427 template parameters. */
26428 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26430 parser
->scope
= saved_scope
;
26431 parser
->object_scope
= saved_object_scope
;
26432 parser
->qualifying_scope
= saved_qualifying_scope
;
26434 if (concept_name
== error_mark_node
)
26435 cp_parser_simulate_error (parser
);
26437 /* Look for opening brace for introduction. */
26438 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
26440 if (!cp_parser_parse_definitely (parser
))
26443 push_deferring_access_checks (dk_deferred
);
26445 /* Build vector of placeholder parameters and grab
26446 matching identifiers. */
26447 tree introduction_list
= cp_parser_introduction_list (parser
);
26449 /* The introduction-list shall not be empty. */
26450 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26453 error ("empty introduction-list");
26457 /* Look for closing brace for introduction. */
26458 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
26461 if (tmpl_decl
== error_mark_node
)
26463 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26468 /* Build and associate the constraint. */
26469 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26470 if (parms
&& parms
!= error_mark_node
)
26472 cp_parser_template_declaration_after_parameters (parser
, parms
,
26477 error_at (token
->location
, "no matching concept for template-introduction");
26481 /* Parse a normal template-declaration following the template keyword. */
26484 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26486 tree parameter_list
;
26487 bool need_lang_pop
;
26488 location_t location
= input_location
;
26490 /* Look for the `<' token. */
26491 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26493 if (at_class_scope_p () && current_function_decl
)
26495 /* 14.5.2.2 [temp.mem]
26497 A local class shall not have member templates. */
26498 error_at (location
,
26499 "invalid declaration of member template in local class");
26500 cp_parser_skip_to_end_of_block_or_statement (parser
);
26505 A template ... shall not have C linkage. */
26506 if (current_lang_name
== lang_name_c
)
26508 error_at (location
, "template with C linkage");
26509 /* Give it C++ linkage to avoid confusing other parts of the
26511 push_lang_context (lang_name_cplusplus
);
26512 need_lang_pop
= true;
26515 need_lang_pop
= false;
26517 /* We cannot perform access checks on the template parameter
26518 declarations until we know what is being declared, just as we
26519 cannot check the decl-specifier list. */
26520 push_deferring_access_checks (dk_deferred
);
26522 /* If the next token is `>', then we have an invalid
26523 specialization. Rather than complain about an invalid template
26524 parameter, issue an error message here. */
26525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26527 cp_parser_error (parser
, "invalid explicit specialization");
26528 begin_specialization ();
26529 parameter_list
= NULL_TREE
;
26533 /* Parse the template parameters. */
26534 parameter_list
= cp_parser_template_parameter_list (parser
);
26537 /* Look for the `>'. */
26538 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26540 /* Manage template requirements */
26543 tree reqs
= get_shorthand_constraints (current_template_parms
);
26544 if (tree r
= cp_parser_requires_clause_opt (parser
))
26545 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26546 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26549 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26552 /* For the erroneous case of a template with C linkage, we pushed an
26553 implicit C++ linkage scope; exit that scope now. */
26555 pop_lang_context ();
26558 /* Parse a template-declaration, assuming that the `export' (and
26559 `extern') keywords, if present, has already been scanned. MEMBER_P
26560 is as for cp_parser_template_declaration. */
26563 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26565 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26567 cp_lexer_consume_token (parser
->lexer
);
26568 cp_parser_explicit_template_declaration (parser
, member_p
);
26571 else if (flag_concepts
)
26572 return cp_parser_template_introduction (parser
, member_p
);
26577 /* Perform the deferred access checks from a template-parameter-list.
26578 CHECKS is a TREE_LIST of access checks, as returned by
26579 get_deferred_access_checks. */
26582 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26584 ++processing_template_parmlist
;
26585 perform_access_checks (checks
, tf_warning_or_error
);
26586 --processing_template_parmlist
;
26589 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26590 `function-definition' sequence that follows a template header.
26591 If MEMBER_P is true, this declaration appears in a class scope.
26593 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26594 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26597 cp_parser_single_declaration (cp_parser
* parser
,
26598 vec
<deferred_access_check
, va_gc
> *checks
,
26600 bool explicit_specialization_p
,
26603 int declares_class_or_enum
;
26604 tree decl
= NULL_TREE
;
26605 cp_decl_specifier_seq decl_specifiers
;
26606 bool function_definition_p
= false;
26607 cp_token
*decl_spec_token_start
;
26609 /* This function is only used when processing a template
26611 gcc_assert (innermost_scope_kind () == sk_template_parms
26612 || innermost_scope_kind () == sk_template_spec
);
26614 /* Defer access checks until we know what is being declared. */
26615 push_deferring_access_checks (dk_deferred
);
26617 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26619 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
26620 cp_parser_decl_specifier_seq (parser
,
26621 CP_PARSER_FLAGS_OPTIONAL
,
26623 &declares_class_or_enum
);
26625 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
26627 /* There are no template typedefs. */
26628 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
26630 error_at (decl_spec_token_start
->location
,
26631 "template declaration of %<typedef%>");
26632 decl
= error_mark_node
;
26635 /* Gather up the access checks that occurred the
26636 decl-specifier-seq. */
26637 stop_deferring_access_checks ();
26639 /* Check for the declaration of a template class. */
26640 if (declares_class_or_enum
)
26642 if (cp_parser_declares_only_class_p (parser
)
26643 || (declares_class_or_enum
& 2))
26645 // If this is a declaration, but not a definition, associate
26646 // any constraints with the type declaration. Constraints
26647 // are associated with definitions in cp_parser_class_specifier.
26648 if (declares_class_or_enum
== 1)
26649 associate_classtype_constraints (decl_specifiers
.type
);
26651 decl
= shadow_tag (&decl_specifiers
);
26656 friend template <typename T> struct A<T>::B;
26659 A<T>::B will be represented by a TYPENAME_TYPE, and
26660 therefore not recognized by shadow_tag. */
26661 if (friend_p
&& *friend_p
26663 && decl_specifiers
.type
26664 && TYPE_P (decl_specifiers
.type
))
26665 decl
= decl_specifiers
.type
;
26667 if (decl
&& decl
!= error_mark_node
)
26668 decl
= TYPE_NAME (decl
);
26670 decl
= error_mark_node
;
26672 /* Perform access checks for template parameters. */
26673 cp_parser_perform_template_parameter_access_checks (checks
);
26675 /* Give a helpful diagnostic for
26676 template <class T> struct A { } a;
26677 if we aren't already recovering from an error. */
26678 if (!cp_parser_declares_only_class_p (parser
)
26681 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26682 "a class template declaration must not declare "
26684 cp_parser_skip_to_end_of_block_or_statement (parser
);
26690 /* Complain about missing 'typename' or other invalid type names. */
26691 if (!decl_specifiers
.any_type_specifiers_p
26692 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
26694 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26695 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26696 the rest of this declaration. */
26697 decl
= error_mark_node
;
26701 /* If it's not a template class, try for a template function. If
26702 the next token is a `;', then this declaration does not declare
26703 anything. But, if there were errors in the decl-specifiers, then
26704 the error might well have come from an attempted class-specifier.
26705 In that case, there's no need to warn about a missing declarator. */
26707 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
26708 || decl_specifiers
.type
!= error_mark_node
))
26710 decl
= cp_parser_init_declarator (parser
,
26713 /*function_definition_allowed_p=*/true,
26715 declares_class_or_enum
,
26716 &function_definition_p
,
26719 /* 7.1.1-1 [dcl.stc]
26721 A storage-class-specifier shall not be specified in an explicit
26722 specialization... */
26724 && explicit_specialization_p
26725 && decl_specifiers
.storage_class
!= sc_none
)
26727 error_at (decl_spec_token_start
->location
,
26728 "explicit template specialization cannot have a storage class");
26729 decl
= error_mark_node
;
26732 if (decl
&& VAR_P (decl
))
26733 check_template_variable (decl
);
26736 /* Look for a trailing `;' after the declaration. */
26737 if (!function_definition_p
26738 && (decl
== error_mark_node
26739 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
26740 cp_parser_skip_to_end_of_block_or_statement (parser
);
26743 pop_deferring_access_checks ();
26745 /* Clear any current qualification; whatever comes next is the start
26746 of something new. */
26747 parser
->scope
= NULL_TREE
;
26748 parser
->qualifying_scope
= NULL_TREE
;
26749 parser
->object_scope
= NULL_TREE
;
26754 /* Parse a cast-expression that is not the operand of a unary "&". */
26757 cp_parser_simple_cast_expression (cp_parser
*parser
)
26759 return cp_parser_cast_expression (parser
, /*address_p=*/false,
26760 /*cast_p=*/false, /*decltype*/false, NULL
);
26763 /* Parse a functional cast to TYPE. Returns an expression
26764 representing the cast. */
26767 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
26769 vec
<tree
, va_gc
> *vec
;
26770 tree expression_list
;
26774 location_t start_loc
= input_location
;
26777 type
= error_mark_node
;
26779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26781 cp_lexer_set_source_position (parser
->lexer
);
26782 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26783 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
26784 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
26785 if (TREE_CODE (type
) == TYPE_DECL
)
26786 type
= TREE_TYPE (type
);
26788 cast
= finish_compound_literal (type
, expression_list
,
26789 tf_warning_or_error
);
26790 /* Create a location of the form:
26793 with caret == start at the start of the type name,
26794 finishing at the closing brace. */
26795 location_t finish_loc
26796 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26797 location_t combined_loc
= make_location (start_loc
, start_loc
,
26799 cast
.set_location (combined_loc
);
26804 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
26806 /*allow_expansion_p=*/true,
26807 /*non_constant_p=*/NULL
);
26809 expression_list
= error_mark_node
;
26812 expression_list
= build_tree_list_vec (vec
);
26813 release_tree_vector (vec
);
26816 cast
= build_functional_cast (type
, expression_list
,
26817 tf_warning_or_error
);
26818 /* [expr.const]/1: In an integral constant expression "only type
26819 conversions to integral or enumeration type can be used". */
26820 if (TREE_CODE (type
) == TYPE_DECL
)
26821 type
= TREE_TYPE (type
);
26822 if (cast
!= error_mark_node
26823 && !cast_valid_in_integral_constant_expression_p (type
)
26824 && cp_parser_non_integral_constant_expression (parser
,
26826 return error_mark_node
;
26828 /* Create a location of the form:
26831 with caret == start at the start of the type name,
26832 finishing at the closing paren. */
26833 location_t finish_loc
26834 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26835 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
26836 cast
.set_location (combined_loc
);
26840 /* Save the tokens that make up the body of a member function defined
26841 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26842 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26843 specifiers applied to the declaration. Returns the FUNCTION_DECL
26844 for the member function. */
26847 cp_parser_save_member_function_body (cp_parser
* parser
,
26848 cp_decl_specifier_seq
*decl_specifiers
,
26849 cp_declarator
*declarator
,
26855 bool function_try_block
= false;
26857 /* Create the FUNCTION_DECL. */
26858 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
26859 cp_finalize_omp_declare_simd (parser
, fn
);
26860 cp_finalize_oacc_routine (parser
, fn
, true);
26861 /* If something went badly wrong, bail out now. */
26862 if (fn
== error_mark_node
)
26864 /* If there's a function-body, skip it. */
26865 if (cp_parser_token_starts_function_definition_p
26866 (cp_lexer_peek_token (parser
->lexer
)))
26867 cp_parser_skip_to_end_of_block_or_statement (parser
);
26868 return error_mark_node
;
26871 /* Remember it, if there default args to post process. */
26872 cp_parser_save_default_args (parser
, fn
);
26874 /* Save away the tokens that make up the body of the
26876 first
= parser
->lexer
->next_token
;
26878 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
26879 cp_lexer_consume_token (parser
->lexer
);
26880 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26881 RID_TRANSACTION_ATOMIC
))
26883 cp_lexer_consume_token (parser
->lexer
);
26884 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
26886 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
26887 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
26888 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
26889 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
26890 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
26892 cp_lexer_consume_token (parser
->lexer
);
26893 cp_lexer_consume_token (parser
->lexer
);
26894 cp_lexer_consume_token (parser
->lexer
);
26895 cp_lexer_consume_token (parser
->lexer
);
26896 cp_lexer_consume_token (parser
->lexer
);
26899 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26900 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
26902 cp_lexer_consume_token (parser
->lexer
);
26903 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26908 /* Handle function try blocks. */
26909 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26911 cp_lexer_consume_token (parser
->lexer
);
26912 function_try_block
= true;
26914 /* We can have braced-init-list mem-initializers before the fn body. */
26915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26917 cp_lexer_consume_token (parser
->lexer
);
26918 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
26920 /* cache_group will stop after an un-nested { } pair, too. */
26921 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26924 /* variadic mem-inits have ... after the ')'. */
26925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26926 cp_lexer_consume_token (parser
->lexer
);
26929 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26930 /* Handle function try blocks. */
26931 if (function_try_block
)
26932 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
26933 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26934 last
= parser
->lexer
->next_token
;
26936 /* Save away the inline definition; we will process it when the
26937 class is complete. */
26938 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
26939 DECL_PENDING_INLINE_P (fn
) = 1;
26941 /* We need to know that this was defined in the class, so that
26942 friend templates are handled correctly. */
26943 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
26945 /* Add FN to the queue of functions to be parsed later. */
26946 vec_safe_push (unparsed_funs_with_definitions
, fn
);
26951 /* Save the tokens that make up the in-class initializer for a non-static
26952 data member. Returns a DEFAULT_ARG. */
26955 cp_parser_save_nsdmi (cp_parser
* parser
)
26957 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
26960 /* Parse a template-argument-list, as well as the trailing ">" (but
26961 not the opening "<"). See cp_parser_template_argument_list for the
26965 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
26969 tree saved_qualifying_scope
;
26970 tree saved_object_scope
;
26971 bool saved_greater_than_is_operator_p
;
26972 int saved_unevaluated_operand
;
26973 int saved_inhibit_evaluation_warnings
;
26977 When parsing a template-id, the first non-nested `>' is taken as
26978 the end of the template-argument-list rather than a greater-than
26980 saved_greater_than_is_operator_p
26981 = parser
->greater_than_is_operator_p
;
26982 parser
->greater_than_is_operator_p
= false;
26983 /* Parsing the argument list may modify SCOPE, so we save it
26985 saved_scope
= parser
->scope
;
26986 saved_qualifying_scope
= parser
->qualifying_scope
;
26987 saved_object_scope
= parser
->object_scope
;
26988 /* We need to evaluate the template arguments, even though this
26989 template-id may be nested within a "sizeof". */
26990 saved_unevaluated_operand
= cp_unevaluated_operand
;
26991 cp_unevaluated_operand
= 0;
26992 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26993 c_inhibit_evaluation_warnings
= 0;
26994 /* Parse the template-argument-list itself. */
26995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
26996 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
26997 arguments
= NULL_TREE
;
26999 arguments
= cp_parser_template_argument_list (parser
);
27000 /* Look for the `>' that ends the template-argument-list. If we find
27001 a '>>' instead, it's probably just a typo. */
27002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27004 if (cxx_dialect
!= cxx98
)
27006 /* In C++0x, a `>>' in a template argument list or cast
27007 expression is considered to be two separate `>'
27008 tokens. So, change the current token to a `>', but don't
27009 consume it: it will be consumed later when the outer
27010 template argument list (or cast expression) is parsed.
27011 Note that this replacement of `>' for `>>' is necessary
27012 even if we are parsing tentatively: in the tentative
27013 case, after calling
27014 cp_parser_enclosed_template_argument_list we will always
27015 throw away all of the template arguments and the first
27016 closing `>', either because the template argument list
27017 was erroneous or because we are replacing those tokens
27018 with a CPP_TEMPLATE_ID token. The second `>' (which will
27019 not have been thrown away) is needed either to close an
27020 outer template argument list or to complete a new-style
27022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27023 token
->type
= CPP_GREATER
;
27025 else if (!saved_greater_than_is_operator_p
)
27027 /* If we're in a nested template argument list, the '>>' has
27028 to be a typo for '> >'. We emit the error message, but we
27029 continue parsing and we push a '>' as next token, so that
27030 the argument list will be parsed correctly. Note that the
27031 global source location is still on the token before the
27032 '>>', so we need to say explicitly where we want it. */
27033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27034 gcc_rich_location
richloc (token
->location
);
27035 richloc
.add_fixit_replace ("> >");
27036 error_at_rich_loc (&richloc
, "%<>>%> should be %<> >%> "
27037 "within a nested template argument list");
27039 token
->type
= CPP_GREATER
;
27043 /* If this is not a nested template argument list, the '>>'
27044 is a typo for '>'. Emit an error message and continue.
27045 Same deal about the token location, but here we can get it
27046 right by consuming the '>>' before issuing the diagnostic. */
27047 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27048 error_at (token
->location
,
27049 "spurious %<>>%>, use %<>%> to terminate "
27050 "a template argument list");
27054 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27055 /* The `>' token might be a greater-than operator again now. */
27056 parser
->greater_than_is_operator_p
27057 = saved_greater_than_is_operator_p
;
27058 /* Restore the SAVED_SCOPE. */
27059 parser
->scope
= saved_scope
;
27060 parser
->qualifying_scope
= saved_qualifying_scope
;
27061 parser
->object_scope
= saved_object_scope
;
27062 cp_unevaluated_operand
= saved_unevaluated_operand
;
27063 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27068 /* MEMBER_FUNCTION is a member function, or a friend. If default
27069 arguments, or the body of the function have not yet been parsed,
27073 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27075 timevar_push (TV_PARSE_INMETH
);
27076 /* If this member is a template, get the underlying
27078 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27079 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27081 /* There should not be any class definitions in progress at this
27082 point; the bodies of members are only parsed outside of all class
27084 gcc_assert (parser
->num_classes_being_defined
== 0);
27085 /* While we're parsing the member functions we might encounter more
27086 classes. We want to handle them right away, but we don't want
27087 them getting mixed up with functions that are currently in the
27089 push_unparsed_function_queues (parser
);
27091 /* Make sure that any template parameters are in scope. */
27092 maybe_begin_member_template_processing (member_function
);
27094 /* If the body of the function has not yet been parsed, parse it
27096 if (DECL_PENDING_INLINE_P (member_function
))
27098 tree function_scope
;
27099 cp_token_cache
*tokens
;
27101 /* The function is no longer pending; we are processing it. */
27102 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27103 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27104 DECL_PENDING_INLINE_P (member_function
) = 0;
27106 /* If this is a local class, enter the scope of the containing
27108 function_scope
= current_function_decl
;
27109 if (function_scope
)
27110 push_function_context ();
27112 /* Push the body of the function onto the lexer stack. */
27113 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27115 /* Let the front end know that we going to be defining this
27117 start_preparsed_function (member_function
, NULL_TREE
,
27118 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27120 /* Don't do access checking if it is a templated function. */
27121 if (processing_template_decl
)
27122 push_deferring_access_checks (dk_no_check
);
27124 /* #pragma omp declare reduction needs special parsing. */
27125 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27127 parser
->lexer
->in_pragma
= true;
27128 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27129 finish_function (/*inline*/2);
27130 cp_check_omp_declare_reduction (member_function
);
27133 /* Now, parse the body of the function. */
27134 cp_parser_function_definition_after_declarator (parser
,
27135 /*inline_p=*/true);
27137 if (processing_template_decl
)
27138 pop_deferring_access_checks ();
27140 /* Leave the scope of the containing function. */
27141 if (function_scope
)
27142 pop_function_context ();
27143 cp_parser_pop_lexer (parser
);
27146 /* Remove any template parameters from the symbol table. */
27147 maybe_end_member_template_processing ();
27149 /* Restore the queue. */
27150 pop_unparsed_function_queues (parser
);
27151 timevar_pop (TV_PARSE_INMETH
);
27154 /* If DECL contains any default args, remember it on the unparsed
27155 functions queue. */
27158 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27162 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27164 probe
= TREE_CHAIN (probe
))
27165 if (TREE_PURPOSE (probe
))
27167 cp_default_arg_entry entry
= {current_class_type
, decl
};
27168 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27173 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27174 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27175 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27176 from the parameter-type-list. */
27179 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27180 tree default_arg
, tree parmtype
)
27182 cp_token_cache
*tokens
;
27186 if (default_arg
== error_mark_node
)
27187 return error_mark_node
;
27189 /* Push the saved tokens for the default argument onto the parser's
27191 tokens
= DEFARG_TOKENS (default_arg
);
27192 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27194 start_lambda_scope (decl
);
27196 /* Parse the default argument. */
27197 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27198 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27199 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27201 finish_lambda_scope ();
27203 if (parsed_arg
== error_mark_node
)
27204 cp_parser_skip_to_end_of_statement (parser
);
27206 if (!processing_template_decl
)
27208 /* In a non-template class, check conversions now. In a template,
27209 we'll wait and instantiate these as needed. */
27210 if (TREE_CODE (decl
) == PARM_DECL
)
27211 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27212 tf_warning_or_error
);
27213 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27214 parsed_arg
= error_mark_node
;
27216 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
27219 /* If the token stream has not been completely used up, then
27220 there was extra junk after the end of the default
27222 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27224 if (TREE_CODE (decl
) == PARM_DECL
)
27225 cp_parser_error (parser
, "expected %<,%>");
27227 cp_parser_error (parser
, "expected %<;%>");
27230 /* Revert to the main lexer. */
27231 cp_parser_pop_lexer (parser
);
27236 /* FIELD is a non-static data member with an initializer which we saved for
27237 later; parse it now. */
27240 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27244 maybe_begin_member_template_processing (field
);
27246 push_unparsed_function_queues (parser
);
27247 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27248 DECL_INITIAL (field
),
27250 pop_unparsed_function_queues (parser
);
27252 maybe_end_member_template_processing ();
27254 DECL_INITIAL (field
) = def
;
27257 /* FN is a FUNCTION_DECL which may contains a parameter with an
27258 unparsed DEFAULT_ARG. Parse the default args now. This function
27259 assumes that the current scope is the scope in which the default
27260 argument should be processed. */
27263 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27265 bool saved_local_variables_forbidden_p
;
27266 tree parm
, parmdecl
;
27268 /* While we're parsing the default args, we might (due to the
27269 statement expression extension) encounter more classes. We want
27270 to handle them right away, but we don't want them getting mixed
27271 up with default args that are currently in the queue. */
27272 push_unparsed_function_queues (parser
);
27274 /* Local variable names (and the `this' keyword) may not appear
27275 in a default argument. */
27276 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27277 parser
->local_variables_forbidden_p
= true;
27279 push_defarg_context (fn
);
27281 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27282 parmdecl
= DECL_ARGUMENTS (fn
);
27283 parm
&& parm
!= void_list_node
;
27284 parm
= TREE_CHAIN (parm
),
27285 parmdecl
= DECL_CHAIN (parmdecl
))
27287 tree default_arg
= TREE_PURPOSE (parm
);
27289 vec
<tree
, va_gc
> *insts
;
27296 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27297 /* This can happen for a friend declaration for a function
27298 already declared with default arguments. */
27302 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27304 TREE_VALUE (parm
));
27305 if (parsed_arg
== error_mark_node
)
27310 TREE_PURPOSE (parm
) = parsed_arg
;
27312 /* Update any instantiations we've already created. */
27313 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27314 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27315 TREE_PURPOSE (copy
) = parsed_arg
;
27318 pop_defarg_context ();
27320 /* Make sure no default arg is missing. */
27321 check_default_args (fn
);
27323 /* Restore the state of local_variables_forbidden_p. */
27324 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27326 /* Restore the queue. */
27327 pop_unparsed_function_queues (parser
);
27330 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27332 sizeof ... ( identifier )
27334 where the 'sizeof' token has already been consumed. */
27337 cp_parser_sizeof_pack (cp_parser
*parser
)
27339 /* Consume the `...'. */
27340 cp_lexer_consume_token (parser
->lexer
);
27341 maybe_warn_variadic_templates ();
27343 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27345 cp_lexer_consume_token (parser
->lexer
);
27347 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27348 "%<sizeof...%> argument must be surrounded by parentheses");
27350 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27351 tree name
= cp_parser_identifier (parser
);
27352 if (name
== error_mark_node
)
27353 return error_mark_node
;
27354 /* The name is not qualified. */
27355 parser
->scope
= NULL_TREE
;
27356 parser
->qualifying_scope
= NULL_TREE
;
27357 parser
->object_scope
= NULL_TREE
;
27358 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27359 if (expr
== error_mark_node
)
27360 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27362 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27363 expr
= TREE_TYPE (expr
);
27364 else if (TREE_CODE (expr
) == CONST_DECL
)
27365 expr
= DECL_INITIAL (expr
);
27366 expr
= make_pack_expansion (expr
);
27367 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27370 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27375 /* Parse the operand of `sizeof' (or a similar operator). Returns
27376 either a TYPE or an expression, depending on the form of the
27377 input. The KEYWORD indicates which kind of expression we have
27381 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27383 tree expr
= NULL_TREE
;
27384 const char *saved_message
;
27386 bool saved_integral_constant_expression_p
;
27387 bool saved_non_integral_constant_expression_p
;
27389 /* If it's a `...', then we are computing the length of a parameter
27391 if (keyword
== RID_SIZEOF
27392 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27393 return cp_parser_sizeof_pack (parser
);
27395 /* Types cannot be defined in a `sizeof' expression. Save away the
27397 saved_message
= parser
->type_definition_forbidden_message
;
27398 /* And create the new one. */
27399 tmp
= concat ("types may not be defined in %<",
27400 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27401 "%> expressions", NULL
);
27402 parser
->type_definition_forbidden_message
= tmp
;
27404 /* The restrictions on constant-expressions do not apply inside
27405 sizeof expressions. */
27406 saved_integral_constant_expression_p
27407 = parser
->integral_constant_expression_p
;
27408 saved_non_integral_constant_expression_p
27409 = parser
->non_integral_constant_expression_p
;
27410 parser
->integral_constant_expression_p
= false;
27412 /* Do not actually evaluate the expression. */
27413 ++cp_unevaluated_operand
;
27414 ++c_inhibit_evaluation_warnings
;
27415 /* If it's a `(', then we might be looking at the type-id
27417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27419 tree type
= NULL_TREE
;
27421 /* We can't be sure yet whether we're looking at a type-id or an
27423 cp_parser_parse_tentatively (parser
);
27424 /* Note: as a GNU Extension, compound literals are considered
27425 postfix-expressions as they are in C99, so they are valid
27426 arguments to sizeof. See comment in cp_parser_cast_expression
27428 if (cp_parser_compound_literal_p (parser
))
27429 cp_parser_simulate_error (parser
);
27432 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27433 parser
->in_type_id_in_expr_p
= true;
27434 /* Look for the type-id. */
27435 type
= cp_parser_type_id (parser
);
27436 /* Look for the closing `)'. */
27437 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27438 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27441 /* If all went well, then we're done. */
27442 if (cp_parser_parse_definitely (parser
))
27444 cp_decl_specifier_seq decl_specs
;
27446 /* Build a trivial decl-specifier-seq. */
27447 clear_decl_specs (&decl_specs
);
27448 decl_specs
.type
= type
;
27450 /* Call grokdeclarator to figure out what type this is. */
27451 expr
= grokdeclarator (NULL
,
27455 /*attrlist=*/NULL
);
27459 /* If the type-id production did not work out, then we must be
27460 looking at the unary-expression production. */
27462 expr
= cp_parser_unary_expression (parser
);
27464 /* Go back to evaluating expressions. */
27465 --cp_unevaluated_operand
;
27466 --c_inhibit_evaluation_warnings
;
27468 /* Free the message we created. */
27470 /* And restore the old one. */
27471 parser
->type_definition_forbidden_message
= saved_message
;
27472 parser
->integral_constant_expression_p
27473 = saved_integral_constant_expression_p
;
27474 parser
->non_integral_constant_expression_p
27475 = saved_non_integral_constant_expression_p
;
27480 /* If the current declaration has no declarator, return true. */
27483 cp_parser_declares_only_class_p (cp_parser
*parser
)
27485 /* If the next token is a `;' or a `,' then there is no
27487 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27488 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27491 /* Update the DECL_SPECS to reflect the storage class indicated by
27495 cp_parser_set_storage_class (cp_parser
*parser
,
27496 cp_decl_specifier_seq
*decl_specs
,
27500 cp_storage_class storage_class
;
27502 if (parser
->in_unbraced_linkage_specification_p
)
27504 error_at (token
->location
, "invalid use of %qD in linkage specification",
27505 ridpointers
[keyword
]);
27508 else if (decl_specs
->storage_class
!= sc_none
)
27510 decl_specs
->conflicting_specifiers_p
= true;
27514 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27515 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27516 && decl_specs
->gnu_thread_keyword_p
)
27518 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27519 "%<__thread%> before %qD", ridpointers
[keyword
]);
27525 storage_class
= sc_auto
;
27528 storage_class
= sc_register
;
27531 storage_class
= sc_static
;
27534 storage_class
= sc_extern
;
27537 storage_class
= sc_mutable
;
27540 gcc_unreachable ();
27542 decl_specs
->storage_class
= storage_class
;
27543 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27545 /* A storage class specifier cannot be applied alongside a typedef
27546 specifier. If there is a typedef specifier present then set
27547 conflicting_specifiers_p which will trigger an error later
27548 on in grokdeclarator. */
27549 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27550 decl_specs
->conflicting_specifiers_p
= true;
27553 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27554 is true, the type is a class or enum definition. */
27557 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27560 bool type_definition_p
)
27562 decl_specs
->any_specifiers_p
= true;
27564 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27565 (with, for example, in "typedef int wchar_t;") we remember that
27566 this is what happened. In system headers, we ignore these
27567 declarations so that G++ can work with system headers that are not
27569 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27570 && !type_definition_p
27571 && (type_spec
== boolean_type_node
27572 || type_spec
== char16_type_node
27573 || type_spec
== char32_type_node
27574 || type_spec
== wchar_type_node
)
27575 && (decl_specs
->type
27576 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27577 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27578 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27579 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27581 decl_specs
->redefined_builtin_type
= type_spec
;
27582 set_and_check_decl_spec_loc (decl_specs
,
27583 ds_redefined_builtin_type_spec
,
27585 if (!decl_specs
->type
)
27587 decl_specs
->type
= type_spec
;
27588 decl_specs
->type_definition_p
= false;
27589 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27592 else if (decl_specs
->type
)
27593 decl_specs
->multiple_types_p
= true;
27596 decl_specs
->type
= type_spec
;
27597 decl_specs
->type_definition_p
= type_definition_p
;
27598 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27599 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27603 /* True iff TOKEN is the GNU keyword __thread. */
27606 token_is__thread (cp_token
*token
)
27608 gcc_assert (token
->keyword
== RID_THREAD
);
27609 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
27612 /* Set the location for a declarator specifier and check if it is
27615 DECL_SPECS is the sequence of declarator specifiers onto which to
27618 DS is the single declarator specifier to set which location is to
27619 be set onto the existing sequence of declarators.
27621 LOCATION is the location for the declarator specifier to
27625 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
27626 cp_decl_spec ds
, cp_token
*token
)
27628 gcc_assert (ds
< ds_last
);
27630 if (decl_specs
== NULL
)
27633 source_location location
= token
->location
;
27635 if (decl_specs
->locations
[ds
] == 0)
27637 decl_specs
->locations
[ds
] = location
;
27638 if (ds
== ds_thread
)
27639 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
27645 if (decl_specs
->locations
[ds_long_long
] != 0)
27646 error_at (location
,
27647 "%<long long long%> is too long for GCC");
27650 decl_specs
->locations
[ds_long_long
] = location
;
27651 pedwarn_cxx98 (location
,
27653 "ISO C++ 1998 does not support %<long long%>");
27656 else if (ds
== ds_thread
)
27658 bool gnu
= token_is__thread (token
);
27659 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
27660 error_at (location
,
27661 "both %<__thread%> and %<thread_local%> specified");
27663 error_at (location
, "duplicate %qD", token
->u
.value
);
27667 static const char *const decl_spec_names
[] = {
27684 error_at (location
,
27685 "duplicate %qs", decl_spec_names
[ds
]);
27690 /* Return true iff the declarator specifier DS is present in the
27691 sequence of declarator specifiers DECL_SPECS. */
27694 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
27697 gcc_assert (ds
< ds_last
);
27699 if (decl_specs
== NULL
)
27702 return decl_specs
->locations
[ds
] != 0;
27705 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27706 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27709 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
27711 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
27714 /* Issue an error message indicating that TOKEN_DESC was expected.
27715 If KEYWORD is true, it indicated this function is called by
27716 cp_parser_require_keword and the required token can only be
27717 a indicated keyword. */
27720 cp_parser_required_error (cp_parser
*parser
,
27721 required_token token_desc
,
27724 switch (token_desc
)
27727 cp_parser_error (parser
, "expected %<new%>");
27730 cp_parser_error (parser
, "expected %<delete%>");
27733 cp_parser_error (parser
, "expected %<return%>");
27736 cp_parser_error (parser
, "expected %<while%>");
27739 cp_parser_error (parser
, "expected %<extern%>");
27741 case RT_STATIC_ASSERT
:
27742 cp_parser_error (parser
, "expected %<static_assert%>");
27745 cp_parser_error (parser
, "expected %<decltype%>");
27748 cp_parser_error (parser
, "expected %<operator%>");
27751 cp_parser_error (parser
, "expected %<class%>");
27754 cp_parser_error (parser
, "expected %<template%>");
27757 cp_parser_error (parser
, "expected %<namespace%>");
27760 cp_parser_error (parser
, "expected %<using%>");
27763 cp_parser_error (parser
, "expected %<asm%>");
27766 cp_parser_error (parser
, "expected %<try%>");
27769 cp_parser_error (parser
, "expected %<catch%>");
27772 cp_parser_error (parser
, "expected %<throw%>");
27775 cp_parser_error (parser
, "expected %<__label__%>");
27778 cp_parser_error (parser
, "expected %<@try%>");
27780 case RT_AT_SYNCHRONIZED
:
27781 cp_parser_error (parser
, "expected %<@synchronized%>");
27784 cp_parser_error (parser
, "expected %<@throw%>");
27786 case RT_TRANSACTION_ATOMIC
:
27787 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
27789 case RT_TRANSACTION_RELAXED
:
27790 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
27797 switch (token_desc
)
27800 cp_parser_error (parser
, "expected %<;%>");
27802 case RT_OPEN_PAREN
:
27803 cp_parser_error (parser
, "expected %<(%>");
27805 case RT_CLOSE_BRACE
:
27806 cp_parser_error (parser
, "expected %<}%>");
27808 case RT_OPEN_BRACE
:
27809 cp_parser_error (parser
, "expected %<{%>");
27811 case RT_CLOSE_SQUARE
:
27812 cp_parser_error (parser
, "expected %<]%>");
27814 case RT_OPEN_SQUARE
:
27815 cp_parser_error (parser
, "expected %<[%>");
27818 cp_parser_error (parser
, "expected %<,%>");
27821 cp_parser_error (parser
, "expected %<::%>");
27824 cp_parser_error (parser
, "expected %<<%>");
27827 cp_parser_error (parser
, "expected %<>%>");
27830 cp_parser_error (parser
, "expected %<=%>");
27833 cp_parser_error (parser
, "expected %<...%>");
27836 cp_parser_error (parser
, "expected %<*%>");
27839 cp_parser_error (parser
, "expected %<~%>");
27842 cp_parser_error (parser
, "expected %<:%>");
27844 case RT_COLON_SCOPE
:
27845 cp_parser_error (parser
, "expected %<:%> or %<::%>");
27847 case RT_CLOSE_PAREN
:
27848 cp_parser_error (parser
, "expected %<)%>");
27850 case RT_COMMA_CLOSE_PAREN
:
27851 cp_parser_error (parser
, "expected %<,%> or %<)%>");
27853 case RT_PRAGMA_EOL
:
27854 cp_parser_error (parser
, "expected end of line");
27857 cp_parser_error (parser
, "expected identifier");
27860 cp_parser_error (parser
, "expected selection-statement");
27862 case RT_INTERATION
:
27863 cp_parser_error (parser
, "expected iteration-statement");
27866 cp_parser_error (parser
, "expected jump-statement");
27869 cp_parser_error (parser
, "expected class-key");
27871 case RT_CLASS_TYPENAME_TEMPLATE
:
27872 cp_parser_error (parser
,
27873 "expected %<class%>, %<typename%>, or %<template%>");
27876 gcc_unreachable ();
27880 gcc_unreachable ();
27885 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27886 issue an error message indicating that TOKEN_DESC was expected.
27888 Returns the token consumed, if the token had the appropriate type.
27889 Otherwise, returns NULL. */
27892 cp_parser_require (cp_parser
* parser
,
27893 enum cpp_ttype type
,
27894 required_token token_desc
)
27896 if (cp_lexer_next_token_is (parser
->lexer
, type
))
27897 return cp_lexer_consume_token (parser
->lexer
);
27900 /* Output the MESSAGE -- unless we're parsing tentatively. */
27901 if (!cp_parser_simulate_error (parser
))
27902 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
27907 /* An error message is produced if the next token is not '>'.
27908 All further tokens are skipped until the desired token is
27909 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27912 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
27914 /* Current level of '< ... >'. */
27915 unsigned level
= 0;
27916 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27917 unsigned nesting_depth
= 0;
27919 /* Are we ready, yet? If not, issue error message. */
27920 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
27923 /* Skip tokens until the desired token is found. */
27926 /* Peek at the next token. */
27927 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27930 if (!nesting_depth
)
27935 if (cxx_dialect
== cxx98
)
27936 /* C++0x views the `>>' operator as two `>' tokens, but
27939 else if (!nesting_depth
&& level
-- == 0)
27941 /* We've hit a `>>' where the first `>' closes the
27942 template argument list, and the second `>' is
27943 spurious. Just consume the `>>' and stop; we've
27944 already produced at least one error. */
27945 cp_lexer_consume_token (parser
->lexer
);
27948 /* Fall through for C++0x, so we handle the second `>' in
27950 gcc_fallthrough ();
27953 if (!nesting_depth
&& level
-- == 0)
27955 /* We've reached the token we want, consume it and stop. */
27956 cp_lexer_consume_token (parser
->lexer
);
27961 case CPP_OPEN_PAREN
:
27962 case CPP_OPEN_SQUARE
:
27966 case CPP_CLOSE_PAREN
:
27967 case CPP_CLOSE_SQUARE
:
27968 if (nesting_depth
-- == 0)
27973 case CPP_PRAGMA_EOL
:
27974 case CPP_SEMICOLON
:
27975 case CPP_OPEN_BRACE
:
27976 case CPP_CLOSE_BRACE
:
27977 /* The '>' was probably forgotten, don't look further. */
27984 /* Consume this token. */
27985 cp_lexer_consume_token (parser
->lexer
);
27989 /* If the next token is the indicated keyword, consume it. Otherwise,
27990 issue an error message indicating that TOKEN_DESC was expected.
27992 Returns the token consumed, if the token had the appropriate type.
27993 Otherwise, returns NULL. */
27996 cp_parser_require_keyword (cp_parser
* parser
,
27998 required_token token_desc
)
28000 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28002 if (token
&& token
->keyword
!= keyword
)
28004 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
28011 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28012 function-definition. */
28015 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28017 return (/* An ordinary function-body begins with an `{'. */
28018 token
->type
== CPP_OPEN_BRACE
28019 /* A ctor-initializer begins with a `:'. */
28020 || token
->type
== CPP_COLON
28021 /* A function-try-block begins with `try'. */
28022 || token
->keyword
== RID_TRY
28023 /* A function-transaction-block begins with `__transaction_atomic'
28024 or `__transaction_relaxed'. */
28025 || token
->keyword
== RID_TRANSACTION_ATOMIC
28026 || token
->keyword
== RID_TRANSACTION_RELAXED
28027 /* The named return value extension begins with `return'. */
28028 || token
->keyword
== RID_RETURN
);
28031 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28035 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28039 token
= cp_lexer_peek_token (parser
->lexer
);
28040 return (token
->type
== CPP_OPEN_BRACE
28041 || (token
->type
== CPP_COLON
28042 && !parser
->colon_doesnt_start_class_def_p
));
28045 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28046 C++0x) ending a template-argument. */
28049 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28053 token
= cp_lexer_peek_token (parser
->lexer
);
28054 return (token
->type
== CPP_COMMA
28055 || token
->type
== CPP_GREATER
28056 || token
->type
== CPP_ELLIPSIS
28057 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28060 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28061 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28064 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28069 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28070 if (token
->type
== CPP_LESS
)
28072 /* Check for the sequence `<::' in the original code. It would be lexed as
28073 `[:', where `[' is a digraph, and there is no whitespace before
28075 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28078 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28079 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28085 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28086 or none_type otherwise. */
28088 static enum tag_types
28089 cp_parser_token_is_class_key (cp_token
* token
)
28091 switch (token
->keyword
)
28096 return record_type
;
28105 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28106 or none_type otherwise or if the token is null. */
28108 static enum tag_types
28109 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28114 switch (token
->keyword
)
28119 return typename_type
;
28126 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28129 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28131 if (type
== error_mark_node
)
28133 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28135 if (permerror (input_location
, "%qs tag used in naming %q#T",
28136 class_key
== union_type
? "union"
28137 : class_key
== record_type
? "struct" : "class",
28139 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28140 "%q#T was previously declared here", type
);
28144 /* Issue an error message if DECL is redeclared with different
28145 access than its original declaration [class.access.spec/3].
28146 This applies to nested classes, nested class templates and
28147 enumerations [class.mem/1]. */
28150 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28153 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28154 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28157 if ((TREE_PRIVATE (decl
)
28158 != (current_access_specifier
== access_private_node
))
28159 || (TREE_PROTECTED (decl
)
28160 != (current_access_specifier
== access_protected_node
)))
28161 error_at (location
, "%qD redeclared with different access", decl
);
28164 /* Look for the `template' keyword, as a syntactic disambiguator.
28165 Return TRUE iff it is present, in which case it will be
28169 cp_parser_optional_template_keyword (cp_parser
*parser
)
28171 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28173 /* In C++98 the `template' keyword can only be used within templates;
28174 outside templates the parser can always figure out what is a
28175 template and what is not. In C++11, per the resolution of DR 468,
28176 `template' is allowed in cases where it is not strictly necessary. */
28177 if (!processing_template_decl
28178 && pedantic
&& cxx_dialect
== cxx98
)
28180 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28181 pedwarn (token
->location
, OPT_Wpedantic
,
28182 "in C++98 %<template%> (as a disambiguator) is only "
28183 "allowed within templates");
28184 /* If this part of the token stream is rescanned, the same
28185 error message would be generated. So, we purge the token
28186 from the stream. */
28187 cp_lexer_purge_token (parser
->lexer
);
28192 /* Consume the `template' keyword. */
28193 cp_lexer_consume_token (parser
->lexer
);
28200 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28201 set PARSER->SCOPE, and perform other related actions. */
28204 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28206 struct tree_check
*check_value
;
28208 /* Get the stored value. */
28209 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28210 /* Set the scope from the stored value. */
28211 parser
->scope
= saved_checks_value (check_value
);
28212 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28213 parser
->object_scope
= NULL_TREE
;
28216 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28217 encounter the end of a block before what we were looking for. */
28220 cp_parser_cache_group (cp_parser
*parser
,
28221 enum cpp_ttype end
,
28226 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28228 /* Abort a parenthesized expression if we encounter a semicolon. */
28229 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28230 && token
->type
== CPP_SEMICOLON
)
28232 /* If we've reached the end of the file, stop. */
28233 if (token
->type
== CPP_EOF
28234 || (end
!= CPP_PRAGMA_EOL
28235 && token
->type
== CPP_PRAGMA_EOL
))
28237 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28238 /* We've hit the end of an enclosing block, so there's been some
28239 kind of syntax error. */
28242 /* Consume the token. */
28243 cp_lexer_consume_token (parser
->lexer
);
28244 /* See if it starts a new group. */
28245 if (token
->type
== CPP_OPEN_BRACE
)
28247 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28248 /* In theory this should probably check end == '}', but
28249 cp_parser_save_member_function_body needs it to exit
28250 after either '}' or ')' when called with ')'. */
28254 else if (token
->type
== CPP_OPEN_PAREN
)
28256 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28257 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28260 else if (token
->type
== CPP_PRAGMA
)
28261 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28262 else if (token
->type
== end
)
28267 /* Like above, for caching a default argument or NSDMI. Both of these are
28268 terminated by a non-nested comma, but it can be unclear whether or not a
28269 comma is nested in a template argument list unless we do more parsing.
28270 In order to handle this ambiguity, when we encounter a ',' after a '<'
28271 we try to parse what follows as a parameter-declaration-list (in the
28272 case of a default argument) or a member-declarator (in the case of an
28273 NSDMI). If that succeeds, then we stop caching. */
28276 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28278 unsigned depth
= 0;
28279 int maybe_template_id
= 0;
28280 cp_token
*first_token
;
28282 tree default_argument
;
28284 /* Add tokens until we have processed the entire default
28285 argument. We add the range [first_token, token). */
28286 first_token
= cp_lexer_peek_token (parser
->lexer
);
28287 if (first_token
->type
== CPP_OPEN_BRACE
)
28289 /* For list-initialization, this is straightforward. */
28290 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28291 token
= cp_lexer_peek_token (parser
->lexer
);
28297 /* Peek at the next token. */
28298 token
= cp_lexer_peek_token (parser
->lexer
);
28299 /* What we do depends on what token we have. */
28300 switch (token
->type
)
28302 /* In valid code, a default argument must be
28303 immediately followed by a `,' `)', or `...'. */
28305 if (depth
== 0 && maybe_template_id
)
28307 /* If we've seen a '<', we might be in a
28308 template-argument-list. Until Core issue 325 is
28309 resolved, we don't know how this situation ought
28310 to be handled, so try to DTRT. We check whether
28311 what comes after the comma is a valid parameter
28312 declaration list. If it is, then the comma ends
28313 the default argument; otherwise the default
28314 argument continues. */
28315 bool error
= false;
28318 /* Set ITALP so cp_parser_parameter_declaration_list
28319 doesn't decide to commit to this parse. */
28320 bool saved_italp
= parser
->in_template_argument_list_p
;
28321 parser
->in_template_argument_list_p
= true;
28323 cp_parser_parse_tentatively (parser
);
28327 /* Parse declarators until we reach a non-comma or
28328 somthing that cannot be an initializer.
28329 Just checking whether we're looking at a single
28330 declarator is insufficient. Consider:
28331 int var = tuple<T,U>::x;
28332 The template parameter 'U' looks exactly like a
28336 int ctor_dtor_or_conv_p
;
28337 cp_lexer_consume_token (parser
->lexer
);
28338 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28339 &ctor_dtor_or_conv_p
,
28340 /*parenthesized_p=*/NULL
,
28342 /*friend_p=*/false);
28343 peek
= cp_lexer_peek_token (parser
->lexer
);
28344 if (cp_parser_error_occurred (parser
))
28347 while (peek
->type
== CPP_COMMA
);
28348 /* If we met an '=' or ';' then the original comma
28349 was the end of the NSDMI. Otherwise assume
28350 we're still in the NSDMI. */
28351 error
= (peek
->type
!= CPP_EQ
28352 && peek
->type
!= CPP_SEMICOLON
);
28356 cp_lexer_consume_token (parser
->lexer
);
28357 begin_scope (sk_function_parms
, NULL_TREE
);
28358 cp_parser_parameter_declaration_list (parser
, &error
);
28359 pop_bindings_and_leave_scope ();
28361 if (!cp_parser_error_occurred (parser
) && !error
)
28363 cp_parser_abort_tentative_parse (parser
);
28365 parser
->in_template_argument_list_p
= saved_italp
;
28369 case CPP_CLOSE_PAREN
:
28371 /* If we run into a non-nested `;', `}', or `]',
28372 then the code is invalid -- but the default
28373 argument is certainly over. */
28374 case CPP_SEMICOLON
:
28375 case CPP_CLOSE_BRACE
:
28376 case CPP_CLOSE_SQUARE
:
28378 /* Handle correctly int n = sizeof ... ( p ); */
28379 && token
->type
!= CPP_ELLIPSIS
)
28381 /* Update DEPTH, if necessary. */
28382 else if (token
->type
== CPP_CLOSE_PAREN
28383 || token
->type
== CPP_CLOSE_BRACE
28384 || token
->type
== CPP_CLOSE_SQUARE
)
28388 case CPP_OPEN_PAREN
:
28389 case CPP_OPEN_SQUARE
:
28390 case CPP_OPEN_BRACE
:
28396 /* This might be the comparison operator, or it might
28397 start a template argument list. */
28398 ++maybe_template_id
;
28402 if (cxx_dialect
== cxx98
)
28404 /* Fall through for C++0x, which treats the `>>'
28405 operator like two `>' tokens in certain
28407 gcc_fallthrough ();
28412 /* This might be an operator, or it might close a
28413 template argument list. But if a previous '<'
28414 started a template argument list, this will have
28415 closed it, so we can't be in one anymore. */
28416 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28417 if (maybe_template_id
< 0)
28418 maybe_template_id
= 0;
28422 /* If we run out of tokens, issue an error message. */
28424 case CPP_PRAGMA_EOL
:
28425 error_at (token
->location
, "file ends in default argument");
28426 return error_mark_node
;
28430 /* In these cases, we should look for template-ids.
28431 For example, if the default argument is
28432 `X<int, double>()', we need to do name lookup to
28433 figure out whether or not `X' is a template; if
28434 so, the `,' does not end the default argument.
28436 That is not yet done. */
28443 /* If we've reached the end, stop. */
28447 /* Add the token to the token block. */
28448 token
= cp_lexer_consume_token (parser
->lexer
);
28451 /* Create a DEFAULT_ARG to represent the unparsed default
28453 default_argument
= make_node (DEFAULT_ARG
);
28454 DEFARG_TOKENS (default_argument
)
28455 = cp_token_cache_new (first_token
, token
);
28456 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28458 return default_argument
;
28461 /* Begin parsing tentatively. We always save tokens while parsing
28462 tentatively so that if the tentative parsing fails we can restore the
28466 cp_parser_parse_tentatively (cp_parser
* parser
)
28468 /* Enter a new parsing context. */
28469 parser
->context
= cp_parser_context_new (parser
->context
);
28470 /* Begin saving tokens. */
28471 cp_lexer_save_tokens (parser
->lexer
);
28472 /* In order to avoid repetitive access control error messages,
28473 access checks are queued up until we are no longer parsing
28475 push_deferring_access_checks (dk_deferred
);
28478 /* Commit to the currently active tentative parse. */
28481 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28483 cp_parser_context
*context
;
28486 /* Mark all of the levels as committed. */
28487 lexer
= parser
->lexer
;
28488 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28490 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28492 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28493 while (!cp_lexer_saving_tokens (lexer
))
28494 lexer
= lexer
->next
;
28495 cp_lexer_commit_tokens (lexer
);
28499 /* Commit to the topmost currently active tentative parse.
28501 Note that this function shouldn't be called when there are
28502 irreversible side-effects while in a tentative state. For
28503 example, we shouldn't create a permanent entry in the symbol
28504 table, or issue an error message that might not apply if the
28505 tentative parse is aborted. */
28508 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28510 cp_parser_context
*context
= parser
->context
;
28511 cp_lexer
*lexer
= parser
->lexer
;
28515 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28517 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28519 while (!cp_lexer_saving_tokens (lexer
))
28520 lexer
= lexer
->next
;
28521 cp_lexer_commit_tokens (lexer
);
28525 /* Abort the currently active tentative parse. All consumed tokens
28526 will be rolled back, and no diagnostics will be issued. */
28529 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28531 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28532 || errorcount
> 0);
28533 cp_parser_simulate_error (parser
);
28534 /* Now, pretend that we want to see if the construct was
28535 successfully parsed. */
28536 cp_parser_parse_definitely (parser
);
28539 /* Stop parsing tentatively. If a parse error has occurred, restore the
28540 token stream. Otherwise, commit to the tokens we have consumed.
28541 Returns true if no error occurred; false otherwise. */
28544 cp_parser_parse_definitely (cp_parser
* parser
)
28546 bool error_occurred
;
28547 cp_parser_context
*context
;
28549 /* Remember whether or not an error occurred, since we are about to
28550 destroy that information. */
28551 error_occurred
= cp_parser_error_occurred (parser
);
28552 /* Remove the topmost context from the stack. */
28553 context
= parser
->context
;
28554 parser
->context
= context
->next
;
28555 /* If no parse errors occurred, commit to the tentative parse. */
28556 if (!error_occurred
)
28558 /* Commit to the tokens read tentatively, unless that was
28560 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28561 cp_lexer_commit_tokens (parser
->lexer
);
28563 pop_to_parent_deferring_access_checks ();
28565 /* Otherwise, if errors occurred, roll back our state so that things
28566 are just as they were before we began the tentative parse. */
28569 cp_lexer_rollback_tokens (parser
->lexer
);
28570 pop_deferring_access_checks ();
28572 /* Add the context to the front of the free list. */
28573 context
->next
= cp_parser_context_free_list
;
28574 cp_parser_context_free_list
= context
;
28576 return !error_occurred
;
28579 /* Returns true if we are parsing tentatively and are not committed to
28580 this tentative parse. */
28583 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
28585 return (cp_parser_parsing_tentatively (parser
)
28586 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
28589 /* Returns nonzero iff an error has occurred during the most recent
28590 tentative parse. */
28593 cp_parser_error_occurred (cp_parser
* parser
)
28595 return (cp_parser_parsing_tentatively (parser
)
28596 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
28599 /* Returns nonzero if GNU extensions are allowed. */
28602 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
28604 return parser
->allow_gnu_extensions_p
;
28607 /* Objective-C++ Productions */
28610 /* Parse an Objective-C expression, which feeds into a primary-expression
28614 objc-message-expression
28615 objc-string-literal
28616 objc-encode-expression
28617 objc-protocol-expression
28618 objc-selector-expression
28620 Returns a tree representation of the expression. */
28623 cp_parser_objc_expression (cp_parser
* parser
)
28625 /* Try to figure out what kind of declaration is present. */
28626 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28630 case CPP_OPEN_SQUARE
:
28631 return cp_parser_objc_message_expression (parser
);
28633 case CPP_OBJC_STRING
:
28634 kwd
= cp_lexer_consume_token (parser
->lexer
);
28635 return objc_build_string_object (kwd
->u
.value
);
28638 switch (kwd
->keyword
)
28640 case RID_AT_ENCODE
:
28641 return cp_parser_objc_encode_expression (parser
);
28643 case RID_AT_PROTOCOL
:
28644 return cp_parser_objc_protocol_expression (parser
);
28646 case RID_AT_SELECTOR
:
28647 return cp_parser_objc_selector_expression (parser
);
28653 error_at (kwd
->location
,
28654 "misplaced %<@%D%> Objective-C++ construct",
28656 cp_parser_skip_to_end_of_block_or_statement (parser
);
28659 return error_mark_node
;
28662 /* Parse an Objective-C message expression.
28664 objc-message-expression:
28665 [ objc-message-receiver objc-message-args ]
28667 Returns a representation of an Objective-C message. */
28670 cp_parser_objc_message_expression (cp_parser
* parser
)
28672 tree receiver
, messageargs
;
28674 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28675 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
28676 receiver
= cp_parser_objc_message_receiver (parser
);
28677 messageargs
= cp_parser_objc_message_args (parser
);
28678 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28679 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28681 tree result
= objc_build_message_expr (receiver
, messageargs
);
28683 /* Construct a location e.g.
28686 ranging from the '[' to the ']', with the caret at the start. */
28687 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
28688 protected_set_expr_location (result
, combined_loc
);
28693 /* Parse an objc-message-receiver.
28695 objc-message-receiver:
28697 simple-type-specifier
28699 Returns a representation of the type or expression. */
28702 cp_parser_objc_message_receiver (cp_parser
* parser
)
28706 /* An Objective-C message receiver may be either (1) a type
28707 or (2) an expression. */
28708 cp_parser_parse_tentatively (parser
);
28709 rcv
= cp_parser_expression (parser
);
28711 /* If that worked out, fine. */
28712 if (cp_parser_parse_definitely (parser
))
28715 cp_parser_parse_tentatively (parser
);
28716 rcv
= cp_parser_simple_type_specifier (parser
,
28717 /*decl_specs=*/NULL
,
28718 CP_PARSER_FLAGS_NONE
);
28720 if (cp_parser_parse_definitely (parser
))
28721 return objc_get_class_reference (rcv
);
28723 cp_parser_error (parser
, "objective-c++ message receiver expected");
28724 return error_mark_node
;
28727 /* Parse the arguments and selectors comprising an Objective-C message.
28732 objc-selector-args , objc-comma-args
28734 objc-selector-args:
28735 objc-selector [opt] : assignment-expression
28736 objc-selector-args objc-selector [opt] : assignment-expression
28739 assignment-expression
28740 objc-comma-args , assignment-expression
28742 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28743 selector arguments and TREE_VALUE containing a list of comma
28747 cp_parser_objc_message_args (cp_parser
* parser
)
28749 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
28750 bool maybe_unary_selector_p
= true;
28751 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28753 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
28755 tree selector
= NULL_TREE
, arg
;
28757 if (token
->type
!= CPP_COLON
)
28758 selector
= cp_parser_objc_selector (parser
);
28760 /* Detect if we have a unary selector. */
28761 if (maybe_unary_selector_p
28762 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28763 return build_tree_list (selector
, NULL_TREE
);
28765 maybe_unary_selector_p
= false;
28766 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
28767 arg
= cp_parser_assignment_expression (parser
);
28770 = chainon (sel_args
,
28771 build_tree_list (selector
, arg
));
28773 token
= cp_lexer_peek_token (parser
->lexer
);
28776 /* Handle non-selector arguments, if any. */
28777 while (token
->type
== CPP_COMMA
)
28781 cp_lexer_consume_token (parser
->lexer
);
28782 arg
= cp_parser_assignment_expression (parser
);
28785 = chainon (addl_args
,
28786 build_tree_list (NULL_TREE
, arg
));
28788 token
= cp_lexer_peek_token (parser
->lexer
);
28791 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
28793 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
28794 return build_tree_list (error_mark_node
, error_mark_node
);
28797 return build_tree_list (sel_args
, addl_args
);
28800 /* Parse an Objective-C encode expression.
28802 objc-encode-expression:
28803 @encode objc-typename
28805 Returns an encoded representation of the type argument. */
28808 cp_parser_objc_encode_expression (cp_parser
* parser
)
28812 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28814 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
28815 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28816 token
= cp_lexer_peek_token (parser
->lexer
);
28817 type
= complete_type (cp_parser_type_id (parser
));
28818 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28822 error_at (token
->location
,
28823 "%<@encode%> must specify a type as an argument");
28824 return error_mark_node
;
28827 /* This happens if we find @encode(T) (where T is a template
28828 typename or something dependent on a template typename) when
28829 parsing a template. In that case, we can't compile it
28830 immediately, but we rather create an AT_ENCODE_EXPR which will
28831 need to be instantiated when the template is used.
28833 if (dependent_type_p (type
))
28835 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
28836 TREE_READONLY (value
) = 1;
28841 /* Build a location of the form:
28844 with caret==start at the @ token, finishing at the close paren. */
28845 location_t combined_loc
28846 = make_location (start_loc
, start_loc
,
28847 cp_lexer_previous_token (parser
->lexer
)->location
);
28849 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
28852 /* Parse an Objective-C @defs expression. */
28855 cp_parser_objc_defs_expression (cp_parser
*parser
)
28859 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
28860 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28861 name
= cp_parser_identifier (parser
);
28862 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28864 return objc_get_class_ivars (name
);
28867 /* Parse an Objective-C protocol expression.
28869 objc-protocol-expression:
28870 @protocol ( identifier )
28872 Returns a representation of the protocol expression. */
28875 cp_parser_objc_protocol_expression (cp_parser
* parser
)
28878 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28880 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28881 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28882 proto
= cp_parser_identifier (parser
);
28883 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28885 /* Build a location of the form:
28888 with caret==start at the @ token, finishing at the close paren. */
28889 location_t combined_loc
28890 = make_location (start_loc
, start_loc
,
28891 cp_lexer_previous_token (parser
->lexer
)->location
);
28892 tree result
= objc_build_protocol_expr (proto
);
28893 protected_set_expr_location (result
, combined_loc
);
28897 /* Parse an Objective-C selector expression.
28899 objc-selector-expression:
28900 @selector ( objc-method-signature )
28902 objc-method-signature:
28908 objc-selector-seq objc-selector :
28910 Returns a representation of the method selector. */
28913 cp_parser_objc_selector_expression (cp_parser
* parser
)
28915 tree sel_seq
= NULL_TREE
;
28916 bool maybe_unary_selector_p
= true;
28918 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28920 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
28921 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28922 token
= cp_lexer_peek_token (parser
->lexer
);
28924 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
28925 || token
->type
== CPP_SCOPE
)
28927 tree selector
= NULL_TREE
;
28929 if (token
->type
!= CPP_COLON
28930 || token
->type
== CPP_SCOPE
)
28931 selector
= cp_parser_objc_selector (parser
);
28933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
28934 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
28936 /* Detect if we have a unary selector. */
28937 if (maybe_unary_selector_p
)
28939 sel_seq
= selector
;
28940 goto finish_selector
;
28944 cp_parser_error (parser
, "expected %<:%>");
28947 maybe_unary_selector_p
= false;
28948 token
= cp_lexer_consume_token (parser
->lexer
);
28950 if (token
->type
== CPP_SCOPE
)
28953 = chainon (sel_seq
,
28954 build_tree_list (selector
, NULL_TREE
));
28956 = chainon (sel_seq
,
28957 build_tree_list (NULL_TREE
, NULL_TREE
));
28961 = chainon (sel_seq
,
28962 build_tree_list (selector
, NULL_TREE
));
28964 token
= cp_lexer_peek_token (parser
->lexer
);
28968 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28971 /* Build a location of the form:
28974 with caret==start at the @ token, finishing at the close paren. */
28975 location_t combined_loc
28976 = make_location (loc
, loc
,
28977 cp_lexer_previous_token (parser
->lexer
)->location
);
28978 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
28979 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28980 protected_set_expr_location (result
, combined_loc
);
28984 /* Parse a list of identifiers.
28986 objc-identifier-list:
28988 objc-identifier-list , identifier
28990 Returns a TREE_LIST of identifier nodes. */
28993 cp_parser_objc_identifier_list (cp_parser
* parser
)
28999 identifier
= cp_parser_identifier (parser
);
29000 if (identifier
== error_mark_node
)
29001 return error_mark_node
;
29003 list
= build_tree_list (NULL_TREE
, identifier
);
29004 sep
= cp_lexer_peek_token (parser
->lexer
);
29006 while (sep
->type
== CPP_COMMA
)
29008 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29009 identifier
= cp_parser_identifier (parser
);
29010 if (identifier
== error_mark_node
)
29013 list
= chainon (list
, build_tree_list (NULL_TREE
,
29015 sep
= cp_lexer_peek_token (parser
->lexer
);
29021 /* Parse an Objective-C alias declaration.
29023 objc-alias-declaration:
29024 @compatibility_alias identifier identifier ;
29026 This function registers the alias mapping with the Objective-C front end.
29027 It returns nothing. */
29030 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29034 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29035 alias
= cp_parser_identifier (parser
);
29036 orig
= cp_parser_identifier (parser
);
29037 objc_declare_alias (alias
, orig
);
29038 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29041 /* Parse an Objective-C class forward-declaration.
29043 objc-class-declaration:
29044 @class objc-identifier-list ;
29046 The function registers the forward declarations with the Objective-C
29047 front end. It returns nothing. */
29050 cp_parser_objc_class_declaration (cp_parser
* parser
)
29052 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29057 id
= cp_parser_identifier (parser
);
29058 if (id
== error_mark_node
)
29061 objc_declare_class (id
);
29063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29064 cp_lexer_consume_token (parser
->lexer
);
29068 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29071 /* Parse a list of Objective-C protocol references.
29073 objc-protocol-refs-opt:
29074 objc-protocol-refs [opt]
29076 objc-protocol-refs:
29077 < objc-identifier-list >
29079 Returns a TREE_LIST of identifiers, if any. */
29082 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29084 tree protorefs
= NULL_TREE
;
29086 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29088 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29089 protorefs
= cp_parser_objc_identifier_list (parser
);
29090 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29096 /* Parse a Objective-C visibility specification. */
29099 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29101 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29103 switch (vis
->keyword
)
29105 case RID_AT_PRIVATE
:
29106 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29108 case RID_AT_PROTECTED
:
29109 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29111 case RID_AT_PUBLIC
:
29112 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29114 case RID_AT_PACKAGE
:
29115 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29121 /* Eat '@private'/'@protected'/'@public'. */
29122 cp_lexer_consume_token (parser
->lexer
);
29125 /* Parse an Objective-C method type. Return 'true' if it is a class
29126 (+) method, and 'false' if it is an instance (-) method. */
29129 cp_parser_objc_method_type (cp_parser
* parser
)
29131 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29137 /* Parse an Objective-C protocol qualifier. */
29140 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29142 tree quals
= NULL_TREE
, node
;
29143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29145 node
= token
->u
.value
;
29147 while (node
&& identifier_p (node
)
29148 && (node
== ridpointers
[(int) RID_IN
]
29149 || node
== ridpointers
[(int) RID_OUT
]
29150 || node
== ridpointers
[(int) RID_INOUT
]
29151 || node
== ridpointers
[(int) RID_BYCOPY
]
29152 || node
== ridpointers
[(int) RID_BYREF
]
29153 || node
== ridpointers
[(int) RID_ONEWAY
]))
29155 quals
= tree_cons (NULL_TREE
, node
, quals
);
29156 cp_lexer_consume_token (parser
->lexer
);
29157 token
= cp_lexer_peek_token (parser
->lexer
);
29158 node
= token
->u
.value
;
29164 /* Parse an Objective-C typename. */
29167 cp_parser_objc_typename (cp_parser
* parser
)
29169 tree type_name
= NULL_TREE
;
29171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29173 tree proto_quals
, cp_type
= NULL_TREE
;
29175 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29176 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29178 /* An ObjC type name may consist of just protocol qualifiers, in which
29179 case the type shall default to 'id'. */
29180 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29182 cp_type
= cp_parser_type_id (parser
);
29184 /* If the type could not be parsed, an error has already
29185 been produced. For error recovery, behave as if it had
29186 not been specified, which will use the default type
29188 if (cp_type
== error_mark_node
)
29190 cp_type
= NULL_TREE
;
29191 /* We need to skip to the closing parenthesis as
29192 cp_parser_type_id() does not seem to do it for
29194 cp_parser_skip_to_closing_parenthesis (parser
,
29195 /*recovering=*/true,
29196 /*or_comma=*/false,
29197 /*consume_paren=*/false);
29201 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29202 type_name
= build_tree_list (proto_quals
, cp_type
);
29208 /* Check to see if TYPE refers to an Objective-C selector name. */
29211 cp_parser_objc_selector_p (enum cpp_ttype type
)
29213 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29214 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29215 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29216 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29217 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29220 /* Parse an Objective-C selector. */
29223 cp_parser_objc_selector (cp_parser
* parser
)
29225 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29227 if (!cp_parser_objc_selector_p (token
->type
))
29229 error_at (token
->location
, "invalid Objective-C++ selector name");
29230 return error_mark_node
;
29233 /* C++ operator names are allowed to appear in ObjC selectors. */
29234 switch (token
->type
)
29236 case CPP_AND_AND
: return get_identifier ("and");
29237 case CPP_AND_EQ
: return get_identifier ("and_eq");
29238 case CPP_AND
: return get_identifier ("bitand");
29239 case CPP_OR
: return get_identifier ("bitor");
29240 case CPP_COMPL
: return get_identifier ("compl");
29241 case CPP_NOT
: return get_identifier ("not");
29242 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29243 case CPP_OR_OR
: return get_identifier ("or");
29244 case CPP_OR_EQ
: return get_identifier ("or_eq");
29245 case CPP_XOR
: return get_identifier ("xor");
29246 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29247 default: return token
->u
.value
;
29251 /* Parse an Objective-C params list. */
29254 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29256 tree params
= NULL_TREE
;
29257 bool maybe_unary_selector_p
= true;
29258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29260 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29262 tree selector
= NULL_TREE
, type_name
, identifier
;
29263 tree parm_attr
= NULL_TREE
;
29265 if (token
->keyword
== RID_ATTRIBUTE
)
29268 if (token
->type
!= CPP_COLON
)
29269 selector
= cp_parser_objc_selector (parser
);
29271 /* Detect if we have a unary selector. */
29272 if (maybe_unary_selector_p
29273 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29275 params
= selector
; /* Might be followed by attributes. */
29279 maybe_unary_selector_p
= false;
29280 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29282 /* Something went quite wrong. There should be a colon
29283 here, but there is not. Stop parsing parameters. */
29286 type_name
= cp_parser_objc_typename (parser
);
29287 /* New ObjC allows attributes on parameters too. */
29288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29289 parm_attr
= cp_parser_attributes_opt (parser
);
29290 identifier
= cp_parser_identifier (parser
);
29294 objc_build_keyword_decl (selector
,
29299 token
= cp_lexer_peek_token (parser
->lexer
);
29302 if (params
== NULL_TREE
)
29304 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29305 return error_mark_node
;
29308 /* We allow tail attributes for the method. */
29309 if (token
->keyword
== RID_ATTRIBUTE
)
29311 *attributes
= cp_parser_attributes_opt (parser
);
29312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29313 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29315 cp_parser_error (parser
,
29316 "method attributes must be specified at the end");
29317 return error_mark_node
;
29320 if (params
== NULL_TREE
)
29322 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29323 return error_mark_node
;
29328 /* Parse the non-keyword Objective-C params. */
29331 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29334 tree params
= make_node (TREE_LIST
);
29335 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29336 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29338 while (token
->type
== CPP_COMMA
)
29340 cp_parameter_declarator
*parmdecl
;
29343 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29344 token
= cp_lexer_peek_token (parser
->lexer
);
29346 if (token
->type
== CPP_ELLIPSIS
)
29348 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29350 token
= cp_lexer_peek_token (parser
->lexer
);
29354 /* TODO: parse attributes for tail parameters. */
29355 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29356 parm
= grokdeclarator (parmdecl
->declarator
,
29357 &parmdecl
->decl_specifiers
,
29358 PARM
, /*initialized=*/0,
29359 /*attrlist=*/NULL
);
29361 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29362 token
= cp_lexer_peek_token (parser
->lexer
);
29365 /* We allow tail attributes for the method. */
29366 if (token
->keyword
== RID_ATTRIBUTE
)
29368 if (*attributes
== NULL_TREE
)
29370 *attributes
= cp_parser_attributes_opt (parser
);
29371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29372 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29376 /* We have an error, but parse the attributes, so that we can
29378 *attributes
= cp_parser_attributes_opt (parser
);
29380 cp_parser_error (parser
,
29381 "method attributes must be specified at the end");
29382 return error_mark_node
;
29388 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29391 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29393 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29395 /* If the next token is `extern' and the following token is a string
29396 literal, then we have a linkage specification. */
29397 if (token
->keyword
== RID_EXTERN
29398 && cp_parser_is_pure_string_literal
29399 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29400 cp_parser_linkage_specification (parser
);
29401 /* Handle #pragma, if any. */
29402 else if (token
->type
== CPP_PRAGMA
)
29403 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29404 /* Allow stray semicolons. */
29405 else if (token
->type
== CPP_SEMICOLON
)
29406 cp_lexer_consume_token (parser
->lexer
);
29407 /* Mark methods as optional or required, when building protocols. */
29408 else if (token
->keyword
== RID_AT_OPTIONAL
)
29410 cp_lexer_consume_token (parser
->lexer
);
29411 objc_set_method_opt (true);
29413 else if (token
->keyword
== RID_AT_REQUIRED
)
29415 cp_lexer_consume_token (parser
->lexer
);
29416 objc_set_method_opt (false);
29418 else if (token
->keyword
== RID_NAMESPACE
)
29419 cp_parser_namespace_definition (parser
);
29420 /* Other stray characters must generate errors. */
29421 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29423 cp_lexer_consume_token (parser
->lexer
);
29424 error ("stray %qs between Objective-C++ methods",
29425 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29427 /* Finally, try to parse a block-declaration, or a function-definition. */
29429 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29432 /* Parse a method signature. */
29435 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29437 tree rettype
, kwdparms
, optparms
;
29438 bool ellipsis
= false;
29439 bool is_class_method
;
29441 is_class_method
= cp_parser_objc_method_type (parser
);
29442 rettype
= cp_parser_objc_typename (parser
);
29443 *attributes
= NULL_TREE
;
29444 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29445 if (kwdparms
== error_mark_node
)
29446 return error_mark_node
;
29447 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29448 if (optparms
== error_mark_node
)
29449 return error_mark_node
;
29451 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29455 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29458 cp_lexer_save_tokens (parser
->lexer
);
29459 tattr
= cp_parser_attributes_opt (parser
);
29460 gcc_assert (tattr
) ;
29462 /* If the attributes are followed by a method introducer, this is not allowed.
29463 Dump the attributes and flag the situation. */
29464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29465 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29468 /* Otherwise, the attributes introduce some interstitial code, possibly so
29469 rewind to allow that check. */
29470 cp_lexer_rollback_tokens (parser
->lexer
);
29474 /* Parse an Objective-C method prototype list. */
29477 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29479 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29481 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29483 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29485 tree attributes
, sig
;
29486 bool is_class_method
;
29487 if (token
->type
== CPP_PLUS
)
29488 is_class_method
= true;
29490 is_class_method
= false;
29491 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29492 if (sig
== error_mark_node
)
29494 cp_parser_skip_to_end_of_block_or_statement (parser
);
29495 token
= cp_lexer_peek_token (parser
->lexer
);
29498 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29499 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29501 else if (token
->keyword
== RID_AT_PROPERTY
)
29502 cp_parser_objc_at_property_declaration (parser
);
29503 else if (token
->keyword
== RID_ATTRIBUTE
29504 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29505 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29507 "prefix attributes are ignored for methods");
29509 /* Allow for interspersed non-ObjC++ code. */
29510 cp_parser_objc_interstitial_code (parser
);
29512 token
= cp_lexer_peek_token (parser
->lexer
);
29515 if (token
->type
!= CPP_EOF
)
29516 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29518 cp_parser_error (parser
, "expected %<@end%>");
29520 objc_finish_interface ();
29523 /* Parse an Objective-C method definition list. */
29526 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29528 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29530 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29534 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29537 tree sig
, attribute
;
29538 bool is_class_method
;
29539 if (token
->type
== CPP_PLUS
)
29540 is_class_method
= true;
29542 is_class_method
= false;
29543 push_deferring_access_checks (dk_deferred
);
29544 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29545 if (sig
== error_mark_node
)
29547 cp_parser_skip_to_end_of_block_or_statement (parser
);
29548 token
= cp_lexer_peek_token (parser
->lexer
);
29551 objc_start_method_definition (is_class_method
, sig
, attribute
,
29554 /* For historical reasons, we accept an optional semicolon. */
29555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29556 cp_lexer_consume_token (parser
->lexer
);
29558 ptk
= cp_lexer_peek_token (parser
->lexer
);
29559 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29560 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29562 perform_deferred_access_checks (tf_warning_or_error
);
29563 stop_deferring_access_checks ();
29564 meth
= cp_parser_function_definition_after_declarator (parser
,
29566 pop_deferring_access_checks ();
29567 objc_finish_method_definition (meth
);
29570 /* The following case will be removed once @synthesize is
29571 completely implemented. */
29572 else if (token
->keyword
== RID_AT_PROPERTY
)
29573 cp_parser_objc_at_property_declaration (parser
);
29574 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
29575 cp_parser_objc_at_synthesize_declaration (parser
);
29576 else if (token
->keyword
== RID_AT_DYNAMIC
)
29577 cp_parser_objc_at_dynamic_declaration (parser
);
29578 else if (token
->keyword
== RID_ATTRIBUTE
29579 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29580 warning_at (token
->location
, OPT_Wattributes
,
29581 "prefix attributes are ignored for methods");
29583 /* Allow for interspersed non-ObjC++ code. */
29584 cp_parser_objc_interstitial_code (parser
);
29586 token
= cp_lexer_peek_token (parser
->lexer
);
29589 if (token
->type
!= CPP_EOF
)
29590 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29592 cp_parser_error (parser
, "expected %<@end%>");
29594 objc_finish_implementation ();
29597 /* Parse Objective-C ivars. */
29600 cp_parser_objc_class_ivars (cp_parser
* parser
)
29602 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29604 if (token
->type
!= CPP_OPEN_BRACE
)
29605 return; /* No ivars specified. */
29607 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
29608 token
= cp_lexer_peek_token (parser
->lexer
);
29610 while (token
->type
!= CPP_CLOSE_BRACE
29611 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29613 cp_decl_specifier_seq declspecs
;
29614 int decl_class_or_enum_p
;
29615 tree prefix_attributes
;
29617 cp_parser_objc_visibility_spec (parser
);
29619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29622 cp_parser_decl_specifier_seq (parser
,
29623 CP_PARSER_FLAGS_OPTIONAL
,
29625 &decl_class_or_enum_p
);
29627 /* auto, register, static, extern, mutable. */
29628 if (declspecs
.storage_class
!= sc_none
)
29630 cp_parser_error (parser
, "invalid type for instance variable");
29631 declspecs
.storage_class
= sc_none
;
29634 /* thread_local. */
29635 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
29637 cp_parser_error (parser
, "invalid type for instance variable");
29638 declspecs
.locations
[ds_thread
] = 0;
29642 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
29644 cp_parser_error (parser
, "invalid type for instance variable");
29645 declspecs
.locations
[ds_typedef
] = 0;
29648 prefix_attributes
= declspecs
.attributes
;
29649 declspecs
.attributes
= NULL_TREE
;
29651 /* Keep going until we hit the `;' at the end of the
29653 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29655 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
29656 cp_declarator
*declarator
= NULL
;
29657 int ctor_dtor_or_conv_p
;
29659 /* Check for a (possibly unnamed) bitfield declaration. */
29660 token
= cp_lexer_peek_token (parser
->lexer
);
29661 if (token
->type
== CPP_COLON
)
29664 if (token
->type
== CPP_NAME
29665 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
29668 /* Get the name of the bitfield. */
29669 declarator
= make_id_declarator (NULL_TREE
,
29670 cp_parser_identifier (parser
),
29674 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29675 /* Get the width of the bitfield. */
29677 = cp_parser_constant_expression (parser
);
29681 /* Parse the declarator. */
29683 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29684 &ctor_dtor_or_conv_p
,
29685 /*parenthesized_p=*/NULL
,
29686 /*member_p=*/false,
29687 /*friend_p=*/false);
29690 /* Look for attributes that apply to the ivar. */
29691 attributes
= cp_parser_attributes_opt (parser
);
29692 /* Remember which attributes are prefix attributes and
29694 first_attribute
= attributes
;
29695 /* Combine the attributes. */
29696 attributes
= chainon (prefix_attributes
, attributes
);
29699 /* Create the bitfield declaration. */
29700 decl
= grokbitfield (declarator
, &declspecs
,
29704 decl
= grokfield (declarator
, &declspecs
,
29705 NULL_TREE
, /*init_const_expr_p=*/false,
29706 NULL_TREE
, attributes
);
29708 /* Add the instance variable. */
29709 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
29710 objc_add_instance_variable (decl
);
29712 /* Reset PREFIX_ATTRIBUTES. */
29713 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
29714 attributes
= TREE_CHAIN (attributes
);
29716 TREE_CHAIN (attributes
) = NULL_TREE
;
29718 token
= cp_lexer_peek_token (parser
->lexer
);
29720 if (token
->type
== CPP_COMMA
)
29722 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29728 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29729 token
= cp_lexer_peek_token (parser
->lexer
);
29732 if (token
->keyword
== RID_AT_END
)
29733 cp_parser_error (parser
, "expected %<}%>");
29735 /* Do not consume the RID_AT_END, so it will be read again as terminating
29736 the @interface of @implementation. */
29737 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29738 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
29740 /* For historical reasons, we accept an optional semicolon. */
29741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29742 cp_lexer_consume_token (parser
->lexer
);
29745 /* Parse an Objective-C protocol declaration. */
29748 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
29750 tree proto
, protorefs
;
29753 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29754 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
29756 tok
= cp_lexer_peek_token (parser
->lexer
);
29757 error_at (tok
->location
, "identifier expected after %<@protocol%>");
29758 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29762 /* See if we have a forward declaration or a definition. */
29763 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29765 /* Try a forward declaration first. */
29766 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
29772 id
= cp_parser_identifier (parser
);
29773 if (id
== error_mark_node
)
29776 objc_declare_protocol (id
, attributes
);
29778 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29779 cp_lexer_consume_token (parser
->lexer
);
29783 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29786 /* Ok, we got a full-fledged definition (or at least should). */
29789 proto
= cp_parser_identifier (parser
);
29790 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
29791 objc_start_protocol (proto
, protorefs
, attributes
);
29792 cp_parser_objc_method_prototype_list (parser
);
29796 /* Parse an Objective-C superclass or category. */
29799 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
29802 tree
*categ
, bool *is_class_extension
)
29804 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
29806 *super
= *categ
= NULL_TREE
;
29807 *is_class_extension
= false;
29808 if (next
->type
== CPP_COLON
)
29810 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29811 *super
= cp_parser_identifier (parser
);
29813 else if (next
->type
== CPP_OPEN_PAREN
)
29815 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29817 /* If there is no category name, and this is an @interface, we
29818 have a class extension. */
29819 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
29821 *categ
= NULL_TREE
;
29822 *is_class_extension
= true;
29825 *categ
= cp_parser_identifier (parser
);
29827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29831 /* Parse an Objective-C class interface. */
29834 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
29836 tree name
, super
, categ
, protos
;
29837 bool is_class_extension
;
29839 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
29840 name
= cp_parser_identifier (parser
);
29841 if (name
== error_mark_node
)
29843 /* It's hard to recover because even if valid @interface stuff
29844 is to follow, we can't compile it (or validate it) if we
29845 don't even know which class it refers to. Let's assume this
29846 was a stray '@interface' token in the stream and skip it.
29850 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
29851 &is_class_extension
);
29852 protos
= cp_parser_objc_protocol_refs_opt (parser
);
29854 /* We have either a class or a category on our hands. */
29855 if (categ
|| is_class_extension
)
29856 objc_start_category_interface (name
, categ
, protos
, attributes
);
29859 objc_start_class_interface (name
, super
, protos
, attributes
);
29860 /* Handle instance variable declarations, if any. */
29861 cp_parser_objc_class_ivars (parser
);
29862 objc_continue_interface ();
29865 cp_parser_objc_method_prototype_list (parser
);
29868 /* Parse an Objective-C class implementation. */
29871 cp_parser_objc_class_implementation (cp_parser
* parser
)
29873 tree name
, super
, categ
;
29874 bool is_class_extension
;
29876 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
29877 name
= cp_parser_identifier (parser
);
29878 if (name
== error_mark_node
)
29880 /* It's hard to recover because even if valid @implementation
29881 stuff is to follow, we can't compile it (or validate it) if
29882 we don't even know which class it refers to. Let's assume
29883 this was a stray '@implementation' token in the stream and
29888 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
29889 &is_class_extension
);
29891 /* We have either a class or a category on our hands. */
29893 objc_start_category_implementation (name
, categ
);
29896 objc_start_class_implementation (name
, super
);
29897 /* Handle instance variable declarations, if any. */
29898 cp_parser_objc_class_ivars (parser
);
29899 objc_continue_implementation ();
29902 cp_parser_objc_method_definition_list (parser
);
29905 /* Consume the @end token and finish off the implementation. */
29908 cp_parser_objc_end_implementation (cp_parser
* parser
)
29910 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29911 objc_finish_implementation ();
29914 /* Parse an Objective-C declaration. */
29917 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
29919 /* Try to figure out what kind of declaration is present. */
29920 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29923 switch (kwd
->keyword
)
29928 error_at (kwd
->location
, "attributes may not be specified before"
29929 " the %<@%D%> Objective-C++ keyword",
29933 case RID_AT_IMPLEMENTATION
:
29934 warning_at (kwd
->location
, OPT_Wattributes
,
29935 "prefix attributes are ignored before %<@%D%>",
29942 switch (kwd
->keyword
)
29945 cp_parser_objc_alias_declaration (parser
);
29948 cp_parser_objc_class_declaration (parser
);
29950 case RID_AT_PROTOCOL
:
29951 cp_parser_objc_protocol_declaration (parser
, attributes
);
29953 case RID_AT_INTERFACE
:
29954 cp_parser_objc_class_interface (parser
, attributes
);
29956 case RID_AT_IMPLEMENTATION
:
29957 cp_parser_objc_class_implementation (parser
);
29960 cp_parser_objc_end_implementation (parser
);
29963 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
29965 cp_parser_skip_to_end_of_block_or_statement (parser
);
29969 /* Parse an Objective-C try-catch-finally statement.
29971 objc-try-catch-finally-stmt:
29972 @try compound-statement objc-catch-clause-seq [opt]
29973 objc-finally-clause [opt]
29975 objc-catch-clause-seq:
29976 objc-catch-clause objc-catch-clause-seq [opt]
29979 @catch ( objc-exception-declaration ) compound-statement
29981 objc-finally-clause:
29982 @finally compound-statement
29984 objc-exception-declaration:
29985 parameter-declaration
29988 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29992 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29993 for C. Keep them in sync. */
29996 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
29998 location_t location
;
30001 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30002 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30003 objc_maybe_warn_exceptions (location
);
30004 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30005 node, lest it get absorbed into the surrounding block. */
30006 stmt
= push_stmt_list ();
30007 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30008 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30010 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30012 cp_parameter_declarator
*parm
;
30013 tree parameter_declaration
= error_mark_node
;
30014 bool seen_open_paren
= false;
30016 cp_lexer_consume_token (parser
->lexer
);
30017 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30018 seen_open_paren
= true;
30019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30021 /* We have "@catch (...)" (where the '...' are literally
30022 what is in the code). Skip the '...'.
30023 parameter_declaration is set to NULL_TREE, and
30024 objc_being_catch_clauses() knows that that means
30026 cp_lexer_consume_token (parser
->lexer
);
30027 parameter_declaration
= NULL_TREE
;
30031 /* We have "@catch (NSException *exception)" or something
30032 like that. Parse the parameter declaration. */
30033 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30035 parameter_declaration
= error_mark_node
;
30037 parameter_declaration
= grokdeclarator (parm
->declarator
,
30038 &parm
->decl_specifiers
,
30039 PARM
, /*initialized=*/0,
30040 /*attrlist=*/NULL
);
30042 if (seen_open_paren
)
30043 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30046 /* If there was no open parenthesis, we are recovering from
30047 an error, and we are trying to figure out what mistake
30048 the user has made. */
30050 /* If there is an immediate closing parenthesis, the user
30051 probably forgot the opening one (ie, they typed "@catch
30052 NSException *e)". Parse the closing parenthesis and keep
30054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30055 cp_lexer_consume_token (parser
->lexer
);
30057 /* If these is no immediate closing parenthesis, the user
30058 probably doesn't know that parenthesis are required at
30059 all (ie, they typed "@catch NSException *e"). So, just
30060 forget about the closing parenthesis and keep going. */
30062 objc_begin_catch_clause (parameter_declaration
);
30063 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30064 objc_finish_catch_clause ();
30066 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30068 cp_lexer_consume_token (parser
->lexer
);
30069 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30070 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30071 node, lest it get absorbed into the surrounding block. */
30072 stmt
= push_stmt_list ();
30073 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30074 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30077 return objc_finish_try_stmt ();
30080 /* Parse an Objective-C synchronized statement.
30082 objc-synchronized-stmt:
30083 @synchronized ( expression ) compound-statement
30085 Returns NULL_TREE. */
30088 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30090 location_t location
;
30093 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30095 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30096 objc_maybe_warn_exceptions (location
);
30097 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
30098 lock
= cp_parser_expression (parser
);
30099 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30101 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30102 node, lest it get absorbed into the surrounding block. */
30103 stmt
= push_stmt_list ();
30104 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30106 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30109 /* Parse an Objective-C throw statement.
30112 @throw assignment-expression [opt] ;
30114 Returns a constructed '@throw' statement. */
30117 cp_parser_objc_throw_statement (cp_parser
*parser
)
30119 tree expr
= NULL_TREE
;
30120 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30122 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30124 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30125 expr
= cp_parser_expression (parser
);
30127 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30129 return objc_build_throw_stmt (loc
, expr
);
30132 /* Parse an Objective-C statement. */
30135 cp_parser_objc_statement (cp_parser
* parser
)
30137 /* Try to figure out what kind of declaration is present. */
30138 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30140 switch (kwd
->keyword
)
30143 return cp_parser_objc_try_catch_finally_statement (parser
);
30144 case RID_AT_SYNCHRONIZED
:
30145 return cp_parser_objc_synchronized_statement (parser
);
30147 return cp_parser_objc_throw_statement (parser
);
30149 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30151 cp_parser_skip_to_end_of_block_or_statement (parser
);
30154 return error_mark_node
;
30157 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30158 look ahead to see if an objc keyword follows the attributes. This
30159 is to detect the use of prefix attributes on ObjC @interface and
30163 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30165 cp_lexer_save_tokens (parser
->lexer
);
30166 *attrib
= cp_parser_attributes_opt (parser
);
30167 gcc_assert (*attrib
);
30168 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30170 cp_lexer_commit_tokens (parser
->lexer
);
30173 cp_lexer_rollback_tokens (parser
->lexer
);
30177 /* This routine is a minimal replacement for
30178 c_parser_struct_declaration () used when parsing the list of
30179 types/names or ObjC++ properties. For example, when parsing the
30182 @property (readonly) int a, b, c;
30184 this function is responsible for parsing "int a, int b, int c" and
30185 returning the declarations as CHAIN of DECLs.
30187 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30188 similar parsing. */
30190 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30192 tree decls
= NULL_TREE
;
30193 cp_decl_specifier_seq declspecs
;
30194 int decl_class_or_enum_p
;
30195 tree prefix_attributes
;
30197 cp_parser_decl_specifier_seq (parser
,
30198 CP_PARSER_FLAGS_NONE
,
30200 &decl_class_or_enum_p
);
30202 if (declspecs
.type
== error_mark_node
)
30203 return error_mark_node
;
30205 /* auto, register, static, extern, mutable. */
30206 if (declspecs
.storage_class
!= sc_none
)
30208 cp_parser_error (parser
, "invalid type for property");
30209 declspecs
.storage_class
= sc_none
;
30212 /* thread_local. */
30213 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30215 cp_parser_error (parser
, "invalid type for property");
30216 declspecs
.locations
[ds_thread
] = 0;
30220 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30222 cp_parser_error (parser
, "invalid type for property");
30223 declspecs
.locations
[ds_typedef
] = 0;
30226 prefix_attributes
= declspecs
.attributes
;
30227 declspecs
.attributes
= NULL_TREE
;
30229 /* Keep going until we hit the `;' at the end of the declaration. */
30230 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30232 tree attributes
, first_attribute
, decl
;
30233 cp_declarator
*declarator
;
30236 /* Parse the declarator. */
30237 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30238 NULL
, NULL
, false, false);
30240 /* Look for attributes that apply to the ivar. */
30241 attributes
= cp_parser_attributes_opt (parser
);
30242 /* Remember which attributes are prefix attributes and
30244 first_attribute
= attributes
;
30245 /* Combine the attributes. */
30246 attributes
= chainon (prefix_attributes
, attributes
);
30248 decl
= grokfield (declarator
, &declspecs
,
30249 NULL_TREE
, /*init_const_expr_p=*/false,
30250 NULL_TREE
, attributes
);
30252 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30253 return error_mark_node
;
30255 /* Reset PREFIX_ATTRIBUTES. */
30256 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30257 attributes
= TREE_CHAIN (attributes
);
30259 TREE_CHAIN (attributes
) = NULL_TREE
;
30261 DECL_CHAIN (decl
) = decls
;
30264 token
= cp_lexer_peek_token (parser
->lexer
);
30265 if (token
->type
== CPP_COMMA
)
30267 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30276 /* Parse an Objective-C @property declaration. The syntax is:
30278 objc-property-declaration:
30279 '@property' objc-property-attributes[opt] struct-declaration ;
30281 objc-property-attributes:
30282 '(' objc-property-attribute-list ')'
30284 objc-property-attribute-list:
30285 objc-property-attribute
30286 objc-property-attribute-list, objc-property-attribute
30288 objc-property-attribute
30289 'getter' = identifier
30290 'setter' = identifier
30299 @property NSString *name;
30300 @property (readonly) id object;
30301 @property (retain, nonatomic, getter=getTheName) id name;
30302 @property int a, b, c;
30304 PS: This function is identical to
30305 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30307 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30309 /* The following variables hold the attributes of the properties as
30310 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30311 seen. When we see an attribute, we set them to 'true' (if they
30312 are boolean properties) or to the identifier (if they have an
30313 argument, ie, for getter and setter). Note that here we only
30314 parse the list of attributes, check the syntax and accumulate the
30315 attributes that we find. objc_add_property_declaration() will
30316 then process the information. */
30317 bool property_assign
= false;
30318 bool property_copy
= false;
30319 tree property_getter_ident
= NULL_TREE
;
30320 bool property_nonatomic
= false;
30321 bool property_readonly
= false;
30322 bool property_readwrite
= false;
30323 bool property_retain
= false;
30324 tree property_setter_ident
= NULL_TREE
;
30326 /* 'properties' is the list of properties that we read. Usually a
30327 single one, but maybe more (eg, in "@property int a, b, c;" there
30332 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30334 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30336 /* Parse the optional attribute list... */
30337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30340 cp_lexer_consume_token (parser
->lexer
);
30344 bool syntax_error
= false;
30345 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30348 if (token
->type
!= CPP_NAME
)
30350 cp_parser_error (parser
, "expected identifier");
30353 keyword
= C_RID_CODE (token
->u
.value
);
30354 cp_lexer_consume_token (parser
->lexer
);
30357 case RID_ASSIGN
: property_assign
= true; break;
30358 case RID_COPY
: property_copy
= true; break;
30359 case RID_NONATOMIC
: property_nonatomic
= true; break;
30360 case RID_READONLY
: property_readonly
= true; break;
30361 case RID_READWRITE
: property_readwrite
= true; break;
30362 case RID_RETAIN
: property_retain
= true; break;
30366 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30368 if (keyword
== RID_GETTER
)
30369 cp_parser_error (parser
,
30370 "missing %<=%> (after %<getter%> attribute)");
30372 cp_parser_error (parser
,
30373 "missing %<=%> (after %<setter%> attribute)");
30374 syntax_error
= true;
30377 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30378 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30380 cp_parser_error (parser
, "expected identifier");
30381 syntax_error
= true;
30384 if (keyword
== RID_SETTER
)
30386 if (property_setter_ident
!= NULL_TREE
)
30388 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30389 cp_lexer_consume_token (parser
->lexer
);
30392 property_setter_ident
= cp_parser_objc_selector (parser
);
30393 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30394 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30396 cp_lexer_consume_token (parser
->lexer
);
30400 if (property_getter_ident
!= NULL_TREE
)
30402 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30403 cp_lexer_consume_token (parser
->lexer
);
30406 property_getter_ident
= cp_parser_objc_selector (parser
);
30410 cp_parser_error (parser
, "unknown property attribute");
30411 syntax_error
= true;
30418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30419 cp_lexer_consume_token (parser
->lexer
);
30424 /* FIXME: "@property (setter, assign);" will generate a spurious
30425 "error: expected ‘)’ before ‘,’ token". This is because
30426 cp_parser_require, unlike the C counterpart, will produce an
30427 error even if we are in error recovery. */
30428 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30430 cp_parser_skip_to_closing_parenthesis (parser
,
30431 /*recovering=*/true,
30432 /*or_comma=*/false,
30433 /*consume_paren=*/true);
30437 /* ... and the property declaration(s). */
30438 properties
= cp_parser_objc_struct_declaration (parser
);
30440 if (properties
== error_mark_node
)
30442 cp_parser_skip_to_end_of_statement (parser
);
30443 /* If the next token is now a `;', consume it. */
30444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30445 cp_lexer_consume_token (parser
->lexer
);
30449 if (properties
== NULL_TREE
)
30450 cp_parser_error (parser
, "expected identifier");
30453 /* Comma-separated properties are chained together in
30454 reverse order; add them one by one. */
30455 properties
= nreverse (properties
);
30457 for (; properties
; properties
= TREE_CHAIN (properties
))
30458 objc_add_property_declaration (loc
, copy_node (properties
),
30459 property_readonly
, property_readwrite
,
30460 property_assign
, property_retain
,
30461 property_copy
, property_nonatomic
,
30462 property_getter_ident
, property_setter_ident
);
30465 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30468 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30470 objc-synthesize-declaration:
30471 @synthesize objc-synthesize-identifier-list ;
30473 objc-synthesize-identifier-list:
30474 objc-synthesize-identifier
30475 objc-synthesize-identifier-list, objc-synthesize-identifier
30477 objc-synthesize-identifier
30479 identifier = identifier
30482 @synthesize MyProperty;
30483 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30485 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30486 for C. Keep them in sync.
30489 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30491 tree list
= NULL_TREE
;
30493 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30495 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30498 tree property
, ivar
;
30499 property
= cp_parser_identifier (parser
);
30500 if (property
== error_mark_node
)
30502 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30507 cp_lexer_consume_token (parser
->lexer
);
30508 ivar
= cp_parser_identifier (parser
);
30509 if (ivar
== error_mark_node
)
30511 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30517 list
= chainon (list
, build_tree_list (ivar
, property
));
30518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30519 cp_lexer_consume_token (parser
->lexer
);
30523 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30524 objc_add_synthesize_declaration (loc
, list
);
30527 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30529 objc-dynamic-declaration:
30530 @dynamic identifier-list ;
30533 @dynamic MyProperty;
30534 @dynamic MyProperty, AnotherProperty;
30536 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30537 for C. Keep them in sync.
30540 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30542 tree list
= NULL_TREE
;
30544 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30546 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30550 property
= cp_parser_identifier (parser
);
30551 if (property
== error_mark_node
)
30553 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30556 list
= chainon (list
, build_tree_list (NULL
, property
));
30557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30558 cp_lexer_consume_token (parser
->lexer
);
30562 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30563 objc_add_dynamic_declaration (loc
, list
);
30567 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30569 /* Returns name of the next clause.
30570 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30571 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30572 returned and the token is consumed. */
30574 static pragma_omp_clause
30575 cp_parser_omp_clause_name (cp_parser
*parser
)
30577 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
30579 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30580 result
= PRAGMA_OACC_CLAUSE_AUTO
;
30581 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
30582 result
= PRAGMA_OMP_CLAUSE_IF
;
30583 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
30584 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
30585 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
30586 result
= PRAGMA_OACC_CLAUSE_DELETE
;
30587 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
30588 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
30589 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30590 result
= PRAGMA_OMP_CLAUSE_FOR
;
30591 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30593 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30594 const char *p
= IDENTIFIER_POINTER (id
);
30599 if (!strcmp ("aligned", p
))
30600 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
30601 else if (!strcmp ("async", p
))
30602 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
30605 if (!strcmp ("collapse", p
))
30606 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
30607 else if (!strcmp ("copy", p
))
30608 result
= PRAGMA_OACC_CLAUSE_COPY
;
30609 else if (!strcmp ("copyin", p
))
30610 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
30611 else if (!strcmp ("copyout", p
))
30612 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
30613 else if (!strcmp ("copyprivate", p
))
30614 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
30615 else if (!strcmp ("create", p
))
30616 result
= PRAGMA_OACC_CLAUSE_CREATE
;
30619 if (!strcmp ("defaultmap", p
))
30620 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
30621 else if (!strcmp ("depend", p
))
30622 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
30623 else if (!strcmp ("device", p
))
30624 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
30625 else if (!strcmp ("deviceptr", p
))
30626 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
30627 else if (!strcmp ("device_resident", p
))
30628 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
30629 else if (!strcmp ("dist_schedule", p
))
30630 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
30633 if (!strcmp ("final", p
))
30634 result
= PRAGMA_OMP_CLAUSE_FINAL
;
30635 else if (!strcmp ("firstprivate", p
))
30636 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
30637 else if (!strcmp ("from", p
))
30638 result
= PRAGMA_OMP_CLAUSE_FROM
;
30641 if (!strcmp ("gang", p
))
30642 result
= PRAGMA_OACC_CLAUSE_GANG
;
30643 else if (!strcmp ("grainsize", p
))
30644 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
30647 if (!strcmp ("hint", p
))
30648 result
= PRAGMA_OMP_CLAUSE_HINT
;
30649 else if (!strcmp ("host", p
))
30650 result
= PRAGMA_OACC_CLAUSE_HOST
;
30653 if (!strcmp ("inbranch", p
))
30654 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
30655 else if (!strcmp ("independent", p
))
30656 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
30657 else if (!strcmp ("is_device_ptr", p
))
30658 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
30661 if (!strcmp ("lastprivate", p
))
30662 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
30663 else if (!strcmp ("linear", p
))
30664 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
30665 else if (!strcmp ("link", p
))
30666 result
= PRAGMA_OMP_CLAUSE_LINK
;
30669 if (!strcmp ("map", p
))
30670 result
= PRAGMA_OMP_CLAUSE_MAP
;
30671 else if (!strcmp ("mergeable", p
))
30672 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
30673 else if (flag_cilkplus
&& !strcmp ("mask", p
))
30674 result
= PRAGMA_CILK_CLAUSE_MASK
;
30677 if (!strcmp ("nogroup", p
))
30678 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
30679 else if (!strcmp ("notinbranch", p
))
30680 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
30681 else if (!strcmp ("nowait", p
))
30682 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
30683 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
30684 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
30685 else if (!strcmp ("num_gangs", p
))
30686 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
30687 else if (!strcmp ("num_tasks", p
))
30688 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
30689 else if (!strcmp ("num_teams", p
))
30690 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
30691 else if (!strcmp ("num_threads", p
))
30692 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
30693 else if (!strcmp ("num_workers", p
))
30694 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
30697 if (!strcmp ("ordered", p
))
30698 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
30701 if (!strcmp ("parallel", p
))
30702 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
30703 else if (!strcmp ("present", p
))
30704 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
30705 else if (!strcmp ("present_or_copy", p
)
30706 || !strcmp ("pcopy", p
))
30707 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
30708 else if (!strcmp ("present_or_copyin", p
)
30709 || !strcmp ("pcopyin", p
))
30710 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
30711 else if (!strcmp ("present_or_copyout", p
)
30712 || !strcmp ("pcopyout", p
))
30713 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
30714 else if (!strcmp ("present_or_create", p
)
30715 || !strcmp ("pcreate", p
))
30716 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
30717 else if (!strcmp ("priority", p
))
30718 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
30719 else if (!strcmp ("proc_bind", p
))
30720 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
30723 if (!strcmp ("reduction", p
))
30724 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
30727 if (!strcmp ("safelen", p
))
30728 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
30729 else if (!strcmp ("schedule", p
))
30730 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
30731 else if (!strcmp ("sections", p
))
30732 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
30733 else if (!strcmp ("self", p
))
30734 result
= PRAGMA_OACC_CLAUSE_SELF
;
30735 else if (!strcmp ("seq", p
))
30736 result
= PRAGMA_OACC_CLAUSE_SEQ
;
30737 else if (!strcmp ("shared", p
))
30738 result
= PRAGMA_OMP_CLAUSE_SHARED
;
30739 else if (!strcmp ("simd", p
))
30740 result
= PRAGMA_OMP_CLAUSE_SIMD
;
30741 else if (!strcmp ("simdlen", p
))
30742 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
30745 if (!strcmp ("taskgroup", p
))
30746 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
30747 else if (!strcmp ("thread_limit", p
))
30748 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
30749 else if (!strcmp ("threads", p
))
30750 result
= PRAGMA_OMP_CLAUSE_THREADS
;
30751 else if (!strcmp ("tile", p
))
30752 result
= PRAGMA_OACC_CLAUSE_TILE
;
30753 else if (!strcmp ("to", p
))
30754 result
= PRAGMA_OMP_CLAUSE_TO
;
30757 if (!strcmp ("uniform", p
))
30758 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
30759 else if (!strcmp ("untied", p
))
30760 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
30761 else if (!strcmp ("use_device", p
))
30762 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
30763 else if (!strcmp ("use_device_ptr", p
))
30764 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
30767 if (!strcmp ("vector", p
))
30768 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
30769 else if (!strcmp ("vector_length", p
))
30770 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
30771 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
30772 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
30775 if (!strcmp ("wait", p
))
30776 result
= PRAGMA_OACC_CLAUSE_WAIT
;
30777 else if (!strcmp ("worker", p
))
30778 result
= PRAGMA_OACC_CLAUSE_WORKER
;
30783 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
30784 cp_lexer_consume_token (parser
->lexer
);
30789 /* Validate that a clause of the given type does not already exist. */
30792 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
30793 const char *name
, location_t location
)
30797 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30798 if (OMP_CLAUSE_CODE (c
) == code
)
30800 error_at (location
, "too many %qs clauses", name
);
30808 variable-list , identifier
30810 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30811 colon). An opening parenthesis will have been consumed by the caller.
30813 If KIND is nonzero, create the appropriate node and install the decl
30814 in OMP_CLAUSE_DECL and add the node to the head of the list.
30816 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30817 return the list created.
30819 COLON can be NULL if only closing parenthesis should end the list,
30820 or pointer to bool which will receive false if the list is terminated
30821 by closing parenthesis or true if the list is terminated by colon. */
30824 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
30825 tree list
, bool *colon
)
30828 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30831 parser
->colon_corrects_to_scope_p
= false;
30838 token
= cp_lexer_peek_token (parser
->lexer
);
30840 && current_class_ptr
30841 && cp_parser_is_keyword (token
, RID_THIS
))
30843 decl
= finish_this_expr ();
30844 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
30845 || CONVERT_EXPR_P (decl
))
30846 decl
= TREE_OPERAND (decl
, 0);
30847 cp_lexer_consume_token (parser
->lexer
);
30851 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30852 /*check_dependency_p=*/true,
30853 /*template_p=*/NULL
,
30854 /*declarator_p=*/false,
30855 /*optional_p=*/false);
30856 if (name
== error_mark_node
)
30859 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
30860 if (decl
== error_mark_node
)
30861 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
30864 if (decl
== error_mark_node
)
30866 else if (kind
!= 0)
30870 case OMP_CLAUSE__CACHE_
:
30871 /* The OpenACC cache directive explicitly only allows "array
30872 elements or subarrays". */
30873 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
30875 error_at (token
->location
, "expected %<[%>");
30876 decl
= error_mark_node
;
30880 case OMP_CLAUSE_MAP
:
30881 case OMP_CLAUSE_FROM
:
30882 case OMP_CLAUSE_TO
:
30883 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
30886 = cp_lexer_peek_token (parser
->lexer
)->location
;
30887 cp_id_kind idk
= CP_ID_KIND_NONE
;
30888 cp_lexer_consume_token (parser
->lexer
);
30889 decl
= convert_from_reference (decl
);
30891 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
30896 case OMP_CLAUSE_DEPEND
:
30897 case OMP_CLAUSE_REDUCTION
:
30898 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
30900 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
30902 parser
->colon_corrects_to_scope_p
= false;
30903 cp_lexer_consume_token (parser
->lexer
);
30904 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30905 low_bound
= cp_parser_expression (parser
);
30907 parser
->colon_corrects_to_scope_p
30908 = saved_colon_corrects_to_scope_p
;
30909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
30910 length
= integer_one_node
;
30913 /* Look for `:'. */
30914 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30916 if (!cp_lexer_next_token_is (parser
->lexer
,
30918 length
= cp_parser_expression (parser
);
30920 /* Look for the closing `]'. */
30921 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
30925 decl
= tree_cons (low_bound
, length
, decl
);
30932 tree u
= build_omp_clause (token
->location
, kind
);
30933 OMP_CLAUSE_DECL (u
) = decl
;
30934 OMP_CLAUSE_CHAIN (u
) = list
;
30938 list
= tree_cons (decl
, NULL_TREE
, list
);
30941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
30943 cp_lexer_consume_token (parser
->lexer
);
30947 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30949 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30952 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30956 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30960 /* Try to resync to an unnested comma. Copied from
30961 cp_parser_parenthesized_expression_list. */
30964 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30965 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
30966 /*recovering=*/true,
30968 /*consume_paren=*/true);
30976 /* Similarly, but expect leading and trailing parenthesis. This is a very
30977 common case for omp clauses. */
30980 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
30982 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30983 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
30988 copy ( variable-list )
30989 copyin ( variable-list )
30990 copyout ( variable-list )
30991 create ( variable-list )
30992 delete ( variable-list )
30993 present ( variable-list )
30994 present_or_copy ( variable-list )
30995 pcopy ( variable-list )
30996 present_or_copyin ( variable-list )
30997 pcopyin ( variable-list )
30998 present_or_copyout ( variable-list )
30999 pcopyout ( variable-list )
31000 present_or_create ( variable-list )
31001 pcreate ( variable-list ) */
31004 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31007 enum gomp_map_kind kind
;
31010 case PRAGMA_OACC_CLAUSE_COPY
:
31011 kind
= GOMP_MAP_FORCE_TOFROM
;
31013 case PRAGMA_OACC_CLAUSE_COPYIN
:
31014 kind
= GOMP_MAP_FORCE_TO
;
31016 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31017 kind
= GOMP_MAP_FORCE_FROM
;
31019 case PRAGMA_OACC_CLAUSE_CREATE
:
31020 kind
= GOMP_MAP_FORCE_ALLOC
;
31022 case PRAGMA_OACC_CLAUSE_DELETE
:
31023 kind
= GOMP_MAP_DELETE
;
31025 case PRAGMA_OACC_CLAUSE_DEVICE
:
31026 kind
= GOMP_MAP_FORCE_TO
;
31028 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31029 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31031 case PRAGMA_OACC_CLAUSE_HOST
:
31032 case PRAGMA_OACC_CLAUSE_SELF
:
31033 kind
= GOMP_MAP_FORCE_FROM
;
31035 case PRAGMA_OACC_CLAUSE_LINK
:
31036 kind
= GOMP_MAP_LINK
;
31038 case PRAGMA_OACC_CLAUSE_PRESENT
:
31039 kind
= GOMP_MAP_FORCE_PRESENT
;
31041 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31042 kind
= GOMP_MAP_TOFROM
;
31044 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31045 kind
= GOMP_MAP_TO
;
31047 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31048 kind
= GOMP_MAP_FROM
;
31050 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31051 kind
= GOMP_MAP_ALLOC
;
31054 gcc_unreachable ();
31057 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31059 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31060 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31066 deviceptr ( variable-list ) */
31069 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31071 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31074 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31075 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31076 variable-list must only allow for pointer variables. */
31077 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31078 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31080 tree v
= TREE_PURPOSE (t
);
31081 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31082 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31083 OMP_CLAUSE_DECL (u
) = v
;
31084 OMP_CLAUSE_CHAIN (u
) = list
;
31098 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31099 enum omp_clause_code code
,
31100 tree list
, location_t location
)
31102 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31103 tree c
= build_omp_clause (location
, code
);
31104 OMP_CLAUSE_CHAIN (c
) = list
;
31109 num_gangs ( expression )
31110 num_workers ( expression )
31111 vector_length ( expression ) */
31114 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31115 const char *str
, tree list
)
31117 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31119 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31122 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31124 if (t
== error_mark_node
31125 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31127 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31128 /*or_comma=*/false,
31129 /*consume_paren=*/true);
31133 check_no_duplicate_clause (list
, code
, str
, loc
);
31135 tree c
= build_omp_clause (loc
, code
);
31136 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31137 OMP_CLAUSE_CHAIN (c
) = list
;
31143 gang [( gang-arg-list )]
31144 worker [( [num:] int-expr )]
31145 vector [( [length:] int-expr )]
31147 where gang-arg is one of:
31152 and size-expr may be:
31159 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31160 const char *str
, tree list
)
31162 const char *id
= "num";
31163 cp_lexer
*lexer
= parser
->lexer
;
31164 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31165 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31167 if (kind
== OMP_CLAUSE_VECTOR
)
31170 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31172 cp_lexer_consume_token (lexer
);
31176 cp_token
*next
= cp_lexer_peek_token (lexer
);
31179 /* Gang static argument. */
31180 if (kind
== OMP_CLAUSE_GANG
31181 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31183 cp_lexer_consume_token (lexer
);
31185 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31186 goto cleanup_error
;
31189 if (ops
[idx
] != NULL
)
31191 cp_parser_error (parser
, "too many %<static%> arguments");
31192 goto cleanup_error
;
31195 /* Check for the '*' argument. */
31196 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31197 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31198 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31201 cp_lexer_consume_token (lexer
);
31202 ops
[idx
] = integer_minus_one_node
;
31204 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31206 cp_lexer_consume_token (lexer
);
31212 /* Worker num: argument and vector length: arguments. */
31213 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31214 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
31215 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31217 cp_lexer_consume_token (lexer
); /* id */
31218 cp_lexer_consume_token (lexer
); /* ':' */
31221 /* Now collect the actual argument. */
31222 if (ops
[idx
] != NULL_TREE
)
31224 cp_parser_error (parser
, "unexpected argument");
31225 goto cleanup_error
;
31228 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31230 if (expr
== error_mark_node
)
31231 goto cleanup_error
;
31233 mark_exp_read (expr
);
31236 if (kind
== OMP_CLAUSE_GANG
31237 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31239 cp_lexer_consume_token (lexer
);
31246 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31247 goto cleanup_error
;
31250 check_no_duplicate_clause (list
, kind
, str
, loc
);
31252 c
= build_omp_clause (loc
, kind
);
31255 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31257 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31258 OMP_CLAUSE_CHAIN (c
) = list
;
31263 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31268 tile ( size-expr-list ) */
31271 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31273 tree c
, expr
= error_mark_node
;
31274 tree tile
= NULL_TREE
;
31276 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31277 so, but the spec authors never considered such a case and have
31278 differing opinions on what it might mean, including 'not
31280 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31281 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31284 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31289 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31293 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31294 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31296 cp_lexer_consume_token (parser
->lexer
);
31297 expr
= integer_zero_node
;
31300 expr
= cp_parser_constant_expression (parser
);
31302 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31304 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31306 /* Consume the trailing ')'. */
31307 cp_lexer_consume_token (parser
->lexer
);
31309 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31310 tile
= nreverse (tile
);
31311 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31312 OMP_CLAUSE_CHAIN (c
) = list
;
31317 Parse wait clause or directive parameters. */
31320 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31322 vec
<tree
, va_gc
> *args
;
31325 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31327 /*allow_expansion_p=*/true,
31328 /*non_constant_p=*/NULL
);
31330 if (args
== NULL
|| args
->length () == 0)
31332 cp_parser_error (parser
, "expected integer expression before ')'");
31334 release_tree_vector (args
);
31338 args_tree
= build_tree_list_vec (args
);
31340 release_tree_vector (args
);
31342 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31344 tree targ
= TREE_VALUE (t
);
31346 if (targ
!= error_mark_node
)
31348 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31349 error ("%<wait%> expression must be integral");
31352 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31354 mark_rvalue_use (targ
);
31355 OMP_CLAUSE_DECL (c
) = targ
;
31356 OMP_CLAUSE_CHAIN (c
) = list
;
31366 wait ( int-expr-list ) */
31369 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31371 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31373 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31376 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31382 collapse ( constant-expression ) */
31385 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31391 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31392 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31395 num
= cp_parser_constant_expression (parser
);
31397 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31398 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31399 /*or_comma=*/false,
31400 /*consume_paren=*/true);
31402 if (num
== error_mark_node
)
31404 num
= fold_non_dependent_expr (num
);
31405 if (!tree_fits_shwi_p (num
)
31406 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31407 || (n
= tree_to_shwi (num
)) <= 0
31410 error_at (loc
, "collapse argument needs positive constant integer expression");
31414 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31415 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31416 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31417 OMP_CLAUSE_CHAIN (c
) = list
;
31418 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31424 default ( shared | none )
31430 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31431 location_t location
, bool is_oacc
)
31433 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31436 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31440 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31441 const char *p
= IDENTIFIER_POINTER (id
);
31446 if (strcmp ("none", p
) != 0)
31448 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31452 if (strcmp ("shared", p
) != 0 || is_oacc
)
31454 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31461 cp_lexer_consume_token (parser
->lexer
);
31467 cp_parser_error (parser
, "expected %<none%>");
31469 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31472 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31473 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31474 /*or_comma=*/false,
31475 /*consume_paren=*/true);
31477 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31480 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31481 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31482 OMP_CLAUSE_CHAIN (c
) = list
;
31483 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31489 final ( expression ) */
31492 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31496 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31499 t
= cp_parser_condition (parser
);
31501 if (t
== error_mark_node
31502 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31504 /*or_comma=*/false,
31505 /*consume_paren=*/true);
31507 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31509 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31510 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31511 OMP_CLAUSE_CHAIN (c
) = list
;
31520 if ( directive-name-modifier : expression )
31522 directive-name-modifier:
31523 parallel | task | taskloop | target data | target | target update
31524 | target enter data | target exit data */
31527 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31531 enum tree_code if_modifier
= ERROR_MARK
;
31533 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31536 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31538 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31539 const char *p
= IDENTIFIER_POINTER (id
);
31542 if (strcmp ("parallel", p
) == 0)
31543 if_modifier
= OMP_PARALLEL
;
31544 else if (strcmp ("task", p
) == 0)
31545 if_modifier
= OMP_TASK
;
31546 else if (strcmp ("taskloop", p
) == 0)
31547 if_modifier
= OMP_TASKLOOP
;
31548 else if (strcmp ("target", p
) == 0)
31550 if_modifier
= OMP_TARGET
;
31551 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31553 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31554 p
= IDENTIFIER_POINTER (id
);
31555 if (strcmp ("data", p
) == 0)
31556 if_modifier
= OMP_TARGET_DATA
;
31557 else if (strcmp ("update", p
) == 0)
31558 if_modifier
= OMP_TARGET_UPDATE
;
31559 else if (strcmp ("enter", p
) == 0)
31560 if_modifier
= OMP_TARGET_ENTER_DATA
;
31561 else if (strcmp ("exit", p
) == 0)
31562 if_modifier
= OMP_TARGET_EXIT_DATA
;
31563 if (if_modifier
!= OMP_TARGET
)
31568 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
31569 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
31571 if_modifier
= ERROR_MARK
;
31573 if (if_modifier
== OMP_TARGET_ENTER_DATA
31574 || if_modifier
== OMP_TARGET_EXIT_DATA
)
31576 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
31578 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
31579 p
= IDENTIFIER_POINTER (id
);
31580 if (strcmp ("data", p
) == 0)
31586 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
31587 error_at (loc
, "expected %<data%>");
31588 if_modifier
= ERROR_MARK
;
31593 if (if_modifier
!= ERROR_MARK
)
31595 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
31598 cp_lexer_consume_token (parser
->lexer
);
31605 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
31606 error_at (loc
, "expected %<:%>");
31608 if_modifier
= ERROR_MARK
;
31613 t
= cp_parser_condition (parser
);
31615 if (t
== error_mark_node
31616 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31617 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31618 /*or_comma=*/false,
31619 /*consume_paren=*/true);
31621 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31622 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
31624 if (if_modifier
!= ERROR_MARK
31625 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31627 const char *p
= NULL
;
31628 switch (if_modifier
)
31630 case OMP_PARALLEL
: p
= "parallel"; break;
31631 case OMP_TASK
: p
= "task"; break;
31632 case OMP_TASKLOOP
: p
= "taskloop"; break;
31633 case OMP_TARGET_DATA
: p
= "target data"; break;
31634 case OMP_TARGET
: p
= "target"; break;
31635 case OMP_TARGET_UPDATE
: p
= "target update"; break;
31636 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
31637 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
31638 default: gcc_unreachable ();
31640 error_at (location
, "too many %<if%> clauses with %qs modifier",
31644 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31647 error_at (location
, "too many %<if%> clauses");
31649 error_at (location
, "too many %<if%> clauses without modifier");
31652 else if (if_modifier
== ERROR_MARK
31653 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
31655 error_at (location
, "if any %<if%> clause has modifier, then all "
31656 "%<if%> clauses have to use modifier");
31661 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
31662 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
31663 OMP_CLAUSE_IF_EXPR (c
) = t
;
31664 OMP_CLAUSE_CHAIN (c
) = list
;
31673 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
31674 tree list
, location_t location
)
31678 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
31681 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
31682 OMP_CLAUSE_CHAIN (c
) = list
;
31690 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
31691 tree list
, location_t location
)
31695 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
31697 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
31698 OMP_CLAUSE_CHAIN (c
) = list
;
31703 num_threads ( expression ) */
31706 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
31707 location_t location
)
31711 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31714 t
= cp_parser_expression (parser
);
31716 if (t
== error_mark_node
31717 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31718 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31719 /*or_comma=*/false,
31720 /*consume_paren=*/true);
31722 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
31723 "num_threads", location
);
31725 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
31726 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
31727 OMP_CLAUSE_CHAIN (c
) = list
;
31733 num_tasks ( expression ) */
31736 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
31737 location_t location
)
31741 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31744 t
= cp_parser_expression (parser
);
31746 if (t
== error_mark_node
31747 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31748 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31749 /*or_comma=*/false,
31750 /*consume_paren=*/true);
31752 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
31753 "num_tasks", location
);
31755 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
31756 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
31757 OMP_CLAUSE_CHAIN (c
) = list
;
31763 grainsize ( expression ) */
31766 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
31767 location_t location
)
31771 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31774 t
= cp_parser_expression (parser
);
31776 if (t
== error_mark_node
31777 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31778 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31779 /*or_comma=*/false,
31780 /*consume_paren=*/true);
31782 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
31783 "grainsize", location
);
31785 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
31786 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
31787 OMP_CLAUSE_CHAIN (c
) = list
;
31793 priority ( expression ) */
31796 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
31797 location_t location
)
31801 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31804 t
= cp_parser_expression (parser
);
31806 if (t
== error_mark_node
31807 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31808 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31809 /*or_comma=*/false,
31810 /*consume_paren=*/true);
31812 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
31813 "priority", location
);
31815 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
31816 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
31817 OMP_CLAUSE_CHAIN (c
) = list
;
31823 hint ( expression ) */
31826 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
31827 location_t location
)
31831 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31834 t
= cp_parser_expression (parser
);
31836 if (t
== error_mark_node
31837 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31838 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31839 /*or_comma=*/false,
31840 /*consume_paren=*/true);
31842 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
31844 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
31845 OMP_CLAUSE_HINT_EXPR (c
) = t
;
31846 OMP_CLAUSE_CHAIN (c
) = list
;
31852 defaultmap ( tofrom : scalar ) */
31855 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
31856 location_t location
)
31861 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31864 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31866 cp_parser_error (parser
, "expected %<tofrom%>");
31869 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31870 p
= IDENTIFIER_POINTER (id
);
31871 if (strcmp (p
, "tofrom") != 0)
31873 cp_parser_error (parser
, "expected %<tofrom%>");
31876 cp_lexer_consume_token (parser
->lexer
);
31877 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31880 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31882 cp_parser_error (parser
, "expected %<scalar%>");
31885 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31886 p
= IDENTIFIER_POINTER (id
);
31887 if (strcmp (p
, "scalar") != 0)
31889 cp_parser_error (parser
, "expected %<scalar%>");
31892 cp_lexer_consume_token (parser
->lexer
);
31893 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31896 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
31899 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
31900 OMP_CLAUSE_CHAIN (c
) = list
;
31904 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31905 /*or_comma=*/false,
31906 /*consume_paren=*/true);
31914 ordered ( constant-expression ) */
31917 cp_parser_omp_clause_ordered (cp_parser
*parser
,
31918 tree list
, location_t location
)
31920 tree c
, num
= NULL_TREE
;
31923 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
31924 "ordered", location
);
31926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31928 cp_lexer_consume_token (parser
->lexer
);
31930 num
= cp_parser_constant_expression (parser
);
31932 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31934 /*or_comma=*/false,
31935 /*consume_paren=*/true);
31937 if (num
== error_mark_node
)
31939 num
= fold_non_dependent_expr (num
);
31940 if (!tree_fits_shwi_p (num
)
31941 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31942 || (n
= tree_to_shwi (num
)) <= 0
31945 error_at (location
,
31946 "ordered argument needs positive constant integer "
31952 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
31953 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
31954 OMP_CLAUSE_CHAIN (c
) = list
;
31959 reduction ( reduction-operator : variable-list )
31961 reduction-operator:
31962 One of: + * - & ^ | && ||
31966 reduction-operator:
31967 One of: + * - & ^ | && || min max
31971 reduction-operator:
31972 One of: + * - & ^ | && ||
31976 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
31978 enum tree_code code
= ERROR_MARK
;
31979 tree nlist
, c
, id
= NULL_TREE
;
31981 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31984 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31986 case CPP_PLUS
: code
= PLUS_EXPR
; break;
31987 case CPP_MULT
: code
= MULT_EXPR
; break;
31988 case CPP_MINUS
: code
= MINUS_EXPR
; break;
31989 case CPP_AND
: code
= BIT_AND_EXPR
; break;
31990 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
31991 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
31992 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
31993 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
31997 if (code
!= ERROR_MARK
)
31998 cp_lexer_consume_token (parser
->lexer
);
32001 bool saved_colon_corrects_to_scope_p
;
32002 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32003 parser
->colon_corrects_to_scope_p
= false;
32004 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32005 /*check_dependency_p=*/true,
32006 /*template_p=*/NULL
,
32007 /*declarator_p=*/false,
32008 /*optional_p=*/false);
32009 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32010 if (identifier_p (id
))
32012 const char *p
= IDENTIFIER_POINTER (id
);
32014 if (strcmp (p
, "min") == 0)
32016 else if (strcmp (p
, "max") == 0)
32018 else if (id
== cp_operator_id (PLUS_EXPR
))
32020 else if (id
== cp_operator_id (MULT_EXPR
))
32022 else if (id
== cp_operator_id (MINUS_EXPR
))
32024 else if (id
== cp_operator_id (BIT_AND_EXPR
))
32025 code
= BIT_AND_EXPR
;
32026 else if (id
== cp_operator_id (BIT_IOR_EXPR
))
32027 code
= BIT_IOR_EXPR
;
32028 else if (id
== cp_operator_id (BIT_XOR_EXPR
))
32029 code
= BIT_XOR_EXPR
;
32030 else if (id
== cp_operator_id (TRUTH_ANDIF_EXPR
))
32031 code
= TRUTH_ANDIF_EXPR
;
32032 else if (id
== cp_operator_id (TRUTH_ORIF_EXPR
))
32033 code
= TRUTH_ORIF_EXPR
;
32034 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32035 tree scope
= parser
->scope
;
32037 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32038 parser
->scope
= NULL_TREE
;
32039 parser
->qualifying_scope
= NULL_TREE
;
32040 parser
->object_scope
= NULL_TREE
;
32044 error ("invalid reduction-identifier");
32046 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32047 /*or_comma=*/false,
32048 /*consume_paren=*/true);
32053 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32056 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32058 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32060 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32061 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32068 schedule ( schedule-kind )
32069 schedule ( schedule-kind , expression )
32072 static | dynamic | guided | runtime | auto
32075 schedule ( schedule-modifier : schedule-kind )
32076 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32084 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32087 int modifiers
= 0, nmodifiers
= 0;
32089 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32092 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32094 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32096 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32097 const char *p
= IDENTIFIER_POINTER (id
);
32098 if (strcmp ("simd", p
) == 0)
32099 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32100 else if (strcmp ("monotonic", p
) == 0)
32101 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32102 else if (strcmp ("nonmonotonic", p
) == 0)
32103 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32106 cp_lexer_consume_token (parser
->lexer
);
32107 if (nmodifiers
++ == 0
32108 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32109 cp_lexer_consume_token (parser
->lexer
);
32112 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32119 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32120 const char *p
= IDENTIFIER_POINTER (id
);
32125 if (strcmp ("dynamic", p
) != 0)
32127 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32131 if (strcmp ("guided", p
) != 0)
32133 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32137 if (strcmp ("runtime", p
) != 0)
32139 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32146 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32147 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32148 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32149 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32152 cp_lexer_consume_token (parser
->lexer
);
32154 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32155 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32156 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32157 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32159 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32167 cp_lexer_consume_token (parser
->lexer
);
32169 token
= cp_lexer_peek_token (parser
->lexer
);
32170 t
= cp_parser_assignment_expression (parser
);
32172 if (t
== error_mark_node
)
32174 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32175 error_at (token
->location
, "schedule %<runtime%> does not take "
32176 "a %<chunk_size%> parameter");
32177 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32178 error_at (token
->location
, "schedule %<auto%> does not take "
32179 "a %<chunk_size%> parameter");
32181 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32183 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32186 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32189 OMP_CLAUSE_SCHEDULE_KIND (c
)
32190 = (enum omp_clause_schedule_kind
)
32191 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32193 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32194 OMP_CLAUSE_CHAIN (c
) = list
;
32198 cp_parser_error (parser
, "invalid schedule kind");
32200 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32201 /*or_comma=*/false,
32202 /*consume_paren=*/true);
32210 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32211 tree list
, location_t location
)
32215 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32217 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32218 OMP_CLAUSE_CHAIN (c
) = list
;
32227 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32228 tree list
, location_t location
)
32230 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32231 tree c
= build_omp_clause (location
, code
);
32232 OMP_CLAUSE_CHAIN (c
) = list
;
32243 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32244 enum omp_clause_code code
,
32245 tree list
, location_t location
)
32247 tree c
= build_omp_clause (location
, code
);
32248 OMP_CLAUSE_CHAIN (c
) = list
;
32256 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32257 tree list
, location_t location
)
32259 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32260 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32261 OMP_CLAUSE_CHAIN (c
) = list
;
32270 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32271 enum omp_clause_code code
,
32272 tree list
, location_t location
)
32274 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32275 tree c
= build_omp_clause (location
, code
);
32276 OMP_CLAUSE_CHAIN (c
) = list
;
32281 num_teams ( expression ) */
32284 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32285 location_t location
)
32289 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32292 t
= cp_parser_expression (parser
);
32294 if (t
== error_mark_node
32295 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32296 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32297 /*or_comma=*/false,
32298 /*consume_paren=*/true);
32300 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32301 "num_teams", location
);
32303 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32304 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32305 OMP_CLAUSE_CHAIN (c
) = list
;
32311 thread_limit ( expression ) */
32314 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32315 location_t location
)
32319 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32322 t
= cp_parser_expression (parser
);
32324 if (t
== error_mark_node
32325 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32326 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32327 /*or_comma=*/false,
32328 /*consume_paren=*/true);
32330 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32331 "thread_limit", location
);
32333 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32334 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32335 OMP_CLAUSE_CHAIN (c
) = list
;
32341 aligned ( variable-list )
32342 aligned ( variable-list : constant-expression ) */
32345 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32347 tree nlist
, c
, alignment
= NULL_TREE
;
32350 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32353 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32358 alignment
= cp_parser_constant_expression (parser
);
32360 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32361 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32362 /*or_comma=*/false,
32363 /*consume_paren=*/true);
32365 if (alignment
== error_mark_node
)
32366 alignment
= NULL_TREE
;
32369 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32370 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32376 linear ( variable-list )
32377 linear ( variable-list : expression )
32380 linear ( modifier ( variable-list ) )
32381 linear ( modifier ( variable-list ) : expression ) */
32384 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32385 bool is_cilk_simd_fn
, bool declare_simd
)
32387 tree nlist
, c
, step
= integer_one_node
;
32389 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32391 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32394 if (!is_cilk_simd_fn
32395 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32397 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32398 const char *p
= IDENTIFIER_POINTER (id
);
32400 if (strcmp ("ref", p
) == 0)
32401 kind
= OMP_CLAUSE_LINEAR_REF
;
32402 else if (strcmp ("val", p
) == 0)
32403 kind
= OMP_CLAUSE_LINEAR_VAL
;
32404 else if (strcmp ("uval", p
) == 0)
32405 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32406 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32407 cp_lexer_consume_token (parser
->lexer
);
32409 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32412 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32413 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32417 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32418 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32420 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32421 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32422 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32423 /*or_comma=*/false,
32424 /*consume_paren=*/true);
32431 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32432 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32434 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32435 cp_parser_parse_tentatively (parser
);
32436 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32437 /*check_dependency_p=*/true,
32438 /*template_p=*/NULL
,
32439 /*declarator_p=*/false,
32440 /*optional_p=*/false);
32441 if (step
!= error_mark_node
)
32442 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32443 if (step
== error_mark_node
)
32446 cp_parser_abort_tentative_parse (parser
);
32448 else if (!cp_parser_parse_definitely (parser
))
32452 step
= cp_parser_expression (parser
);
32454 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
32456 sorry ("using parameters for %<linear%> step is not supported yet");
32457 step
= integer_one_node
;
32459 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32460 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32461 /*or_comma=*/false,
32462 /*consume_paren=*/true);
32464 if (step
== error_mark_node
)
32468 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32470 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32471 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32478 safelen ( constant-expression ) */
32481 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32482 location_t location
)
32486 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32489 t
= cp_parser_constant_expression (parser
);
32491 if (t
== error_mark_node
32492 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32493 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32494 /*or_comma=*/false,
32495 /*consume_paren=*/true);
32497 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32499 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32500 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32501 OMP_CLAUSE_CHAIN (c
) = list
;
32507 simdlen ( constant-expression ) */
32510 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32511 location_t location
)
32515 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32518 t
= cp_parser_constant_expression (parser
);
32520 if (t
== error_mark_node
32521 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32522 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32523 /*or_comma=*/false,
32524 /*consume_paren=*/true);
32526 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32528 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32529 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32530 OMP_CLAUSE_CHAIN (c
) = list
;
32537 identifier [+/- integer]
32538 vec , identifier [+/- integer]
32542 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32547 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32549 cp_parser_error (parser
, "expected identifier");
32553 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32555 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32556 tree t
, identifier
= cp_parser_identifier (parser
);
32557 tree addend
= NULL
;
32559 if (identifier
== error_mark_node
)
32560 t
= error_mark_node
;
32563 t
= cp_parser_lookup_name_simple
32564 (parser
, identifier
,
32565 cp_lexer_peek_token (parser
->lexer
)->location
);
32566 if (t
== error_mark_node
)
32567 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
32572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
32574 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
32576 addend
= integer_zero_node
;
32577 goto add_to_vector
;
32579 cp_lexer_consume_token (parser
->lexer
);
32581 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
32583 cp_parser_error (parser
, "expected integer");
32587 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32588 if (TREE_CODE (addend
) != INTEGER_CST
)
32590 cp_parser_error (parser
, "expected integer");
32593 cp_lexer_consume_token (parser
->lexer
);
32596 if (t
!= error_mark_node
)
32598 vec
= tree_cons (addend
, t
, vec
);
32600 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
32603 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32606 cp_lexer_consume_token (parser
->lexer
);
32609 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
32611 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
32612 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
32613 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
32614 OMP_CLAUSE_CHAIN (u
) = list
;
32621 depend ( depend-kind : variable-list )
32629 depend ( sink : vec ) */
32632 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
32635 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
32637 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32642 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32643 const char *p
= IDENTIFIER_POINTER (id
);
32645 if (strcmp ("in", p
) == 0)
32646 kind
= OMP_CLAUSE_DEPEND_IN
;
32647 else if (strcmp ("inout", p
) == 0)
32648 kind
= OMP_CLAUSE_DEPEND_INOUT
;
32649 else if (strcmp ("out", p
) == 0)
32650 kind
= OMP_CLAUSE_DEPEND_OUT
;
32651 else if (strcmp ("source", p
) == 0)
32652 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
32653 else if (strcmp ("sink", p
) == 0)
32654 kind
= OMP_CLAUSE_DEPEND_SINK
;
32661 cp_lexer_consume_token (parser
->lexer
);
32663 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
32665 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
32666 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32667 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
32668 OMP_CLAUSE_CHAIN (c
) = list
;
32669 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32670 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32671 /*or_comma=*/false,
32672 /*consume_paren=*/true);
32676 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32679 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
32680 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
32683 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
32686 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32687 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32692 cp_parser_error (parser
, "invalid depend kind");
32694 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32695 /*or_comma=*/false,
32696 /*consume_paren=*/true);
32701 map ( map-kind : variable-list )
32702 map ( variable-list )
32705 alloc | to | from | tofrom
32709 alloc | to | from | tofrom | release | delete
32711 map ( always [,] map-kind: variable-list ) */
32714 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
32717 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
32718 bool always
= false;
32720 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32725 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32726 const char *p
= IDENTIFIER_POINTER (id
);
32728 if (strcmp ("always", p
) == 0)
32731 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
32733 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
32734 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
32736 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
32740 cp_lexer_consume_token (parser
->lexer
);
32742 cp_lexer_consume_token (parser
->lexer
);
32747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32748 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32750 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32751 const char *p
= IDENTIFIER_POINTER (id
);
32753 if (strcmp ("alloc", p
) == 0)
32754 kind
= GOMP_MAP_ALLOC
;
32755 else if (strcmp ("to", p
) == 0)
32756 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
32757 else if (strcmp ("from", p
) == 0)
32758 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
32759 else if (strcmp ("tofrom", p
) == 0)
32760 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
32761 else if (strcmp ("release", p
) == 0)
32762 kind
= GOMP_MAP_RELEASE
;
32765 cp_parser_error (parser
, "invalid map kind");
32766 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32767 /*or_comma=*/false,
32768 /*consume_paren=*/true);
32771 cp_lexer_consume_token (parser
->lexer
);
32772 cp_lexer_consume_token (parser
->lexer
);
32774 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
32775 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32777 kind
= GOMP_MAP_DELETE
;
32778 cp_lexer_consume_token (parser
->lexer
);
32779 cp_lexer_consume_token (parser
->lexer
);
32782 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
32785 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32786 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32792 device ( expression ) */
32795 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
32796 location_t location
)
32800 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32803 t
= cp_parser_expression (parser
);
32805 if (t
== error_mark_node
32806 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32807 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32808 /*or_comma=*/false,
32809 /*consume_paren=*/true);
32811 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
32812 "device", location
);
32814 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
32815 OMP_CLAUSE_DEVICE_ID (c
) = t
;
32816 OMP_CLAUSE_CHAIN (c
) = list
;
32822 dist_schedule ( static )
32823 dist_schedule ( static , expression ) */
32826 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
32827 location_t location
)
32831 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32834 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
32836 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32838 cp_lexer_consume_token (parser
->lexer
);
32840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32842 cp_lexer_consume_token (parser
->lexer
);
32844 t
= cp_parser_assignment_expression (parser
);
32846 if (t
== error_mark_node
)
32848 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
32850 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32853 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32856 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
32858 OMP_CLAUSE_CHAIN (c
) = list
;
32862 cp_parser_error (parser
, "invalid dist_schedule kind");
32864 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32865 /*or_comma=*/false,
32866 /*consume_paren=*/true);
32871 proc_bind ( proc-bind-kind )
32874 master | close | spread */
32877 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
32878 location_t location
)
32881 enum omp_clause_proc_bind_kind kind
;
32883 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32888 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32889 const char *p
= IDENTIFIER_POINTER (id
);
32891 if (strcmp ("master", p
) == 0)
32892 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
32893 else if (strcmp ("close", p
) == 0)
32894 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
32895 else if (strcmp ("spread", p
) == 0)
32896 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
32903 cp_lexer_consume_token (parser
->lexer
);
32904 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32907 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
32908 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
32910 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
32911 OMP_CLAUSE_CHAIN (c
) = list
;
32915 cp_parser_error (parser
, "invalid depend kind");
32917 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32918 /*or_comma=*/false,
32919 /*consume_paren=*/true);
32924 async [( int-expr )] */
32927 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
32930 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32932 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
32934 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32936 cp_lexer_consume_token (parser
->lexer
);
32938 t
= cp_parser_expression (parser
);
32939 if (t
== error_mark_node
32940 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32941 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32942 /*or_comma=*/false,
32943 /*consume_paren=*/true);
32946 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
32948 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
32949 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
32950 OMP_CLAUSE_CHAIN (c
) = list
;
32956 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32957 is a bitmask in MASK. Return the list of clauses found. */
32960 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
32961 const char *where
, cp_token
*pragma_tok
,
32962 bool finish_p
= true)
32964 tree clauses
= NULL
;
32967 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32970 pragma_omp_clause c_kind
;
32971 omp_clause_code code
;
32972 const char *c_name
;
32973 tree prev
= clauses
;
32975 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32976 cp_lexer_consume_token (parser
->lexer
);
32978 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
32979 c_kind
= cp_parser_omp_clause_name (parser
);
32983 case PRAGMA_OACC_CLAUSE_ASYNC
:
32984 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
32987 case PRAGMA_OACC_CLAUSE_AUTO
:
32988 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
32992 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
32993 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
32994 c_name
= "collapse";
32996 case PRAGMA_OACC_CLAUSE_COPY
:
32997 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33000 case PRAGMA_OACC_CLAUSE_COPYIN
:
33001 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33004 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33005 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33006 c_name
= "copyout";
33008 case PRAGMA_OACC_CLAUSE_CREATE
:
33009 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33012 case PRAGMA_OACC_CLAUSE_DELETE
:
33013 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33016 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33017 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33018 c_name
= "default";
33020 case PRAGMA_OACC_CLAUSE_DEVICE
:
33021 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33024 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33025 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33026 c_name
= "deviceptr";
33028 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33029 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33030 c_name
= "device_resident";
33032 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33033 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33035 c_name
= "firstprivate";
33037 case PRAGMA_OACC_CLAUSE_GANG
:
33039 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33042 case PRAGMA_OACC_CLAUSE_HOST
:
33043 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33046 case PRAGMA_OACC_CLAUSE_IF
:
33047 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33050 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33051 clauses
= cp_parser_oacc_simple_clause (parser
,
33052 OMP_CLAUSE_INDEPENDENT
,
33054 c_name
= "independent";
33056 case PRAGMA_OACC_CLAUSE_LINK
:
33057 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33060 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33061 code
= OMP_CLAUSE_NUM_GANGS
;
33062 c_name
= "num_gangs";
33063 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33066 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33067 c_name
= "num_workers";
33068 code
= OMP_CLAUSE_NUM_WORKERS
;
33069 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33072 case PRAGMA_OACC_CLAUSE_PRESENT
:
33073 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33074 c_name
= "present";
33076 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33077 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33078 c_name
= "present_or_copy";
33080 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33081 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33082 c_name
= "present_or_copyin";
33084 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33085 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33086 c_name
= "present_or_copyout";
33088 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33089 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33090 c_name
= "present_or_create";
33092 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33093 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33095 c_name
= "private";
33097 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33098 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33099 c_name
= "reduction";
33101 case PRAGMA_OACC_CLAUSE_SELF
:
33102 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33105 case PRAGMA_OACC_CLAUSE_SEQ
:
33106 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33110 case PRAGMA_OACC_CLAUSE_TILE
:
33111 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33114 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33115 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33117 c_name
= "use_device";
33119 case PRAGMA_OACC_CLAUSE_VECTOR
:
33121 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33124 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33125 c_name
= "vector_length";
33126 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33127 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33130 case PRAGMA_OACC_CLAUSE_WAIT
:
33131 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33134 case PRAGMA_OACC_CLAUSE_WORKER
:
33136 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33140 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33146 if (((mask
>> c_kind
) & 1) == 0)
33148 /* Remove the invalid clause(s) from the list to avoid
33149 confusing the rest of the compiler. */
33151 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33156 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33159 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33164 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33165 is a bitmask in MASK. Return the list of clauses found; the result
33166 of clause default goes in *pdefault. */
33169 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33170 const char *where
, cp_token
*pragma_tok
,
33171 bool finish_p
= true)
33173 tree clauses
= NULL
;
33175 cp_token
*token
= NULL
;
33177 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33179 pragma_omp_clause c_kind
;
33180 const char *c_name
;
33181 tree prev
= clauses
;
33183 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33184 cp_lexer_consume_token (parser
->lexer
);
33186 token
= cp_lexer_peek_token (parser
->lexer
);
33187 c_kind
= cp_parser_omp_clause_name (parser
);
33191 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33192 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33194 c_name
= "collapse";
33196 case PRAGMA_OMP_CLAUSE_COPYIN
:
33197 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33200 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33201 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33203 c_name
= "copyprivate";
33205 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33206 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33207 token
->location
, false);
33208 c_name
= "default";
33210 case PRAGMA_OMP_CLAUSE_FINAL
:
33211 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33214 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33215 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33217 c_name
= "firstprivate";
33219 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33220 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33222 c_name
= "grainsize";
33224 case PRAGMA_OMP_CLAUSE_HINT
:
33225 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33229 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33230 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33232 c_name
= "defaultmap";
33234 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33235 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33237 c_name
= "use_device_ptr";
33239 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33240 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33242 c_name
= "is_device_ptr";
33244 case PRAGMA_OMP_CLAUSE_IF
:
33245 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33249 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33250 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33252 c_name
= "lastprivate";
33254 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33255 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33257 c_name
= "mergeable";
33259 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33260 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33263 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33264 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33266 c_name
= "num_tasks";
33268 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33269 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33271 c_name
= "num_threads";
33273 case PRAGMA_OMP_CLAUSE_ORDERED
:
33274 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33276 c_name
= "ordered";
33278 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33279 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33281 c_name
= "priority";
33283 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33284 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33286 c_name
= "private";
33288 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33289 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33290 c_name
= "reduction";
33292 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33293 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33295 c_name
= "schedule";
33297 case PRAGMA_OMP_CLAUSE_SHARED
:
33298 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33302 case PRAGMA_OMP_CLAUSE_UNTIED
:
33303 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33307 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33308 case PRAGMA_CILK_CLAUSE_MASK
:
33309 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33310 clauses
, token
->location
);
33311 c_name
= "inbranch";
33313 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33314 case PRAGMA_CILK_CLAUSE_NOMASK
:
33315 clauses
= cp_parser_omp_clause_branch (parser
,
33316 OMP_CLAUSE_NOTINBRANCH
,
33317 clauses
, token
->location
);
33318 c_name
= "notinbranch";
33320 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33321 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33322 clauses
, token
->location
);
33323 c_name
= "parallel";
33327 error_at (token
->location
, "%qs must be the first clause of %qs",
33332 case PRAGMA_OMP_CLAUSE_FOR
:
33333 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33334 clauses
, token
->location
);
33337 goto clause_not_first
;
33339 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33340 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33341 clauses
, token
->location
);
33342 c_name
= "sections";
33344 goto clause_not_first
;
33346 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33347 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33348 clauses
, token
->location
);
33349 c_name
= "taskgroup";
33351 goto clause_not_first
;
33353 case PRAGMA_OMP_CLAUSE_LINK
:
33354 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33357 case PRAGMA_OMP_CLAUSE_TO
:
33358 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33359 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33362 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33365 case PRAGMA_OMP_CLAUSE_FROM
:
33366 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33369 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33370 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33372 c_name
= "uniform";
33374 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33375 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33377 c_name
= "num_teams";
33379 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33380 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33382 c_name
= "thread_limit";
33384 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33385 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33386 c_name
= "aligned";
33388 case PRAGMA_OMP_CLAUSE_LINEAR
:
33390 bool cilk_simd_fn
= false, declare_simd
= false;
33391 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
33392 cilk_simd_fn
= true;
33393 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33394 declare_simd
= true;
33395 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
33396 cilk_simd_fn
, declare_simd
);
33400 case PRAGMA_OMP_CLAUSE_DEPEND
:
33401 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33405 case PRAGMA_OMP_CLAUSE_MAP
:
33406 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33409 case PRAGMA_OMP_CLAUSE_DEVICE
:
33410 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33414 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33415 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33417 c_name
= "dist_schedule";
33419 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33420 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33422 c_name
= "proc_bind";
33424 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33425 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33427 c_name
= "safelen";
33429 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33430 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33432 c_name
= "simdlen";
33434 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33435 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33437 c_name
= "nogroup";
33439 case PRAGMA_OMP_CLAUSE_THREADS
:
33441 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33442 clauses
, token
->location
);
33443 c_name
= "threads";
33445 case PRAGMA_OMP_CLAUSE_SIMD
:
33447 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33448 clauses
, token
->location
);
33451 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
33452 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
33453 c_name
= "simdlen";
33456 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33462 if (((mask
>> c_kind
) & 1) == 0)
33464 /* Remove the invalid clause(s) from the list to avoid
33465 confusing the rest of the compiler. */
33467 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33471 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33472 no reason to skip to the end. */
33473 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
33474 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33477 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33478 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33480 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33489 In practice, we're also interested in adding the statement to an
33490 outer node. So it is convenient if we work around the fact that
33491 cp_parser_statement calls add_stmt. */
33494 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33496 unsigned save
= parser
->in_statement
;
33498 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33499 This preserves the "not within loop or switch" style error messages
33500 for nonsense cases like
33506 if (parser
->in_statement
)
33507 parser
->in_statement
= IN_OMP_BLOCK
;
33513 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33515 parser
->in_statement
= save
;
33519 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33521 tree stmt
= begin_omp_structured_block ();
33522 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33524 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33526 cp_parser_end_omp_structured_block (parser
, save
);
33527 return finish_omp_structured_block (stmt
);
33531 # pragma omp atomic new-line
33535 x binop= expr | x++ | ++x | x-- | --x
33537 +, *, -, /, &, ^, |, <<, >>
33539 where x is an lvalue expression with scalar type.
33542 # pragma omp atomic new-line
33545 # pragma omp atomic read new-line
33548 # pragma omp atomic write new-line
33551 # pragma omp atomic update new-line
33554 # pragma omp atomic capture new-line
33557 # pragma omp atomic capture new-line
33565 expression-stmt | x = x binop expr
33567 v = expression-stmt
33569 { v = x; update-stmt; } | { update-stmt; v = x; }
33573 expression-stmt | x = x binop expr | x = expr binop x
33577 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33579 where x and v are lvalue expressions with scalar type. */
33582 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
33584 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
33585 tree rhs1
= NULL_TREE
, orig_lhs
;
33586 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
33587 bool structured_block
= false;
33588 bool seq_cst
= false;
33590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33592 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33593 const char *p
= IDENTIFIER_POINTER (id
);
33595 if (!strcmp (p
, "seq_cst"))
33598 cp_lexer_consume_token (parser
->lexer
);
33599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33600 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33601 cp_lexer_consume_token (parser
->lexer
);
33604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33606 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33607 const char *p
= IDENTIFIER_POINTER (id
);
33609 if (!strcmp (p
, "read"))
33610 code
= OMP_ATOMIC_READ
;
33611 else if (!strcmp (p
, "write"))
33613 else if (!strcmp (p
, "update"))
33615 else if (!strcmp (p
, "capture"))
33616 code
= OMP_ATOMIC_CAPTURE_NEW
;
33620 cp_lexer_consume_token (parser
->lexer
);
33624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33625 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33626 cp_lexer_consume_token (parser
->lexer
);
33628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33630 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33631 const char *p
= IDENTIFIER_POINTER (id
);
33633 if (!strcmp (p
, "seq_cst"))
33636 cp_lexer_consume_token (parser
->lexer
);
33640 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33644 case OMP_ATOMIC_READ
:
33645 case NOP_EXPR
: /* atomic write */
33646 v
= cp_parser_unary_expression (parser
);
33647 if (v
== error_mark_node
)
33649 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33651 if (code
== NOP_EXPR
)
33652 lhs
= cp_parser_expression (parser
);
33654 lhs
= cp_parser_unary_expression (parser
);
33655 if (lhs
== error_mark_node
)
33657 if (code
== NOP_EXPR
)
33659 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33667 case OMP_ATOMIC_CAPTURE_NEW
:
33668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33670 cp_lexer_consume_token (parser
->lexer
);
33671 structured_block
= true;
33675 v
= cp_parser_unary_expression (parser
);
33676 if (v
== error_mark_node
)
33678 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33686 lhs
= cp_parser_unary_expression (parser
);
33688 switch (TREE_CODE (lhs
))
33693 case POSTINCREMENT_EXPR
:
33694 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33695 code
= OMP_ATOMIC_CAPTURE_OLD
;
33697 case PREINCREMENT_EXPR
:
33698 lhs
= TREE_OPERAND (lhs
, 0);
33699 opcode
= PLUS_EXPR
;
33700 rhs
= integer_one_node
;
33703 case POSTDECREMENT_EXPR
:
33704 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33705 code
= OMP_ATOMIC_CAPTURE_OLD
;
33707 case PREDECREMENT_EXPR
:
33708 lhs
= TREE_OPERAND (lhs
, 0);
33709 opcode
= MINUS_EXPR
;
33710 rhs
= integer_one_node
;
33713 case COMPOUND_EXPR
:
33714 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
33715 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
33716 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
33717 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
33718 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33719 (TREE_OPERAND (lhs
, 1), 0), 0)))
33721 /* Undo effects of boolean_increment for post {in,de}crement. */
33722 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
33725 if (TREE_CODE (lhs
) == MODIFY_EXPR
33726 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
33728 /* Undo effects of boolean_increment. */
33729 if (integer_onep (TREE_OPERAND (lhs
, 1)))
33731 /* This is pre or post increment. */
33732 rhs
= TREE_OPERAND (lhs
, 1);
33733 lhs
= TREE_OPERAND (lhs
, 0);
33735 if (code
== OMP_ATOMIC_CAPTURE_NEW
33736 && !structured_block
33737 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
33738 code
= OMP_ATOMIC_CAPTURE_OLD
;
33744 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33747 opcode
= MULT_EXPR
;
33750 opcode
= TRUNC_DIV_EXPR
;
33753 opcode
= PLUS_EXPR
;
33756 opcode
= MINUS_EXPR
;
33758 case CPP_LSHIFT_EQ
:
33759 opcode
= LSHIFT_EXPR
;
33761 case CPP_RSHIFT_EQ
:
33762 opcode
= RSHIFT_EXPR
;
33765 opcode
= BIT_AND_EXPR
;
33768 opcode
= BIT_IOR_EXPR
;
33771 opcode
= BIT_XOR_EXPR
;
33774 enum cp_parser_prec oprec
;
33776 cp_lexer_consume_token (parser
->lexer
);
33777 cp_parser_parse_tentatively (parser
);
33778 rhs1
= cp_parser_simple_cast_expression (parser
);
33779 if (rhs1
== error_mark_node
)
33781 cp_parser_abort_tentative_parse (parser
);
33782 cp_parser_simple_cast_expression (parser
);
33785 token
= cp_lexer_peek_token (parser
->lexer
);
33786 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
33788 cp_parser_abort_tentative_parse (parser
);
33789 cp_parser_parse_tentatively (parser
);
33790 rhs
= cp_parser_binary_expression (parser
, false, true,
33791 PREC_NOT_OPERATOR
, NULL
);
33792 if (rhs
== error_mark_node
)
33794 cp_parser_abort_tentative_parse (parser
);
33795 cp_parser_binary_expression (parser
, false, true,
33796 PREC_NOT_OPERATOR
, NULL
);
33799 switch (TREE_CODE (rhs
))
33802 case TRUNC_DIV_EXPR
:
33811 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
33813 if (cp_parser_parse_definitely (parser
))
33815 opcode
= TREE_CODE (rhs
);
33816 rhs1
= TREE_OPERAND (rhs
, 0);
33817 rhs
= TREE_OPERAND (rhs
, 1);
33827 cp_parser_abort_tentative_parse (parser
);
33828 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
33830 rhs
= cp_parser_expression (parser
);
33831 if (rhs
== error_mark_node
)
33837 cp_parser_error (parser
,
33838 "invalid form of %<#pragma omp atomic%>");
33841 if (!cp_parser_parse_definitely (parser
))
33843 switch (token
->type
)
33845 case CPP_SEMICOLON
:
33846 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33848 code
= OMP_ATOMIC_CAPTURE_OLD
;
33853 cp_lexer_consume_token (parser
->lexer
);
33856 else if (structured_block
)
33863 cp_parser_error (parser
,
33864 "invalid form of %<#pragma omp atomic%>");
33867 opcode
= MULT_EXPR
;
33870 opcode
= TRUNC_DIV_EXPR
;
33873 opcode
= PLUS_EXPR
;
33876 opcode
= MINUS_EXPR
;
33879 opcode
= LSHIFT_EXPR
;
33882 opcode
= RSHIFT_EXPR
;
33885 opcode
= BIT_AND_EXPR
;
33888 opcode
= BIT_IOR_EXPR
;
33891 opcode
= BIT_XOR_EXPR
;
33894 cp_parser_error (parser
,
33895 "invalid operator for %<#pragma omp atomic%>");
33898 oprec
= TOKEN_PRECEDENCE (token
);
33899 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
33900 if (commutative_tree_code (opcode
))
33901 oprec
= (enum cp_parser_prec
) (oprec
- 1);
33902 cp_lexer_consume_token (parser
->lexer
);
33903 rhs
= cp_parser_binary_expression (parser
, false, false,
33905 if (rhs
== error_mark_node
)
33910 cp_parser_error (parser
,
33911 "invalid operator for %<#pragma omp atomic%>");
33914 cp_lexer_consume_token (parser
->lexer
);
33916 rhs
= cp_parser_expression (parser
);
33917 if (rhs
== error_mark_node
)
33922 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33924 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
33926 v
= cp_parser_unary_expression (parser
);
33927 if (v
== error_mark_node
)
33929 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33931 lhs1
= cp_parser_unary_expression (parser
);
33932 if (lhs1
== error_mark_node
)
33935 if (structured_block
)
33937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33938 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33941 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
33942 if (!structured_block
)
33943 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33947 cp_parser_skip_to_end_of_block_or_statement (parser
);
33948 if (structured_block
)
33950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33951 cp_lexer_consume_token (parser
->lexer
);
33952 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
33954 cp_parser_skip_to_end_of_block_or_statement (parser
);
33955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33956 cp_lexer_consume_token (parser
->lexer
);
33963 # pragma omp barrier new-line */
33966 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
33968 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33969 finish_omp_barrier ();
33973 # pragma omp critical [(name)] new-line
33977 # pragma omp critical [(name) [hint(expression)]] new-line
33978 structured-block */
33980 #define OMP_CRITICAL_CLAUSE_MASK \
33981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33984 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
33986 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
33988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33990 cp_lexer_consume_token (parser
->lexer
);
33992 name
= cp_parser_identifier (parser
);
33994 if (name
== error_mark_node
33995 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33996 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33997 /*or_comma=*/false,
33998 /*consume_paren=*/true);
33999 if (name
== error_mark_node
)
34002 clauses
= cp_parser_omp_all_clauses (parser
,
34003 OMP_CRITICAL_CLAUSE_MASK
,
34004 "#pragma omp critical", pragma_tok
);
34007 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34009 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34010 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34014 # pragma omp flush flush-vars[opt] new-line
34017 ( variable-list ) */
34020 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34023 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34024 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34026 finish_omp_flush ();
34029 /* Helper function, to parse omp for increment expression. */
34032 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
34034 tree cond
= cp_parser_binary_expression (parser
, false, true,
34035 PREC_NOT_OPERATOR
, NULL
);
34036 if (cond
== error_mark_node
34037 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34039 cp_parser_skip_to_end_of_statement (parser
);
34040 return error_mark_node
;
34043 switch (TREE_CODE (cond
))
34051 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34053 /* Fall through: OpenMP disallows NE_EXPR. */
34054 gcc_fallthrough ();
34056 return error_mark_node
;
34059 /* If decl is an iterator, preserve LHS and RHS of the relational
34060 expr until finish_omp_for. */
34062 && (type_dependent_expression_p (decl
)
34063 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34066 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34068 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34069 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34070 /*overload=*/NULL
, tf_warning_or_error
);
34073 /* Helper function, to parse omp for increment expression. */
34076 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34078 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34084 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34086 op
= (token
->type
== CPP_PLUS_PLUS
34087 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34088 cp_lexer_consume_token (parser
->lexer
);
34089 lhs
= cp_parser_simple_cast_expression (parser
);
34091 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34092 return error_mark_node
;
34093 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34096 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34098 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34099 return error_mark_node
;
34101 token
= cp_lexer_peek_token (parser
->lexer
);
34102 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34104 op
= (token
->type
== CPP_PLUS_PLUS
34105 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34106 cp_lexer_consume_token (parser
->lexer
);
34107 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34110 op
= cp_parser_assignment_operator_opt (parser
);
34111 if (op
== ERROR_MARK
)
34112 return error_mark_node
;
34114 if (op
!= NOP_EXPR
)
34116 rhs
= cp_parser_assignment_expression (parser
);
34117 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34118 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34121 lhs
= cp_parser_binary_expression (parser
, false, false,
34122 PREC_ADDITIVE_EXPRESSION
, NULL
);
34123 token
= cp_lexer_peek_token (parser
->lexer
);
34124 decl_first
= (lhs
== decl
34125 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34128 if (token
->type
!= CPP_PLUS
34129 && token
->type
!= CPP_MINUS
)
34130 return error_mark_node
;
34134 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34135 cp_lexer_consume_token (parser
->lexer
);
34136 rhs
= cp_parser_binary_expression (parser
, false, false,
34137 PREC_ADDITIVE_EXPRESSION
, NULL
);
34138 token
= cp_lexer_peek_token (parser
->lexer
);
34139 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34141 if (lhs
== NULL_TREE
)
34143 if (op
== PLUS_EXPR
)
34146 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34147 tf_warning_or_error
);
34150 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34151 ERROR_MARK
, NULL
, tf_warning_or_error
);
34154 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34159 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34160 || op
== MINUS_EXPR
)
34161 return error_mark_node
;
34162 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34165 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34167 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34170 /* Parse the initialization statement of either an OpenMP for loop or
34171 a Cilk Plus for loop.
34173 Return true if the resulting construct should have an
34174 OMP_CLAUSE_PRIVATE added to it. */
34177 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34178 enum tree_code code
,
34179 tree
&this_pre_body
,
34180 vec
<tree
, va_gc
> *for_block
,
34186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34189 tree add_private_clause
= NULL_TREE
;
34191 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34195 integer-type var = lb
34196 random-access-iterator-type var = lb
34197 pointer-type var = lb
34199 cp_decl_specifier_seq type_specifiers
;
34201 /* First, try to parse as an initialized declaration. See
34202 cp_parser_condition, from whence the bulk of this is copied. */
34204 cp_parser_parse_tentatively (parser
);
34205 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34206 /*is_trailing_return=*/false,
34208 if (cp_parser_parse_definitely (parser
))
34210 /* If parsing a type specifier seq succeeded, then this
34211 MUST be a initialized declaration. */
34212 tree asm_specification
, attributes
;
34213 cp_declarator
*declarator
;
34215 declarator
= cp_parser_declarator (parser
,
34216 CP_PARSER_DECLARATOR_NAMED
,
34217 /*ctor_dtor_or_conv_p=*/NULL
,
34218 /*parenthesized_p=*/NULL
,
34219 /*member_p=*/false,
34220 /*friend_p=*/false);
34221 attributes
= cp_parser_attributes_opt (parser
);
34222 asm_specification
= cp_parser_asm_specification_opt (parser
);
34224 if (declarator
== cp_error_declarator
)
34225 cp_parser_skip_to_end_of_statement (parser
);
34229 tree pushed_scope
, auto_node
;
34231 decl
= start_decl (declarator
, &type_specifiers
,
34232 SD_INITIALIZED
, attributes
,
34233 /*prefix_attributes=*/NULL_TREE
,
34236 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34237 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34239 if (cp_lexer_next_token_is (parser
->lexer
,
34242 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
34243 error ("parenthesized initialization is not allowed in "
34244 "OpenMP %<for%> loop");
34246 error ("parenthesized initialization is "
34247 "not allowed in for-loop");
34250 /* Trigger an error. */
34251 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34253 init
= error_mark_node
;
34254 cp_parser_skip_to_end_of_statement (parser
);
34256 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34257 || type_dependent_expression_p (decl
)
34260 bool is_direct_init
, is_non_constant_init
;
34262 init
= cp_parser_initializer (parser
,
34264 &is_non_constant_init
);
34269 = do_auto_deduction (TREE_TYPE (decl
), init
,
34272 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34273 && !type_dependent_expression_p (decl
))
34277 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34279 LOOKUP_ONLYCONVERTING
);
34281 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34283 vec_safe_push (for_block
, this_pre_body
);
34288 init
= pop_stmt_list (this_pre_body
);
34289 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34291 tree_stmt_iterator i
= tsi_start (init
);
34292 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34293 while (!tsi_end_p (i
))
34295 tree t
= tsi_stmt (i
);
34296 if (TREE_CODE (t
) == DECL_EXPR
34297 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34300 vec_safe_push (for_block
, t
);
34305 if (tsi_one_before_end_p (i
))
34307 tree t
= tsi_stmt (i
);
34309 free_stmt_list (init
);
34314 this_pre_body
= NULL_TREE
;
34319 cp_lexer_consume_token (parser
->lexer
);
34320 init
= cp_parser_assignment_expression (parser
);
34323 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34324 init
= error_mark_node
;
34326 cp_finish_decl (decl
, NULL_TREE
,
34327 /*init_const_expr_p=*/false,
34329 LOOKUP_ONLYCONVERTING
);
34333 pop_scope (pushed_scope
);
34339 /* If parsing a type specifier sequence failed, then
34340 this MUST be a simple expression. */
34341 if (code
== CILK_FOR
)
34342 error ("%<_Cilk_for%> allows expression instead of declaration only "
34343 "in C, not in C++");
34344 cp_parser_parse_tentatively (parser
);
34345 decl
= cp_parser_primary_expression (parser
, false, false,
34347 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34348 if (!cp_parser_error_occurred (parser
)
34350 && (TREE_CODE (decl
) == COMPONENT_REF
34351 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34353 cp_parser_abort_tentative_parse (parser
);
34354 cp_parser_parse_tentatively (parser
);
34355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34356 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34357 /*check_dependency_p=*/true,
34358 /*template_p=*/NULL
,
34359 /*declarator_p=*/false,
34360 /*optional_p=*/false);
34361 if (name
!= error_mark_node
34362 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34364 decl
= cp_parser_lookup_name_simple (parser
, name
,
34366 if (TREE_CODE (decl
) == FIELD_DECL
)
34367 add_private_clause
= omp_privatize_field (decl
, false);
34369 cp_parser_abort_tentative_parse (parser
);
34370 cp_parser_parse_tentatively (parser
);
34371 decl
= cp_parser_primary_expression (parser
, false, false,
34374 if (!cp_parser_error_occurred (parser
)
34377 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34381 cp_parser_parse_definitely (parser
);
34382 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34383 rhs
= cp_parser_assignment_expression (parser
);
34385 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34388 tf_warning_or_error
));
34389 if (!add_private_clause
)
34390 add_private_clause
= decl
;
34395 cp_parser_abort_tentative_parse (parser
);
34396 init
= cp_parser_expression (parser
);
34399 if (TREE_CODE (init
) == MODIFY_EXPR
34400 || TREE_CODE (init
) == MODOP_EXPR
)
34401 real_decl
= TREE_OPERAND (init
, 0);
34405 return add_private_clause
;
34408 /* Parse the restricted form of the for statement allowed by OpenMP. */
34411 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34412 tree
*cclauses
, bool *if_p
)
34414 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34415 tree real_decl
, initv
, condv
, incrv
, declv
;
34416 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34417 location_t loc_first
;
34418 bool collapse_err
= false;
34419 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34420 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34421 auto_vec
<tree
, 4> orig_inits
;
34422 bool tiling
= false;
34424 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34425 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34426 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34427 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34430 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34432 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34433 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34436 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34439 if (ordered
&& ordered
< collapse
)
34441 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34442 "%<ordered%> clause parameter is less than %<collapse%>");
34443 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34444 = build_int_cst (NULL_TREE
, collapse
);
34445 ordered
= collapse
;
34449 for (tree
*pc
= &clauses
; *pc
; )
34450 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34452 error_at (OMP_CLAUSE_LOCATION (*pc
),
34453 "%<linear%> clause may not be specified together "
34454 "with %<ordered%> clause with a parameter");
34455 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34458 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34461 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34462 count
= ordered
? ordered
: collapse
;
34464 declv
= make_tree_vec (count
);
34465 initv
= make_tree_vec (count
);
34466 condv
= make_tree_vec (count
);
34467 incrv
= make_tree_vec (count
);
34469 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34471 for (i
= 0; i
< count
; i
++)
34473 int bracecount
= 0;
34474 tree add_private_clause
= NULL_TREE
;
34477 if (code
!= CILK_FOR
34478 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34481 cp_parser_error (parser
, "for statement expected");
34484 if (code
== CILK_FOR
34485 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
34488 cp_parser_error (parser
, "_Cilk_for statement expected");
34491 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34493 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34496 init
= orig_init
= decl
= real_decl
= NULL
;
34497 this_pre_body
= push_stmt_list ();
34500 = cp_parser_omp_for_loop_init (parser
, code
,
34501 this_pre_body
, for_block
,
34502 init
, orig_init
, decl
, real_decl
);
34504 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34507 this_pre_body
= pop_stmt_list (this_pre_body
);
34511 pre_body
= push_stmt_list ();
34513 add_stmt (this_pre_body
);
34514 pre_body
= pop_stmt_list (pre_body
);
34517 pre_body
= this_pre_body
;
34522 if (cclauses
!= NULL
34523 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34524 && real_decl
!= NULL_TREE
)
34527 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34528 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34529 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34531 error_at (loc
, "iteration variable %qD"
34532 " should not be firstprivate", real_decl
);
34533 *c
= OMP_CLAUSE_CHAIN (*c
);
34535 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34536 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34538 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34540 *c
= OMP_CLAUSE_CHAIN (*c
);
34541 if (code
== OMP_SIMD
)
34543 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34544 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34548 OMP_CLAUSE_CHAIN (l
) = clauses
;
34551 add_private_clause
= NULL_TREE
;
34555 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34556 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34557 add_private_clause
= NULL_TREE
;
34558 c
= &OMP_CLAUSE_CHAIN (*c
);
34562 if (add_private_clause
)
34565 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34567 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34568 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34569 && OMP_CLAUSE_DECL (c
) == decl
)
34571 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34572 && OMP_CLAUSE_DECL (c
) == decl
)
34573 error_at (loc
, "iteration variable %qD "
34574 "should not be firstprivate",
34576 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34577 && OMP_CLAUSE_DECL (c
) == decl
)
34578 error_at (loc
, "iteration variable %qD should not be reduction",
34583 if (code
!= OMP_SIMD
)
34584 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
34585 else if (collapse
== 1)
34586 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
34588 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
34589 OMP_CLAUSE_DECL (c
) = add_private_clause
;
34590 c
= finish_omp_clauses (c
, C_ORT_OMP
);
34593 OMP_CLAUSE_CHAIN (c
) = clauses
;
34595 /* For linear, signal that we need to fill up
34596 the so far unknown linear step. */
34597 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
34598 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
34604 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34605 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
34606 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34609 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
34611 /* If decl is an iterator, preserve the operator on decl
34612 until finish_omp_for. */
34614 && ((processing_template_decl
34615 && (TREE_TYPE (real_decl
) == NULL_TREE
34616 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
34617 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
34618 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
34620 incr
= cp_parser_expression (parser
);
34621 if (!EXPR_HAS_LOCATION (incr
))
34622 protected_set_expr_location (incr
, input_location
);
34625 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34626 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34627 /*or_comma=*/false,
34628 /*consume_paren=*/true);
34630 TREE_VEC_ELT (declv
, i
) = decl
;
34631 TREE_VEC_ELT (initv
, i
) = init
;
34632 TREE_VEC_ELT (condv
, i
) = cond
;
34633 TREE_VEC_ELT (incrv
, i
) = incr
;
34636 orig_inits
.safe_grow_cleared (i
+ 1);
34637 orig_inits
[i
] = orig_init
;
34640 if (i
== count
- 1)
34643 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34644 in between the collapsed for loops to be still considered perfectly
34645 nested. Hopefully the final version clarifies this.
34646 For now handle (multiple) {'s and empty statements. */
34647 cp_parser_parse_tentatively (parser
);
34650 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34652 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34654 cp_lexer_consume_token (parser
->lexer
);
34657 else if (bracecount
34658 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34659 cp_lexer_consume_token (parser
->lexer
);
34662 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34663 error_at (loc
, "not enough for loops to collapse");
34664 collapse_err
= true;
34665 cp_parser_abort_tentative_parse (parser
);
34673 cp_parser_parse_definitely (parser
);
34674 nbraces
+= bracecount
;
34681 /* Note that we saved the original contents of this flag when we entered
34682 the structured block, and so we don't need to re-save it here. */
34683 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34684 parser
->in_statement
= IN_CILK_SIMD_FOR
;
34686 parser
->in_statement
= IN_OMP_FOR
;
34688 /* Note that the grammar doesn't call for a structured block here,
34689 though the loop as a whole is a structured block. */
34690 body
= push_stmt_list ();
34691 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34692 body
= pop_stmt_list (body
);
34694 if (declv
== NULL_TREE
)
34697 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
34698 body
, pre_body
, &orig_inits
, clauses
);
34702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34704 cp_lexer_consume_token (parser
->lexer
);
34707 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34708 cp_lexer_consume_token (parser
->lexer
);
34713 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
34714 "collapsed loops not perfectly nested");
34716 collapse_err
= true;
34717 cp_parser_statement_seq_opt (parser
, NULL
);
34718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
34723 while (!for_block
->is_empty ())
34725 tree t
= for_block
->pop ();
34726 if (TREE_CODE (t
) == STATEMENT_LIST
)
34727 add_stmt (pop_stmt_list (t
));
34731 release_tree_vector (for_block
);
34736 /* Helper function for OpenMP parsing, split clauses and call
34737 finish_omp_clauses on each of the set of clauses afterwards. */
34740 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
34741 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
34744 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
34745 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
34747 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
34751 #pragma omp simd simd-clause[optseq] new-line
34754 #define OMP_SIMD_CLAUSE_MASK \
34755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34765 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
34766 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34769 tree clauses
, sb
, ret
;
34771 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34773 strcat (p_name
, " simd");
34774 mask
|= OMP_SIMD_CLAUSE_MASK
;
34776 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34780 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
34781 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
34782 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
34783 OMP_CLAUSE_ORDERED
);
34784 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
34786 error_at (OMP_CLAUSE_LOCATION (c
),
34787 "%<ordered%> clause with parameter may not be specified "
34788 "on %qs construct", p_name
);
34789 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
34793 sb
= begin_omp_structured_block ();
34794 save
= cp_parser_begin_omp_structured_block (parser
);
34796 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
34798 cp_parser_end_omp_structured_block (parser
, save
);
34799 add_stmt (finish_omp_structured_block (sb
));
34805 #pragma omp for for-clause[optseq] new-line
34809 #pragma omp for simd for-simd-clause[optseq] new-line
34812 #define OMP_FOR_CLAUSE_MASK \
34813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34824 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
34825 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34828 tree clauses
, sb
, ret
;
34830 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34832 strcat (p_name
, " for");
34833 mask
|= OMP_FOR_CLAUSE_MASK
;
34834 /* parallel for{, simd} disallows nowait clause, but for
34835 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34836 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
34837 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
34838 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34839 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34840 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
34842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34844 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34845 const char *p
= IDENTIFIER_POINTER (id
);
34847 if (strcmp (p
, "simd") == 0)
34849 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34850 if (cclauses
== NULL
)
34851 cclauses
= cclauses_buf
;
34853 cp_lexer_consume_token (parser
->lexer
);
34854 if (!flag_openmp
) /* flag_openmp_simd */
34855 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34857 sb
= begin_omp_structured_block ();
34858 save
= cp_parser_begin_omp_structured_block (parser
);
34859 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34861 cp_parser_end_omp_structured_block (parser
, save
);
34862 tree body
= finish_omp_structured_block (sb
);
34865 ret
= make_node (OMP_FOR
);
34866 TREE_TYPE (ret
) = void_type_node
;
34867 OMP_FOR_BODY (ret
) = body
;
34868 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34869 SET_EXPR_LOCATION (ret
, loc
);
34874 if (!flag_openmp
) /* flag_openmp_simd */
34876 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34880 /* Composite distribute parallel for disallows linear clause. */
34881 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34882 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
34884 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34888 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
34889 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34892 sb
= begin_omp_structured_block ();
34893 save
= cp_parser_begin_omp_structured_block (parser
);
34895 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
34897 cp_parser_end_omp_structured_block (parser
, save
);
34898 add_stmt (finish_omp_structured_block (sb
));
34904 # pragma omp master new-line
34905 structured-block */
34908 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34910 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34911 return c_finish_omp_master (input_location
,
34912 cp_parser_omp_structured_block (parser
, if_p
));
34916 # pragma omp ordered new-line
34920 # pragma omp ordered ordered-clauses new-line
34921 structured-block */
34923 #define OMP_ORDERED_CLAUSE_MASK \
34924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34927 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34928 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34931 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
34932 enum pragma_context context
, bool *if_p
)
34934 location_t loc
= pragma_tok
->location
;
34936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34938 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34939 const char *p
= IDENTIFIER_POINTER (id
);
34941 if (strcmp (p
, "depend") == 0)
34943 if (context
== pragma_stmt
)
34945 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
34946 "%<depend%> clause may only be used in compound "
34948 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34952 = cp_parser_omp_all_clauses (parser
,
34953 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
34954 "#pragma omp ordered", pragma_tok
);
34955 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
34961 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
34962 "#pragma omp ordered", pragma_tok
);
34963 c_finish_omp_ordered (loc
, clauses
,
34964 cp_parser_omp_structured_block (parser
, if_p
));
34971 { section-sequence }
34974 section-directive[opt] structured-block
34975 section-sequence section-directive structured-block */
34978 cp_parser_omp_sections_scope (cp_parser
*parser
)
34980 tree stmt
, substmt
;
34981 bool error_suppress
= false;
34984 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
34987 stmt
= push_stmt_list ();
34989 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
34990 != PRAGMA_OMP_SECTION
)
34992 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34993 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34994 add_stmt (substmt
);
34999 tok
= cp_lexer_peek_token (parser
->lexer
);
35000 if (tok
->type
== CPP_CLOSE_BRACE
)
35002 if (tok
->type
== CPP_EOF
)
35005 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35007 cp_lexer_consume_token (parser
->lexer
);
35008 cp_parser_require_pragma_eol (parser
, tok
);
35009 error_suppress
= false;
35011 else if (!error_suppress
)
35013 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35014 error_suppress
= true;
35017 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35018 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35019 add_stmt (substmt
);
35021 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35023 substmt
= pop_stmt_list (stmt
);
35025 stmt
= make_node (OMP_SECTIONS
);
35026 TREE_TYPE (stmt
) = void_type_node
;
35027 OMP_SECTIONS_BODY (stmt
) = substmt
;
35034 # pragma omp sections sections-clause[optseq] newline
35037 #define OMP_SECTIONS_CLAUSE_MASK \
35038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35045 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35046 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35049 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35051 strcat (p_name
, " sections");
35052 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35054 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35056 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35060 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35061 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35064 ret
= cp_parser_omp_sections_scope (parser
);
35066 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35072 # pragma omp parallel parallel-clause[optseq] new-line
35074 # pragma omp parallel for parallel-for-clause[optseq] new-line
35076 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35080 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35081 structured-block */
35083 #define OMP_PARALLEL_CLAUSE_MASK \
35084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35095 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35096 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35099 tree stmt
, clauses
, block
;
35101 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35103 strcat (p_name
, " parallel");
35104 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35105 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35106 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35107 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35108 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35110 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35112 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35113 if (cclauses
== NULL
)
35114 cclauses
= cclauses_buf
;
35116 cp_lexer_consume_token (parser
->lexer
);
35117 if (!flag_openmp
) /* flag_openmp_simd */
35118 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35120 block
= begin_omp_parallel ();
35121 save
= cp_parser_begin_omp_structured_block (parser
);
35122 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35124 cp_parser_end_omp_structured_block (parser
, save
);
35125 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35127 if (ret
== NULL_TREE
)
35129 OMP_PARALLEL_COMBINED (stmt
) = 1;
35132 /* When combined with distribute, parallel has to be followed by for.
35133 #pragma omp target parallel is allowed though. */
35135 && (mask
& (OMP_CLAUSE_MASK_1
35136 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35138 error_at (loc
, "expected %<for%> after %qs", p_name
);
35139 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35142 else if (!flag_openmp
) /* flag_openmp_simd */
35144 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35147 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35150 const char *p
= IDENTIFIER_POINTER (id
);
35151 if (strcmp (p
, "sections") == 0)
35153 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35154 cclauses
= cclauses_buf
;
35156 cp_lexer_consume_token (parser
->lexer
);
35157 block
= begin_omp_parallel ();
35158 save
= cp_parser_begin_omp_structured_block (parser
);
35159 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35160 cp_parser_end_omp_structured_block (parser
, save
);
35161 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35163 OMP_PARALLEL_COMBINED (stmt
) = 1;
35168 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35172 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35173 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35176 block
= begin_omp_parallel ();
35177 save
= cp_parser_begin_omp_structured_block (parser
);
35178 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35179 cp_parser_end_omp_structured_block (parser
, save
);
35180 stmt
= finish_omp_parallel (clauses
, block
);
35185 # pragma omp single single-clause[optseq] new-line
35186 structured-block */
35188 #define OMP_SINGLE_CLAUSE_MASK \
35189 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35195 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35197 tree stmt
= make_node (OMP_SINGLE
);
35198 TREE_TYPE (stmt
) = void_type_node
;
35200 OMP_SINGLE_CLAUSES (stmt
)
35201 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35202 "#pragma omp single", pragma_tok
);
35203 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35205 return add_stmt (stmt
);
35209 # pragma omp task task-clause[optseq] new-line
35210 structured-block */
35212 #define OMP_TASK_CLAUSE_MASK \
35213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35225 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35227 tree clauses
, block
;
35230 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35231 "#pragma omp task", pragma_tok
);
35232 block
= begin_omp_task ();
35233 save
= cp_parser_begin_omp_structured_block (parser
);
35234 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35235 cp_parser_end_omp_structured_block (parser
, save
);
35236 return finish_omp_task (clauses
, block
);
35240 # pragma omp taskwait new-line */
35243 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35245 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35246 finish_omp_taskwait ();
35250 # pragma omp taskyield new-line */
35253 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35255 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35256 finish_omp_taskyield ();
35260 # pragma omp taskgroup new-line
35261 structured-block */
35264 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35266 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35267 return c_finish_omp_taskgroup (input_location
,
35268 cp_parser_omp_structured_block (parser
,
35274 # pragma omp threadprivate (variable-list) */
35277 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35281 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35282 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35284 finish_omp_threadprivate (vars
);
35288 # pragma omp cancel cancel-clause[optseq] new-line */
35290 #define OMP_CANCEL_CLAUSE_MASK \
35291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35298 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35300 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35301 "#pragma omp cancel", pragma_tok
);
35302 finish_omp_cancel (clauses
);
35306 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35308 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35315 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35316 enum pragma_context context
)
35319 bool point_seen
= false;
35321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35323 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35324 const char *p
= IDENTIFIER_POINTER (id
);
35326 if (strcmp (p
, "point") == 0)
35328 cp_lexer_consume_token (parser
->lexer
);
35334 cp_parser_error (parser
, "expected %<point%>");
35335 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35339 if (context
!= pragma_compound
)
35341 if (context
== pragma_stmt
)
35342 error_at (pragma_tok
->location
,
35343 "%<#pragma omp cancellation point%> may only be used in"
35344 " compound statements");
35346 cp_parser_error (parser
, "expected declaration specifiers");
35347 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35351 clauses
= cp_parser_omp_all_clauses (parser
,
35352 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35353 "#pragma omp cancellation point",
35355 finish_omp_cancellation_point (clauses
);
35359 #pragma omp distribute distribute-clause[optseq] new-line
35362 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35370 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35371 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35374 tree clauses
, sb
, ret
;
35376 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35378 strcat (p_name
, " distribute");
35379 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35383 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35384 const char *p
= IDENTIFIER_POINTER (id
);
35386 bool parallel
= false;
35388 if (strcmp (p
, "simd") == 0)
35391 parallel
= strcmp (p
, "parallel") == 0;
35392 if (parallel
|| simd
)
35394 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35395 if (cclauses
== NULL
)
35396 cclauses
= cclauses_buf
;
35397 cp_lexer_consume_token (parser
->lexer
);
35398 if (!flag_openmp
) /* flag_openmp_simd */
35401 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35404 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35407 sb
= begin_omp_structured_block ();
35408 save
= cp_parser_begin_omp_structured_block (parser
);
35410 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35413 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35415 cp_parser_end_omp_structured_block (parser
, save
);
35416 tree body
= finish_omp_structured_block (sb
);
35419 ret
= make_node (OMP_DISTRIBUTE
);
35420 TREE_TYPE (ret
) = void_type_node
;
35421 OMP_FOR_BODY (ret
) = body
;
35422 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35423 SET_EXPR_LOCATION (ret
, loc
);
35428 if (!flag_openmp
) /* flag_openmp_simd */
35430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35434 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35438 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35439 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35442 sb
= begin_omp_structured_block ();
35443 save
= cp_parser_begin_omp_structured_block (parser
);
35445 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35447 cp_parser_end_omp_structured_block (parser
, save
);
35448 add_stmt (finish_omp_structured_block (sb
));
35454 # pragma omp teams teams-clause[optseq] new-line
35455 structured-block */
35457 #define OMP_TEAMS_CLAUSE_MASK \
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_SHARED) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35467 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35468 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35471 tree clauses
, sb
, ret
;
35473 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35475 strcat (p_name
, " teams");
35476 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35480 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35481 const char *p
= IDENTIFIER_POINTER (id
);
35482 if (strcmp (p
, "distribute") == 0)
35484 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35485 if (cclauses
== NULL
)
35486 cclauses
= cclauses_buf
;
35488 cp_lexer_consume_token (parser
->lexer
);
35489 if (!flag_openmp
) /* flag_openmp_simd */
35490 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35492 sb
= begin_omp_structured_block ();
35493 save
= cp_parser_begin_omp_structured_block (parser
);
35494 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35496 cp_parser_end_omp_structured_block (parser
, save
);
35497 tree body
= finish_omp_structured_block (sb
);
35500 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35501 ret
= make_node (OMP_TEAMS
);
35502 TREE_TYPE (ret
) = void_type_node
;
35503 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35504 OMP_TEAMS_BODY (ret
) = body
;
35505 OMP_TEAMS_COMBINED (ret
) = 1;
35506 SET_EXPR_LOCATION (ret
, loc
);
35507 return add_stmt (ret
);
35510 if (!flag_openmp
) /* flag_openmp_simd */
35512 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35516 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35520 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35521 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35524 tree stmt
= make_node (OMP_TEAMS
);
35525 TREE_TYPE (stmt
) = void_type_node
;
35526 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35527 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35528 SET_EXPR_LOCATION (stmt
, loc
);
35530 return add_stmt (stmt
);
35534 # pragma omp target data target-data-clause[optseq] new-line
35535 structured-block */
35537 #define OMP_TARGET_DATA_CLAUSE_MASK \
35538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35544 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35547 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35548 "#pragma omp target data", pragma_tok
);
35550 for (tree
*pc
= &clauses
; *pc
;)
35552 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35553 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35556 case GOMP_MAP_ALWAYS_TO
:
35557 case GOMP_MAP_FROM
:
35558 case GOMP_MAP_ALWAYS_FROM
:
35559 case GOMP_MAP_TOFROM
:
35560 case GOMP_MAP_ALWAYS_TOFROM
:
35561 case GOMP_MAP_ALLOC
:
35564 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35565 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35566 case GOMP_MAP_ALWAYS_POINTER
:
35570 error_at (OMP_CLAUSE_LOCATION (*pc
),
35571 "%<#pragma omp target data%> with map-type other "
35572 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35573 "on %<map%> clause");
35574 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35577 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35583 error_at (pragma_tok
->location
,
35584 "%<#pragma omp target data%> must contain at least "
35585 "one %<map%> clause");
35589 tree stmt
= make_node (OMP_TARGET_DATA
);
35590 TREE_TYPE (stmt
) = void_type_node
;
35591 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
35593 keep_next_level (true);
35594 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35596 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35597 return add_stmt (stmt
);
35601 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35602 structured-block */
35604 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35612 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35613 enum pragma_context context
)
35615 bool data_seen
= false;
35616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35618 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35619 const char *p
= IDENTIFIER_POINTER (id
);
35621 if (strcmp (p
, "data") == 0)
35623 cp_lexer_consume_token (parser
->lexer
);
35629 cp_parser_error (parser
, "expected %<data%>");
35630 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35634 if (context
== pragma_stmt
)
35636 error_at (pragma_tok
->location
,
35637 "%<#pragma omp target enter data%> may only be "
35638 "used in compound statements");
35639 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35644 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
35645 "#pragma omp target enter data", pragma_tok
);
35647 for (tree
*pc
= &clauses
; *pc
;)
35649 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35650 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35653 case GOMP_MAP_ALWAYS_TO
:
35654 case GOMP_MAP_ALLOC
:
35657 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35658 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35659 case GOMP_MAP_ALWAYS_POINTER
:
35663 error_at (OMP_CLAUSE_LOCATION (*pc
),
35664 "%<#pragma omp target enter data%> with map-type other "
35665 "than %<to%> or %<alloc%> on %<map%> clause");
35666 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35669 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35675 error_at (pragma_tok
->location
,
35676 "%<#pragma omp target enter data%> must contain at least "
35677 "one %<map%> clause");
35681 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
35682 TREE_TYPE (stmt
) = void_type_node
;
35683 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
35684 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35685 return add_stmt (stmt
);
35689 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35690 structured-block */
35692 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35700 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35701 enum pragma_context context
)
35703 bool data_seen
= false;
35704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35706 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35707 const char *p
= IDENTIFIER_POINTER (id
);
35709 if (strcmp (p
, "data") == 0)
35711 cp_lexer_consume_token (parser
->lexer
);
35717 cp_parser_error (parser
, "expected %<data%>");
35718 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35722 if (context
== pragma_stmt
)
35724 error_at (pragma_tok
->location
,
35725 "%<#pragma omp target exit data%> may only be "
35726 "used in compound statements");
35727 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35732 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
35733 "#pragma omp target exit data", pragma_tok
);
35735 for (tree
*pc
= &clauses
; *pc
;)
35737 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35738 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35740 case GOMP_MAP_FROM
:
35741 case GOMP_MAP_ALWAYS_FROM
:
35742 case GOMP_MAP_RELEASE
:
35743 case GOMP_MAP_DELETE
:
35746 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35747 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35748 case GOMP_MAP_ALWAYS_POINTER
:
35752 error_at (OMP_CLAUSE_LOCATION (*pc
),
35753 "%<#pragma omp target exit data%> with map-type other "
35754 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35756 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35759 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35765 error_at (pragma_tok
->location
,
35766 "%<#pragma omp target exit data%> must contain at least "
35767 "one %<map%> clause");
35771 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
35772 TREE_TYPE (stmt
) = void_type_node
;
35773 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
35774 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35775 return add_stmt (stmt
);
35779 # pragma omp target update target-update-clause[optseq] new-line */
35781 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35790 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
35791 enum pragma_context context
)
35793 if (context
== pragma_stmt
)
35795 error_at (pragma_tok
->location
,
35796 "%<#pragma omp target update%> may only be "
35797 "used in compound statements");
35798 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35803 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
35804 "#pragma omp target update", pragma_tok
);
35805 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
35806 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
35808 error_at (pragma_tok
->location
,
35809 "%<#pragma omp target update%> must contain at least one "
35810 "%<from%> or %<to%> clauses");
35814 tree stmt
= make_node (OMP_TARGET_UPDATE
);
35815 TREE_TYPE (stmt
) = void_type_node
;
35816 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
35817 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35823 # pragma omp target target-clause[optseq] new-line
35824 structured-block */
35826 #define OMP_TARGET_CLAUSE_MASK \
35827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35838 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
35839 enum pragma_context context
, bool *if_p
)
35841 tree
*pc
= NULL
, stmt
;
35843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35845 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35846 const char *p
= IDENTIFIER_POINTER (id
);
35847 enum tree_code ccode
= ERROR_MARK
;
35849 if (strcmp (p
, "teams") == 0)
35851 else if (strcmp (p
, "parallel") == 0)
35852 ccode
= OMP_PARALLEL
;
35853 else if (strcmp (p
, "simd") == 0)
35855 if (ccode
!= ERROR_MARK
)
35857 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
35858 char p_name
[sizeof ("#pragma omp target teams distribute "
35859 "parallel for simd")];
35861 cp_lexer_consume_token (parser
->lexer
);
35862 strcpy (p_name
, "#pragma omp target");
35863 if (!flag_openmp
) /* flag_openmp_simd */
35869 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35870 OMP_TARGET_CLAUSE_MASK
,
35874 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35875 OMP_TARGET_CLAUSE_MASK
,
35879 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35880 OMP_TARGET_CLAUSE_MASK
,
35884 gcc_unreachable ();
35886 return stmt
!= NULL_TREE
;
35888 keep_next_level (true);
35889 tree sb
= begin_omp_structured_block (), ret
;
35890 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
35894 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35895 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35899 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35900 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35904 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35905 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35909 gcc_unreachable ();
35911 cp_parser_end_omp_structured_block (parser
, save
);
35912 tree body
= finish_omp_structured_block (sb
);
35913 if (ret
== NULL_TREE
)
35915 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
35917 /* For combined target teams, ensure the num_teams and
35918 thread_limit clause expressions are evaluated on the host,
35919 before entering the target construct. */
35921 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35922 c
; c
= OMP_CLAUSE_CHAIN (c
))
35923 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
35924 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
35925 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
35927 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
35928 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
35929 if (expr
== error_mark_node
)
35931 tree tmp
= TARGET_EXPR_SLOT (expr
);
35933 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
35934 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
35935 OMP_CLAUSE_FIRSTPRIVATE
);
35936 OMP_CLAUSE_DECL (tc
) = tmp
;
35937 OMP_CLAUSE_CHAIN (tc
)
35938 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35939 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
35942 tree stmt
= make_node (OMP_TARGET
);
35943 TREE_TYPE (stmt
) = void_type_node
;
35944 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35945 OMP_TARGET_BODY (stmt
) = body
;
35946 OMP_TARGET_COMBINED (stmt
) = 1;
35947 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35949 pc
= &OMP_TARGET_CLAUSES (stmt
);
35950 goto check_clauses
;
35952 else if (!flag_openmp
) /* flag_openmp_simd */
35954 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35957 else if (strcmp (p
, "data") == 0)
35959 cp_lexer_consume_token (parser
->lexer
);
35960 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
35963 else if (strcmp (p
, "enter") == 0)
35965 cp_lexer_consume_token (parser
->lexer
);
35966 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
35969 else if (strcmp (p
, "exit") == 0)
35971 cp_lexer_consume_token (parser
->lexer
);
35972 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
35975 else if (strcmp (p
, "update") == 0)
35977 cp_lexer_consume_token (parser
->lexer
);
35978 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
35981 if (!flag_openmp
) /* flag_openmp_simd */
35983 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35987 stmt
= make_node (OMP_TARGET
);
35988 TREE_TYPE (stmt
) = void_type_node
;
35990 OMP_TARGET_CLAUSES (stmt
)
35991 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
35992 "#pragma omp target", pragma_tok
);
35993 pc
= &OMP_TARGET_CLAUSES (stmt
);
35994 keep_next_level (true);
35995 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35997 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36003 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36004 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36007 case GOMP_MAP_ALWAYS_TO
:
36008 case GOMP_MAP_FROM
:
36009 case GOMP_MAP_ALWAYS_FROM
:
36010 case GOMP_MAP_TOFROM
:
36011 case GOMP_MAP_ALWAYS_TOFROM
:
36012 case GOMP_MAP_ALLOC
:
36013 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36014 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36015 case GOMP_MAP_ALWAYS_POINTER
:
36018 error_at (OMP_CLAUSE_LOCATION (*pc
),
36019 "%<#pragma omp target%> with map-type other "
36020 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36021 "on %<map%> clause");
36022 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36025 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36031 # pragma acc cache (variable-list) new-line
36035 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36037 tree stmt
, clauses
;
36039 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36040 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36042 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36044 stmt
= make_node (OACC_CACHE
);
36045 TREE_TYPE (stmt
) = void_type_node
;
36046 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36047 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36054 # pragma acc data oacc-data-clause[optseq] new-line
36055 structured-block */
36057 #define OACC_DATA_CLAUSE_MASK \
36058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36071 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36073 tree stmt
, clauses
, block
;
36076 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36077 "#pragma acc data", pragma_tok
);
36079 block
= begin_omp_parallel ();
36080 save
= cp_parser_begin_omp_structured_block (parser
);
36081 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36082 cp_parser_end_omp_structured_block (parser
, save
);
36083 stmt
= finish_oacc_data (clauses
, block
);
36088 # pragma acc host_data <clauses> new-line
36089 structured-block */
36091 #define OACC_HOST_DATA_CLAUSE_MASK \
36092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36095 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36097 tree stmt
, clauses
, block
;
36100 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36101 "#pragma acc host_data", pragma_tok
);
36103 block
= begin_omp_parallel ();
36104 save
= cp_parser_begin_omp_structured_block (parser
);
36105 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36106 cp_parser_end_omp_structured_block (parser
, save
);
36107 stmt
= finish_oacc_host_data (clauses
, block
);
36112 # pragma acc declare oacc-data-clause[optseq] new-line
36115 #define OACC_DECLARE_CLAUSE_MASK \
36116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36130 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36132 tree clauses
, stmt
;
36133 bool error
= false;
36135 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36136 "#pragma acc declare", pragma_tok
, true);
36139 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36141 error_at (pragma_tok
->location
,
36142 "no valid clauses specified in %<#pragma acc declare%>");
36146 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36148 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36149 tree decl
= OMP_CLAUSE_DECL (t
);
36150 if (!DECL_P (decl
))
36152 error_at (loc
, "array section in %<#pragma acc declare%>");
36156 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36157 switch (OMP_CLAUSE_MAP_KIND (t
))
36159 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36160 case GOMP_MAP_FORCE_ALLOC
:
36161 case GOMP_MAP_FORCE_TO
:
36162 case GOMP_MAP_FORCE_DEVICEPTR
:
36163 case GOMP_MAP_DEVICE_RESIDENT
:
36166 case GOMP_MAP_LINK
:
36167 if (!global_bindings_p ()
36168 && (TREE_STATIC (decl
)
36169 || !DECL_EXTERNAL (decl
)))
36172 "%qD must be a global variable in "
36173 "%<#pragma acc declare link%>",
36181 if (global_bindings_p ())
36183 error_at (loc
, "invalid OpenACC clause at file scope");
36187 if (DECL_EXTERNAL (decl
))
36190 "invalid use of %<extern%> variable %qD "
36191 "in %<#pragma acc declare%>", decl
);
36195 else if (TREE_PUBLIC (decl
))
36198 "invalid use of %<global%> variable %qD "
36199 "in %<#pragma acc declare%>", decl
);
36206 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36207 || lookup_attribute ("omp declare target link",
36208 DECL_ATTRIBUTES (decl
)))
36210 error_at (loc
, "variable %qD used more than once with "
36211 "%<#pragma acc declare%>", decl
);
36220 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36221 id
= get_identifier ("omp declare target link");
36223 id
= get_identifier ("omp declare target");
36225 DECL_ATTRIBUTES (decl
)
36226 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36227 if (global_bindings_p ())
36229 symtab_node
*node
= symtab_node::get (decl
);
36232 node
->offloadable
= 1;
36233 if (ENABLE_OFFLOADING
)
36235 g
->have_offload
= true;
36236 if (is_a
<varpool_node
*> (node
))
36237 vec_safe_push (offload_vars
, decl
);
36244 if (error
|| global_bindings_p ())
36247 stmt
= make_node (OACC_DECLARE
);
36248 TREE_TYPE (stmt
) = void_type_node
;
36249 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36250 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36258 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36262 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36264 LOC is the location of the #pragma token.
36267 #define OACC_ENTER_DATA_CLAUSE_MASK \
36268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36276 #define OACC_EXIT_DATA_CLAUSE_MASK \
36277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36284 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36287 location_t loc
= pragma_tok
->location
;
36288 tree stmt
, clauses
;
36289 const char *p
= "";
36291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36292 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36294 if (strcmp (p
, "data") != 0)
36296 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36297 enter
? "enter" : "exit");
36298 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36302 cp_lexer_consume_token (parser
->lexer
);
36305 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36306 "#pragma acc enter data", pragma_tok
);
36308 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36309 "#pragma acc exit data", pragma_tok
);
36311 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36313 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36314 enter
? "enter" : "exit");
36318 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36319 TREE_TYPE (stmt
) = void_type_node
;
36320 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36321 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36327 # pragma acc loop oacc-loop-clause[optseq] new-line
36328 structured-block */
36330 #define OACC_LOOP_CLAUSE_MASK \
36331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36343 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36344 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36346 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36348 strcat (p_name
, " loop");
36349 mask
|= OACC_LOOP_CLAUSE_MASK
;
36351 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36355 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36357 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36359 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36362 tree block
= begin_omp_structured_block ();
36363 int save
= cp_parser_begin_omp_structured_block (parser
);
36364 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36365 cp_parser_end_omp_structured_block (parser
, save
);
36366 add_stmt (finish_omp_structured_block (block
));
36372 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36377 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36381 #define OACC_KERNELS_CLAUSE_MASK \
36382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36397 #define OACC_PARALLEL_CLAUSE_MASK \
36398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36420 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36421 char *p_name
, bool *if_p
)
36423 omp_clause_mask mask
;
36424 enum tree_code code
;
36425 switch (cp_parser_pragma_kind (pragma_tok
))
36427 case PRAGMA_OACC_KERNELS
:
36428 strcat (p_name
, " kernels");
36429 mask
= OACC_KERNELS_CLAUSE_MASK
;
36430 code
= OACC_KERNELS
;
36432 case PRAGMA_OACC_PARALLEL
:
36433 strcat (p_name
, " parallel");
36434 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36435 code
= OACC_PARALLEL
;
36438 gcc_unreachable ();
36441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36444 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36445 if (strcmp (p
, "loop") == 0)
36447 cp_lexer_consume_token (parser
->lexer
);
36448 tree block
= begin_omp_parallel ();
36450 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36452 return finish_omp_construct (code
, block
, clauses
);
36456 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36458 tree block
= begin_omp_parallel ();
36459 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36460 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36461 cp_parser_end_omp_structured_block (parser
, save
);
36462 return finish_omp_construct (code
, block
, clauses
);
36466 # pragma acc update oacc-update-clause[optseq] new-line
36469 #define OACC_UPDATE_CLAUSE_MASK \
36470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36478 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36480 tree stmt
, clauses
;
36482 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36483 "#pragma acc update", pragma_tok
);
36485 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36487 error_at (pragma_tok
->location
,
36488 "%<#pragma acc update%> must contain at least one "
36489 "%<device%> or %<host%> or %<self%> clause");
36493 stmt
= make_node (OACC_UPDATE
);
36494 TREE_TYPE (stmt
) = void_type_node
;
36495 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36496 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36502 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36504 LOC is the location of the #pragma token.
36507 #define OACC_WAIT_CLAUSE_MASK \
36508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36511 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36513 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36514 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36516 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36517 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36519 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36520 "#pragma acc wait", pragma_tok
);
36522 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36523 stmt
= finish_expr_stmt (stmt
);
36529 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36531 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36540 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36541 enum pragma_context context
)
36543 bool first_p
= parser
->omp_declare_simd
== NULL
;
36544 cp_omp_declare_simd_data data
;
36547 data
.error_seen
= false;
36548 data
.fndecl_seen
= false;
36549 data
.tokens
= vNULL
;
36550 data
.clauses
= NULL_TREE
;
36551 /* It is safe to take the address of a local variable; it will only be
36552 used while this scope is live. */
36553 parser
->omp_declare_simd
= &data
;
36556 /* Store away all pragma tokens. */
36557 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36558 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36559 cp_lexer_consume_token (parser
->lexer
);
36560 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36561 parser
->omp_declare_simd
->error_seen
= true;
36562 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36563 struct cp_token_cache
*cp
36564 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36565 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36569 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36570 cp_parser_pragma (parser
, context
, NULL
);
36573 case pragma_external
:
36574 cp_parser_declaration (parser
);
36576 case pragma_member
:
36577 cp_parser_member_declaration (parser
);
36579 case pragma_objc_icode
:
36580 cp_parser_block_declaration (parser
, /*statement_p=*/false);
36583 cp_parser_declaration_statement (parser
);
36586 if (parser
->omp_declare_simd
36587 && !parser
->omp_declare_simd
->error_seen
36588 && !parser
->omp_declare_simd
->fndecl_seen
)
36589 error_at (pragma_tok
->location
,
36590 "%<#pragma omp declare simd%> not immediately followed by "
36591 "function declaration or definition");
36592 data
.tokens
.release ();
36593 parser
->omp_declare_simd
= NULL
;
36597 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36598 This function is modelled similar to the late parsing of omp declare
36602 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
36604 struct cp_token_cache
*ce
;
36605 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
36608 if (parser
->omp_declare_simd
!= NULL
36609 || lookup_attribute ("simd", attrs
))
36611 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36612 "used in the same function marked as a Cilk Plus SIMD-enabled "
36614 parser
->cilk_simd_fn_info
->tokens
.release ();
36615 XDELETE (parser
->cilk_simd_fn_info
);
36616 parser
->cilk_simd_fn_info
= NULL
;
36619 if (!info
->error_seen
&& info
->fndecl_seen
)
36621 error ("vector attribute not immediately followed by a single function"
36622 " declaration or definition");
36623 info
->error_seen
= true;
36625 if (info
->error_seen
)
36628 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
36632 cp_parser_push_lexer_for_tokens (parser
, ce
);
36633 parser
->lexer
->in_pragma
= true;
36634 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
36635 "SIMD-enabled functions attribute",
36637 cp_parser_pop_lexer (parser
);
36639 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36641 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
36642 TREE_CHAIN (c
) = attrs
;
36645 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36646 TREE_CHAIN (c
) = attrs
;
36647 if (processing_template_decl
)
36648 ATTR_IS_DEPENDENT (c
) = 1;
36651 info
->fndecl_seen
= true;
36652 parser
->cilk_simd_fn_info
->tokens
.release ();
36653 XDELETE (parser
->cilk_simd_fn_info
);
36654 parser
->cilk_simd_fn_info
= NULL
;
36658 /* Finalize #pragma omp declare simd clauses after direct declarator has
36659 been parsed, and put that into "omp declare simd" attribute. */
36662 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
36664 struct cp_token_cache
*ce
;
36665 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
36668 if (!data
->error_seen
&& data
->fndecl_seen
)
36670 error ("%<#pragma omp declare simd%> not immediately followed by "
36671 "a single function declaration or definition");
36672 data
->error_seen
= true;
36674 if (data
->error_seen
)
36677 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
36681 cp_parser_push_lexer_for_tokens (parser
, ce
);
36682 parser
->lexer
->in_pragma
= true;
36683 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
36684 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36685 cp_lexer_consume_token (parser
->lexer
);
36686 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
36687 "#pragma omp declare simd", pragma_tok
);
36688 cp_parser_pop_lexer (parser
);
36690 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36691 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36692 TREE_CHAIN (c
) = attrs
;
36693 if (processing_template_decl
)
36694 ATTR_IS_DEPENDENT (c
) = 1;
36698 data
->fndecl_seen
= true;
36704 # pragma omp declare target new-line
36705 declarations and definitions
36706 # pragma omp end declare target new-line
36709 # pragma omp declare target ( extended-list ) new-line
36711 # pragma omp declare target declare-target-clauses[seq] new-line */
36713 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36718 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36720 tree clauses
= NULL_TREE
;
36721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36723 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
36724 "#pragma omp declare target", pragma_tok
);
36725 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36727 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
36729 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36730 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36734 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36735 scope_chain
->omp_declare_target_attribute
++;
36738 if (scope_chain
->omp_declare_target_attribute
)
36739 error_at (pragma_tok
->location
,
36740 "%<#pragma omp declare target%> with clauses in between "
36741 "%<#pragma omp declare target%> without clauses and "
36742 "%<#pragma omp end declare target%>");
36743 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36745 tree t
= OMP_CLAUSE_DECL (c
), id
;
36746 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
36747 tree at2
= lookup_attribute ("omp declare target link",
36748 DECL_ATTRIBUTES (t
));
36749 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
36751 id
= get_identifier ("omp declare target link");
36752 std::swap (at1
, at2
);
36755 id
= get_identifier ("omp declare target");
36758 error_at (OMP_CLAUSE_LOCATION (c
),
36759 "%qD specified both in declare target %<link%> and %<to%>"
36765 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
36766 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
36769 symtab_node
*node
= symtab_node::get (t
);
36772 node
->offloadable
= 1;
36773 if (ENABLE_OFFLOADING
)
36775 g
->have_offload
= true;
36776 if (is_a
<varpool_node
*> (node
))
36777 vec_safe_push (offload_vars
, t
);
36785 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36787 const char *p
= "";
36788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36790 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36791 p
= IDENTIFIER_POINTER (id
);
36793 if (strcmp (p
, "declare") == 0)
36795 cp_lexer_consume_token (parser
->lexer
);
36797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36799 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36800 p
= IDENTIFIER_POINTER (id
);
36802 if (strcmp (p
, "target") == 0)
36803 cp_lexer_consume_token (parser
->lexer
);
36806 cp_parser_error (parser
, "expected %<target%>");
36807 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36813 cp_parser_error (parser
, "expected %<declare%>");
36814 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36817 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36818 if (!scope_chain
->omp_declare_target_attribute
)
36819 error_at (pragma_tok
->location
,
36820 "%<#pragma omp end declare target%> without corresponding "
36821 "%<#pragma omp declare target%>");
36823 scope_chain
->omp_declare_target_attribute
--;
36826 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36827 expression and optional initializer clause of
36828 #pragma omp declare reduction. We store the expression(s) as
36829 either 3, 6 or 7 special statements inside of the artificial function's
36830 body. The first two statements are DECL_EXPRs for the artificial
36831 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36832 expression that uses those variables.
36833 If there was any INITIALIZER clause, this is followed by further statements,
36834 the fourth and fifth statements are DECL_EXPRs for the artificial
36835 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36836 constructor variant (first token after open paren is not omp_priv),
36837 then the sixth statement is a statement with the function call expression
36838 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36839 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36840 to initialize the OMP_PRIV artificial variable and there is seventh
36841 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36844 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
36846 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
36847 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
36848 type
= TREE_TYPE (type
);
36849 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
36850 DECL_ARTIFICIAL (omp_out
) = 1;
36851 pushdecl (omp_out
);
36852 add_decl_expr (omp_out
);
36853 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
36854 DECL_ARTIFICIAL (omp_in
) = 1;
36856 add_decl_expr (omp_in
);
36858 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
36860 keep_next_level (true);
36861 tree block
= begin_omp_structured_block ();
36862 combiner
= cp_parser_expression (parser
);
36863 finish_expr_stmt (combiner
);
36864 block
= finish_omp_structured_block (block
);
36867 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36870 const char *p
= "";
36871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36873 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36874 p
= IDENTIFIER_POINTER (id
);
36877 if (strcmp (p
, "initializer") == 0)
36879 cp_lexer_consume_token (parser
->lexer
);
36880 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36886 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36887 p
= IDENTIFIER_POINTER (id
);
36890 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
36891 DECL_ARTIFICIAL (omp_priv
) = 1;
36892 pushdecl (omp_priv
);
36893 add_decl_expr (omp_priv
);
36894 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
36895 DECL_ARTIFICIAL (omp_orig
) = 1;
36896 pushdecl (omp_orig
);
36897 add_decl_expr (omp_orig
);
36899 keep_next_level (true);
36900 block
= begin_omp_structured_block ();
36903 if (strcmp (p
, "omp_priv") == 0)
36905 bool is_direct_init
, is_non_constant_init
;
36907 cp_lexer_consume_token (parser
->lexer
);
36908 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
36910 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36911 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
36913 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
36914 == CPP_CLOSE_PAREN
))
36916 finish_omp_structured_block (block
);
36917 error ("invalid initializer clause");
36920 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
36921 &is_non_constant_init
);
36922 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
36923 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
36927 cp_parser_parse_tentatively (parser
);
36928 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36929 /*check_dependency_p=*/true,
36930 /*template_p=*/NULL
,
36931 /*declarator_p=*/false,
36932 /*optional_p=*/false);
36933 vec
<tree
, va_gc
> *args
;
36934 if (fn_name
== error_mark_node
36935 || cp_parser_error_occurred (parser
)
36936 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36937 || ((args
= cp_parser_parenthesized_expression_list
36938 (parser
, non_attr
, /*cast_p=*/false,
36939 /*allow_expansion_p=*/true,
36940 /*non_constant_p=*/NULL
)),
36941 cp_parser_error_occurred (parser
)))
36943 finish_omp_structured_block (block
);
36944 cp_parser_abort_tentative_parse (parser
);
36945 cp_parser_error (parser
, "expected id-expression (arguments)");
36950 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
36951 if (arg
== omp_priv
36952 || (TREE_CODE (arg
) == ADDR_EXPR
36953 && TREE_OPERAND (arg
, 0) == omp_priv
))
36955 cp_parser_abort_tentative_parse (parser
);
36956 if (arg
== NULL_TREE
)
36957 error ("one of the initializer call arguments should be %<omp_priv%>"
36958 " or %<&omp_priv%>");
36959 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
36961 finish_expr_stmt (initializer
);
36964 block
= finish_omp_structured_block (block
);
36965 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
36969 add_decl_expr (omp_orig
);
36971 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36975 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
36976 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
36982 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36983 initializer-clause[opt] new-line
36985 initializer-clause:
36986 initializer (omp_priv initializer)
36987 initializer (function-name (argument-list)) */
36990 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
36991 enum pragma_context
)
36993 auto_vec
<tree
> types
;
36994 enum tree_code reduc_code
= ERROR_MARK
;
36995 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
36997 cp_token
*first_token
;
36998 cp_token_cache
*cp
;
37002 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37003 p
= obstack_alloc (&declarator_obstack
, 0);
37005 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37008 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37011 reduc_code
= PLUS_EXPR
;
37014 reduc_code
= MULT_EXPR
;
37017 reduc_code
= MINUS_EXPR
;
37020 reduc_code
= BIT_AND_EXPR
;
37023 reduc_code
= BIT_XOR_EXPR
;
37026 reduc_code
= BIT_IOR_EXPR
;
37029 reduc_code
= TRUTH_ANDIF_EXPR
;
37032 reduc_code
= TRUTH_ORIF_EXPR
;
37035 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37038 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37039 "%<|%>, %<&&%>, %<||%> or identifier");
37043 if (reduc_code
!= ERROR_MARK
)
37044 cp_lexer_consume_token (parser
->lexer
);
37046 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37047 if (reduc_id
== error_mark_node
)
37050 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37053 /* Types may not be defined in declare reduction type list. */
37054 const char *saved_message
;
37055 saved_message
= parser
->type_definition_forbidden_message
;
37056 parser
->type_definition_forbidden_message
37057 = G_("types may not be defined in declare reduction type list");
37058 bool saved_colon_corrects_to_scope_p
;
37059 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37060 parser
->colon_corrects_to_scope_p
= false;
37061 bool saved_colon_doesnt_start_class_def_p
;
37062 saved_colon_doesnt_start_class_def_p
37063 = parser
->colon_doesnt_start_class_def_p
;
37064 parser
->colon_doesnt_start_class_def_p
= true;
37068 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37069 type
= cp_parser_type_id (parser
);
37070 if (type
== error_mark_node
)
37072 else if (ARITHMETIC_TYPE_P (type
)
37073 && (orig_reduc_id
== NULL_TREE
37074 || (TREE_CODE (type
) != COMPLEX_TYPE
37075 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37077 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37079 error_at (loc
, "predeclared arithmetic type %qT in "
37080 "%<#pragma omp declare reduction%>", type
);
37081 else if (TREE_CODE (type
) == FUNCTION_TYPE
37082 || TREE_CODE (type
) == METHOD_TYPE
37083 || TREE_CODE (type
) == ARRAY_TYPE
)
37084 error_at (loc
, "function or array type %qT in "
37085 "%<#pragma omp declare reduction%>", type
);
37086 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37087 error_at (loc
, "reference type %qT in "
37088 "%<#pragma omp declare reduction%>", type
);
37089 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37090 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37091 "%<#pragma omp declare reduction%>", type
);
37093 types
.safe_push (type
);
37095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37096 cp_lexer_consume_token (parser
->lexer
);
37101 /* Restore the saved message. */
37102 parser
->type_definition_forbidden_message
= saved_message
;
37103 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37104 parser
->colon_doesnt_start_class_def_p
37105 = saved_colon_doesnt_start_class_def_p
;
37107 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37108 || types
.is_empty ())
37111 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37115 first_token
= cp_lexer_peek_token (parser
->lexer
);
37118 FOR_EACH_VEC_ELT (types
, i
, type
)
37121 = build_function_type_list (void_type_node
,
37122 cp_build_reference_type (type
, false),
37124 tree this_reduc_id
= reduc_id
;
37125 if (!dependent_type_p (type
))
37126 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37127 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37128 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37129 DECL_ARTIFICIAL (fndecl
) = 1;
37130 DECL_EXTERNAL (fndecl
) = 1;
37131 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37132 DECL_IGNORED_P (fndecl
) = 1;
37133 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37134 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37135 DECL_ATTRIBUTES (fndecl
)
37136 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37137 DECL_ATTRIBUTES (fndecl
));
37138 if (processing_template_decl
)
37139 fndecl
= push_template_decl (fndecl
);
37140 bool block_scope
= false;
37141 tree block
= NULL_TREE
;
37142 if (current_function_decl
)
37144 block_scope
= true;
37145 DECL_CONTEXT (fndecl
) = global_namespace
;
37146 if (!processing_template_decl
)
37149 else if (current_class_type
)
37153 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37154 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37155 cp_lexer_consume_token (parser
->lexer
);
37156 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37158 cp
= cp_token_cache_new (first_token
,
37159 cp_lexer_peek_nth_token (parser
->lexer
,
37162 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37163 finish_member_declaration (fndecl
);
37164 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37165 DECL_PENDING_INLINE_P (fndecl
) = 1;
37166 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37171 DECL_CONTEXT (fndecl
) = current_namespace
;
37175 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37177 block
= begin_omp_structured_block ();
37180 cp_parser_push_lexer_for_tokens (parser
, cp
);
37181 parser
->lexer
->in_pragma
= true;
37183 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37186 finish_function (0);
37188 DECL_CONTEXT (fndecl
) = current_function_decl
;
37190 cp_parser_pop_lexer (parser
);
37194 cp_parser_pop_lexer (parser
);
37196 finish_function (0);
37199 DECL_CONTEXT (fndecl
) = current_function_decl
;
37200 block
= finish_omp_structured_block (block
);
37201 if (TREE_CODE (block
) == BIND_EXPR
)
37202 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37203 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37204 DECL_SAVED_TREE (fndecl
) = block
;
37205 if (processing_template_decl
)
37206 add_decl_expr (fndecl
);
37208 cp_check_omp_declare_reduction (fndecl
);
37209 if (cp
== NULL
&& types
.length () > 1)
37210 cp
= cp_token_cache_new (first_token
,
37211 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37212 if (errs
!= errorcount
)
37216 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37219 /* Free any declarators allocated. */
37220 obstack_free (&declarator_obstack
, p
);
37224 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37225 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37226 initializer-clause[opt] new-line
37227 #pragma omp declare target new-line */
37230 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37231 enum pragma_context context
)
37233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37235 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37236 const char *p
= IDENTIFIER_POINTER (id
);
37238 if (strcmp (p
, "simd") == 0)
37240 cp_lexer_consume_token (parser
->lexer
);
37241 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37245 cp_ensure_no_omp_declare_simd (parser
);
37246 if (strcmp (p
, "reduction") == 0)
37248 cp_lexer_consume_token (parser
->lexer
);
37249 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37253 if (!flag_openmp
) /* flag_openmp_simd */
37255 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37258 if (strcmp (p
, "target") == 0)
37260 cp_lexer_consume_token (parser
->lexer
);
37261 cp_parser_omp_declare_target (parser
, pragma_tok
);
37265 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37267 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37271 #pragma omp taskloop taskloop-clause[optseq] new-line
37274 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37277 #define OMP_TASKLOOP_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37294 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37295 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37298 tree clauses
, sb
, ret
;
37300 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37302 strcat (p_name
, " taskloop");
37303 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37308 const char *p
= IDENTIFIER_POINTER (id
);
37310 if (strcmp (p
, "simd") == 0)
37312 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37313 if (cclauses
== NULL
)
37314 cclauses
= cclauses_buf
;
37316 cp_lexer_consume_token (parser
->lexer
);
37317 if (!flag_openmp
) /* flag_openmp_simd */
37318 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37320 sb
= begin_omp_structured_block ();
37321 save
= cp_parser_begin_omp_structured_block (parser
);
37322 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37324 cp_parser_end_omp_structured_block (parser
, save
);
37325 tree body
= finish_omp_structured_block (sb
);
37328 ret
= make_node (OMP_TASKLOOP
);
37329 TREE_TYPE (ret
) = void_type_node
;
37330 OMP_FOR_BODY (ret
) = body
;
37331 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37332 SET_EXPR_LOCATION (ret
, loc
);
37337 if (!flag_openmp
) /* flag_openmp_simd */
37339 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37343 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37347 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37348 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37351 sb
= begin_omp_structured_block ();
37352 save
= cp_parser_begin_omp_structured_block (parser
);
37354 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37357 cp_parser_end_omp_structured_block (parser
, save
);
37358 add_stmt (finish_omp_structured_block (sb
));
37365 # pragma acc routine oacc-routine-clause[optseq] new-line
37366 function-definition
37368 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37371 #define OACC_ROUTINE_CLAUSE_MASK \
37372 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37378 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37379 component, which must resolve to a declared namespace-scope
37380 function. The clauses are either processed directly (for a named
37381 function), or defered until the immediatley following declaration
37385 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37386 enum pragma_context context
)
37388 gcc_checking_assert (context
== pragma_external
);
37389 /* The checking for "another pragma following this one" in the "no optional
37390 '( name )'" case makes sure that we dont re-enter. */
37391 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37393 cp_oacc_routine_data data
;
37394 data
.error_seen
= false;
37395 data
.fndecl_seen
= false;
37396 data
.tokens
= vNULL
;
37397 data
.clauses
= NULL_TREE
;
37398 data
.loc
= pragma_tok
->location
;
37399 /* It is safe to take the address of a local variable; it will only be
37400 used while this scope is live. */
37401 parser
->oacc_routine
= &data
;
37403 /* Look for optional '( name )'. */
37404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37406 cp_lexer_consume_token (parser
->lexer
); /* '(' */
37408 /* We parse the name as an id-expression. If it resolves to
37409 anything other than a non-overloaded function at namespace
37410 scope, it's an error. */
37411 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37412 tree name
= cp_parser_id_expression (parser
,
37413 /*template_keyword_p=*/false,
37414 /*check_dependency_p=*/false,
37415 /*template_p=*/NULL
,
37416 /*declarator_p=*/false,
37417 /*optional_p=*/false);
37418 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37419 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37420 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37422 if (decl
== error_mark_node
37423 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37425 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37426 parser
->oacc_routine
= NULL
;
37431 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37432 "#pragma acc routine",
37433 cp_lexer_peek_token (parser
->lexer
));
37435 if (decl
&& is_overloaded_fn (decl
)
37436 && (TREE_CODE (decl
) != FUNCTION_DECL
37437 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37439 error_at (name_loc
,
37440 "%<#pragma acc routine%> names a set of overloads");
37441 parser
->oacc_routine
= NULL
;
37445 /* Perhaps we should use the same rule as declarations in different
37447 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37449 error_at (name_loc
,
37450 "%qD does not refer to a namespace scope function", decl
);
37451 parser
->oacc_routine
= NULL
;
37455 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37457 error_at (name_loc
, "%qD does not refer to a function", decl
);
37458 parser
->oacc_routine
= NULL
;
37462 cp_finalize_oacc_routine (parser
, decl
, false);
37463 parser
->oacc_routine
= NULL
;
37465 else /* No optional '( name )'. */
37467 /* Store away all pragma tokens. */
37468 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37469 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37470 cp_lexer_consume_token (parser
->lexer
);
37471 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37472 parser
->oacc_routine
->error_seen
= true;
37473 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37474 struct cp_token_cache
*cp
37475 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37476 parser
->oacc_routine
->tokens
.safe_push (cp
);
37478 /* Emit a helpful diagnostic if there's another pragma following this
37480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37482 cp_ensure_no_oacc_routine (parser
);
37483 data
.tokens
.release ();
37484 /* ..., and then just keep going. */
37488 /* We only have to consider the pragma_external case here. */
37489 cp_parser_declaration (parser
);
37490 if (parser
->oacc_routine
37491 && !parser
->oacc_routine
->fndecl_seen
)
37492 cp_ensure_no_oacc_routine (parser
);
37494 parser
->oacc_routine
= NULL
;
37495 data
.tokens
.release ();
37499 /* Finalize #pragma acc routine clauses after direct declarator has
37503 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37505 struct cp_token_cache
*ce
;
37506 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37508 if (!data
->error_seen
&& data
->fndecl_seen
)
37510 error_at (data
->loc
,
37511 "%<#pragma acc routine%> not immediately followed by "
37512 "a single function declaration or definition");
37513 data
->error_seen
= true;
37515 if (data
->error_seen
)
37518 gcc_checking_assert (data
->tokens
.length () == 1);
37519 ce
= data
->tokens
[0];
37521 cp_parser_push_lexer_for_tokens (parser
, ce
);
37522 parser
->lexer
->in_pragma
= true;
37523 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37525 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37526 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37527 parser
->oacc_routine
->clauses
37528 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37529 "#pragma acc routine", pragma_tok
);
37530 cp_parser_pop_lexer (parser
);
37531 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37537 /* Apply any saved OpenACC routine clauses to a just-parsed
37541 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37543 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37545 /* Keep going if we're in error reporting mode. */
37546 if (parser
->oacc_routine
->error_seen
37547 || fndecl
== error_mark_node
)
37550 if (parser
->oacc_routine
->fndecl_seen
)
37552 error_at (parser
->oacc_routine
->loc
,
37553 "%<#pragma acc routine%> not immediately followed by"
37554 " a single function declaration or definition");
37555 parser
->oacc_routine
= NULL
;
37558 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37560 cp_ensure_no_oacc_routine (parser
);
37564 if (oacc_get_fn_attrib (fndecl
))
37566 error_at (parser
->oacc_routine
->loc
,
37567 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37568 parser
->oacc_routine
= NULL
;
37572 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37574 error_at (parser
->oacc_routine
->loc
,
37576 ? G_("%<#pragma acc routine%> must be applied before use")
37577 : G_("%<#pragma acc routine%> must be applied before "
37579 parser
->oacc_routine
= NULL
;
37583 /* Process the routine's dimension clauses. */
37584 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37585 oacc_replace_fn_attrib (fndecl
, dims
);
37587 /* Add an "omp declare target" attribute. */
37588 DECL_ATTRIBUTES (fndecl
)
37589 = tree_cons (get_identifier ("omp declare target"),
37590 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37592 /* Don't unset parser->oacc_routine here: we may still need it to
37593 diagnose wrong usage. But, remember that we've used this "#pragma acc
37595 parser
->oacc_routine
->fndecl_seen
= true;
37599 /* Main entry point to OpenMP statement pragmas. */
37602 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37605 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37606 omp_clause_mask
mask (0);
37608 switch (cp_parser_pragma_kind (pragma_tok
))
37610 case PRAGMA_OACC_ATOMIC
:
37611 cp_parser_omp_atomic (parser
, pragma_tok
);
37613 case PRAGMA_OACC_CACHE
:
37614 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37616 case PRAGMA_OACC_DATA
:
37617 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37619 case PRAGMA_OACC_ENTER_DATA
:
37620 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37622 case PRAGMA_OACC_EXIT_DATA
:
37623 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
37625 case PRAGMA_OACC_HOST_DATA
:
37626 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
37628 case PRAGMA_OACC_KERNELS
:
37629 case PRAGMA_OACC_PARALLEL
:
37630 strcpy (p_name
, "#pragma acc");
37631 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
37634 case PRAGMA_OACC_LOOP
:
37635 strcpy (p_name
, "#pragma acc");
37636 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37639 case PRAGMA_OACC_UPDATE
:
37640 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
37642 case PRAGMA_OACC_WAIT
:
37643 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
37645 case PRAGMA_OMP_ATOMIC
:
37646 cp_parser_omp_atomic (parser
, pragma_tok
);
37648 case PRAGMA_OMP_CRITICAL
:
37649 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
37651 case PRAGMA_OMP_DISTRIBUTE
:
37652 strcpy (p_name
, "#pragma omp");
37653 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
37656 case PRAGMA_OMP_FOR
:
37657 strcpy (p_name
, "#pragma omp");
37658 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
37661 case PRAGMA_OMP_MASTER
:
37662 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
37664 case PRAGMA_OMP_PARALLEL
:
37665 strcpy (p_name
, "#pragma omp");
37666 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
37669 case PRAGMA_OMP_SECTIONS
:
37670 strcpy (p_name
, "#pragma omp");
37671 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
37673 case PRAGMA_OMP_SIMD
:
37674 strcpy (p_name
, "#pragma omp");
37675 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
37678 case PRAGMA_OMP_SINGLE
:
37679 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
37681 case PRAGMA_OMP_TASK
:
37682 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
37684 case PRAGMA_OMP_TASKGROUP
:
37685 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
37687 case PRAGMA_OMP_TASKLOOP
:
37688 strcpy (p_name
, "#pragma omp");
37689 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37692 case PRAGMA_OMP_TEAMS
:
37693 strcpy (p_name
, "#pragma omp");
37694 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
37698 gcc_unreachable ();
37701 protected_set_expr_location (stmt
, pragma_tok
->location
);
37704 /* Transactional Memory parsing routines. */
37706 /* Parse a transaction attribute.
37712 We use this instead of cp_parser_attributes_opt for transactions to avoid
37713 the pedwarn in C++98 mode. */
37716 cp_parser_txn_attribute_opt (cp_parser
*parser
)
37719 tree attr_name
, attr
= NULL
;
37721 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
37722 return cp_parser_attributes_opt (parser
);
37724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
37726 cp_lexer_consume_token (parser
->lexer
);
37727 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
37730 token
= cp_lexer_peek_token (parser
->lexer
);
37731 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
37733 token
= cp_lexer_consume_token (parser
->lexer
);
37735 attr_name
= (token
->type
== CPP_KEYWORD
37736 /* For keywords, use the canonical spelling,
37737 not the parsed identifier. */
37738 ? ridpointers
[(int) token
->keyword
]
37740 attr
= build_tree_list (attr_name
, NULL_TREE
);
37743 cp_parser_error (parser
, "expected identifier");
37745 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37747 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37751 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37753 transaction-statement:
37754 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37756 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37760 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
37762 unsigned char old_in
= parser
->in_transaction
;
37763 unsigned char this_in
= 1, new_in
;
37764 enum rid keyword
= token
->keyword
;
37765 tree stmt
, attrs
, noex
;
37767 cp_lexer_consume_token (parser
->lexer
);
37769 if (keyword
== RID_TRANSACTION_RELAXED
37770 || keyword
== RID_SYNCHRONIZED
)
37771 this_in
|= TM_STMT_ATTR_RELAXED
;
37774 attrs
= cp_parser_txn_attribute_opt (parser
);
37776 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37779 /* Parse a noexcept specification. */
37780 if (keyword
== RID_ATOMIC_NOEXCEPT
)
37781 noex
= boolean_true_node
;
37782 else if (keyword
== RID_ATOMIC_CANCEL
)
37784 /* cancel-and-throw is unimplemented. */
37785 sorry ("atomic_cancel");
37789 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
37791 /* Keep track if we're in the lexical scope of an outer transaction. */
37792 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
37794 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
37796 parser
->in_transaction
= new_in
;
37797 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
37798 parser
->in_transaction
= old_in
;
37800 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
37805 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37807 transaction-expression:
37808 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37809 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37813 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
37815 unsigned char old_in
= parser
->in_transaction
;
37816 unsigned char this_in
= 1;
37820 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37822 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37823 || keyword
== RID_TRANSACTION_RELAXED
);
37827 keyword
== RID_TRANSACTION_RELAXED
37828 ? G_("%<__transaction_relaxed%> without transactional memory "
37830 : G_("%<__transaction_atomic%> without transactional memory "
37831 "support enabled"));
37833 token
= cp_parser_require_keyword (parser
, keyword
,
37834 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37835 : RT_TRANSACTION_RELAXED
));
37836 gcc_assert (token
!= NULL
);
37838 if (keyword
== RID_TRANSACTION_RELAXED
)
37839 this_in
|= TM_STMT_ATTR_RELAXED
;
37841 /* Set this early. This might mean that we allow transaction_cancel in
37842 an expression that we find out later actually has to be a constexpr.
37843 However, we expect that cxx_constant_value will be able to deal with
37844 this; also, if the noexcept has no constexpr, then what we parse next
37845 really is a transaction's body. */
37846 parser
->in_transaction
= this_in
;
37848 /* Parse a noexcept specification. */
37849 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
37852 if (!noex
|| !noex_expr
37853 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37855 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
37857 expr
= cp_parser_expression (parser
);
37858 expr
= finish_parenthesized_expr (expr
);
37860 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
37864 /* The only expression that is available got parsed for the noexcept
37865 already. noexcept is true then. */
37867 noex
= boolean_true_node
;
37870 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
37871 parser
->in_transaction
= old_in
;
37873 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
37874 return error_mark_node
;
37876 return (flag_tm
? expr
: error_mark_node
);
37879 /* Parse a function-transaction-block.
37881 function-transaction-block:
37882 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37884 __transaction_atomic txn-attribute[opt] function-try-block
37885 __transaction_relaxed ctor-initializer[opt] function-body
37886 __transaction_relaxed function-try-block
37890 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
37892 unsigned char old_in
= parser
->in_transaction
;
37893 unsigned char new_in
= 1;
37894 tree compound_stmt
, stmt
, attrs
;
37895 bool ctor_initializer_p
;
37898 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37899 || keyword
== RID_TRANSACTION_RELAXED
);
37900 token
= cp_parser_require_keyword (parser
, keyword
,
37901 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37902 : RT_TRANSACTION_RELAXED
));
37903 gcc_assert (token
!= NULL
);
37905 if (keyword
== RID_TRANSACTION_RELAXED
)
37906 new_in
|= TM_STMT_ATTR_RELAXED
;
37909 attrs
= cp_parser_txn_attribute_opt (parser
);
37911 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37914 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
37916 parser
->in_transaction
= new_in
;
37918 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
37919 ctor_initializer_p
= cp_parser_function_try_block (parser
);
37921 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
37922 (parser
, /*in_function_try_block=*/false);
37924 parser
->in_transaction
= old_in
;
37926 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
37928 return ctor_initializer_p
;
37931 /* Parse a __transaction_cancel statement.
37934 __transaction_cancel txn-attribute[opt] ;
37935 __transaction_cancel txn-attribute[opt] throw-expression ;
37937 ??? Cancel and throw is not yet implemented. */
37940 cp_parser_transaction_cancel (cp_parser
*parser
)
37943 bool is_outer
= false;
37946 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
37947 RT_TRANSACTION_CANCEL
);
37948 gcc_assert (token
!= NULL
);
37950 attrs
= cp_parser_txn_attribute_opt (parser
);
37952 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
37954 /* ??? Parse cancel-and-throw here. */
37956 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37960 error_at (token
->location
, "%<__transaction_cancel%> without "
37961 "transactional memory support enabled");
37962 return error_mark_node
;
37964 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
37966 error_at (token
->location
, "%<__transaction_cancel%> within a "
37967 "%<__transaction_relaxed%>");
37968 return error_mark_node
;
37972 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
37973 && !is_tm_may_cancel_outer (current_function_decl
))
37975 error_at (token
->location
, "outer %<__transaction_cancel%> not "
37976 "within outer %<__transaction_atomic%>");
37977 error_at (token
->location
,
37978 " or a %<transaction_may_cancel_outer%> function");
37979 return error_mark_node
;
37982 else if (parser
->in_transaction
== 0)
37984 error_at (token
->location
, "%<__transaction_cancel%> not within "
37985 "%<__transaction_atomic%>");
37986 return error_mark_node
;
37989 stmt
= build_tm_abort_call (token
->location
, is_outer
);
37997 static GTY (()) cp_parser
*the_parser
;
38000 /* Special handling for the first token or line in the file. The first
38001 thing in the file might be #pragma GCC pch_preprocess, which loads a
38002 PCH file, which is a GC collection point. So we need to handle this
38003 first pragma without benefit of an existing lexer structure.
38005 Always returns one token to the caller in *FIRST_TOKEN. This is
38006 either the true first token of the file, or the first token after
38007 the initial pragma. */
38010 cp_parser_initial_pragma (cp_token
*first_token
)
38014 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38015 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38018 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38019 if (first_token
->type
== CPP_STRING
)
38021 name
= first_token
->u
.value
;
38023 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38024 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38025 error_at (first_token
->location
,
38026 "junk at end of %<#pragma GCC pch_preprocess%>");
38029 error_at (first_token
->location
, "expected string literal");
38031 /* Skip to the end of the pragma. */
38032 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38033 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38035 /* Now actually load the PCH file. */
38037 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38039 /* Read one more token to return to our caller. We have to do this
38040 after reading the PCH file in, since its pointers have to be
38042 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38045 /* Parses the grainsize pragma for the _Cilk_for statement.
38047 #pragma cilk grainsize = <VALUE>. */
38050 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38052 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38054 tree exp
= cp_parser_binary_expression (parser
, false, false,
38055 PREC_NOT_OPERATOR
, NULL
);
38056 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38057 if (!exp
|| exp
== error_mark_node
)
38059 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
38063 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38064 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
38065 cp_parser_cilk_for (parser
, exp
, if_p
);
38067 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
38068 "%<#pragma cilk grainsize%> is not followed by "
38072 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38075 /* Normal parsing of a pragma token. Here we can (and must) use the
38079 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38081 cp_token
*pragma_tok
;
38086 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38087 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38088 parser
->lexer
->in_pragma
= true;
38090 id
= cp_parser_pragma_kind (pragma_tok
);
38091 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38092 cp_ensure_no_omp_declare_simd (parser
);
38095 case PRAGMA_GCC_PCH_PREPROCESS
:
38096 error_at (pragma_tok
->location
,
38097 "%<#pragma GCC pch_preprocess%> must be first");
38100 case PRAGMA_OMP_BARRIER
:
38103 case pragma_compound
:
38104 cp_parser_omp_barrier (parser
, pragma_tok
);
38107 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
38108 "used in compound statements");
38115 case PRAGMA_OMP_FLUSH
:
38118 case pragma_compound
:
38119 cp_parser_omp_flush (parser
, pragma_tok
);
38122 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
38123 "used in compound statements");
38130 case PRAGMA_OMP_TASKWAIT
:
38133 case pragma_compound
:
38134 cp_parser_omp_taskwait (parser
, pragma_tok
);
38137 error_at (pragma_tok
->location
,
38138 "%<#pragma omp taskwait%> may only be "
38139 "used in compound statements");
38146 case PRAGMA_OMP_TASKYIELD
:
38149 case pragma_compound
:
38150 cp_parser_omp_taskyield (parser
, pragma_tok
);
38153 error_at (pragma_tok
->location
,
38154 "%<#pragma omp taskyield%> may only be "
38155 "used in compound statements");
38162 case PRAGMA_OMP_CANCEL
:
38165 case pragma_compound
:
38166 cp_parser_omp_cancel (parser
, pragma_tok
);
38169 error_at (pragma_tok
->location
,
38170 "%<#pragma omp cancel%> may only be "
38171 "used in compound statements");
38178 case PRAGMA_OMP_CANCELLATION_POINT
:
38179 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38182 case PRAGMA_OMP_THREADPRIVATE
:
38183 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38186 case PRAGMA_OMP_DECLARE
:
38187 cp_parser_omp_declare (parser
, pragma_tok
, context
);
38190 case PRAGMA_OACC_DECLARE
:
38191 cp_parser_oacc_declare (parser
, pragma_tok
);
38194 case PRAGMA_OACC_ENTER_DATA
:
38195 if (context
== pragma_stmt
)
38197 cp_parser_error (parser
, "%<#pragma acc enter data%> may only be "
38198 "used in compound statements");
38201 else if (context
!= pragma_compound
)
38203 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38206 case PRAGMA_OACC_EXIT_DATA
:
38207 if (context
== pragma_stmt
)
38209 cp_parser_error (parser
, "%<#pragma acc exit data%> may only be "
38210 "used in compound statements");
38213 else if (context
!= pragma_compound
)
38215 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38218 case PRAGMA_OACC_ROUTINE
:
38219 if (context
!= pragma_external
)
38221 error_at (pragma_tok
->location
,
38222 "%<#pragma acc routine%> must be at file scope");
38225 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38228 case PRAGMA_OACC_UPDATE
:
38229 if (context
== pragma_stmt
)
38231 cp_parser_error (parser
, "%<#pragma acc update%> may only be "
38232 "used in compound statements");
38235 else if (context
!= pragma_compound
)
38237 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38240 case PRAGMA_OACC_WAIT
:
38241 if (context
== pragma_stmt
)
38243 cp_parser_error (parser
, "%<#pragma acc wait%> may only be "
38244 "used in compound statements");
38247 else if (context
!= pragma_compound
)
38249 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38252 case PRAGMA_OACC_ATOMIC
:
38253 case PRAGMA_OACC_CACHE
:
38254 case PRAGMA_OACC_DATA
:
38255 case PRAGMA_OACC_HOST_DATA
:
38256 case PRAGMA_OACC_KERNELS
:
38257 case PRAGMA_OACC_PARALLEL
:
38258 case PRAGMA_OACC_LOOP
:
38259 case PRAGMA_OMP_ATOMIC
:
38260 case PRAGMA_OMP_CRITICAL
:
38261 case PRAGMA_OMP_DISTRIBUTE
:
38262 case PRAGMA_OMP_FOR
:
38263 case PRAGMA_OMP_MASTER
:
38264 case PRAGMA_OMP_PARALLEL
:
38265 case PRAGMA_OMP_SECTIONS
:
38266 case PRAGMA_OMP_SIMD
:
38267 case PRAGMA_OMP_SINGLE
:
38268 case PRAGMA_OMP_TASK
:
38269 case PRAGMA_OMP_TASKGROUP
:
38270 case PRAGMA_OMP_TASKLOOP
:
38271 case PRAGMA_OMP_TEAMS
:
38272 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38274 stmt
= push_omp_privatization_clauses (false);
38275 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38276 pop_omp_privatization_clauses (stmt
);
38279 case PRAGMA_OMP_ORDERED
:
38280 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38282 stmt
= push_omp_privatization_clauses (false);
38283 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38284 pop_omp_privatization_clauses (stmt
);
38287 case PRAGMA_OMP_TARGET
:
38288 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38290 stmt
= push_omp_privatization_clauses (false);
38291 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38292 pop_omp_privatization_clauses (stmt
);
38295 case PRAGMA_OMP_END_DECLARE_TARGET
:
38296 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38299 case PRAGMA_OMP_SECTION
:
38300 error_at (pragma_tok
->location
,
38301 "%<#pragma omp section%> may only be used in "
38302 "%<#pragma omp sections%> construct");
38307 if (context
== pragma_external
)
38309 error_at (pragma_tok
->location
,
38310 "%<#pragma GCC ivdep%> must be inside a function");
38313 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38315 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38316 if (tok
->type
!= CPP_KEYWORD
38317 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38318 && tok
->keyword
!= RID_DO
))
38320 cp_parser_error (parser
, "for, while or do statement expected");
38323 cp_parser_iteration_statement (parser
, if_p
, true);
38327 case PRAGMA_CILK_SIMD
:
38328 if (context
== pragma_external
)
38330 error_at (pragma_tok
->location
,
38331 "%<#pragma simd%> must be inside a function");
38334 stmt
= push_omp_privatization_clauses (false);
38335 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
38336 pop_omp_privatization_clauses (stmt
);
38339 case PRAGMA_CILK_GRAINSIZE
:
38340 if (context
== pragma_external
)
38342 error_at (pragma_tok
->location
,
38343 "%<#pragma cilk grainsize%> must be inside a function");
38347 /* Ignore the pragma if Cilk Plus is not enabled. */
38350 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
38355 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
38356 "%<#pragma cilk grainsize%>");
38361 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38362 c_invoke_pragma_handler (id
);
38366 cp_parser_error (parser
, "expected declaration specifiers");
38370 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38374 /* The interface the pragma parsers have to the lexer. */
38377 pragma_lex (tree
*value
, location_t
*loc
)
38379 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38380 enum cpp_ttype ret
= tok
->type
;
38382 *value
= tok
->u
.value
;
38384 *loc
= tok
->location
;
38386 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38388 else if (ret
== CPP_STRING
)
38389 *value
= cp_parser_string_literal (the_parser
, false, false);
38392 if (ret
== CPP_KEYWORD
)
38394 cp_lexer_consume_token (the_parser
->lexer
);
38401 /* External interface. */
38403 /* Parse one entire translation unit. */
38406 c_parse_file (void)
38408 static bool already_called
= false;
38410 if (already_called
)
38411 fatal_error (input_location
,
38412 "inter-module optimizations not implemented for C++");
38413 already_called
= true;
38415 the_parser
= cp_parser_new ();
38416 push_deferring_access_checks (flag_access_control
38417 ? dk_no_deferred
: dk_no_check
);
38418 cp_parser_translation_unit (the_parser
);
38422 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38423 vectorlength clause:
38425 vectorlength ( constant-expression ) */
38428 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
38431 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38433 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38434 safelen clause. Thus, vectorlength is represented as OMP 4.0
38435 safelen. For SIMD-enabled function it is represented by OMP 4.0
38438 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
38441 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
38444 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38445 return error_mark_node
;
38447 expr
= cp_parser_constant_expression (parser
);
38448 expr
= maybe_constant_value (expr
);
38450 /* If expr == error_mark_node, then don't emit any errors nor
38451 create a clause. if any of the above functions returns
38452 error mark node then they would have emitted an error message. */
38453 if (expr
== error_mark_node
)
38455 else if (!TREE_TYPE (expr
)
38456 || !TREE_CONSTANT (expr
)
38457 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
38458 error_at (loc
, "vectorlength must be an integer constant");
38459 else if (TREE_CONSTANT (expr
)
38460 && !pow2p_hwi (TREE_INT_CST_LOW (expr
)))
38461 error_at (loc
, "vectorlength must be a power of 2");
38467 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
38468 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
38469 OMP_CLAUSE_CHAIN (c
) = clauses
;
38474 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
38475 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
38476 OMP_CLAUSE_CHAIN (c
) = clauses
;
38481 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38482 return error_mark_node
;
38486 /* Handles the Cilk Plus #pragma simd linear clause.
38488 linear ( simd-linear-variable-list )
38490 simd-linear-variable-list:
38491 simd-linear-variable
38492 simd-linear-variable-list , simd-linear-variable
38494 simd-linear-variable:
38496 id-expression : simd-linear-step
38499 conditional-expression */
38502 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
38504 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38506 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38508 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38510 cp_parser_error (parser
, "expected identifier");
38511 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38512 return error_mark_node
;
38515 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38516 parser
->colon_corrects_to_scope_p
= false;
38519 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38520 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38522 cp_parser_error (parser
, "expected variable-name");
38523 clauses
= error_mark_node
;
38527 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
38529 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
38531 if (decl
== error_mark_node
)
38533 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
38535 clauses
= error_mark_node
;
38539 tree e
= NULL_TREE
;
38540 tree step_size
= integer_one_node
;
38542 /* If present, parse the linear step. Otherwise, assume the default
38544 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
38546 cp_lexer_consume_token (parser
->lexer
);
38548 e
= cp_parser_assignment_expression (parser
);
38549 e
= maybe_constant_value (e
);
38551 if (e
== error_mark_node
)
38553 /* If an error has occurred, then the whole pragma is
38554 considered ill-formed. Thus, no reason to keep
38556 clauses
= error_mark_node
;
38559 else if (type_dependent_expression_p (e
)
38560 || value_dependent_expression_p (e
)
38562 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
38563 && (TREE_CONSTANT (e
)
38567 cp_parser_error (parser
,
38568 "step size must be an integer constant "
38569 "expression or an integer variable");
38572 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38573 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
38574 OMP_CLAUSE_DECL (l
) = decl
;
38575 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
38576 OMP_CLAUSE_CHAIN (l
) = clauses
;
38579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38580 cp_lexer_consume_token (parser
->lexer
);
38581 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38585 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38586 "expected %<,%> or %<)%> after %qE", decl
);
38587 clauses
= error_mark_node
;
38591 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38592 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38596 /* Returns the name of the next clause. If the clause is not
38597 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38598 token is not consumed. Otherwise, the appropriate enum from the
38599 pragma_simd_clause is returned and the token is consumed. */
38601 static pragma_omp_clause
38602 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
38604 pragma_omp_clause clause_type
;
38605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38607 if (token
->keyword
== RID_PRIVATE
)
38608 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
38609 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
38610 return PRAGMA_CILK_CLAUSE_NONE
;
38611 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
38612 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
38613 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
38614 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
38615 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
38616 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
38617 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
38618 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
38619 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
38620 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
38622 return PRAGMA_CILK_CLAUSE_NONE
;
38624 cp_lexer_consume_token (parser
->lexer
);
38625 return clause_type
;
38628 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38631 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
38633 tree clauses
= NULL_TREE
;
38635 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38636 && clauses
!= error_mark_node
)
38638 pragma_omp_clause c_kind
;
38639 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
38640 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
38641 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
38642 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
38643 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
38644 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
38645 /* Use the OpenMP 4.0 equivalent function. */
38646 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
38647 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
38648 /* Use the OpenMP 4.0 equivalent function. */
38649 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
38651 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
38652 /* Use the OMP 4.0 equivalent function. */
38653 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
38655 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
38656 /* Use the OMP 4.0 equivalent function. */
38657 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
38660 clauses
= error_mark_node
;
38661 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
38666 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
38668 if (clauses
== error_mark_node
)
38669 return error_mark_node
;
38671 return finish_omp_clauses (clauses
, C_ORT_CILK
);
38674 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38677 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
38679 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
38681 if (clauses
== error_mark_node
)
38684 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
38686 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38687 "for statement expected");
38691 tree sb
= begin_omp_structured_block ();
38692 int save
= cp_parser_begin_omp_structured_block (parser
);
38693 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
38695 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
38696 cp_parser_end_omp_structured_block (parser
, save
);
38697 add_stmt (finish_omp_structured_block (sb
));
38700 /* Main entry-point for parsing Cilk Plus _Cilk_for
38701 loops. The return value is error_mark_node
38702 when errors happen and CILK_FOR tree on success. */
38705 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
38707 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
38708 gcc_unreachable ();
38710 tree sb
= begin_omp_structured_block ();
38711 int save
= cp_parser_begin_omp_structured_block (parser
);
38713 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
38714 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
38715 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
38716 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
38718 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
38720 cpp_validate_cilk_plus_loop (ret
);
38722 ret
= error_mark_node
;
38724 cp_parser_end_omp_structured_block (parser
, save
);
38725 add_stmt (finish_omp_structured_block (sb
));
38729 /* Create an identifier for a generic parameter type (a synthesized
38730 template parameter implied by `auto' or a concept identifier). */
38732 static GTY(()) int generic_parm_count
;
38734 make_generic_type_name ()
38737 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38738 return get_identifier (buf
);
38741 /* Predicate that behaves as is_auto_or_concept but matches the parent
38742 node of the generic type rather than the generic type itself. This
38743 allows for type transformation in add_implicit_template_parms. */
38746 tree_type_is_auto_or_concept (const_tree t
)
38748 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
38751 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38752 (creating a new template parameter list if necessary). Returns the newly
38753 created template type parm. */
38756 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38758 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38760 /* Before committing to modifying any scope, if we're in an
38761 implicit template scope, and we're trying to synthesize a
38762 constrained parameter, try to find a previous parameter with
38763 the same name. This is the same-type rule for abbreviated
38764 function templates.
38766 NOTE: We can generate implicit parameters when tentatively
38767 parsing a nested name specifier, only to reject that parse
38768 later. However, matching the same template-id as part of a
38769 direct-declarator should generate an identical template
38770 parameter, so this rule will merge them. */
38771 if (parser
->implicit_template_scope
&& constr
)
38773 tree t
= parser
->implicit_template_parms
;
38776 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38778 tree d
= TREE_VALUE (t
);
38779 if (TREE_CODE (d
) == PARM_DECL
)
38780 /* Return the TEMPLATE_PARM_INDEX. */
38781 d
= DECL_INITIAL (d
);
38784 t
= TREE_CHAIN (t
);
38788 /* We are either continuing a function template that already contains implicit
38789 template parameters, creating a new fully-implicit function template, or
38790 extending an existing explicit function template with implicit template
38793 cp_binding_level
*const entry_scope
= current_binding_level
;
38795 bool become_template
= false;
38796 cp_binding_level
*parent_scope
= 0;
38798 if (parser
->implicit_template_scope
)
38800 gcc_assert (parser
->implicit_template_parms
);
38802 current_binding_level
= parser
->implicit_template_scope
;
38806 /* Roll back to the existing template parameter scope (in the case of
38807 extending an explicit function template) or introduce a new template
38808 parameter scope ahead of the function parameter scope (or class scope
38809 in the case of out-of-line member definitions). The function scope is
38810 added back after template parameter synthesis below. */
38812 cp_binding_level
*scope
= entry_scope
;
38814 while (scope
->kind
== sk_function_parms
)
38816 parent_scope
= scope
;
38817 scope
= scope
->level_chain
;
38819 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38821 /* If not defining a class, then any class scope is a scope level in
38822 an out-of-line member definition. In this case simply wind back
38823 beyond the first such scope to inject the template parameter list.
38824 Otherwise wind back to the class being defined. The latter can
38825 occur in class member friend declarations such as:
38831 friend void A::foo (auto);
38834 The template parameter list synthesized for the friend declaration
38835 must be injected in the scope of 'B'. This can also occur in
38836 erroneous cases such as:
38842 void B::foo (auto) {}
38845 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38846 but, nevertheless, the template parameter list synthesized for the
38847 declarator should be injected into the scope of 'A' as if the
38848 ill-formed template was specified explicitly. */
38850 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38852 parent_scope
= scope
;
38853 scope
= scope
->level_chain
;
38857 current_binding_level
= scope
;
38859 if (scope
->kind
!= sk_template_parms
38860 || !function_being_declared_is_template_p (parser
))
38862 /* Introduce a new template parameter list for implicit template
38865 become_template
= true;
38867 parser
->implicit_template_scope
38868 = begin_scope (sk_template_parms
, NULL
);
38870 ++processing_template_decl
;
38872 parser
->fully_implicit_function_template_p
= true;
38873 ++parser
->num_template_parameter_lists
;
38877 /* Synthesize implicit template parameters at the end of the explicit
38878 template parameter list. */
38880 gcc_assert (current_template_parms
);
38882 parser
->implicit_template_scope
= scope
;
38884 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38885 parser
->implicit_template_parms
38886 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38890 /* Synthesize a new template parameter and track the current template
38891 parameter chain with implicit_template_parms. */
38893 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38894 tree synth_id
= make_generic_type_name ();
38895 tree synth_tmpl_parm
;
38896 bool non_type
= false;
38898 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38900 = finish_template_type_parm (class_type_node
, synth_id
);
38901 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38903 = finish_constrained_template_template_parm (proto
, synth_id
);
38906 synth_tmpl_parm
= copy_decl (proto
);
38907 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38911 // Attach the constraint to the parm before processing.
38912 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38913 TREE_TYPE (node
) = constr
;
38915 = process_template_parm (parser
->implicit_template_parms
,
38918 /*non_type=*/non_type
,
38919 /*param_pack=*/false);
38921 // Chain the new parameter to the list of implicit parameters.
38922 if (parser
->implicit_template_parms
)
38923 parser
->implicit_template_parms
38924 = TREE_CHAIN (parser
->implicit_template_parms
);
38926 parser
->implicit_template_parms
= new_parm
;
38928 tree new_decl
= getdecls ();
38930 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38931 new_decl
= DECL_INITIAL (new_decl
);
38933 /* If creating a fully implicit function template, start the new implicit
38934 template parameter list with this synthesized type, otherwise grow the
38935 current template parameter list. */
38937 if (become_template
)
38939 parent_scope
->level_chain
= current_binding_level
;
38941 tree new_parms
= make_tree_vec (1);
38942 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
38943 current_template_parms
= tree_cons (size_int (processing_template_decl
),
38944 new_parms
, current_template_parms
);
38948 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38949 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
38950 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
38951 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
38954 // If the new parameter was constrained, we need to add that to the
38955 // constraints in the template parameter list.
38956 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
38958 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
38959 reqs
= conjoin_constraints (reqs
, req
);
38960 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
38963 current_binding_level
= entry_scope
;
38968 /* Finish the declaration of a fully implicit function template. Such a
38969 template has no explicit template parameter list so has not been through the
38970 normal template head and tail processing. synthesize_implicit_template_parm
38971 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38972 provided if the declaration is a class member such that its template
38973 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38974 form is returned. Otherwise NULL_TREE is returned. */
38977 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
38979 gcc_assert (parser
->fully_implicit_function_template_p
);
38981 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
38982 && DECL_VIRTUAL_P (member_decl_opt
))
38984 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
38985 "implicit templates may not be %<virtual%>");
38986 DECL_VIRTUAL_P (member_decl_opt
) = false;
38989 if (member_decl_opt
)
38990 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
38991 end_template_decl ();
38993 parser
->fully_implicit_function_template_p
= false;
38994 --parser
->num_template_parameter_lists
;
38996 return member_decl_opt
;
38999 #include "gt-cp-parser.h"