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 apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1592 make_call_declarator (cp_declarator
*target
,
1594 cp_cv_quals cv_qualifiers
,
1595 cp_virt_specifiers virt_specifiers
,
1596 cp_ref_qualifier ref_qualifier
,
1598 tree exception_specification
,
1599 tree late_return_type
,
1600 tree requires_clause
)
1602 cp_declarator
*declarator
;
1604 declarator
= make_declarator (cdk_function
);
1605 declarator
->declarator
= target
;
1606 declarator
->u
.function
.parameters
= parms
;
1607 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1608 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1609 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1610 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1611 declarator
->u
.function
.exception_specification
= exception_specification
;
1612 declarator
->u
.function
.late_return_type
= late_return_type
;
1613 declarator
->u
.function
.requires_clause
= requires_clause
;
1616 declarator
->id_loc
= target
->id_loc
;
1617 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1618 target
->parameter_pack_p
= false;
1621 declarator
->parameter_pack_p
= false;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1630 make_array_declarator (cp_declarator
*element
, tree bounds
)
1632 cp_declarator
*declarator
;
1634 declarator
= make_declarator (cdk_array
);
1635 declarator
->declarator
= element
;
1636 declarator
->u
.array
.bounds
= bounds
;
1639 declarator
->id_loc
= element
->id_loc
;
1640 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1641 element
->parameter_pack_p
= false;
1644 declarator
->parameter_pack_p
= false;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1652 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1654 if (declarator
&& declarator
->parameter_pack_p
)
1655 /* We already saw an ellipsis. */
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1663 while (declarator
&& !found
)
1665 switch ((int)declarator
->kind
)
1677 declarator
= declarator
->declarator
;
1685 cp_parameter_declarator
*no_parameters
;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator
*
1691 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1692 cp_declarator
*declarator
,
1693 tree default_argument
,
1694 bool template_parameter_pack_p
= false)
1696 cp_parameter_declarator
*parameter
;
1698 parameter
= ((cp_parameter_declarator
*)
1699 alloc_declarator (sizeof (cp_parameter_declarator
)));
1700 parameter
->next
= NULL
;
1701 if (decl_specifiers
)
1702 parameter
->decl_specifiers
= *decl_specifiers
;
1704 clear_decl_specs (¶meter
->decl_specifiers
);
1705 parameter
->declarator
= declarator
;
1706 parameter
->default_argument
= default_argument
;
1707 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1715 function_declarator_p (const cp_declarator
*declarator
)
1719 if (declarator
->kind
== cdk_function
1720 && declarator
->declarator
->kind
== cdk_id
)
1722 if (declarator
->kind
== cdk_id
1723 || declarator
->kind
== cdk_decomp
1724 || declarator
->kind
== cdk_error
)
1726 declarator
= declarator
->declarator
;
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1792 CP_PARSER_FLAGS_NONE
= 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags
;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT
,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED
,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1832 PREC_LOGICAL_OR_EXPRESSION
,
1833 PREC_LOGICAL_AND_EXPRESSION
,
1834 PREC_INCLUSIVE_OR_EXPRESSION
,
1835 PREC_EXCLUSIVE_OR_EXPRESSION
,
1836 PREC_AND_EXPRESSION
,
1837 PREC_EQUALITY_EXPRESSION
,
1838 PREC_RELATIONAL_EXPRESSION
,
1839 PREC_SHIFT_EXPRESSION
,
1840 PREC_ADDITIVE_EXPRESSION
,
1841 PREC_MULTIPLICATIVE_EXPRESSION
,
1843 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type
;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type
;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec
;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type
;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type
;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec
;
1871 /* Location of the binary operation we are parsing. */
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1883 /* Constructors and destructors. */
1885 static cp_parser_context
*cp_parser_context_new
1886 (cp_parser_context
*);
1888 /* Class variables. */
1890 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1896 static const cp_parser_binary_operations_map_node binops
[] = {
1897 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1898 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1900 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1901 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1902 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1904 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1905 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1907 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1908 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1910 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1912 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1913 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1915 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1916 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1918 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1920 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1922 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1924 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1926 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1932 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context
*
1940 cp_parser_context_new (cp_parser_context
* next
)
1942 cp_parser_context
*context
;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list
!= NULL
)
1947 /* Pull the first entry from the free list. */
1948 context
= cp_parser_context_free_list
;
1949 cp_parser_context_free_list
= context
->next
;
1950 memset (context
, 0, sizeof (*context
));
1953 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1955 /* No errors have occurred yet in this context. */
1956 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context
->object_type
= next
->object_type
;
1964 /* Thread the stack. */
1965 context
->next
= next
;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1983 push_unparsed_function_queues (cp_parser
*parser
)
1985 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1986 vec_safe_push (parser
->unparsed_queues
, e
);
1990 pop_unparsed_function_queues (cp_parser
*parser
)
1992 release_tree_vector (unparsed_funs_with_definitions
);
1993 parser
->unparsed_queues
->pop ();
1998 /* Constructors and destructors. */
2000 static cp_parser
*cp_parser_new
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser
*, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2023 static tree cp_parser_userdef_string_literal
2025 static cp_expr cp_parser_userdef_numeric_literal
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser
*, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser
*, bool, bool, bool, bool);
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_AGGREGATE
:
5125 case RID_IS_BASE_OF
:
5130 case RID_IS_LITERAL_TYPE
:
5132 case RID_IS_POLYMORPHIC
:
5133 case RID_IS_SAME_AS
:
5134 case RID_IS_STD_LAYOUT
:
5135 case RID_IS_TRIVIAL
:
5136 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5137 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5138 case RID_IS_TRIVIALLY_COPYABLE
:
5140 return cp_parser_trait_expr (parser
, token
->keyword
);
5144 return cp_parser_requires_expression (parser
);
5146 /* Objective-C++ expressions. */
5148 case RID_AT_PROTOCOL
:
5149 case RID_AT_SELECTOR
:
5150 return cp_parser_objc_expression (parser
);
5153 if (parser
->in_function_body
5154 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5157 error_at (token
->location
,
5158 "a template declaration cannot appear at block scope");
5159 cp_parser_skip_to_end_of_block_or_statement (parser
);
5160 return error_mark_node
;
5164 cp_parser_error (parser
, "expected primary-expression");
5165 return error_mark_node
;
5168 /* An id-expression can start with either an identifier, a
5169 `::' as the beginning of a qualified-id, or the "operator"
5173 case CPP_TEMPLATE_ID
:
5174 case CPP_NESTED_NAME_SPECIFIER
:
5177 cp_expr id_expression
;
5179 const char *error_msg
;
5182 cp_token
*id_expr_token
;
5184 /* Parse the id-expression. */
5186 = cp_parser_id_expression (parser
,
5187 /*template_keyword_p=*/false,
5188 /*check_dependency_p=*/true,
5190 /*declarator_p=*/false,
5191 /*optional_p=*/false);
5192 if (id_expression
== error_mark_node
)
5193 return error_mark_node
;
5194 id_expr_token
= token
;
5195 token
= cp_lexer_peek_token (parser
->lexer
);
5196 done
= (token
->type
!= CPP_OPEN_SQUARE
5197 && token
->type
!= CPP_OPEN_PAREN
5198 && token
->type
!= CPP_DOT
5199 && token
->type
!= CPP_DEREF
5200 && token
->type
!= CPP_PLUS_PLUS
5201 && token
->type
!= CPP_MINUS_MINUS
);
5202 /* If we have a template-id, then no further lookup is
5203 required. If the template-id was for a template-class, we
5204 will sometimes have a TYPE_DECL at this point. */
5205 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5206 || TREE_CODE (id_expression
) == TYPE_DECL
)
5207 decl
= id_expression
;
5208 /* Look up the name. */
5211 tree ambiguous_decls
;
5213 /* If we already know that this lookup is ambiguous, then
5214 we've already issued an error message; there's no reason
5216 if (id_expr_token
->type
== CPP_NAME
5217 && id_expr_token
->error_reported
)
5219 cp_parser_simulate_error (parser
);
5220 return error_mark_node
;
5223 decl
= cp_parser_lookup_name (parser
, id_expression
,
5226 /*is_namespace=*/false,
5227 /*check_dependency=*/true,
5229 id_expr_token
->location
);
5230 /* If the lookup was ambiguous, an error will already have
5232 if (ambiguous_decls
)
5233 return error_mark_node
;
5235 /* In Objective-C++, we may have an Objective-C 2.0
5236 dot-syntax for classes here. */
5237 if (c_dialect_objc ()
5238 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5239 && TREE_CODE (decl
) == TYPE_DECL
5240 && objc_is_class_name (decl
))
5243 cp_lexer_consume_token (parser
->lexer
);
5244 component
= cp_parser_identifier (parser
);
5245 if (component
== error_mark_node
)
5246 return error_mark_node
;
5248 tree result
= objc_build_class_component_ref (id_expression
,
5250 /* Build a location of the form:
5253 with caret at the start of the component name (at
5254 input_location), ranging from the start of the id_expression
5255 to the end of the component name. */
5256 location_t combined_loc
5257 = make_location (input_location
, id_expression
.get_start (),
5258 get_finish (input_location
));
5259 protected_set_expr_location (result
, combined_loc
);
5263 /* In Objective-C++, an instance variable (ivar) may be preferred
5264 to whatever cp_parser_lookup_name() found.
5265 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5266 rest of c-family, we have to do a little extra work to preserve
5267 any location information in cp_expr "decl". Given that
5268 objc_lookup_ivar is implemented in "c-family" and "objc", we
5269 have a trip through the pure "tree" type, rather than cp_expr.
5270 Naively copying it back to "decl" would implicitly give the
5271 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5272 store an EXPR_LOCATION. Hence we only update "decl" (and
5273 hence its location_t) if we get back a different tree node. */
5274 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5276 if (decl_tree
!= decl
.get_value ())
5277 decl
= cp_expr (decl_tree
);
5279 /* If name lookup gives us a SCOPE_REF, then the
5280 qualifying scope was dependent. */
5281 if (TREE_CODE (decl
) == SCOPE_REF
)
5283 /* At this point, we do not know if DECL is a valid
5284 integral constant expression. We assume that it is
5285 in fact such an expression, so that code like:
5287 template <int N> struct A {
5291 is accepted. At template-instantiation time, we
5292 will check that B<N>::i is actually a constant. */
5295 /* Check to see if DECL is a local variable in a context
5296 where that is forbidden. */
5297 if (parser
->local_variables_forbidden_p
5298 && local_variable_p (decl
))
5300 /* It might be that we only found DECL because we are
5301 trying to be generous with pre-ISO scoping rules.
5302 For example, consider:
5306 for (int i = 0; i < 10; ++i) {}
5307 extern void f(int j = i);
5310 Here, name look up will originally find the out
5311 of scope `i'. We need to issue a warning message,
5312 but then use the global `i'. */
5313 decl
= check_for_out_of_scope_variable (decl
);
5314 if (local_variable_p (decl
))
5316 error_at (id_expr_token
->location
,
5317 "local variable %qD may not appear in this context",
5319 return error_mark_node
;
5324 decl
= (finish_id_expression
5325 (id_expression
, decl
, parser
->scope
,
5327 parser
->integral_constant_expression_p
,
5328 parser
->allow_non_integral_constant_expression_p
,
5329 &parser
->non_integral_constant_expression_p
,
5330 template_p
, done
, address_p
,
5333 id_expression
.get_location ()));
5335 cp_parser_error (parser
, error_msg
);
5336 decl
.set_location (id_expr_token
->location
);
5340 /* Anything else is an error. */
5342 cp_parser_error (parser
, "expected primary-expression");
5343 return error_mark_node
;
5347 static inline cp_expr
5348 cp_parser_primary_expression (cp_parser
*parser
,
5351 bool template_arg_p
,
5354 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5355 /*decltype*/false, idk
);
5358 /* Parse an id-expression.
5365 :: [opt] nested-name-specifier template [opt] unqualified-id
5367 :: operator-function-id
5370 Return a representation of the unqualified portion of the
5371 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5372 a `::' or nested-name-specifier.
5374 Often, if the id-expression was a qualified-id, the caller will
5375 want to make a SCOPE_REF to represent the qualified-id. This
5376 function does not do this in order to avoid wastefully creating
5377 SCOPE_REFs when they are not required.
5379 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5382 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5383 uninstantiated templates.
5385 If *TEMPLATE_P is non-NULL, it is set to true iff the
5386 `template' keyword is used to explicitly indicate that the entity
5387 named is a template.
5389 If DECLARATOR_P is true, the id-expression is appearing as part of
5390 a declarator, rather than as part of an expression. */
5393 cp_parser_id_expression (cp_parser
*parser
,
5394 bool template_keyword_p
,
5395 bool check_dependency_p
,
5400 bool global_scope_p
;
5401 bool nested_name_specifier_p
;
5403 /* Assume the `template' keyword was not used. */
5405 *template_p
= template_keyword_p
;
5407 /* Look for the optional `::' operator. */
5409 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5411 /* Look for the optional nested-name-specifier. */
5412 nested_name_specifier_p
5413 = (cp_parser_nested_name_specifier_opt (parser
,
5414 /*typename_keyword_p=*/false,
5419 /* If there is a nested-name-specifier, then we are looking at
5420 the first qualified-id production. */
5421 if (nested_name_specifier_p
)
5424 tree saved_object_scope
;
5425 tree saved_qualifying_scope
;
5426 cp_expr unqualified_id
;
5429 /* See if the next token is the `template' keyword. */
5431 template_p
= &is_template
;
5432 *template_p
= cp_parser_optional_template_keyword (parser
);
5433 /* Name lookup we do during the processing of the
5434 unqualified-id might obliterate SCOPE. */
5435 saved_scope
= parser
->scope
;
5436 saved_object_scope
= parser
->object_scope
;
5437 saved_qualifying_scope
= parser
->qualifying_scope
;
5438 /* Process the final unqualified-id. */
5439 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5442 /*optional_p=*/false);
5443 /* Restore the SAVED_SCOPE for our caller. */
5444 parser
->scope
= saved_scope
;
5445 parser
->object_scope
= saved_object_scope
;
5446 parser
->qualifying_scope
= saved_qualifying_scope
;
5448 return unqualified_id
;
5450 /* Otherwise, if we are in global scope, then we are looking at one
5451 of the other qualified-id productions. */
5452 else if (global_scope_p
)
5457 /* Peek at the next token. */
5458 token
= cp_lexer_peek_token (parser
->lexer
);
5460 /* If it's an identifier, and the next token is not a "<", then
5461 we can avoid the template-id case. This is an optimization
5462 for this common case. */
5463 if (token
->type
== CPP_NAME
5464 && !cp_parser_nth_token_starts_template_argument_list_p
5466 return cp_parser_identifier (parser
);
5468 cp_parser_parse_tentatively (parser
);
5469 /* Try a template-id. */
5470 id
= cp_parser_template_id (parser
,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5475 /* If that worked, we're done. */
5476 if (cp_parser_parse_definitely (parser
))
5479 /* Peek at the next token. (Changes in the token buffer may
5480 have invalidated the pointer obtained above.) */
5481 token
= cp_lexer_peek_token (parser
->lexer
);
5483 switch (token
->type
)
5486 return cp_parser_identifier (parser
);
5489 if (token
->keyword
== RID_OPERATOR
)
5490 return cp_parser_operator_function_id (parser
);
5494 cp_parser_error (parser
, "expected id-expression");
5495 return error_mark_node
;
5499 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5500 /*check_dependency_p=*/true,
5505 /* Parse an unqualified-id.
5509 operator-function-id
5510 conversion-function-id
5514 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5515 keyword, in a construct like `A::template ...'.
5517 Returns a representation of unqualified-id. For the `identifier'
5518 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5519 production a BIT_NOT_EXPR is returned; the operand of the
5520 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5521 other productions, see the documentation accompanying the
5522 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5523 names are looked up in uninstantiated templates. If DECLARATOR_P
5524 is true, the unqualified-id is appearing as part of a declarator,
5525 rather than as part of an expression. */
5528 cp_parser_unqualified_id (cp_parser
* parser
,
5529 bool template_keyword_p
,
5530 bool check_dependency_p
,
5536 /* Peek at the next token. */
5537 token
= cp_lexer_peek_token (parser
->lexer
);
5539 switch ((int) token
->type
)
5545 /* We don't know yet whether or not this will be a
5547 cp_parser_parse_tentatively (parser
);
5548 /* Try a template-id. */
5549 id
= cp_parser_template_id (parser
, template_keyword_p
,
5553 /* If it worked, we're done. */
5554 if (cp_parser_parse_definitely (parser
))
5556 /* Otherwise, it's an ordinary identifier. */
5557 return cp_parser_identifier (parser
);
5560 case CPP_TEMPLATE_ID
:
5561 return cp_parser_template_id (parser
, template_keyword_p
,
5569 tree qualifying_scope
;
5574 /* Consume the `~' token. */
5575 cp_lexer_consume_token (parser
->lexer
);
5576 /* Parse the class-name. The standard, as written, seems to
5579 template <typename T> struct S { ~S (); };
5580 template <typename T> S<T>::~S() {}
5582 is invalid, since `~' must be followed by a class-name, but
5583 `S<T>' is dependent, and so not known to be a class.
5584 That's not right; we need to look in uninstantiated
5585 templates. A further complication arises from:
5587 template <typename T> void f(T t) {
5591 Here, it is not possible to look up `T' in the scope of `T'
5592 itself. We must look in both the current scope, and the
5593 scope of the containing complete expression.
5595 Yet another issue is:
5604 The standard does not seem to say that the `S' in `~S'
5605 should refer to the type `S' and not the data member
5608 /* DR 244 says that we look up the name after the "~" in the
5609 same scope as we looked up the qualifying name. That idea
5610 isn't fully worked out; it's more complicated than that. */
5611 scope
= parser
->scope
;
5612 object_scope
= parser
->object_scope
;
5613 qualifying_scope
= parser
->qualifying_scope
;
5615 /* Check for invalid scopes. */
5616 if (scope
== error_mark_node
)
5618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5619 cp_lexer_consume_token (parser
->lexer
);
5620 return error_mark_node
;
5622 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5624 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5625 error_at (token
->location
,
5626 "scope %qT before %<~%> is not a class-name",
5628 cp_parser_simulate_error (parser
);
5629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5630 cp_lexer_consume_token (parser
->lexer
);
5631 return error_mark_node
;
5633 gcc_assert (!scope
|| TYPE_P (scope
));
5635 /* If the name is of the form "X::~X" it's OK even if X is a
5637 token
= cp_lexer_peek_token (parser
->lexer
);
5639 && token
->type
== CPP_NAME
5640 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5642 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5643 || (CLASS_TYPE_P (scope
)
5644 && constructor_name_p (token
->u
.value
, scope
))))
5646 cp_lexer_consume_token (parser
->lexer
);
5647 return build_nt (BIT_NOT_EXPR
, scope
);
5650 /* ~auto means the destructor of whatever the object is. */
5651 if (cp_parser_is_keyword (token
, RID_AUTO
))
5653 if (cxx_dialect
< cxx14
)
5654 pedwarn (input_location
, 0,
5655 "%<~auto%> only available with "
5656 "-std=c++14 or -std=gnu++14");
5657 cp_lexer_consume_token (parser
->lexer
);
5658 return build_nt (BIT_NOT_EXPR
, make_auto ());
5661 /* If there was an explicit qualification (S::~T), first look
5662 in the scope given by the qualification (i.e., S).
5664 Note: in the calls to cp_parser_class_name below we pass
5665 typename_type so that lookup finds the injected-class-name
5666 rather than the constructor. */
5668 type_decl
= NULL_TREE
;
5671 cp_parser_parse_tentatively (parser
);
5672 type_decl
= cp_parser_class_name (parser
,
5673 /*typename_keyword_p=*/false,
5674 /*template_keyword_p=*/false,
5676 /*check_dependency=*/false,
5677 /*class_head_p=*/false,
5679 if (cp_parser_parse_definitely (parser
))
5682 /* In "N::S::~S", look in "N" as well. */
5683 if (!done
&& scope
&& qualifying_scope
)
5685 cp_parser_parse_tentatively (parser
);
5686 parser
->scope
= qualifying_scope
;
5687 parser
->object_scope
= NULL_TREE
;
5688 parser
->qualifying_scope
= NULL_TREE
;
5690 = cp_parser_class_name (parser
,
5691 /*typename_keyword_p=*/false,
5692 /*template_keyword_p=*/false,
5694 /*check_dependency=*/false,
5695 /*class_head_p=*/false,
5697 if (cp_parser_parse_definitely (parser
))
5700 /* In "p->S::~T", look in the scope given by "*p" as well. */
5701 else if (!done
&& object_scope
)
5703 cp_parser_parse_tentatively (parser
);
5704 parser
->scope
= object_scope
;
5705 parser
->object_scope
= NULL_TREE
;
5706 parser
->qualifying_scope
= NULL_TREE
;
5708 = cp_parser_class_name (parser
,
5709 /*typename_keyword_p=*/false,
5710 /*template_keyword_p=*/false,
5712 /*check_dependency=*/false,
5713 /*class_head_p=*/false,
5715 if (cp_parser_parse_definitely (parser
))
5718 /* Look in the surrounding context. */
5721 parser
->scope
= NULL_TREE
;
5722 parser
->object_scope
= NULL_TREE
;
5723 parser
->qualifying_scope
= NULL_TREE
;
5724 if (processing_template_decl
)
5725 cp_parser_parse_tentatively (parser
);
5727 = cp_parser_class_name (parser
,
5728 /*typename_keyword_p=*/false,
5729 /*template_keyword_p=*/false,
5731 /*check_dependency=*/false,
5732 /*class_head_p=*/false,
5734 if (processing_template_decl
5735 && ! cp_parser_parse_definitely (parser
))
5737 /* We couldn't find a type with this name. If we're parsing
5738 tentatively, fail and try something else. */
5739 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5741 cp_parser_simulate_error (parser
);
5742 return error_mark_node
;
5744 /* Otherwise, accept it and check for a match at instantiation
5746 type_decl
= cp_parser_identifier (parser
);
5747 if (type_decl
!= error_mark_node
)
5748 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5752 /* If an error occurred, assume that the name of the
5753 destructor is the same as the name of the qualifying
5754 class. That allows us to keep parsing after running
5755 into ill-formed destructor names. */
5756 if (type_decl
== error_mark_node
&& scope
)
5757 return build_nt (BIT_NOT_EXPR
, scope
);
5758 else if (type_decl
== error_mark_node
)
5759 return error_mark_node
;
5761 /* Check that destructor name and scope match. */
5762 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5764 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5765 error_at (token
->location
,
5766 "declaration of %<~%T%> as member of %qT",
5768 cp_parser_simulate_error (parser
);
5769 return error_mark_node
;
5774 A typedef-name that names a class shall not be used as the
5775 identifier in the declarator for a destructor declaration. */
5777 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5778 && !DECL_SELF_REFERENCE_P (type_decl
)
5779 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5780 error_at (token
->location
,
5781 "typedef-name %qD used as destructor declarator",
5784 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5788 if (token
->keyword
== RID_OPERATOR
)
5792 /* This could be a template-id, so we try that first. */
5793 cp_parser_parse_tentatively (parser
);
5794 /* Try a template-id. */
5795 id
= cp_parser_template_id (parser
, template_keyword_p
,
5796 /*check_dependency_p=*/true,
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser
))
5802 /* We still don't know whether we're looking at an
5803 operator-function-id or a conversion-function-id. */
5804 cp_parser_parse_tentatively (parser
);
5805 /* Try an operator-function-id. */
5806 id
= cp_parser_operator_function_id (parser
);
5807 /* If that didn't work, try a conversion-function-id. */
5808 if (!cp_parser_parse_definitely (parser
))
5809 id
= cp_parser_conversion_function_id (parser
);
5810 else if (UDLIT_OPER_P (id
))
5813 const char *name
= UDLIT_OP_SUFFIX (id
);
5814 if (name
[0] != '_' && !in_system_header_at (input_location
)
5816 warning (OPT_Wliteral_suffix
,
5817 "literal operator suffixes not preceded by %<_%>"
5818 " are reserved for future standardization");
5828 cp_parser_error (parser
, "expected unqualified-id");
5829 return error_mark_node
;
5833 /* Parse an (optional) nested-name-specifier.
5835 nested-name-specifier: [C++98]
5836 class-or-namespace-name :: nested-name-specifier [opt]
5837 class-or-namespace-name :: template nested-name-specifier [opt]
5839 nested-name-specifier: [C++0x]
5842 nested-name-specifier identifier ::
5843 nested-name-specifier template [opt] simple-template-id ::
5845 PARSER->SCOPE should be set appropriately before this function is
5846 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5847 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5850 Sets PARSER->SCOPE to the class (TYPE) or namespace
5851 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5852 it unchanged if there is no nested-name-specifier. Returns the new
5853 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5855 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5856 part of a declaration and/or decl-specifier. */
5859 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5860 bool typename_keyword_p
,
5861 bool check_dependency_p
,
5863 bool is_declaration
)
5865 bool success
= false;
5866 cp_token_position start
= 0;
5869 /* Remember where the nested-name-specifier starts. */
5870 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5872 start
= cp_lexer_token_position (parser
->lexer
, false);
5873 push_deferring_access_checks (dk_deferred
);
5880 tree saved_qualifying_scope
;
5881 bool template_keyword_p
;
5883 /* Spot cases that cannot be the beginning of a
5884 nested-name-specifier. */
5885 token
= cp_lexer_peek_token (parser
->lexer
);
5887 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5888 the already parsed nested-name-specifier. */
5889 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5891 /* Grab the nested-name-specifier and continue the loop. */
5892 cp_parser_pre_parsed_nested_name_specifier (parser
);
5893 /* If we originally encountered this nested-name-specifier
5894 with IS_DECLARATION set to false, we will not have
5895 resolved TYPENAME_TYPEs, so we must do so here. */
5897 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5899 new_scope
= resolve_typename_type (parser
->scope
,
5900 /*only_current_p=*/false);
5901 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5902 parser
->scope
= new_scope
;
5908 /* Spot cases that cannot be the beginning of a
5909 nested-name-specifier. On the second and subsequent times
5910 through the loop, we look for the `template' keyword. */
5911 if (success
&& token
->keyword
== RID_TEMPLATE
)
5913 /* A template-id can start a nested-name-specifier. */
5914 else if (token
->type
== CPP_TEMPLATE_ID
)
5916 /* DR 743: decltype can be used in a nested-name-specifier. */
5917 else if (token_is_decltype (token
))
5921 /* If the next token is not an identifier, then it is
5922 definitely not a type-name or namespace-name. */
5923 if (token
->type
!= CPP_NAME
)
5925 /* If the following token is neither a `<' (to begin a
5926 template-id), nor a `::', then we are not looking at a
5927 nested-name-specifier. */
5928 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5930 if (token
->type
== CPP_COLON
5931 && parser
->colon_corrects_to_scope_p
5932 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5934 error_at (token
->location
,
5935 "found %<:%> in nested-name-specifier, expected %<::%>");
5936 token
->type
= CPP_SCOPE
;
5939 if (token
->type
!= CPP_SCOPE
5940 && !cp_parser_nth_token_starts_template_argument_list_p
5945 /* The nested-name-specifier is optional, so we parse
5947 cp_parser_parse_tentatively (parser
);
5949 /* Look for the optional `template' keyword, if this isn't the
5950 first time through the loop. */
5952 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5954 template_keyword_p
= false;
5956 /* Save the old scope since the name lookup we are about to do
5957 might destroy it. */
5958 old_scope
= parser
->scope
;
5959 saved_qualifying_scope
= parser
->qualifying_scope
;
5960 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5961 look up names in "X<T>::I" in order to determine that "Y" is
5962 a template. So, if we have a typename at this point, we make
5963 an effort to look through it. */
5965 && !typename_keyword_p
5967 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5968 parser
->scope
= resolve_typename_type (parser
->scope
,
5969 /*only_current_p=*/false);
5970 /* Parse the qualifying entity. */
5972 = cp_parser_qualifying_entity (parser
,
5978 /* Look for the `::' token. */
5979 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5981 /* If we found what we wanted, we keep going; otherwise, we're
5983 if (!cp_parser_parse_definitely (parser
))
5985 bool error_p
= false;
5987 /* Restore the OLD_SCOPE since it was valid before the
5988 failed attempt at finding the last
5989 class-or-namespace-name. */
5990 parser
->scope
= old_scope
;
5991 parser
->qualifying_scope
= saved_qualifying_scope
;
5993 /* If the next token is a decltype, and the one after that is a
5994 `::', then the decltype has failed to resolve to a class or
5995 enumeration type. Give this error even when parsing
5996 tentatively since it can't possibly be valid--and we're going
5997 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5998 won't get another chance.*/
5999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6000 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6003 token
= cp_lexer_consume_token (parser
->lexer
);
6004 error_at (token
->location
, "decltype evaluates to %qT, "
6005 "which is not a class or enumeration type",
6006 token
->u
.tree_check_value
->value
);
6007 parser
->scope
= error_mark_node
;
6011 cp_lexer_consume_token (parser
->lexer
);
6014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6015 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6017 /* If we have a non-type template-id followed by ::, it can't
6018 possibly be valid. */
6019 token
= cp_lexer_peek_token (parser
->lexer
);
6020 tree tid
= token
->u
.tree_check_value
->value
;
6021 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6022 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6024 tree tmpl
= NULL_TREE
;
6025 if (is_overloaded_fn (tid
))
6027 tree fns
= get_fns (tid
);
6028 if (!OVL_CHAIN (fns
))
6029 tmpl
= OVL_CURRENT (fns
);
6030 error_at (token
->location
, "function template-id %qD "
6031 "in nested-name-specifier", tid
);
6035 /* Variable template. */
6036 tmpl
= TREE_OPERAND (tid
, 0);
6037 gcc_assert (variable_template_p (tmpl
));
6038 error_at (token
->location
, "variable template-id %qD "
6039 "in nested-name-specifier", tid
);
6042 inform (DECL_SOURCE_LOCATION (tmpl
),
6043 "%qD declared here", tmpl
);
6045 parser
->scope
= error_mark_node
;
6049 cp_lexer_consume_token (parser
->lexer
);
6050 cp_lexer_consume_token (parser
->lexer
);
6054 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6056 /* If the next token is an identifier, and the one after
6057 that is a `::', then any valid interpretation would have
6058 found a class-or-namespace-name. */
6059 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6060 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6062 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6065 token
= cp_lexer_consume_token (parser
->lexer
);
6068 if (!token
->error_reported
)
6071 tree ambiguous_decls
;
6073 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6075 /*is_template=*/false,
6076 /*is_namespace=*/false,
6077 /*check_dependency=*/true,
6080 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6081 error_at (token
->location
,
6082 "%qD used without template parameters",
6084 else if (ambiguous_decls
)
6086 // cp_parser_lookup_name has the same diagnostic,
6087 // thus make sure to emit it at most once.
6088 if (cp_parser_uncommitted_to_tentative_parse_p
6091 error_at (token
->location
,
6092 "reference to %qD is ambiguous",
6094 print_candidates (ambiguous_decls
);
6096 decl
= error_mark_node
;
6100 if (cxx_dialect
!= cxx98
)
6101 cp_parser_name_lookup_error
6102 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6105 cp_parser_name_lookup_error
6106 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6110 parser
->scope
= error_mark_node
;
6112 /* Treat this as a successful nested-name-specifier
6117 If the name found is not a class-name (clause
6118 _class_) or namespace-name (_namespace.def_), the
6119 program is ill-formed. */
6122 cp_lexer_consume_token (parser
->lexer
);
6126 /* We've found one valid nested-name-specifier. */
6128 /* Name lookup always gives us a DECL. */
6129 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6130 new_scope
= TREE_TYPE (new_scope
);
6131 /* Uses of "template" must be followed by actual templates. */
6132 if (template_keyword_p
6133 && !(CLASS_TYPE_P (new_scope
)
6134 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6135 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6136 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6137 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6138 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6139 == TEMPLATE_ID_EXPR
)))
6140 permerror (input_location
, TYPE_P (new_scope
)
6141 ? G_("%qT is not a template")
6142 : G_("%qD is not a template"),
6144 /* If it is a class scope, try to complete it; we are about to
6145 be looking up names inside the class. */
6146 if (TYPE_P (new_scope
)
6147 /* Since checking types for dependency can be expensive,
6148 avoid doing it if the type is already complete. */
6149 && !COMPLETE_TYPE_P (new_scope
)
6150 /* Do not try to complete dependent types. */
6151 && !dependent_type_p (new_scope
))
6153 new_scope
= complete_type (new_scope
);
6154 /* If it is a typedef to current class, use the current
6155 class instead, as the typedef won't have any names inside
6157 if (!COMPLETE_TYPE_P (new_scope
)
6158 && currently_open_class (new_scope
))
6159 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6161 /* Make sure we look in the right scope the next time through
6163 parser
->scope
= new_scope
;
6166 /* If parsing tentatively, replace the sequence of tokens that makes
6167 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6168 token. That way, should we re-parse the token stream, we will
6169 not have to repeat the effort required to do the parse, nor will
6170 we issue duplicate error messages. */
6171 if (success
&& start
)
6175 token
= cp_lexer_token_at (parser
->lexer
, start
);
6176 /* Reset the contents of the START token. */
6177 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6178 /* Retrieve any deferred checks. Do not pop this access checks yet
6179 so the memory will not be reclaimed during token replacing below. */
6180 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6181 token
->u
.tree_check_value
->value
= parser
->scope
;
6182 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6183 token
->u
.tree_check_value
->qualifying_scope
=
6184 parser
->qualifying_scope
;
6185 token
->keyword
= RID_MAX
;
6187 /* Purge all subsequent tokens. */
6188 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6192 pop_to_parent_deferring_access_checks ();
6194 return success
? parser
->scope
: NULL_TREE
;
6197 /* Parse a nested-name-specifier. See
6198 cp_parser_nested_name_specifier_opt for details. This function
6199 behaves identically, except that it will an issue an error if no
6200 nested-name-specifier is present. */
6203 cp_parser_nested_name_specifier (cp_parser
*parser
,
6204 bool typename_keyword_p
,
6205 bool check_dependency_p
,
6207 bool is_declaration
)
6211 /* Look for the nested-name-specifier. */
6212 scope
= cp_parser_nested_name_specifier_opt (parser
,
6217 /* If it was not present, issue an error message. */
6220 cp_parser_error (parser
, "expected nested-name-specifier");
6221 parser
->scope
= NULL_TREE
;
6227 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6228 this is either a class-name or a namespace-name (which corresponds
6229 to the class-or-namespace-name production in the grammar). For
6230 C++0x, it can also be a type-name that refers to an enumeration
6231 type or a simple-template-id.
6233 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6234 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6235 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6236 TYPE_P is TRUE iff the next name should be taken as a class-name,
6237 even the same name is declared to be another entity in the same
6240 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6241 specified by the class-or-namespace-name. If neither is found the
6242 ERROR_MARK_NODE is returned. */
6245 cp_parser_qualifying_entity (cp_parser
*parser
,
6246 bool typename_keyword_p
,
6247 bool template_keyword_p
,
6248 bool check_dependency_p
,
6250 bool is_declaration
)
6253 tree saved_qualifying_scope
;
6254 tree saved_object_scope
;
6257 bool successful_parse_p
;
6259 /* DR 743: decltype can appear in a nested-name-specifier. */
6260 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6262 scope
= cp_parser_decltype (parser
);
6263 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6264 && !MAYBE_CLASS_TYPE_P (scope
))
6266 cp_parser_simulate_error (parser
);
6267 return error_mark_node
;
6269 if (TYPE_NAME (scope
))
6270 scope
= TYPE_NAME (scope
);
6274 /* Before we try to parse the class-name, we must save away the
6275 current PARSER->SCOPE since cp_parser_class_name will destroy
6277 saved_scope
= parser
->scope
;
6278 saved_qualifying_scope
= parser
->qualifying_scope
;
6279 saved_object_scope
= parser
->object_scope
;
6280 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6281 there is no need to look for a namespace-name. */
6282 only_class_p
= template_keyword_p
6283 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6285 cp_parser_parse_tentatively (parser
);
6286 scope
= cp_parser_class_name (parser
,
6289 type_p
? class_type
: none_type
,
6291 /*class_head_p=*/false,
6293 /*enum_ok=*/cxx_dialect
> cxx98
);
6294 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6295 /* If that didn't work, try for a namespace-name. */
6296 if (!only_class_p
&& !successful_parse_p
)
6298 /* Restore the saved scope. */
6299 parser
->scope
= saved_scope
;
6300 parser
->qualifying_scope
= saved_qualifying_scope
;
6301 parser
->object_scope
= saved_object_scope
;
6302 /* If we are not looking at an identifier followed by the scope
6303 resolution operator, then this is not part of a
6304 nested-name-specifier. (Note that this function is only used
6305 to parse the components of a nested-name-specifier.) */
6306 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6307 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6308 return error_mark_node
;
6309 scope
= cp_parser_namespace_name (parser
);
6315 /* Return true if we are looking at a compound-literal, false otherwise. */
6318 cp_parser_compound_literal_p (cp_parser
*parser
)
6320 /* Consume the `('. */
6321 cp_lexer_consume_token (parser
->lexer
);
6323 cp_lexer_save_tokens (parser
->lexer
);
6325 /* Skip tokens until the next token is a closing parenthesis.
6326 If we find the closing `)', and the next token is a `{', then
6327 we are looking at a compound-literal. */
6328 bool compound_literal_p
6329 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6330 /*consume_paren=*/true)
6331 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6333 /* Roll back the tokens we skipped. */
6334 cp_lexer_rollback_tokens (parser
->lexer
);
6336 return compound_literal_p
;
6339 /* Parse a postfix-expression.
6343 postfix-expression [ expression ]
6344 postfix-expression ( expression-list [opt] )
6345 simple-type-specifier ( expression-list [opt] )
6346 typename :: [opt] nested-name-specifier identifier
6347 ( expression-list [opt] )
6348 typename :: [opt] nested-name-specifier template [opt] template-id
6349 ( expression-list [opt] )
6350 postfix-expression . template [opt] id-expression
6351 postfix-expression -> template [opt] id-expression
6352 postfix-expression . pseudo-destructor-name
6353 postfix-expression -> pseudo-destructor-name
6354 postfix-expression ++
6355 postfix-expression --
6356 dynamic_cast < type-id > ( expression )
6357 static_cast < type-id > ( expression )
6358 reinterpret_cast < type-id > ( expression )
6359 const_cast < type-id > ( expression )
6360 typeid ( expression )
6366 ( type-id ) { initializer-list , [opt] }
6368 This extension is a GNU version of the C99 compound-literal
6369 construct. (The C99 grammar uses `type-name' instead of `type-id',
6370 but they are essentially the same concept.)
6372 If ADDRESS_P is true, the postfix expression is the operand of the
6373 `&' operator. CAST_P is true if this expression is the target of a
6376 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6377 class member access expressions [expr.ref].
6379 Returns a representation of the expression. */
6382 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6383 bool member_access_only_p
, bool decltype_p
,
6384 cp_id_kind
* pidk_return
)
6389 cp_id_kind idk
= CP_ID_KIND_NONE
;
6390 cp_expr postfix_expression
= NULL_TREE
;
6391 bool is_member_access
= false;
6392 int saved_in_statement
= -1;
6394 /* Peek at the next token. */
6395 token
= cp_lexer_peek_token (parser
->lexer
);
6396 loc
= token
->location
;
6397 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6399 /* Some of the productions are determined by keywords. */
6400 keyword
= token
->keyword
;
6410 const char *saved_message
;
6411 bool saved_in_type_id_in_expr_p
;
6413 /* All of these can be handled in the same way from the point
6414 of view of parsing. Begin by consuming the token
6415 identifying the cast. */
6416 cp_lexer_consume_token (parser
->lexer
);
6418 /* New types cannot be defined in the cast. */
6419 saved_message
= parser
->type_definition_forbidden_message
;
6420 parser
->type_definition_forbidden_message
6421 = G_("types may not be defined in casts");
6423 /* Look for the opening `<'. */
6424 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6425 /* Parse the type to which we are casting. */
6426 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6427 parser
->in_type_id_in_expr_p
= true;
6428 type
= cp_parser_type_id (parser
);
6429 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6430 /* Look for the closing `>'. */
6431 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6432 /* Restore the old message. */
6433 parser
->type_definition_forbidden_message
= saved_message
;
6435 bool saved_greater_than_is_operator_p
6436 = parser
->greater_than_is_operator_p
;
6437 parser
->greater_than_is_operator_p
= true;
6439 /* And the expression which is being cast. */
6440 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6441 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6442 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6444 location_t end_loc
= close_paren
?
6445 close_paren
->location
: UNKNOWN_LOCATION
;
6447 parser
->greater_than_is_operator_p
6448 = saved_greater_than_is_operator_p
;
6450 /* Only type conversions to integral or enumeration types
6451 can be used in constant-expressions. */
6452 if (!cast_valid_in_integral_constant_expression_p (type
)
6453 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6455 postfix_expression
= error_mark_node
;
6463 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6467 = build_static_cast (type
, expression
, tf_warning_or_error
);
6471 = build_reinterpret_cast (type
, expression
,
6472 tf_warning_or_error
);
6476 = build_const_cast (type
, expression
, tf_warning_or_error
);
6482 /* Construct a location e.g. :
6483 reinterpret_cast <int *> (expr)
6484 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6485 ranging from the start of the "*_cast" token to the final closing
6486 paren, with the caret at the start. */
6487 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6488 postfix_expression
.set_location (cp_cast_loc
);
6495 const char *saved_message
;
6496 bool saved_in_type_id_in_expr_p
;
6498 /* Consume the `typeid' token. */
6499 cp_lexer_consume_token (parser
->lexer
);
6500 /* Look for the `(' token. */
6501 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6502 /* Types cannot be defined in a `typeid' expression. */
6503 saved_message
= parser
->type_definition_forbidden_message
;
6504 parser
->type_definition_forbidden_message
6505 = G_("types may not be defined in a %<typeid%> expression");
6506 /* We can't be sure yet whether we're looking at a type-id or an
6508 cp_parser_parse_tentatively (parser
);
6509 /* Try a type-id first. */
6510 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6511 parser
->in_type_id_in_expr_p
= true;
6512 type
= cp_parser_type_id (parser
);
6513 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6514 /* Look for the `)' token. Otherwise, we can't be sure that
6515 we're not looking at an expression: consider `typeid (int
6516 (3))', for example. */
6517 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6518 /* If all went well, simply lookup the type-id. */
6519 if (cp_parser_parse_definitely (parser
))
6520 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6521 /* Otherwise, fall back to the expression variant. */
6526 /* Look for an expression. */
6527 expression
= cp_parser_expression (parser
, & idk
);
6528 /* Compute its typeid. */
6529 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6530 /* Look for the `)' token. */
6531 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6533 /* Restore the saved message. */
6534 parser
->type_definition_forbidden_message
= saved_message
;
6535 /* `typeid' may not appear in an integral constant expression. */
6536 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6537 postfix_expression
= error_mark_node
;
6544 /* The syntax permitted here is the same permitted for an
6545 elaborated-type-specifier. */
6546 ++parser
->prevent_constrained_type_specifiers
;
6547 type
= cp_parser_elaborated_type_specifier (parser
,
6548 /*is_friend=*/false,
6549 /*is_declaration=*/false);
6550 --parser
->prevent_constrained_type_specifiers
;
6551 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6555 case RID_CILK_SPAWN
:
6557 location_t cilk_spawn_loc
6558 = cp_lexer_peek_token (parser
->lexer
)->location
;
6559 cp_lexer_consume_token (parser
->lexer
);
6560 token
= cp_lexer_peek_token (parser
->lexer
);
6561 if (token
->type
== CPP_SEMICOLON
)
6563 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6565 postfix_expression
= error_mark_node
;
6568 else if (!current_function_decl
)
6570 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6571 "inside a function");
6572 postfix_expression
= error_mark_node
;
6577 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6578 saved_in_statement
= parser
->in_statement
;
6579 parser
->in_statement
|= IN_CILK_SPAWN
;
6581 cfun
->calls_cilk_spawn
= 1;
6582 postfix_expression
=
6583 cp_parser_postfix_expression (parser
, false, false,
6584 false, false, &idk
);
6587 error_at (token
->location
, "-fcilkplus must be enabled to use"
6588 " %<_Cilk_spawn%>");
6589 cfun
->calls_cilk_spawn
= 0;
6591 else if (saved_in_statement
& IN_CILK_SPAWN
)
6593 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6594 "are not permitted");
6595 postfix_expression
= error_mark_node
;
6596 cfun
->calls_cilk_spawn
= 0;
6600 location_t loc
= postfix_expression
.get_location ();
6601 postfix_expression
= build_cilk_spawn (token
->location
,
6602 postfix_expression
);
6603 /* Build a location of the form:
6606 with caret at the expr, ranging from the start of the
6607 _Cilk_spawn token to the end of the expression. */
6608 location_t combined_loc
=
6609 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6610 postfix_expression
.set_location (combined_loc
);
6611 if (postfix_expression
!= error_mark_node
)
6612 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6613 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6619 case RID_BUILTIN_SHUFFLE
:
6620 case RID_BUILTIN_LAUNDER
:
6622 vec
<tree
, va_gc
> *vec
;
6626 cp_lexer_consume_token (parser
->lexer
);
6627 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6628 /*cast_p=*/false, /*allow_expansion_p=*/true,
6629 /*non_constant_p=*/NULL
);
6632 postfix_expression
= error_mark_node
;
6636 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6642 if (vec
->length () == 1)
6644 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6647 error_at (loc
, "wrong number of arguments to "
6648 "%<__builtin_addressof%>");
6649 postfix_expression
= error_mark_node
;
6653 case RID_BUILTIN_LAUNDER
:
6654 if (vec
->length () == 1)
6655 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6656 tf_warning_or_error
);
6659 error_at (loc
, "wrong number of arguments to "
6660 "%<__builtin_launder%>");
6661 postfix_expression
= error_mark_node
;
6665 case RID_BUILTIN_SHUFFLE
:
6666 if (vec
->length () == 2)
6668 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6669 (*vec
)[1], tf_warning_or_error
);
6670 else if (vec
->length () == 3)
6672 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6673 (*vec
)[2], tf_warning_or_error
);
6676 error_at (loc
, "wrong number of arguments to "
6677 "%<__builtin_shuffle%>");
6678 postfix_expression
= error_mark_node
;
6692 /* If the next thing is a simple-type-specifier, we may be
6693 looking at a functional cast. We could also be looking at
6694 an id-expression. So, we try the functional cast, and if
6695 that doesn't work we fall back to the primary-expression. */
6696 cp_parser_parse_tentatively (parser
);
6697 /* Look for the simple-type-specifier. */
6698 ++parser
->prevent_constrained_type_specifiers
;
6699 type
= cp_parser_simple_type_specifier (parser
,
6700 /*decl_specs=*/NULL
,
6701 CP_PARSER_FLAGS_NONE
);
6702 --parser
->prevent_constrained_type_specifiers
;
6703 /* Parse the cast itself. */
6704 if (!cp_parser_error_occurred (parser
))
6706 = cp_parser_functional_cast (parser
, type
);
6707 /* If that worked, we're done. */
6708 if (cp_parser_parse_definitely (parser
))
6711 /* If the functional-cast didn't work out, try a
6712 compound-literal. */
6713 if (cp_parser_allow_gnu_extensions_p (parser
)
6714 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6716 cp_expr initializer
= NULL_TREE
;
6718 cp_parser_parse_tentatively (parser
);
6720 /* Avoid calling cp_parser_type_id pointlessly, see comment
6721 in cp_parser_cast_expression about c++/29234. */
6722 if (!cp_parser_compound_literal_p (parser
))
6723 cp_parser_simulate_error (parser
);
6726 /* Parse the type. */
6727 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6728 parser
->in_type_id_in_expr_p
= true;
6729 type
= cp_parser_type_id (parser
);
6730 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6731 /* Look for the `)'. */
6732 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6735 /* If things aren't going well, there's no need to
6737 if (!cp_parser_error_occurred (parser
))
6739 bool non_constant_p
;
6740 /* Parse the brace-enclosed initializer list. */
6741 initializer
= cp_parser_braced_list (parser
,
6744 /* If that worked, we're definitely looking at a
6745 compound-literal expression. */
6746 if (cp_parser_parse_definitely (parser
))
6748 /* Warn the user that a compound literal is not
6749 allowed in standard C++. */
6750 pedwarn (input_location
, OPT_Wpedantic
,
6751 "ISO C++ forbids compound-literals");
6752 /* For simplicity, we disallow compound literals in
6753 constant-expressions. We could
6754 allow compound literals of integer type, whose
6755 initializer was a constant, in constant
6756 expressions. Permitting that usage, as a further
6757 extension, would not change the meaning of any
6758 currently accepted programs. (Of course, as
6759 compound literals are not part of ISO C++, the
6760 standard has nothing to say.) */
6761 if (cp_parser_non_integral_constant_expression (parser
,
6764 postfix_expression
= error_mark_node
;
6767 /* Form the representation of the compound-literal. */
6769 = finish_compound_literal (type
, initializer
,
6770 tf_warning_or_error
);
6771 postfix_expression
.set_location (initializer
.get_location ());
6776 /* It must be a primary-expression. */
6778 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6779 /*template_arg_p=*/false,
6786 /* Note that we don't need to worry about calling build_cplus_new on a
6787 class-valued CALL_EXPR in decltype when it isn't the end of the
6788 postfix-expression; unary_complex_lvalue will take care of that for
6791 /* Keep looping until the postfix-expression is complete. */
6794 if (idk
== CP_ID_KIND_UNQUALIFIED
6795 && identifier_p (postfix_expression
)
6796 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6797 /* It is not a Koenig lookup function call. */
6799 = unqualified_name_lookup_error (postfix_expression
);
6801 /* Peek at the next token. */
6802 token
= cp_lexer_peek_token (parser
->lexer
);
6804 switch (token
->type
)
6806 case CPP_OPEN_SQUARE
:
6807 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6809 cp_parser_error (parser
,
6810 "two consecutive %<[%> shall "
6811 "only introduce an attribute");
6812 return error_mark_node
;
6815 = cp_parser_postfix_open_square_expression (parser
,
6819 postfix_expression
.set_range (start_loc
,
6820 postfix_expression
.get_location ());
6822 idk
= CP_ID_KIND_NONE
;
6823 is_member_access
= false;
6826 case CPP_OPEN_PAREN
:
6827 /* postfix-expression ( expression-list [opt] ) */
6830 bool is_builtin_constant_p
;
6831 bool saved_integral_constant_expression_p
= false;
6832 bool saved_non_integral_constant_expression_p
= false;
6833 tsubst_flags_t complain
= complain_flags (decltype_p
);
6834 vec
<tree
, va_gc
> *args
;
6835 location_t close_paren_loc
= UNKNOWN_LOCATION
;
6837 is_member_access
= false;
6839 is_builtin_constant_p
6840 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6841 if (is_builtin_constant_p
)
6843 /* The whole point of __builtin_constant_p is to allow
6844 non-constant expressions to appear as arguments. */
6845 saved_integral_constant_expression_p
6846 = parser
->integral_constant_expression_p
;
6847 saved_non_integral_constant_expression_p
6848 = parser
->non_integral_constant_expression_p
;
6849 parser
->integral_constant_expression_p
= false;
6851 args
= (cp_parser_parenthesized_expression_list
6853 /*cast_p=*/false, /*allow_expansion_p=*/true,
6854 /*non_constant_p=*/NULL
,
6855 /*close_paren_loc=*/&close_paren_loc
));
6856 if (is_builtin_constant_p
)
6858 parser
->integral_constant_expression_p
6859 = saved_integral_constant_expression_p
;
6860 parser
->non_integral_constant_expression_p
6861 = saved_non_integral_constant_expression_p
;
6866 postfix_expression
= error_mark_node
;
6870 /* Function calls are not permitted in
6871 constant-expressions. */
6872 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6873 && cp_parser_non_integral_constant_expression (parser
,
6876 postfix_expression
= error_mark_node
;
6877 release_tree_vector (args
);
6882 if (idk
== CP_ID_KIND_UNQUALIFIED
6883 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6885 if (identifier_p (postfix_expression
))
6887 if (!args
->is_empty ())
6890 if (!any_type_dependent_arguments_p (args
))
6892 = perform_koenig_lookup (postfix_expression
, args
,
6897 = unqualified_fn_lookup_error (postfix_expression
);
6899 /* We do not perform argument-dependent lookup if
6900 normal lookup finds a non-function, in accordance
6901 with the expected resolution of DR 218. */
6902 else if (!args
->is_empty ()
6903 && is_overloaded_fn (postfix_expression
))
6905 tree fn
= get_first_fn (postfix_expression
);
6906 fn
= STRIP_TEMPLATE (fn
);
6908 /* Do not do argument dependent lookup if regular
6909 lookup finds a member function or a block-scope
6910 function declaration. [basic.lookup.argdep]/3 */
6911 if (!DECL_FUNCTION_MEMBER_P (fn
)
6912 && !DECL_LOCAL_FUNCTION_P (fn
))
6915 if (!any_type_dependent_arguments_p (args
))
6917 = perform_koenig_lookup (postfix_expression
, args
,
6923 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6924 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6925 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6926 && vec_safe_length (args
) == 3)
6928 tree arg0
= (*args
)[0];
6929 tree arg1
= (*args
)[1];
6930 tree arg2
= (*args
)[2];
6931 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
6932 | (!!integer_zerop (arg2
) << 2));
6933 if (TREE_CODE (arg2
) == CONST_DECL
)
6934 arg2
= DECL_INITIAL (arg2
);
6935 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
6938 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6940 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6941 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6943 if (processing_template_decl
6944 && (type_dependent_object_expression_p (instance
)
6945 || (!BASELINK_P (fn
)
6946 && TREE_CODE (fn
) != FIELD_DECL
)
6947 || type_dependent_expression_p (fn
)
6948 || any_type_dependent_arguments_p (args
)))
6950 maybe_generic_this_capture (instance
, fn
);
6952 = build_nt_call_vec (postfix_expression
, args
);
6953 release_tree_vector (args
);
6957 if (BASELINK_P (fn
))
6960 = (build_new_method_call
6961 (instance
, fn
, &args
, NULL_TREE
,
6962 (idk
== CP_ID_KIND_QUALIFIED
6963 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6970 = finish_call_expr (postfix_expression
, &args
,
6971 /*disallow_virtual=*/false,
6975 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6976 || TREE_CODE (postfix_expression
) == MEMBER_REF
6977 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6978 postfix_expression
= (build_offset_ref_call_from_tree
6979 (postfix_expression
, &args
,
6981 else if (idk
== CP_ID_KIND_QUALIFIED
)
6982 /* A call to a static class member, or a namespace-scope
6985 = finish_call_expr (postfix_expression
, &args
,
6986 /*disallow_virtual=*/true,
6990 /* All other function calls. */
6992 = finish_call_expr (postfix_expression
, &args
,
6993 /*disallow_virtual=*/false,
6997 if (close_paren_loc
!= UNKNOWN_LOCATION
)
6999 location_t combined_loc
= make_location (token
->location
,
7002 postfix_expression
.set_location (combined_loc
);
7005 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7006 idk
= CP_ID_KIND_NONE
;
7008 release_tree_vector (args
);
7014 /* postfix-expression . template [opt] id-expression
7015 postfix-expression . pseudo-destructor-name
7016 postfix-expression -> template [opt] id-expression
7017 postfix-expression -> pseudo-destructor-name */
7019 /* Consume the `.' or `->' operator. */
7020 cp_lexer_consume_token (parser
->lexer
);
7023 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7027 is_member_access
= true;
7031 /* postfix-expression ++ */
7032 /* Consume the `++' token. */
7033 cp_lexer_consume_token (parser
->lexer
);
7034 /* Generate a representation for the complete expression. */
7036 = finish_increment_expr (postfix_expression
,
7037 POSTINCREMENT_EXPR
);
7038 /* Increments may not appear in constant-expressions. */
7039 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7040 postfix_expression
= error_mark_node
;
7041 idk
= CP_ID_KIND_NONE
;
7042 is_member_access
= false;
7045 case CPP_MINUS_MINUS
:
7046 /* postfix-expression -- */
7047 /* Consume the `--' token. */
7048 cp_lexer_consume_token (parser
->lexer
);
7049 /* Generate a representation for the complete expression. */
7051 = finish_increment_expr (postfix_expression
,
7052 POSTDECREMENT_EXPR
);
7053 /* Decrements may not appear in constant-expressions. */
7054 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7055 postfix_expression
= error_mark_node
;
7056 idk
= CP_ID_KIND_NONE
;
7057 is_member_access
= false;
7061 if (pidk_return
!= NULL
)
7062 * pidk_return
= idk
;
7063 if (member_access_only_p
)
7064 return is_member_access
7065 ? postfix_expression
7066 : cp_expr (error_mark_node
);
7068 return postfix_expression
;
7072 /* We should never get here. */
7074 return error_mark_node
;
7077 /* This function parses Cilk Plus array notations. If a normal array expr. is
7078 parsed then the array index is passed back to the caller through *INIT_INDEX
7079 and the function returns a NULL_TREE. If array notation expr. is parsed,
7080 then *INIT_INDEX is ignored by the caller and the function returns
7081 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7085 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
7088 cp_token
*token
= NULL
;
7089 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
7090 if (!array_value
|| array_value
== error_mark_node
)
7092 cp_parser_skip_to_end_of_statement (parser
);
7093 return error_mark_node
;
7096 array_type
= TREE_TYPE (array_value
);
7098 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7099 parser
->colon_corrects_to_scope_p
= false;
7100 token
= cp_lexer_peek_token (parser
->lexer
);
7104 cp_parser_error (parser
, "expected %<:%> or numeral");
7105 return error_mark_node
;
7107 else if (token
->type
== CPP_COLON
)
7109 /* Consume the ':'. */
7110 cp_lexer_consume_token (parser
->lexer
);
7112 /* If we are here, then we have a case like this A[:]. */
7113 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7115 cp_parser_error (parser
, "expected %<]%>");
7116 cp_parser_skip_to_end_of_statement (parser
);
7117 return error_mark_node
;
7119 *init_index
= NULL_TREE
;
7121 length_index
= NULL_TREE
;
7125 /* If we are here, then there are three valid possibilities:
7127 2. ARRAY [ EXP : EXP ]
7128 3. ARRAY [ EXP : EXP : EXP ] */
7130 *init_index
= cp_parser_expression (parser
);
7131 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7133 /* This indicates that we have a normal array expression. */
7134 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7138 /* Consume the ':'. */
7139 cp_lexer_consume_token (parser
->lexer
);
7140 length_index
= cp_parser_expression (parser
);
7141 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7143 cp_lexer_consume_token (parser
->lexer
);
7144 stride
= cp_parser_expression (parser
);
7147 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7149 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7150 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7152 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7153 cp_lexer_consume_token (parser
->lexer
);
7154 return error_mark_node
;
7156 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7158 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7159 length_index
, stride
, array_type
);
7163 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7164 by cp_parser_builtin_offsetof. We're looking for
7166 postfix-expression [ expression ]
7167 postfix-expression [ braced-init-list ] (C++11)
7169 FOR_OFFSETOF is set if we're being called in that context, which
7170 changes how we deal with integer constant expressions. */
7173 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7174 tree postfix_expression
,
7178 tree index
= NULL_TREE
;
7179 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7180 bool saved_greater_than_is_operator_p
;
7182 /* Consume the `[' token. */
7183 cp_lexer_consume_token (parser
->lexer
);
7185 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7186 parser
->greater_than_is_operator_p
= true;
7188 /* Parse the index expression. */
7189 /* ??? For offsetof, there is a question of what to allow here. If
7190 offsetof is not being used in an integral constant expression context,
7191 then we *could* get the right answer by computing the value at runtime.
7192 If we are in an integral constant expression context, then we might
7193 could accept any constant expression; hard to say without analysis.
7194 Rather than open the barn door too wide right away, allow only integer
7195 constant expressions here. */
7197 index
= cp_parser_constant_expression (parser
);
7200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7202 bool expr_nonconst_p
;
7203 cp_lexer_set_source_position (parser
->lexer
);
7204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7205 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7207 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7209 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7210 "braced list index is not allowed with array "
7212 cp_parser_skip_to_end_of_statement (parser
);
7213 return error_mark_node
;
7216 else if (flag_cilkplus
)
7218 /* Here are have these two options:
7219 ARRAY[EXP : EXP] - Array notation expr with default
7221 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7223 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7224 postfix_expression
);
7229 index
= cp_parser_expression (parser
);
7232 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7234 /* Look for the closing `]'. */
7235 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7237 /* Build the ARRAY_REF. */
7238 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7241 /* When not doing offsetof, array references are not permitted in
7242 constant-expressions. */
7244 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7245 postfix_expression
= error_mark_node
;
7247 return postfix_expression
;
7250 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7251 by cp_parser_builtin_offsetof. We're looking for
7253 postfix-expression . template [opt] id-expression
7254 postfix-expression . pseudo-destructor-name
7255 postfix-expression -> template [opt] id-expression
7256 postfix-expression -> pseudo-destructor-name
7258 FOR_OFFSETOF is set if we're being called in that context. That sorta
7259 limits what of the above we'll actually accept, but nevermind.
7260 TOKEN_TYPE is the "." or "->" token, which will already have been
7261 removed from the stream. */
7264 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7265 enum cpp_ttype token_type
,
7266 cp_expr postfix_expression
,
7267 bool for_offsetof
, cp_id_kind
*idk
,
7268 location_t location
)
7272 bool pseudo_destructor_p
;
7273 tree scope
= NULL_TREE
;
7274 location_t start_loc
= postfix_expression
.get_start ();
7276 /* If this is a `->' operator, dereference the pointer. */
7277 if (token_type
== CPP_DEREF
)
7278 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7279 tf_warning_or_error
);
7280 /* Check to see whether or not the expression is type-dependent and
7281 not the current instantiation. */
7282 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7283 /* The identifier following the `->' or `.' is not qualified. */
7284 parser
->scope
= NULL_TREE
;
7285 parser
->qualifying_scope
= NULL_TREE
;
7286 parser
->object_scope
= NULL_TREE
;
7287 *idk
= CP_ID_KIND_NONE
;
7289 /* Enter the scope corresponding to the type of the object
7290 given by the POSTFIX_EXPRESSION. */
7293 scope
= TREE_TYPE (postfix_expression
);
7294 /* According to the standard, no expression should ever have
7295 reference type. Unfortunately, we do not currently match
7296 the standard in this respect in that our internal representation
7297 of an expression may have reference type even when the standard
7298 says it does not. Therefore, we have to manually obtain the
7299 underlying type here. */
7300 scope
= non_reference (scope
);
7301 /* The type of the POSTFIX_EXPRESSION must be complete. */
7302 /* Unlike the object expression in other contexts, *this is not
7303 required to be of complete type for purposes of class member
7304 access (5.2.5) outside the member function body. */
7305 if (postfix_expression
!= current_class_ref
7306 && scope
!= error_mark_node
7307 && !(processing_template_decl
7308 && current_class_type
7309 && (same_type_ignoring_top_level_qualifiers_p
7310 (scope
, current_class_type
))))
7312 scope
= complete_type (scope
);
7313 if (!COMPLETE_TYPE_P (scope
)
7314 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7315 && EXPR_P (postfix_expression
))
7317 /* In a template, be permissive by treating an object expression
7318 of incomplete type as dependent (after a pedwarn). */
7319 diagnostic_t kind
= (processing_template_decl
7322 cxx_incomplete_type_diagnostic
7323 (location_of (postfix_expression
),
7324 postfix_expression
, scope
, kind
);
7325 if (processing_template_decl
)
7328 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7335 /* Let the name lookup machinery know that we are processing a
7336 class member access expression. */
7337 parser
->context
->object_type
= scope
;
7338 /* If something went wrong, we want to be able to discern that case,
7339 as opposed to the case where there was no SCOPE due to the type
7340 of expression being dependent. */
7342 scope
= error_mark_node
;
7343 /* If the SCOPE was erroneous, make the various semantic analysis
7344 functions exit quickly -- and without issuing additional error
7346 if (scope
== error_mark_node
)
7347 postfix_expression
= error_mark_node
;
7352 /* Tell cp_parser_lookup_name that there was an object, even though it's
7354 parser
->context
->object_type
= unknown_type_node
;
7356 /* Assume this expression is not a pseudo-destructor access. */
7357 pseudo_destructor_p
= false;
7359 /* If the SCOPE is a scalar type, then, if this is a valid program,
7360 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7361 is type dependent, it can be pseudo-destructor-name or something else.
7362 Try to parse it as pseudo-destructor-name first. */
7363 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7368 cp_parser_parse_tentatively (parser
);
7369 /* Parse the pseudo-destructor-name. */
7371 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7374 && (cp_parser_error_occurred (parser
)
7375 || !SCALAR_TYPE_P (type
)))
7376 cp_parser_abort_tentative_parse (parser
);
7377 else if (cp_parser_parse_definitely (parser
))
7379 pseudo_destructor_p
= true;
7381 = finish_pseudo_destructor_expr (postfix_expression
,
7386 if (!pseudo_destructor_p
)
7388 /* If the SCOPE is not a scalar type, we are looking at an
7389 ordinary class member access expression, rather than a
7390 pseudo-destructor-name. */
7392 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7393 /* Parse the id-expression. */
7394 name
= (cp_parser_id_expression
7396 cp_parser_optional_template_keyword (parser
),
7397 /*check_dependency_p=*/true,
7399 /*declarator_p=*/false,
7400 /*optional_p=*/false));
7401 /* In general, build a SCOPE_REF if the member name is qualified.
7402 However, if the name was not dependent and has already been
7403 resolved; there is no need to build the SCOPE_REF. For example;
7405 struct X { void f(); };
7406 template <typename T> void f(T* t) { t->X::f(); }
7408 Even though "t" is dependent, "X::f" is not and has been resolved
7409 to a BASELINK; there is no need to include scope information. */
7411 /* But we do need to remember that there was an explicit scope for
7412 virtual function calls. */
7414 *idk
= CP_ID_KIND_QUALIFIED
;
7416 /* If the name is a template-id that names a type, we will get a
7417 TYPE_DECL here. That is invalid code. */
7418 if (TREE_CODE (name
) == TYPE_DECL
)
7420 error_at (token
->location
, "invalid use of %qD", name
);
7421 postfix_expression
= error_mark_node
;
7425 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7427 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7429 error_at (token
->location
, "%<%D::%D%> is not a class member",
7430 parser
->scope
, name
);
7431 postfix_expression
= error_mark_node
;
7434 name
= build_qualified_name (/*type=*/NULL_TREE
,
7438 parser
->scope
= NULL_TREE
;
7439 parser
->qualifying_scope
= NULL_TREE
;
7440 parser
->object_scope
= NULL_TREE
;
7442 if (parser
->scope
&& name
&& BASELINK_P (name
))
7443 adjust_result_of_qualified_name_lookup
7444 (name
, parser
->scope
, scope
);
7446 = finish_class_member_access_expr (postfix_expression
, name
,
7448 tf_warning_or_error
);
7449 /* Build a location e.g.:
7452 where the caret is at the deref token, ranging from
7453 the start of postfix_expression to the end of the access expr. */
7455 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7456 location_t combined_loc
7457 = make_location (input_location
, start_loc
, end_loc
);
7458 protected_set_expr_location (postfix_expression
, combined_loc
);
7462 /* We no longer need to look up names in the scope of the object on
7463 the left-hand side of the `.' or `->' operator. */
7464 parser
->context
->object_type
= NULL_TREE
;
7466 /* Outside of offsetof, these operators may not appear in
7467 constant-expressions. */
7469 && (cp_parser_non_integral_constant_expression
7470 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7471 postfix_expression
= error_mark_node
;
7473 return postfix_expression
;
7476 /* Parse a parenthesized expression-list.
7479 assignment-expression
7480 expression-list, assignment-expression
7485 identifier, expression-list
7487 CAST_P is true if this expression is the target of a cast.
7489 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7492 Returns a vector of trees. Each element is a representation of an
7493 assignment-expression. NULL is returned if the ( and or ) are
7494 missing. An empty, but allocated, vector is returned on no
7495 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7496 if we are parsing an attribute list for an attribute that wants a
7497 plain identifier argument, normal_attr for an attribute that wants
7498 an expression, or non_attr if we aren't parsing an attribute list. If
7499 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7500 not all of the expressions in the list were constant.
7501 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7502 will be written to with the location of the closing parenthesis. If
7503 an error occurs, it may or may not be written to. */
7505 static vec
<tree
, va_gc
> *
7506 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7507 int is_attribute_list
,
7509 bool allow_expansion_p
,
7510 bool *non_constant_p
,
7511 location_t
*close_paren_loc
)
7513 vec
<tree
, va_gc
> *expression_list
;
7514 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7515 tree identifier
= NULL_TREE
;
7516 bool saved_greater_than_is_operator_p
;
7518 /* Assume all the expressions will be constant. */
7520 *non_constant_p
= false;
7522 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7525 expression_list
= make_tree_vector ();
7527 /* Within a parenthesized expression, a `>' token is always
7528 the greater-than operator. */
7529 saved_greater_than_is_operator_p
7530 = parser
->greater_than_is_operator_p
;
7531 parser
->greater_than_is_operator_p
= true;
7533 /* Consume expressions until there are no more. */
7534 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7539 /* At the beginning of attribute lists, check to see if the
7540 next token is an identifier. */
7541 if (is_attribute_list
== id_attr
7542 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7546 /* Consume the identifier. */
7547 token
= cp_lexer_consume_token (parser
->lexer
);
7548 /* Save the identifier. */
7549 identifier
= token
->u
.value
;
7553 bool expr_non_constant_p
;
7555 /* Parse the next assignment-expression. */
7556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7558 /* A braced-init-list. */
7559 cp_lexer_set_source_position (parser
->lexer
);
7560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7561 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7562 if (non_constant_p
&& expr_non_constant_p
)
7563 *non_constant_p
= true;
7565 else if (non_constant_p
)
7567 expr
= (cp_parser_constant_expression
7568 (parser
, /*allow_non_constant_p=*/true,
7569 &expr_non_constant_p
));
7570 if (expr_non_constant_p
)
7571 *non_constant_p
= true;
7574 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7578 expr
= instantiate_non_dependent_expr (expr
);
7580 /* If we have an ellipsis, then this is an expression
7582 if (allow_expansion_p
7583 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7585 /* Consume the `...'. */
7586 cp_lexer_consume_token (parser
->lexer
);
7588 /* Build the argument pack. */
7589 expr
= make_pack_expansion (expr
);
7592 /* Add it to the list. We add error_mark_node
7593 expressions to the list, so that we can still tell if
7594 the correct form for a parenthesized expression-list
7595 is found. That gives better errors. */
7596 vec_safe_push (expression_list
, expr
);
7598 if (expr
== error_mark_node
)
7602 /* After the first item, attribute lists look the same as
7603 expression lists. */
7604 is_attribute_list
= non_attr
;
7607 /* If the next token isn't a `,', then we are done. */
7608 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7611 /* Otherwise, consume the `,' and keep going. */
7612 cp_lexer_consume_token (parser
->lexer
);
7615 if (close_paren_loc
)
7616 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7618 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7623 /* We try and resync to an unnested comma, as that will give the
7624 user better diagnostics. */
7625 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7626 /*recovering=*/true,
7628 /*consume_paren=*/true);
7633 parser
->greater_than_is_operator_p
7634 = saved_greater_than_is_operator_p
;
7639 parser
->greater_than_is_operator_p
7640 = saved_greater_than_is_operator_p
;
7643 vec_safe_insert (expression_list
, 0, identifier
);
7645 return expression_list
;
7648 /* Parse a pseudo-destructor-name.
7650 pseudo-destructor-name:
7651 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7652 :: [opt] nested-name-specifier template template-id :: ~ type-name
7653 :: [opt] nested-name-specifier [opt] ~ type-name
7655 If either of the first two productions is used, sets *SCOPE to the
7656 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7657 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7658 or ERROR_MARK_NODE if the parse fails. */
7661 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7666 bool nested_name_specifier_p
;
7669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7670 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7671 && !type_dependent_expression_p (object
))
7673 if (cxx_dialect
< cxx14
)
7674 pedwarn (input_location
, 0,
7675 "%<~auto%> only available with "
7676 "-std=c++14 or -std=gnu++14");
7677 cp_lexer_consume_token (parser
->lexer
);
7678 cp_lexer_consume_token (parser
->lexer
);
7680 *type
= TREE_TYPE (object
);
7684 /* Assume that things will not work out. */
7685 *type
= error_mark_node
;
7687 /* Look for the optional `::' operator. */
7688 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7689 /* Look for the optional nested-name-specifier. */
7690 nested_name_specifier_p
7691 = (cp_parser_nested_name_specifier_opt (parser
,
7692 /*typename_keyword_p=*/false,
7693 /*check_dependency_p=*/true,
7695 /*is_declaration=*/false)
7697 /* Now, if we saw a nested-name-specifier, we might be doing the
7698 second production. */
7699 if (nested_name_specifier_p
7700 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7702 /* Consume the `template' keyword. */
7703 cp_lexer_consume_token (parser
->lexer
);
7704 /* Parse the template-id. */
7705 cp_parser_template_id (parser
,
7706 /*template_keyword_p=*/true,
7707 /*check_dependency_p=*/false,
7709 /*is_declaration=*/true);
7710 /* Look for the `::' token. */
7711 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7713 /* If the next token is not a `~', then there might be some
7714 additional qualification. */
7715 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7717 /* At this point, we're looking for "type-name :: ~". The type-name
7718 must not be a class-name, since this is a pseudo-destructor. So,
7719 it must be either an enum-name, or a typedef-name -- both of which
7720 are just identifiers. So, we peek ahead to check that the "::"
7721 and "~" tokens are present; if they are not, then we can avoid
7722 calling type_name. */
7723 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7724 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7725 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7727 cp_parser_error (parser
, "non-scalar type");
7731 /* Look for the type-name. */
7732 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7733 if (*scope
== error_mark_node
)
7736 /* Look for the `::' token. */
7737 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7742 /* Look for the `~'. */
7743 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7745 /* Once we see the ~, this has to be a pseudo-destructor. */
7746 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7747 cp_parser_commit_to_topmost_tentative_parse (parser
);
7749 /* Look for the type-name again. We are not responsible for
7750 checking that it matches the first type-name. */
7751 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7754 /* Parse a unary-expression.
7760 unary-operator cast-expression
7761 sizeof unary-expression
7763 alignof ( type-id ) [C++0x]
7770 __extension__ cast-expression
7771 __alignof__ unary-expression
7772 __alignof__ ( type-id )
7773 alignof unary-expression [C++0x]
7774 __real__ cast-expression
7775 __imag__ cast-expression
7777 sizeof ( type-id ) { initializer-list , [opt] }
7778 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7779 __alignof__ ( type-id ) { initializer-list , [opt] }
7781 ADDRESS_P is true iff the unary-expression is appearing as the
7782 operand of the `&' operator. CAST_P is true if this expression is
7783 the target of a cast.
7785 Returns a representation of the expression. */
7788 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7789 bool address_p
, bool cast_p
, bool decltype_p
)
7792 enum tree_code unary_operator
;
7794 /* Peek at the next token. */
7795 token
= cp_lexer_peek_token (parser
->lexer
);
7796 /* Some keywords give away the kind of expression. */
7797 if (token
->type
== CPP_KEYWORD
)
7799 enum rid keyword
= token
->keyword
;
7808 location_t first_loc
;
7810 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7811 /* Consume the token. */
7812 cp_lexer_consume_token (parser
->lexer
);
7813 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7814 /* Parse the operand. */
7815 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7817 if (TYPE_P (operand
))
7818 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7821 /* ISO C++ defines alignof only with types, not with
7822 expressions. So pedwarn if alignof is used with a non-
7823 type expression. However, __alignof__ is ok. */
7824 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7825 pedwarn (token
->location
, OPT_Wpedantic
,
7826 "ISO C++ does not allow %<alignof%> "
7829 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7831 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7832 SIZEOF_EXPR with the original operand. */
7833 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7835 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7837 if (!processing_template_decl
&& TYPE_P (operand
))
7839 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7840 build1 (NOP_EXPR
, operand
,
7842 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7845 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7846 TREE_SIDE_EFFECTS (ret
) = 0;
7847 TREE_READONLY (ret
) = 1;
7849 SET_EXPR_LOCATION (ret
, first_loc
);
7855 return cp_parser_new_expression (parser
);
7858 return cp_parser_delete_expression (parser
);
7862 /* The saved value of the PEDANTIC flag. */
7866 /* Save away the PEDANTIC flag. */
7867 cp_parser_extension_opt (parser
, &saved_pedantic
);
7868 /* Parse the cast-expression. */
7869 expr
= cp_parser_simple_cast_expression (parser
);
7870 /* Restore the PEDANTIC flag. */
7871 pedantic
= saved_pedantic
;
7881 /* Consume the `__real__' or `__imag__' token. */
7882 cp_lexer_consume_token (parser
->lexer
);
7883 /* Parse the cast-expression. */
7884 expression
= cp_parser_simple_cast_expression (parser
);
7885 /* Create the complete representation. */
7886 return build_x_unary_op (token
->location
,
7887 (keyword
== RID_REALPART
7888 ? REALPART_EXPR
: IMAGPART_EXPR
),
7890 tf_warning_or_error
);
7894 case RID_TRANSACTION_ATOMIC
:
7895 case RID_TRANSACTION_RELAXED
:
7896 return cp_parser_transaction_expression (parser
, keyword
);
7901 const char *saved_message
;
7902 bool saved_integral_constant_expression_p
;
7903 bool saved_non_integral_constant_expression_p
;
7904 bool saved_greater_than_is_operator_p
;
7906 cp_lexer_consume_token (parser
->lexer
);
7907 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7909 saved_message
= parser
->type_definition_forbidden_message
;
7910 parser
->type_definition_forbidden_message
7911 = G_("types may not be defined in %<noexcept%> expressions");
7913 saved_integral_constant_expression_p
7914 = parser
->integral_constant_expression_p
;
7915 saved_non_integral_constant_expression_p
7916 = parser
->non_integral_constant_expression_p
;
7917 parser
->integral_constant_expression_p
= false;
7919 saved_greater_than_is_operator_p
7920 = parser
->greater_than_is_operator_p
;
7921 parser
->greater_than_is_operator_p
= true;
7923 ++cp_unevaluated_operand
;
7924 ++c_inhibit_evaluation_warnings
;
7925 ++cp_noexcept_operand
;
7926 expr
= cp_parser_expression (parser
);
7927 --cp_noexcept_operand
;
7928 --c_inhibit_evaluation_warnings
;
7929 --cp_unevaluated_operand
;
7931 parser
->greater_than_is_operator_p
7932 = saved_greater_than_is_operator_p
;
7934 parser
->integral_constant_expression_p
7935 = saved_integral_constant_expression_p
;
7936 parser
->non_integral_constant_expression_p
7937 = saved_non_integral_constant_expression_p
;
7939 parser
->type_definition_forbidden_message
= saved_message
;
7941 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7942 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7950 /* Look for the `:: new' and `:: delete', which also signal the
7951 beginning of a new-expression, or delete-expression,
7952 respectively. If the next token is `::', then it might be one of
7954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7958 /* See if the token after the `::' is one of the keywords in
7959 which we're interested. */
7960 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7961 /* If it's `new', we have a new-expression. */
7962 if (keyword
== RID_NEW
)
7963 return cp_parser_new_expression (parser
);
7964 /* Similarly, for `delete'. */
7965 else if (keyword
== RID_DELETE
)
7966 return cp_parser_delete_expression (parser
);
7969 /* Look for a unary operator. */
7970 unary_operator
= cp_parser_unary_operator (token
);
7971 /* The `++' and `--' operators can be handled similarly, even though
7972 they are not technically unary-operators in the grammar. */
7973 if (unary_operator
== ERROR_MARK
)
7975 if (token
->type
== CPP_PLUS_PLUS
)
7976 unary_operator
= PREINCREMENT_EXPR
;
7977 else if (token
->type
== CPP_MINUS_MINUS
)
7978 unary_operator
= PREDECREMENT_EXPR
;
7979 /* Handle the GNU address-of-label extension. */
7980 else if (cp_parser_allow_gnu_extensions_p (parser
)
7981 && token
->type
== CPP_AND_AND
)
7985 location_t start_loc
= token
->location
;
7987 /* Consume the '&&' token. */
7988 cp_lexer_consume_token (parser
->lexer
);
7989 /* Look for the identifier. */
7990 location_t finish_loc
7991 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
7992 identifier
= cp_parser_identifier (parser
);
7993 /* Construct a location of the form:
7996 with caret==start at the "&&", finish at the end of the label. */
7997 location_t combined_loc
7998 = make_location (start_loc
, start_loc
, finish_loc
);
7999 /* Create an expression representing the address. */
8000 expression
= finish_label_address_expr (identifier
, combined_loc
);
8001 if (cp_parser_non_integral_constant_expression (parser
,
8003 expression
= error_mark_node
;
8007 if (unary_operator
!= ERROR_MARK
)
8009 cp_expr cast_expression
;
8010 cp_expr expression
= error_mark_node
;
8011 non_integral_constant non_constant_p
= NIC_NONE
;
8012 location_t loc
= token
->location
;
8013 tsubst_flags_t complain
= complain_flags (decltype_p
);
8015 /* Consume the operator token. */
8016 token
= cp_lexer_consume_token (parser
->lexer
);
8017 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8019 /* Parse the cast-expression. */
8021 = cp_parser_cast_expression (parser
,
8022 unary_operator
== ADDR_EXPR
,
8028 OP_TOKEN CAST_EXPRESSION
8029 ^~~~~~~~~~~~~~~~~~~~~~~~~
8030 with start==caret at the operator token, and
8031 extending to the end of the cast_expression. */
8032 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8034 /* Now, build an appropriate representation. */
8035 switch (unary_operator
)
8038 non_constant_p
= NIC_STAR
;
8039 expression
= build_x_indirect_ref (loc
, cast_expression
,
8042 /* TODO: build_x_indirect_ref does not always honor the
8043 location, so ensure it is set. */
8044 expression
.set_location (loc
);
8048 non_constant_p
= NIC_ADDR
;
8051 expression
= build_x_unary_op (loc
, unary_operator
,
8054 /* TODO: build_x_unary_op does not always honor the location,
8055 so ensure it is set. */
8056 expression
.set_location (loc
);
8059 case PREINCREMENT_EXPR
:
8060 case PREDECREMENT_EXPR
:
8061 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8062 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8065 /* Immediately fold negation of a constant, unless the constant is 0
8066 (since -0 == 0) or it would overflow. */
8067 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8068 && CONSTANT_CLASS_P (cast_expression
)
8069 && !integer_zerop (cast_expression
)
8070 && !TREE_OVERFLOW (cast_expression
))
8072 tree folded
= fold_build1 (unary_operator
,
8073 TREE_TYPE (cast_expression
),
8075 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8077 expression
= cp_expr (folded
, loc
);
8082 case UNARY_PLUS_EXPR
:
8083 case TRUTH_NOT_EXPR
:
8084 expression
= finish_unary_op_expr (loc
, unary_operator
,
8085 cast_expression
, complain
);
8092 if (non_constant_p
!= NIC_NONE
8093 && cp_parser_non_integral_constant_expression (parser
,
8095 expression
= error_mark_node
;
8100 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8101 /*member_access_only_p=*/false,
8106 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8107 unary-operator, the corresponding tree code is returned. */
8109 static enum tree_code
8110 cp_parser_unary_operator (cp_token
* token
)
8112 switch (token
->type
)
8115 return INDIRECT_REF
;
8121 return UNARY_PLUS_EXPR
;
8127 return TRUTH_NOT_EXPR
;
8130 return BIT_NOT_EXPR
;
8137 /* Parse a new-expression.
8140 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8141 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8143 Returns a representation of the expression. */
8146 cp_parser_new_expression (cp_parser
* parser
)
8148 bool global_scope_p
;
8149 vec
<tree
, va_gc
> *placement
;
8151 vec
<tree
, va_gc
> *initializer
;
8152 tree nelts
= NULL_TREE
;
8155 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8157 /* Look for the optional `::' operator. */
8159 = (cp_parser_global_scope_opt (parser
,
8160 /*current_scope_valid_p=*/false)
8162 /* Look for the `new' operator. */
8163 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8164 /* There's no easy way to tell a new-placement from the
8165 `( type-id )' construct. */
8166 cp_parser_parse_tentatively (parser
);
8167 /* Look for a new-placement. */
8168 placement
= cp_parser_new_placement (parser
);
8169 /* If that didn't work out, there's no new-placement. */
8170 if (!cp_parser_parse_definitely (parser
))
8172 if (placement
!= NULL
)
8173 release_tree_vector (placement
);
8177 /* If the next token is a `(', then we have a parenthesized
8179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8182 const char *saved_message
= parser
->type_definition_forbidden_message
;
8184 /* Consume the `('. */
8185 cp_lexer_consume_token (parser
->lexer
);
8187 /* Parse the type-id. */
8188 parser
->type_definition_forbidden_message
8189 = G_("types may not be defined in a new-expression");
8191 type_id_in_expr_sentinel
s (parser
);
8192 type
= cp_parser_type_id (parser
);
8194 parser
->type_definition_forbidden_message
= saved_message
;
8196 /* Look for the closing `)'. */
8197 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8198 token
= cp_lexer_peek_token (parser
->lexer
);
8199 /* There should not be a direct-new-declarator in this production,
8200 but GCC used to allowed this, so we check and emit a sensible error
8201 message for this case. */
8202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8204 error_at (token
->location
,
8205 "array bound forbidden after parenthesized type-id");
8206 inform (token
->location
,
8207 "try removing the parentheses around the type-id");
8208 cp_parser_direct_new_declarator (parser
);
8211 /* Otherwise, there must be a new-type-id. */
8213 type
= cp_parser_new_type_id (parser
, &nelts
);
8215 /* If the next token is a `(' or '{', then we have a new-initializer. */
8216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8217 if (token
->type
== CPP_OPEN_PAREN
8218 || token
->type
== CPP_OPEN_BRACE
)
8219 initializer
= cp_parser_new_initializer (parser
);
8223 /* A new-expression may not appear in an integral constant
8225 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8226 ret
= error_mark_node
;
8227 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8228 of a new-type-id or type-id of a new-expression, the new-expression shall
8229 contain a new-initializer of the form ( assignment-expression )".
8230 Additionally, consistently with the spirit of DR 1467, we want to accept
8231 'new auto { 2 }' too. */
8232 else if ((ret
= type_uses_auto (type
))
8233 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8234 && (vec_safe_length (initializer
) != 1
8235 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8236 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8238 error_at (token
->location
,
8239 "initialization of new-expression for type %<auto%> "
8240 "requires exactly one element");
8241 ret
= error_mark_node
;
8245 /* Construct a location e.g.:
8248 with caret == start at the start of the "new" token, and the end
8249 at the end of the final token we consumed. */
8250 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8251 location_t end_loc
= get_finish (end_tok
->location
);
8252 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8254 /* Create a representation of the new-expression. */
8255 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8256 tf_warning_or_error
);
8257 protected_set_expr_location (ret
, combined_loc
);
8260 if (placement
!= NULL
)
8261 release_tree_vector (placement
);
8262 if (initializer
!= NULL
)
8263 release_tree_vector (initializer
);
8268 /* Parse a new-placement.
8273 Returns the same representation as for an expression-list. */
8275 static vec
<tree
, va_gc
> *
8276 cp_parser_new_placement (cp_parser
* parser
)
8278 vec
<tree
, va_gc
> *expression_list
;
8280 /* Parse the expression-list. */
8281 expression_list
= (cp_parser_parenthesized_expression_list
8282 (parser
, non_attr
, /*cast_p=*/false,
8283 /*allow_expansion_p=*/true,
8284 /*non_constant_p=*/NULL
));
8286 if (expression_list
&& expression_list
->is_empty ())
8287 error ("expected expression-list or type-id");
8289 return expression_list
;
8292 /* Parse a new-type-id.
8295 type-specifier-seq new-declarator [opt]
8297 Returns the TYPE allocated. If the new-type-id indicates an array
8298 type, *NELTS is set to the number of elements in the last array
8299 bound; the TYPE will not include the last array bound. */
8302 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8304 cp_decl_specifier_seq type_specifier_seq
;
8305 cp_declarator
*new_declarator
;
8306 cp_declarator
*declarator
;
8307 cp_declarator
*outer_declarator
;
8308 const char *saved_message
;
8310 /* The type-specifier sequence must not contain type definitions.
8311 (It cannot contain declarations of new types either, but if they
8312 are not definitions we will catch that because they are not
8314 saved_message
= parser
->type_definition_forbidden_message
;
8315 parser
->type_definition_forbidden_message
8316 = G_("types may not be defined in a new-type-id");
8317 /* Parse the type-specifier-seq. */
8318 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8319 /*is_trailing_return=*/false,
8320 &type_specifier_seq
);
8321 /* Restore the old message. */
8322 parser
->type_definition_forbidden_message
= saved_message
;
8324 if (type_specifier_seq
.type
== error_mark_node
)
8325 return error_mark_node
;
8327 /* Parse the new-declarator. */
8328 new_declarator
= cp_parser_new_declarator_opt (parser
);
8330 /* Determine the number of elements in the last array dimension, if
8333 /* Skip down to the last array dimension. */
8334 declarator
= new_declarator
;
8335 outer_declarator
= NULL
;
8336 while (declarator
&& (declarator
->kind
== cdk_pointer
8337 || declarator
->kind
== cdk_ptrmem
))
8339 outer_declarator
= declarator
;
8340 declarator
= declarator
->declarator
;
8343 && declarator
->kind
== cdk_array
8344 && declarator
->declarator
8345 && declarator
->declarator
->kind
== cdk_array
)
8347 outer_declarator
= declarator
;
8348 declarator
= declarator
->declarator
;
8351 if (declarator
&& declarator
->kind
== cdk_array
)
8353 *nelts
= declarator
->u
.array
.bounds
;
8354 if (*nelts
== error_mark_node
)
8355 *nelts
= integer_one_node
;
8357 if (outer_declarator
)
8358 outer_declarator
->declarator
= declarator
->declarator
;
8360 new_declarator
= NULL
;
8363 return groktypename (&type_specifier_seq
, new_declarator
, false);
8366 /* Parse an (optional) new-declarator.
8369 ptr-operator new-declarator [opt]
8370 direct-new-declarator
8372 Returns the declarator. */
8374 static cp_declarator
*
8375 cp_parser_new_declarator_opt (cp_parser
* parser
)
8377 enum tree_code code
;
8378 tree type
, std_attributes
= NULL_TREE
;
8379 cp_cv_quals cv_quals
;
8381 /* We don't know if there's a ptr-operator next, or not. */
8382 cp_parser_parse_tentatively (parser
);
8383 /* Look for a ptr-operator. */
8384 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8385 /* If that worked, look for more new-declarators. */
8386 if (cp_parser_parse_definitely (parser
))
8388 cp_declarator
*declarator
;
8390 /* Parse another optional declarator. */
8391 declarator
= cp_parser_new_declarator_opt (parser
);
8393 declarator
= cp_parser_make_indirect_declarator
8394 (code
, type
, cv_quals
, declarator
, std_attributes
);
8399 /* If the next token is a `[', there is a direct-new-declarator. */
8400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8401 return cp_parser_direct_new_declarator (parser
);
8406 /* Parse a direct-new-declarator.
8408 direct-new-declarator:
8410 direct-new-declarator [constant-expression]
8414 static cp_declarator
*
8415 cp_parser_direct_new_declarator (cp_parser
* parser
)
8417 cp_declarator
*declarator
= NULL
;
8424 /* Look for the opening `['. */
8425 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8427 token
= cp_lexer_peek_token (parser
->lexer
);
8428 expression
= cp_parser_expression (parser
);
8429 /* The standard requires that the expression have integral
8430 type. DR 74 adds enumeration types. We believe that the
8431 real intent is that these expressions be handled like the
8432 expression in a `switch' condition, which also allows
8433 classes with a single conversion to integral or
8434 enumeration type. */
8435 if (!processing_template_decl
)
8438 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8443 error_at (token
->location
,
8444 "expression in new-declarator must have integral "
8445 "or enumeration type");
8446 expression
= error_mark_node
;
8450 /* Look for the closing `]'. */
8451 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8453 /* Add this bound to the declarator. */
8454 declarator
= make_array_declarator (declarator
, expression
);
8456 /* If the next token is not a `[', then there are no more
8458 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8465 /* Parse a new-initializer.
8468 ( expression-list [opt] )
8471 Returns a representation of the expression-list. */
8473 static vec
<tree
, va_gc
> *
8474 cp_parser_new_initializer (cp_parser
* parser
)
8476 vec
<tree
, va_gc
> *expression_list
;
8478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8481 bool expr_non_constant_p
;
8482 cp_lexer_set_source_position (parser
->lexer
);
8483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8484 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8485 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8486 expression_list
= make_tree_vector_single (t
);
8489 expression_list
= (cp_parser_parenthesized_expression_list
8490 (parser
, non_attr
, /*cast_p=*/false,
8491 /*allow_expansion_p=*/true,
8492 /*non_constant_p=*/NULL
));
8494 return expression_list
;
8497 /* Parse a delete-expression.
8500 :: [opt] delete cast-expression
8501 :: [opt] delete [ ] cast-expression
8503 Returns a representation of the expression. */
8506 cp_parser_delete_expression (cp_parser
* parser
)
8508 bool global_scope_p
;
8512 /* Look for the optional `::' operator. */
8514 = (cp_parser_global_scope_opt (parser
,
8515 /*current_scope_valid_p=*/false)
8517 /* Look for the `delete' keyword. */
8518 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8519 /* See if the array syntax is in use. */
8520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8522 /* Consume the `[' token. */
8523 cp_lexer_consume_token (parser
->lexer
);
8524 /* Look for the `]' token. */
8525 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8526 /* Remember that this is the `[]' construct. */
8532 /* Parse the cast-expression. */
8533 expression
= cp_parser_simple_cast_expression (parser
);
8535 /* A delete-expression may not appear in an integral constant
8537 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8538 return error_mark_node
;
8540 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8541 tf_warning_or_error
);
8544 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8545 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8549 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8552 switch (token
->type
)
8558 case CPP_CLOSE_SQUARE
:
8559 case CPP_CLOSE_PAREN
:
8560 case CPP_CLOSE_BRACE
:
8561 case CPP_OPEN_BRACE
:
8565 case CPP_DEREF_STAR
:
8573 case CPP_GREATER_EQ
:
8594 case CPP_OPEN_PAREN
:
8595 /* In ((type ()) () the last () isn't a valid cast-expression,
8596 so the whole must be parsed as postfix-expression. */
8597 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8600 case CPP_OPEN_SQUARE
:
8601 /* '[' may start a primary-expression in obj-c++ and in C++11,
8602 as a lambda-expression, eg, '(void)[]{}'. */
8603 if (cxx_dialect
>= cxx11
)
8605 return c_dialect_objc ();
8608 case CPP_MINUS_MINUS
:
8609 /* '++' and '--' may or may not start a cast-expression:
8611 struct T { void operator++(int); };
8612 void f() { (T())++; }
8625 /* Parse a cast-expression.
8629 ( type-id ) cast-expression
8631 ADDRESS_P is true iff the unary-expression is appearing as the
8632 operand of the `&' operator. CAST_P is true if this expression is
8633 the target of a cast.
8635 Returns a representation of the expression. */
8638 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8639 bool decltype_p
, cp_id_kind
* pidk
)
8641 /* If it's a `(', then we might be looking at a cast. */
8642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8644 tree type
= NULL_TREE
;
8645 cp_expr
expr (NULL_TREE
);
8646 int cast_expression
= 0;
8647 const char *saved_message
;
8649 /* There's no way to know yet whether or not this is a cast.
8650 For example, `(int (3))' is a unary-expression, while `(int)
8651 3' is a cast. So, we resort to parsing tentatively. */
8652 cp_parser_parse_tentatively (parser
);
8653 /* Types may not be defined in a cast. */
8654 saved_message
= parser
->type_definition_forbidden_message
;
8655 parser
->type_definition_forbidden_message
8656 = G_("types may not be defined in casts");
8657 /* Consume the `('. */
8658 cp_token
*open_paren
= cp_lexer_consume_token (parser
->lexer
);
8659 location_t open_paren_loc
= open_paren
->location
;
8661 /* A very tricky bit is that `(struct S) { 3 }' is a
8662 compound-literal (which we permit in C++ as an extension).
8663 But, that construct is not a cast-expression -- it is a
8664 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8665 is legal; if the compound-literal were a cast-expression,
8666 you'd need an extra set of parentheses.) But, if we parse
8667 the type-id, and it happens to be a class-specifier, then we
8668 will commit to the parse at that point, because we cannot
8669 undo the action that is done when creating a new class. So,
8670 then we cannot back up and do a postfix-expression.
8672 Another tricky case is the following (c++/29234):
8674 struct S { void operator () (); };
8681 As a type-id we parse the parenthesized S()() as a function
8682 returning a function, groktypename complains and we cannot
8683 back up in this case either.
8685 Therefore, we scan ahead to the closing `)', and check to see
8686 if the tokens after the `)' can start a cast-expression. Otherwise
8687 we are dealing with an unary-expression, a postfix-expression
8690 Yet another tricky case, in C++11, is the following (c++/54891):
8694 The issue is that usually, besides the case of lambda-expressions,
8695 the parenthesized type-id cannot be followed by '[', and, eg, we
8696 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8697 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8698 we don't commit, we try a cast-expression, then an unary-expression.
8700 Save tokens so that we can put them back. */
8701 cp_lexer_save_tokens (parser
->lexer
);
8703 /* We may be looking at a cast-expression. */
8704 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8705 /*consume_paren=*/true))
8707 = cp_parser_tokens_start_cast_expression (parser
);
8709 /* Roll back the tokens we skipped. */
8710 cp_lexer_rollback_tokens (parser
->lexer
);
8711 /* If we aren't looking at a cast-expression, simulate an error so
8712 that the call to cp_parser_error_occurred below returns true. */
8713 if (!cast_expression
)
8714 cp_parser_simulate_error (parser
);
8717 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8718 parser
->in_type_id_in_expr_p
= true;
8719 /* Look for the type-id. */
8720 type
= cp_parser_type_id (parser
);
8721 /* Look for the closing `)'. */
8722 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8723 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8726 /* Restore the saved message. */
8727 parser
->type_definition_forbidden_message
= saved_message
;
8729 /* At this point this can only be either a cast or a
8730 parenthesized ctor such as `(T ())' that looks like a cast to
8731 function returning T. */
8732 if (!cp_parser_error_occurred (parser
))
8734 /* Only commit if the cast-expression doesn't start with
8735 '++', '--', or '[' in C++11. */
8736 if (cast_expression
> 0)
8737 cp_parser_commit_to_topmost_tentative_parse (parser
);
8739 expr
= cp_parser_cast_expression (parser
,
8740 /*address_p=*/false,
8742 /*decltype_p=*/false,
8745 if (cp_parser_parse_definitely (parser
))
8747 /* Warn about old-style casts, if so requested. */
8748 if (warn_old_style_cast
8749 && !in_system_header_at (input_location
)
8750 && !VOID_TYPE_P (type
)
8751 && current_lang_name
!= lang_name_c
)
8752 warning (OPT_Wold_style_cast
, "use of old-style cast");
8754 /* Only type conversions to integral or enumeration types
8755 can be used in constant-expressions. */
8756 if (!cast_valid_in_integral_constant_expression_p (type
)
8757 && cp_parser_non_integral_constant_expression (parser
,
8759 return error_mark_node
;
8761 /* Perform the cast. */
8765 with start==caret at the open paren, extending to the
8767 location_t cast_loc
= make_location (open_paren_loc
,
8769 expr
.get_finish ());
8770 expr
= build_c_cast (cast_loc
, type
, expr
);
8775 cp_parser_abort_tentative_parse (parser
);
8778 /* If we get here, then it's not a cast, so it must be a
8779 unary-expression. */
8780 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8781 cast_p
, decltype_p
);
8784 /* Parse a binary expression of the general form:
8788 pm-expression .* cast-expression
8789 pm-expression ->* cast-expression
8791 multiplicative-expression:
8793 multiplicative-expression * pm-expression
8794 multiplicative-expression / pm-expression
8795 multiplicative-expression % pm-expression
8797 additive-expression:
8798 multiplicative-expression
8799 additive-expression + multiplicative-expression
8800 additive-expression - multiplicative-expression
8804 shift-expression << additive-expression
8805 shift-expression >> additive-expression
8807 relational-expression:
8809 relational-expression < shift-expression
8810 relational-expression > shift-expression
8811 relational-expression <= shift-expression
8812 relational-expression >= shift-expression
8816 relational-expression:
8817 relational-expression <? shift-expression
8818 relational-expression >? shift-expression
8820 equality-expression:
8821 relational-expression
8822 equality-expression == relational-expression
8823 equality-expression != relational-expression
8827 and-expression & equality-expression
8829 exclusive-or-expression:
8831 exclusive-or-expression ^ and-expression
8833 inclusive-or-expression:
8834 exclusive-or-expression
8835 inclusive-or-expression | exclusive-or-expression
8837 logical-and-expression:
8838 inclusive-or-expression
8839 logical-and-expression && inclusive-or-expression
8841 logical-or-expression:
8842 logical-and-expression
8843 logical-or-expression || logical-and-expression
8845 All these are implemented with a single function like:
8848 simple-cast-expression
8849 binary-expression <token> binary-expression
8851 CAST_P is true if this expression is the target of a cast.
8853 The binops_by_token map is used to get the tree codes for each <token> type.
8854 binary-expressions are associated according to a precedence table. */
8856 #define TOKEN_PRECEDENCE(token) \
8857 (((token->type == CPP_GREATER \
8858 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8859 && !parser->greater_than_is_operator_p) \
8860 ? PREC_NOT_OPERATOR \
8861 : binops_by_token[token->type].prec)
8864 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8865 bool no_toplevel_fold_p
,
8867 enum cp_parser_prec prec
,
8870 cp_parser_expression_stack stack
;
8871 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8872 cp_parser_expression_stack_entry current
;
8875 enum tree_code rhs_type
;
8876 enum cp_parser_prec new_prec
, lookahead_prec
;
8879 /* Parse the first expression. */
8880 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8881 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8882 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8883 cast_p
, decltype_p
, pidk
);
8884 current
.prec
= prec
;
8886 if (cp_parser_error_occurred (parser
))
8887 return error_mark_node
;
8891 /* Get an operator token. */
8892 token
= cp_lexer_peek_token (parser
->lexer
);
8894 if (warn_cxx11_compat
8895 && token
->type
== CPP_RSHIFT
8896 && !parser
->greater_than_is_operator_p
)
8898 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8899 "%<>>%> operator is treated"
8900 " as two right angle brackets in C++11"))
8901 inform (token
->location
,
8902 "suggest parentheses around %<>>%> expression");
8905 new_prec
= TOKEN_PRECEDENCE (token
);
8906 if (new_prec
!= PREC_NOT_OPERATOR
8907 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8908 /* This is a fold-expression; handle it later. */
8909 new_prec
= PREC_NOT_OPERATOR
;
8911 /* Popping an entry off the stack means we completed a subexpression:
8912 - either we found a token which is not an operator (`>' where it is not
8913 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8914 will happen repeatedly;
8915 - or, we found an operator which has lower priority. This is the case
8916 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8918 if (new_prec
<= current
.prec
)
8927 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8928 current
.loc
= token
->location
;
8930 /* We used the operator token. */
8931 cp_lexer_consume_token (parser
->lexer
);
8933 /* For "false && x" or "true || x", x will never be executed;
8934 disable warnings while evaluating it. */
8935 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8936 c_inhibit_evaluation_warnings
+=
8937 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8938 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8939 c_inhibit_evaluation_warnings
+=
8940 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8942 /* Extract another operand. It may be the RHS of this expression
8943 or the LHS of a new, higher priority expression. */
8944 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8945 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8946 rhs
= cp_parser_simple_cast_expression (parser
);
8948 /* Get another operator token. Look up its precedence to avoid
8949 building a useless (immediately popped) stack entry for common
8950 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8951 token
= cp_lexer_peek_token (parser
->lexer
);
8952 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8953 if (lookahead_prec
!= PREC_NOT_OPERATOR
8954 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8955 lookahead_prec
= PREC_NOT_OPERATOR
;
8956 if (lookahead_prec
> new_prec
)
8958 /* ... and prepare to parse the RHS of the new, higher priority
8959 expression. Since precedence levels on the stack are
8960 monotonically increasing, we do not have to care about
8965 current
.lhs_type
= rhs_type
;
8966 current
.prec
= new_prec
;
8967 new_prec
= lookahead_prec
;
8971 lookahead_prec
= new_prec
;
8972 /* If the stack is not empty, we have parsed into LHS the right side
8973 (`4' in the example above) of an expression we had suspended.
8974 We can use the information on the stack to recover the LHS (`3')
8975 from the stack together with the tree code (`MULT_EXPR'), and
8976 the precedence of the higher level subexpression
8977 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8978 which will be used to actually build the additive expression. */
8980 rhs_type
= current
.lhs_type
;
8985 /* Undo the disabling of warnings done above. */
8986 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8987 c_inhibit_evaluation_warnings
-=
8988 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8989 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8990 c_inhibit_evaluation_warnings
-=
8991 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8993 if (warn_logical_not_paren
8994 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8995 && current
.lhs_type
== TRUTH_NOT_EXPR
8996 /* Avoid warning for !!x == y. */
8997 && (TREE_CODE (current
.lhs
) != NE_EXPR
8998 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8999 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9000 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9001 /* Avoid warning for !b == y where b is boolean. */
9002 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9003 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9005 /* Avoid warning for !!b == y where b is boolean. */
9006 && (!DECL_P (current
.lhs
)
9007 || TREE_TYPE (current
.lhs
) == NULL_TREE
9008 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9009 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9010 current
.lhs
, maybe_constant_value (rhs
));
9014 location_t combined_loc
= make_location (current
.loc
,
9015 current
.lhs
.get_start (),
9018 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9019 ERROR_MARK for everything that is not a binary expression.
9020 This makes warn_about_parentheses miss some warnings that
9021 involve unary operators. For unary expressions we should
9022 pass the correct tree_code unless the unary expression was
9023 surrounded by parentheses.
9025 if (no_toplevel_fold_p
9026 && lookahead_prec
<= current
.prec
9028 current
.lhs
= build2_loc (combined_loc
,
9030 TREE_CODE_CLASS (current
.tree_type
)
9032 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9036 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9037 current
.lhs
, current
.lhs_type
,
9038 rhs
, rhs_type
, &overload
,
9039 complain_flags (decltype_p
));
9040 /* TODO: build_x_binary_op doesn't always honor the location. */
9041 current
.lhs
.set_location (combined_loc
);
9043 current
.lhs_type
= current
.tree_type
;
9045 /* If the binary operator required the use of an overloaded operator,
9046 then this expression cannot be an integral constant-expression.
9047 An overloaded operator can be used even if both operands are
9048 otherwise permissible in an integral constant-expression if at
9049 least one of the operands is of enumeration type. */
9052 && cp_parser_non_integral_constant_expression (parser
,
9054 return error_mark_node
;
9061 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9062 bool no_toplevel_fold_p
,
9063 enum cp_parser_prec prec
,
9066 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9067 /*decltype*/false, prec
, pidk
);
9070 /* Parse the `? expression : assignment-expression' part of a
9071 conditional-expression. The LOGICAL_OR_EXPR is the
9072 logical-or-expression that started the conditional-expression.
9073 Returns a representation of the entire conditional-expression.
9075 This routine is used by cp_parser_assignment_expression.
9077 ? expression : assignment-expression
9081 ? : assignment-expression */
9084 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9086 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9087 cp_expr assignment_expr
;
9088 struct cp_token
*token
;
9089 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9091 /* Consume the `?' token. */
9092 cp_lexer_consume_token (parser
->lexer
);
9093 token
= cp_lexer_peek_token (parser
->lexer
);
9094 if (cp_parser_allow_gnu_extensions_p (parser
)
9095 && token
->type
== CPP_COLON
)
9097 pedwarn (token
->location
, OPT_Wpedantic
,
9098 "ISO C++ does not allow ?: with omitted middle operand");
9099 /* Implicit true clause. */
9101 c_inhibit_evaluation_warnings
+=
9102 folded_logical_or_expr
== truthvalue_true_node
;
9103 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9107 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9108 parser
->colon_corrects_to_scope_p
= false;
9109 /* Parse the expression. */
9110 c_inhibit_evaluation_warnings
+=
9111 folded_logical_or_expr
== truthvalue_false_node
;
9112 expr
= cp_parser_expression (parser
);
9113 c_inhibit_evaluation_warnings
+=
9114 ((folded_logical_or_expr
== truthvalue_true_node
)
9115 - (folded_logical_or_expr
== truthvalue_false_node
));
9116 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9119 /* The next token should be a `:'. */
9120 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9121 /* Parse the assignment-expression. */
9122 assignment_expr
= cp_parser_assignment_expression (parser
);
9123 c_inhibit_evaluation_warnings
-=
9124 folded_logical_or_expr
== truthvalue_true_node
;
9127 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9128 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9129 with the caret at the "?", ranging from the start of
9130 the logical_or_expr to the end of the assignment_expr. */
9131 loc
= make_location (loc
,
9132 logical_or_expr
.get_start (),
9133 assignment_expr
.get_finish ());
9135 /* Build the conditional-expression. */
9136 return build_x_conditional_expr (loc
, logical_or_expr
,
9139 tf_warning_or_error
);
9142 /* Parse an assignment-expression.
9144 assignment-expression:
9145 conditional-expression
9146 logical-or-expression assignment-operator assignment_expression
9149 CAST_P is true if this expression is the target of a cast.
9150 DECLTYPE_P is true if this expression is the operand of decltype.
9152 Returns a representation for the expression. */
9155 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9156 bool cast_p
, bool decltype_p
)
9160 /* If the next token is the `throw' keyword, then we're looking at
9161 a throw-expression. */
9162 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9163 expr
= cp_parser_throw_expression (parser
);
9164 /* Otherwise, it must be that we are looking at a
9165 logical-or-expression. */
9168 /* Parse the binary expressions (logical-or-expression). */
9169 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9171 PREC_NOT_OPERATOR
, pidk
);
9172 /* If the next token is a `?' then we're actually looking at a
9173 conditional-expression. */
9174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9175 return cp_parser_question_colon_clause (parser
, expr
);
9178 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9180 /* If it's an assignment-operator, we're using the second
9182 enum tree_code assignment_operator
9183 = cp_parser_assignment_operator_opt (parser
);
9184 if (assignment_operator
!= ERROR_MARK
)
9186 bool non_constant_p
;
9188 /* Parse the right-hand side of the assignment. */
9189 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9192 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9193 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9195 /* An assignment may not appear in a
9196 constant-expression. */
9197 if (cp_parser_non_integral_constant_expression (parser
,
9199 return error_mark_node
;
9200 /* Build the assignment expression. Its default
9204 is the location of the '=' token as the
9205 caret, ranging from the start of the lhs to the
9207 loc
= make_location (loc
,
9210 expr
= build_x_modify_expr (loc
, expr
,
9211 assignment_operator
,
9213 complain_flags (decltype_p
));
9214 /* TODO: build_x_modify_expr doesn't honor the location,
9215 so we must set it here. */
9216 expr
.set_location (loc
);
9224 /* Parse an (optional) assignment-operator.
9226 assignment-operator: one of
9227 = *= /= %= += -= >>= <<= &= ^= |=
9231 assignment-operator: one of
9234 If the next token is an assignment operator, the corresponding tree
9235 code is returned, and the token is consumed. For example, for
9236 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9237 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9238 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9239 operator, ERROR_MARK is returned. */
9241 static enum tree_code
9242 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9247 /* Peek at the next token. */
9248 token
= cp_lexer_peek_token (parser
->lexer
);
9250 switch (token
->type
)
9261 op
= TRUNC_DIV_EXPR
;
9265 op
= TRUNC_MOD_EXPR
;
9297 /* Nothing else is an assignment operator. */
9301 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9302 if (op
!= ERROR_MARK
9303 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9306 /* If it was an assignment operator, consume it. */
9307 if (op
!= ERROR_MARK
)
9308 cp_lexer_consume_token (parser
->lexer
);
9313 /* Parse an expression.
9316 assignment-expression
9317 expression , assignment-expression
9319 CAST_P is true if this expression is the target of a cast.
9320 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9321 except possibly parenthesized or on the RHS of a comma (N3276).
9323 Returns a representation of the expression. */
9326 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9327 bool cast_p
, bool decltype_p
)
9329 cp_expr expression
= NULL_TREE
;
9330 location_t loc
= UNKNOWN_LOCATION
;
9334 cp_expr assignment_expression
;
9336 /* Parse the next assignment-expression. */
9337 assignment_expression
9338 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9340 /* We don't create a temporary for a call that is the immediate operand
9341 of decltype or on the RHS of a comma. But when we see a comma, we
9342 need to create a temporary for a call on the LHS. */
9343 if (decltype_p
&& !processing_template_decl
9344 && TREE_CODE (assignment_expression
) == CALL_EXPR
9345 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9346 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9347 assignment_expression
9348 = build_cplus_new (TREE_TYPE (assignment_expression
),
9349 assignment_expression
, tf_warning_or_error
);
9351 /* If this is the first assignment-expression, we can just
9354 expression
= assignment_expression
;
9357 /* Create a location with caret at the comma, ranging
9358 from the start of the LHS to the end of the RHS. */
9359 loc
= make_location (loc
,
9360 expression
.get_start (),
9361 assignment_expression
.get_finish ());
9362 expression
= build_x_compound_expr (loc
, expression
,
9363 assignment_expression
,
9364 complain_flags (decltype_p
));
9365 expression
.set_location (loc
);
9367 /* If the next token is not a comma, or we're in a fold-expression, then
9368 we are done with the expression. */
9369 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9370 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9372 /* Consume the `,'. */
9373 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9374 cp_lexer_consume_token (parser
->lexer
);
9375 /* A comma operator cannot appear in a constant-expression. */
9376 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9377 expression
= error_mark_node
;
9383 /* Parse a constant-expression.
9385 constant-expression:
9386 conditional-expression
9388 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9389 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9390 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9391 is false, NON_CONSTANT_P should be NULL. */
9394 cp_parser_constant_expression (cp_parser
* parser
,
9395 bool allow_non_constant_p
,
9396 bool *non_constant_p
)
9398 bool saved_integral_constant_expression_p
;
9399 bool saved_allow_non_integral_constant_expression_p
;
9400 bool saved_non_integral_constant_expression_p
;
9403 /* It might seem that we could simply parse the
9404 conditional-expression, and then check to see if it were
9405 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9406 one that the compiler can figure out is constant, possibly after
9407 doing some simplifications or optimizations. The standard has a
9408 precise definition of constant-expression, and we must honor
9409 that, even though it is somewhat more restrictive.
9415 is not a legal declaration, because `(2, 3)' is not a
9416 constant-expression. The `,' operator is forbidden in a
9417 constant-expression. However, GCC's constant-folding machinery
9418 will fold this operation to an INTEGER_CST for `3'. */
9420 /* Save the old settings. */
9421 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9422 saved_allow_non_integral_constant_expression_p
9423 = parser
->allow_non_integral_constant_expression_p
;
9424 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9425 /* We are now parsing a constant-expression. */
9426 parser
->integral_constant_expression_p
= true;
9427 parser
->allow_non_integral_constant_expression_p
9428 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9429 parser
->non_integral_constant_expression_p
= false;
9430 /* Although the grammar says "conditional-expression", we parse an
9431 "assignment-expression", which also permits "throw-expression"
9432 and the use of assignment operators. In the case that
9433 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9434 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9435 actually essential that we look for an assignment-expression.
9436 For example, cp_parser_initializer_clauses uses this function to
9437 determine whether a particular assignment-expression is in fact
9439 expression
= cp_parser_assignment_expression (parser
);
9440 /* Restore the old settings. */
9441 parser
->integral_constant_expression_p
9442 = saved_integral_constant_expression_p
;
9443 parser
->allow_non_integral_constant_expression_p
9444 = saved_allow_non_integral_constant_expression_p
;
9445 if (cxx_dialect
>= cxx11
)
9447 /* Require an rvalue constant expression here; that's what our
9448 callers expect. Reference constant expressions are handled
9449 separately in e.g. cp_parser_template_argument. */
9450 bool is_const
= potential_rvalue_constant_expression (expression
);
9451 parser
->non_integral_constant_expression_p
= !is_const
;
9452 if (!is_const
&& !allow_non_constant_p
)
9453 require_potential_rvalue_constant_expression (expression
);
9455 if (allow_non_constant_p
)
9456 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9457 parser
->non_integral_constant_expression_p
9458 = saved_non_integral_constant_expression_p
;
9463 /* Parse __builtin_offsetof.
9465 offsetof-expression:
9466 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9468 offsetof-member-designator:
9470 | offsetof-member-designator "." id-expression
9471 | offsetof-member-designator "[" expression "]"
9472 | offsetof-member-designator "->" id-expression */
9475 cp_parser_builtin_offsetof (cp_parser
*parser
)
9477 int save_ice_p
, save_non_ice_p
;
9482 location_t finish_loc
;
9484 /* We're about to accept non-integral-constant things, but will
9485 definitely yield an integral constant expression. Save and
9486 restore these values around our local parsing. */
9487 save_ice_p
= parser
->integral_constant_expression_p
;
9488 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9490 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9492 /* Consume the "__builtin_offsetof" token. */
9493 cp_lexer_consume_token (parser
->lexer
);
9494 /* Consume the opening `('. */
9495 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9496 /* Parse the type-id. */
9497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9498 type
= cp_parser_type_id (parser
);
9499 /* Look for the `,'. */
9500 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9501 token
= cp_lexer_peek_token (parser
->lexer
);
9503 /* Build the (type *)null that begins the traditional offsetof macro. */
9505 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9506 tf_warning_or_error
);
9508 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9509 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9510 true, &dummy
, token
->location
);
9513 token
= cp_lexer_peek_token (parser
->lexer
);
9514 switch (token
->type
)
9516 case CPP_OPEN_SQUARE
:
9517 /* offsetof-member-designator "[" expression "]" */
9518 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9523 /* offsetof-member-designator "->" identifier */
9524 expr
= grok_array_decl (token
->location
, expr
,
9525 integer_zero_node
, false);
9529 /* offsetof-member-designator "." identifier */
9530 cp_lexer_consume_token (parser
->lexer
);
9531 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9536 case CPP_CLOSE_PAREN
:
9537 /* Consume the ")" token. */
9538 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9539 cp_lexer_consume_token (parser
->lexer
);
9543 /* Error. We know the following require will fail, but
9544 that gives the proper error message. */
9545 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9546 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9547 expr
= error_mark_node
;
9553 /* Make a location of the form:
9554 __builtin_offsetof (struct s, f)
9555 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9556 with caret at the type-id, ranging from the start of the
9557 "_builtin_offsetof" token to the close paren. */
9558 loc
= make_location (loc
, start_loc
, finish_loc
);
9559 /* The result will be an INTEGER_CST, so we need to explicitly
9560 preserve the location. */
9561 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9564 parser
->integral_constant_expression_p
= save_ice_p
;
9565 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9570 /* Parse a trait expression.
9572 Returns a representation of the expression, the underlying type
9573 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9576 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9579 tree type1
, type2
= NULL_TREE
;
9580 bool binary
= false;
9581 bool variadic
= false;
9585 case RID_HAS_NOTHROW_ASSIGN
:
9586 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9588 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9589 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9591 case RID_HAS_NOTHROW_COPY
:
9592 kind
= CPTK_HAS_NOTHROW_COPY
;
9594 case RID_HAS_TRIVIAL_ASSIGN
:
9595 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9597 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9598 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9600 case RID_HAS_TRIVIAL_COPY
:
9601 kind
= CPTK_HAS_TRIVIAL_COPY
;
9603 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9604 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9606 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9607 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9609 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9610 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9612 case RID_IS_ABSTRACT
:
9613 kind
= CPTK_IS_ABSTRACT
;
9615 case RID_IS_AGGREGATE
:
9616 kind
= CPTK_IS_AGGREGATE
;
9618 case RID_IS_BASE_OF
:
9619 kind
= CPTK_IS_BASE_OF
;
9623 kind
= CPTK_IS_CLASS
;
9626 kind
= CPTK_IS_EMPTY
;
9629 kind
= CPTK_IS_ENUM
;
9632 kind
= CPTK_IS_FINAL
;
9634 case RID_IS_LITERAL_TYPE
:
9635 kind
= CPTK_IS_LITERAL_TYPE
;
9640 case RID_IS_POLYMORPHIC
:
9641 kind
= CPTK_IS_POLYMORPHIC
;
9643 case RID_IS_SAME_AS
:
9644 kind
= CPTK_IS_SAME_AS
;
9647 case RID_IS_STD_LAYOUT
:
9648 kind
= CPTK_IS_STD_LAYOUT
;
9650 case RID_IS_TRIVIAL
:
9651 kind
= CPTK_IS_TRIVIAL
;
9653 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9654 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9657 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9658 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9661 case RID_IS_TRIVIALLY_COPYABLE
:
9662 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9665 kind
= CPTK_IS_UNION
;
9667 case RID_UNDERLYING_TYPE
:
9668 kind
= CPTK_UNDERLYING_TYPE
;
9673 case RID_DIRECT_BASES
:
9674 kind
= CPTK_DIRECT_BASES
;
9680 /* Consume the token. */
9681 cp_lexer_consume_token (parser
->lexer
);
9683 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9686 type_id_in_expr_sentinel
s (parser
);
9687 type1
= cp_parser_type_id (parser
);
9690 if (type1
== error_mark_node
)
9691 return error_mark_node
;
9695 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9698 type_id_in_expr_sentinel
s (parser
);
9699 type2
= cp_parser_type_id (parser
);
9702 if (type2
== error_mark_node
)
9703 return error_mark_node
;
9707 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9709 cp_lexer_consume_token (parser
->lexer
);
9710 tree elt
= cp_parser_type_id (parser
);
9711 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9713 cp_lexer_consume_token (parser
->lexer
);
9714 elt
= make_pack_expansion (elt
);
9716 if (elt
== error_mark_node
)
9717 return error_mark_node
;
9718 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9722 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9724 /* Complete the trait expression, which may mean either processing
9725 the trait expr now or saving it for template instantiation. */
9728 case CPTK_UNDERLYING_TYPE
:
9729 return finish_underlying_type (type1
);
9731 return finish_bases (type1
, false);
9732 case CPTK_DIRECT_BASES
:
9733 return finish_bases (type1
, true);
9735 return finish_trait_expr (kind
, type1
, type2
);
9739 /* Lambdas that appear in variable initializer or default argument scope
9740 get that in their mangling, so we need to record it. We might as well
9741 use the count for function and namespace scopes as well. */
9742 static GTY(()) tree lambda_scope
;
9743 static GTY(()) int lambda_count
;
9744 struct GTY(()) tree_int
9749 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9752 start_lambda_scope (tree decl
)
9756 /* Once we're inside a function, we ignore other scopes and just push
9757 the function again so that popping works properly. */
9758 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9759 decl
= current_function_decl
;
9760 ti
.t
= lambda_scope
;
9761 ti
.i
= lambda_count
;
9762 vec_safe_push (lambda_scope_stack
, ti
);
9763 if (lambda_scope
!= decl
)
9765 /* Don't reset the count if we're still in the same function. */
9766 lambda_scope
= decl
;
9772 record_lambda_scope (tree lambda
)
9774 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9775 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9779 finish_lambda_scope (void)
9781 tree_int
*p
= &lambda_scope_stack
->last ();
9782 if (lambda_scope
!= p
->t
)
9784 lambda_scope
= p
->t
;
9785 lambda_count
= p
->i
;
9787 lambda_scope_stack
->pop ();
9790 /* Parse a lambda expression.
9793 lambda-introducer lambda-declarator [opt] compound-statement
9795 Returns a representation of the expression. */
9798 cp_parser_lambda_expression (cp_parser
* parser
)
9800 tree lambda_expr
= build_lambda_expr ();
9803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9804 cp_token_position start
= 0;
9806 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9808 if (cp_unevaluated_operand
)
9810 if (!token
->error_reported
)
9812 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9813 "lambda-expression in unevaluated context");
9814 token
->error_reported
= true;
9818 else if (parser
->in_template_argument_list_p
)
9820 if (!token
->error_reported
)
9822 error_at (token
->location
, "lambda-expression in template-argument");
9823 token
->error_reported
= true;
9828 /* We may be in the middle of deferred access check. Disable
9830 push_deferring_access_checks (dk_no_deferred
);
9832 cp_parser_lambda_introducer (parser
, lambda_expr
);
9834 type
= begin_lambda_type (lambda_expr
);
9835 if (type
== error_mark_node
)
9836 return error_mark_node
;
9838 record_lambda_scope (lambda_expr
);
9840 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9841 determine_visibility (TYPE_NAME (type
));
9843 /* Now that we've started the type, add the capture fields for any
9844 explicit captures. */
9845 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9848 /* Inside the class, surrounding template-parameter-lists do not apply. */
9849 unsigned int saved_num_template_parameter_lists
9850 = parser
->num_template_parameter_lists
;
9851 unsigned char in_statement
= parser
->in_statement
;
9852 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9853 bool fully_implicit_function_template_p
9854 = parser
->fully_implicit_function_template_p
;
9855 tree implicit_template_parms
= parser
->implicit_template_parms
;
9856 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9857 bool auto_is_implicit_function_template_parm_p
9858 = parser
->auto_is_implicit_function_template_parm_p
;
9860 parser
->num_template_parameter_lists
= 0;
9861 parser
->in_statement
= 0;
9862 parser
->in_switch_statement_p
= false;
9863 parser
->fully_implicit_function_template_p
= false;
9864 parser
->implicit_template_parms
= 0;
9865 parser
->implicit_template_scope
= 0;
9866 parser
->auto_is_implicit_function_template_parm_p
= false;
9868 /* By virtue of defining a local class, a lambda expression has access to
9869 the private variables of enclosing classes. */
9871 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9873 if (ok
&& cp_parser_error_occurred (parser
))
9878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9879 && cp_parser_start_tentative_firewall (parser
))
9881 cp_parser_lambda_body (parser
, lambda_expr
);
9883 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9885 if (cp_parser_skip_to_closing_brace (parser
))
9886 cp_lexer_consume_token (parser
->lexer
);
9889 /* The capture list was built up in reverse order; fix that now. */
9890 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9891 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9894 maybe_add_lambda_conv_op (type
);
9896 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9898 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9899 parser
->in_statement
= in_statement
;
9900 parser
->in_switch_statement_p
= in_switch_statement_p
;
9901 parser
->fully_implicit_function_template_p
9902 = fully_implicit_function_template_p
;
9903 parser
->implicit_template_parms
= implicit_template_parms
;
9904 parser
->implicit_template_scope
= implicit_template_scope
;
9905 parser
->auto_is_implicit_function_template_parm_p
9906 = auto_is_implicit_function_template_parm_p
;
9909 /* This field is only used during parsing of the lambda. */
9910 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9912 /* This lambda shouldn't have any proxies left at this point. */
9913 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9914 /* And now that we're done, push proxies for an enclosing lambda. */
9915 insert_pending_capture_proxies ();
9918 lambda_expr
= build_lambda_object (lambda_expr
);
9920 lambda_expr
= error_mark_node
;
9922 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9924 pop_deferring_access_checks ();
9929 /* Parse the beginning of a lambda expression.
9932 [ lambda-capture [opt] ]
9934 LAMBDA_EXPR is the current representation of the lambda expression. */
9937 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9939 /* Need commas after the first capture. */
9942 /* Eat the leading `['. */
9943 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9945 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9947 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9948 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9949 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9950 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9952 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9954 cp_lexer_consume_token (parser
->lexer
);
9958 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9960 cp_token
* capture_token
;
9962 tree capture_init_expr
;
9963 cp_id_kind idk
= CP_ID_KIND_NONE
;
9964 bool explicit_init_p
= false;
9966 enum capture_kind_type
9971 enum capture_kind_type capture_kind
= BY_COPY
;
9973 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9975 error ("expected end of capture-list");
9982 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9984 /* Possibly capture `this'. */
9985 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9987 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9988 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9989 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9990 "with by-copy capture default");
9991 cp_lexer_consume_token (parser
->lexer
);
9992 add_capture (lambda_expr
,
9993 /*id=*/this_identifier
,
9994 /*initializer=*/finish_this_expr (),
9995 /*by_reference_p=*/true,
10000 /* Possibly capture `*this'. */
10001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10002 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10004 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10005 if (cxx_dialect
< cxx1z
)
10006 pedwarn (loc
, 0, "%<*this%> capture only available with "
10007 "-std=c++1z or -std=gnu++1z");
10008 cp_lexer_consume_token (parser
->lexer
);
10009 cp_lexer_consume_token (parser
->lexer
);
10010 add_capture (lambda_expr
,
10011 /*id=*/this_identifier
,
10012 /*initializer=*/finish_this_expr (),
10013 /*by_reference_p=*/false,
10018 /* Remember whether we want to capture as a reference or not. */
10019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10021 capture_kind
= BY_REFERENCE
;
10022 cp_lexer_consume_token (parser
->lexer
);
10025 /* Get the identifier. */
10026 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10027 capture_id
= cp_parser_identifier (parser
);
10029 if (capture_id
== error_mark_node
)
10030 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10031 delimiters, but I modified this to stop on unnested ']' as well. It
10032 was already changed to stop on unnested '}', so the
10033 "closing_parenthesis" name is no more misleading with my change. */
10035 cp_parser_skip_to_closing_parenthesis (parser
,
10036 /*recovering=*/true,
10038 /*consume_paren=*/true);
10042 /* Find the initializer for this capture. */
10043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10044 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10045 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10047 bool direct
, non_constant
;
10048 /* An explicit initializer exists. */
10049 if (cxx_dialect
< cxx14
)
10050 pedwarn (input_location
, 0,
10051 "lambda capture initializers "
10052 "only available with -std=c++14 or -std=gnu++14");
10053 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10055 explicit_init_p
= true;
10056 if (capture_init_expr
== NULL_TREE
)
10058 error ("empty initializer for lambda init-capture");
10059 capture_init_expr
= error_mark_node
;
10064 const char* error_msg
;
10066 /* Turn the identifier into an id-expression. */
10068 = cp_parser_lookup_name_simple (parser
, capture_id
,
10069 capture_token
->location
);
10071 if (capture_init_expr
== error_mark_node
)
10073 unqualified_name_lookup_error (capture_id
);
10076 else if (DECL_P (capture_init_expr
)
10077 && (!VAR_P (capture_init_expr
)
10078 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10080 error_at (capture_token
->location
,
10081 "capture of non-variable %qD ",
10082 capture_init_expr
);
10083 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10084 "%q#D declared here", capture_init_expr
);
10087 if (VAR_P (capture_init_expr
)
10088 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10090 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10091 "%qD with non-automatic storage duration",
10092 capture_init_expr
))
10093 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10094 "%q#D declared here", capture_init_expr
);
10099 = finish_id_expression
10104 /*integral_constant_expression_p=*/false,
10105 /*allow_non_integral_constant_expression_p=*/false,
10106 /*non_integral_constant_expression_p=*/NULL
,
10107 /*template_p=*/false,
10109 /*address_p=*/false,
10110 /*template_arg_p=*/false,
10112 capture_token
->location
);
10114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10116 cp_lexer_consume_token (parser
->lexer
);
10117 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10120 check_for_bare_parameter_packs (capture_init_expr
);
10123 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10124 && !explicit_init_p
)
10126 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10127 && capture_kind
== BY_COPY
)
10128 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10129 "of %qD redundant with by-copy capture default",
10131 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10132 && capture_kind
== BY_REFERENCE
)
10133 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10134 "capture of %qD redundant with by-reference capture "
10135 "default", capture_id
);
10138 add_capture (lambda_expr
,
10141 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10145 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10148 /* Parse the (optional) middle of a lambda expression.
10151 < template-parameter-list [opt] >
10152 ( parameter-declaration-clause [opt] )
10153 attribute-specifier [opt]
10154 decl-specifier-seq [opt]
10155 exception-specification [opt]
10156 lambda-return-type-clause [opt]
10158 LAMBDA_EXPR is the current representation of the lambda expression. */
10161 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10163 /* 5.1.1.4 of the standard says:
10164 If a lambda-expression does not include a lambda-declarator, it is as if
10165 the lambda-declarator were ().
10166 This means an empty parameter list, no attributes, and no exception
10168 tree param_list
= void_list_node
;
10169 tree attributes
= NULL_TREE
;
10170 tree exception_spec
= NULL_TREE
;
10171 tree template_param_list
= NULL_TREE
;
10172 tree tx_qual
= NULL_TREE
;
10173 cp_decl_specifier_seq lambda_specs
;
10174 clear_decl_specs (&lambda_specs
);
10176 /* The template-parameter-list is optional, but must begin with
10177 an opening angle if present. */
10178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10180 if (cxx_dialect
< cxx14
)
10181 pedwarn (parser
->lexer
->next_token
->location
, 0,
10182 "lambda templates are only available with "
10183 "-std=c++14 or -std=gnu++14");
10185 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10186 "ISO C++ does not support lambda templates");
10188 cp_lexer_consume_token (parser
->lexer
);
10190 template_param_list
= cp_parser_template_parameter_list (parser
);
10192 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10194 /* We just processed one more parameter list. */
10195 ++parser
->num_template_parameter_lists
;
10198 /* The parameter-declaration-clause is optional (unless
10199 template-parameter-list was given), but must begin with an
10200 opening parenthesis if present. */
10201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10203 cp_lexer_consume_token (parser
->lexer
);
10205 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10207 /* Parse parameters. */
10208 param_list
= cp_parser_parameter_declaration_clause (parser
);
10210 /* Default arguments shall not be specified in the
10211 parameter-declaration-clause of a lambda-declarator. */
10212 if (cxx_dialect
< cxx14
)
10213 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10214 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10215 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10216 "default argument specified for lambda parameter");
10218 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10220 attributes
= cp_parser_attributes_opt (parser
);
10222 /* In the decl-specifier-seq of the lambda-declarator, each
10223 decl-specifier shall either be mutable or constexpr. */
10224 int declares_class_or_enum
;
10225 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10226 cp_parser_decl_specifier_seq (parser
,
10227 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10228 &lambda_specs
, &declares_class_or_enum
);
10229 if (lambda_specs
.storage_class
== sc_mutable
)
10231 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10232 if (lambda_specs
.conflicting_specifiers_p
)
10233 error_at (lambda_specs
.locations
[ds_storage_class
],
10234 "duplicate %<mutable%>");
10237 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10239 /* Parse optional exception specification. */
10240 exception_spec
= cp_parser_exception_specification_opt (parser
);
10242 /* Parse optional trailing return type. */
10243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10245 cp_lexer_consume_token (parser
->lexer
);
10246 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10247 = cp_parser_trailing_type_id (parser
);
10250 /* The function parameters must be in scope all the way until after the
10251 trailing-return-type in case of decltype. */
10252 pop_bindings_and_leave_scope ();
10254 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10255 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10257 /* Create the function call operator.
10259 Messing with declarators like this is no uglier than building up the
10260 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10263 cp_decl_specifier_seq return_type_specs
;
10264 cp_declarator
* declarator
;
10269 clear_decl_specs (&return_type_specs
);
10270 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10271 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
10273 /* Maybe we will deduce the return type later. */
10274 return_type_specs
.type
= make_auto ();
10276 if (lambda_specs
.locations
[ds_constexpr
])
10278 if (cxx_dialect
>= cxx1z
)
10279 return_type_specs
.locations
[ds_constexpr
]
10280 = lambda_specs
.locations
[ds_constexpr
];
10282 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10283 "lambda only available with -std=c++1z or -std=gnu++1z");
10286 p
= obstack_alloc (&declarator_obstack
, 0);
10288 declarator
= make_id_declarator (NULL_TREE
, cp_operator_id (CALL_EXPR
),
10291 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10292 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10293 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10294 VIRT_SPEC_UNSPECIFIED
,
10298 /*late_return_type=*/NULL_TREE
,
10299 /*requires_clause*/NULL_TREE
);
10300 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10302 fco
= grokmethod (&return_type_specs
,
10305 if (fco
!= error_mark_node
)
10307 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10308 DECL_ARTIFICIAL (fco
) = 1;
10309 /* Give the object parameter a different name. */
10310 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10311 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10312 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10314 if (template_param_list
)
10316 fco
= finish_member_template_decl (fco
);
10317 finish_template_decl (template_param_list
);
10318 --parser
->num_template_parameter_lists
;
10320 else if (parser
->fully_implicit_function_template_p
)
10321 fco
= finish_fully_implicit_template (parser
, fco
);
10323 finish_member_declaration (fco
);
10325 obstack_free (&declarator_obstack
, p
);
10327 return (fco
!= error_mark_node
);
10331 /* Parse the body of a lambda expression, which is simply
10335 but which requires special handling.
10336 LAMBDA_EXPR is the current representation of the lambda expression. */
10339 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10341 bool nested
= (current_function_decl
!= NULL_TREE
);
10342 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10344 push_function_context ();
10346 /* Still increment function_depth so that we don't GC in the
10347 middle of an expression. */
10349 vec
<tree
> omp_privatization_save
;
10350 save_omp_privatization_clauses (omp_privatization_save
);
10351 /* Clear this in case we're in the middle of a default argument. */
10352 parser
->local_variables_forbidden_p
= false;
10354 /* Finish the function call operator
10356 + late_parsing_for_member
10357 + function_definition_after_declarator
10358 + ctor_initializer_opt_and_function_body */
10360 tree fco
= lambda_function (lambda_expr
);
10363 tree compound_stmt
;
10366 /* Let the front end know that we are going to be defining this
10368 start_preparsed_function (fco
,
10370 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
10372 start_lambda_scope (fco
);
10373 body
= begin_function_body ();
10375 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10378 /* Push the proxies for any explicit captures. */
10379 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
10380 cap
= TREE_CHAIN (cap
))
10381 build_capture_proxy (TREE_PURPOSE (cap
));
10383 compound_stmt
= begin_compound_stmt (0);
10385 /* 5.1.1.4 of the standard says:
10386 If a lambda-expression does not include a trailing-return-type, it
10387 is as if the trailing-return-type denotes the following type:
10388 * if the compound-statement is of the form
10389 { return attribute-specifier [opt] expression ; }
10390 the type of the returned expression after lvalue-to-rvalue
10391 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10392 (_conv.array_ 4.2), and function-to-pointer conversion
10394 * otherwise, void. */
10396 /* In a lambda that has neither a lambda-return-type-clause
10397 nor a deducible form, errors should be reported for return statements
10398 in the body. Since we used void as the placeholder return type, parsing
10399 the body as usual will give such desired behavior. */
10400 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10401 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10402 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10404 tree expr
= NULL_TREE
;
10405 cp_id_kind idk
= CP_ID_KIND_NONE
;
10407 /* Parse tentatively in case there's more after the initial return
10409 cp_parser_parse_tentatively (parser
);
10411 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10413 expr
= cp_parser_expression (parser
, &idk
);
10415 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10416 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10418 if (cp_parser_parse_definitely (parser
))
10420 if (!processing_template_decl
)
10422 tree type
= lambda_return_type (expr
);
10423 apply_deduced_return_type (fco
, type
);
10424 if (type
== error_mark_node
)
10425 expr
= error_mark_node
;
10428 /* Will get error here if type not deduced yet. */
10429 finish_return_stmt (expr
);
10437 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10438 cp_parser_label_declaration (parser
);
10439 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10440 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10443 finish_compound_stmt (compound_stmt
);
10446 finish_function_body (body
);
10447 finish_lambda_scope ();
10449 /* Finish the function and generate code for it if necessary. */
10450 tree fn
= finish_function (/*inline*/2);
10452 /* Only expand if the call op is not a template. */
10453 if (!DECL_TEMPLATE_INFO (fco
))
10454 expand_or_defer_fn (fn
);
10457 restore_omp_privatization_clauses (omp_privatization_save
);
10458 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10460 pop_function_context();
10465 /* Statements [gram.stmt.stmt] */
10467 /* Parse a statement.
10471 expression-statement
10473 selection-statement
10474 iteration-statement
10476 declaration-statement
10483 attribute-specifier-seq (opt) expression-statement
10484 attribute-specifier-seq (opt) compound-statement
10485 attribute-specifier-seq (opt) selection-statement
10486 attribute-specifier-seq (opt) iteration-statement
10487 attribute-specifier-seq (opt) jump-statement
10488 declaration-statement
10489 attribute-specifier-seq (opt) try-block
10492 expression-statement
10500 IN_COMPOUND is true when the statement is nested inside a
10501 cp_parser_compound_statement; this matters for certain pragmas.
10503 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10504 is a (possibly labeled) if statement which is not enclosed in braces
10505 and has an else clause. This is used to implement -Wparentheses.
10507 CHAIN is a vector of if-else-if conditions. */
10510 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10511 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
10513 tree statement
, std_attrs
= NULL_TREE
;
10515 location_t statement_location
, attrs_location
;
10520 /* There is no statement yet. */
10521 statement
= NULL_TREE
;
10523 saved_token_sentinel
saved_tokens (parser
->lexer
);
10524 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10525 if (c_dialect_objc ())
10526 /* In obj-c++, seeing '[[' might be the either the beginning of
10527 c++11 attributes, or a nested objc-message-expression. So
10528 let's parse the c++11 attributes tentatively. */
10529 cp_parser_parse_tentatively (parser
);
10530 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10531 if (c_dialect_objc ())
10533 if (!cp_parser_parse_definitely (parser
))
10534 std_attrs
= NULL_TREE
;
10537 /* Peek at the next token. */
10538 token
= cp_lexer_peek_token (parser
->lexer
);
10539 /* Remember the location of the first token in the statement. */
10540 statement_location
= token
->location
;
10541 /* If this is a keyword, then that will often determine what kind of
10542 statement we have. */
10543 if (token
->type
== CPP_KEYWORD
)
10545 enum rid keyword
= token
->keyword
;
10551 /* Looks like a labeled-statement with a case label.
10552 Parse the label, and then use tail recursion to parse
10554 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10555 in_compound
= false;
10560 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10566 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10570 if (!flag_cilkplus
)
10572 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10573 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10574 cp_lexer_consume_token (parser
->lexer
);
10575 statement
= error_mark_node
;
10578 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10585 statement
= cp_parser_jump_statement (parser
);
10588 case RID_CILK_SYNC
:
10589 cp_lexer_consume_token (parser
->lexer
);
10592 tree sync_expr
= build_cilk_sync ();
10593 SET_EXPR_LOCATION (sync_expr
,
10595 statement
= finish_expr_stmt (sync_expr
);
10599 error_at (token
->location
, "-fcilkplus must be enabled to use"
10600 " %<_Cilk_sync%>");
10601 statement
= error_mark_node
;
10603 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10606 /* Objective-C++ exception-handling constructs. */
10609 case RID_AT_FINALLY
:
10610 case RID_AT_SYNCHRONIZED
:
10612 statement
= cp_parser_objc_statement (parser
);
10616 statement
= cp_parser_try_block (parser
);
10619 case RID_NAMESPACE
:
10620 /* This must be a namespace alias definition. */
10621 cp_parser_declaration_statement (parser
);
10624 case RID_TRANSACTION_ATOMIC
:
10625 case RID_TRANSACTION_RELAXED
:
10626 case RID_SYNCHRONIZED
:
10627 case RID_ATOMIC_NOEXCEPT
:
10628 case RID_ATOMIC_CANCEL
:
10629 statement
= cp_parser_transaction (parser
, token
);
10631 case RID_TRANSACTION_CANCEL
:
10632 statement
= cp_parser_transaction_cancel (parser
);
10636 /* It might be a keyword like `int' that can start a
10637 declaration-statement. */
10641 else if (token
->type
== CPP_NAME
)
10643 /* If the next token is a `:', then we are looking at a
10644 labeled-statement. */
10645 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10646 if (token
->type
== CPP_COLON
)
10648 /* Looks like a labeled-statement with an ordinary label.
10649 Parse the label, and then use tail recursion to parse
10652 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10653 in_compound
= false;
10657 /* Anything that starts with a `{' must be a compound-statement. */
10658 else if (token
->type
== CPP_OPEN_BRACE
)
10659 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10660 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10661 a statement all its own. */
10662 else if (token
->type
== CPP_PRAGMA
)
10664 /* Only certain OpenMP pragmas are attached to statements, and thus
10665 are considered statements themselves. All others are not. In
10666 the context of a compound, accept the pragma as a "statement" and
10667 return so that we can check for a close brace. Otherwise we
10668 require a real statement and must go back and read one. */
10670 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10671 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10675 else if (token
->type
== CPP_EOF
)
10677 cp_parser_error (parser
, "expected statement");
10681 /* Everything else must be a declaration-statement or an
10682 expression-statement. Try for the declaration-statement
10683 first, unless we are looking at a `;', in which case we know that
10684 we have an expression-statement. */
10687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10689 if (std_attrs
!= NULL_TREE
)
10691 /* Attributes should be parsed as part of the the
10692 declaration, so let's un-parse them. */
10693 saved_tokens
.rollback();
10694 std_attrs
= NULL_TREE
;
10697 cp_parser_parse_tentatively (parser
);
10698 /* Try to parse the declaration-statement. */
10699 cp_parser_declaration_statement (parser
);
10700 /* If that worked, we're done. */
10701 if (cp_parser_parse_definitely (parser
))
10704 /* Look for an expression-statement instead. */
10705 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10707 /* Handle [[fallthrough]];. */
10708 if (attribute_fallthrough_p (std_attrs
))
10710 /* The next token after the fallthrough attribute is ';'. */
10711 if (statement
== NULL_TREE
)
10713 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10714 statement
= build_call_expr_internal_loc (statement_location
,
10716 void_type_node
, 0);
10717 finish_expr_stmt (statement
);
10720 warning_at (statement_location
, OPT_Wattributes
,
10721 "%<fallthrough%> attribute not followed by %<;%>");
10722 std_attrs
= NULL_TREE
;
10726 /* Set the line number for the statement. */
10727 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10728 SET_EXPR_LOCATION (statement
, statement_location
);
10730 /* Allow "[[fallthrough]];", but warn otherwise. */
10731 if (std_attrs
!= NULL_TREE
)
10732 warning_at (attrs_location
,
10734 "attributes at the beginning of statement are ignored");
10737 /* Parse the label for a labeled-statement, i.e.
10740 case constant-expression :
10744 case constant-expression ... constant-expression : statement
10746 When a label is parsed without errors, the label is added to the
10747 parse tree by the finish_* functions, so this function doesn't
10748 have to return the label. */
10751 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10754 tree label
= NULL_TREE
;
10755 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10757 /* The next token should be an identifier. */
10758 token
= cp_lexer_peek_token (parser
->lexer
);
10759 if (token
->type
!= CPP_NAME
10760 && token
->type
!= CPP_KEYWORD
)
10762 cp_parser_error (parser
, "expected labeled-statement");
10766 /* Remember whether this case or a user-defined label is allowed to fall
10768 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10770 parser
->colon_corrects_to_scope_p
= false;
10771 switch (token
->keyword
)
10775 tree expr
, expr_hi
;
10776 cp_token
*ellipsis
;
10778 /* Consume the `case' token. */
10779 cp_lexer_consume_token (parser
->lexer
);
10780 /* Parse the constant-expression. */
10781 expr
= cp_parser_constant_expression (parser
);
10782 if (check_for_bare_parameter_packs (expr
))
10783 expr
= error_mark_node
;
10785 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10786 if (ellipsis
->type
== CPP_ELLIPSIS
)
10788 /* Consume the `...' token. */
10789 cp_lexer_consume_token (parser
->lexer
);
10790 expr_hi
= cp_parser_constant_expression (parser
);
10791 if (check_for_bare_parameter_packs (expr_hi
))
10792 expr_hi
= error_mark_node
;
10794 /* We don't need to emit warnings here, as the common code
10795 will do this for us. */
10798 expr_hi
= NULL_TREE
;
10800 if (parser
->in_switch_statement_p
)
10802 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10803 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10804 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10807 error_at (token
->location
,
10808 "case label %qE not within a switch statement",
10814 /* Consume the `default' token. */
10815 cp_lexer_consume_token (parser
->lexer
);
10817 if (parser
->in_switch_statement_p
)
10819 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10820 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10821 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10824 error_at (token
->location
, "case label not within a switch statement");
10828 /* Anything else must be an ordinary label. */
10829 label
= finish_label_stmt (cp_parser_identifier (parser
));
10830 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
10831 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
10835 /* Require the `:' token. */
10836 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10838 /* An ordinary label may optionally be followed by attributes.
10839 However, this is only permitted if the attributes are then
10840 followed by a semicolon. This is because, for backward
10841 compatibility, when parsing
10842 lab: __attribute__ ((unused)) int i;
10843 we want the attribute to attach to "i", not "lab". */
10844 if (label
!= NULL_TREE
10845 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10848 cp_parser_parse_tentatively (parser
);
10849 attrs
= cp_parser_gnu_attributes_opt (parser
);
10850 if (attrs
== NULL_TREE
10851 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10852 cp_parser_abort_tentative_parse (parser
);
10853 else if (!cp_parser_parse_definitely (parser
))
10856 attributes
= chainon (attributes
, attrs
);
10859 if (attributes
!= NULL_TREE
)
10860 cplus_decl_attributes (&label
, attributes
, 0);
10862 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10865 /* Parse an expression-statement.
10867 expression-statement:
10870 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10871 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10872 indicates whether this expression-statement is part of an
10873 expression statement. */
10876 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10878 tree statement
= NULL_TREE
;
10879 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10880 location_t loc
= token
->location
;
10882 /* There might be attribute fallthrough. */
10883 tree attr
= cp_parser_gnu_attributes_opt (parser
);
10885 /* If the next token is a ';', then there is no expression
10887 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10889 statement
= cp_parser_expression (parser
);
10890 if (statement
== error_mark_node
10891 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10893 cp_parser_skip_to_end_of_block_or_statement (parser
);
10894 return error_mark_node
;
10898 /* Handle [[fallthrough]];. */
10899 if (attribute_fallthrough_p (attr
))
10901 /* The next token after the fallthrough attribute is ';'. */
10902 if (statement
== NULL_TREE
)
10903 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10904 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
10905 void_type_node
, 0);
10907 warning_at (loc
, OPT_Wattributes
,
10908 "%<fallthrough%> attribute not followed by %<;%>");
10912 /* Allow "[[fallthrough]];", but warn otherwise. */
10913 if (attr
!= NULL_TREE
)
10914 warning_at (loc
, OPT_Wattributes
,
10915 "attributes at the beginning of statement are ignored");
10917 /* Give a helpful message for "A<T>::type t;" and the like. */
10918 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10919 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10921 if (TREE_CODE (statement
) == SCOPE_REF
)
10922 error_at (token
->location
, "need %<typename%> before %qE because "
10923 "%qT is a dependent scope",
10924 statement
, TREE_OPERAND (statement
, 0));
10925 else if (is_overloaded_fn (statement
)
10926 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10929 tree fn
= get_first_fn (statement
);
10930 error_at (token
->location
,
10931 "%<%T::%D%> names the constructor, not the type",
10932 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10936 /* Consume the final `;'. */
10937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10939 if (in_statement_expr
10940 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10941 /* This is the final expression statement of a statement
10943 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10944 else if (statement
)
10945 statement
= finish_expr_stmt (statement
);
10950 /* Parse a compound-statement.
10952 compound-statement:
10953 { statement-seq [opt] }
10957 compound-statement:
10958 { label-declaration-seq [opt] statement-seq [opt] }
10960 label-declaration-seq:
10962 label-declaration-seq label-declaration
10964 Returns a tree representing the statement. */
10967 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10968 int bcs_flags
, bool function_body
)
10970 tree compound_stmt
;
10972 /* Consume the `{'. */
10973 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10974 return error_mark_node
;
10975 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10976 && !function_body
&& cxx_dialect
< cxx14
)
10977 pedwarn (input_location
, OPT_Wpedantic
,
10978 "compound-statement in constexpr function");
10979 /* Begin the compound-statement. */
10980 compound_stmt
= begin_compound_stmt (bcs_flags
);
10981 /* If the next keyword is `__label__' we have a label declaration. */
10982 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10983 cp_parser_label_declaration (parser
);
10984 /* Parse an (optional) statement-seq. */
10985 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10986 /* Finish the compound-statement. */
10987 finish_compound_stmt (compound_stmt
);
10988 /* Consume the `}'. */
10989 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10991 return compound_stmt
;
10994 /* Parse an (optional) statement-seq.
10998 statement-seq [opt] statement */
11001 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11003 /* Scan statements until there aren't any more. */
11006 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11008 /* If we are looking at a `}', then we have run out of
11009 statements; the same is true if we have reached the end
11010 of file, or have stumbled upon a stray '@end'. */
11011 if (token
->type
== CPP_CLOSE_BRACE
11012 || token
->type
== CPP_EOF
11013 || token
->type
== CPP_PRAGMA_EOL
11014 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11017 /* If we are in a compound statement and find 'else' then
11018 something went wrong. */
11019 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11021 if (parser
->in_statement
& IN_IF_STMT
)
11025 token
= cp_lexer_consume_token (parser
->lexer
);
11026 error_at (token
->location
, "%<else%> without a previous %<if%>");
11030 /* Parse the statement. */
11031 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11035 /* Return true if we're looking at (init; cond), false otherwise. */
11038 cp_parser_init_statement_p (cp_parser
*parser
)
11040 /* Save tokens so that we can put them back. */
11041 cp_lexer_save_tokens (parser
->lexer
);
11043 /* Look for ';' that is not nested in () or {}. */
11044 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11045 /*recovering=*/false,
11047 /*consume_paren=*/false);
11049 /* Roll back the tokens we skipped. */
11050 cp_lexer_rollback_tokens (parser
->lexer
);
11055 /* Parse a selection-statement.
11057 selection-statement:
11058 if ( init-statement [opt] condition ) statement
11059 if ( init-statement [opt] condition ) statement else statement
11060 switch ( init-statement [opt] condition ) statement
11062 Returns the new IF_STMT or SWITCH_STMT.
11064 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11065 is a (possibly labeled) if statement which is not enclosed in
11066 braces and has an else clause. This is used to implement
11069 CHAIN is a vector of if-else-if conditions. This is used to implement
11070 -Wduplicated-cond. */
11073 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11078 token_indent_info guard_tinfo
;
11083 /* Peek at the next token. */
11084 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11085 guard_tinfo
= get_token_indent_info (token
);
11087 /* See what kind of keyword it is. */
11088 keyword
= token
->keyword
;
11098 if (keyword
== RID_IF
11099 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11103 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11104 if (cxx_dialect
< cxx1z
&& !in_system_header_at (tok
->location
))
11105 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11106 "with -std=c++1z or -std=gnu++1z");
11109 /* Look for the `('. */
11110 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11112 cp_parser_skip_to_end_of_statement (parser
);
11113 return error_mark_node
;
11116 /* Begin the selection-statement. */
11117 if (keyword
== RID_IF
)
11119 statement
= begin_if_stmt ();
11120 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11123 statement
= begin_switch_stmt ();
11125 /* Parse the optional init-statement. */
11126 if (cp_parser_init_statement_p (parser
))
11129 if (cxx_dialect
< cxx1z
)
11130 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11131 "init-statement in selection statements only available "
11132 "with -std=c++1z or -std=gnu++1z");
11133 cp_parser_init_statement (parser
, &decl
);
11136 /* Parse the condition. */
11137 condition
= cp_parser_condition (parser
);
11138 /* Look for the `)'. */
11139 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11140 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11141 /*consume_paren=*/true);
11143 if (keyword
== RID_IF
)
11146 unsigned char in_statement
;
11148 /* Add the condition. */
11149 condition
= finish_if_stmt_cond (condition
, statement
);
11151 if (warn_duplicated_cond
)
11152 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11155 /* Parse the then-clause. */
11156 in_statement
= parser
->in_statement
;
11157 parser
->in_statement
|= IN_IF_STMT
;
11159 /* Outside a template, the non-selected branch of a constexpr
11160 if is a 'discarded statement', i.e. unevaluated. */
11161 bool was_discarded
= in_discarded_stmt
;
11162 bool discard_then
= (cx
&& !processing_template_decl
11163 && integer_zerop (condition
));
11166 in_discarded_stmt
= true;
11167 ++c_inhibit_evaluation_warnings
;
11170 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11173 parser
->in_statement
= in_statement
;
11175 finish_then_clause (statement
);
11179 THEN_CLAUSE (statement
) = NULL_TREE
;
11180 in_discarded_stmt
= was_discarded
;
11181 --c_inhibit_evaluation_warnings
;
11184 /* If the next token is `else', parse the else-clause. */
11185 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11188 bool discard_else
= (cx
&& !processing_template_decl
11189 && integer_nonzerop (condition
));
11192 in_discarded_stmt
= true;
11193 ++c_inhibit_evaluation_warnings
;
11197 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11198 /* Consume the `else' keyword. */
11199 cp_lexer_consume_token (parser
->lexer
);
11200 if (warn_duplicated_cond
)
11202 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11206 /* We've got "if (COND) else if (COND2)". Start
11207 the condition chain and add COND as the first
11209 chain
= new vec
<tree
> ();
11210 if (!CONSTANT_CLASS_P (condition
)
11211 && !TREE_SIDE_EFFECTS (condition
))
11213 /* Wrap it in a NOP_EXPR so that we can set the
11214 location of the condition. */
11215 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11217 SET_EXPR_LOCATION (e
, token
->location
);
11218 chain
->safe_push (e
);
11221 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11224 /* This is if-else without subsequent if. Zap the
11225 condition chain; we would have already warned at
11231 begin_else_clause (statement
);
11232 /* Parse the else-clause. */
11233 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11234 guard_tinfo
, chain
);
11236 finish_else_clause (statement
);
11238 /* If we are currently parsing a then-clause, then
11239 IF_P will not be NULL. We set it to true to
11240 indicate that this if statement has an else clause.
11241 This may trigger the Wparentheses warning below
11242 when we get back up to the parent if statement. */
11248 ELSE_CLAUSE (statement
) = NULL_TREE
;
11249 in_discarded_stmt
= was_discarded
;
11250 --c_inhibit_evaluation_warnings
;
11255 /* This if statement does not have an else clause. If
11256 NESTED_IF is true, then the then-clause has an if
11257 statement which does have an else clause. We warn
11258 about the potential ambiguity. */
11260 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11261 "suggest explicit braces to avoid ambiguous"
11263 if (warn_duplicated_cond
)
11265 /* We don't need the condition chain anymore. */
11271 /* Now we're all done with the if-statement. */
11272 finish_if_stmt (statement
);
11276 bool in_switch_statement_p
;
11277 unsigned char in_statement
;
11279 /* Add the condition. */
11280 finish_switch_cond (condition
, statement
);
11282 /* Parse the body of the switch-statement. */
11283 in_switch_statement_p
= parser
->in_switch_statement_p
;
11284 in_statement
= parser
->in_statement
;
11285 parser
->in_switch_statement_p
= true;
11286 parser
->in_statement
|= IN_SWITCH_STMT
;
11287 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11289 parser
->in_switch_statement_p
= in_switch_statement_p
;
11290 parser
->in_statement
= in_statement
;
11292 /* Now we're all done with the switch-statement. */
11293 finish_switch_stmt (statement
);
11301 cp_parser_error (parser
, "expected selection-statement");
11302 return error_mark_node
;
11306 /* Parse a condition.
11310 type-specifier-seq declarator = initializer-clause
11311 type-specifier-seq declarator braced-init-list
11316 type-specifier-seq declarator asm-specification [opt]
11317 attributes [opt] = assignment-expression
11319 Returns the expression that should be tested. */
11322 cp_parser_condition (cp_parser
* parser
)
11324 cp_decl_specifier_seq type_specifiers
;
11325 const char *saved_message
;
11326 int declares_class_or_enum
;
11328 /* Try the declaration first. */
11329 cp_parser_parse_tentatively (parser
);
11330 /* New types are not allowed in the type-specifier-seq for a
11332 saved_message
= parser
->type_definition_forbidden_message
;
11333 parser
->type_definition_forbidden_message
11334 = G_("types may not be defined in conditions");
11335 /* Parse the type-specifier-seq. */
11336 cp_parser_decl_specifier_seq (parser
,
11337 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11339 &declares_class_or_enum
);
11340 /* Restore the saved message. */
11341 parser
->type_definition_forbidden_message
= saved_message
;
11342 /* If all is well, we might be looking at a declaration. */
11343 if (!cp_parser_error_occurred (parser
))
11346 tree asm_specification
;
11348 cp_declarator
*declarator
;
11349 tree initializer
= NULL_TREE
;
11351 /* Parse the declarator. */
11352 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11353 /*ctor_dtor_or_conv_p=*/NULL
,
11354 /*parenthesized_p=*/NULL
,
11355 /*member_p=*/false,
11356 /*friend_p=*/false);
11357 /* Parse the attributes. */
11358 attributes
= cp_parser_attributes_opt (parser
);
11359 /* Parse the asm-specification. */
11360 asm_specification
= cp_parser_asm_specification_opt (parser
);
11361 /* If the next token is not an `=' or '{', then we might still be
11362 looking at an expression. For example:
11366 looks like a decl-specifier-seq and a declarator -- but then
11367 there is no `=', so this is an expression. */
11368 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11369 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11370 cp_parser_simulate_error (parser
);
11372 /* If we did see an `=' or '{', then we are looking at a declaration
11374 if (cp_parser_parse_definitely (parser
))
11377 bool non_constant_p
;
11378 int flags
= LOOKUP_ONLYCONVERTING
;
11380 /* Create the declaration. */
11381 decl
= start_decl (declarator
, &type_specifiers
,
11382 /*initialized_p=*/true,
11383 attributes
, /*prefix_attributes=*/NULL_TREE
,
11386 /* Parse the initializer. */
11387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11389 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11390 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11395 /* Consume the `='. */
11396 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11397 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11399 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11400 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11402 /* Process the initializer. */
11403 cp_finish_decl (decl
,
11404 initializer
, !non_constant_p
,
11409 pop_scope (pushed_scope
);
11411 return convert_from_reference (decl
);
11414 /* If we didn't even get past the declarator successfully, we are
11415 definitely not looking at a declaration. */
11417 cp_parser_abort_tentative_parse (parser
);
11419 /* Otherwise, we are looking at an expression. */
11420 return cp_parser_expression (parser
);
11423 /* Parses a for-statement or range-for-statement until the closing ')',
11427 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11429 tree init
, scope
, decl
;
11432 /* Begin the for-statement. */
11433 scope
= begin_for_scope (&init
);
11435 /* Parse the initialization. */
11436 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11439 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11441 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11445 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11447 /* Normal for loop */
11448 tree condition
= NULL_TREE
;
11449 tree expression
= NULL_TREE
;
11452 stmt
= begin_for_stmt (scope
, init
);
11453 /* The init-statement has already been parsed in
11454 cp_parser_init_statement, so no work is needed here. */
11455 finish_init_stmt (stmt
);
11457 /* If there's a condition, process it. */
11458 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11459 condition
= cp_parser_condition (parser
);
11462 cp_parser_error (parser
, "missing loop condition in loop with "
11463 "%<GCC ivdep%> pragma");
11464 condition
= error_mark_node
;
11466 finish_for_cond (condition
, stmt
, ivdep
);
11467 /* Look for the `;'. */
11468 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11470 /* If there's an expression, process it. */
11471 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11472 expression
= cp_parser_expression (parser
);
11473 finish_for_expr (expression
, stmt
);
11478 /* Tries to parse a range-based for-statement:
11481 decl-specifier-seq declarator : expression
11483 The decl-specifier-seq declarator and the `:' are already parsed by
11484 cp_parser_init_statement. If processing_template_decl it returns a
11485 newly created RANGE_FOR_STMT; if not, it is converted to a
11486 regular FOR_STMT. */
11489 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11492 tree stmt
, range_expr
;
11493 auto_vec
<cxx_binding
*, 16> bindings
;
11494 auto_vec
<tree
, 16> names
;
11495 tree decomp_first_name
= NULL_TREE
;
11496 unsigned int decomp_cnt
= 0;
11498 /* Get the range declaration momentarily out of the way so that
11499 the range expression doesn't clash with it. */
11500 if (range_decl
!= error_mark_node
)
11502 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11504 tree v
= DECL_VALUE_EXPR (range_decl
);
11505 /* For decomposition declaration get all of the corresponding
11506 declarations out of the way. */
11507 if (TREE_CODE (v
) == ARRAY_REF
11508 && VAR_P (TREE_OPERAND (v
, 0))
11509 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11511 tree d
= range_decl
;
11512 range_decl
= TREE_OPERAND (v
, 0);
11513 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11514 decomp_first_name
= d
;
11515 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11517 tree name
= DECL_NAME (d
);
11518 names
.safe_push (name
);
11519 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11520 IDENTIFIER_BINDING (name
)
11521 = IDENTIFIER_BINDING (name
)->previous
;
11525 if (names
.is_empty ())
11527 tree name
= DECL_NAME (range_decl
);
11528 names
.safe_push (name
);
11529 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11530 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11536 bool expr_non_constant_p
;
11537 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11540 range_expr
= cp_parser_expression (parser
);
11542 /* Put the range declaration(s) back into scope. */
11543 for (unsigned int i
= 0; i
< names
.length (); i
++)
11545 cxx_binding
*binding
= bindings
[i
];
11546 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11547 IDENTIFIER_BINDING (names
[i
]) = binding
;
11550 /* If in template, STMT is converted to a normal for-statement
11551 at instantiation. If not, it is done just ahead. */
11552 if (processing_template_decl
)
11554 if (check_for_bare_parameter_packs (range_expr
))
11555 range_expr
= error_mark_node
;
11556 stmt
= begin_range_for_stmt (scope
, init
);
11558 RANGE_FOR_IVDEP (stmt
) = 1;
11559 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11560 if (!type_dependent_expression_p (range_expr
)
11561 /* do_auto_deduction doesn't mess with template init-lists. */
11562 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11563 do_range_for_auto_deduction (range_decl
, range_expr
);
11567 stmt
= begin_for_stmt (scope
, init
);
11568 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11569 decomp_first_name
, decomp_cnt
, ivdep
);
11574 /* Subroutine of cp_convert_range_for: given the initializer expression,
11575 builds up the range temporary. */
11578 build_range_temp (tree range_expr
)
11580 tree range_type
, range_temp
;
11582 /* Find out the type deduced by the declaration
11583 `auto &&__range = range_expr'. */
11584 range_type
= cp_build_reference_type (make_auto (), true);
11585 range_type
= do_auto_deduction (range_type
, range_expr
,
11586 type_uses_auto (range_type
));
11588 /* Create the __range variable. */
11589 range_temp
= build_decl (input_location
, VAR_DECL
,
11590 get_identifier ("__for_range"), range_type
);
11591 TREE_USED (range_temp
) = 1;
11592 DECL_ARTIFICIAL (range_temp
) = 1;
11597 /* Used by cp_parser_range_for in template context: we aren't going to
11598 do a full conversion yet, but we still need to resolve auto in the
11599 type of the for-range-declaration if present. This is basically
11600 a shortcut version of cp_convert_range_for. */
11603 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11605 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11608 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11609 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11610 iter_type
= (cp_parser_perform_range_for_lookup
11611 (range_temp
, &begin_dummy
, &end_dummy
));
11614 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11616 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
11617 tf_warning_or_error
);
11618 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11619 iter_decl
, auto_node
);
11624 /* Converts a range-based for-statement into a normal
11625 for-statement, as per the definition.
11627 for (RANGE_DECL : RANGE_EXPR)
11630 should be equivalent to:
11633 auto &&__range = RANGE_EXPR;
11634 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11638 RANGE_DECL = *__begin;
11643 If RANGE_EXPR is an array:
11644 BEGIN_EXPR = __range
11645 END_EXPR = __range + ARRAY_SIZE(__range)
11646 Else if RANGE_EXPR has a member 'begin' or 'end':
11647 BEGIN_EXPR = __range.begin()
11648 END_EXPR = __range.end()
11650 BEGIN_EXPR = begin(__range)
11651 END_EXPR = end(__range);
11653 If __range has a member 'begin' but not 'end', or vice versa, we must
11654 still use the second alternative (it will surely fail, however).
11655 When calling begin()/end() in the third alternative we must use
11656 argument dependent lookup, but always considering 'std' as an associated
11660 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11661 tree decomp_first_name
, unsigned int decomp_cnt
,
11665 tree iter_type
, begin_expr
, end_expr
;
11666 tree condition
, expression
;
11668 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11669 /* If an error happened previously do nothing or else a lot of
11670 unhelpful errors would be issued. */
11671 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11676 if (VAR_P (range_expr
)
11677 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11678 /* Can't bind a reference to an array of runtime bound. */
11679 range_temp
= range_expr
;
11682 range_temp
= build_range_temp (range_expr
);
11683 pushdecl (range_temp
);
11684 cp_finish_decl (range_temp
, range_expr
,
11685 /*is_constant_init*/false, NULL_TREE
,
11686 LOOKUP_ONLYCONVERTING
);
11687 range_temp
= convert_from_reference (range_temp
);
11689 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11690 &begin_expr
, &end_expr
);
11693 /* The new for initialization statement. */
11694 begin
= build_decl (input_location
, VAR_DECL
,
11695 get_identifier ("__for_begin"), iter_type
);
11696 TREE_USED (begin
) = 1;
11697 DECL_ARTIFICIAL (begin
) = 1;
11699 cp_finish_decl (begin
, begin_expr
,
11700 /*is_constant_init*/false, NULL_TREE
,
11701 LOOKUP_ONLYCONVERTING
);
11703 if (cxx_dialect
>= cxx1z
)
11704 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11705 end
= build_decl (input_location
, VAR_DECL
,
11706 get_identifier ("__for_end"), iter_type
);
11707 TREE_USED (end
) = 1;
11708 DECL_ARTIFICIAL (end
) = 1;
11710 cp_finish_decl (end
, end_expr
,
11711 /*is_constant_init*/false, NULL_TREE
,
11712 LOOKUP_ONLYCONVERTING
);
11714 finish_init_stmt (statement
);
11716 /* The new for condition. */
11717 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11720 NULL
, tf_warning_or_error
);
11721 finish_for_cond (condition
, statement
, ivdep
);
11723 /* The new increment expression. */
11724 expression
= finish_unary_op_expr (input_location
,
11725 PREINCREMENT_EXPR
, begin
,
11726 tf_warning_or_error
);
11727 finish_for_expr (expression
, statement
);
11729 /* The declaration is initialized with *__begin inside the loop body. */
11730 cp_finish_decl (range_decl
,
11731 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11732 tf_warning_or_error
),
11733 /*is_constant_init*/false, NULL_TREE
,
11734 LOOKUP_ONLYCONVERTING
);
11735 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11736 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11741 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11742 We need to solve both at the same time because the method used
11743 depends on the existence of members begin or end.
11744 Returns the type deduced for the iterator expression. */
11747 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11749 if (error_operand_p (range
))
11751 *begin
= *end
= error_mark_node
;
11752 return error_mark_node
;
11755 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11757 error ("range-based %<for%> expression of type %qT "
11758 "has incomplete type", TREE_TYPE (range
));
11759 *begin
= *end
= error_mark_node
;
11760 return error_mark_node
;
11762 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11764 /* If RANGE is an array, we will use pointer arithmetic. */
11765 *begin
= decay_conversion (range
, tf_warning_or_error
);
11766 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11768 array_type_nelts_top (TREE_TYPE (range
)),
11770 return TREE_TYPE (*begin
);
11774 /* If it is not an array, we must do a bit of magic. */
11775 tree id_begin
, id_end
;
11776 tree member_begin
, member_end
;
11778 *begin
= *end
= error_mark_node
;
11780 id_begin
= get_identifier ("begin");
11781 id_end
= get_identifier ("end");
11782 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11783 /*protect=*/2, /*want_type=*/false,
11784 tf_warning_or_error
);
11785 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11786 /*protect=*/2, /*want_type=*/false,
11787 tf_warning_or_error
);
11789 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11791 /* Use the member functions. */
11792 if (member_begin
!= NULL_TREE
)
11793 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11795 error ("range-based %<for%> expression of type %qT has an "
11796 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11798 if (member_end
!= NULL_TREE
)
11799 *end
= cp_parser_range_for_member_function (range
, id_end
);
11801 error ("range-based %<for%> expression of type %qT has a "
11802 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11806 /* Use global functions with ADL. */
11807 vec
<tree
, va_gc
> *vec
;
11808 vec
= make_tree_vector ();
11810 vec_safe_push (vec
, range
);
11812 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11813 tf_warning_or_error
);
11814 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11815 tf_warning_or_error
);
11816 member_end
= perform_koenig_lookup (id_end
, vec
,
11817 tf_warning_or_error
);
11818 *end
= finish_call_expr (member_end
, &vec
, false, true,
11819 tf_warning_or_error
);
11821 release_tree_vector (vec
);
11824 /* Last common checks. */
11825 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11827 /* If one of the expressions is an error do no more checks. */
11828 *begin
= *end
= error_mark_node
;
11829 return error_mark_node
;
11831 else if (type_dependent_expression_p (*begin
)
11832 || type_dependent_expression_p (*end
))
11833 /* Can happen, when, eg, in a template context, Koenig lookup
11834 can't resolve begin/end (c++/58503). */
11838 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11839 /* The unqualified type of the __begin and __end temporaries should
11840 be the same, as required by the multiple auto declaration. */
11841 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11843 if (cxx_dialect
>= cxx1z
11844 && (build_x_binary_op (input_location
, NE_EXPR
,
11845 *begin
, ERROR_MARK
,
11848 != error_mark_node
))
11849 /* P0184R0 allows __begin and __end to have different types,
11850 but make sure they are comparable so we can give a better
11853 error ("inconsistent begin/end types in range-based %<for%> "
11854 "statement: %qT and %qT",
11855 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11862 /* Helper function for cp_parser_perform_range_for_lookup.
11863 Builds a tree for RANGE.IDENTIFIER(). */
11866 cp_parser_range_for_member_function (tree range
, tree identifier
)
11869 vec
<tree
, va_gc
> *vec
;
11871 member
= finish_class_member_access_expr (range
, identifier
,
11872 false, tf_warning_or_error
);
11873 if (member
== error_mark_node
)
11874 return error_mark_node
;
11876 vec
= make_tree_vector ();
11877 res
= finish_call_expr (member
, &vec
,
11878 /*disallow_virtual=*/false,
11879 /*koenig_p=*/false,
11880 tf_warning_or_error
);
11881 release_tree_vector (vec
);
11885 /* Parse an iteration-statement.
11887 iteration-statement:
11888 while ( condition ) statement
11889 do statement while ( expression ) ;
11890 for ( init-statement condition [opt] ; expression [opt] )
11893 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11896 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
11901 unsigned char in_statement
;
11902 token_indent_info guard_tinfo
;
11904 /* Peek at the next token. */
11905 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11907 return error_mark_node
;
11909 guard_tinfo
= get_token_indent_info (token
);
11911 /* Remember whether or not we are already within an iteration
11913 in_statement
= parser
->in_statement
;
11915 /* See what kind of keyword it is. */
11916 keyword
= token
->keyword
;
11923 /* Begin the while-statement. */
11924 statement
= begin_while_stmt ();
11925 /* Look for the `('. */
11926 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11927 /* Parse the condition. */
11928 condition
= cp_parser_condition (parser
);
11929 finish_while_stmt_cond (condition
, statement
, ivdep
);
11930 /* Look for the `)'. */
11931 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11932 /* Parse the dependent statement. */
11933 parser
->in_statement
= IN_ITERATION_STMT
;
11934 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11935 parser
->in_statement
= in_statement
;
11936 /* We're done with the while-statement. */
11937 finish_while_stmt (statement
);
11945 /* Begin the do-statement. */
11946 statement
= begin_do_stmt ();
11947 /* Parse the body of the do-statement. */
11948 parser
->in_statement
= IN_ITERATION_STMT
;
11949 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11950 parser
->in_statement
= in_statement
;
11951 finish_do_body (statement
);
11952 /* Look for the `while' keyword. */
11953 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11954 /* Look for the `('. */
11955 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11956 /* Parse the expression. */
11957 expression
= cp_parser_expression (parser
);
11958 /* We're done with the do-statement. */
11959 finish_do_stmt (expression
, statement
, ivdep
);
11960 /* Look for the `)'. */
11961 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11962 /* Look for the `;'. */
11963 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11969 /* Look for the `('. */
11970 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11972 statement
= cp_parser_for (parser
, ivdep
);
11974 /* Look for the `)'. */
11975 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11977 /* Parse the body of the for-statement. */
11978 parser
->in_statement
= IN_ITERATION_STMT
;
11979 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11980 parser
->in_statement
= in_statement
;
11982 /* We're done with the for-statement. */
11983 finish_for_stmt (statement
);
11988 cp_parser_error (parser
, "expected iteration-statement");
11989 statement
= error_mark_node
;
11996 /* Parse a init-statement or the declarator of a range-based-for.
11997 Returns true if a range-based-for declaration is seen.
12000 expression-statement
12001 simple-declaration */
12004 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12006 /* If the next token is a `;', then we have an empty
12007 expression-statement. Grammatically, this is also a
12008 simple-declaration, but an invalid one, because it does not
12009 declare anything. Therefore, if we did not handle this case
12010 specially, we would issue an error message about an invalid
12012 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12014 bool is_range_for
= false;
12015 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12017 /* A colon is used in range-based for. */
12018 parser
->colon_corrects_to_scope_p
= false;
12020 /* We're going to speculatively look for a declaration, falling back
12021 to an expression, if necessary. */
12022 cp_parser_parse_tentatively (parser
);
12023 /* Parse the declaration. */
12024 cp_parser_simple_declaration (parser
,
12025 /*function_definition_allowed_p=*/false,
12027 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12030 /* It is a range-for, consume the ':' */
12031 cp_lexer_consume_token (parser
->lexer
);
12032 is_range_for
= true;
12033 if (cxx_dialect
< cxx11
)
12035 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12036 "range-based %<for%> loops only available with "
12037 "-std=c++11 or -std=gnu++11");
12038 *decl
= error_mark_node
;
12042 /* The ';' is not consumed yet because we told
12043 cp_parser_simple_declaration not to. */
12044 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12046 if (cp_parser_parse_definitely (parser
))
12047 return is_range_for
;
12048 /* If the tentative parse failed, then we shall need to look for an
12049 expression-statement. */
12051 /* If we are here, it is an expression-statement. */
12052 cp_parser_expression_statement (parser
, NULL_TREE
);
12056 /* Parse a jump-statement.
12061 return expression [opt] ;
12062 return braced-init-list ;
12068 goto * expression ;
12070 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12073 cp_parser_jump_statement (cp_parser
* parser
)
12075 tree statement
= error_mark_node
;
12078 unsigned char in_statement
;
12080 /* Peek at the next token. */
12081 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12083 return error_mark_node
;
12085 /* See what kind of keyword it is. */
12086 keyword
= token
->keyword
;
12090 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12091 switch (in_statement
)
12094 error_at (token
->location
, "break statement not within loop or switch");
12097 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12098 || in_statement
== IN_ITERATION_STMT
);
12099 statement
= finish_break_stmt ();
12100 if (in_statement
== IN_ITERATION_STMT
)
12101 break_maybe_infinite_loop ();
12104 error_at (token
->location
, "invalid exit from OpenMP structured block");
12107 error_at (token
->location
, "break statement used with OpenMP for loop");
12109 case IN_CILK_SIMD_FOR
:
12110 error_at (token
->location
, "break statement used with Cilk Plus for loop");
12113 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12117 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12120 error_at (token
->location
, "continue statement not within a loop");
12122 case IN_CILK_SIMD_FOR
:
12123 error_at (token
->location
,
12124 "continue statement within %<#pragma simd%> loop body");
12125 /* Fall through. */
12126 case IN_ITERATION_STMT
:
12128 statement
= finish_continue_stmt ();
12131 error_at (token
->location
, "invalid exit from OpenMP structured block");
12134 gcc_unreachable ();
12136 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12142 bool expr_non_constant_p
;
12144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12146 cp_lexer_set_source_position (parser
->lexer
);
12147 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12148 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12150 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12151 expr
= cp_parser_expression (parser
);
12153 /* If the next token is a `;', then there is no
12156 /* Build the return-statement. */
12157 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12158 /* Don't deduce from a discarded return statement. */;
12160 statement
= finish_return_stmt (expr
);
12161 /* Look for the final `;'. */
12162 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12167 if (parser
->in_function_body
12168 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12170 error ("%<goto%> in %<constexpr%> function");
12171 cp_function_chain
->invalid_constexpr
= true;
12174 /* Create the goto-statement. */
12175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12177 /* Issue a warning about this use of a GNU extension. */
12178 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12179 /* Consume the '*' token. */
12180 cp_lexer_consume_token (parser
->lexer
);
12181 /* Parse the dependent expression. */
12182 finish_goto_stmt (cp_parser_expression (parser
));
12185 finish_goto_stmt (cp_parser_identifier (parser
));
12186 /* Look for the final `;'. */
12187 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12191 cp_parser_error (parser
, "expected jump-statement");
12198 /* Parse a declaration-statement.
12200 declaration-statement:
12201 block-declaration */
12204 cp_parser_declaration_statement (cp_parser
* parser
)
12208 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12209 p
= obstack_alloc (&declarator_obstack
, 0);
12211 /* Parse the block-declaration. */
12212 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12214 /* Free any declarators allocated. */
12215 obstack_free (&declarator_obstack
, p
);
12218 /* Some dependent statements (like `if (cond) statement'), are
12219 implicitly in their own scope. In other words, if the statement is
12220 a single statement (as opposed to a compound-statement), it is
12221 none-the-less treated as if it were enclosed in braces. Any
12222 declarations appearing in the dependent statement are out of scope
12223 after control passes that point. This function parses a statement,
12224 but ensures that is in its own scope, even if it is not a
12225 compound-statement.
12227 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12228 is a (possibly labeled) if statement which is not enclosed in
12229 braces and has an else clause. This is used to implement
12232 CHAIN is a vector of if-else-if conditions. This is used to implement
12235 Returns the new statement. */
12238 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12239 const token_indent_info
&guard_tinfo
,
12243 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12244 token_indent_info body_tinfo
12245 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12250 /* Mark if () ; with a special NOP_EXPR. */
12251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12253 cp_lexer_consume_token (parser
->lexer
);
12254 statement
= add_stmt (build_empty_stmt (body_loc
));
12256 if (guard_tinfo
.keyword
== RID_IF
12257 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12258 warning_at (body_loc
, OPT_Wempty_body
,
12259 "suggest braces around empty body in an %<if%> statement");
12260 else if (guard_tinfo
.keyword
== RID_ELSE
)
12261 warning_at (body_loc
, OPT_Wempty_body
,
12262 "suggest braces around empty body in an %<else%> statement");
12264 /* if a compound is opened, we simply parse the statement directly. */
12265 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12266 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12267 /* If the token is not a `{', then we must take special action. */
12270 /* Create a compound-statement. */
12271 statement
= begin_compound_stmt (0);
12272 /* Parse the dependent-statement. */
12273 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
12274 /* Finish the dummy compound-statement. */
12275 finish_compound_stmt (statement
);
12278 token_indent_info next_tinfo
12279 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12280 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12282 /* Return the statement. */
12286 /* For some dependent statements (like `while (cond) statement'), we
12287 have already created a scope. Therefore, even if the dependent
12288 statement is a compound-statement, we do not want to create another
12292 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12293 const token_indent_info
&guard_tinfo
)
12295 /* If the token is a `{', then we must take special action. */
12296 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12298 token_indent_info body_tinfo
12299 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12301 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
12302 token_indent_info next_tinfo
12303 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12304 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12308 /* Avoid calling cp_parser_compound_statement, so that we
12309 don't create a new scope. Do everything else by hand. */
12310 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
12311 /* If the next keyword is `__label__' we have a label declaration. */
12312 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12313 cp_parser_label_declaration (parser
);
12314 /* Parse an (optional) statement-seq. */
12315 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12316 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12320 /* Declarations [gram.dcl.dcl] */
12322 /* Parse an optional declaration-sequence.
12326 declaration-seq declaration */
12329 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12335 token
= cp_lexer_peek_token (parser
->lexer
);
12337 if (token
->type
== CPP_CLOSE_BRACE
12338 || token
->type
== CPP_EOF
12339 || token
->type
== CPP_PRAGMA_EOL
)
12342 if (token
->type
== CPP_SEMICOLON
)
12344 /* A declaration consisting of a single semicolon is
12345 invalid. Allow it unless we're being pedantic. */
12346 cp_lexer_consume_token (parser
->lexer
);
12347 if (!in_system_header_at (input_location
))
12348 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12352 /* If we're entering or exiting a region that's implicitly
12353 extern "C", modify the lang context appropriately. */
12354 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12356 push_lang_context (lang_name_c
);
12357 parser
->implicit_extern_c
= true;
12359 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12361 pop_lang_context ();
12362 parser
->implicit_extern_c
= false;
12365 if (token
->type
== CPP_PRAGMA
)
12367 /* A top-level declaration can consist solely of a #pragma.
12368 A nested declaration cannot, so this is done here and not
12369 in cp_parser_declaration. (A #pragma at block scope is
12370 handled in cp_parser_statement.) */
12371 cp_parser_pragma (parser
, pragma_external
, NULL
);
12375 /* Parse the declaration itself. */
12376 cp_parser_declaration (parser
);
12380 /* Parse a declaration.
12384 function-definition
12385 template-declaration
12386 explicit-instantiation
12387 explicit-specialization
12388 linkage-specification
12389 namespace-definition
12397 __extension__ declaration */
12400 cp_parser_declaration (cp_parser
* parser
)
12404 int saved_pedantic
;
12406 tree attributes
= NULL_TREE
;
12408 /* Check for the `__extension__' keyword. */
12409 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12411 /* Parse the qualified declaration. */
12412 cp_parser_declaration (parser
);
12413 /* Restore the PEDANTIC flag. */
12414 pedantic
= saved_pedantic
;
12419 /* Try to figure out what kind of declaration is present. */
12420 token1
= *cp_lexer_peek_token (parser
->lexer
);
12422 if (token1
.type
!= CPP_EOF
)
12423 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12426 token2
.type
= CPP_EOF
;
12427 token2
.keyword
= RID_MAX
;
12430 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12431 p
= obstack_alloc (&declarator_obstack
, 0);
12433 /* If the next token is `extern' and the following token is a string
12434 literal, then we have a linkage specification. */
12435 if (token1
.keyword
== RID_EXTERN
12436 && cp_parser_is_pure_string_literal (&token2
))
12437 cp_parser_linkage_specification (parser
);
12438 /* If the next token is `template', then we have either a template
12439 declaration, an explicit instantiation, or an explicit
12441 else if (token1
.keyword
== RID_TEMPLATE
)
12443 /* `template <>' indicates a template specialization. */
12444 if (token2
.type
== CPP_LESS
12445 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12446 cp_parser_explicit_specialization (parser
);
12447 /* `template <' indicates a template declaration. */
12448 else if (token2
.type
== CPP_LESS
)
12449 cp_parser_template_declaration (parser
, /*member_p=*/false);
12450 /* Anything else must be an explicit instantiation. */
12452 cp_parser_explicit_instantiation (parser
);
12454 /* If the next token is `export', then we have a template
12456 else if (token1
.keyword
== RID_EXPORT
)
12457 cp_parser_template_declaration (parser
, /*member_p=*/false);
12458 /* If the next token is `extern', 'static' or 'inline' and the one
12459 after that is `template', we have a GNU extended explicit
12460 instantiation directive. */
12461 else if (cp_parser_allow_gnu_extensions_p (parser
)
12462 && (token1
.keyword
== RID_EXTERN
12463 || token1
.keyword
== RID_STATIC
12464 || token1
.keyword
== RID_INLINE
)
12465 && token2
.keyword
== RID_TEMPLATE
)
12466 cp_parser_explicit_instantiation (parser
);
12467 /* If the next token is `namespace', check for a named or unnamed
12468 namespace definition. */
12469 else if (token1
.keyword
== RID_NAMESPACE
12470 && (/* A named namespace definition. */
12471 (token2
.type
== CPP_NAME
12472 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12474 || (token2
.type
== CPP_OPEN_SQUARE
12475 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12476 == CPP_OPEN_SQUARE
)
12477 /* An unnamed namespace definition. */
12478 || token2
.type
== CPP_OPEN_BRACE
12479 || token2
.keyword
== RID_ATTRIBUTE
))
12480 cp_parser_namespace_definition (parser
);
12481 /* An inline (associated) namespace definition. */
12482 else if (token1
.keyword
== RID_INLINE
12483 && token2
.keyword
== RID_NAMESPACE
)
12484 cp_parser_namespace_definition (parser
);
12485 /* Objective-C++ declaration/definition. */
12486 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12487 cp_parser_objc_declaration (parser
, NULL_TREE
);
12488 else if (c_dialect_objc ()
12489 && token1
.keyword
== RID_ATTRIBUTE
12490 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12491 cp_parser_objc_declaration (parser
, attributes
);
12492 /* At this point we may have a template declared by a concept
12494 else if (flag_concepts
12495 && cp_parser_template_declaration_after_export (parser
,
12496 /*member_p=*/false))
12499 /* Try to parse a block-declaration, or a function-definition. */
12500 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12502 /* Free any declarators allocated. */
12503 obstack_free (&declarator_obstack
, p
);
12506 /* Parse a block-declaration.
12511 namespace-alias-definition
12518 __extension__ block-declaration
12523 static_assert-declaration
12525 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12526 part of a declaration-statement. */
12529 cp_parser_block_declaration (cp_parser
*parser
,
12533 int saved_pedantic
;
12535 /* Check for the `__extension__' keyword. */
12536 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12538 /* Parse the qualified declaration. */
12539 cp_parser_block_declaration (parser
, statement_p
);
12540 /* Restore the PEDANTIC flag. */
12541 pedantic
= saved_pedantic
;
12546 /* Peek at the next token to figure out which kind of declaration is
12548 token1
= cp_lexer_peek_token (parser
->lexer
);
12550 /* If the next keyword is `asm', we have an asm-definition. */
12551 if (token1
->keyword
== RID_ASM
)
12554 cp_parser_commit_to_tentative_parse (parser
);
12555 cp_parser_asm_definition (parser
);
12557 /* If the next keyword is `namespace', we have a
12558 namespace-alias-definition. */
12559 else if (token1
->keyword
== RID_NAMESPACE
)
12560 cp_parser_namespace_alias_definition (parser
);
12561 /* If the next keyword is `using', we have a
12562 using-declaration, a using-directive, or an alias-declaration. */
12563 else if (token1
->keyword
== RID_USING
)
12568 cp_parser_commit_to_tentative_parse (parser
);
12569 /* If the token after `using' is `namespace', then we have a
12570 using-directive. */
12571 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12572 if (token2
->keyword
== RID_NAMESPACE
)
12573 cp_parser_using_directive (parser
);
12574 /* If the second token after 'using' is '=', then we have an
12575 alias-declaration. */
12576 else if (cxx_dialect
>= cxx11
12577 && token2
->type
== CPP_NAME
12578 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12579 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12580 cp_parser_alias_declaration (parser
);
12581 /* Otherwise, it's a using-declaration. */
12583 cp_parser_using_declaration (parser
,
12584 /*access_declaration_p=*/false);
12586 /* If the next keyword is `__label__' we have a misplaced label
12588 else if (token1
->keyword
== RID_LABEL
)
12590 cp_lexer_consume_token (parser
->lexer
);
12591 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12592 cp_parser_skip_to_end_of_statement (parser
);
12593 /* If the next token is now a `;', consume it. */
12594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12595 cp_lexer_consume_token (parser
->lexer
);
12597 /* If the next token is `static_assert' we have a static assertion. */
12598 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12599 cp_parser_static_assert (parser
, /*member_p=*/false);
12600 /* Anything else must be a simple-declaration. */
12602 cp_parser_simple_declaration (parser
, !statement_p
,
12603 /*maybe_range_for_decl*/NULL
);
12606 /* Parse a simple-declaration.
12608 simple-declaration:
12609 decl-specifier-seq [opt] init-declarator-list [opt] ;
12610 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12611 brace-or-equal-initializer ;
12613 init-declarator-list:
12615 init-declarator-list , init-declarator
12617 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12618 function-definition as a simple-declaration.
12620 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12621 parsed declaration if it is an uninitialized single declarator not followed
12622 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12623 if present, will not be consumed. */
12626 cp_parser_simple_declaration (cp_parser
* parser
,
12627 bool function_definition_allowed_p
,
12628 tree
*maybe_range_for_decl
)
12630 cp_decl_specifier_seq decl_specifiers
;
12631 int declares_class_or_enum
;
12632 bool saw_declarator
;
12633 location_t comma_loc
= UNKNOWN_LOCATION
;
12634 location_t init_loc
= UNKNOWN_LOCATION
;
12636 if (maybe_range_for_decl
)
12637 *maybe_range_for_decl
= NULL_TREE
;
12639 /* Defer access checks until we know what is being declared; the
12640 checks for names appearing in the decl-specifier-seq should be
12641 done as if we were in the scope of the thing being declared. */
12642 push_deferring_access_checks (dk_deferred
);
12644 /* Parse the decl-specifier-seq. We have to keep track of whether
12645 or not the decl-specifier-seq declares a named class or
12646 enumeration type, since that is the only case in which the
12647 init-declarator-list is allowed to be empty.
12651 In a simple-declaration, the optional init-declarator-list can be
12652 omitted only when declaring a class or enumeration, that is when
12653 the decl-specifier-seq contains either a class-specifier, an
12654 elaborated-type-specifier, or an enum-specifier. */
12655 cp_parser_decl_specifier_seq (parser
,
12656 CP_PARSER_FLAGS_OPTIONAL
,
12658 &declares_class_or_enum
);
12659 /* We no longer need to defer access checks. */
12660 stop_deferring_access_checks ();
12662 /* In a block scope, a valid declaration must always have a
12663 decl-specifier-seq. By not trying to parse declarators, we can
12664 resolve the declaration/expression ambiguity more quickly. */
12665 if (!function_definition_allowed_p
12666 && !decl_specifiers
.any_specifiers_p
)
12668 cp_parser_error (parser
, "expected declaration");
12672 /* If the next two tokens are both identifiers, the code is
12673 erroneous. The usual cause of this situation is code like:
12677 where "T" should name a type -- but does not. */
12678 if (!decl_specifiers
.any_type_specifiers_p
12679 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12681 /* If parsing tentatively, we should commit; we really are
12682 looking at a declaration. */
12683 cp_parser_commit_to_tentative_parse (parser
);
12688 /* If we have seen at least one decl-specifier, and the next token
12689 is not a parenthesis, then we must be looking at a declaration.
12690 (After "int (" we might be looking at a functional cast.) */
12691 if (decl_specifiers
.any_specifiers_p
12692 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12693 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12694 && !cp_parser_error_occurred (parser
))
12695 cp_parser_commit_to_tentative_parse (parser
);
12697 /* Look for C++17 decomposition declaration. */
12698 for (size_t n
= 1; ; n
++)
12699 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12700 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12702 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12703 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12704 && decl_specifiers
.any_specifiers_p
)
12707 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12708 maybe_range_for_decl
,
12711 /* The next token should be either a `,' or a `;'. */
12712 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12713 /* If it's a `;', we are done. */
12714 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12716 /* Anything else is an error. */
12719 /* If we have already issued an error message we don't need
12720 to issue another one. */
12721 if ((decl
!= error_mark_node
12722 && DECL_INITIAL (decl
) != error_mark_node
)
12723 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12724 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12725 /* Skip tokens until we reach the end of the statement. */
12726 cp_parser_skip_to_end_of_statement (parser
);
12727 /* If the next token is now a `;', consume it. */
12728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12729 cp_lexer_consume_token (parser
->lexer
);
12737 bool auto_specifier_p
;
12738 /* NULL_TREE if both variable and function declaration are allowed,
12739 error_mark_node if function declaration are not allowed and
12740 a FUNCTION_DECL that should be diagnosed if it is followed by
12741 variable declarations. */
12742 tree auto_function_declaration
;
12744 last_type
= NULL_TREE
;
12746 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12747 auto_function_declaration
= NULL_TREE
;
12749 /* Keep going until we hit the `;' at the end of the simple
12751 saw_declarator
= false;
12752 while (cp_lexer_next_token_is_not (parser
->lexer
,
12756 bool function_definition_p
;
12758 tree auto_result
= NULL_TREE
;
12760 if (saw_declarator
)
12762 /* If we are processing next declarator, comma is expected */
12763 token
= cp_lexer_peek_token (parser
->lexer
);
12764 gcc_assert (token
->type
== CPP_COMMA
);
12765 cp_lexer_consume_token (parser
->lexer
);
12766 if (maybe_range_for_decl
)
12768 *maybe_range_for_decl
= error_mark_node
;
12769 if (comma_loc
== UNKNOWN_LOCATION
)
12770 comma_loc
= token
->location
;
12774 saw_declarator
= true;
12776 /* Parse the init-declarator. */
12777 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12779 function_definition_allowed_p
,
12780 /*member_p=*/false,
12781 declares_class_or_enum
,
12782 &function_definition_p
,
12783 maybe_range_for_decl
,
12786 /* If an error occurred while parsing tentatively, exit quickly.
12787 (That usually happens when in the body of a function; each
12788 statement is treated as a declaration-statement until proven
12790 if (cp_parser_error_occurred (parser
))
12793 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
12795 /* If the init-declarator-list contains more than one
12796 init-declarator, they shall all form declarations of
12798 if (auto_function_declaration
== NULL_TREE
)
12799 auto_function_declaration
12800 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
12801 else if (TREE_CODE (decl
) == FUNCTION_DECL
12802 || auto_function_declaration
!= error_mark_node
)
12804 error_at (decl_specifiers
.locations
[ds_type_spec
],
12805 "non-variable %qD in declaration with more than one "
12806 "declarator with placeholder type",
12807 TREE_CODE (decl
) == FUNCTION_DECL
12808 ? decl
: auto_function_declaration
);
12809 auto_function_declaration
= error_mark_node
;
12814 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
12817 && last_type
!= error_mark_node
12818 && !same_type_p (auto_result
, last_type
))
12820 /* If the list of declarators contains more than one declarator,
12821 the type of each declared variable is determined as described
12822 above. If the type deduced for the template parameter U is not
12823 the same in each deduction, the program is ill-formed. */
12824 error_at (decl_specifiers
.locations
[ds_type_spec
],
12825 "inconsistent deduction for %qT: %qT and then %qT",
12826 decl_specifiers
.type
, last_type
, auto_result
);
12827 last_type
= error_mark_node
;
12830 last_type
= auto_result
;
12833 /* Handle function definitions specially. */
12834 if (function_definition_p
)
12836 /* If the next token is a `,', then we are probably
12837 processing something like:
12841 which is erroneous. */
12842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12844 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12845 error_at (token
->location
,
12847 " declarations and function-definitions is forbidden");
12849 /* Otherwise, we're done with the list of declarators. */
12852 pop_deferring_access_checks ();
12856 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12857 *maybe_range_for_decl
= decl
;
12858 /* The next token should be either a `,' or a `;'. */
12859 token
= cp_lexer_peek_token (parser
->lexer
);
12860 /* If it's a `,', there are more declarators to come. */
12861 if (token
->type
== CPP_COMMA
)
12862 /* will be consumed next time around */;
12863 /* If it's a `;', we are done. */
12864 else if (token
->type
== CPP_SEMICOLON
)
12866 else if (maybe_range_for_decl
)
12868 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
12869 permerror (decl_specifiers
.locations
[ds_type_spec
],
12870 "types may not be defined in a for-range-declaration");
12873 /* Anything else is an error. */
12876 /* If we have already issued an error message we don't need
12877 to issue another one. */
12878 if ((decl
!= error_mark_node
12879 && DECL_INITIAL (decl
) != error_mark_node
)
12880 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12881 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12882 /* Skip tokens until we reach the end of the statement. */
12883 cp_parser_skip_to_end_of_statement (parser
);
12884 /* If the next token is now a `;', consume it. */
12885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12886 cp_lexer_consume_token (parser
->lexer
);
12889 /* After the first time around, a function-definition is not
12890 allowed -- even if it was OK at first. For example:
12895 function_definition_allowed_p
= false;
12898 /* Issue an error message if no declarators are present, and the
12899 decl-specifier-seq does not itself declare a class or
12900 enumeration: [dcl.dcl]/3. */
12901 if (!saw_declarator
)
12903 if (cp_parser_declares_only_class_p (parser
))
12905 if (!declares_class_or_enum
12906 && decl_specifiers
.type
12907 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12908 /* Ensure an error is issued anyway when finish_decltype_type,
12909 called via cp_parser_decl_specifier_seq, returns a class or
12910 an enumeration (c++/51786). */
12911 decl_specifiers
.type
= NULL_TREE
;
12912 shadow_tag (&decl_specifiers
);
12914 /* Perform any deferred access checks. */
12915 perform_deferred_access_checks (tf_warning_or_error
);
12918 /* Consume the `;'. */
12920 if (!maybe_range_for_decl
)
12921 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12922 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12924 if (init_loc
!= UNKNOWN_LOCATION
)
12925 error_at (init_loc
, "initializer in range-based %<for%> loop");
12926 if (comma_loc
!= UNKNOWN_LOCATION
)
12927 error_at (comma_loc
,
12928 "multiple declarations in range-based %<for%> loop");
12932 pop_deferring_access_checks ();
12935 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12936 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12940 cp_parser_decomposition_declaration (cp_parser
*parser
,
12941 cp_decl_specifier_seq
*decl_specifiers
,
12942 tree
*maybe_range_for_decl
,
12943 location_t
*init_loc
)
12945 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
12946 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12947 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
12949 /* Parse the identifier-list. */
12950 auto_vec
<cp_expr
, 10> v
;
12951 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12954 cp_expr e
= cp_parser_identifier (parser
);
12955 if (e
.get_value () == error_mark_node
)
12958 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12960 cp_lexer_consume_token (parser
->lexer
);
12963 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12964 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
12966 end_loc
= UNKNOWN_LOCATION
;
12967 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
12969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12970 cp_lexer_consume_token (parser
->lexer
);
12973 cp_parser_skip_to_end_of_statement (parser
);
12974 return error_mark_node
;
12978 if (cxx_dialect
< cxx1z
)
12979 pedwarn (loc
, 0, "decomposition declaration only available with "
12980 "-std=c++1z or -std=gnu++1z");
12983 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
12984 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
12985 declarator
->id_loc
= loc
;
12986 if (ref_qual
!= REF_QUAL_NONE
)
12987 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
12988 ref_qual
== REF_QUAL_RVALUE
,
12990 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
12991 NULL_TREE
, decl_specifiers
->attributes
,
12993 tree orig_decl
= decl
;
12997 cp_decl_specifier_seq decl_specs
;
12998 clear_decl_specs (&decl_specs
);
12999 decl_specs
.type
= make_auto ();
13001 FOR_EACH_VEC_ELT (v
, i
, e
)
13004 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13006 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13007 declarator
->id_loc
= e
.get_location ();
13008 tree elt_pushed_scope
;
13009 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13010 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13011 if (decl2
== error_mark_node
)
13012 decl
= error_mark_node
;
13013 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13015 /* Ensure we've diagnosed redeclaration if we aren't creating
13017 gcc_assert (errorcount
);
13018 decl
= error_mark_node
;
13022 if (elt_pushed_scope
)
13023 pop_scope (elt_pushed_scope
);
13028 error_at (loc
, "empty decomposition declaration");
13029 decl
= error_mark_node
;
13032 if (maybe_range_for_decl
== NULL
13033 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13035 bool non_constant_p
= false, is_direct_init
= false;
13036 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13037 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13040 if (decl
!= error_mark_node
)
13042 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13043 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13044 cp_finish_decomp (decl
, prev
, v
.length ());
13047 else if (decl
!= error_mark_node
)
13049 *maybe_range_for_decl
= prev
;
13050 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13051 the underlying DECL. */
13052 cp_finish_decomp (decl
, prev
, v
.length ());
13056 pop_scope (pushed_scope
);
13058 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13060 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13061 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13067 /* Parse a decl-specifier-seq.
13069 decl-specifier-seq:
13070 decl-specifier-seq [opt] decl-specifier
13071 decl-specifier attribute-specifier-seq [opt] (C++11)
13074 storage-class-specifier
13085 Concepts Extension:
13090 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13092 The parser flags FLAGS is used to control type-specifier parsing.
13094 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13097 1: one of the decl-specifiers is an elaborated-type-specifier
13098 (i.e., a type declaration)
13099 2: one of the decl-specifiers is an enum-specifier or a
13100 class-specifier (i.e., a type definition)
13105 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13106 cp_parser_flags flags
,
13107 cp_decl_specifier_seq
*decl_specs
,
13108 int* declares_class_or_enum
)
13110 bool constructor_possible_p
= !parser
->in_declarator_p
;
13111 bool found_decl_spec
= false;
13112 cp_token
*start_token
= NULL
;
13115 /* Clear DECL_SPECS. */
13116 clear_decl_specs (decl_specs
);
13118 /* Assume no class or enumeration type is declared. */
13119 *declares_class_or_enum
= 0;
13121 /* Keep reading specifiers until there are no more to read. */
13124 bool constructor_p
;
13128 /* Peek at the next token. */
13129 token
= cp_lexer_peek_token (parser
->lexer
);
13131 /* Save the first token of the decl spec list for error
13134 start_token
= token
;
13135 /* Handle attributes. */
13136 if (cp_next_tokens_can_be_attribute_p (parser
))
13138 /* Parse the attributes. */
13139 tree attrs
= cp_parser_attributes_opt (parser
);
13141 /* In a sequence of declaration specifiers, c++11 attributes
13142 appertain to the type that precede them. In that case
13145 The attribute-specifier-seq affects the type only for
13146 the declaration it appears in, not other declarations
13147 involving the same type.
13149 But for now let's force the user to position the
13150 attribute either at the beginning of the declaration or
13151 after the declarator-id, which would clearly mean that it
13152 applies to the declarator. */
13153 if (cxx11_attribute_p (attrs
))
13155 if (!found_decl_spec
)
13156 /* The c++11 attribute is at the beginning of the
13157 declaration. It appertains to the entity being
13161 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13163 /* This is an attribute following a
13164 class-specifier. */
13165 if (decl_specs
->type_definition_p
)
13166 warn_misplaced_attr_for_class_type (token
->location
,
13172 decl_specs
->std_attributes
13173 = chainon (decl_specs
->std_attributes
,
13175 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13176 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13182 decl_specs
->attributes
13183 = chainon (decl_specs
->attributes
,
13185 if (decl_specs
->locations
[ds_attribute
] == 0)
13186 decl_specs
->locations
[ds_attribute
] = token
->location
;
13189 /* Assume we will find a decl-specifier keyword. */
13190 found_decl_spec
= true;
13191 /* If the next token is an appropriate keyword, we can simply
13192 add it to the list. */
13193 switch (token
->keyword
)
13199 if (!at_class_scope_p ())
13201 error_at (token
->location
, "%<friend%> used outside of class");
13202 cp_lexer_purge_token (parser
->lexer
);
13207 /* Consume the token. */
13208 cp_lexer_consume_token (parser
->lexer
);
13212 case RID_CONSTEXPR
:
13214 cp_lexer_consume_token (parser
->lexer
);
13219 cp_lexer_consume_token (parser
->lexer
);
13222 /* function-specifier:
13229 cp_parser_function_specifier_opt (parser
, decl_specs
);
13236 /* Consume the token. */
13237 cp_lexer_consume_token (parser
->lexer
);
13238 /* A constructor declarator cannot appear in a typedef. */
13239 constructor_possible_p
= false;
13240 /* The "typedef" keyword can only occur in a declaration; we
13241 may as well commit at this point. */
13242 cp_parser_commit_to_tentative_parse (parser
);
13244 if (decl_specs
->storage_class
!= sc_none
)
13245 decl_specs
->conflicting_specifiers_p
= true;
13248 /* storage-class-specifier:
13258 if (cxx_dialect
== cxx98
)
13260 /* Consume the token. */
13261 cp_lexer_consume_token (parser
->lexer
);
13263 /* Complain about `auto' as a storage specifier, if
13264 we're complaining about C++0x compatibility. */
13265 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
13266 " changes meaning in C++11; please remove it");
13268 /* Set the storage class anyway. */
13269 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13273 /* C++0x auto type-specifier. */
13274 found_decl_spec
= false;
13281 /* Consume the token. */
13282 cp_lexer_consume_token (parser
->lexer
);
13283 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13287 /* Consume the token. */
13289 cp_lexer_consume_token (parser
->lexer
);
13293 /* We did not yet find a decl-specifier yet. */
13294 found_decl_spec
= false;
13298 if (found_decl_spec
13299 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13300 && token
->keyword
!= RID_CONSTEXPR
)
13301 error ("decl-specifier invalid in condition");
13303 if (found_decl_spec
13304 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13305 && token
->keyword
!= RID_MUTABLE
13306 && token
->keyword
!= RID_CONSTEXPR
)
13307 error_at (token
->location
, "%qD invalid in lambda",
13308 ridpointers
[token
->keyword
]);
13311 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13313 /* Constructors are a special case. The `S' in `S()' is not a
13314 decl-specifier; it is the beginning of the declarator. */
13316 = (!found_decl_spec
13317 && constructor_possible_p
13318 && (cp_parser_constructor_declarator_p
13319 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13321 /* If we don't have a DECL_SPEC yet, then we must be looking at
13322 a type-specifier. */
13323 if (!found_decl_spec
&& !constructor_p
)
13325 int decl_spec_declares_class_or_enum
;
13326 bool is_cv_qualifier
;
13330 = cp_parser_type_specifier (parser
, flags
,
13332 /*is_declaration=*/true,
13333 &decl_spec_declares_class_or_enum
,
13335 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13337 /* If this type-specifier referenced a user-defined type
13338 (a typedef, class-name, etc.), then we can't allow any
13339 more such type-specifiers henceforth.
13343 The longest sequence of decl-specifiers that could
13344 possibly be a type name is taken as the
13345 decl-specifier-seq of a declaration. The sequence shall
13346 be self-consistent as described below.
13350 As a general rule, at most one type-specifier is allowed
13351 in the complete decl-specifier-seq of a declaration. The
13352 only exceptions are the following:
13354 -- const or volatile can be combined with any other
13357 -- signed or unsigned can be combined with char, long,
13365 void g (const int Pc);
13367 Here, Pc is *not* part of the decl-specifier seq; it's
13368 the declarator. Therefore, once we see a type-specifier
13369 (other than a cv-qualifier), we forbid any additional
13370 user-defined types. We *do* still allow things like `int
13371 int' to be considered a decl-specifier-seq, and issue the
13372 error message later. */
13373 if (type_spec
&& !is_cv_qualifier
)
13374 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13375 /* A constructor declarator cannot follow a type-specifier. */
13378 constructor_possible_p
= false;
13379 found_decl_spec
= true;
13380 if (!is_cv_qualifier
)
13381 decl_specs
->any_type_specifiers_p
= true;
13385 /* If we still do not have a DECL_SPEC, then there are no more
13386 decl-specifiers. */
13387 if (!found_decl_spec
)
13390 decl_specs
->any_specifiers_p
= true;
13391 /* After we see one decl-specifier, further decl-specifiers are
13392 always optional. */
13393 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13396 /* Don't allow a friend specifier with a class definition. */
13397 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13398 && (*declares_class_or_enum
& 2))
13399 error_at (decl_specs
->locations
[ds_friend
],
13400 "class definition may not be declared a friend");
13403 /* Parse an (optional) storage-class-specifier.
13405 storage-class-specifier:
13414 storage-class-specifier:
13417 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13420 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13422 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13425 if (cxx_dialect
!= cxx98
)
13427 /* Fall through for C++98. */
13428 gcc_fallthrough ();
13435 /* Consume the token. */
13436 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13443 /* Parse an (optional) function-specifier.
13445 function-specifier:
13450 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13451 Updates DECL_SPECS, if it is non-NULL. */
13454 cp_parser_function_specifier_opt (cp_parser
* parser
,
13455 cp_decl_specifier_seq
*decl_specs
)
13457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13458 switch (token
->keyword
)
13461 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13465 /* 14.5.2.3 [temp.mem]
13467 A member function template shall not be virtual. */
13468 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13469 && current_class_type
)
13470 error_at (token
->location
, "templates may not be %<virtual%>");
13472 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13476 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13483 /* Consume the token. */
13484 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13487 /* Parse a linkage-specification.
13489 linkage-specification:
13490 extern string-literal { declaration-seq [opt] }
13491 extern string-literal declaration */
13494 cp_parser_linkage_specification (cp_parser
* parser
)
13498 /* Look for the `extern' keyword. */
13499 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13501 /* Look for the string-literal. */
13502 linkage
= cp_parser_string_literal (parser
, false, false);
13504 /* Transform the literal into an identifier. If the literal is a
13505 wide-character string, or contains embedded NULs, then we can't
13506 handle it as the user wants. */
13507 if (strlen (TREE_STRING_POINTER (linkage
))
13508 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13510 cp_parser_error (parser
, "invalid linkage-specification");
13511 /* Assume C++ linkage. */
13512 linkage
= lang_name_cplusplus
;
13515 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13517 /* We're now using the new linkage. */
13518 push_lang_context (linkage
);
13520 /* If the next token is a `{', then we're using the first
13522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13524 cp_ensure_no_omp_declare_simd (parser
);
13525 cp_ensure_no_oacc_routine (parser
);
13527 /* Consume the `{' token. */
13528 cp_lexer_consume_token (parser
->lexer
);
13529 /* Parse the declarations. */
13530 cp_parser_declaration_seq_opt (parser
);
13531 /* Look for the closing `}'. */
13532 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13534 /* Otherwise, there's just one declaration. */
13537 bool saved_in_unbraced_linkage_specification_p
;
13539 saved_in_unbraced_linkage_specification_p
13540 = parser
->in_unbraced_linkage_specification_p
;
13541 parser
->in_unbraced_linkage_specification_p
= true;
13542 cp_parser_declaration (parser
);
13543 parser
->in_unbraced_linkage_specification_p
13544 = saved_in_unbraced_linkage_specification_p
;
13547 /* We're done with the linkage-specification. */
13548 pop_lang_context ();
13551 /* Parse a static_assert-declaration.
13553 static_assert-declaration:
13554 static_assert ( constant-expression , string-literal ) ;
13555 static_assert ( constant-expression ) ; (C++1Z)
13557 If MEMBER_P, this static_assert is a class member. */
13560 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13565 location_t saved_loc
;
13568 /* Peek at the `static_assert' token so we can keep track of exactly
13569 where the static assertion started. */
13570 token
= cp_lexer_peek_token (parser
->lexer
);
13571 saved_loc
= token
->location
;
13573 /* Look for the `static_assert' keyword. */
13574 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13578 /* We know we are in a static assertion; commit to any tentative
13580 if (cp_parser_parsing_tentatively (parser
))
13581 cp_parser_commit_to_tentative_parse (parser
);
13583 /* Parse the `(' starting the static assertion condition. */
13584 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
13586 /* Parse the constant-expression. Allow a non-constant expression
13587 here in order to give better diagnostics in finish_static_assert. */
13589 cp_parser_constant_expression (parser
,
13590 /*allow_non_constant_p=*/true,
13591 /*non_constant_p=*/&dummy
);
13593 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13595 if (cxx_dialect
< cxx1z
)
13596 pedwarn (input_location
, OPT_Wpedantic
,
13597 "static_assert without a message "
13598 "only available with -std=c++1z or -std=gnu++1z");
13600 cp_lexer_consume_token (parser
->lexer
);
13601 message
= build_string (1, "");
13602 TREE_TYPE (message
) = char_array_type_node
;
13603 fix_string_type (message
);
13607 /* Parse the separating `,'. */
13608 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13610 /* Parse the string-literal message. */
13611 message
= cp_parser_string_literal (parser
,
13612 /*translate=*/false,
13615 /* A `)' completes the static assertion. */
13616 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13617 cp_parser_skip_to_closing_parenthesis (parser
,
13618 /*recovering=*/true,
13619 /*or_comma=*/false,
13620 /*consume_paren=*/true);
13623 /* A semicolon terminates the declaration. */
13624 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13626 /* Complete the static assertion, which may mean either processing
13627 the static assert now or saving it for template instantiation. */
13628 finish_static_assert (condition
, message
, saved_loc
, member_p
);
13631 /* Parse the expression in decltype ( expression ). */
13634 cp_parser_decltype_expr (cp_parser
*parser
,
13635 bool &id_expression_or_member_access_p
)
13637 cp_token
*id_expr_start_token
;
13640 /* Since we're going to preserve any side-effects from this parse, set up a
13641 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13642 in the expression. */
13643 tentative_firewall
firewall (parser
);
13645 /* First, try parsing an id-expression. */
13646 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13647 cp_parser_parse_tentatively (parser
);
13648 expr
= cp_parser_id_expression (parser
,
13649 /*template_keyword_p=*/false,
13650 /*check_dependency_p=*/true,
13651 /*template_p=*/NULL
,
13652 /*declarator_p=*/false,
13653 /*optional_p=*/false);
13655 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13657 bool non_integral_constant_expression_p
= false;
13658 tree id_expression
= expr
;
13660 const char *error_msg
;
13662 if (identifier_p (expr
))
13663 /* Lookup the name we got back from the id-expression. */
13664 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13665 id_expr_start_token
->location
);
13668 && expr
!= error_mark_node
13669 && TREE_CODE (expr
) != TYPE_DECL
13670 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13671 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13672 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13674 /* Complete lookup of the id-expression. */
13675 expr
= (finish_id_expression
13676 (id_expression
, expr
, parser
->scope
, &idk
,
13677 /*integral_constant_expression_p=*/false,
13678 /*allow_non_integral_constant_expression_p=*/true,
13679 &non_integral_constant_expression_p
,
13680 /*template_p=*/false,
13682 /*address_p=*/false,
13683 /*template_arg_p=*/false,
13685 id_expr_start_token
->location
));
13687 if (expr
== error_mark_node
)
13688 /* We found an id-expression, but it was something that we
13689 should not have found. This is an error, not something
13690 we can recover from, so note that we found an
13691 id-expression and we'll recover as gracefully as
13693 id_expression_or_member_access_p
= true;
13697 && expr
!= error_mark_node
13698 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13699 /* We have an id-expression. */
13700 id_expression_or_member_access_p
= true;
13703 if (!id_expression_or_member_access_p
)
13705 /* Abort the id-expression parse. */
13706 cp_parser_abort_tentative_parse (parser
);
13708 /* Parsing tentatively, again. */
13709 cp_parser_parse_tentatively (parser
);
13711 /* Parse a class member access. */
13712 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13713 /*cast_p=*/false, /*decltype*/true,
13714 /*member_access_only_p=*/true, NULL
);
13717 && expr
!= error_mark_node
13718 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13719 /* We have an id-expression. */
13720 id_expression_or_member_access_p
= true;
13723 if (id_expression_or_member_access_p
)
13724 /* We have parsed the complete id-expression or member access. */
13725 cp_parser_parse_definitely (parser
);
13728 /* Abort our attempt to parse an id-expression or member access
13730 cp_parser_abort_tentative_parse (parser
);
13732 /* Parse a full expression. */
13733 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13734 /*decltype_p=*/true);
13740 /* Parse a `decltype' type. Returns the type.
13742 simple-type-specifier:
13743 decltype ( expression )
13745 decltype ( auto ) */
13748 cp_parser_decltype (cp_parser
*parser
)
13751 bool id_expression_or_member_access_p
= false;
13752 const char *saved_message
;
13753 bool saved_integral_constant_expression_p
;
13754 bool saved_non_integral_constant_expression_p
;
13755 bool saved_greater_than_is_operator_p
;
13756 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13758 if (start_token
->type
== CPP_DECLTYPE
)
13760 /* Already parsed. */
13761 cp_lexer_consume_token (parser
->lexer
);
13762 return saved_checks_value (start_token
->u
.tree_check_value
);
13765 /* Look for the `decltype' token. */
13766 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
13767 return error_mark_node
;
13769 /* Parse the opening `('. */
13770 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13771 return error_mark_node
;
13773 /* decltype (auto) */
13774 if (cxx_dialect
>= cxx14
13775 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
13777 cp_lexer_consume_token (parser
->lexer
);
13778 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13779 return error_mark_node
;
13780 expr
= make_decltype_auto ();
13781 AUTO_IS_DECLTYPE (expr
) = true;
13785 /* Types cannot be defined in a `decltype' expression. Save away the
13787 saved_message
= parser
->type_definition_forbidden_message
;
13789 /* And create the new one. */
13790 parser
->type_definition_forbidden_message
13791 = G_("types may not be defined in %<decltype%> expressions");
13793 /* The restrictions on constant-expressions do not apply inside
13794 decltype expressions. */
13795 saved_integral_constant_expression_p
13796 = parser
->integral_constant_expression_p
;
13797 saved_non_integral_constant_expression_p
13798 = parser
->non_integral_constant_expression_p
;
13799 parser
->integral_constant_expression_p
= false;
13801 /* Within a parenthesized expression, a `>' token is always
13802 the greater-than operator. */
13803 saved_greater_than_is_operator_p
13804 = parser
->greater_than_is_operator_p
;
13805 parser
->greater_than_is_operator_p
= true;
13807 /* Do not actually evaluate the expression. */
13808 ++cp_unevaluated_operand
;
13810 /* Do not warn about problems with the expression. */
13811 ++c_inhibit_evaluation_warnings
;
13813 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
13815 /* Go back to evaluating expressions. */
13816 --cp_unevaluated_operand
;
13817 --c_inhibit_evaluation_warnings
;
13819 /* The `>' token might be the end of a template-id or
13820 template-parameter-list now. */
13821 parser
->greater_than_is_operator_p
13822 = saved_greater_than_is_operator_p
;
13824 /* Restore the old message and the integral constant expression
13826 parser
->type_definition_forbidden_message
= saved_message
;
13827 parser
->integral_constant_expression_p
13828 = saved_integral_constant_expression_p
;
13829 parser
->non_integral_constant_expression_p
13830 = saved_non_integral_constant_expression_p
;
13832 /* Parse to the closing `)'. */
13833 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13835 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13836 /*consume_paren=*/true);
13837 return error_mark_node
;
13840 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
13841 tf_warning_or_error
);
13844 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13846 start_token
->type
= CPP_DECLTYPE
;
13847 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13848 start_token
->u
.tree_check_value
->value
= expr
;
13849 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13850 start_token
->keyword
= RID_MAX
;
13851 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
13856 /* Special member functions [gram.special] */
13858 /* Parse a conversion-function-id.
13860 conversion-function-id:
13861 operator conversion-type-id
13863 Returns an IDENTIFIER_NODE representing the operator. */
13866 cp_parser_conversion_function_id (cp_parser
* parser
)
13870 tree saved_qualifying_scope
;
13871 tree saved_object_scope
;
13872 tree pushed_scope
= NULL_TREE
;
13874 /* Look for the `operator' token. */
13875 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13876 return error_mark_node
;
13877 /* When we parse the conversion-type-id, the current scope will be
13878 reset. However, we need that information in able to look up the
13879 conversion function later, so we save it here. */
13880 saved_scope
= parser
->scope
;
13881 saved_qualifying_scope
= parser
->qualifying_scope
;
13882 saved_object_scope
= parser
->object_scope
;
13883 /* We must enter the scope of the class so that the names of
13884 entities declared within the class are available in the
13885 conversion-type-id. For example, consider:
13892 S::operator I() { ... }
13894 In order to see that `I' is a type-name in the definition, we
13895 must be in the scope of `S'. */
13897 pushed_scope
= push_scope (saved_scope
);
13898 /* Parse the conversion-type-id. */
13899 type
= cp_parser_conversion_type_id (parser
);
13900 /* Leave the scope of the class, if any. */
13902 pop_scope (pushed_scope
);
13903 /* Restore the saved scope. */
13904 parser
->scope
= saved_scope
;
13905 parser
->qualifying_scope
= saved_qualifying_scope
;
13906 parser
->object_scope
= saved_object_scope
;
13907 /* If the TYPE is invalid, indicate failure. */
13908 if (type
== error_mark_node
)
13909 return error_mark_node
;
13910 return mangle_conv_op_name_for_type (type
);
13913 /* Parse a conversion-type-id:
13915 conversion-type-id:
13916 type-specifier-seq conversion-declarator [opt]
13918 Returns the TYPE specified. */
13921 cp_parser_conversion_type_id (cp_parser
* parser
)
13924 cp_decl_specifier_seq type_specifiers
;
13925 cp_declarator
*declarator
;
13926 tree type_specified
;
13927 const char *saved_message
;
13929 /* Parse the attributes. */
13930 attributes
= cp_parser_attributes_opt (parser
);
13932 saved_message
= parser
->type_definition_forbidden_message
;
13933 parser
->type_definition_forbidden_message
13934 = G_("types may not be defined in a conversion-type-id");
13936 /* Parse the type-specifiers. */
13937 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13938 /*is_trailing_return=*/false,
13941 parser
->type_definition_forbidden_message
= saved_message
;
13943 /* If that didn't work, stop. */
13944 if (type_specifiers
.type
== error_mark_node
)
13945 return error_mark_node
;
13946 /* Parse the conversion-declarator. */
13947 declarator
= cp_parser_conversion_declarator_opt (parser
);
13949 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
13950 /*initialized=*/0, &attributes
);
13952 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
13954 /* Don't give this error when parsing tentatively. This happens to
13955 work because we always parse this definitively once. */
13956 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
13957 && type_uses_auto (type_specified
))
13959 if (cxx_dialect
< cxx14
)
13961 error ("invalid use of %<auto%> in conversion operator");
13962 return error_mark_node
;
13964 else if (template_parm_scope_p ())
13965 warning (0, "use of %<auto%> in member template "
13966 "conversion operator can never be deduced");
13969 return type_specified
;
13972 /* Parse an (optional) conversion-declarator.
13974 conversion-declarator:
13975 ptr-operator conversion-declarator [opt]
13979 static cp_declarator
*
13980 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
13982 enum tree_code code
;
13983 tree class_type
, std_attributes
= NULL_TREE
;
13984 cp_cv_quals cv_quals
;
13986 /* We don't know if there's a ptr-operator next, or not. */
13987 cp_parser_parse_tentatively (parser
);
13988 /* Try the ptr-operator. */
13989 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
13991 /* If it worked, look for more conversion-declarators. */
13992 if (cp_parser_parse_definitely (parser
))
13994 cp_declarator
*declarator
;
13996 /* Parse another optional declarator. */
13997 declarator
= cp_parser_conversion_declarator_opt (parser
);
13999 declarator
= cp_parser_make_indirect_declarator
14000 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14008 /* Parse an (optional) ctor-initializer.
14011 : mem-initializer-list
14013 Returns TRUE iff the ctor-initializer was actually present. */
14016 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14018 /* If the next token is not a `:', then there is no
14019 ctor-initializer. */
14020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14022 /* Do default initialization of any bases and members. */
14023 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14024 finish_mem_initializers (NULL_TREE
);
14029 /* Consume the `:' token. */
14030 cp_lexer_consume_token (parser
->lexer
);
14031 /* And the mem-initializer-list. */
14032 cp_parser_mem_initializer_list (parser
);
14037 /* Parse a mem-initializer-list.
14039 mem-initializer-list:
14040 mem-initializer ... [opt]
14041 mem-initializer ... [opt] , mem-initializer-list */
14044 cp_parser_mem_initializer_list (cp_parser
* parser
)
14046 tree mem_initializer_list
= NULL_TREE
;
14047 tree target_ctor
= error_mark_node
;
14048 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14050 /* Let the semantic analysis code know that we are starting the
14051 mem-initializer-list. */
14052 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14053 error_at (token
->location
,
14054 "only constructors take member initializers");
14056 /* Loop through the list. */
14059 tree mem_initializer
;
14061 token
= cp_lexer_peek_token (parser
->lexer
);
14062 /* Parse the mem-initializer. */
14063 mem_initializer
= cp_parser_mem_initializer (parser
);
14064 /* If the next token is a `...', we're expanding member initializers. */
14065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14067 /* Consume the `...'. */
14068 cp_lexer_consume_token (parser
->lexer
);
14070 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14071 can be expanded but members cannot. */
14072 if (mem_initializer
!= error_mark_node
14073 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14075 error_at (token
->location
,
14076 "cannot expand initializer for member %<%D%>",
14077 TREE_PURPOSE (mem_initializer
));
14078 mem_initializer
= error_mark_node
;
14081 /* Construct the pack expansion type. */
14082 if (mem_initializer
!= error_mark_node
)
14083 mem_initializer
= make_pack_expansion (mem_initializer
);
14085 if (target_ctor
!= error_mark_node
14086 && mem_initializer
!= error_mark_node
)
14088 error ("mem-initializer for %qD follows constructor delegation",
14089 TREE_PURPOSE (mem_initializer
));
14090 mem_initializer
= error_mark_node
;
14092 /* Look for a target constructor. */
14093 if (mem_initializer
!= error_mark_node
14094 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14095 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14097 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14098 if (mem_initializer_list
)
14100 error ("constructor delegation follows mem-initializer for %qD",
14101 TREE_PURPOSE (mem_initializer_list
));
14102 mem_initializer
= error_mark_node
;
14104 target_ctor
= mem_initializer
;
14106 /* Add it to the list, unless it was erroneous. */
14107 if (mem_initializer
!= error_mark_node
)
14109 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14110 mem_initializer_list
= mem_initializer
;
14112 /* If the next token is not a `,', we're done. */
14113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14115 /* Consume the `,' token. */
14116 cp_lexer_consume_token (parser
->lexer
);
14119 /* Perform semantic analysis. */
14120 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14121 finish_mem_initializers (mem_initializer_list
);
14124 /* Parse a mem-initializer.
14127 mem-initializer-id ( expression-list [opt] )
14128 mem-initializer-id braced-init-list
14133 ( expression-list [opt] )
14135 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14136 class) or FIELD_DECL (for a non-static data member) to initialize;
14137 the TREE_VALUE is the expression-list. An empty initialization
14138 list is represented by void_list_node. */
14141 cp_parser_mem_initializer (cp_parser
* parser
)
14143 tree mem_initializer_id
;
14144 tree expression_list
;
14146 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14148 /* Find out what is being initialized. */
14149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14151 permerror (token
->location
,
14152 "anachronistic old-style base class initializer");
14153 mem_initializer_id
= NULL_TREE
;
14157 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14158 if (mem_initializer_id
== error_mark_node
)
14159 return mem_initializer_id
;
14161 member
= expand_member_init (mem_initializer_id
);
14162 if (member
&& !DECL_P (member
))
14163 in_base_initializer
= 1;
14165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14167 bool expr_non_constant_p
;
14168 cp_lexer_set_source_position (parser
->lexer
);
14169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14170 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14171 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14172 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14176 vec
<tree
, va_gc
> *vec
;
14177 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14179 /*allow_expansion_p=*/true,
14180 /*non_constant_p=*/NULL
);
14182 return error_mark_node
;
14183 expression_list
= build_tree_list_vec (vec
);
14184 release_tree_vector (vec
);
14187 if (expression_list
== error_mark_node
)
14188 return error_mark_node
;
14189 if (!expression_list
)
14190 expression_list
= void_type_node
;
14192 in_base_initializer
= 0;
14194 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14197 /* Parse a mem-initializer-id.
14199 mem-initializer-id:
14200 :: [opt] nested-name-specifier [opt] class-name
14201 decltype-specifier (C++11)
14204 Returns a TYPE indicating the class to be initialized for the first
14205 production (and the second in C++11). Returns an IDENTIFIER_NODE
14206 indicating the data member to be initialized for the last production. */
14209 cp_parser_mem_initializer_id (cp_parser
* parser
)
14211 bool global_scope_p
;
14212 bool nested_name_specifier_p
;
14213 bool template_p
= false;
14216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14218 /* `typename' is not allowed in this context ([temp.res]). */
14219 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14221 error_at (token
->location
,
14222 "keyword %<typename%> not allowed in this context (a qualified "
14223 "member initializer is implicitly a type)");
14224 cp_lexer_consume_token (parser
->lexer
);
14226 /* Look for the optional `::' operator. */
14228 = (cp_parser_global_scope_opt (parser
,
14229 /*current_scope_valid_p=*/false)
14231 /* Look for the optional nested-name-specifier. The simplest way to
14236 The keyword `typename' is not permitted in a base-specifier or
14237 mem-initializer; in these contexts a qualified name that
14238 depends on a template-parameter is implicitly assumed to be a
14241 is to assume that we have seen the `typename' keyword at this
14243 nested_name_specifier_p
14244 = (cp_parser_nested_name_specifier_opt (parser
,
14245 /*typename_keyword_p=*/true,
14246 /*check_dependency_p=*/true,
14248 /*is_declaration=*/true)
14250 if (nested_name_specifier_p
)
14251 template_p
= cp_parser_optional_template_keyword (parser
);
14252 /* If there is a `::' operator or a nested-name-specifier, then we
14253 are definitely looking for a class-name. */
14254 if (global_scope_p
|| nested_name_specifier_p
)
14255 return cp_parser_class_name (parser
,
14256 /*typename_keyword_p=*/true,
14257 /*template_keyword_p=*/template_p
,
14259 /*check_dependency_p=*/true,
14260 /*class_head_p=*/false,
14261 /*is_declaration=*/true);
14262 /* Otherwise, we could also be looking for an ordinary identifier. */
14263 cp_parser_parse_tentatively (parser
);
14264 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14265 /* Try a decltype-specifier. */
14266 id
= cp_parser_decltype (parser
);
14268 /* Otherwise, try a class-name. */
14269 id
= cp_parser_class_name (parser
,
14270 /*typename_keyword_p=*/true,
14271 /*template_keyword_p=*/false,
14273 /*check_dependency_p=*/true,
14274 /*class_head_p=*/false,
14275 /*is_declaration=*/true);
14276 /* If we found one, we're done. */
14277 if (cp_parser_parse_definitely (parser
))
14279 /* Otherwise, look for an ordinary identifier. */
14280 return cp_parser_identifier (parser
);
14283 /* Overloading [gram.over] */
14285 /* Parse an operator-function-id.
14287 operator-function-id:
14290 Returns an IDENTIFIER_NODE for the operator which is a
14291 human-readable spelling of the identifier, e.g., `operator +'. */
14294 cp_parser_operator_function_id (cp_parser
* parser
)
14296 /* Look for the `operator' keyword. */
14297 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14298 return error_mark_node
;
14299 /* And then the name of the operator itself. */
14300 return cp_parser_operator (parser
);
14303 /* Return an identifier node for a user-defined literal operator.
14304 The suffix identifier is chained to the operator name identifier. */
14307 cp_literal_operator_id (const char* name
)
14310 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14311 + strlen (name
) + 10);
14312 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14313 identifier
= get_identifier (buffer
);
14318 /* Parse an operator.
14321 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14322 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14323 || ++ -- , ->* -> () []
14330 Returns an IDENTIFIER_NODE for the operator which is a
14331 human-readable spelling of the identifier, e.g., `operator +'. */
14334 cp_parser_operator (cp_parser
* parser
)
14336 tree id
= NULL_TREE
;
14340 /* Peek at the next token. */
14341 token
= cp_lexer_peek_token (parser
->lexer
);
14343 location_t start_loc
= token
->location
;
14345 /* Figure out which operator we have. */
14346 switch (token
->type
)
14352 /* The keyword should be either `new' or `delete'. */
14353 if (token
->keyword
== RID_NEW
)
14355 else if (token
->keyword
== RID_DELETE
)
14360 /* Consume the `new' or `delete' token. */
14361 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14363 /* Peek at the next token. */
14364 token
= cp_lexer_peek_token (parser
->lexer
);
14365 /* If it's a `[' token then this is the array variant of the
14367 if (token
->type
== CPP_OPEN_SQUARE
)
14369 /* Consume the `[' token. */
14370 cp_lexer_consume_token (parser
->lexer
);
14371 /* Look for the `]' token. */
14372 if (cp_token
*close_token
14373 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14374 end_loc
= close_token
->location
;
14375 id
= cp_operator_id (op
== NEW_EXPR
14376 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
14378 /* Otherwise, we have the non-array variant. */
14380 id
= cp_operator_id (op
);
14382 location_t loc
= make_location (start_loc
, start_loc
, end_loc
);
14384 return cp_expr (id
, loc
);
14388 id
= cp_operator_id (PLUS_EXPR
);
14392 id
= cp_operator_id (MINUS_EXPR
);
14396 id
= cp_operator_id (MULT_EXPR
);
14400 id
= cp_operator_id (TRUNC_DIV_EXPR
);
14404 id
= cp_operator_id (TRUNC_MOD_EXPR
);
14408 id
= cp_operator_id (BIT_XOR_EXPR
);
14412 id
= cp_operator_id (BIT_AND_EXPR
);
14416 id
= cp_operator_id (BIT_IOR_EXPR
);
14420 id
= cp_operator_id (BIT_NOT_EXPR
);
14424 id
= cp_operator_id (TRUTH_NOT_EXPR
);
14428 id
= cp_assignment_operator_id (NOP_EXPR
);
14432 id
= cp_operator_id (LT_EXPR
);
14436 id
= cp_operator_id (GT_EXPR
);
14440 id
= cp_assignment_operator_id (PLUS_EXPR
);
14444 id
= cp_assignment_operator_id (MINUS_EXPR
);
14448 id
= cp_assignment_operator_id (MULT_EXPR
);
14452 id
= cp_assignment_operator_id (TRUNC_DIV_EXPR
);
14456 id
= cp_assignment_operator_id (TRUNC_MOD_EXPR
);
14460 id
= cp_assignment_operator_id (BIT_XOR_EXPR
);
14464 id
= cp_assignment_operator_id (BIT_AND_EXPR
);
14468 id
= cp_assignment_operator_id (BIT_IOR_EXPR
);
14472 id
= cp_operator_id (LSHIFT_EXPR
);
14476 id
= cp_operator_id (RSHIFT_EXPR
);
14479 case CPP_LSHIFT_EQ
:
14480 id
= cp_assignment_operator_id (LSHIFT_EXPR
);
14483 case CPP_RSHIFT_EQ
:
14484 id
= cp_assignment_operator_id (RSHIFT_EXPR
);
14488 id
= cp_operator_id (EQ_EXPR
);
14492 id
= cp_operator_id (NE_EXPR
);
14496 id
= cp_operator_id (LE_EXPR
);
14499 case CPP_GREATER_EQ
:
14500 id
= cp_operator_id (GE_EXPR
);
14504 id
= cp_operator_id (TRUTH_ANDIF_EXPR
);
14508 id
= cp_operator_id (TRUTH_ORIF_EXPR
);
14511 case CPP_PLUS_PLUS
:
14512 id
= cp_operator_id (POSTINCREMENT_EXPR
);
14515 case CPP_MINUS_MINUS
:
14516 id
= cp_operator_id (PREDECREMENT_EXPR
);
14520 id
= cp_operator_id (COMPOUND_EXPR
);
14523 case CPP_DEREF_STAR
:
14524 id
= cp_operator_id (MEMBER_REF
);
14528 id
= cp_operator_id (COMPONENT_REF
);
14531 case CPP_OPEN_PAREN
:
14532 /* Consume the `('. */
14533 cp_lexer_consume_token (parser
->lexer
);
14534 /* Look for the matching `)'. */
14535 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14536 return cp_operator_id (CALL_EXPR
);
14538 case CPP_OPEN_SQUARE
:
14539 /* Consume the `['. */
14540 cp_lexer_consume_token (parser
->lexer
);
14541 /* Look for the matching `]'. */
14542 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14543 return cp_operator_id (ARRAY_REF
);
14545 case CPP_UTF8STRING
:
14546 case CPP_UTF8STRING_USERDEF
:
14553 case CPP_STRING_USERDEF
:
14554 case CPP_WSTRING_USERDEF
:
14555 case CPP_STRING16_USERDEF
:
14556 case CPP_STRING32_USERDEF
:
14558 tree str
, string_tree
;
14561 if (cxx_dialect
== cxx98
)
14562 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14564 /* Consume the string. */
14565 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14566 /*wide_ok=*/true, /*lookup_udlit=*/false);
14567 if (str
== error_mark_node
)
14568 return error_mark_node
;
14569 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14571 string_tree
= USERDEF_LITERAL_VALUE (str
);
14572 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14577 /* Look for the suffix identifier. */
14578 token
= cp_lexer_peek_token (parser
->lexer
);
14579 if (token
->type
== CPP_NAME
)
14580 id
= cp_parser_identifier (parser
);
14581 else if (token
->type
== CPP_KEYWORD
)
14583 error ("unexpected keyword;"
14584 " remove space between quotes and suffix identifier");
14585 return error_mark_node
;
14589 error ("expected suffix identifier");
14590 return error_mark_node
;
14593 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14594 (TREE_TYPE (TREE_TYPE (string_tree
))));
14595 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14598 error ("expected empty string after %<operator%> keyword");
14599 return error_mark_node
;
14601 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14604 error ("invalid encoding prefix in literal operator");
14605 return error_mark_node
;
14607 if (id
!= error_mark_node
)
14609 const char *name
= IDENTIFIER_POINTER (id
);
14610 id
= cp_literal_operator_id (name
);
14616 /* Anything else is an error. */
14620 /* If we have selected an identifier, we need to consume the
14623 cp_lexer_consume_token (parser
->lexer
);
14624 /* Otherwise, no valid operator name was present. */
14627 cp_parser_error (parser
, "expected operator");
14628 id
= error_mark_node
;
14631 return cp_expr (id
, start_loc
);
14634 /* Parse a template-declaration.
14636 template-declaration:
14637 export [opt] template < template-parameter-list > declaration
14639 If MEMBER_P is TRUE, this template-declaration occurs within a
14642 The grammar rule given by the standard isn't correct. What
14643 is really meant is:
14645 template-declaration:
14646 export [opt] template-parameter-list-seq
14647 decl-specifier-seq [opt] init-declarator [opt] ;
14648 export [opt] template-parameter-list-seq
14649 function-definition
14651 template-parameter-list-seq:
14652 template-parameter-list-seq [opt]
14653 template < template-parameter-list >
14655 Concept Extensions:
14657 template-parameter-list-seq:
14658 template < template-parameter-list > requires-clause [opt]
14661 requires logical-or-expression */
14664 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14666 /* Check for `export'. */
14667 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14669 /* Consume the `export' token. */
14670 cp_lexer_consume_token (parser
->lexer
);
14671 /* Warn that we do not support `export'. */
14672 warning (0, "keyword %<export%> not implemented, and will be ignored");
14675 cp_parser_template_declaration_after_export (parser
, member_p
);
14678 /* Parse a template-parameter-list.
14680 template-parameter-list:
14682 template-parameter-list , template-parameter
14684 Returns a TREE_LIST. Each node represents a template parameter.
14685 The nodes are connected via their TREE_CHAINs. */
14688 cp_parser_template_parameter_list (cp_parser
* parser
)
14690 tree parameter_list
= NULL_TREE
;
14692 begin_template_parm_list ();
14694 /* The loop below parses the template parms. We first need to know
14695 the total number of template parms to be able to compute proper
14696 canonical types of each dependent type. So after the loop, when
14697 we know the total number of template parms,
14698 end_template_parm_list computes the proper canonical types and
14699 fixes up the dependent types accordingly. */
14704 bool is_parameter_pack
;
14705 location_t parm_loc
;
14707 /* Parse the template-parameter. */
14708 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14709 parameter
= cp_parser_template_parameter (parser
,
14711 &is_parameter_pack
);
14712 /* Add it to the list. */
14713 if (parameter
!= error_mark_node
)
14714 parameter_list
= process_template_parm (parameter_list
,
14718 is_parameter_pack
);
14721 tree err_parm
= build_tree_list (parameter
, parameter
);
14722 parameter_list
= chainon (parameter_list
, err_parm
);
14725 /* If the next token is not a `,', we're done. */
14726 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14728 /* Otherwise, consume the `,' token. */
14729 cp_lexer_consume_token (parser
->lexer
);
14732 return end_template_parm_list (parameter_list
);
14735 /* Parse a introduction-list.
14738 introduced-parameter
14739 introduction-list , introduced-parameter
14741 introduced-parameter:
14742 ...[opt] identifier
14744 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14745 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14746 WILDCARD_DECL will also have DECL_NAME set and token location in
14747 DECL_SOURCE_LOCATION. */
14750 cp_parser_introduction_list (cp_parser
*parser
)
14752 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
14756 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14758 cp_lexer_consume_token (parser
->lexer
);
14760 /* Build placeholder. */
14761 tree parm
= build_nt (WILDCARD_DECL
);
14762 DECL_SOURCE_LOCATION (parm
)
14763 = cp_lexer_peek_token (parser
->lexer
)->location
;
14764 DECL_NAME (parm
) = cp_parser_identifier (parser
);
14765 WILDCARD_PACK_P (parm
) = is_pack
;
14766 vec_safe_push (introduction_vec
, parm
);
14768 /* If the next token is not a `,', we're done. */
14769 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14771 /* Otherwise, consume the `,' token. */
14772 cp_lexer_consume_token (parser
->lexer
);
14775 /* Convert the vec into a TREE_VEC. */
14776 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
14779 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
14780 TREE_VEC_ELT (introduction_list
, n
) = parm
;
14782 release_tree_vector (introduction_vec
);
14783 return introduction_list
;
14786 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14787 is an abstract declarator. */
14789 static inline cp_declarator
*
14790 get_id_declarator (cp_declarator
*declarator
)
14792 cp_declarator
*d
= declarator
;
14793 while (d
&& d
->kind
!= cdk_id
)
14798 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14799 is an abstract declarator. */
14802 get_unqualified_id (cp_declarator
*declarator
)
14804 declarator
= get_id_declarator (declarator
);
14806 return declarator
->u
.id
.unqualified_name
;
14811 /* Returns true if DECL represents a constrained-parameter. */
14814 is_constrained_parameter (tree decl
)
14817 && TREE_CODE (decl
) == TYPE_DECL
14818 && CONSTRAINED_PARM_CONCEPT (decl
)
14819 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
14822 /* Returns true if PARM declares a constrained-parameter. */
14825 is_constrained_parameter (cp_parameter_declarator
*parm
)
14827 return is_constrained_parameter (parm
->decl_specifiers
.type
);
14830 /* Check that the type parameter is only a declarator-id, and that its
14831 type is not cv-qualified. */
14834 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
14835 cp_parameter_declarator
*parm
)
14837 if (!parm
->declarator
)
14840 if (parm
->declarator
->kind
!= cdk_id
)
14842 cp_parser_error (parser
, "invalid constrained type parameter");
14846 /* Don't allow cv-qualified type parameters. */
14847 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
14848 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
14850 cp_parser_error (parser
, "cv-qualified type parameter");
14857 /* Finish parsing/processing a template type parameter and checking
14858 various restrictions. */
14861 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
14863 cp_parameter_declarator
* parmdecl
)
14865 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14866 return finish_template_type_parm (class_type_node
, id
);
14868 return error_mark_node
;
14872 finish_constrained_template_template_parm (tree proto
, tree id
)
14874 /* FIXME: This should probably be copied, and we may need to adjust
14875 the template parameter depths. */
14876 tree saved_parms
= current_template_parms
;
14877 begin_template_parm_list ();
14878 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
14879 end_template_parm_list ();
14881 tree parm
= finish_template_template_parm (class_type_node
, id
);
14882 current_template_parms
= saved_parms
;
14887 /* Finish parsing/processing a template template parameter by borrowing
14888 the template parameter list from the prototype parameter. */
14891 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
14894 cp_parameter_declarator
*parmdecl
)
14896 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14897 return error_mark_node
;
14898 return finish_constrained_template_template_parm (proto
, id
);
14901 /* Create a new non-type template parameter from the given PARM
14905 constrained_non_type_template_parm (bool *is_non_type
,
14906 cp_parameter_declarator
*parm
)
14908 *is_non_type
= true;
14909 cp_declarator
*decl
= parm
->declarator
;
14910 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
14911 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
14912 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
14915 /* Build a constrained template parameter based on the PARMDECL
14916 declarator. The type of PARMDECL is the constrained type, which
14917 refers to the prototype template parameter that ultimately
14918 specifies the type of the declared parameter. */
14921 finish_constrained_parameter (cp_parser
*parser
,
14922 cp_parameter_declarator
*parmdecl
,
14924 bool *is_parameter_pack
)
14926 tree decl
= parmdecl
->decl_specifiers
.type
;
14927 tree id
= get_unqualified_id (parmdecl
->declarator
);
14928 tree def
= parmdecl
->default_argument
;
14929 tree proto
= DECL_INITIAL (decl
);
14931 /* A template parameter constrained by a variadic concept shall also
14932 be declared as a template parameter pack. */
14933 bool is_variadic
= template_parameter_pack_p (proto
);
14934 if (is_variadic
&& !*is_parameter_pack
)
14935 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
14937 /* Build the parameter. Return an error if the declarator was invalid. */
14939 if (TREE_CODE (proto
) == TYPE_DECL
)
14940 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
14941 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
14942 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
14945 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
14946 if (parm
== error_mark_node
)
14947 return error_mark_node
;
14949 /* Finish the parameter decl and create a node attaching the
14950 default argument and constraint. */
14951 parm
= build_tree_list (def
, parm
);
14952 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
14957 /* Returns true if the parsed type actually represents the declaration
14958 of a type template-parameter. */
14961 declares_constrained_type_template_parameter (tree type
)
14963 return (is_constrained_parameter (type
)
14964 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
14968 /* Returns true if the parsed type actually represents the declaration of
14969 a template template-parameter. */
14972 declares_constrained_template_template_parameter (tree type
)
14974 return (is_constrained_parameter (type
)
14975 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
14978 /* Parse a default argument for a type template-parameter.
14979 Note that diagnostics are handled in cp_parser_template_parameter. */
14982 cp_parser_default_type_template_argument (cp_parser
*parser
)
14984 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14986 /* Consume the `=' token. */
14987 cp_lexer_consume_token (parser
->lexer
);
14989 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14991 /* Parse the default-argument. */
14992 push_deferring_access_checks (dk_no_deferred
);
14993 tree default_argument
= cp_parser_type_id (parser
);
14994 pop_deferring_access_checks ();
14996 if (flag_concepts
&& type_uses_auto (default_argument
))
14998 error_at (token
->location
,
14999 "invalid use of %<auto%> in default template argument");
15000 return error_mark_node
;
15003 return default_argument
;
15006 /* Parse a default argument for a template template-parameter. */
15009 cp_parser_default_template_template_argument (cp_parser
*parser
)
15011 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15015 /* Consume the `='. */
15016 cp_lexer_consume_token (parser
->lexer
);
15017 /* Parse the id-expression. */
15018 push_deferring_access_checks (dk_no_deferred
);
15019 /* save token before parsing the id-expression, for error
15021 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15022 tree default_argument
15023 = cp_parser_id_expression (parser
,
15024 /*template_keyword_p=*/false,
15025 /*check_dependency_p=*/true,
15026 /*template_p=*/&is_template
,
15027 /*declarator_p=*/false,
15028 /*optional_p=*/false);
15029 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15030 /* If the id-expression was a template-id that refers to
15031 a template-class, we already have the declaration here,
15032 so no further lookup is needed. */
15035 /* Look up the name. */
15037 = cp_parser_lookup_name (parser
, default_argument
,
15039 /*is_template=*/is_template
,
15040 /*is_namespace=*/false,
15041 /*check_dependency=*/true,
15042 /*ambiguous_decls=*/NULL
,
15044 /* See if the default argument is valid. */
15045 default_argument
= check_template_template_default_arg (default_argument
);
15046 pop_deferring_access_checks ();
15047 return default_argument
;
15050 /* Parse a template-parameter.
15052 template-parameter:
15054 parameter-declaration
15056 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15057 the parameter. The TREE_PURPOSE is the default value, if any.
15058 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15059 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15060 set to true iff this parameter is a parameter pack. */
15063 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15064 bool *is_parameter_pack
)
15067 cp_parameter_declarator
*parameter_declarator
;
15070 /* Assume it is a type parameter or a template parameter. */
15071 *is_non_type
= false;
15072 /* Assume it not a parameter pack. */
15073 *is_parameter_pack
= false;
15074 /* Peek at the next token. */
15075 token
= cp_lexer_peek_token (parser
->lexer
);
15076 /* If it is `class' or `template', we have a type-parameter. */
15077 if (token
->keyword
== RID_TEMPLATE
)
15078 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15079 /* If it is `class' or `typename' we do not know yet whether it is a
15080 type parameter or a non-type parameter. Consider:
15082 template <typename T, typename T::X X> ...
15086 template <class C, class D*> ...
15088 Here, the first parameter is a type parameter, and the second is
15089 a non-type parameter. We can tell by looking at the token after
15090 the identifier -- if it is a `,', `=', or `>' then we have a type
15092 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15094 /* Peek at the token after `class' or `typename'. */
15095 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15096 /* If it's an ellipsis, we have a template type parameter
15098 if (token
->type
== CPP_ELLIPSIS
)
15099 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15100 /* If it's an identifier, skip it. */
15101 if (token
->type
== CPP_NAME
)
15102 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15103 /* Now, see if the token looks like the end of a template
15105 if (token
->type
== CPP_COMMA
15106 || token
->type
== CPP_EQ
15107 || token
->type
== CPP_GREATER
)
15108 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15111 /* Otherwise, it is a non-type parameter or a constrained parameter.
15115 When parsing a default template-argument for a non-type
15116 template-parameter, the first non-nested `>' is taken as the end
15117 of the template parameter-list rather than a greater-than
15119 parameter_declarator
15120 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15121 /*parenthesized_p=*/NULL
);
15123 if (!parameter_declarator
)
15124 return error_mark_node
;
15126 /* If the parameter declaration is marked as a parameter pack, set
15127 *IS_PARAMETER_PACK to notify the caller. */
15128 if (parameter_declarator
->template_parameter_pack_p
)
15129 *is_parameter_pack
= true;
15131 if (parameter_declarator
->default_argument
)
15133 /* Can happen in some cases of erroneous input (c++/34892). */
15134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15135 /* Consume the `...' for better error recovery. */
15136 cp_lexer_consume_token (parser
->lexer
);
15139 // The parameter may have been constrained.
15140 if (is_constrained_parameter (parameter_declarator
))
15141 return finish_constrained_parameter (parser
,
15142 parameter_declarator
,
15144 is_parameter_pack
);
15146 // Now we're sure that the parameter is a non-type parameter.
15147 *is_non_type
= true;
15149 parm
= grokdeclarator (parameter_declarator
->declarator
,
15150 ¶meter_declarator
->decl_specifiers
,
15151 TPARM
, /*initialized=*/0,
15152 /*attrlist=*/NULL
);
15153 if (parm
== error_mark_node
)
15154 return error_mark_node
;
15156 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15159 /* Parse a type-parameter.
15162 class identifier [opt]
15163 class identifier [opt] = type-id
15164 typename identifier [opt]
15165 typename identifier [opt] = type-id
15166 template < template-parameter-list > class identifier [opt]
15167 template < template-parameter-list > class identifier [opt]
15170 GNU Extension (variadic templates):
15173 class ... identifier [opt]
15174 typename ... identifier [opt]
15176 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15177 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15178 the declaration of the parameter.
15180 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15183 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15188 /* Look for a keyword to tell us what kind of parameter this is. */
15189 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15191 return error_mark_node
;
15193 switch (token
->keyword
)
15199 tree default_argument
;
15201 /* If the next token is an ellipsis, we have a template
15203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15205 /* Consume the `...' token. */
15206 cp_lexer_consume_token (parser
->lexer
);
15207 maybe_warn_variadic_templates ();
15209 *is_parameter_pack
= true;
15212 /* If the next token is an identifier, then it names the
15214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15215 identifier
= cp_parser_identifier (parser
);
15217 identifier
= NULL_TREE
;
15219 /* Create the parameter. */
15220 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15222 /* If the next token is an `=', we have a default argument. */
15223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15226 = cp_parser_default_type_template_argument (parser
);
15228 /* Template parameter packs cannot have default
15230 if (*is_parameter_pack
)
15233 error_at (token
->location
,
15234 "template parameter pack %qD cannot have a "
15235 "default argument", identifier
);
15237 error_at (token
->location
,
15238 "template parameter packs cannot have "
15239 "default arguments");
15240 default_argument
= NULL_TREE
;
15242 else if (check_for_bare_parameter_packs (default_argument
))
15243 default_argument
= error_mark_node
;
15246 default_argument
= NULL_TREE
;
15248 /* Create the combined representation of the parameter and the
15249 default argument. */
15250 parameter
= build_tree_list (default_argument
, parameter
);
15257 tree default_argument
;
15259 /* Look for the `<'. */
15260 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15261 /* Parse the template-parameter-list. */
15262 cp_parser_template_parameter_list (parser
);
15263 /* Look for the `>'. */
15264 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15266 // If template requirements are present, parse them.
15269 tree reqs
= get_shorthand_constraints (current_template_parms
);
15270 if (tree r
= cp_parser_requires_clause_opt (parser
))
15271 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15272 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15275 /* Look for the `class' or 'typename' keywords. */
15276 cp_parser_type_parameter_key (parser
);
15277 /* If the next token is an ellipsis, we have a template
15279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15281 /* Consume the `...' token. */
15282 cp_lexer_consume_token (parser
->lexer
);
15283 maybe_warn_variadic_templates ();
15285 *is_parameter_pack
= true;
15287 /* If the next token is an `=', then there is a
15288 default-argument. If the next token is a `>', we are at
15289 the end of the parameter-list. If the next token is a `,',
15290 then we are at the end of this parameter. */
15291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15293 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15295 identifier
= cp_parser_identifier (parser
);
15296 /* Treat invalid names as if the parameter were nameless. */
15297 if (identifier
== error_mark_node
)
15298 identifier
= NULL_TREE
;
15301 identifier
= NULL_TREE
;
15303 /* Create the template parameter. */
15304 parameter
= finish_template_template_parm (class_type_node
,
15307 /* If the next token is an `=', then there is a
15308 default-argument. */
15309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15312 = cp_parser_default_template_template_argument (parser
);
15314 /* Template parameter packs cannot have default
15316 if (*is_parameter_pack
)
15319 error_at (token
->location
,
15320 "template parameter pack %qD cannot "
15321 "have a default argument",
15324 error_at (token
->location
, "template parameter packs cannot "
15325 "have default arguments");
15326 default_argument
= NULL_TREE
;
15330 default_argument
= NULL_TREE
;
15332 /* Create the combined representation of the parameter and the
15333 default argument. */
15334 parameter
= build_tree_list (default_argument
, parameter
);
15339 gcc_unreachable ();
15346 /* Parse a template-id.
15349 template-name < template-argument-list [opt] >
15351 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15352 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15353 returned. Otherwise, if the template-name names a function, or set
15354 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15355 names a class, returns a TYPE_DECL for the specialization.
15357 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15358 uninstantiated templates. */
15361 cp_parser_template_id (cp_parser
*parser
,
15362 bool template_keyword_p
,
15363 bool check_dependency_p
,
15364 enum tag_types tag_type
,
15365 bool is_declaration
)
15370 cp_token_position start_of_id
= 0;
15371 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15372 bool is_identifier
;
15374 /* If the next token corresponds to a template-id, there is no need
15376 next_token
= cp_lexer_peek_token (parser
->lexer
);
15377 if (next_token
->type
== CPP_TEMPLATE_ID
)
15379 cp_lexer_consume_token (parser
->lexer
);
15380 return saved_checks_value (next_token
->u
.tree_check_value
);
15383 /* Avoid performing name lookup if there is no possibility of
15384 finding a template-id. */
15385 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
15386 || (next_token
->type
== CPP_NAME
15387 && !cp_parser_nth_token_starts_template_argument_list_p
15390 cp_parser_error (parser
, "expected template-id");
15391 return error_mark_node
;
15394 /* Remember where the template-id starts. */
15395 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15396 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15398 push_deferring_access_checks (dk_deferred
);
15400 /* Parse the template-name. */
15401 is_identifier
= false;
15402 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15403 check_dependency_p
,
15407 if (templ
== error_mark_node
|| is_identifier
)
15409 pop_deferring_access_checks ();
15413 /* Since we're going to preserve any side-effects from this parse, set up a
15414 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15415 in the template arguments. */
15416 tentative_firewall
firewall (parser
);
15418 /* If we find the sequence `[:' after a template-name, it's probably
15419 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15420 parse correctly the argument list. */
15421 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15422 == CPP_OPEN_SQUARE
)
15423 && next_token
->flags
& DIGRAPH
15424 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15426 && !(next_token_2
->flags
& PREV_WHITE
))
15428 cp_parser_parse_tentatively (parser
);
15429 /* Change `:' into `::'. */
15430 next_token_2
->type
= CPP_SCOPE
;
15431 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15433 cp_lexer_consume_token (parser
->lexer
);
15435 /* Parse the arguments. */
15436 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15437 if (!cp_parser_parse_definitely (parser
))
15439 /* If we couldn't parse an argument list, then we revert our changes
15440 and return simply an error. Maybe this is not a template-id
15442 next_token_2
->type
= CPP_COLON
;
15443 cp_parser_error (parser
, "expected %<<%>");
15444 pop_deferring_access_checks ();
15445 return error_mark_node
;
15447 /* Otherwise, emit an error about the invalid digraph, but continue
15448 parsing because we got our argument list. */
15449 if (permerror (next_token
->location
,
15450 "%<<::%> cannot begin a template-argument list"))
15452 static bool hint
= false;
15453 inform (next_token
->location
,
15454 "%<<:%> is an alternate spelling for %<[%>."
15455 " Insert whitespace between %<<%> and %<::%>");
15456 if (!hint
&& !flag_permissive
)
15458 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15459 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15460 "accept your code)");
15467 /* Look for the `<' that starts the template-argument-list. */
15468 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15470 pop_deferring_access_checks ();
15471 return error_mark_node
;
15473 /* Parse the arguments. */
15474 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15477 /* Build a representation of the specialization. */
15478 if (identifier_p (templ
))
15479 template_id
= build_min_nt_loc (next_token
->location
,
15482 else if (DECL_TYPE_TEMPLATE_P (templ
)
15483 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15485 bool entering_scope
;
15486 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15487 template (rather than some instantiation thereof) only if
15488 is not nested within some other construct. For example, in
15489 "template <typename T> void f(T) { A<T>::", A<T> is just an
15490 instantiation of A. */
15491 entering_scope
= (template_parm_scope_p ()
15492 && cp_lexer_next_token_is (parser
->lexer
,
15495 = finish_template_type (templ
, arguments
, entering_scope
);
15497 /* A template-like identifier may be a partial concept id. */
15498 else if (flag_concepts
15499 && (template_id
= (cp_parser_maybe_partial_concept_id
15500 (parser
, templ
, arguments
))))
15501 return template_id
;
15502 else if (variable_template_p (templ
))
15504 template_id
= lookup_template_variable (templ
, arguments
);
15505 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15506 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15510 /* If it's not a class-template or a template-template, it should be
15511 a function-template. */
15512 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15513 || TREE_CODE (templ
) == OVERLOAD
15514 || BASELINK_P (templ
)));
15516 template_id
= lookup_template_function (templ
, arguments
);
15517 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15518 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15521 /* If parsing tentatively, replace the sequence of tokens that makes
15522 up the template-id with a CPP_TEMPLATE_ID token. That way,
15523 should we re-parse the token stream, we will not have to repeat
15524 the effort required to do the parse, nor will we issue duplicate
15525 error messages about problems during instantiation of the
15528 /* Don't do this if we had a parse error in a declarator; re-parsing
15529 might succeed if a name changes meaning (60361). */
15530 && !(cp_parser_error_occurred (parser
)
15531 && cp_parser_parsing_tentatively (parser
)
15532 && parser
->in_declarator_p
))
15534 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
15536 /* Reset the contents of the START_OF_ID token. */
15537 token
->type
= CPP_TEMPLATE_ID
;
15539 /* Update the location to be of the form:
15540 template-name < template-argument-list [opt] >
15541 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15542 with caret == start at the start of the template-name,
15543 ranging until the closing '>'. */
15544 location_t finish_loc
15545 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15546 location_t combined_loc
15547 = make_location (token
->location
, token
->location
, finish_loc
);
15548 token
->location
= combined_loc
;
15550 /* Retrieve any deferred checks. Do not pop this access checks yet
15551 so the memory will not be reclaimed during token replacing below. */
15552 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15553 token
->u
.tree_check_value
->value
= template_id
;
15554 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15555 token
->keyword
= RID_MAX
;
15557 /* Purge all subsequent tokens. */
15558 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15560 /* ??? Can we actually assume that, if template_id ==
15561 error_mark_node, we will have issued a diagnostic to the
15562 user, as opposed to simply marking the tentative parse as
15564 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15565 error_at (token
->location
, "parse error in template argument list");
15568 pop_to_parent_deferring_access_checks ();
15569 return template_id
;
15572 /* Parse a template-name.
15577 The standard should actually say:
15581 operator-function-id
15583 A defect report has been filed about this issue.
15585 A conversion-function-id cannot be a template name because they cannot
15586 be part of a template-id. In fact, looking at this code:
15588 a.operator K<int>()
15590 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15591 It is impossible to call a templated conversion-function-id with an
15592 explicit argument list, since the only allowed template parameter is
15593 the type to which it is converting.
15595 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15596 `template' keyword, in a construction like:
15600 In that case `f' is taken to be a template-name, even though there
15601 is no way of knowing for sure.
15603 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15604 name refers to a set of overloaded functions, at least one of which
15605 is a template, or an IDENTIFIER_NODE with the name of the template,
15606 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15607 names are looked up inside uninstantiated templates. */
15610 cp_parser_template_name (cp_parser
* parser
,
15611 bool template_keyword_p
,
15612 bool check_dependency_p
,
15613 bool is_declaration
,
15614 enum tag_types tag_type
,
15615 bool *is_identifier
)
15620 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15622 /* If the next token is `operator', then we have either an
15623 operator-function-id or a conversion-function-id. */
15624 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15626 /* We don't know whether we're looking at an
15627 operator-function-id or a conversion-function-id. */
15628 cp_parser_parse_tentatively (parser
);
15629 /* Try an operator-function-id. */
15630 identifier
= cp_parser_operator_function_id (parser
);
15631 /* If that didn't work, try a conversion-function-id. */
15632 if (!cp_parser_parse_definitely (parser
))
15634 cp_parser_error (parser
, "expected template-name");
15635 return error_mark_node
;
15638 /* Look for the identifier. */
15640 identifier
= cp_parser_identifier (parser
);
15642 /* If we didn't find an identifier, we don't have a template-id. */
15643 if (identifier
== error_mark_node
)
15644 return error_mark_node
;
15646 /* If the name immediately followed the `template' keyword, then it
15647 is a template-name. However, if the next token is not `<', then
15648 we do not treat it as a template-name, since it is not being used
15649 as part of a template-id. This enables us to handle constructs
15652 template <typename T> struct S { S(); };
15653 template <typename T> S<T>::S();
15655 correctly. We would treat `S' as a template -- if it were `S<T>'
15656 -- but we do not if there is no `<'. */
15658 if (processing_template_decl
15659 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15661 /* In a declaration, in a dependent context, we pretend that the
15662 "template" keyword was present in order to improve error
15663 recovery. For example, given:
15665 template <typename T> void f(T::X<int>);
15667 we want to treat "X<int>" as a template-id. */
15669 && !template_keyword_p
15670 && parser
->scope
&& TYPE_P (parser
->scope
)
15671 && check_dependency_p
15672 && dependent_scope_p (parser
->scope
)
15673 /* Do not do this for dtors (or ctors), since they never
15674 need the template keyword before their name. */
15675 && !constructor_name_p (identifier
, parser
->scope
))
15677 cp_token_position start
= 0;
15679 /* Explain what went wrong. */
15680 error_at (token
->location
, "non-template %qD used as template",
15682 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15683 parser
->scope
, identifier
);
15684 /* If parsing tentatively, find the location of the "<" token. */
15685 if (cp_parser_simulate_error (parser
))
15686 start
= cp_lexer_token_position (parser
->lexer
, true);
15687 /* Parse the template arguments so that we can issue error
15688 messages about them. */
15689 cp_lexer_consume_token (parser
->lexer
);
15690 cp_parser_enclosed_template_argument_list (parser
);
15691 /* Skip tokens until we find a good place from which to
15692 continue parsing. */
15693 cp_parser_skip_to_closing_parenthesis (parser
,
15694 /*recovering=*/true,
15696 /*consume_paren=*/false);
15697 /* If parsing tentatively, permanently remove the
15698 template argument list. That will prevent duplicate
15699 error messages from being issued about the missing
15700 "template" keyword. */
15702 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15704 *is_identifier
= true;
15705 parser
->context
->object_type
= NULL_TREE
;
15709 /* If the "template" keyword is present, then there is generally
15710 no point in doing name-lookup, so we just return IDENTIFIER.
15711 But, if the qualifying scope is non-dependent then we can
15712 (and must) do name-lookup normally. */
15713 if (template_keyword_p
15715 || (TYPE_P (parser
->scope
)
15716 && dependent_type_p (parser
->scope
))))
15718 /* We're optimizing away the call to cp_parser_lookup_name, but we
15719 still need to do this. */
15720 parser
->context
->object_type
= NULL_TREE
;
15725 /* Look up the name. */
15726 decl
= cp_parser_lookup_name (parser
, identifier
,
15728 /*is_template=*/true,
15729 /*is_namespace=*/false,
15730 check_dependency_p
,
15731 /*ambiguous_decls=*/NULL
,
15734 decl
= strip_using_decl (decl
);
15736 /* If DECL is a template, then the name was a template-name. */
15737 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
15739 if (TREE_DEPRECATED (decl
)
15740 && deprecated_state
!= DEPRECATED_SUPPRESS
)
15741 warn_deprecated_use (decl
, NULL_TREE
);
15745 tree fn
= NULL_TREE
;
15747 /* The standard does not explicitly indicate whether a name that
15748 names a set of overloaded declarations, some of which are
15749 templates, is a template-name. However, such a name should
15750 be a template-name; otherwise, there is no way to form a
15751 template-id for the overloaded templates. */
15752 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
15753 if (TREE_CODE (fns
) == OVERLOAD
)
15754 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
15755 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
15760 /* The name does not name a template. */
15761 cp_parser_error (parser
, "expected template-name");
15762 return error_mark_node
;
15766 /* If DECL is dependent, and refers to a function, then just return
15767 its name; we will look it up again during template instantiation. */
15768 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
15770 tree scope
= ovl_scope (decl
);
15771 if (TYPE_P (scope
) && dependent_type_p (scope
))
15778 /* Parse a template-argument-list.
15780 template-argument-list:
15781 template-argument ... [opt]
15782 template-argument-list , template-argument ... [opt]
15784 Returns a TREE_VEC containing the arguments. */
15787 cp_parser_template_argument_list (cp_parser
* parser
)
15789 tree fixed_args
[10];
15790 unsigned n_args
= 0;
15791 unsigned alloced
= 10;
15792 tree
*arg_ary
= fixed_args
;
15794 bool saved_in_template_argument_list_p
;
15796 bool saved_non_ice_p
;
15798 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
15799 parser
->in_template_argument_list_p
= true;
15800 /* Even if the template-id appears in an integral
15801 constant-expression, the contents of the argument list do
15803 saved_ice_p
= parser
->integral_constant_expression_p
;
15804 parser
->integral_constant_expression_p
= false;
15805 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
15806 parser
->non_integral_constant_expression_p
= false;
15808 /* Parse the arguments. */
15814 /* Consume the comma. */
15815 cp_lexer_consume_token (parser
->lexer
);
15817 /* Parse the template-argument. */
15818 argument
= cp_parser_template_argument (parser
);
15820 /* If the next token is an ellipsis, we're expanding a template
15822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15824 if (argument
== error_mark_node
)
15826 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15827 error_at (token
->location
,
15828 "expected parameter pack before %<...%>");
15830 /* Consume the `...' token. */
15831 cp_lexer_consume_token (parser
->lexer
);
15833 /* Make the argument into a TYPE_PACK_EXPANSION or
15834 EXPR_PACK_EXPANSION. */
15835 argument
= make_pack_expansion (argument
);
15838 if (n_args
== alloced
)
15842 if (arg_ary
== fixed_args
)
15844 arg_ary
= XNEWVEC (tree
, alloced
);
15845 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
15848 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
15850 arg_ary
[n_args
++] = argument
;
15852 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15854 vec
= make_tree_vec (n_args
);
15857 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
15859 if (arg_ary
!= fixed_args
)
15861 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
15862 parser
->integral_constant_expression_p
= saved_ice_p
;
15863 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
15865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
15869 /* Parse a template-argument.
15872 assignment-expression
15876 The representation is that of an assignment-expression, type-id, or
15877 id-expression -- except that the qualified id-expression is
15878 evaluated, so that the value returned is either a DECL or an
15881 Although the standard says "assignment-expression", it forbids
15882 throw-expressions or assignments in the template argument.
15883 Therefore, we use "conditional-expression" instead. */
15886 cp_parser_template_argument (cp_parser
* parser
)
15891 bool maybe_type_id
= false;
15892 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
15893 location_t loc
= 0;
15896 /* There's really no way to know what we're looking at, so we just
15897 try each alternative in order.
15901 In a template-argument, an ambiguity between a type-id and an
15902 expression is resolved to a type-id, regardless of the form of
15903 the corresponding template-parameter.
15905 Therefore, we try a type-id first. */
15906 cp_parser_parse_tentatively (parser
);
15907 argument
= cp_parser_template_type_arg (parser
);
15908 /* If there was no error parsing the type-id but the next token is a
15909 '>>', our behavior depends on which dialect of C++ we're
15910 parsing. In C++98, we probably found a typo for '> >'. But there
15911 are type-id which are also valid expressions. For instance:
15913 struct X { int operator >> (int); };
15914 template <int V> struct Foo {};
15917 Here 'X()' is a valid type-id of a function type, but the user just
15918 wanted to write the expression "X() >> 5". Thus, we remember that we
15919 found a valid type-id, but we still try to parse the argument as an
15920 expression to see what happens.
15922 In C++0x, the '>>' will be considered two separate '>'
15924 if (!cp_parser_error_occurred (parser
)
15925 && cxx_dialect
== cxx98
15926 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15928 maybe_type_id
= true;
15929 cp_parser_abort_tentative_parse (parser
);
15933 /* If the next token isn't a `,' or a `>', then this argument wasn't
15934 really finished. This means that the argument is not a valid
15936 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15937 cp_parser_error (parser
, "expected template-argument");
15938 /* If that worked, we're done. */
15939 if (cp_parser_parse_definitely (parser
))
15942 /* We're still not sure what the argument will be. */
15943 cp_parser_parse_tentatively (parser
);
15944 /* Try a template. */
15945 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
15946 argument
= cp_parser_id_expression (parser
,
15947 /*template_keyword_p=*/false,
15948 /*check_dependency_p=*/true,
15950 /*declarator_p=*/false,
15951 /*optional_p=*/false);
15952 /* If the next token isn't a `,' or a `>', then this argument wasn't
15953 really finished. */
15954 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15955 cp_parser_error (parser
, "expected template-argument");
15956 if (!cp_parser_error_occurred (parser
))
15958 /* Figure out what is being referred to. If the id-expression
15959 was for a class template specialization, then we will have a
15960 TYPE_DECL at this point. There is no need to do name lookup
15961 at this point in that case. */
15962 if (TREE_CODE (argument
) != TYPE_DECL
)
15963 argument
= cp_parser_lookup_name (parser
, argument
,
15965 /*is_template=*/template_p
,
15966 /*is_namespace=*/false,
15967 /*check_dependency=*/true,
15968 /*ambiguous_decls=*/NULL
,
15969 argument_start_token
->location
);
15970 /* Handle a constrained-type-specifier for a non-type template
15972 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
15974 else if (TREE_CODE (argument
) != TEMPLATE_DECL
15975 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
15976 cp_parser_error (parser
, "expected template-name");
15978 if (cp_parser_parse_definitely (parser
))
15980 if (TREE_DEPRECATED (argument
))
15981 warn_deprecated_use (argument
, NULL_TREE
);
15984 /* It must be a non-type argument. In C++17 any constant-expression is
15986 if (cxx_dialect
> cxx14
)
15989 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15991 -- an integral constant-expression of integral or enumeration
15994 -- the name of a non-type template-parameter; or
15996 -- the name of an object or function with external linkage...
15998 -- the address of an object or function with external linkage...
16000 -- a pointer to member... */
16001 /* Look for a non-type template parameter. */
16002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16004 cp_parser_parse_tentatively (parser
);
16005 argument
= cp_parser_primary_expression (parser
,
16006 /*address_p=*/false,
16008 /*template_arg_p=*/true,
16010 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16011 || !cp_parser_next_token_ends_template_argument_p (parser
))
16012 cp_parser_simulate_error (parser
);
16013 if (cp_parser_parse_definitely (parser
))
16017 /* If the next token is "&", the argument must be the address of an
16018 object or function with external linkage. */
16019 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16022 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16023 cp_lexer_consume_token (parser
->lexer
);
16025 /* See if we might have an id-expression. */
16026 token
= cp_lexer_peek_token (parser
->lexer
);
16027 if (token
->type
== CPP_NAME
16028 || token
->keyword
== RID_OPERATOR
16029 || token
->type
== CPP_SCOPE
16030 || token
->type
== CPP_TEMPLATE_ID
16031 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16033 cp_parser_parse_tentatively (parser
);
16034 argument
= cp_parser_primary_expression (parser
,
16037 /*template_arg_p=*/true,
16039 if (cp_parser_error_occurred (parser
)
16040 || !cp_parser_next_token_ends_template_argument_p (parser
))
16041 cp_parser_abort_tentative_parse (parser
);
16046 if (INDIRECT_REF_P (argument
))
16048 /* Strip the dereference temporarily. */
16049 gcc_assert (REFERENCE_REF_P (argument
));
16050 argument
= TREE_OPERAND (argument
, 0);
16053 /* If we're in a template, we represent a qualified-id referring
16054 to a static data member as a SCOPE_REF even if the scope isn't
16055 dependent so that we can check access control later. */
16057 if (TREE_CODE (probe
) == SCOPE_REF
)
16058 probe
= TREE_OPERAND (probe
, 1);
16061 /* A variable without external linkage might still be a
16062 valid constant-expression, so no error is issued here
16063 if the external-linkage check fails. */
16064 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16065 cp_parser_simulate_error (parser
);
16067 else if (is_overloaded_fn (argument
))
16068 /* All overloaded functions are allowed; if the external
16069 linkage test does not pass, an error will be issued
16073 && (TREE_CODE (argument
) == OFFSET_REF
16074 || TREE_CODE (argument
) == SCOPE_REF
))
16075 /* A pointer-to-member. */
16077 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16080 cp_parser_simulate_error (parser
);
16082 if (cp_parser_parse_definitely (parser
))
16085 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16086 tf_warning_or_error
);
16088 argument
= convert_from_reference (argument
);
16093 /* If the argument started with "&", there are no other valid
16094 alternatives at this point. */
16097 cp_parser_error (parser
, "invalid non-type template argument");
16098 return error_mark_node
;
16102 /* If the argument wasn't successfully parsed as a type-id followed
16103 by '>>', the argument can only be a constant expression now.
16104 Otherwise, we try parsing the constant-expression tentatively,
16105 because the argument could really be a type-id. */
16107 cp_parser_parse_tentatively (parser
);
16109 if (cxx_dialect
<= cxx14
)
16110 argument
= cp_parser_constant_expression (parser
);
16113 /* With C++17 generalized non-type template arguments we need to handle
16114 lvalue constant expressions, too. */
16115 argument
= cp_parser_assignment_expression (parser
);
16116 require_potential_constant_expression (argument
);
16119 if (!maybe_type_id
)
16121 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16122 cp_parser_error (parser
, "expected template-argument");
16123 if (cp_parser_parse_definitely (parser
))
16125 /* We did our best to parse the argument as a non type-id, but that
16126 was the only alternative that matched (albeit with a '>' after
16127 it). We can assume it's just a typo from the user, and a
16128 diagnostic will then be issued. */
16129 return cp_parser_template_type_arg (parser
);
16132 /* Parse an explicit-instantiation.
16134 explicit-instantiation:
16135 template declaration
16137 Although the standard says `declaration', what it really means is:
16139 explicit-instantiation:
16140 template decl-specifier-seq [opt] declarator [opt] ;
16142 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16143 supposed to be allowed. A defect report has been filed about this
16148 explicit-instantiation:
16149 storage-class-specifier template
16150 decl-specifier-seq [opt] declarator [opt] ;
16151 function-specifier template
16152 decl-specifier-seq [opt] declarator [opt] ; */
16155 cp_parser_explicit_instantiation (cp_parser
* parser
)
16157 int declares_class_or_enum
;
16158 cp_decl_specifier_seq decl_specifiers
;
16159 tree extension_specifier
= NULL_TREE
;
16161 timevar_push (TV_TEMPLATE_INST
);
16163 /* Look for an (optional) storage-class-specifier or
16164 function-specifier. */
16165 if (cp_parser_allow_gnu_extensions_p (parser
))
16167 extension_specifier
16168 = cp_parser_storage_class_specifier_opt (parser
);
16169 if (!extension_specifier
)
16170 extension_specifier
16171 = cp_parser_function_specifier_opt (parser
,
16172 /*decl_specs=*/NULL
);
16175 /* Look for the `template' keyword. */
16176 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16177 /* Let the front end know that we are processing an explicit
16179 begin_explicit_instantiation ();
16180 /* [temp.explicit] says that we are supposed to ignore access
16181 control while processing explicit instantiation directives. */
16182 push_deferring_access_checks (dk_no_check
);
16183 /* Parse a decl-specifier-seq. */
16184 cp_parser_decl_specifier_seq (parser
,
16185 CP_PARSER_FLAGS_OPTIONAL
,
16187 &declares_class_or_enum
);
16188 /* If there was exactly one decl-specifier, and it declared a class,
16189 and there's no declarator, then we have an explicit type
16191 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16195 type
= check_tag_decl (&decl_specifiers
,
16196 /*explicit_type_instantiation_p=*/true);
16197 /* Turn access control back on for names used during
16198 template instantiation. */
16199 pop_deferring_access_checks ();
16201 do_type_instantiation (type
, extension_specifier
,
16202 /*complain=*/tf_error
);
16206 cp_declarator
*declarator
;
16209 /* Parse the declarator. */
16211 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16212 /*ctor_dtor_or_conv_p=*/NULL
,
16213 /*parenthesized_p=*/NULL
,
16214 /*member_p=*/false,
16215 /*friend_p=*/false);
16216 if (declares_class_or_enum
& 2)
16217 cp_parser_check_for_definition_in_return_type (declarator
,
16218 decl_specifiers
.type
,
16219 decl_specifiers
.locations
[ds_type_spec
]);
16220 if (declarator
!= cp_error_declarator
)
16222 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16223 permerror (decl_specifiers
.locations
[ds_inline
],
16224 "explicit instantiation shall not use"
16225 " %<inline%> specifier");
16226 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16227 permerror (decl_specifiers
.locations
[ds_constexpr
],
16228 "explicit instantiation shall not use"
16229 " %<constexpr%> specifier");
16231 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16232 NORMAL
, 0, &decl_specifiers
.attributes
);
16233 /* Turn access control back on for names used during
16234 template instantiation. */
16235 pop_deferring_access_checks ();
16236 /* Do the explicit instantiation. */
16237 do_decl_instantiation (decl
, extension_specifier
);
16241 pop_deferring_access_checks ();
16242 /* Skip the body of the explicit instantiation. */
16243 cp_parser_skip_to_end_of_statement (parser
);
16246 /* We're done with the instantiation. */
16247 end_explicit_instantiation ();
16249 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16251 timevar_pop (TV_TEMPLATE_INST
);
16254 /* Parse an explicit-specialization.
16256 explicit-specialization:
16257 template < > declaration
16259 Although the standard says `declaration', what it really means is:
16261 explicit-specialization:
16262 template <> decl-specifier [opt] init-declarator [opt] ;
16263 template <> function-definition
16264 template <> explicit-specialization
16265 template <> template-declaration */
16268 cp_parser_explicit_specialization (cp_parser
* parser
)
16270 bool need_lang_pop
;
16271 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16273 /* Look for the `template' keyword. */
16274 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16275 /* Look for the `<'. */
16276 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16277 /* Look for the `>'. */
16278 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16279 /* We have processed another parameter list. */
16280 ++parser
->num_template_parameter_lists
;
16283 A template ... explicit specialization ... shall not have C
16285 if (current_lang_name
== lang_name_c
)
16287 error_at (token
->location
, "template specialization with C linkage");
16288 /* Give it C++ linkage to avoid confusing other parts of the
16290 push_lang_context (lang_name_cplusplus
);
16291 need_lang_pop
= true;
16294 need_lang_pop
= false;
16295 /* Let the front end know that we are beginning a specialization. */
16296 if (!begin_specialization ())
16298 end_specialization ();
16302 /* If the next keyword is `template', we need to figure out whether
16303 or not we're looking a template-declaration. */
16304 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16306 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16307 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16308 cp_parser_template_declaration_after_export (parser
,
16309 /*member_p=*/false);
16311 cp_parser_explicit_specialization (parser
);
16314 /* Parse the dependent declaration. */
16315 cp_parser_single_declaration (parser
,
16317 /*member_p=*/false,
16318 /*explicit_specialization_p=*/true,
16319 /*friend_p=*/NULL
);
16320 /* We're done with the specialization. */
16321 end_specialization ();
16322 /* For the erroneous case of a template with C linkage, we pushed an
16323 implicit C++ linkage scope; exit that scope now. */
16325 pop_lang_context ();
16326 /* We're done with this parameter list. */
16327 --parser
->num_template_parameter_lists
;
16330 /* Parse a type-specifier.
16333 simple-type-specifier
16336 elaborated-type-specifier
16344 Returns a representation of the type-specifier. For a
16345 class-specifier, enum-specifier, or elaborated-type-specifier, a
16346 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16348 The parser flags FLAGS is used to control type-specifier parsing.
16350 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16351 in a decl-specifier-seq.
16353 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16354 class-specifier, enum-specifier, or elaborated-type-specifier, then
16355 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16356 if a type is declared; 2 if it is defined. Otherwise, it is set to
16359 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16360 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16361 is set to FALSE. */
16364 cp_parser_type_specifier (cp_parser
* parser
,
16365 cp_parser_flags flags
,
16366 cp_decl_specifier_seq
*decl_specs
,
16367 bool is_declaration
,
16368 int* declares_class_or_enum
,
16369 bool* is_cv_qualifier
)
16371 tree type_spec
= NULL_TREE
;
16374 cp_decl_spec ds
= ds_last
;
16376 /* Assume this type-specifier does not declare a new type. */
16377 if (declares_class_or_enum
)
16378 *declares_class_or_enum
= 0;
16379 /* And that it does not specify a cv-qualifier. */
16380 if (is_cv_qualifier
)
16381 *is_cv_qualifier
= false;
16382 /* Peek at the next token. */
16383 token
= cp_lexer_peek_token (parser
->lexer
);
16385 /* If we're looking at a keyword, we can use that to guide the
16386 production we choose. */
16387 keyword
= token
->keyword
;
16391 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16392 goto elaborated_type_specifier
;
16394 /* Look for the enum-specifier. */
16395 type_spec
= cp_parser_enum_specifier (parser
);
16396 /* If that worked, we're done. */
16399 if (declares_class_or_enum
)
16400 *declares_class_or_enum
= 2;
16402 cp_parser_set_decl_spec_type (decl_specs
,
16405 /*type_definition_p=*/true);
16409 goto elaborated_type_specifier
;
16411 /* Any of these indicate either a class-specifier, or an
16412 elaborated-type-specifier. */
16416 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16417 goto elaborated_type_specifier
;
16419 /* Parse tentatively so that we can back up if we don't find a
16420 class-specifier. */
16421 cp_parser_parse_tentatively (parser
);
16422 /* Look for the class-specifier. */
16423 type_spec
= cp_parser_class_specifier (parser
);
16424 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16425 /* If that worked, we're done. */
16426 if (cp_parser_parse_definitely (parser
))
16428 if (declares_class_or_enum
)
16429 *declares_class_or_enum
= 2;
16431 cp_parser_set_decl_spec_type (decl_specs
,
16434 /*type_definition_p=*/true);
16438 /* Fall through. */
16439 elaborated_type_specifier
:
16440 /* We're declaring (not defining) a class or enum. */
16441 if (declares_class_or_enum
)
16442 *declares_class_or_enum
= 1;
16444 /* Fall through. */
16446 /* Look for an elaborated-type-specifier. */
16448 = (cp_parser_elaborated_type_specifier
16450 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16453 cp_parser_set_decl_spec_type (decl_specs
,
16456 /*type_definition_p=*/false);
16461 if (is_cv_qualifier
)
16462 *is_cv_qualifier
= true;
16467 if (is_cv_qualifier
)
16468 *is_cv_qualifier
= true;
16473 if (is_cv_qualifier
)
16474 *is_cv_qualifier
= true;
16478 /* The `__complex__' keyword is a GNU extension. */
16486 /* Handle simple keywords. */
16491 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16492 decl_specs
->any_specifiers_p
= true;
16494 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16497 /* If we do not already have a type-specifier, assume we are looking
16498 at a simple-type-specifier. */
16499 type_spec
= cp_parser_simple_type_specifier (parser
,
16503 /* If we didn't find a type-specifier, and a type-specifier was not
16504 optional in this context, issue an error message. */
16505 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16507 cp_parser_error (parser
, "expected type specifier");
16508 return error_mark_node
;
16514 /* Parse a simple-type-specifier.
16516 simple-type-specifier:
16517 :: [opt] nested-name-specifier [opt] type-name
16518 :: [opt] nested-name-specifier template template-id
16533 simple-type-specifier:
16535 decltype ( expression )
16538 __underlying_type ( type-id )
16542 nested-name-specifier(opt) template-name
16546 simple-type-specifier:
16548 __typeof__ unary-expression
16549 __typeof__ ( type-id )
16550 __typeof__ ( type-id ) { initializer-list , [opt] }
16552 Concepts Extension:
16554 simple-type-specifier:
16555 constrained-type-specifier
16557 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16558 appropriately updated. */
16561 cp_parser_simple_type_specifier (cp_parser
* parser
,
16562 cp_decl_specifier_seq
*decl_specs
,
16563 cp_parser_flags flags
)
16565 tree type
= NULL_TREE
;
16569 /* Peek at the next token. */
16570 token
= cp_lexer_peek_token (parser
->lexer
);
16572 /* If we're looking at a keyword, things are easy. */
16573 switch (token
->keyword
)
16577 decl_specs
->explicit_char_p
= true;
16578 type
= char_type_node
;
16581 type
= char16_type_node
;
16584 type
= char32_type_node
;
16587 type
= wchar_type_node
;
16590 type
= boolean_type_node
;
16593 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16594 type
= short_integer_type_node
;
16598 decl_specs
->explicit_int_p
= true;
16599 type
= integer_type_node
;
16605 idx
= token
->keyword
- RID_INT_N_0
;
16606 if (! int_n_enabled_p
[idx
])
16610 decl_specs
->explicit_intN_p
= true;
16611 decl_specs
->int_n_idx
= idx
;
16613 type
= int_n_trees
[idx
].signed_type
;
16617 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16618 type
= long_integer_type_node
;
16621 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16622 type
= integer_type_node
;
16625 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16626 type
= unsigned_type_node
;
16629 type
= float_type_node
;
16632 type
= double_type_node
;
16635 type
= void_type_node
;
16639 maybe_warn_cpp0x (CPP0X_AUTO
);
16640 if (parser
->auto_is_implicit_function_template_parm_p
)
16642 /* The 'auto' might be the placeholder return type for a function decl
16643 with trailing return type. */
16644 bool have_trailing_return_fn_decl
= false;
16646 cp_parser_parse_tentatively (parser
);
16647 cp_lexer_consume_token (parser
->lexer
);
16648 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16649 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16650 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16651 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16655 cp_lexer_consume_token (parser
->lexer
);
16656 cp_parser_skip_to_closing_parenthesis (parser
,
16657 /*recovering*/false,
16659 /*consume_paren*/true);
16663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16665 have_trailing_return_fn_decl
= true;
16669 cp_lexer_consume_token (parser
->lexer
);
16671 cp_parser_abort_tentative_parse (parser
);
16673 if (have_trailing_return_fn_decl
)
16675 type
= make_auto ();
16679 if (cxx_dialect
>= cxx14
)
16681 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16682 type
= TREE_TYPE (type
);
16685 type
= error_mark_node
;
16687 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16689 if (cxx_dialect
< cxx14
)
16690 error_at (token
->location
,
16691 "use of %<auto%> in lambda parameter declaration "
16692 "only available with "
16693 "-std=c++14 or -std=gnu++14");
16695 else if (cxx_dialect
< cxx14
)
16696 error_at (token
->location
,
16697 "use of %<auto%> in parameter declaration "
16698 "only available with "
16699 "-std=c++14 or -std=gnu++14");
16700 else if (!flag_concepts
)
16701 pedwarn (token
->location
, OPT_Wpedantic
,
16702 "ISO C++ forbids use of %<auto%> in parameter "
16706 type
= make_auto ();
16710 /* Since DR 743, decltype can either be a simple-type-specifier by
16711 itself or begin a nested-name-specifier. Parsing it will replace
16712 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16713 handling below decide what to do. */
16714 cp_parser_decltype (parser
);
16715 cp_lexer_set_token_position (parser
->lexer
, token
);
16719 /* Consume the `typeof' token. */
16720 cp_lexer_consume_token (parser
->lexer
);
16721 /* Parse the operand to `typeof'. */
16722 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16723 /* If it is not already a TYPE, take its type. */
16724 if (!TYPE_P (type
))
16725 type
= finish_typeof (type
);
16728 cp_parser_set_decl_spec_type (decl_specs
, type
,
16730 /*type_definition_p=*/false);
16734 case RID_UNDERLYING_TYPE
:
16735 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
16737 cp_parser_set_decl_spec_type (decl_specs
, type
,
16739 /*type_definition_p=*/false);
16744 case RID_DIRECT_BASES
:
16745 type
= cp_parser_trait_expr (parser
, token
->keyword
);
16747 cp_parser_set_decl_spec_type (decl_specs
, type
,
16749 /*type_definition_p=*/false);
16755 /* If token is an already-parsed decltype not followed by ::,
16756 it's a simple-type-specifier. */
16757 if (token
->type
== CPP_DECLTYPE
16758 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
16760 type
= saved_checks_value (token
->u
.tree_check_value
);
16763 cp_parser_set_decl_spec_type (decl_specs
, type
,
16765 /*type_definition_p=*/false);
16766 /* Remember that we are handling a decltype in order to
16767 implement the resolution of DR 1510 when the argument
16768 isn't instantiation dependent. */
16769 decl_specs
->decltype_p
= true;
16771 cp_lexer_consume_token (parser
->lexer
);
16775 /* If the type-specifier was for a built-in type, we're done. */
16778 /* Record the type. */
16780 && (token
->keyword
!= RID_SIGNED
16781 && token
->keyword
!= RID_UNSIGNED
16782 && token
->keyword
!= RID_SHORT
16783 && token
->keyword
!= RID_LONG
))
16784 cp_parser_set_decl_spec_type (decl_specs
,
16787 /*type_definition_p=*/false);
16789 decl_specs
->any_specifiers_p
= true;
16791 /* Consume the token. */
16792 cp_lexer_consume_token (parser
->lexer
);
16794 if (type
== error_mark_node
)
16795 return error_mark_node
;
16797 /* There is no valid C++ program where a non-template type is
16798 followed by a "<". That usually indicates that the user thought
16799 that the type was a template. */
16800 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
16803 return TYPE_NAME (type
);
16806 /* The type-specifier must be a user-defined type. */
16807 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
16812 /* Don't gobble tokens or issue error messages if this is an
16813 optional type-specifier. */
16814 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16815 cp_parser_parse_tentatively (parser
);
16817 token
= cp_lexer_peek_token (parser
->lexer
);
16819 /* Look for the optional `::' operator. */
16821 = (cp_parser_global_scope_opt (parser
,
16822 /*current_scope_valid_p=*/false)
16824 /* Look for the nested-name specifier. */
16826 = (cp_parser_nested_name_specifier_opt (parser
,
16827 /*typename_keyword_p=*/false,
16828 /*check_dependency_p=*/true,
16830 /*is_declaration=*/false)
16832 /* If we have seen a nested-name-specifier, and the next token
16833 is `template', then we are using the template-id production. */
16835 && cp_parser_optional_template_keyword (parser
))
16837 /* Look for the template-id. */
16838 type
= cp_parser_template_id (parser
,
16839 /*template_keyword_p=*/true,
16840 /*check_dependency_p=*/true,
16842 /*is_declaration=*/false);
16843 /* If the template-id did not name a type, we are out of
16845 if (TREE_CODE (type
) != TYPE_DECL
)
16847 cp_parser_error (parser
, "expected template-id for type");
16851 /* Otherwise, look for a type-name. */
16853 type
= cp_parser_type_name (parser
);
16854 /* Keep track of all name-lookups performed in class scopes. */
16858 && TREE_CODE (type
) == TYPE_DECL
16859 && identifier_p (DECL_NAME (type
)))
16860 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
16861 /* If it didn't work out, we don't have a TYPE. */
16862 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16863 && !cp_parser_parse_definitely (parser
))
16865 if (!type
&& cxx_dialect
>= cxx1z
)
16867 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
16868 cp_parser_parse_tentatively (parser
);
16870 cp_parser_global_scope_opt (parser
,
16871 /*current_scope_valid_p=*/false);
16872 cp_parser_nested_name_specifier_opt (parser
,
16873 /*typename_keyword_p=*/false,
16874 /*check_dependency_p=*/true,
16876 /*is_declaration=*/false);
16877 tree name
= cp_parser_identifier (parser
);
16878 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
16879 && parser
->scope
!= error_mark_node
)
16881 tree tmpl
= cp_parser_lookup_name (parser
, name
,
16883 /*is_template=*/false,
16884 /*is_namespace=*/false,
16885 /*check_dependency=*/true,
16886 /*ambiguous_decls=*/NULL
,
16888 if (tmpl
&& tmpl
!= error_mark_node
16889 && (DECL_CLASS_TEMPLATE_P (tmpl
)
16890 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
16891 type
= make_template_placeholder (tmpl
);
16894 type
= error_mark_node
;
16895 if (!cp_parser_simulate_error (parser
))
16896 cp_parser_name_lookup_error (parser
, name
, tmpl
,
16897 NLE_TYPE
, token
->location
);
16901 type
= error_mark_node
;
16903 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
16904 && !cp_parser_parse_definitely (parser
))
16907 if (type
&& decl_specs
)
16908 cp_parser_set_decl_spec_type (decl_specs
, type
,
16910 /*type_definition_p=*/false);
16913 /* If we didn't get a type-name, issue an error message. */
16914 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16916 cp_parser_error (parser
, "expected type-name");
16917 return error_mark_node
;
16920 if (type
&& type
!= error_mark_node
)
16922 /* See if TYPE is an Objective-C type, and if so, parse and
16923 accept any protocol references following it. Do this before
16924 the cp_parser_check_for_invalid_template_id() call, because
16925 Objective-C types can be followed by '<...>' which would
16926 enclose protocol names rather than template arguments, and so
16927 everything is fine. */
16928 if (c_dialect_objc () && !parser
->scope
16929 && (objc_is_id (type
) || objc_is_class_name (type
)))
16931 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16932 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
16934 /* Clobber the "unqualified" type previously entered into
16935 DECL_SPECS with the new, improved protocol-qualified version. */
16937 decl_specs
->type
= qual_type
;
16942 /* There is no valid C++ program where a non-template type is
16943 followed by a "<". That usually indicates that the user
16944 thought that the type was a template. */
16945 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
16953 /* Parse a type-name.
16959 simple-template-id [in c++0x]
16976 Returns a TYPE_DECL for the type. */
16979 cp_parser_type_name (cp_parser
* parser
)
16981 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
16986 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
16990 /* We can't know yet whether it is a class-name or not. */
16991 cp_parser_parse_tentatively (parser
);
16992 /* Try a class-name. */
16993 type_decl
= cp_parser_class_name (parser
,
16994 typename_keyword_p
,
16995 /*template_keyword_p=*/false,
16997 /*check_dependency_p=*/true,
16998 /*class_head_p=*/false,
16999 /*is_declaration=*/false);
17000 /* If it's not a class-name, keep looking. */
17001 if (!cp_parser_parse_definitely (parser
))
17003 if (cxx_dialect
< cxx11
)
17004 /* It must be a typedef-name or an enum-name. */
17005 return cp_parser_nonclass_name (parser
);
17007 cp_parser_parse_tentatively (parser
);
17008 /* It is either a simple-template-id representing an
17009 instantiation of an alias template... */
17010 type_decl
= cp_parser_template_id (parser
,
17011 /*template_keyword_p=*/false,
17012 /*check_dependency_p=*/true,
17014 /*is_declaration=*/false);
17015 /* Note that this must be an instantiation of an alias template
17016 because [temp.names]/6 says:
17018 A template-id that names an alias template specialization
17021 Whereas [temp.names]/7 says:
17023 A simple-template-id that names a class template
17024 specialization is a class-name.
17026 With concepts, this could also be a partial-concept-id that
17027 declares a non-type template parameter. */
17028 if (type_decl
!= NULL_TREE
17029 && TREE_CODE (type_decl
) == TYPE_DECL
17030 && TYPE_DECL_ALIAS_P (type_decl
))
17031 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17032 else if (is_constrained_parameter (type_decl
))
17033 /* Don't do anything. */ ;
17035 cp_parser_simulate_error (parser
);
17037 if (!cp_parser_parse_definitely (parser
))
17038 /* ... Or a typedef-name or an enum-name. */
17039 return cp_parser_nonclass_name (parser
);
17045 /* Check if DECL and ARGS can form a constrained-type-specifier.
17046 If ARGS is non-null, we try to form a concept check of the
17047 form DECL<?, ARGS> where ? is a wildcard that matches any
17048 kind of template argument. If ARGS is NULL, then we try to
17049 form a concept check of the form DECL<?>. */
17052 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17053 tree decl
, tree args
)
17055 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17057 /* If we a constrained-type-specifier cannot be deduced. */
17058 if (parser
->prevent_constrained_type_specifiers
)
17061 /* A constrained type specifier can only be found in an
17062 overload set or as a reference to a template declaration.
17064 FIXME: This might be masking a bug. It's possible that
17065 that the deduction below is causing template specializations
17066 to be formed with the wildcard as an argument. */
17067 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17070 /* Try to build a call expression that evaluates the
17071 concept. This can fail if the overload set refers
17072 only to non-templates. */
17073 tree placeholder
= build_nt (WILDCARD_DECL
);
17074 tree check
= build_concept_check (decl
, placeholder
, args
);
17075 if (check
== error_mark_node
)
17078 /* Deduce the checked constraint and the prototype parameter.
17080 FIXME: In certain cases, failure to deduce should be a
17081 diagnosable error. */
17084 if (!deduce_constrained_parameter (check
, conc
, proto
))
17087 /* In template parameter scope, this results in a constrained
17088 parameter. Return a descriptor of that parm. */
17089 if (processing_template_parmlist
)
17090 return build_constrained_parameter (conc
, proto
, args
);
17092 /* In a parameter-declaration-clause, constrained-type
17093 specifiers result in invented template parameters. */
17094 if (parser
->auto_is_implicit_function_template_parm_p
)
17096 tree x
= build_constrained_parameter (conc
, proto
, args
);
17097 return synthesize_implicit_template_parm (parser
, x
);
17101 /* Otherwise, we're in a context where the constrained
17102 type name is deduced and the constraint applies
17103 after deduction. */
17104 return make_constrained_auto (conc
, args
);
17110 /* If DECL refers to a concept, return a TYPE_DECL representing
17111 the result of using the constrained type specifier in the
17112 current context. DECL refers to a concept if
17114 - it is an overload set containing a function concept taking a single
17117 - it is a variable concept taking a single type argument. */
17120 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17123 && (TREE_CODE (decl
) == OVERLOAD
17124 || BASELINK_P (decl
)
17125 || variable_concept_p (decl
)))
17126 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17131 /* Check if DECL and ARGS form a partial-concept-id. If so,
17132 assign ID to the resulting constrained placeholder.
17134 Returns true if the partial-concept-id designates a placeholder
17135 and false otherwise. Note that *id is set to NULL_TREE in
17139 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17141 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17144 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17156 Returns a TYPE_DECL for the type. */
17159 cp_parser_nonclass_name (cp_parser
* parser
)
17164 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17165 identifier
= cp_parser_identifier (parser
);
17166 if (identifier
== error_mark_node
)
17167 return error_mark_node
;
17169 /* Look up the type-name. */
17170 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17172 type_decl
= strip_using_decl (type_decl
);
17174 /* If we found an overload set, then it may refer to a concept-name. */
17175 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17178 if (TREE_CODE (type_decl
) != TYPE_DECL
17179 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17181 /* See if this is an Objective-C type. */
17182 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17183 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17185 type_decl
= TYPE_NAME (type
);
17188 /* Issue an error if we did not find a type-name. */
17189 if (TREE_CODE (type_decl
) != TYPE_DECL
17190 /* In Objective-C, we have the complication that class names are
17191 normally type names and start declarations (eg, the
17192 "NSObject" in "NSObject *object;"), but can be used in an
17193 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17194 is an expression. So, a classname followed by a dot is not a
17195 valid type-name. */
17196 || (objc_is_class_name (TREE_TYPE (type_decl
))
17197 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17199 if (!cp_parser_simulate_error (parser
))
17200 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17201 NLE_TYPE
, token
->location
);
17202 return error_mark_node
;
17204 /* Remember that the name was used in the definition of the
17205 current class so that we can check later to see if the
17206 meaning would have been different after the class was
17207 entirely defined. */
17208 else if (type_decl
!= error_mark_node
17210 maybe_note_name_used_in_class (identifier
, type_decl
);
17215 /* Parse an elaborated-type-specifier. Note that the grammar given
17216 here incorporates the resolution to DR68.
17218 elaborated-type-specifier:
17219 class-key :: [opt] nested-name-specifier [opt] identifier
17220 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17221 enum-key :: [opt] nested-name-specifier [opt] identifier
17222 typename :: [opt] nested-name-specifier identifier
17223 typename :: [opt] nested-name-specifier template [opt]
17228 elaborated-type-specifier:
17229 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17230 class-key attributes :: [opt] nested-name-specifier [opt]
17231 template [opt] template-id
17232 enum attributes :: [opt] nested-name-specifier [opt] identifier
17234 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17235 declared `friend'. If IS_DECLARATION is TRUE, then this
17236 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17237 something is being declared.
17239 Returns the TYPE specified. */
17242 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17244 bool is_declaration
)
17246 enum tag_types tag_type
;
17248 tree type
= NULL_TREE
;
17249 tree attributes
= NULL_TREE
;
17251 cp_token
*token
= NULL
;
17253 /* See if we're looking at the `enum' keyword. */
17254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17256 /* Consume the `enum' token. */
17257 cp_lexer_consume_token (parser
->lexer
);
17258 /* Remember that it's an enumeration type. */
17259 tag_type
= enum_type
;
17260 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17261 enums) is used here. */
17262 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17263 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17265 pedwarn (input_location
, 0, "elaborated-type-specifier "
17266 "for a scoped enum must not use the %<%D%> keyword",
17267 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
17268 /* Consume the `struct' or `class' and parse it anyway. */
17269 cp_lexer_consume_token (parser
->lexer
);
17271 /* Parse the attributes. */
17272 attributes
= cp_parser_attributes_opt (parser
);
17274 /* Or, it might be `typename'. */
17275 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17278 /* Consume the `typename' token. */
17279 cp_lexer_consume_token (parser
->lexer
);
17280 /* Remember that it's a `typename' type. */
17281 tag_type
= typename_type
;
17283 /* Otherwise it must be a class-key. */
17286 tag_type
= cp_parser_class_key (parser
);
17287 if (tag_type
== none_type
)
17288 return error_mark_node
;
17289 /* Parse the attributes. */
17290 attributes
= cp_parser_attributes_opt (parser
);
17293 /* Look for the `::' operator. */
17294 globalscope
= cp_parser_global_scope_opt (parser
,
17295 /*current_scope_valid_p=*/false);
17296 /* Look for the nested-name-specifier. */
17297 tree nested_name_specifier
;
17298 if (tag_type
== typename_type
&& !globalscope
)
17300 nested_name_specifier
17301 = cp_parser_nested_name_specifier (parser
,
17302 /*typename_keyword_p=*/true,
17303 /*check_dependency_p=*/true,
17306 if (!nested_name_specifier
)
17307 return error_mark_node
;
17310 /* Even though `typename' is not present, the proposed resolution
17311 to Core Issue 180 says that in `class A<T>::B', `B' should be
17312 considered a type-name, even if `A<T>' is dependent. */
17313 nested_name_specifier
17314 = cp_parser_nested_name_specifier_opt (parser
,
17315 /*typename_keyword_p=*/true,
17316 /*check_dependency_p=*/true,
17319 /* For everything but enumeration types, consider a template-id.
17320 For an enumeration type, consider only a plain identifier. */
17321 if (tag_type
!= enum_type
)
17323 bool template_p
= false;
17326 /* Allow the `template' keyword. */
17327 template_p
= cp_parser_optional_template_keyword (parser
);
17328 /* If we didn't see `template', we don't know if there's a
17329 template-id or not. */
17331 cp_parser_parse_tentatively (parser
);
17332 /* Parse the template-id. */
17333 token
= cp_lexer_peek_token (parser
->lexer
);
17334 decl
= cp_parser_template_id (parser
, template_p
,
17335 /*check_dependency_p=*/true,
17338 /* If we didn't find a template-id, look for an ordinary
17340 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17342 /* We can get here when cp_parser_template_id, called by
17343 cp_parser_class_name with tag_type == none_type, succeeds
17344 and caches a BASELINK. Then, when called again here,
17345 instead of failing and returning an error_mark_node
17346 returns it (see template/typename17.C in C++11).
17347 ??? Could we diagnose this earlier? */
17348 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17350 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17351 type
= error_mark_node
;
17353 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17354 in effect, then we must assume that, upon instantiation, the
17355 template will correspond to a class. */
17356 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17357 && tag_type
== typename_type
)
17358 type
= make_typename_type (parser
->scope
, decl
,
17360 /*complain=*/tf_error
);
17361 /* If the `typename' keyword is in effect and DECL is not a type
17362 decl, then type is non existent. */
17363 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17365 else if (TREE_CODE (decl
) == TYPE_DECL
)
17367 type
= check_elaborated_type_specifier (tag_type
, decl
,
17368 /*allow_template_p=*/true);
17370 /* If the next token is a semicolon, this must be a specialization,
17371 instantiation, or friend declaration. Check the scope while we
17372 still know whether or not we had a nested-name-specifier. */
17373 if (type
!= error_mark_node
17374 && !nested_name_specifier
&& !is_friend
17375 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17376 check_unqualified_spec_or_inst (type
, token
->location
);
17378 else if (decl
== error_mark_node
)
17379 type
= error_mark_node
;
17384 token
= cp_lexer_peek_token (parser
->lexer
);
17385 identifier
= cp_parser_identifier (parser
);
17387 if (identifier
== error_mark_node
)
17389 parser
->scope
= NULL_TREE
;
17390 return error_mark_node
;
17393 /* For a `typename', we needn't call xref_tag. */
17394 if (tag_type
== typename_type
17395 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17396 return cp_parser_make_typename_type (parser
, identifier
,
17399 /* Template parameter lists apply only if we are not within a
17400 function parameter list. */
17401 bool template_parm_lists_apply
17402 = parser
->num_template_parameter_lists
;
17403 if (template_parm_lists_apply
)
17404 for (cp_binding_level
*s
= current_binding_level
;
17405 s
&& s
->kind
!= sk_template_parms
;
17406 s
= s
->level_chain
)
17407 if (s
->kind
== sk_function_parms
)
17408 template_parm_lists_apply
= false;
17410 /* Look up a qualified name in the usual way. */
17414 tree ambiguous_decls
;
17416 decl
= cp_parser_lookup_name (parser
, identifier
,
17418 /*is_template=*/false,
17419 /*is_namespace=*/false,
17420 /*check_dependency=*/true,
17424 /* If the lookup was ambiguous, an error will already have been
17426 if (ambiguous_decls
)
17427 return error_mark_node
;
17429 /* If we are parsing friend declaration, DECL may be a
17430 TEMPLATE_DECL tree node here. However, we need to check
17431 whether this TEMPLATE_DECL results in valid code. Consider
17432 the following example:
17435 template <class T> class C {};
17438 template <class T> friend class N::C; // #1, valid code
17440 template <class T> class Y {
17441 friend class N::C; // #2, invalid code
17444 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17445 name lookup of `N::C'. We see that friend declaration must
17446 be template for the code to be valid. Note that
17447 processing_template_decl does not work here since it is
17448 always 1 for the above two cases. */
17450 decl
= (cp_parser_maybe_treat_template_as_class
17451 (decl
, /*tag_name_p=*/is_friend
17452 && template_parm_lists_apply
));
17454 if (TREE_CODE (decl
) != TYPE_DECL
)
17456 cp_parser_diagnose_invalid_type_name (parser
,
17459 return error_mark_node
;
17462 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17464 bool allow_template
= (template_parm_lists_apply
17465 || DECL_SELF_REFERENCE_P (decl
));
17466 type
= check_elaborated_type_specifier (tag_type
, decl
,
17469 if (type
== error_mark_node
)
17470 return error_mark_node
;
17473 /* Forward declarations of nested types, such as
17478 are invalid unless all components preceding the final '::'
17479 are complete. If all enclosing types are complete, these
17480 declarations become merely pointless.
17482 Invalid forward declarations of nested types are errors
17483 caught elsewhere in parsing. Those that are pointless arrive
17486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17487 && !is_friend
&& !processing_explicit_instantiation
)
17488 warning (0, "declaration %qD does not declare anything", decl
);
17490 type
= TREE_TYPE (decl
);
17494 /* An elaborated-type-specifier sometimes introduces a new type and
17495 sometimes names an existing type. Normally, the rule is that it
17496 introduces a new type only if there is not an existing type of
17497 the same name already in scope. For example, given:
17500 void f() { struct S s; }
17502 the `struct S' in the body of `f' is the same `struct S' as in
17503 the global scope; the existing definition is used. However, if
17504 there were no global declaration, this would introduce a new
17505 local class named `S'.
17507 An exception to this rule applies to the following code:
17509 namespace N { struct S; }
17511 Here, the elaborated-type-specifier names a new type
17512 unconditionally; even if there is already an `S' in the
17513 containing scope this declaration names a new type.
17514 This exception only applies if the elaborated-type-specifier
17515 forms the complete declaration:
17519 A declaration consisting solely of `class-key identifier ;' is
17520 either a redeclaration of the name in the current scope or a
17521 forward declaration of the identifier as a class name. It
17522 introduces the name into the current scope.
17524 We are in this situation precisely when the next token is a `;'.
17526 An exception to the exception is that a `friend' declaration does
17527 *not* name a new type; i.e., given:
17529 struct S { friend struct T; };
17531 `T' is not a new type in the scope of `S'.
17533 Also, `new struct S' or `sizeof (struct S)' never results in the
17534 definition of a new type; a new type can only be declared in a
17535 declaration context. */
17541 /* Friends have special name lookup rules. */
17542 ts
= ts_within_enclosing_non_class
;
17543 else if (is_declaration
17544 && cp_lexer_next_token_is (parser
->lexer
,
17546 /* This is a `class-key identifier ;' */
17552 (template_parm_lists_apply
17553 && (cp_parser_next_token_starts_class_definition_p (parser
)
17554 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17555 /* An unqualified name was used to reference this type, so
17556 there were no qualifying templates. */
17557 if (template_parm_lists_apply
17558 && !cp_parser_check_template_parameters (parser
,
17559 /*num_templates=*/0,
17561 /*declarator=*/NULL
))
17562 return error_mark_node
;
17563 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17567 if (type
== error_mark_node
)
17568 return error_mark_node
;
17570 /* Allow attributes on forward declarations of classes. */
17573 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17574 warning (OPT_Wattributes
,
17575 "attributes ignored on uninstantiated type");
17576 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17577 && ! processing_explicit_instantiation
)
17578 warning (OPT_Wattributes
,
17579 "attributes ignored on template instantiation");
17580 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17581 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17583 warning (OPT_Wattributes
,
17584 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17587 if (tag_type
!= enum_type
)
17589 /* Indicate whether this class was declared as a `class' or as a
17591 if (CLASS_TYPE_P (type
))
17592 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17593 cp_parser_check_class_key (tag_type
, type
);
17596 /* A "<" cannot follow an elaborated type specifier. If that
17597 happens, the user was probably trying to form a template-id. */
17598 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17604 /* Parse an enum-specifier.
17607 enum-head { enumerator-list [opt] }
17608 enum-head { enumerator-list , } [C++0x]
17611 enum-key identifier [opt] enum-base [opt]
17612 enum-key nested-name-specifier identifier enum-base [opt]
17617 enum struct [C++0x]
17620 : type-specifier-seq
17622 opaque-enum-specifier:
17623 enum-key identifier enum-base [opt] ;
17626 enum-key attributes[opt] identifier [opt] enum-base [opt]
17627 { enumerator-list [opt] }attributes[opt]
17628 enum-key attributes[opt] identifier [opt] enum-base [opt]
17629 { enumerator-list, }attributes[opt] [C++0x]
17631 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17632 if the token stream isn't an enum-specifier after all. */
17635 cp_parser_enum_specifier (cp_parser
* parser
)
17638 tree type
= NULL_TREE
;
17640 tree nested_name_specifier
= NULL_TREE
;
17642 bool scoped_enum_p
= false;
17643 bool has_underlying_type
= false;
17644 bool nested_being_defined
= false;
17645 bool new_value_list
= false;
17646 bool is_new_type
= false;
17647 bool is_unnamed
= false;
17648 tree underlying_type
= NULL_TREE
;
17649 cp_token
*type_start_token
= NULL
;
17650 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17652 parser
->colon_corrects_to_scope_p
= false;
17654 /* Parse tentatively so that we can back up if we don't find a
17656 cp_parser_parse_tentatively (parser
);
17658 /* Caller guarantees that the current token is 'enum', an identifier
17659 possibly follows, and the token after that is an opening brace.
17660 If we don't have an identifier, fabricate an anonymous name for
17661 the enumeration being defined. */
17662 cp_lexer_consume_token (parser
->lexer
);
17664 /* Parse the "class" or "struct", which indicates a scoped
17665 enumeration type in C++0x. */
17666 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17667 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17669 if (cxx_dialect
< cxx11
)
17670 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17672 /* Consume the `struct' or `class' token. */
17673 cp_lexer_consume_token (parser
->lexer
);
17675 scoped_enum_p
= true;
17678 attributes
= cp_parser_attributes_opt (parser
);
17680 /* Clear the qualification. */
17681 parser
->scope
= NULL_TREE
;
17682 parser
->qualifying_scope
= NULL_TREE
;
17683 parser
->object_scope
= NULL_TREE
;
17685 /* Figure out in what scope the declaration is being placed. */
17686 prev_scope
= current_scope ();
17688 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17690 push_deferring_access_checks (dk_no_check
);
17691 nested_name_specifier
17692 = cp_parser_nested_name_specifier_opt (parser
,
17693 /*typename_keyword_p=*/true,
17694 /*check_dependency_p=*/false,
17696 /*is_declaration=*/false);
17698 if (nested_name_specifier
)
17702 identifier
= cp_parser_identifier (parser
);
17703 name
= cp_parser_lookup_name (parser
, identifier
,
17705 /*is_template=*/false,
17706 /*is_namespace=*/false,
17707 /*check_dependency=*/true,
17708 /*ambiguous_decls=*/NULL
,
17710 if (name
&& name
!= error_mark_node
)
17712 type
= TREE_TYPE (name
);
17713 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17715 /* Are template enums allowed in ISO? */
17716 if (template_parm_scope_p ())
17717 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17718 "%qD is an enumeration template", name
);
17719 /* ignore a typename reference, for it will be solved by name
17724 else if (nested_name_specifier
== error_mark_node
)
17725 /* We already issued an error. */;
17728 error_at (type_start_token
->location
,
17729 "%qD does not name an enumeration in %qT",
17730 identifier
, nested_name_specifier
);
17731 nested_name_specifier
= error_mark_node
;
17736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17737 identifier
= cp_parser_identifier (parser
);
17740 identifier
= make_anon_name ();
17743 error_at (type_start_token
->location
,
17744 "unnamed scoped enum is not allowed");
17747 pop_deferring_access_checks ();
17749 /* Check for the `:' that denotes a specified underlying type in C++0x.
17750 Note that a ':' could also indicate a bitfield width, however. */
17751 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17753 cp_decl_specifier_seq type_specifiers
;
17755 /* Consume the `:'. */
17756 cp_lexer_consume_token (parser
->lexer
);
17758 /* Parse the type-specifier-seq. */
17759 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17760 /*is_trailing_return=*/false,
17763 /* At this point this is surely not elaborated type specifier. */
17764 if (!cp_parser_parse_definitely (parser
))
17767 if (cxx_dialect
< cxx11
)
17768 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17770 has_underlying_type
= true;
17772 /* If that didn't work, stop. */
17773 if (type_specifiers
.type
!= error_mark_node
)
17775 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
17776 /*initialized=*/0, NULL
);
17777 if (underlying_type
== error_mark_node
17778 || check_for_bare_parameter_packs (underlying_type
))
17779 underlying_type
= NULL_TREE
;
17783 /* Look for the `{' but don't consume it yet. */
17784 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17786 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
17788 cp_parser_error (parser
, "expected %<{%>");
17789 if (has_underlying_type
)
17795 /* An opaque-enum-specifier must have a ';' here. */
17796 if ((scoped_enum_p
|| underlying_type
)
17797 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17799 cp_parser_error (parser
, "expected %<;%> or %<{%>");
17800 if (has_underlying_type
)
17808 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
17811 if (nested_name_specifier
)
17813 if (CLASS_TYPE_P (nested_name_specifier
))
17815 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
17816 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
17817 push_scope (nested_name_specifier
);
17819 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17821 push_nested_namespace (nested_name_specifier
);
17825 /* Issue an error message if type-definitions are forbidden here. */
17826 if (!cp_parser_check_type_definition (parser
))
17827 type
= error_mark_node
;
17829 /* Create the new type. We do this before consuming the opening
17830 brace so the enum will be recorded as being on the line of its
17831 tag (or the 'enum' keyword, if there is no tag). */
17832 type
= start_enum (identifier
, type
, underlying_type
,
17833 attributes
, scoped_enum_p
, &is_new_type
);
17835 /* If the next token is not '{' it is an opaque-enum-specifier or an
17836 elaborated-type-specifier. */
17837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17839 timevar_push (TV_PARSE_ENUM
);
17840 if (nested_name_specifier
17841 && nested_name_specifier
!= error_mark_node
)
17843 /* The following catches invalid code such as:
17844 enum class S<int>::E { A, B, C }; */
17845 if (!processing_specialization
17846 && CLASS_TYPE_P (nested_name_specifier
)
17847 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
17848 error_at (type_start_token
->location
, "cannot add an enumerator "
17849 "list to a template instantiation");
17851 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
17853 error_at (type_start_token
->location
,
17854 "%<%T::%E%> has not been declared",
17855 TYPE_CONTEXT (nested_name_specifier
),
17856 nested_name_specifier
);
17857 type
= error_mark_node
;
17859 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
17860 && !CLASS_TYPE_P (nested_name_specifier
))
17862 error_at (type_start_token
->location
, "nested name specifier "
17863 "%qT for enum declaration does not name a class "
17864 "or namespace", nested_name_specifier
);
17865 type
= error_mark_node
;
17867 /* If that scope does not contain the scope in which the
17868 class was originally declared, the program is invalid. */
17869 else if (prev_scope
&& !is_ancestor (prev_scope
,
17870 nested_name_specifier
))
17872 if (at_namespace_scope_p ())
17873 error_at (type_start_token
->location
,
17874 "declaration of %qD in namespace %qD which does not "
17876 type
, prev_scope
, nested_name_specifier
);
17878 error_at (type_start_token
->location
,
17879 "declaration of %qD in %qD which does not "
17881 type
, prev_scope
, nested_name_specifier
);
17882 type
= error_mark_node
;
17884 /* If that scope is the scope where the declaration is being placed
17885 the program is invalid. */
17886 else if (CLASS_TYPE_P (nested_name_specifier
)
17887 && CLASS_TYPE_P (prev_scope
)
17888 && same_type_p (nested_name_specifier
, prev_scope
))
17890 permerror (type_start_token
->location
,
17891 "extra qualification not allowed");
17892 nested_name_specifier
= NULL_TREE
;
17897 begin_scope (sk_scoped_enum
, type
);
17899 /* Consume the opening brace. */
17900 cp_lexer_consume_token (parser
->lexer
);
17902 if (type
== error_mark_node
)
17903 ; /* Nothing to add */
17904 else if (OPAQUE_ENUM_P (type
)
17905 || (cxx_dialect
> cxx98
&& processing_specialization
))
17907 new_value_list
= true;
17908 SET_OPAQUE_ENUM_P (type
, false);
17909 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17913 error_at (type_start_token
->location
,
17914 "multiple definition of %q#T", type
);
17915 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
17916 "previous definition here");
17917 type
= error_mark_node
;
17920 if (type
== error_mark_node
)
17921 cp_parser_skip_to_end_of_block_or_statement (parser
);
17922 /* If the next token is not '}', then there are some enumerators. */
17923 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17925 if (is_unnamed
&& !scoped_enum_p
)
17926 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17927 "ISO C++ forbids empty unnamed enum");
17930 cp_parser_enumerator_list (parser
, type
);
17932 /* Consume the final '}'. */
17933 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17937 timevar_pop (TV_PARSE_ENUM
);
17941 /* If a ';' follows, then it is an opaque-enum-specifier
17942 and additional restrictions apply. */
17943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17946 error_at (type_start_token
->location
,
17947 "opaque-enum-specifier without name");
17948 else if (nested_name_specifier
)
17949 error_at (type_start_token
->location
,
17950 "opaque-enum-specifier must use a simple identifier");
17954 /* Look for trailing attributes to apply to this enumeration, and
17955 apply them if appropriate. */
17956 if (cp_parser_allow_gnu_extensions_p (parser
))
17958 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
17959 cplus_decl_attributes (&type
,
17961 (int) ATTR_FLAG_TYPE_IN_PLACE
);
17964 /* Finish up the enumeration. */
17965 if (type
!= error_mark_node
)
17967 if (new_value_list
)
17968 finish_enum_value_list (type
);
17970 finish_enum (type
);
17973 if (nested_name_specifier
)
17975 if (CLASS_TYPE_P (nested_name_specifier
))
17977 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
17978 pop_scope (nested_name_specifier
);
17980 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17982 pop_nested_namespace (nested_name_specifier
);
17986 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17990 /* Parse an enumerator-list. The enumerators all have the indicated
17994 enumerator-definition
17995 enumerator-list , enumerator-definition */
17998 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18002 /* Parse an enumerator-definition. */
18003 cp_parser_enumerator_definition (parser
, type
);
18005 /* If the next token is not a ',', we've reached the end of
18007 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18009 /* Otherwise, consume the `,' and keep going. */
18010 cp_lexer_consume_token (parser
->lexer
);
18011 /* If the next token is a `}', there is a trailing comma. */
18012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18014 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18015 pedwarn (input_location
, OPT_Wpedantic
,
18016 "comma at end of enumerator list");
18022 /* Parse an enumerator-definition. The enumerator has the indicated
18025 enumerator-definition:
18027 enumerator = constant-expression
18034 enumerator-definition:
18035 enumerator attributes [opt]
18036 enumerator attributes [opt] = constant-expression */
18039 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18045 /* Save the input location because we are interested in the location
18046 of the identifier and not the location of the explicit value. */
18047 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18049 /* Look for the identifier. */
18050 identifier
= cp_parser_identifier (parser
);
18051 if (identifier
== error_mark_node
)
18054 /* Parse any specified attributes. */
18055 tree attrs
= cp_parser_attributes_opt (parser
);
18057 /* If the next token is an '=', then there is an explicit value. */
18058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18060 /* Consume the `=' token. */
18061 cp_lexer_consume_token (parser
->lexer
);
18062 /* Parse the value. */
18063 value
= cp_parser_constant_expression (parser
);
18068 /* If we are processing a template, make sure the initializer of the
18069 enumerator doesn't contain any bare template parameter pack. */
18070 if (check_for_bare_parameter_packs (value
))
18071 value
= error_mark_node
;
18073 /* Create the enumerator. */
18074 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18077 /* Parse a namespace-name.
18080 original-namespace-name
18083 Returns the NAMESPACE_DECL for the namespace. */
18086 cp_parser_namespace_name (cp_parser
* parser
)
18089 tree namespace_decl
;
18091 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18093 /* Get the name of the namespace. */
18094 identifier
= cp_parser_identifier (parser
);
18095 if (identifier
== error_mark_node
)
18096 return error_mark_node
;
18098 /* Look up the identifier in the currently active scope. Look only
18099 for namespaces, due to:
18101 [basic.lookup.udir]
18103 When looking up a namespace-name in a using-directive or alias
18104 definition, only namespace names are considered.
18108 [basic.lookup.qual]
18110 During the lookup of a name preceding the :: scope resolution
18111 operator, object, function, and enumerator names are ignored.
18113 (Note that cp_parser_qualifying_entity only calls this
18114 function if the token after the name is the scope resolution
18116 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18118 /*is_template=*/false,
18119 /*is_namespace=*/true,
18120 /*check_dependency=*/true,
18121 /*ambiguous_decls=*/NULL
,
18123 /* If it's not a namespace, issue an error. */
18124 if (namespace_decl
== error_mark_node
18125 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18127 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18128 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18129 cp_parser_error (parser
, "expected namespace-name");
18130 namespace_decl
= error_mark_node
;
18133 return namespace_decl
;
18136 /* Parse a namespace-definition.
18138 namespace-definition:
18139 named-namespace-definition
18140 unnamed-namespace-definition
18142 named-namespace-definition:
18143 original-namespace-definition
18144 extension-namespace-definition
18146 original-namespace-definition:
18147 namespace identifier { namespace-body }
18149 extension-namespace-definition:
18150 namespace original-namespace-name { namespace-body }
18152 unnamed-namespace-definition:
18153 namespace { namespace-body } */
18156 cp_parser_namespace_definition (cp_parser
* parser
)
18158 tree identifier
, attribs
;
18159 bool has_visibility
;
18162 int nested_definition_count
= 0;
18164 cp_ensure_no_omp_declare_simd (parser
);
18165 cp_ensure_no_oacc_routine (parser
);
18166 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
18168 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18170 cp_lexer_consume_token (parser
->lexer
);
18175 /* Look for the `namespace' keyword. */
18176 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18178 /* Parse any specified attributes before the identifier. */
18179 attribs
= cp_parser_attributes_opt (parser
);
18181 /* Get the name of the namespace. We do not attempt to distinguish
18182 between an original-namespace-definition and an
18183 extension-namespace-definition at this point. The semantic
18184 analysis routines are responsible for that. */
18185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18186 identifier
= cp_parser_identifier (parser
);
18188 identifier
= NULL_TREE
;
18190 /* Parse any specified attributes after the identifier. */
18191 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
18192 if (post_ident_attribs
)
18195 attribs
= chainon (attribs
, post_ident_attribs
);
18197 attribs
= post_ident_attribs
;
18200 /* Start the namespace. */
18201 bool ok
= push_namespace (identifier
);
18203 /* Parse any nested namespace definition. */
18204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18207 error_at (token
->location
, "a nested namespace definition cannot have attributes");
18208 if (cxx_dialect
< cxx1z
)
18209 pedwarn (input_location
, OPT_Wpedantic
,
18210 "nested namespace definitions only available with "
18211 "-std=c++1z or -std=gnu++1z");
18213 error_at (token
->location
, "a nested namespace definition cannot be inline");
18214 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18216 cp_lexer_consume_token (parser
->lexer
);
18217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18218 identifier
= cp_parser_identifier (parser
);
18221 cp_parser_error (parser
, "nested identifier required");
18224 if (push_namespace (identifier
))
18225 ++nested_definition_count
;
18229 /* Look for the `{' to validate starting the namespace. */
18230 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
18232 /* "inline namespace" is equivalent to a stub namespace definition
18233 followed by a strong using directive. */
18234 if (is_inline
&& ok
)
18236 tree name_space
= current_namespace
;
18237 /* Set up namespace association. */
18238 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
18239 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
18240 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
18241 /* Import the contents of the inline namespace. */
18243 do_using_directive (name_space
);
18244 push_namespace (identifier
);
18247 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18249 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18251 /* Parse the body of the namespace. */
18252 cp_parser_namespace_body (parser
);
18254 if (has_visibility
)
18255 pop_visibility (1);
18257 /* Finish the nested namespace definitions. */
18258 while (nested_definition_count
--)
18261 /* Finish the namespace. */
18264 /* Look for the final `}'. */
18265 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18268 /* Parse a namespace-body.
18271 declaration-seq [opt] */
18274 cp_parser_namespace_body (cp_parser
* parser
)
18276 cp_parser_declaration_seq_opt (parser
);
18279 /* Parse a namespace-alias-definition.
18281 namespace-alias-definition:
18282 namespace identifier = qualified-namespace-specifier ; */
18285 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18288 tree namespace_specifier
;
18290 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18292 /* Look for the `namespace' keyword. */
18293 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18294 /* Look for the identifier. */
18295 identifier
= cp_parser_identifier (parser
);
18296 if (identifier
== error_mark_node
)
18298 /* Look for the `=' token. */
18299 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18300 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18302 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18303 /* Skip the definition. */
18304 cp_lexer_consume_token (parser
->lexer
);
18305 if (cp_parser_skip_to_closing_brace (parser
))
18306 cp_lexer_consume_token (parser
->lexer
);
18309 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18310 /* Look for the qualified-namespace-specifier. */
18311 namespace_specifier
18312 = cp_parser_qualified_namespace_specifier (parser
);
18313 /* Look for the `;' token. */
18314 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18316 /* Register the alias in the symbol table. */
18317 do_namespace_alias (identifier
, namespace_specifier
);
18320 /* Parse a qualified-namespace-specifier.
18322 qualified-namespace-specifier:
18323 :: [opt] nested-name-specifier [opt] namespace-name
18325 Returns a NAMESPACE_DECL corresponding to the specified
18329 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18331 /* Look for the optional `::'. */
18332 cp_parser_global_scope_opt (parser
,
18333 /*current_scope_valid_p=*/false);
18335 /* Look for the optional nested-name-specifier. */
18336 cp_parser_nested_name_specifier_opt (parser
,
18337 /*typename_keyword_p=*/false,
18338 /*check_dependency_p=*/true,
18340 /*is_declaration=*/true);
18342 return cp_parser_namespace_name (parser
);
18345 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18346 access declaration.
18349 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18350 using :: unqualified-id ;
18352 access-declaration:
18358 cp_parser_using_declaration (cp_parser
* parser
,
18359 bool access_declaration_p
)
18362 bool typename_p
= false;
18363 bool global_scope_p
;
18367 int oldcount
= errorcount
;
18368 cp_token
*diag_token
= NULL
;
18370 if (access_declaration_p
)
18372 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18373 cp_parser_parse_tentatively (parser
);
18377 /* Look for the `using' keyword. */
18378 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18381 /* Peek at the next token. */
18382 token
= cp_lexer_peek_token (parser
->lexer
);
18383 /* See if it's `typename'. */
18384 if (token
->keyword
== RID_TYPENAME
)
18386 /* Remember that we've seen it. */
18388 /* Consume the `typename' token. */
18389 cp_lexer_consume_token (parser
->lexer
);
18393 /* Look for the optional global scope qualification. */
18395 = (cp_parser_global_scope_opt (parser
,
18396 /*current_scope_valid_p=*/false)
18399 /* If we saw `typename', or didn't see `::', then there must be a
18400 nested-name-specifier present. */
18401 if (typename_p
|| !global_scope_p
)
18403 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18404 /*check_dependency_p=*/true,
18406 /*is_declaration=*/true);
18407 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18409 cp_parser_skip_to_end_of_block_or_statement (parser
);
18413 /* Otherwise, we could be in either of the two productions. In that
18414 case, treat the nested-name-specifier as optional. */
18416 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18417 /*typename_keyword_p=*/false,
18418 /*check_dependency_p=*/true,
18420 /*is_declaration=*/true);
18422 qscope
= global_namespace
;
18423 else if (UNSCOPED_ENUM_P (qscope
))
18424 qscope
= CP_TYPE_CONTEXT (qscope
);
18426 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18427 /* Something has already gone wrong; there's no need to parse
18428 further. Since an error has occurred, the return value of
18429 cp_parser_parse_definitely will be false, as required. */
18430 return cp_parser_parse_definitely (parser
);
18432 token
= cp_lexer_peek_token (parser
->lexer
);
18433 /* Parse the unqualified-id. */
18434 identifier
= cp_parser_unqualified_id (parser
,
18435 /*template_keyword_p=*/false,
18436 /*check_dependency_p=*/true,
18437 /*declarator_p=*/true,
18438 /*optional_p=*/false);
18440 if (access_declaration_p
)
18442 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18443 cp_parser_simulate_error (parser
);
18444 if (!cp_parser_parse_definitely (parser
))
18447 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18449 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18450 if (cxx_dialect
< cxx1z
18451 && !in_system_header_at (ell
->location
))
18452 pedwarn (ell
->location
, 0,
18453 "pack expansion in using-declaration only available "
18454 "with -std=c++1z or -std=gnu++1z");
18455 qscope
= make_pack_expansion (qscope
);
18458 /* The function we call to handle a using-declaration is different
18459 depending on what scope we are in. */
18460 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18462 else if (!identifier_p (identifier
)
18463 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18464 /* [namespace.udecl]
18466 A using declaration shall not name a template-id. */
18467 error_at (token
->location
,
18468 "a template-id may not appear in a using-declaration");
18471 if (at_class_scope_p ())
18473 /* Create the USING_DECL. */
18474 decl
= do_class_using_decl (qscope
, identifier
);
18476 if (decl
&& typename_p
)
18477 USING_DECL_TYPENAME_P (decl
) = 1;
18479 if (check_for_bare_parameter_packs (decl
))
18481 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18485 /* Add it to the list of members in this class. */
18486 finish_member_declaration (decl
);
18490 decl
= cp_parser_lookup_name_simple (parser
,
18493 if (decl
== error_mark_node
)
18494 cp_parser_name_lookup_error (parser
, identifier
,
18497 else if (check_for_bare_parameter_packs (decl
))
18499 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18502 else if (!at_namespace_scope_p ())
18503 do_local_using_decl (decl
, qscope
, identifier
);
18505 do_toplevel_using_decl (decl
, qscope
, identifier
);
18509 if (!access_declaration_p
18510 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18512 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18513 if (cxx_dialect
< cxx1z
)
18514 pedwarn (comma
->location
, 0,
18515 "comma-separated list in using-declaration only available "
18516 "with -std=c++1z or -std=gnu++1z");
18520 /* Look for the final `;'. */
18521 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18523 if (access_declaration_p
&& errorcount
== oldcount
)
18524 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18525 "access declarations are deprecated "
18526 "in favour of using-declarations; "
18527 "suggestion: add the %<using%> keyword");
18532 /* Parse an alias-declaration.
18535 using identifier attribute-specifier-seq [opt] = type-id */
18538 cp_parser_alias_declaration (cp_parser
* parser
)
18540 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18541 location_t id_location
;
18542 cp_declarator
*declarator
;
18543 cp_decl_specifier_seq decl_specs
;
18545 const char *saved_message
= NULL
;
18547 /* Look for the `using' keyword. */
18548 cp_token
*using_token
18549 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18550 if (using_token
== NULL
)
18551 return error_mark_node
;
18553 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18554 id
= cp_parser_identifier (parser
);
18555 if (id
== error_mark_node
)
18556 return error_mark_node
;
18558 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18559 attributes
= cp_parser_attributes_opt (parser
);
18560 if (attributes
== error_mark_node
)
18561 return error_mark_node
;
18563 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18565 if (cp_parser_error_occurred (parser
))
18566 return error_mark_node
;
18568 cp_parser_commit_to_tentative_parse (parser
);
18570 /* Now we are going to parse the type-id of the declaration. */
18575 "A type-specifier-seq shall not define a class or enumeration
18576 unless it appears in the type-id of an alias-declaration (7.1.3) that
18577 is not the declaration of a template-declaration."
18579 In other words, if we currently are in an alias template, the
18580 type-id should not define a type.
18582 So let's set parser->type_definition_forbidden_message in that
18583 case; cp_parser_check_type_definition (called by
18584 cp_parser_class_specifier) will then emit an error if a type is
18585 defined in the type-id. */
18586 if (parser
->num_template_parameter_lists
)
18588 saved_message
= parser
->type_definition_forbidden_message
;
18589 parser
->type_definition_forbidden_message
=
18590 G_("types may not be defined in alias template declarations");
18593 type
= cp_parser_type_id (parser
);
18595 /* Restore the error message if need be. */
18596 if (parser
->num_template_parameter_lists
)
18597 parser
->type_definition_forbidden_message
= saved_message
;
18599 if (type
== error_mark_node
18600 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18602 cp_parser_skip_to_end_of_block_or_statement (parser
);
18603 return error_mark_node
;
18606 /* A typedef-name can also be introduced by an alias-declaration. The
18607 identifier following the using keyword becomes a typedef-name. It has
18608 the same semantics as if it were introduced by the typedef
18609 specifier. In particular, it does not define a new type and it shall
18610 not appear in the type-id. */
18612 clear_decl_specs (&decl_specs
);
18613 decl_specs
.type
= type
;
18614 if (attributes
!= NULL_TREE
)
18616 decl_specs
.attributes
= attributes
;
18617 set_and_check_decl_spec_loc (&decl_specs
,
18621 set_and_check_decl_spec_loc (&decl_specs
,
18624 set_and_check_decl_spec_loc (&decl_specs
,
18628 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18629 declarator
->id_loc
= id_location
;
18631 member_p
= at_class_scope_p ();
18633 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18634 NULL_TREE
, attributes
);
18636 decl
= start_decl (declarator
, &decl_specs
, 0,
18637 attributes
, NULL_TREE
, &pushed_scope
);
18638 if (decl
== error_mark_node
)
18641 // Attach constraints to the alias declaration.
18642 if (flag_concepts
&& current_template_parms
)
18644 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18645 tree constr
= build_constraints (reqs
, NULL_TREE
);
18646 set_constraints (decl
, constr
);
18649 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18652 pop_scope (pushed_scope
);
18654 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18655 added into the symbol table; otherwise, return the TYPE_DECL. */
18656 if (DECL_LANG_SPECIFIC (decl
)
18657 && DECL_TEMPLATE_INFO (decl
)
18658 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18660 decl
= DECL_TI_TEMPLATE (decl
);
18662 check_member_template (decl
);
18668 /* Parse a using-directive.
18671 using namespace :: [opt] nested-name-specifier [opt]
18672 namespace-name ; */
18675 cp_parser_using_directive (cp_parser
* parser
)
18677 tree namespace_decl
;
18680 /* Look for the `using' keyword. */
18681 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18682 /* And the `namespace' keyword. */
18683 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18684 /* Look for the optional `::' operator. */
18685 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18686 /* And the optional nested-name-specifier. */
18687 cp_parser_nested_name_specifier_opt (parser
,
18688 /*typename_keyword_p=*/false,
18689 /*check_dependency_p=*/true,
18691 /*is_declaration=*/true);
18692 /* Get the namespace being used. */
18693 namespace_decl
= cp_parser_namespace_name (parser
);
18694 /* And any specified attributes. */
18695 attribs
= cp_parser_attributes_opt (parser
);
18696 /* Update the symbol table. */
18697 parse_using_directive (namespace_decl
, attribs
);
18698 /* Look for the final `;'. */
18699 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18702 /* Parse an asm-definition.
18705 asm ( string-literal ) ;
18710 asm volatile [opt] ( string-literal ) ;
18711 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18712 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18713 : asm-operand-list [opt] ) ;
18714 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18715 : asm-operand-list [opt]
18716 : asm-clobber-list [opt] ) ;
18717 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18718 : asm-clobber-list [opt]
18719 : asm-goto-list ) ; */
18722 cp_parser_asm_definition (cp_parser
* parser
)
18725 tree outputs
= NULL_TREE
;
18726 tree inputs
= NULL_TREE
;
18727 tree clobbers
= NULL_TREE
;
18728 tree labels
= NULL_TREE
;
18730 bool volatile_p
= false;
18731 bool extended_p
= false;
18732 bool invalid_inputs_p
= false;
18733 bool invalid_outputs_p
= false;
18734 bool goto_p
= false;
18735 required_token missing
= RT_NONE
;
18737 /* Look for the `asm' keyword. */
18738 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
18740 if (parser
->in_function_body
18741 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
18743 error ("%<asm%> in %<constexpr%> function");
18744 cp_function_chain
->invalid_constexpr
= true;
18747 /* See if the next token is `volatile'. */
18748 if (cp_parser_allow_gnu_extensions_p (parser
)
18749 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
18751 /* Remember that we saw the `volatile' keyword. */
18753 /* Consume the token. */
18754 cp_lexer_consume_token (parser
->lexer
);
18756 if (cp_parser_allow_gnu_extensions_p (parser
)
18757 && parser
->in_function_body
18758 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
18760 /* Remember that we saw the `goto' keyword. */
18762 /* Consume the token. */
18763 cp_lexer_consume_token (parser
->lexer
);
18765 /* Look for the opening `('. */
18766 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
18768 /* Look for the string. */
18769 string
= cp_parser_string_literal (parser
, false, false);
18770 if (string
== error_mark_node
)
18772 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18773 /*consume_paren=*/true);
18777 /* If we're allowing GNU extensions, check for the extended assembly
18778 syntax. Unfortunately, the `:' tokens need not be separated by
18779 a space in C, and so, for compatibility, we tolerate that here
18780 too. Doing that means that we have to treat the `::' operator as
18782 if (cp_parser_allow_gnu_extensions_p (parser
)
18783 && parser
->in_function_body
18784 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
18785 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
18787 bool inputs_p
= false;
18788 bool clobbers_p
= false;
18789 bool labels_p
= false;
18791 /* The extended syntax was used. */
18794 /* Look for outputs. */
18795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18797 /* Consume the `:'. */
18798 cp_lexer_consume_token (parser
->lexer
);
18799 /* Parse the output-operands. */
18800 if (cp_lexer_next_token_is_not (parser
->lexer
,
18802 && cp_lexer_next_token_is_not (parser
->lexer
,
18804 && cp_lexer_next_token_is_not (parser
->lexer
,
18808 outputs
= cp_parser_asm_operand_list (parser
);
18809 if (outputs
== error_mark_node
)
18810 invalid_outputs_p
= true;
18813 /* If the next token is `::', there are no outputs, and the
18814 next token is the beginning of the inputs. */
18815 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18816 /* The inputs are coming next. */
18819 /* Look for inputs. */
18821 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18823 /* Consume the `:' or `::'. */
18824 cp_lexer_consume_token (parser
->lexer
);
18825 /* Parse the output-operands. */
18826 if (cp_lexer_next_token_is_not (parser
->lexer
,
18828 && cp_lexer_next_token_is_not (parser
->lexer
,
18830 && cp_lexer_next_token_is_not (parser
->lexer
,
18833 inputs
= cp_parser_asm_operand_list (parser
);
18834 if (inputs
== error_mark_node
)
18835 invalid_inputs_p
= true;
18838 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18839 /* The clobbers are coming next. */
18842 /* Look for clobbers. */
18844 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18847 /* Consume the `:' or `::'. */
18848 cp_lexer_consume_token (parser
->lexer
);
18849 /* Parse the clobbers. */
18850 if (cp_lexer_next_token_is_not (parser
->lexer
,
18852 && cp_lexer_next_token_is_not (parser
->lexer
,
18854 clobbers
= cp_parser_asm_clobber_list (parser
);
18857 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18858 /* The labels are coming next. */
18861 /* Look for labels. */
18863 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
18866 /* Consume the `:' or `::'. */
18867 cp_lexer_consume_token (parser
->lexer
);
18868 /* Parse the labels. */
18869 labels
= cp_parser_asm_label_list (parser
);
18872 if (goto_p
&& !labels_p
)
18873 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
18876 missing
= RT_COLON_SCOPE
;
18878 /* Look for the closing `)'. */
18879 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
18880 missing
? missing
: RT_CLOSE_PAREN
))
18881 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18882 /*consume_paren=*/true);
18883 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18885 if (!invalid_inputs_p
&& !invalid_outputs_p
)
18887 /* Create the ASM_EXPR. */
18888 if (parser
->in_function_body
)
18890 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
18891 inputs
, clobbers
, labels
);
18892 /* If the extended syntax was not used, mark the ASM_EXPR. */
18895 tree temp
= asm_stmt
;
18896 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
18897 temp
= TREE_OPERAND (temp
, 0);
18899 ASM_INPUT_P (temp
) = 1;
18903 symtab
->finalize_toplevel_asm (string
);
18907 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18908 type that comes from the decl-specifier-seq. */
18911 strip_declarator_types (tree type
, cp_declarator
*declarator
)
18913 for (cp_declarator
*d
= declarator
; d
;)
18923 if (TYPE_PTRMEMFUNC_P (type
))
18924 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
18925 type
= TREE_TYPE (type
);
18933 /* Declarators [gram.dcl.decl] */
18935 /* Parse an init-declarator.
18938 declarator initializer [opt]
18943 declarator asm-specification [opt] attributes [opt] initializer [opt]
18945 function-definition:
18946 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18948 decl-specifier-seq [opt] declarator function-try-block
18952 function-definition:
18953 __extension__ function-definition
18957 function-definition:
18958 decl-specifier-seq [opt] declarator function-transaction-block
18960 The DECL_SPECIFIERS apply to this declarator. Returns a
18961 representation of the entity declared. If MEMBER_P is TRUE, then
18962 this declarator appears in a class scope. The new DECL created by
18963 this declarator is returned.
18965 The CHECKS are access checks that should be performed once we know
18966 what entity is being declared (and, therefore, what classes have
18969 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18970 for a function-definition here as well. If the declarator is a
18971 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18972 be TRUE upon return. By that point, the function-definition will
18973 have been completely parsed.
18975 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18978 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18979 parsed declaration if it is an uninitialized single declarator not followed
18980 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18981 if present, will not be consumed. If returned, this declarator will be
18982 created with SD_INITIALIZED but will not call cp_finish_decl.
18984 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18985 and there is an initializer, the pointed location_t is set to the
18986 location of the '=' or `(', or '{' in C++11 token introducing the
18990 cp_parser_init_declarator (cp_parser
* parser
,
18991 cp_decl_specifier_seq
*decl_specifiers
,
18992 vec
<deferred_access_check
, va_gc
> *checks
,
18993 bool function_definition_allowed_p
,
18995 int declares_class_or_enum
,
18996 bool* function_definition_p
,
18997 tree
* maybe_range_for_decl
,
18998 location_t
* init_loc
,
19001 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19002 *attributes_start_token
= NULL
;
19003 cp_declarator
*declarator
;
19004 tree prefix_attributes
;
19005 tree attributes
= NULL
;
19006 tree asm_specification
;
19008 tree decl
= NULL_TREE
;
19010 int is_initialized
;
19011 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19012 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19014 enum cpp_ttype initialization_kind
;
19015 bool is_direct_init
= false;
19016 bool is_non_constant_init
;
19017 int ctor_dtor_or_conv_p
;
19018 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19019 tree pushed_scope
= NULL_TREE
;
19020 bool range_for_decl_p
= false;
19021 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19022 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19024 /* Gather the attributes that were provided with the
19025 decl-specifiers. */
19026 prefix_attributes
= decl_specifiers
->attributes
;
19028 /* Assume that this is not the declarator for a function
19030 if (function_definition_p
)
19031 *function_definition_p
= false;
19033 /* Default arguments are only permitted for function parameters. */
19034 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19035 parser
->default_arg_ok_p
= false;
19037 /* Defer access checks while parsing the declarator; we cannot know
19038 what names are accessible until we know what is being
19040 resume_deferring_access_checks ();
19042 token
= cp_lexer_peek_token (parser
->lexer
);
19044 /* Parse the declarator. */
19046 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19047 &ctor_dtor_or_conv_p
,
19048 /*parenthesized_p=*/NULL
,
19049 member_p
, friend_p
);
19050 /* Gather up the deferred checks. */
19051 stop_deferring_access_checks ();
19053 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19055 /* If the DECLARATOR was erroneous, there's no need to go
19057 if (declarator
== cp_error_declarator
)
19058 return error_mark_node
;
19060 /* Check that the number of template-parameter-lists is OK. */
19061 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19063 return error_mark_node
;
19065 if (declares_class_or_enum
& 2)
19066 cp_parser_check_for_definition_in_return_type (declarator
,
19067 decl_specifiers
->type
,
19068 decl_specifiers
->locations
[ds_type_spec
]);
19070 /* Figure out what scope the entity declared by the DECLARATOR is
19071 located in. `grokdeclarator' sometimes changes the scope, so
19072 we compute it now. */
19073 scope
= get_scope_of_declarator (declarator
);
19075 /* Perform any lookups in the declared type which were thought to be
19076 dependent, but are not in the scope of the declarator. */
19077 decl_specifiers
->type
19078 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19080 /* If we're allowing GNU extensions, look for an
19081 asm-specification. */
19082 if (cp_parser_allow_gnu_extensions_p (parser
))
19084 /* Look for an asm-specification. */
19085 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19086 asm_specification
= cp_parser_asm_specification_opt (parser
);
19089 asm_specification
= NULL_TREE
;
19091 /* Look for attributes. */
19092 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19093 attributes
= cp_parser_attributes_opt (parser
);
19095 /* Peek at the next token. */
19096 token
= cp_lexer_peek_token (parser
->lexer
);
19098 bool bogus_implicit_tmpl
= false;
19100 if (function_declarator_p (declarator
))
19102 /* Handle C++17 deduction guides. */
19103 if (!decl_specifiers
->type
19104 && ctor_dtor_or_conv_p
<= 0
19105 && cxx_dialect
>= cxx1z
)
19107 cp_declarator
*id
= get_id_declarator (declarator
);
19108 tree name
= id
->u
.id
.unqualified_name
;
19109 parser
->scope
= id
->u
.id
.qualifying_scope
;
19110 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19112 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19113 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19115 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19116 id
->u
.id
.sfk
= sfk_deduction_guide
;
19117 ctor_dtor_or_conv_p
= 1;
19121 /* Check to see if the token indicates the start of a
19122 function-definition. */
19123 if (cp_parser_token_starts_function_definition_p (token
))
19125 if (!function_definition_allowed_p
)
19127 /* If a function-definition should not appear here, issue an
19129 cp_parser_error (parser
,
19130 "a function-definition is not allowed here");
19131 return error_mark_node
;
19134 location_t func_brace_location
19135 = cp_lexer_peek_token (parser
->lexer
)->location
;
19137 /* Neither attributes nor an asm-specification are allowed
19138 on a function-definition. */
19139 if (asm_specification
)
19140 error_at (asm_spec_start_token
->location
,
19141 "an asm-specification is not allowed "
19142 "on a function-definition");
19144 error_at (attributes_start_token
->location
,
19145 "attributes are not allowed "
19146 "on a function-definition");
19147 /* This is a function-definition. */
19148 *function_definition_p
= true;
19150 /* Parse the function definition. */
19152 decl
= cp_parser_save_member_function_body (parser
,
19155 prefix_attributes
);
19158 (cp_parser_function_definition_from_specifiers_and_declarator
19159 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19161 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19163 /* This is where the prologue starts... */
19164 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19165 = func_brace_location
;
19171 else if (parser
->fully_implicit_function_template_p
)
19173 /* A non-template declaration involving a function parameter list
19174 containing an implicit template parameter will be made into a
19175 template. If the resulting declaration is not going to be an
19176 actual function then finish the template scope here to prevent it.
19177 An error message will be issued once we have a decl to talk about.
19179 FIXME probably we should do type deduction rather than create an
19180 implicit template, but the standard currently doesn't allow it. */
19181 bogus_implicit_tmpl
= true;
19182 finish_fully_implicit_template (parser
, NULL_TREE
);
19187 Only in function declarations for constructors, destructors, type
19188 conversions, and deduction guides can the decl-specifier-seq be omitted.
19190 We explicitly postpone this check past the point where we handle
19191 function-definitions because we tolerate function-definitions
19192 that are missing their return types in some modes. */
19193 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19195 cp_parser_error (parser
,
19196 "expected constructor, destructor, or type conversion");
19197 return error_mark_node
;
19200 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19201 if (token
->type
== CPP_EQ
19202 || token
->type
== CPP_OPEN_PAREN
19203 || token
->type
== CPP_OPEN_BRACE
)
19205 is_initialized
= SD_INITIALIZED
;
19206 initialization_kind
= token
->type
;
19207 if (maybe_range_for_decl
)
19208 *maybe_range_for_decl
= error_mark_node
;
19209 tmp_init_loc
= token
->location
;
19210 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19211 *init_loc
= tmp_init_loc
;
19213 if (token
->type
== CPP_EQ
19214 && function_declarator_p (declarator
))
19216 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19217 if (t2
->keyword
== RID_DEFAULT
)
19218 is_initialized
= SD_DEFAULTED
;
19219 else if (t2
->keyword
== RID_DELETE
)
19220 is_initialized
= SD_DELETED
;
19225 /* If the init-declarator isn't initialized and isn't followed by a
19226 `,' or `;', it's not a valid init-declarator. */
19227 if (token
->type
!= CPP_COMMA
19228 && token
->type
!= CPP_SEMICOLON
)
19230 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19231 range_for_decl_p
= true;
19234 if (!maybe_range_for_decl
)
19235 cp_parser_error (parser
, "expected initializer");
19236 return error_mark_node
;
19239 is_initialized
= SD_UNINITIALIZED
;
19240 initialization_kind
= CPP_EOF
;
19243 /* Because start_decl has side-effects, we should only call it if we
19244 know we're going ahead. By this point, we know that we cannot
19245 possibly be looking at any other construct. */
19246 cp_parser_commit_to_tentative_parse (parser
);
19248 /* Enter the newly declared entry in the symbol table. If we're
19249 processing a declaration in a class-specifier, we wait until
19250 after processing the initializer. */
19253 if (parser
->in_unbraced_linkage_specification_p
)
19254 decl_specifiers
->storage_class
= sc_extern
;
19255 decl
= start_decl (declarator
, decl_specifiers
,
19256 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19257 attributes
, prefix_attributes
, &pushed_scope
);
19258 cp_finalize_omp_declare_simd (parser
, decl
);
19259 cp_finalize_oacc_routine (parser
, decl
, false);
19260 /* Adjust location of decl if declarator->id_loc is more appropriate:
19261 set, and decl wasn't merged with another decl, in which case its
19262 location would be different from input_location, and more accurate. */
19264 && declarator
->id_loc
!= UNKNOWN_LOCATION
19265 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19266 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19269 /* Enter the SCOPE. That way unqualified names appearing in the
19270 initializer will be looked up in SCOPE. */
19271 pushed_scope
= push_scope (scope
);
19273 /* Perform deferred access control checks, now that we know in which
19274 SCOPE the declared entity resides. */
19275 if (!member_p
&& decl
)
19277 tree saved_current_function_decl
= NULL_TREE
;
19279 /* If the entity being declared is a function, pretend that we
19280 are in its scope. If it is a `friend', it may have access to
19281 things that would not otherwise be accessible. */
19282 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19284 saved_current_function_decl
= current_function_decl
;
19285 current_function_decl
= decl
;
19288 /* Perform access checks for template parameters. */
19289 cp_parser_perform_template_parameter_access_checks (checks
);
19291 /* Perform the access control checks for the declarator and the
19292 decl-specifiers. */
19293 perform_deferred_access_checks (tf_warning_or_error
);
19295 /* Restore the saved value. */
19296 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19297 current_function_decl
= saved_current_function_decl
;
19300 /* Parse the initializer. */
19301 initializer
= NULL_TREE
;
19302 is_direct_init
= false;
19303 is_non_constant_init
= true;
19304 if (is_initialized
)
19306 if (function_declarator_p (declarator
))
19308 if (initialization_kind
== CPP_EQ
)
19309 initializer
= cp_parser_pure_specifier (parser
);
19312 /* If the declaration was erroneous, we don't really
19313 know what the user intended, so just silently
19314 consume the initializer. */
19315 if (decl
!= error_mark_node
)
19316 error_at (tmp_init_loc
, "initializer provided for function");
19317 cp_parser_skip_to_closing_parenthesis (parser
,
19318 /*recovering=*/true,
19319 /*or_comma=*/false,
19320 /*consume_paren=*/true);
19325 /* We want to record the extra mangling scope for in-class
19326 initializers of class members and initializers of static data
19327 member templates. The former involves deferring
19328 parsing of the initializer until end of class as with default
19329 arguments. So right here we only handle the latter. */
19330 if (!member_p
&& processing_template_decl
)
19331 start_lambda_scope (decl
);
19332 initializer
= cp_parser_initializer (parser
,
19334 &is_non_constant_init
);
19335 if (!member_p
&& processing_template_decl
)
19336 finish_lambda_scope ();
19337 if (initializer
== error_mark_node
)
19338 cp_parser_skip_to_end_of_statement (parser
);
19342 /* The old parser allows attributes to appear after a parenthesized
19343 initializer. Mark Mitchell proposed removing this functionality
19344 on the GCC mailing lists on 2002-08-13. This parser accepts the
19345 attributes -- but ignores them. */
19346 if (cp_parser_allow_gnu_extensions_p (parser
)
19347 && initialization_kind
== CPP_OPEN_PAREN
)
19348 if (cp_parser_attributes_opt (parser
))
19349 warning (OPT_Wattributes
,
19350 "attributes after parenthesized initializer ignored");
19352 /* And now complain about a non-function implicit template. */
19353 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19354 error_at (DECL_SOURCE_LOCATION (decl
),
19355 "non-function %qD declared as implicit template", decl
);
19357 /* For an in-class declaration, use `grokfield' to create the
19363 pop_scope (pushed_scope
);
19364 pushed_scope
= NULL_TREE
;
19366 decl
= grokfield (declarator
, decl_specifiers
,
19367 initializer
, !is_non_constant_init
,
19368 /*asmspec=*/NULL_TREE
,
19369 chainon (attributes
, prefix_attributes
));
19370 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19371 cp_parser_save_default_args (parser
, decl
);
19372 cp_finalize_omp_declare_simd (parser
, decl
);
19373 cp_finalize_oacc_routine (parser
, decl
, false);
19376 /* Finish processing the declaration. But, skip member
19378 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19380 cp_finish_decl (decl
,
19381 initializer
, !is_non_constant_init
,
19383 /* If the initializer is in parentheses, then this is
19384 a direct-initialization, which means that an
19385 `explicit' constructor is OK. Otherwise, an
19386 `explicit' constructor cannot be used. */
19387 ((is_direct_init
|| !is_initialized
)
19388 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19390 else if ((cxx_dialect
!= cxx98
) && friend_p
19391 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19392 /* Core issue #226 (C++0x only): A default template-argument
19393 shall not be specified in a friend class template
19395 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19396 /*is_partial=*/false, /*is_friend_decl=*/1);
19398 if (!friend_p
&& pushed_scope
)
19399 pop_scope (pushed_scope
);
19401 if (function_declarator_p (declarator
)
19402 && parser
->fully_implicit_function_template_p
)
19405 decl
= finish_fully_implicit_template (parser
, decl
);
19407 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19410 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19411 && type_uses_auto (decl_specifiers
->type
))
19412 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19417 /* Parse a declarator.
19421 ptr-operator declarator
19423 abstract-declarator:
19424 ptr-operator abstract-declarator [opt]
19425 direct-abstract-declarator
19430 attributes [opt] direct-declarator
19431 attributes [opt] ptr-operator declarator
19433 abstract-declarator:
19434 attributes [opt] ptr-operator abstract-declarator [opt]
19435 attributes [opt] direct-abstract-declarator
19437 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19438 detect constructors, destructors, deduction guides, or conversion operators.
19439 It is set to -1 if the declarator is a name, and +1 if it is a
19440 function. Otherwise it is set to zero. Usually you just want to
19441 test for >0, but internally the negative value is used.
19443 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19444 a decl-specifier-seq unless it declares a constructor, destructor,
19445 or conversion. It might seem that we could check this condition in
19446 semantic analysis, rather than parsing, but that makes it difficult
19447 to handle something like `f()'. We want to notice that there are
19448 no decl-specifiers, and therefore realize that this is an
19449 expression, not a declaration.)
19451 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19452 the declarator is a direct-declarator of the form "(...)".
19454 MEMBER_P is true iff this declarator is a member-declarator.
19456 FRIEND_P is true iff this declarator is a friend. */
19458 static cp_declarator
*
19459 cp_parser_declarator (cp_parser
* parser
,
19460 cp_parser_declarator_kind dcl_kind
,
19461 int* ctor_dtor_or_conv_p
,
19462 bool* parenthesized_p
,
19463 bool member_p
, bool friend_p
)
19465 cp_declarator
*declarator
;
19466 enum tree_code code
;
19467 cp_cv_quals cv_quals
;
19469 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19471 /* Assume this is not a constructor, destructor, or type-conversion
19473 if (ctor_dtor_or_conv_p
)
19474 *ctor_dtor_or_conv_p
= 0;
19476 if (cp_parser_allow_gnu_extensions_p (parser
))
19477 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19479 /* Check for the ptr-operator production. */
19480 cp_parser_parse_tentatively (parser
);
19481 /* Parse the ptr-operator. */
19482 code
= cp_parser_ptr_operator (parser
,
19487 /* If that worked, then we have a ptr-operator. */
19488 if (cp_parser_parse_definitely (parser
))
19490 /* If a ptr-operator was found, then this declarator was not
19492 if (parenthesized_p
)
19493 *parenthesized_p
= true;
19494 /* The dependent declarator is optional if we are parsing an
19495 abstract-declarator. */
19496 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19497 cp_parser_parse_tentatively (parser
);
19499 /* Parse the dependent declarator. */
19500 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19501 /*ctor_dtor_or_conv_p=*/NULL
,
19502 /*parenthesized_p=*/NULL
,
19503 /*member_p=*/false,
19506 /* If we are parsing an abstract-declarator, we must handle the
19507 case where the dependent declarator is absent. */
19508 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19509 && !cp_parser_parse_definitely (parser
))
19512 declarator
= cp_parser_make_indirect_declarator
19513 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19515 /* Everything else is a direct-declarator. */
19518 if (parenthesized_p
)
19519 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19521 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19522 ctor_dtor_or_conv_p
,
19523 member_p
, friend_p
);
19526 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19527 declarator
->attributes
= gnu_attributes
;
19531 /* Parse a direct-declarator or direct-abstract-declarator.
19535 direct-declarator ( parameter-declaration-clause )
19536 cv-qualifier-seq [opt]
19537 ref-qualifier [opt]
19538 exception-specification [opt]
19539 direct-declarator [ constant-expression [opt] ]
19542 direct-abstract-declarator:
19543 direct-abstract-declarator [opt]
19544 ( parameter-declaration-clause )
19545 cv-qualifier-seq [opt]
19546 ref-qualifier [opt]
19547 exception-specification [opt]
19548 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19549 ( abstract-declarator )
19551 Returns a representation of the declarator. DCL_KIND is
19552 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19553 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19554 we are parsing a direct-declarator. It is
19555 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19556 of ambiguity we prefer an abstract declarator, as per
19557 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19558 as for cp_parser_declarator. */
19560 static cp_declarator
*
19561 cp_parser_direct_declarator (cp_parser
* parser
,
19562 cp_parser_declarator_kind dcl_kind
,
19563 int* ctor_dtor_or_conv_p
,
19564 bool member_p
, bool friend_p
)
19567 cp_declarator
*declarator
= NULL
;
19568 tree scope
= NULL_TREE
;
19569 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19570 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19572 tree pushed_scope
= NULL_TREE
;
19576 /* Peek at the next token. */
19577 token
= cp_lexer_peek_token (parser
->lexer
);
19578 if (token
->type
== CPP_OPEN_PAREN
)
19580 /* This is either a parameter-declaration-clause, or a
19581 parenthesized declarator. When we know we are parsing a
19582 named declarator, it must be a parenthesized declarator
19583 if FIRST is true. For instance, `(int)' is a
19584 parameter-declaration-clause, with an omitted
19585 direct-abstract-declarator. But `((*))', is a
19586 parenthesized abstract declarator. Finally, when T is a
19587 template parameter `(T)' is a
19588 parameter-declaration-clause, and not a parenthesized
19591 We first try and parse a parameter-declaration-clause,
19592 and then try a nested declarator (if FIRST is true).
19594 It is not an error for it not to be a
19595 parameter-declaration-clause, even when FIRST is
19601 The first is the declaration of a function while the
19602 second is the definition of a variable, including its
19605 Having seen only the parenthesis, we cannot know which of
19606 these two alternatives should be selected. Even more
19607 complex are examples like:
19612 The former is a function-declaration; the latter is a
19613 variable initialization.
19615 Thus again, we try a parameter-declaration-clause, and if
19616 that fails, we back out and return. */
19618 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19621 bool is_declarator
= false;
19623 /* In a member-declarator, the only valid interpretation
19624 of a parenthesis is the start of a
19625 parameter-declaration-clause. (It is invalid to
19626 initialize a static data member with a parenthesized
19627 initializer; only the "=" form of initialization is
19630 cp_parser_parse_tentatively (parser
);
19632 /* Consume the `('. */
19633 cp_lexer_consume_token (parser
->lexer
);
19636 /* If this is going to be an abstract declarator, we're
19637 in a declarator and we can't have default args. */
19638 parser
->default_arg_ok_p
= false;
19639 parser
->in_declarator_p
= true;
19642 begin_scope (sk_function_parms
, NULL_TREE
);
19644 /* Parse the parameter-declaration-clause. */
19645 params
= cp_parser_parameter_declaration_clause (parser
);
19647 /* Consume the `)'. */
19648 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19650 /* If all went well, parse the cv-qualifier-seq,
19651 ref-qualifier and the exception-specification. */
19652 if (member_p
|| cp_parser_parse_definitely (parser
))
19654 cp_cv_quals cv_quals
;
19655 cp_virt_specifiers virt_specifiers
;
19656 cp_ref_qualifier ref_qual
;
19657 tree exception_specification
;
19660 bool memfn
= (member_p
|| (pushed_scope
19661 && CLASS_TYPE_P (pushed_scope
)));
19663 is_declarator
= true;
19665 if (ctor_dtor_or_conv_p
)
19666 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19669 /* Parse the cv-qualifier-seq. */
19670 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19671 /* Parse the ref-qualifier. */
19672 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19673 /* Parse the tx-qualifier. */
19674 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19675 /* And the exception-specification. */
19676 exception_specification
19677 = cp_parser_exception_specification_opt (parser
);
19679 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19681 /* In here, we handle cases where attribute is used after
19682 the function declaration. For example:
19683 void func (int x) __attribute__((vector(..))); */
19684 tree gnu_attrs
= NULL_TREE
;
19686 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
19688 cp_parser_parse_tentatively (parser
);
19689 tree attr
= cp_parser_gnu_attributes_opt (parser
);
19690 if (cp_lexer_next_token_is_not (parser
->lexer
,
19692 && cp_lexer_next_token_is_not (parser
->lexer
,
19694 cp_parser_abort_tentative_parse (parser
);
19695 else if (!cp_parser_parse_definitely (parser
))
19700 tree requires_clause
= NULL_TREE
;
19701 late_return
= (cp_parser_late_return_type_opt
19702 (parser
, declarator
, requires_clause
,
19703 memfn
? cv_quals
: -1));
19705 /* Parse the virt-specifier-seq. */
19706 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19708 /* Create the function-declarator. */
19709 declarator
= make_call_declarator (declarator
,
19715 exception_specification
,
19718 declarator
->std_attributes
= attrs
;
19719 declarator
->attributes
= gnu_attrs
;
19720 /* Any subsequent parameter lists are to do with
19721 return type, so are not those of the declared
19723 parser
->default_arg_ok_p
= false;
19726 /* Remove the function parms from scope. */
19727 pop_bindings_and_leave_scope ();
19730 /* Repeat the main loop. */
19734 /* If this is the first, we can try a parenthesized
19738 bool saved_in_type_id_in_expr_p
;
19740 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19741 parser
->in_declarator_p
= saved_in_declarator_p
;
19743 /* Consume the `('. */
19744 cp_lexer_consume_token (parser
->lexer
);
19745 /* Parse the nested declarator. */
19746 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19747 parser
->in_type_id_in_expr_p
= true;
19749 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
19750 /*parenthesized_p=*/NULL
,
19751 member_p
, friend_p
);
19752 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19754 /* Expect a `)'. */
19755 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
19756 declarator
= cp_error_declarator
;
19757 if (declarator
== cp_error_declarator
)
19760 goto handle_declarator
;
19762 /* Otherwise, we must be done. */
19766 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19767 && token
->type
== CPP_OPEN_SQUARE
19768 && !cp_next_tokens_can_be_attribute_p (parser
))
19770 /* Parse an array-declarator. */
19771 tree bounds
, attrs
;
19773 if (ctor_dtor_or_conv_p
)
19774 *ctor_dtor_or_conv_p
= 0;
19777 parser
->default_arg_ok_p
= false;
19778 parser
->in_declarator_p
= true;
19779 /* Consume the `['. */
19780 cp_lexer_consume_token (parser
->lexer
);
19781 /* Peek at the next token. */
19782 token
= cp_lexer_peek_token (parser
->lexer
);
19783 /* If the next token is `]', then there is no
19784 constant-expression. */
19785 if (token
->type
!= CPP_CLOSE_SQUARE
)
19787 bool non_constant_p
;
19789 = cp_parser_constant_expression (parser
,
19790 /*allow_non_constant=*/true,
19792 if (!non_constant_p
)
19794 else if (error_operand_p (bounds
))
19795 /* Already gave an error. */;
19796 else if (!parser
->in_function_body
19797 || current_binding_level
->kind
== sk_function_parms
)
19799 /* Normally, the array bound must be an integral constant
19800 expression. However, as an extension, we allow VLAs
19801 in function scopes as long as they aren't part of a
19802 parameter declaration. */
19803 cp_parser_error (parser
,
19804 "array bound is not an integer constant");
19805 bounds
= error_mark_node
;
19807 else if (processing_template_decl
19808 && !type_dependent_expression_p (bounds
))
19810 /* Remember this wasn't a constant-expression. */
19811 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
19812 TREE_SIDE_EFFECTS (bounds
) = 1;
19816 bounds
= NULL_TREE
;
19817 /* Look for the closing `]'. */
19818 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
19820 declarator
= cp_error_declarator
;
19824 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19825 declarator
= make_array_declarator (declarator
, bounds
);
19826 declarator
->std_attributes
= attrs
;
19828 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
19831 tree qualifying_scope
;
19832 tree unqualified_name
;
19834 special_function_kind sfk
;
19836 bool pack_expansion_p
= false;
19837 cp_token
*declarator_id_start_token
;
19839 /* Parse a declarator-id */
19840 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
19843 cp_parser_parse_tentatively (parser
);
19845 /* If we see an ellipsis, we should be looking at a
19847 if (token
->type
== CPP_ELLIPSIS
)
19849 /* Consume the `...' */
19850 cp_lexer_consume_token (parser
->lexer
);
19852 pack_expansion_p
= true;
19856 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
19858 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
19859 qualifying_scope
= parser
->scope
;
19864 if (!unqualified_name
&& pack_expansion_p
)
19866 /* Check whether an error occurred. */
19867 okay
= !cp_parser_error_occurred (parser
);
19869 /* We already consumed the ellipsis to mark a
19870 parameter pack, but we have no way to report it,
19871 so abort the tentative parse. We will be exiting
19872 immediately anyway. */
19873 cp_parser_abort_tentative_parse (parser
);
19876 okay
= cp_parser_parse_definitely (parser
);
19879 unqualified_name
= error_mark_node
;
19880 else if (unqualified_name
19881 && (qualifying_scope
19882 || (!identifier_p (unqualified_name
))))
19884 cp_parser_error (parser
, "expected unqualified-id");
19885 unqualified_name
= error_mark_node
;
19889 if (!unqualified_name
)
19891 if (unqualified_name
== error_mark_node
)
19893 declarator
= cp_error_declarator
;
19894 pack_expansion_p
= false;
19895 declarator
->parameter_pack_p
= false;
19899 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19901 if (qualifying_scope
&& at_namespace_scope_p ()
19902 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
19904 /* In the declaration of a member of a template class
19905 outside of the class itself, the SCOPE will sometimes
19906 be a TYPENAME_TYPE. For example, given:
19908 template <typename T>
19909 int S<T>::R::i = 3;
19911 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19912 this context, we must resolve S<T>::R to an ordinary
19913 type, rather than a typename type.
19915 The reason we normally avoid resolving TYPENAME_TYPEs
19916 is that a specialization of `S' might render
19917 `S<T>::R' not a type. However, if `S' is
19918 specialized, then this `i' will not be used, so there
19919 is no harm in resolving the types here. */
19922 /* Resolve the TYPENAME_TYPE. */
19923 type
= resolve_typename_type (qualifying_scope
,
19924 /*only_current_p=*/false);
19925 /* If that failed, the declarator is invalid. */
19926 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19928 if (typedef_variant_p (type
))
19929 error_at (declarator_id_start_token
->location
,
19930 "cannot define member of dependent typedef "
19933 error_at (declarator_id_start_token
->location
,
19934 "%<%T::%E%> is not a type",
19935 TYPE_CONTEXT (qualifying_scope
),
19936 TYPE_IDENTIFIER (qualifying_scope
));
19938 qualifying_scope
= type
;
19943 if (unqualified_name
)
19947 if (qualifying_scope
19948 && CLASS_TYPE_P (qualifying_scope
))
19949 class_type
= qualifying_scope
;
19951 class_type
= current_class_type
;
19953 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
19955 tree name_type
= TREE_TYPE (unqualified_name
);
19956 if (class_type
&& same_type_p (name_type
, class_type
))
19958 if (qualifying_scope
19959 && CLASSTYPE_USE_TEMPLATE (name_type
))
19961 error_at (declarator_id_start_token
->location
,
19962 "invalid use of constructor as a template");
19963 inform (declarator_id_start_token
->location
,
19964 "use %<%T::%D%> instead of %<%T::%D%> to "
19965 "name the constructor in a qualified name",
19967 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
19968 class_type
, name_type
);
19969 declarator
= cp_error_declarator
;
19973 unqualified_name
= constructor_name (class_type
);
19977 /* We do not attempt to print the declarator
19978 here because we do not have enough
19979 information about its original syntactic
19981 cp_parser_error (parser
, "invalid declarator");
19982 declarator
= cp_error_declarator
;
19989 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
19990 sfk
= sfk_destructor
;
19991 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
19992 sfk
= sfk_conversion
;
19993 else if (/* There's no way to declare a constructor
19994 for an unnamed type, even if the type
19995 got a name for linkage purposes. */
19996 !TYPE_WAS_UNNAMED (class_type
)
19997 /* Handle correctly (c++/19200):
20011 friend void N::S();
20014 && class_type
!= qualifying_scope
)
20015 && constructor_name_p (unqualified_name
,
20018 unqualified_name
= constructor_name (class_type
);
20019 sfk
= sfk_constructor
;
20021 else if (is_overloaded_fn (unqualified_name
)
20022 && DECL_CONSTRUCTOR_P (get_first_fn
20023 (unqualified_name
)))
20024 sfk
= sfk_constructor
;
20026 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20027 *ctor_dtor_or_conv_p
= -1;
20030 declarator
= make_id_declarator (qualifying_scope
,
20033 declarator
->std_attributes
= attrs
;
20034 declarator
->id_loc
= token
->location
;
20035 declarator
->parameter_pack_p
= pack_expansion_p
;
20037 if (pack_expansion_p
)
20038 maybe_warn_variadic_templates ();
20041 handle_declarator
:;
20042 scope
= get_scope_of_declarator (declarator
);
20045 /* Any names that appear after the declarator-id for a
20046 member are looked up in the containing scope. */
20047 if (at_function_scope_p ())
20049 /* But declarations with qualified-ids can't appear in a
20051 cp_parser_error (parser
, "qualified-id in declaration");
20052 declarator
= cp_error_declarator
;
20055 pushed_scope
= push_scope (scope
);
20057 parser
->in_declarator_p
= true;
20058 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20059 || (declarator
&& declarator
->kind
== cdk_id
))
20060 /* Default args are only allowed on function
20062 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20064 parser
->default_arg_ok_p
= false;
20073 /* For an abstract declarator, we might wind up with nothing at this
20074 point. That's an error; the declarator is not optional. */
20076 cp_parser_error (parser
, "expected declarator");
20078 /* If we entered a scope, we must exit it now. */
20080 pop_scope (pushed_scope
);
20082 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20083 parser
->in_declarator_p
= saved_in_declarator_p
;
20088 /* Parse a ptr-operator.
20091 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20092 * cv-qualifier-seq [opt]
20094 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20095 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20100 & cv-qualifier-seq [opt]
20102 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20103 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20104 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20105 filled in with the TYPE containing the member. *CV_QUALS is
20106 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20107 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20108 Note that the tree codes returned by this function have nothing
20109 to do with the types of trees that will be eventually be created
20110 to represent the pointer or reference type being parsed. They are
20111 just constants with suggestive names. */
20112 static enum tree_code
20113 cp_parser_ptr_operator (cp_parser
* parser
,
20115 cp_cv_quals
*cv_quals
,
20118 enum tree_code code
= ERROR_MARK
;
20120 tree attrs
= NULL_TREE
;
20122 /* Assume that it's not a pointer-to-member. */
20124 /* And that there are no cv-qualifiers. */
20125 *cv_quals
= TYPE_UNQUALIFIED
;
20127 /* Peek at the next token. */
20128 token
= cp_lexer_peek_token (parser
->lexer
);
20130 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20131 if (token
->type
== CPP_MULT
)
20132 code
= INDIRECT_REF
;
20133 else if (token
->type
== CPP_AND
)
20135 else if ((cxx_dialect
!= cxx98
) &&
20136 token
->type
== CPP_AND_AND
) /* C++0x only */
20137 code
= NON_LVALUE_EXPR
;
20139 if (code
!= ERROR_MARK
)
20141 /* Consume the `*', `&' or `&&'. */
20142 cp_lexer_consume_token (parser
->lexer
);
20144 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20145 `&', if we are allowing GNU extensions. (The only qualifier
20146 that can legally appear after `&' is `restrict', but that is
20147 enforced during semantic analysis. */
20148 if (code
== INDIRECT_REF
20149 || cp_parser_allow_gnu_extensions_p (parser
))
20150 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20152 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20153 if (attributes
!= NULL
)
20154 *attributes
= attrs
;
20158 /* Try the pointer-to-member case. */
20159 cp_parser_parse_tentatively (parser
);
20160 /* Look for the optional `::' operator. */
20161 cp_parser_global_scope_opt (parser
,
20162 /*current_scope_valid_p=*/false);
20163 /* Look for the nested-name specifier. */
20164 token
= cp_lexer_peek_token (parser
->lexer
);
20165 cp_parser_nested_name_specifier (parser
,
20166 /*typename_keyword_p=*/false,
20167 /*check_dependency_p=*/true,
20169 /*is_declaration=*/false);
20170 /* If we found it, and the next token is a `*', then we are
20171 indeed looking at a pointer-to-member operator. */
20172 if (!cp_parser_error_occurred (parser
)
20173 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20175 /* Indicate that the `*' operator was used. */
20176 code
= INDIRECT_REF
;
20178 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20179 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20180 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20181 error_at (token
->location
, "cannot form pointer to member of "
20182 "non-class %q#T", parser
->scope
);
20185 /* The type of which the member is a member is given by the
20187 *type
= parser
->scope
;
20188 /* The next name will not be qualified. */
20189 parser
->scope
= NULL_TREE
;
20190 parser
->qualifying_scope
= NULL_TREE
;
20191 parser
->object_scope
= NULL_TREE
;
20192 /* Look for optional c++11 attributes. */
20193 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20194 if (attributes
!= NULL
)
20195 *attributes
= attrs
;
20196 /* Look for the optional cv-qualifier-seq. */
20197 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20200 /* If that didn't work we don't have a ptr-operator. */
20201 if (!cp_parser_parse_definitely (parser
))
20202 cp_parser_error (parser
, "expected ptr-operator");
20208 /* Parse an (optional) cv-qualifier-seq.
20211 cv-qualifier cv-qualifier-seq [opt]
20222 Returns a bitmask representing the cv-qualifiers. */
20225 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20227 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20232 cp_cv_quals cv_qualifier
;
20234 /* Peek at the next token. */
20235 token
= cp_lexer_peek_token (parser
->lexer
);
20236 /* See if it's a cv-qualifier. */
20237 switch (token
->keyword
)
20240 cv_qualifier
= TYPE_QUAL_CONST
;
20244 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20248 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20252 cv_qualifier
= TYPE_UNQUALIFIED
;
20259 if (cv_quals
& cv_qualifier
)
20261 error_at (token
->location
, "duplicate cv-qualifier");
20262 cp_lexer_purge_token (parser
->lexer
);
20266 cp_lexer_consume_token (parser
->lexer
);
20267 cv_quals
|= cv_qualifier
;
20274 /* Parse an (optional) ref-qualifier
20280 Returns cp_ref_qualifier representing ref-qualifier. */
20282 static cp_ref_qualifier
20283 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20285 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20287 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20288 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20293 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20296 switch (token
->type
)
20299 curr_ref_qual
= REF_QUAL_LVALUE
;
20303 curr_ref_qual
= REF_QUAL_RVALUE
;
20307 curr_ref_qual
= REF_QUAL_NONE
;
20311 if (!curr_ref_qual
)
20315 error_at (token
->location
, "multiple ref-qualifiers");
20316 cp_lexer_purge_token (parser
->lexer
);
20320 ref_qual
= curr_ref_qual
;
20321 cp_lexer_consume_token (parser
->lexer
);
20328 /* Parse an optional tx-qualifier.
20332 transaction_safe_dynamic */
20335 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20338 if (token
->type
== CPP_NAME
)
20340 tree name
= token
->u
.value
;
20341 const char *p
= IDENTIFIER_POINTER (name
);
20342 const int len
= strlen ("transaction_safe");
20343 if (!strncmp (p
, "transaction_safe", len
))
20347 || !strcmp (p
, "_dynamic"))
20349 cp_lexer_consume_token (parser
->lexer
);
20352 error ("%E requires %<-fgnu-tm%>", name
);
20363 /* Parse an (optional) virt-specifier-seq.
20365 virt-specifier-seq:
20366 virt-specifier virt-specifier-seq [opt]
20372 Returns a bitmask representing the virt-specifiers. */
20374 static cp_virt_specifiers
20375 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20377 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20382 cp_virt_specifiers virt_specifier
;
20384 /* Peek at the next token. */
20385 token
= cp_lexer_peek_token (parser
->lexer
);
20386 /* See if it's a virt-specifier-qualifier. */
20387 if (token
->type
!= CPP_NAME
)
20389 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
20391 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20392 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20394 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
20396 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20397 virt_specifier
= VIRT_SPEC_FINAL
;
20399 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
20401 virt_specifier
= VIRT_SPEC_FINAL
;
20406 if (virt_specifiers
& virt_specifier
)
20408 error_at (token
->location
, "duplicate virt-specifier");
20409 cp_lexer_purge_token (parser
->lexer
);
20413 cp_lexer_consume_token (parser
->lexer
);
20414 virt_specifiers
|= virt_specifier
;
20417 return virt_specifiers
;
20420 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20421 is in scope even though it isn't real. */
20424 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20428 if (current_class_ptr
)
20430 /* We don't clear this between NSDMIs. Is it already what we want? */
20431 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20432 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20433 && cp_type_quals (type
) == quals
)
20437 this_parm
= build_this_parm (ctype
, quals
);
20438 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20439 current_class_ptr
= NULL_TREE
;
20441 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
20442 current_class_ptr
= this_parm
;
20445 /* Return true iff our current scope is a non-static data member
20449 parsing_nsdmi (void)
20451 /* We recognize NSDMI context by the context-less 'this' pointer set up
20452 by the function above. */
20453 if (current_class_ptr
20454 && TREE_CODE (current_class_ptr
) == PARM_DECL
20455 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20460 /* Return true iff our current scope is a default capturing generic lambda
20461 defined within a template. FIXME: This is part of a workaround (see
20462 semantics.c) to handle building lambda closure types correctly in templates
20463 which we ultimately want to defer to instantiation time. */
20466 parsing_default_capturing_generic_lambda_in_template (void)
20468 if (!processing_template_decl
|| !current_class_type
)
20471 tree lam
= CLASSTYPE_LAMBDA_EXPR (current_class_type
);
20472 if (!lam
|| LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) == CPLD_NONE
)
20475 tree callop
= lambda_function (lam
);
20479 return (DECL_TEMPLATE_INFO (callop
)
20480 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop
)) == callop
)
20481 && ((current_nonlambda_class_type ()
20482 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20483 || ((current_nonlambda_function ()
20484 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20487 /* Parse a late-specified return type, if any. This is not a separate
20488 non-terminal, but part of a function declarator, which looks like
20490 -> trailing-type-specifier-seq abstract-declarator(opt)
20492 Returns the type indicated by the type-id.
20494 In addition to this, parse any queued up #pragma omp declare simd
20495 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20496 #pragma acc routine clauses.
20498 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20502 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20503 tree
& requires_clause
, cp_cv_quals quals
)
20506 tree type
= NULL_TREE
;
20507 bool declare_simd_p
= (parser
->omp_declare_simd
20509 && declarator
->kind
== cdk_id
);
20511 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
20512 && declarator
&& declarator
->kind
== cdk_id
);
20514 bool oacc_routine_p
= (parser
->oacc_routine
20516 && declarator
->kind
== cdk_id
);
20518 /* Peek at the next token. */
20519 token
= cp_lexer_peek_token (parser
->lexer
);
20520 /* A late-specified return type is indicated by an initial '->'. */
20521 if (token
->type
!= CPP_DEREF
20522 && token
->keyword
!= RID_REQUIRES
20523 && !(token
->type
== CPP_NAME
20524 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20525 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
20528 tree save_ccp
= current_class_ptr
;
20529 tree save_ccr
= current_class_ref
;
20532 /* DR 1207: 'this' is in scope in the trailing return type. */
20533 inject_this_parameter (current_class_type
, quals
);
20536 if (token
->type
== CPP_DEREF
)
20538 /* Consume the ->. */
20539 cp_lexer_consume_token (parser
->lexer
);
20541 type
= cp_parser_trailing_type_id (parser
);
20544 /* Function declarations may be followed by a trailing
20545 requires-clause. */
20546 requires_clause
= cp_parser_requires_clause_opt (parser
);
20548 if (cilk_simd_fn_vector_p
)
20549 declarator
->attributes
20550 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
20551 declarator
->attributes
);
20552 if (declare_simd_p
)
20553 declarator
->attributes
20554 = cp_parser_late_parsing_omp_declare_simd (parser
,
20555 declarator
->attributes
);
20556 if (oacc_routine_p
)
20557 declarator
->attributes
20558 = cp_parser_late_parsing_oacc_routine (parser
,
20559 declarator
->attributes
);
20563 current_class_ptr
= save_ccp
;
20564 current_class_ref
= save_ccr
;
20570 /* Parse a declarator-id.
20574 :: [opt] nested-name-specifier [opt] type-name
20576 In the `id-expression' case, the value returned is as for
20577 cp_parser_id_expression if the id-expression was an unqualified-id.
20578 If the id-expression was a qualified-id, then a SCOPE_REF is
20579 returned. The first operand is the scope (either a NAMESPACE_DECL
20580 or TREE_TYPE), but the second is still just a representation of an
20584 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20587 /* The expression must be an id-expression. Assume that qualified
20588 names are the names of types so that:
20591 int S<T>::R::i = 3;
20593 will work; we must treat `S<T>::R' as the name of a type.
20594 Similarly, assume that qualified names are templates, where
20598 int S<T>::R<T>::i = 3;
20601 id
= cp_parser_id_expression (parser
,
20602 /*template_keyword_p=*/false,
20603 /*check_dependency_p=*/false,
20604 /*template_p=*/NULL
,
20605 /*declarator_p=*/true,
20607 if (id
&& BASELINK_P (id
))
20608 id
= BASELINK_FUNCTIONS (id
);
20612 /* Parse a type-id.
20615 type-specifier-seq abstract-declarator [opt]
20617 Returns the TYPE specified. */
20620 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20621 bool is_trailing_return
)
20623 cp_decl_specifier_seq type_specifier_seq
;
20624 cp_declarator
*abstract_declarator
;
20626 /* Parse the type-specifier-seq. */
20627 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20628 is_trailing_return
,
20629 &type_specifier_seq
);
20630 if (is_template_arg
&& type_specifier_seq
.type
20631 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20632 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20633 /* A bare template name as a template argument is a template template
20634 argument, not a placeholder, so fail parsing it as a type argument. */
20636 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20637 cp_parser_simulate_error (parser
);
20638 return error_mark_node
;
20640 if (type_specifier_seq
.type
== error_mark_node
)
20641 return error_mark_node
;
20643 /* There might or might not be an abstract declarator. */
20644 cp_parser_parse_tentatively (parser
);
20645 /* Look for the declarator. */
20646 abstract_declarator
20647 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20648 /*parenthesized_p=*/NULL
,
20649 /*member_p=*/false,
20650 /*friend_p=*/false);
20651 /* Check to see if there really was a declarator. */
20652 if (!cp_parser_parse_definitely (parser
))
20653 abstract_declarator
= NULL
;
20655 if (type_specifier_seq
.type
20656 /* The concepts TS allows 'auto' as a type-id. */
20657 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20658 /* None of the valid uses of 'auto' in C++14 involve the type-id
20659 nonterminal, but it is valid in a trailing-return-type. */
20660 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20661 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20663 /* A type-id with type 'auto' is only ok if the abstract declarator
20664 is a function declarator with a late-specified return type.
20666 A type-id with 'auto' is also valid in a trailing-return-type
20667 in a compound-requirement. */
20668 if (abstract_declarator
20669 && abstract_declarator
->kind
== cdk_function
20670 && abstract_declarator
->u
.function
.late_return_type
)
20672 else if (parser
->in_result_type_constraint_p
)
20676 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20677 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20679 error_at (loc
, "missing template arguments after %qT",
20681 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20685 error_at (loc
, "invalid use of %qT", auto_node
);
20686 return error_mark_node
;
20690 return groktypename (&type_specifier_seq
, abstract_declarator
,
20695 cp_parser_type_id (cp_parser
*parser
)
20697 return cp_parser_type_id_1 (parser
, false, false);
20701 cp_parser_template_type_arg (cp_parser
*parser
)
20704 const char *saved_message
= parser
->type_definition_forbidden_message
;
20705 parser
->type_definition_forbidden_message
20706 = G_("types may not be defined in template arguments");
20707 r
= cp_parser_type_id_1 (parser
, true, false);
20708 parser
->type_definition_forbidden_message
= saved_message
;
20709 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20711 error ("invalid use of %<auto%> in template argument");
20712 r
= error_mark_node
;
20718 cp_parser_trailing_type_id (cp_parser
*parser
)
20720 return cp_parser_type_id_1 (parser
, false, true);
20723 /* Parse a type-specifier-seq.
20725 type-specifier-seq:
20726 type-specifier type-specifier-seq [opt]
20730 type-specifier-seq:
20731 attributes type-specifier-seq [opt]
20733 If IS_DECLARATION is true, we are at the start of a "condition" or
20734 exception-declaration, so we might be followed by a declarator-id.
20736 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20737 i.e. we've just seen "->".
20739 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20742 cp_parser_type_specifier_seq (cp_parser
* parser
,
20743 bool is_declaration
,
20744 bool is_trailing_return
,
20745 cp_decl_specifier_seq
*type_specifier_seq
)
20747 bool seen_type_specifier
= false;
20748 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20749 cp_token
*start_token
= NULL
;
20751 /* Clear the TYPE_SPECIFIER_SEQ. */
20752 clear_decl_specs (type_specifier_seq
);
20754 /* In the context of a trailing return type, enum E { } is an
20755 elaborated-type-specifier followed by a function-body, not an
20757 if (is_trailing_return
)
20758 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
20760 /* Parse the type-specifiers and attributes. */
20763 tree type_specifier
;
20764 bool is_cv_qualifier
;
20766 /* Check for attributes first. */
20767 if (cp_next_tokens_can_be_attribute_p (parser
))
20769 type_specifier_seq
->attributes
=
20770 chainon (type_specifier_seq
->attributes
,
20771 cp_parser_attributes_opt (parser
));
20775 /* record the token of the beginning of the type specifier seq,
20776 for error reporting purposes*/
20778 start_token
= cp_lexer_peek_token (parser
->lexer
);
20780 /* Look for the type-specifier. */
20781 type_specifier
= cp_parser_type_specifier (parser
,
20783 type_specifier_seq
,
20784 /*is_declaration=*/false,
20787 if (!type_specifier
)
20789 /* If the first type-specifier could not be found, this is not a
20790 type-specifier-seq at all. */
20791 if (!seen_type_specifier
)
20793 /* Set in_declarator_p to avoid skipping to the semicolon. */
20794 int in_decl
= parser
->in_declarator_p
;
20795 parser
->in_declarator_p
= true;
20797 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
20798 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20799 cp_parser_error (parser
, "expected type-specifier");
20801 parser
->in_declarator_p
= in_decl
;
20803 type_specifier_seq
->type
= error_mark_node
;
20806 /* If subsequent type-specifiers could not be found, the
20807 type-specifier-seq is complete. */
20811 seen_type_specifier
= true;
20812 /* The standard says that a condition can be:
20814 type-specifier-seq declarator = assignment-expression
20821 we should treat the "S" as a declarator, not as a
20822 type-specifier. The standard doesn't say that explicitly for
20823 type-specifier-seq, but it does say that for
20824 decl-specifier-seq in an ordinary declaration. Perhaps it
20825 would be clearer just to allow a decl-specifier-seq here, and
20826 then add a semantic restriction that if any decl-specifiers
20827 that are not type-specifiers appear, the program is invalid. */
20828 if (is_declaration
&& !is_cv_qualifier
)
20829 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
20833 /* Return whether the function currently being declared has an associated
20834 template parameter list. */
20837 function_being_declared_is_template_p (cp_parser
* parser
)
20839 if (!current_template_parms
|| processing_template_parmlist
)
20842 if (parser
->implicit_template_scope
)
20845 if (at_class_scope_p ()
20846 && TYPE_BEING_DEFINED (current_class_type
))
20847 return parser
->num_template_parameter_lists
!= 0;
20849 return ((int) parser
->num_template_parameter_lists
> template_class_depth
20850 (current_class_type
));
20853 /* Parse a parameter-declaration-clause.
20855 parameter-declaration-clause:
20856 parameter-declaration-list [opt] ... [opt]
20857 parameter-declaration-list , ...
20859 Returns a representation for the parameter declarations. A return
20860 value of NULL indicates a parameter-declaration-clause consisting
20861 only of an ellipsis. */
20864 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
20873 int auto_is_implicit_function_template_parm_p
;
20875 parser
->auto_is_implicit_function_template_parm_p
20876 = auto_is_implicit_function_template_parm_p
;
20878 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
20882 if (!processing_specialization
20883 && !processing_template_parmlist
20884 && !processing_explicit_instantiation
)
20885 if (!current_function_decl
20886 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
20887 parser
->auto_is_implicit_function_template_parm_p
= true;
20889 /* Peek at the next token. */
20890 token
= cp_lexer_peek_token (parser
->lexer
);
20891 /* Check for trivial parameter-declaration-clauses. */
20892 if (token
->type
== CPP_ELLIPSIS
)
20894 /* Consume the `...' token. */
20895 cp_lexer_consume_token (parser
->lexer
);
20898 else if (token
->type
== CPP_CLOSE_PAREN
)
20899 /* There are no parameters. */
20901 #ifndef NO_IMPLICIT_EXTERN_C
20902 if (in_system_header_at (input_location
)
20903 && current_class_type
== NULL
20904 && current_lang_name
== lang_name_c
)
20908 return void_list_node
;
20910 /* Check for `(void)', too, which is a special case. */
20911 else if (token
->keyword
== RID_VOID
20912 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20913 == CPP_CLOSE_PAREN
))
20915 /* Consume the `void' token. */
20916 cp_lexer_consume_token (parser
->lexer
);
20917 /* There are no parameters. */
20918 return void_list_node
;
20921 /* Parse the parameter-declaration-list. */
20922 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
20923 /* If a parse error occurred while parsing the
20924 parameter-declaration-list, then the entire
20925 parameter-declaration-clause is erroneous. */
20929 /* Peek at the next token. */
20930 token
= cp_lexer_peek_token (parser
->lexer
);
20931 /* If it's a `,', the clause should terminate with an ellipsis. */
20932 if (token
->type
== CPP_COMMA
)
20934 /* Consume the `,'. */
20935 cp_lexer_consume_token (parser
->lexer
);
20936 /* Expect an ellipsis. */
20938 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
20940 /* It might also be `...' if the optional trailing `,' was
20942 else if (token
->type
== CPP_ELLIPSIS
)
20944 /* Consume the `...' token. */
20945 cp_lexer_consume_token (parser
->lexer
);
20946 /* And remember that we saw it. */
20950 ellipsis_p
= false;
20952 /* Finish the parameter list. */
20954 parameters
= chainon (parameters
, void_list_node
);
20959 /* Parse a parameter-declaration-list.
20961 parameter-declaration-list:
20962 parameter-declaration
20963 parameter-declaration-list , parameter-declaration
20965 Returns a representation of the parameter-declaration-list, as for
20966 cp_parser_parameter_declaration_clause. However, the
20967 `void_list_node' is never appended to the list. Upon return,
20968 *IS_ERROR will be true iff an error occurred. */
20971 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
20973 tree parameters
= NULL_TREE
;
20974 tree
*tail
= ¶meters
;
20975 bool saved_in_unbraced_linkage_specification_p
;
20978 /* Assume all will go well. */
20980 /* The special considerations that apply to a function within an
20981 unbraced linkage specifications do not apply to the parameters
20982 to the function. */
20983 saved_in_unbraced_linkage_specification_p
20984 = parser
->in_unbraced_linkage_specification_p
;
20985 parser
->in_unbraced_linkage_specification_p
= false;
20987 /* Look for more parameters. */
20990 cp_parameter_declarator
*parameter
;
20991 tree decl
= error_mark_node
;
20992 bool parenthesized_p
= false;
20993 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
20994 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20995 (current_template_parms
)) : 0);
20997 /* Parse the parameter. */
20999 = cp_parser_parameter_declaration (parser
,
21000 /*template_parm_p=*/false,
21003 /* We don't know yet if the enclosing context is deprecated, so wait
21004 and warn in grokparms if appropriate. */
21005 deprecated_state
= DEPRECATED_SUPPRESS
;
21009 /* If a function parameter pack was specified and an implicit template
21010 parameter was introduced during cp_parser_parameter_declaration,
21011 change any implicit parameters introduced into packs. */
21012 if (parser
->implicit_template_parms
21013 && parameter
->declarator
21014 && parameter
->declarator
->parameter_pack_p
)
21016 int latest_template_parm_idx
= TREE_VEC_LENGTH
21017 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21019 if (latest_template_parm_idx
!= template_parm_idx
)
21020 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21021 (parameter
->decl_specifiers
.type
,
21022 template_parm_idx
, latest_template_parm_idx
);
21025 decl
= grokdeclarator (parameter
->declarator
,
21026 ¶meter
->decl_specifiers
,
21028 parameter
->default_argument
!= NULL_TREE
,
21029 ¶meter
->decl_specifiers
.attributes
);
21032 deprecated_state
= DEPRECATED_NORMAL
;
21034 /* If a parse error occurred parsing the parameter declaration,
21035 then the entire parameter-declaration-list is erroneous. */
21036 if (decl
== error_mark_node
)
21039 parameters
= error_mark_node
;
21043 if (parameter
->decl_specifiers
.attributes
)
21044 cplus_decl_attributes (&decl
,
21045 parameter
->decl_specifiers
.attributes
,
21047 if (DECL_NAME (decl
))
21048 decl
= pushdecl (decl
);
21050 if (decl
!= error_mark_node
)
21052 retrofit_lang_decl (decl
);
21053 DECL_PARM_INDEX (decl
) = ++index
;
21054 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21057 /* Add the new parameter to the list. */
21058 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21059 tail
= &TREE_CHAIN (*tail
);
21061 /* Peek at the next token. */
21062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21063 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21064 /* These are for Objective-C++ */
21065 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21066 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21067 /* The parameter-declaration-list is complete. */
21069 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21073 /* Peek at the next token. */
21074 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21075 /* If it's an ellipsis, then the list is complete. */
21076 if (token
->type
== CPP_ELLIPSIS
)
21078 /* Otherwise, there must be more parameters. Consume the
21080 cp_lexer_consume_token (parser
->lexer
);
21081 /* When parsing something like:
21083 int i(float f, double d)
21085 we can tell after seeing the declaration for "f" that we
21086 are not looking at an initialization of a variable "i",
21087 but rather at the declaration of a function "i".
21089 Due to the fact that the parsing of template arguments
21090 (as specified to a template-id) requires backtracking we
21091 cannot use this technique when inside a template argument
21093 if (!parser
->in_template_argument_list_p
21094 && !parser
->in_type_id_in_expr_p
21095 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21096 /* However, a parameter-declaration of the form
21097 "float(f)" (which is a valid declaration of a
21098 parameter "f") can also be interpreted as an
21099 expression (the conversion of "f" to "float"). */
21100 && !parenthesized_p
)
21101 cp_parser_commit_to_tentative_parse (parser
);
21105 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21106 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21107 cp_parser_skip_to_closing_parenthesis (parser
,
21108 /*recovering=*/true,
21109 /*or_comma=*/false,
21110 /*consume_paren=*/false);
21115 parser
->in_unbraced_linkage_specification_p
21116 = saved_in_unbraced_linkage_specification_p
;
21118 /* Reset implicit_template_scope if we are about to leave the function
21119 parameter list that introduced it. Note that for out-of-line member
21120 definitions, there will be one or more class scopes before we get to
21121 the template parameter scope. */
21123 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21124 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21126 while (maybe_its
->kind
== sk_class
)
21127 maybe_its
= maybe_its
->level_chain
;
21128 if (maybe_its
== its
)
21130 parser
->implicit_template_parms
= 0;
21131 parser
->implicit_template_scope
= 0;
21138 /* Parse a parameter declaration.
21140 parameter-declaration:
21141 decl-specifier-seq ... [opt] declarator
21142 decl-specifier-seq declarator = assignment-expression
21143 decl-specifier-seq ... [opt] abstract-declarator [opt]
21144 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21146 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21147 declares a template parameter. (In that case, a non-nested `>'
21148 token encountered during the parsing of the assignment-expression
21149 is not interpreted as a greater-than operator.)
21151 Returns a representation of the parameter, or NULL if an error
21152 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21153 true iff the declarator is of the form "(p)". */
21155 static cp_parameter_declarator
*
21156 cp_parser_parameter_declaration (cp_parser
*parser
,
21157 bool template_parm_p
,
21158 bool *parenthesized_p
)
21160 int declares_class_or_enum
;
21161 cp_decl_specifier_seq decl_specifiers
;
21162 cp_declarator
*declarator
;
21163 tree default_argument
;
21164 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21165 const char *saved_message
;
21166 bool template_parameter_pack_p
= false;
21168 /* In a template parameter, `>' is not an operator.
21172 When parsing a default template-argument for a non-type
21173 template-parameter, the first non-nested `>' is taken as the end
21174 of the template parameter-list rather than a greater-than
21177 /* Type definitions may not appear in parameter types. */
21178 saved_message
= parser
->type_definition_forbidden_message
;
21179 parser
->type_definition_forbidden_message
21180 = G_("types may not be defined in parameter types");
21182 /* Parse the declaration-specifiers. */
21183 cp_parser_decl_specifier_seq (parser
,
21184 CP_PARSER_FLAGS_NONE
,
21186 &declares_class_or_enum
);
21188 /* Complain about missing 'typename' or other invalid type names. */
21189 if (!decl_specifiers
.any_type_specifiers_p
21190 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21191 decl_specifiers
.type
= error_mark_node
;
21193 /* If an error occurred, there's no reason to attempt to parse the
21194 rest of the declaration. */
21195 if (cp_parser_error_occurred (parser
))
21197 parser
->type_definition_forbidden_message
= saved_message
;
21201 /* Peek at the next token. */
21202 token
= cp_lexer_peek_token (parser
->lexer
);
21204 /* If the next token is a `)', `,', `=', `>', or `...', then there
21205 is no declarator. However, when variadic templates are enabled,
21206 there may be a declarator following `...'. */
21207 if (token
->type
== CPP_CLOSE_PAREN
21208 || token
->type
== CPP_COMMA
21209 || token
->type
== CPP_EQ
21210 || token
->type
== CPP_GREATER
)
21213 if (parenthesized_p
)
21214 *parenthesized_p
= false;
21216 /* Otherwise, there should be a declarator. */
21219 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21220 parser
->default_arg_ok_p
= false;
21222 /* After seeing a decl-specifier-seq, if the next token is not a
21223 "(", there is no possibility that the code is a valid
21224 expression. Therefore, if parsing tentatively, we commit at
21226 if (!parser
->in_template_argument_list_p
21227 /* In an expression context, having seen:
21231 we cannot be sure whether we are looking at a
21232 function-type (taking a "char" as a parameter) or a cast
21233 of some object of type "char" to "int". */
21234 && !parser
->in_type_id_in_expr_p
21235 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21236 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21237 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21238 cp_parser_commit_to_tentative_parse (parser
);
21239 /* Parse the declarator. */
21240 declarator_token_start
= token
;
21241 declarator
= cp_parser_declarator (parser
,
21242 CP_PARSER_DECLARATOR_EITHER
,
21243 /*ctor_dtor_or_conv_p=*/NULL
,
21245 /*member_p=*/false,
21246 /*friend_p=*/false);
21247 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21248 /* After the declarator, allow more attributes. */
21249 decl_specifiers
.attributes
21250 = chainon (decl_specifiers
.attributes
,
21251 cp_parser_attributes_opt (parser
));
21253 /* If the declarator is a template parameter pack, remember that and
21254 clear the flag in the declarator itself so we don't get errors
21255 from grokdeclarator. */
21256 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21258 declarator
->parameter_pack_p
= false;
21259 template_parameter_pack_p
= true;
21263 /* If the next token is an ellipsis, and we have not seen a declarator
21264 name, and if either the type of the declarator contains parameter
21265 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21266 for, eg, abbreviated integral type names), then we actually have a
21267 parameter pack expansion expression. Otherwise, leave the ellipsis
21268 for a C-style variadic function. */
21269 token
= cp_lexer_peek_token (parser
->lexer
);
21270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21272 tree type
= decl_specifiers
.type
;
21274 if (type
&& DECL_P (type
))
21275 type
= TREE_TYPE (type
);
21278 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21279 && (template_parm_p
|| uses_parameter_packs (type
)))
21280 || (!type
&& template_parm_p
))
21281 && declarator_can_be_parameter_pack (declarator
))
21283 /* Consume the `...'. */
21284 cp_lexer_consume_token (parser
->lexer
);
21285 maybe_warn_variadic_templates ();
21287 /* Build a pack expansion type */
21288 if (template_parm_p
)
21289 template_parameter_pack_p
= true;
21290 else if (declarator
)
21291 declarator
->parameter_pack_p
= true;
21293 decl_specifiers
.type
= make_pack_expansion (type
);
21297 /* The restriction on defining new types applies only to the type
21298 of the parameter, not to the default argument. */
21299 parser
->type_definition_forbidden_message
= saved_message
;
21301 /* If the next token is `=', then process a default argument. */
21302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21304 tree type
= decl_specifiers
.type
;
21305 token
= cp_lexer_peek_token (parser
->lexer
);
21306 /* If we are defining a class, then the tokens that make up the
21307 default argument must be saved and processed later. */
21308 if (!template_parm_p
&& at_class_scope_p ()
21309 && TYPE_BEING_DEFINED (current_class_type
)
21310 && !LAMBDA_TYPE_P (current_class_type
))
21311 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21313 // A constrained-type-specifier may declare a type template-parameter.
21314 else if (declares_constrained_type_template_parameter (type
))
21316 = cp_parser_default_type_template_argument (parser
);
21318 // A constrained-type-specifier may declare a template-template-parameter.
21319 else if (declares_constrained_template_template_parameter (type
))
21321 = cp_parser_default_template_template_argument (parser
);
21323 /* Outside of a class definition, we can just parse the
21324 assignment-expression. */
21327 = cp_parser_default_argument (parser
, template_parm_p
);
21329 if (!parser
->default_arg_ok_p
)
21331 permerror (token
->location
,
21332 "default arguments are only "
21333 "permitted for function parameters");
21335 else if ((declarator
&& declarator
->parameter_pack_p
)
21336 || template_parameter_pack_p
21337 || (decl_specifiers
.type
21338 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21340 /* Find the name of the parameter pack. */
21341 cp_declarator
*id_declarator
= declarator
;
21342 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21343 id_declarator
= id_declarator
->declarator
;
21345 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21346 error_at (declarator_token_start
->location
,
21348 ? G_("template parameter pack %qD "
21349 "cannot have a default argument")
21350 : G_("parameter pack %qD cannot have "
21351 "a default argument"),
21352 id_declarator
->u
.id
.unqualified_name
);
21354 error_at (declarator_token_start
->location
,
21356 ? G_("template parameter pack cannot have "
21357 "a default argument")
21358 : G_("parameter pack cannot have a "
21359 "default argument"));
21361 default_argument
= NULL_TREE
;
21365 default_argument
= NULL_TREE
;
21367 return make_parameter_declarator (&decl_specifiers
,
21370 template_parameter_pack_p
);
21373 /* Parse a default argument and return it.
21375 TEMPLATE_PARM_P is true if this is a default argument for a
21376 non-type template parameter. */
21378 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21380 tree default_argument
= NULL_TREE
;
21381 bool saved_greater_than_is_operator_p
;
21382 bool saved_local_variables_forbidden_p
;
21383 bool non_constant_p
, is_direct_init
;
21385 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21387 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21388 parser
->greater_than_is_operator_p
= !template_parm_p
;
21389 /* Local variable names (and the `this' keyword) may not
21390 appear in a default argument. */
21391 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21392 parser
->local_variables_forbidden_p
= true;
21393 /* Parse the assignment-expression. */
21394 if (template_parm_p
)
21395 push_deferring_access_checks (dk_no_deferred
);
21396 tree saved_class_ptr
= NULL_TREE
;
21397 tree saved_class_ref
= NULL_TREE
;
21398 /* The "this" pointer is not valid in a default argument. */
21401 saved_class_ptr
= current_class_ptr
;
21402 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21403 saved_class_ref
= current_class_ref
;
21404 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21407 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21408 /* Restore the "this" pointer. */
21411 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21412 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21414 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21415 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21416 if (template_parm_p
)
21417 pop_deferring_access_checks ();
21418 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21419 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21421 return default_argument
;
21424 /* Parse a function-body.
21427 compound_statement */
21430 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21432 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21433 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21437 /* Parse a ctor-initializer-opt followed by a function-body. Return
21438 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21439 is true we are parsing a function-try-block. */
21442 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21443 bool in_function_try_block
)
21446 bool ctor_initializer_p
;
21447 const bool check_body_p
=
21448 DECL_CONSTRUCTOR_P (current_function_decl
)
21449 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21452 /* Begin the function body. */
21453 body
= begin_function_body ();
21454 /* Parse the optional ctor-initializer. */
21455 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
21457 /* If we're parsing a constexpr constructor definition, we need
21458 to check that the constructor body is indeed empty. However,
21459 before we get to cp_parser_function_body lot of junk has been
21460 generated, so we can't just check that we have an empty block.
21461 Rather we take a snapshot of the outermost block, and check whether
21462 cp_parser_function_body changed its state. */
21465 list
= cur_stmt_list
;
21466 if (STATEMENT_LIST_TAIL (list
))
21467 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21469 /* Parse the function-body. */
21470 cp_parser_function_body (parser
, in_function_try_block
);
21472 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21473 /* Finish the function body. */
21474 finish_function_body (body
);
21476 return ctor_initializer_p
;
21479 /* Parse an initializer.
21482 = initializer-clause
21483 ( expression-list )
21485 Returns an expression representing the initializer. If no
21486 initializer is present, NULL_TREE is returned.
21488 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21489 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21490 set to TRUE if there is no initializer present. If there is an
21491 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21492 is set to true; otherwise it is set to false. */
21495 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21496 bool* non_constant_p
)
21501 /* Peek at the next token. */
21502 token
= cp_lexer_peek_token (parser
->lexer
);
21504 /* Let our caller know whether or not this initializer was
21506 *is_direct_init
= (token
->type
!= CPP_EQ
);
21507 /* Assume that the initializer is constant. */
21508 *non_constant_p
= false;
21510 if (token
->type
== CPP_EQ
)
21512 /* Consume the `='. */
21513 cp_lexer_consume_token (parser
->lexer
);
21514 /* Parse the initializer-clause. */
21515 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21517 else if (token
->type
== CPP_OPEN_PAREN
)
21519 vec
<tree
, va_gc
> *vec
;
21520 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21522 /*allow_expansion_p=*/true,
21525 return error_mark_node
;
21526 init
= build_tree_list_vec (vec
);
21527 release_tree_vector (vec
);
21529 else if (token
->type
== CPP_OPEN_BRACE
)
21531 cp_lexer_set_source_position (parser
->lexer
);
21532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21533 init
= cp_parser_braced_list (parser
, non_constant_p
);
21534 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21538 /* Anything else is an error. */
21539 cp_parser_error (parser
, "expected initializer");
21540 init
= error_mark_node
;
21543 if (check_for_bare_parameter_packs (init
))
21544 init
= error_mark_node
;
21549 /* Parse an initializer-clause.
21551 initializer-clause:
21552 assignment-expression
21555 Returns an expression representing the initializer.
21557 If the `assignment-expression' production is used the value
21558 returned is simply a representation for the expression.
21560 Otherwise, calls cp_parser_braced_list. */
21563 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21565 cp_expr initializer
;
21567 /* Assume the expression is constant. */
21568 *non_constant_p
= false;
21570 /* If it is not a `{', then we are looking at an
21571 assignment-expression. */
21572 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21575 = cp_parser_constant_expression (parser
,
21576 /*allow_non_constant_p=*/true,
21580 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21582 return initializer
;
21585 /* Parse a brace-enclosed initializer list.
21588 { initializer-list , [opt] }
21591 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21592 the elements of the initializer-list (or NULL, if the last
21593 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21594 NULL_TREE. There is no way to detect whether or not the optional
21595 trailing `,' was provided. NON_CONSTANT_P is as for
21596 cp_parser_initializer. */
21599 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21602 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21604 /* Consume the `{' token. */
21605 cp_lexer_consume_token (parser
->lexer
);
21606 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21607 initializer
= make_node (CONSTRUCTOR
);
21608 /* If it's not a `}', then there is a non-trivial initializer. */
21609 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21611 /* Parse the initializer list. */
21612 CONSTRUCTOR_ELTS (initializer
)
21613 = cp_parser_initializer_list (parser
, non_constant_p
);
21614 /* A trailing `,' token is allowed. */
21615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21616 cp_lexer_consume_token (parser
->lexer
);
21619 *non_constant_p
= false;
21620 /* Now, there should be a trailing `}'. */
21621 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21622 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21623 TREE_TYPE (initializer
) = init_list_type_node
;
21625 cp_expr
result (initializer
);
21626 /* Build a location of the form:
21629 with caret==start at the open brace, finish at the close brace. */
21630 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21631 result
.set_location (combined_loc
);
21635 /* Consume tokens up to, and including, the next non-nested closing `]'.
21636 Returns true iff we found a closing `]'. */
21639 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21641 unsigned square_depth
= 0;
21645 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21647 switch (token
->type
)
21650 case CPP_PRAGMA_EOL
:
21651 /* If we've run out of tokens, then there is no closing `]'. */
21654 case CPP_OPEN_SQUARE
:
21658 case CPP_CLOSE_SQUARE
:
21659 if (!square_depth
--)
21661 cp_lexer_consume_token (parser
->lexer
);
21670 /* Consume the token. */
21671 cp_lexer_consume_token (parser
->lexer
);
21675 /* Return true if we are looking at an array-designator, false otherwise. */
21678 cp_parser_array_designator_p (cp_parser
*parser
)
21680 /* Consume the `['. */
21681 cp_lexer_consume_token (parser
->lexer
);
21683 cp_lexer_save_tokens (parser
->lexer
);
21685 /* Skip tokens until the next token is a closing square bracket.
21686 If we find the closing `]', and the next token is a `=', then
21687 we are looking at an array designator. */
21688 bool array_designator_p
21689 = (cp_parser_skip_to_closing_square_bracket (parser
)
21690 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21692 /* Roll back the tokens we skipped. */
21693 cp_lexer_rollback_tokens (parser
->lexer
);
21695 return array_designator_p
;
21698 /* Parse an initializer-list.
21701 initializer-clause ... [opt]
21702 initializer-list , initializer-clause ... [opt]
21707 designation initializer-clause ...[opt]
21708 initializer-list , designation initializer-clause ...[opt]
21713 [ constant-expression ] =
21715 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21716 for the initializer. If the INDEX of the elt is non-NULL, it is the
21717 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21718 as for cp_parser_initializer. */
21720 static vec
<constructor_elt
, va_gc
> *
21721 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
21723 vec
<constructor_elt
, va_gc
> *v
= NULL
;
21725 /* Assume all of the expressions are constant. */
21726 *non_constant_p
= false;
21728 /* Parse the rest of the list. */
21734 bool clause_non_constant_p
;
21736 /* If the next token is an identifier and the following one is a
21737 colon, we are looking at the GNU designated-initializer
21739 if (cp_parser_allow_gnu_extensions_p (parser
)
21740 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
21741 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
21743 /* Warn the user that they are using an extension. */
21744 pedwarn (input_location
, OPT_Wpedantic
,
21745 "ISO C++ does not allow designated initializers");
21746 /* Consume the identifier. */
21747 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21748 /* Consume the `:'. */
21749 cp_lexer_consume_token (parser
->lexer
);
21751 /* Also handle the C99 syntax, '. id ='. */
21752 else if (cp_parser_allow_gnu_extensions_p (parser
)
21753 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
21754 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
21755 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
21757 /* Warn the user that they are using an extension. */
21758 pedwarn (input_location
, OPT_Wpedantic
,
21759 "ISO C++ does not allow C99 designated initializers");
21760 /* Consume the `.'. */
21761 cp_lexer_consume_token (parser
->lexer
);
21762 /* Consume the identifier. */
21763 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21764 /* Consume the `='. */
21765 cp_lexer_consume_token (parser
->lexer
);
21767 /* Also handle C99 array designators, '[ const ] ='. */
21768 else if (cp_parser_allow_gnu_extensions_p (parser
)
21769 && !c_dialect_objc ()
21770 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21772 /* In C++11, [ could start a lambda-introducer. */
21773 bool non_const
= false;
21775 cp_parser_parse_tentatively (parser
);
21777 if (!cp_parser_array_designator_p (parser
))
21779 cp_parser_simulate_error (parser
);
21780 designator
= NULL_TREE
;
21784 designator
= cp_parser_constant_expression (parser
, true,
21786 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21787 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21790 if (!cp_parser_parse_definitely (parser
))
21791 designator
= NULL_TREE
;
21792 else if (non_const
)
21793 require_potential_rvalue_constant_expression (designator
);
21796 designator
= NULL_TREE
;
21798 /* Parse the initializer. */
21799 initializer
= cp_parser_initializer_clause (parser
,
21800 &clause_non_constant_p
);
21801 /* If any clause is non-constant, so is the entire initializer. */
21802 if (clause_non_constant_p
)
21803 *non_constant_p
= true;
21805 /* If we have an ellipsis, this is an initializer pack
21807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21809 /* Consume the `...'. */
21810 cp_lexer_consume_token (parser
->lexer
);
21812 /* Turn the initializer into an initializer expansion. */
21813 initializer
= make_pack_expansion (initializer
);
21816 /* Add it to the vector. */
21817 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
21819 /* If the next token is not a comma, we have reached the end of
21821 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21824 /* Peek at the next token. */
21825 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21826 /* If the next token is a `}', then we're still done. An
21827 initializer-clause can have a trailing `,' after the
21828 initializer-list and before the closing `}'. */
21829 if (token
->type
== CPP_CLOSE_BRACE
)
21832 /* Consume the `,' token. */
21833 cp_lexer_consume_token (parser
->lexer
);
21839 /* Classes [gram.class] */
21841 /* Parse a class-name.
21847 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21848 to indicate that names looked up in dependent types should be
21849 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21850 keyword has been used to indicate that the name that appears next
21851 is a template. TAG_TYPE indicates the explicit tag given before
21852 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21853 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21854 is the class being defined in a class-head. If ENUM_OK is TRUE,
21855 enum-names are also accepted.
21857 Returns the TYPE_DECL representing the class. */
21860 cp_parser_class_name (cp_parser
*parser
,
21861 bool typename_keyword_p
,
21862 bool template_keyword_p
,
21863 enum tag_types tag_type
,
21864 bool check_dependency_p
,
21866 bool is_declaration
,
21873 tree identifier
= NULL_TREE
;
21875 /* All class-names start with an identifier. */
21876 token
= cp_lexer_peek_token (parser
->lexer
);
21877 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
21879 cp_parser_error (parser
, "expected class-name");
21880 return error_mark_node
;
21883 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21884 to a template-id, so we save it here. */
21885 scope
= parser
->scope
;
21886 if (scope
== error_mark_node
)
21887 return error_mark_node
;
21889 /* Any name names a type if we're following the `typename' keyword
21890 in a qualified name where the enclosing scope is type-dependent. */
21891 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
21892 && dependent_type_p (scope
));
21893 /* Handle the common case (an identifier, but not a template-id)
21895 if (token
->type
== CPP_NAME
21896 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
21898 cp_token
*identifier_token
;
21901 /* Look for the identifier. */
21902 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
21903 ambiguous_p
= identifier_token
->error_reported
;
21904 identifier
= cp_parser_identifier (parser
);
21905 /* If the next token isn't an identifier, we are certainly not
21906 looking at a class-name. */
21907 if (identifier
== error_mark_node
)
21908 decl
= error_mark_node
;
21909 /* If we know this is a type-name, there's no need to look it
21911 else if (typename_p
)
21915 tree ambiguous_decls
;
21916 /* If we already know that this lookup is ambiguous, then
21917 we've already issued an error message; there's no reason
21921 cp_parser_simulate_error (parser
);
21922 return error_mark_node
;
21924 /* If the next token is a `::', then the name must be a type
21927 [basic.lookup.qual]
21929 During the lookup for a name preceding the :: scope
21930 resolution operator, object, function, and enumerator
21931 names are ignored. */
21932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21933 tag_type
= scope_type
;
21934 /* Look up the name. */
21935 decl
= cp_parser_lookup_name (parser
, identifier
,
21937 /*is_template=*/false,
21938 /*is_namespace=*/false,
21939 check_dependency_p
,
21941 identifier_token
->location
);
21942 if (ambiguous_decls
)
21944 if (cp_parser_parsing_tentatively (parser
))
21945 cp_parser_simulate_error (parser
);
21946 return error_mark_node
;
21952 /* Try a template-id. */
21953 decl
= cp_parser_template_id (parser
, template_keyword_p
,
21954 check_dependency_p
,
21957 if (decl
== error_mark_node
)
21958 return error_mark_node
;
21961 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
21963 /* If this is a typename, create a TYPENAME_TYPE. */
21964 if (typename_p
&& decl
!= error_mark_node
)
21966 decl
= make_typename_type (scope
, decl
, typename_type
,
21967 /*complain=*/tf_error
);
21968 if (decl
!= error_mark_node
)
21969 decl
= TYPE_NAME (decl
);
21972 decl
= strip_using_decl (decl
);
21974 /* Check to see that it is really the name of a class. */
21975 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
21976 && identifier_p (TREE_OPERAND (decl
, 0))
21977 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21978 /* Situations like this:
21980 template <typename T> struct A {
21981 typename T::template X<int>::I i;
21984 are problematic. Is `T::template X<int>' a class-name? The
21985 standard does not seem to be definitive, but there is no other
21986 valid interpretation of the following `::'. Therefore, those
21987 names are considered class-names. */
21989 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
21990 if (decl
!= error_mark_node
)
21991 decl
= TYPE_NAME (decl
);
21993 else if (TREE_CODE (decl
) != TYPE_DECL
21994 || TREE_TYPE (decl
) == error_mark_node
21995 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
21996 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
21997 /* In Objective-C 2.0, a classname followed by '.' starts a
21998 dot-syntax expression, and it's not a type-name. */
21999 || (c_dialect_objc ()
22000 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22001 && objc_is_class_name (decl
)))
22002 decl
= error_mark_node
;
22004 if (decl
== error_mark_node
)
22005 cp_parser_error (parser
, "expected class-name");
22006 else if (identifier
&& !parser
->scope
)
22007 maybe_note_name_used_in_class (identifier
, decl
);
22012 /* Parse a class-specifier.
22015 class-head { member-specification [opt] }
22017 Returns the TREE_TYPE representing the class. */
22020 cp_parser_class_specifier_1 (cp_parser
* parser
)
22023 tree attributes
= NULL_TREE
;
22024 bool nested_name_specifier_p
;
22025 unsigned saved_num_template_parameter_lists
;
22026 bool saved_in_function_body
;
22027 unsigned char in_statement
;
22028 bool in_switch_statement_p
;
22029 bool saved_in_unbraced_linkage_specification_p
;
22030 tree old_scope
= NULL_TREE
;
22031 tree scope
= NULL_TREE
;
22032 cp_token
*closing_brace
;
22034 push_deferring_access_checks (dk_no_deferred
);
22036 /* Parse the class-head. */
22037 type
= cp_parser_class_head (parser
,
22038 &nested_name_specifier_p
);
22039 /* If the class-head was a semantic disaster, skip the entire body
22043 cp_parser_skip_to_end_of_block_or_statement (parser
);
22044 pop_deferring_access_checks ();
22045 return error_mark_node
;
22048 /* Look for the `{'. */
22049 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
22051 pop_deferring_access_checks ();
22052 return error_mark_node
;
22055 cp_ensure_no_omp_declare_simd (parser
);
22056 cp_ensure_no_oacc_routine (parser
);
22058 /* Issue an error message if type-definitions are forbidden here. */
22059 cp_parser_check_type_definition (parser
);
22060 /* Remember that we are defining one more class. */
22061 ++parser
->num_classes_being_defined
;
22062 /* Inside the class, surrounding template-parameter-lists do not
22064 saved_num_template_parameter_lists
22065 = parser
->num_template_parameter_lists
;
22066 parser
->num_template_parameter_lists
= 0;
22067 /* We are not in a function body. */
22068 saved_in_function_body
= parser
->in_function_body
;
22069 parser
->in_function_body
= false;
22070 /* Or in a loop. */
22071 in_statement
= parser
->in_statement
;
22072 parser
->in_statement
= 0;
22073 /* Or in a switch. */
22074 in_switch_statement_p
= parser
->in_switch_statement_p
;
22075 parser
->in_switch_statement_p
= false;
22076 /* We are not immediately inside an extern "lang" block. */
22077 saved_in_unbraced_linkage_specification_p
22078 = parser
->in_unbraced_linkage_specification_p
;
22079 parser
->in_unbraced_linkage_specification_p
= false;
22081 // Associate constraints with the type.
22083 type
= associate_classtype_constraints (type
);
22085 /* Start the class. */
22086 if (nested_name_specifier_p
)
22088 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22089 old_scope
= push_inner_scope (scope
);
22091 type
= begin_class_definition (type
);
22093 if (type
== error_mark_node
)
22094 /* If the type is erroneous, skip the entire body of the class. */
22095 cp_parser_skip_to_closing_brace (parser
);
22097 /* Parse the member-specification. */
22098 cp_parser_member_specification_opt (parser
);
22100 /* Look for the trailing `}'. */
22101 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22102 /* Look for trailing attributes to apply to this class. */
22103 if (cp_parser_allow_gnu_extensions_p (parser
))
22104 attributes
= cp_parser_gnu_attributes_opt (parser
);
22105 if (type
!= error_mark_node
)
22106 type
= finish_struct (type
, attributes
);
22107 if (nested_name_specifier_p
)
22108 pop_inner_scope (old_scope
, scope
);
22110 /* We've finished a type definition. Check for the common syntax
22111 error of forgetting a semicolon after the definition. We need to
22112 be careful, as we can't just check for not-a-semicolon and be done
22113 with it; the user might have typed:
22115 class X { } c = ...;
22116 class X { } *p = ...;
22118 and so forth. Instead, enumerate all the possible tokens that
22119 might follow this production; if we don't see one of them, then
22120 complain and silently insert the semicolon. */
22122 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22123 bool want_semicolon
= true;
22125 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22126 /* Don't try to parse c++11 attributes here. As per the
22127 grammar, that should be a task for
22128 cp_parser_decl_specifier_seq. */
22129 want_semicolon
= false;
22131 switch (token
->type
)
22134 case CPP_SEMICOLON
:
22137 case CPP_OPEN_PAREN
:
22138 case CPP_CLOSE_PAREN
:
22140 want_semicolon
= false;
22143 /* While it's legal for type qualifiers and storage class
22144 specifiers to follow type definitions in the grammar, only
22145 compiler testsuites contain code like that. Assume that if
22146 we see such code, then what we're really seeing is a case
22150 const <type> var = ...;
22155 static <type> func (...) ...
22157 i.e. the qualifier or specifier applies to the next
22158 declaration. To do so, however, we need to look ahead one
22159 more token to see if *that* token is a type specifier.
22161 This code could be improved to handle:
22164 static const <type> var = ...; */
22166 if (keyword_is_decl_specifier (token
->keyword
))
22168 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22170 /* Handling user-defined types here would be nice, but very
22173 = (lookahead
->type
== CPP_KEYWORD
22174 && keyword_begins_type_specifier (lookahead
->keyword
));
22181 /* If we don't have a type, then something is very wrong and we
22182 shouldn't try to do anything clever. Likewise for not seeing the
22184 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22186 /* Locate the closing brace. */
22187 cp_token_position prev
22188 = cp_lexer_previous_token_position (parser
->lexer
);
22189 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22190 location_t loc
= prev_token
->location
;
22192 /* We want to suggest insertion of a ';' immediately *after* the
22193 closing brace, so, if we can, offset the location by 1 column. */
22194 location_t next_loc
= loc
;
22195 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22196 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22198 rich_location
richloc (line_table
, next_loc
);
22200 /* If we successfully offset the location, suggest the fix-it. */
22201 if (next_loc
!= loc
)
22202 richloc
.add_fixit_insert_before (next_loc
, ";");
22204 if (CLASSTYPE_DECLARED_CLASS (type
))
22205 error_at_rich_loc (&richloc
,
22206 "expected %<;%> after class definition");
22207 else if (TREE_CODE (type
) == RECORD_TYPE
)
22208 error_at_rich_loc (&richloc
,
22209 "expected %<;%> after struct definition");
22210 else if (TREE_CODE (type
) == UNION_TYPE
)
22211 error_at_rich_loc (&richloc
,
22212 "expected %<;%> after union definition");
22214 gcc_unreachable ();
22216 /* Unget one token and smash it to look as though we encountered
22217 a semicolon in the input stream. */
22218 cp_lexer_set_token_position (parser
->lexer
, prev
);
22219 token
= cp_lexer_peek_token (parser
->lexer
);
22220 token
->type
= CPP_SEMICOLON
;
22221 token
->keyword
= RID_MAX
;
22225 /* If this class is not itself within the scope of another class,
22226 then we need to parse the bodies of all of the queued function
22227 definitions. Note that the queued functions defined in a class
22228 are not always processed immediately following the
22229 class-specifier for that class. Consider:
22232 struct B { void f() { sizeof (A); } };
22235 If `f' were processed before the processing of `A' were
22236 completed, there would be no way to compute the size of `A'.
22237 Note that the nesting we are interested in here is lexical --
22238 not the semantic nesting given by TYPE_CONTEXT. In particular,
22241 struct A { struct B; };
22242 struct A::B { void f() { } };
22244 there is no need to delay the parsing of `A::B::f'. */
22245 if (--parser
->num_classes_being_defined
== 0)
22248 tree class_type
= NULL_TREE
;
22249 tree pushed_scope
= NULL_TREE
;
22251 cp_default_arg_entry
*e
;
22252 tree save_ccp
, save_ccr
;
22254 /* In a first pass, parse default arguments to the functions.
22255 Then, in a second pass, parse the bodies of the functions.
22256 This two-phased approach handles cases like:
22264 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22267 /* If there are default arguments that have not yet been processed,
22268 take care of them now. */
22269 if (class_type
!= e
->class_type
)
22272 pop_scope (pushed_scope
);
22273 class_type
= e
->class_type
;
22274 pushed_scope
= push_scope (class_type
);
22276 /* Make sure that any template parameters are in scope. */
22277 maybe_begin_member_template_processing (decl
);
22278 /* Parse the default argument expressions. */
22279 cp_parser_late_parsing_default_args (parser
, decl
);
22280 /* Remove any template parameters from the symbol table. */
22281 maybe_end_member_template_processing ();
22283 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22284 /* Now parse any NSDMIs. */
22285 save_ccp
= current_class_ptr
;
22286 save_ccr
= current_class_ref
;
22287 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22289 if (class_type
!= DECL_CONTEXT (decl
))
22292 pop_scope (pushed_scope
);
22293 class_type
= DECL_CONTEXT (decl
);
22294 pushed_scope
= push_scope (class_type
);
22296 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22297 cp_parser_late_parsing_nsdmi (parser
, decl
);
22299 vec_safe_truncate (unparsed_nsdmis
, 0);
22300 current_class_ptr
= save_ccp
;
22301 current_class_ref
= save_ccr
;
22303 pop_scope (pushed_scope
);
22305 /* Now do some post-NSDMI bookkeeping. */
22306 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22307 after_nsdmi_defaulted_late_checks (class_type
);
22308 vec_safe_truncate (unparsed_classes
, 0);
22309 after_nsdmi_defaulted_late_checks (type
);
22311 /* Now parse the body of the functions. */
22314 /* OpenMP UDRs need to be parsed before all other functions. */
22315 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22316 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22317 cp_parser_late_parsing_for_member (parser
, decl
);
22318 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22319 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22320 cp_parser_late_parsing_for_member (parser
, decl
);
22323 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22324 cp_parser_late_parsing_for_member (parser
, decl
);
22325 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22328 vec_safe_push (unparsed_classes
, type
);
22330 /* Put back any saved access checks. */
22331 pop_deferring_access_checks ();
22333 /* Restore saved state. */
22334 parser
->in_switch_statement_p
= in_switch_statement_p
;
22335 parser
->in_statement
= in_statement
;
22336 parser
->in_function_body
= saved_in_function_body
;
22337 parser
->num_template_parameter_lists
22338 = saved_num_template_parameter_lists
;
22339 parser
->in_unbraced_linkage_specification_p
22340 = saved_in_unbraced_linkage_specification_p
;
22346 cp_parser_class_specifier (cp_parser
* parser
)
22349 timevar_push (TV_PARSE_STRUCT
);
22350 ret
= cp_parser_class_specifier_1 (parser
);
22351 timevar_pop (TV_PARSE_STRUCT
);
22355 /* Parse a class-head.
22358 class-key identifier [opt] base-clause [opt]
22359 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22360 class-key nested-name-specifier [opt] template-id
22363 class-virt-specifier:
22367 class-key attributes identifier [opt] base-clause [opt]
22368 class-key attributes nested-name-specifier identifier base-clause [opt]
22369 class-key attributes nested-name-specifier [opt] template-id
22372 Upon return BASES is initialized to the list of base classes (or
22373 NULL, if there are none) in the same form returned by
22374 cp_parser_base_clause.
22376 Returns the TYPE of the indicated class. Sets
22377 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22378 involving a nested-name-specifier was used, and FALSE otherwise.
22380 Returns error_mark_node if this is not a class-head.
22382 Returns NULL_TREE if the class-head is syntactically valid, but
22383 semantically invalid in a way that means we should skip the entire
22384 body of the class. */
22387 cp_parser_class_head (cp_parser
* parser
,
22388 bool* nested_name_specifier_p
)
22390 tree nested_name_specifier
;
22391 enum tag_types class_key
;
22392 tree id
= NULL_TREE
;
22393 tree type
= NULL_TREE
;
22396 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22397 bool template_id_p
= false;
22398 bool qualified_p
= false;
22399 bool invalid_nested_name_p
= false;
22400 bool invalid_explicit_specialization_p
= false;
22401 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22402 tree pushed_scope
= NULL_TREE
;
22403 unsigned num_templates
;
22404 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22405 /* Assume no nested-name-specifier will be present. */
22406 *nested_name_specifier_p
= false;
22407 /* Assume no template parameter lists will be used in defining the
22410 parser
->colon_corrects_to_scope_p
= false;
22412 /* Look for the class-key. */
22413 class_key
= cp_parser_class_key (parser
);
22414 if (class_key
== none_type
)
22415 return error_mark_node
;
22417 location_t class_head_start_location
= input_location
;
22419 /* Parse the attributes. */
22420 attributes
= cp_parser_attributes_opt (parser
);
22422 /* If the next token is `::', that is invalid -- but sometimes
22423 people do try to write:
22427 Handle this gracefully by accepting the extra qualifier, and then
22428 issuing an error about it later if this really is a
22429 class-head. If it turns out just to be an elaborated type
22430 specifier, remain silent. */
22431 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22432 qualified_p
= true;
22434 push_deferring_access_checks (dk_no_check
);
22436 /* Determine the name of the class. Begin by looking for an
22437 optional nested-name-specifier. */
22438 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22439 nested_name_specifier
22440 = cp_parser_nested_name_specifier_opt (parser
,
22441 /*typename_keyword_p=*/false,
22442 /*check_dependency_p=*/false,
22444 /*is_declaration=*/false);
22445 /* If there was a nested-name-specifier, then there *must* be an
22447 if (nested_name_specifier
)
22449 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22450 /* Although the grammar says `identifier', it really means
22451 `class-name' or `template-name'. You are only allowed to
22452 define a class that has already been declared with this
22455 The proposed resolution for Core Issue 180 says that wherever
22456 you see `class T::X' you should treat `X' as a type-name.
22458 It is OK to define an inaccessible class; for example:
22460 class A { class B; };
22463 We do not know if we will see a class-name, or a
22464 template-name. We look for a class-name first, in case the
22465 class-name is a template-id; if we looked for the
22466 template-name first we would stop after the template-name. */
22467 cp_parser_parse_tentatively (parser
);
22468 type
= cp_parser_class_name (parser
,
22469 /*typename_keyword_p=*/false,
22470 /*template_keyword_p=*/false,
22472 /*check_dependency_p=*/false,
22473 /*class_head_p=*/true,
22474 /*is_declaration=*/false);
22475 /* If that didn't work, ignore the nested-name-specifier. */
22476 if (!cp_parser_parse_definitely (parser
))
22478 invalid_nested_name_p
= true;
22479 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22480 id
= cp_parser_identifier (parser
);
22481 if (id
== error_mark_node
)
22484 /* If we could not find a corresponding TYPE, treat this
22485 declaration like an unqualified declaration. */
22486 if (type
== error_mark_node
)
22487 nested_name_specifier
= NULL_TREE
;
22488 /* Otherwise, count the number of templates used in TYPE and its
22489 containing scopes. */
22494 for (scope
= TREE_TYPE (type
);
22495 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22496 scope
= get_containing_scope (scope
))
22498 && CLASS_TYPE_P (scope
)
22499 && CLASSTYPE_TEMPLATE_INFO (scope
)
22500 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22501 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22502 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22506 /* Otherwise, the identifier is optional. */
22509 /* We don't know whether what comes next is a template-id,
22510 an identifier, or nothing at all. */
22511 cp_parser_parse_tentatively (parser
);
22512 /* Check for a template-id. */
22513 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22514 id
= cp_parser_template_id (parser
,
22515 /*template_keyword_p=*/false,
22516 /*check_dependency_p=*/true,
22518 /*is_declaration=*/true);
22519 /* If that didn't work, it could still be an identifier. */
22520 if (!cp_parser_parse_definitely (parser
))
22522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22524 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22525 id
= cp_parser_identifier (parser
);
22532 template_id_p
= true;
22537 pop_deferring_access_checks ();
22541 cp_parser_check_for_invalid_template_id (parser
, id
,
22543 type_start_token
->location
);
22545 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22547 /* If it's not a `:' or a `{' then we can't really be looking at a
22548 class-head, since a class-head only appears as part of a
22549 class-specifier. We have to detect this situation before calling
22550 xref_tag, since that has irreversible side-effects. */
22551 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22553 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22554 type
= error_mark_node
;
22558 /* At this point, we're going ahead with the class-specifier, even
22559 if some other problem occurs. */
22560 cp_parser_commit_to_tentative_parse (parser
);
22561 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22563 cp_parser_error (parser
,
22564 "cannot specify %<override%> for a class");
22565 type
= error_mark_node
;
22568 /* Issue the error about the overly-qualified name now. */
22571 cp_parser_error (parser
,
22572 "global qualification of class name is invalid");
22573 type
= error_mark_node
;
22576 else if (invalid_nested_name_p
)
22578 cp_parser_error (parser
,
22579 "qualified name does not name a class");
22580 type
= error_mark_node
;
22583 else if (nested_name_specifier
)
22587 /* Reject typedef-names in class heads. */
22588 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22590 error_at (type_start_token
->location
,
22591 "invalid class name in declaration of %qD",
22597 /* Figure out in what scope the declaration is being placed. */
22598 scope
= current_scope ();
22599 /* If that scope does not contain the scope in which the
22600 class was originally declared, the program is invalid. */
22601 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22603 if (at_namespace_scope_p ())
22604 error_at (type_start_token
->location
,
22605 "declaration of %qD in namespace %qD which does not "
22607 type
, scope
, nested_name_specifier
);
22609 error_at (type_start_token
->location
,
22610 "declaration of %qD in %qD which does not enclose %qD",
22611 type
, scope
, nested_name_specifier
);
22617 A declarator-id shall not be qualified except for the
22618 definition of a ... nested class outside of its class
22619 ... [or] the definition or explicit instantiation of a
22620 class member of a namespace outside of its namespace. */
22621 if (scope
== nested_name_specifier
)
22623 permerror (nested_name_specifier_token_start
->location
,
22624 "extra qualification not allowed");
22625 nested_name_specifier
= NULL_TREE
;
22629 /* An explicit-specialization must be preceded by "template <>". If
22630 it is not, try to recover gracefully. */
22631 if (at_namespace_scope_p ()
22632 && parser
->num_template_parameter_lists
== 0
22633 && !processing_template_parmlist
22636 /* Build a location of this form:
22637 struct typename <ARGS>
22638 ^~~~~~~~~~~~~~~~~~~~~~
22639 with caret==start at the start token, and
22640 finishing at the end of the type. */
22641 location_t reported_loc
22642 = make_location (class_head_start_location
,
22643 class_head_start_location
,
22644 get_finish (type_start_token
->location
));
22645 rich_location
richloc (line_table
, reported_loc
);
22646 richloc
.add_fixit_insert_before (class_head_start_location
,
22650 "an explicit specialization must be preceded by %<template <>%>");
22651 invalid_explicit_specialization_p
= true;
22652 /* Take the same action that would have been taken by
22653 cp_parser_explicit_specialization. */
22654 ++parser
->num_template_parameter_lists
;
22655 begin_specialization ();
22657 /* There must be no "return" statements between this point and the
22658 end of this function; set "type "to the correct return value and
22659 use "goto done;" to return. */
22660 /* Make sure that the right number of template parameters were
22662 if (!cp_parser_check_template_parameters (parser
, num_templates
,
22663 type_start_token
->location
,
22664 /*declarator=*/NULL
))
22666 /* If something went wrong, there is no point in even trying to
22667 process the class-definition. */
22672 /* Look up the type. */
22675 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
22676 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
22677 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
22679 error_at (type_start_token
->location
,
22680 "function template %qD redeclared as a class template", id
);
22681 type
= error_mark_node
;
22685 type
= TREE_TYPE (id
);
22686 type
= maybe_process_partial_specialization (type
);
22688 /* Check the scope while we still know whether or not we had a
22689 nested-name-specifier. */
22690 if (type
!= error_mark_node
)
22691 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
22693 if (nested_name_specifier
)
22694 pushed_scope
= push_scope (nested_name_specifier
);
22696 else if (nested_name_specifier
)
22702 template <typename T> struct S { struct T };
22703 template <typename T> struct S<T>::T { };
22705 we will get a TYPENAME_TYPE when processing the definition of
22706 `S::T'. We need to resolve it to the actual type before we
22707 try to define it. */
22708 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
22710 class_type
= resolve_typename_type (TREE_TYPE (type
),
22711 /*only_current_p=*/false);
22712 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
22713 type
= TYPE_NAME (class_type
);
22716 cp_parser_error (parser
, "could not resolve typename type");
22717 type
= error_mark_node
;
22721 if (maybe_process_partial_specialization (TREE_TYPE (type
))
22722 == error_mark_node
)
22728 class_type
= current_class_type
;
22729 /* Enter the scope indicated by the nested-name-specifier. */
22730 pushed_scope
= push_scope (nested_name_specifier
);
22731 /* Get the canonical version of this type. */
22732 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
22733 /* Call push_template_decl if it seems like we should be defining a
22734 template either from the template headers or the type we're
22735 defining, so that we diagnose both extra and missing headers. */
22736 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22737 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
22738 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
22740 type
= push_template_decl (type
);
22741 if (type
== error_mark_node
)
22748 type
= TREE_TYPE (type
);
22749 *nested_name_specifier_p
= true;
22751 else /* The name is not a nested name. */
22753 /* If the class was unnamed, create a dummy name. */
22755 id
= make_anon_name ();
22756 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
22757 ? ts_within_enclosing_non_class
22759 type
= xref_tag (class_key
, id
, tag_scope
,
22760 parser
->num_template_parameter_lists
);
22763 /* Indicate whether this class was declared as a `class' or as a
22765 if (TREE_CODE (type
) == RECORD_TYPE
)
22766 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
22767 cp_parser_check_class_key (class_key
, type
);
22769 /* If this type was already complete, and we see another definition,
22770 that's an error. */
22771 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
22773 error_at (type_start_token
->location
, "redefinition of %q#T",
22775 inform (location_of (type
), "previous definition of %q#T",
22780 else if (type
== error_mark_node
)
22785 /* Apply attributes now, before any use of the class as a template
22786 argument in its base list. */
22787 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
22788 fixup_attribute_variants (type
);
22791 /* We will have entered the scope containing the class; the names of
22792 base classes should be looked up in that context. For example:
22794 struct A { struct B {}; struct C; };
22795 struct A::C : B {};
22799 /* Get the list of base-classes, if there is one. */
22800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22802 /* PR59482: enter the class scope so that base-specifiers are looked
22806 bases
= cp_parser_base_clause (parser
);
22807 /* PR59482: get out of the previously pushed class scope so that the
22808 subsequent pops pop the right thing. */
22815 /* If we're really defining a class, process the base classes.
22816 If they're invalid, fail. */
22817 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22818 xref_basetypes (type
, bases
);
22821 /* Leave the scope given by the nested-name-specifier. We will
22822 enter the class scope itself while processing the members. */
22824 pop_scope (pushed_scope
);
22826 if (invalid_explicit_specialization_p
)
22828 end_specialization ();
22829 --parser
->num_template_parameter_lists
;
22833 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
22834 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
22835 CLASSTYPE_FINAL (type
) = 1;
22837 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22841 /* Parse a class-key.
22848 Returns the kind of class-key specified, or none_type to indicate
22851 static enum tag_types
22852 cp_parser_class_key (cp_parser
* parser
)
22855 enum tag_types tag_type
;
22857 /* Look for the class-key. */
22858 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
22862 /* Check to see if the TOKEN is a class-key. */
22863 tag_type
= cp_parser_token_is_class_key (token
);
22865 cp_parser_error (parser
, "expected class-key");
22869 /* Parse a type-parameter-key.
22871 type-parameter-key:
22877 cp_parser_type_parameter_key (cp_parser
* parser
)
22879 /* Look for the type-parameter-key. */
22880 enum tag_types tag_type
= none_type
;
22881 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22882 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
22884 cp_lexer_consume_token (parser
->lexer
);
22885 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
22886 /* typename is not allowed in a template template parameter
22887 by the standard until C++1Z. */
22888 pedwarn (token
->location
, OPT_Wpedantic
,
22889 "ISO C++ forbids typename key in template template parameter;"
22890 " use -std=c++1z or -std=gnu++1z");
22893 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
22898 /* Parse an (optional) member-specification.
22900 member-specification:
22901 member-declaration member-specification [opt]
22902 access-specifier : member-specification [opt] */
22905 cp_parser_member_specification_opt (cp_parser
* parser
)
22912 /* Peek at the next token. */
22913 token
= cp_lexer_peek_token (parser
->lexer
);
22914 /* If it's a `}', or EOF then we've seen all the members. */
22915 if (token
->type
== CPP_CLOSE_BRACE
22916 || token
->type
== CPP_EOF
22917 || token
->type
== CPP_PRAGMA_EOL
)
22920 /* See if this token is a keyword. */
22921 keyword
= token
->keyword
;
22925 case RID_PROTECTED
:
22927 /* Consume the access-specifier. */
22928 cp_lexer_consume_token (parser
->lexer
);
22929 /* Remember which access-specifier is active. */
22930 current_access_specifier
= token
->u
.value
;
22931 /* Look for the `:'. */
22932 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22936 /* Accept #pragmas at class scope. */
22937 if (token
->type
== CPP_PRAGMA
)
22939 cp_parser_pragma (parser
, pragma_member
, NULL
);
22943 /* Otherwise, the next construction must be a
22944 member-declaration. */
22945 cp_parser_member_declaration (parser
);
22950 /* Parse a member-declaration.
22952 member-declaration:
22953 decl-specifier-seq [opt] member-declarator-list [opt] ;
22954 function-definition ; [opt]
22955 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22957 template-declaration
22960 member-declarator-list:
22962 member-declarator-list , member-declarator
22965 declarator pure-specifier [opt]
22966 declarator constant-initializer [opt]
22967 identifier [opt] : constant-expression
22971 member-declaration:
22972 __extension__ member-declaration
22975 declarator attributes [opt] pure-specifier [opt]
22976 declarator attributes [opt] constant-initializer [opt]
22977 identifier [opt] attributes [opt] : constant-expression
22981 member-declaration:
22982 static_assert-declaration */
22985 cp_parser_member_declaration (cp_parser
* parser
)
22987 cp_decl_specifier_seq decl_specifiers
;
22988 tree prefix_attributes
;
22990 int declares_class_or_enum
;
22992 cp_token
*token
= NULL
;
22993 cp_token
*decl_spec_token_start
= NULL
;
22994 cp_token
*initializer_token_start
= NULL
;
22995 int saved_pedantic
;
22996 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22998 /* Check for the `__extension__' keyword. */
22999 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23002 cp_parser_member_declaration (parser
);
23003 /* Restore the old value of the PEDANTIC flag. */
23004 pedantic
= saved_pedantic
;
23009 /* Check for a template-declaration. */
23010 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23012 /* An explicit specialization here is an error condition, and we
23013 expect the specialization handler to detect and report this. */
23014 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23015 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23016 cp_parser_explicit_specialization (parser
);
23018 cp_parser_template_declaration (parser
, /*member_p=*/true);
23022 /* Check for a template introduction. */
23023 else if (cp_parser_template_declaration_after_export (parser
, true))
23026 /* Check for a using-declaration. */
23027 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23029 if (cxx_dialect
< cxx11
)
23031 /* Parse the using-declaration. */
23032 cp_parser_using_declaration (parser
,
23033 /*access_declaration_p=*/false);
23039 bool alias_decl_expected
;
23040 cp_parser_parse_tentatively (parser
);
23041 decl
= cp_parser_alias_declaration (parser
);
23042 /* Note that if we actually see the '=' token after the
23043 identifier, cp_parser_alias_declaration commits the
23044 tentative parse. In that case, we really expect an
23045 alias-declaration. Otherwise, we expect a using
23047 alias_decl_expected
=
23048 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23049 cp_parser_parse_definitely (parser
);
23051 if (alias_decl_expected
)
23052 finish_member_declaration (decl
);
23054 cp_parser_using_declaration (parser
,
23055 /*access_declaration_p=*/false);
23060 /* Check for @defs. */
23061 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23064 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23065 ivar
= ivar_chains
;
23069 ivar
= TREE_CHAIN (member
);
23070 TREE_CHAIN (member
) = NULL_TREE
;
23071 finish_member_declaration (member
);
23076 /* If the next token is `static_assert' we have a static assertion. */
23077 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23079 cp_parser_static_assert (parser
, /*member_p=*/true);
23083 parser
->colon_corrects_to_scope_p
= false;
23085 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23088 /* Parse the decl-specifier-seq. */
23089 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23090 cp_parser_decl_specifier_seq (parser
,
23091 CP_PARSER_FLAGS_OPTIONAL
,
23093 &declares_class_or_enum
);
23094 /* Check for an invalid type-name. */
23095 if (!decl_specifiers
.any_type_specifiers_p
23096 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23098 /* If there is no declarator, then the decl-specifier-seq should
23100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23102 /* If there was no decl-specifier-seq, and the next token is a
23103 `;', then we have something like:
23109 Each member-declaration shall declare at least one member
23110 name of the class. */
23111 if (!decl_specifiers
.any_specifiers_p
)
23113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23114 if (!in_system_header_at (token
->location
))
23115 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
23121 /* See if this declaration is a friend. */
23122 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23123 /* If there were decl-specifiers, check to see if there was
23124 a class-declaration. */
23125 type
= check_tag_decl (&decl_specifiers
,
23126 /*explicit_type_instantiation_p=*/false);
23127 /* Nested classes have already been added to the class, but
23128 a `friend' needs to be explicitly registered. */
23131 /* If the `friend' keyword was present, the friend must
23132 be introduced with a class-key. */
23133 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23134 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23135 "in C++03 a class-key must be used "
23136 "when declaring a friend");
23139 template <typename T> struct A {
23140 friend struct A<T>::B;
23143 A<T>::B will be represented by a TYPENAME_TYPE, and
23144 therefore not recognized by check_tag_decl. */
23147 type
= decl_specifiers
.type
;
23148 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23149 type
= TREE_TYPE (type
);
23151 if (!type
|| !TYPE_P (type
))
23152 error_at (decl_spec_token_start
->location
,
23153 "friend declaration does not name a class or "
23156 make_friend_class (current_class_type
, type
,
23157 /*complain=*/true);
23159 /* If there is no TYPE, an error message will already have
23161 else if (!type
|| type
== error_mark_node
)
23163 /* An anonymous aggregate has to be handled specially; such
23164 a declaration really declares a data member (with a
23165 particular type), as opposed to a nested class. */
23166 else if (ANON_AGGR_TYPE_P (type
))
23169 if (decl_specifiers
.storage_class
!= sc_none
)
23170 error_at (decl_spec_token_start
->location
,
23171 "a storage class on an anonymous aggregate "
23172 "in class scope is not allowed");
23174 /* Remove constructors and such from TYPE, now that we
23175 know it is an anonymous aggregate. */
23176 fixup_anonymous_aggr (type
);
23177 /* And make the corresponding data member. */
23178 decl
= build_decl (decl_spec_token_start
->location
,
23179 FIELD_DECL
, NULL_TREE
, type
);
23180 /* Add it to the class. */
23181 finish_member_declaration (decl
);
23184 cp_parser_check_access_in_redeclaration
23186 decl_spec_token_start
->location
);
23191 bool assume_semicolon
= false;
23193 /* Clear attributes from the decl_specifiers but keep them
23194 around as prefix attributes that apply them to the entity
23196 prefix_attributes
= decl_specifiers
.attributes
;
23197 decl_specifiers
.attributes
= NULL_TREE
;
23199 /* See if these declarations will be friends. */
23200 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23202 /* Keep going until we hit the `;' at the end of the
23204 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23206 tree attributes
= NULL_TREE
;
23207 tree first_attribute
;
23209 /* Peek at the next token. */
23210 token
= cp_lexer_peek_token (parser
->lexer
);
23212 /* Check for a bitfield declaration. */
23213 if (token
->type
== CPP_COLON
23214 || (token
->type
== CPP_NAME
23215 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23221 /* Get the name of the bitfield. Note that we cannot just
23222 check TOKEN here because it may have been invalidated by
23223 the call to cp_lexer_peek_nth_token above. */
23224 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
23225 identifier
= cp_parser_identifier (parser
);
23227 identifier
= NULL_TREE
;
23229 /* Consume the `:' token. */
23230 cp_lexer_consume_token (parser
->lexer
);
23231 /* Get the width of the bitfield. */
23233 = cp_parser_constant_expression (parser
);
23235 /* Look for attributes that apply to the bitfield. */
23236 attributes
= cp_parser_attributes_opt (parser
);
23237 /* Remember which attributes are prefix attributes and
23239 first_attribute
= attributes
;
23240 /* Combine the attributes. */
23241 attributes
= chainon (prefix_attributes
, attributes
);
23243 /* Create the bitfield declaration. */
23244 decl
= grokbitfield (identifier
23245 ? make_id_declarator (NULL_TREE
,
23255 cp_declarator
*declarator
;
23257 tree asm_specification
;
23258 int ctor_dtor_or_conv_p
;
23260 /* Parse the declarator. */
23262 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23263 &ctor_dtor_or_conv_p
,
23264 /*parenthesized_p=*/NULL
,
23268 /* If something went wrong parsing the declarator, make sure
23269 that we at least consume some tokens. */
23270 if (declarator
== cp_error_declarator
)
23272 /* Skip to the end of the statement. */
23273 cp_parser_skip_to_end_of_statement (parser
);
23274 /* If the next token is not a semicolon, that is
23275 probably because we just skipped over the body of
23276 a function. So, we consume a semicolon if
23277 present, but do not issue an error message if it
23279 if (cp_lexer_next_token_is (parser
->lexer
,
23281 cp_lexer_consume_token (parser
->lexer
);
23285 if (declares_class_or_enum
& 2)
23286 cp_parser_check_for_definition_in_return_type
23287 (declarator
, decl_specifiers
.type
,
23288 decl_specifiers
.locations
[ds_type_spec
]);
23290 /* Look for an asm-specification. */
23291 asm_specification
= cp_parser_asm_specification_opt (parser
);
23292 /* Look for attributes that apply to the declaration. */
23293 attributes
= cp_parser_attributes_opt (parser
);
23294 /* Remember which attributes are prefix attributes and
23296 first_attribute
= attributes
;
23297 /* Combine the attributes. */
23298 attributes
= chainon (prefix_attributes
, attributes
);
23300 /* If it's an `=', then we have a constant-initializer or a
23301 pure-specifier. It is not correct to parse the
23302 initializer before registering the member declaration
23303 since the member declaration should be in scope while
23304 its initializer is processed. However, the rest of the
23305 front end does not yet provide an interface that allows
23306 us to handle this correctly. */
23307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23311 A pure-specifier shall be used only in the declaration of
23312 a virtual function.
23314 A member-declarator can contain a constant-initializer
23315 only if it declares a static member of integral or
23318 Therefore, if the DECLARATOR is for a function, we look
23319 for a pure-specifier; otherwise, we look for a
23320 constant-initializer. When we call `grokfield', it will
23321 perform more stringent semantics checks. */
23322 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23323 if (function_declarator_p (declarator
)
23324 || (decl_specifiers
.type
23325 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23326 && declarator
->kind
== cdk_id
23327 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23328 == FUNCTION_TYPE
)))
23329 initializer
= cp_parser_pure_specifier (parser
);
23330 else if (decl_specifiers
.storage_class
!= sc_static
)
23331 initializer
= cp_parser_save_nsdmi (parser
);
23332 else if (cxx_dialect
>= cxx11
)
23335 /* Don't require a constant rvalue in C++11, since we
23336 might want a reference constant. We'll enforce
23337 constancy later. */
23338 cp_lexer_consume_token (parser
->lexer
);
23339 /* Parse the initializer. */
23340 initializer
= cp_parser_initializer_clause (parser
,
23344 /* Parse the initializer. */
23345 initializer
= cp_parser_constant_initializer (parser
);
23347 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23348 && !function_declarator_p (declarator
))
23351 if (decl_specifiers
.storage_class
!= sc_static
)
23352 initializer
= cp_parser_save_nsdmi (parser
);
23354 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23356 /* Otherwise, there is no initializer. */
23358 initializer
= NULL_TREE
;
23360 /* See if we are probably looking at a function
23361 definition. We are certainly not looking at a
23362 member-declarator. Calling `grokfield' has
23363 side-effects, so we must not do it unless we are sure
23364 that we are looking at a member-declarator. */
23365 if (cp_parser_token_starts_function_definition_p
23366 (cp_lexer_peek_token (parser
->lexer
)))
23368 /* The grammar does not allow a pure-specifier to be
23369 used when a member function is defined. (It is
23370 possible that this fact is an oversight in the
23371 standard, since a pure function may be defined
23372 outside of the class-specifier. */
23373 if (initializer
&& initializer_token_start
)
23374 error_at (initializer_token_start
->location
,
23375 "pure-specifier on function-definition");
23376 decl
= cp_parser_save_member_function_body (parser
,
23380 if (parser
->fully_implicit_function_template_p
)
23381 decl
= finish_fully_implicit_template (parser
, decl
);
23382 /* If the member was not a friend, declare it here. */
23384 finish_member_declaration (decl
);
23385 /* Peek at the next token. */
23386 token
= cp_lexer_peek_token (parser
->lexer
);
23387 /* If the next token is a semicolon, consume it. */
23388 if (token
->type
== CPP_SEMICOLON
)
23389 cp_lexer_consume_token (parser
->lexer
);
23393 if (declarator
->kind
== cdk_function
)
23394 declarator
->id_loc
= token
->location
;
23395 /* Create the declaration. */
23396 decl
= grokfield (declarator
, &decl_specifiers
,
23397 initializer
, /*init_const_expr_p=*/true,
23398 asm_specification
, attributes
);
23399 if (parser
->fully_implicit_function_template_p
)
23402 finish_fully_implicit_template (parser
, 0);
23404 decl
= finish_fully_implicit_template (parser
, decl
);
23408 cp_finalize_omp_declare_simd (parser
, decl
);
23409 cp_finalize_oacc_routine (parser
, decl
, false);
23411 /* Reset PREFIX_ATTRIBUTES. */
23412 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23413 attributes
= TREE_CHAIN (attributes
);
23415 TREE_CHAIN (attributes
) = NULL_TREE
;
23417 /* If there is any qualification still in effect, clear it
23418 now; we will be starting fresh with the next declarator. */
23419 parser
->scope
= NULL_TREE
;
23420 parser
->qualifying_scope
= NULL_TREE
;
23421 parser
->object_scope
= NULL_TREE
;
23422 /* If it's a `,', then there are more declarators. */
23423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23425 cp_lexer_consume_token (parser
->lexer
);
23426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23428 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23429 error_at (token
->location
,
23430 "stray %<,%> at end of member declaration");
23433 /* If the next token isn't a `;', then we have a parse error. */
23434 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23437 /* The next token might be a ways away from where the
23438 actual semicolon is missing. Find the previous token
23439 and use that for our error position. */
23440 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23441 error_at (token
->location
,
23442 "expected %<;%> at end of member declaration");
23444 /* Assume that the user meant to provide a semicolon. If
23445 we were to cp_parser_skip_to_end_of_statement, we might
23446 skip to a semicolon inside a member function definition
23447 and issue nonsensical error messages. */
23448 assume_semicolon
= true;
23453 /* Add DECL to the list of members. */
23455 /* Explicitly include, eg, NSDMIs, for better error
23456 recovery (c++/58650). */
23457 || !DECL_DECLARES_FUNCTION_P (decl
))
23458 finish_member_declaration (decl
);
23460 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23461 cp_parser_save_default_args (parser
, decl
);
23462 else if (TREE_CODE (decl
) == FIELD_DECL
23463 && !DECL_C_BIT_FIELD (decl
)
23464 && DECL_INITIAL (decl
))
23465 /* Add DECL to the queue of NSDMI to be parsed later. */
23466 vec_safe_push (unparsed_nsdmis
, decl
);
23469 if (assume_semicolon
)
23474 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23476 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23479 /* Parse a pure-specifier.
23484 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23485 Otherwise, ERROR_MARK_NODE is returned. */
23488 cp_parser_pure_specifier (cp_parser
* parser
)
23492 /* Look for the `=' token. */
23493 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23494 return error_mark_node
;
23495 /* Look for the `0' token. */
23496 token
= cp_lexer_peek_token (parser
->lexer
);
23498 if (token
->type
== CPP_EOF
23499 || token
->type
== CPP_PRAGMA_EOL
)
23500 return error_mark_node
;
23502 cp_lexer_consume_token (parser
->lexer
);
23504 /* Accept = default or = delete in c++0x mode. */
23505 if (token
->keyword
== RID_DEFAULT
23506 || token
->keyword
== RID_DELETE
)
23508 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23509 return token
->u
.value
;
23512 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23513 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23515 cp_parser_error (parser
,
23516 "invalid pure specifier (only %<= 0%> is allowed)");
23517 cp_parser_skip_to_end_of_statement (parser
);
23518 return error_mark_node
;
23520 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23522 error_at (token
->location
, "templates may not be %<virtual%>");
23523 return error_mark_node
;
23526 return integer_zero_node
;
23529 /* Parse a constant-initializer.
23531 constant-initializer:
23532 = constant-expression
23534 Returns a representation of the constant-expression. */
23537 cp_parser_constant_initializer (cp_parser
* parser
)
23539 /* Look for the `=' token. */
23540 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23541 return error_mark_node
;
23543 /* It is invalid to write:
23545 struct S { static const int i = { 7 }; };
23548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23550 cp_parser_error (parser
,
23551 "a brace-enclosed initializer is not allowed here");
23552 /* Consume the opening brace. */
23553 cp_lexer_consume_token (parser
->lexer
);
23554 /* Skip the initializer. */
23555 cp_parser_skip_to_closing_brace (parser
);
23556 /* Look for the trailing `}'. */
23557 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
23559 return error_mark_node
;
23562 return cp_parser_constant_expression (parser
);
23565 /* Derived classes [gram.class.derived] */
23567 /* Parse a base-clause.
23570 : base-specifier-list
23572 base-specifier-list:
23573 base-specifier ... [opt]
23574 base-specifier-list , base-specifier ... [opt]
23576 Returns a TREE_LIST representing the base-classes, in the order in
23577 which they were declared. The representation of each node is as
23578 described by cp_parser_base_specifier.
23580 In the case that no bases are specified, this function will return
23581 NULL_TREE, not ERROR_MARK_NODE. */
23584 cp_parser_base_clause (cp_parser
* parser
)
23586 tree bases
= NULL_TREE
;
23588 /* Look for the `:' that begins the list. */
23589 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23591 /* Scan the base-specifier-list. */
23596 bool pack_expansion_p
= false;
23598 /* Look for the base-specifier. */
23599 base
= cp_parser_base_specifier (parser
);
23600 /* Look for the (optional) ellipsis. */
23601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23603 /* Consume the `...'. */
23604 cp_lexer_consume_token (parser
->lexer
);
23606 pack_expansion_p
= true;
23609 /* Add BASE to the front of the list. */
23610 if (base
&& base
!= error_mark_node
)
23612 if (pack_expansion_p
)
23613 /* Make this a pack expansion type. */
23614 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
23616 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
23618 TREE_CHAIN (base
) = bases
;
23622 /* Peek at the next token. */
23623 token
= cp_lexer_peek_token (parser
->lexer
);
23624 /* If it's not a comma, then the list is complete. */
23625 if (token
->type
!= CPP_COMMA
)
23627 /* Consume the `,'. */
23628 cp_lexer_consume_token (parser
->lexer
);
23631 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23632 base class had a qualified name. However, the next name that
23633 appears is certainly not qualified. */
23634 parser
->scope
= NULL_TREE
;
23635 parser
->qualifying_scope
= NULL_TREE
;
23636 parser
->object_scope
= NULL_TREE
;
23638 return nreverse (bases
);
23641 /* Parse a base-specifier.
23644 :: [opt] nested-name-specifier [opt] class-name
23645 virtual access-specifier [opt] :: [opt] nested-name-specifier
23647 access-specifier virtual [opt] :: [opt] nested-name-specifier
23650 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23651 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23652 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23653 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23656 cp_parser_base_specifier (cp_parser
* parser
)
23660 bool virtual_p
= false;
23661 bool duplicate_virtual_error_issued_p
= false;
23662 bool duplicate_access_error_issued_p
= false;
23663 bool class_scope_p
, template_p
;
23664 tree access
= access_default_node
;
23667 /* Process the optional `virtual' and `access-specifier'. */
23670 /* Peek at the next token. */
23671 token
= cp_lexer_peek_token (parser
->lexer
);
23672 /* Process `virtual'. */
23673 switch (token
->keyword
)
23676 /* If `virtual' appears more than once, issue an error. */
23677 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
23679 cp_parser_error (parser
,
23680 "%<virtual%> specified more than once in base-specified");
23681 duplicate_virtual_error_issued_p
= true;
23686 /* Consume the `virtual' token. */
23687 cp_lexer_consume_token (parser
->lexer
);
23692 case RID_PROTECTED
:
23694 /* If more than one access specifier appears, issue an
23696 if (access
!= access_default_node
23697 && !duplicate_access_error_issued_p
)
23699 cp_parser_error (parser
,
23700 "more than one access specifier in base-specified");
23701 duplicate_access_error_issued_p
= true;
23704 access
= ridpointers
[(int) token
->keyword
];
23706 /* Consume the access-specifier. */
23707 cp_lexer_consume_token (parser
->lexer
);
23716 /* It is not uncommon to see programs mechanically, erroneously, use
23717 the 'typename' keyword to denote (dependent) qualified types
23718 as base classes. */
23719 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23721 token
= cp_lexer_peek_token (parser
->lexer
);
23722 if (!processing_template_decl
)
23723 error_at (token
->location
,
23724 "keyword %<typename%> not allowed outside of templates");
23726 error_at (token
->location
,
23727 "keyword %<typename%> not allowed in this context "
23728 "(the base class is implicitly a type)");
23729 cp_lexer_consume_token (parser
->lexer
);
23732 /* Look for the optional `::' operator. */
23733 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
23734 /* Look for the nested-name-specifier. The simplest way to
23739 The keyword `typename' is not permitted in a base-specifier or
23740 mem-initializer; in these contexts a qualified name that
23741 depends on a template-parameter is implicitly assumed to be a
23744 is to pretend that we have seen the `typename' keyword at this
23746 cp_parser_nested_name_specifier_opt (parser
,
23747 /*typename_keyword_p=*/true,
23748 /*check_dependency_p=*/true,
23750 /*is_declaration=*/true);
23751 /* If the base class is given by a qualified name, assume that names
23752 we see are type names or templates, as appropriate. */
23753 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
23754 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
23757 && cp_lexer_next_token_is_decltype (parser
->lexer
))
23758 /* DR 950 allows decltype as a base-specifier. */
23759 type
= cp_parser_decltype (parser
);
23762 /* Otherwise, look for the class-name. */
23763 type
= cp_parser_class_name (parser
,
23767 /*check_dependency_p=*/true,
23768 /*class_head_p=*/false,
23769 /*is_declaration=*/true);
23770 type
= TREE_TYPE (type
);
23773 if (type
== error_mark_node
)
23774 return error_mark_node
;
23776 return finish_base_specifier (type
, access
, virtual_p
);
23779 /* Exception handling [gram.exception] */
23781 /* Parse an (optional) noexcept-specification.
23783 noexcept-specification:
23784 noexcept ( constant-expression ) [opt]
23786 If no noexcept-specification is present, returns NULL_TREE.
23787 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23788 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23789 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23790 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23791 in which case a boolean condition is returned instead. */
23794 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
23795 bool require_constexpr
,
23796 bool* consumed_expr
,
23800 const char *saved_message
;
23802 /* Peek at the next token. */
23803 token
= cp_lexer_peek_token (parser
->lexer
);
23805 /* Is it a noexcept-specification? */
23806 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
23809 cp_lexer_consume_token (parser
->lexer
);
23811 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
23813 cp_lexer_consume_token (parser
->lexer
);
23815 if (require_constexpr
)
23817 /* Types may not be defined in an exception-specification. */
23818 saved_message
= parser
->type_definition_forbidden_message
;
23819 parser
->type_definition_forbidden_message
23820 = G_("types may not be defined in an exception-specification");
23822 expr
= cp_parser_constant_expression (parser
);
23824 /* Restore the saved message. */
23825 parser
->type_definition_forbidden_message
= saved_message
;
23829 expr
= cp_parser_expression (parser
);
23830 *consumed_expr
= true;
23833 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23837 expr
= boolean_true_node
;
23838 if (!require_constexpr
)
23839 *consumed_expr
= false;
23842 /* We cannot build a noexcept-spec right away because this will check
23843 that expr is a constexpr. */
23845 return build_noexcept_spec (expr
, tf_warning_or_error
);
23853 /* Parse an (optional) exception-specification.
23855 exception-specification:
23856 throw ( type-id-list [opt] )
23858 Returns a TREE_LIST representing the exception-specification. The
23859 TREE_VALUE of each node is a type. */
23862 cp_parser_exception_specification_opt (cp_parser
* parser
)
23866 const char *saved_message
;
23868 /* Peek at the next token. */
23869 token
= cp_lexer_peek_token (parser
->lexer
);
23871 /* Is it a noexcept-specification? */
23872 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
23874 if (type_id_list
!= NULL_TREE
)
23875 return type_id_list
;
23877 /* If it's not `throw', then there's no exception-specification. */
23878 if (!cp_parser_is_keyword (token
, RID_THROW
))
23881 location_t loc
= token
->location
;
23883 /* Consume the `throw'. */
23884 cp_lexer_consume_token (parser
->lexer
);
23886 /* Look for the `('. */
23887 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23889 /* Peek at the next token. */
23890 token
= cp_lexer_peek_token (parser
->lexer
);
23891 /* If it's not a `)', then there is a type-id-list. */
23892 if (token
->type
!= CPP_CLOSE_PAREN
)
23894 /* Types may not be defined in an exception-specification. */
23895 saved_message
= parser
->type_definition_forbidden_message
;
23896 parser
->type_definition_forbidden_message
23897 = G_("types may not be defined in an exception-specification");
23898 /* Parse the type-id-list. */
23899 type_id_list
= cp_parser_type_id_list (parser
);
23900 /* Restore the saved message. */
23901 parser
->type_definition_forbidden_message
= saved_message
;
23903 if (cxx_dialect
>= cxx1z
)
23905 error_at (loc
, "ISO C++1z does not allow dynamic exception "
23907 type_id_list
= NULL_TREE
;
23909 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
23910 warning_at (loc
, OPT_Wdeprecated
,
23911 "dynamic exception specifications are deprecated in "
23914 /* In C++17, throw() is equivalent to noexcept (true). throw()
23915 is deprecated in C++11 and above as well, but is still widely used,
23916 so don't warn about it yet. */
23917 else if (cxx_dialect
>= cxx1z
)
23918 type_id_list
= noexcept_true_spec
;
23920 type_id_list
= empty_except_spec
;
23922 /* Look for the `)'. */
23923 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23925 return type_id_list
;
23928 /* Parse an (optional) type-id-list.
23932 type-id-list , type-id ... [opt]
23934 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23935 in the order that the types were presented. */
23938 cp_parser_type_id_list (cp_parser
* parser
)
23940 tree types
= NULL_TREE
;
23947 token
= cp_lexer_peek_token (parser
->lexer
);
23949 /* Get the next type-id. */
23950 type
= cp_parser_type_id (parser
);
23951 /* Check for invalid 'auto'. */
23952 if (flag_concepts
&& type_uses_auto (type
))
23954 error_at (token
->location
,
23955 "invalid use of %<auto%> in exception-specification");
23956 type
= error_mark_node
;
23958 /* Parse the optional ellipsis. */
23959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23961 /* Consume the `...'. */
23962 cp_lexer_consume_token (parser
->lexer
);
23964 /* Turn the type into a pack expansion expression. */
23965 type
= make_pack_expansion (type
);
23967 /* Add it to the list. */
23968 types
= add_exception_specifier (types
, type
, /*complain=*/1);
23969 /* Peek at the next token. */
23970 token
= cp_lexer_peek_token (parser
->lexer
);
23971 /* If it is not a `,', we are done. */
23972 if (token
->type
!= CPP_COMMA
)
23974 /* Consume the `,'. */
23975 cp_lexer_consume_token (parser
->lexer
);
23978 return nreverse (types
);
23981 /* Parse a try-block.
23984 try compound-statement handler-seq */
23987 cp_parser_try_block (cp_parser
* parser
)
23991 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
23992 if (parser
->in_function_body
23993 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
23994 error ("%<try%> in %<constexpr%> function");
23996 try_block
= begin_try_block ();
23997 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
23998 finish_try_block (try_block
);
23999 cp_parser_handler_seq (parser
);
24000 finish_handler_sequence (try_block
);
24005 /* Parse a function-try-block.
24007 function-try-block:
24008 try ctor-initializer [opt] function-body handler-seq */
24011 cp_parser_function_try_block (cp_parser
* parser
)
24013 tree compound_stmt
;
24015 bool ctor_initializer_p
;
24017 /* Look for the `try' keyword. */
24018 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24020 /* Let the rest of the front end know where we are. */
24021 try_block
= begin_function_try_block (&compound_stmt
);
24022 /* Parse the function-body. */
24023 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
24024 (parser
, /*in_function_try_block=*/true);
24025 /* We're done with the `try' part. */
24026 finish_function_try_block (try_block
);
24027 /* Parse the handlers. */
24028 cp_parser_handler_seq (parser
);
24029 /* We're done with the handlers. */
24030 finish_function_handler_sequence (try_block
, compound_stmt
);
24032 return ctor_initializer_p
;
24035 /* Parse a handler-seq.
24038 handler handler-seq [opt] */
24041 cp_parser_handler_seq (cp_parser
* parser
)
24047 /* Parse the handler. */
24048 cp_parser_handler (parser
);
24049 /* Peek at the next token. */
24050 token
= cp_lexer_peek_token (parser
->lexer
);
24051 /* If it's not `catch' then there are no more handlers. */
24052 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24057 /* Parse a handler.
24060 catch ( exception-declaration ) compound-statement */
24063 cp_parser_handler (cp_parser
* parser
)
24068 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24069 handler
= begin_handler ();
24070 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24071 declaration
= cp_parser_exception_declaration (parser
);
24072 finish_handler_parms (declaration
, handler
);
24073 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24074 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24075 finish_handler (handler
);
24078 /* Parse an exception-declaration.
24080 exception-declaration:
24081 type-specifier-seq declarator
24082 type-specifier-seq abstract-declarator
24086 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24087 ellipsis variant is used. */
24090 cp_parser_exception_declaration (cp_parser
* parser
)
24092 cp_decl_specifier_seq type_specifiers
;
24093 cp_declarator
*declarator
;
24094 const char *saved_message
;
24096 /* If it's an ellipsis, it's easy to handle. */
24097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24099 /* Consume the `...' token. */
24100 cp_lexer_consume_token (parser
->lexer
);
24104 /* Types may not be defined in exception-declarations. */
24105 saved_message
= parser
->type_definition_forbidden_message
;
24106 parser
->type_definition_forbidden_message
24107 = G_("types may not be defined in exception-declarations");
24109 /* Parse the type-specifier-seq. */
24110 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24111 /*is_trailing_return=*/false,
24113 /* If it's a `)', then there is no declarator. */
24114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24117 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24118 /*ctor_dtor_or_conv_p=*/NULL
,
24119 /*parenthesized_p=*/NULL
,
24120 /*member_p=*/false,
24121 /*friend_p=*/false);
24123 /* Restore the saved message. */
24124 parser
->type_definition_forbidden_message
= saved_message
;
24126 if (!type_specifiers
.any_specifiers_p
)
24127 return error_mark_node
;
24129 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24132 /* Parse a throw-expression.
24135 throw assignment-expression [opt]
24137 Returns a THROW_EXPR representing the throw-expression. */
24140 cp_parser_throw_expression (cp_parser
* parser
)
24145 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24146 token
= cp_lexer_peek_token (parser
->lexer
);
24147 /* Figure out whether or not there is an assignment-expression
24148 following the "throw" keyword. */
24149 if (token
->type
== CPP_COMMA
24150 || token
->type
== CPP_SEMICOLON
24151 || token
->type
== CPP_CLOSE_PAREN
24152 || token
->type
== CPP_CLOSE_SQUARE
24153 || token
->type
== CPP_CLOSE_BRACE
24154 || token
->type
== CPP_COLON
)
24155 expression
= NULL_TREE
;
24157 expression
= cp_parser_assignment_expression (parser
);
24159 return build_throw (expression
);
24162 /* GNU Extensions */
24164 /* Parse an (optional) asm-specification.
24167 asm ( string-literal )
24169 If the asm-specification is present, returns a STRING_CST
24170 corresponding to the string-literal. Otherwise, returns
24174 cp_parser_asm_specification_opt (cp_parser
* parser
)
24177 tree asm_specification
;
24179 /* Peek at the next token. */
24180 token
= cp_lexer_peek_token (parser
->lexer
);
24181 /* If the next token isn't the `asm' keyword, then there's no
24182 asm-specification. */
24183 if (!cp_parser_is_keyword (token
, RID_ASM
))
24186 /* Consume the `asm' token. */
24187 cp_lexer_consume_token (parser
->lexer
);
24188 /* Look for the `('. */
24189 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24191 /* Look for the string-literal. */
24192 asm_specification
= cp_parser_string_literal (parser
, false, false);
24194 /* Look for the `)'. */
24195 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24197 return asm_specification
;
24200 /* Parse an asm-operand-list.
24204 asm-operand-list , asm-operand
24207 string-literal ( expression )
24208 [ string-literal ] string-literal ( expression )
24210 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24211 each node is the expression. The TREE_PURPOSE is itself a
24212 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24213 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24214 is a STRING_CST for the string literal before the parenthesis. Returns
24215 ERROR_MARK_NODE if any of the operands are invalid. */
24218 cp_parser_asm_operand_list (cp_parser
* parser
)
24220 tree asm_operands
= NULL_TREE
;
24221 bool invalid_operands
= false;
24225 tree string_literal
;
24229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24231 /* Consume the `[' token. */
24232 cp_lexer_consume_token (parser
->lexer
);
24233 /* Read the operand name. */
24234 name
= cp_parser_identifier (parser
);
24235 if (name
!= error_mark_node
)
24236 name
= build_string (IDENTIFIER_LENGTH (name
),
24237 IDENTIFIER_POINTER (name
));
24238 /* Look for the closing `]'. */
24239 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24243 /* Look for the string-literal. */
24244 string_literal
= cp_parser_string_literal (parser
, false, false);
24246 /* Look for the `('. */
24247 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24248 /* Parse the expression. */
24249 expression
= cp_parser_expression (parser
);
24250 /* Look for the `)'. */
24251 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24253 if (name
== error_mark_node
24254 || string_literal
== error_mark_node
24255 || expression
== error_mark_node
)
24256 invalid_operands
= true;
24258 /* Add this operand to the list. */
24259 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24262 /* If the next token is not a `,', there are no more
24264 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24266 /* Consume the `,'. */
24267 cp_lexer_consume_token (parser
->lexer
);
24270 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24273 /* Parse an asm-clobber-list.
24277 asm-clobber-list , string-literal
24279 Returns a TREE_LIST, indicating the clobbers in the order that they
24280 appeared. The TREE_VALUE of each node is a STRING_CST. */
24283 cp_parser_asm_clobber_list (cp_parser
* parser
)
24285 tree clobbers
= NULL_TREE
;
24289 tree string_literal
;
24291 /* Look for the string literal. */
24292 string_literal
= cp_parser_string_literal (parser
, false, false);
24293 /* Add it to the list. */
24294 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24295 /* If the next token is not a `,', then the list is
24297 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24299 /* Consume the `,' token. */
24300 cp_lexer_consume_token (parser
->lexer
);
24306 /* Parse an asm-label-list.
24310 asm-label-list , identifier
24312 Returns a TREE_LIST, indicating the labels in the order that they
24313 appeared. The TREE_VALUE of each node is a label. */
24316 cp_parser_asm_label_list (cp_parser
* parser
)
24318 tree labels
= NULL_TREE
;
24322 tree identifier
, label
, name
;
24324 /* Look for the identifier. */
24325 identifier
= cp_parser_identifier (parser
);
24326 if (!error_operand_p (identifier
))
24328 label
= lookup_label (identifier
);
24329 if (TREE_CODE (label
) == LABEL_DECL
)
24331 TREE_USED (label
) = 1;
24332 check_goto (label
);
24333 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24334 IDENTIFIER_POINTER (identifier
));
24335 labels
= tree_cons (name
, label
, labels
);
24338 /* If the next token is not a `,', then the list is
24340 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24342 /* Consume the `,' token. */
24343 cp_lexer_consume_token (parser
->lexer
);
24346 return nreverse (labels
);
24349 /* Return TRUE iff the next tokens in the stream are possibly the
24350 beginning of a GNU extension attribute. */
24353 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24355 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24358 /* Return TRUE iff the next tokens in the stream are possibly the
24359 beginning of a standard C++-11 attribute specifier. */
24362 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24364 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24367 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24368 beginning of a standard C++-11 attribute specifier. */
24371 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24373 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24375 return (cxx_dialect
>= cxx11
24376 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24377 || (token
->type
== CPP_OPEN_SQUARE
24378 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24379 && token
->type
== CPP_OPEN_SQUARE
)));
24382 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24383 beginning of a GNU extension attribute. */
24386 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24388 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24390 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24393 /* Return true iff the next tokens can be the beginning of either a
24394 GNU attribute list, or a standard C++11 attribute sequence. */
24397 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24399 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24400 || cp_next_tokens_can_be_std_attribute_p (parser
));
24403 /* Return true iff the next Nth tokens can be the beginning of either
24404 a GNU attribute list, or a standard C++11 attribute sequence. */
24407 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24409 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24410 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24413 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24414 of GNU attributes, or return NULL. */
24417 cp_parser_attributes_opt (cp_parser
*parser
)
24419 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24420 return cp_parser_gnu_attributes_opt (parser
);
24421 return cp_parser_std_attribute_spec_seq (parser
);
24424 #define CILK_SIMD_FN_CLAUSE_MASK \
24425 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24426 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24427 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24428 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24429 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24431 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24432 vector [(<clauses>)] */
24435 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
24437 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
24438 cp_token
*token
= v_token
;
24441 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
24442 parser
->cilk_simd_fn_info
->error_seen
= false;
24443 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
24444 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
24445 parser
->cilk_simd_fn_info
->clauses
= NULL_TREE
;
24447 int paren_scope
= 0;
24448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24450 cp_lexer_consume_token (parser
->lexer
);
24451 v_token
= cp_lexer_peek_token (parser
->lexer
);
24454 while (paren_scope
> 0)
24456 token
= cp_lexer_peek_token (parser
->lexer
);
24457 if (token
->type
== CPP_OPEN_PAREN
)
24459 else if (token
->type
== CPP_CLOSE_PAREN
)
24461 /* Do not push the last ')' */
24462 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
24463 cp_lexer_consume_token (parser
->lexer
);
24466 token
->type
= CPP_PRAGMA_EOL
;
24467 parser
->lexer
->next_token
= token
;
24468 cp_lexer_consume_token (parser
->lexer
);
24470 struct cp_token_cache
*cp
24471 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
24472 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
24475 /* Parse an (optional) series of attributes.
24478 attributes attribute
24481 __attribute__ (( attribute-list [opt] ))
24483 The return value is as for cp_parser_gnu_attribute_list. */
24486 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24488 tree attributes
= NULL_TREE
;
24493 tree attribute_list
;
24496 /* Peek at the next token. */
24497 token
= cp_lexer_peek_token (parser
->lexer
);
24498 /* If it's not `__attribute__', then we're done. */
24499 if (token
->keyword
!= RID_ATTRIBUTE
)
24502 /* Consume the `__attribute__' keyword. */
24503 cp_lexer_consume_token (parser
->lexer
);
24504 /* Look for the two `(' tokens. */
24505 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24506 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24508 /* Peek at the next token. */
24509 token
= cp_lexer_peek_token (parser
->lexer
);
24510 if (token
->type
!= CPP_CLOSE_PAREN
)
24511 /* Parse the attribute-list. */
24512 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24514 /* If the next token is a `)', then there is no attribute
24516 attribute_list
= NULL
;
24518 /* Look for the two `)' tokens. */
24519 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24521 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24524 cp_parser_skip_to_end_of_statement (parser
);
24526 /* Add these new attributes to the list. */
24527 attributes
= chainon (attributes
, attribute_list
);
24533 /* Parse a GNU attribute-list.
24537 attribute-list , attribute
24541 identifier ( identifier )
24542 identifier ( identifier , expression-list )
24543 identifier ( expression-list )
24545 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24546 to an attribute. The TREE_PURPOSE of each node is the identifier
24547 indicating which attribute is in use. The TREE_VALUE represents
24548 the arguments, if any. */
24551 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24553 tree attribute_list
= NULL_TREE
;
24554 bool save_translate_strings_p
= parser
->translate_strings_p
;
24556 parser
->translate_strings_p
= false;
24563 /* Look for the identifier. We also allow keywords here; for
24564 example `__attribute__ ((const))' is legal. */
24565 token
= cp_lexer_peek_token (parser
->lexer
);
24566 if (token
->type
== CPP_NAME
24567 || token
->type
== CPP_KEYWORD
)
24569 tree arguments
= NULL_TREE
;
24571 /* Consume the token, but save it since we need it for the
24572 SIMD enabled function parsing. */
24573 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24575 /* Save away the identifier that indicates which attribute
24577 identifier
= (token
->type
== CPP_KEYWORD
)
24578 /* For keywords, use the canonical spelling, not the
24579 parsed identifier. */
24580 ? ridpointers
[(int) token
->keyword
]
24581 : id_token
->u
.value
;
24583 attribute
= build_tree_list (identifier
, NULL_TREE
);
24585 /* Peek at the next token. */
24586 token
= cp_lexer_peek_token (parser
->lexer
);
24587 /* If it's an `(', then parse the attribute arguments. */
24588 if (token
->type
== CPP_OPEN_PAREN
)
24590 vec
<tree
, va_gc
> *vec
;
24591 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24592 ? id_attr
: normal_attr
);
24593 if (is_cilkplus_vector_p (identifier
))
24595 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24599 vec
= cp_parser_parenthesized_expression_list
24600 (parser
, attr_flag
, /*cast_p=*/false,
24601 /*allow_expansion_p=*/false,
24602 /*non_constant_p=*/NULL
);
24604 arguments
= error_mark_node
;
24607 arguments
= build_tree_list_vec (vec
);
24608 release_tree_vector (vec
);
24610 /* Save the arguments away. */
24611 TREE_VALUE (attribute
) = arguments
;
24613 else if (is_cilkplus_vector_p (identifier
))
24615 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24619 if (arguments
!= error_mark_node
)
24621 /* Add this attribute to the list. */
24622 TREE_CHAIN (attribute
) = attribute_list
;
24623 attribute_list
= attribute
;
24626 token
= cp_lexer_peek_token (parser
->lexer
);
24628 /* Now, look for more attributes. If the next token isn't a
24629 `,', we're done. */
24630 if (token
->type
!= CPP_COMMA
)
24633 /* Consume the comma and keep going. */
24634 cp_lexer_consume_token (parser
->lexer
);
24636 parser
->translate_strings_p
= save_translate_strings_p
;
24638 /* We built up the list in reverse order. */
24639 return nreverse (attribute_list
);
24642 /* Parse a standard C++11 attribute.
24644 The returned representation is a TREE_LIST which TREE_PURPOSE is
24645 the scoped name of the attribute, and the TREE_VALUE is its
24648 Note that the scoped name of the attribute is itself a TREE_LIST
24649 which TREE_PURPOSE is the namespace of the attribute, and
24650 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24651 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24652 and which TREE_PURPOSE is directly the attribute name.
24654 Clients of the attribute code should use get_attribute_namespace
24655 and get_attribute_name to get the actual namespace and name of
24656 attributes, regardless of their being GNU or C++11 attributes.
24659 attribute-token attribute-argument-clause [opt]
24663 attribute-scoped-token
24665 attribute-scoped-token:
24666 attribute-namespace :: identifier
24668 attribute-namespace:
24671 attribute-argument-clause:
24672 ( balanced-token-seq )
24674 balanced-token-seq:
24675 balanced-token [opt]
24676 balanced-token-seq balanced-token
24679 ( balanced-token-seq )
24680 [ balanced-token-seq ]
24681 { balanced-token-seq }. */
24684 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
24686 tree attribute
, attr_id
= NULL_TREE
, arguments
;
24689 /* First, parse name of the attribute, a.k.a attribute-token. */
24691 token
= cp_lexer_peek_token (parser
->lexer
);
24692 if (token
->type
== CPP_NAME
)
24693 attr_id
= token
->u
.value
;
24694 else if (token
->type
== CPP_KEYWORD
)
24695 attr_id
= ridpointers
[(int) token
->keyword
];
24696 else if (token
->flags
& NAMED_OP
)
24697 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24699 if (attr_id
== NULL_TREE
)
24702 cp_lexer_consume_token (parser
->lexer
);
24704 token
= cp_lexer_peek_token (parser
->lexer
);
24705 if (token
->type
== CPP_SCOPE
)
24707 /* We are seeing a scoped attribute token. */
24709 cp_lexer_consume_token (parser
->lexer
);
24711 error_at (token
->location
, "attribute using prefix used together "
24712 "with scoped attribute token");
24715 token
= cp_lexer_consume_token (parser
->lexer
);
24716 if (token
->type
== CPP_NAME
)
24717 attr_id
= token
->u
.value
;
24718 else if (token
->type
== CPP_KEYWORD
)
24719 attr_id
= ridpointers
[(int) token
->keyword
];
24720 else if (token
->flags
& NAMED_OP
)
24721 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24724 error_at (token
->location
,
24725 "expected an identifier for the attribute name");
24726 return error_mark_node
;
24728 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24730 token
= cp_lexer_peek_token (parser
->lexer
);
24733 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24737 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
24739 /* C++11 noreturn attribute is equivalent to GNU's. */
24740 if (is_attribute_p ("noreturn", attr_id
))
24741 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24742 /* C++14 deprecated attribute is equivalent to GNU's. */
24743 else if (is_attribute_p ("deprecated", attr_id
))
24744 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24745 /* C++17 fallthrough attribute is equivalent to GNU's. */
24746 else if (is_attribute_p ("fallthrough", attr_id
))
24747 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24748 /* Transactional Memory TS optimize_for_synchronized attribute is
24749 equivalent to GNU transaction_callable. */
24750 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
24751 TREE_PURPOSE (attribute
)
24752 = get_identifier ("transaction_callable");
24753 /* Transactional Memory attributes are GNU attributes. */
24754 else if (tm_attr_to_mask (attr_id
))
24755 TREE_PURPOSE (attribute
) = attr_id
;
24758 /* Now parse the optional argument clause of the attribute. */
24760 if (token
->type
!= CPP_OPEN_PAREN
)
24764 vec
<tree
, va_gc
> *vec
;
24765 int attr_flag
= normal_attr
;
24767 if (attr_ns
== get_identifier ("gnu")
24768 && attribute_takes_identifier_p (attr_id
))
24769 /* A GNU attribute that takes an identifier in parameter. */
24770 attr_flag
= id_attr
;
24772 vec
= cp_parser_parenthesized_expression_list
24773 (parser
, attr_flag
, /*cast_p=*/false,
24774 /*allow_expansion_p=*/true,
24775 /*non_constant_p=*/NULL
);
24777 arguments
= error_mark_node
;
24780 arguments
= build_tree_list_vec (vec
);
24781 release_tree_vector (vec
);
24784 if (arguments
== error_mark_node
)
24785 attribute
= error_mark_node
;
24787 TREE_VALUE (attribute
) = arguments
;
24793 /* Check that the attribute ATTRIBUTE appears at most once in the
24794 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24795 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24796 isn't implemented yet in GCC. */
24799 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
24803 tree name
= get_attribute_name (attribute
);
24804 if (is_attribute_p ("noreturn", name
)
24805 && lookup_attribute ("noreturn", attributes
))
24806 error ("attribute %<noreturn%> can appear at most once "
24807 "in an attribute-list");
24808 else if (is_attribute_p ("deprecated", name
)
24809 && lookup_attribute ("deprecated", attributes
))
24810 error ("attribute %<deprecated%> can appear at most once "
24811 "in an attribute-list");
24815 /* Parse a list of standard C++-11 attributes.
24819 attribute-list , attribute[opt]
24821 attribute-list , attribute ...
24825 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
24827 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
24828 cp_token
*token
= NULL
;
24832 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
24833 if (attribute
== error_mark_node
)
24835 if (attribute
!= NULL_TREE
)
24837 cp_parser_check_std_attribute (attributes
, attribute
);
24838 TREE_CHAIN (attribute
) = attributes
;
24839 attributes
= attribute
;
24841 token
= cp_lexer_peek_token (parser
->lexer
);
24842 if (token
->type
== CPP_ELLIPSIS
)
24844 cp_lexer_consume_token (parser
->lexer
);
24845 if (attribute
== NULL_TREE
)
24846 error_at (token
->location
,
24847 "expected attribute before %<...%>");
24849 TREE_VALUE (attribute
)
24850 = make_pack_expansion (TREE_VALUE (attribute
));
24851 token
= cp_lexer_peek_token (parser
->lexer
);
24853 if (token
->type
!= CPP_COMMA
)
24855 cp_lexer_consume_token (parser
->lexer
);
24857 attributes
= nreverse (attributes
);
24861 /* Parse a standard C++-11 attribute specifier.
24863 attribute-specifier:
24864 [ [ attribute-using-prefix [opt] attribute-list ] ]
24865 alignment-specifier
24867 attribute-using-prefix:
24868 using attribute-namespace :
24870 alignment-specifier:
24871 alignas ( type-id ... [opt] )
24872 alignas ( alignment-expression ... [opt] ). */
24875 cp_parser_std_attribute_spec (cp_parser
*parser
)
24877 tree attributes
= NULL_TREE
;
24878 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24880 if (token
->type
== CPP_OPEN_SQUARE
24881 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
24883 tree attr_ns
= NULL_TREE
;
24885 cp_lexer_consume_token (parser
->lexer
);
24886 cp_lexer_consume_token (parser
->lexer
);
24888 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24890 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24891 if (token
->type
== CPP_NAME
)
24892 attr_ns
= token
->u
.value
;
24893 else if (token
->type
== CPP_KEYWORD
)
24894 attr_ns
= ridpointers
[(int) token
->keyword
];
24895 else if (token
->flags
& NAMED_OP
)
24896 attr_ns
= get_identifier (cpp_type2name (token
->type
,
24899 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
24901 if (cxx_dialect
< cxx1z
24902 && !in_system_header_at (input_location
))
24903 pedwarn (input_location
, 0,
24904 "attribute using prefix only available "
24905 "with -std=c++1z or -std=gnu++1z");
24907 cp_lexer_consume_token (parser
->lexer
);
24908 cp_lexer_consume_token (parser
->lexer
);
24909 cp_lexer_consume_token (parser
->lexer
);
24912 attr_ns
= NULL_TREE
;
24915 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
24917 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
24918 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
24919 cp_parser_skip_to_end_of_statement (parser
);
24921 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24922 when we are sure that we have actually parsed them. */
24923 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24929 /* Look for an alignment-specifier. */
24931 token
= cp_lexer_peek_token (parser
->lexer
);
24933 if (token
->type
!= CPP_KEYWORD
24934 || token
->keyword
!= RID_ALIGNAS
)
24937 cp_lexer_consume_token (parser
->lexer
);
24938 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24940 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
24942 cp_parser_error (parser
, "expected %<(%>");
24943 return error_mark_node
;
24946 cp_parser_parse_tentatively (parser
);
24947 alignas_expr
= cp_parser_type_id (parser
);
24949 if (!cp_parser_parse_definitely (parser
))
24951 alignas_expr
= cp_parser_assignment_expression (parser
);
24952 if (alignas_expr
== error_mark_node
)
24953 cp_parser_skip_to_end_of_statement (parser
);
24954 if (alignas_expr
== NULL_TREE
24955 || alignas_expr
== error_mark_node
)
24956 return alignas_expr
;
24959 alignas_expr
= cxx_alignas_expr (alignas_expr
);
24960 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
24962 /* Handle alignas (pack...). */
24963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24965 cp_lexer_consume_token (parser
->lexer
);
24966 alignas_expr
= make_pack_expansion (alignas_expr
);
24969 /* Something went wrong, so don't build the attribute. */
24970 if (alignas_expr
== error_mark_node
)
24971 return error_mark_node
;
24973 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
24975 cp_parser_error (parser
, "expected %<)%>");
24976 return error_mark_node
;
24979 /* Build the C++-11 representation of an 'aligned'
24982 build_tree_list (build_tree_list (get_identifier ("gnu"),
24983 get_identifier ("aligned")),
24990 /* Parse a standard C++-11 attribute-specifier-seq.
24992 attribute-specifier-seq:
24993 attribute-specifier-seq [opt] attribute-specifier
24997 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
24999 tree attr_specs
= NULL_TREE
;
25000 tree attr_last
= NULL_TREE
;
25004 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25005 if (attr_spec
== NULL_TREE
)
25007 if (attr_spec
== error_mark_node
)
25008 return error_mark_node
;
25011 TREE_CHAIN (attr_last
) = attr_spec
;
25013 attr_specs
= attr_last
= attr_spec
;
25014 attr_last
= tree_last (attr_last
);
25020 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25021 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25022 current value of the PEDANTIC flag, regardless of whether or not
25023 the `__extension__' keyword is present. The caller is responsible
25024 for restoring the value of the PEDANTIC flag. */
25027 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25029 /* Save the old value of the PEDANTIC flag. */
25030 *saved_pedantic
= pedantic
;
25032 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25034 /* Consume the `__extension__' token. */
25035 cp_lexer_consume_token (parser
->lexer
);
25036 /* We're not being pedantic while the `__extension__' keyword is
25046 /* Parse a label declaration.
25049 __label__ label-declarator-seq ;
25051 label-declarator-seq:
25052 identifier , label-declarator-seq
25056 cp_parser_label_declaration (cp_parser
* parser
)
25058 /* Look for the `__label__' keyword. */
25059 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25065 /* Look for an identifier. */
25066 identifier
= cp_parser_identifier (parser
);
25067 /* If we failed, stop. */
25068 if (identifier
== error_mark_node
)
25070 /* Declare it as a label. */
25071 finish_label_decl (identifier
);
25072 /* If the next token is a `;', stop. */
25073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25075 /* Look for the `,' separating the label declarations. */
25076 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25079 /* Look for the final `;'. */
25080 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25083 // -------------------------------------------------------------------------- //
25086 // Parse a requires clause.
25088 // requires-clause:
25089 // 'requires' logical-or-expression
25091 // The required logical-or-expression must be a constant expression. Note
25092 // that we don't check that the expression is constepxr here. We defer until
25093 // we analyze constraints and then, we only check atomic constraints.
25095 cp_parser_requires_clause (cp_parser
*parser
)
25097 // Parse the requires clause so that it is not automatically folded.
25098 ++processing_template_decl
;
25099 tree expr
= cp_parser_binary_expression (parser
, false, false,
25100 PREC_NOT_OPERATOR
, NULL
);
25101 if (check_for_bare_parameter_packs (expr
))
25102 expr
= error_mark_node
;
25103 --processing_template_decl
;
25107 // Optionally parse a requires clause:
25109 cp_parser_requires_clause_opt (cp_parser
*parser
)
25111 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25112 if (tok
->keyword
!= RID_REQUIRES
)
25114 if (!flag_concepts
&& tok
->type
== CPP_NAME
25115 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25117 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25118 "%<requires%> only available with -fconcepts");
25119 /* Parse and discard the requires-clause. */
25120 cp_lexer_consume_token (parser
->lexer
);
25121 cp_parser_requires_clause (parser
);
25125 cp_lexer_consume_token (parser
->lexer
);
25126 return cp_parser_requires_clause (parser
);
25130 /*---------------------------------------------------------------------------
25131 Requires expressions
25132 ---------------------------------------------------------------------------*/
25134 /* Parse a requires expression
25136 requirement-expression:
25137 'requires' requirement-parameter-list [opt] requirement-body */
25139 cp_parser_requires_expression (cp_parser
*parser
)
25141 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25142 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25144 /* A requires-expression shall appear only within a concept
25145 definition or a requires-clause.
25147 TODO: Implement this diagnostic correctly. */
25148 if (!processing_template_decl
)
25150 error_at (loc
, "a requires expression cannot appear outside a template");
25151 cp_parser_skip_to_end_of_statement (parser
);
25152 return error_mark_node
;
25157 /* Local parameters are delared as variables within the scope
25158 of the expression. They are not visible past the end of
25159 the expression. Expressions within the requires-expression
25160 are unevaluated. */
25161 struct scope_sentinel
25165 ++cp_unevaluated_operand
;
25166 begin_scope (sk_block
, NULL_TREE
);
25171 pop_bindings_and_leave_scope ();
25172 --cp_unevaluated_operand
;
25176 /* Parse the optional parameter list. */
25177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25179 parms
= cp_parser_requirement_parameter_list (parser
);
25180 if (parms
== error_mark_node
)
25181 return error_mark_node
;
25186 /* Parse the requirement body. */
25187 reqs
= cp_parser_requirement_body (parser
);
25188 if (reqs
== error_mark_node
)
25189 return error_mark_node
;
25192 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25194 grokparms (parms
, &parms
);
25195 return finish_requires_expr (parms
, reqs
);
25198 /* Parse a parameterized requirement.
25200 requirement-parameter-list:
25201 '(' parameter-declaration-clause ')' */
25203 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25205 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25206 return error_mark_node
;
25208 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25210 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25211 return error_mark_node
;
25216 /* Parse the body of a requirement.
25219 '{' requirement-list '}' */
25221 cp_parser_requirement_body (cp_parser
*parser
)
25223 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25224 return error_mark_node
;
25226 tree reqs
= cp_parser_requirement_list (parser
);
25228 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25229 return error_mark_node
;
25234 /* Parse a list of requirements.
25238 requirement-list ';' requirement[opt] */
25240 cp_parser_requirement_list (cp_parser
*parser
)
25242 tree result
= NULL_TREE
;
25245 tree req
= cp_parser_requirement (parser
);
25246 if (req
== error_mark_node
)
25247 return error_mark_node
;
25249 result
= tree_cons (NULL_TREE
, req
, result
);
25251 /* If we see a semi-colon, consume it. */
25252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25253 cp_lexer_consume_token (parser
->lexer
);
25255 /* Stop processing at the end of the list. */
25256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25260 /* Reverse the order of requirements so they are analyzed in
25261 declaration order. */
25262 return nreverse (result
);
25265 /* Parse a syntactic requirement or type requirement.
25269 compound-requirement
25271 nested-requirement */
25273 cp_parser_requirement (cp_parser
*parser
)
25275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25276 return cp_parser_compound_requirement (parser
);
25277 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25278 return cp_parser_type_requirement (parser
);
25279 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25280 return cp_parser_nested_requirement (parser
);
25282 return cp_parser_simple_requirement (parser
);
25285 /* Parse a simple requirement.
25287 simple-requirement:
25290 cp_parser_simple_requirement (cp_parser
*parser
)
25292 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25293 if (!expr
|| expr
== error_mark_node
)
25294 return error_mark_node
;
25296 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25297 return error_mark_node
;
25299 return finish_simple_requirement (expr
);
25302 /* Parse a type requirement
25305 nested-name-specifier [opt] required-type-name ';'
25307 required-type-name:
25309 'template' [opt] simple-template-id */
25311 cp_parser_type_requirement (cp_parser
*parser
)
25313 cp_lexer_consume_token (parser
->lexer
);
25315 // Save the scope before parsing name specifiers.
25316 tree saved_scope
= parser
->scope
;
25317 tree saved_object_scope
= parser
->object_scope
;
25318 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25319 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25320 cp_parser_nested_name_specifier_opt (parser
,
25321 /*typename_keyword_p=*/true,
25322 /*check_dependency_p=*/false,
25324 /*is_declaration=*/false);
25327 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25329 cp_lexer_consume_token (parser
->lexer
);
25330 type
= cp_parser_template_id (parser
,
25331 /*template_keyword_p=*/true,
25332 /*check_dependency=*/false,
25333 /*tag_type=*/none_type
,
25334 /*is_declaration=*/false);
25335 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25336 /*complain=*/tf_error
);
25339 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25341 if (TREE_CODE (type
) == TYPE_DECL
)
25342 type
= TREE_TYPE (type
);
25344 parser
->scope
= saved_scope
;
25345 parser
->object_scope
= saved_object_scope
;
25346 parser
->qualifying_scope
= saved_qualifying_scope
;
25348 if (type
== error_mark_node
)
25349 cp_parser_skip_to_end_of_statement (parser
);
25351 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25352 return error_mark_node
;
25353 if (type
== error_mark_node
)
25354 return error_mark_node
;
25356 return finish_type_requirement (type
);
25359 /* Parse a compound requirement
25361 compound-requirement:
25362 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25364 cp_parser_compound_requirement (cp_parser
*parser
)
25366 /* Parse an expression enclosed in '{ }'s. */
25367 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25368 return error_mark_node
;
25370 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25371 if (!expr
|| expr
== error_mark_node
)
25372 return error_mark_node
;
25374 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25375 return error_mark_node
;
25377 /* Parse the optional noexcept. */
25378 bool noexcept_p
= false;
25379 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25381 cp_lexer_consume_token (parser
->lexer
);
25385 /* Parse the optional trailing return type. */
25386 tree type
= NULL_TREE
;
25387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25389 cp_lexer_consume_token (parser
->lexer
);
25390 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25391 parser
->in_result_type_constraint_p
= true;
25392 type
= cp_parser_trailing_type_id (parser
);
25393 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25394 if (type
== error_mark_node
)
25395 return error_mark_node
;
25398 return finish_compound_requirement (expr
, type
, noexcept_p
);
25401 /* Parse a nested requirement. This is the same as a requires clause.
25403 nested-requirement:
25406 cp_parser_nested_requirement (cp_parser
*parser
)
25408 cp_lexer_consume_token (parser
->lexer
);
25409 tree req
= cp_parser_requires_clause (parser
);
25410 if (req
== error_mark_node
)
25411 return error_mark_node
;
25412 return finish_nested_requirement (req
);
25415 /* Support Functions */
25417 /* Return the appropriate prefer_type argument for lookup_name_real based on
25418 tag_type and template_mem_access. */
25421 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25423 /* DR 141: When looking in the current enclosing context for a template-name
25424 after -> or ., only consider class templates. */
25425 if (template_mem_access
)
25429 case none_type
: return 0; // No preference.
25430 case scope_type
: return 1; // Type or namespace.
25431 default: return 2; // Type only.
25435 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25436 NAME should have one of the representations used for an
25437 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25438 is returned. If PARSER->SCOPE is a dependent type, then a
25439 SCOPE_REF is returned.
25441 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25442 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25443 was formed. Abstractly, such entities should not be passed to this
25444 function, because they do not need to be looked up, but it is
25445 simpler to check for this special case here, rather than at the
25448 In cases not explicitly covered above, this function returns a
25449 DECL, OVERLOAD, or baselink representing the result of the lookup.
25450 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25453 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25454 (e.g., "struct") that was used. In that case bindings that do not
25455 refer to types are ignored.
25457 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25460 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25463 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25466 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25467 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25468 NULL_TREE otherwise. */
25471 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25472 enum tag_types tag_type
,
25475 bool check_dependency
,
25476 tree
*ambiguous_decls
,
25477 location_t name_location
)
25480 tree object_type
= parser
->context
->object_type
;
25482 /* Assume that the lookup will be unambiguous. */
25483 if (ambiguous_decls
)
25484 *ambiguous_decls
= NULL_TREE
;
25486 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25487 no longer valid. Note that if we are parsing tentatively, and
25488 the parse fails, OBJECT_TYPE will be automatically restored. */
25489 parser
->context
->object_type
= NULL_TREE
;
25491 if (name
== error_mark_node
)
25492 return error_mark_node
;
25494 /* A template-id has already been resolved; there is no lookup to
25496 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25498 if (BASELINK_P (name
))
25500 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25501 == TEMPLATE_ID_EXPR
);
25505 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25506 it should already have been checked to make sure that the name
25507 used matches the type being destroyed. */
25508 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25512 /* Figure out to which type this destructor applies. */
25514 type
= parser
->scope
;
25515 else if (object_type
)
25516 type
= object_type
;
25518 type
= current_class_type
;
25519 /* If that's not a class type, there is no destructor. */
25520 if (!type
|| !CLASS_TYPE_P (type
))
25521 return error_mark_node
;
25522 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25523 lazily_declare_fn (sfk_destructor
, type
);
25524 if (!CLASSTYPE_DESTRUCTORS (type
))
25525 return error_mark_node
;
25526 /* If it was a class type, return the destructor. */
25527 return CLASSTYPE_DESTRUCTORS (type
);
25530 /* By this point, the NAME should be an ordinary identifier. If
25531 the id-expression was a qualified name, the qualifying scope is
25532 stored in PARSER->SCOPE at this point. */
25533 gcc_assert (identifier_p (name
));
25535 /* Perform the lookup. */
25540 if (parser
->scope
== error_mark_node
)
25541 return error_mark_node
;
25543 /* If the SCOPE is dependent, the lookup must be deferred until
25544 the template is instantiated -- unless we are explicitly
25545 looking up names in uninstantiated templates. Even then, we
25546 cannot look up the name if the scope is not a class type; it
25547 might, for example, be a template type parameter. */
25548 dependent_p
= (TYPE_P (parser
->scope
)
25549 && dependent_scope_p (parser
->scope
));
25550 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25552 /* Defer lookup. */
25553 decl
= error_mark_node
;
25556 tree pushed_scope
= NULL_TREE
;
25558 /* If PARSER->SCOPE is a dependent type, then it must be a
25559 class type, and we must not be checking dependencies;
25560 otherwise, we would have processed this lookup above. So
25561 that PARSER->SCOPE is not considered a dependent base by
25562 lookup_member, we must enter the scope here. */
25564 pushed_scope
= push_scope (parser
->scope
);
25566 /* If the PARSER->SCOPE is a template specialization, it
25567 may be instantiated during name lookup. In that case,
25568 errors may be issued. Even if we rollback the current
25569 tentative parse, those errors are valid. */
25570 decl
= lookup_qualified_name (parser
->scope
, name
,
25571 prefer_type_arg (tag_type
),
25572 /*complain=*/true);
25574 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25575 lookup result and the nested-name-specifier nominates a class C:
25576 * if the name specified after the nested-name-specifier, when
25577 looked up in C, is the injected-class-name of C (Clause 9), or
25578 * if the name specified after the nested-name-specifier is the
25579 same as the identifier or the simple-template-id's template-
25580 name in the last component of the nested-name-specifier,
25581 the name is instead considered to name the constructor of
25582 class C. [ Note: for example, the constructor is not an
25583 acceptable lookup result in an elaborated-type-specifier so
25584 the constructor would not be used in place of the
25585 injected-class-name. --end note ] Such a constructor name
25586 shall be used only in the declarator-id of a declaration that
25587 names a constructor or in a using-declaration. */
25588 if (tag_type
== none_type
25589 && DECL_SELF_REFERENCE_P (decl
)
25590 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25591 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25592 prefer_type_arg (tag_type
),
25593 /*complain=*/true);
25595 /* If we have a single function from a using decl, pull it out. */
25596 if (TREE_CODE (decl
) == OVERLOAD
25597 && !really_overloaded_fn (decl
))
25598 decl
= OVL_FUNCTION (decl
);
25601 pop_scope (pushed_scope
);
25604 /* If the scope is a dependent type and either we deferred lookup or
25605 we did lookup but didn't find the name, rememeber the name. */
25606 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25607 && dependent_type_p (parser
->scope
))
25613 /* The resolution to Core Issue 180 says that `struct
25614 A::B' should be considered a type-name, even if `A'
25616 type
= make_typename_type (parser
->scope
, name
, tag_type
,
25617 /*complain=*/tf_error
);
25618 if (type
!= error_mark_node
)
25619 decl
= TYPE_NAME (type
);
25621 else if (is_template
25622 && (cp_parser_next_token_ends_template_argument_p (parser
)
25623 || cp_lexer_next_token_is (parser
->lexer
,
25625 decl
= make_unbound_class_template (parser
->scope
,
25627 /*complain=*/tf_error
);
25629 decl
= build_qualified_name (/*type=*/NULL_TREE
,
25630 parser
->scope
, name
,
25633 parser
->qualifying_scope
= parser
->scope
;
25634 parser
->object_scope
= NULL_TREE
;
25636 else if (object_type
)
25638 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25639 OBJECT_TYPE is not a class. */
25640 if (CLASS_TYPE_P (object_type
))
25641 /* If the OBJECT_TYPE is a template specialization, it may
25642 be instantiated during name lookup. In that case, errors
25643 may be issued. Even if we rollback the current tentative
25644 parse, those errors are valid. */
25645 decl
= lookup_member (object_type
,
25648 prefer_type_arg (tag_type
),
25649 tf_warning_or_error
);
25654 /* Look it up in the enclosing context. DR 141: When looking for a
25655 template-name after -> or ., only consider class templates. */
25656 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
25658 /*block_p=*/true, is_namespace
, 0);
25659 if (object_type
== unknown_type_node
)
25660 /* The object is type-dependent, so we can't look anything up; we used
25661 this to get the DR 141 behavior. */
25662 object_type
= NULL_TREE
;
25663 parser
->object_scope
= object_type
;
25664 parser
->qualifying_scope
= NULL_TREE
;
25668 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
25670 /*block_p=*/true, is_namespace
, 0);
25671 parser
->qualifying_scope
= NULL_TREE
;
25672 parser
->object_scope
= NULL_TREE
;
25675 /* If the lookup failed, let our caller know. */
25676 if (!decl
|| decl
== error_mark_node
)
25677 return error_mark_node
;
25679 /* Pull out the template from an injected-class-name (or multiple). */
25681 decl
= maybe_get_template_decl_from_type_decl (decl
);
25683 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25684 if (TREE_CODE (decl
) == TREE_LIST
)
25686 if (ambiguous_decls
)
25687 *ambiguous_decls
= decl
;
25688 /* The error message we have to print is too complicated for
25689 cp_parser_error, so we incorporate its actions directly. */
25690 if (!cp_parser_simulate_error (parser
))
25692 error_at (name_location
, "reference to %qD is ambiguous",
25694 print_candidates (decl
);
25696 return error_mark_node
;
25699 gcc_assert (DECL_P (decl
)
25700 || TREE_CODE (decl
) == OVERLOAD
25701 || TREE_CODE (decl
) == SCOPE_REF
25702 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
25703 || BASELINK_P (decl
));
25705 /* If we have resolved the name of a member declaration, check to
25706 see if the declaration is accessible. When the name resolves to
25707 set of overloaded functions, accessibility is checked when
25708 overload resolution is done.
25710 During an explicit instantiation, access is not checked at all,
25711 as per [temp.explicit]. */
25713 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
25715 maybe_record_typedef_use (decl
);
25717 return cp_expr (decl
, name_location
);
25720 /* Like cp_parser_lookup_name, but for use in the typical case where
25721 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25722 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25725 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
25727 return cp_parser_lookup_name (parser
, name
,
25729 /*is_template=*/false,
25730 /*is_namespace=*/false,
25731 /*check_dependency=*/true,
25732 /*ambiguous_decls=*/NULL
,
25736 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25737 the current context, return the TYPE_DECL. If TAG_NAME_P is
25738 true, the DECL indicates the class being defined in a class-head,
25739 or declared in an elaborated-type-specifier.
25741 Otherwise, return DECL. */
25744 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
25746 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25747 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25750 template <typename T> struct B;
25753 template <typename T> struct A::B {};
25755 Similarly, in an elaborated-type-specifier:
25757 namespace N { struct X{}; }
25760 template <typename T> friend struct N::X;
25763 However, if the DECL refers to a class type, and we are in
25764 the scope of the class, then the name lookup automatically
25765 finds the TYPE_DECL created by build_self_reference rather
25766 than a TEMPLATE_DECL. For example, in:
25768 template <class T> struct S {
25772 there is no need to handle such case. */
25774 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
25775 return DECL_TEMPLATE_RESULT (decl
);
25780 /* If too many, or too few, template-parameter lists apply to the
25781 declarator, issue an error message. Returns TRUE if all went well,
25782 and FALSE otherwise. */
25785 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
25786 cp_declarator
*declarator
,
25787 location_t declarator_location
)
25789 switch (declarator
->kind
)
25793 unsigned num_templates
= 0;
25794 tree scope
= declarator
->u
.id
.qualifying_scope
;
25797 num_templates
= num_template_headers_for_class (scope
);
25798 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
25799 == TEMPLATE_ID_EXPR
)
25800 /* If the DECLARATOR has the form `X<y>' then it uses one
25801 additional level of template parameters. */
25804 return cp_parser_check_template_parameters
25805 (parser
, num_templates
, declarator_location
, declarator
);
25811 case cdk_reference
:
25813 return (cp_parser_check_declarator_template_parameters
25814 (parser
, declarator
->declarator
, declarator_location
));
25821 gcc_unreachable ();
25826 /* NUM_TEMPLATES were used in the current declaration. If that is
25827 invalid, return FALSE and issue an error messages. Otherwise,
25828 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25829 declarator and we can print more accurate diagnostics. */
25832 cp_parser_check_template_parameters (cp_parser
* parser
,
25833 unsigned num_templates
,
25834 location_t location
,
25835 cp_declarator
*declarator
)
25837 /* If there are the same number of template classes and parameter
25838 lists, that's OK. */
25839 if (parser
->num_template_parameter_lists
== num_templates
)
25841 /* If there are more, but only one more, then we are referring to a
25842 member template. That's OK too. */
25843 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
25845 /* If there are more template classes than parameter lists, we have
25848 template <class T> void S<T>::R<T>::f (); */
25849 if (parser
->num_template_parameter_lists
< num_templates
)
25851 if (declarator
&& !current_function_decl
)
25852 error_at (location
, "specializing member %<%T::%E%> "
25853 "requires %<template<>%> syntax",
25854 declarator
->u
.id
.qualifying_scope
,
25855 declarator
->u
.id
.unqualified_name
);
25856 else if (declarator
)
25857 error_at (location
, "invalid declaration of %<%T::%E%>",
25858 declarator
->u
.id
.qualifying_scope
,
25859 declarator
->u
.id
.unqualified_name
);
25861 error_at (location
, "too few template-parameter-lists");
25864 /* Otherwise, there are too many template parameter lists. We have
25867 template <class T> template <class U> void S::f(); */
25868 error_at (location
, "too many template-parameter-lists");
25872 /* Parse an optional `::' token indicating that the following name is
25873 from the global namespace. If so, PARSER->SCOPE is set to the
25874 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25875 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25876 Returns the new value of PARSER->SCOPE, if the `::' token is
25877 present, and NULL_TREE otherwise. */
25880 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
25884 /* Peek at the next token. */
25885 token
= cp_lexer_peek_token (parser
->lexer
);
25886 /* If we're looking at a `::' token then we're starting from the
25887 global namespace, not our current location. */
25888 if (token
->type
== CPP_SCOPE
)
25890 /* Consume the `::' token. */
25891 cp_lexer_consume_token (parser
->lexer
);
25892 /* Set the SCOPE so that we know where to start the lookup. */
25893 parser
->scope
= global_namespace
;
25894 parser
->qualifying_scope
= global_namespace
;
25895 parser
->object_scope
= NULL_TREE
;
25897 return parser
->scope
;
25899 else if (!current_scope_valid_p
)
25901 parser
->scope
= NULL_TREE
;
25902 parser
->qualifying_scope
= NULL_TREE
;
25903 parser
->object_scope
= NULL_TREE
;
25909 /* Returns TRUE if the upcoming token sequence is the start of a
25910 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25911 declarator is preceded by the `friend' specifier. */
25914 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
25916 bool constructor_p
;
25917 bool outside_class_specifier_p
;
25918 tree nested_name_specifier
;
25919 cp_token
*next_token
;
25921 /* The common case is that this is not a constructor declarator, so
25922 try to avoid doing lots of work if at all possible. It's not
25923 valid declare a constructor at function scope. */
25924 if (parser
->in_function_body
)
25926 /* And only certain tokens can begin a constructor declarator. */
25927 next_token
= cp_lexer_peek_token (parser
->lexer
);
25928 if (next_token
->type
!= CPP_NAME
25929 && next_token
->type
!= CPP_SCOPE
25930 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
25931 && next_token
->type
!= CPP_TEMPLATE_ID
)
25934 /* Parse tentatively; we are going to roll back all of the tokens
25936 cp_parser_parse_tentatively (parser
);
25937 /* Assume that we are looking at a constructor declarator. */
25938 constructor_p
= true;
25940 /* Look for the optional `::' operator. */
25941 cp_parser_global_scope_opt (parser
,
25942 /*current_scope_valid_p=*/false);
25943 /* Look for the nested-name-specifier. */
25944 nested_name_specifier
25945 = (cp_parser_nested_name_specifier_opt (parser
,
25946 /*typename_keyword_p=*/false,
25947 /*check_dependency_p=*/false,
25949 /*is_declaration=*/false));
25951 outside_class_specifier_p
= (!at_class_scope_p ()
25952 || !TYPE_BEING_DEFINED (current_class_type
)
25955 /* Outside of a class-specifier, there must be a
25956 nested-name-specifier. Except in C++17 mode, where we
25957 might be declaring a guiding declaration. */
25958 if (!nested_name_specifier
&& outside_class_specifier_p
25959 && cxx_dialect
< cxx1z
)
25960 constructor_p
= false;
25961 else if (nested_name_specifier
== error_mark_node
)
25962 constructor_p
= false;
25964 /* If we have a class scope, this is easy; DR 147 says that S::S always
25965 names the constructor, and no other qualified name could. */
25966 if (constructor_p
&& nested_name_specifier
25967 && CLASS_TYPE_P (nested_name_specifier
))
25969 tree id
= cp_parser_unqualified_id (parser
,
25970 /*template_keyword_p=*/false,
25971 /*check_dependency_p=*/false,
25972 /*declarator_p=*/true,
25973 /*optional_p=*/false);
25974 if (is_overloaded_fn (id
))
25975 id
= DECL_NAME (get_first_fn (id
));
25976 if (!constructor_name_p (id
, nested_name_specifier
))
25977 constructor_p
= false;
25979 /* If we still think that this might be a constructor-declarator,
25980 look for a class-name. */
25981 else if (constructor_p
)
25985 template <typename T> struct S {
25989 we must recognize that the nested `S' names a class. */
25990 if (cxx_dialect
>= cxx1z
)
25991 cp_parser_parse_tentatively (parser
);
25994 type_decl
= cp_parser_class_name (parser
,
25995 /*typename_keyword_p=*/false,
25996 /*template_keyword_p=*/false,
25998 /*check_dependency_p=*/false,
25999 /*class_head_p=*/false,
26000 /*is_declaration=*/false);
26002 if (cxx_dialect
>= cxx1z
26003 && !cp_parser_parse_definitely (parser
))
26005 type_decl
= NULL_TREE
;
26006 tree tmpl
= cp_parser_template_name (parser
,
26007 /*template_keyword*/false,
26008 /*check_dependency_p*/false,
26009 /*is_declaration*/false,
26011 /*is_identifier*/NULL
);
26012 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26013 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26014 /* It's a deduction guide, return true. */;
26016 cp_parser_simulate_error (parser
);
26019 /* If there was no class-name, then this is not a constructor.
26020 Otherwise, if we are in a class-specifier and we aren't
26021 handling a friend declaration, check that its type matches
26022 current_class_type (c++/38313). Note: error_mark_node
26023 is left alone for error recovery purposes. */
26024 constructor_p
= (!cp_parser_error_occurred (parser
)
26025 && (outside_class_specifier_p
26026 || type_decl
== NULL_TREE
26027 || type_decl
== error_mark_node
26028 || same_type_p (current_class_type
,
26029 TREE_TYPE (type_decl
))));
26031 /* If we're still considering a constructor, we have to see a `(',
26032 to begin the parameter-declaration-clause, followed by either a
26033 `)', an `...', or a decl-specifier. We need to check for a
26034 type-specifier to avoid being fooled into thinking that:
26038 is a constructor. (It is actually a function named `f' that
26039 takes one parameter (of type `int') and returns a value of type
26042 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26043 constructor_p
= false;
26046 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26047 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26048 /* A parameter declaration begins with a decl-specifier,
26049 which is either the "attribute" keyword, a storage class
26050 specifier, or (usually) a type-specifier. */
26051 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26054 tree pushed_scope
= NULL_TREE
;
26055 unsigned saved_num_template_parameter_lists
;
26057 /* Names appearing in the type-specifier should be looked up
26058 in the scope of the class. */
26059 if (current_class_type
)
26061 else if (type_decl
)
26063 type
= TREE_TYPE (type_decl
);
26064 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26066 type
= resolve_typename_type (type
,
26067 /*only_current_p=*/false);
26068 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26070 cp_parser_abort_tentative_parse (parser
);
26074 pushed_scope
= push_scope (type
);
26077 /* Inside the constructor parameter list, surrounding
26078 template-parameter-lists do not apply. */
26079 saved_num_template_parameter_lists
26080 = parser
->num_template_parameter_lists
;
26081 parser
->num_template_parameter_lists
= 0;
26083 /* Look for the type-specifier. */
26084 cp_parser_type_specifier (parser
,
26085 CP_PARSER_FLAGS_NONE
,
26086 /*decl_specs=*/NULL
,
26087 /*is_declarator=*/true,
26088 /*declares_class_or_enum=*/NULL
,
26089 /*is_cv_qualifier=*/NULL
);
26091 parser
->num_template_parameter_lists
26092 = saved_num_template_parameter_lists
;
26094 /* Leave the scope of the class. */
26096 pop_scope (pushed_scope
);
26098 constructor_p
= !cp_parser_error_occurred (parser
);
26102 /* We did not really want to consume any tokens. */
26103 cp_parser_abort_tentative_parse (parser
);
26105 return constructor_p
;
26108 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26109 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26110 they must be performed once we are in the scope of the function.
26112 Returns the function defined. */
26115 cp_parser_function_definition_from_specifiers_and_declarator
26116 (cp_parser
* parser
,
26117 cp_decl_specifier_seq
*decl_specifiers
,
26119 const cp_declarator
*declarator
)
26124 /* Begin the function-definition. */
26125 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26127 /* The things we're about to see are not directly qualified by any
26128 template headers we've seen thus far. */
26129 reset_specialization ();
26131 /* If there were names looked up in the decl-specifier-seq that we
26132 did not check, check them now. We must wait until we are in the
26133 scope of the function to perform the checks, since the function
26134 might be a friend. */
26135 perform_deferred_access_checks (tf_warning_or_error
);
26139 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26140 parser
->omp_declare_simd
= NULL
;
26141 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26142 parser
->oacc_routine
= NULL
;
26147 /* Skip the entire function. */
26148 cp_parser_skip_to_end_of_block_or_statement (parser
);
26149 fn
= error_mark_node
;
26151 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26153 /* Seen already, skip it. An error message has already been output. */
26154 cp_parser_skip_to_end_of_block_or_statement (parser
);
26155 fn
= current_function_decl
;
26156 current_function_decl
= NULL_TREE
;
26157 /* If this is a function from a class, pop the nested class. */
26158 if (current_class_name
)
26159 pop_nested_class ();
26164 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26165 tv
= TV_PARSE_INLINE
;
26167 tv
= TV_PARSE_FUNC
;
26169 fn
= cp_parser_function_definition_after_declarator (parser
,
26170 /*inline_p=*/false);
26177 /* Parse the part of a function-definition that follows the
26178 declarator. INLINE_P is TRUE iff this function is an inline
26179 function defined within a class-specifier.
26181 Returns the function defined. */
26184 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26188 bool ctor_initializer_p
= false;
26189 bool saved_in_unbraced_linkage_specification_p
;
26190 bool saved_in_function_body
;
26191 unsigned saved_num_template_parameter_lists
;
26193 bool fully_implicit_function_template_p
26194 = parser
->fully_implicit_function_template_p
;
26195 parser
->fully_implicit_function_template_p
= false;
26196 tree implicit_template_parms
26197 = parser
->implicit_template_parms
;
26198 parser
->implicit_template_parms
= 0;
26199 cp_binding_level
* implicit_template_scope
26200 = parser
->implicit_template_scope
;
26201 parser
->implicit_template_scope
= 0;
26203 saved_in_function_body
= parser
->in_function_body
;
26204 parser
->in_function_body
= true;
26205 /* If the next token is `return', then the code may be trying to
26206 make use of the "named return value" extension that G++ used to
26208 token
= cp_lexer_peek_token (parser
->lexer
);
26209 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26211 /* Consume the `return' keyword. */
26212 cp_lexer_consume_token (parser
->lexer
);
26213 /* Look for the identifier that indicates what value is to be
26215 cp_parser_identifier (parser
);
26216 /* Issue an error message. */
26217 error_at (token
->location
,
26218 "named return values are no longer supported");
26219 /* Skip tokens until we reach the start of the function body. */
26222 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26223 if (token
->type
== CPP_OPEN_BRACE
26224 || token
->type
== CPP_EOF
26225 || token
->type
== CPP_PRAGMA_EOL
)
26227 cp_lexer_consume_token (parser
->lexer
);
26230 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26231 anything declared inside `f'. */
26232 saved_in_unbraced_linkage_specification_p
26233 = parser
->in_unbraced_linkage_specification_p
;
26234 parser
->in_unbraced_linkage_specification_p
= false;
26235 /* Inside the function, surrounding template-parameter-lists do not
26237 saved_num_template_parameter_lists
26238 = parser
->num_template_parameter_lists
;
26239 parser
->num_template_parameter_lists
= 0;
26241 start_lambda_scope (current_function_decl
);
26243 /* If the next token is `try', `__transaction_atomic', or
26244 `__transaction_relaxed`, then we are looking at either function-try-block
26245 or function-transaction-block. Note that all of these include the
26247 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26248 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26249 RID_TRANSACTION_ATOMIC
);
26250 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26251 RID_TRANSACTION_RELAXED
))
26252 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26253 RID_TRANSACTION_RELAXED
);
26254 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26255 ctor_initializer_p
= cp_parser_function_try_block (parser
);
26257 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
26258 (parser
, /*in_function_try_block=*/false);
26260 finish_lambda_scope ();
26262 /* Finish the function. */
26263 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
26264 (inline_p
? 2 : 0));
26265 /* Generate code for it, if necessary. */
26266 expand_or_defer_fn (fn
);
26267 /* Restore the saved values. */
26268 parser
->in_unbraced_linkage_specification_p
26269 = saved_in_unbraced_linkage_specification_p
;
26270 parser
->num_template_parameter_lists
26271 = saved_num_template_parameter_lists
;
26272 parser
->in_function_body
= saved_in_function_body
;
26274 parser
->fully_implicit_function_template_p
26275 = fully_implicit_function_template_p
;
26276 parser
->implicit_template_parms
26277 = implicit_template_parms
;
26278 parser
->implicit_template_scope
26279 = implicit_template_scope
;
26281 if (parser
->fully_implicit_function_template_p
)
26282 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26287 /* Parse a template-declaration body (following argument list). */
26290 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26291 tree parameter_list
,
26294 tree decl
= NULL_TREE
;
26295 bool friend_p
= false;
26297 /* We just processed one more parameter list. */
26298 ++parser
->num_template_parameter_lists
;
26300 /* Get the deferred access checks from the parameter list. These
26301 will be checked once we know what is being declared, as for a
26302 member template the checks must be performed in the scope of the
26303 class containing the member. */
26304 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26306 /* Tentatively parse for a new template parameter list, which can either be
26307 the template keyword or a template introduction. */
26308 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26310 else if (cxx_dialect
>= cxx11
26311 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26312 decl
= cp_parser_alias_declaration (parser
);
26315 /* There are no access checks when parsing a template, as we do not
26316 know if a specialization will be a friend. */
26317 push_deferring_access_checks (dk_no_check
);
26318 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26319 decl
= cp_parser_single_declaration (parser
,
26322 /*explicit_specialization_p=*/false,
26324 pop_deferring_access_checks ();
26326 /* If this is a member template declaration, let the front
26328 if (member_p
&& !friend_p
&& decl
)
26330 if (TREE_CODE (decl
) == TYPE_DECL
)
26331 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26333 decl
= finish_member_template_decl (decl
);
26335 else if (friend_p
&& decl
26336 && DECL_DECLARES_TYPE_P (decl
))
26337 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26338 /*complain=*/true);
26340 /* We are done with the current parameter list. */
26341 --parser
->num_template_parameter_lists
;
26343 pop_deferring_access_checks ();
26346 finish_template_decl (parameter_list
);
26348 /* Check the template arguments for a literal operator template. */
26350 && DECL_DECLARES_FUNCTION_P (decl
)
26351 && UDLIT_OPER_P (DECL_NAME (decl
)))
26354 if (parameter_list
== NULL_TREE
)
26358 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26359 if (num_parms
== 1)
26361 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26362 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26363 if (TREE_TYPE (parm
) != char_type_node
26364 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26367 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26369 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26370 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26371 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26372 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26373 if (parm
== error_mark_node
26374 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26375 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26383 if (cxx_dialect
>= cxx14
)
26384 error ("literal operator template %qD has invalid parameter list."
26385 " Expected non-type template argument pack <char...>"
26386 " or <typename CharT, CharT...>",
26389 error ("literal operator template %qD has invalid parameter list."
26390 " Expected non-type template argument pack <char...>",
26395 /* Register member declarations. */
26396 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26397 finish_member_declaration (decl
);
26398 /* If DECL is a function template, we must return to parse it later.
26399 (Even though there is no definition, there might be default
26400 arguments that need handling.) */
26401 if (member_p
&& decl
26402 && DECL_DECLARES_FUNCTION_P (decl
))
26403 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26406 /* Parse a template introduction header for a template-declaration. Returns
26407 false if tentative parse fails. */
26410 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26412 cp_parser_parse_tentatively (parser
);
26414 tree saved_scope
= parser
->scope
;
26415 tree saved_object_scope
= parser
->object_scope
;
26416 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26418 /* Look for the optional `::' operator. */
26419 cp_parser_global_scope_opt (parser
,
26420 /*current_scope_valid_p=*/false);
26421 /* Look for the nested-name-specifier. */
26422 cp_parser_nested_name_specifier_opt (parser
,
26423 /*typename_keyword_p=*/false,
26424 /*check_dependency_p=*/true,
26426 /*is_declaration=*/false);
26428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26429 tree concept_name
= cp_parser_identifier (parser
);
26431 /* Look up the concept for which we will be matching
26432 template parameters. */
26433 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26435 parser
->scope
= saved_scope
;
26436 parser
->object_scope
= saved_object_scope
;
26437 parser
->qualifying_scope
= saved_qualifying_scope
;
26439 if (concept_name
== error_mark_node
)
26440 cp_parser_simulate_error (parser
);
26442 /* Look for opening brace for introduction. */
26443 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
26445 if (!cp_parser_parse_definitely (parser
))
26448 push_deferring_access_checks (dk_deferred
);
26450 /* Build vector of placeholder parameters and grab
26451 matching identifiers. */
26452 tree introduction_list
= cp_parser_introduction_list (parser
);
26454 /* The introduction-list shall not be empty. */
26455 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26458 error ("empty introduction-list");
26462 /* Look for closing brace for introduction. */
26463 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
26466 if (tmpl_decl
== error_mark_node
)
26468 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26473 /* Build and associate the constraint. */
26474 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26475 if (parms
&& parms
!= error_mark_node
)
26477 cp_parser_template_declaration_after_parameters (parser
, parms
,
26482 error_at (token
->location
, "no matching concept for template-introduction");
26486 /* Parse a normal template-declaration following the template keyword. */
26489 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26491 tree parameter_list
;
26492 bool need_lang_pop
;
26493 location_t location
= input_location
;
26495 /* Look for the `<' token. */
26496 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26498 if (at_class_scope_p () && current_function_decl
)
26500 /* 14.5.2.2 [temp.mem]
26502 A local class shall not have member templates. */
26503 error_at (location
,
26504 "invalid declaration of member template in local class");
26505 cp_parser_skip_to_end_of_block_or_statement (parser
);
26510 A template ... shall not have C linkage. */
26511 if (current_lang_name
== lang_name_c
)
26513 error_at (location
, "template with C linkage");
26514 /* Give it C++ linkage to avoid confusing other parts of the
26516 push_lang_context (lang_name_cplusplus
);
26517 need_lang_pop
= true;
26520 need_lang_pop
= false;
26522 /* We cannot perform access checks on the template parameter
26523 declarations until we know what is being declared, just as we
26524 cannot check the decl-specifier list. */
26525 push_deferring_access_checks (dk_deferred
);
26527 /* If the next token is `>', then we have an invalid
26528 specialization. Rather than complain about an invalid template
26529 parameter, issue an error message here. */
26530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26532 cp_parser_error (parser
, "invalid explicit specialization");
26533 begin_specialization ();
26534 parameter_list
= NULL_TREE
;
26538 /* Parse the template parameters. */
26539 parameter_list
= cp_parser_template_parameter_list (parser
);
26542 /* Look for the `>'. */
26543 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26545 /* Manage template requirements */
26548 tree reqs
= get_shorthand_constraints (current_template_parms
);
26549 if (tree r
= cp_parser_requires_clause_opt (parser
))
26550 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26551 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26554 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26557 /* For the erroneous case of a template with C linkage, we pushed an
26558 implicit C++ linkage scope; exit that scope now. */
26560 pop_lang_context ();
26563 /* Parse a template-declaration, assuming that the `export' (and
26564 `extern') keywords, if present, has already been scanned. MEMBER_P
26565 is as for cp_parser_template_declaration. */
26568 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26572 cp_lexer_consume_token (parser
->lexer
);
26573 cp_parser_explicit_template_declaration (parser
, member_p
);
26576 else if (flag_concepts
)
26577 return cp_parser_template_introduction (parser
, member_p
);
26582 /* Perform the deferred access checks from a template-parameter-list.
26583 CHECKS is a TREE_LIST of access checks, as returned by
26584 get_deferred_access_checks. */
26587 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26589 ++processing_template_parmlist
;
26590 perform_access_checks (checks
, tf_warning_or_error
);
26591 --processing_template_parmlist
;
26594 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26595 `function-definition' sequence that follows a template header.
26596 If MEMBER_P is true, this declaration appears in a class scope.
26598 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26599 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26602 cp_parser_single_declaration (cp_parser
* parser
,
26603 vec
<deferred_access_check
, va_gc
> *checks
,
26605 bool explicit_specialization_p
,
26608 int declares_class_or_enum
;
26609 tree decl
= NULL_TREE
;
26610 cp_decl_specifier_seq decl_specifiers
;
26611 bool function_definition_p
= false;
26612 cp_token
*decl_spec_token_start
;
26614 /* This function is only used when processing a template
26616 gcc_assert (innermost_scope_kind () == sk_template_parms
26617 || innermost_scope_kind () == sk_template_spec
);
26619 /* Defer access checks until we know what is being declared. */
26620 push_deferring_access_checks (dk_deferred
);
26622 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26624 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
26625 cp_parser_decl_specifier_seq (parser
,
26626 CP_PARSER_FLAGS_OPTIONAL
,
26628 &declares_class_or_enum
);
26630 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
26632 /* There are no template typedefs. */
26633 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
26635 error_at (decl_spec_token_start
->location
,
26636 "template declaration of %<typedef%>");
26637 decl
= error_mark_node
;
26640 /* Gather up the access checks that occurred the
26641 decl-specifier-seq. */
26642 stop_deferring_access_checks ();
26644 /* Check for the declaration of a template class. */
26645 if (declares_class_or_enum
)
26647 if (cp_parser_declares_only_class_p (parser
)
26648 || (declares_class_or_enum
& 2))
26650 // If this is a declaration, but not a definition, associate
26651 // any constraints with the type declaration. Constraints
26652 // are associated with definitions in cp_parser_class_specifier.
26653 if (declares_class_or_enum
== 1)
26654 associate_classtype_constraints (decl_specifiers
.type
);
26656 decl
= shadow_tag (&decl_specifiers
);
26661 friend template <typename T> struct A<T>::B;
26664 A<T>::B will be represented by a TYPENAME_TYPE, and
26665 therefore not recognized by shadow_tag. */
26666 if (friend_p
&& *friend_p
26668 && decl_specifiers
.type
26669 && TYPE_P (decl_specifiers
.type
))
26670 decl
= decl_specifiers
.type
;
26672 if (decl
&& decl
!= error_mark_node
)
26673 decl
= TYPE_NAME (decl
);
26675 decl
= error_mark_node
;
26677 /* Perform access checks for template parameters. */
26678 cp_parser_perform_template_parameter_access_checks (checks
);
26680 /* Give a helpful diagnostic for
26681 template <class T> struct A { } a;
26682 if we aren't already recovering from an error. */
26683 if (!cp_parser_declares_only_class_p (parser
)
26686 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26687 "a class template declaration must not declare "
26689 cp_parser_skip_to_end_of_block_or_statement (parser
);
26695 /* Complain about missing 'typename' or other invalid type names. */
26696 if (!decl_specifiers
.any_type_specifiers_p
26697 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
26699 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26700 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26701 the rest of this declaration. */
26702 decl
= error_mark_node
;
26706 /* If it's not a template class, try for a template function. If
26707 the next token is a `;', then this declaration does not declare
26708 anything. But, if there were errors in the decl-specifiers, then
26709 the error might well have come from an attempted class-specifier.
26710 In that case, there's no need to warn about a missing declarator. */
26712 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
26713 || decl_specifiers
.type
!= error_mark_node
))
26715 decl
= cp_parser_init_declarator (parser
,
26718 /*function_definition_allowed_p=*/true,
26720 declares_class_or_enum
,
26721 &function_definition_p
,
26724 /* 7.1.1-1 [dcl.stc]
26726 A storage-class-specifier shall not be specified in an explicit
26727 specialization... */
26729 && explicit_specialization_p
26730 && decl_specifiers
.storage_class
!= sc_none
)
26732 error_at (decl_spec_token_start
->location
,
26733 "explicit template specialization cannot have a storage class");
26734 decl
= error_mark_node
;
26737 if (decl
&& VAR_P (decl
))
26738 check_template_variable (decl
);
26741 /* Look for a trailing `;' after the declaration. */
26742 if (!function_definition_p
26743 && (decl
== error_mark_node
26744 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
26745 cp_parser_skip_to_end_of_block_or_statement (parser
);
26748 pop_deferring_access_checks ();
26750 /* Clear any current qualification; whatever comes next is the start
26751 of something new. */
26752 parser
->scope
= NULL_TREE
;
26753 parser
->qualifying_scope
= NULL_TREE
;
26754 parser
->object_scope
= NULL_TREE
;
26759 /* Parse a cast-expression that is not the operand of a unary "&". */
26762 cp_parser_simple_cast_expression (cp_parser
*parser
)
26764 return cp_parser_cast_expression (parser
, /*address_p=*/false,
26765 /*cast_p=*/false, /*decltype*/false, NULL
);
26768 /* Parse a functional cast to TYPE. Returns an expression
26769 representing the cast. */
26772 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
26774 vec
<tree
, va_gc
> *vec
;
26775 tree expression_list
;
26779 location_t start_loc
= input_location
;
26782 type
= error_mark_node
;
26784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26786 cp_lexer_set_source_position (parser
->lexer
);
26787 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26788 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
26789 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
26790 if (TREE_CODE (type
) == TYPE_DECL
)
26791 type
= TREE_TYPE (type
);
26793 cast
= finish_compound_literal (type
, expression_list
,
26794 tf_warning_or_error
);
26795 /* Create a location of the form:
26798 with caret == start at the start of the type name,
26799 finishing at the closing brace. */
26800 location_t finish_loc
26801 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26802 location_t combined_loc
= make_location (start_loc
, start_loc
,
26804 cast
.set_location (combined_loc
);
26809 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
26811 /*allow_expansion_p=*/true,
26812 /*non_constant_p=*/NULL
);
26814 expression_list
= error_mark_node
;
26817 expression_list
= build_tree_list_vec (vec
);
26818 release_tree_vector (vec
);
26821 cast
= build_functional_cast (type
, expression_list
,
26822 tf_warning_or_error
);
26823 /* [expr.const]/1: In an integral constant expression "only type
26824 conversions to integral or enumeration type can be used". */
26825 if (TREE_CODE (type
) == TYPE_DECL
)
26826 type
= TREE_TYPE (type
);
26827 if (cast
!= error_mark_node
26828 && !cast_valid_in_integral_constant_expression_p (type
)
26829 && cp_parser_non_integral_constant_expression (parser
,
26831 return error_mark_node
;
26833 /* Create a location of the form:
26836 with caret == start at the start of the type name,
26837 finishing at the closing paren. */
26838 location_t finish_loc
26839 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26840 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
26841 cast
.set_location (combined_loc
);
26845 /* Save the tokens that make up the body of a member function defined
26846 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26847 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26848 specifiers applied to the declaration. Returns the FUNCTION_DECL
26849 for the member function. */
26852 cp_parser_save_member_function_body (cp_parser
* parser
,
26853 cp_decl_specifier_seq
*decl_specifiers
,
26854 cp_declarator
*declarator
,
26860 bool function_try_block
= false;
26862 /* Create the FUNCTION_DECL. */
26863 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
26864 cp_finalize_omp_declare_simd (parser
, fn
);
26865 cp_finalize_oacc_routine (parser
, fn
, true);
26866 /* If something went badly wrong, bail out now. */
26867 if (fn
== error_mark_node
)
26869 /* If there's a function-body, skip it. */
26870 if (cp_parser_token_starts_function_definition_p
26871 (cp_lexer_peek_token (parser
->lexer
)))
26872 cp_parser_skip_to_end_of_block_or_statement (parser
);
26873 return error_mark_node
;
26876 /* Remember it, if there default args to post process. */
26877 cp_parser_save_default_args (parser
, fn
);
26879 /* Save away the tokens that make up the body of the
26881 first
= parser
->lexer
->next_token
;
26883 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
26884 cp_lexer_consume_token (parser
->lexer
);
26885 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26886 RID_TRANSACTION_ATOMIC
))
26888 cp_lexer_consume_token (parser
->lexer
);
26889 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
26891 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
26892 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
26893 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
26894 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
26895 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
26897 cp_lexer_consume_token (parser
->lexer
);
26898 cp_lexer_consume_token (parser
->lexer
);
26899 cp_lexer_consume_token (parser
->lexer
);
26900 cp_lexer_consume_token (parser
->lexer
);
26901 cp_lexer_consume_token (parser
->lexer
);
26904 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26905 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
26907 cp_lexer_consume_token (parser
->lexer
);
26908 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26913 /* Handle function try blocks. */
26914 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26916 cp_lexer_consume_token (parser
->lexer
);
26917 function_try_block
= true;
26919 /* We can have braced-init-list mem-initializers before the fn body. */
26920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26922 cp_lexer_consume_token (parser
->lexer
);
26923 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
26925 /* cache_group will stop after an un-nested { } pair, too. */
26926 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26929 /* variadic mem-inits have ... after the ')'. */
26930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26931 cp_lexer_consume_token (parser
->lexer
);
26934 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26935 /* Handle function try blocks. */
26936 if (function_try_block
)
26937 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
26938 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26939 last
= parser
->lexer
->next_token
;
26941 /* Save away the inline definition; we will process it when the
26942 class is complete. */
26943 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
26944 DECL_PENDING_INLINE_P (fn
) = 1;
26946 /* We need to know that this was defined in the class, so that
26947 friend templates are handled correctly. */
26948 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
26950 /* Add FN to the queue of functions to be parsed later. */
26951 vec_safe_push (unparsed_funs_with_definitions
, fn
);
26956 /* Save the tokens that make up the in-class initializer for a non-static
26957 data member. Returns a DEFAULT_ARG. */
26960 cp_parser_save_nsdmi (cp_parser
* parser
)
26962 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
26965 /* Parse a template-argument-list, as well as the trailing ">" (but
26966 not the opening "<"). See cp_parser_template_argument_list for the
26970 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
26974 tree saved_qualifying_scope
;
26975 tree saved_object_scope
;
26976 bool saved_greater_than_is_operator_p
;
26977 int saved_unevaluated_operand
;
26978 int saved_inhibit_evaluation_warnings
;
26982 When parsing a template-id, the first non-nested `>' is taken as
26983 the end of the template-argument-list rather than a greater-than
26985 saved_greater_than_is_operator_p
26986 = parser
->greater_than_is_operator_p
;
26987 parser
->greater_than_is_operator_p
= false;
26988 /* Parsing the argument list may modify SCOPE, so we save it
26990 saved_scope
= parser
->scope
;
26991 saved_qualifying_scope
= parser
->qualifying_scope
;
26992 saved_object_scope
= parser
->object_scope
;
26993 /* We need to evaluate the template arguments, even though this
26994 template-id may be nested within a "sizeof". */
26995 saved_unevaluated_operand
= cp_unevaluated_operand
;
26996 cp_unevaluated_operand
= 0;
26997 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26998 c_inhibit_evaluation_warnings
= 0;
26999 /* Parse the template-argument-list itself. */
27000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27001 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27002 arguments
= NULL_TREE
;
27004 arguments
= cp_parser_template_argument_list (parser
);
27005 /* Look for the `>' that ends the template-argument-list. If we find
27006 a '>>' instead, it's probably just a typo. */
27007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27009 if (cxx_dialect
!= cxx98
)
27011 /* In C++0x, a `>>' in a template argument list or cast
27012 expression is considered to be two separate `>'
27013 tokens. So, change the current token to a `>', but don't
27014 consume it: it will be consumed later when the outer
27015 template argument list (or cast expression) is parsed.
27016 Note that this replacement of `>' for `>>' is necessary
27017 even if we are parsing tentatively: in the tentative
27018 case, after calling
27019 cp_parser_enclosed_template_argument_list we will always
27020 throw away all of the template arguments and the first
27021 closing `>', either because the template argument list
27022 was erroneous or because we are replacing those tokens
27023 with a CPP_TEMPLATE_ID token. The second `>' (which will
27024 not have been thrown away) is needed either to close an
27025 outer template argument list or to complete a new-style
27027 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27028 token
->type
= CPP_GREATER
;
27030 else if (!saved_greater_than_is_operator_p
)
27032 /* If we're in a nested template argument list, the '>>' has
27033 to be a typo for '> >'. We emit the error message, but we
27034 continue parsing and we push a '>' as next token, so that
27035 the argument list will be parsed correctly. Note that the
27036 global source location is still on the token before the
27037 '>>', so we need to say explicitly where we want it. */
27038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27039 gcc_rich_location
richloc (token
->location
);
27040 richloc
.add_fixit_replace ("> >");
27041 error_at_rich_loc (&richloc
, "%<>>%> should be %<> >%> "
27042 "within a nested template argument list");
27044 token
->type
= CPP_GREATER
;
27048 /* If this is not a nested template argument list, the '>>'
27049 is a typo for '>'. Emit an error message and continue.
27050 Same deal about the token location, but here we can get it
27051 right by consuming the '>>' before issuing the diagnostic. */
27052 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27053 error_at (token
->location
,
27054 "spurious %<>>%>, use %<>%> to terminate "
27055 "a template argument list");
27059 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27060 /* The `>' token might be a greater-than operator again now. */
27061 parser
->greater_than_is_operator_p
27062 = saved_greater_than_is_operator_p
;
27063 /* Restore the SAVED_SCOPE. */
27064 parser
->scope
= saved_scope
;
27065 parser
->qualifying_scope
= saved_qualifying_scope
;
27066 parser
->object_scope
= saved_object_scope
;
27067 cp_unevaluated_operand
= saved_unevaluated_operand
;
27068 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27073 /* MEMBER_FUNCTION is a member function, or a friend. If default
27074 arguments, or the body of the function have not yet been parsed,
27078 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27080 timevar_push (TV_PARSE_INMETH
);
27081 /* If this member is a template, get the underlying
27083 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27084 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27086 /* There should not be any class definitions in progress at this
27087 point; the bodies of members are only parsed outside of all class
27089 gcc_assert (parser
->num_classes_being_defined
== 0);
27090 /* While we're parsing the member functions we might encounter more
27091 classes. We want to handle them right away, but we don't want
27092 them getting mixed up with functions that are currently in the
27094 push_unparsed_function_queues (parser
);
27096 /* Make sure that any template parameters are in scope. */
27097 maybe_begin_member_template_processing (member_function
);
27099 /* If the body of the function has not yet been parsed, parse it
27101 if (DECL_PENDING_INLINE_P (member_function
))
27103 tree function_scope
;
27104 cp_token_cache
*tokens
;
27106 /* The function is no longer pending; we are processing it. */
27107 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27108 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27109 DECL_PENDING_INLINE_P (member_function
) = 0;
27111 /* If this is a local class, enter the scope of the containing
27113 function_scope
= current_function_decl
;
27114 if (function_scope
)
27115 push_function_context ();
27117 /* Push the body of the function onto the lexer stack. */
27118 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27120 /* Let the front end know that we going to be defining this
27122 start_preparsed_function (member_function
, NULL_TREE
,
27123 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27125 /* Don't do access checking if it is a templated function. */
27126 if (processing_template_decl
)
27127 push_deferring_access_checks (dk_no_check
);
27129 /* #pragma omp declare reduction needs special parsing. */
27130 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27132 parser
->lexer
->in_pragma
= true;
27133 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27134 finish_function (/*inline*/2);
27135 cp_check_omp_declare_reduction (member_function
);
27138 /* Now, parse the body of the function. */
27139 cp_parser_function_definition_after_declarator (parser
,
27140 /*inline_p=*/true);
27142 if (processing_template_decl
)
27143 pop_deferring_access_checks ();
27145 /* Leave the scope of the containing function. */
27146 if (function_scope
)
27147 pop_function_context ();
27148 cp_parser_pop_lexer (parser
);
27151 /* Remove any template parameters from the symbol table. */
27152 maybe_end_member_template_processing ();
27154 /* Restore the queue. */
27155 pop_unparsed_function_queues (parser
);
27156 timevar_pop (TV_PARSE_INMETH
);
27159 /* If DECL contains any default args, remember it on the unparsed
27160 functions queue. */
27163 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27167 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27169 probe
= TREE_CHAIN (probe
))
27170 if (TREE_PURPOSE (probe
))
27172 cp_default_arg_entry entry
= {current_class_type
, decl
};
27173 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27178 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27179 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27180 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27181 from the parameter-type-list. */
27184 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27185 tree default_arg
, tree parmtype
)
27187 cp_token_cache
*tokens
;
27191 if (default_arg
== error_mark_node
)
27192 return error_mark_node
;
27194 /* Push the saved tokens for the default argument onto the parser's
27196 tokens
= DEFARG_TOKENS (default_arg
);
27197 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27199 start_lambda_scope (decl
);
27201 /* Parse the default argument. */
27202 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27203 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27206 finish_lambda_scope ();
27208 if (parsed_arg
== error_mark_node
)
27209 cp_parser_skip_to_end_of_statement (parser
);
27211 if (!processing_template_decl
)
27213 /* In a non-template class, check conversions now. In a template,
27214 we'll wait and instantiate these as needed. */
27215 if (TREE_CODE (decl
) == PARM_DECL
)
27216 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27217 tf_warning_or_error
);
27218 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27219 parsed_arg
= error_mark_node
;
27221 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
27224 /* If the token stream has not been completely used up, then
27225 there was extra junk after the end of the default
27227 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27229 if (TREE_CODE (decl
) == PARM_DECL
)
27230 cp_parser_error (parser
, "expected %<,%>");
27232 cp_parser_error (parser
, "expected %<;%>");
27235 /* Revert to the main lexer. */
27236 cp_parser_pop_lexer (parser
);
27241 /* FIELD is a non-static data member with an initializer which we saved for
27242 later; parse it now. */
27245 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27249 maybe_begin_member_template_processing (field
);
27251 push_unparsed_function_queues (parser
);
27252 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27253 DECL_INITIAL (field
),
27255 pop_unparsed_function_queues (parser
);
27257 maybe_end_member_template_processing ();
27259 DECL_INITIAL (field
) = def
;
27262 /* FN is a FUNCTION_DECL which may contains a parameter with an
27263 unparsed DEFAULT_ARG. Parse the default args now. This function
27264 assumes that the current scope is the scope in which the default
27265 argument should be processed. */
27268 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27270 bool saved_local_variables_forbidden_p
;
27271 tree parm
, parmdecl
;
27273 /* While we're parsing the default args, we might (due to the
27274 statement expression extension) encounter more classes. We want
27275 to handle them right away, but we don't want them getting mixed
27276 up with default args that are currently in the queue. */
27277 push_unparsed_function_queues (parser
);
27279 /* Local variable names (and the `this' keyword) may not appear
27280 in a default argument. */
27281 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27282 parser
->local_variables_forbidden_p
= true;
27284 push_defarg_context (fn
);
27286 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27287 parmdecl
= DECL_ARGUMENTS (fn
);
27288 parm
&& parm
!= void_list_node
;
27289 parm
= TREE_CHAIN (parm
),
27290 parmdecl
= DECL_CHAIN (parmdecl
))
27292 tree default_arg
= TREE_PURPOSE (parm
);
27294 vec
<tree
, va_gc
> *insts
;
27301 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27302 /* This can happen for a friend declaration for a function
27303 already declared with default arguments. */
27307 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27309 TREE_VALUE (parm
));
27310 if (parsed_arg
== error_mark_node
)
27315 TREE_PURPOSE (parm
) = parsed_arg
;
27317 /* Update any instantiations we've already created. */
27318 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27319 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27320 TREE_PURPOSE (copy
) = parsed_arg
;
27323 pop_defarg_context ();
27325 /* Make sure no default arg is missing. */
27326 check_default_args (fn
);
27328 /* Restore the state of local_variables_forbidden_p. */
27329 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27331 /* Restore the queue. */
27332 pop_unparsed_function_queues (parser
);
27335 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27337 sizeof ... ( identifier )
27339 where the 'sizeof' token has already been consumed. */
27342 cp_parser_sizeof_pack (cp_parser
*parser
)
27344 /* Consume the `...'. */
27345 cp_lexer_consume_token (parser
->lexer
);
27346 maybe_warn_variadic_templates ();
27348 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27350 cp_lexer_consume_token (parser
->lexer
);
27352 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27353 "%<sizeof...%> argument must be surrounded by parentheses");
27355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27356 tree name
= cp_parser_identifier (parser
);
27357 if (name
== error_mark_node
)
27358 return error_mark_node
;
27359 /* The name is not qualified. */
27360 parser
->scope
= NULL_TREE
;
27361 parser
->qualifying_scope
= NULL_TREE
;
27362 parser
->object_scope
= NULL_TREE
;
27363 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27364 if (expr
== error_mark_node
)
27365 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27367 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27368 expr
= TREE_TYPE (expr
);
27369 else if (TREE_CODE (expr
) == CONST_DECL
)
27370 expr
= DECL_INITIAL (expr
);
27371 expr
= make_pack_expansion (expr
);
27372 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27375 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27380 /* Parse the operand of `sizeof' (or a similar operator). Returns
27381 either a TYPE or an expression, depending on the form of the
27382 input. The KEYWORD indicates which kind of expression we have
27386 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27388 tree expr
= NULL_TREE
;
27389 const char *saved_message
;
27391 bool saved_integral_constant_expression_p
;
27392 bool saved_non_integral_constant_expression_p
;
27394 /* If it's a `...', then we are computing the length of a parameter
27396 if (keyword
== RID_SIZEOF
27397 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27398 return cp_parser_sizeof_pack (parser
);
27400 /* Types cannot be defined in a `sizeof' expression. Save away the
27402 saved_message
= parser
->type_definition_forbidden_message
;
27403 /* And create the new one. */
27404 tmp
= concat ("types may not be defined in %<",
27405 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27406 "%> expressions", NULL
);
27407 parser
->type_definition_forbidden_message
= tmp
;
27409 /* The restrictions on constant-expressions do not apply inside
27410 sizeof expressions. */
27411 saved_integral_constant_expression_p
27412 = parser
->integral_constant_expression_p
;
27413 saved_non_integral_constant_expression_p
27414 = parser
->non_integral_constant_expression_p
;
27415 parser
->integral_constant_expression_p
= false;
27417 /* Do not actually evaluate the expression. */
27418 ++cp_unevaluated_operand
;
27419 ++c_inhibit_evaluation_warnings
;
27420 /* If it's a `(', then we might be looking at the type-id
27422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27424 tree type
= NULL_TREE
;
27426 /* We can't be sure yet whether we're looking at a type-id or an
27428 cp_parser_parse_tentatively (parser
);
27429 /* Note: as a GNU Extension, compound literals are considered
27430 postfix-expressions as they are in C99, so they are valid
27431 arguments to sizeof. See comment in cp_parser_cast_expression
27433 if (cp_parser_compound_literal_p (parser
))
27434 cp_parser_simulate_error (parser
);
27437 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27438 parser
->in_type_id_in_expr_p
= true;
27439 /* Look for the type-id. */
27440 type
= cp_parser_type_id (parser
);
27441 /* Look for the closing `)'. */
27442 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27443 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27446 /* If all went well, then we're done. */
27447 if (cp_parser_parse_definitely (parser
))
27449 cp_decl_specifier_seq decl_specs
;
27451 /* Build a trivial decl-specifier-seq. */
27452 clear_decl_specs (&decl_specs
);
27453 decl_specs
.type
= type
;
27455 /* Call grokdeclarator to figure out what type this is. */
27456 expr
= grokdeclarator (NULL
,
27460 /*attrlist=*/NULL
);
27464 /* If the type-id production did not work out, then we must be
27465 looking at the unary-expression production. */
27467 expr
= cp_parser_unary_expression (parser
);
27469 /* Go back to evaluating expressions. */
27470 --cp_unevaluated_operand
;
27471 --c_inhibit_evaluation_warnings
;
27473 /* Free the message we created. */
27475 /* And restore the old one. */
27476 parser
->type_definition_forbidden_message
= saved_message
;
27477 parser
->integral_constant_expression_p
27478 = saved_integral_constant_expression_p
;
27479 parser
->non_integral_constant_expression_p
27480 = saved_non_integral_constant_expression_p
;
27485 /* If the current declaration has no declarator, return true. */
27488 cp_parser_declares_only_class_p (cp_parser
*parser
)
27490 /* If the next token is a `;' or a `,' then there is no
27492 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27493 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27496 /* Update the DECL_SPECS to reflect the storage class indicated by
27500 cp_parser_set_storage_class (cp_parser
*parser
,
27501 cp_decl_specifier_seq
*decl_specs
,
27505 cp_storage_class storage_class
;
27507 if (parser
->in_unbraced_linkage_specification_p
)
27509 error_at (token
->location
, "invalid use of %qD in linkage specification",
27510 ridpointers
[keyword
]);
27513 else if (decl_specs
->storage_class
!= sc_none
)
27515 decl_specs
->conflicting_specifiers_p
= true;
27519 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27520 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27521 && decl_specs
->gnu_thread_keyword_p
)
27523 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27524 "%<__thread%> before %qD", ridpointers
[keyword
]);
27530 storage_class
= sc_auto
;
27533 storage_class
= sc_register
;
27536 storage_class
= sc_static
;
27539 storage_class
= sc_extern
;
27542 storage_class
= sc_mutable
;
27545 gcc_unreachable ();
27547 decl_specs
->storage_class
= storage_class
;
27548 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27550 /* A storage class specifier cannot be applied alongside a typedef
27551 specifier. If there is a typedef specifier present then set
27552 conflicting_specifiers_p which will trigger an error later
27553 on in grokdeclarator. */
27554 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27555 decl_specs
->conflicting_specifiers_p
= true;
27558 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27559 is true, the type is a class or enum definition. */
27562 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27565 bool type_definition_p
)
27567 decl_specs
->any_specifiers_p
= true;
27569 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27570 (with, for example, in "typedef int wchar_t;") we remember that
27571 this is what happened. In system headers, we ignore these
27572 declarations so that G++ can work with system headers that are not
27574 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27575 && !type_definition_p
27576 && (type_spec
== boolean_type_node
27577 || type_spec
== char16_type_node
27578 || type_spec
== char32_type_node
27579 || type_spec
== wchar_type_node
)
27580 && (decl_specs
->type
27581 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27582 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27583 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27584 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27586 decl_specs
->redefined_builtin_type
= type_spec
;
27587 set_and_check_decl_spec_loc (decl_specs
,
27588 ds_redefined_builtin_type_spec
,
27590 if (!decl_specs
->type
)
27592 decl_specs
->type
= type_spec
;
27593 decl_specs
->type_definition_p
= false;
27594 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27597 else if (decl_specs
->type
)
27598 decl_specs
->multiple_types_p
= true;
27601 decl_specs
->type
= type_spec
;
27602 decl_specs
->type_definition_p
= type_definition_p
;
27603 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27604 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27608 /* True iff TOKEN is the GNU keyword __thread. */
27611 token_is__thread (cp_token
*token
)
27613 gcc_assert (token
->keyword
== RID_THREAD
);
27614 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
27617 /* Set the location for a declarator specifier and check if it is
27620 DECL_SPECS is the sequence of declarator specifiers onto which to
27623 DS is the single declarator specifier to set which location is to
27624 be set onto the existing sequence of declarators.
27626 LOCATION is the location for the declarator specifier to
27630 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
27631 cp_decl_spec ds
, cp_token
*token
)
27633 gcc_assert (ds
< ds_last
);
27635 if (decl_specs
== NULL
)
27638 source_location location
= token
->location
;
27640 if (decl_specs
->locations
[ds
] == 0)
27642 decl_specs
->locations
[ds
] = location
;
27643 if (ds
== ds_thread
)
27644 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
27650 if (decl_specs
->locations
[ds_long_long
] != 0)
27651 error_at (location
,
27652 "%<long long long%> is too long for GCC");
27655 decl_specs
->locations
[ds_long_long
] = location
;
27656 pedwarn_cxx98 (location
,
27658 "ISO C++ 1998 does not support %<long long%>");
27661 else if (ds
== ds_thread
)
27663 bool gnu
= token_is__thread (token
);
27664 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
27665 error_at (location
,
27666 "both %<__thread%> and %<thread_local%> specified");
27668 error_at (location
, "duplicate %qD", token
->u
.value
);
27672 static const char *const decl_spec_names
[] = {
27689 error_at (location
,
27690 "duplicate %qs", decl_spec_names
[ds
]);
27695 /* Return true iff the declarator specifier DS is present in the
27696 sequence of declarator specifiers DECL_SPECS. */
27699 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
27702 gcc_assert (ds
< ds_last
);
27704 if (decl_specs
== NULL
)
27707 return decl_specs
->locations
[ds
] != 0;
27710 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27711 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27714 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
27716 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
27719 /* Issue an error message indicating that TOKEN_DESC was expected.
27720 If KEYWORD is true, it indicated this function is called by
27721 cp_parser_require_keword and the required token can only be
27722 a indicated keyword. */
27725 cp_parser_required_error (cp_parser
*parser
,
27726 required_token token_desc
,
27729 switch (token_desc
)
27732 cp_parser_error (parser
, "expected %<new%>");
27735 cp_parser_error (parser
, "expected %<delete%>");
27738 cp_parser_error (parser
, "expected %<return%>");
27741 cp_parser_error (parser
, "expected %<while%>");
27744 cp_parser_error (parser
, "expected %<extern%>");
27746 case RT_STATIC_ASSERT
:
27747 cp_parser_error (parser
, "expected %<static_assert%>");
27750 cp_parser_error (parser
, "expected %<decltype%>");
27753 cp_parser_error (parser
, "expected %<operator%>");
27756 cp_parser_error (parser
, "expected %<class%>");
27759 cp_parser_error (parser
, "expected %<template%>");
27762 cp_parser_error (parser
, "expected %<namespace%>");
27765 cp_parser_error (parser
, "expected %<using%>");
27768 cp_parser_error (parser
, "expected %<asm%>");
27771 cp_parser_error (parser
, "expected %<try%>");
27774 cp_parser_error (parser
, "expected %<catch%>");
27777 cp_parser_error (parser
, "expected %<throw%>");
27780 cp_parser_error (parser
, "expected %<__label__%>");
27783 cp_parser_error (parser
, "expected %<@try%>");
27785 case RT_AT_SYNCHRONIZED
:
27786 cp_parser_error (parser
, "expected %<@synchronized%>");
27789 cp_parser_error (parser
, "expected %<@throw%>");
27791 case RT_TRANSACTION_ATOMIC
:
27792 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
27794 case RT_TRANSACTION_RELAXED
:
27795 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
27802 switch (token_desc
)
27805 cp_parser_error (parser
, "expected %<;%>");
27807 case RT_OPEN_PAREN
:
27808 cp_parser_error (parser
, "expected %<(%>");
27810 case RT_CLOSE_BRACE
:
27811 cp_parser_error (parser
, "expected %<}%>");
27813 case RT_OPEN_BRACE
:
27814 cp_parser_error (parser
, "expected %<{%>");
27816 case RT_CLOSE_SQUARE
:
27817 cp_parser_error (parser
, "expected %<]%>");
27819 case RT_OPEN_SQUARE
:
27820 cp_parser_error (parser
, "expected %<[%>");
27823 cp_parser_error (parser
, "expected %<,%>");
27826 cp_parser_error (parser
, "expected %<::%>");
27829 cp_parser_error (parser
, "expected %<<%>");
27832 cp_parser_error (parser
, "expected %<>%>");
27835 cp_parser_error (parser
, "expected %<=%>");
27838 cp_parser_error (parser
, "expected %<...%>");
27841 cp_parser_error (parser
, "expected %<*%>");
27844 cp_parser_error (parser
, "expected %<~%>");
27847 cp_parser_error (parser
, "expected %<:%>");
27849 case RT_COLON_SCOPE
:
27850 cp_parser_error (parser
, "expected %<:%> or %<::%>");
27852 case RT_CLOSE_PAREN
:
27853 cp_parser_error (parser
, "expected %<)%>");
27855 case RT_COMMA_CLOSE_PAREN
:
27856 cp_parser_error (parser
, "expected %<,%> or %<)%>");
27858 case RT_PRAGMA_EOL
:
27859 cp_parser_error (parser
, "expected end of line");
27862 cp_parser_error (parser
, "expected identifier");
27865 cp_parser_error (parser
, "expected selection-statement");
27867 case RT_INTERATION
:
27868 cp_parser_error (parser
, "expected iteration-statement");
27871 cp_parser_error (parser
, "expected jump-statement");
27874 cp_parser_error (parser
, "expected class-key");
27876 case RT_CLASS_TYPENAME_TEMPLATE
:
27877 cp_parser_error (parser
,
27878 "expected %<class%>, %<typename%>, or %<template%>");
27881 gcc_unreachable ();
27885 gcc_unreachable ();
27890 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27891 issue an error message indicating that TOKEN_DESC was expected.
27893 Returns the token consumed, if the token had the appropriate type.
27894 Otherwise, returns NULL. */
27897 cp_parser_require (cp_parser
* parser
,
27898 enum cpp_ttype type
,
27899 required_token token_desc
)
27901 if (cp_lexer_next_token_is (parser
->lexer
, type
))
27902 return cp_lexer_consume_token (parser
->lexer
);
27905 /* Output the MESSAGE -- unless we're parsing tentatively. */
27906 if (!cp_parser_simulate_error (parser
))
27907 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
27912 /* An error message is produced if the next token is not '>'.
27913 All further tokens are skipped until the desired token is
27914 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27917 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
27919 /* Current level of '< ... >'. */
27920 unsigned level
= 0;
27921 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27922 unsigned nesting_depth
= 0;
27924 /* Are we ready, yet? If not, issue error message. */
27925 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
27928 /* Skip tokens until the desired token is found. */
27931 /* Peek at the next token. */
27932 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27935 if (!nesting_depth
)
27940 if (cxx_dialect
== cxx98
)
27941 /* C++0x views the `>>' operator as two `>' tokens, but
27944 else if (!nesting_depth
&& level
-- == 0)
27946 /* We've hit a `>>' where the first `>' closes the
27947 template argument list, and the second `>' is
27948 spurious. Just consume the `>>' and stop; we've
27949 already produced at least one error. */
27950 cp_lexer_consume_token (parser
->lexer
);
27953 /* Fall through for C++0x, so we handle the second `>' in
27955 gcc_fallthrough ();
27958 if (!nesting_depth
&& level
-- == 0)
27960 /* We've reached the token we want, consume it and stop. */
27961 cp_lexer_consume_token (parser
->lexer
);
27966 case CPP_OPEN_PAREN
:
27967 case CPP_OPEN_SQUARE
:
27971 case CPP_CLOSE_PAREN
:
27972 case CPP_CLOSE_SQUARE
:
27973 if (nesting_depth
-- == 0)
27978 case CPP_PRAGMA_EOL
:
27979 case CPP_SEMICOLON
:
27980 case CPP_OPEN_BRACE
:
27981 case CPP_CLOSE_BRACE
:
27982 /* The '>' was probably forgotten, don't look further. */
27989 /* Consume this token. */
27990 cp_lexer_consume_token (parser
->lexer
);
27994 /* If the next token is the indicated keyword, consume it. Otherwise,
27995 issue an error message indicating that TOKEN_DESC was expected.
27997 Returns the token consumed, if the token had the appropriate type.
27998 Otherwise, returns NULL. */
28001 cp_parser_require_keyword (cp_parser
* parser
,
28003 required_token token_desc
)
28005 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28007 if (token
&& token
->keyword
!= keyword
)
28009 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
28016 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28017 function-definition. */
28020 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28022 return (/* An ordinary function-body begins with an `{'. */
28023 token
->type
== CPP_OPEN_BRACE
28024 /* A ctor-initializer begins with a `:'. */
28025 || token
->type
== CPP_COLON
28026 /* A function-try-block begins with `try'. */
28027 || token
->keyword
== RID_TRY
28028 /* A function-transaction-block begins with `__transaction_atomic'
28029 or `__transaction_relaxed'. */
28030 || token
->keyword
== RID_TRANSACTION_ATOMIC
28031 || token
->keyword
== RID_TRANSACTION_RELAXED
28032 /* The named return value extension begins with `return'. */
28033 || token
->keyword
== RID_RETURN
);
28036 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28040 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28044 token
= cp_lexer_peek_token (parser
->lexer
);
28045 return (token
->type
== CPP_OPEN_BRACE
28046 || (token
->type
== CPP_COLON
28047 && !parser
->colon_doesnt_start_class_def_p
));
28050 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28051 C++0x) ending a template-argument. */
28054 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28058 token
= cp_lexer_peek_token (parser
->lexer
);
28059 return (token
->type
== CPP_COMMA
28060 || token
->type
== CPP_GREATER
28061 || token
->type
== CPP_ELLIPSIS
28062 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28065 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28066 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28069 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28074 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28075 if (token
->type
== CPP_LESS
)
28077 /* Check for the sequence `<::' in the original code. It would be lexed as
28078 `[:', where `[' is a digraph, and there is no whitespace before
28080 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28083 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28084 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28090 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28091 or none_type otherwise. */
28093 static enum tag_types
28094 cp_parser_token_is_class_key (cp_token
* token
)
28096 switch (token
->keyword
)
28101 return record_type
;
28110 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28111 or none_type otherwise or if the token is null. */
28113 static enum tag_types
28114 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28119 switch (token
->keyword
)
28124 return typename_type
;
28131 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28134 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28136 if (type
== error_mark_node
)
28138 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28140 if (permerror (input_location
, "%qs tag used in naming %q#T",
28141 class_key
== union_type
? "union"
28142 : class_key
== record_type
? "struct" : "class",
28144 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28145 "%q#T was previously declared here", type
);
28149 /* Issue an error message if DECL is redeclared with different
28150 access than its original declaration [class.access.spec/3].
28151 This applies to nested classes, nested class templates and
28152 enumerations [class.mem/1]. */
28155 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28158 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28159 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28162 if ((TREE_PRIVATE (decl
)
28163 != (current_access_specifier
== access_private_node
))
28164 || (TREE_PROTECTED (decl
)
28165 != (current_access_specifier
== access_protected_node
)))
28166 error_at (location
, "%qD redeclared with different access", decl
);
28169 /* Look for the `template' keyword, as a syntactic disambiguator.
28170 Return TRUE iff it is present, in which case it will be
28174 cp_parser_optional_template_keyword (cp_parser
*parser
)
28176 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28178 /* In C++98 the `template' keyword can only be used within templates;
28179 outside templates the parser can always figure out what is a
28180 template and what is not. In C++11, per the resolution of DR 468,
28181 `template' is allowed in cases where it is not strictly necessary. */
28182 if (!processing_template_decl
28183 && pedantic
&& cxx_dialect
== cxx98
)
28185 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28186 pedwarn (token
->location
, OPT_Wpedantic
,
28187 "in C++98 %<template%> (as a disambiguator) is only "
28188 "allowed within templates");
28189 /* If this part of the token stream is rescanned, the same
28190 error message would be generated. So, we purge the token
28191 from the stream. */
28192 cp_lexer_purge_token (parser
->lexer
);
28197 /* Consume the `template' keyword. */
28198 cp_lexer_consume_token (parser
->lexer
);
28205 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28206 set PARSER->SCOPE, and perform other related actions. */
28209 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28211 struct tree_check
*check_value
;
28213 /* Get the stored value. */
28214 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28215 /* Set the scope from the stored value. */
28216 parser
->scope
= saved_checks_value (check_value
);
28217 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28218 parser
->object_scope
= NULL_TREE
;
28221 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28222 encounter the end of a block before what we were looking for. */
28225 cp_parser_cache_group (cp_parser
*parser
,
28226 enum cpp_ttype end
,
28231 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28233 /* Abort a parenthesized expression if we encounter a semicolon. */
28234 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28235 && token
->type
== CPP_SEMICOLON
)
28237 /* If we've reached the end of the file, stop. */
28238 if (token
->type
== CPP_EOF
28239 || (end
!= CPP_PRAGMA_EOL
28240 && token
->type
== CPP_PRAGMA_EOL
))
28242 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28243 /* We've hit the end of an enclosing block, so there's been some
28244 kind of syntax error. */
28247 /* Consume the token. */
28248 cp_lexer_consume_token (parser
->lexer
);
28249 /* See if it starts a new group. */
28250 if (token
->type
== CPP_OPEN_BRACE
)
28252 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28253 /* In theory this should probably check end == '}', but
28254 cp_parser_save_member_function_body needs it to exit
28255 after either '}' or ')' when called with ')'. */
28259 else if (token
->type
== CPP_OPEN_PAREN
)
28261 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28262 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28265 else if (token
->type
== CPP_PRAGMA
)
28266 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28267 else if (token
->type
== end
)
28272 /* Like above, for caching a default argument or NSDMI. Both of these are
28273 terminated by a non-nested comma, but it can be unclear whether or not a
28274 comma is nested in a template argument list unless we do more parsing.
28275 In order to handle this ambiguity, when we encounter a ',' after a '<'
28276 we try to parse what follows as a parameter-declaration-list (in the
28277 case of a default argument) or a member-declarator (in the case of an
28278 NSDMI). If that succeeds, then we stop caching. */
28281 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28283 unsigned depth
= 0;
28284 int maybe_template_id
= 0;
28285 cp_token
*first_token
;
28287 tree default_argument
;
28289 /* Add tokens until we have processed the entire default
28290 argument. We add the range [first_token, token). */
28291 first_token
= cp_lexer_peek_token (parser
->lexer
);
28292 if (first_token
->type
== CPP_OPEN_BRACE
)
28294 /* For list-initialization, this is straightforward. */
28295 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28296 token
= cp_lexer_peek_token (parser
->lexer
);
28302 /* Peek at the next token. */
28303 token
= cp_lexer_peek_token (parser
->lexer
);
28304 /* What we do depends on what token we have. */
28305 switch (token
->type
)
28307 /* In valid code, a default argument must be
28308 immediately followed by a `,' `)', or `...'. */
28310 if (depth
== 0 && maybe_template_id
)
28312 /* If we've seen a '<', we might be in a
28313 template-argument-list. Until Core issue 325 is
28314 resolved, we don't know how this situation ought
28315 to be handled, so try to DTRT. We check whether
28316 what comes after the comma is a valid parameter
28317 declaration list. If it is, then the comma ends
28318 the default argument; otherwise the default
28319 argument continues. */
28320 bool error
= false;
28323 /* Set ITALP so cp_parser_parameter_declaration_list
28324 doesn't decide to commit to this parse. */
28325 bool saved_italp
= parser
->in_template_argument_list_p
;
28326 parser
->in_template_argument_list_p
= true;
28328 cp_parser_parse_tentatively (parser
);
28332 /* Parse declarators until we reach a non-comma or
28333 somthing that cannot be an initializer.
28334 Just checking whether we're looking at a single
28335 declarator is insufficient. Consider:
28336 int var = tuple<T,U>::x;
28337 The template parameter 'U' looks exactly like a
28341 int ctor_dtor_or_conv_p
;
28342 cp_lexer_consume_token (parser
->lexer
);
28343 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28344 &ctor_dtor_or_conv_p
,
28345 /*parenthesized_p=*/NULL
,
28347 /*friend_p=*/false);
28348 peek
= cp_lexer_peek_token (parser
->lexer
);
28349 if (cp_parser_error_occurred (parser
))
28352 while (peek
->type
== CPP_COMMA
);
28353 /* If we met an '=' or ';' then the original comma
28354 was the end of the NSDMI. Otherwise assume
28355 we're still in the NSDMI. */
28356 error
= (peek
->type
!= CPP_EQ
28357 && peek
->type
!= CPP_SEMICOLON
);
28361 cp_lexer_consume_token (parser
->lexer
);
28362 begin_scope (sk_function_parms
, NULL_TREE
);
28363 cp_parser_parameter_declaration_list (parser
, &error
);
28364 pop_bindings_and_leave_scope ();
28366 if (!cp_parser_error_occurred (parser
) && !error
)
28368 cp_parser_abort_tentative_parse (parser
);
28370 parser
->in_template_argument_list_p
= saved_italp
;
28374 case CPP_CLOSE_PAREN
:
28376 /* If we run into a non-nested `;', `}', or `]',
28377 then the code is invalid -- but the default
28378 argument is certainly over. */
28379 case CPP_SEMICOLON
:
28380 case CPP_CLOSE_BRACE
:
28381 case CPP_CLOSE_SQUARE
:
28383 /* Handle correctly int n = sizeof ... ( p ); */
28384 && token
->type
!= CPP_ELLIPSIS
)
28386 /* Update DEPTH, if necessary. */
28387 else if (token
->type
== CPP_CLOSE_PAREN
28388 || token
->type
== CPP_CLOSE_BRACE
28389 || token
->type
== CPP_CLOSE_SQUARE
)
28393 case CPP_OPEN_PAREN
:
28394 case CPP_OPEN_SQUARE
:
28395 case CPP_OPEN_BRACE
:
28401 /* This might be the comparison operator, or it might
28402 start a template argument list. */
28403 ++maybe_template_id
;
28407 if (cxx_dialect
== cxx98
)
28409 /* Fall through for C++0x, which treats the `>>'
28410 operator like two `>' tokens in certain
28412 gcc_fallthrough ();
28417 /* This might be an operator, or it might close a
28418 template argument list. But if a previous '<'
28419 started a template argument list, this will have
28420 closed it, so we can't be in one anymore. */
28421 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28422 if (maybe_template_id
< 0)
28423 maybe_template_id
= 0;
28427 /* If we run out of tokens, issue an error message. */
28429 case CPP_PRAGMA_EOL
:
28430 error_at (token
->location
, "file ends in default argument");
28431 return error_mark_node
;
28435 /* In these cases, we should look for template-ids.
28436 For example, if the default argument is
28437 `X<int, double>()', we need to do name lookup to
28438 figure out whether or not `X' is a template; if
28439 so, the `,' does not end the default argument.
28441 That is not yet done. */
28448 /* If we've reached the end, stop. */
28452 /* Add the token to the token block. */
28453 token
= cp_lexer_consume_token (parser
->lexer
);
28456 /* Create a DEFAULT_ARG to represent the unparsed default
28458 default_argument
= make_node (DEFAULT_ARG
);
28459 DEFARG_TOKENS (default_argument
)
28460 = cp_token_cache_new (first_token
, token
);
28461 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28463 return default_argument
;
28466 /* Begin parsing tentatively. We always save tokens while parsing
28467 tentatively so that if the tentative parsing fails we can restore the
28471 cp_parser_parse_tentatively (cp_parser
* parser
)
28473 /* Enter a new parsing context. */
28474 parser
->context
= cp_parser_context_new (parser
->context
);
28475 /* Begin saving tokens. */
28476 cp_lexer_save_tokens (parser
->lexer
);
28477 /* In order to avoid repetitive access control error messages,
28478 access checks are queued up until we are no longer parsing
28480 push_deferring_access_checks (dk_deferred
);
28483 /* Commit to the currently active tentative parse. */
28486 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28488 cp_parser_context
*context
;
28491 /* Mark all of the levels as committed. */
28492 lexer
= parser
->lexer
;
28493 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28495 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28497 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28498 while (!cp_lexer_saving_tokens (lexer
))
28499 lexer
= lexer
->next
;
28500 cp_lexer_commit_tokens (lexer
);
28504 /* Commit to the topmost currently active tentative parse.
28506 Note that this function shouldn't be called when there are
28507 irreversible side-effects while in a tentative state. For
28508 example, we shouldn't create a permanent entry in the symbol
28509 table, or issue an error message that might not apply if the
28510 tentative parse is aborted. */
28513 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28515 cp_parser_context
*context
= parser
->context
;
28516 cp_lexer
*lexer
= parser
->lexer
;
28520 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28522 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28524 while (!cp_lexer_saving_tokens (lexer
))
28525 lexer
= lexer
->next
;
28526 cp_lexer_commit_tokens (lexer
);
28530 /* Abort the currently active tentative parse. All consumed tokens
28531 will be rolled back, and no diagnostics will be issued. */
28534 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28536 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28537 || errorcount
> 0);
28538 cp_parser_simulate_error (parser
);
28539 /* Now, pretend that we want to see if the construct was
28540 successfully parsed. */
28541 cp_parser_parse_definitely (parser
);
28544 /* Stop parsing tentatively. If a parse error has occurred, restore the
28545 token stream. Otherwise, commit to the tokens we have consumed.
28546 Returns true if no error occurred; false otherwise. */
28549 cp_parser_parse_definitely (cp_parser
* parser
)
28551 bool error_occurred
;
28552 cp_parser_context
*context
;
28554 /* Remember whether or not an error occurred, since we are about to
28555 destroy that information. */
28556 error_occurred
= cp_parser_error_occurred (parser
);
28557 /* Remove the topmost context from the stack. */
28558 context
= parser
->context
;
28559 parser
->context
= context
->next
;
28560 /* If no parse errors occurred, commit to the tentative parse. */
28561 if (!error_occurred
)
28563 /* Commit to the tokens read tentatively, unless that was
28565 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28566 cp_lexer_commit_tokens (parser
->lexer
);
28568 pop_to_parent_deferring_access_checks ();
28570 /* Otherwise, if errors occurred, roll back our state so that things
28571 are just as they were before we began the tentative parse. */
28574 cp_lexer_rollback_tokens (parser
->lexer
);
28575 pop_deferring_access_checks ();
28577 /* Add the context to the front of the free list. */
28578 context
->next
= cp_parser_context_free_list
;
28579 cp_parser_context_free_list
= context
;
28581 return !error_occurred
;
28584 /* Returns true if we are parsing tentatively and are not committed to
28585 this tentative parse. */
28588 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
28590 return (cp_parser_parsing_tentatively (parser
)
28591 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
28594 /* Returns nonzero iff an error has occurred during the most recent
28595 tentative parse. */
28598 cp_parser_error_occurred (cp_parser
* parser
)
28600 return (cp_parser_parsing_tentatively (parser
)
28601 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
28604 /* Returns nonzero if GNU extensions are allowed. */
28607 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
28609 return parser
->allow_gnu_extensions_p
;
28612 /* Objective-C++ Productions */
28615 /* Parse an Objective-C expression, which feeds into a primary-expression
28619 objc-message-expression
28620 objc-string-literal
28621 objc-encode-expression
28622 objc-protocol-expression
28623 objc-selector-expression
28625 Returns a tree representation of the expression. */
28628 cp_parser_objc_expression (cp_parser
* parser
)
28630 /* Try to figure out what kind of declaration is present. */
28631 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28635 case CPP_OPEN_SQUARE
:
28636 return cp_parser_objc_message_expression (parser
);
28638 case CPP_OBJC_STRING
:
28639 kwd
= cp_lexer_consume_token (parser
->lexer
);
28640 return objc_build_string_object (kwd
->u
.value
);
28643 switch (kwd
->keyword
)
28645 case RID_AT_ENCODE
:
28646 return cp_parser_objc_encode_expression (parser
);
28648 case RID_AT_PROTOCOL
:
28649 return cp_parser_objc_protocol_expression (parser
);
28651 case RID_AT_SELECTOR
:
28652 return cp_parser_objc_selector_expression (parser
);
28658 error_at (kwd
->location
,
28659 "misplaced %<@%D%> Objective-C++ construct",
28661 cp_parser_skip_to_end_of_block_or_statement (parser
);
28664 return error_mark_node
;
28667 /* Parse an Objective-C message expression.
28669 objc-message-expression:
28670 [ objc-message-receiver objc-message-args ]
28672 Returns a representation of an Objective-C message. */
28675 cp_parser_objc_message_expression (cp_parser
* parser
)
28677 tree receiver
, messageargs
;
28679 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28680 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
28681 receiver
= cp_parser_objc_message_receiver (parser
);
28682 messageargs
= cp_parser_objc_message_args (parser
);
28683 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28684 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28686 tree result
= objc_build_message_expr (receiver
, messageargs
);
28688 /* Construct a location e.g.
28691 ranging from the '[' to the ']', with the caret at the start. */
28692 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
28693 protected_set_expr_location (result
, combined_loc
);
28698 /* Parse an objc-message-receiver.
28700 objc-message-receiver:
28702 simple-type-specifier
28704 Returns a representation of the type or expression. */
28707 cp_parser_objc_message_receiver (cp_parser
* parser
)
28711 /* An Objective-C message receiver may be either (1) a type
28712 or (2) an expression. */
28713 cp_parser_parse_tentatively (parser
);
28714 rcv
= cp_parser_expression (parser
);
28716 /* If that worked out, fine. */
28717 if (cp_parser_parse_definitely (parser
))
28720 cp_parser_parse_tentatively (parser
);
28721 rcv
= cp_parser_simple_type_specifier (parser
,
28722 /*decl_specs=*/NULL
,
28723 CP_PARSER_FLAGS_NONE
);
28725 if (cp_parser_parse_definitely (parser
))
28726 return objc_get_class_reference (rcv
);
28728 cp_parser_error (parser
, "objective-c++ message receiver expected");
28729 return error_mark_node
;
28732 /* Parse the arguments and selectors comprising an Objective-C message.
28737 objc-selector-args , objc-comma-args
28739 objc-selector-args:
28740 objc-selector [opt] : assignment-expression
28741 objc-selector-args objc-selector [opt] : assignment-expression
28744 assignment-expression
28745 objc-comma-args , assignment-expression
28747 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28748 selector arguments and TREE_VALUE containing a list of comma
28752 cp_parser_objc_message_args (cp_parser
* parser
)
28754 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
28755 bool maybe_unary_selector_p
= true;
28756 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28758 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
28760 tree selector
= NULL_TREE
, arg
;
28762 if (token
->type
!= CPP_COLON
)
28763 selector
= cp_parser_objc_selector (parser
);
28765 /* Detect if we have a unary selector. */
28766 if (maybe_unary_selector_p
28767 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28768 return build_tree_list (selector
, NULL_TREE
);
28770 maybe_unary_selector_p
= false;
28771 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
28772 arg
= cp_parser_assignment_expression (parser
);
28775 = chainon (sel_args
,
28776 build_tree_list (selector
, arg
));
28778 token
= cp_lexer_peek_token (parser
->lexer
);
28781 /* Handle non-selector arguments, if any. */
28782 while (token
->type
== CPP_COMMA
)
28786 cp_lexer_consume_token (parser
->lexer
);
28787 arg
= cp_parser_assignment_expression (parser
);
28790 = chainon (addl_args
,
28791 build_tree_list (NULL_TREE
, arg
));
28793 token
= cp_lexer_peek_token (parser
->lexer
);
28796 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
28798 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
28799 return build_tree_list (error_mark_node
, error_mark_node
);
28802 return build_tree_list (sel_args
, addl_args
);
28805 /* Parse an Objective-C encode expression.
28807 objc-encode-expression:
28808 @encode objc-typename
28810 Returns an encoded representation of the type argument. */
28813 cp_parser_objc_encode_expression (cp_parser
* parser
)
28817 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28819 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
28820 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28821 token
= cp_lexer_peek_token (parser
->lexer
);
28822 type
= complete_type (cp_parser_type_id (parser
));
28823 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28827 error_at (token
->location
,
28828 "%<@encode%> must specify a type as an argument");
28829 return error_mark_node
;
28832 /* This happens if we find @encode(T) (where T is a template
28833 typename or something dependent on a template typename) when
28834 parsing a template. In that case, we can't compile it
28835 immediately, but we rather create an AT_ENCODE_EXPR which will
28836 need to be instantiated when the template is used.
28838 if (dependent_type_p (type
))
28840 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
28841 TREE_READONLY (value
) = 1;
28846 /* Build a location of the form:
28849 with caret==start at the @ token, finishing at the close paren. */
28850 location_t combined_loc
28851 = make_location (start_loc
, start_loc
,
28852 cp_lexer_previous_token (parser
->lexer
)->location
);
28854 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
28857 /* Parse an Objective-C @defs expression. */
28860 cp_parser_objc_defs_expression (cp_parser
*parser
)
28864 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
28865 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28866 name
= cp_parser_identifier (parser
);
28867 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28869 return objc_get_class_ivars (name
);
28872 /* Parse an Objective-C protocol expression.
28874 objc-protocol-expression:
28875 @protocol ( identifier )
28877 Returns a representation of the protocol expression. */
28880 cp_parser_objc_protocol_expression (cp_parser
* parser
)
28883 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28885 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28886 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28887 proto
= cp_parser_identifier (parser
);
28888 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28890 /* Build a location of the form:
28893 with caret==start at the @ token, finishing at the close paren. */
28894 location_t combined_loc
28895 = make_location (start_loc
, start_loc
,
28896 cp_lexer_previous_token (parser
->lexer
)->location
);
28897 tree result
= objc_build_protocol_expr (proto
);
28898 protected_set_expr_location (result
, combined_loc
);
28902 /* Parse an Objective-C selector expression.
28904 objc-selector-expression:
28905 @selector ( objc-method-signature )
28907 objc-method-signature:
28913 objc-selector-seq objc-selector :
28915 Returns a representation of the method selector. */
28918 cp_parser_objc_selector_expression (cp_parser
* parser
)
28920 tree sel_seq
= NULL_TREE
;
28921 bool maybe_unary_selector_p
= true;
28923 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28925 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
28926 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28927 token
= cp_lexer_peek_token (parser
->lexer
);
28929 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
28930 || token
->type
== CPP_SCOPE
)
28932 tree selector
= NULL_TREE
;
28934 if (token
->type
!= CPP_COLON
28935 || token
->type
== CPP_SCOPE
)
28936 selector
= cp_parser_objc_selector (parser
);
28938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
28939 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
28941 /* Detect if we have a unary selector. */
28942 if (maybe_unary_selector_p
)
28944 sel_seq
= selector
;
28945 goto finish_selector
;
28949 cp_parser_error (parser
, "expected %<:%>");
28952 maybe_unary_selector_p
= false;
28953 token
= cp_lexer_consume_token (parser
->lexer
);
28955 if (token
->type
== CPP_SCOPE
)
28958 = chainon (sel_seq
,
28959 build_tree_list (selector
, NULL_TREE
));
28961 = chainon (sel_seq
,
28962 build_tree_list (NULL_TREE
, NULL_TREE
));
28966 = chainon (sel_seq
,
28967 build_tree_list (selector
, NULL_TREE
));
28969 token
= cp_lexer_peek_token (parser
->lexer
);
28973 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28976 /* Build a location of the form:
28979 with caret==start at the @ token, finishing at the close paren. */
28980 location_t combined_loc
28981 = make_location (loc
, loc
,
28982 cp_lexer_previous_token (parser
->lexer
)->location
);
28983 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
28984 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28985 protected_set_expr_location (result
, combined_loc
);
28989 /* Parse a list of identifiers.
28991 objc-identifier-list:
28993 objc-identifier-list , identifier
28995 Returns a TREE_LIST of identifier nodes. */
28998 cp_parser_objc_identifier_list (cp_parser
* parser
)
29004 identifier
= cp_parser_identifier (parser
);
29005 if (identifier
== error_mark_node
)
29006 return error_mark_node
;
29008 list
= build_tree_list (NULL_TREE
, identifier
);
29009 sep
= cp_lexer_peek_token (parser
->lexer
);
29011 while (sep
->type
== CPP_COMMA
)
29013 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29014 identifier
= cp_parser_identifier (parser
);
29015 if (identifier
== error_mark_node
)
29018 list
= chainon (list
, build_tree_list (NULL_TREE
,
29020 sep
= cp_lexer_peek_token (parser
->lexer
);
29026 /* Parse an Objective-C alias declaration.
29028 objc-alias-declaration:
29029 @compatibility_alias identifier identifier ;
29031 This function registers the alias mapping with the Objective-C front end.
29032 It returns nothing. */
29035 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29039 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29040 alias
= cp_parser_identifier (parser
);
29041 orig
= cp_parser_identifier (parser
);
29042 objc_declare_alias (alias
, orig
);
29043 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29046 /* Parse an Objective-C class forward-declaration.
29048 objc-class-declaration:
29049 @class objc-identifier-list ;
29051 The function registers the forward declarations with the Objective-C
29052 front end. It returns nothing. */
29055 cp_parser_objc_class_declaration (cp_parser
* parser
)
29057 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29062 id
= cp_parser_identifier (parser
);
29063 if (id
== error_mark_node
)
29066 objc_declare_class (id
);
29068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29069 cp_lexer_consume_token (parser
->lexer
);
29073 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29076 /* Parse a list of Objective-C protocol references.
29078 objc-protocol-refs-opt:
29079 objc-protocol-refs [opt]
29081 objc-protocol-refs:
29082 < objc-identifier-list >
29084 Returns a TREE_LIST of identifiers, if any. */
29087 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29089 tree protorefs
= NULL_TREE
;
29091 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29093 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29094 protorefs
= cp_parser_objc_identifier_list (parser
);
29095 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29101 /* Parse a Objective-C visibility specification. */
29104 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29106 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29108 switch (vis
->keyword
)
29110 case RID_AT_PRIVATE
:
29111 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29113 case RID_AT_PROTECTED
:
29114 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29116 case RID_AT_PUBLIC
:
29117 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29119 case RID_AT_PACKAGE
:
29120 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29126 /* Eat '@private'/'@protected'/'@public'. */
29127 cp_lexer_consume_token (parser
->lexer
);
29130 /* Parse an Objective-C method type. Return 'true' if it is a class
29131 (+) method, and 'false' if it is an instance (-) method. */
29134 cp_parser_objc_method_type (cp_parser
* parser
)
29136 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29142 /* Parse an Objective-C protocol qualifier. */
29145 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29147 tree quals
= NULL_TREE
, node
;
29148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29150 node
= token
->u
.value
;
29152 while (node
&& identifier_p (node
)
29153 && (node
== ridpointers
[(int) RID_IN
]
29154 || node
== ridpointers
[(int) RID_OUT
]
29155 || node
== ridpointers
[(int) RID_INOUT
]
29156 || node
== ridpointers
[(int) RID_BYCOPY
]
29157 || node
== ridpointers
[(int) RID_BYREF
]
29158 || node
== ridpointers
[(int) RID_ONEWAY
]))
29160 quals
= tree_cons (NULL_TREE
, node
, quals
);
29161 cp_lexer_consume_token (parser
->lexer
);
29162 token
= cp_lexer_peek_token (parser
->lexer
);
29163 node
= token
->u
.value
;
29169 /* Parse an Objective-C typename. */
29172 cp_parser_objc_typename (cp_parser
* parser
)
29174 tree type_name
= NULL_TREE
;
29176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29178 tree proto_quals
, cp_type
= NULL_TREE
;
29180 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29181 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29183 /* An ObjC type name may consist of just protocol qualifiers, in which
29184 case the type shall default to 'id'. */
29185 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29187 cp_type
= cp_parser_type_id (parser
);
29189 /* If the type could not be parsed, an error has already
29190 been produced. For error recovery, behave as if it had
29191 not been specified, which will use the default type
29193 if (cp_type
== error_mark_node
)
29195 cp_type
= NULL_TREE
;
29196 /* We need to skip to the closing parenthesis as
29197 cp_parser_type_id() does not seem to do it for
29199 cp_parser_skip_to_closing_parenthesis (parser
,
29200 /*recovering=*/true,
29201 /*or_comma=*/false,
29202 /*consume_paren=*/false);
29206 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29207 type_name
= build_tree_list (proto_quals
, cp_type
);
29213 /* Check to see if TYPE refers to an Objective-C selector name. */
29216 cp_parser_objc_selector_p (enum cpp_ttype type
)
29218 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29219 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29220 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29221 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29222 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29225 /* Parse an Objective-C selector. */
29228 cp_parser_objc_selector (cp_parser
* parser
)
29230 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29232 if (!cp_parser_objc_selector_p (token
->type
))
29234 error_at (token
->location
, "invalid Objective-C++ selector name");
29235 return error_mark_node
;
29238 /* C++ operator names are allowed to appear in ObjC selectors. */
29239 switch (token
->type
)
29241 case CPP_AND_AND
: return get_identifier ("and");
29242 case CPP_AND_EQ
: return get_identifier ("and_eq");
29243 case CPP_AND
: return get_identifier ("bitand");
29244 case CPP_OR
: return get_identifier ("bitor");
29245 case CPP_COMPL
: return get_identifier ("compl");
29246 case CPP_NOT
: return get_identifier ("not");
29247 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29248 case CPP_OR_OR
: return get_identifier ("or");
29249 case CPP_OR_EQ
: return get_identifier ("or_eq");
29250 case CPP_XOR
: return get_identifier ("xor");
29251 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29252 default: return token
->u
.value
;
29256 /* Parse an Objective-C params list. */
29259 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29261 tree params
= NULL_TREE
;
29262 bool maybe_unary_selector_p
= true;
29263 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29265 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29267 tree selector
= NULL_TREE
, type_name
, identifier
;
29268 tree parm_attr
= NULL_TREE
;
29270 if (token
->keyword
== RID_ATTRIBUTE
)
29273 if (token
->type
!= CPP_COLON
)
29274 selector
= cp_parser_objc_selector (parser
);
29276 /* Detect if we have a unary selector. */
29277 if (maybe_unary_selector_p
29278 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29280 params
= selector
; /* Might be followed by attributes. */
29284 maybe_unary_selector_p
= false;
29285 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29287 /* Something went quite wrong. There should be a colon
29288 here, but there is not. Stop parsing parameters. */
29291 type_name
= cp_parser_objc_typename (parser
);
29292 /* New ObjC allows attributes on parameters too. */
29293 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29294 parm_attr
= cp_parser_attributes_opt (parser
);
29295 identifier
= cp_parser_identifier (parser
);
29299 objc_build_keyword_decl (selector
,
29304 token
= cp_lexer_peek_token (parser
->lexer
);
29307 if (params
== NULL_TREE
)
29309 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29310 return error_mark_node
;
29313 /* We allow tail attributes for the method. */
29314 if (token
->keyword
== RID_ATTRIBUTE
)
29316 *attributes
= cp_parser_attributes_opt (parser
);
29317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29318 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29320 cp_parser_error (parser
,
29321 "method attributes must be specified at the end");
29322 return error_mark_node
;
29325 if (params
== NULL_TREE
)
29327 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29328 return error_mark_node
;
29333 /* Parse the non-keyword Objective-C params. */
29336 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29339 tree params
= make_node (TREE_LIST
);
29340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29341 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29343 while (token
->type
== CPP_COMMA
)
29345 cp_parameter_declarator
*parmdecl
;
29348 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29349 token
= cp_lexer_peek_token (parser
->lexer
);
29351 if (token
->type
== CPP_ELLIPSIS
)
29353 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29355 token
= cp_lexer_peek_token (parser
->lexer
);
29359 /* TODO: parse attributes for tail parameters. */
29360 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29361 parm
= grokdeclarator (parmdecl
->declarator
,
29362 &parmdecl
->decl_specifiers
,
29363 PARM
, /*initialized=*/0,
29364 /*attrlist=*/NULL
);
29366 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29367 token
= cp_lexer_peek_token (parser
->lexer
);
29370 /* We allow tail attributes for the method. */
29371 if (token
->keyword
== RID_ATTRIBUTE
)
29373 if (*attributes
== NULL_TREE
)
29375 *attributes
= cp_parser_attributes_opt (parser
);
29376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29377 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29381 /* We have an error, but parse the attributes, so that we can
29383 *attributes
= cp_parser_attributes_opt (parser
);
29385 cp_parser_error (parser
,
29386 "method attributes must be specified at the end");
29387 return error_mark_node
;
29393 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29396 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29398 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29400 /* If the next token is `extern' and the following token is a string
29401 literal, then we have a linkage specification. */
29402 if (token
->keyword
== RID_EXTERN
29403 && cp_parser_is_pure_string_literal
29404 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29405 cp_parser_linkage_specification (parser
);
29406 /* Handle #pragma, if any. */
29407 else if (token
->type
== CPP_PRAGMA
)
29408 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29409 /* Allow stray semicolons. */
29410 else if (token
->type
== CPP_SEMICOLON
)
29411 cp_lexer_consume_token (parser
->lexer
);
29412 /* Mark methods as optional or required, when building protocols. */
29413 else if (token
->keyword
== RID_AT_OPTIONAL
)
29415 cp_lexer_consume_token (parser
->lexer
);
29416 objc_set_method_opt (true);
29418 else if (token
->keyword
== RID_AT_REQUIRED
)
29420 cp_lexer_consume_token (parser
->lexer
);
29421 objc_set_method_opt (false);
29423 else if (token
->keyword
== RID_NAMESPACE
)
29424 cp_parser_namespace_definition (parser
);
29425 /* Other stray characters must generate errors. */
29426 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29428 cp_lexer_consume_token (parser
->lexer
);
29429 error ("stray %qs between Objective-C++ methods",
29430 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29432 /* Finally, try to parse a block-declaration, or a function-definition. */
29434 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29437 /* Parse a method signature. */
29440 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29442 tree rettype
, kwdparms
, optparms
;
29443 bool ellipsis
= false;
29444 bool is_class_method
;
29446 is_class_method
= cp_parser_objc_method_type (parser
);
29447 rettype
= cp_parser_objc_typename (parser
);
29448 *attributes
= NULL_TREE
;
29449 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29450 if (kwdparms
== error_mark_node
)
29451 return error_mark_node
;
29452 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29453 if (optparms
== error_mark_node
)
29454 return error_mark_node
;
29456 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29460 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29463 cp_lexer_save_tokens (parser
->lexer
);
29464 tattr
= cp_parser_attributes_opt (parser
);
29465 gcc_assert (tattr
) ;
29467 /* If the attributes are followed by a method introducer, this is not allowed.
29468 Dump the attributes and flag the situation. */
29469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29470 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29473 /* Otherwise, the attributes introduce some interstitial code, possibly so
29474 rewind to allow that check. */
29475 cp_lexer_rollback_tokens (parser
->lexer
);
29479 /* Parse an Objective-C method prototype list. */
29482 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29484 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29486 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29488 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29490 tree attributes
, sig
;
29491 bool is_class_method
;
29492 if (token
->type
== CPP_PLUS
)
29493 is_class_method
= true;
29495 is_class_method
= false;
29496 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29497 if (sig
== error_mark_node
)
29499 cp_parser_skip_to_end_of_block_or_statement (parser
);
29500 token
= cp_lexer_peek_token (parser
->lexer
);
29503 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29504 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29506 else if (token
->keyword
== RID_AT_PROPERTY
)
29507 cp_parser_objc_at_property_declaration (parser
);
29508 else if (token
->keyword
== RID_ATTRIBUTE
29509 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29510 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29512 "prefix attributes are ignored for methods");
29514 /* Allow for interspersed non-ObjC++ code. */
29515 cp_parser_objc_interstitial_code (parser
);
29517 token
= cp_lexer_peek_token (parser
->lexer
);
29520 if (token
->type
!= CPP_EOF
)
29521 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29523 cp_parser_error (parser
, "expected %<@end%>");
29525 objc_finish_interface ();
29528 /* Parse an Objective-C method definition list. */
29531 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29533 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29535 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29539 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29542 tree sig
, attribute
;
29543 bool is_class_method
;
29544 if (token
->type
== CPP_PLUS
)
29545 is_class_method
= true;
29547 is_class_method
= false;
29548 push_deferring_access_checks (dk_deferred
);
29549 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29550 if (sig
== error_mark_node
)
29552 cp_parser_skip_to_end_of_block_or_statement (parser
);
29553 token
= cp_lexer_peek_token (parser
->lexer
);
29556 objc_start_method_definition (is_class_method
, sig
, attribute
,
29559 /* For historical reasons, we accept an optional semicolon. */
29560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29561 cp_lexer_consume_token (parser
->lexer
);
29563 ptk
= cp_lexer_peek_token (parser
->lexer
);
29564 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29565 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29567 perform_deferred_access_checks (tf_warning_or_error
);
29568 stop_deferring_access_checks ();
29569 meth
= cp_parser_function_definition_after_declarator (parser
,
29571 pop_deferring_access_checks ();
29572 objc_finish_method_definition (meth
);
29575 /* The following case will be removed once @synthesize is
29576 completely implemented. */
29577 else if (token
->keyword
== RID_AT_PROPERTY
)
29578 cp_parser_objc_at_property_declaration (parser
);
29579 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
29580 cp_parser_objc_at_synthesize_declaration (parser
);
29581 else if (token
->keyword
== RID_AT_DYNAMIC
)
29582 cp_parser_objc_at_dynamic_declaration (parser
);
29583 else if (token
->keyword
== RID_ATTRIBUTE
29584 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29585 warning_at (token
->location
, OPT_Wattributes
,
29586 "prefix attributes are ignored for methods");
29588 /* Allow for interspersed non-ObjC++ code. */
29589 cp_parser_objc_interstitial_code (parser
);
29591 token
= cp_lexer_peek_token (parser
->lexer
);
29594 if (token
->type
!= CPP_EOF
)
29595 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29597 cp_parser_error (parser
, "expected %<@end%>");
29599 objc_finish_implementation ();
29602 /* Parse Objective-C ivars. */
29605 cp_parser_objc_class_ivars (cp_parser
* parser
)
29607 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29609 if (token
->type
!= CPP_OPEN_BRACE
)
29610 return; /* No ivars specified. */
29612 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
29613 token
= cp_lexer_peek_token (parser
->lexer
);
29615 while (token
->type
!= CPP_CLOSE_BRACE
29616 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29618 cp_decl_specifier_seq declspecs
;
29619 int decl_class_or_enum_p
;
29620 tree prefix_attributes
;
29622 cp_parser_objc_visibility_spec (parser
);
29624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29627 cp_parser_decl_specifier_seq (parser
,
29628 CP_PARSER_FLAGS_OPTIONAL
,
29630 &decl_class_or_enum_p
);
29632 /* auto, register, static, extern, mutable. */
29633 if (declspecs
.storage_class
!= sc_none
)
29635 cp_parser_error (parser
, "invalid type for instance variable");
29636 declspecs
.storage_class
= sc_none
;
29639 /* thread_local. */
29640 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
29642 cp_parser_error (parser
, "invalid type for instance variable");
29643 declspecs
.locations
[ds_thread
] = 0;
29647 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
29649 cp_parser_error (parser
, "invalid type for instance variable");
29650 declspecs
.locations
[ds_typedef
] = 0;
29653 prefix_attributes
= declspecs
.attributes
;
29654 declspecs
.attributes
= NULL_TREE
;
29656 /* Keep going until we hit the `;' at the end of the
29658 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29660 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
29661 cp_declarator
*declarator
= NULL
;
29662 int ctor_dtor_or_conv_p
;
29664 /* Check for a (possibly unnamed) bitfield declaration. */
29665 token
= cp_lexer_peek_token (parser
->lexer
);
29666 if (token
->type
== CPP_COLON
)
29669 if (token
->type
== CPP_NAME
29670 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
29673 /* Get the name of the bitfield. */
29674 declarator
= make_id_declarator (NULL_TREE
,
29675 cp_parser_identifier (parser
),
29679 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29680 /* Get the width of the bitfield. */
29682 = cp_parser_constant_expression (parser
);
29686 /* Parse the declarator. */
29688 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29689 &ctor_dtor_or_conv_p
,
29690 /*parenthesized_p=*/NULL
,
29691 /*member_p=*/false,
29692 /*friend_p=*/false);
29695 /* Look for attributes that apply to the ivar. */
29696 attributes
= cp_parser_attributes_opt (parser
);
29697 /* Remember which attributes are prefix attributes and
29699 first_attribute
= attributes
;
29700 /* Combine the attributes. */
29701 attributes
= chainon (prefix_attributes
, attributes
);
29704 /* Create the bitfield declaration. */
29705 decl
= grokbitfield (declarator
, &declspecs
,
29709 decl
= grokfield (declarator
, &declspecs
,
29710 NULL_TREE
, /*init_const_expr_p=*/false,
29711 NULL_TREE
, attributes
);
29713 /* Add the instance variable. */
29714 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
29715 objc_add_instance_variable (decl
);
29717 /* Reset PREFIX_ATTRIBUTES. */
29718 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
29719 attributes
= TREE_CHAIN (attributes
);
29721 TREE_CHAIN (attributes
) = NULL_TREE
;
29723 token
= cp_lexer_peek_token (parser
->lexer
);
29725 if (token
->type
== CPP_COMMA
)
29727 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29733 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29734 token
= cp_lexer_peek_token (parser
->lexer
);
29737 if (token
->keyword
== RID_AT_END
)
29738 cp_parser_error (parser
, "expected %<}%>");
29740 /* Do not consume the RID_AT_END, so it will be read again as terminating
29741 the @interface of @implementation. */
29742 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29743 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
29745 /* For historical reasons, we accept an optional semicolon. */
29746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29747 cp_lexer_consume_token (parser
->lexer
);
29750 /* Parse an Objective-C protocol declaration. */
29753 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
29755 tree proto
, protorefs
;
29758 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29759 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
29761 tok
= cp_lexer_peek_token (parser
->lexer
);
29762 error_at (tok
->location
, "identifier expected after %<@protocol%>");
29763 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29767 /* See if we have a forward declaration or a definition. */
29768 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29770 /* Try a forward declaration first. */
29771 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
29777 id
= cp_parser_identifier (parser
);
29778 if (id
== error_mark_node
)
29781 objc_declare_protocol (id
, attributes
);
29783 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29784 cp_lexer_consume_token (parser
->lexer
);
29788 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29791 /* Ok, we got a full-fledged definition (or at least should). */
29794 proto
= cp_parser_identifier (parser
);
29795 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
29796 objc_start_protocol (proto
, protorefs
, attributes
);
29797 cp_parser_objc_method_prototype_list (parser
);
29801 /* Parse an Objective-C superclass or category. */
29804 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
29807 tree
*categ
, bool *is_class_extension
)
29809 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
29811 *super
= *categ
= NULL_TREE
;
29812 *is_class_extension
= false;
29813 if (next
->type
== CPP_COLON
)
29815 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29816 *super
= cp_parser_identifier (parser
);
29818 else if (next
->type
== CPP_OPEN_PAREN
)
29820 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29822 /* If there is no category name, and this is an @interface, we
29823 have a class extension. */
29824 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
29826 *categ
= NULL_TREE
;
29827 *is_class_extension
= true;
29830 *categ
= cp_parser_identifier (parser
);
29832 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29836 /* Parse an Objective-C class interface. */
29839 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
29841 tree name
, super
, categ
, protos
;
29842 bool is_class_extension
;
29844 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
29845 name
= cp_parser_identifier (parser
);
29846 if (name
== error_mark_node
)
29848 /* It's hard to recover because even if valid @interface stuff
29849 is to follow, we can't compile it (or validate it) if we
29850 don't even know which class it refers to. Let's assume this
29851 was a stray '@interface' token in the stream and skip it.
29855 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
29856 &is_class_extension
);
29857 protos
= cp_parser_objc_protocol_refs_opt (parser
);
29859 /* We have either a class or a category on our hands. */
29860 if (categ
|| is_class_extension
)
29861 objc_start_category_interface (name
, categ
, protos
, attributes
);
29864 objc_start_class_interface (name
, super
, protos
, attributes
);
29865 /* Handle instance variable declarations, if any. */
29866 cp_parser_objc_class_ivars (parser
);
29867 objc_continue_interface ();
29870 cp_parser_objc_method_prototype_list (parser
);
29873 /* Parse an Objective-C class implementation. */
29876 cp_parser_objc_class_implementation (cp_parser
* parser
)
29878 tree name
, super
, categ
;
29879 bool is_class_extension
;
29881 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
29882 name
= cp_parser_identifier (parser
);
29883 if (name
== error_mark_node
)
29885 /* It's hard to recover because even if valid @implementation
29886 stuff is to follow, we can't compile it (or validate it) if
29887 we don't even know which class it refers to. Let's assume
29888 this was a stray '@implementation' token in the stream and
29893 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
29894 &is_class_extension
);
29896 /* We have either a class or a category on our hands. */
29898 objc_start_category_implementation (name
, categ
);
29901 objc_start_class_implementation (name
, super
);
29902 /* Handle instance variable declarations, if any. */
29903 cp_parser_objc_class_ivars (parser
);
29904 objc_continue_implementation ();
29907 cp_parser_objc_method_definition_list (parser
);
29910 /* Consume the @end token and finish off the implementation. */
29913 cp_parser_objc_end_implementation (cp_parser
* parser
)
29915 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29916 objc_finish_implementation ();
29919 /* Parse an Objective-C declaration. */
29922 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
29924 /* Try to figure out what kind of declaration is present. */
29925 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29928 switch (kwd
->keyword
)
29933 error_at (kwd
->location
, "attributes may not be specified before"
29934 " the %<@%D%> Objective-C++ keyword",
29938 case RID_AT_IMPLEMENTATION
:
29939 warning_at (kwd
->location
, OPT_Wattributes
,
29940 "prefix attributes are ignored before %<@%D%>",
29947 switch (kwd
->keyword
)
29950 cp_parser_objc_alias_declaration (parser
);
29953 cp_parser_objc_class_declaration (parser
);
29955 case RID_AT_PROTOCOL
:
29956 cp_parser_objc_protocol_declaration (parser
, attributes
);
29958 case RID_AT_INTERFACE
:
29959 cp_parser_objc_class_interface (parser
, attributes
);
29961 case RID_AT_IMPLEMENTATION
:
29962 cp_parser_objc_class_implementation (parser
);
29965 cp_parser_objc_end_implementation (parser
);
29968 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
29970 cp_parser_skip_to_end_of_block_or_statement (parser
);
29974 /* Parse an Objective-C try-catch-finally statement.
29976 objc-try-catch-finally-stmt:
29977 @try compound-statement objc-catch-clause-seq [opt]
29978 objc-finally-clause [opt]
29980 objc-catch-clause-seq:
29981 objc-catch-clause objc-catch-clause-seq [opt]
29984 @catch ( objc-exception-declaration ) compound-statement
29986 objc-finally-clause:
29987 @finally compound-statement
29989 objc-exception-declaration:
29990 parameter-declaration
29993 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29997 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29998 for C. Keep them in sync. */
30001 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30003 location_t location
;
30006 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30007 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30008 objc_maybe_warn_exceptions (location
);
30009 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30010 node, lest it get absorbed into the surrounding block. */
30011 stmt
= push_stmt_list ();
30012 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30013 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30015 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30017 cp_parameter_declarator
*parm
;
30018 tree parameter_declaration
= error_mark_node
;
30019 bool seen_open_paren
= false;
30021 cp_lexer_consume_token (parser
->lexer
);
30022 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30023 seen_open_paren
= true;
30024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30026 /* We have "@catch (...)" (where the '...' are literally
30027 what is in the code). Skip the '...'.
30028 parameter_declaration is set to NULL_TREE, and
30029 objc_being_catch_clauses() knows that that means
30031 cp_lexer_consume_token (parser
->lexer
);
30032 parameter_declaration
= NULL_TREE
;
30036 /* We have "@catch (NSException *exception)" or something
30037 like that. Parse the parameter declaration. */
30038 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30040 parameter_declaration
= error_mark_node
;
30042 parameter_declaration
= grokdeclarator (parm
->declarator
,
30043 &parm
->decl_specifiers
,
30044 PARM
, /*initialized=*/0,
30045 /*attrlist=*/NULL
);
30047 if (seen_open_paren
)
30048 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30051 /* If there was no open parenthesis, we are recovering from
30052 an error, and we are trying to figure out what mistake
30053 the user has made. */
30055 /* If there is an immediate closing parenthesis, the user
30056 probably forgot the opening one (ie, they typed "@catch
30057 NSException *e)". Parse the closing parenthesis and keep
30059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30060 cp_lexer_consume_token (parser
->lexer
);
30062 /* If these is no immediate closing parenthesis, the user
30063 probably doesn't know that parenthesis are required at
30064 all (ie, they typed "@catch NSException *e"). So, just
30065 forget about the closing parenthesis and keep going. */
30067 objc_begin_catch_clause (parameter_declaration
);
30068 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30069 objc_finish_catch_clause ();
30071 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30073 cp_lexer_consume_token (parser
->lexer
);
30074 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30075 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30076 node, lest it get absorbed into the surrounding block. */
30077 stmt
= push_stmt_list ();
30078 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30079 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30082 return objc_finish_try_stmt ();
30085 /* Parse an Objective-C synchronized statement.
30087 objc-synchronized-stmt:
30088 @synchronized ( expression ) compound-statement
30090 Returns NULL_TREE. */
30093 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30095 location_t location
;
30098 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30100 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30101 objc_maybe_warn_exceptions (location
);
30102 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
30103 lock
= cp_parser_expression (parser
);
30104 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30106 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30107 node, lest it get absorbed into the surrounding block. */
30108 stmt
= push_stmt_list ();
30109 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30111 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30114 /* Parse an Objective-C throw statement.
30117 @throw assignment-expression [opt] ;
30119 Returns a constructed '@throw' statement. */
30122 cp_parser_objc_throw_statement (cp_parser
*parser
)
30124 tree expr
= NULL_TREE
;
30125 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30127 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30129 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30130 expr
= cp_parser_expression (parser
);
30132 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30134 return objc_build_throw_stmt (loc
, expr
);
30137 /* Parse an Objective-C statement. */
30140 cp_parser_objc_statement (cp_parser
* parser
)
30142 /* Try to figure out what kind of declaration is present. */
30143 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30145 switch (kwd
->keyword
)
30148 return cp_parser_objc_try_catch_finally_statement (parser
);
30149 case RID_AT_SYNCHRONIZED
:
30150 return cp_parser_objc_synchronized_statement (parser
);
30152 return cp_parser_objc_throw_statement (parser
);
30154 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30156 cp_parser_skip_to_end_of_block_or_statement (parser
);
30159 return error_mark_node
;
30162 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30163 look ahead to see if an objc keyword follows the attributes. This
30164 is to detect the use of prefix attributes on ObjC @interface and
30168 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30170 cp_lexer_save_tokens (parser
->lexer
);
30171 *attrib
= cp_parser_attributes_opt (parser
);
30172 gcc_assert (*attrib
);
30173 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30175 cp_lexer_commit_tokens (parser
->lexer
);
30178 cp_lexer_rollback_tokens (parser
->lexer
);
30182 /* This routine is a minimal replacement for
30183 c_parser_struct_declaration () used when parsing the list of
30184 types/names or ObjC++ properties. For example, when parsing the
30187 @property (readonly) int a, b, c;
30189 this function is responsible for parsing "int a, int b, int c" and
30190 returning the declarations as CHAIN of DECLs.
30192 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30193 similar parsing. */
30195 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30197 tree decls
= NULL_TREE
;
30198 cp_decl_specifier_seq declspecs
;
30199 int decl_class_or_enum_p
;
30200 tree prefix_attributes
;
30202 cp_parser_decl_specifier_seq (parser
,
30203 CP_PARSER_FLAGS_NONE
,
30205 &decl_class_or_enum_p
);
30207 if (declspecs
.type
== error_mark_node
)
30208 return error_mark_node
;
30210 /* auto, register, static, extern, mutable. */
30211 if (declspecs
.storage_class
!= sc_none
)
30213 cp_parser_error (parser
, "invalid type for property");
30214 declspecs
.storage_class
= sc_none
;
30217 /* thread_local. */
30218 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30220 cp_parser_error (parser
, "invalid type for property");
30221 declspecs
.locations
[ds_thread
] = 0;
30225 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30227 cp_parser_error (parser
, "invalid type for property");
30228 declspecs
.locations
[ds_typedef
] = 0;
30231 prefix_attributes
= declspecs
.attributes
;
30232 declspecs
.attributes
= NULL_TREE
;
30234 /* Keep going until we hit the `;' at the end of the declaration. */
30235 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30237 tree attributes
, first_attribute
, decl
;
30238 cp_declarator
*declarator
;
30241 /* Parse the declarator. */
30242 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30243 NULL
, NULL
, false, false);
30245 /* Look for attributes that apply to the ivar. */
30246 attributes
= cp_parser_attributes_opt (parser
);
30247 /* Remember which attributes are prefix attributes and
30249 first_attribute
= attributes
;
30250 /* Combine the attributes. */
30251 attributes
= chainon (prefix_attributes
, attributes
);
30253 decl
= grokfield (declarator
, &declspecs
,
30254 NULL_TREE
, /*init_const_expr_p=*/false,
30255 NULL_TREE
, attributes
);
30257 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30258 return error_mark_node
;
30260 /* Reset PREFIX_ATTRIBUTES. */
30261 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30262 attributes
= TREE_CHAIN (attributes
);
30264 TREE_CHAIN (attributes
) = NULL_TREE
;
30266 DECL_CHAIN (decl
) = decls
;
30269 token
= cp_lexer_peek_token (parser
->lexer
);
30270 if (token
->type
== CPP_COMMA
)
30272 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30281 /* Parse an Objective-C @property declaration. The syntax is:
30283 objc-property-declaration:
30284 '@property' objc-property-attributes[opt] struct-declaration ;
30286 objc-property-attributes:
30287 '(' objc-property-attribute-list ')'
30289 objc-property-attribute-list:
30290 objc-property-attribute
30291 objc-property-attribute-list, objc-property-attribute
30293 objc-property-attribute
30294 'getter' = identifier
30295 'setter' = identifier
30304 @property NSString *name;
30305 @property (readonly) id object;
30306 @property (retain, nonatomic, getter=getTheName) id name;
30307 @property int a, b, c;
30309 PS: This function is identical to
30310 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30312 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30314 /* The following variables hold the attributes of the properties as
30315 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30316 seen. When we see an attribute, we set them to 'true' (if they
30317 are boolean properties) or to the identifier (if they have an
30318 argument, ie, for getter and setter). Note that here we only
30319 parse the list of attributes, check the syntax and accumulate the
30320 attributes that we find. objc_add_property_declaration() will
30321 then process the information. */
30322 bool property_assign
= false;
30323 bool property_copy
= false;
30324 tree property_getter_ident
= NULL_TREE
;
30325 bool property_nonatomic
= false;
30326 bool property_readonly
= false;
30327 bool property_readwrite
= false;
30328 bool property_retain
= false;
30329 tree property_setter_ident
= NULL_TREE
;
30331 /* 'properties' is the list of properties that we read. Usually a
30332 single one, but maybe more (eg, in "@property int a, b, c;" there
30337 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30339 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30341 /* Parse the optional attribute list... */
30342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30345 cp_lexer_consume_token (parser
->lexer
);
30349 bool syntax_error
= false;
30350 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30353 if (token
->type
!= CPP_NAME
)
30355 cp_parser_error (parser
, "expected identifier");
30358 keyword
= C_RID_CODE (token
->u
.value
);
30359 cp_lexer_consume_token (parser
->lexer
);
30362 case RID_ASSIGN
: property_assign
= true; break;
30363 case RID_COPY
: property_copy
= true; break;
30364 case RID_NONATOMIC
: property_nonatomic
= true; break;
30365 case RID_READONLY
: property_readonly
= true; break;
30366 case RID_READWRITE
: property_readwrite
= true; break;
30367 case RID_RETAIN
: property_retain
= true; break;
30371 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30373 if (keyword
== RID_GETTER
)
30374 cp_parser_error (parser
,
30375 "missing %<=%> (after %<getter%> attribute)");
30377 cp_parser_error (parser
,
30378 "missing %<=%> (after %<setter%> attribute)");
30379 syntax_error
= true;
30382 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30383 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30385 cp_parser_error (parser
, "expected identifier");
30386 syntax_error
= true;
30389 if (keyword
== RID_SETTER
)
30391 if (property_setter_ident
!= NULL_TREE
)
30393 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30394 cp_lexer_consume_token (parser
->lexer
);
30397 property_setter_ident
= cp_parser_objc_selector (parser
);
30398 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30399 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30401 cp_lexer_consume_token (parser
->lexer
);
30405 if (property_getter_ident
!= NULL_TREE
)
30407 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30408 cp_lexer_consume_token (parser
->lexer
);
30411 property_getter_ident
= cp_parser_objc_selector (parser
);
30415 cp_parser_error (parser
, "unknown property attribute");
30416 syntax_error
= true;
30423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30424 cp_lexer_consume_token (parser
->lexer
);
30429 /* FIXME: "@property (setter, assign);" will generate a spurious
30430 "error: expected ‘)’ before ‘,’ token". This is because
30431 cp_parser_require, unlike the C counterpart, will produce an
30432 error even if we are in error recovery. */
30433 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30435 cp_parser_skip_to_closing_parenthesis (parser
,
30436 /*recovering=*/true,
30437 /*or_comma=*/false,
30438 /*consume_paren=*/true);
30442 /* ... and the property declaration(s). */
30443 properties
= cp_parser_objc_struct_declaration (parser
);
30445 if (properties
== error_mark_node
)
30447 cp_parser_skip_to_end_of_statement (parser
);
30448 /* If the next token is now a `;', consume it. */
30449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30450 cp_lexer_consume_token (parser
->lexer
);
30454 if (properties
== NULL_TREE
)
30455 cp_parser_error (parser
, "expected identifier");
30458 /* Comma-separated properties are chained together in
30459 reverse order; add them one by one. */
30460 properties
= nreverse (properties
);
30462 for (; properties
; properties
= TREE_CHAIN (properties
))
30463 objc_add_property_declaration (loc
, copy_node (properties
),
30464 property_readonly
, property_readwrite
,
30465 property_assign
, property_retain
,
30466 property_copy
, property_nonatomic
,
30467 property_getter_ident
, property_setter_ident
);
30470 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30473 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30475 objc-synthesize-declaration:
30476 @synthesize objc-synthesize-identifier-list ;
30478 objc-synthesize-identifier-list:
30479 objc-synthesize-identifier
30480 objc-synthesize-identifier-list, objc-synthesize-identifier
30482 objc-synthesize-identifier
30484 identifier = identifier
30487 @synthesize MyProperty;
30488 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30490 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30491 for C. Keep them in sync.
30494 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30496 tree list
= NULL_TREE
;
30498 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30500 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30503 tree property
, ivar
;
30504 property
= cp_parser_identifier (parser
);
30505 if (property
== error_mark_node
)
30507 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30512 cp_lexer_consume_token (parser
->lexer
);
30513 ivar
= cp_parser_identifier (parser
);
30514 if (ivar
== error_mark_node
)
30516 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30522 list
= chainon (list
, build_tree_list (ivar
, property
));
30523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30524 cp_lexer_consume_token (parser
->lexer
);
30528 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30529 objc_add_synthesize_declaration (loc
, list
);
30532 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30534 objc-dynamic-declaration:
30535 @dynamic identifier-list ;
30538 @dynamic MyProperty;
30539 @dynamic MyProperty, AnotherProperty;
30541 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30542 for C. Keep them in sync.
30545 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30547 tree list
= NULL_TREE
;
30549 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30551 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30555 property
= cp_parser_identifier (parser
);
30556 if (property
== error_mark_node
)
30558 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30561 list
= chainon (list
, build_tree_list (NULL
, property
));
30562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30563 cp_lexer_consume_token (parser
->lexer
);
30567 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30568 objc_add_dynamic_declaration (loc
, list
);
30572 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30574 /* Returns name of the next clause.
30575 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30576 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30577 returned and the token is consumed. */
30579 static pragma_omp_clause
30580 cp_parser_omp_clause_name (cp_parser
*parser
)
30582 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
30584 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30585 result
= PRAGMA_OACC_CLAUSE_AUTO
;
30586 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
30587 result
= PRAGMA_OMP_CLAUSE_IF
;
30588 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
30589 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
30590 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
30591 result
= PRAGMA_OACC_CLAUSE_DELETE
;
30592 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
30593 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
30594 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30595 result
= PRAGMA_OMP_CLAUSE_FOR
;
30596 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30598 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30599 const char *p
= IDENTIFIER_POINTER (id
);
30604 if (!strcmp ("aligned", p
))
30605 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
30606 else if (!strcmp ("async", p
))
30607 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
30610 if (!strcmp ("collapse", p
))
30611 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
30612 else if (!strcmp ("copy", p
))
30613 result
= PRAGMA_OACC_CLAUSE_COPY
;
30614 else if (!strcmp ("copyin", p
))
30615 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
30616 else if (!strcmp ("copyout", p
))
30617 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
30618 else if (!strcmp ("copyprivate", p
))
30619 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
30620 else if (!strcmp ("create", p
))
30621 result
= PRAGMA_OACC_CLAUSE_CREATE
;
30624 if (!strcmp ("defaultmap", p
))
30625 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
30626 else if (!strcmp ("depend", p
))
30627 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
30628 else if (!strcmp ("device", p
))
30629 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
30630 else if (!strcmp ("deviceptr", p
))
30631 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
30632 else if (!strcmp ("device_resident", p
))
30633 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
30634 else if (!strcmp ("dist_schedule", p
))
30635 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
30638 if (!strcmp ("final", p
))
30639 result
= PRAGMA_OMP_CLAUSE_FINAL
;
30640 else if (!strcmp ("firstprivate", p
))
30641 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
30642 else if (!strcmp ("from", p
))
30643 result
= PRAGMA_OMP_CLAUSE_FROM
;
30646 if (!strcmp ("gang", p
))
30647 result
= PRAGMA_OACC_CLAUSE_GANG
;
30648 else if (!strcmp ("grainsize", p
))
30649 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
30652 if (!strcmp ("hint", p
))
30653 result
= PRAGMA_OMP_CLAUSE_HINT
;
30654 else if (!strcmp ("host", p
))
30655 result
= PRAGMA_OACC_CLAUSE_HOST
;
30658 if (!strcmp ("inbranch", p
))
30659 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
30660 else if (!strcmp ("independent", p
))
30661 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
30662 else if (!strcmp ("is_device_ptr", p
))
30663 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
30666 if (!strcmp ("lastprivate", p
))
30667 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
30668 else if (!strcmp ("linear", p
))
30669 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
30670 else if (!strcmp ("link", p
))
30671 result
= PRAGMA_OMP_CLAUSE_LINK
;
30674 if (!strcmp ("map", p
))
30675 result
= PRAGMA_OMP_CLAUSE_MAP
;
30676 else if (!strcmp ("mergeable", p
))
30677 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
30678 else if (flag_cilkplus
&& !strcmp ("mask", p
))
30679 result
= PRAGMA_CILK_CLAUSE_MASK
;
30682 if (!strcmp ("nogroup", p
))
30683 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
30684 else if (!strcmp ("notinbranch", p
))
30685 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
30686 else if (!strcmp ("nowait", p
))
30687 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
30688 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
30689 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
30690 else if (!strcmp ("num_gangs", p
))
30691 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
30692 else if (!strcmp ("num_tasks", p
))
30693 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
30694 else if (!strcmp ("num_teams", p
))
30695 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
30696 else if (!strcmp ("num_threads", p
))
30697 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
30698 else if (!strcmp ("num_workers", p
))
30699 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
30702 if (!strcmp ("ordered", p
))
30703 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
30706 if (!strcmp ("parallel", p
))
30707 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
30708 else if (!strcmp ("present", p
))
30709 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
30710 else if (!strcmp ("present_or_copy", p
)
30711 || !strcmp ("pcopy", p
))
30712 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
30713 else if (!strcmp ("present_or_copyin", p
)
30714 || !strcmp ("pcopyin", p
))
30715 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
30716 else if (!strcmp ("present_or_copyout", p
)
30717 || !strcmp ("pcopyout", p
))
30718 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
30719 else if (!strcmp ("present_or_create", p
)
30720 || !strcmp ("pcreate", p
))
30721 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
30722 else if (!strcmp ("priority", p
))
30723 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
30724 else if (!strcmp ("proc_bind", p
))
30725 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
30728 if (!strcmp ("reduction", p
))
30729 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
30732 if (!strcmp ("safelen", p
))
30733 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
30734 else if (!strcmp ("schedule", p
))
30735 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
30736 else if (!strcmp ("sections", p
))
30737 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
30738 else if (!strcmp ("self", p
))
30739 result
= PRAGMA_OACC_CLAUSE_SELF
;
30740 else if (!strcmp ("seq", p
))
30741 result
= PRAGMA_OACC_CLAUSE_SEQ
;
30742 else if (!strcmp ("shared", p
))
30743 result
= PRAGMA_OMP_CLAUSE_SHARED
;
30744 else if (!strcmp ("simd", p
))
30745 result
= PRAGMA_OMP_CLAUSE_SIMD
;
30746 else if (!strcmp ("simdlen", p
))
30747 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
30750 if (!strcmp ("taskgroup", p
))
30751 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
30752 else if (!strcmp ("thread_limit", p
))
30753 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
30754 else if (!strcmp ("threads", p
))
30755 result
= PRAGMA_OMP_CLAUSE_THREADS
;
30756 else if (!strcmp ("tile", p
))
30757 result
= PRAGMA_OACC_CLAUSE_TILE
;
30758 else if (!strcmp ("to", p
))
30759 result
= PRAGMA_OMP_CLAUSE_TO
;
30762 if (!strcmp ("uniform", p
))
30763 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
30764 else if (!strcmp ("untied", p
))
30765 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
30766 else if (!strcmp ("use_device", p
))
30767 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
30768 else if (!strcmp ("use_device_ptr", p
))
30769 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
30772 if (!strcmp ("vector", p
))
30773 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
30774 else if (!strcmp ("vector_length", p
))
30775 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
30776 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
30777 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
30780 if (!strcmp ("wait", p
))
30781 result
= PRAGMA_OACC_CLAUSE_WAIT
;
30782 else if (!strcmp ("worker", p
))
30783 result
= PRAGMA_OACC_CLAUSE_WORKER
;
30788 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
30789 cp_lexer_consume_token (parser
->lexer
);
30794 /* Validate that a clause of the given type does not already exist. */
30797 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
30798 const char *name
, location_t location
)
30802 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30803 if (OMP_CLAUSE_CODE (c
) == code
)
30805 error_at (location
, "too many %qs clauses", name
);
30813 variable-list , identifier
30815 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30816 colon). An opening parenthesis will have been consumed by the caller.
30818 If KIND is nonzero, create the appropriate node and install the decl
30819 in OMP_CLAUSE_DECL and add the node to the head of the list.
30821 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30822 return the list created.
30824 COLON can be NULL if only closing parenthesis should end the list,
30825 or pointer to bool which will receive false if the list is terminated
30826 by closing parenthesis or true if the list is terminated by colon. */
30829 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
30830 tree list
, bool *colon
)
30833 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30836 parser
->colon_corrects_to_scope_p
= false;
30843 token
= cp_lexer_peek_token (parser
->lexer
);
30845 && current_class_ptr
30846 && cp_parser_is_keyword (token
, RID_THIS
))
30848 decl
= finish_this_expr ();
30849 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
30850 || CONVERT_EXPR_P (decl
))
30851 decl
= TREE_OPERAND (decl
, 0);
30852 cp_lexer_consume_token (parser
->lexer
);
30856 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30857 /*check_dependency_p=*/true,
30858 /*template_p=*/NULL
,
30859 /*declarator_p=*/false,
30860 /*optional_p=*/false);
30861 if (name
== error_mark_node
)
30864 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
30865 if (decl
== error_mark_node
)
30866 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
30869 if (decl
== error_mark_node
)
30871 else if (kind
!= 0)
30875 case OMP_CLAUSE__CACHE_
:
30876 /* The OpenACC cache directive explicitly only allows "array
30877 elements or subarrays". */
30878 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
30880 error_at (token
->location
, "expected %<[%>");
30881 decl
= error_mark_node
;
30885 case OMP_CLAUSE_MAP
:
30886 case OMP_CLAUSE_FROM
:
30887 case OMP_CLAUSE_TO
:
30888 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
30891 = cp_lexer_peek_token (parser
->lexer
)->location
;
30892 cp_id_kind idk
= CP_ID_KIND_NONE
;
30893 cp_lexer_consume_token (parser
->lexer
);
30894 decl
= convert_from_reference (decl
);
30896 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
30901 case OMP_CLAUSE_DEPEND
:
30902 case OMP_CLAUSE_REDUCTION
:
30903 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
30905 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
30907 parser
->colon_corrects_to_scope_p
= false;
30908 cp_lexer_consume_token (parser
->lexer
);
30909 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30910 low_bound
= cp_parser_expression (parser
);
30912 parser
->colon_corrects_to_scope_p
30913 = saved_colon_corrects_to_scope_p
;
30914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
30915 length
= integer_one_node
;
30918 /* Look for `:'. */
30919 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30921 if (!cp_lexer_next_token_is (parser
->lexer
,
30923 length
= cp_parser_expression (parser
);
30925 /* Look for the closing `]'. */
30926 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
30930 decl
= tree_cons (low_bound
, length
, decl
);
30937 tree u
= build_omp_clause (token
->location
, kind
);
30938 OMP_CLAUSE_DECL (u
) = decl
;
30939 OMP_CLAUSE_CHAIN (u
) = list
;
30943 list
= tree_cons (decl
, NULL_TREE
, list
);
30946 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
30948 cp_lexer_consume_token (parser
->lexer
);
30952 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30954 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30957 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30961 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30965 /* Try to resync to an unnested comma. Copied from
30966 cp_parser_parenthesized_expression_list. */
30969 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30970 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
30971 /*recovering=*/true,
30973 /*consume_paren=*/true);
30981 /* Similarly, but expect leading and trailing parenthesis. This is a very
30982 common case for omp clauses. */
30985 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
30987 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30988 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
30993 copy ( variable-list )
30994 copyin ( variable-list )
30995 copyout ( variable-list )
30996 create ( variable-list )
30997 delete ( variable-list )
30998 present ( variable-list )
30999 present_or_copy ( variable-list )
31000 pcopy ( variable-list )
31001 present_or_copyin ( variable-list )
31002 pcopyin ( variable-list )
31003 present_or_copyout ( variable-list )
31004 pcopyout ( variable-list )
31005 present_or_create ( variable-list )
31006 pcreate ( variable-list ) */
31009 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31012 enum gomp_map_kind kind
;
31015 case PRAGMA_OACC_CLAUSE_COPY
:
31016 kind
= GOMP_MAP_FORCE_TOFROM
;
31018 case PRAGMA_OACC_CLAUSE_COPYIN
:
31019 kind
= GOMP_MAP_FORCE_TO
;
31021 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31022 kind
= GOMP_MAP_FORCE_FROM
;
31024 case PRAGMA_OACC_CLAUSE_CREATE
:
31025 kind
= GOMP_MAP_FORCE_ALLOC
;
31027 case PRAGMA_OACC_CLAUSE_DELETE
:
31028 kind
= GOMP_MAP_DELETE
;
31030 case PRAGMA_OACC_CLAUSE_DEVICE
:
31031 kind
= GOMP_MAP_FORCE_TO
;
31033 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31034 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31036 case PRAGMA_OACC_CLAUSE_HOST
:
31037 case PRAGMA_OACC_CLAUSE_SELF
:
31038 kind
= GOMP_MAP_FORCE_FROM
;
31040 case PRAGMA_OACC_CLAUSE_LINK
:
31041 kind
= GOMP_MAP_LINK
;
31043 case PRAGMA_OACC_CLAUSE_PRESENT
:
31044 kind
= GOMP_MAP_FORCE_PRESENT
;
31046 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31047 kind
= GOMP_MAP_TOFROM
;
31049 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31050 kind
= GOMP_MAP_TO
;
31052 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31053 kind
= GOMP_MAP_FROM
;
31055 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31056 kind
= GOMP_MAP_ALLOC
;
31059 gcc_unreachable ();
31062 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31064 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31065 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31071 deviceptr ( variable-list ) */
31074 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31076 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31079 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31080 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31081 variable-list must only allow for pointer variables. */
31082 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31083 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31085 tree v
= TREE_PURPOSE (t
);
31086 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31087 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31088 OMP_CLAUSE_DECL (u
) = v
;
31089 OMP_CLAUSE_CHAIN (u
) = list
;
31103 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31104 enum omp_clause_code code
,
31105 tree list
, location_t location
)
31107 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31108 tree c
= build_omp_clause (location
, code
);
31109 OMP_CLAUSE_CHAIN (c
) = list
;
31114 num_gangs ( expression )
31115 num_workers ( expression )
31116 vector_length ( expression ) */
31119 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31120 const char *str
, tree list
)
31122 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31124 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31127 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31129 if (t
== error_mark_node
31130 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31132 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31133 /*or_comma=*/false,
31134 /*consume_paren=*/true);
31138 check_no_duplicate_clause (list
, code
, str
, loc
);
31140 tree c
= build_omp_clause (loc
, code
);
31141 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31142 OMP_CLAUSE_CHAIN (c
) = list
;
31148 gang [( gang-arg-list )]
31149 worker [( [num:] int-expr )]
31150 vector [( [length:] int-expr )]
31152 where gang-arg is one of:
31157 and size-expr may be:
31164 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31165 const char *str
, tree list
)
31167 const char *id
= "num";
31168 cp_lexer
*lexer
= parser
->lexer
;
31169 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31170 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31172 if (kind
== OMP_CLAUSE_VECTOR
)
31175 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31177 cp_lexer_consume_token (lexer
);
31181 cp_token
*next
= cp_lexer_peek_token (lexer
);
31184 /* Gang static argument. */
31185 if (kind
== OMP_CLAUSE_GANG
31186 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31188 cp_lexer_consume_token (lexer
);
31190 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31191 goto cleanup_error
;
31194 if (ops
[idx
] != NULL
)
31196 cp_parser_error (parser
, "too many %<static%> arguments");
31197 goto cleanup_error
;
31200 /* Check for the '*' argument. */
31201 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31202 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31203 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31206 cp_lexer_consume_token (lexer
);
31207 ops
[idx
] = integer_minus_one_node
;
31209 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31211 cp_lexer_consume_token (lexer
);
31217 /* Worker num: argument and vector length: arguments. */
31218 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31219 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
31220 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31222 cp_lexer_consume_token (lexer
); /* id */
31223 cp_lexer_consume_token (lexer
); /* ':' */
31226 /* Now collect the actual argument. */
31227 if (ops
[idx
] != NULL_TREE
)
31229 cp_parser_error (parser
, "unexpected argument");
31230 goto cleanup_error
;
31233 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31235 if (expr
== error_mark_node
)
31236 goto cleanup_error
;
31238 mark_exp_read (expr
);
31241 if (kind
== OMP_CLAUSE_GANG
31242 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31244 cp_lexer_consume_token (lexer
);
31251 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31252 goto cleanup_error
;
31255 check_no_duplicate_clause (list
, kind
, str
, loc
);
31257 c
= build_omp_clause (loc
, kind
);
31260 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31262 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31263 OMP_CLAUSE_CHAIN (c
) = list
;
31268 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31273 tile ( size-expr-list ) */
31276 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31278 tree c
, expr
= error_mark_node
;
31279 tree tile
= NULL_TREE
;
31281 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31282 so, but the spec authors never considered such a case and have
31283 differing opinions on what it might mean, including 'not
31285 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31286 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31289 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31294 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31298 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31299 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31301 cp_lexer_consume_token (parser
->lexer
);
31302 expr
= integer_zero_node
;
31305 expr
= cp_parser_constant_expression (parser
);
31307 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31309 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31311 /* Consume the trailing ')'. */
31312 cp_lexer_consume_token (parser
->lexer
);
31314 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31315 tile
= nreverse (tile
);
31316 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31317 OMP_CLAUSE_CHAIN (c
) = list
;
31322 Parse wait clause or directive parameters. */
31325 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31327 vec
<tree
, va_gc
> *args
;
31330 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31332 /*allow_expansion_p=*/true,
31333 /*non_constant_p=*/NULL
);
31335 if (args
== NULL
|| args
->length () == 0)
31337 cp_parser_error (parser
, "expected integer expression before ')'");
31339 release_tree_vector (args
);
31343 args_tree
= build_tree_list_vec (args
);
31345 release_tree_vector (args
);
31347 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31349 tree targ
= TREE_VALUE (t
);
31351 if (targ
!= error_mark_node
)
31353 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31354 error ("%<wait%> expression must be integral");
31357 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31359 mark_rvalue_use (targ
);
31360 OMP_CLAUSE_DECL (c
) = targ
;
31361 OMP_CLAUSE_CHAIN (c
) = list
;
31371 wait ( int-expr-list ) */
31374 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31376 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31378 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31381 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31387 collapse ( constant-expression ) */
31390 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31396 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31397 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31400 num
= cp_parser_constant_expression (parser
);
31402 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31403 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31404 /*or_comma=*/false,
31405 /*consume_paren=*/true);
31407 if (num
== error_mark_node
)
31409 num
= fold_non_dependent_expr (num
);
31410 if (!tree_fits_shwi_p (num
)
31411 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31412 || (n
= tree_to_shwi (num
)) <= 0
31415 error_at (loc
, "collapse argument needs positive constant integer expression");
31419 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31420 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31421 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31422 OMP_CLAUSE_CHAIN (c
) = list
;
31423 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31429 default ( shared | none )
31435 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31436 location_t location
, bool is_oacc
)
31438 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31441 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31445 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31446 const char *p
= IDENTIFIER_POINTER (id
);
31451 if (strcmp ("none", p
) != 0)
31453 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31457 if (strcmp ("shared", p
) != 0 || is_oacc
)
31459 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31466 cp_lexer_consume_token (parser
->lexer
);
31472 cp_parser_error (parser
, "expected %<none%>");
31474 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31477 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31478 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31479 /*or_comma=*/false,
31480 /*consume_paren=*/true);
31482 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31485 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31486 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31487 OMP_CLAUSE_CHAIN (c
) = list
;
31488 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31494 final ( expression ) */
31497 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31501 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31504 t
= cp_parser_condition (parser
);
31506 if (t
== error_mark_node
31507 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31508 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31509 /*or_comma=*/false,
31510 /*consume_paren=*/true);
31512 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31514 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31515 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31516 OMP_CLAUSE_CHAIN (c
) = list
;
31525 if ( directive-name-modifier : expression )
31527 directive-name-modifier:
31528 parallel | task | taskloop | target data | target | target update
31529 | target enter data | target exit data */
31532 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31536 enum tree_code if_modifier
= ERROR_MARK
;
31538 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31541 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31543 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31544 const char *p
= IDENTIFIER_POINTER (id
);
31547 if (strcmp ("parallel", p
) == 0)
31548 if_modifier
= OMP_PARALLEL
;
31549 else if (strcmp ("task", p
) == 0)
31550 if_modifier
= OMP_TASK
;
31551 else if (strcmp ("taskloop", p
) == 0)
31552 if_modifier
= OMP_TASKLOOP
;
31553 else if (strcmp ("target", p
) == 0)
31555 if_modifier
= OMP_TARGET
;
31556 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31558 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31559 p
= IDENTIFIER_POINTER (id
);
31560 if (strcmp ("data", p
) == 0)
31561 if_modifier
= OMP_TARGET_DATA
;
31562 else if (strcmp ("update", p
) == 0)
31563 if_modifier
= OMP_TARGET_UPDATE
;
31564 else if (strcmp ("enter", p
) == 0)
31565 if_modifier
= OMP_TARGET_ENTER_DATA
;
31566 else if (strcmp ("exit", p
) == 0)
31567 if_modifier
= OMP_TARGET_EXIT_DATA
;
31568 if (if_modifier
!= OMP_TARGET
)
31573 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
31574 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
31576 if_modifier
= ERROR_MARK
;
31578 if (if_modifier
== OMP_TARGET_ENTER_DATA
31579 || if_modifier
== OMP_TARGET_EXIT_DATA
)
31581 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
31583 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
31584 p
= IDENTIFIER_POINTER (id
);
31585 if (strcmp ("data", p
) == 0)
31591 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
31592 error_at (loc
, "expected %<data%>");
31593 if_modifier
= ERROR_MARK
;
31598 if (if_modifier
!= ERROR_MARK
)
31600 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
31603 cp_lexer_consume_token (parser
->lexer
);
31610 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
31611 error_at (loc
, "expected %<:%>");
31613 if_modifier
= ERROR_MARK
;
31618 t
= cp_parser_condition (parser
);
31620 if (t
== error_mark_node
31621 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31622 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31623 /*or_comma=*/false,
31624 /*consume_paren=*/true);
31626 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31627 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
31629 if (if_modifier
!= ERROR_MARK
31630 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31632 const char *p
= NULL
;
31633 switch (if_modifier
)
31635 case OMP_PARALLEL
: p
= "parallel"; break;
31636 case OMP_TASK
: p
= "task"; break;
31637 case OMP_TASKLOOP
: p
= "taskloop"; break;
31638 case OMP_TARGET_DATA
: p
= "target data"; break;
31639 case OMP_TARGET
: p
= "target"; break;
31640 case OMP_TARGET_UPDATE
: p
= "target update"; break;
31641 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
31642 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
31643 default: gcc_unreachable ();
31645 error_at (location
, "too many %<if%> clauses with %qs modifier",
31649 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31652 error_at (location
, "too many %<if%> clauses");
31654 error_at (location
, "too many %<if%> clauses without modifier");
31657 else if (if_modifier
== ERROR_MARK
31658 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
31660 error_at (location
, "if any %<if%> clause has modifier, then all "
31661 "%<if%> clauses have to use modifier");
31666 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
31667 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
31668 OMP_CLAUSE_IF_EXPR (c
) = t
;
31669 OMP_CLAUSE_CHAIN (c
) = list
;
31678 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
31679 tree list
, location_t location
)
31683 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
31686 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
31687 OMP_CLAUSE_CHAIN (c
) = list
;
31695 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
31696 tree list
, location_t location
)
31700 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
31702 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
31703 OMP_CLAUSE_CHAIN (c
) = list
;
31708 num_threads ( expression ) */
31711 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
31712 location_t location
)
31716 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31719 t
= cp_parser_expression (parser
);
31721 if (t
== error_mark_node
31722 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31723 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31724 /*or_comma=*/false,
31725 /*consume_paren=*/true);
31727 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
31728 "num_threads", location
);
31730 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
31731 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
31732 OMP_CLAUSE_CHAIN (c
) = list
;
31738 num_tasks ( expression ) */
31741 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
31742 location_t location
)
31746 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31749 t
= cp_parser_expression (parser
);
31751 if (t
== error_mark_node
31752 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31753 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31754 /*or_comma=*/false,
31755 /*consume_paren=*/true);
31757 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
31758 "num_tasks", location
);
31760 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
31761 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
31762 OMP_CLAUSE_CHAIN (c
) = list
;
31768 grainsize ( expression ) */
31771 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
31772 location_t location
)
31776 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31779 t
= cp_parser_expression (parser
);
31781 if (t
== error_mark_node
31782 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31783 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31784 /*or_comma=*/false,
31785 /*consume_paren=*/true);
31787 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
31788 "grainsize", location
);
31790 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
31791 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
31792 OMP_CLAUSE_CHAIN (c
) = list
;
31798 priority ( expression ) */
31801 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
31802 location_t location
)
31806 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31809 t
= cp_parser_expression (parser
);
31811 if (t
== error_mark_node
31812 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31813 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31814 /*or_comma=*/false,
31815 /*consume_paren=*/true);
31817 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
31818 "priority", location
);
31820 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
31821 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
31822 OMP_CLAUSE_CHAIN (c
) = list
;
31828 hint ( expression ) */
31831 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
31832 location_t location
)
31836 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31839 t
= cp_parser_expression (parser
);
31841 if (t
== error_mark_node
31842 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31843 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31844 /*or_comma=*/false,
31845 /*consume_paren=*/true);
31847 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
31849 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
31850 OMP_CLAUSE_HINT_EXPR (c
) = t
;
31851 OMP_CLAUSE_CHAIN (c
) = list
;
31857 defaultmap ( tofrom : scalar ) */
31860 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
31861 location_t location
)
31866 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31869 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31871 cp_parser_error (parser
, "expected %<tofrom%>");
31874 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31875 p
= IDENTIFIER_POINTER (id
);
31876 if (strcmp (p
, "tofrom") != 0)
31878 cp_parser_error (parser
, "expected %<tofrom%>");
31881 cp_lexer_consume_token (parser
->lexer
);
31882 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31885 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31887 cp_parser_error (parser
, "expected %<scalar%>");
31890 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31891 p
= IDENTIFIER_POINTER (id
);
31892 if (strcmp (p
, "scalar") != 0)
31894 cp_parser_error (parser
, "expected %<scalar%>");
31897 cp_lexer_consume_token (parser
->lexer
);
31898 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31901 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
31904 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
31905 OMP_CLAUSE_CHAIN (c
) = list
;
31909 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31910 /*or_comma=*/false,
31911 /*consume_paren=*/true);
31919 ordered ( constant-expression ) */
31922 cp_parser_omp_clause_ordered (cp_parser
*parser
,
31923 tree list
, location_t location
)
31925 tree c
, num
= NULL_TREE
;
31928 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
31929 "ordered", location
);
31931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31933 cp_lexer_consume_token (parser
->lexer
);
31935 num
= cp_parser_constant_expression (parser
);
31937 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31938 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31939 /*or_comma=*/false,
31940 /*consume_paren=*/true);
31942 if (num
== error_mark_node
)
31944 num
= fold_non_dependent_expr (num
);
31945 if (!tree_fits_shwi_p (num
)
31946 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31947 || (n
= tree_to_shwi (num
)) <= 0
31950 error_at (location
,
31951 "ordered argument needs positive constant integer "
31957 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
31958 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
31959 OMP_CLAUSE_CHAIN (c
) = list
;
31964 reduction ( reduction-operator : variable-list )
31966 reduction-operator:
31967 One of: + * - & ^ | && ||
31971 reduction-operator:
31972 One of: + * - & ^ | && || min max
31976 reduction-operator:
31977 One of: + * - & ^ | && ||
31981 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
31983 enum tree_code code
= ERROR_MARK
;
31984 tree nlist
, c
, id
= NULL_TREE
;
31986 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31989 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31991 case CPP_PLUS
: code
= PLUS_EXPR
; break;
31992 case CPP_MULT
: code
= MULT_EXPR
; break;
31993 case CPP_MINUS
: code
= MINUS_EXPR
; break;
31994 case CPP_AND
: code
= BIT_AND_EXPR
; break;
31995 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
31996 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
31997 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
31998 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32002 if (code
!= ERROR_MARK
)
32003 cp_lexer_consume_token (parser
->lexer
);
32006 bool saved_colon_corrects_to_scope_p
;
32007 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32008 parser
->colon_corrects_to_scope_p
= false;
32009 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32010 /*check_dependency_p=*/true,
32011 /*template_p=*/NULL
,
32012 /*declarator_p=*/false,
32013 /*optional_p=*/false);
32014 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32015 if (identifier_p (id
))
32017 const char *p
= IDENTIFIER_POINTER (id
);
32019 if (strcmp (p
, "min") == 0)
32021 else if (strcmp (p
, "max") == 0)
32023 else if (id
== cp_operator_id (PLUS_EXPR
))
32025 else if (id
== cp_operator_id (MULT_EXPR
))
32027 else if (id
== cp_operator_id (MINUS_EXPR
))
32029 else if (id
== cp_operator_id (BIT_AND_EXPR
))
32030 code
= BIT_AND_EXPR
;
32031 else if (id
== cp_operator_id (BIT_IOR_EXPR
))
32032 code
= BIT_IOR_EXPR
;
32033 else if (id
== cp_operator_id (BIT_XOR_EXPR
))
32034 code
= BIT_XOR_EXPR
;
32035 else if (id
== cp_operator_id (TRUTH_ANDIF_EXPR
))
32036 code
= TRUTH_ANDIF_EXPR
;
32037 else if (id
== cp_operator_id (TRUTH_ORIF_EXPR
))
32038 code
= TRUTH_ORIF_EXPR
;
32039 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32040 tree scope
= parser
->scope
;
32042 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32043 parser
->scope
= NULL_TREE
;
32044 parser
->qualifying_scope
= NULL_TREE
;
32045 parser
->object_scope
= NULL_TREE
;
32049 error ("invalid reduction-identifier");
32051 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32052 /*or_comma=*/false,
32053 /*consume_paren=*/true);
32058 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32061 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32063 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32065 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32066 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32073 schedule ( schedule-kind )
32074 schedule ( schedule-kind , expression )
32077 static | dynamic | guided | runtime | auto
32080 schedule ( schedule-modifier : schedule-kind )
32081 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32089 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32092 int modifiers
= 0, nmodifiers
= 0;
32094 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32097 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32099 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32101 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32102 const char *p
= IDENTIFIER_POINTER (id
);
32103 if (strcmp ("simd", p
) == 0)
32104 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32105 else if (strcmp ("monotonic", p
) == 0)
32106 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32107 else if (strcmp ("nonmonotonic", p
) == 0)
32108 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32111 cp_lexer_consume_token (parser
->lexer
);
32112 if (nmodifiers
++ == 0
32113 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32114 cp_lexer_consume_token (parser
->lexer
);
32117 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32124 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32125 const char *p
= IDENTIFIER_POINTER (id
);
32130 if (strcmp ("dynamic", p
) != 0)
32132 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32136 if (strcmp ("guided", p
) != 0)
32138 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32142 if (strcmp ("runtime", p
) != 0)
32144 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32151 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32152 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32153 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32154 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32157 cp_lexer_consume_token (parser
->lexer
);
32159 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32160 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32161 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32162 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32164 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32172 cp_lexer_consume_token (parser
->lexer
);
32174 token
= cp_lexer_peek_token (parser
->lexer
);
32175 t
= cp_parser_assignment_expression (parser
);
32177 if (t
== error_mark_node
)
32179 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32180 error_at (token
->location
, "schedule %<runtime%> does not take "
32181 "a %<chunk_size%> parameter");
32182 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32183 error_at (token
->location
, "schedule %<auto%> does not take "
32184 "a %<chunk_size%> parameter");
32186 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32188 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32191 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32194 OMP_CLAUSE_SCHEDULE_KIND (c
)
32195 = (enum omp_clause_schedule_kind
)
32196 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32198 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32199 OMP_CLAUSE_CHAIN (c
) = list
;
32203 cp_parser_error (parser
, "invalid schedule kind");
32205 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32206 /*or_comma=*/false,
32207 /*consume_paren=*/true);
32215 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32216 tree list
, location_t location
)
32220 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32222 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32223 OMP_CLAUSE_CHAIN (c
) = list
;
32232 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32233 tree list
, location_t location
)
32235 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32236 tree c
= build_omp_clause (location
, code
);
32237 OMP_CLAUSE_CHAIN (c
) = list
;
32248 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32249 enum omp_clause_code code
,
32250 tree list
, location_t location
)
32252 tree c
= build_omp_clause (location
, code
);
32253 OMP_CLAUSE_CHAIN (c
) = list
;
32261 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32262 tree list
, location_t location
)
32264 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32265 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32266 OMP_CLAUSE_CHAIN (c
) = list
;
32275 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32276 enum omp_clause_code code
,
32277 tree list
, location_t location
)
32279 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32280 tree c
= build_omp_clause (location
, code
);
32281 OMP_CLAUSE_CHAIN (c
) = list
;
32286 num_teams ( expression ) */
32289 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32290 location_t location
)
32294 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32297 t
= cp_parser_expression (parser
);
32299 if (t
== error_mark_node
32300 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32301 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32302 /*or_comma=*/false,
32303 /*consume_paren=*/true);
32305 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32306 "num_teams", location
);
32308 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32309 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32310 OMP_CLAUSE_CHAIN (c
) = list
;
32316 thread_limit ( expression ) */
32319 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32320 location_t location
)
32324 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32327 t
= cp_parser_expression (parser
);
32329 if (t
== error_mark_node
32330 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32331 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32332 /*or_comma=*/false,
32333 /*consume_paren=*/true);
32335 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32336 "thread_limit", location
);
32338 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32339 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32340 OMP_CLAUSE_CHAIN (c
) = list
;
32346 aligned ( variable-list )
32347 aligned ( variable-list : constant-expression ) */
32350 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32352 tree nlist
, c
, alignment
= NULL_TREE
;
32355 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32358 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32363 alignment
= cp_parser_constant_expression (parser
);
32365 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32366 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32367 /*or_comma=*/false,
32368 /*consume_paren=*/true);
32370 if (alignment
== error_mark_node
)
32371 alignment
= NULL_TREE
;
32374 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32375 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32381 linear ( variable-list )
32382 linear ( variable-list : expression )
32385 linear ( modifier ( variable-list ) )
32386 linear ( modifier ( variable-list ) : expression ) */
32389 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32390 bool is_cilk_simd_fn
, bool declare_simd
)
32392 tree nlist
, c
, step
= integer_one_node
;
32394 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32396 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32399 if (!is_cilk_simd_fn
32400 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32402 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32403 const char *p
= IDENTIFIER_POINTER (id
);
32405 if (strcmp ("ref", p
) == 0)
32406 kind
= OMP_CLAUSE_LINEAR_REF
;
32407 else if (strcmp ("val", p
) == 0)
32408 kind
= OMP_CLAUSE_LINEAR_VAL
;
32409 else if (strcmp ("uval", p
) == 0)
32410 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32411 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32412 cp_lexer_consume_token (parser
->lexer
);
32414 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32417 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32418 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32422 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32423 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32425 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32426 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32427 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32428 /*or_comma=*/false,
32429 /*consume_paren=*/true);
32436 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32437 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32440 cp_parser_parse_tentatively (parser
);
32441 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32442 /*check_dependency_p=*/true,
32443 /*template_p=*/NULL
,
32444 /*declarator_p=*/false,
32445 /*optional_p=*/false);
32446 if (step
!= error_mark_node
)
32447 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32448 if (step
== error_mark_node
)
32451 cp_parser_abort_tentative_parse (parser
);
32453 else if (!cp_parser_parse_definitely (parser
))
32457 step
= cp_parser_expression (parser
);
32459 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
32461 sorry ("using parameters for %<linear%> step is not supported yet");
32462 step
= integer_one_node
;
32464 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32465 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32466 /*or_comma=*/false,
32467 /*consume_paren=*/true);
32469 if (step
== error_mark_node
)
32473 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32475 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32476 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32483 safelen ( constant-expression ) */
32486 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32487 location_t location
)
32491 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32494 t
= cp_parser_constant_expression (parser
);
32496 if (t
== error_mark_node
32497 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32498 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32499 /*or_comma=*/false,
32500 /*consume_paren=*/true);
32502 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32504 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32505 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32506 OMP_CLAUSE_CHAIN (c
) = list
;
32512 simdlen ( constant-expression ) */
32515 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32516 location_t location
)
32520 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32523 t
= cp_parser_constant_expression (parser
);
32525 if (t
== error_mark_node
32526 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32527 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32528 /*or_comma=*/false,
32529 /*consume_paren=*/true);
32531 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32533 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32534 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32535 OMP_CLAUSE_CHAIN (c
) = list
;
32542 identifier [+/- integer]
32543 vec , identifier [+/- integer]
32547 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32554 cp_parser_error (parser
, "expected identifier");
32558 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32560 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32561 tree t
, identifier
= cp_parser_identifier (parser
);
32562 tree addend
= NULL
;
32564 if (identifier
== error_mark_node
)
32565 t
= error_mark_node
;
32568 t
= cp_parser_lookup_name_simple
32569 (parser
, identifier
,
32570 cp_lexer_peek_token (parser
->lexer
)->location
);
32571 if (t
== error_mark_node
)
32572 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
32577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
32579 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
32581 addend
= integer_zero_node
;
32582 goto add_to_vector
;
32584 cp_lexer_consume_token (parser
->lexer
);
32586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
32588 cp_parser_error (parser
, "expected integer");
32592 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32593 if (TREE_CODE (addend
) != INTEGER_CST
)
32595 cp_parser_error (parser
, "expected integer");
32598 cp_lexer_consume_token (parser
->lexer
);
32601 if (t
!= error_mark_node
)
32603 vec
= tree_cons (addend
, t
, vec
);
32605 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
32608 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32611 cp_lexer_consume_token (parser
->lexer
);
32614 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
32616 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
32617 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
32618 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
32619 OMP_CLAUSE_CHAIN (u
) = list
;
32626 depend ( depend-kind : variable-list )
32634 depend ( sink : vec ) */
32637 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
32640 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
32642 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32647 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32648 const char *p
= IDENTIFIER_POINTER (id
);
32650 if (strcmp ("in", p
) == 0)
32651 kind
= OMP_CLAUSE_DEPEND_IN
;
32652 else if (strcmp ("inout", p
) == 0)
32653 kind
= OMP_CLAUSE_DEPEND_INOUT
;
32654 else if (strcmp ("out", p
) == 0)
32655 kind
= OMP_CLAUSE_DEPEND_OUT
;
32656 else if (strcmp ("source", p
) == 0)
32657 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
32658 else if (strcmp ("sink", p
) == 0)
32659 kind
= OMP_CLAUSE_DEPEND_SINK
;
32666 cp_lexer_consume_token (parser
->lexer
);
32668 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
32670 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
32671 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32672 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
32673 OMP_CLAUSE_CHAIN (c
) = list
;
32674 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32675 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32676 /*or_comma=*/false,
32677 /*consume_paren=*/true);
32681 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32684 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
32685 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
32688 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
32691 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32692 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32697 cp_parser_error (parser
, "invalid depend kind");
32699 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32700 /*or_comma=*/false,
32701 /*consume_paren=*/true);
32706 map ( map-kind : variable-list )
32707 map ( variable-list )
32710 alloc | to | from | tofrom
32714 alloc | to | from | tofrom | release | delete
32716 map ( always [,] map-kind: variable-list ) */
32719 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
32722 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
32723 bool always
= false;
32725 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32730 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32731 const char *p
= IDENTIFIER_POINTER (id
);
32733 if (strcmp ("always", p
) == 0)
32736 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
32738 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
32739 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
32741 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
32745 cp_lexer_consume_token (parser
->lexer
);
32747 cp_lexer_consume_token (parser
->lexer
);
32752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32753 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32755 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32756 const char *p
= IDENTIFIER_POINTER (id
);
32758 if (strcmp ("alloc", p
) == 0)
32759 kind
= GOMP_MAP_ALLOC
;
32760 else if (strcmp ("to", p
) == 0)
32761 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
32762 else if (strcmp ("from", p
) == 0)
32763 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
32764 else if (strcmp ("tofrom", p
) == 0)
32765 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
32766 else if (strcmp ("release", p
) == 0)
32767 kind
= GOMP_MAP_RELEASE
;
32770 cp_parser_error (parser
, "invalid map kind");
32771 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32772 /*or_comma=*/false,
32773 /*consume_paren=*/true);
32776 cp_lexer_consume_token (parser
->lexer
);
32777 cp_lexer_consume_token (parser
->lexer
);
32779 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
32780 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32782 kind
= GOMP_MAP_DELETE
;
32783 cp_lexer_consume_token (parser
->lexer
);
32784 cp_lexer_consume_token (parser
->lexer
);
32787 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
32790 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32791 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32797 device ( expression ) */
32800 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
32801 location_t location
)
32805 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32808 t
= cp_parser_expression (parser
);
32810 if (t
== error_mark_node
32811 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32812 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32813 /*or_comma=*/false,
32814 /*consume_paren=*/true);
32816 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
32817 "device", location
);
32819 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
32820 OMP_CLAUSE_DEVICE_ID (c
) = t
;
32821 OMP_CLAUSE_CHAIN (c
) = list
;
32827 dist_schedule ( static )
32828 dist_schedule ( static , expression ) */
32831 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
32832 location_t location
)
32836 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32839 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
32841 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32843 cp_lexer_consume_token (parser
->lexer
);
32845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32847 cp_lexer_consume_token (parser
->lexer
);
32849 t
= cp_parser_assignment_expression (parser
);
32851 if (t
== error_mark_node
)
32853 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
32855 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32858 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32861 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
32863 OMP_CLAUSE_CHAIN (c
) = list
;
32867 cp_parser_error (parser
, "invalid dist_schedule kind");
32869 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32870 /*or_comma=*/false,
32871 /*consume_paren=*/true);
32876 proc_bind ( proc-bind-kind )
32879 master | close | spread */
32882 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
32883 location_t location
)
32886 enum omp_clause_proc_bind_kind kind
;
32888 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32893 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32894 const char *p
= IDENTIFIER_POINTER (id
);
32896 if (strcmp ("master", p
) == 0)
32897 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
32898 else if (strcmp ("close", p
) == 0)
32899 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
32900 else if (strcmp ("spread", p
) == 0)
32901 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
32908 cp_lexer_consume_token (parser
->lexer
);
32909 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32912 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
32913 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
32915 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
32916 OMP_CLAUSE_CHAIN (c
) = list
;
32920 cp_parser_error (parser
, "invalid depend kind");
32922 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32923 /*or_comma=*/false,
32924 /*consume_paren=*/true);
32929 async [( int-expr )] */
32932 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
32935 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32937 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
32939 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32941 cp_lexer_consume_token (parser
->lexer
);
32943 t
= cp_parser_expression (parser
);
32944 if (t
== error_mark_node
32945 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32946 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32947 /*or_comma=*/false,
32948 /*consume_paren=*/true);
32951 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
32953 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
32954 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
32955 OMP_CLAUSE_CHAIN (c
) = list
;
32961 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32962 is a bitmask in MASK. Return the list of clauses found. */
32965 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
32966 const char *where
, cp_token
*pragma_tok
,
32967 bool finish_p
= true)
32969 tree clauses
= NULL
;
32972 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32975 pragma_omp_clause c_kind
;
32976 omp_clause_code code
;
32977 const char *c_name
;
32978 tree prev
= clauses
;
32980 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32981 cp_lexer_consume_token (parser
->lexer
);
32983 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
32984 c_kind
= cp_parser_omp_clause_name (parser
);
32988 case PRAGMA_OACC_CLAUSE_ASYNC
:
32989 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
32992 case PRAGMA_OACC_CLAUSE_AUTO
:
32993 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
32997 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
32998 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
32999 c_name
= "collapse";
33001 case PRAGMA_OACC_CLAUSE_COPY
:
33002 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33005 case PRAGMA_OACC_CLAUSE_COPYIN
:
33006 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33009 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33010 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33011 c_name
= "copyout";
33013 case PRAGMA_OACC_CLAUSE_CREATE
:
33014 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33017 case PRAGMA_OACC_CLAUSE_DELETE
:
33018 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33021 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33022 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33023 c_name
= "default";
33025 case PRAGMA_OACC_CLAUSE_DEVICE
:
33026 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33029 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33030 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33031 c_name
= "deviceptr";
33033 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33034 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33035 c_name
= "device_resident";
33037 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33038 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33040 c_name
= "firstprivate";
33042 case PRAGMA_OACC_CLAUSE_GANG
:
33044 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33047 case PRAGMA_OACC_CLAUSE_HOST
:
33048 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33051 case PRAGMA_OACC_CLAUSE_IF
:
33052 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33055 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33056 clauses
= cp_parser_oacc_simple_clause (parser
,
33057 OMP_CLAUSE_INDEPENDENT
,
33059 c_name
= "independent";
33061 case PRAGMA_OACC_CLAUSE_LINK
:
33062 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33065 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33066 code
= OMP_CLAUSE_NUM_GANGS
;
33067 c_name
= "num_gangs";
33068 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33071 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33072 c_name
= "num_workers";
33073 code
= OMP_CLAUSE_NUM_WORKERS
;
33074 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33077 case PRAGMA_OACC_CLAUSE_PRESENT
:
33078 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33079 c_name
= "present";
33081 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33082 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33083 c_name
= "present_or_copy";
33085 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33086 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33087 c_name
= "present_or_copyin";
33089 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33090 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33091 c_name
= "present_or_copyout";
33093 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33094 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33095 c_name
= "present_or_create";
33097 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33098 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33100 c_name
= "private";
33102 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33103 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33104 c_name
= "reduction";
33106 case PRAGMA_OACC_CLAUSE_SELF
:
33107 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33110 case PRAGMA_OACC_CLAUSE_SEQ
:
33111 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33115 case PRAGMA_OACC_CLAUSE_TILE
:
33116 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33119 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33120 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33122 c_name
= "use_device";
33124 case PRAGMA_OACC_CLAUSE_VECTOR
:
33126 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33129 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33130 c_name
= "vector_length";
33131 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33132 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33135 case PRAGMA_OACC_CLAUSE_WAIT
:
33136 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33139 case PRAGMA_OACC_CLAUSE_WORKER
:
33141 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33145 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33151 if (((mask
>> c_kind
) & 1) == 0)
33153 /* Remove the invalid clause(s) from the list to avoid
33154 confusing the rest of the compiler. */
33156 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33161 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33164 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33169 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33170 is a bitmask in MASK. Return the list of clauses found; the result
33171 of clause default goes in *pdefault. */
33174 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33175 const char *where
, cp_token
*pragma_tok
,
33176 bool finish_p
= true)
33178 tree clauses
= NULL
;
33180 cp_token
*token
= NULL
;
33182 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33184 pragma_omp_clause c_kind
;
33185 const char *c_name
;
33186 tree prev
= clauses
;
33188 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33189 cp_lexer_consume_token (parser
->lexer
);
33191 token
= cp_lexer_peek_token (parser
->lexer
);
33192 c_kind
= cp_parser_omp_clause_name (parser
);
33196 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33197 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33199 c_name
= "collapse";
33201 case PRAGMA_OMP_CLAUSE_COPYIN
:
33202 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33205 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33206 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33208 c_name
= "copyprivate";
33210 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33211 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33212 token
->location
, false);
33213 c_name
= "default";
33215 case PRAGMA_OMP_CLAUSE_FINAL
:
33216 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33219 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33220 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33222 c_name
= "firstprivate";
33224 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33225 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33227 c_name
= "grainsize";
33229 case PRAGMA_OMP_CLAUSE_HINT
:
33230 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33234 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33235 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33237 c_name
= "defaultmap";
33239 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33240 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33242 c_name
= "use_device_ptr";
33244 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33245 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33247 c_name
= "is_device_ptr";
33249 case PRAGMA_OMP_CLAUSE_IF
:
33250 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33254 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33255 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33257 c_name
= "lastprivate";
33259 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33260 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33262 c_name
= "mergeable";
33264 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33265 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33268 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33269 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33271 c_name
= "num_tasks";
33273 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33274 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33276 c_name
= "num_threads";
33278 case PRAGMA_OMP_CLAUSE_ORDERED
:
33279 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33281 c_name
= "ordered";
33283 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33284 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33286 c_name
= "priority";
33288 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33289 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33291 c_name
= "private";
33293 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33294 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33295 c_name
= "reduction";
33297 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33298 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33300 c_name
= "schedule";
33302 case PRAGMA_OMP_CLAUSE_SHARED
:
33303 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33307 case PRAGMA_OMP_CLAUSE_UNTIED
:
33308 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33312 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33313 case PRAGMA_CILK_CLAUSE_MASK
:
33314 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33315 clauses
, token
->location
);
33316 c_name
= "inbranch";
33318 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33319 case PRAGMA_CILK_CLAUSE_NOMASK
:
33320 clauses
= cp_parser_omp_clause_branch (parser
,
33321 OMP_CLAUSE_NOTINBRANCH
,
33322 clauses
, token
->location
);
33323 c_name
= "notinbranch";
33325 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33326 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33327 clauses
, token
->location
);
33328 c_name
= "parallel";
33332 error_at (token
->location
, "%qs must be the first clause of %qs",
33337 case PRAGMA_OMP_CLAUSE_FOR
:
33338 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33339 clauses
, token
->location
);
33342 goto clause_not_first
;
33344 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33345 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33346 clauses
, token
->location
);
33347 c_name
= "sections";
33349 goto clause_not_first
;
33351 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33352 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33353 clauses
, token
->location
);
33354 c_name
= "taskgroup";
33356 goto clause_not_first
;
33358 case PRAGMA_OMP_CLAUSE_LINK
:
33359 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33362 case PRAGMA_OMP_CLAUSE_TO
:
33363 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33364 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33367 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33370 case PRAGMA_OMP_CLAUSE_FROM
:
33371 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33374 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33375 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33377 c_name
= "uniform";
33379 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33380 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33382 c_name
= "num_teams";
33384 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33385 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33387 c_name
= "thread_limit";
33389 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33390 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33391 c_name
= "aligned";
33393 case PRAGMA_OMP_CLAUSE_LINEAR
:
33395 bool cilk_simd_fn
= false, declare_simd
= false;
33396 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
33397 cilk_simd_fn
= true;
33398 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33399 declare_simd
= true;
33400 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
33401 cilk_simd_fn
, declare_simd
);
33405 case PRAGMA_OMP_CLAUSE_DEPEND
:
33406 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33410 case PRAGMA_OMP_CLAUSE_MAP
:
33411 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33414 case PRAGMA_OMP_CLAUSE_DEVICE
:
33415 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33419 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33420 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33422 c_name
= "dist_schedule";
33424 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33425 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33427 c_name
= "proc_bind";
33429 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33430 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33432 c_name
= "safelen";
33434 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33435 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33437 c_name
= "simdlen";
33439 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33440 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33442 c_name
= "nogroup";
33444 case PRAGMA_OMP_CLAUSE_THREADS
:
33446 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33447 clauses
, token
->location
);
33448 c_name
= "threads";
33450 case PRAGMA_OMP_CLAUSE_SIMD
:
33452 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33453 clauses
, token
->location
);
33456 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
33457 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
33458 c_name
= "simdlen";
33461 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33467 if (((mask
>> c_kind
) & 1) == 0)
33469 /* Remove the invalid clause(s) from the list to avoid
33470 confusing the rest of the compiler. */
33472 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33476 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33477 no reason to skip to the end. */
33478 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
33479 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33482 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33483 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33485 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33494 In practice, we're also interested in adding the statement to an
33495 outer node. So it is convenient if we work around the fact that
33496 cp_parser_statement calls add_stmt. */
33499 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33501 unsigned save
= parser
->in_statement
;
33503 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33504 This preserves the "not within loop or switch" style error messages
33505 for nonsense cases like
33511 if (parser
->in_statement
)
33512 parser
->in_statement
= IN_OMP_BLOCK
;
33518 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33520 parser
->in_statement
= save
;
33524 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33526 tree stmt
= begin_omp_structured_block ();
33527 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33529 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33531 cp_parser_end_omp_structured_block (parser
, save
);
33532 return finish_omp_structured_block (stmt
);
33536 # pragma omp atomic new-line
33540 x binop= expr | x++ | ++x | x-- | --x
33542 +, *, -, /, &, ^, |, <<, >>
33544 where x is an lvalue expression with scalar type.
33547 # pragma omp atomic new-line
33550 # pragma omp atomic read new-line
33553 # pragma omp atomic write new-line
33556 # pragma omp atomic update new-line
33559 # pragma omp atomic capture new-line
33562 # pragma omp atomic capture new-line
33570 expression-stmt | x = x binop expr
33572 v = expression-stmt
33574 { v = x; update-stmt; } | { update-stmt; v = x; }
33578 expression-stmt | x = x binop expr | x = expr binop x
33582 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33584 where x and v are lvalue expressions with scalar type. */
33587 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
33589 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
33590 tree rhs1
= NULL_TREE
, orig_lhs
;
33591 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
33592 bool structured_block
= false;
33593 bool seq_cst
= false;
33595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33597 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33598 const char *p
= IDENTIFIER_POINTER (id
);
33600 if (!strcmp (p
, "seq_cst"))
33603 cp_lexer_consume_token (parser
->lexer
);
33604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33605 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33606 cp_lexer_consume_token (parser
->lexer
);
33609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33611 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33612 const char *p
= IDENTIFIER_POINTER (id
);
33614 if (!strcmp (p
, "read"))
33615 code
= OMP_ATOMIC_READ
;
33616 else if (!strcmp (p
, "write"))
33618 else if (!strcmp (p
, "update"))
33620 else if (!strcmp (p
, "capture"))
33621 code
= OMP_ATOMIC_CAPTURE_NEW
;
33625 cp_lexer_consume_token (parser
->lexer
);
33629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33630 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33631 cp_lexer_consume_token (parser
->lexer
);
33633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33635 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33636 const char *p
= IDENTIFIER_POINTER (id
);
33638 if (!strcmp (p
, "seq_cst"))
33641 cp_lexer_consume_token (parser
->lexer
);
33645 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33649 case OMP_ATOMIC_READ
:
33650 case NOP_EXPR
: /* atomic write */
33651 v
= cp_parser_unary_expression (parser
);
33652 if (v
== error_mark_node
)
33654 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33656 if (code
== NOP_EXPR
)
33657 lhs
= cp_parser_expression (parser
);
33659 lhs
= cp_parser_unary_expression (parser
);
33660 if (lhs
== error_mark_node
)
33662 if (code
== NOP_EXPR
)
33664 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33672 case OMP_ATOMIC_CAPTURE_NEW
:
33673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33675 cp_lexer_consume_token (parser
->lexer
);
33676 structured_block
= true;
33680 v
= cp_parser_unary_expression (parser
);
33681 if (v
== error_mark_node
)
33683 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33691 lhs
= cp_parser_unary_expression (parser
);
33693 switch (TREE_CODE (lhs
))
33698 case POSTINCREMENT_EXPR
:
33699 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33700 code
= OMP_ATOMIC_CAPTURE_OLD
;
33702 case PREINCREMENT_EXPR
:
33703 lhs
= TREE_OPERAND (lhs
, 0);
33704 opcode
= PLUS_EXPR
;
33705 rhs
= integer_one_node
;
33708 case POSTDECREMENT_EXPR
:
33709 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33710 code
= OMP_ATOMIC_CAPTURE_OLD
;
33712 case PREDECREMENT_EXPR
:
33713 lhs
= TREE_OPERAND (lhs
, 0);
33714 opcode
= MINUS_EXPR
;
33715 rhs
= integer_one_node
;
33718 case COMPOUND_EXPR
:
33719 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
33720 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
33721 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
33722 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
33723 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33724 (TREE_OPERAND (lhs
, 1), 0), 0)))
33726 /* Undo effects of boolean_increment for post {in,de}crement. */
33727 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
33730 if (TREE_CODE (lhs
) == MODIFY_EXPR
33731 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
33733 /* Undo effects of boolean_increment. */
33734 if (integer_onep (TREE_OPERAND (lhs
, 1)))
33736 /* This is pre or post increment. */
33737 rhs
= TREE_OPERAND (lhs
, 1);
33738 lhs
= TREE_OPERAND (lhs
, 0);
33740 if (code
== OMP_ATOMIC_CAPTURE_NEW
33741 && !structured_block
33742 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
33743 code
= OMP_ATOMIC_CAPTURE_OLD
;
33749 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33752 opcode
= MULT_EXPR
;
33755 opcode
= TRUNC_DIV_EXPR
;
33758 opcode
= PLUS_EXPR
;
33761 opcode
= MINUS_EXPR
;
33763 case CPP_LSHIFT_EQ
:
33764 opcode
= LSHIFT_EXPR
;
33766 case CPP_RSHIFT_EQ
:
33767 opcode
= RSHIFT_EXPR
;
33770 opcode
= BIT_AND_EXPR
;
33773 opcode
= BIT_IOR_EXPR
;
33776 opcode
= BIT_XOR_EXPR
;
33779 enum cp_parser_prec oprec
;
33781 cp_lexer_consume_token (parser
->lexer
);
33782 cp_parser_parse_tentatively (parser
);
33783 rhs1
= cp_parser_simple_cast_expression (parser
);
33784 if (rhs1
== error_mark_node
)
33786 cp_parser_abort_tentative_parse (parser
);
33787 cp_parser_simple_cast_expression (parser
);
33790 token
= cp_lexer_peek_token (parser
->lexer
);
33791 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
33793 cp_parser_abort_tentative_parse (parser
);
33794 cp_parser_parse_tentatively (parser
);
33795 rhs
= cp_parser_binary_expression (parser
, false, true,
33796 PREC_NOT_OPERATOR
, NULL
);
33797 if (rhs
== error_mark_node
)
33799 cp_parser_abort_tentative_parse (parser
);
33800 cp_parser_binary_expression (parser
, false, true,
33801 PREC_NOT_OPERATOR
, NULL
);
33804 switch (TREE_CODE (rhs
))
33807 case TRUNC_DIV_EXPR
:
33816 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
33818 if (cp_parser_parse_definitely (parser
))
33820 opcode
= TREE_CODE (rhs
);
33821 rhs1
= TREE_OPERAND (rhs
, 0);
33822 rhs
= TREE_OPERAND (rhs
, 1);
33832 cp_parser_abort_tentative_parse (parser
);
33833 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
33835 rhs
= cp_parser_expression (parser
);
33836 if (rhs
== error_mark_node
)
33842 cp_parser_error (parser
,
33843 "invalid form of %<#pragma omp atomic%>");
33846 if (!cp_parser_parse_definitely (parser
))
33848 switch (token
->type
)
33850 case CPP_SEMICOLON
:
33851 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33853 code
= OMP_ATOMIC_CAPTURE_OLD
;
33858 cp_lexer_consume_token (parser
->lexer
);
33861 else if (structured_block
)
33868 cp_parser_error (parser
,
33869 "invalid form of %<#pragma omp atomic%>");
33872 opcode
= MULT_EXPR
;
33875 opcode
= TRUNC_DIV_EXPR
;
33878 opcode
= PLUS_EXPR
;
33881 opcode
= MINUS_EXPR
;
33884 opcode
= LSHIFT_EXPR
;
33887 opcode
= RSHIFT_EXPR
;
33890 opcode
= BIT_AND_EXPR
;
33893 opcode
= BIT_IOR_EXPR
;
33896 opcode
= BIT_XOR_EXPR
;
33899 cp_parser_error (parser
,
33900 "invalid operator for %<#pragma omp atomic%>");
33903 oprec
= TOKEN_PRECEDENCE (token
);
33904 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
33905 if (commutative_tree_code (opcode
))
33906 oprec
= (enum cp_parser_prec
) (oprec
- 1);
33907 cp_lexer_consume_token (parser
->lexer
);
33908 rhs
= cp_parser_binary_expression (parser
, false, false,
33910 if (rhs
== error_mark_node
)
33915 cp_parser_error (parser
,
33916 "invalid operator for %<#pragma omp atomic%>");
33919 cp_lexer_consume_token (parser
->lexer
);
33921 rhs
= cp_parser_expression (parser
);
33922 if (rhs
== error_mark_node
)
33927 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33929 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
33931 v
= cp_parser_unary_expression (parser
);
33932 if (v
== error_mark_node
)
33934 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33936 lhs1
= cp_parser_unary_expression (parser
);
33937 if (lhs1
== error_mark_node
)
33940 if (structured_block
)
33942 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33943 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33946 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
33947 if (!structured_block
)
33948 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33952 cp_parser_skip_to_end_of_block_or_statement (parser
);
33953 if (structured_block
)
33955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33956 cp_lexer_consume_token (parser
->lexer
);
33957 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
33959 cp_parser_skip_to_end_of_block_or_statement (parser
);
33960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33961 cp_lexer_consume_token (parser
->lexer
);
33968 # pragma omp barrier new-line */
33971 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
33973 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33974 finish_omp_barrier ();
33978 # pragma omp critical [(name)] new-line
33982 # pragma omp critical [(name) [hint(expression)]] new-line
33983 structured-block */
33985 #define OMP_CRITICAL_CLAUSE_MASK \
33986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33989 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
33991 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
33993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33995 cp_lexer_consume_token (parser
->lexer
);
33997 name
= cp_parser_identifier (parser
);
33999 if (name
== error_mark_node
34000 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34001 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34002 /*or_comma=*/false,
34003 /*consume_paren=*/true);
34004 if (name
== error_mark_node
)
34007 clauses
= cp_parser_omp_all_clauses (parser
,
34008 OMP_CRITICAL_CLAUSE_MASK
,
34009 "#pragma omp critical", pragma_tok
);
34012 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34014 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34015 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34019 # pragma omp flush flush-vars[opt] new-line
34022 ( variable-list ) */
34025 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34028 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34029 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34031 finish_omp_flush ();
34034 /* Helper function, to parse omp for increment expression. */
34037 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
34039 tree cond
= cp_parser_binary_expression (parser
, false, true,
34040 PREC_NOT_OPERATOR
, NULL
);
34041 if (cond
== error_mark_node
34042 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34044 cp_parser_skip_to_end_of_statement (parser
);
34045 return error_mark_node
;
34048 switch (TREE_CODE (cond
))
34056 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34058 /* Fall through: OpenMP disallows NE_EXPR. */
34059 gcc_fallthrough ();
34061 return error_mark_node
;
34064 /* If decl is an iterator, preserve LHS and RHS of the relational
34065 expr until finish_omp_for. */
34067 && (type_dependent_expression_p (decl
)
34068 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34071 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34073 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34074 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34075 /*overload=*/NULL
, tf_warning_or_error
);
34078 /* Helper function, to parse omp for increment expression. */
34081 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34083 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34089 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34091 op
= (token
->type
== CPP_PLUS_PLUS
34092 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34093 cp_lexer_consume_token (parser
->lexer
);
34094 lhs
= cp_parser_simple_cast_expression (parser
);
34096 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34097 return error_mark_node
;
34098 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34101 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34103 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34104 return error_mark_node
;
34106 token
= cp_lexer_peek_token (parser
->lexer
);
34107 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34109 op
= (token
->type
== CPP_PLUS_PLUS
34110 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34111 cp_lexer_consume_token (parser
->lexer
);
34112 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34115 op
= cp_parser_assignment_operator_opt (parser
);
34116 if (op
== ERROR_MARK
)
34117 return error_mark_node
;
34119 if (op
!= NOP_EXPR
)
34121 rhs
= cp_parser_assignment_expression (parser
);
34122 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34123 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34126 lhs
= cp_parser_binary_expression (parser
, false, false,
34127 PREC_ADDITIVE_EXPRESSION
, NULL
);
34128 token
= cp_lexer_peek_token (parser
->lexer
);
34129 decl_first
= (lhs
== decl
34130 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34133 if (token
->type
!= CPP_PLUS
34134 && token
->type
!= CPP_MINUS
)
34135 return error_mark_node
;
34139 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34140 cp_lexer_consume_token (parser
->lexer
);
34141 rhs
= cp_parser_binary_expression (parser
, false, false,
34142 PREC_ADDITIVE_EXPRESSION
, NULL
);
34143 token
= cp_lexer_peek_token (parser
->lexer
);
34144 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34146 if (lhs
== NULL_TREE
)
34148 if (op
== PLUS_EXPR
)
34151 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34152 tf_warning_or_error
);
34155 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34156 ERROR_MARK
, NULL
, tf_warning_or_error
);
34159 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34164 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34165 || op
== MINUS_EXPR
)
34166 return error_mark_node
;
34167 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34170 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34172 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34175 /* Parse the initialization statement of either an OpenMP for loop or
34176 a Cilk Plus for loop.
34178 Return true if the resulting construct should have an
34179 OMP_CLAUSE_PRIVATE added to it. */
34182 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34183 enum tree_code code
,
34184 tree
&this_pre_body
,
34185 vec
<tree
, va_gc
> *for_block
,
34191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34194 tree add_private_clause
= NULL_TREE
;
34196 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34200 integer-type var = lb
34201 random-access-iterator-type var = lb
34202 pointer-type var = lb
34204 cp_decl_specifier_seq type_specifiers
;
34206 /* First, try to parse as an initialized declaration. See
34207 cp_parser_condition, from whence the bulk of this is copied. */
34209 cp_parser_parse_tentatively (parser
);
34210 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34211 /*is_trailing_return=*/false,
34213 if (cp_parser_parse_definitely (parser
))
34215 /* If parsing a type specifier seq succeeded, then this
34216 MUST be a initialized declaration. */
34217 tree asm_specification
, attributes
;
34218 cp_declarator
*declarator
;
34220 declarator
= cp_parser_declarator (parser
,
34221 CP_PARSER_DECLARATOR_NAMED
,
34222 /*ctor_dtor_or_conv_p=*/NULL
,
34223 /*parenthesized_p=*/NULL
,
34224 /*member_p=*/false,
34225 /*friend_p=*/false);
34226 attributes
= cp_parser_attributes_opt (parser
);
34227 asm_specification
= cp_parser_asm_specification_opt (parser
);
34229 if (declarator
== cp_error_declarator
)
34230 cp_parser_skip_to_end_of_statement (parser
);
34234 tree pushed_scope
, auto_node
;
34236 decl
= start_decl (declarator
, &type_specifiers
,
34237 SD_INITIALIZED
, attributes
,
34238 /*prefix_attributes=*/NULL_TREE
,
34241 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34244 if (cp_lexer_next_token_is (parser
->lexer
,
34247 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
34248 error ("parenthesized initialization is not allowed in "
34249 "OpenMP %<for%> loop");
34251 error ("parenthesized initialization is "
34252 "not allowed in for-loop");
34255 /* Trigger an error. */
34256 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34258 init
= error_mark_node
;
34259 cp_parser_skip_to_end_of_statement (parser
);
34261 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34262 || type_dependent_expression_p (decl
)
34265 bool is_direct_init
, is_non_constant_init
;
34267 init
= cp_parser_initializer (parser
,
34269 &is_non_constant_init
);
34274 = do_auto_deduction (TREE_TYPE (decl
), init
,
34277 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34278 && !type_dependent_expression_p (decl
))
34282 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34284 LOOKUP_ONLYCONVERTING
);
34286 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34288 vec_safe_push (for_block
, this_pre_body
);
34293 init
= pop_stmt_list (this_pre_body
);
34294 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34296 tree_stmt_iterator i
= tsi_start (init
);
34297 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34298 while (!tsi_end_p (i
))
34300 tree t
= tsi_stmt (i
);
34301 if (TREE_CODE (t
) == DECL_EXPR
34302 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34305 vec_safe_push (for_block
, t
);
34310 if (tsi_one_before_end_p (i
))
34312 tree t
= tsi_stmt (i
);
34314 free_stmt_list (init
);
34319 this_pre_body
= NULL_TREE
;
34324 cp_lexer_consume_token (parser
->lexer
);
34325 init
= cp_parser_assignment_expression (parser
);
34328 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34329 init
= error_mark_node
;
34331 cp_finish_decl (decl
, NULL_TREE
,
34332 /*init_const_expr_p=*/false,
34334 LOOKUP_ONLYCONVERTING
);
34338 pop_scope (pushed_scope
);
34344 /* If parsing a type specifier sequence failed, then
34345 this MUST be a simple expression. */
34346 if (code
== CILK_FOR
)
34347 error ("%<_Cilk_for%> allows expression instead of declaration only "
34348 "in C, not in C++");
34349 cp_parser_parse_tentatively (parser
);
34350 decl
= cp_parser_primary_expression (parser
, false, false,
34352 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34353 if (!cp_parser_error_occurred (parser
)
34355 && (TREE_CODE (decl
) == COMPONENT_REF
34356 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34358 cp_parser_abort_tentative_parse (parser
);
34359 cp_parser_parse_tentatively (parser
);
34360 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34361 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34362 /*check_dependency_p=*/true,
34363 /*template_p=*/NULL
,
34364 /*declarator_p=*/false,
34365 /*optional_p=*/false);
34366 if (name
!= error_mark_node
34367 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34369 decl
= cp_parser_lookup_name_simple (parser
, name
,
34371 if (TREE_CODE (decl
) == FIELD_DECL
)
34372 add_private_clause
= omp_privatize_field (decl
, false);
34374 cp_parser_abort_tentative_parse (parser
);
34375 cp_parser_parse_tentatively (parser
);
34376 decl
= cp_parser_primary_expression (parser
, false, false,
34379 if (!cp_parser_error_occurred (parser
)
34382 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34386 cp_parser_parse_definitely (parser
);
34387 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34388 rhs
= cp_parser_assignment_expression (parser
);
34390 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34393 tf_warning_or_error
));
34394 if (!add_private_clause
)
34395 add_private_clause
= decl
;
34400 cp_parser_abort_tentative_parse (parser
);
34401 init
= cp_parser_expression (parser
);
34404 if (TREE_CODE (init
) == MODIFY_EXPR
34405 || TREE_CODE (init
) == MODOP_EXPR
)
34406 real_decl
= TREE_OPERAND (init
, 0);
34410 return add_private_clause
;
34413 /* Parse the restricted form of the for statement allowed by OpenMP. */
34416 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34417 tree
*cclauses
, bool *if_p
)
34419 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34420 tree real_decl
, initv
, condv
, incrv
, declv
;
34421 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34422 location_t loc_first
;
34423 bool collapse_err
= false;
34424 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34425 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34426 auto_vec
<tree
, 4> orig_inits
;
34427 bool tiling
= false;
34429 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34430 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34431 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34432 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34435 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34437 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34438 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34441 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34444 if (ordered
&& ordered
< collapse
)
34446 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34447 "%<ordered%> clause parameter is less than %<collapse%>");
34448 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34449 = build_int_cst (NULL_TREE
, collapse
);
34450 ordered
= collapse
;
34454 for (tree
*pc
= &clauses
; *pc
; )
34455 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34457 error_at (OMP_CLAUSE_LOCATION (*pc
),
34458 "%<linear%> clause may not be specified together "
34459 "with %<ordered%> clause with a parameter");
34460 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34463 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34466 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34467 count
= ordered
? ordered
: collapse
;
34469 declv
= make_tree_vec (count
);
34470 initv
= make_tree_vec (count
);
34471 condv
= make_tree_vec (count
);
34472 incrv
= make_tree_vec (count
);
34474 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34476 for (i
= 0; i
< count
; i
++)
34478 int bracecount
= 0;
34479 tree add_private_clause
= NULL_TREE
;
34482 if (code
!= CILK_FOR
34483 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34486 cp_parser_error (parser
, "for statement expected");
34489 if (code
== CILK_FOR
34490 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
34493 cp_parser_error (parser
, "_Cilk_for statement expected");
34496 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34498 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34501 init
= orig_init
= decl
= real_decl
= NULL
;
34502 this_pre_body
= push_stmt_list ();
34505 = cp_parser_omp_for_loop_init (parser
, code
,
34506 this_pre_body
, for_block
,
34507 init
, orig_init
, decl
, real_decl
);
34509 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34512 this_pre_body
= pop_stmt_list (this_pre_body
);
34516 pre_body
= push_stmt_list ();
34518 add_stmt (this_pre_body
);
34519 pre_body
= pop_stmt_list (pre_body
);
34522 pre_body
= this_pre_body
;
34527 if (cclauses
!= NULL
34528 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34529 && real_decl
!= NULL_TREE
)
34532 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34533 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34534 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34536 error_at (loc
, "iteration variable %qD"
34537 " should not be firstprivate", real_decl
);
34538 *c
= OMP_CLAUSE_CHAIN (*c
);
34540 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34541 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34543 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34545 *c
= OMP_CLAUSE_CHAIN (*c
);
34546 if (code
== OMP_SIMD
)
34548 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34549 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34553 OMP_CLAUSE_CHAIN (l
) = clauses
;
34556 add_private_clause
= NULL_TREE
;
34560 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34561 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34562 add_private_clause
= NULL_TREE
;
34563 c
= &OMP_CLAUSE_CHAIN (*c
);
34567 if (add_private_clause
)
34570 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34572 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34573 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34574 && OMP_CLAUSE_DECL (c
) == decl
)
34576 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34577 && OMP_CLAUSE_DECL (c
) == decl
)
34578 error_at (loc
, "iteration variable %qD "
34579 "should not be firstprivate",
34581 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34582 && OMP_CLAUSE_DECL (c
) == decl
)
34583 error_at (loc
, "iteration variable %qD should not be reduction",
34588 if (code
!= OMP_SIMD
)
34589 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
34590 else if (collapse
== 1)
34591 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
34593 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
34594 OMP_CLAUSE_DECL (c
) = add_private_clause
;
34595 c
= finish_omp_clauses (c
, C_ORT_OMP
);
34598 OMP_CLAUSE_CHAIN (c
) = clauses
;
34600 /* For linear, signal that we need to fill up
34601 the so far unknown linear step. */
34602 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
34603 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
34609 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34610 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
34611 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34614 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
34616 /* If decl is an iterator, preserve the operator on decl
34617 until finish_omp_for. */
34619 && ((processing_template_decl
34620 && (TREE_TYPE (real_decl
) == NULL_TREE
34621 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
34622 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
34623 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
34625 incr
= cp_parser_expression (parser
);
34626 if (!EXPR_HAS_LOCATION (incr
))
34627 protected_set_expr_location (incr
, input_location
);
34630 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34631 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34632 /*or_comma=*/false,
34633 /*consume_paren=*/true);
34635 TREE_VEC_ELT (declv
, i
) = decl
;
34636 TREE_VEC_ELT (initv
, i
) = init
;
34637 TREE_VEC_ELT (condv
, i
) = cond
;
34638 TREE_VEC_ELT (incrv
, i
) = incr
;
34641 orig_inits
.safe_grow_cleared (i
+ 1);
34642 orig_inits
[i
] = orig_init
;
34645 if (i
== count
- 1)
34648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34649 in between the collapsed for loops to be still considered perfectly
34650 nested. Hopefully the final version clarifies this.
34651 For now handle (multiple) {'s and empty statements. */
34652 cp_parser_parse_tentatively (parser
);
34655 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34657 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34659 cp_lexer_consume_token (parser
->lexer
);
34662 else if (bracecount
34663 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34664 cp_lexer_consume_token (parser
->lexer
);
34667 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34668 error_at (loc
, "not enough for loops to collapse");
34669 collapse_err
= true;
34670 cp_parser_abort_tentative_parse (parser
);
34678 cp_parser_parse_definitely (parser
);
34679 nbraces
+= bracecount
;
34686 /* Note that we saved the original contents of this flag when we entered
34687 the structured block, and so we don't need to re-save it here. */
34688 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34689 parser
->in_statement
= IN_CILK_SIMD_FOR
;
34691 parser
->in_statement
= IN_OMP_FOR
;
34693 /* Note that the grammar doesn't call for a structured block here,
34694 though the loop as a whole is a structured block. */
34695 body
= push_stmt_list ();
34696 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34697 body
= pop_stmt_list (body
);
34699 if (declv
== NULL_TREE
)
34702 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
34703 body
, pre_body
, &orig_inits
, clauses
);
34707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34709 cp_lexer_consume_token (parser
->lexer
);
34712 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34713 cp_lexer_consume_token (parser
->lexer
);
34718 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
34719 "collapsed loops not perfectly nested");
34721 collapse_err
= true;
34722 cp_parser_statement_seq_opt (parser
, NULL
);
34723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
34728 while (!for_block
->is_empty ())
34730 tree t
= for_block
->pop ();
34731 if (TREE_CODE (t
) == STATEMENT_LIST
)
34732 add_stmt (pop_stmt_list (t
));
34736 release_tree_vector (for_block
);
34741 /* Helper function for OpenMP parsing, split clauses and call
34742 finish_omp_clauses on each of the set of clauses afterwards. */
34745 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
34746 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
34749 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
34750 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
34752 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
34756 #pragma omp simd simd-clause[optseq] new-line
34759 #define OMP_SIMD_CLAUSE_MASK \
34760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34770 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
34771 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34774 tree clauses
, sb
, ret
;
34776 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34778 strcat (p_name
, " simd");
34779 mask
|= OMP_SIMD_CLAUSE_MASK
;
34781 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34785 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
34786 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
34787 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
34788 OMP_CLAUSE_ORDERED
);
34789 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
34791 error_at (OMP_CLAUSE_LOCATION (c
),
34792 "%<ordered%> clause with parameter may not be specified "
34793 "on %qs construct", p_name
);
34794 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
34798 sb
= begin_omp_structured_block ();
34799 save
= cp_parser_begin_omp_structured_block (parser
);
34801 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
34803 cp_parser_end_omp_structured_block (parser
, save
);
34804 add_stmt (finish_omp_structured_block (sb
));
34810 #pragma omp for for-clause[optseq] new-line
34814 #pragma omp for simd for-simd-clause[optseq] new-line
34817 #define OMP_FOR_CLAUSE_MASK \
34818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34829 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
34830 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34833 tree clauses
, sb
, ret
;
34835 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34837 strcat (p_name
, " for");
34838 mask
|= OMP_FOR_CLAUSE_MASK
;
34839 /* parallel for{, simd} disallows nowait clause, but for
34840 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34841 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
34842 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
34843 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34844 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34845 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
34847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34849 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34850 const char *p
= IDENTIFIER_POINTER (id
);
34852 if (strcmp (p
, "simd") == 0)
34854 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34855 if (cclauses
== NULL
)
34856 cclauses
= cclauses_buf
;
34858 cp_lexer_consume_token (parser
->lexer
);
34859 if (!flag_openmp
) /* flag_openmp_simd */
34860 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34862 sb
= begin_omp_structured_block ();
34863 save
= cp_parser_begin_omp_structured_block (parser
);
34864 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34866 cp_parser_end_omp_structured_block (parser
, save
);
34867 tree body
= finish_omp_structured_block (sb
);
34870 ret
= make_node (OMP_FOR
);
34871 TREE_TYPE (ret
) = void_type_node
;
34872 OMP_FOR_BODY (ret
) = body
;
34873 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34874 SET_EXPR_LOCATION (ret
, loc
);
34879 if (!flag_openmp
) /* flag_openmp_simd */
34881 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34885 /* Composite distribute parallel for disallows linear clause. */
34886 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34887 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
34889 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34893 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
34894 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34897 sb
= begin_omp_structured_block ();
34898 save
= cp_parser_begin_omp_structured_block (parser
);
34900 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
34902 cp_parser_end_omp_structured_block (parser
, save
);
34903 add_stmt (finish_omp_structured_block (sb
));
34909 # pragma omp master new-line
34910 structured-block */
34913 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34915 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34916 return c_finish_omp_master (input_location
,
34917 cp_parser_omp_structured_block (parser
, if_p
));
34921 # pragma omp ordered new-line
34925 # pragma omp ordered ordered-clauses new-line
34926 structured-block */
34928 #define OMP_ORDERED_CLAUSE_MASK \
34929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34932 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34933 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34936 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
34937 enum pragma_context context
, bool *if_p
)
34939 location_t loc
= pragma_tok
->location
;
34941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34943 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34944 const char *p
= IDENTIFIER_POINTER (id
);
34946 if (strcmp (p
, "depend") == 0)
34948 if (context
== pragma_stmt
)
34950 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
34951 "%<depend%> clause may only be used in compound "
34953 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34957 = cp_parser_omp_all_clauses (parser
,
34958 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
34959 "#pragma omp ordered", pragma_tok
);
34960 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
34966 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
34967 "#pragma omp ordered", pragma_tok
);
34968 c_finish_omp_ordered (loc
, clauses
,
34969 cp_parser_omp_structured_block (parser
, if_p
));
34976 { section-sequence }
34979 section-directive[opt] structured-block
34980 section-sequence section-directive structured-block */
34983 cp_parser_omp_sections_scope (cp_parser
*parser
)
34985 tree stmt
, substmt
;
34986 bool error_suppress
= false;
34989 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
34992 stmt
= push_stmt_list ();
34994 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
34995 != PRAGMA_OMP_SECTION
)
34997 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34998 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34999 add_stmt (substmt
);
35004 tok
= cp_lexer_peek_token (parser
->lexer
);
35005 if (tok
->type
== CPP_CLOSE_BRACE
)
35007 if (tok
->type
== CPP_EOF
)
35010 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35012 cp_lexer_consume_token (parser
->lexer
);
35013 cp_parser_require_pragma_eol (parser
, tok
);
35014 error_suppress
= false;
35016 else if (!error_suppress
)
35018 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35019 error_suppress
= true;
35022 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35023 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35024 add_stmt (substmt
);
35026 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
35028 substmt
= pop_stmt_list (stmt
);
35030 stmt
= make_node (OMP_SECTIONS
);
35031 TREE_TYPE (stmt
) = void_type_node
;
35032 OMP_SECTIONS_BODY (stmt
) = substmt
;
35039 # pragma omp sections sections-clause[optseq] newline
35042 #define OMP_SECTIONS_CLAUSE_MASK \
35043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35050 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35051 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35054 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35056 strcat (p_name
, " sections");
35057 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35059 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35061 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35065 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35066 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35069 ret
= cp_parser_omp_sections_scope (parser
);
35071 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35077 # pragma omp parallel parallel-clause[optseq] new-line
35079 # pragma omp parallel for parallel-for-clause[optseq] new-line
35081 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35085 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35086 structured-block */
35088 #define OMP_PARALLEL_CLAUSE_MASK \
35089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35100 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35101 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35104 tree stmt
, clauses
, block
;
35106 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35108 strcat (p_name
, " parallel");
35109 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35110 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35111 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35112 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35113 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35115 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35117 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35118 if (cclauses
== NULL
)
35119 cclauses
= cclauses_buf
;
35121 cp_lexer_consume_token (parser
->lexer
);
35122 if (!flag_openmp
) /* flag_openmp_simd */
35123 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35125 block
= begin_omp_parallel ();
35126 save
= cp_parser_begin_omp_structured_block (parser
);
35127 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35129 cp_parser_end_omp_structured_block (parser
, save
);
35130 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35132 if (ret
== NULL_TREE
)
35134 OMP_PARALLEL_COMBINED (stmt
) = 1;
35137 /* When combined with distribute, parallel has to be followed by for.
35138 #pragma omp target parallel is allowed though. */
35140 && (mask
& (OMP_CLAUSE_MASK_1
35141 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35143 error_at (loc
, "expected %<for%> after %qs", p_name
);
35144 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35147 else if (!flag_openmp
) /* flag_openmp_simd */
35149 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35152 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35154 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35155 const char *p
= IDENTIFIER_POINTER (id
);
35156 if (strcmp (p
, "sections") == 0)
35158 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35159 cclauses
= cclauses_buf
;
35161 cp_lexer_consume_token (parser
->lexer
);
35162 block
= begin_omp_parallel ();
35163 save
= cp_parser_begin_omp_structured_block (parser
);
35164 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35165 cp_parser_end_omp_structured_block (parser
, save
);
35166 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35168 OMP_PARALLEL_COMBINED (stmt
) = 1;
35173 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35177 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35178 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35181 block
= begin_omp_parallel ();
35182 save
= cp_parser_begin_omp_structured_block (parser
);
35183 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35184 cp_parser_end_omp_structured_block (parser
, save
);
35185 stmt
= finish_omp_parallel (clauses
, block
);
35190 # pragma omp single single-clause[optseq] new-line
35191 structured-block */
35193 #define OMP_SINGLE_CLAUSE_MASK \
35194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35200 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35202 tree stmt
= make_node (OMP_SINGLE
);
35203 TREE_TYPE (stmt
) = void_type_node
;
35205 OMP_SINGLE_CLAUSES (stmt
)
35206 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35207 "#pragma omp single", pragma_tok
);
35208 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35210 return add_stmt (stmt
);
35214 # pragma omp task task-clause[optseq] new-line
35215 structured-block */
35217 #define OMP_TASK_CLAUSE_MASK \
35218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35230 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35232 tree clauses
, block
;
35235 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35236 "#pragma omp task", pragma_tok
);
35237 block
= begin_omp_task ();
35238 save
= cp_parser_begin_omp_structured_block (parser
);
35239 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35240 cp_parser_end_omp_structured_block (parser
, save
);
35241 return finish_omp_task (clauses
, block
);
35245 # pragma omp taskwait new-line */
35248 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35250 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35251 finish_omp_taskwait ();
35255 # pragma omp taskyield new-line */
35258 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35260 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35261 finish_omp_taskyield ();
35265 # pragma omp taskgroup new-line
35266 structured-block */
35269 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35271 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35272 return c_finish_omp_taskgroup (input_location
,
35273 cp_parser_omp_structured_block (parser
,
35279 # pragma omp threadprivate (variable-list) */
35282 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35286 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35287 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35289 finish_omp_threadprivate (vars
);
35293 # pragma omp cancel cancel-clause[optseq] new-line */
35295 #define OMP_CANCEL_CLAUSE_MASK \
35296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35303 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35305 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35306 "#pragma omp cancel", pragma_tok
);
35307 finish_omp_cancel (clauses
);
35311 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35313 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35320 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35321 enum pragma_context context
)
35324 bool point_seen
= false;
35326 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35328 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35329 const char *p
= IDENTIFIER_POINTER (id
);
35331 if (strcmp (p
, "point") == 0)
35333 cp_lexer_consume_token (parser
->lexer
);
35339 cp_parser_error (parser
, "expected %<point%>");
35340 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35344 if (context
!= pragma_compound
)
35346 if (context
== pragma_stmt
)
35347 error_at (pragma_tok
->location
,
35348 "%<#pragma %s%> may only be used in compound statements",
35349 "omp cancellation point");
35351 cp_parser_error (parser
, "expected declaration specifiers");
35352 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35356 clauses
= cp_parser_omp_all_clauses (parser
,
35357 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35358 "#pragma omp cancellation point",
35360 finish_omp_cancellation_point (clauses
);
35364 #pragma omp distribute distribute-clause[optseq] new-line
35367 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35375 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35376 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35379 tree clauses
, sb
, ret
;
35381 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35383 strcat (p_name
, " distribute");
35384 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35388 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35389 const char *p
= IDENTIFIER_POINTER (id
);
35391 bool parallel
= false;
35393 if (strcmp (p
, "simd") == 0)
35396 parallel
= strcmp (p
, "parallel") == 0;
35397 if (parallel
|| simd
)
35399 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35400 if (cclauses
== NULL
)
35401 cclauses
= cclauses_buf
;
35402 cp_lexer_consume_token (parser
->lexer
);
35403 if (!flag_openmp
) /* flag_openmp_simd */
35406 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35409 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35412 sb
= begin_omp_structured_block ();
35413 save
= cp_parser_begin_omp_structured_block (parser
);
35415 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35418 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35420 cp_parser_end_omp_structured_block (parser
, save
);
35421 tree body
= finish_omp_structured_block (sb
);
35424 ret
= make_node (OMP_DISTRIBUTE
);
35425 TREE_TYPE (ret
) = void_type_node
;
35426 OMP_FOR_BODY (ret
) = body
;
35427 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35428 SET_EXPR_LOCATION (ret
, loc
);
35433 if (!flag_openmp
) /* flag_openmp_simd */
35435 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35439 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35443 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35444 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35447 sb
= begin_omp_structured_block ();
35448 save
= cp_parser_begin_omp_structured_block (parser
);
35450 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35452 cp_parser_end_omp_structured_block (parser
, save
);
35453 add_stmt (finish_omp_structured_block (sb
));
35459 # pragma omp teams teams-clause[optseq] new-line
35460 structured-block */
35462 #define OMP_TEAMS_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35472 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35473 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35476 tree clauses
, sb
, ret
;
35478 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35480 strcat (p_name
, " teams");
35481 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35485 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35486 const char *p
= IDENTIFIER_POINTER (id
);
35487 if (strcmp (p
, "distribute") == 0)
35489 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35490 if (cclauses
== NULL
)
35491 cclauses
= cclauses_buf
;
35493 cp_lexer_consume_token (parser
->lexer
);
35494 if (!flag_openmp
) /* flag_openmp_simd */
35495 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35497 sb
= begin_omp_structured_block ();
35498 save
= cp_parser_begin_omp_structured_block (parser
);
35499 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35501 cp_parser_end_omp_structured_block (parser
, save
);
35502 tree body
= finish_omp_structured_block (sb
);
35505 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35506 ret
= make_node (OMP_TEAMS
);
35507 TREE_TYPE (ret
) = void_type_node
;
35508 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35509 OMP_TEAMS_BODY (ret
) = body
;
35510 OMP_TEAMS_COMBINED (ret
) = 1;
35511 SET_EXPR_LOCATION (ret
, loc
);
35512 return add_stmt (ret
);
35515 if (!flag_openmp
) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35521 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35525 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35526 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35529 tree stmt
= make_node (OMP_TEAMS
);
35530 TREE_TYPE (stmt
) = void_type_node
;
35531 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35532 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35533 SET_EXPR_LOCATION (stmt
, loc
);
35535 return add_stmt (stmt
);
35539 # pragma omp target data target-data-clause[optseq] new-line
35540 structured-block */
35542 #define OMP_TARGET_DATA_CLAUSE_MASK \
35543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35549 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35552 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35553 "#pragma omp target data", pragma_tok
);
35555 for (tree
*pc
= &clauses
; *pc
;)
35557 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35558 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35561 case GOMP_MAP_ALWAYS_TO
:
35562 case GOMP_MAP_FROM
:
35563 case GOMP_MAP_ALWAYS_FROM
:
35564 case GOMP_MAP_TOFROM
:
35565 case GOMP_MAP_ALWAYS_TOFROM
:
35566 case GOMP_MAP_ALLOC
:
35569 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35570 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35571 case GOMP_MAP_ALWAYS_POINTER
:
35575 error_at (OMP_CLAUSE_LOCATION (*pc
),
35576 "%<#pragma omp target data%> with map-type other "
35577 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35578 "on %<map%> clause");
35579 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35582 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35588 error_at (pragma_tok
->location
,
35589 "%<#pragma omp target data%> must contain at least "
35590 "one %<map%> clause");
35594 tree stmt
= make_node (OMP_TARGET_DATA
);
35595 TREE_TYPE (stmt
) = void_type_node
;
35596 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
35598 keep_next_level (true);
35599 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35601 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35602 return add_stmt (stmt
);
35606 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35607 structured-block */
35609 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35610 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35617 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35618 enum pragma_context context
)
35620 bool data_seen
= false;
35621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35623 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35624 const char *p
= IDENTIFIER_POINTER (id
);
35626 if (strcmp (p
, "data") == 0)
35628 cp_lexer_consume_token (parser
->lexer
);
35634 cp_parser_error (parser
, "expected %<data%>");
35635 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35639 if (context
== pragma_stmt
)
35641 error_at (pragma_tok
->location
,
35642 "%<#pragma %s%> may only be used in compound statements",
35643 "omp target enter data");
35644 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35649 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
35650 "#pragma omp target enter data", pragma_tok
);
35652 for (tree
*pc
= &clauses
; *pc
;)
35654 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35655 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35658 case GOMP_MAP_ALWAYS_TO
:
35659 case GOMP_MAP_ALLOC
:
35662 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35663 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35664 case GOMP_MAP_ALWAYS_POINTER
:
35668 error_at (OMP_CLAUSE_LOCATION (*pc
),
35669 "%<#pragma omp target enter data%> with map-type other "
35670 "than %<to%> or %<alloc%> on %<map%> clause");
35671 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35674 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35680 error_at (pragma_tok
->location
,
35681 "%<#pragma omp target enter data%> must contain at least "
35682 "one %<map%> clause");
35686 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
35687 TREE_TYPE (stmt
) = void_type_node
;
35688 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
35689 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35690 return add_stmt (stmt
);
35694 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35695 structured-block */
35697 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35705 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35706 enum pragma_context context
)
35708 bool data_seen
= false;
35709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35711 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35712 const char *p
= IDENTIFIER_POINTER (id
);
35714 if (strcmp (p
, "data") == 0)
35716 cp_lexer_consume_token (parser
->lexer
);
35722 cp_parser_error (parser
, "expected %<data%>");
35723 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35727 if (context
== pragma_stmt
)
35729 error_at (pragma_tok
->location
,
35730 "%<#pragma %s%> may only be used in compound statements",
35731 "omp target exit data");
35732 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35737 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
35738 "#pragma omp target exit data", pragma_tok
);
35740 for (tree
*pc
= &clauses
; *pc
;)
35742 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35743 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35745 case GOMP_MAP_FROM
:
35746 case GOMP_MAP_ALWAYS_FROM
:
35747 case GOMP_MAP_RELEASE
:
35748 case GOMP_MAP_DELETE
:
35751 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35752 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35753 case GOMP_MAP_ALWAYS_POINTER
:
35757 error_at (OMP_CLAUSE_LOCATION (*pc
),
35758 "%<#pragma omp target exit data%> with map-type other "
35759 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35761 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35764 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35770 error_at (pragma_tok
->location
,
35771 "%<#pragma omp target exit data%> must contain at least "
35772 "one %<map%> clause");
35776 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
35777 TREE_TYPE (stmt
) = void_type_node
;
35778 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
35779 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35780 return add_stmt (stmt
);
35784 # pragma omp target update target-update-clause[optseq] new-line */
35786 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35795 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
35796 enum pragma_context context
)
35798 if (context
== pragma_stmt
)
35800 error_at (pragma_tok
->location
,
35801 "%<#pragma %s%> may only be used in compound statements",
35802 "omp target update");
35803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35808 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
35809 "#pragma omp target update", pragma_tok
);
35810 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
35811 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
35813 error_at (pragma_tok
->location
,
35814 "%<#pragma omp target update%> must contain at least one "
35815 "%<from%> or %<to%> clauses");
35819 tree stmt
= make_node (OMP_TARGET_UPDATE
);
35820 TREE_TYPE (stmt
) = void_type_node
;
35821 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
35822 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35828 # pragma omp target target-clause[optseq] new-line
35829 structured-block */
35831 #define OMP_TARGET_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35843 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
35844 enum pragma_context context
, bool *if_p
)
35846 tree
*pc
= NULL
, stmt
;
35848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35850 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35851 const char *p
= IDENTIFIER_POINTER (id
);
35852 enum tree_code ccode
= ERROR_MARK
;
35854 if (strcmp (p
, "teams") == 0)
35856 else if (strcmp (p
, "parallel") == 0)
35857 ccode
= OMP_PARALLEL
;
35858 else if (strcmp (p
, "simd") == 0)
35860 if (ccode
!= ERROR_MARK
)
35862 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
35863 char p_name
[sizeof ("#pragma omp target teams distribute "
35864 "parallel for simd")];
35866 cp_lexer_consume_token (parser
->lexer
);
35867 strcpy (p_name
, "#pragma omp target");
35868 if (!flag_openmp
) /* flag_openmp_simd */
35874 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35875 OMP_TARGET_CLAUSE_MASK
,
35879 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35880 OMP_TARGET_CLAUSE_MASK
,
35884 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35885 OMP_TARGET_CLAUSE_MASK
,
35889 gcc_unreachable ();
35891 return stmt
!= NULL_TREE
;
35893 keep_next_level (true);
35894 tree sb
= begin_omp_structured_block (), ret
;
35895 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
35899 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35900 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35904 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35905 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35909 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35910 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35914 gcc_unreachable ();
35916 cp_parser_end_omp_structured_block (parser
, save
);
35917 tree body
= finish_omp_structured_block (sb
);
35918 if (ret
== NULL_TREE
)
35920 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
35922 /* For combined target teams, ensure the num_teams and
35923 thread_limit clause expressions are evaluated on the host,
35924 before entering the target construct. */
35926 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35927 c
; c
= OMP_CLAUSE_CHAIN (c
))
35928 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
35929 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
35930 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
35932 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
35933 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
35934 if (expr
== error_mark_node
)
35936 tree tmp
= TARGET_EXPR_SLOT (expr
);
35938 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
35939 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
35940 OMP_CLAUSE_FIRSTPRIVATE
);
35941 OMP_CLAUSE_DECL (tc
) = tmp
;
35942 OMP_CLAUSE_CHAIN (tc
)
35943 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35944 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
35947 tree stmt
= make_node (OMP_TARGET
);
35948 TREE_TYPE (stmt
) = void_type_node
;
35949 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35950 OMP_TARGET_BODY (stmt
) = body
;
35951 OMP_TARGET_COMBINED (stmt
) = 1;
35952 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35954 pc
= &OMP_TARGET_CLAUSES (stmt
);
35955 goto check_clauses
;
35957 else if (!flag_openmp
) /* flag_openmp_simd */
35959 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35962 else if (strcmp (p
, "data") == 0)
35964 cp_lexer_consume_token (parser
->lexer
);
35965 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
35968 else if (strcmp (p
, "enter") == 0)
35970 cp_lexer_consume_token (parser
->lexer
);
35971 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
35974 else if (strcmp (p
, "exit") == 0)
35976 cp_lexer_consume_token (parser
->lexer
);
35977 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
35980 else if (strcmp (p
, "update") == 0)
35982 cp_lexer_consume_token (parser
->lexer
);
35983 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
35986 if (!flag_openmp
) /* flag_openmp_simd */
35988 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35992 stmt
= make_node (OMP_TARGET
);
35993 TREE_TYPE (stmt
) = void_type_node
;
35995 OMP_TARGET_CLAUSES (stmt
)
35996 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
35997 "#pragma omp target", pragma_tok
);
35998 pc
= &OMP_TARGET_CLAUSES (stmt
);
35999 keep_next_level (true);
36000 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36002 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36008 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36009 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36012 case GOMP_MAP_ALWAYS_TO
:
36013 case GOMP_MAP_FROM
:
36014 case GOMP_MAP_ALWAYS_FROM
:
36015 case GOMP_MAP_TOFROM
:
36016 case GOMP_MAP_ALWAYS_TOFROM
:
36017 case GOMP_MAP_ALLOC
:
36018 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36019 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36020 case GOMP_MAP_ALWAYS_POINTER
:
36023 error_at (OMP_CLAUSE_LOCATION (*pc
),
36024 "%<#pragma omp target%> with map-type other "
36025 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36026 "on %<map%> clause");
36027 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36030 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36036 # pragma acc cache (variable-list) new-line
36040 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36042 tree stmt
, clauses
;
36044 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36045 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36047 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36049 stmt
= make_node (OACC_CACHE
);
36050 TREE_TYPE (stmt
) = void_type_node
;
36051 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36052 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36059 # pragma acc data oacc-data-clause[optseq] new-line
36060 structured-block */
36062 #define OACC_DATA_CLAUSE_MASK \
36063 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36076 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36078 tree stmt
, clauses
, block
;
36081 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36082 "#pragma acc data", pragma_tok
);
36084 block
= begin_omp_parallel ();
36085 save
= cp_parser_begin_omp_structured_block (parser
);
36086 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36087 cp_parser_end_omp_structured_block (parser
, save
);
36088 stmt
= finish_oacc_data (clauses
, block
);
36093 # pragma acc host_data <clauses> new-line
36094 structured-block */
36096 #define OACC_HOST_DATA_CLAUSE_MASK \
36097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36100 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36102 tree stmt
, clauses
, block
;
36105 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36106 "#pragma acc host_data", pragma_tok
);
36108 block
= begin_omp_parallel ();
36109 save
= cp_parser_begin_omp_structured_block (parser
);
36110 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36111 cp_parser_end_omp_structured_block (parser
, save
);
36112 stmt
= finish_oacc_host_data (clauses
, block
);
36117 # pragma acc declare oacc-data-clause[optseq] new-line
36120 #define OACC_DECLARE_CLAUSE_MASK \
36121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36135 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36137 tree clauses
, stmt
;
36138 bool error
= false;
36140 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36141 "#pragma acc declare", pragma_tok
, true);
36144 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36146 error_at (pragma_tok
->location
,
36147 "no valid clauses specified in %<#pragma acc declare%>");
36151 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36153 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36154 tree decl
= OMP_CLAUSE_DECL (t
);
36155 if (!DECL_P (decl
))
36157 error_at (loc
, "array section in %<#pragma acc declare%>");
36161 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36162 switch (OMP_CLAUSE_MAP_KIND (t
))
36164 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36165 case GOMP_MAP_FORCE_ALLOC
:
36166 case GOMP_MAP_FORCE_TO
:
36167 case GOMP_MAP_FORCE_DEVICEPTR
:
36168 case GOMP_MAP_DEVICE_RESIDENT
:
36171 case GOMP_MAP_LINK
:
36172 if (!global_bindings_p ()
36173 && (TREE_STATIC (decl
)
36174 || !DECL_EXTERNAL (decl
)))
36177 "%qD must be a global variable in "
36178 "%<#pragma acc declare link%>",
36186 if (global_bindings_p ())
36188 error_at (loc
, "invalid OpenACC clause at file scope");
36192 if (DECL_EXTERNAL (decl
))
36195 "invalid use of %<extern%> variable %qD "
36196 "in %<#pragma acc declare%>", decl
);
36200 else if (TREE_PUBLIC (decl
))
36203 "invalid use of %<global%> variable %qD "
36204 "in %<#pragma acc declare%>", decl
);
36211 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36212 || lookup_attribute ("omp declare target link",
36213 DECL_ATTRIBUTES (decl
)))
36215 error_at (loc
, "variable %qD used more than once with "
36216 "%<#pragma acc declare%>", decl
);
36225 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36226 id
= get_identifier ("omp declare target link");
36228 id
= get_identifier ("omp declare target");
36230 DECL_ATTRIBUTES (decl
)
36231 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36232 if (global_bindings_p ())
36234 symtab_node
*node
= symtab_node::get (decl
);
36237 node
->offloadable
= 1;
36238 if (ENABLE_OFFLOADING
)
36240 g
->have_offload
= true;
36241 if (is_a
<varpool_node
*> (node
))
36242 vec_safe_push (offload_vars
, decl
);
36249 if (error
|| global_bindings_p ())
36252 stmt
= make_node (OACC_DECLARE
);
36253 TREE_TYPE (stmt
) = void_type_node
;
36254 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36255 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36263 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36267 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36269 LOC is the location of the #pragma token.
36272 #define OACC_ENTER_DATA_CLAUSE_MASK \
36273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36281 #define OACC_EXIT_DATA_CLAUSE_MASK \
36282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36289 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36292 location_t loc
= pragma_tok
->location
;
36293 tree stmt
, clauses
;
36294 const char *p
= "";
36296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36297 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36299 if (strcmp (p
, "data") != 0)
36301 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36302 enter
? "enter" : "exit");
36303 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36307 cp_lexer_consume_token (parser
->lexer
);
36310 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36311 "#pragma acc enter data", pragma_tok
);
36313 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36314 "#pragma acc exit data", pragma_tok
);
36316 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36318 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36319 enter
? "enter" : "exit");
36323 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36324 TREE_TYPE (stmt
) = void_type_node
;
36325 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36326 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36332 # pragma acc loop oacc-loop-clause[optseq] new-line
36333 structured-block */
36335 #define OACC_LOOP_CLAUSE_MASK \
36336 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36348 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36349 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36351 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36353 strcat (p_name
, " loop");
36354 mask
|= OACC_LOOP_CLAUSE_MASK
;
36356 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36360 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36362 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36364 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36367 tree block
= begin_omp_structured_block ();
36368 int save
= cp_parser_begin_omp_structured_block (parser
);
36369 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36370 cp_parser_end_omp_structured_block (parser
, save
);
36371 add_stmt (finish_omp_structured_block (block
));
36377 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36382 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36386 #define OACC_KERNELS_CLAUSE_MASK \
36387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36402 #define OACC_PARALLEL_CLAUSE_MASK \
36403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36425 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36426 char *p_name
, bool *if_p
)
36428 omp_clause_mask mask
;
36429 enum tree_code code
;
36430 switch (cp_parser_pragma_kind (pragma_tok
))
36432 case PRAGMA_OACC_KERNELS
:
36433 strcat (p_name
, " kernels");
36434 mask
= OACC_KERNELS_CLAUSE_MASK
;
36435 code
= OACC_KERNELS
;
36437 case PRAGMA_OACC_PARALLEL
:
36438 strcat (p_name
, " parallel");
36439 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36440 code
= OACC_PARALLEL
;
36443 gcc_unreachable ();
36446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36449 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36450 if (strcmp (p
, "loop") == 0)
36452 cp_lexer_consume_token (parser
->lexer
);
36453 tree block
= begin_omp_parallel ();
36455 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36457 return finish_omp_construct (code
, block
, clauses
);
36461 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36463 tree block
= begin_omp_parallel ();
36464 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36465 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36466 cp_parser_end_omp_structured_block (parser
, save
);
36467 return finish_omp_construct (code
, block
, clauses
);
36471 # pragma acc update oacc-update-clause[optseq] new-line
36474 #define OACC_UPDATE_CLAUSE_MASK \
36475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36483 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36485 tree stmt
, clauses
;
36487 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36488 "#pragma acc update", pragma_tok
);
36490 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36492 error_at (pragma_tok
->location
,
36493 "%<#pragma acc update%> must contain at least one "
36494 "%<device%> or %<host%> or %<self%> clause");
36498 stmt
= make_node (OACC_UPDATE
);
36499 TREE_TYPE (stmt
) = void_type_node
;
36500 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36501 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36507 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36509 LOC is the location of the #pragma token.
36512 #define OACC_WAIT_CLAUSE_MASK \
36513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36516 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36518 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36519 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36521 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36522 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36524 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36525 "#pragma acc wait", pragma_tok
);
36527 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36528 stmt
= finish_expr_stmt (stmt
);
36534 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36536 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36545 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36546 enum pragma_context context
)
36548 bool first_p
= parser
->omp_declare_simd
== NULL
;
36549 cp_omp_declare_simd_data data
;
36552 data
.error_seen
= false;
36553 data
.fndecl_seen
= false;
36554 data
.tokens
= vNULL
;
36555 data
.clauses
= NULL_TREE
;
36556 /* It is safe to take the address of a local variable; it will only be
36557 used while this scope is live. */
36558 parser
->omp_declare_simd
= &data
;
36561 /* Store away all pragma tokens. */
36562 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36563 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36564 cp_lexer_consume_token (parser
->lexer
);
36565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36566 parser
->omp_declare_simd
->error_seen
= true;
36567 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36568 struct cp_token_cache
*cp
36569 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36570 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36574 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36575 cp_parser_pragma (parser
, context
, NULL
);
36578 case pragma_external
:
36579 cp_parser_declaration (parser
);
36581 case pragma_member
:
36582 cp_parser_member_declaration (parser
);
36584 case pragma_objc_icode
:
36585 cp_parser_block_declaration (parser
, /*statement_p=*/false);
36588 cp_parser_declaration_statement (parser
);
36591 if (parser
->omp_declare_simd
36592 && !parser
->omp_declare_simd
->error_seen
36593 && !parser
->omp_declare_simd
->fndecl_seen
)
36594 error_at (pragma_tok
->location
,
36595 "%<#pragma omp declare simd%> not immediately followed by "
36596 "function declaration or definition");
36597 data
.tokens
.release ();
36598 parser
->omp_declare_simd
= NULL
;
36602 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36603 This function is modelled similar to the late parsing of omp declare
36607 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
36609 struct cp_token_cache
*ce
;
36610 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
36613 if (parser
->omp_declare_simd
!= NULL
36614 || lookup_attribute ("simd", attrs
))
36616 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36617 "used in the same function marked as a Cilk Plus SIMD-enabled "
36619 parser
->cilk_simd_fn_info
->tokens
.release ();
36620 XDELETE (parser
->cilk_simd_fn_info
);
36621 parser
->cilk_simd_fn_info
= NULL
;
36624 if (!info
->error_seen
&& info
->fndecl_seen
)
36626 error ("vector attribute not immediately followed by a single function"
36627 " declaration or definition");
36628 info
->error_seen
= true;
36630 if (info
->error_seen
)
36633 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
36637 cp_parser_push_lexer_for_tokens (parser
, ce
);
36638 parser
->lexer
->in_pragma
= true;
36639 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
36640 "SIMD-enabled functions attribute",
36642 cp_parser_pop_lexer (parser
);
36644 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36646 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
36647 TREE_CHAIN (c
) = attrs
;
36650 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36651 TREE_CHAIN (c
) = attrs
;
36652 if (processing_template_decl
)
36653 ATTR_IS_DEPENDENT (c
) = 1;
36656 info
->fndecl_seen
= true;
36657 parser
->cilk_simd_fn_info
->tokens
.release ();
36658 XDELETE (parser
->cilk_simd_fn_info
);
36659 parser
->cilk_simd_fn_info
= NULL
;
36663 /* Finalize #pragma omp declare simd clauses after direct declarator has
36664 been parsed, and put that into "omp declare simd" attribute. */
36667 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
36669 struct cp_token_cache
*ce
;
36670 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
36673 if (!data
->error_seen
&& data
->fndecl_seen
)
36675 error ("%<#pragma omp declare simd%> not immediately followed by "
36676 "a single function declaration or definition");
36677 data
->error_seen
= true;
36679 if (data
->error_seen
)
36682 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
36686 cp_parser_push_lexer_for_tokens (parser
, ce
);
36687 parser
->lexer
->in_pragma
= true;
36688 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
36689 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36690 cp_lexer_consume_token (parser
->lexer
);
36691 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
36692 "#pragma omp declare simd", pragma_tok
);
36693 cp_parser_pop_lexer (parser
);
36695 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36696 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36697 TREE_CHAIN (c
) = attrs
;
36698 if (processing_template_decl
)
36699 ATTR_IS_DEPENDENT (c
) = 1;
36703 data
->fndecl_seen
= true;
36709 # pragma omp declare target new-line
36710 declarations and definitions
36711 # pragma omp end declare target new-line
36714 # pragma omp declare target ( extended-list ) new-line
36716 # pragma omp declare target declare-target-clauses[seq] new-line */
36718 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36723 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36725 tree clauses
= NULL_TREE
;
36726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36728 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
36729 "#pragma omp declare target", pragma_tok
);
36730 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36732 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
36734 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36735 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36739 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36740 scope_chain
->omp_declare_target_attribute
++;
36743 if (scope_chain
->omp_declare_target_attribute
)
36744 error_at (pragma_tok
->location
,
36745 "%<#pragma omp declare target%> with clauses in between "
36746 "%<#pragma omp declare target%> without clauses and "
36747 "%<#pragma omp end declare target%>");
36748 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36750 tree t
= OMP_CLAUSE_DECL (c
), id
;
36751 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
36752 tree at2
= lookup_attribute ("omp declare target link",
36753 DECL_ATTRIBUTES (t
));
36754 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
36756 id
= get_identifier ("omp declare target link");
36757 std::swap (at1
, at2
);
36760 id
= get_identifier ("omp declare target");
36763 error_at (OMP_CLAUSE_LOCATION (c
),
36764 "%qD specified both in declare target %<link%> and %<to%>"
36770 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
36771 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
36774 symtab_node
*node
= symtab_node::get (t
);
36777 node
->offloadable
= 1;
36778 if (ENABLE_OFFLOADING
)
36780 g
->have_offload
= true;
36781 if (is_a
<varpool_node
*> (node
))
36782 vec_safe_push (offload_vars
, t
);
36790 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36792 const char *p
= "";
36793 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36795 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36796 p
= IDENTIFIER_POINTER (id
);
36798 if (strcmp (p
, "declare") == 0)
36800 cp_lexer_consume_token (parser
->lexer
);
36802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36804 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36805 p
= IDENTIFIER_POINTER (id
);
36807 if (strcmp (p
, "target") == 0)
36808 cp_lexer_consume_token (parser
->lexer
);
36811 cp_parser_error (parser
, "expected %<target%>");
36812 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36818 cp_parser_error (parser
, "expected %<declare%>");
36819 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36822 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36823 if (!scope_chain
->omp_declare_target_attribute
)
36824 error_at (pragma_tok
->location
,
36825 "%<#pragma omp end declare target%> without corresponding "
36826 "%<#pragma omp declare target%>");
36828 scope_chain
->omp_declare_target_attribute
--;
36831 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36832 expression and optional initializer clause of
36833 #pragma omp declare reduction. We store the expression(s) as
36834 either 3, 6 or 7 special statements inside of the artificial function's
36835 body. The first two statements are DECL_EXPRs for the artificial
36836 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36837 expression that uses those variables.
36838 If there was any INITIALIZER clause, this is followed by further statements,
36839 the fourth and fifth statements are DECL_EXPRs for the artificial
36840 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36841 constructor variant (first token after open paren is not omp_priv),
36842 then the sixth statement is a statement with the function call expression
36843 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36844 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36845 to initialize the OMP_PRIV artificial variable and there is seventh
36846 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36849 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
36851 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
36852 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
36853 type
= TREE_TYPE (type
);
36854 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
36855 DECL_ARTIFICIAL (omp_out
) = 1;
36856 pushdecl (omp_out
);
36857 add_decl_expr (omp_out
);
36858 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
36859 DECL_ARTIFICIAL (omp_in
) = 1;
36861 add_decl_expr (omp_in
);
36863 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
36865 keep_next_level (true);
36866 tree block
= begin_omp_structured_block ();
36867 combiner
= cp_parser_expression (parser
);
36868 finish_expr_stmt (combiner
);
36869 block
= finish_omp_structured_block (block
);
36872 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36875 const char *p
= "";
36876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36878 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36879 p
= IDENTIFIER_POINTER (id
);
36882 if (strcmp (p
, "initializer") == 0)
36884 cp_lexer_consume_token (parser
->lexer
);
36885 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36889 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36891 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36892 p
= IDENTIFIER_POINTER (id
);
36895 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
36896 DECL_ARTIFICIAL (omp_priv
) = 1;
36897 pushdecl (omp_priv
);
36898 add_decl_expr (omp_priv
);
36899 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
36900 DECL_ARTIFICIAL (omp_orig
) = 1;
36901 pushdecl (omp_orig
);
36902 add_decl_expr (omp_orig
);
36904 keep_next_level (true);
36905 block
= begin_omp_structured_block ();
36908 if (strcmp (p
, "omp_priv") == 0)
36910 bool is_direct_init
, is_non_constant_init
;
36912 cp_lexer_consume_token (parser
->lexer
);
36913 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
36915 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36916 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
36918 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
36919 == CPP_CLOSE_PAREN
))
36921 finish_omp_structured_block (block
);
36922 error ("invalid initializer clause");
36925 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
36926 &is_non_constant_init
);
36927 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
36928 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
36932 cp_parser_parse_tentatively (parser
);
36933 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36934 /*check_dependency_p=*/true,
36935 /*template_p=*/NULL
,
36936 /*declarator_p=*/false,
36937 /*optional_p=*/false);
36938 vec
<tree
, va_gc
> *args
;
36939 if (fn_name
== error_mark_node
36940 || cp_parser_error_occurred (parser
)
36941 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36942 || ((args
= cp_parser_parenthesized_expression_list
36943 (parser
, non_attr
, /*cast_p=*/false,
36944 /*allow_expansion_p=*/true,
36945 /*non_constant_p=*/NULL
)),
36946 cp_parser_error_occurred (parser
)))
36948 finish_omp_structured_block (block
);
36949 cp_parser_abort_tentative_parse (parser
);
36950 cp_parser_error (parser
, "expected id-expression (arguments)");
36955 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
36956 if (arg
== omp_priv
36957 || (TREE_CODE (arg
) == ADDR_EXPR
36958 && TREE_OPERAND (arg
, 0) == omp_priv
))
36960 cp_parser_abort_tentative_parse (parser
);
36961 if (arg
== NULL_TREE
)
36962 error ("one of the initializer call arguments should be %<omp_priv%>"
36963 " or %<&omp_priv%>");
36964 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
36966 finish_expr_stmt (initializer
);
36969 block
= finish_omp_structured_block (block
);
36970 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
36974 add_decl_expr (omp_orig
);
36976 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36980 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
36981 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
36987 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36988 initializer-clause[opt] new-line
36990 initializer-clause:
36991 initializer (omp_priv initializer)
36992 initializer (function-name (argument-list)) */
36995 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
36996 enum pragma_context
)
36998 auto_vec
<tree
> types
;
36999 enum tree_code reduc_code
= ERROR_MARK
;
37000 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37002 cp_token
*first_token
;
37003 cp_token_cache
*cp
;
37007 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37008 p
= obstack_alloc (&declarator_obstack
, 0);
37010 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37013 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37016 reduc_code
= PLUS_EXPR
;
37019 reduc_code
= MULT_EXPR
;
37022 reduc_code
= MINUS_EXPR
;
37025 reduc_code
= BIT_AND_EXPR
;
37028 reduc_code
= BIT_XOR_EXPR
;
37031 reduc_code
= BIT_IOR_EXPR
;
37034 reduc_code
= TRUTH_ANDIF_EXPR
;
37037 reduc_code
= TRUTH_ORIF_EXPR
;
37040 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37043 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37044 "%<|%>, %<&&%>, %<||%> or identifier");
37048 if (reduc_code
!= ERROR_MARK
)
37049 cp_lexer_consume_token (parser
->lexer
);
37051 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37052 if (reduc_id
== error_mark_node
)
37055 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37058 /* Types may not be defined in declare reduction type list. */
37059 const char *saved_message
;
37060 saved_message
= parser
->type_definition_forbidden_message
;
37061 parser
->type_definition_forbidden_message
37062 = G_("types may not be defined in declare reduction type list");
37063 bool saved_colon_corrects_to_scope_p
;
37064 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37065 parser
->colon_corrects_to_scope_p
= false;
37066 bool saved_colon_doesnt_start_class_def_p
;
37067 saved_colon_doesnt_start_class_def_p
37068 = parser
->colon_doesnt_start_class_def_p
;
37069 parser
->colon_doesnt_start_class_def_p
= true;
37073 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37074 type
= cp_parser_type_id (parser
);
37075 if (type
== error_mark_node
)
37077 else if (ARITHMETIC_TYPE_P (type
)
37078 && (orig_reduc_id
== NULL_TREE
37079 || (TREE_CODE (type
) != COMPLEX_TYPE
37080 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37082 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37084 error_at (loc
, "predeclared arithmetic type %qT in "
37085 "%<#pragma omp declare reduction%>", type
);
37086 else if (TREE_CODE (type
) == FUNCTION_TYPE
37087 || TREE_CODE (type
) == METHOD_TYPE
37088 || TREE_CODE (type
) == ARRAY_TYPE
)
37089 error_at (loc
, "function or array type %qT in "
37090 "%<#pragma omp declare reduction%>", type
);
37091 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37092 error_at (loc
, "reference type %qT in "
37093 "%<#pragma omp declare reduction%>", type
);
37094 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37095 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37096 "%<#pragma omp declare reduction%>", type
);
37098 types
.safe_push (type
);
37100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37101 cp_lexer_consume_token (parser
->lexer
);
37106 /* Restore the saved message. */
37107 parser
->type_definition_forbidden_message
= saved_message
;
37108 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37109 parser
->colon_doesnt_start_class_def_p
37110 = saved_colon_doesnt_start_class_def_p
;
37112 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37113 || types
.is_empty ())
37116 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37120 first_token
= cp_lexer_peek_token (parser
->lexer
);
37123 FOR_EACH_VEC_ELT (types
, i
, type
)
37126 = build_function_type_list (void_type_node
,
37127 cp_build_reference_type (type
, false),
37129 tree this_reduc_id
= reduc_id
;
37130 if (!dependent_type_p (type
))
37131 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37132 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37133 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37134 DECL_ARTIFICIAL (fndecl
) = 1;
37135 DECL_EXTERNAL (fndecl
) = 1;
37136 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37137 DECL_IGNORED_P (fndecl
) = 1;
37138 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37139 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37140 DECL_ATTRIBUTES (fndecl
)
37141 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37142 DECL_ATTRIBUTES (fndecl
));
37143 if (processing_template_decl
)
37144 fndecl
= push_template_decl (fndecl
);
37145 bool block_scope
= false;
37146 tree block
= NULL_TREE
;
37147 if (current_function_decl
)
37149 block_scope
= true;
37150 DECL_CONTEXT (fndecl
) = global_namespace
;
37151 if (!processing_template_decl
)
37154 else if (current_class_type
)
37158 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37159 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37160 cp_lexer_consume_token (parser
->lexer
);
37161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37163 cp
= cp_token_cache_new (first_token
,
37164 cp_lexer_peek_nth_token (parser
->lexer
,
37167 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37168 finish_member_declaration (fndecl
);
37169 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37170 DECL_PENDING_INLINE_P (fndecl
) = 1;
37171 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37176 DECL_CONTEXT (fndecl
) = current_namespace
;
37180 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37182 block
= begin_omp_structured_block ();
37185 cp_parser_push_lexer_for_tokens (parser
, cp
);
37186 parser
->lexer
->in_pragma
= true;
37188 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37191 finish_function (0);
37193 DECL_CONTEXT (fndecl
) = current_function_decl
;
37195 cp_parser_pop_lexer (parser
);
37199 cp_parser_pop_lexer (parser
);
37201 finish_function (0);
37204 DECL_CONTEXT (fndecl
) = current_function_decl
;
37205 block
= finish_omp_structured_block (block
);
37206 if (TREE_CODE (block
) == BIND_EXPR
)
37207 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37208 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37209 DECL_SAVED_TREE (fndecl
) = block
;
37210 if (processing_template_decl
)
37211 add_decl_expr (fndecl
);
37213 cp_check_omp_declare_reduction (fndecl
);
37214 if (cp
== NULL
&& types
.length () > 1)
37215 cp
= cp_token_cache_new (first_token
,
37216 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37217 if (errs
!= errorcount
)
37221 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37224 /* Free any declarators allocated. */
37225 obstack_free (&declarator_obstack
, p
);
37229 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37230 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37231 initializer-clause[opt] new-line
37232 #pragma omp declare target new-line */
37235 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37236 enum pragma_context context
)
37238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37240 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37241 const char *p
= IDENTIFIER_POINTER (id
);
37243 if (strcmp (p
, "simd") == 0)
37245 cp_lexer_consume_token (parser
->lexer
);
37246 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37250 cp_ensure_no_omp_declare_simd (parser
);
37251 if (strcmp (p
, "reduction") == 0)
37253 cp_lexer_consume_token (parser
->lexer
);
37254 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37258 if (!flag_openmp
) /* flag_openmp_simd */
37260 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37263 if (strcmp (p
, "target") == 0)
37265 cp_lexer_consume_token (parser
->lexer
);
37266 cp_parser_omp_declare_target (parser
, pragma_tok
);
37270 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37272 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37276 #pragma omp taskloop taskloop-clause[optseq] new-line
37279 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37282 #define OMP_TASKLOOP_CLAUSE_MASK \
37283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37299 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37300 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37303 tree clauses
, sb
, ret
;
37305 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37307 strcat (p_name
, " taskloop");
37308 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37312 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37313 const char *p
= IDENTIFIER_POINTER (id
);
37315 if (strcmp (p
, "simd") == 0)
37317 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37318 if (cclauses
== NULL
)
37319 cclauses
= cclauses_buf
;
37321 cp_lexer_consume_token (parser
->lexer
);
37322 if (!flag_openmp
) /* flag_openmp_simd */
37323 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37325 sb
= begin_omp_structured_block ();
37326 save
= cp_parser_begin_omp_structured_block (parser
);
37327 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37329 cp_parser_end_omp_structured_block (parser
, save
);
37330 tree body
= finish_omp_structured_block (sb
);
37333 ret
= make_node (OMP_TASKLOOP
);
37334 TREE_TYPE (ret
) = void_type_node
;
37335 OMP_FOR_BODY (ret
) = body
;
37336 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37337 SET_EXPR_LOCATION (ret
, loc
);
37342 if (!flag_openmp
) /* flag_openmp_simd */
37344 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37348 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37352 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37353 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37356 sb
= begin_omp_structured_block ();
37357 save
= cp_parser_begin_omp_structured_block (parser
);
37359 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37362 cp_parser_end_omp_structured_block (parser
, save
);
37363 add_stmt (finish_omp_structured_block (sb
));
37370 # pragma acc routine oacc-routine-clause[optseq] new-line
37371 function-definition
37373 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37376 #define OACC_ROUTINE_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37383 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37384 component, which must resolve to a declared namespace-scope
37385 function. The clauses are either processed directly (for a named
37386 function), or defered until the immediatley following declaration
37390 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37391 enum pragma_context context
)
37393 gcc_checking_assert (context
== pragma_external
);
37394 /* The checking for "another pragma following this one" in the "no optional
37395 '( name )'" case makes sure that we dont re-enter. */
37396 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37398 cp_oacc_routine_data data
;
37399 data
.error_seen
= false;
37400 data
.fndecl_seen
= false;
37401 data
.tokens
= vNULL
;
37402 data
.clauses
= NULL_TREE
;
37403 data
.loc
= pragma_tok
->location
;
37404 /* It is safe to take the address of a local variable; it will only be
37405 used while this scope is live. */
37406 parser
->oacc_routine
= &data
;
37408 /* Look for optional '( name )'. */
37409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37411 cp_lexer_consume_token (parser
->lexer
); /* '(' */
37413 /* We parse the name as an id-expression. If it resolves to
37414 anything other than a non-overloaded function at namespace
37415 scope, it's an error. */
37416 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37417 tree name
= cp_parser_id_expression (parser
,
37418 /*template_keyword_p=*/false,
37419 /*check_dependency_p=*/false,
37420 /*template_p=*/NULL
,
37421 /*declarator_p=*/false,
37422 /*optional_p=*/false);
37423 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37424 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37425 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37427 if (decl
== error_mark_node
37428 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37431 parser
->oacc_routine
= NULL
;
37436 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37437 "#pragma acc routine",
37438 cp_lexer_peek_token (parser
->lexer
));
37440 if (decl
&& is_overloaded_fn (decl
)
37441 && (TREE_CODE (decl
) != FUNCTION_DECL
37442 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37444 error_at (name_loc
,
37445 "%<#pragma acc routine%> names a set of overloads");
37446 parser
->oacc_routine
= NULL
;
37450 /* Perhaps we should use the same rule as declarations in different
37452 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37454 error_at (name_loc
,
37455 "%qD does not refer to a namespace scope function", decl
);
37456 parser
->oacc_routine
= NULL
;
37460 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37462 error_at (name_loc
, "%qD does not refer to a function", decl
);
37463 parser
->oacc_routine
= NULL
;
37467 cp_finalize_oacc_routine (parser
, decl
, false);
37468 parser
->oacc_routine
= NULL
;
37470 else /* No optional '( name )'. */
37472 /* Store away all pragma tokens. */
37473 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37474 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37475 cp_lexer_consume_token (parser
->lexer
);
37476 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37477 parser
->oacc_routine
->error_seen
= true;
37478 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37479 struct cp_token_cache
*cp
37480 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37481 parser
->oacc_routine
->tokens
.safe_push (cp
);
37483 /* Emit a helpful diagnostic if there's another pragma following this
37485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37487 cp_ensure_no_oacc_routine (parser
);
37488 data
.tokens
.release ();
37489 /* ..., and then just keep going. */
37493 /* We only have to consider the pragma_external case here. */
37494 cp_parser_declaration (parser
);
37495 if (parser
->oacc_routine
37496 && !parser
->oacc_routine
->fndecl_seen
)
37497 cp_ensure_no_oacc_routine (parser
);
37499 parser
->oacc_routine
= NULL
;
37500 data
.tokens
.release ();
37504 /* Finalize #pragma acc routine clauses after direct declarator has
37508 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37510 struct cp_token_cache
*ce
;
37511 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37513 if (!data
->error_seen
&& data
->fndecl_seen
)
37515 error_at (data
->loc
,
37516 "%<#pragma acc routine%> not immediately followed by "
37517 "a single function declaration or definition");
37518 data
->error_seen
= true;
37520 if (data
->error_seen
)
37523 gcc_checking_assert (data
->tokens
.length () == 1);
37524 ce
= data
->tokens
[0];
37526 cp_parser_push_lexer_for_tokens (parser
, ce
);
37527 parser
->lexer
->in_pragma
= true;
37528 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37530 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37531 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37532 parser
->oacc_routine
->clauses
37533 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37534 "#pragma acc routine", pragma_tok
);
37535 cp_parser_pop_lexer (parser
);
37536 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37542 /* Apply any saved OpenACC routine clauses to a just-parsed
37546 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37548 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37550 /* Keep going if we're in error reporting mode. */
37551 if (parser
->oacc_routine
->error_seen
37552 || fndecl
== error_mark_node
)
37555 if (parser
->oacc_routine
->fndecl_seen
)
37557 error_at (parser
->oacc_routine
->loc
,
37558 "%<#pragma acc routine%> not immediately followed by"
37559 " a single function declaration or definition");
37560 parser
->oacc_routine
= NULL
;
37563 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37565 cp_ensure_no_oacc_routine (parser
);
37569 if (oacc_get_fn_attrib (fndecl
))
37571 error_at (parser
->oacc_routine
->loc
,
37572 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37573 parser
->oacc_routine
= NULL
;
37577 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37579 error_at (parser
->oacc_routine
->loc
,
37581 ? G_("%<#pragma acc routine%> must be applied before use")
37582 : G_("%<#pragma acc routine%> must be applied before "
37584 parser
->oacc_routine
= NULL
;
37588 /* Process the routine's dimension clauses. */
37589 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37590 oacc_replace_fn_attrib (fndecl
, dims
);
37592 /* Add an "omp declare target" attribute. */
37593 DECL_ATTRIBUTES (fndecl
)
37594 = tree_cons (get_identifier ("omp declare target"),
37595 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37597 /* Don't unset parser->oacc_routine here: we may still need it to
37598 diagnose wrong usage. But, remember that we've used this "#pragma acc
37600 parser
->oacc_routine
->fndecl_seen
= true;
37604 /* Main entry point to OpenMP statement pragmas. */
37607 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37610 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37611 omp_clause_mask
mask (0);
37613 switch (cp_parser_pragma_kind (pragma_tok
))
37615 case PRAGMA_OACC_ATOMIC
:
37616 cp_parser_omp_atomic (parser
, pragma_tok
);
37618 case PRAGMA_OACC_CACHE
:
37619 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37621 case PRAGMA_OACC_DATA
:
37622 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37624 case PRAGMA_OACC_ENTER_DATA
:
37625 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37627 case PRAGMA_OACC_EXIT_DATA
:
37628 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
37630 case PRAGMA_OACC_HOST_DATA
:
37631 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
37633 case PRAGMA_OACC_KERNELS
:
37634 case PRAGMA_OACC_PARALLEL
:
37635 strcpy (p_name
, "#pragma acc");
37636 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
37639 case PRAGMA_OACC_LOOP
:
37640 strcpy (p_name
, "#pragma acc");
37641 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37644 case PRAGMA_OACC_UPDATE
:
37645 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
37647 case PRAGMA_OACC_WAIT
:
37648 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
37650 case PRAGMA_OMP_ATOMIC
:
37651 cp_parser_omp_atomic (parser
, pragma_tok
);
37653 case PRAGMA_OMP_CRITICAL
:
37654 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
37656 case PRAGMA_OMP_DISTRIBUTE
:
37657 strcpy (p_name
, "#pragma omp");
37658 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
37661 case PRAGMA_OMP_FOR
:
37662 strcpy (p_name
, "#pragma omp");
37663 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
37666 case PRAGMA_OMP_MASTER
:
37667 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
37669 case PRAGMA_OMP_PARALLEL
:
37670 strcpy (p_name
, "#pragma omp");
37671 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
37674 case PRAGMA_OMP_SECTIONS
:
37675 strcpy (p_name
, "#pragma omp");
37676 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
37678 case PRAGMA_OMP_SIMD
:
37679 strcpy (p_name
, "#pragma omp");
37680 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
37683 case PRAGMA_OMP_SINGLE
:
37684 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
37686 case PRAGMA_OMP_TASK
:
37687 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
37689 case PRAGMA_OMP_TASKGROUP
:
37690 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
37692 case PRAGMA_OMP_TASKLOOP
:
37693 strcpy (p_name
, "#pragma omp");
37694 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37697 case PRAGMA_OMP_TEAMS
:
37698 strcpy (p_name
, "#pragma omp");
37699 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
37703 gcc_unreachable ();
37706 protected_set_expr_location (stmt
, pragma_tok
->location
);
37709 /* Transactional Memory parsing routines. */
37711 /* Parse a transaction attribute.
37717 We use this instead of cp_parser_attributes_opt for transactions to avoid
37718 the pedwarn in C++98 mode. */
37721 cp_parser_txn_attribute_opt (cp_parser
*parser
)
37724 tree attr_name
, attr
= NULL
;
37726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
37727 return cp_parser_attributes_opt (parser
);
37729 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
37731 cp_lexer_consume_token (parser
->lexer
);
37732 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
37735 token
= cp_lexer_peek_token (parser
->lexer
);
37736 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
37738 token
= cp_lexer_consume_token (parser
->lexer
);
37740 attr_name
= (token
->type
== CPP_KEYWORD
37741 /* For keywords, use the canonical spelling,
37742 not the parsed identifier. */
37743 ? ridpointers
[(int) token
->keyword
]
37745 attr
= build_tree_list (attr_name
, NULL_TREE
);
37748 cp_parser_error (parser
, "expected identifier");
37750 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37752 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37756 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37758 transaction-statement:
37759 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37761 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37765 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
37767 unsigned char old_in
= parser
->in_transaction
;
37768 unsigned char this_in
= 1, new_in
;
37769 enum rid keyword
= token
->keyword
;
37770 tree stmt
, attrs
, noex
;
37772 cp_lexer_consume_token (parser
->lexer
);
37774 if (keyword
== RID_TRANSACTION_RELAXED
37775 || keyword
== RID_SYNCHRONIZED
)
37776 this_in
|= TM_STMT_ATTR_RELAXED
;
37779 attrs
= cp_parser_txn_attribute_opt (parser
);
37781 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37784 /* Parse a noexcept specification. */
37785 if (keyword
== RID_ATOMIC_NOEXCEPT
)
37786 noex
= boolean_true_node
;
37787 else if (keyword
== RID_ATOMIC_CANCEL
)
37789 /* cancel-and-throw is unimplemented. */
37790 sorry ("atomic_cancel");
37794 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
37796 /* Keep track if we're in the lexical scope of an outer transaction. */
37797 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
37799 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
37801 parser
->in_transaction
= new_in
;
37802 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
37803 parser
->in_transaction
= old_in
;
37805 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
37810 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37812 transaction-expression:
37813 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37814 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37818 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
37820 unsigned char old_in
= parser
->in_transaction
;
37821 unsigned char this_in
= 1;
37825 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37827 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37828 || keyword
== RID_TRANSACTION_RELAXED
);
37832 keyword
== RID_TRANSACTION_RELAXED
37833 ? G_("%<__transaction_relaxed%> without transactional memory "
37835 : G_("%<__transaction_atomic%> without transactional memory "
37836 "support enabled"));
37838 token
= cp_parser_require_keyword (parser
, keyword
,
37839 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37840 : RT_TRANSACTION_RELAXED
));
37841 gcc_assert (token
!= NULL
);
37843 if (keyword
== RID_TRANSACTION_RELAXED
)
37844 this_in
|= TM_STMT_ATTR_RELAXED
;
37846 /* Set this early. This might mean that we allow transaction_cancel in
37847 an expression that we find out later actually has to be a constexpr.
37848 However, we expect that cxx_constant_value will be able to deal with
37849 this; also, if the noexcept has no constexpr, then what we parse next
37850 really is a transaction's body. */
37851 parser
->in_transaction
= this_in
;
37853 /* Parse a noexcept specification. */
37854 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
37857 if (!noex
|| !noex_expr
37858 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37860 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
37862 expr
= cp_parser_expression (parser
);
37863 expr
= finish_parenthesized_expr (expr
);
37865 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
37869 /* The only expression that is available got parsed for the noexcept
37870 already. noexcept is true then. */
37872 noex
= boolean_true_node
;
37875 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
37876 parser
->in_transaction
= old_in
;
37878 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
37879 return error_mark_node
;
37881 return (flag_tm
? expr
: error_mark_node
);
37884 /* Parse a function-transaction-block.
37886 function-transaction-block:
37887 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37889 __transaction_atomic txn-attribute[opt] function-try-block
37890 __transaction_relaxed ctor-initializer[opt] function-body
37891 __transaction_relaxed function-try-block
37895 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
37897 unsigned char old_in
= parser
->in_transaction
;
37898 unsigned char new_in
= 1;
37899 tree compound_stmt
, stmt
, attrs
;
37900 bool ctor_initializer_p
;
37903 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37904 || keyword
== RID_TRANSACTION_RELAXED
);
37905 token
= cp_parser_require_keyword (parser
, keyword
,
37906 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37907 : RT_TRANSACTION_RELAXED
));
37908 gcc_assert (token
!= NULL
);
37910 if (keyword
== RID_TRANSACTION_RELAXED
)
37911 new_in
|= TM_STMT_ATTR_RELAXED
;
37914 attrs
= cp_parser_txn_attribute_opt (parser
);
37916 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37919 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
37921 parser
->in_transaction
= new_in
;
37923 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
37924 ctor_initializer_p
= cp_parser_function_try_block (parser
);
37926 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
37927 (parser
, /*in_function_try_block=*/false);
37929 parser
->in_transaction
= old_in
;
37931 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
37933 return ctor_initializer_p
;
37936 /* Parse a __transaction_cancel statement.
37939 __transaction_cancel txn-attribute[opt] ;
37940 __transaction_cancel txn-attribute[opt] throw-expression ;
37942 ??? Cancel and throw is not yet implemented. */
37945 cp_parser_transaction_cancel (cp_parser
*parser
)
37948 bool is_outer
= false;
37951 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
37952 RT_TRANSACTION_CANCEL
);
37953 gcc_assert (token
!= NULL
);
37955 attrs
= cp_parser_txn_attribute_opt (parser
);
37957 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
37959 /* ??? Parse cancel-and-throw here. */
37961 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37965 error_at (token
->location
, "%<__transaction_cancel%> without "
37966 "transactional memory support enabled");
37967 return error_mark_node
;
37969 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
37971 error_at (token
->location
, "%<__transaction_cancel%> within a "
37972 "%<__transaction_relaxed%>");
37973 return error_mark_node
;
37977 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
37978 && !is_tm_may_cancel_outer (current_function_decl
))
37980 error_at (token
->location
, "outer %<__transaction_cancel%> not "
37981 "within outer %<__transaction_atomic%>");
37982 error_at (token
->location
,
37983 " or a %<transaction_may_cancel_outer%> function");
37984 return error_mark_node
;
37987 else if (parser
->in_transaction
== 0)
37989 error_at (token
->location
, "%<__transaction_cancel%> not within "
37990 "%<__transaction_atomic%>");
37991 return error_mark_node
;
37994 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38002 static GTY (()) cp_parser
*the_parser
;
38005 /* Special handling for the first token or line in the file. The first
38006 thing in the file might be #pragma GCC pch_preprocess, which loads a
38007 PCH file, which is a GC collection point. So we need to handle this
38008 first pragma without benefit of an existing lexer structure.
38010 Always returns one token to the caller in *FIRST_TOKEN. This is
38011 either the true first token of the file, or the first token after
38012 the initial pragma. */
38015 cp_parser_initial_pragma (cp_token
*first_token
)
38019 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38020 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38023 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38024 if (first_token
->type
== CPP_STRING
)
38026 name
= first_token
->u
.value
;
38028 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38029 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38030 error_at (first_token
->location
,
38031 "junk at end of %<#pragma GCC pch_preprocess%>");
38034 error_at (first_token
->location
, "expected string literal");
38036 /* Skip to the end of the pragma. */
38037 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38038 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38040 /* Now actually load the PCH file. */
38042 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38044 /* Read one more token to return to our caller. We have to do this
38045 after reading the PCH file in, since its pointers have to be
38047 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38050 /* Parses the grainsize pragma for the _Cilk_for statement.
38052 #pragma cilk grainsize = <VALUE>. */
38055 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38057 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38059 tree exp
= cp_parser_binary_expression (parser
, false, false,
38060 PREC_NOT_OPERATOR
, NULL
);
38061 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38062 if (!exp
|| exp
== error_mark_node
)
38064 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
38068 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38069 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
38070 cp_parser_cilk_for (parser
, exp
, if_p
);
38072 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
38073 "%<#pragma cilk grainsize%> is not followed by "
38077 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38080 /* Normal parsing of a pragma token. Here we can (and must) use the
38084 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38086 cp_token
*pragma_tok
;
38091 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38092 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38093 parser
->lexer
->in_pragma
= true;
38095 id
= cp_parser_pragma_kind (pragma_tok
);
38096 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38097 cp_ensure_no_omp_declare_simd (parser
);
38100 case PRAGMA_GCC_PCH_PREPROCESS
:
38101 error_at (pragma_tok
->location
,
38102 "%<#pragma GCC pch_preprocess%> must be first");
38105 case PRAGMA_OMP_BARRIER
:
38108 case pragma_compound
:
38109 cp_parser_omp_barrier (parser
, pragma_tok
);
38112 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38113 "used in compound statements", "omp barrier");
38120 case PRAGMA_OMP_FLUSH
:
38123 case pragma_compound
:
38124 cp_parser_omp_flush (parser
, pragma_tok
);
38127 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38128 "used in compound statements", "omp flush");
38135 case PRAGMA_OMP_TASKWAIT
:
38138 case pragma_compound
:
38139 cp_parser_omp_taskwait (parser
, pragma_tok
);
38142 error_at (pragma_tok
->location
,
38143 "%<#pragma %s%> may only be used in compound statements",
38151 case PRAGMA_OMP_TASKYIELD
:
38154 case pragma_compound
:
38155 cp_parser_omp_taskyield (parser
, pragma_tok
);
38158 error_at (pragma_tok
->location
,
38159 "%<#pragma %s%> may only be used in compound statements",
38167 case PRAGMA_OMP_CANCEL
:
38170 case pragma_compound
:
38171 cp_parser_omp_cancel (parser
, pragma_tok
);
38174 error_at (pragma_tok
->location
,
38175 "%<#pragma %s%> may only be used in compound statements",
38183 case PRAGMA_OMP_CANCELLATION_POINT
:
38184 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38187 case PRAGMA_OMP_THREADPRIVATE
:
38188 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38191 case PRAGMA_OMP_DECLARE
:
38192 cp_parser_omp_declare (parser
, pragma_tok
, context
);
38195 case PRAGMA_OACC_DECLARE
:
38196 cp_parser_oacc_declare (parser
, pragma_tok
);
38199 case PRAGMA_OACC_ENTER_DATA
:
38200 if (context
== pragma_stmt
)
38202 error_at (pragma_tok
->location
,
38203 "%<#pragma %s%> may only be used in compound statements",
38207 else if (context
!= pragma_compound
)
38209 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38212 case PRAGMA_OACC_EXIT_DATA
:
38213 if (context
== pragma_stmt
)
38215 error_at (pragma_tok
->location
,
38216 "%<#pragma %s%> may only be used in compound statements",
38220 else if (context
!= pragma_compound
)
38222 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38225 case PRAGMA_OACC_ROUTINE
:
38226 if (context
!= pragma_external
)
38228 error_at (pragma_tok
->location
,
38229 "%<#pragma acc routine%> must be at file scope");
38232 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38235 case PRAGMA_OACC_UPDATE
:
38236 if (context
== pragma_stmt
)
38238 error_at (pragma_tok
->location
,
38239 "%<#pragma %s%> may only be used in compound statements",
38243 else if (context
!= pragma_compound
)
38245 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38248 case PRAGMA_OACC_WAIT
:
38249 if (context
== pragma_stmt
)
38251 error_at (pragma_tok
->location
,
38252 "%<#pragma %s%> may only be used in compound statements",
38256 else if (context
!= pragma_compound
)
38258 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38261 case PRAGMA_OACC_ATOMIC
:
38262 case PRAGMA_OACC_CACHE
:
38263 case PRAGMA_OACC_DATA
:
38264 case PRAGMA_OACC_HOST_DATA
:
38265 case PRAGMA_OACC_KERNELS
:
38266 case PRAGMA_OACC_PARALLEL
:
38267 case PRAGMA_OACC_LOOP
:
38268 case PRAGMA_OMP_ATOMIC
:
38269 case PRAGMA_OMP_CRITICAL
:
38270 case PRAGMA_OMP_DISTRIBUTE
:
38271 case PRAGMA_OMP_FOR
:
38272 case PRAGMA_OMP_MASTER
:
38273 case PRAGMA_OMP_PARALLEL
:
38274 case PRAGMA_OMP_SECTIONS
:
38275 case PRAGMA_OMP_SIMD
:
38276 case PRAGMA_OMP_SINGLE
:
38277 case PRAGMA_OMP_TASK
:
38278 case PRAGMA_OMP_TASKGROUP
:
38279 case PRAGMA_OMP_TASKLOOP
:
38280 case PRAGMA_OMP_TEAMS
:
38281 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38283 stmt
= push_omp_privatization_clauses (false);
38284 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38285 pop_omp_privatization_clauses (stmt
);
38288 case PRAGMA_OMP_ORDERED
:
38289 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38291 stmt
= push_omp_privatization_clauses (false);
38292 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38293 pop_omp_privatization_clauses (stmt
);
38296 case PRAGMA_OMP_TARGET
:
38297 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38299 stmt
= push_omp_privatization_clauses (false);
38300 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38301 pop_omp_privatization_clauses (stmt
);
38304 case PRAGMA_OMP_END_DECLARE_TARGET
:
38305 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38308 case PRAGMA_OMP_SECTION
:
38309 error_at (pragma_tok
->location
,
38310 "%<#pragma omp section%> may only be used in "
38311 "%<#pragma omp sections%> construct");
38316 if (context
== pragma_external
)
38318 error_at (pragma_tok
->location
,
38319 "%<#pragma GCC ivdep%> must be inside a function");
38322 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38324 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38325 if (tok
->type
!= CPP_KEYWORD
38326 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38327 && tok
->keyword
!= RID_DO
))
38329 cp_parser_error (parser
, "for, while or do statement expected");
38332 cp_parser_iteration_statement (parser
, if_p
, true);
38336 case PRAGMA_CILK_SIMD
:
38337 if (context
== pragma_external
)
38339 error_at (pragma_tok
->location
,
38340 "%<#pragma simd%> must be inside a function");
38343 stmt
= push_omp_privatization_clauses (false);
38344 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
38345 pop_omp_privatization_clauses (stmt
);
38348 case PRAGMA_CILK_GRAINSIZE
:
38349 if (context
== pragma_external
)
38351 error_at (pragma_tok
->location
,
38352 "%<#pragma cilk grainsize%> must be inside a function");
38356 /* Ignore the pragma if Cilk Plus is not enabled. */
38359 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
38364 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
38365 "%<#pragma cilk grainsize%>");
38370 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38371 c_invoke_pragma_handler (id
);
38375 cp_parser_error (parser
, "expected declaration specifiers");
38379 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38383 /* The interface the pragma parsers have to the lexer. */
38386 pragma_lex (tree
*value
, location_t
*loc
)
38388 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38389 enum cpp_ttype ret
= tok
->type
;
38391 *value
= tok
->u
.value
;
38393 *loc
= tok
->location
;
38395 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38397 else if (ret
== CPP_STRING
)
38398 *value
= cp_parser_string_literal (the_parser
, false, false);
38401 if (ret
== CPP_KEYWORD
)
38403 cp_lexer_consume_token (the_parser
->lexer
);
38410 /* External interface. */
38412 /* Parse one entire translation unit. */
38415 c_parse_file (void)
38417 static bool already_called
= false;
38419 if (already_called
)
38420 fatal_error (input_location
,
38421 "inter-module optimizations not implemented for C++");
38422 already_called
= true;
38424 the_parser
= cp_parser_new ();
38425 push_deferring_access_checks (flag_access_control
38426 ? dk_no_deferred
: dk_no_check
);
38427 cp_parser_translation_unit (the_parser
);
38431 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38432 vectorlength clause:
38434 vectorlength ( constant-expression ) */
38437 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
38440 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38442 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38443 safelen clause. Thus, vectorlength is represented as OMP 4.0
38444 safelen. For SIMD-enabled function it is represented by OMP 4.0
38447 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
38450 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
38453 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38454 return error_mark_node
;
38456 expr
= cp_parser_constant_expression (parser
);
38457 expr
= maybe_constant_value (expr
);
38459 /* If expr == error_mark_node, then don't emit any errors nor
38460 create a clause. if any of the above functions returns
38461 error mark node then they would have emitted an error message. */
38462 if (expr
== error_mark_node
)
38464 else if (!TREE_TYPE (expr
)
38465 || !TREE_CONSTANT (expr
)
38466 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
38467 error_at (loc
, "vectorlength must be an integer constant");
38468 else if (TREE_CONSTANT (expr
)
38469 && !pow2p_hwi (TREE_INT_CST_LOW (expr
)))
38470 error_at (loc
, "vectorlength must be a power of 2");
38476 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
38477 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
38478 OMP_CLAUSE_CHAIN (c
) = clauses
;
38483 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
38484 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
38485 OMP_CLAUSE_CHAIN (c
) = clauses
;
38490 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38491 return error_mark_node
;
38495 /* Handles the Cilk Plus #pragma simd linear clause.
38497 linear ( simd-linear-variable-list )
38499 simd-linear-variable-list:
38500 simd-linear-variable
38501 simd-linear-variable-list , simd-linear-variable
38503 simd-linear-variable:
38505 id-expression : simd-linear-step
38508 conditional-expression */
38511 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
38513 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38515 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38517 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38519 cp_parser_error (parser
, "expected identifier");
38520 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38521 return error_mark_node
;
38524 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38525 parser
->colon_corrects_to_scope_p
= false;
38528 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38529 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38531 cp_parser_error (parser
, "expected variable-name");
38532 clauses
= error_mark_node
;
38536 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
38538 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
38540 if (decl
== error_mark_node
)
38542 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
38544 clauses
= error_mark_node
;
38548 tree e
= NULL_TREE
;
38549 tree step_size
= integer_one_node
;
38551 /* If present, parse the linear step. Otherwise, assume the default
38553 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
38555 cp_lexer_consume_token (parser
->lexer
);
38557 e
= cp_parser_assignment_expression (parser
);
38558 e
= maybe_constant_value (e
);
38560 if (e
== error_mark_node
)
38562 /* If an error has occurred, then the whole pragma is
38563 considered ill-formed. Thus, no reason to keep
38565 clauses
= error_mark_node
;
38568 else if (type_dependent_expression_p (e
)
38569 || value_dependent_expression_p (e
)
38571 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
38572 && (TREE_CONSTANT (e
)
38576 cp_parser_error (parser
,
38577 "step size must be an integer constant "
38578 "expression or an integer variable");
38581 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38582 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
38583 OMP_CLAUSE_DECL (l
) = decl
;
38584 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
38585 OMP_CLAUSE_CHAIN (l
) = clauses
;
38588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38589 cp_lexer_consume_token (parser
->lexer
);
38590 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38594 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38595 "expected %<,%> or %<)%> after %qE", decl
);
38596 clauses
= error_mark_node
;
38600 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38601 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38605 /* Returns the name of the next clause. If the clause is not
38606 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38607 token is not consumed. Otherwise, the appropriate enum from the
38608 pragma_simd_clause is returned and the token is consumed. */
38610 static pragma_omp_clause
38611 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
38613 pragma_omp_clause clause_type
;
38614 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38616 if (token
->keyword
== RID_PRIVATE
)
38617 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
38618 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
38619 return PRAGMA_CILK_CLAUSE_NONE
;
38620 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
38621 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
38622 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
38623 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
38624 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
38625 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
38626 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
38627 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
38628 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
38629 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
38631 return PRAGMA_CILK_CLAUSE_NONE
;
38633 cp_lexer_consume_token (parser
->lexer
);
38634 return clause_type
;
38637 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38640 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
38642 tree clauses
= NULL_TREE
;
38644 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38645 && clauses
!= error_mark_node
)
38647 pragma_omp_clause c_kind
;
38648 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
38649 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
38650 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
38651 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
38652 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
38653 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
38654 /* Use the OpenMP 4.0 equivalent function. */
38655 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
38656 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
38657 /* Use the OpenMP 4.0 equivalent function. */
38658 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
38660 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
38661 /* Use the OMP 4.0 equivalent function. */
38662 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
38664 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
38665 /* Use the OMP 4.0 equivalent function. */
38666 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
38669 clauses
= error_mark_node
;
38670 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
38675 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
38677 if (clauses
== error_mark_node
)
38678 return error_mark_node
;
38680 return finish_omp_clauses (clauses
, C_ORT_CILK
);
38683 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38686 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
38688 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
38690 if (clauses
== error_mark_node
)
38693 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
38695 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38696 "for statement expected");
38700 tree sb
= begin_omp_structured_block ();
38701 int save
= cp_parser_begin_omp_structured_block (parser
);
38702 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
38704 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
38705 cp_parser_end_omp_structured_block (parser
, save
);
38706 add_stmt (finish_omp_structured_block (sb
));
38709 /* Main entry-point for parsing Cilk Plus _Cilk_for
38710 loops. The return value is error_mark_node
38711 when errors happen and CILK_FOR tree on success. */
38714 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
38716 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
38717 gcc_unreachable ();
38719 tree sb
= begin_omp_structured_block ();
38720 int save
= cp_parser_begin_omp_structured_block (parser
);
38722 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
38723 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
38724 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
38725 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
38727 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
38729 cpp_validate_cilk_plus_loop (ret
);
38731 ret
= error_mark_node
;
38733 cp_parser_end_omp_structured_block (parser
, save
);
38734 add_stmt (finish_omp_structured_block (sb
));
38738 /* Create an identifier for a generic parameter type (a synthesized
38739 template parameter implied by `auto' or a concept identifier). */
38741 static GTY(()) int generic_parm_count
;
38743 make_generic_type_name ()
38746 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38747 return get_identifier (buf
);
38750 /* Predicate that behaves as is_auto_or_concept but matches the parent
38751 node of the generic type rather than the generic type itself. This
38752 allows for type transformation in add_implicit_template_parms. */
38755 tree_type_is_auto_or_concept (const_tree t
)
38757 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
38760 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38761 (creating a new template parameter list if necessary). Returns the newly
38762 created template type parm. */
38765 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38767 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38769 /* Before committing to modifying any scope, if we're in an
38770 implicit template scope, and we're trying to synthesize a
38771 constrained parameter, try to find a previous parameter with
38772 the same name. This is the same-type rule for abbreviated
38773 function templates.
38775 NOTE: We can generate implicit parameters when tentatively
38776 parsing a nested name specifier, only to reject that parse
38777 later. However, matching the same template-id as part of a
38778 direct-declarator should generate an identical template
38779 parameter, so this rule will merge them. */
38780 if (parser
->implicit_template_scope
&& constr
)
38782 tree t
= parser
->implicit_template_parms
;
38785 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38787 tree d
= TREE_VALUE (t
);
38788 if (TREE_CODE (d
) == PARM_DECL
)
38789 /* Return the TEMPLATE_PARM_INDEX. */
38790 d
= DECL_INITIAL (d
);
38793 t
= TREE_CHAIN (t
);
38797 /* We are either continuing a function template that already contains implicit
38798 template parameters, creating a new fully-implicit function template, or
38799 extending an existing explicit function template with implicit template
38802 cp_binding_level
*const entry_scope
= current_binding_level
;
38804 bool become_template
= false;
38805 cp_binding_level
*parent_scope
= 0;
38807 if (parser
->implicit_template_scope
)
38809 gcc_assert (parser
->implicit_template_parms
);
38811 current_binding_level
= parser
->implicit_template_scope
;
38815 /* Roll back to the existing template parameter scope (in the case of
38816 extending an explicit function template) or introduce a new template
38817 parameter scope ahead of the function parameter scope (or class scope
38818 in the case of out-of-line member definitions). The function scope is
38819 added back after template parameter synthesis below. */
38821 cp_binding_level
*scope
= entry_scope
;
38823 while (scope
->kind
== sk_function_parms
)
38825 parent_scope
= scope
;
38826 scope
= scope
->level_chain
;
38828 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38830 /* If not defining a class, then any class scope is a scope level in
38831 an out-of-line member definition. In this case simply wind back
38832 beyond the first such scope to inject the template parameter list.
38833 Otherwise wind back to the class being defined. The latter can
38834 occur in class member friend declarations such as:
38840 friend void A::foo (auto);
38843 The template parameter list synthesized for the friend declaration
38844 must be injected in the scope of 'B'. This can also occur in
38845 erroneous cases such as:
38851 void B::foo (auto) {}
38854 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38855 but, nevertheless, the template parameter list synthesized for the
38856 declarator should be injected into the scope of 'A' as if the
38857 ill-formed template was specified explicitly. */
38859 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38861 parent_scope
= scope
;
38862 scope
= scope
->level_chain
;
38866 current_binding_level
= scope
;
38868 if (scope
->kind
!= sk_template_parms
38869 || !function_being_declared_is_template_p (parser
))
38871 /* Introduce a new template parameter list for implicit template
38874 become_template
= true;
38876 parser
->implicit_template_scope
38877 = begin_scope (sk_template_parms
, NULL
);
38879 ++processing_template_decl
;
38881 parser
->fully_implicit_function_template_p
= true;
38882 ++parser
->num_template_parameter_lists
;
38886 /* Synthesize implicit template parameters at the end of the explicit
38887 template parameter list. */
38889 gcc_assert (current_template_parms
);
38891 parser
->implicit_template_scope
= scope
;
38893 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38894 parser
->implicit_template_parms
38895 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38899 /* Synthesize a new template parameter and track the current template
38900 parameter chain with implicit_template_parms. */
38902 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38903 tree synth_id
= make_generic_type_name ();
38904 tree synth_tmpl_parm
;
38905 bool non_type
= false;
38907 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38909 = finish_template_type_parm (class_type_node
, synth_id
);
38910 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38912 = finish_constrained_template_template_parm (proto
, synth_id
);
38915 synth_tmpl_parm
= copy_decl (proto
);
38916 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38920 // Attach the constraint to the parm before processing.
38921 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38922 TREE_TYPE (node
) = constr
;
38924 = process_template_parm (parser
->implicit_template_parms
,
38927 /*non_type=*/non_type
,
38928 /*param_pack=*/false);
38930 // Chain the new parameter to the list of implicit parameters.
38931 if (parser
->implicit_template_parms
)
38932 parser
->implicit_template_parms
38933 = TREE_CHAIN (parser
->implicit_template_parms
);
38935 parser
->implicit_template_parms
= new_parm
;
38937 tree new_decl
= getdecls ();
38939 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38940 new_decl
= DECL_INITIAL (new_decl
);
38942 /* If creating a fully implicit function template, start the new implicit
38943 template parameter list with this synthesized type, otherwise grow the
38944 current template parameter list. */
38946 if (become_template
)
38948 parent_scope
->level_chain
= current_binding_level
;
38950 tree new_parms
= make_tree_vec (1);
38951 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
38952 current_template_parms
= tree_cons (size_int (processing_template_decl
),
38953 new_parms
, current_template_parms
);
38957 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38958 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
38959 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
38960 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
38963 // If the new parameter was constrained, we need to add that to the
38964 // constraints in the template parameter list.
38965 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
38967 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
38968 reqs
= conjoin_constraints (reqs
, req
);
38969 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
38972 current_binding_level
= entry_scope
;
38977 /* Finish the declaration of a fully implicit function template. Such a
38978 template has no explicit template parameter list so has not been through the
38979 normal template head and tail processing. synthesize_implicit_template_parm
38980 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38981 provided if the declaration is a class member such that its template
38982 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38983 form is returned. Otherwise NULL_TREE is returned. */
38986 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
38988 gcc_assert (parser
->fully_implicit_function_template_p
);
38990 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
38991 && DECL_VIRTUAL_P (member_decl_opt
))
38993 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
38994 "implicit templates may not be %<virtual%>");
38995 DECL_VIRTUAL_P (member_decl_opt
) = false;
38998 if (member_decl_opt
)
38999 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39000 end_template_decl ();
39002 parser
->fully_implicit_function_template_p
= false;
39003 --parser
->num_template_parameter_lists
;
39005 return member_decl_opt
;
39008 #include "gt-cp-parser.h"