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 tree cp_literal_operator_id
255 static void cp_parser_cilk_simd
256 (cp_parser
*, cp_token
*, bool *);
257 static tree cp_parser_cilk_for
258 (cp_parser
*, tree
, bool *);
259 static bool cp_parser_omp_declare_reduction_exprs
261 static tree cp_parser_cilk_simd_vectorlength
262 (cp_parser
*, tree
, bool);
263 static void cp_finalize_oacc_routine
264 (cp_parser
*, tree
, bool);
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream
;
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand
;
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
286 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
287 cp_token
*start_token
, unsigned num
,
288 cp_token
*curr_token
)
290 unsigned i
, nprinted
;
294 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
300 num
= buffer
->length ();
302 if (start_token
== NULL
)
303 start_token
= buffer
->address ();
305 if (start_token
> buffer
->address ())
307 cp_lexer_print_token (file
, &(*buffer
)[0]);
308 fprintf (file
, " ... ");
313 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
315 if (token
== start_token
)
322 if (token
== curr_token
)
323 fprintf (file
, "[[");
325 cp_lexer_print_token (file
, token
);
327 if (token
== curr_token
)
328 fprintf (file
, "]]");
334 case CPP_CLOSE_BRACE
:
344 if (i
== num
&& i
< buffer
->length ())
346 fprintf (file
, " ... ");
347 cp_lexer_print_token (file
, &buffer
->last ());
350 fprintf (file
, "\n");
354 /* Dump all tokens in BUFFER to stderr. */
357 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
359 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
363 debug (vec
<cp_token
, va_gc
> &ref
)
365 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
369 debug (vec
<cp_token
, va_gc
> *ptr
)
374 fprintf (stderr
, "<nil>\n");
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
382 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
386 fprintf (file
, "%s: ", desc
);
387 print_node_brief (file
, "", t
, 0);
392 /* Dump parser context C to FILE. */
395 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
397 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
399 print_node_brief (file
, "", c
->object_type
, 0);
400 fprintf (file
, "}\n");
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
407 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
410 cp_parser_context
*c
;
412 fprintf (file
, "Parsing context stack:\n");
413 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
415 fprintf (file
, "\t#%u: ", i
);
416 cp_debug_print_context (file
, c
);
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
424 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
427 fprintf (file
, "%s: true\n", desc
);
431 /* Print an unparsed function entry UF to FILE. */
434 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
437 cp_default_arg_entry
*default_arg_fn
;
440 fprintf (file
, "\tFunctions with default args:\n");
442 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
445 fprintf (file
, "\t\tClass type: ");
446 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
447 fprintf (file
, "\t\tDeclaration: ");
448 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
449 fprintf (file
, "\n");
452 fprintf (file
, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
456 print_node_brief (file
, "", fn
, 0);
459 fprintf (file
, "\n");
461 fprintf (file
, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
465 print_node_brief (file
, "", fn
, 0);
468 fprintf (file
, "\n");
472 /* Print the stack of unparsed member functions S to FILE. */
475 cp_debug_print_unparsed_queues (FILE *file
,
476 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
479 cp_unparsed_functions_entry
*uf
;
481 fprintf (file
, "Unparsed functions\n");
482 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
484 fprintf (file
, "#%u:\n", i
);
485 cp_debug_print_unparsed_function (file
, uf
);
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
494 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
496 cp_token
*next_token
, *first_token
, *start_token
;
501 next_token
= parser
->lexer
->next_token
;
502 first_token
= parser
->lexer
->buffer
->address ();
503 start_token
= (next_token
> first_token
+ window_size
/ 2)
504 ? next_token
- window_size
/ 2
506 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
515 cp_debug_parser (FILE *file
, cp_parser
*parser
)
517 const size_t window_size
= 20;
519 expanded_location eloc
;
524 fprintf (file
, "Parser state\n\n");
525 fprintf (file
, "Number of tokens: %u\n",
526 vec_safe_length (parser
->lexer
->buffer
));
527 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
528 cp_debug_print_tree_if_set (file
, "Object scope",
529 parser
->object_scope
);
530 cp_debug_print_tree_if_set (file
, "Qualifying scope",
531 parser
->qualifying_scope
);
532 cp_debug_print_context_stack (file
, parser
->context
);
533 cp_debug_print_flag (file
, "Allow GNU extensions",
534 parser
->allow_gnu_extensions_p
);
535 cp_debug_print_flag (file
, "'>' token is greater-than",
536 parser
->greater_than_is_operator_p
);
537 cp_debug_print_flag (file
, "Default args allowed in current "
538 "parameter list", parser
->default_arg_ok_p
);
539 cp_debug_print_flag (file
, "Parsing integral constant-expression",
540 parser
->integral_constant_expression_p
);
541 cp_debug_print_flag (file
, "Allow non-constant expression in current "
542 "constant-expression",
543 parser
->allow_non_integral_constant_expression_p
);
544 cp_debug_print_flag (file
, "Seen non-constant expression",
545 parser
->non_integral_constant_expression_p
);
546 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
548 parser
->local_variables_forbidden_p
);
549 cp_debug_print_flag (file
, "In unbraced linkage specification",
550 parser
->in_unbraced_linkage_specification_p
);
551 cp_debug_print_flag (file
, "Parsing a declarator",
552 parser
->in_declarator_p
);
553 cp_debug_print_flag (file
, "In template argument list",
554 parser
->in_template_argument_list_p
);
555 cp_debug_print_flag (file
, "Parsing an iteration statement",
556 parser
->in_statement
& IN_ITERATION_STMT
);
557 cp_debug_print_flag (file
, "Parsing a switch statement",
558 parser
->in_statement
& IN_SWITCH_STMT
);
559 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
560 parser
->in_statement
& IN_OMP_BLOCK
);
561 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
562 parser
->in_statement
& IN_CILK_SIMD_FOR
);
563 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
564 parser
->in_statement
& IN_OMP_FOR
);
565 cp_debug_print_flag (file
, "Parsing an if statement",
566 parser
->in_statement
& IN_IF_STMT
);
567 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
568 "context", parser
->in_type_id_in_expr_p
);
569 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
570 parser
->implicit_extern_c
);
571 cp_debug_print_flag (file
, "String expressions should be translated "
572 "to execution character set",
573 parser
->translate_strings_p
);
574 cp_debug_print_flag (file
, "Parsing function body outside of a "
575 "local class", parser
->in_function_body
);
576 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
577 parser
->colon_corrects_to_scope_p
);
578 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
579 parser
->colon_doesnt_start_class_def_p
);
580 if (parser
->type_definition_forbidden_message
)
581 fprintf (file
, "Error message for forbidden type definitions: %s\n",
582 parser
->type_definition_forbidden_message
);
583 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
584 fprintf (file
, "Number of class definitions in progress: %u\n",
585 parser
->num_classes_being_defined
);
586 fprintf (file
, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser
->num_template_parameter_lists
);
588 cp_debug_parser_tokens (file
, parser
, window_size
);
589 token
= parser
->lexer
->next_token
;
590 fprintf (file
, "Next token to parse:\n");
591 fprintf (file
, "\tToken: ");
592 cp_lexer_print_token (file
, token
);
593 eloc
= expand_location (token
->location
);
594 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
595 fprintf (file
, "\tLine: %d\n", eloc
.line
);
596 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
600 debug (cp_parser
&ref
)
602 cp_debug_parser (stderr
, &ref
);
606 debug (cp_parser
*ptr
)
611 fprintf (stderr
, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
617 cp_lexer_alloc (void)
621 c_common_no_more_pch ();
623 /* Allocate the memory. */
624 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
626 /* Initially we are not debugging. */
627 lexer
->debugging_p
= false;
629 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
631 /* Create the buffer. */
632 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
642 cp_lexer_new_main (void)
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token
);
652 lexer
= cp_lexer_alloc ();
654 /* Put the first token in the buffer. */
655 lexer
->buffer
->quick_push (token
);
657 /* Get the remaining tokens from the preprocessor. */
658 while (token
.type
!= CPP_EOF
)
660 cp_lexer_get_preprocessor_token (lexer
, &token
);
661 vec_safe_push (lexer
->buffer
, token
);
664 lexer
->last_token
= lexer
->buffer
->address ()
665 + lexer
->buffer
->length ()
667 lexer
->next_token
= lexer
->buffer
->length ()
668 ? lexer
->buffer
->address ()
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
675 gcc_assert (!lexer
->next_token
->purged_p
);
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
683 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
685 cp_token
*first
= cache
->first
;
686 cp_token
*last
= cache
->last
;
687 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
689 /* We do not own the buffer. */
690 lexer
->buffer
= NULL
;
691 lexer
->next_token
= first
== last
? &eof_token
: first
;
692 lexer
->last_token
= last
;
694 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
696 /* Initially we are not debugging. */
697 lexer
->debugging_p
= false;
699 gcc_assert (!lexer
->next_token
->purged_p
);
703 /* Frees all resources associated with LEXER. */
706 cp_lexer_destroy (cp_lexer
*lexer
)
708 vec_free (lexer
->buffer
);
709 lexer
->saved_tokens
.release ();
713 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
714 be used. The point of this flag is to help the compiler to fold away calls
715 to cp_lexer_debugging_p within this source file at compile time, when the
716 lexer is not being debugged. */
718 #define LEXER_DEBUGGING_ENABLED_P false
720 /* Returns nonzero if debugging information should be output. */
723 cp_lexer_debugging_p (cp_lexer
*lexer
)
725 if (!LEXER_DEBUGGING_ENABLED_P
)
728 return lexer
->debugging_p
;
732 static inline cp_token_position
733 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
735 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
737 return lexer
->next_token
- previous_p
;
740 static inline cp_token
*
741 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
747 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
749 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
752 static inline cp_token_position
753 cp_lexer_previous_token_position (cp_lexer
*lexer
)
755 if (lexer
->next_token
== &eof_token
)
756 return lexer
->last_token
- 1;
758 return cp_lexer_token_position (lexer
, true);
761 static inline cp_token
*
762 cp_lexer_previous_token (cp_lexer
*lexer
)
764 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
766 /* Skip past purged tokens. */
769 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
773 return cp_lexer_token_at (lexer
, tp
);
776 /* nonzero if we are presently saving tokens. */
779 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
781 return lexer
->saved_tokens
.length () != 0;
784 /* Store the next token from the preprocessor in *TOKEN. Return true
785 if we reach EOF. If LEXER is NULL, assume we are handling an
786 initial #pragma pch_preprocess, and thus want the lexer to return
787 processed strings. */
790 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
792 static int is_extern_c
= 0;
794 /* Get a new token from the preprocessor. */
796 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
797 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
798 token
->keyword
= RID_MAX
;
799 token
->purged_p
= false;
800 token
->error_reported
= false;
802 /* On some systems, some header files are surrounded by an
803 implicit extern "C" block. Set a flag in the token if it
804 comes from such a header. */
805 is_extern_c
+= pending_lang_change
;
806 pending_lang_change
= 0;
807 token
->implicit_extern_c
= is_extern_c
> 0;
809 /* Check to see if this token is a keyword. */
810 if (token
->type
== CPP_NAME
)
812 if (C_IS_RESERVED_WORD (token
->u
.value
))
814 /* Mark this token as a keyword. */
815 token
->type
= CPP_KEYWORD
;
816 /* Record which keyword. */
817 token
->keyword
= C_RID_CODE (token
->u
.value
);
821 if (warn_cxx11_compat
822 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
823 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
825 /* Warn about the C++0x keyword (but still treat it as
827 warning (OPT_Wc__11_compat
,
828 "identifier %qE is a keyword in C++11",
831 /* Clear out the C_RID_CODE so we don't warn about this
832 particular identifier-turned-keyword again. */
833 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
836 token
->keyword
= RID_MAX
;
839 else if (token
->type
== CPP_AT_NAME
)
841 /* This only happens in Objective-C++; it must be a keyword. */
842 token
->type
= CPP_KEYWORD
;
843 switch (C_RID_CODE (token
->u
.value
))
845 /* Replace 'class' with '@class', 'private' with '@private',
846 etc. This prevents confusion with the C++ keyword
847 'class', and makes the tokens consistent with other
848 Objective-C 'AT' keywords. For example '@class' is
849 reported as RID_AT_CLASS which is consistent with
850 '@synchronized', which is reported as
853 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
854 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
855 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
856 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
857 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
858 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
859 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
860 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
861 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
866 /* Update the globals input_location and the input file stack from TOKEN. */
868 cp_lexer_set_source_position_from_token (cp_token
*token
)
870 if (token
->type
!= CPP_EOF
)
872 input_location
= token
->location
;
876 /* Update the globals input_location and the input file stack from LEXER. */
878 cp_lexer_set_source_position (cp_lexer
*lexer
)
880 cp_token
*token
= cp_lexer_peek_token (lexer
);
881 cp_lexer_set_source_position_from_token (token
);
884 /* Return a pointer to the next token in the token stream, but do not
887 static inline cp_token
*
888 cp_lexer_peek_token (cp_lexer
*lexer
)
890 if (cp_lexer_debugging_p (lexer
))
892 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
893 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
894 putc ('\n', cp_lexer_debug_stream
);
896 return lexer
->next_token
;
899 /* Return true if the next token has the indicated TYPE. */
902 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
904 return cp_lexer_peek_token (lexer
)->type
== type
;
907 /* Return true if the next token does not have the indicated TYPE. */
910 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
912 return !cp_lexer_next_token_is (lexer
, type
);
915 /* Return true if the next token is the indicated KEYWORD. */
918 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
920 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
924 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
926 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
930 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
932 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
935 /* Return true if the next token is not the indicated KEYWORD. */
938 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
940 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
943 /* Return true if KEYWORD can start a decl-specifier. */
946 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
953 /* Storage classes. */
959 /* Elaborated type specifiers. */
965 /* Simple type specifiers. */
979 /* GNU extensions. */
982 /* C++0x extensions. */
984 case RID_UNDERLYING_TYPE
:
989 if (keyword
>= RID_FIRST_INT_N
990 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
991 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
997 /* Return true if the next token is a keyword for a decl-specifier. */
1000 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1004 token
= cp_lexer_peek_token (lexer
);
1005 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1008 /* Returns TRUE iff the token T begins a decltype type. */
1011 token_is_decltype (cp_token
*t
)
1013 return (t
->keyword
== RID_DECLTYPE
1014 || t
->type
== CPP_DECLTYPE
);
1017 /* Returns TRUE iff the next token begins a decltype type. */
1020 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1022 cp_token
*t
= cp_lexer_peek_token (lexer
);
1023 return token_is_decltype (t
);
1026 /* Called when processing a token with tree_check_value; perform or defer the
1027 associated checks and return the value. */
1030 saved_checks_value (struct tree_check
*check_value
)
1032 /* Perform any access checks that were deferred. */
1033 vec
<deferred_access_check
, va_gc
> *checks
;
1034 deferred_access_check
*chk
;
1035 checks
= check_value
->checks
;
1039 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1040 perform_or_defer_access_check (chk
->binfo
,
1042 chk
->diag_decl
, tf_warning_or_error
);
1044 /* Return the stored value. */
1045 return check_value
->value
;
1048 /* Return a pointer to the Nth token in the token stream. If N is 1,
1049 then this is precisely equivalent to cp_lexer_peek_token (except
1050 that it is not inline). One would like to disallow that case, but
1051 there is one case (cp_parser_nth_token_starts_template_id) where
1052 the caller passes a variable for N and it might be 1. */
1055 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1059 /* N is 1-based, not zero-based. */
1062 if (cp_lexer_debugging_p (lexer
))
1063 fprintf (cp_lexer_debug_stream
,
1064 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1067 token
= lexer
->next_token
;
1068 gcc_assert (!n
|| token
!= &eof_token
);
1072 if (token
== lexer
->last_token
)
1078 if (!token
->purged_p
)
1082 if (cp_lexer_debugging_p (lexer
))
1084 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1085 putc ('\n', cp_lexer_debug_stream
);
1091 /* Return the next token, and advance the lexer's next_token pointer
1092 to point to the next non-purged token. */
1095 cp_lexer_consume_token (cp_lexer
* lexer
)
1097 cp_token
*token
= lexer
->next_token
;
1099 gcc_assert (token
!= &eof_token
);
1100 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1104 lexer
->next_token
++;
1105 if (lexer
->next_token
== lexer
->last_token
)
1107 lexer
->next_token
= &eof_token
;
1112 while (lexer
->next_token
->purged_p
);
1114 cp_lexer_set_source_position_from_token (token
);
1116 /* Provide debugging output. */
1117 if (cp_lexer_debugging_p (lexer
))
1119 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1120 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1121 putc ('\n', cp_lexer_debug_stream
);
1127 /* Permanently remove the next token from the token stream, and
1128 advance the next_token pointer to refer to the next non-purged
1132 cp_lexer_purge_token (cp_lexer
*lexer
)
1134 cp_token
*tok
= lexer
->next_token
;
1136 gcc_assert (tok
!= &eof_token
);
1137 tok
->purged_p
= true;
1138 tok
->location
= UNKNOWN_LOCATION
;
1139 tok
->u
.value
= NULL_TREE
;
1140 tok
->keyword
= RID_MAX
;
1145 if (tok
== lexer
->last_token
)
1151 while (tok
->purged_p
);
1152 lexer
->next_token
= tok
;
1155 /* Permanently remove all tokens after TOK, up to, but not
1156 including, the token that will be returned next by
1157 cp_lexer_peek_token. */
1160 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1162 cp_token
*peek
= lexer
->next_token
;
1164 if (peek
== &eof_token
)
1165 peek
= lexer
->last_token
;
1167 gcc_assert (tok
< peek
);
1169 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1171 tok
->purged_p
= true;
1172 tok
->location
= UNKNOWN_LOCATION
;
1173 tok
->u
.value
= NULL_TREE
;
1174 tok
->keyword
= RID_MAX
;
1178 /* Begin saving tokens. All tokens consumed after this point will be
1182 cp_lexer_save_tokens (cp_lexer
* lexer
)
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer
))
1186 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1188 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1191 /* Commit to the portion of the token stream most recently saved. */
1194 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer
))
1198 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1200 lexer
->saved_tokens
.pop ();
1203 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1204 to the token stream. Stop saving tokens. */
1207 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1209 /* Provide debugging output. */
1210 if (cp_lexer_debugging_p (lexer
))
1211 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1213 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1216 /* RAII wrapper around the above functions, with sanity checking. Creating
1217 a variable saves tokens, which are committed when the variable is
1218 destroyed unless they are explicitly rolled back by calling the rollback
1221 struct saved_token_sentinel
1226 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1228 len
= lexer
->saved_tokens
.length ();
1229 cp_lexer_save_tokens (lexer
);
1233 cp_lexer_rollback_tokens (lexer
);
1236 ~saved_token_sentinel()
1239 cp_lexer_commit_tokens (lexer
);
1240 gcc_assert (lexer
->saved_tokens
.length () == len
);
1244 /* Print a representation of the TOKEN on the STREAM. */
1247 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1249 /* We don't use cpp_type2name here because the parser defines
1250 a few tokens of its own. */
1251 static const char *const token_names
[] = {
1252 /* cpplib-defined token types */
1253 #define OP(e, s) #e,
1254 #define TK(e, s) #e,
1258 /* C++ parser token types - see "Manifest constants", above. */
1261 "NESTED_NAME_SPECIFIER",
1264 /* For some tokens, print the associated data. */
1265 switch (token
->type
)
1268 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1269 For example, `struct' is mapped to an INTEGER_CST. */
1270 if (!identifier_p (token
->u
.value
))
1274 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1281 case CPP_UTF8STRING
:
1282 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1286 print_generic_expr (stream
, token
->u
.value
, 0);
1290 /* If we have a name for the token, print it out. Otherwise, we
1291 simply give the numeric code. */
1292 if (token
->type
< ARRAY_SIZE(token_names
))
1293 fputs (token_names
[token
->type
], stream
);
1295 fprintf (stream
, "[%d]", token
->type
);
1301 debug (cp_token
&ref
)
1303 cp_lexer_print_token (stderr
, &ref
);
1304 fprintf (stderr
, "\n");
1308 debug (cp_token
*ptr
)
1313 fprintf (stderr
, "<nil>\n");
1317 /* Start emitting debugging information. */
1320 cp_lexer_start_debugging (cp_lexer
* lexer
)
1322 if (!LEXER_DEBUGGING_ENABLED_P
)
1323 fatal_error (input_location
,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326 lexer
->debugging_p
= true;
1327 cp_lexer_debug_stream
= stderr
;
1330 /* Stop emitting debugging information. */
1333 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1335 if (!LEXER_DEBUGGING_ENABLED_P
)
1336 fatal_error (input_location
,
1337 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1339 lexer
->debugging_p
= false;
1340 cp_lexer_debug_stream
= NULL
;
1343 /* Create a new cp_token_cache, representing a range of tokens. */
1345 static cp_token_cache
*
1346 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1348 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1349 cache
->first
= first
;
1354 /* Diagnose if #pragma omp declare simd isn't followed immediately
1355 by function declaration or definition. */
1358 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1360 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1362 error ("%<#pragma omp declare simd%> not immediately followed by "
1363 "function declaration or definition");
1364 parser
->omp_declare_simd
= NULL
;
1368 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1369 and put that into "omp declare simd" attribute. */
1372 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1374 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1376 if (fndecl
== error_mark_node
)
1378 parser
->omp_declare_simd
= NULL
;
1381 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1383 cp_ensure_no_omp_declare_simd (parser
);
1389 /* Diagnose if #pragma acc routine isn't followed immediately by function
1390 declaration or definition. */
1393 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1395 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1397 error_at (parser
->oacc_routine
->loc
,
1398 "%<#pragma acc routine%> not immediately followed by "
1399 "function declaration or definition");
1400 parser
->oacc_routine
= NULL
;
1404 /* Decl-specifiers. */
1406 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1409 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1411 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1416 /* Nothing other than the parser should be creating declarators;
1417 declarators are a semi-syntactic representation of C++ entities.
1418 Other parts of the front end that need to create entities (like
1419 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1421 static cp_declarator
*make_call_declarator
1422 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1423 static cp_declarator
*make_array_declarator
1424 (cp_declarator
*, tree
);
1425 static cp_declarator
*make_pointer_declarator
1426 (cp_cv_quals
, cp_declarator
*, tree
);
1427 static cp_declarator
*make_reference_declarator
1428 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1429 static cp_declarator
*make_ptrmem_declarator
1430 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1432 /* An erroneous declarator. */
1433 static cp_declarator
*cp_error_declarator
;
1435 /* The obstack on which declarators and related data structures are
1437 static struct obstack declarator_obstack
;
1439 /* Alloc BYTES from the declarator memory pool. */
1441 static inline void *
1442 alloc_declarator (size_t bytes
)
1444 return obstack_alloc (&declarator_obstack
, bytes
);
1447 /* Allocate a declarator of the indicated KIND. Clear fields that are
1448 common to all declarators. */
1450 static cp_declarator
*
1451 make_declarator (cp_declarator_kind kind
)
1453 cp_declarator
*declarator
;
1455 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1456 declarator
->kind
= kind
;
1457 declarator
->attributes
= NULL_TREE
;
1458 declarator
->std_attributes
= NULL_TREE
;
1459 declarator
->declarator
= NULL
;
1460 declarator
->parameter_pack_p
= false;
1461 declarator
->id_loc
= UNKNOWN_LOCATION
;
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1472 static cp_declarator
*
1473 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1474 special_function_kind sfk
)
1476 cp_declarator
*declarator
;
1478 /* It is valid to write:
1480 class C { void f(); };
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1488 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1489 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1491 gcc_assert (identifier_p (unqualified_name
)
1492 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1495 declarator
= make_declarator (cdk_id
);
1496 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1497 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1498 declarator
->u
.id
.sfk
= sfk
;
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1512 cp_declarator
*declarator
;
1514 declarator
= make_declarator (cdk_pointer
);
1515 declarator
->declarator
= target
;
1516 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1517 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1520 declarator
->id_loc
= target
->id_loc
;
1521 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1522 target
->parameter_pack_p
= false;
1525 declarator
->parameter_pack_p
= false;
1527 declarator
->std_attributes
= attributes
;
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1537 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1538 bool rvalue_ref
, tree attributes
)
1540 cp_declarator
*declarator
;
1542 declarator
= make_declarator (cdk_reference
);
1543 declarator
->declarator
= target
;
1544 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1545 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1548 declarator
->id_loc
= target
->id_loc
;
1549 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1550 target
->parameter_pack_p
= false;
1553 declarator
->parameter_pack_p
= false;
1555 declarator
->std_attributes
= attributes
;
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1566 cp_declarator
*pointee
,
1569 cp_declarator
*declarator
;
1571 declarator
= make_declarator (cdk_ptrmem
);
1572 declarator
->declarator
= pointee
;
1573 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1574 declarator
->u
.pointer
.class_type
= class_type
;
1578 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1579 pointee
->parameter_pack_p
= false;
1582 declarator
->parameter_pack_p
= false;
1584 declarator
->std_attributes
= attributes
;
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1595 make_call_declarator (cp_declarator
*target
,
1597 cp_cv_quals cv_qualifiers
,
1598 cp_virt_specifiers virt_specifiers
,
1599 cp_ref_qualifier ref_qualifier
,
1601 tree exception_specification
,
1602 tree late_return_type
,
1603 tree requires_clause
)
1605 cp_declarator
*declarator
;
1607 declarator
= make_declarator (cdk_function
);
1608 declarator
->declarator
= target
;
1609 declarator
->u
.function
.parameters
= parms
;
1610 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1611 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1612 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1613 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1614 declarator
->u
.function
.exception_specification
= exception_specification
;
1615 declarator
->u
.function
.late_return_type
= late_return_type
;
1616 declarator
->u
.function
.requires_clause
= requires_clause
;
1619 declarator
->id_loc
= target
->id_loc
;
1620 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1621 target
->parameter_pack_p
= false;
1624 declarator
->parameter_pack_p
= false;
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1633 make_array_declarator (cp_declarator
*element
, tree bounds
)
1635 cp_declarator
*declarator
;
1637 declarator
= make_declarator (cdk_array
);
1638 declarator
->declarator
= element
;
1639 declarator
->u
.array
.bounds
= bounds
;
1642 declarator
->id_loc
= element
->id_loc
;
1643 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1644 element
->parameter_pack_p
= false;
1647 declarator
->parameter_pack_p
= false;
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1655 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1657 if (declarator
&& declarator
->parameter_pack_p
)
1658 /* We already saw an ellipsis. */
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1666 while (declarator
&& !found
)
1668 switch ((int)declarator
->kind
)
1680 declarator
= declarator
->declarator
;
1688 cp_parameter_declarator
*no_parameters
;
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1693 cp_parameter_declarator
*
1694 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1695 cp_declarator
*declarator
,
1696 tree default_argument
,
1697 bool template_parameter_pack_p
= false)
1699 cp_parameter_declarator
*parameter
;
1701 parameter
= ((cp_parameter_declarator
*)
1702 alloc_declarator (sizeof (cp_parameter_declarator
)));
1703 parameter
->next
= NULL
;
1704 if (decl_specifiers
)
1705 parameter
->decl_specifiers
= *decl_specifiers
;
1707 clear_decl_specs (¶meter
->decl_specifiers
);
1708 parameter
->declarator
= declarator
;
1709 parameter
->default_argument
= default_argument
;
1710 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1715 /* Returns true iff DECLARATOR is a declaration for a function. */
1718 function_declarator_p (const cp_declarator
*declarator
)
1722 if (declarator
->kind
== cdk_function
1723 && declarator
->declarator
->kind
== cdk_id
)
1725 if (declarator
->kind
== cdk_id
1726 || declarator
->kind
== cdk_decomp
1727 || declarator
->kind
== cdk_error
)
1729 declarator
= declarator
->declarator
;
1739 A cp_parser parses the token stream as specified by the C++
1740 grammar. Its job is purely parsing, not semantic analysis. For
1741 example, the parser breaks the token stream into declarators,
1742 expressions, statements, and other similar syntactic constructs.
1743 It does not check that the types of the expressions on either side
1744 of an assignment-statement are compatible, or that a function is
1745 not declared with a parameter of type `void'.
1747 The parser invokes routines elsewhere in the compiler to perform
1748 semantic analysis and to build up the abstract syntax tree for the
1751 The parser (and the template instantiation code, which is, in a
1752 way, a close relative of parsing) are the only parts of the
1753 compiler that should be calling push_scope and pop_scope, or
1754 related functions. The parser (and template instantiation code)
1755 keeps track of what scope is presently active; everything else
1756 should simply honor that. (The code that generates static
1757 initializers may also need to set the scope, in order to check
1758 access control correctly when emitting the initializers.)
1763 The parser is of the standard recursive-descent variety. Upcoming
1764 tokens in the token stream are examined in order to determine which
1765 production to use when parsing a non-terminal. Some C++ constructs
1766 require arbitrary look ahead to disambiguate. For example, it is
1767 impossible, in the general case, to tell whether a statement is an
1768 expression or declaration without scanning the entire statement.
1769 Therefore, the parser is capable of "parsing tentatively." When the
1770 parser is not sure what construct comes next, it enters this mode.
1771 Then, while we attempt to parse the construct, the parser queues up
1772 error messages, rather than issuing them immediately, and saves the
1773 tokens it consumes. If the construct is parsed successfully, the
1774 parser "commits", i.e., it issues any queued error messages and
1775 the tokens that were being preserved are permanently discarded.
1776 If, however, the construct is not parsed successfully, the parser
1777 rolls back its state completely so that it can resume parsing using
1778 a different alternative.
1783 The performance of the parser could probably be improved substantially.
1784 We could often eliminate the need to parse tentatively by looking ahead
1785 a little bit. In some places, this approach might not entirely eliminate
1786 the need to parse tentatively, but it might still speed up the average
1789 /* Flags that are passed to some parsing functions. These values can
1790 be bitwise-ored together. */
1795 CP_PARSER_FLAGS_NONE
= 0x0,
1796 /* The construct is optional. If it is not present, then no error
1797 should be issued. */
1798 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1799 /* When parsing a type-specifier, treat user-defined type-names
1800 as non-type identifiers. */
1801 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1802 /* When parsing a type-specifier, do not try to parse a class-specifier
1803 or enum-specifier. */
1804 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1805 /* When parsing a decl-specifier-seq, only allow type-specifier or
1807 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1808 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1809 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1812 /* This type is used for parameters and variables which hold
1813 combinations of the above flags. */
1814 typedef int cp_parser_flags
;
1816 /* The different kinds of declarators we want to parse. */
1818 enum cp_parser_declarator_kind
1820 /* We want an abstract declarator. */
1821 CP_PARSER_DECLARATOR_ABSTRACT
,
1822 /* We want a named declarator. */
1823 CP_PARSER_DECLARATOR_NAMED
,
1824 /* We don't mind, but the name must be an unqualified-id. */
1825 CP_PARSER_DECLARATOR_EITHER
1828 /* The precedence values used to parse binary expressions. The minimum value
1829 of PREC must be 1, because zero is reserved to quickly discriminate
1830 binary operators from other tokens. */
1835 PREC_LOGICAL_OR_EXPRESSION
,
1836 PREC_LOGICAL_AND_EXPRESSION
,
1837 PREC_INCLUSIVE_OR_EXPRESSION
,
1838 PREC_EXCLUSIVE_OR_EXPRESSION
,
1839 PREC_AND_EXPRESSION
,
1840 PREC_EQUALITY_EXPRESSION
,
1841 PREC_RELATIONAL_EXPRESSION
,
1842 PREC_SHIFT_EXPRESSION
,
1843 PREC_ADDITIVE_EXPRESSION
,
1844 PREC_MULTIPLICATIVE_EXPRESSION
,
1846 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1849 /* A mapping from a token type to a corresponding tree node type, with a
1850 precedence value. */
1852 struct cp_parser_binary_operations_map_node
1854 /* The token type. */
1855 enum cpp_ttype token_type
;
1856 /* The corresponding tree code. */
1857 enum tree_code tree_type
;
1858 /* The precedence of this operator. */
1859 enum cp_parser_prec prec
;
1862 struct cp_parser_expression_stack_entry
1864 /* Left hand side of the binary operation we are currently
1867 /* Original tree code for left hand side, if it was a binary
1868 expression itself (used for -Wparentheses). */
1869 enum tree_code lhs_type
;
1870 /* Tree code for the binary operation we are parsing. */
1871 enum tree_code tree_type
;
1872 /* Precedence of the binary operation we are parsing. */
1873 enum cp_parser_prec prec
;
1874 /* Location of the binary operation we are parsing. */
1878 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1879 entries because precedence levels on the stack are monotonically
1881 typedef struct cp_parser_expression_stack_entry
1882 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1886 /* Constructors and destructors. */
1888 static cp_parser_context
*cp_parser_context_new
1889 (cp_parser_context
*);
1891 /* Class variables. */
1893 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1895 /* The operator-precedence table used by cp_parser_binary_expression.
1896 Transformed into an associative array (binops_by_token) by
1899 static const cp_parser_binary_operations_map_node binops
[] = {
1900 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1901 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1903 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1904 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1905 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1907 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1908 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1910 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1911 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1913 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1914 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1915 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1916 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1918 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1919 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1921 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1923 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1925 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1927 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1929 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1932 /* The same as binops, but initialized by cp_parser_new so that
1933 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1935 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1937 /* Constructors and destructors. */
1939 /* Construct a new context. The context below this one on the stack
1940 is given by NEXT. */
1942 static cp_parser_context
*
1943 cp_parser_context_new (cp_parser_context
* next
)
1945 cp_parser_context
*context
;
1947 /* Allocate the storage. */
1948 if (cp_parser_context_free_list
!= NULL
)
1950 /* Pull the first entry from the free list. */
1951 context
= cp_parser_context_free_list
;
1952 cp_parser_context_free_list
= context
->next
;
1953 memset (context
, 0, sizeof (*context
));
1956 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1958 /* No errors have occurred yet in this context. */
1959 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1960 /* If this is not the bottommost context, copy information that we
1961 need from the previous context. */
1964 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1965 expression, then we are parsing one in this context, too. */
1966 context
->object_type
= next
->object_type
;
1967 /* Thread the stack. */
1968 context
->next
= next
;
1974 /* Managing the unparsed function queues. */
1976 #define unparsed_funs_with_default_args \
1977 parser->unparsed_queues->last ().funs_with_default_args
1978 #define unparsed_funs_with_definitions \
1979 parser->unparsed_queues->last ().funs_with_definitions
1980 #define unparsed_nsdmis \
1981 parser->unparsed_queues->last ().nsdmis
1982 #define unparsed_classes \
1983 parser->unparsed_queues->last ().classes
1986 push_unparsed_function_queues (cp_parser
*parser
)
1988 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1989 vec_safe_push (parser
->unparsed_queues
, e
);
1993 pop_unparsed_function_queues (cp_parser
*parser
)
1995 release_tree_vector (unparsed_funs_with_definitions
);
1996 parser
->unparsed_queues
->pop ();
2001 /* Constructors and destructors. */
2003 static cp_parser
*cp_parser_new
2006 /* Routines to parse various constructs.
2008 Those that return `tree' will return the error_mark_node (rather
2009 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2010 Sometimes, they will return an ordinary node if error-recovery was
2011 attempted, even though a parse error occurred. So, to check
2012 whether or not a parse error occurred, you should always use
2013 cp_parser_error_occurred. If the construct is optional (indicated
2014 either by an `_opt' in the name of the function that does the
2015 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2016 the construct is not present. */
2018 /* Lexical conventions [gram.lex] */
2020 static cp_expr cp_parser_identifier
2022 static cp_expr cp_parser_string_literal
2023 (cp_parser
*, bool, bool, bool);
2024 static cp_expr cp_parser_userdef_char_literal
2026 static tree cp_parser_userdef_string_literal
2028 static cp_expr cp_parser_userdef_numeric_literal
2031 /* Basic concepts [gram.basic] */
2033 static bool cp_parser_translation_unit
2036 /* Expressions [gram.expr] */
2038 static cp_expr cp_parser_primary_expression
2039 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2040 static cp_expr cp_parser_id_expression
2041 (cp_parser
*, bool, bool, bool *, bool, bool);
2042 static cp_expr cp_parser_unqualified_id
2043 (cp_parser
*, bool, bool, bool, bool);
2044 static tree cp_parser_nested_name_specifier_opt
2045 (cp_parser
*, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier
2047 (cp_parser
*, bool, bool, bool, bool);
2048 static tree cp_parser_qualifying_entity
2049 (cp_parser
*, bool, bool, bool, bool, bool);
2050 static cp_expr cp_parser_postfix_expression
2051 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2052 static tree cp_parser_postfix_open_square_expression
2053 (cp_parser
*, tree
, bool, bool);
2054 static tree cp_parser_postfix_dot_deref_expression
2055 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2056 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2057 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
);
2058 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2059 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2060 static void cp_parser_pseudo_destructor_name
2061 (cp_parser
*, tree
, tree
*, tree
*);
2062 static cp_expr cp_parser_unary_expression
2063 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2064 static enum tree_code cp_parser_unary_operator
2066 static tree cp_parser_new_expression
2068 static vec
<tree
, va_gc
> *cp_parser_new_placement
2070 static tree cp_parser_new_type_id
2071 (cp_parser
*, tree
*);
2072 static cp_declarator
*cp_parser_new_declarator_opt
2074 static cp_declarator
*cp_parser_direct_new_declarator
2076 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2078 static tree cp_parser_delete_expression
2080 static cp_expr cp_parser_cast_expression
2081 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2082 static cp_expr cp_parser_binary_expression
2083 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2084 static tree cp_parser_question_colon_clause
2085 (cp_parser
*, cp_expr
);
2086 static cp_expr cp_parser_assignment_expression
2087 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2088 static enum tree_code cp_parser_assignment_operator_opt
2090 static cp_expr cp_parser_expression
2091 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2092 static cp_expr cp_parser_constant_expression
2093 (cp_parser
*, bool = false, bool * = NULL
);
2094 static cp_expr cp_parser_builtin_offsetof
2096 static cp_expr cp_parser_lambda_expression
2098 static void cp_parser_lambda_introducer
2099 (cp_parser
*, tree
);
2100 static bool cp_parser_lambda_declarator_opt
2101 (cp_parser
*, tree
);
2102 static void cp_parser_lambda_body
2103 (cp_parser
*, tree
);
2105 /* Statements [gram.stmt.stmt] */
2107 static void cp_parser_statement
2108 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
);
2109 static void cp_parser_label_for_labeled_statement
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_expression_statement
2112 (cp_parser
*, tree
);
2113 static tree cp_parser_compound_statement
2114 (cp_parser
*, tree
, int, bool);
2115 static void cp_parser_statement_seq_opt
2116 (cp_parser
*, tree
);
2117 static tree cp_parser_selection_statement
2118 (cp_parser
*, bool *, vec
<tree
> *);
2119 static tree cp_parser_condition
2121 static tree cp_parser_iteration_statement
2122 (cp_parser
*, bool *, bool);
2123 static bool cp_parser_init_statement
2124 (cp_parser
*, tree
*decl
);
2125 static tree cp_parser_for
2126 (cp_parser
*, bool);
2127 static tree cp_parser_c_for
2128 (cp_parser
*, tree
, tree
, bool);
2129 static tree cp_parser_range_for
2130 (cp_parser
*, tree
, tree
, tree
, bool);
2131 static void do_range_for_auto_deduction
2133 static tree cp_parser_perform_range_for_lookup
2134 (tree
, tree
*, tree
*);
2135 static tree cp_parser_range_for_member_function
2137 static tree cp_parser_jump_statement
2139 static void cp_parser_declaration_statement
2142 static tree cp_parser_implicitly_scoped_statement
2143 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2144 static void cp_parser_already_scoped_statement
2145 (cp_parser
*, bool *, const token_indent_info
&);
2147 /* Declarations [gram.dcl.dcl] */
2149 static void cp_parser_declaration_seq_opt
2151 static void cp_parser_declaration
2153 static void cp_parser_block_declaration
2154 (cp_parser
*, bool);
2155 static void cp_parser_simple_declaration
2156 (cp_parser
*, bool, tree
*);
2157 static void cp_parser_decl_specifier_seq
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2159 static tree cp_parser_storage_class_specifier_opt
2161 static tree cp_parser_function_specifier_opt
2162 (cp_parser
*, cp_decl_specifier_seq
*);
2163 static tree cp_parser_type_specifier
2164 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2166 static tree cp_parser_simple_type_specifier
2167 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2168 static tree cp_parser_type_name
2169 (cp_parser
*, bool);
2170 static tree cp_parser_type_name
2172 static tree cp_parser_nonclass_name
2173 (cp_parser
* parser
);
2174 static tree cp_parser_elaborated_type_specifier
2175 (cp_parser
*, bool, bool);
2176 static tree cp_parser_enum_specifier
2178 static void cp_parser_enumerator_list
2179 (cp_parser
*, tree
);
2180 static void cp_parser_enumerator_definition
2181 (cp_parser
*, tree
);
2182 static tree cp_parser_namespace_name
2184 static void cp_parser_namespace_definition
2186 static void cp_parser_namespace_body
2188 static tree cp_parser_qualified_namespace_specifier
2190 static void cp_parser_namespace_alias_definition
2192 static bool cp_parser_using_declaration
2193 (cp_parser
*, bool);
2194 static void cp_parser_using_directive
2196 static tree cp_parser_alias_declaration
2198 static void cp_parser_asm_definition
2200 static void cp_parser_linkage_specification
2202 static void cp_parser_static_assert
2203 (cp_parser
*, bool);
2204 static tree cp_parser_decltype
2206 static tree cp_parser_decomposition_declaration
2207 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2209 /* Declarators [gram.dcl.decl] */
2211 static tree cp_parser_init_declarator
2212 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2213 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2214 static cp_declarator
*cp_parser_declarator
2215 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2216 static cp_declarator
*cp_parser_direct_declarator
2217 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2218 static enum tree_code cp_parser_ptr_operator
2219 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2220 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2222 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2224 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2226 static tree cp_parser_tx_qualifier_opt
2228 static tree cp_parser_late_return_type_opt
2229 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2230 static tree cp_parser_declarator_id
2231 (cp_parser
*, bool);
2232 static tree cp_parser_type_id
2234 static tree cp_parser_template_type_arg
2236 static tree
cp_parser_trailing_type_id (cp_parser
*);
2237 static tree cp_parser_type_id_1
2238 (cp_parser
*, bool, bool);
2239 static void cp_parser_type_specifier_seq
2240 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2241 static tree cp_parser_parameter_declaration_clause
2243 static tree cp_parser_parameter_declaration_list
2244 (cp_parser
*, bool *);
2245 static cp_parameter_declarator
*cp_parser_parameter_declaration
2246 (cp_parser
*, bool, bool *);
2247 static tree cp_parser_default_argument
2248 (cp_parser
*, bool);
2249 static void cp_parser_function_body
2250 (cp_parser
*, bool);
2251 static tree cp_parser_initializer
2252 (cp_parser
*, bool *, bool *);
2253 static cp_expr cp_parser_initializer_clause
2254 (cp_parser
*, bool *);
2255 static cp_expr cp_parser_braced_list
2256 (cp_parser
*, bool*);
2257 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2258 (cp_parser
*, bool *);
2260 static bool cp_parser_ctor_initializer_opt_and_function_body
2261 (cp_parser
*, bool);
2263 static tree cp_parser_late_parsing_omp_declare_simd
2264 (cp_parser
*, tree
);
2266 static tree cp_parser_late_parsing_cilk_simd_fn_info
2267 (cp_parser
*, tree
);
2269 static tree cp_parser_late_parsing_oacc_routine
2270 (cp_parser
*, tree
);
2272 static tree synthesize_implicit_template_parm
2273 (cp_parser
*, tree
);
2274 static tree finish_fully_implicit_template
2275 (cp_parser
*, tree
);
2277 /* Classes [gram.class] */
2279 static tree cp_parser_class_name
2280 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2281 static tree cp_parser_class_specifier
2283 static tree cp_parser_class_head
2284 (cp_parser
*, bool *);
2285 static enum tag_types cp_parser_class_key
2287 static void cp_parser_type_parameter_key
2288 (cp_parser
* parser
);
2289 static void cp_parser_member_specification_opt
2291 static void cp_parser_member_declaration
2293 static tree cp_parser_pure_specifier
2295 static tree cp_parser_constant_initializer
2298 /* Derived classes [gram.class.derived] */
2300 static tree cp_parser_base_clause
2302 static tree cp_parser_base_specifier
2305 /* Special member functions [gram.special] */
2307 static tree cp_parser_conversion_function_id
2309 static tree cp_parser_conversion_type_id
2311 static cp_declarator
*cp_parser_conversion_declarator_opt
2313 static bool cp_parser_ctor_initializer_opt
2315 static void cp_parser_mem_initializer_list
2317 static tree cp_parser_mem_initializer
2319 static tree cp_parser_mem_initializer_id
2322 /* Overloading [gram.over] */
2324 static cp_expr cp_parser_operator_function_id
2326 static cp_expr cp_parser_operator
2329 /* Templates [gram.temp] */
2331 static void cp_parser_template_declaration
2332 (cp_parser
*, bool);
2333 static tree cp_parser_template_parameter_list
2335 static tree cp_parser_template_parameter
2336 (cp_parser
*, bool *, bool *);
2337 static tree cp_parser_type_parameter
2338 (cp_parser
*, bool *);
2339 static tree cp_parser_template_id
2340 (cp_parser
*, bool, bool, enum tag_types
, bool);
2341 static tree cp_parser_template_name
2342 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2343 static tree cp_parser_template_argument_list
2345 static tree cp_parser_template_argument
2347 static void cp_parser_explicit_instantiation
2349 static void cp_parser_explicit_specialization
2352 /* Exception handling [gram.exception] */
2354 static tree cp_parser_try_block
2356 static bool cp_parser_function_try_block
2358 static void cp_parser_handler_seq
2360 static void cp_parser_handler
2362 static tree cp_parser_exception_declaration
2364 static tree cp_parser_throw_expression
2366 static tree cp_parser_exception_specification_opt
2368 static tree cp_parser_type_id_list
2371 /* GNU Extensions */
2373 static tree cp_parser_asm_specification_opt
2375 static tree cp_parser_asm_operand_list
2377 static tree cp_parser_asm_clobber_list
2379 static tree cp_parser_asm_label_list
2381 static bool cp_next_tokens_can_be_attribute_p
2383 static bool cp_next_tokens_can_be_gnu_attribute_p
2385 static bool cp_next_tokens_can_be_std_attribute_p
2387 static bool cp_nth_tokens_can_be_std_attribute_p
2388 (cp_parser
*, size_t);
2389 static bool cp_nth_tokens_can_be_gnu_attribute_p
2390 (cp_parser
*, size_t);
2391 static bool cp_nth_tokens_can_be_attribute_p
2392 (cp_parser
*, size_t);
2393 static tree cp_parser_attributes_opt
2395 static tree cp_parser_gnu_attributes_opt
2397 static tree cp_parser_gnu_attribute_list
2399 static tree cp_parser_std_attribute
2400 (cp_parser
*, tree
);
2401 static tree cp_parser_std_attribute_spec
2403 static tree cp_parser_std_attribute_spec_seq
2405 static bool cp_parser_extension_opt
2406 (cp_parser
*, int *);
2407 static void cp_parser_label_declaration
2410 /* Concept Extensions */
2412 static tree cp_parser_requires_clause
2414 static tree cp_parser_requires_clause_opt
2416 static tree cp_parser_requires_expression
2418 static tree cp_parser_requirement_parameter_list
2420 static tree cp_parser_requirement_body
2422 static tree cp_parser_requirement_list
2424 static tree cp_parser_requirement
2426 static tree cp_parser_simple_requirement
2428 static tree cp_parser_compound_requirement
2430 static tree cp_parser_type_requirement
2432 static tree cp_parser_nested_requirement
2435 /* Transactional Memory Extensions */
2437 static tree cp_parser_transaction
2438 (cp_parser
*, cp_token
*);
2439 static tree cp_parser_transaction_expression
2440 (cp_parser
*, enum rid
);
2441 static bool cp_parser_function_transaction
2442 (cp_parser
*, enum rid
);
2443 static tree cp_parser_transaction_cancel
2446 enum pragma_context
{
2453 static bool cp_parser_pragma
2454 (cp_parser
*, enum pragma_context
, bool *);
2456 /* Objective-C++ Productions */
2458 static tree cp_parser_objc_message_receiver
2460 static tree cp_parser_objc_message_args
2462 static tree cp_parser_objc_message_expression
2464 static cp_expr cp_parser_objc_encode_expression
2466 static tree cp_parser_objc_defs_expression
2468 static tree cp_parser_objc_protocol_expression
2470 static tree cp_parser_objc_selector_expression
2472 static cp_expr cp_parser_objc_expression
2474 static bool cp_parser_objc_selector_p
2476 static tree cp_parser_objc_selector
2478 static tree cp_parser_objc_protocol_refs_opt
2480 static void cp_parser_objc_declaration
2481 (cp_parser
*, tree
);
2482 static tree cp_parser_objc_statement
2484 static bool cp_parser_objc_valid_prefix_attributes
2485 (cp_parser
*, tree
*);
2486 static void cp_parser_objc_at_property_declaration
2488 static void cp_parser_objc_at_synthesize_declaration
2490 static void cp_parser_objc_at_dynamic_declaration
2492 static tree cp_parser_objc_struct_declaration
2495 /* Utility Routines */
2497 static cp_expr cp_parser_lookup_name
2498 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2499 static tree cp_parser_lookup_name_simple
2500 (cp_parser
*, tree
, location_t
);
2501 static tree cp_parser_maybe_treat_template_as_class
2503 static bool cp_parser_check_declarator_template_parameters
2504 (cp_parser
*, cp_declarator
*, location_t
);
2505 static bool cp_parser_check_template_parameters
2506 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2507 static cp_expr cp_parser_simple_cast_expression
2509 static tree cp_parser_global_scope_opt
2510 (cp_parser
*, bool);
2511 static bool cp_parser_constructor_declarator_p
2512 (cp_parser
*, bool);
2513 static tree cp_parser_function_definition_from_specifiers_and_declarator
2514 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2515 static tree cp_parser_function_definition_after_declarator
2516 (cp_parser
*, bool);
2517 static bool cp_parser_template_declaration_after_export
2518 (cp_parser
*, bool);
2519 static void cp_parser_perform_template_parameter_access_checks
2520 (vec
<deferred_access_check
, va_gc
> *);
2521 static tree cp_parser_single_declaration
2522 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2523 static cp_expr cp_parser_functional_cast
2524 (cp_parser
*, tree
);
2525 static tree cp_parser_save_member_function_body
2526 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2527 static tree cp_parser_save_nsdmi
2529 static tree cp_parser_enclosed_template_argument_list
2531 static void cp_parser_save_default_args
2532 (cp_parser
*, tree
);
2533 static void cp_parser_late_parsing_for_member
2534 (cp_parser
*, tree
);
2535 static tree cp_parser_late_parse_one_default_arg
2536 (cp_parser
*, tree
, tree
, tree
);
2537 static void cp_parser_late_parsing_nsdmi
2538 (cp_parser
*, tree
);
2539 static void cp_parser_late_parsing_default_args
2540 (cp_parser
*, tree
);
2541 static tree cp_parser_sizeof_operand
2542 (cp_parser
*, enum rid
);
2543 static tree cp_parser_trait_expr
2544 (cp_parser
*, enum rid
);
2545 static bool cp_parser_declares_only_class_p
2547 static void cp_parser_set_storage_class
2548 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2549 static void cp_parser_set_decl_spec_type
2550 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2551 static void set_and_check_decl_spec_loc
2552 (cp_decl_specifier_seq
*decl_specs
,
2553 cp_decl_spec ds
, cp_token
*);
2554 static bool cp_parser_friend_p
2555 (const cp_decl_specifier_seq
*);
2556 static void cp_parser_required_error
2557 (cp_parser
*, required_token
, bool);
2558 static cp_token
*cp_parser_require
2559 (cp_parser
*, enum cpp_ttype
, required_token
);
2560 static cp_token
*cp_parser_require_keyword
2561 (cp_parser
*, enum rid
, required_token
);
2562 static bool cp_parser_token_starts_function_definition_p
2564 static bool cp_parser_next_token_starts_class_definition_p
2566 static bool cp_parser_next_token_ends_template_argument_p
2568 static bool cp_parser_nth_token_starts_template_argument_list_p
2569 (cp_parser
*, size_t);
2570 static enum tag_types cp_parser_token_is_class_key
2572 static enum tag_types cp_parser_token_is_type_parameter_key
2574 static void cp_parser_check_class_key
2575 (enum tag_types
, tree type
);
2576 static void cp_parser_check_access_in_redeclaration
2577 (tree type
, location_t location
);
2578 static bool cp_parser_optional_template_keyword
2580 static void cp_parser_pre_parsed_nested_name_specifier
2582 static bool cp_parser_cache_group
2583 (cp_parser
*, enum cpp_ttype
, unsigned);
2584 static tree cp_parser_cache_defarg
2585 (cp_parser
*parser
, bool nsdmi
);
2586 static void cp_parser_parse_tentatively
2588 static void cp_parser_commit_to_tentative_parse
2590 static void cp_parser_commit_to_topmost_tentative_parse
2592 static void cp_parser_abort_tentative_parse
2594 static bool cp_parser_parse_definitely
2596 static inline bool cp_parser_parsing_tentatively
2598 static bool cp_parser_uncommitted_to_tentative_parse_p
2600 static void cp_parser_error
2601 (cp_parser
*, const char *);
2602 static void cp_parser_name_lookup_error
2603 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2604 static bool cp_parser_simulate_error
2606 static bool cp_parser_check_type_definition
2608 static void cp_parser_check_for_definition_in_return_type
2609 (cp_declarator
*, tree
, location_t type_location
);
2610 static void cp_parser_check_for_invalid_template_id
2611 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2612 static bool cp_parser_non_integral_constant_expression
2613 (cp_parser
*, non_integral_constant
);
2614 static void cp_parser_diagnose_invalid_type_name
2615 (cp_parser
*, tree
, location_t
);
2616 static bool cp_parser_parse_and_diagnose_invalid_type_name
2618 static int cp_parser_skip_to_closing_parenthesis
2619 (cp_parser
*, bool, bool, bool);
2620 static void cp_parser_skip_to_end_of_statement
2622 static void cp_parser_consume_semicolon_at_end_of_statement
2624 static void cp_parser_skip_to_end_of_block_or_statement
2626 static bool cp_parser_skip_to_closing_brace
2628 static void cp_parser_skip_to_end_of_template_parameter_list
2630 static void cp_parser_skip_to_pragma_eol
2631 (cp_parser
*, cp_token
*);
2632 static bool cp_parser_error_occurred
2634 static bool cp_parser_allow_gnu_extensions_p
2636 static bool cp_parser_is_pure_string_literal
2638 static bool cp_parser_is_string_literal
2640 static bool cp_parser_is_keyword
2641 (cp_token
*, enum rid
);
2642 static tree cp_parser_make_typename_type
2643 (cp_parser
*, tree
, location_t location
);
2644 static cp_declarator
* cp_parser_make_indirect_declarator
2645 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2646 static bool cp_parser_compound_literal_p
2648 static bool cp_parser_array_designator_p
2650 static bool cp_parser_init_statement_p
2652 static bool cp_parser_skip_to_closing_square_bracket
2655 /* Concept-related syntactic transformations */
2657 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2658 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2663 // Implementation of an RAII helper for unevaluated operand parsing.
2664 cp_unevaluated::cp_unevaluated ()
2666 ++cp_unevaluated_operand
;
2667 ++c_inhibit_evaluation_warnings
;
2670 cp_unevaluated::~cp_unevaluated ()
2672 --c_inhibit_evaluation_warnings
;
2673 --cp_unevaluated_operand
;
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2679 /* Returns nonzero if we are parsing tentatively. */
2682 cp_parser_parsing_tentatively (cp_parser
* parser
)
2684 return parser
->context
->next
!= NULL
;
2687 /* Returns nonzero if TOKEN is a string literal. */
2690 cp_parser_is_pure_string_literal (cp_token
* token
)
2692 return (token
->type
== CPP_STRING
||
2693 token
->type
== CPP_STRING16
||
2694 token
->type
== CPP_STRING32
||
2695 token
->type
== CPP_WSTRING
||
2696 token
->type
== CPP_UTF8STRING
);
2699 /* Returns nonzero if TOKEN is a string literal
2700 of a user-defined string literal. */
2703 cp_parser_is_string_literal (cp_token
* token
)
2705 return (cp_parser_is_pure_string_literal (token
) ||
2706 token
->type
== CPP_STRING_USERDEF
||
2707 token
->type
== CPP_STRING16_USERDEF
||
2708 token
->type
== CPP_STRING32_USERDEF
||
2709 token
->type
== CPP_WSTRING_USERDEF
||
2710 token
->type
== CPP_UTF8STRING_USERDEF
);
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2716 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2718 return token
->keyword
== keyword
;
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2724 static enum pragma_kind
2725 cp_parser_pragma_kind (cp_token
*token
)
2727 if (token
->type
!= CPP_PRAGMA
)
2729 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2730 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2733 /* Helper function for cp_parser_error.
2734 Having peeked a token of kind TOK1_KIND that might signify
2735 a conflict marker, peek successor tokens to determine
2736 if we actually do have a conflict marker.
2737 Specifically, we consider a run of 7 '<', '=' or '>' characters
2738 at the start of a line as a conflict marker.
2739 These come through the lexer as three pairs and a single,
2740 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741 If it returns true, *OUT_LOC is written to with the location/range
2745 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2746 location_t
*out_loc
)
2748 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2749 if (token2
->type
!= tok1_kind
)
2751 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2752 if (token3
->type
!= tok1_kind
)
2754 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2755 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2758 /* It must be at the start of the line. */
2759 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2760 if (LOCATION_COLUMN (start_loc
) != 1)
2763 /* We have a conflict marker. Construct a location of the form:
2766 with start == caret, finishing at the end of the marker. */
2767 location_t finish_loc
= get_finish (token4
->location
);
2768 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2773 /* If not parsing tentatively, issue a diagnostic of the form
2774 FILE:LINE: MESSAGE before TOKEN
2775 where TOKEN is the next token in the input stream. MESSAGE
2776 (specified by the caller) is usually of the form "expected
2780 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2782 if (!cp_parser_simulate_error (parser
))
2784 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2785 /* This diagnostic makes more sense if it is tagged to the line
2786 of the token we just peeked at. */
2787 cp_lexer_set_source_position_from_token (token
);
2789 if (token
->type
== CPP_PRAGMA
)
2791 error_at (token
->location
,
2792 "%<#pragma%> is not allowed here");
2793 cp_parser_skip_to_pragma_eol (parser
, token
);
2797 /* If this is actually a conflict marker, report it as such. */
2798 if (token
->type
== CPP_LSHIFT
2799 || token
->type
== CPP_RSHIFT
2800 || token
->type
== CPP_EQ_EQ
)
2803 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2805 error_at (loc
, "version control conflict marker in file");
2810 c_parse_error (gmsgid
,
2811 /* Because c_parser_error does not understand
2812 CPP_KEYWORD, keywords are treated like
2814 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2815 token
->u
.value
, token
->flags
);
2819 /* Issue an error about name-lookup failing. NAME is the
2820 IDENTIFIER_NODE DECL is the result of
2821 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2822 the thing that we hoped to find. */
2825 cp_parser_name_lookup_error (cp_parser
* parser
,
2828 name_lookup_error desired
,
2829 location_t location
)
2831 /* If name lookup completely failed, tell the user that NAME was not
2833 if (decl
== error_mark_node
)
2835 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2836 error_at (location
, "%<%E::%E%> has not been declared",
2837 parser
->scope
, name
);
2838 else if (parser
->scope
== global_namespace
)
2839 error_at (location
, "%<::%E%> has not been declared", name
);
2840 else if (parser
->object_scope
2841 && !CLASS_TYPE_P (parser
->object_scope
))
2842 error_at (location
, "request for member %qE in non-class type %qT",
2843 name
, parser
->object_scope
);
2844 else if (parser
->object_scope
)
2845 error_at (location
, "%<%T::%E%> has not been declared",
2846 parser
->object_scope
, name
);
2848 error_at (location
, "%qE has not been declared", name
);
2850 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2855 error_at (location
, "%<%E::%E%> is not a type",
2856 parser
->scope
, name
);
2859 error_at (location
, "%<%E::%E%> is not a class or namespace",
2860 parser
->scope
, name
);
2864 "%<%E::%E%> is not a class, namespace, or enumeration",
2865 parser
->scope
, name
);
2872 else if (parser
->scope
== global_namespace
)
2877 error_at (location
, "%<::%E%> is not a type", name
);
2880 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2884 "%<::%E%> is not a class, namespace, or enumeration",
2896 error_at (location
, "%qE is not a type", name
);
2899 error_at (location
, "%qE is not a class or namespace", name
);
2903 "%qE is not a class, namespace, or enumeration", name
);
2911 /* If we are parsing tentatively, remember that an error has occurred
2912 during this tentative parse. Returns true if the error was
2913 simulated; false if a message should be issued by the caller. */
2916 cp_parser_simulate_error (cp_parser
* parser
)
2918 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2920 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2926 /* This function is called when a type is defined. If type
2927 definitions are forbidden at this point, an error message is
2931 cp_parser_check_type_definition (cp_parser
* parser
)
2933 /* If types are forbidden here, issue a message. */
2934 if (parser
->type_definition_forbidden_message
)
2936 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2937 in the message need to be interpreted. */
2938 error (parser
->type_definition_forbidden_message
);
2944 /* This function is called when the DECLARATOR is processed. The TYPE
2945 was a type defined in the decl-specifiers. If it is invalid to
2946 define a type in the decl-specifiers for DECLARATOR, an error is
2947 issued. TYPE_LOCATION is the location of TYPE and is used
2948 for error reporting. */
2951 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2952 tree type
, location_t type_location
)
2954 /* [dcl.fct] forbids type definitions in return types.
2955 Unfortunately, it's not easy to know whether or not we are
2956 processing a return type until after the fact. */
2958 && (declarator
->kind
== cdk_pointer
2959 || declarator
->kind
== cdk_reference
2960 || declarator
->kind
== cdk_ptrmem
))
2961 declarator
= declarator
->declarator
;
2963 && declarator
->kind
== cdk_function
)
2965 error_at (type_location
,
2966 "new types may not be defined in a return type");
2967 inform (type_location
,
2968 "(perhaps a semicolon is missing after the definition of %qT)",
2973 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2974 "<" in any valid C++ program. If the next token is indeed "<",
2975 issue a message warning the user about what appears to be an
2976 invalid attempt to form a template-id. LOCATION is the location
2977 of the type-specifier (TYPE) */
2980 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2982 enum tag_types tag_type
,
2983 location_t location
)
2985 cp_token_position start
= 0;
2987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2990 error_at (location
, "%qT is not a template", type
);
2991 else if (identifier_p (type
))
2993 if (tag_type
!= none_type
)
2994 error_at (location
, "%qE is not a class template", type
);
2996 error_at (location
, "%qE is not a template", type
);
2999 error_at (location
, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3002 start
= cp_lexer_token_position (parser
->lexer
, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser
->lexer
);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser
);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3010 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3020 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3021 non_integral_constant thing
)
3023 parser
->non_integral_constant_expression_p
= true;
3024 if (parser
->integral_constant_expression_p
)
3026 if (!parser
->allow_non_integral_constant_expression_p
)
3028 const char *msg
= NULL
;
3032 pedwarn (input_location
, OPT_Wpedantic
,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3065 case NIC_ADDR_LABEL
:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3069 case NIC_OVERLOADED
:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3073 case NIC_ASSIGNMENT
:
3074 error ("an assignment cannot appear in a constant-expression");
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3080 case NIC_CONSTRUCTOR
:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3084 case NIC_TRANSACTION
:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3092 msg
= "__FUNCTION__";
3094 case NIC_PRETTY_FUNC
:
3095 msg
= "__PRETTY_FUNCTION__";
3115 case NIC_PREINCREMENT
:
3118 case NIC_PREDECREMENT
:
3131 error ("%qs cannot appear in a constant-expression", msg
);
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3144 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3145 location_t location
)
3147 tree decl
, ambiguous_decls
;
3148 cp_parser_commit_to_tentative_parse (parser
);
3149 /* Try to lookup the identifier. */
3150 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls
, location
);
3155 if (ambiguous_decls
)
3156 /* If the lookup was ambiguous, an error will already have
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl
))
3164 "invalid use of template-name %qE without an argument list",
3166 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx1z
)
3167 inform (location
, "class template argument deduction is only available "
3168 "with -std=c++1z or -std=gnu++1z");
3169 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3171 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3172 error_at (location
, "invalid use of destructor %qD as a type", id
);
3173 else if (TREE_CODE (decl
) == TYPE_DECL
)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location
, "invalid combination of multiple type-specifiers");
3176 else if (!parser
->scope
)
3178 /* Issue an error message. */
3179 const char *suggestion
= NULL
;
3180 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3181 suggestion
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
);
3184 gcc_rich_location
richloc (location
);
3185 richloc
.add_fixit_replace (suggestion
);
3186 error_at_rich_loc (&richloc
,
3187 "%qE does not name a type; did you mean %qs?",
3191 error_at (location
, "%qE does not name a type", id
);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3200 inform (location
, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3203 inform (location
, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect
< cxx11
3206 && TREE_CODE (id
) == IDENTIFIER_NODE
3207 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
3208 inform (location
, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3211 inform (location
, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl
&& current_class_type
3213 && TYPE_BINFO (current_class_type
))
3217 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3221 tree base_type
= BINFO_TYPE (b
);
3222 if (CLASS_TYPE_P (base_type
)
3223 && dependent_type_p (base_type
))
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3230 for (field
= TYPE_FIELDS (base_type
);
3232 field
= DECL_CHAIN (field
))
3233 if (TREE_CODE (field
) == TYPE_DECL
3234 && DECL_NAME (field
) == id
)
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b
), id
);
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser
->scope
!= error_mark_node
)
3251 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3254 error_at (location_of (id
),
3255 "%qE in namespace %qE does not name a template type",
3258 error_at (location_of (id
),
3259 "%qE in namespace %qE does not name a type",
3262 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3264 else if (CLASS_TYPE_P (parser
->scope
)
3265 && constructor_name_p (id
, parser
->scope
))
3268 error_at (location
, "%<%T::%E%> names the constructor, not"
3269 " the type", parser
->scope
, id
);
3270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3271 error_at (location
, "and %qT has no template constructors",
3274 else if (TYPE_P (parser
->scope
)
3275 && dependent_scope_p (parser
->scope
))
3276 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3277 "%qT is a dependent scope",
3278 parser
->scope
, id
, parser
->scope
);
3279 else if (TYPE_P (parser
->scope
))
3281 if (!COMPLETE_TYPE_P (parser
->scope
))
3282 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3284 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3285 error_at (location_of (id
),
3286 "%qE in %q#T does not name a template type",
3289 error_at (location_of (id
),
3290 "%qE in %q#T does not name a type",
3293 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3300 /* Check for a common situation where a type-name should be present,
3301 but is not, and issue a sensible error message. Returns true if an
3302 invalid type-name was detected.
3304 The situation handled by this function are variable declarations of the
3305 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3306 Usually, `ID' should name a type, but if we got here it means that it
3307 does not. We try to emit the best possible error message depending on
3308 how exactly the id-expression looks like. */
3311 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3314 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3316 /* Avoid duplicate error about ambiguous lookup. */
3317 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3319 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3320 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3324 cp_parser_parse_tentatively (parser
);
3325 id
= cp_parser_id_expression (parser
,
3326 /*template_keyword_p=*/false,
3327 /*check_dependency_p=*/true,
3328 /*template_p=*/NULL
,
3329 /*declarator_p=*/true,
3330 /*optional_p=*/false);
3331 /* If the next token is a (, this is a function with no explicit return
3332 type, i.e. constructor, destructor or conversion op. */
3333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3334 || TREE_CODE (id
) == TYPE_DECL
)
3336 cp_parser_abort_tentative_parse (parser
);
3339 if (!cp_parser_parse_definitely (parser
))
3342 /* Emit a diagnostic for the invalid type. */
3343 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3345 /* If we aren't in the middle of a declarator (i.e. in a
3346 parameter-declaration-clause), skip to the end of the declaration;
3347 there's no point in trying to process it. */
3348 if (!parser
->in_declarator_p
)
3349 cp_parser_skip_to_end_of_block_or_statement (parser
);
3353 /* Consume tokens up to, and including, the next non-nested closing `)'.
3354 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3355 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3356 found an unnested token of that type. */
3359 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3364 unsigned paren_depth
= 0;
3365 unsigned brace_depth
= 0;
3366 unsigned square_depth
= 0;
3368 if (recovering
&& or_ttype
== CPP_EOF
3369 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3374 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3376 /* Have we found what we're looking for before the closing paren? */
3377 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3378 && !brace_depth
&& !paren_depth
&& !square_depth
)
3381 switch (token
->type
)
3384 case CPP_PRAGMA_EOL
:
3385 /* If we've run out of tokens, then there is no closing `)'. */
3388 /* This is good for lambda expression capture-lists. */
3389 case CPP_OPEN_SQUARE
:
3392 case CPP_CLOSE_SQUARE
:
3393 if (!square_depth
--)
3398 /* This matches the processing in skip_to_end_of_statement. */
3403 case CPP_OPEN_BRACE
:
3406 case CPP_CLOSE_BRACE
:
3411 case CPP_OPEN_PAREN
:
3416 case CPP_CLOSE_PAREN
:
3417 if (!brace_depth
&& !paren_depth
--)
3420 cp_lexer_consume_token (parser
->lexer
);
3429 /* Consume the token. */
3430 cp_lexer_consume_token (parser
->lexer
);
3434 /* Consume tokens up to, and including, the next non-nested closing `)'.
3435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3436 are doing error recovery. Returns -1 if OR_COMMA is true and we
3437 found an unnested token of that type. */
3440 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3445 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3446 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3447 ttype
, consume_paren
);
3450 /* Consume tokens until we reach the end of the current statement.
3451 Normally, that will be just before consuming a `;'. However, if a
3452 non-nested `}' comes first, then we stop before consuming that. */
3455 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3457 unsigned nesting_depth
= 0;
3459 /* Unwind generic function template scope if necessary. */
3460 if (parser
->fully_implicit_function_template_p
)
3461 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3465 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3467 switch (token
->type
)
3470 case CPP_PRAGMA_EOL
:
3471 /* If we've run out of tokens, stop. */
3475 /* If the next token is a `;', we have reached the end of the
3481 case CPP_CLOSE_BRACE
:
3482 /* If this is a non-nested '}', stop before consuming it.
3483 That way, when confronted with something like:
3487 we stop before consuming the closing '}', even though we
3488 have not yet reached a `;'. */
3489 if (nesting_depth
== 0)
3492 /* If it is the closing '}' for a block that we have
3493 scanned, stop -- but only after consuming the token.
3499 we will stop after the body of the erroneously declared
3500 function, but before consuming the following `typedef'
3502 if (--nesting_depth
== 0)
3504 cp_lexer_consume_token (parser
->lexer
);
3509 case CPP_OPEN_BRACE
:
3517 /* Consume the token. */
3518 cp_lexer_consume_token (parser
->lexer
);
3522 /* This function is called at the end of a statement or declaration.
3523 If the next token is a semicolon, it is consumed; otherwise, error
3524 recovery is attempted. */
3527 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3529 /* Look for the trailing `;'. */
3530 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3532 /* If there is additional (erroneous) input, skip to the end of
3534 cp_parser_skip_to_end_of_statement (parser
);
3535 /* If the next token is now a `;', consume it. */
3536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3537 cp_lexer_consume_token (parser
->lexer
);
3541 /* Skip tokens until we have consumed an entire block, or until we
3542 have consumed a non-nested `;'. */
3545 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3547 int nesting_depth
= 0;
3549 /* Unwind generic function template scope if necessary. */
3550 if (parser
->fully_implicit_function_template_p
)
3551 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3553 while (nesting_depth
>= 0)
3555 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3557 switch (token
->type
)
3560 case CPP_PRAGMA_EOL
:
3561 /* If we've run out of tokens, stop. */
3565 /* Stop if this is an unnested ';'. */
3570 case CPP_CLOSE_BRACE
:
3571 /* Stop if this is an unnested '}', or closes the outermost
3574 if (nesting_depth
< 0)
3580 case CPP_OPEN_BRACE
:
3589 /* Consume the token. */
3590 cp_lexer_consume_token (parser
->lexer
);
3594 /* Skip tokens until a non-nested closing curly brace is the next
3595 token, or there are no more tokens. Return true in the first case,
3599 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3601 unsigned nesting_depth
= 0;
3605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3607 switch (token
->type
)
3610 case CPP_PRAGMA_EOL
:
3611 /* If we've run out of tokens, stop. */
3614 case CPP_CLOSE_BRACE
:
3615 /* If the next token is a non-nested `}', then we have reached
3616 the end of the current block. */
3617 if (nesting_depth
-- == 0)
3621 case CPP_OPEN_BRACE
:
3622 /* If it the next token is a `{', then we are entering a new
3623 block. Consume the entire block. */
3631 /* Consume the token. */
3632 cp_lexer_consume_token (parser
->lexer
);
3636 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3637 parameter is the PRAGMA token, allowing us to purge the entire pragma
3641 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3645 parser
->lexer
->in_pragma
= false;
3648 token
= cp_lexer_consume_token (parser
->lexer
);
3649 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3651 /* Ensure that the pragma is not parsed again. */
3652 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3655 /* Require pragma end of line, resyncing with it as necessary. The
3656 arguments are as for cp_parser_skip_to_pragma_eol. */
3659 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3661 parser
->lexer
->in_pragma
= false;
3662 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3663 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3666 /* This is a simple wrapper around make_typename_type. When the id is
3667 an unresolved identifier node, we can provide a superior diagnostic
3668 using cp_parser_diagnose_invalid_type_name. */
3671 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3672 location_t id_location
)
3675 if (identifier_p (id
))
3677 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3678 /*complain=*/tf_none
);
3679 if (result
== error_mark_node
)
3680 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3683 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3686 /* This is a wrapper around the
3687 make_{pointer,ptrmem,reference}_declarator functions that decides
3688 which one to call based on the CODE and CLASS_TYPE arguments. The
3689 CODE argument should be one of the values returned by
3690 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3691 appertain to the pointer or reference. */
3693 static cp_declarator
*
3694 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3695 cp_cv_quals cv_qualifiers
,
3696 cp_declarator
*target
,
3699 if (code
== ERROR_MARK
)
3700 return cp_error_declarator
;
3702 if (code
== INDIRECT_REF
)
3703 if (class_type
== NULL_TREE
)
3704 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3706 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3707 target
, attributes
);
3708 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3709 return make_reference_declarator (cv_qualifiers
, target
,
3711 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3712 return make_reference_declarator (cv_qualifiers
, target
,
3717 /* Create a new C++ parser. */
3720 cp_parser_new (void)
3726 /* cp_lexer_new_main is called before doing GC allocation because
3727 cp_lexer_new_main might load a PCH file. */
3728 lexer
= cp_lexer_new_main ();
3730 /* Initialize the binops_by_token so that we can get the tree
3731 directly from the token. */
3732 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3733 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3735 parser
= ggc_cleared_alloc
<cp_parser
> ();
3736 parser
->lexer
= lexer
;
3737 parser
->context
= cp_parser_context_new (NULL
);
3739 /* For now, we always accept GNU extensions. */
3740 parser
->allow_gnu_extensions_p
= 1;
3742 /* The `>' token is a greater-than operator, not the end of a
3744 parser
->greater_than_is_operator_p
= true;
3746 parser
->default_arg_ok_p
= true;
3748 /* We are not parsing a constant-expression. */
3749 parser
->integral_constant_expression_p
= false;
3750 parser
->allow_non_integral_constant_expression_p
= false;
3751 parser
->non_integral_constant_expression_p
= false;
3753 /* Local variable names are not forbidden. */
3754 parser
->local_variables_forbidden_p
= false;
3756 /* We are not processing an `extern "C"' declaration. */
3757 parser
->in_unbraced_linkage_specification_p
= false;
3759 /* We are not processing a declarator. */
3760 parser
->in_declarator_p
= false;
3762 /* We are not processing a template-argument-list. */
3763 parser
->in_template_argument_list_p
= false;
3765 /* We are not in an iteration statement. */
3766 parser
->in_statement
= 0;
3768 /* We are not in a switch statement. */
3769 parser
->in_switch_statement_p
= false;
3771 /* We are not parsing a type-id inside an expression. */
3772 parser
->in_type_id_in_expr_p
= false;
3774 /* Declarations aren't implicitly extern "C". */
3775 parser
->implicit_extern_c
= false;
3777 /* String literals should be translated to the execution character set. */
3778 parser
->translate_strings_p
= true;
3780 /* We are not parsing a function body. */
3781 parser
->in_function_body
= false;
3783 /* We can correct until told otherwise. */
3784 parser
->colon_corrects_to_scope_p
= true;
3786 /* The unparsed function queue is empty. */
3787 push_unparsed_function_queues (parser
);
3789 /* There are no classes being defined. */
3790 parser
->num_classes_being_defined
= 0;
3792 /* No template parameters apply. */
3793 parser
->num_template_parameter_lists
= 0;
3795 /* Special parsing data structures. */
3796 parser
->omp_declare_simd
= NULL
;
3797 parser
->cilk_simd_fn_info
= NULL
;
3798 parser
->oacc_routine
= NULL
;
3800 /* Not declaring an implicit function template. */
3801 parser
->auto_is_implicit_function_template_parm_p
= false;
3802 parser
->fully_implicit_function_template_p
= false;
3803 parser
->implicit_template_parms
= 0;
3804 parser
->implicit_template_scope
= 0;
3806 /* Allow constrained-type-specifiers. */
3807 parser
->prevent_constrained_type_specifiers
= 0;
3812 /* Create a cp_lexer structure which will emit the tokens in CACHE
3813 and push it onto the parser's lexer stack. This is used for delayed
3814 parsing of in-class method bodies and default arguments, and should
3815 not be confused with tentative parsing. */
3817 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3819 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3820 lexer
->next
= parser
->lexer
;
3821 parser
->lexer
= lexer
;
3823 /* Move the current source position to that of the first token in the
3825 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3828 /* Pop the top lexer off the parser stack. This is never used for the
3829 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3831 cp_parser_pop_lexer (cp_parser
*parser
)
3833 cp_lexer
*lexer
= parser
->lexer
;
3834 parser
->lexer
= lexer
->next
;
3835 cp_lexer_destroy (lexer
);
3837 /* Put the current source position back where it was before this
3838 lexer was pushed. */
3839 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3842 /* Lexical conventions [gram.lex] */
3844 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3848 cp_parser_identifier (cp_parser
* parser
)
3852 /* Look for the identifier. */
3853 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3854 /* Return the value. */
3856 return cp_expr (token
->u
.value
, token
->location
);
3858 return error_mark_node
;
3861 /* Parse a sequence of adjacent string constants. Returns a
3862 TREE_STRING representing the combined, nul-terminated string
3863 constant. If TRANSLATE is true, translate the string to the
3864 execution character set. If WIDE_OK is true, a wide string is
3867 C++98 [lex.string] says that if a narrow string literal token is
3868 adjacent to a wide string literal token, the behavior is undefined.
3869 However, C99 6.4.5p4 says that this results in a wide string literal.
3870 We follow C99 here, for consistency with the C front end.
3872 This code is largely lifted from lex_string() in c-lex.c.
3874 FUTURE: ObjC++ will need to handle @-strings here. */
3876 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3877 bool lookup_udlit
= true)
3881 struct obstack str_ob
;
3882 cpp_string str
, istr
, *strs
;
3884 enum cpp_ttype type
, curr_type
;
3885 int have_suffix_p
= 0;
3887 tree suffix_id
= NULL_TREE
;
3888 bool curr_tok_is_userdef_p
= false;
3890 tok
= cp_lexer_peek_token (parser
->lexer
);
3891 if (!cp_parser_is_string_literal (tok
))
3893 cp_parser_error (parser
, "expected string-literal");
3894 return error_mark_node
;
3897 location_t loc
= tok
->location
;
3899 if (cpp_userdef_string_p (tok
->type
))
3901 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3902 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3903 curr_tok_is_userdef_p
= true;
3907 string_tree
= tok
->u
.value
;
3908 curr_type
= tok
->type
;
3912 /* Try to avoid the overhead of creating and destroying an obstack
3913 for the common case of just one string. */
3914 if (!cp_parser_is_string_literal
3915 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3917 cp_lexer_consume_token (parser
->lexer
);
3919 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3920 str
.len
= TREE_STRING_LENGTH (string_tree
);
3923 if (curr_tok_is_userdef_p
)
3925 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3927 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3930 curr_type
= tok
->type
;
3936 location_t last_tok_loc
= tok
->location
;
3937 gcc_obstack_init (&str_ob
);
3942 cp_lexer_consume_token (parser
->lexer
);
3944 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3945 str
.len
= TREE_STRING_LENGTH (string_tree
);
3947 if (curr_tok_is_userdef_p
)
3949 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3950 if (have_suffix_p
== 0)
3952 suffix_id
= curr_suffix_id
;
3955 else if (have_suffix_p
== 1
3956 && curr_suffix_id
!= suffix_id
)
3958 error ("inconsistent user-defined literal suffixes"
3959 " %qD and %qD in string literal",
3960 suffix_id
, curr_suffix_id
);
3963 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3966 curr_type
= tok
->type
;
3968 if (type
!= curr_type
)
3970 if (type
== CPP_STRING
)
3972 else if (curr_type
!= CPP_STRING
)
3974 rich_location
rich_loc (line_table
, tok
->location
);
3975 rich_loc
.add_range (last_tok_loc
, false);
3976 error_at_rich_loc (&rich_loc
,
3977 "unsupported non-standard concatenation "
3978 "of string literals");
3982 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3984 last_tok_loc
= tok
->location
;
3986 tok
= cp_lexer_peek_token (parser
->lexer
);
3987 if (cpp_userdef_string_p (tok
->type
))
3989 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3990 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3991 curr_tok_is_userdef_p
= true;
3995 string_tree
= tok
->u
.value
;
3996 curr_type
= tok
->type
;
3997 curr_tok_is_userdef_p
= false;
4000 while (cp_parser_is_string_literal (tok
));
4002 /* A string literal built by concatenation has its caret=start at
4003 the start of the initial string, and its finish at the finish of
4004 the final string literal. */
4005 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4007 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4010 if (type
!= CPP_STRING
&& !wide_ok
)
4012 cp_parser_error (parser
, "a wide string is invalid in this context");
4016 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4017 (parse_in
, strs
, count
, &istr
, type
))
4019 value
= build_string (istr
.len
, (const char *)istr
.text
);
4020 free (CONST_CAST (unsigned char *, istr
.text
));
4026 case CPP_UTF8STRING
:
4027 TREE_TYPE (value
) = char_array_type_node
;
4030 TREE_TYPE (value
) = char16_array_type_node
;
4033 TREE_TYPE (value
) = char32_array_type_node
;
4036 TREE_TYPE (value
) = wchar_array_type_node
;
4040 value
= fix_string_type (value
);
4044 tree literal
= build_userdef_literal (suffix_id
, value
,
4045 OT_NONE
, NULL_TREE
);
4047 value
= cp_parser_userdef_string_literal (literal
);
4053 /* cpp_interpret_string has issued an error. */
4054 value
= error_mark_node
;
4057 obstack_free (&str_ob
, 0);
4059 return cp_expr (value
, loc
);
4062 /* Look up a literal operator with the name and the exact arguments. */
4065 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4068 decl
= lookup_name (name
);
4069 if (!decl
|| !is_overloaded_fn (decl
))
4070 return error_mark_node
;
4072 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
4076 tree fn
= OVL_CURRENT (fns
);
4077 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4078 if (parmtypes
!= NULL_TREE
)
4080 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4081 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4083 tree tparm
= TREE_VALUE (parmtypes
);
4084 tree targ
= TREE_TYPE ((*args
)[ix
]);
4085 bool ptr
= TYPE_PTR_P (tparm
);
4086 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4087 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4089 || !same_type_p (TREE_TYPE (tparm
),
4094 && ix
== vec_safe_length (args
)
4095 /* May be this should be sufficient_parms_p instead,
4096 depending on how exactly should user-defined literals
4097 work in presence of default arguments on the literal
4098 operator parameters. */
4099 && parmtypes
== void_list_node
)
4104 return error_mark_node
;
4107 /* Parse a user-defined char constant. Returns a call to a user-defined
4108 literal operator taking the character as an argument. */
4111 cp_parser_userdef_char_literal (cp_parser
*parser
)
4113 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4114 tree literal
= token
->u
.value
;
4115 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4116 tree value
= USERDEF_LITERAL_VALUE (literal
);
4117 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4120 /* Build up a call to the user-defined operator */
4121 /* Lookup the name we got back from the id-expression. */
4122 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4123 vec_safe_push (args
, value
);
4124 decl
= lookup_literal_operator (name
, args
);
4125 if (!decl
|| decl
== error_mark_node
)
4127 error ("unable to find character literal operator %qD with %qT argument",
4128 name
, TREE_TYPE (value
));
4129 release_tree_vector (args
);
4130 return error_mark_node
;
4132 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4133 release_tree_vector (args
);
4137 /* A subroutine of cp_parser_userdef_numeric_literal to
4138 create a char... template parameter pack from a string node. */
4141 make_char_string_pack (tree value
)
4144 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4145 const char *str
= TREE_STRING_POINTER (value
);
4146 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4147 tree argvec
= make_tree_vec (1);
4149 /* Fill in CHARVEC with all of the parameters. */
4150 charvec
= make_tree_vec (len
);
4151 for (i
= 0; i
< len
; ++i
)
4152 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4154 /* Build the argument packs. */
4155 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4156 TREE_TYPE (argpack
) = char_type_node
;
4158 TREE_VEC_ELT (argvec
, 0) = argpack
;
4163 /* A subroutine of cp_parser_userdef_numeric_literal to
4164 create a char... template parameter pack from a string node. */
4167 make_string_pack (tree value
)
4170 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4171 const unsigned char *str
4172 = (const unsigned char *) TREE_STRING_POINTER (value
);
4173 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4174 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4175 tree argvec
= make_tree_vec (2);
4177 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4178 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4180 /* First template parm is character type. */
4181 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4183 /* Fill in CHARVEC with all of the parameters. */
4184 charvec
= make_tree_vec (len
);
4185 for (int i
= 0; i
< len
; ++i
)
4186 TREE_VEC_ELT (charvec
, i
)
4187 = double_int_to_tree (str_char_type_node
,
4188 double_int::from_buffer (str
+ i
* sz
, sz
));
4190 /* Build the argument packs. */
4191 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4192 TREE_TYPE (argpack
) = str_char_type_node
;
4194 TREE_VEC_ELT (argvec
, 1) = argpack
;
4199 /* Parse a user-defined numeric constant. returns a call to a user-defined
4200 literal operator. */
4203 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4205 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4206 tree literal
= token
->u
.value
;
4207 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4208 tree value
= USERDEF_LITERAL_VALUE (literal
);
4209 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4210 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4211 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4213 vec
<tree
, va_gc
> *args
;
4215 /* Look for a literal operator taking the exact type of numeric argument
4216 as the literal value. */
4217 args
= make_tree_vector ();
4218 vec_safe_push (args
, value
);
4219 decl
= lookup_literal_operator (name
, args
);
4220 if (decl
&& decl
!= error_mark_node
)
4222 result
= finish_call_expr (decl
, &args
, false, true,
4223 tf_warning_or_error
);
4225 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4227 warning_at (token
->location
, OPT_Woverflow
,
4228 "integer literal exceeds range of %qT type",
4229 long_long_unsigned_type_node
);
4234 warning_at (token
->location
, OPT_Woverflow
,
4235 "floating literal exceeds range of %qT type",
4236 long_double_type_node
);
4237 else if (overflow
< 0)
4238 warning_at (token
->location
, OPT_Woverflow
,
4239 "floating literal truncated to zero");
4242 release_tree_vector (args
);
4245 release_tree_vector (args
);
4247 /* If the numeric argument didn't work, look for a raw literal
4248 operator taking a const char* argument consisting of the number
4249 in string format. */
4250 args
= make_tree_vector ();
4251 vec_safe_push (args
, num_string
);
4252 decl
= lookup_literal_operator (name
, args
);
4253 if (decl
&& decl
!= error_mark_node
)
4255 result
= finish_call_expr (decl
, &args
, false, true,
4256 tf_warning_or_error
);
4257 release_tree_vector (args
);
4260 release_tree_vector (args
);
4262 /* If the raw literal didn't work, look for a non-type template
4263 function with parameter pack char.... Call the function with
4264 template parameter characters representing the number. */
4265 args
= make_tree_vector ();
4266 decl
= lookup_literal_operator (name
, args
);
4267 if (decl
&& decl
!= error_mark_node
)
4269 tree tmpl_args
= make_char_string_pack (num_string
);
4270 decl
= lookup_template_function (decl
, tmpl_args
);
4271 result
= finish_call_expr (decl
, &args
, false, true,
4272 tf_warning_or_error
);
4273 release_tree_vector (args
);
4277 release_tree_vector (args
);
4279 error ("unable to find numeric literal operator %qD", name
);
4280 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4281 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4282 "to enable more built-in suffixes");
4283 return error_mark_node
;
4286 /* Parse a user-defined string constant. Returns a call to a user-defined
4287 literal operator taking a character pointer and the length of the string
4291 cp_parser_userdef_string_literal (tree literal
)
4293 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4294 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4295 tree value
= USERDEF_LITERAL_VALUE (literal
);
4296 int len
= TREE_STRING_LENGTH (value
)
4297 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4299 vec
<tree
, va_gc
> *args
;
4301 /* Build up a call to the user-defined operator. */
4302 /* Lookup the name we got back from the id-expression. */
4303 args
= make_tree_vector ();
4304 vec_safe_push (args
, value
);
4305 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4306 decl
= lookup_literal_operator (name
, args
);
4308 if (decl
&& decl
!= error_mark_node
)
4310 result
= finish_call_expr (decl
, &args
, false, true,
4311 tf_warning_or_error
);
4312 release_tree_vector (args
);
4315 release_tree_vector (args
);
4317 /* Look for a template function with typename parameter CharT
4318 and parameter pack CharT... Call the function with
4319 template parameter characters representing the string. */
4320 args
= make_tree_vector ();
4321 decl
= lookup_literal_operator (name
, args
);
4322 if (decl
&& decl
!= error_mark_node
)
4324 tree tmpl_args
= make_string_pack (value
);
4325 decl
= lookup_template_function (decl
, tmpl_args
);
4326 result
= finish_call_expr (decl
, &args
, false, true,
4327 tf_warning_or_error
);
4328 release_tree_vector (args
);
4331 release_tree_vector (args
);
4333 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4334 name
, TREE_TYPE (value
), size_type_node
);
4335 return error_mark_node
;
4339 /* Basic concepts [gram.basic] */
4341 /* Parse a translation-unit.
4344 declaration-seq [opt]
4346 Returns TRUE if all went well. */
4349 cp_parser_translation_unit (cp_parser
* parser
)
4351 /* The address of the first non-permanent object on the declarator
4353 static void *declarator_obstack_base
;
4357 /* Create the declarator obstack, if necessary. */
4358 if (!cp_error_declarator
)
4360 gcc_obstack_init (&declarator_obstack
);
4361 /* Create the error declarator. */
4362 cp_error_declarator
= make_declarator (cdk_error
);
4363 /* Create the empty parameter list. */
4364 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4365 /* Remember where the base of the declarator obstack lies. */
4366 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4369 cp_parser_declaration_seq_opt (parser
);
4371 /* If there are no tokens left then all went well. */
4372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4374 /* Get rid of the token array; we don't need it any more. */
4375 cp_lexer_destroy (parser
->lexer
);
4376 parser
->lexer
= NULL
;
4378 /* This file might have been a context that's implicitly extern
4379 "C". If so, pop the lang context. (Only relevant for PCH.) */
4380 if (parser
->implicit_extern_c
)
4382 pop_lang_context ();
4383 parser
->implicit_extern_c
= false;
4387 finish_translation_unit ();
4393 cp_parser_error (parser
, "expected declaration");
4397 /* Make sure the declarator obstack was fully cleaned up. */
4398 gcc_assert (obstack_next_free (&declarator_obstack
)
4399 == declarator_obstack_base
);
4401 /* All went well. */
4405 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4406 decltype context. */
4408 static inline tsubst_flags_t
4409 complain_flags (bool decltype_p
)
4411 tsubst_flags_t complain
= tf_warning_or_error
;
4413 complain
|= tf_decltype
;
4417 /* We're about to parse a collection of statements. If we're currently
4418 parsing tentatively, set up a firewall so that any nested
4419 cp_parser_commit_to_tentative_parse won't affect the current context. */
4421 static cp_token_position
4422 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4427 cp_parser_parse_tentatively (parser
);
4428 cp_parser_commit_to_topmost_tentative_parse (parser
);
4429 return cp_lexer_token_position (parser
->lexer
, false);
4432 /* We've finished parsing the collection of statements. Wrap up the
4433 firewall and replace the relevant tokens with the parsed form. */
4436 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4442 /* Finish the firewall level. */
4443 cp_parser_parse_definitely (parser
);
4444 /* And remember the result of the parse for when we try again. */
4445 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4446 token
->type
= CPP_PREPARSED_EXPR
;
4447 token
->u
.value
= expr
;
4448 token
->keyword
= RID_MAX
;
4449 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4452 /* Like the above functions, but let the user modify the tokens. Used by
4453 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4454 later parses, so it makes sense to localize the effects of
4455 cp_parser_commit_to_tentative_parse. */
4457 struct tentative_firewall
4462 tentative_firewall (cp_parser
*p
): parser(p
)
4464 /* If we're currently parsing tentatively, start a committed level as a
4465 firewall and then an inner tentative parse. */
4466 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4468 cp_parser_parse_tentatively (parser
);
4469 cp_parser_commit_to_topmost_tentative_parse (parser
);
4470 cp_parser_parse_tentatively (parser
);
4474 ~tentative_firewall()
4478 /* Finish the inner tentative parse and the firewall, propagating any
4479 uncommitted error state to the outer tentative parse. */
4480 bool err
= cp_parser_error_occurred (parser
);
4481 cp_parser_parse_definitely (parser
);
4482 cp_parser_parse_definitely (parser
);
4484 cp_parser_simulate_error (parser
);
4489 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4490 enclosing parentheses. */
4493 cp_parser_statement_expr (cp_parser
*parser
)
4495 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4497 /* Consume the '('. */
4498 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4499 cp_lexer_consume_token (parser
->lexer
);
4500 /* Start the statement-expression. */
4501 tree expr
= begin_stmt_expr ();
4502 /* Parse the compound-statement. */
4503 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4505 expr
= finish_stmt_expr (expr
, false);
4506 /* Consume the ')'. */
4507 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4508 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4509 cp_parser_skip_to_end_of_statement (parser
);
4511 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4512 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4513 return cp_expr (expr
, combined_loc
);
4516 /* Expressions [gram.expr] */
4518 /* Parse a fold-operator.
4521 - * / % ^ & | = < > << >>
4522 = -= *= /= %= ^= &= |= <<= >>=
4523 == != <= >= && || , .* ->*
4525 This returns the tree code corresponding to the matched operator
4526 as an int. When the current token matches a compound assignment
4527 opertor, the resulting tree code is the negative value of the
4528 non-assignment operator. */
4531 cp_parser_fold_operator (cp_token
*token
)
4533 switch (token
->type
)
4535 case CPP_PLUS
: return PLUS_EXPR
;
4536 case CPP_MINUS
: return MINUS_EXPR
;
4537 case CPP_MULT
: return MULT_EXPR
;
4538 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4539 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4540 case CPP_XOR
: return BIT_XOR_EXPR
;
4541 case CPP_AND
: return BIT_AND_EXPR
;
4542 case CPP_OR
: return BIT_IOR_EXPR
;
4543 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4544 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4546 case CPP_EQ
: return -NOP_EXPR
;
4547 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4548 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4549 case CPP_MULT_EQ
: return -MULT_EXPR
;
4550 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4551 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4552 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4553 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4554 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4555 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4556 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4558 case CPP_EQ_EQ
: return EQ_EXPR
;
4559 case CPP_NOT_EQ
: return NE_EXPR
;
4560 case CPP_LESS
: return LT_EXPR
;
4561 case CPP_GREATER
: return GT_EXPR
;
4562 case CPP_LESS_EQ
: return LE_EXPR
;
4563 case CPP_GREATER_EQ
: return GE_EXPR
;
4565 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4566 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4568 case CPP_COMMA
: return COMPOUND_EXPR
;
4570 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4571 case CPP_DEREF_STAR
: return MEMBER_REF
;
4573 default: return ERROR_MARK
;
4577 /* Returns true if CODE indicates a binary expression, which is not allowed in
4578 the LHS of a fold-expression. More codes will need to be added to use this
4579 function in other contexts. */
4582 is_binary_op (tree_code code
)
4587 case POINTER_PLUS_EXPR
:
4590 case TRUNC_DIV_EXPR
:
4591 case TRUNC_MOD_EXPR
:
4607 case TRUTH_ANDIF_EXPR
:
4608 case TRUTH_ORIF_EXPR
:
4621 /* If the next token is a suitable fold operator, consume it and return as
4622 the function above. */
4625 cp_parser_fold_operator (cp_parser
*parser
)
4627 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4628 int code
= cp_parser_fold_operator (token
);
4629 if (code
!= ERROR_MARK
)
4630 cp_lexer_consume_token (parser
->lexer
);
4634 /* Parse a fold-expression.
4637 ( ... folding-operator cast-expression)
4638 ( cast-expression folding-operator ... )
4639 ( cast-expression folding operator ... folding-operator cast-expression)
4641 Note that the '(' and ')' are matched in primary expression. */
4644 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4651 cp_lexer_consume_token (parser
->lexer
);
4652 int op
= cp_parser_fold_operator (parser
);
4653 if (op
== ERROR_MARK
)
4655 cp_parser_error (parser
, "expected binary operator");
4656 return error_mark_node
;
4659 tree expr
= cp_parser_cast_expression (parser
, false, false,
4661 if (expr
== error_mark_node
)
4662 return error_mark_node
;
4663 return finish_left_unary_fold_expr (expr
, op
);
4666 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4667 int op
= cp_parser_fold_operator (parser
);
4668 if (op
== ERROR_MARK
)
4670 cp_parser_error (parser
, "expected binary operator");
4671 return error_mark_node
;
4674 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4676 cp_parser_error (parser
, "expected ...");
4677 return error_mark_node
;
4679 cp_lexer_consume_token (parser
->lexer
);
4681 /* The operands of a fold-expression are cast-expressions, so binary or
4682 conditional expressions are not allowed. We check this here to avoid
4683 tentative parsing. */
4684 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4685 /* OK, the expression was parenthesized. */;
4686 else if (is_binary_op (TREE_CODE (expr1
)))
4687 error_at (location_of (expr1
),
4688 "binary expression in operand of fold-expression");
4689 else if (TREE_CODE (expr1
) == COND_EXPR
)
4690 error_at (location_of (expr1
),
4691 "conditional expression in operand of fold-expression");
4694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4695 return finish_right_unary_fold_expr (expr1
, op
);
4697 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4699 cp_parser_error (parser
, "mismatched operator in fold-expression");
4700 return error_mark_node
;
4702 cp_lexer_consume_token (parser
->lexer
);
4704 // Binary left or right fold.
4705 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4706 if (expr2
== error_mark_node
)
4707 return error_mark_node
;
4708 return finish_binary_fold_expr (expr1
, expr2
, op
);
4711 /* Parse a primary-expression.
4718 lambda-expression (C++11)
4723 ( compound-statement )
4724 __builtin_va_arg ( assignment-expression , type-id )
4725 __builtin_offsetof ( type-id , offsetof-expression )
4728 __has_nothrow_assign ( type-id )
4729 __has_nothrow_constructor ( type-id )
4730 __has_nothrow_copy ( type-id )
4731 __has_trivial_assign ( type-id )
4732 __has_trivial_constructor ( type-id )
4733 __has_trivial_copy ( type-id )
4734 __has_trivial_destructor ( type-id )
4735 __has_virtual_destructor ( type-id )
4736 __is_abstract ( type-id )
4737 __is_base_of ( type-id , type-id )
4738 __is_class ( type-id )
4739 __is_empty ( type-id )
4740 __is_enum ( type-id )
4741 __is_final ( type-id )
4742 __is_literal_type ( type-id )
4743 __is_pod ( type-id )
4744 __is_polymorphic ( type-id )
4745 __is_std_layout ( type-id )
4746 __is_trivial ( type-id )
4747 __is_union ( type-id )
4749 Objective-C++ Extension:
4757 ADDRESS_P is true iff this expression was immediately preceded by
4758 "&" and therefore might denote a pointer-to-member. CAST_P is true
4759 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4760 true iff this expression is a template argument.
4762 Returns a representation of the expression. Upon return, *IDK
4763 indicates what kind of id-expression (if any) was present. */
4766 cp_parser_primary_expression (cp_parser
*parser
,
4769 bool template_arg_p
,
4773 cp_token
*token
= NULL
;
4775 /* Assume the primary expression is not an id-expression. */
4776 *idk
= CP_ID_KIND_NONE
;
4778 /* Peek at the next token. */
4779 token
= cp_lexer_peek_token (parser
->lexer
);
4780 switch ((int) token
->type
)
4789 user-defined-literal */
4796 case CPP_PREPARSED_EXPR
:
4797 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4798 return cp_parser_userdef_numeric_literal (parser
);
4799 token
= cp_lexer_consume_token (parser
->lexer
);
4800 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4802 error_at (token
->location
,
4803 "fixed-point types not supported in C++");
4804 return error_mark_node
;
4806 /* Floating-point literals are only allowed in an integral
4807 constant expression if they are cast to an integral or
4808 enumeration type. */
4809 if (TREE_CODE (token
->u
.value
) == REAL_CST
4810 && parser
->integral_constant_expression_p
4813 /* CAST_P will be set even in invalid code like "int(2.7 +
4814 ...)". Therefore, we have to check that the next token
4815 is sure to end the cast. */
4818 cp_token
*next_token
;
4820 next_token
= cp_lexer_peek_token (parser
->lexer
);
4821 if (/* The comma at the end of an
4822 enumerator-definition. */
4823 next_token
->type
!= CPP_COMMA
4824 /* The curly brace at the end of an enum-specifier. */
4825 && next_token
->type
!= CPP_CLOSE_BRACE
4826 /* The end of a statement. */
4827 && next_token
->type
!= CPP_SEMICOLON
4828 /* The end of the cast-expression. */
4829 && next_token
->type
!= CPP_CLOSE_PAREN
4830 /* The end of an array bound. */
4831 && next_token
->type
!= CPP_CLOSE_SQUARE
4832 /* The closing ">" in a template-argument-list. */
4833 && (next_token
->type
!= CPP_GREATER
4834 || parser
->greater_than_is_operator_p
)
4835 /* C++0x only: A ">>" treated like two ">" tokens,
4836 in a template-argument-list. */
4837 && (next_token
->type
!= CPP_RSHIFT
4838 || (cxx_dialect
== cxx98
)
4839 || parser
->greater_than_is_operator_p
))
4843 /* If we are within a cast, then the constraint that the
4844 cast is to an integral or enumeration type will be
4845 checked at that point. If we are not within a cast, then
4846 this code is invalid. */
4848 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4850 return cp_expr (token
->u
.value
, token
->location
);
4852 case CPP_CHAR_USERDEF
:
4853 case CPP_CHAR16_USERDEF
:
4854 case CPP_CHAR32_USERDEF
:
4855 case CPP_WCHAR_USERDEF
:
4856 case CPP_UTF8CHAR_USERDEF
:
4857 return cp_parser_userdef_char_literal (parser
);
4863 case CPP_UTF8STRING
:
4864 case CPP_STRING_USERDEF
:
4865 case CPP_STRING16_USERDEF
:
4866 case CPP_STRING32_USERDEF
:
4867 case CPP_WSTRING_USERDEF
:
4868 case CPP_UTF8STRING_USERDEF
:
4869 /* ??? Should wide strings be allowed when parser->translate_strings_p
4870 is false (i.e. in attributes)? If not, we can kill the third
4871 argument to cp_parser_string_literal. */
4872 return cp_parser_string_literal (parser
,
4873 parser
->translate_strings_p
,
4876 case CPP_OPEN_PAREN
:
4877 /* If we see `( { ' then we are looking at the beginning of
4878 a GNU statement-expression. */
4879 if (cp_parser_allow_gnu_extensions_p (parser
)
4880 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4882 /* Statement-expressions are not allowed by the standard. */
4883 pedwarn (token
->location
, OPT_Wpedantic
,
4884 "ISO C++ forbids braced-groups within expressions");
4886 /* And they're not allowed outside of a function-body; you
4887 cannot, for example, write:
4889 int i = ({ int j = 3; j + 1; });
4891 at class or namespace scope. */
4892 if (!parser
->in_function_body
4893 || parser
->in_template_argument_list_p
)
4895 error_at (token
->location
,
4896 "statement-expressions are not allowed outside "
4897 "functions nor in template-argument lists");
4898 cp_parser_skip_to_end_of_block_or_statement (parser
);
4899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4900 cp_lexer_consume_token (parser
->lexer
);
4901 return error_mark_node
;
4904 return cp_parser_statement_expr (parser
);
4906 /* Otherwise it's a normal parenthesized expression. */
4909 bool saved_greater_than_is_operator_p
;
4911 location_t open_paren_loc
= token
->location
;
4913 /* Consume the `('. */
4914 cp_lexer_consume_token (parser
->lexer
);
4915 /* Within a parenthesized expression, a `>' token is always
4916 the greater-than operator. */
4917 saved_greater_than_is_operator_p
4918 = parser
->greater_than_is_operator_p
;
4919 parser
->greater_than_is_operator_p
= true;
4921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4922 /* Left fold expression. */
4925 /* Parse the parenthesized expression. */
4926 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4928 token
= cp_lexer_peek_token (parser
->lexer
);
4929 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
4931 expr
= cp_parser_fold_expression (parser
, expr
);
4932 if (expr
!= error_mark_node
4933 && cxx_dialect
< cxx1z
4934 && !in_system_header_at (input_location
))
4935 pedwarn (input_location
, 0, "fold-expressions only available "
4936 "with -std=c++1z or -std=gnu++1z");
4939 /* Let the front end know that this expression was
4940 enclosed in parentheses. This matters in case, for
4941 example, the expression is of the form `A::B', since
4942 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4944 expr
= finish_parenthesized_expr (expr
);
4946 /* DR 705: Wrapping an unqualified name in parentheses
4947 suppresses arg-dependent lookup. We want to pass back
4948 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4949 (c++/37862), but none of the others. */
4950 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4951 *idk
= CP_ID_KIND_NONE
;
4953 /* The `>' token might be the end of a template-id or
4954 template-parameter-list now. */
4955 parser
->greater_than_is_operator_p
4956 = saved_greater_than_is_operator_p
;
4958 /* Consume the `)'. */
4959 token
= cp_lexer_peek_token (parser
->lexer
);
4960 location_t close_paren_loc
= token
->location
;
4961 expr
.set_range (open_paren_loc
, close_paren_loc
);
4962 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
)
4963 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4964 cp_parser_skip_to_end_of_statement (parser
);
4969 case CPP_OPEN_SQUARE
:
4971 if (c_dialect_objc ())
4973 /* We might have an Objective-C++ message. */
4974 cp_parser_parse_tentatively (parser
);
4975 tree msg
= cp_parser_objc_message_expression (parser
);
4976 /* If that works out, we're done ... */
4977 if (cp_parser_parse_definitely (parser
))
4979 /* ... else, fall though to see if it's a lambda. */
4981 cp_expr lam
= cp_parser_lambda_expression (parser
);
4982 /* Don't warn about a failed tentative parse. */
4983 if (cp_parser_error_occurred (parser
))
4984 return error_mark_node
;
4985 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4989 case CPP_OBJC_STRING
:
4990 if (c_dialect_objc ())
4991 /* We have an Objective-C++ string literal. */
4992 return cp_parser_objc_expression (parser
);
4993 cp_parser_error (parser
, "expected primary-expression");
4994 return error_mark_node
;
4997 switch (token
->keyword
)
4999 /* These two are the boolean literals. */
5001 cp_lexer_consume_token (parser
->lexer
);
5002 return cp_expr (boolean_true_node
, token
->location
);
5004 cp_lexer_consume_token (parser
->lexer
);
5005 return cp_expr (boolean_false_node
, token
->location
);
5007 /* The `__null' literal. */
5009 cp_lexer_consume_token (parser
->lexer
);
5010 return cp_expr (null_node
, token
->location
);
5012 /* The `nullptr' literal. */
5014 cp_lexer_consume_token (parser
->lexer
);
5015 return cp_expr (nullptr_node
, token
->location
);
5017 /* Recognize the `this' keyword. */
5019 cp_lexer_consume_token (parser
->lexer
);
5020 if (parser
->local_variables_forbidden_p
)
5022 error_at (token
->location
,
5023 "%<this%> may not be used in this context");
5024 return error_mark_node
;
5026 /* Pointers cannot appear in constant-expressions. */
5027 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5028 return error_mark_node
;
5029 return cp_expr (finish_this_expr (), token
->location
);
5031 /* The `operator' keyword can be the beginning of an
5036 case RID_FUNCTION_NAME
:
5037 case RID_PRETTY_FUNCTION_NAME
:
5038 case RID_C99_FUNCTION_NAME
:
5040 non_integral_constant name
;
5042 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5043 __func__ are the names of variables -- but they are
5044 treated specially. Therefore, they are handled here,
5045 rather than relying on the generic id-expression logic
5046 below. Grammatically, these names are id-expressions.
5048 Consume the token. */
5049 token
= cp_lexer_consume_token (parser
->lexer
);
5051 switch (token
->keyword
)
5053 case RID_FUNCTION_NAME
:
5054 name
= NIC_FUNC_NAME
;
5056 case RID_PRETTY_FUNCTION_NAME
:
5057 name
= NIC_PRETTY_FUNC
;
5059 case RID_C99_FUNCTION_NAME
:
5060 name
= NIC_C99_FUNC
;
5066 if (cp_parser_non_integral_constant_expression (parser
, name
))
5067 return error_mark_node
;
5069 /* Look up the name. */
5070 return finish_fname (token
->u
.value
);
5077 source_location type_location
;
5078 location_t start_loc
5079 = cp_lexer_peek_token (parser
->lexer
)->location
;
5080 /* The `__builtin_va_arg' construct is used to handle
5081 `va_arg'. Consume the `__builtin_va_arg' token. */
5082 cp_lexer_consume_token (parser
->lexer
);
5083 /* Look for the opening `('. */
5084 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5085 /* Now, parse the assignment-expression. */
5086 expression
= cp_parser_assignment_expression (parser
);
5087 /* Look for the `,'. */
5088 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5089 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5090 /* Parse the type-id. */
5092 type_id_in_expr_sentinel
s (parser
);
5093 type
= cp_parser_type_id (parser
);
5095 /* Look for the closing `)'. */
5096 location_t finish_loc
5097 = cp_lexer_peek_token (parser
->lexer
)->location
;
5098 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5099 /* Using `va_arg' in a constant-expression is not
5101 if (cp_parser_non_integral_constant_expression (parser
,
5103 return error_mark_node
;
5104 /* Construct a location of the form:
5105 __builtin_va_arg (v, int)
5106 ~~~~~~~~~~~~~~~~~~~~~^~~~
5107 with the caret at the type, ranging from the start of the
5108 "__builtin_va_arg" token to the close paren. */
5109 location_t combined_loc
5110 = make_location (type_location
, start_loc
, finish_loc
);
5111 return build_x_va_arg (combined_loc
, expression
, type
);
5115 return cp_parser_builtin_offsetof (parser
);
5117 case RID_HAS_NOTHROW_ASSIGN
:
5118 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5119 case RID_HAS_NOTHROW_COPY
:
5120 case RID_HAS_TRIVIAL_ASSIGN
:
5121 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5122 case RID_HAS_TRIVIAL_COPY
:
5123 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5125 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5126 case RID_IS_ABSTRACT
:
5127 case RID_IS_BASE_OF
:
5132 case RID_IS_LITERAL_TYPE
:
5134 case RID_IS_POLYMORPHIC
:
5135 case RID_IS_SAME_AS
:
5136 case RID_IS_STD_LAYOUT
:
5137 case RID_IS_TRIVIAL
:
5138 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5139 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5140 case RID_IS_TRIVIALLY_COPYABLE
:
5142 return cp_parser_trait_expr (parser
, token
->keyword
);
5146 return cp_parser_requires_expression (parser
);
5148 /* Objective-C++ expressions. */
5150 case RID_AT_PROTOCOL
:
5151 case RID_AT_SELECTOR
:
5152 return cp_parser_objc_expression (parser
);
5155 if (parser
->in_function_body
5156 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5159 error_at (token
->location
,
5160 "a template declaration cannot appear at block scope");
5161 cp_parser_skip_to_end_of_block_or_statement (parser
);
5162 return error_mark_node
;
5166 cp_parser_error (parser
, "expected primary-expression");
5167 return error_mark_node
;
5170 /* An id-expression can start with either an identifier, a
5171 `::' as the beginning of a qualified-id, or the "operator"
5175 case CPP_TEMPLATE_ID
:
5176 case CPP_NESTED_NAME_SPECIFIER
:
5179 cp_expr id_expression
;
5181 const char *error_msg
;
5184 cp_token
*id_expr_token
;
5186 /* Parse the id-expression. */
5188 = cp_parser_id_expression (parser
,
5189 /*template_keyword_p=*/false,
5190 /*check_dependency_p=*/true,
5192 /*declarator_p=*/false,
5193 /*optional_p=*/false);
5194 if (id_expression
== error_mark_node
)
5195 return error_mark_node
;
5196 id_expr_token
= token
;
5197 token
= cp_lexer_peek_token (parser
->lexer
);
5198 done
= (token
->type
!= CPP_OPEN_SQUARE
5199 && token
->type
!= CPP_OPEN_PAREN
5200 && token
->type
!= CPP_DOT
5201 && token
->type
!= CPP_DEREF
5202 && token
->type
!= CPP_PLUS_PLUS
5203 && token
->type
!= CPP_MINUS_MINUS
);
5204 /* If we have a template-id, then no further lookup is
5205 required. If the template-id was for a template-class, we
5206 will sometimes have a TYPE_DECL at this point. */
5207 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5208 || TREE_CODE (id_expression
) == TYPE_DECL
)
5209 decl
= id_expression
;
5210 /* Look up the name. */
5213 tree ambiguous_decls
;
5215 /* If we already know that this lookup is ambiguous, then
5216 we've already issued an error message; there's no reason
5218 if (id_expr_token
->type
== CPP_NAME
5219 && id_expr_token
->error_reported
)
5221 cp_parser_simulate_error (parser
);
5222 return error_mark_node
;
5225 decl
= cp_parser_lookup_name (parser
, id_expression
,
5228 /*is_namespace=*/false,
5229 /*check_dependency=*/true,
5231 id_expr_token
->location
);
5232 /* If the lookup was ambiguous, an error will already have
5234 if (ambiguous_decls
)
5235 return error_mark_node
;
5237 /* In Objective-C++, we may have an Objective-C 2.0
5238 dot-syntax for classes here. */
5239 if (c_dialect_objc ()
5240 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5241 && TREE_CODE (decl
) == TYPE_DECL
5242 && objc_is_class_name (decl
))
5245 cp_lexer_consume_token (parser
->lexer
);
5246 component
= cp_parser_identifier (parser
);
5247 if (component
== error_mark_node
)
5248 return error_mark_node
;
5250 tree result
= objc_build_class_component_ref (id_expression
,
5252 /* Build a location of the form:
5255 with caret at the start of the component name (at
5256 input_location), ranging from the start of the id_expression
5257 to the end of the component name. */
5258 location_t combined_loc
5259 = make_location (input_location
, id_expression
.get_start (),
5260 get_finish (input_location
));
5261 protected_set_expr_location (result
, combined_loc
);
5265 /* In Objective-C++, an instance variable (ivar) may be preferred
5266 to whatever cp_parser_lookup_name() found.
5267 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5268 rest of c-family, we have to do a little extra work to preserve
5269 any location information in cp_expr "decl". Given that
5270 objc_lookup_ivar is implemented in "c-family" and "objc", we
5271 have a trip through the pure "tree" type, rather than cp_expr.
5272 Naively copying it back to "decl" would implicitly give the
5273 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5274 store an EXPR_LOCATION. Hence we only update "decl" (and
5275 hence its location_t) if we get back a different tree node. */
5276 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5278 if (decl_tree
!= decl
.get_value ())
5279 decl
= cp_expr (decl_tree
);
5281 /* If name lookup gives us a SCOPE_REF, then the
5282 qualifying scope was dependent. */
5283 if (TREE_CODE (decl
) == SCOPE_REF
)
5285 /* At this point, we do not know if DECL is a valid
5286 integral constant expression. We assume that it is
5287 in fact such an expression, so that code like:
5289 template <int N> struct A {
5293 is accepted. At template-instantiation time, we
5294 will check that B<N>::i is actually a constant. */
5297 /* Check to see if DECL is a local variable in a context
5298 where that is forbidden. */
5299 if (parser
->local_variables_forbidden_p
5300 && local_variable_p (decl
))
5302 /* It might be that we only found DECL because we are
5303 trying to be generous with pre-ISO scoping rules.
5304 For example, consider:
5308 for (int i = 0; i < 10; ++i) {}
5309 extern void f(int j = i);
5312 Here, name look up will originally find the out
5313 of scope `i'. We need to issue a warning message,
5314 but then use the global `i'. */
5315 decl
= check_for_out_of_scope_variable (decl
);
5316 if (local_variable_p (decl
))
5318 error_at (id_expr_token
->location
,
5319 "local variable %qD may not appear in this context",
5321 return error_mark_node
;
5326 decl
= (finish_id_expression
5327 (id_expression
, decl
, parser
->scope
,
5329 parser
->integral_constant_expression_p
,
5330 parser
->allow_non_integral_constant_expression_p
,
5331 &parser
->non_integral_constant_expression_p
,
5332 template_p
, done
, address_p
,
5335 id_expression
.get_location ()));
5337 cp_parser_error (parser
, error_msg
);
5338 decl
.set_location (id_expr_token
->location
);
5342 /* Anything else is an error. */
5344 cp_parser_error (parser
, "expected primary-expression");
5345 return error_mark_node
;
5349 static inline cp_expr
5350 cp_parser_primary_expression (cp_parser
*parser
,
5353 bool template_arg_p
,
5356 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5357 /*decltype*/false, idk
);
5360 /* Parse an id-expression.
5367 :: [opt] nested-name-specifier template [opt] unqualified-id
5369 :: operator-function-id
5372 Return a representation of the unqualified portion of the
5373 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5374 a `::' or nested-name-specifier.
5376 Often, if the id-expression was a qualified-id, the caller will
5377 want to make a SCOPE_REF to represent the qualified-id. This
5378 function does not do this in order to avoid wastefully creating
5379 SCOPE_REFs when they are not required.
5381 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5384 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5385 uninstantiated templates.
5387 If *TEMPLATE_P is non-NULL, it is set to true iff the
5388 `template' keyword is used to explicitly indicate that the entity
5389 named is a template.
5391 If DECLARATOR_P is true, the id-expression is appearing as part of
5392 a declarator, rather than as part of an expression. */
5395 cp_parser_id_expression (cp_parser
*parser
,
5396 bool template_keyword_p
,
5397 bool check_dependency_p
,
5402 bool global_scope_p
;
5403 bool nested_name_specifier_p
;
5405 /* Assume the `template' keyword was not used. */
5407 *template_p
= template_keyword_p
;
5409 /* Look for the optional `::' operator. */
5411 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5413 /* Look for the optional nested-name-specifier. */
5414 nested_name_specifier_p
5415 = (cp_parser_nested_name_specifier_opt (parser
,
5416 /*typename_keyword_p=*/false,
5421 /* If there is a nested-name-specifier, then we are looking at
5422 the first qualified-id production. */
5423 if (nested_name_specifier_p
)
5426 tree saved_object_scope
;
5427 tree saved_qualifying_scope
;
5428 cp_expr unqualified_id
;
5431 /* See if the next token is the `template' keyword. */
5433 template_p
= &is_template
;
5434 *template_p
= cp_parser_optional_template_keyword (parser
);
5435 /* Name lookup we do during the processing of the
5436 unqualified-id might obliterate SCOPE. */
5437 saved_scope
= parser
->scope
;
5438 saved_object_scope
= parser
->object_scope
;
5439 saved_qualifying_scope
= parser
->qualifying_scope
;
5440 /* Process the final unqualified-id. */
5441 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5444 /*optional_p=*/false);
5445 /* Restore the SAVED_SCOPE for our caller. */
5446 parser
->scope
= saved_scope
;
5447 parser
->object_scope
= saved_object_scope
;
5448 parser
->qualifying_scope
= saved_qualifying_scope
;
5450 return unqualified_id
;
5452 /* Otherwise, if we are in global scope, then we are looking at one
5453 of the other qualified-id productions. */
5454 else if (global_scope_p
)
5459 /* Peek at the next token. */
5460 token
= cp_lexer_peek_token (parser
->lexer
);
5462 /* If it's an identifier, and the next token is not a "<", then
5463 we can avoid the template-id case. This is an optimization
5464 for this common case. */
5465 if (token
->type
== CPP_NAME
5466 && !cp_parser_nth_token_starts_template_argument_list_p
5468 return cp_parser_identifier (parser
);
5470 cp_parser_parse_tentatively (parser
);
5471 /* Try a template-id. */
5472 id
= cp_parser_template_id (parser
,
5473 /*template_keyword_p=*/false,
5474 /*check_dependency_p=*/true,
5477 /* If that worked, we're done. */
5478 if (cp_parser_parse_definitely (parser
))
5481 /* Peek at the next token. (Changes in the token buffer may
5482 have invalidated the pointer obtained above.) */
5483 token
= cp_lexer_peek_token (parser
->lexer
);
5485 switch (token
->type
)
5488 return cp_parser_identifier (parser
);
5491 if (token
->keyword
== RID_OPERATOR
)
5492 return cp_parser_operator_function_id (parser
);
5496 cp_parser_error (parser
, "expected id-expression");
5497 return error_mark_node
;
5501 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5502 /*check_dependency_p=*/true,
5507 /* Parse an unqualified-id.
5511 operator-function-id
5512 conversion-function-id
5516 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5517 keyword, in a construct like `A::template ...'.
5519 Returns a representation of unqualified-id. For the `identifier'
5520 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5521 production a BIT_NOT_EXPR is returned; the operand of the
5522 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5523 other productions, see the documentation accompanying the
5524 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5525 names are looked up in uninstantiated templates. If DECLARATOR_P
5526 is true, the unqualified-id is appearing as part of a declarator,
5527 rather than as part of an expression. */
5530 cp_parser_unqualified_id (cp_parser
* parser
,
5531 bool template_keyword_p
,
5532 bool check_dependency_p
,
5538 /* Peek at the next token. */
5539 token
= cp_lexer_peek_token (parser
->lexer
);
5541 switch ((int) token
->type
)
5547 /* We don't know yet whether or not this will be a
5549 cp_parser_parse_tentatively (parser
);
5550 /* Try a template-id. */
5551 id
= cp_parser_template_id (parser
, template_keyword_p
,
5555 /* If it worked, we're done. */
5556 if (cp_parser_parse_definitely (parser
))
5558 /* Otherwise, it's an ordinary identifier. */
5559 return cp_parser_identifier (parser
);
5562 case CPP_TEMPLATE_ID
:
5563 return cp_parser_template_id (parser
, template_keyword_p
,
5571 tree qualifying_scope
;
5576 /* Consume the `~' token. */
5577 cp_lexer_consume_token (parser
->lexer
);
5578 /* Parse the class-name. The standard, as written, seems to
5581 template <typename T> struct S { ~S (); };
5582 template <typename T> S<T>::~S() {}
5584 is invalid, since `~' must be followed by a class-name, but
5585 `S<T>' is dependent, and so not known to be a class.
5586 That's not right; we need to look in uninstantiated
5587 templates. A further complication arises from:
5589 template <typename T> void f(T t) {
5593 Here, it is not possible to look up `T' in the scope of `T'
5594 itself. We must look in both the current scope, and the
5595 scope of the containing complete expression.
5597 Yet another issue is:
5606 The standard does not seem to say that the `S' in `~S'
5607 should refer to the type `S' and not the data member
5610 /* DR 244 says that we look up the name after the "~" in the
5611 same scope as we looked up the qualifying name. That idea
5612 isn't fully worked out; it's more complicated than that. */
5613 scope
= parser
->scope
;
5614 object_scope
= parser
->object_scope
;
5615 qualifying_scope
= parser
->qualifying_scope
;
5617 /* Check for invalid scopes. */
5618 if (scope
== error_mark_node
)
5620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5621 cp_lexer_consume_token (parser
->lexer
);
5622 return error_mark_node
;
5624 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5627 error_at (token
->location
,
5628 "scope %qT before %<~%> is not a class-name",
5630 cp_parser_simulate_error (parser
);
5631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5632 cp_lexer_consume_token (parser
->lexer
);
5633 return error_mark_node
;
5635 gcc_assert (!scope
|| TYPE_P (scope
));
5637 /* If the name is of the form "X::~X" it's OK even if X is a
5639 token
= cp_lexer_peek_token (parser
->lexer
);
5641 && token
->type
== CPP_NAME
5642 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5644 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5645 || (CLASS_TYPE_P (scope
)
5646 && constructor_name_p (token
->u
.value
, scope
))))
5648 cp_lexer_consume_token (parser
->lexer
);
5649 return build_nt (BIT_NOT_EXPR
, scope
);
5652 /* ~auto means the destructor of whatever the object is. */
5653 if (cp_parser_is_keyword (token
, RID_AUTO
))
5655 if (cxx_dialect
< cxx14
)
5656 pedwarn (input_location
, 0,
5657 "%<~auto%> only available with "
5658 "-std=c++14 or -std=gnu++14");
5659 cp_lexer_consume_token (parser
->lexer
);
5660 return build_nt (BIT_NOT_EXPR
, make_auto ());
5663 /* If there was an explicit qualification (S::~T), first look
5664 in the scope given by the qualification (i.e., S).
5666 Note: in the calls to cp_parser_class_name below we pass
5667 typename_type so that lookup finds the injected-class-name
5668 rather than the constructor. */
5670 type_decl
= NULL_TREE
;
5673 cp_parser_parse_tentatively (parser
);
5674 type_decl
= cp_parser_class_name (parser
,
5675 /*typename_keyword_p=*/false,
5676 /*template_keyword_p=*/false,
5678 /*check_dependency=*/false,
5679 /*class_head_p=*/false,
5681 if (cp_parser_parse_definitely (parser
))
5684 /* In "N::S::~S", look in "N" as well. */
5685 if (!done
&& scope
&& qualifying_scope
)
5687 cp_parser_parse_tentatively (parser
);
5688 parser
->scope
= qualifying_scope
;
5689 parser
->object_scope
= NULL_TREE
;
5690 parser
->qualifying_scope
= NULL_TREE
;
5692 = cp_parser_class_name (parser
,
5693 /*typename_keyword_p=*/false,
5694 /*template_keyword_p=*/false,
5696 /*check_dependency=*/false,
5697 /*class_head_p=*/false,
5699 if (cp_parser_parse_definitely (parser
))
5702 /* In "p->S::~T", look in the scope given by "*p" as well. */
5703 else if (!done
&& object_scope
)
5705 cp_parser_parse_tentatively (parser
);
5706 parser
->scope
= object_scope
;
5707 parser
->object_scope
= NULL_TREE
;
5708 parser
->qualifying_scope
= NULL_TREE
;
5710 = cp_parser_class_name (parser
,
5711 /*typename_keyword_p=*/false,
5712 /*template_keyword_p=*/false,
5714 /*check_dependency=*/false,
5715 /*class_head_p=*/false,
5717 if (cp_parser_parse_definitely (parser
))
5720 /* Look in the surrounding context. */
5723 parser
->scope
= NULL_TREE
;
5724 parser
->object_scope
= NULL_TREE
;
5725 parser
->qualifying_scope
= NULL_TREE
;
5726 if (processing_template_decl
)
5727 cp_parser_parse_tentatively (parser
);
5729 = cp_parser_class_name (parser
,
5730 /*typename_keyword_p=*/false,
5731 /*template_keyword_p=*/false,
5733 /*check_dependency=*/false,
5734 /*class_head_p=*/false,
5736 if (processing_template_decl
5737 && ! cp_parser_parse_definitely (parser
))
5739 /* We couldn't find a type with this name. If we're parsing
5740 tentatively, fail and try something else. */
5741 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5743 cp_parser_simulate_error (parser
);
5744 return error_mark_node
;
5746 /* Otherwise, accept it and check for a match at instantiation
5748 type_decl
= cp_parser_identifier (parser
);
5749 if (type_decl
!= error_mark_node
)
5750 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5754 /* If an error occurred, assume that the name of the
5755 destructor is the same as the name of the qualifying
5756 class. That allows us to keep parsing after running
5757 into ill-formed destructor names. */
5758 if (type_decl
== error_mark_node
&& scope
)
5759 return build_nt (BIT_NOT_EXPR
, scope
);
5760 else if (type_decl
== error_mark_node
)
5761 return error_mark_node
;
5763 /* Check that destructor name and scope match. */
5764 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5767 error_at (token
->location
,
5768 "declaration of %<~%T%> as member of %qT",
5770 cp_parser_simulate_error (parser
);
5771 return error_mark_node
;
5776 A typedef-name that names a class shall not be used as the
5777 identifier in the declarator for a destructor declaration. */
5779 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5780 && !DECL_SELF_REFERENCE_P (type_decl
)
5781 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5782 error_at (token
->location
,
5783 "typedef-name %qD used as destructor declarator",
5786 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5790 if (token
->keyword
== RID_OPERATOR
)
5794 /* This could be a template-id, so we try that first. */
5795 cp_parser_parse_tentatively (parser
);
5796 /* Try a template-id. */
5797 id
= cp_parser_template_id (parser
, template_keyword_p
,
5798 /*check_dependency_p=*/true,
5801 /* If that worked, we're done. */
5802 if (cp_parser_parse_definitely (parser
))
5804 /* We still don't know whether we're looking at an
5805 operator-function-id or a conversion-function-id. */
5806 cp_parser_parse_tentatively (parser
);
5807 /* Try an operator-function-id. */
5808 id
= cp_parser_operator_function_id (parser
);
5809 /* If that didn't work, try a conversion-function-id. */
5810 if (!cp_parser_parse_definitely (parser
))
5811 id
= cp_parser_conversion_function_id (parser
);
5812 else if (UDLIT_OPER_P (id
))
5815 const char *name
= UDLIT_OP_SUFFIX (id
);
5816 if (name
[0] != '_' && !in_system_header_at (input_location
)
5818 warning (0, "literal operator suffixes not preceded by %<_%>"
5819 " are reserved for future standardization");
5829 cp_parser_error (parser
, "expected unqualified-id");
5830 return error_mark_node
;
5834 /* Parse an (optional) nested-name-specifier.
5836 nested-name-specifier: [C++98]
5837 class-or-namespace-name :: nested-name-specifier [opt]
5838 class-or-namespace-name :: template nested-name-specifier [opt]
5840 nested-name-specifier: [C++0x]
5843 nested-name-specifier identifier ::
5844 nested-name-specifier template [opt] simple-template-id ::
5846 PARSER->SCOPE should be set appropriately before this function is
5847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5851 Sets PARSER->SCOPE to the class (TYPE) or namespace
5852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5853 it unchanged if there is no nested-name-specifier. Returns the new
5854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5856 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5857 part of a declaration and/or decl-specifier. */
5860 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5861 bool typename_keyword_p
,
5862 bool check_dependency_p
,
5864 bool is_declaration
)
5866 bool success
= false;
5867 cp_token_position start
= 0;
5870 /* Remember where the nested-name-specifier starts. */
5871 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5873 start
= cp_lexer_token_position (parser
->lexer
, false);
5874 push_deferring_access_checks (dk_deferred
);
5881 tree saved_qualifying_scope
;
5882 bool template_keyword_p
;
5884 /* Spot cases that cannot be the beginning of a
5885 nested-name-specifier. */
5886 token
= cp_lexer_peek_token (parser
->lexer
);
5888 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5889 the already parsed nested-name-specifier. */
5890 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5892 /* Grab the nested-name-specifier and continue the loop. */
5893 cp_parser_pre_parsed_nested_name_specifier (parser
);
5894 /* If we originally encountered this nested-name-specifier
5895 with IS_DECLARATION set to false, we will not have
5896 resolved TYPENAME_TYPEs, so we must do so here. */
5898 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5900 new_scope
= resolve_typename_type (parser
->scope
,
5901 /*only_current_p=*/false);
5902 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5903 parser
->scope
= new_scope
;
5909 /* Spot cases that cannot be the beginning of a
5910 nested-name-specifier. On the second and subsequent times
5911 through the loop, we look for the `template' keyword. */
5912 if (success
&& token
->keyword
== RID_TEMPLATE
)
5914 /* A template-id can start a nested-name-specifier. */
5915 else if (token
->type
== CPP_TEMPLATE_ID
)
5917 /* DR 743: decltype can be used in a nested-name-specifier. */
5918 else if (token_is_decltype (token
))
5922 /* If the next token is not an identifier, then it is
5923 definitely not a type-name or namespace-name. */
5924 if (token
->type
!= CPP_NAME
)
5926 /* If the following token is neither a `<' (to begin a
5927 template-id), nor a `::', then we are not looking at a
5928 nested-name-specifier. */
5929 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5931 if (token
->type
== CPP_COLON
5932 && parser
->colon_corrects_to_scope_p
5933 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5935 error_at (token
->location
,
5936 "found %<:%> in nested-name-specifier, expected %<::%>");
5937 token
->type
= CPP_SCOPE
;
5940 if (token
->type
!= CPP_SCOPE
5941 && !cp_parser_nth_token_starts_template_argument_list_p
5946 /* The nested-name-specifier is optional, so we parse
5948 cp_parser_parse_tentatively (parser
);
5950 /* Look for the optional `template' keyword, if this isn't the
5951 first time through the loop. */
5953 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5955 template_keyword_p
= false;
5957 /* Save the old scope since the name lookup we are about to do
5958 might destroy it. */
5959 old_scope
= parser
->scope
;
5960 saved_qualifying_scope
= parser
->qualifying_scope
;
5961 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5962 look up names in "X<T>::I" in order to determine that "Y" is
5963 a template. So, if we have a typename at this point, we make
5964 an effort to look through it. */
5966 && !typename_keyword_p
5968 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5969 parser
->scope
= resolve_typename_type (parser
->scope
,
5970 /*only_current_p=*/false);
5971 /* Parse the qualifying entity. */
5973 = cp_parser_qualifying_entity (parser
,
5979 /* Look for the `::' token. */
5980 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5982 /* If we found what we wanted, we keep going; otherwise, we're
5984 if (!cp_parser_parse_definitely (parser
))
5986 bool error_p
= false;
5988 /* Restore the OLD_SCOPE since it was valid before the
5989 failed attempt at finding the last
5990 class-or-namespace-name. */
5991 parser
->scope
= old_scope
;
5992 parser
->qualifying_scope
= saved_qualifying_scope
;
5994 /* If the next token is a decltype, and the one after that is a
5995 `::', then the decltype has failed to resolve to a class or
5996 enumeration type. Give this error even when parsing
5997 tentatively since it can't possibly be valid--and we're going
5998 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5999 won't get another chance.*/
6000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6001 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6004 token
= cp_lexer_consume_token (parser
->lexer
);
6005 error_at (token
->location
, "decltype evaluates to %qT, "
6006 "which is not a class or enumeration type",
6007 token
->u
.tree_check_value
->value
);
6008 parser
->scope
= error_mark_node
;
6012 cp_lexer_consume_token (parser
->lexer
);
6015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6016 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6018 /* If we have a non-type template-id followed by ::, it can't
6019 possibly be valid. */
6020 token
= cp_lexer_peek_token (parser
->lexer
);
6021 tree tid
= token
->u
.tree_check_value
->value
;
6022 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6023 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6025 tree tmpl
= NULL_TREE
;
6026 if (is_overloaded_fn (tid
))
6028 tree fns
= get_fns (tid
);
6029 if (!OVL_CHAIN (fns
))
6030 tmpl
= OVL_CURRENT (fns
);
6031 error_at (token
->location
, "function template-id %qD "
6032 "in nested-name-specifier", tid
);
6036 /* Variable template. */
6037 tmpl
= TREE_OPERAND (tid
, 0);
6038 gcc_assert (variable_template_p (tmpl
));
6039 error_at (token
->location
, "variable template-id %qD "
6040 "in nested-name-specifier", tid
);
6043 inform (DECL_SOURCE_LOCATION (tmpl
),
6044 "%qD declared here", tmpl
);
6046 parser
->scope
= error_mark_node
;
6050 cp_lexer_consume_token (parser
->lexer
);
6051 cp_lexer_consume_token (parser
->lexer
);
6055 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6057 /* If the next token is an identifier, and the one after
6058 that is a `::', then any valid interpretation would have
6059 found a class-or-namespace-name. */
6060 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6061 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6063 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6066 token
= cp_lexer_consume_token (parser
->lexer
);
6069 if (!token
->error_reported
)
6072 tree ambiguous_decls
;
6074 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6076 /*is_template=*/false,
6077 /*is_namespace=*/false,
6078 /*check_dependency=*/true,
6081 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6082 error_at (token
->location
,
6083 "%qD used without template parameters",
6085 else if (ambiguous_decls
)
6087 // cp_parser_lookup_name has the same diagnostic,
6088 // thus make sure to emit it at most once.
6089 if (cp_parser_uncommitted_to_tentative_parse_p
6092 error_at (token
->location
,
6093 "reference to %qD is ambiguous",
6095 print_candidates (ambiguous_decls
);
6097 decl
= error_mark_node
;
6101 if (cxx_dialect
!= cxx98
)
6102 cp_parser_name_lookup_error
6103 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6106 cp_parser_name_lookup_error
6107 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6111 parser
->scope
= error_mark_node
;
6113 /* Treat this as a successful nested-name-specifier
6118 If the name found is not a class-name (clause
6119 _class_) or namespace-name (_namespace.def_), the
6120 program is ill-formed. */
6123 cp_lexer_consume_token (parser
->lexer
);
6127 /* We've found one valid nested-name-specifier. */
6129 /* Name lookup always gives us a DECL. */
6130 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6131 new_scope
= TREE_TYPE (new_scope
);
6132 /* Uses of "template" must be followed by actual templates. */
6133 if (template_keyword_p
6134 && !(CLASS_TYPE_P (new_scope
)
6135 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6136 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6137 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6138 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6139 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6140 == TEMPLATE_ID_EXPR
)))
6141 permerror (input_location
, TYPE_P (new_scope
)
6142 ? G_("%qT is not a template")
6143 : G_("%qD is not a template"),
6145 /* If it is a class scope, try to complete it; we are about to
6146 be looking up names inside the class. */
6147 if (TYPE_P (new_scope
)
6148 /* Since checking types for dependency can be expensive,
6149 avoid doing it if the type is already complete. */
6150 && !COMPLETE_TYPE_P (new_scope
)
6151 /* Do not try to complete dependent types. */
6152 && !dependent_type_p (new_scope
))
6154 new_scope
= complete_type (new_scope
);
6155 /* If it is a typedef to current class, use the current
6156 class instead, as the typedef won't have any names inside
6158 if (!COMPLETE_TYPE_P (new_scope
)
6159 && currently_open_class (new_scope
))
6160 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6162 /* Make sure we look in the right scope the next time through
6164 parser
->scope
= new_scope
;
6167 /* If parsing tentatively, replace the sequence of tokens that makes
6168 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6169 token. That way, should we re-parse the token stream, we will
6170 not have to repeat the effort required to do the parse, nor will
6171 we issue duplicate error messages. */
6172 if (success
&& start
)
6176 token
= cp_lexer_token_at (parser
->lexer
, start
);
6177 /* Reset the contents of the START token. */
6178 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6179 /* Retrieve any deferred checks. Do not pop this access checks yet
6180 so the memory will not be reclaimed during token replacing below. */
6181 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6182 token
->u
.tree_check_value
->value
= parser
->scope
;
6183 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6184 token
->u
.tree_check_value
->qualifying_scope
=
6185 parser
->qualifying_scope
;
6186 token
->keyword
= RID_MAX
;
6188 /* Purge all subsequent tokens. */
6189 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6193 pop_to_parent_deferring_access_checks ();
6195 return success
? parser
->scope
: NULL_TREE
;
6198 /* Parse a nested-name-specifier. See
6199 cp_parser_nested_name_specifier_opt for details. This function
6200 behaves identically, except that it will an issue an error if no
6201 nested-name-specifier is present. */
6204 cp_parser_nested_name_specifier (cp_parser
*parser
,
6205 bool typename_keyword_p
,
6206 bool check_dependency_p
,
6208 bool is_declaration
)
6212 /* Look for the nested-name-specifier. */
6213 scope
= cp_parser_nested_name_specifier_opt (parser
,
6218 /* If it was not present, issue an error message. */
6221 cp_parser_error (parser
, "expected nested-name-specifier");
6222 parser
->scope
= NULL_TREE
;
6228 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6229 this is either a class-name or a namespace-name (which corresponds
6230 to the class-or-namespace-name production in the grammar). For
6231 C++0x, it can also be a type-name that refers to an enumeration
6232 type or a simple-template-id.
6234 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6235 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6236 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6237 TYPE_P is TRUE iff the next name should be taken as a class-name,
6238 even the same name is declared to be another entity in the same
6241 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6242 specified by the class-or-namespace-name. If neither is found the
6243 ERROR_MARK_NODE is returned. */
6246 cp_parser_qualifying_entity (cp_parser
*parser
,
6247 bool typename_keyword_p
,
6248 bool template_keyword_p
,
6249 bool check_dependency_p
,
6251 bool is_declaration
)
6254 tree saved_qualifying_scope
;
6255 tree saved_object_scope
;
6258 bool successful_parse_p
;
6260 /* DR 743: decltype can appear in a nested-name-specifier. */
6261 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6263 scope
= cp_parser_decltype (parser
);
6264 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6265 && !MAYBE_CLASS_TYPE_P (scope
))
6267 cp_parser_simulate_error (parser
);
6268 return error_mark_node
;
6270 if (TYPE_NAME (scope
))
6271 scope
= TYPE_NAME (scope
);
6275 /* Before we try to parse the class-name, we must save away the
6276 current PARSER->SCOPE since cp_parser_class_name will destroy
6278 saved_scope
= parser
->scope
;
6279 saved_qualifying_scope
= parser
->qualifying_scope
;
6280 saved_object_scope
= parser
->object_scope
;
6281 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6282 there is no need to look for a namespace-name. */
6283 only_class_p
= template_keyword_p
6284 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6286 cp_parser_parse_tentatively (parser
);
6287 scope
= cp_parser_class_name (parser
,
6290 type_p
? class_type
: none_type
,
6292 /*class_head_p=*/false,
6294 /*enum_ok=*/cxx_dialect
> cxx98
);
6295 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6296 /* If that didn't work, try for a namespace-name. */
6297 if (!only_class_p
&& !successful_parse_p
)
6299 /* Restore the saved scope. */
6300 parser
->scope
= saved_scope
;
6301 parser
->qualifying_scope
= saved_qualifying_scope
;
6302 parser
->object_scope
= saved_object_scope
;
6303 /* If we are not looking at an identifier followed by the scope
6304 resolution operator, then this is not part of a
6305 nested-name-specifier. (Note that this function is only used
6306 to parse the components of a nested-name-specifier.) */
6307 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6308 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6309 return error_mark_node
;
6310 scope
= cp_parser_namespace_name (parser
);
6316 /* Return true if we are looking at a compound-literal, false otherwise. */
6319 cp_parser_compound_literal_p (cp_parser
*parser
)
6321 /* Consume the `('. */
6322 cp_lexer_consume_token (parser
->lexer
);
6324 cp_lexer_save_tokens (parser
->lexer
);
6326 /* Skip tokens until the next token is a closing parenthesis.
6327 If we find the closing `)', and the next token is a `{', then
6328 we are looking at a compound-literal. */
6329 bool compound_literal_p
6330 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6331 /*consume_paren=*/true)
6332 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6334 /* Roll back the tokens we skipped. */
6335 cp_lexer_rollback_tokens (parser
->lexer
);
6337 return compound_literal_p
;
6340 /* Parse a postfix-expression.
6344 postfix-expression [ expression ]
6345 postfix-expression ( expression-list [opt] )
6346 simple-type-specifier ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier identifier
6348 ( expression-list [opt] )
6349 typename :: [opt] nested-name-specifier template [opt] template-id
6350 ( expression-list [opt] )
6351 postfix-expression . template [opt] id-expression
6352 postfix-expression -> template [opt] id-expression
6353 postfix-expression . pseudo-destructor-name
6354 postfix-expression -> pseudo-destructor-name
6355 postfix-expression ++
6356 postfix-expression --
6357 dynamic_cast < type-id > ( expression )
6358 static_cast < type-id > ( expression )
6359 reinterpret_cast < type-id > ( expression )
6360 const_cast < type-id > ( expression )
6361 typeid ( expression )
6367 ( type-id ) { initializer-list , [opt] }
6369 This extension is a GNU version of the C99 compound-literal
6370 construct. (The C99 grammar uses `type-name' instead of `type-id',
6371 but they are essentially the same concept.)
6373 If ADDRESS_P is true, the postfix expression is the operand of the
6374 `&' operator. CAST_P is true if this expression is the target of a
6377 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6378 class member access expressions [expr.ref].
6380 Returns a representation of the expression. */
6383 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6384 bool member_access_only_p
, bool decltype_p
,
6385 cp_id_kind
* pidk_return
)
6390 cp_id_kind idk
= CP_ID_KIND_NONE
;
6391 cp_expr postfix_expression
= NULL_TREE
;
6392 bool is_member_access
= false;
6393 int saved_in_statement
= -1;
6395 /* Peek at the next token. */
6396 token
= cp_lexer_peek_token (parser
->lexer
);
6397 loc
= token
->location
;
6398 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6400 /* Some of the productions are determined by keywords. */
6401 keyword
= token
->keyword
;
6411 const char *saved_message
;
6412 bool saved_in_type_id_in_expr_p
;
6414 /* All of these can be handled in the same way from the point
6415 of view of parsing. Begin by consuming the token
6416 identifying the cast. */
6417 cp_lexer_consume_token (parser
->lexer
);
6419 /* New types cannot be defined in the cast. */
6420 saved_message
= parser
->type_definition_forbidden_message
;
6421 parser
->type_definition_forbidden_message
6422 = G_("types may not be defined in casts");
6424 /* Look for the opening `<'. */
6425 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6426 /* Parse the type to which we are casting. */
6427 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6428 parser
->in_type_id_in_expr_p
= true;
6429 type
= cp_parser_type_id (parser
);
6430 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6431 /* Look for the closing `>'. */
6432 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6433 /* Restore the old message. */
6434 parser
->type_definition_forbidden_message
= saved_message
;
6436 bool saved_greater_than_is_operator_p
6437 = parser
->greater_than_is_operator_p
;
6438 parser
->greater_than_is_operator_p
= true;
6440 /* And the expression which is being cast. */
6441 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6442 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6443 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6445 location_t end_loc
= close_paren
?
6446 close_paren
->location
: UNKNOWN_LOCATION
;
6448 parser
->greater_than_is_operator_p
6449 = saved_greater_than_is_operator_p
;
6451 /* Only type conversions to integral or enumeration types
6452 can be used in constant-expressions. */
6453 if (!cast_valid_in_integral_constant_expression_p (type
)
6454 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6456 postfix_expression
= error_mark_node
;
6464 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6468 = build_static_cast (type
, expression
, tf_warning_or_error
);
6472 = build_reinterpret_cast (type
, expression
,
6473 tf_warning_or_error
);
6477 = build_const_cast (type
, expression
, tf_warning_or_error
);
6483 /* Construct a location e.g. :
6484 reinterpret_cast <int *> (expr)
6485 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6486 ranging from the start of the "*_cast" token to the final closing
6487 paren, with the caret at the start. */
6488 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6489 postfix_expression
.set_location (cp_cast_loc
);
6496 const char *saved_message
;
6497 bool saved_in_type_id_in_expr_p
;
6499 /* Consume the `typeid' token. */
6500 cp_lexer_consume_token (parser
->lexer
);
6501 /* Look for the `(' token. */
6502 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6503 /* Types cannot be defined in a `typeid' expression. */
6504 saved_message
= parser
->type_definition_forbidden_message
;
6505 parser
->type_definition_forbidden_message
6506 = G_("types may not be defined in a %<typeid%> expression");
6507 /* We can't be sure yet whether we're looking at a type-id or an
6509 cp_parser_parse_tentatively (parser
);
6510 /* Try a type-id first. */
6511 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6512 parser
->in_type_id_in_expr_p
= true;
6513 type
= cp_parser_type_id (parser
);
6514 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6515 /* Look for the `)' token. Otherwise, we can't be sure that
6516 we're not looking at an expression: consider `typeid (int
6517 (3))', for example. */
6518 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6519 /* If all went well, simply lookup the type-id. */
6520 if (cp_parser_parse_definitely (parser
))
6521 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6522 /* Otherwise, fall back to the expression variant. */
6527 /* Look for an expression. */
6528 expression
= cp_parser_expression (parser
, & idk
);
6529 /* Compute its typeid. */
6530 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6531 /* Look for the `)' token. */
6532 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6534 /* Restore the saved message. */
6535 parser
->type_definition_forbidden_message
= saved_message
;
6536 /* `typeid' may not appear in an integral constant expression. */
6537 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6538 postfix_expression
= error_mark_node
;
6545 /* The syntax permitted here is the same permitted for an
6546 elaborated-type-specifier. */
6547 ++parser
->prevent_constrained_type_specifiers
;
6548 type
= cp_parser_elaborated_type_specifier (parser
,
6549 /*is_friend=*/false,
6550 /*is_declaration=*/false);
6551 --parser
->prevent_constrained_type_specifiers
;
6552 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6556 case RID_CILK_SPAWN
:
6558 location_t cilk_spawn_loc
6559 = cp_lexer_peek_token (parser
->lexer
)->location
;
6560 cp_lexer_consume_token (parser
->lexer
);
6561 token
= cp_lexer_peek_token (parser
->lexer
);
6562 if (token
->type
== CPP_SEMICOLON
)
6564 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6566 postfix_expression
= error_mark_node
;
6569 else if (!current_function_decl
)
6571 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6572 "inside a function");
6573 postfix_expression
= error_mark_node
;
6578 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6579 saved_in_statement
= parser
->in_statement
;
6580 parser
->in_statement
|= IN_CILK_SPAWN
;
6582 cfun
->calls_cilk_spawn
= 1;
6583 postfix_expression
=
6584 cp_parser_postfix_expression (parser
, false, false,
6585 false, false, &idk
);
6588 error_at (token
->location
, "-fcilkplus must be enabled to use"
6589 " %<_Cilk_spawn%>");
6590 cfun
->calls_cilk_spawn
= 0;
6592 else if (saved_in_statement
& IN_CILK_SPAWN
)
6594 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6595 "are not permitted");
6596 postfix_expression
= error_mark_node
;
6597 cfun
->calls_cilk_spawn
= 0;
6601 location_t loc
= postfix_expression
.get_location ();
6602 postfix_expression
= build_cilk_spawn (token
->location
,
6603 postfix_expression
);
6604 /* Build a location of the form:
6607 with caret at the expr, ranging from the start of the
6608 _Cilk_spawn token to the end of the expression. */
6609 location_t combined_loc
=
6610 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6611 postfix_expression
.set_location (combined_loc
);
6612 if (postfix_expression
!= error_mark_node
)
6613 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6614 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6620 case RID_BUILTIN_SHUFFLE
:
6621 case RID_BUILTIN_LAUNDER
:
6623 vec
<tree
, va_gc
> *vec
;
6627 cp_lexer_consume_token (parser
->lexer
);
6628 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6629 /*cast_p=*/false, /*allow_expansion_p=*/true,
6630 /*non_constant_p=*/NULL
);
6633 postfix_expression
= error_mark_node
;
6637 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6643 if (vec
->length () == 1)
6645 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6648 error_at (loc
, "wrong number of arguments to "
6649 "%<__builtin_addressof%>");
6650 postfix_expression
= error_mark_node
;
6654 case RID_BUILTIN_LAUNDER
:
6655 if (vec
->length () == 1)
6656 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6657 tf_warning_or_error
);
6660 error_at (loc
, "wrong number of arguments to "
6661 "%<__builtin_launder%>");
6662 postfix_expression
= error_mark_node
;
6666 case RID_BUILTIN_SHUFFLE
:
6667 if (vec
->length () == 2)
6669 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6670 (*vec
)[1], tf_warning_or_error
);
6671 else if (vec
->length () == 3)
6673 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6674 (*vec
)[2], tf_warning_or_error
);
6677 error_at (loc
, "wrong number of arguments to "
6678 "%<__builtin_shuffle%>");
6679 postfix_expression
= error_mark_node
;
6693 /* If the next thing is a simple-type-specifier, we may be
6694 looking at a functional cast. We could also be looking at
6695 an id-expression. So, we try the functional cast, and if
6696 that doesn't work we fall back to the primary-expression. */
6697 cp_parser_parse_tentatively (parser
);
6698 /* Look for the simple-type-specifier. */
6699 ++parser
->prevent_constrained_type_specifiers
;
6700 type
= cp_parser_simple_type_specifier (parser
,
6701 /*decl_specs=*/NULL
,
6702 CP_PARSER_FLAGS_NONE
);
6703 --parser
->prevent_constrained_type_specifiers
;
6704 /* Parse the cast itself. */
6705 if (!cp_parser_error_occurred (parser
))
6707 = cp_parser_functional_cast (parser
, type
);
6708 /* If that worked, we're done. */
6709 if (cp_parser_parse_definitely (parser
))
6712 /* If the functional-cast didn't work out, try a
6713 compound-literal. */
6714 if (cp_parser_allow_gnu_extensions_p (parser
)
6715 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6717 cp_expr initializer
= NULL_TREE
;
6719 cp_parser_parse_tentatively (parser
);
6721 /* Avoid calling cp_parser_type_id pointlessly, see comment
6722 in cp_parser_cast_expression about c++/29234. */
6723 if (!cp_parser_compound_literal_p (parser
))
6724 cp_parser_simulate_error (parser
);
6727 /* Parse the type. */
6728 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6729 parser
->in_type_id_in_expr_p
= true;
6730 type
= cp_parser_type_id (parser
);
6731 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6732 /* Look for the `)'. */
6733 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6736 /* If things aren't going well, there's no need to
6738 if (!cp_parser_error_occurred (parser
))
6740 bool non_constant_p
;
6741 /* Parse the brace-enclosed initializer list. */
6742 initializer
= cp_parser_braced_list (parser
,
6745 /* If that worked, we're definitely looking at a
6746 compound-literal expression. */
6747 if (cp_parser_parse_definitely (parser
))
6749 /* Warn the user that a compound literal is not
6750 allowed in standard C++. */
6751 pedwarn (input_location
, OPT_Wpedantic
,
6752 "ISO C++ forbids compound-literals");
6753 /* For simplicity, we disallow compound literals in
6754 constant-expressions. We could
6755 allow compound literals of integer type, whose
6756 initializer was a constant, in constant
6757 expressions. Permitting that usage, as a further
6758 extension, would not change the meaning of any
6759 currently accepted programs. (Of course, as
6760 compound literals are not part of ISO C++, the
6761 standard has nothing to say.) */
6762 if (cp_parser_non_integral_constant_expression (parser
,
6765 postfix_expression
= error_mark_node
;
6768 /* Form the representation of the compound-literal. */
6770 = finish_compound_literal (type
, initializer
,
6771 tf_warning_or_error
);
6772 postfix_expression
.set_location (initializer
.get_location ());
6777 /* It must be a primary-expression. */
6779 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6780 /*template_arg_p=*/false,
6787 /* Note that we don't need to worry about calling build_cplus_new on a
6788 class-valued CALL_EXPR in decltype when it isn't the end of the
6789 postfix-expression; unary_complex_lvalue will take care of that for
6792 /* Keep looping until the postfix-expression is complete. */
6795 if (idk
== CP_ID_KIND_UNQUALIFIED
6796 && identifier_p (postfix_expression
)
6797 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6798 /* It is not a Koenig lookup function call. */
6800 = unqualified_name_lookup_error (postfix_expression
);
6802 /* Peek at the next token. */
6803 token
= cp_lexer_peek_token (parser
->lexer
);
6805 switch (token
->type
)
6807 case CPP_OPEN_SQUARE
:
6808 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6810 cp_parser_error (parser
,
6811 "two consecutive %<[%> shall "
6812 "only introduce an attribute");
6813 return error_mark_node
;
6816 = cp_parser_postfix_open_square_expression (parser
,
6820 postfix_expression
.set_range (start_loc
,
6821 postfix_expression
.get_location ());
6823 idk
= CP_ID_KIND_NONE
;
6824 is_member_access
= false;
6827 case CPP_OPEN_PAREN
:
6828 /* postfix-expression ( expression-list [opt] ) */
6831 bool is_builtin_constant_p
;
6832 bool saved_integral_constant_expression_p
= false;
6833 bool saved_non_integral_constant_expression_p
= false;
6834 tsubst_flags_t complain
= complain_flags (decltype_p
);
6835 vec
<tree
, va_gc
> *args
;
6836 location_t close_paren_loc
= UNKNOWN_LOCATION
;
6838 is_member_access
= false;
6840 is_builtin_constant_p
6841 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6842 if (is_builtin_constant_p
)
6844 /* The whole point of __builtin_constant_p is to allow
6845 non-constant expressions to appear as arguments. */
6846 saved_integral_constant_expression_p
6847 = parser
->integral_constant_expression_p
;
6848 saved_non_integral_constant_expression_p
6849 = parser
->non_integral_constant_expression_p
;
6850 parser
->integral_constant_expression_p
= false;
6852 args
= (cp_parser_parenthesized_expression_list
6854 /*cast_p=*/false, /*allow_expansion_p=*/true,
6855 /*non_constant_p=*/NULL
,
6856 /*close_paren_loc=*/&close_paren_loc
));
6857 if (is_builtin_constant_p
)
6859 parser
->integral_constant_expression_p
6860 = saved_integral_constant_expression_p
;
6861 parser
->non_integral_constant_expression_p
6862 = saved_non_integral_constant_expression_p
;
6867 postfix_expression
= error_mark_node
;
6871 /* Function calls are not permitted in
6872 constant-expressions. */
6873 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6874 && cp_parser_non_integral_constant_expression (parser
,
6877 postfix_expression
= error_mark_node
;
6878 release_tree_vector (args
);
6883 if (idk
== CP_ID_KIND_UNQUALIFIED
6884 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6886 if (identifier_p (postfix_expression
))
6888 if (!args
->is_empty ())
6891 if (!any_type_dependent_arguments_p (args
))
6893 = perform_koenig_lookup (postfix_expression
, args
,
6898 = unqualified_fn_lookup_error (postfix_expression
);
6900 /* We do not perform argument-dependent lookup if
6901 normal lookup finds a non-function, in accordance
6902 with the expected resolution of DR 218. */
6903 else if (!args
->is_empty ()
6904 && is_overloaded_fn (postfix_expression
))
6906 tree fn
= get_first_fn (postfix_expression
);
6907 fn
= STRIP_TEMPLATE (fn
);
6909 /* Do not do argument dependent lookup if regular
6910 lookup finds a member function or a block-scope
6911 function declaration. [basic.lookup.argdep]/3 */
6912 if (!DECL_FUNCTION_MEMBER_P (fn
)
6913 && !DECL_LOCAL_FUNCTION_P (fn
))
6916 if (!any_type_dependent_arguments_p (args
))
6918 = perform_koenig_lookup (postfix_expression
, args
,
6924 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6925 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6926 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6927 && vec_safe_length (args
) == 3)
6929 tree arg0
= (*args
)[0];
6930 tree arg1
= (*args
)[1];
6931 tree arg2
= (*args
)[2];
6932 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
6933 | (!!integer_zerop (arg2
) << 2));
6934 if (TREE_CODE (arg2
) == CONST_DECL
)
6935 arg2
= DECL_INITIAL (arg2
);
6936 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
6939 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6944 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6945 TREE_VISITED (arg
) = 0;
6947 unsigned param_pos
= 0;
6948 for (tree decl
= DECL_ARGUMENTS (postfix_expression
);
6950 decl
= DECL_CHAIN (decl
), param_pos
++)
6952 tree type
= TREE_TYPE (decl
);
6953 if (POINTER_TYPE_P (type
) && TYPE_RESTRICT (type
)
6954 && !TYPE_READONLY (TREE_TYPE (type
)))
6955 warn_for_restrict (param_pos
, args
);
6958 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6959 TREE_VISITED (arg
) = 0;
6962 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6964 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6965 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6967 if (processing_template_decl
6968 && (type_dependent_object_expression_p (instance
)
6969 || (!BASELINK_P (fn
)
6970 && TREE_CODE (fn
) != FIELD_DECL
)
6971 || type_dependent_expression_p (fn
)
6972 || any_type_dependent_arguments_p (args
)))
6974 maybe_generic_this_capture (instance
, fn
);
6976 = build_nt_call_vec (postfix_expression
, args
);
6977 release_tree_vector (args
);
6981 if (BASELINK_P (fn
))
6984 = (build_new_method_call
6985 (instance
, fn
, &args
, NULL_TREE
,
6986 (idk
== CP_ID_KIND_QUALIFIED
6987 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6994 = finish_call_expr (postfix_expression
, &args
,
6995 /*disallow_virtual=*/false,
6999 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7000 || TREE_CODE (postfix_expression
) == MEMBER_REF
7001 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7002 postfix_expression
= (build_offset_ref_call_from_tree
7003 (postfix_expression
, &args
,
7005 else if (idk
== CP_ID_KIND_QUALIFIED
)
7006 /* A call to a static class member, or a namespace-scope
7009 = finish_call_expr (postfix_expression
, &args
,
7010 /*disallow_virtual=*/true,
7014 /* All other function calls. */
7016 = finish_call_expr (postfix_expression
, &args
,
7017 /*disallow_virtual=*/false,
7021 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7023 location_t combined_loc
= make_location (token
->location
,
7026 postfix_expression
.set_location (combined_loc
);
7029 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7030 idk
= CP_ID_KIND_NONE
;
7032 release_tree_vector (args
);
7038 /* postfix-expression . template [opt] id-expression
7039 postfix-expression . pseudo-destructor-name
7040 postfix-expression -> template [opt] id-expression
7041 postfix-expression -> pseudo-destructor-name */
7043 /* Consume the `.' or `->' operator. */
7044 cp_lexer_consume_token (parser
->lexer
);
7047 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7051 is_member_access
= true;
7055 /* postfix-expression ++ */
7056 /* Consume the `++' token. */
7057 cp_lexer_consume_token (parser
->lexer
);
7058 /* Generate a representation for the complete expression. */
7060 = finish_increment_expr (postfix_expression
,
7061 POSTINCREMENT_EXPR
);
7062 /* Increments may not appear in constant-expressions. */
7063 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7064 postfix_expression
= error_mark_node
;
7065 idk
= CP_ID_KIND_NONE
;
7066 is_member_access
= false;
7069 case CPP_MINUS_MINUS
:
7070 /* postfix-expression -- */
7071 /* Consume the `--' token. */
7072 cp_lexer_consume_token (parser
->lexer
);
7073 /* Generate a representation for the complete expression. */
7075 = finish_increment_expr (postfix_expression
,
7076 POSTDECREMENT_EXPR
);
7077 /* Decrements may not appear in constant-expressions. */
7078 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7079 postfix_expression
= error_mark_node
;
7080 idk
= CP_ID_KIND_NONE
;
7081 is_member_access
= false;
7085 if (pidk_return
!= NULL
)
7086 * pidk_return
= idk
;
7087 if (member_access_only_p
)
7088 return is_member_access
7089 ? postfix_expression
7090 : cp_expr (error_mark_node
);
7092 return postfix_expression
;
7096 /* We should never get here. */
7098 return error_mark_node
;
7101 /* This function parses Cilk Plus array notations. If a normal array expr. is
7102 parsed then the array index is passed back to the caller through *INIT_INDEX
7103 and the function returns a NULL_TREE. If array notation expr. is parsed,
7104 then *INIT_INDEX is ignored by the caller and the function returns
7105 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7109 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
7112 cp_token
*token
= NULL
;
7113 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
7114 if (!array_value
|| array_value
== error_mark_node
)
7116 cp_parser_skip_to_end_of_statement (parser
);
7117 return error_mark_node
;
7120 array_type
= TREE_TYPE (array_value
);
7122 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7123 parser
->colon_corrects_to_scope_p
= false;
7124 token
= cp_lexer_peek_token (parser
->lexer
);
7128 cp_parser_error (parser
, "expected %<:%> or numeral");
7129 return error_mark_node
;
7131 else if (token
->type
== CPP_COLON
)
7133 /* Consume the ':'. */
7134 cp_lexer_consume_token (parser
->lexer
);
7136 /* If we are here, then we have a case like this A[:]. */
7137 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7139 cp_parser_error (parser
, "expected %<]%>");
7140 cp_parser_skip_to_end_of_statement (parser
);
7141 return error_mark_node
;
7143 *init_index
= NULL_TREE
;
7145 length_index
= NULL_TREE
;
7149 /* If we are here, then there are three valid possibilities:
7151 2. ARRAY [ EXP : EXP ]
7152 3. ARRAY [ EXP : EXP : EXP ] */
7154 *init_index
= cp_parser_expression (parser
);
7155 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7157 /* This indicates that we have a normal array expression. */
7158 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7162 /* Consume the ':'. */
7163 cp_lexer_consume_token (parser
->lexer
);
7164 length_index
= cp_parser_expression (parser
);
7165 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7167 cp_lexer_consume_token (parser
->lexer
);
7168 stride
= cp_parser_expression (parser
);
7171 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7173 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7174 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7176 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7177 cp_lexer_consume_token (parser
->lexer
);
7178 return error_mark_node
;
7180 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7182 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7183 length_index
, stride
, array_type
);
7187 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7188 by cp_parser_builtin_offsetof. We're looking for
7190 postfix-expression [ expression ]
7191 postfix-expression [ braced-init-list ] (C++11)
7193 FOR_OFFSETOF is set if we're being called in that context, which
7194 changes how we deal with integer constant expressions. */
7197 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7198 tree postfix_expression
,
7202 tree index
= NULL_TREE
;
7203 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7204 bool saved_greater_than_is_operator_p
;
7206 /* Consume the `[' token. */
7207 cp_lexer_consume_token (parser
->lexer
);
7209 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7210 parser
->greater_than_is_operator_p
= true;
7212 /* Parse the index expression. */
7213 /* ??? For offsetof, there is a question of what to allow here. If
7214 offsetof is not being used in an integral constant expression context,
7215 then we *could* get the right answer by computing the value at runtime.
7216 If we are in an integral constant expression context, then we might
7217 could accept any constant expression; hard to say without analysis.
7218 Rather than open the barn door too wide right away, allow only integer
7219 constant expressions here. */
7221 index
= cp_parser_constant_expression (parser
);
7224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7226 bool expr_nonconst_p
;
7227 cp_lexer_set_source_position (parser
->lexer
);
7228 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7229 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7231 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7233 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7234 "braced list index is not allowed with array "
7236 cp_parser_skip_to_end_of_statement (parser
);
7237 return error_mark_node
;
7240 else if (flag_cilkplus
)
7242 /* Here are have these two options:
7243 ARRAY[EXP : EXP] - Array notation expr with default
7245 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7247 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7248 postfix_expression
);
7253 index
= cp_parser_expression (parser
);
7256 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7258 /* Look for the closing `]'. */
7259 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7261 /* Build the ARRAY_REF. */
7262 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7265 /* When not doing offsetof, array references are not permitted in
7266 constant-expressions. */
7268 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7269 postfix_expression
= error_mark_node
;
7271 return postfix_expression
;
7274 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7275 by cp_parser_builtin_offsetof. We're looking for
7277 postfix-expression . template [opt] id-expression
7278 postfix-expression . pseudo-destructor-name
7279 postfix-expression -> template [opt] id-expression
7280 postfix-expression -> pseudo-destructor-name
7282 FOR_OFFSETOF is set if we're being called in that context. That sorta
7283 limits what of the above we'll actually accept, but nevermind.
7284 TOKEN_TYPE is the "." or "->" token, which will already have been
7285 removed from the stream. */
7288 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7289 enum cpp_ttype token_type
,
7290 cp_expr postfix_expression
,
7291 bool for_offsetof
, cp_id_kind
*idk
,
7292 location_t location
)
7296 bool pseudo_destructor_p
;
7297 tree scope
= NULL_TREE
;
7298 location_t start_loc
= postfix_expression
.get_start ();
7300 /* If this is a `->' operator, dereference the pointer. */
7301 if (token_type
== CPP_DEREF
)
7302 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7303 tf_warning_or_error
);
7304 /* Check to see whether or not the expression is type-dependent and
7305 not the current instantiation. */
7306 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7307 /* The identifier following the `->' or `.' is not qualified. */
7308 parser
->scope
= NULL_TREE
;
7309 parser
->qualifying_scope
= NULL_TREE
;
7310 parser
->object_scope
= NULL_TREE
;
7311 *idk
= CP_ID_KIND_NONE
;
7313 /* Enter the scope corresponding to the type of the object
7314 given by the POSTFIX_EXPRESSION. */
7315 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
7317 scope
= TREE_TYPE (postfix_expression
);
7318 /* According to the standard, no expression should ever have
7319 reference type. Unfortunately, we do not currently match
7320 the standard in this respect in that our internal representation
7321 of an expression may have reference type even when the standard
7322 says it does not. Therefore, we have to manually obtain the
7323 underlying type here. */
7324 scope
= non_reference (scope
);
7325 /* The type of the POSTFIX_EXPRESSION must be complete. */
7326 /* Unlike the object expression in other contexts, *this is not
7327 required to be of complete type for purposes of class member
7328 access (5.2.5) outside the member function body. */
7329 if (postfix_expression
!= current_class_ref
7330 && !(processing_template_decl
7331 && current_class_type
7332 && (same_type_ignoring_top_level_qualifiers_p
7333 (scope
, current_class_type
))))
7334 scope
= complete_type_or_else (scope
, postfix_expression
);
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
;
7350 /* Tell cp_parser_lookup_name that there was an object, even though it's
7352 parser
->context
->object_type
= unknown_type_node
;
7354 /* Assume this expression is not a pseudo-destructor access. */
7355 pseudo_destructor_p
= false;
7357 /* If the SCOPE is a scalar type, then, if this is a valid program,
7358 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7359 is type dependent, it can be pseudo-destructor-name or something else.
7360 Try to parse it as pseudo-destructor-name first. */
7361 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7366 cp_parser_parse_tentatively (parser
);
7367 /* Parse the pseudo-destructor-name. */
7369 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7372 && (cp_parser_error_occurred (parser
)
7373 || !SCALAR_TYPE_P (type
)))
7374 cp_parser_abort_tentative_parse (parser
);
7375 else if (cp_parser_parse_definitely (parser
))
7377 pseudo_destructor_p
= true;
7379 = finish_pseudo_destructor_expr (postfix_expression
,
7384 if (!pseudo_destructor_p
)
7386 /* If the SCOPE is not a scalar type, we are looking at an
7387 ordinary class member access expression, rather than a
7388 pseudo-destructor-name. */
7390 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7391 /* Parse the id-expression. */
7392 name
= (cp_parser_id_expression
7394 cp_parser_optional_template_keyword (parser
),
7395 /*check_dependency_p=*/true,
7397 /*declarator_p=*/false,
7398 /*optional_p=*/false));
7399 /* In general, build a SCOPE_REF if the member name is qualified.
7400 However, if the name was not dependent and has already been
7401 resolved; there is no need to build the SCOPE_REF. For example;
7403 struct X { void f(); };
7404 template <typename T> void f(T* t) { t->X::f(); }
7406 Even though "t" is dependent, "X::f" is not and has been resolved
7407 to a BASELINK; there is no need to include scope information. */
7409 /* But we do need to remember that there was an explicit scope for
7410 virtual function calls. */
7412 *idk
= CP_ID_KIND_QUALIFIED
;
7414 /* If the name is a template-id that names a type, we will get a
7415 TYPE_DECL here. That is invalid code. */
7416 if (TREE_CODE (name
) == TYPE_DECL
)
7418 error_at (token
->location
, "invalid use of %qD", name
);
7419 postfix_expression
= error_mark_node
;
7423 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7425 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7427 error_at (token
->location
, "%<%D::%D%> is not a class member",
7428 parser
->scope
, name
);
7429 postfix_expression
= error_mark_node
;
7432 name
= build_qualified_name (/*type=*/NULL_TREE
,
7436 parser
->scope
= NULL_TREE
;
7437 parser
->qualifying_scope
= NULL_TREE
;
7438 parser
->object_scope
= NULL_TREE
;
7440 if (parser
->scope
&& name
&& BASELINK_P (name
))
7441 adjust_result_of_qualified_name_lookup
7442 (name
, parser
->scope
, scope
);
7444 = finish_class_member_access_expr (postfix_expression
, name
,
7446 tf_warning_or_error
);
7447 /* Build a location e.g.:
7450 where the caret is at the deref token, ranging from
7451 the start of postfix_expression to the end of the access expr. */
7453 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7454 location_t combined_loc
7455 = make_location (input_location
, start_loc
, end_loc
);
7456 protected_set_expr_location (postfix_expression
, combined_loc
);
7460 /* We no longer need to look up names in the scope of the object on
7461 the left-hand side of the `.' or `->' operator. */
7462 parser
->context
->object_type
= NULL_TREE
;
7464 /* Outside of offsetof, these operators may not appear in
7465 constant-expressions. */
7467 && (cp_parser_non_integral_constant_expression
7468 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7469 postfix_expression
= error_mark_node
;
7471 return postfix_expression
;
7474 /* Parse a parenthesized expression-list.
7477 assignment-expression
7478 expression-list, assignment-expression
7483 identifier, expression-list
7485 CAST_P is true if this expression is the target of a cast.
7487 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7490 Returns a vector of trees. Each element is a representation of an
7491 assignment-expression. NULL is returned if the ( and or ) are
7492 missing. An empty, but allocated, vector is returned on no
7493 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7494 if we are parsing an attribute list for an attribute that wants a
7495 plain identifier argument, normal_attr for an attribute that wants
7496 an expression, or non_attr if we aren't parsing an attribute list. If
7497 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7498 not all of the expressions in the list were constant.
7499 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7500 will be written to with the location of the closing parenthesis. If
7501 an error occurs, it may or may not be written to. */
7503 static vec
<tree
, va_gc
> *
7504 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7505 int is_attribute_list
,
7507 bool allow_expansion_p
,
7508 bool *non_constant_p
,
7509 location_t
*close_paren_loc
)
7511 vec
<tree
, va_gc
> *expression_list
;
7512 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7513 tree identifier
= NULL_TREE
;
7514 bool saved_greater_than_is_operator_p
;
7516 /* Assume all the expressions will be constant. */
7518 *non_constant_p
= false;
7520 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7523 expression_list
= make_tree_vector ();
7525 /* Within a parenthesized expression, a `>' token is always
7526 the greater-than operator. */
7527 saved_greater_than_is_operator_p
7528 = parser
->greater_than_is_operator_p
;
7529 parser
->greater_than_is_operator_p
= true;
7531 /* Consume expressions until there are no more. */
7532 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7537 /* At the beginning of attribute lists, check to see if the
7538 next token is an identifier. */
7539 if (is_attribute_list
== id_attr
7540 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7544 /* Consume the identifier. */
7545 token
= cp_lexer_consume_token (parser
->lexer
);
7546 /* Save the identifier. */
7547 identifier
= token
->u
.value
;
7551 bool expr_non_constant_p
;
7553 /* Parse the next assignment-expression. */
7554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7556 /* A braced-init-list. */
7557 cp_lexer_set_source_position (parser
->lexer
);
7558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7559 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7560 if (non_constant_p
&& expr_non_constant_p
)
7561 *non_constant_p
= true;
7563 else if (non_constant_p
)
7565 expr
= (cp_parser_constant_expression
7566 (parser
, /*allow_non_constant_p=*/true,
7567 &expr_non_constant_p
));
7568 if (expr_non_constant_p
)
7569 *non_constant_p
= true;
7572 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7576 expr
= instantiate_non_dependent_expr (expr
);
7578 /* If we have an ellipsis, then this is an expression
7580 if (allow_expansion_p
7581 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7583 /* Consume the `...'. */
7584 cp_lexer_consume_token (parser
->lexer
);
7586 /* Build the argument pack. */
7587 expr
= make_pack_expansion (expr
);
7590 /* Add it to the list. We add error_mark_node
7591 expressions to the list, so that we can still tell if
7592 the correct form for a parenthesized expression-list
7593 is found. That gives better errors. */
7594 vec_safe_push (expression_list
, expr
);
7596 if (expr
== error_mark_node
)
7600 /* After the first item, attribute lists look the same as
7601 expression lists. */
7602 is_attribute_list
= non_attr
;
7605 /* If the next token isn't a `,', then we are done. */
7606 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7609 /* Otherwise, consume the `,' and keep going. */
7610 cp_lexer_consume_token (parser
->lexer
);
7613 if (close_paren_loc
)
7614 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7616 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7621 /* We try and resync to an unnested comma, as that will give the
7622 user better diagnostics. */
7623 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7624 /*recovering=*/true,
7626 /*consume_paren=*/true);
7631 parser
->greater_than_is_operator_p
7632 = saved_greater_than_is_operator_p
;
7637 parser
->greater_than_is_operator_p
7638 = saved_greater_than_is_operator_p
;
7641 vec_safe_insert (expression_list
, 0, identifier
);
7643 return expression_list
;
7646 /* Parse a pseudo-destructor-name.
7648 pseudo-destructor-name:
7649 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7650 :: [opt] nested-name-specifier template template-id :: ~ type-name
7651 :: [opt] nested-name-specifier [opt] ~ type-name
7653 If either of the first two productions is used, sets *SCOPE to the
7654 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7655 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7656 or ERROR_MARK_NODE if the parse fails. */
7659 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7664 bool nested_name_specifier_p
;
7667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7668 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7669 && !type_dependent_expression_p (object
))
7671 if (cxx_dialect
< cxx14
)
7672 pedwarn (input_location
, 0,
7673 "%<~auto%> only available with "
7674 "-std=c++14 or -std=gnu++14");
7675 cp_lexer_consume_token (parser
->lexer
);
7676 cp_lexer_consume_token (parser
->lexer
);
7678 *type
= TREE_TYPE (object
);
7682 /* Assume that things will not work out. */
7683 *type
= error_mark_node
;
7685 /* Look for the optional `::' operator. */
7686 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7687 /* Look for the optional nested-name-specifier. */
7688 nested_name_specifier_p
7689 = (cp_parser_nested_name_specifier_opt (parser
,
7690 /*typename_keyword_p=*/false,
7691 /*check_dependency_p=*/true,
7693 /*is_declaration=*/false)
7695 /* Now, if we saw a nested-name-specifier, we might be doing the
7696 second production. */
7697 if (nested_name_specifier_p
7698 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7700 /* Consume the `template' keyword. */
7701 cp_lexer_consume_token (parser
->lexer
);
7702 /* Parse the template-id. */
7703 cp_parser_template_id (parser
,
7704 /*template_keyword_p=*/true,
7705 /*check_dependency_p=*/false,
7707 /*is_declaration=*/true);
7708 /* Look for the `::' token. */
7709 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7711 /* If the next token is not a `~', then there might be some
7712 additional qualification. */
7713 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7715 /* At this point, we're looking for "type-name :: ~". The type-name
7716 must not be a class-name, since this is a pseudo-destructor. So,
7717 it must be either an enum-name, or a typedef-name -- both of which
7718 are just identifiers. So, we peek ahead to check that the "::"
7719 and "~" tokens are present; if they are not, then we can avoid
7720 calling type_name. */
7721 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7722 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7723 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7725 cp_parser_error (parser
, "non-scalar type");
7729 /* Look for the type-name. */
7730 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7731 if (*scope
== error_mark_node
)
7734 /* Look for the `::' token. */
7735 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7740 /* Look for the `~'. */
7741 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7743 /* Once we see the ~, this has to be a pseudo-destructor. */
7744 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7745 cp_parser_commit_to_topmost_tentative_parse (parser
);
7747 /* Look for the type-name again. We are not responsible for
7748 checking that it matches the first type-name. */
7749 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7752 /* Parse a unary-expression.
7758 unary-operator cast-expression
7759 sizeof unary-expression
7761 alignof ( type-id ) [C++0x]
7768 __extension__ cast-expression
7769 __alignof__ unary-expression
7770 __alignof__ ( type-id )
7771 alignof unary-expression [C++0x]
7772 __real__ cast-expression
7773 __imag__ cast-expression
7775 sizeof ( type-id ) { initializer-list , [opt] }
7776 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7777 __alignof__ ( type-id ) { initializer-list , [opt] }
7779 ADDRESS_P is true iff the unary-expression is appearing as the
7780 operand of the `&' operator. CAST_P is true if this expression is
7781 the target of a cast.
7783 Returns a representation of the expression. */
7786 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7787 bool address_p
, bool cast_p
, bool decltype_p
)
7790 enum tree_code unary_operator
;
7792 /* Peek at the next token. */
7793 token
= cp_lexer_peek_token (parser
->lexer
);
7794 /* Some keywords give away the kind of expression. */
7795 if (token
->type
== CPP_KEYWORD
)
7797 enum rid keyword
= token
->keyword
;
7806 location_t first_loc
;
7808 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7809 /* Consume the token. */
7810 cp_lexer_consume_token (parser
->lexer
);
7811 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7812 /* Parse the operand. */
7813 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7815 if (TYPE_P (operand
))
7816 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7819 /* ISO C++ defines alignof only with types, not with
7820 expressions. So pedwarn if alignof is used with a non-
7821 type expression. However, __alignof__ is ok. */
7822 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7823 pedwarn (token
->location
, OPT_Wpedantic
,
7824 "ISO C++ does not allow %<alignof%> "
7827 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7829 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7830 SIZEOF_EXPR with the original operand. */
7831 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7833 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7835 if (!processing_template_decl
&& TYPE_P (operand
))
7837 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7838 build1 (NOP_EXPR
, operand
,
7840 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7843 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7844 TREE_SIDE_EFFECTS (ret
) = 0;
7845 TREE_READONLY (ret
) = 1;
7847 SET_EXPR_LOCATION (ret
, first_loc
);
7853 return cp_parser_new_expression (parser
);
7856 return cp_parser_delete_expression (parser
);
7860 /* The saved value of the PEDANTIC flag. */
7864 /* Save away the PEDANTIC flag. */
7865 cp_parser_extension_opt (parser
, &saved_pedantic
);
7866 /* Parse the cast-expression. */
7867 expr
= cp_parser_simple_cast_expression (parser
);
7868 /* Restore the PEDANTIC flag. */
7869 pedantic
= saved_pedantic
;
7879 /* Consume the `__real__' or `__imag__' token. */
7880 cp_lexer_consume_token (parser
->lexer
);
7881 /* Parse the cast-expression. */
7882 expression
= cp_parser_simple_cast_expression (parser
);
7883 /* Create the complete representation. */
7884 return build_x_unary_op (token
->location
,
7885 (keyword
== RID_REALPART
7886 ? REALPART_EXPR
: IMAGPART_EXPR
),
7888 tf_warning_or_error
);
7892 case RID_TRANSACTION_ATOMIC
:
7893 case RID_TRANSACTION_RELAXED
:
7894 return cp_parser_transaction_expression (parser
, keyword
);
7899 const char *saved_message
;
7900 bool saved_integral_constant_expression_p
;
7901 bool saved_non_integral_constant_expression_p
;
7902 bool saved_greater_than_is_operator_p
;
7904 cp_lexer_consume_token (parser
->lexer
);
7905 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7907 saved_message
= parser
->type_definition_forbidden_message
;
7908 parser
->type_definition_forbidden_message
7909 = G_("types may not be defined in %<noexcept%> expressions");
7911 saved_integral_constant_expression_p
7912 = parser
->integral_constant_expression_p
;
7913 saved_non_integral_constant_expression_p
7914 = parser
->non_integral_constant_expression_p
;
7915 parser
->integral_constant_expression_p
= false;
7917 saved_greater_than_is_operator_p
7918 = parser
->greater_than_is_operator_p
;
7919 parser
->greater_than_is_operator_p
= true;
7921 ++cp_unevaluated_operand
;
7922 ++c_inhibit_evaluation_warnings
;
7923 ++cp_noexcept_operand
;
7924 expr
= cp_parser_expression (parser
);
7925 --cp_noexcept_operand
;
7926 --c_inhibit_evaluation_warnings
;
7927 --cp_unevaluated_operand
;
7929 parser
->greater_than_is_operator_p
7930 = saved_greater_than_is_operator_p
;
7932 parser
->integral_constant_expression_p
7933 = saved_integral_constant_expression_p
;
7934 parser
->non_integral_constant_expression_p
7935 = saved_non_integral_constant_expression_p
;
7937 parser
->type_definition_forbidden_message
= saved_message
;
7939 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7940 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7948 /* Look for the `:: new' and `:: delete', which also signal the
7949 beginning of a new-expression, or delete-expression,
7950 respectively. If the next token is `::', then it might be one of
7952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7956 /* See if the token after the `::' is one of the keywords in
7957 which we're interested. */
7958 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7959 /* If it's `new', we have a new-expression. */
7960 if (keyword
== RID_NEW
)
7961 return cp_parser_new_expression (parser
);
7962 /* Similarly, for `delete'. */
7963 else if (keyword
== RID_DELETE
)
7964 return cp_parser_delete_expression (parser
);
7967 /* Look for a unary operator. */
7968 unary_operator
= cp_parser_unary_operator (token
);
7969 /* The `++' and `--' operators can be handled similarly, even though
7970 they are not technically unary-operators in the grammar. */
7971 if (unary_operator
== ERROR_MARK
)
7973 if (token
->type
== CPP_PLUS_PLUS
)
7974 unary_operator
= PREINCREMENT_EXPR
;
7975 else if (token
->type
== CPP_MINUS_MINUS
)
7976 unary_operator
= PREDECREMENT_EXPR
;
7977 /* Handle the GNU address-of-label extension. */
7978 else if (cp_parser_allow_gnu_extensions_p (parser
)
7979 && token
->type
== CPP_AND_AND
)
7983 location_t start_loc
= token
->location
;
7985 /* Consume the '&&' token. */
7986 cp_lexer_consume_token (parser
->lexer
);
7987 /* Look for the identifier. */
7988 location_t finish_loc
7989 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
7990 identifier
= cp_parser_identifier (parser
);
7991 /* Construct a location of the form:
7994 with caret==start at the "&&", finish at the end of the label. */
7995 location_t combined_loc
7996 = make_location (start_loc
, start_loc
, finish_loc
);
7997 /* Create an expression representing the address. */
7998 expression
= finish_label_address_expr (identifier
, combined_loc
);
7999 if (cp_parser_non_integral_constant_expression (parser
,
8001 expression
= error_mark_node
;
8005 if (unary_operator
!= ERROR_MARK
)
8007 cp_expr cast_expression
;
8008 cp_expr expression
= error_mark_node
;
8009 non_integral_constant non_constant_p
= NIC_NONE
;
8010 location_t loc
= token
->location
;
8011 tsubst_flags_t complain
= complain_flags (decltype_p
);
8013 /* Consume the operator token. */
8014 token
= cp_lexer_consume_token (parser
->lexer
);
8015 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8017 /* Parse the cast-expression. */
8019 = cp_parser_cast_expression (parser
,
8020 unary_operator
== ADDR_EXPR
,
8026 OP_TOKEN CAST_EXPRESSION
8027 ^~~~~~~~~~~~~~~~~~~~~~~~~
8028 with start==caret at the operator token, and
8029 extending to the end of the cast_expression. */
8030 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8032 /* Now, build an appropriate representation. */
8033 switch (unary_operator
)
8036 non_constant_p
= NIC_STAR
;
8037 expression
= build_x_indirect_ref (loc
, cast_expression
,
8040 /* TODO: build_x_indirect_ref does not always honor the
8041 location, so ensure it is set. */
8042 expression
.set_location (loc
);
8046 non_constant_p
= NIC_ADDR
;
8049 expression
= build_x_unary_op (loc
, unary_operator
,
8052 /* TODO: build_x_unary_op does not always honor the location,
8053 so ensure it is set. */
8054 expression
.set_location (loc
);
8057 case PREINCREMENT_EXPR
:
8058 case PREDECREMENT_EXPR
:
8059 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8060 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8063 /* Immediately fold negation of a constant, unless the constant is 0
8064 (since -0 == 0) or it would overflow. */
8065 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8066 && CONSTANT_CLASS_P (cast_expression
)
8067 && !integer_zerop (cast_expression
)
8068 && !TREE_OVERFLOW (cast_expression
))
8070 tree folded
= fold_build1 (unary_operator
,
8071 TREE_TYPE (cast_expression
),
8073 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8075 expression
= cp_expr (folded
, loc
);
8080 case UNARY_PLUS_EXPR
:
8081 case TRUTH_NOT_EXPR
:
8082 expression
= finish_unary_op_expr (loc
, unary_operator
,
8083 cast_expression
, complain
);
8090 if (non_constant_p
!= NIC_NONE
8091 && cp_parser_non_integral_constant_expression (parser
,
8093 expression
= error_mark_node
;
8098 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8099 /*member_access_only_p=*/false,
8104 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8105 unary-operator, the corresponding tree code is returned. */
8107 static enum tree_code
8108 cp_parser_unary_operator (cp_token
* token
)
8110 switch (token
->type
)
8113 return INDIRECT_REF
;
8119 return UNARY_PLUS_EXPR
;
8125 return TRUTH_NOT_EXPR
;
8128 return BIT_NOT_EXPR
;
8135 /* Parse a new-expression.
8138 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8139 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8141 Returns a representation of the expression. */
8144 cp_parser_new_expression (cp_parser
* parser
)
8146 bool global_scope_p
;
8147 vec
<tree
, va_gc
> *placement
;
8149 vec
<tree
, va_gc
> *initializer
;
8150 tree nelts
= NULL_TREE
;
8153 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8155 /* Look for the optional `::' operator. */
8157 = (cp_parser_global_scope_opt (parser
,
8158 /*current_scope_valid_p=*/false)
8160 /* Look for the `new' operator. */
8161 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8162 /* There's no easy way to tell a new-placement from the
8163 `( type-id )' construct. */
8164 cp_parser_parse_tentatively (parser
);
8165 /* Look for a new-placement. */
8166 placement
= cp_parser_new_placement (parser
);
8167 /* If that didn't work out, there's no new-placement. */
8168 if (!cp_parser_parse_definitely (parser
))
8170 if (placement
!= NULL
)
8171 release_tree_vector (placement
);
8175 /* If the next token is a `(', then we have a parenthesized
8177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8180 const char *saved_message
= parser
->type_definition_forbidden_message
;
8182 /* Consume the `('. */
8183 cp_lexer_consume_token (parser
->lexer
);
8185 /* Parse the type-id. */
8186 parser
->type_definition_forbidden_message
8187 = G_("types may not be defined in a new-expression");
8189 type_id_in_expr_sentinel
s (parser
);
8190 type
= cp_parser_type_id (parser
);
8192 parser
->type_definition_forbidden_message
= saved_message
;
8194 /* Look for the closing `)'. */
8195 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8196 token
= cp_lexer_peek_token (parser
->lexer
);
8197 /* There should not be a direct-new-declarator in this production,
8198 but GCC used to allowed this, so we check and emit a sensible error
8199 message for this case. */
8200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8202 error_at (token
->location
,
8203 "array bound forbidden after parenthesized type-id");
8204 inform (token
->location
,
8205 "try removing the parentheses around the type-id");
8206 cp_parser_direct_new_declarator (parser
);
8209 /* Otherwise, there must be a new-type-id. */
8211 type
= cp_parser_new_type_id (parser
, &nelts
);
8213 /* If the next token is a `(' or '{', then we have a new-initializer. */
8214 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8215 if (token
->type
== CPP_OPEN_PAREN
8216 || token
->type
== CPP_OPEN_BRACE
)
8217 initializer
= cp_parser_new_initializer (parser
);
8221 /* A new-expression may not appear in an integral constant
8223 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8224 ret
= error_mark_node
;
8225 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8226 of a new-type-id or type-id of a new-expression, the new-expression shall
8227 contain a new-initializer of the form ( assignment-expression )".
8228 Additionally, consistently with the spirit of DR 1467, we want to accept
8229 'new auto { 2 }' too. */
8230 else if (type_uses_auto (type
)
8231 && (vec_safe_length (initializer
) != 1
8232 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8233 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8235 error_at (token
->location
,
8236 "initialization of new-expression for type %<auto%> "
8237 "requires exactly one element");
8238 ret
= error_mark_node
;
8242 /* Construct a location e.g.:
8245 with caret == start at the start of the "new" token, and the end
8246 at the end of the final token we consumed. */
8247 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8248 location_t end_loc
= get_finish (end_tok
->location
);
8249 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8251 /* Create a representation of the new-expression. */
8252 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8253 tf_warning_or_error
);
8254 protected_set_expr_location (ret
, combined_loc
);
8257 if (placement
!= NULL
)
8258 release_tree_vector (placement
);
8259 if (initializer
!= NULL
)
8260 release_tree_vector (initializer
);
8265 /* Parse a new-placement.
8270 Returns the same representation as for an expression-list. */
8272 static vec
<tree
, va_gc
> *
8273 cp_parser_new_placement (cp_parser
* parser
)
8275 vec
<tree
, va_gc
> *expression_list
;
8277 /* Parse the expression-list. */
8278 expression_list
= (cp_parser_parenthesized_expression_list
8279 (parser
, non_attr
, /*cast_p=*/false,
8280 /*allow_expansion_p=*/true,
8281 /*non_constant_p=*/NULL
));
8283 if (expression_list
&& expression_list
->is_empty ())
8284 error ("expected expression-list or type-id");
8286 return expression_list
;
8289 /* Parse a new-type-id.
8292 type-specifier-seq new-declarator [opt]
8294 Returns the TYPE allocated. If the new-type-id indicates an array
8295 type, *NELTS is set to the number of elements in the last array
8296 bound; the TYPE will not include the last array bound. */
8299 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8301 cp_decl_specifier_seq type_specifier_seq
;
8302 cp_declarator
*new_declarator
;
8303 cp_declarator
*declarator
;
8304 cp_declarator
*outer_declarator
;
8305 const char *saved_message
;
8307 /* The type-specifier sequence must not contain type definitions.
8308 (It cannot contain declarations of new types either, but if they
8309 are not definitions we will catch that because they are not
8311 saved_message
= parser
->type_definition_forbidden_message
;
8312 parser
->type_definition_forbidden_message
8313 = G_("types may not be defined in a new-type-id");
8314 /* Parse the type-specifier-seq. */
8315 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8316 /*is_trailing_return=*/false,
8317 &type_specifier_seq
);
8318 /* Restore the old message. */
8319 parser
->type_definition_forbidden_message
= saved_message
;
8321 if (type_specifier_seq
.type
== error_mark_node
)
8322 return error_mark_node
;
8324 /* Parse the new-declarator. */
8325 new_declarator
= cp_parser_new_declarator_opt (parser
);
8327 /* Determine the number of elements in the last array dimension, if
8330 /* Skip down to the last array dimension. */
8331 declarator
= new_declarator
;
8332 outer_declarator
= NULL
;
8333 while (declarator
&& (declarator
->kind
== cdk_pointer
8334 || declarator
->kind
== cdk_ptrmem
))
8336 outer_declarator
= declarator
;
8337 declarator
= declarator
->declarator
;
8340 && declarator
->kind
== cdk_array
8341 && declarator
->declarator
8342 && declarator
->declarator
->kind
== cdk_array
)
8344 outer_declarator
= declarator
;
8345 declarator
= declarator
->declarator
;
8348 if (declarator
&& declarator
->kind
== cdk_array
)
8350 *nelts
= declarator
->u
.array
.bounds
;
8351 if (*nelts
== error_mark_node
)
8352 *nelts
= integer_one_node
;
8354 if (outer_declarator
)
8355 outer_declarator
->declarator
= declarator
->declarator
;
8357 new_declarator
= NULL
;
8360 return groktypename (&type_specifier_seq
, new_declarator
, false);
8363 /* Parse an (optional) new-declarator.
8366 ptr-operator new-declarator [opt]
8367 direct-new-declarator
8369 Returns the declarator. */
8371 static cp_declarator
*
8372 cp_parser_new_declarator_opt (cp_parser
* parser
)
8374 enum tree_code code
;
8375 tree type
, std_attributes
= NULL_TREE
;
8376 cp_cv_quals cv_quals
;
8378 /* We don't know if there's a ptr-operator next, or not. */
8379 cp_parser_parse_tentatively (parser
);
8380 /* Look for a ptr-operator. */
8381 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8382 /* If that worked, look for more new-declarators. */
8383 if (cp_parser_parse_definitely (parser
))
8385 cp_declarator
*declarator
;
8387 /* Parse another optional declarator. */
8388 declarator
= cp_parser_new_declarator_opt (parser
);
8390 declarator
= cp_parser_make_indirect_declarator
8391 (code
, type
, cv_quals
, declarator
, std_attributes
);
8396 /* If the next token is a `[', there is a direct-new-declarator. */
8397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8398 return cp_parser_direct_new_declarator (parser
);
8403 /* Parse a direct-new-declarator.
8405 direct-new-declarator:
8407 direct-new-declarator [constant-expression]
8411 static cp_declarator
*
8412 cp_parser_direct_new_declarator (cp_parser
* parser
)
8414 cp_declarator
*declarator
= NULL
;
8421 /* Look for the opening `['. */
8422 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8424 token
= cp_lexer_peek_token (parser
->lexer
);
8425 expression
= cp_parser_expression (parser
);
8426 /* The standard requires that the expression have integral
8427 type. DR 74 adds enumeration types. We believe that the
8428 real intent is that these expressions be handled like the
8429 expression in a `switch' condition, which also allows
8430 classes with a single conversion to integral or
8431 enumeration type. */
8432 if (!processing_template_decl
)
8435 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8440 error_at (token
->location
,
8441 "expression in new-declarator must have integral "
8442 "or enumeration type");
8443 expression
= error_mark_node
;
8447 /* Look for the closing `]'. */
8448 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8450 /* Add this bound to the declarator. */
8451 declarator
= make_array_declarator (declarator
, expression
);
8453 /* If the next token is not a `[', then there are no more
8455 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8462 /* Parse a new-initializer.
8465 ( expression-list [opt] )
8468 Returns a representation of the expression-list. */
8470 static vec
<tree
, va_gc
> *
8471 cp_parser_new_initializer (cp_parser
* parser
)
8473 vec
<tree
, va_gc
> *expression_list
;
8475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8478 bool expr_non_constant_p
;
8479 cp_lexer_set_source_position (parser
->lexer
);
8480 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8481 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8482 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8483 expression_list
= make_tree_vector_single (t
);
8486 expression_list
= (cp_parser_parenthesized_expression_list
8487 (parser
, non_attr
, /*cast_p=*/false,
8488 /*allow_expansion_p=*/true,
8489 /*non_constant_p=*/NULL
));
8491 return expression_list
;
8494 /* Parse a delete-expression.
8497 :: [opt] delete cast-expression
8498 :: [opt] delete [ ] cast-expression
8500 Returns a representation of the expression. */
8503 cp_parser_delete_expression (cp_parser
* parser
)
8505 bool global_scope_p
;
8509 /* Look for the optional `::' operator. */
8511 = (cp_parser_global_scope_opt (parser
,
8512 /*current_scope_valid_p=*/false)
8514 /* Look for the `delete' keyword. */
8515 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8516 /* See if the array syntax is in use. */
8517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8519 /* Consume the `[' token. */
8520 cp_lexer_consume_token (parser
->lexer
);
8521 /* Look for the `]' token. */
8522 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8523 /* Remember that this is the `[]' construct. */
8529 /* Parse the cast-expression. */
8530 expression
= cp_parser_simple_cast_expression (parser
);
8532 /* A delete-expression may not appear in an integral constant
8534 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8535 return error_mark_node
;
8537 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8538 tf_warning_or_error
);
8541 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8542 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8546 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8549 switch (token
->type
)
8555 case CPP_CLOSE_SQUARE
:
8556 case CPP_CLOSE_PAREN
:
8557 case CPP_CLOSE_BRACE
:
8558 case CPP_OPEN_BRACE
:
8562 case CPP_DEREF_STAR
:
8570 case CPP_GREATER_EQ
:
8591 case CPP_OPEN_PAREN
:
8592 /* In ((type ()) () the last () isn't a valid cast-expression,
8593 so the whole must be parsed as postfix-expression. */
8594 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8597 case CPP_OPEN_SQUARE
:
8598 /* '[' may start a primary-expression in obj-c++ and in C++11,
8599 as a lambda-expression, eg, '(void)[]{}'. */
8600 if (cxx_dialect
>= cxx11
)
8602 return c_dialect_objc ();
8605 case CPP_MINUS_MINUS
:
8606 /* '++' and '--' may or may not start a cast-expression:
8608 struct T { void operator++(int); };
8609 void f() { (T())++; }
8622 /* Parse a cast-expression.
8626 ( type-id ) cast-expression
8628 ADDRESS_P is true iff the unary-expression is appearing as the
8629 operand of the `&' operator. CAST_P is true if this expression is
8630 the target of a cast.
8632 Returns a representation of the expression. */
8635 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8636 bool decltype_p
, cp_id_kind
* pidk
)
8638 /* If it's a `(', then we might be looking at a cast. */
8639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8641 tree type
= NULL_TREE
;
8642 cp_expr
expr (NULL_TREE
);
8643 int cast_expression
= 0;
8644 const char *saved_message
;
8646 /* There's no way to know yet whether or not this is a cast.
8647 For example, `(int (3))' is a unary-expression, while `(int)
8648 3' is a cast. So, we resort to parsing tentatively. */
8649 cp_parser_parse_tentatively (parser
);
8650 /* Types may not be defined in a cast. */
8651 saved_message
= parser
->type_definition_forbidden_message
;
8652 parser
->type_definition_forbidden_message
8653 = G_("types may not be defined in casts");
8654 /* Consume the `('. */
8655 cp_token
*open_paren
= cp_lexer_consume_token (parser
->lexer
);
8656 location_t open_paren_loc
= open_paren
->location
;
8658 /* A very tricky bit is that `(struct S) { 3 }' is a
8659 compound-literal (which we permit in C++ as an extension).
8660 But, that construct is not a cast-expression -- it is a
8661 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8662 is legal; if the compound-literal were a cast-expression,
8663 you'd need an extra set of parentheses.) But, if we parse
8664 the type-id, and it happens to be a class-specifier, then we
8665 will commit to the parse at that point, because we cannot
8666 undo the action that is done when creating a new class. So,
8667 then we cannot back up and do a postfix-expression.
8669 Another tricky case is the following (c++/29234):
8671 struct S { void operator () (); };
8678 As a type-id we parse the parenthesized S()() as a function
8679 returning a function, groktypename complains and we cannot
8680 back up in this case either.
8682 Therefore, we scan ahead to the closing `)', and check to see
8683 if the tokens after the `)' can start a cast-expression. Otherwise
8684 we are dealing with an unary-expression, a postfix-expression
8687 Yet another tricky case, in C++11, is the following (c++/54891):
8691 The issue is that usually, besides the case of lambda-expressions,
8692 the parenthesized type-id cannot be followed by '[', and, eg, we
8693 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8694 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8695 we don't commit, we try a cast-expression, then an unary-expression.
8697 Save tokens so that we can put them back. */
8698 cp_lexer_save_tokens (parser
->lexer
);
8700 /* We may be looking at a cast-expression. */
8701 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8702 /*consume_paren=*/true))
8704 = cp_parser_tokens_start_cast_expression (parser
);
8706 /* Roll back the tokens we skipped. */
8707 cp_lexer_rollback_tokens (parser
->lexer
);
8708 /* If we aren't looking at a cast-expression, simulate an error so
8709 that the call to cp_parser_error_occurred below returns true. */
8710 if (!cast_expression
)
8711 cp_parser_simulate_error (parser
);
8714 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8715 parser
->in_type_id_in_expr_p
= true;
8716 /* Look for the type-id. */
8717 type
= cp_parser_type_id (parser
);
8718 /* Look for the closing `)'. */
8719 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8720 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8723 /* Restore the saved message. */
8724 parser
->type_definition_forbidden_message
= saved_message
;
8726 /* At this point this can only be either a cast or a
8727 parenthesized ctor such as `(T ())' that looks like a cast to
8728 function returning T. */
8729 if (!cp_parser_error_occurred (parser
))
8731 /* Only commit if the cast-expression doesn't start with
8732 '++', '--', or '[' in C++11. */
8733 if (cast_expression
> 0)
8734 cp_parser_commit_to_topmost_tentative_parse (parser
);
8736 expr
= cp_parser_cast_expression (parser
,
8737 /*address_p=*/false,
8739 /*decltype_p=*/false,
8742 if (cp_parser_parse_definitely (parser
))
8744 /* Warn about old-style casts, if so requested. */
8745 if (warn_old_style_cast
8746 && !in_system_header_at (input_location
)
8747 && !VOID_TYPE_P (type
)
8748 && current_lang_name
!= lang_name_c
)
8749 warning (OPT_Wold_style_cast
, "use of old-style cast");
8751 /* Only type conversions to integral or enumeration types
8752 can be used in constant-expressions. */
8753 if (!cast_valid_in_integral_constant_expression_p (type
)
8754 && cp_parser_non_integral_constant_expression (parser
,
8756 return error_mark_node
;
8758 /* Perform the cast. */
8762 with start==caret at the open paren, extending to the
8764 location_t cast_loc
= make_location (open_paren_loc
,
8766 expr
.get_finish ());
8767 expr
= build_c_cast (cast_loc
, type
, expr
);
8772 cp_parser_abort_tentative_parse (parser
);
8775 /* If we get here, then it's not a cast, so it must be a
8776 unary-expression. */
8777 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8778 cast_p
, decltype_p
);
8781 /* Parse a binary expression of the general form:
8785 pm-expression .* cast-expression
8786 pm-expression ->* cast-expression
8788 multiplicative-expression:
8790 multiplicative-expression * pm-expression
8791 multiplicative-expression / pm-expression
8792 multiplicative-expression % pm-expression
8794 additive-expression:
8795 multiplicative-expression
8796 additive-expression + multiplicative-expression
8797 additive-expression - multiplicative-expression
8801 shift-expression << additive-expression
8802 shift-expression >> additive-expression
8804 relational-expression:
8806 relational-expression < shift-expression
8807 relational-expression > shift-expression
8808 relational-expression <= shift-expression
8809 relational-expression >= shift-expression
8813 relational-expression:
8814 relational-expression <? shift-expression
8815 relational-expression >? shift-expression
8817 equality-expression:
8818 relational-expression
8819 equality-expression == relational-expression
8820 equality-expression != relational-expression
8824 and-expression & equality-expression
8826 exclusive-or-expression:
8828 exclusive-or-expression ^ and-expression
8830 inclusive-or-expression:
8831 exclusive-or-expression
8832 inclusive-or-expression | exclusive-or-expression
8834 logical-and-expression:
8835 inclusive-or-expression
8836 logical-and-expression && inclusive-or-expression
8838 logical-or-expression:
8839 logical-and-expression
8840 logical-or-expression || logical-and-expression
8842 All these are implemented with a single function like:
8845 simple-cast-expression
8846 binary-expression <token> binary-expression
8848 CAST_P is true if this expression is the target of a cast.
8850 The binops_by_token map is used to get the tree codes for each <token> type.
8851 binary-expressions are associated according to a precedence table. */
8853 #define TOKEN_PRECEDENCE(token) \
8854 (((token->type == CPP_GREATER \
8855 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8856 && !parser->greater_than_is_operator_p) \
8857 ? PREC_NOT_OPERATOR \
8858 : binops_by_token[token->type].prec)
8861 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8862 bool no_toplevel_fold_p
,
8864 enum cp_parser_prec prec
,
8867 cp_parser_expression_stack stack
;
8868 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8869 cp_parser_expression_stack_entry current
;
8872 enum tree_code rhs_type
;
8873 enum cp_parser_prec new_prec
, lookahead_prec
;
8876 /* Parse the first expression. */
8877 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8878 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8879 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8880 cast_p
, decltype_p
, pidk
);
8881 current
.prec
= prec
;
8883 if (cp_parser_error_occurred (parser
))
8884 return error_mark_node
;
8888 /* Get an operator token. */
8889 token
= cp_lexer_peek_token (parser
->lexer
);
8891 if (warn_cxx11_compat
8892 && token
->type
== CPP_RSHIFT
8893 && !parser
->greater_than_is_operator_p
)
8895 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8896 "%<>>%> operator is treated"
8897 " as two right angle brackets in C++11"))
8898 inform (token
->location
,
8899 "suggest parentheses around %<>>%> expression");
8902 new_prec
= TOKEN_PRECEDENCE (token
);
8903 if (new_prec
!= PREC_NOT_OPERATOR
8904 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8905 /* This is a fold-expression; handle it later. */
8906 new_prec
= PREC_NOT_OPERATOR
;
8908 /* Popping an entry off the stack means we completed a subexpression:
8909 - either we found a token which is not an operator (`>' where it is not
8910 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8911 will happen repeatedly;
8912 - or, we found an operator which has lower priority. This is the case
8913 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8915 if (new_prec
<= current
.prec
)
8924 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8925 current
.loc
= token
->location
;
8927 /* We used the operator token. */
8928 cp_lexer_consume_token (parser
->lexer
);
8930 /* For "false && x" or "true || x", x will never be executed;
8931 disable warnings while evaluating it. */
8932 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8933 c_inhibit_evaluation_warnings
+=
8934 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8935 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8936 c_inhibit_evaluation_warnings
+=
8937 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8939 /* Extract another operand. It may be the RHS of this expression
8940 or the LHS of a new, higher priority expression. */
8941 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8942 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8943 rhs
= cp_parser_simple_cast_expression (parser
);
8945 /* Get another operator token. Look up its precedence to avoid
8946 building a useless (immediately popped) stack entry for common
8947 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8948 token
= cp_lexer_peek_token (parser
->lexer
);
8949 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8950 if (lookahead_prec
!= PREC_NOT_OPERATOR
8951 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8952 lookahead_prec
= PREC_NOT_OPERATOR
;
8953 if (lookahead_prec
> new_prec
)
8955 /* ... and prepare to parse the RHS of the new, higher priority
8956 expression. Since precedence levels on the stack are
8957 monotonically increasing, we do not have to care about
8962 current
.lhs_type
= rhs_type
;
8963 current
.prec
= new_prec
;
8964 new_prec
= lookahead_prec
;
8968 lookahead_prec
= new_prec
;
8969 /* If the stack is not empty, we have parsed into LHS the right side
8970 (`4' in the example above) of an expression we had suspended.
8971 We can use the information on the stack to recover the LHS (`3')
8972 from the stack together with the tree code (`MULT_EXPR'), and
8973 the precedence of the higher level subexpression
8974 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8975 which will be used to actually build the additive expression. */
8977 rhs_type
= current
.lhs_type
;
8982 /* Undo the disabling of warnings done above. */
8983 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8984 c_inhibit_evaluation_warnings
-=
8985 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8986 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8987 c_inhibit_evaluation_warnings
-=
8988 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8990 if (warn_logical_not_paren
8991 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8992 && current
.lhs_type
== TRUTH_NOT_EXPR
8993 /* Avoid warning for !!x == y. */
8994 && (TREE_CODE (current
.lhs
) != NE_EXPR
8995 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8996 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8997 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8998 /* Avoid warning for !b == y where b is boolean. */
8999 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9000 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9002 /* Avoid warning for !!b == y where b is boolean. */
9003 && (!DECL_P (current
.lhs
)
9004 || TREE_TYPE (current
.lhs
) == NULL_TREE
9005 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9006 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9007 current
.lhs
, maybe_constant_value (rhs
));
9011 location_t combined_loc
= make_location (current
.loc
,
9012 current
.lhs
.get_start (),
9015 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9016 ERROR_MARK for everything that is not a binary expression.
9017 This makes warn_about_parentheses miss some warnings that
9018 involve unary operators. For unary expressions we should
9019 pass the correct tree_code unless the unary expression was
9020 surrounded by parentheses.
9022 if (no_toplevel_fold_p
9023 && lookahead_prec
<= current
.prec
9025 current
.lhs
= build2_loc (combined_loc
,
9027 TREE_CODE_CLASS (current
.tree_type
)
9029 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9033 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9034 current
.lhs
, current
.lhs_type
,
9035 rhs
, rhs_type
, &overload
,
9036 complain_flags (decltype_p
));
9037 /* TODO: build_x_binary_op doesn't always honor the location. */
9038 current
.lhs
.set_location (combined_loc
);
9040 current
.lhs_type
= current
.tree_type
;
9042 /* If the binary operator required the use of an overloaded operator,
9043 then this expression cannot be an integral constant-expression.
9044 An overloaded operator can be used even if both operands are
9045 otherwise permissible in an integral constant-expression if at
9046 least one of the operands is of enumeration type. */
9049 && cp_parser_non_integral_constant_expression (parser
,
9051 return error_mark_node
;
9058 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9059 bool no_toplevel_fold_p
,
9060 enum cp_parser_prec prec
,
9063 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9064 /*decltype*/false, prec
, pidk
);
9067 /* Parse the `? expression : assignment-expression' part of a
9068 conditional-expression. The LOGICAL_OR_EXPR is the
9069 logical-or-expression that started the conditional-expression.
9070 Returns a representation of the entire conditional-expression.
9072 This routine is used by cp_parser_assignment_expression.
9074 ? expression : assignment-expression
9078 ? : assignment-expression */
9081 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9083 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9084 cp_expr assignment_expr
;
9085 struct cp_token
*token
;
9086 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9088 /* Consume the `?' token. */
9089 cp_lexer_consume_token (parser
->lexer
);
9090 token
= cp_lexer_peek_token (parser
->lexer
);
9091 if (cp_parser_allow_gnu_extensions_p (parser
)
9092 && token
->type
== CPP_COLON
)
9094 pedwarn (token
->location
, OPT_Wpedantic
,
9095 "ISO C++ does not allow ?: with omitted middle operand");
9096 /* Implicit true clause. */
9098 c_inhibit_evaluation_warnings
+=
9099 folded_logical_or_expr
== truthvalue_true_node
;
9100 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9104 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9105 parser
->colon_corrects_to_scope_p
= false;
9106 /* Parse the expression. */
9107 c_inhibit_evaluation_warnings
+=
9108 folded_logical_or_expr
== truthvalue_false_node
;
9109 expr
= cp_parser_expression (parser
);
9110 c_inhibit_evaluation_warnings
+=
9111 ((folded_logical_or_expr
== truthvalue_true_node
)
9112 - (folded_logical_or_expr
== truthvalue_false_node
));
9113 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9116 /* The next token should be a `:'. */
9117 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9118 /* Parse the assignment-expression. */
9119 assignment_expr
= cp_parser_assignment_expression (parser
);
9120 c_inhibit_evaluation_warnings
-=
9121 folded_logical_or_expr
== truthvalue_true_node
;
9124 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9125 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9126 with the caret at the "?", ranging from the start of
9127 the logical_or_expr to the end of the assignment_expr. */
9128 loc
= make_location (loc
,
9129 logical_or_expr
.get_start (),
9130 assignment_expr
.get_finish ());
9132 /* Build the conditional-expression. */
9133 return build_x_conditional_expr (loc
, logical_or_expr
,
9136 tf_warning_or_error
);
9139 /* Parse an assignment-expression.
9141 assignment-expression:
9142 conditional-expression
9143 logical-or-expression assignment-operator assignment_expression
9146 CAST_P is true if this expression is the target of a cast.
9147 DECLTYPE_P is true if this expression is the operand of decltype.
9149 Returns a representation for the expression. */
9152 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9153 bool cast_p
, bool decltype_p
)
9157 /* If the next token is the `throw' keyword, then we're looking at
9158 a throw-expression. */
9159 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9160 expr
= cp_parser_throw_expression (parser
);
9161 /* Otherwise, it must be that we are looking at a
9162 logical-or-expression. */
9165 /* Parse the binary expressions (logical-or-expression). */
9166 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9168 PREC_NOT_OPERATOR
, pidk
);
9169 /* If the next token is a `?' then we're actually looking at a
9170 conditional-expression. */
9171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9172 return cp_parser_question_colon_clause (parser
, expr
);
9175 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9177 /* If it's an assignment-operator, we're using the second
9179 enum tree_code assignment_operator
9180 = cp_parser_assignment_operator_opt (parser
);
9181 if (assignment_operator
!= ERROR_MARK
)
9183 bool non_constant_p
;
9185 /* Parse the right-hand side of the assignment. */
9186 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9189 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9190 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9192 /* An assignment may not appear in a
9193 constant-expression. */
9194 if (cp_parser_non_integral_constant_expression (parser
,
9196 return error_mark_node
;
9197 /* Build the assignment expression. Its default
9201 is the location of the '=' token as the
9202 caret, ranging from the start of the lhs to the
9204 loc
= make_location (loc
,
9207 expr
= build_x_modify_expr (loc
, expr
,
9208 assignment_operator
,
9210 complain_flags (decltype_p
));
9211 /* TODO: build_x_modify_expr doesn't honor the location,
9212 so we must set it here. */
9213 expr
.set_location (loc
);
9221 /* Parse an (optional) assignment-operator.
9223 assignment-operator: one of
9224 = *= /= %= += -= >>= <<= &= ^= |=
9228 assignment-operator: one of
9231 If the next token is an assignment operator, the corresponding tree
9232 code is returned, and the token is consumed. For example, for
9233 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9234 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9235 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9236 operator, ERROR_MARK is returned. */
9238 static enum tree_code
9239 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9244 /* Peek at the next token. */
9245 token
= cp_lexer_peek_token (parser
->lexer
);
9247 switch (token
->type
)
9258 op
= TRUNC_DIV_EXPR
;
9262 op
= TRUNC_MOD_EXPR
;
9294 /* Nothing else is an assignment operator. */
9298 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9299 if (op
!= ERROR_MARK
9300 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9303 /* If it was an assignment operator, consume it. */
9304 if (op
!= ERROR_MARK
)
9305 cp_lexer_consume_token (parser
->lexer
);
9310 /* Parse an expression.
9313 assignment-expression
9314 expression , assignment-expression
9316 CAST_P is true if this expression is the target of a cast.
9317 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9318 except possibly parenthesized or on the RHS of a comma (N3276).
9320 Returns a representation of the expression. */
9323 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9324 bool cast_p
, bool decltype_p
)
9326 cp_expr expression
= NULL_TREE
;
9327 location_t loc
= UNKNOWN_LOCATION
;
9331 cp_expr assignment_expression
;
9333 /* Parse the next assignment-expression. */
9334 assignment_expression
9335 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9337 /* We don't create a temporary for a call that is the immediate operand
9338 of decltype or on the RHS of a comma. But when we see a comma, we
9339 need to create a temporary for a call on the LHS. */
9340 if (decltype_p
&& !processing_template_decl
9341 && TREE_CODE (assignment_expression
) == CALL_EXPR
9342 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9343 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9344 assignment_expression
9345 = build_cplus_new (TREE_TYPE (assignment_expression
),
9346 assignment_expression
, tf_warning_or_error
);
9348 /* If this is the first assignment-expression, we can just
9351 expression
= assignment_expression
;
9354 /* Create a location with caret at the comma, ranging
9355 from the start of the LHS to the end of the RHS. */
9356 loc
= make_location (loc
,
9357 expression
.get_start (),
9358 assignment_expression
.get_finish ());
9359 expression
= build_x_compound_expr (loc
, expression
,
9360 assignment_expression
,
9361 complain_flags (decltype_p
));
9362 expression
.set_location (loc
);
9364 /* If the next token is not a comma, or we're in a fold-expression, then
9365 we are done with the expression. */
9366 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9367 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9369 /* Consume the `,'. */
9370 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9371 cp_lexer_consume_token (parser
->lexer
);
9372 /* A comma operator cannot appear in a constant-expression. */
9373 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9374 expression
= error_mark_node
;
9380 /* Parse a constant-expression.
9382 constant-expression:
9383 conditional-expression
9385 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9386 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9387 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9388 is false, NON_CONSTANT_P should be NULL. */
9391 cp_parser_constant_expression (cp_parser
* parser
,
9392 bool allow_non_constant_p
,
9393 bool *non_constant_p
)
9395 bool saved_integral_constant_expression_p
;
9396 bool saved_allow_non_integral_constant_expression_p
;
9397 bool saved_non_integral_constant_expression_p
;
9400 /* It might seem that we could simply parse the
9401 conditional-expression, and then check to see if it were
9402 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9403 one that the compiler can figure out is constant, possibly after
9404 doing some simplifications or optimizations. The standard has a
9405 precise definition of constant-expression, and we must honor
9406 that, even though it is somewhat more restrictive.
9412 is not a legal declaration, because `(2, 3)' is not a
9413 constant-expression. The `,' operator is forbidden in a
9414 constant-expression. However, GCC's constant-folding machinery
9415 will fold this operation to an INTEGER_CST for `3'. */
9417 /* Save the old settings. */
9418 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9419 saved_allow_non_integral_constant_expression_p
9420 = parser
->allow_non_integral_constant_expression_p
;
9421 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9422 /* We are now parsing a constant-expression. */
9423 parser
->integral_constant_expression_p
= true;
9424 parser
->allow_non_integral_constant_expression_p
9425 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9426 parser
->non_integral_constant_expression_p
= false;
9427 /* Although the grammar says "conditional-expression", we parse an
9428 "assignment-expression", which also permits "throw-expression"
9429 and the use of assignment operators. In the case that
9430 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9431 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9432 actually essential that we look for an assignment-expression.
9433 For example, cp_parser_initializer_clauses uses this function to
9434 determine whether a particular assignment-expression is in fact
9436 expression
= cp_parser_assignment_expression (parser
);
9437 /* Restore the old settings. */
9438 parser
->integral_constant_expression_p
9439 = saved_integral_constant_expression_p
;
9440 parser
->allow_non_integral_constant_expression_p
9441 = saved_allow_non_integral_constant_expression_p
;
9442 if (cxx_dialect
>= cxx11
)
9444 /* Require an rvalue constant expression here; that's what our
9445 callers expect. Reference constant expressions are handled
9446 separately in e.g. cp_parser_template_argument. */
9447 bool is_const
= potential_rvalue_constant_expression (expression
);
9448 parser
->non_integral_constant_expression_p
= !is_const
;
9449 if (!is_const
&& !allow_non_constant_p
)
9450 require_potential_rvalue_constant_expression (expression
);
9452 if (allow_non_constant_p
)
9453 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9454 parser
->non_integral_constant_expression_p
9455 = saved_non_integral_constant_expression_p
;
9460 /* Parse __builtin_offsetof.
9462 offsetof-expression:
9463 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9465 offsetof-member-designator:
9467 | offsetof-member-designator "." id-expression
9468 | offsetof-member-designator "[" expression "]"
9469 | offsetof-member-designator "->" id-expression */
9472 cp_parser_builtin_offsetof (cp_parser
*parser
)
9474 int save_ice_p
, save_non_ice_p
;
9479 location_t finish_loc
;
9481 /* We're about to accept non-integral-constant things, but will
9482 definitely yield an integral constant expression. Save and
9483 restore these values around our local parsing. */
9484 save_ice_p
= parser
->integral_constant_expression_p
;
9485 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9487 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9489 /* Consume the "__builtin_offsetof" token. */
9490 cp_lexer_consume_token (parser
->lexer
);
9491 /* Consume the opening `('. */
9492 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9493 /* Parse the type-id. */
9494 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9495 type
= cp_parser_type_id (parser
);
9496 /* Look for the `,'. */
9497 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9498 token
= cp_lexer_peek_token (parser
->lexer
);
9500 /* Build the (type *)null that begins the traditional offsetof macro. */
9501 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
9502 tf_warning_or_error
);
9504 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9505 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
9506 true, &dummy
, token
->location
);
9509 token
= cp_lexer_peek_token (parser
->lexer
);
9510 switch (token
->type
)
9512 case CPP_OPEN_SQUARE
:
9513 /* offsetof-member-designator "[" expression "]" */
9514 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9519 /* offsetof-member-designator "->" identifier */
9520 expr
= grok_array_decl (token
->location
, expr
,
9521 integer_zero_node
, false);
9525 /* offsetof-member-designator "." identifier */
9526 cp_lexer_consume_token (parser
->lexer
);
9527 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9532 case CPP_CLOSE_PAREN
:
9533 /* Consume the ")" token. */
9534 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9535 cp_lexer_consume_token (parser
->lexer
);
9539 /* Error. We know the following require will fail, but
9540 that gives the proper error message. */
9541 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9542 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9543 expr
= error_mark_node
;
9549 /* Make a location of the form:
9550 __builtin_offsetof (struct s, f)
9551 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9552 with caret at the type-id, ranging from the start of the
9553 "_builtin_offsetof" token to the close paren. */
9554 loc
= make_location (loc
, start_loc
, finish_loc
);
9555 /* The result will be an INTEGER_CST, so we need to explicitly
9556 preserve the location. */
9557 expr
= cp_expr (finish_offsetof (expr
, loc
), loc
);
9560 parser
->integral_constant_expression_p
= save_ice_p
;
9561 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9566 /* Parse a trait expression.
9568 Returns a representation of the expression, the underlying type
9569 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9572 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9575 tree type1
, type2
= NULL_TREE
;
9576 bool binary
= false;
9577 bool variadic
= false;
9581 case RID_HAS_NOTHROW_ASSIGN
:
9582 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9584 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9585 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9587 case RID_HAS_NOTHROW_COPY
:
9588 kind
= CPTK_HAS_NOTHROW_COPY
;
9590 case RID_HAS_TRIVIAL_ASSIGN
:
9591 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9593 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9594 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9596 case RID_HAS_TRIVIAL_COPY
:
9597 kind
= CPTK_HAS_TRIVIAL_COPY
;
9599 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9600 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9602 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9603 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9605 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9606 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9608 case RID_IS_ABSTRACT
:
9609 kind
= CPTK_IS_ABSTRACT
;
9611 case RID_IS_BASE_OF
:
9612 kind
= CPTK_IS_BASE_OF
;
9616 kind
= CPTK_IS_CLASS
;
9619 kind
= CPTK_IS_EMPTY
;
9622 kind
= CPTK_IS_ENUM
;
9625 kind
= CPTK_IS_FINAL
;
9627 case RID_IS_LITERAL_TYPE
:
9628 kind
= CPTK_IS_LITERAL_TYPE
;
9633 case RID_IS_POLYMORPHIC
:
9634 kind
= CPTK_IS_POLYMORPHIC
;
9636 case RID_IS_SAME_AS
:
9637 kind
= CPTK_IS_SAME_AS
;
9640 case RID_IS_STD_LAYOUT
:
9641 kind
= CPTK_IS_STD_LAYOUT
;
9643 case RID_IS_TRIVIAL
:
9644 kind
= CPTK_IS_TRIVIAL
;
9646 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9647 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9650 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9651 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9654 case RID_IS_TRIVIALLY_COPYABLE
:
9655 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9658 kind
= CPTK_IS_UNION
;
9660 case RID_UNDERLYING_TYPE
:
9661 kind
= CPTK_UNDERLYING_TYPE
;
9666 case RID_DIRECT_BASES
:
9667 kind
= CPTK_DIRECT_BASES
;
9673 /* Consume the token. */
9674 cp_lexer_consume_token (parser
->lexer
);
9676 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9679 type_id_in_expr_sentinel
s (parser
);
9680 type1
= cp_parser_type_id (parser
);
9683 if (type1
== error_mark_node
)
9684 return error_mark_node
;
9688 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9691 type_id_in_expr_sentinel
s (parser
);
9692 type2
= cp_parser_type_id (parser
);
9695 if (type2
== error_mark_node
)
9696 return error_mark_node
;
9700 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9702 cp_lexer_consume_token (parser
->lexer
);
9703 tree elt
= cp_parser_type_id (parser
);
9704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9706 cp_lexer_consume_token (parser
->lexer
);
9707 elt
= make_pack_expansion (elt
);
9709 if (elt
== error_mark_node
)
9710 return error_mark_node
;
9711 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9715 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9717 /* Complete the trait expression, which may mean either processing
9718 the trait expr now or saving it for template instantiation. */
9721 case CPTK_UNDERLYING_TYPE
:
9722 return finish_underlying_type (type1
);
9724 return finish_bases (type1
, false);
9725 case CPTK_DIRECT_BASES
:
9726 return finish_bases (type1
, true);
9728 return finish_trait_expr (kind
, type1
, type2
);
9732 /* Lambdas that appear in variable initializer or default argument scope
9733 get that in their mangling, so we need to record it. We might as well
9734 use the count for function and namespace scopes as well. */
9735 static GTY(()) tree lambda_scope
;
9736 static GTY(()) int lambda_count
;
9737 struct GTY(()) tree_int
9742 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9745 start_lambda_scope (tree decl
)
9749 /* Once we're inside a function, we ignore other scopes and just push
9750 the function again so that popping works properly. */
9751 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9752 decl
= current_function_decl
;
9753 ti
.t
= lambda_scope
;
9754 ti
.i
= lambda_count
;
9755 vec_safe_push (lambda_scope_stack
, ti
);
9756 if (lambda_scope
!= decl
)
9758 /* Don't reset the count if we're still in the same function. */
9759 lambda_scope
= decl
;
9765 record_lambda_scope (tree lambda
)
9767 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9768 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9772 finish_lambda_scope (void)
9774 tree_int
*p
= &lambda_scope_stack
->last ();
9775 if (lambda_scope
!= p
->t
)
9777 lambda_scope
= p
->t
;
9778 lambda_count
= p
->i
;
9780 lambda_scope_stack
->pop ();
9783 /* Parse a lambda expression.
9786 lambda-introducer lambda-declarator [opt] compound-statement
9788 Returns a representation of the expression. */
9791 cp_parser_lambda_expression (cp_parser
* parser
)
9793 tree lambda_expr
= build_lambda_expr ();
9796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9797 cp_token_position start
= 0;
9799 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9801 if (cp_unevaluated_operand
)
9803 if (!token
->error_reported
)
9805 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9806 "lambda-expression in unevaluated context");
9807 token
->error_reported
= true;
9811 else if (parser
->in_template_argument_list_p
)
9813 if (!token
->error_reported
)
9815 error_at (token
->location
, "lambda-expression in template-argument");
9816 token
->error_reported
= true;
9821 /* We may be in the middle of deferred access check. Disable
9823 push_deferring_access_checks (dk_no_deferred
);
9825 cp_parser_lambda_introducer (parser
, lambda_expr
);
9827 type
= begin_lambda_type (lambda_expr
);
9828 if (type
== error_mark_node
)
9829 return error_mark_node
;
9831 record_lambda_scope (lambda_expr
);
9833 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9834 determine_visibility (TYPE_NAME (type
));
9836 /* Now that we've started the type, add the capture fields for any
9837 explicit captures. */
9838 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9841 /* Inside the class, surrounding template-parameter-lists do not apply. */
9842 unsigned int saved_num_template_parameter_lists
9843 = parser
->num_template_parameter_lists
;
9844 unsigned char in_statement
= parser
->in_statement
;
9845 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9846 bool fully_implicit_function_template_p
9847 = parser
->fully_implicit_function_template_p
;
9848 tree implicit_template_parms
= parser
->implicit_template_parms
;
9849 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9850 bool auto_is_implicit_function_template_parm_p
9851 = parser
->auto_is_implicit_function_template_parm_p
;
9853 parser
->num_template_parameter_lists
= 0;
9854 parser
->in_statement
= 0;
9855 parser
->in_switch_statement_p
= false;
9856 parser
->fully_implicit_function_template_p
= false;
9857 parser
->implicit_template_parms
= 0;
9858 parser
->implicit_template_scope
= 0;
9859 parser
->auto_is_implicit_function_template_parm_p
= false;
9861 /* By virtue of defining a local class, a lambda expression has access to
9862 the private variables of enclosing classes. */
9864 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9866 if (ok
&& cp_parser_error_occurred (parser
))
9871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9872 && cp_parser_start_tentative_firewall (parser
))
9874 cp_parser_lambda_body (parser
, lambda_expr
);
9876 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9878 if (cp_parser_skip_to_closing_brace (parser
))
9879 cp_lexer_consume_token (parser
->lexer
);
9882 /* The capture list was built up in reverse order; fix that now. */
9883 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9884 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9887 maybe_add_lambda_conv_op (type
);
9889 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9891 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9892 parser
->in_statement
= in_statement
;
9893 parser
->in_switch_statement_p
= in_switch_statement_p
;
9894 parser
->fully_implicit_function_template_p
9895 = fully_implicit_function_template_p
;
9896 parser
->implicit_template_parms
= implicit_template_parms
;
9897 parser
->implicit_template_scope
= implicit_template_scope
;
9898 parser
->auto_is_implicit_function_template_parm_p
9899 = auto_is_implicit_function_template_parm_p
;
9902 /* This field is only used during parsing of the lambda. */
9903 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9905 /* This lambda shouldn't have any proxies left at this point. */
9906 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9907 /* And now that we're done, push proxies for an enclosing lambda. */
9908 insert_pending_capture_proxies ();
9911 lambda_expr
= build_lambda_object (lambda_expr
);
9913 lambda_expr
= error_mark_node
;
9915 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9917 pop_deferring_access_checks ();
9922 /* Parse the beginning of a lambda expression.
9925 [ lambda-capture [opt] ]
9927 LAMBDA_EXPR is the current representation of the lambda expression. */
9930 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9932 /* Need commas after the first capture. */
9935 /* Eat the leading `['. */
9936 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9938 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9940 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9941 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9942 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9945 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9947 cp_lexer_consume_token (parser
->lexer
);
9951 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9953 cp_token
* capture_token
;
9955 tree capture_init_expr
;
9956 cp_id_kind idk
= CP_ID_KIND_NONE
;
9957 bool explicit_init_p
= false;
9959 enum capture_kind_type
9964 enum capture_kind_type capture_kind
= BY_COPY
;
9966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9968 error ("expected end of capture-list");
9975 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9977 /* Possibly capture `this'. */
9978 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9980 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9981 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9982 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9983 "with by-copy capture default");
9984 cp_lexer_consume_token (parser
->lexer
);
9985 add_capture (lambda_expr
,
9986 /*id=*/this_identifier
,
9987 /*initializer=*/finish_this_expr (),
9988 /*by_reference_p=*/true,
9993 /* Possibly capture `*this'. */
9994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
9995 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
9997 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9998 if (cxx_dialect
< cxx1z
)
9999 pedwarn (loc
, 0, "%<*this%> capture only available with "
10000 "-std=c++1z or -std=gnu++1z");
10001 cp_lexer_consume_token (parser
->lexer
);
10002 cp_lexer_consume_token (parser
->lexer
);
10003 add_capture (lambda_expr
,
10004 /*id=*/this_identifier
,
10005 /*initializer=*/finish_this_expr (),
10006 /*by_reference_p=*/false,
10011 /* Remember whether we want to capture as a reference or not. */
10012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10014 capture_kind
= BY_REFERENCE
;
10015 cp_lexer_consume_token (parser
->lexer
);
10018 /* Get the identifier. */
10019 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10020 capture_id
= cp_parser_identifier (parser
);
10022 if (capture_id
== error_mark_node
)
10023 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10024 delimiters, but I modified this to stop on unnested ']' as well. It
10025 was already changed to stop on unnested '}', so the
10026 "closing_parenthesis" name is no more misleading with my change. */
10028 cp_parser_skip_to_closing_parenthesis (parser
,
10029 /*recovering=*/true,
10031 /*consume_paren=*/true);
10035 /* Find the initializer for this capture. */
10036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10037 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10038 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10040 bool direct
, non_constant
;
10041 /* An explicit initializer exists. */
10042 if (cxx_dialect
< cxx14
)
10043 pedwarn (input_location
, 0,
10044 "lambda capture initializers "
10045 "only available with -std=c++14 or -std=gnu++14");
10046 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10048 explicit_init_p
= true;
10049 if (capture_init_expr
== NULL_TREE
)
10051 error ("empty initializer for lambda init-capture");
10052 capture_init_expr
= error_mark_node
;
10057 const char* error_msg
;
10059 /* Turn the identifier into an id-expression. */
10061 = cp_parser_lookup_name_simple (parser
, capture_id
,
10062 capture_token
->location
);
10064 if (capture_init_expr
== error_mark_node
)
10066 unqualified_name_lookup_error (capture_id
);
10069 else if (DECL_P (capture_init_expr
)
10070 && (!VAR_P (capture_init_expr
)
10071 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10073 error_at (capture_token
->location
,
10074 "capture of non-variable %qD ",
10075 capture_init_expr
);
10076 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10077 "%q#D declared here", capture_init_expr
);
10080 if (VAR_P (capture_init_expr
)
10081 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10083 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10084 "%qD with non-automatic storage duration",
10085 capture_init_expr
))
10086 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10087 "%q#D declared here", capture_init_expr
);
10092 = finish_id_expression
10097 /*integral_constant_expression_p=*/false,
10098 /*allow_non_integral_constant_expression_p=*/false,
10099 /*non_integral_constant_expression_p=*/NULL
,
10100 /*template_p=*/false,
10102 /*address_p=*/false,
10103 /*template_arg_p=*/false,
10105 capture_token
->location
);
10107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10109 cp_lexer_consume_token (parser
->lexer
);
10110 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10113 check_for_bare_parameter_packs (capture_init_expr
);
10116 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10117 && !explicit_init_p
)
10119 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10120 && capture_kind
== BY_COPY
)
10121 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10122 "of %qD redundant with by-copy capture default",
10124 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10125 && capture_kind
== BY_REFERENCE
)
10126 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10127 "capture of %qD redundant with by-reference capture "
10128 "default", capture_id
);
10131 add_capture (lambda_expr
,
10134 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10138 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10141 /* Parse the (optional) middle of a lambda expression.
10144 < template-parameter-list [opt] >
10145 ( parameter-declaration-clause [opt] )
10146 attribute-specifier [opt]
10147 decl-specifier-seq [opt]
10148 exception-specification [opt]
10149 lambda-return-type-clause [opt]
10151 LAMBDA_EXPR is the current representation of the lambda expression. */
10154 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10156 /* 5.1.1.4 of the standard says:
10157 If a lambda-expression does not include a lambda-declarator, it is as if
10158 the lambda-declarator were ().
10159 This means an empty parameter list, no attributes, and no exception
10161 tree param_list
= void_list_node
;
10162 tree attributes
= NULL_TREE
;
10163 tree exception_spec
= NULL_TREE
;
10164 tree template_param_list
= NULL_TREE
;
10165 tree tx_qual
= NULL_TREE
;
10166 cp_decl_specifier_seq lambda_specs
;
10167 clear_decl_specs (&lambda_specs
);
10169 /* The template-parameter-list is optional, but must begin with
10170 an opening angle if present. */
10171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10173 if (cxx_dialect
< cxx14
)
10174 pedwarn (parser
->lexer
->next_token
->location
, 0,
10175 "lambda templates are only available with "
10176 "-std=c++14 or -std=gnu++14");
10178 cp_lexer_consume_token (parser
->lexer
);
10180 template_param_list
= cp_parser_template_parameter_list (parser
);
10182 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10184 /* We just processed one more parameter list. */
10185 ++parser
->num_template_parameter_lists
;
10188 /* The parameter-declaration-clause is optional (unless
10189 template-parameter-list was given), but must begin with an
10190 opening parenthesis if present. */
10191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10193 cp_lexer_consume_token (parser
->lexer
);
10195 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10197 /* Parse parameters. */
10198 param_list
= cp_parser_parameter_declaration_clause (parser
);
10200 /* Default arguments shall not be specified in the
10201 parameter-declaration-clause of a lambda-declarator. */
10202 if (cxx_dialect
< cxx14
)
10203 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10204 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10205 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10206 "default argument specified for lambda parameter");
10208 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10210 attributes
= cp_parser_attributes_opt (parser
);
10212 /* In the decl-specifier-seq of the lambda-declarator, each
10213 decl-specifier shall either be mutable or constexpr. */
10214 int declares_class_or_enum
;
10215 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10216 cp_parser_decl_specifier_seq (parser
,
10217 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10218 &lambda_specs
, &declares_class_or_enum
);
10219 if (lambda_specs
.storage_class
== sc_mutable
)
10221 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10222 if (lambda_specs
.conflicting_specifiers_p
)
10223 error_at (lambda_specs
.locations
[ds_storage_class
],
10224 "duplicate %<mutable%>");
10227 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10229 /* Parse optional exception specification. */
10230 exception_spec
= cp_parser_exception_specification_opt (parser
);
10232 /* Parse optional trailing return type. */
10233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10235 cp_lexer_consume_token (parser
->lexer
);
10236 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10237 = cp_parser_trailing_type_id (parser
);
10240 /* The function parameters must be in scope all the way until after the
10241 trailing-return-type in case of decltype. */
10242 pop_bindings_and_leave_scope ();
10244 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10245 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10247 /* Create the function call operator.
10249 Messing with declarators like this is no uglier than building up the
10250 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10253 cp_decl_specifier_seq return_type_specs
;
10254 cp_declarator
* declarator
;
10259 clear_decl_specs (&return_type_specs
);
10260 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10261 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
10263 /* Maybe we will deduce the return type later. */
10264 return_type_specs
.type
= make_auto ();
10266 if (lambda_specs
.locations
[ds_constexpr
])
10268 if (cxx_dialect
>= cxx1z
)
10269 return_type_specs
.locations
[ds_constexpr
]
10270 = lambda_specs
.locations
[ds_constexpr
];
10272 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10273 "lambda only available with -std=c++1z or -std=gnu++1z");
10276 p
= obstack_alloc (&declarator_obstack
, 0);
10278 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
10281 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10282 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10283 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10284 VIRT_SPEC_UNSPECIFIED
,
10288 /*late_return_type=*/NULL_TREE
,
10289 /*requires_clause*/NULL_TREE
);
10290 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10292 fco
= grokmethod (&return_type_specs
,
10295 if (fco
!= error_mark_node
)
10297 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10298 DECL_ARTIFICIAL (fco
) = 1;
10299 /* Give the object parameter a different name. */
10300 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10301 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10302 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10304 if (template_param_list
)
10306 fco
= finish_member_template_decl (fco
);
10307 finish_template_decl (template_param_list
);
10308 --parser
->num_template_parameter_lists
;
10310 else if (parser
->fully_implicit_function_template_p
)
10311 fco
= finish_fully_implicit_template (parser
, fco
);
10313 finish_member_declaration (fco
);
10315 obstack_free (&declarator_obstack
, p
);
10317 return (fco
!= error_mark_node
);
10321 /* Parse the body of a lambda expression, which is simply
10325 but which requires special handling.
10326 LAMBDA_EXPR is the current representation of the lambda expression. */
10329 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10331 bool nested
= (current_function_decl
!= NULL_TREE
);
10332 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10334 push_function_context ();
10336 /* Still increment function_depth so that we don't GC in the
10337 middle of an expression. */
10339 vec
<tree
> omp_privatization_save
;
10340 save_omp_privatization_clauses (omp_privatization_save
);
10341 /* Clear this in case we're in the middle of a default argument. */
10342 parser
->local_variables_forbidden_p
= false;
10344 /* Finish the function call operator
10346 + late_parsing_for_member
10347 + function_definition_after_declarator
10348 + ctor_initializer_opt_and_function_body */
10350 tree fco
= lambda_function (lambda_expr
);
10353 tree compound_stmt
;
10356 /* Let the front end know that we are going to be defining this
10358 start_preparsed_function (fco
,
10360 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
10362 start_lambda_scope (fco
);
10363 body
= begin_function_body ();
10365 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10368 /* Push the proxies for any explicit captures. */
10369 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
10370 cap
= TREE_CHAIN (cap
))
10371 build_capture_proxy (TREE_PURPOSE (cap
));
10373 compound_stmt
= begin_compound_stmt (0);
10375 /* 5.1.1.4 of the standard says:
10376 If a lambda-expression does not include a trailing-return-type, it
10377 is as if the trailing-return-type denotes the following type:
10378 * if the compound-statement is of the form
10379 { return attribute-specifier [opt] expression ; }
10380 the type of the returned expression after lvalue-to-rvalue
10381 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10382 (_conv.array_ 4.2), and function-to-pointer conversion
10384 * otherwise, void. */
10386 /* In a lambda that has neither a lambda-return-type-clause
10387 nor a deducible form, errors should be reported for return statements
10388 in the body. Since we used void as the placeholder return type, parsing
10389 the body as usual will give such desired behavior. */
10390 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10391 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10392 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10394 tree expr
= NULL_TREE
;
10395 cp_id_kind idk
= CP_ID_KIND_NONE
;
10397 /* Parse tentatively in case there's more after the initial return
10399 cp_parser_parse_tentatively (parser
);
10401 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10403 expr
= cp_parser_expression (parser
, &idk
);
10405 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10406 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10408 if (cp_parser_parse_definitely (parser
))
10410 if (!processing_template_decl
)
10412 tree type
= lambda_return_type (expr
);
10413 apply_deduced_return_type (fco
, type
);
10414 if (type
== error_mark_node
)
10415 expr
= error_mark_node
;
10418 /* Will get error here if type not deduced yet. */
10419 finish_return_stmt (expr
);
10427 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10428 cp_parser_label_declaration (parser
);
10429 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10430 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10433 finish_compound_stmt (compound_stmt
);
10436 finish_function_body (body
);
10437 finish_lambda_scope ();
10439 /* Finish the function and generate code for it if necessary. */
10440 tree fn
= finish_function (/*inline*/2);
10442 /* Only expand if the call op is not a template. */
10443 if (!DECL_TEMPLATE_INFO (fco
))
10444 expand_or_defer_fn (fn
);
10447 restore_omp_privatization_clauses (omp_privatization_save
);
10448 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10450 pop_function_context();
10455 /* Statements [gram.stmt.stmt] */
10457 /* Parse a statement.
10461 expression-statement
10463 selection-statement
10464 iteration-statement
10466 declaration-statement
10473 attribute-specifier-seq (opt) expression-statement
10474 attribute-specifier-seq (opt) compound-statement
10475 attribute-specifier-seq (opt) selection-statement
10476 attribute-specifier-seq (opt) iteration-statement
10477 attribute-specifier-seq (opt) jump-statement
10478 declaration-statement
10479 attribute-specifier-seq (opt) try-block
10482 expression-statement
10490 IN_COMPOUND is true when the statement is nested inside a
10491 cp_parser_compound_statement; this matters for certain pragmas.
10493 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10494 is a (possibly labeled) if statement which is not enclosed in braces
10495 and has an else clause. This is used to implement -Wparentheses.
10497 CHAIN is a vector of if-else-if conditions. */
10500 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10501 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
10503 tree statement
, std_attrs
= NULL_TREE
;
10505 location_t statement_location
, attrs_location
;
10510 /* There is no statement yet. */
10511 statement
= NULL_TREE
;
10513 saved_token_sentinel
saved_tokens (parser
->lexer
);
10514 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10515 if (c_dialect_objc ())
10516 /* In obj-c++, seeing '[[' might be the either the beginning of
10517 c++11 attributes, or a nested objc-message-expression. So
10518 let's parse the c++11 attributes tentatively. */
10519 cp_parser_parse_tentatively (parser
);
10520 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10521 if (c_dialect_objc ())
10523 if (!cp_parser_parse_definitely (parser
))
10524 std_attrs
= NULL_TREE
;
10527 /* Peek at the next token. */
10528 token
= cp_lexer_peek_token (parser
->lexer
);
10529 /* Remember the location of the first token in the statement. */
10530 statement_location
= token
->location
;
10531 /* If this is a keyword, then that will often determine what kind of
10532 statement we have. */
10533 if (token
->type
== CPP_KEYWORD
)
10535 enum rid keyword
= token
->keyword
;
10541 /* Looks like a labeled-statement with a case label.
10542 Parse the label, and then use tail recursion to parse
10544 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10545 in_compound
= false;
10550 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10556 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10560 if (!flag_cilkplus
)
10562 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10563 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10564 cp_lexer_consume_token (parser
->lexer
);
10565 statement
= error_mark_node
;
10568 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10575 statement
= cp_parser_jump_statement (parser
);
10578 case RID_CILK_SYNC
:
10579 cp_lexer_consume_token (parser
->lexer
);
10582 tree sync_expr
= build_cilk_sync ();
10583 SET_EXPR_LOCATION (sync_expr
,
10585 statement
= finish_expr_stmt (sync_expr
);
10589 error_at (token
->location
, "-fcilkplus must be enabled to use"
10590 " %<_Cilk_sync%>");
10591 statement
= error_mark_node
;
10593 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10596 /* Objective-C++ exception-handling constructs. */
10599 case RID_AT_FINALLY
:
10600 case RID_AT_SYNCHRONIZED
:
10602 statement
= cp_parser_objc_statement (parser
);
10606 statement
= cp_parser_try_block (parser
);
10609 case RID_NAMESPACE
:
10610 /* This must be a namespace alias definition. */
10611 cp_parser_declaration_statement (parser
);
10614 case RID_TRANSACTION_ATOMIC
:
10615 case RID_TRANSACTION_RELAXED
:
10616 case RID_SYNCHRONIZED
:
10617 case RID_ATOMIC_NOEXCEPT
:
10618 case RID_ATOMIC_CANCEL
:
10619 statement
= cp_parser_transaction (parser
, token
);
10621 case RID_TRANSACTION_CANCEL
:
10622 statement
= cp_parser_transaction_cancel (parser
);
10626 /* It might be a keyword like `int' that can start a
10627 declaration-statement. */
10631 else if (token
->type
== CPP_NAME
)
10633 /* If the next token is a `:', then we are looking at a
10634 labeled-statement. */
10635 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10636 if (token
->type
== CPP_COLON
)
10638 /* Looks like a labeled-statement with an ordinary label.
10639 Parse the label, and then use tail recursion to parse
10642 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10643 in_compound
= false;
10647 /* Anything that starts with a `{' must be a compound-statement. */
10648 else if (token
->type
== CPP_OPEN_BRACE
)
10649 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10650 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10651 a statement all its own. */
10652 else if (token
->type
== CPP_PRAGMA
)
10654 /* Only certain OpenMP pragmas are attached to statements, and thus
10655 are considered statements themselves. All others are not. In
10656 the context of a compound, accept the pragma as a "statement" and
10657 return so that we can check for a close brace. Otherwise we
10658 require a real statement and must go back and read one. */
10660 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10661 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10665 else if (token
->type
== CPP_EOF
)
10667 cp_parser_error (parser
, "expected statement");
10671 /* Everything else must be a declaration-statement or an
10672 expression-statement. Try for the declaration-statement
10673 first, unless we are looking at a `;', in which case we know that
10674 we have an expression-statement. */
10677 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10679 if (std_attrs
!= NULL_TREE
)
10681 /* Attributes should be parsed as part of the the
10682 declaration, so let's un-parse them. */
10683 saved_tokens
.rollback();
10684 std_attrs
= NULL_TREE
;
10687 cp_parser_parse_tentatively (parser
);
10688 /* Try to parse the declaration-statement. */
10689 cp_parser_declaration_statement (parser
);
10690 /* If that worked, we're done. */
10691 if (cp_parser_parse_definitely (parser
))
10694 /* Look for an expression-statement instead. */
10695 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10697 /* Handle [[fallthrough]];. */
10698 if (attribute_fallthrough_p (std_attrs
))
10700 /* The next token after the fallthrough attribute is ';'. */
10701 if (statement
== NULL_TREE
)
10703 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10704 statement
= build_call_expr_internal_loc (statement_location
,
10706 void_type_node
, 0);
10707 finish_expr_stmt (statement
);
10710 warning_at (statement_location
, OPT_Wattributes
,
10711 "%<fallthrough%> attribute not followed by %<;%>");
10712 std_attrs
= NULL_TREE
;
10716 /* Set the line number for the statement. */
10717 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10718 SET_EXPR_LOCATION (statement
, statement_location
);
10720 /* Allow "[[fallthrough]];", but warn otherwise. */
10721 if (std_attrs
!= NULL_TREE
)
10722 warning_at (attrs_location
,
10724 "attributes at the beginning of statement are ignored");
10727 /* Parse the label for a labeled-statement, i.e.
10730 case constant-expression :
10734 case constant-expression ... constant-expression : statement
10736 When a label is parsed without errors, the label is added to the
10737 parse tree by the finish_* functions, so this function doesn't
10738 have to return the label. */
10741 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10744 tree label
= NULL_TREE
;
10745 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10747 /* The next token should be an identifier. */
10748 token
= cp_lexer_peek_token (parser
->lexer
);
10749 if (token
->type
!= CPP_NAME
10750 && token
->type
!= CPP_KEYWORD
)
10752 cp_parser_error (parser
, "expected labeled-statement");
10756 /* Remember whether this case or a user-defined label is allowed to fall
10758 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10760 parser
->colon_corrects_to_scope_p
= false;
10761 switch (token
->keyword
)
10765 tree expr
, expr_hi
;
10766 cp_token
*ellipsis
;
10768 /* Consume the `case' token. */
10769 cp_lexer_consume_token (parser
->lexer
);
10770 /* Parse the constant-expression. */
10771 expr
= cp_parser_constant_expression (parser
);
10772 if (check_for_bare_parameter_packs (expr
))
10773 expr
= error_mark_node
;
10775 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10776 if (ellipsis
->type
== CPP_ELLIPSIS
)
10778 /* Consume the `...' token. */
10779 cp_lexer_consume_token (parser
->lexer
);
10780 expr_hi
= cp_parser_constant_expression (parser
);
10781 if (check_for_bare_parameter_packs (expr_hi
))
10782 expr_hi
= error_mark_node
;
10784 /* We don't need to emit warnings here, as the common code
10785 will do this for us. */
10788 expr_hi
= NULL_TREE
;
10790 if (parser
->in_switch_statement_p
)
10792 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10793 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10794 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10797 error_at (token
->location
,
10798 "case label %qE not within a switch statement",
10804 /* Consume the `default' token. */
10805 cp_lexer_consume_token (parser
->lexer
);
10807 if (parser
->in_switch_statement_p
)
10809 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10810 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10811 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10814 error_at (token
->location
, "case label not within a switch statement");
10818 /* Anything else must be an ordinary label. */
10819 label
= finish_label_stmt (cp_parser_identifier (parser
));
10820 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
10821 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
10825 /* Require the `:' token. */
10826 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10828 /* An ordinary label may optionally be followed by attributes.
10829 However, this is only permitted if the attributes are then
10830 followed by a semicolon. This is because, for backward
10831 compatibility, when parsing
10832 lab: __attribute__ ((unused)) int i;
10833 we want the attribute to attach to "i", not "lab". */
10834 if (label
!= NULL_TREE
10835 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10838 cp_parser_parse_tentatively (parser
);
10839 attrs
= cp_parser_gnu_attributes_opt (parser
);
10840 if (attrs
== NULL_TREE
10841 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10842 cp_parser_abort_tentative_parse (parser
);
10843 else if (!cp_parser_parse_definitely (parser
))
10846 attributes
= chainon (attributes
, attrs
);
10849 if (attributes
!= NULL_TREE
)
10850 cplus_decl_attributes (&label
, attributes
, 0);
10852 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10855 /* Parse an expression-statement.
10857 expression-statement:
10860 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10861 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10862 indicates whether this expression-statement is part of an
10863 expression statement. */
10866 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10868 tree statement
= NULL_TREE
;
10869 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10870 location_t loc
= token
->location
;
10872 /* There might be attribute fallthrough. */
10873 tree attr
= cp_parser_gnu_attributes_opt (parser
);
10875 /* If the next token is a ';', then there is no expression
10877 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10879 statement
= cp_parser_expression (parser
);
10880 if (statement
== error_mark_node
10881 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10883 cp_parser_skip_to_end_of_block_or_statement (parser
);
10884 return error_mark_node
;
10888 /* Handle [[fallthrough]];. */
10889 if (attribute_fallthrough_p (attr
))
10891 /* The next token after the fallthrough attribute is ';'. */
10892 if (statement
== NULL_TREE
)
10893 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10894 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
10895 void_type_node
, 0);
10897 warning_at (loc
, OPT_Wattributes
,
10898 "%<fallthrough%> attribute not followed by %<;%>");
10902 /* Allow "[[fallthrough]];", but warn otherwise. */
10903 if (attr
!= NULL_TREE
)
10904 warning_at (loc
, OPT_Wattributes
,
10905 "attributes at the beginning of statement are ignored");
10907 /* Give a helpful message for "A<T>::type t;" and the like. */
10908 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10909 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10911 if (TREE_CODE (statement
) == SCOPE_REF
)
10912 error_at (token
->location
, "need %<typename%> before %qE because "
10913 "%qT is a dependent scope",
10914 statement
, TREE_OPERAND (statement
, 0));
10915 else if (is_overloaded_fn (statement
)
10916 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10919 tree fn
= get_first_fn (statement
);
10920 error_at (token
->location
,
10921 "%<%T::%D%> names the constructor, not the type",
10922 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10926 /* Consume the final `;'. */
10927 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10929 if (in_statement_expr
10930 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10931 /* This is the final expression statement of a statement
10933 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10934 else if (statement
)
10935 statement
= finish_expr_stmt (statement
);
10940 /* Parse a compound-statement.
10942 compound-statement:
10943 { statement-seq [opt] }
10947 compound-statement:
10948 { label-declaration-seq [opt] statement-seq [opt] }
10950 label-declaration-seq:
10952 label-declaration-seq label-declaration
10954 Returns a tree representing the statement. */
10957 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10958 int bcs_flags
, bool function_body
)
10960 tree compound_stmt
;
10962 /* Consume the `{'. */
10963 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10964 return error_mark_node
;
10965 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10966 && !function_body
&& cxx_dialect
< cxx14
)
10967 pedwarn (input_location
, OPT_Wpedantic
,
10968 "compound-statement in constexpr function");
10969 /* Begin the compound-statement. */
10970 compound_stmt
= begin_compound_stmt (bcs_flags
);
10971 /* If the next keyword is `__label__' we have a label declaration. */
10972 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10973 cp_parser_label_declaration (parser
);
10974 /* Parse an (optional) statement-seq. */
10975 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10976 /* Finish the compound-statement. */
10977 finish_compound_stmt (compound_stmt
);
10978 /* Consume the `}'. */
10979 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10981 return compound_stmt
;
10984 /* Parse an (optional) statement-seq.
10988 statement-seq [opt] statement */
10991 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10993 /* Scan statements until there aren't any more. */
10996 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10998 /* If we are looking at a `}', then we have run out of
10999 statements; the same is true if we have reached the end
11000 of file, or have stumbled upon a stray '@end'. */
11001 if (token
->type
== CPP_CLOSE_BRACE
11002 || token
->type
== CPP_EOF
11003 || token
->type
== CPP_PRAGMA_EOL
11004 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11007 /* If we are in a compound statement and find 'else' then
11008 something went wrong. */
11009 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11011 if (parser
->in_statement
& IN_IF_STMT
)
11015 token
= cp_lexer_consume_token (parser
->lexer
);
11016 error_at (token
->location
, "%<else%> without a previous %<if%>");
11020 /* Parse the statement. */
11021 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11025 /* Return true if we're looking at (init; cond), false otherwise. */
11028 cp_parser_init_statement_p (cp_parser
*parser
)
11030 /* Save tokens so that we can put them back. */
11031 cp_lexer_save_tokens (parser
->lexer
);
11033 /* Look for ';' that is not nested in () or {}. */
11034 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11035 /*recovering=*/false,
11037 /*consume_paren=*/false);
11039 /* Roll back the tokens we skipped. */
11040 cp_lexer_rollback_tokens (parser
->lexer
);
11045 /* Parse a selection-statement.
11047 selection-statement:
11048 if ( init-statement [opt] condition ) statement
11049 if ( init-statement [opt] condition ) statement else statement
11050 switch ( init-statement [opt] condition ) statement
11052 Returns the new IF_STMT or SWITCH_STMT.
11054 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11055 is a (possibly labeled) if statement which is not enclosed in
11056 braces and has an else clause. This is used to implement
11059 CHAIN is a vector of if-else-if conditions. This is used to implement
11060 -Wduplicated-cond. */
11063 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11068 token_indent_info guard_tinfo
;
11073 /* Peek at the next token. */
11074 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11075 guard_tinfo
= get_token_indent_info (token
);
11077 /* See what kind of keyword it is. */
11078 keyword
= token
->keyword
;
11088 if (keyword
== RID_IF
11089 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11093 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11094 if (cxx_dialect
< cxx1z
&& !in_system_header_at (tok
->location
))
11095 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11096 "with -std=c++1z or -std=gnu++1z");
11099 /* Look for the `('. */
11100 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11102 cp_parser_skip_to_end_of_statement (parser
);
11103 return error_mark_node
;
11106 /* Begin the selection-statement. */
11107 if (keyword
== RID_IF
)
11109 statement
= begin_if_stmt ();
11110 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11113 statement
= begin_switch_stmt ();
11115 /* Parse the optional init-statement. */
11116 if (cp_parser_init_statement_p (parser
))
11119 if (cxx_dialect
< cxx1z
)
11120 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11121 "init-statement in selection statements only available "
11122 "with -std=c++1z or -std=gnu++1z");
11123 cp_parser_init_statement (parser
, &decl
);
11126 /* Parse the condition. */
11127 condition
= cp_parser_condition (parser
);
11128 /* Look for the `)'. */
11129 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11130 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11131 /*consume_paren=*/true);
11133 if (keyword
== RID_IF
)
11136 unsigned char in_statement
;
11138 /* Add the condition. */
11139 condition
= finish_if_stmt_cond (condition
, statement
);
11141 if (warn_duplicated_cond
)
11142 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11145 /* Parse the then-clause. */
11146 in_statement
= parser
->in_statement
;
11147 parser
->in_statement
|= IN_IF_STMT
;
11149 /* Outside a template, the non-selected branch of a constexpr
11150 if is a 'discarded statement', i.e. unevaluated. */
11151 bool was_discarded
= in_discarded_stmt
;
11152 bool discard_then
= (cx
&& !processing_template_decl
11153 && integer_zerop (condition
));
11156 in_discarded_stmt
= true;
11157 ++c_inhibit_evaluation_warnings
;
11160 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11163 parser
->in_statement
= in_statement
;
11165 finish_then_clause (statement
);
11169 THEN_CLAUSE (statement
) = NULL_TREE
;
11170 in_discarded_stmt
= was_discarded
;
11171 --c_inhibit_evaluation_warnings
;
11174 /* If the next token is `else', parse the else-clause. */
11175 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11178 bool discard_else
= (cx
&& !processing_template_decl
11179 && integer_nonzerop (condition
));
11182 in_discarded_stmt
= true;
11183 ++c_inhibit_evaluation_warnings
;
11187 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11188 /* Consume the `else' keyword. */
11189 cp_lexer_consume_token (parser
->lexer
);
11190 if (warn_duplicated_cond
)
11192 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11196 /* We've got "if (COND) else if (COND2)". Start
11197 the condition chain and add COND as the first
11199 chain
= new vec
<tree
> ();
11200 if (!CONSTANT_CLASS_P (condition
)
11201 && !TREE_SIDE_EFFECTS (condition
))
11203 /* Wrap it in a NOP_EXPR so that we can set the
11204 location of the condition. */
11205 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11207 SET_EXPR_LOCATION (e
, token
->location
);
11208 chain
->safe_push (e
);
11211 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11214 /* This is if-else without subsequent if. Zap the
11215 condition chain; we would have already warned at
11221 begin_else_clause (statement
);
11222 /* Parse the else-clause. */
11223 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11224 guard_tinfo
, chain
);
11226 finish_else_clause (statement
);
11228 /* If we are currently parsing a then-clause, then
11229 IF_P will not be NULL. We set it to true to
11230 indicate that this if statement has an else clause.
11231 This may trigger the Wparentheses warning below
11232 when we get back up to the parent if statement. */
11238 ELSE_CLAUSE (statement
) = NULL_TREE
;
11239 in_discarded_stmt
= was_discarded
;
11240 --c_inhibit_evaluation_warnings
;
11245 /* This if statement does not have an else clause. If
11246 NESTED_IF is true, then the then-clause has an if
11247 statement which does have an else clause. We warn
11248 about the potential ambiguity. */
11250 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11251 "suggest explicit braces to avoid ambiguous"
11253 if (warn_duplicated_cond
)
11255 /* We don't need the condition chain anymore. */
11261 /* Now we're all done with the if-statement. */
11262 finish_if_stmt (statement
);
11266 bool in_switch_statement_p
;
11267 unsigned char in_statement
;
11269 /* Add the condition. */
11270 finish_switch_cond (condition
, statement
);
11272 /* Parse the body of the switch-statement. */
11273 in_switch_statement_p
= parser
->in_switch_statement_p
;
11274 in_statement
= parser
->in_statement
;
11275 parser
->in_switch_statement_p
= true;
11276 parser
->in_statement
|= IN_SWITCH_STMT
;
11277 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11279 parser
->in_switch_statement_p
= in_switch_statement_p
;
11280 parser
->in_statement
= in_statement
;
11282 /* Now we're all done with the switch-statement. */
11283 finish_switch_stmt (statement
);
11291 cp_parser_error (parser
, "expected selection-statement");
11292 return error_mark_node
;
11296 /* Parse a condition.
11300 type-specifier-seq declarator = initializer-clause
11301 type-specifier-seq declarator braced-init-list
11306 type-specifier-seq declarator asm-specification [opt]
11307 attributes [opt] = assignment-expression
11309 Returns the expression that should be tested. */
11312 cp_parser_condition (cp_parser
* parser
)
11314 cp_decl_specifier_seq type_specifiers
;
11315 const char *saved_message
;
11316 int declares_class_or_enum
;
11318 /* Try the declaration first. */
11319 cp_parser_parse_tentatively (parser
);
11320 /* New types are not allowed in the type-specifier-seq for a
11322 saved_message
= parser
->type_definition_forbidden_message
;
11323 parser
->type_definition_forbidden_message
11324 = G_("types may not be defined in conditions");
11325 /* Parse the type-specifier-seq. */
11326 cp_parser_decl_specifier_seq (parser
,
11327 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11329 &declares_class_or_enum
);
11330 /* Restore the saved message. */
11331 parser
->type_definition_forbidden_message
= saved_message
;
11332 /* If all is well, we might be looking at a declaration. */
11333 if (!cp_parser_error_occurred (parser
))
11336 tree asm_specification
;
11338 cp_declarator
*declarator
;
11339 tree initializer
= NULL_TREE
;
11341 /* Parse the declarator. */
11342 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11343 /*ctor_dtor_or_conv_p=*/NULL
,
11344 /*parenthesized_p=*/NULL
,
11345 /*member_p=*/false,
11346 /*friend_p=*/false);
11347 /* Parse the attributes. */
11348 attributes
= cp_parser_attributes_opt (parser
);
11349 /* Parse the asm-specification. */
11350 asm_specification
= cp_parser_asm_specification_opt (parser
);
11351 /* If the next token is not an `=' or '{', then we might still be
11352 looking at an expression. For example:
11356 looks like a decl-specifier-seq and a declarator -- but then
11357 there is no `=', so this is an expression. */
11358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11359 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11360 cp_parser_simulate_error (parser
);
11362 /* If we did see an `=' or '{', then we are looking at a declaration
11364 if (cp_parser_parse_definitely (parser
))
11367 bool non_constant_p
;
11368 int flags
= LOOKUP_ONLYCONVERTING
;
11370 /* Create the declaration. */
11371 decl
= start_decl (declarator
, &type_specifiers
,
11372 /*initialized_p=*/true,
11373 attributes
, /*prefix_attributes=*/NULL_TREE
,
11376 /* Parse the initializer. */
11377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11379 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11380 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11385 /* Consume the `='. */
11386 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11387 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11389 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11390 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11392 /* Process the initializer. */
11393 cp_finish_decl (decl
,
11394 initializer
, !non_constant_p
,
11399 pop_scope (pushed_scope
);
11401 return convert_from_reference (decl
);
11404 /* If we didn't even get past the declarator successfully, we are
11405 definitely not looking at a declaration. */
11407 cp_parser_abort_tentative_parse (parser
);
11409 /* Otherwise, we are looking at an expression. */
11410 return cp_parser_expression (parser
);
11413 /* Parses a for-statement or range-for-statement until the closing ')',
11417 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11419 tree init
, scope
, decl
;
11422 /* Begin the for-statement. */
11423 scope
= begin_for_scope (&init
);
11425 /* Parse the initialization. */
11426 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11429 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11431 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11435 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11437 /* Normal for loop */
11438 tree condition
= NULL_TREE
;
11439 tree expression
= NULL_TREE
;
11442 stmt
= begin_for_stmt (scope
, init
);
11443 /* The init-statement has already been parsed in
11444 cp_parser_init_statement, so no work is needed here. */
11445 finish_init_stmt (stmt
);
11447 /* If there's a condition, process it. */
11448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11449 condition
= cp_parser_condition (parser
);
11452 cp_parser_error (parser
, "missing loop condition in loop with "
11453 "%<GCC ivdep%> pragma");
11454 condition
= error_mark_node
;
11456 finish_for_cond (condition
, stmt
, ivdep
);
11457 /* Look for the `;'. */
11458 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11460 /* If there's an expression, process it. */
11461 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11462 expression
= cp_parser_expression (parser
);
11463 finish_for_expr (expression
, stmt
);
11468 /* Tries to parse a range-based for-statement:
11471 decl-specifier-seq declarator : expression
11473 The decl-specifier-seq declarator and the `:' are already parsed by
11474 cp_parser_init_statement. If processing_template_decl it returns a
11475 newly created RANGE_FOR_STMT; if not, it is converted to a
11476 regular FOR_STMT. */
11479 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11482 tree stmt
, range_expr
;
11483 auto_vec
<cxx_binding
*, 16> bindings
;
11484 auto_vec
<tree
, 16> names
;
11485 tree decomp_first_name
= NULL_TREE
;
11486 unsigned int decomp_cnt
= 0;
11488 /* Get the range declaration momentarily out of the way so that
11489 the range expression doesn't clash with it. */
11490 if (range_decl
!= error_mark_node
)
11492 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11494 tree v
= DECL_VALUE_EXPR (range_decl
);
11495 /* For decomposition declaration get all of the corresponding
11496 declarations out of the way. */
11497 if (TREE_CODE (v
) == ARRAY_REF
11498 && VAR_P (TREE_OPERAND (v
, 0))
11499 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11501 tree d
= range_decl
;
11502 range_decl
= TREE_OPERAND (v
, 0);
11503 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11504 decomp_first_name
= d
;
11505 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11507 tree name
= DECL_NAME (d
);
11508 names
.safe_push (name
);
11509 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11510 IDENTIFIER_BINDING (name
)
11511 = IDENTIFIER_BINDING (name
)->previous
;
11515 if (names
.is_empty ())
11517 tree name
= DECL_NAME (range_decl
);
11518 names
.safe_push (name
);
11519 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11520 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11526 bool expr_non_constant_p
;
11527 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11530 range_expr
= cp_parser_expression (parser
);
11532 /* Put the range declaration(s) back into scope. */
11533 for (unsigned int i
= 0; i
< names
.length (); i
++)
11535 cxx_binding
*binding
= bindings
[i
];
11536 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11537 IDENTIFIER_BINDING (names
[i
]) = binding
;
11540 /* If in template, STMT is converted to a normal for-statement
11541 at instantiation. If not, it is done just ahead. */
11542 if (processing_template_decl
)
11544 if (check_for_bare_parameter_packs (range_expr
))
11545 range_expr
= error_mark_node
;
11546 stmt
= begin_range_for_stmt (scope
, init
);
11548 RANGE_FOR_IVDEP (stmt
) = 1;
11549 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11550 if (!type_dependent_expression_p (range_expr
)
11551 /* do_auto_deduction doesn't mess with template init-lists. */
11552 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11553 do_range_for_auto_deduction (range_decl
, range_expr
);
11557 stmt
= begin_for_stmt (scope
, init
);
11558 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11559 decomp_first_name
, decomp_cnt
, ivdep
);
11564 /* Subroutine of cp_convert_range_for: given the initializer expression,
11565 builds up the range temporary. */
11568 build_range_temp (tree range_expr
)
11570 tree range_type
, range_temp
;
11572 /* Find out the type deduced by the declaration
11573 `auto &&__range = range_expr'. */
11574 range_type
= cp_build_reference_type (make_auto (), true);
11575 range_type
= do_auto_deduction (range_type
, range_expr
,
11576 type_uses_auto (range_type
));
11578 /* Create the __range variable. */
11579 range_temp
= build_decl (input_location
, VAR_DECL
,
11580 get_identifier ("__for_range"), range_type
);
11581 TREE_USED (range_temp
) = 1;
11582 DECL_ARTIFICIAL (range_temp
) = 1;
11587 /* Used by cp_parser_range_for in template context: we aren't going to
11588 do a full conversion yet, but we still need to resolve auto in the
11589 type of the for-range-declaration if present. This is basically
11590 a shortcut version of cp_convert_range_for. */
11593 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11595 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11598 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11599 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11600 iter_type
= (cp_parser_perform_range_for_lookup
11601 (range_temp
, &begin_dummy
, &end_dummy
));
11604 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11606 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
11607 tf_warning_or_error
);
11608 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11609 iter_decl
, auto_node
);
11614 /* Converts a range-based for-statement into a normal
11615 for-statement, as per the definition.
11617 for (RANGE_DECL : RANGE_EXPR)
11620 should be equivalent to:
11623 auto &&__range = RANGE_EXPR;
11624 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11628 RANGE_DECL = *__begin;
11633 If RANGE_EXPR is an array:
11634 BEGIN_EXPR = __range
11635 END_EXPR = __range + ARRAY_SIZE(__range)
11636 Else if RANGE_EXPR has a member 'begin' or 'end':
11637 BEGIN_EXPR = __range.begin()
11638 END_EXPR = __range.end()
11640 BEGIN_EXPR = begin(__range)
11641 END_EXPR = end(__range);
11643 If __range has a member 'begin' but not 'end', or vice versa, we must
11644 still use the second alternative (it will surely fail, however).
11645 When calling begin()/end() in the third alternative we must use
11646 argument dependent lookup, but always considering 'std' as an associated
11650 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11651 tree decomp_first_name
, unsigned int decomp_cnt
,
11655 tree iter_type
, begin_expr
, end_expr
;
11656 tree condition
, expression
;
11658 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11659 /* If an error happened previously do nothing or else a lot of
11660 unhelpful errors would be issued. */
11661 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11666 if (VAR_P (range_expr
)
11667 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11668 /* Can't bind a reference to an array of runtime bound. */
11669 range_temp
= range_expr
;
11672 range_temp
= build_range_temp (range_expr
);
11673 pushdecl (range_temp
);
11674 cp_finish_decl (range_temp
, range_expr
,
11675 /*is_constant_init*/false, NULL_TREE
,
11676 LOOKUP_ONLYCONVERTING
);
11677 range_temp
= convert_from_reference (range_temp
);
11679 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11680 &begin_expr
, &end_expr
);
11683 /* The new for initialization statement. */
11684 begin
= build_decl (input_location
, VAR_DECL
,
11685 get_identifier ("__for_begin"), iter_type
);
11686 TREE_USED (begin
) = 1;
11687 DECL_ARTIFICIAL (begin
) = 1;
11689 cp_finish_decl (begin
, begin_expr
,
11690 /*is_constant_init*/false, NULL_TREE
,
11691 LOOKUP_ONLYCONVERTING
);
11693 if (cxx_dialect
>= cxx1z
)
11694 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11695 end
= build_decl (input_location
, VAR_DECL
,
11696 get_identifier ("__for_end"), iter_type
);
11697 TREE_USED (end
) = 1;
11698 DECL_ARTIFICIAL (end
) = 1;
11700 cp_finish_decl (end
, end_expr
,
11701 /*is_constant_init*/false, NULL_TREE
,
11702 LOOKUP_ONLYCONVERTING
);
11704 finish_init_stmt (statement
);
11706 /* The new for condition. */
11707 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11710 NULL
, tf_warning_or_error
);
11711 finish_for_cond (condition
, statement
, ivdep
);
11713 /* The new increment expression. */
11714 expression
= finish_unary_op_expr (input_location
,
11715 PREINCREMENT_EXPR
, begin
,
11716 tf_warning_or_error
);
11717 finish_for_expr (expression
, statement
);
11719 /* The declaration is initialized with *__begin inside the loop body. */
11720 cp_finish_decl (range_decl
,
11721 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11722 tf_warning_or_error
),
11723 /*is_constant_init*/false, NULL_TREE
,
11724 LOOKUP_ONLYCONVERTING
);
11725 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11726 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11731 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11732 We need to solve both at the same time because the method used
11733 depends on the existence of members begin or end.
11734 Returns the type deduced for the iterator expression. */
11737 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11739 if (error_operand_p (range
))
11741 *begin
= *end
= error_mark_node
;
11742 return error_mark_node
;
11745 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11747 error ("range-based %<for%> expression of type %qT "
11748 "has incomplete type", TREE_TYPE (range
));
11749 *begin
= *end
= error_mark_node
;
11750 return error_mark_node
;
11752 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11754 /* If RANGE is an array, we will use pointer arithmetic. */
11755 *begin
= decay_conversion (range
, tf_warning_or_error
);
11756 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11758 array_type_nelts_top (TREE_TYPE (range
)),
11760 return TREE_TYPE (*begin
);
11764 /* If it is not an array, we must do a bit of magic. */
11765 tree id_begin
, id_end
;
11766 tree member_begin
, member_end
;
11768 *begin
= *end
= error_mark_node
;
11770 id_begin
= get_identifier ("begin");
11771 id_end
= get_identifier ("end");
11772 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11773 /*protect=*/2, /*want_type=*/false,
11774 tf_warning_or_error
);
11775 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11776 /*protect=*/2, /*want_type=*/false,
11777 tf_warning_or_error
);
11779 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11781 /* Use the member functions. */
11782 if (member_begin
!= NULL_TREE
)
11783 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11785 error ("range-based %<for%> expression of type %qT has an "
11786 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11788 if (member_end
!= NULL_TREE
)
11789 *end
= cp_parser_range_for_member_function (range
, id_end
);
11791 error ("range-based %<for%> expression of type %qT has a "
11792 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11796 /* Use global functions with ADL. */
11797 vec
<tree
, va_gc
> *vec
;
11798 vec
= make_tree_vector ();
11800 vec_safe_push (vec
, range
);
11802 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11803 tf_warning_or_error
);
11804 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11805 tf_warning_or_error
);
11806 member_end
= perform_koenig_lookup (id_end
, vec
,
11807 tf_warning_or_error
);
11808 *end
= finish_call_expr (member_end
, &vec
, false, true,
11809 tf_warning_or_error
);
11811 release_tree_vector (vec
);
11814 /* Last common checks. */
11815 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11817 /* If one of the expressions is an error do no more checks. */
11818 *begin
= *end
= error_mark_node
;
11819 return error_mark_node
;
11821 else if (type_dependent_expression_p (*begin
)
11822 || type_dependent_expression_p (*end
))
11823 /* Can happen, when, eg, in a template context, Koenig lookup
11824 can't resolve begin/end (c++/58503). */
11828 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11829 /* The unqualified type of the __begin and __end temporaries should
11830 be the same, as required by the multiple auto declaration. */
11831 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11833 if (cxx_dialect
>= cxx1z
11834 && (build_x_binary_op (input_location
, NE_EXPR
,
11835 *begin
, ERROR_MARK
,
11838 != error_mark_node
))
11839 /* P0184R0 allows __begin and __end to have different types,
11840 but make sure they are comparable so we can give a better
11843 error ("inconsistent begin/end types in range-based %<for%> "
11844 "statement: %qT and %qT",
11845 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11852 /* Helper function for cp_parser_perform_range_for_lookup.
11853 Builds a tree for RANGE.IDENTIFIER(). */
11856 cp_parser_range_for_member_function (tree range
, tree identifier
)
11859 vec
<tree
, va_gc
> *vec
;
11861 member
= finish_class_member_access_expr (range
, identifier
,
11862 false, tf_warning_or_error
);
11863 if (member
== error_mark_node
)
11864 return error_mark_node
;
11866 vec
= make_tree_vector ();
11867 res
= finish_call_expr (member
, &vec
,
11868 /*disallow_virtual=*/false,
11869 /*koenig_p=*/false,
11870 tf_warning_or_error
);
11871 release_tree_vector (vec
);
11875 /* Parse an iteration-statement.
11877 iteration-statement:
11878 while ( condition ) statement
11879 do statement while ( expression ) ;
11880 for ( init-statement condition [opt] ; expression [opt] )
11883 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11886 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
11891 unsigned char in_statement
;
11892 token_indent_info guard_tinfo
;
11894 /* Peek at the next token. */
11895 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11897 return error_mark_node
;
11899 guard_tinfo
= get_token_indent_info (token
);
11901 /* Remember whether or not we are already within an iteration
11903 in_statement
= parser
->in_statement
;
11905 /* See what kind of keyword it is. */
11906 keyword
= token
->keyword
;
11913 /* Begin the while-statement. */
11914 statement
= begin_while_stmt ();
11915 /* Look for the `('. */
11916 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11917 /* Parse the condition. */
11918 condition
= cp_parser_condition (parser
);
11919 finish_while_stmt_cond (condition
, statement
, ivdep
);
11920 /* Look for the `)'. */
11921 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11922 /* Parse the dependent statement. */
11923 parser
->in_statement
= IN_ITERATION_STMT
;
11924 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11925 parser
->in_statement
= in_statement
;
11926 /* We're done with the while-statement. */
11927 finish_while_stmt (statement
);
11935 /* Begin the do-statement. */
11936 statement
= begin_do_stmt ();
11937 /* Parse the body of the do-statement. */
11938 parser
->in_statement
= IN_ITERATION_STMT
;
11939 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11940 parser
->in_statement
= in_statement
;
11941 finish_do_body (statement
);
11942 /* Look for the `while' keyword. */
11943 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11944 /* Look for the `('. */
11945 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11946 /* Parse the expression. */
11947 expression
= cp_parser_expression (parser
);
11948 /* We're done with the do-statement. */
11949 finish_do_stmt (expression
, statement
, ivdep
);
11950 /* Look for the `)'. */
11951 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11952 /* Look for the `;'. */
11953 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11959 /* Look for the `('. */
11960 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11962 statement
= cp_parser_for (parser
, ivdep
);
11964 /* Look for the `)'. */
11965 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11967 /* Parse the body of the for-statement. */
11968 parser
->in_statement
= IN_ITERATION_STMT
;
11969 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11970 parser
->in_statement
= in_statement
;
11972 /* We're done with the for-statement. */
11973 finish_for_stmt (statement
);
11978 cp_parser_error (parser
, "expected iteration-statement");
11979 statement
= error_mark_node
;
11986 /* Parse a init-statement or the declarator of a range-based-for.
11987 Returns true if a range-based-for declaration is seen.
11990 expression-statement
11991 simple-declaration */
11994 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
11996 /* If the next token is a `;', then we have an empty
11997 expression-statement. Grammatically, this is also a
11998 simple-declaration, but an invalid one, because it does not
11999 declare anything. Therefore, if we did not handle this case
12000 specially, we would issue an error message about an invalid
12002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12004 bool is_range_for
= false;
12005 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12007 /* A colon is used in range-based for. */
12008 parser
->colon_corrects_to_scope_p
= false;
12010 /* We're going to speculatively look for a declaration, falling back
12011 to an expression, if necessary. */
12012 cp_parser_parse_tentatively (parser
);
12013 /* Parse the declaration. */
12014 cp_parser_simple_declaration (parser
,
12015 /*function_definition_allowed_p=*/false,
12017 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12020 /* It is a range-for, consume the ':' */
12021 cp_lexer_consume_token (parser
->lexer
);
12022 is_range_for
= true;
12023 if (cxx_dialect
< cxx11
)
12025 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12026 "range-based %<for%> loops only available with "
12027 "-std=c++11 or -std=gnu++11");
12028 *decl
= error_mark_node
;
12032 /* The ';' is not consumed yet because we told
12033 cp_parser_simple_declaration not to. */
12034 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12036 if (cp_parser_parse_definitely (parser
))
12037 return is_range_for
;
12038 /* If the tentative parse failed, then we shall need to look for an
12039 expression-statement. */
12041 /* If we are here, it is an expression-statement. */
12042 cp_parser_expression_statement (parser
, NULL_TREE
);
12046 /* Parse a jump-statement.
12051 return expression [opt] ;
12052 return braced-init-list ;
12058 goto * expression ;
12060 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12063 cp_parser_jump_statement (cp_parser
* parser
)
12065 tree statement
= error_mark_node
;
12068 unsigned char in_statement
;
12070 /* Peek at the next token. */
12071 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12073 return error_mark_node
;
12075 /* See what kind of keyword it is. */
12076 keyword
= token
->keyword
;
12080 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12081 switch (in_statement
)
12084 error_at (token
->location
, "break statement not within loop or switch");
12087 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12088 || in_statement
== IN_ITERATION_STMT
);
12089 statement
= finish_break_stmt ();
12090 if (in_statement
== IN_ITERATION_STMT
)
12091 break_maybe_infinite_loop ();
12094 error_at (token
->location
, "invalid exit from OpenMP structured block");
12097 error_at (token
->location
, "break statement used with OpenMP for loop");
12099 case IN_CILK_SIMD_FOR
:
12100 error_at (token
->location
, "break statement used with Cilk Plus for loop");
12103 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12107 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12110 error_at (token
->location
, "continue statement not within a loop");
12112 case IN_CILK_SIMD_FOR
:
12113 error_at (token
->location
,
12114 "continue statement within %<#pragma simd%> loop body");
12115 /* Fall through. */
12116 case IN_ITERATION_STMT
:
12118 statement
= finish_continue_stmt ();
12121 error_at (token
->location
, "invalid exit from OpenMP structured block");
12124 gcc_unreachable ();
12126 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12132 bool expr_non_constant_p
;
12134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12136 cp_lexer_set_source_position (parser
->lexer
);
12137 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12138 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12140 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12141 expr
= cp_parser_expression (parser
);
12143 /* If the next token is a `;', then there is no
12146 /* Build the return-statement. */
12147 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12148 /* Don't deduce from a discarded return statement. */;
12150 statement
= finish_return_stmt (expr
);
12151 /* Look for the final `;'. */
12152 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12157 if (parser
->in_function_body
12158 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12160 error ("%<goto%> in %<constexpr%> function");
12161 cp_function_chain
->invalid_constexpr
= true;
12164 /* Create the goto-statement. */
12165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12167 /* Issue a warning about this use of a GNU extension. */
12168 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12169 /* Consume the '*' token. */
12170 cp_lexer_consume_token (parser
->lexer
);
12171 /* Parse the dependent expression. */
12172 finish_goto_stmt (cp_parser_expression (parser
));
12175 finish_goto_stmt (cp_parser_identifier (parser
));
12176 /* Look for the final `;'. */
12177 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12181 cp_parser_error (parser
, "expected jump-statement");
12188 /* Parse a declaration-statement.
12190 declaration-statement:
12191 block-declaration */
12194 cp_parser_declaration_statement (cp_parser
* parser
)
12198 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12199 p
= obstack_alloc (&declarator_obstack
, 0);
12201 /* Parse the block-declaration. */
12202 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12204 /* Free any declarators allocated. */
12205 obstack_free (&declarator_obstack
, p
);
12208 /* Some dependent statements (like `if (cond) statement'), are
12209 implicitly in their own scope. In other words, if the statement is
12210 a single statement (as opposed to a compound-statement), it is
12211 none-the-less treated as if it were enclosed in braces. Any
12212 declarations appearing in the dependent statement are out of scope
12213 after control passes that point. This function parses a statement,
12214 but ensures that is in its own scope, even if it is not a
12215 compound-statement.
12217 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12218 is a (possibly labeled) if statement which is not enclosed in
12219 braces and has an else clause. This is used to implement
12222 CHAIN is a vector of if-else-if conditions. This is used to implement
12225 Returns the new statement. */
12228 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12229 const token_indent_info
&guard_tinfo
,
12233 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12234 token_indent_info body_tinfo
12235 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12240 /* Mark if () ; with a special NOP_EXPR. */
12241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12243 cp_lexer_consume_token (parser
->lexer
);
12244 statement
= add_stmt (build_empty_stmt (body_loc
));
12246 if (guard_tinfo
.keyword
== RID_IF
12247 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12248 warning_at (body_loc
, OPT_Wempty_body
,
12249 "suggest braces around empty body in an %<if%> statement");
12250 else if (guard_tinfo
.keyword
== RID_ELSE
)
12251 warning_at (body_loc
, OPT_Wempty_body
,
12252 "suggest braces around empty body in an %<else%> statement");
12254 /* if a compound is opened, we simply parse the statement directly. */
12255 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12256 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12257 /* If the token is not a `{', then we must take special action. */
12260 /* Create a compound-statement. */
12261 statement
= begin_compound_stmt (0);
12262 /* Parse the dependent-statement. */
12263 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
12264 /* Finish the dummy compound-statement. */
12265 finish_compound_stmt (statement
);
12268 token_indent_info next_tinfo
12269 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12270 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12272 /* Return the statement. */
12276 /* For some dependent statements (like `while (cond) statement'), we
12277 have already created a scope. Therefore, even if the dependent
12278 statement is a compound-statement, we do not want to create another
12282 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12283 const token_indent_info
&guard_tinfo
)
12285 /* If the token is a `{', then we must take special action. */
12286 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12288 token_indent_info body_tinfo
12289 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12291 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
12292 token_indent_info next_tinfo
12293 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12294 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12298 /* Avoid calling cp_parser_compound_statement, so that we
12299 don't create a new scope. Do everything else by hand. */
12300 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
12301 /* If the next keyword is `__label__' we have a label declaration. */
12302 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12303 cp_parser_label_declaration (parser
);
12304 /* Parse an (optional) statement-seq. */
12305 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12306 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12310 /* Declarations [gram.dcl.dcl] */
12312 /* Parse an optional declaration-sequence.
12316 declaration-seq declaration */
12319 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12325 token
= cp_lexer_peek_token (parser
->lexer
);
12327 if (token
->type
== CPP_CLOSE_BRACE
12328 || token
->type
== CPP_EOF
12329 || token
->type
== CPP_PRAGMA_EOL
)
12332 if (token
->type
== CPP_SEMICOLON
)
12334 /* A declaration consisting of a single semicolon is
12335 invalid. Allow it unless we're being pedantic. */
12336 cp_lexer_consume_token (parser
->lexer
);
12337 if (!in_system_header_at (input_location
))
12338 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12342 /* If we're entering or exiting a region that's implicitly
12343 extern "C", modify the lang context appropriately. */
12344 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12346 push_lang_context (lang_name_c
);
12347 parser
->implicit_extern_c
= true;
12349 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12351 pop_lang_context ();
12352 parser
->implicit_extern_c
= false;
12355 if (token
->type
== CPP_PRAGMA
)
12357 /* A top-level declaration can consist solely of a #pragma.
12358 A nested declaration cannot, so this is done here and not
12359 in cp_parser_declaration. (A #pragma at block scope is
12360 handled in cp_parser_statement.) */
12361 cp_parser_pragma (parser
, pragma_external
, NULL
);
12365 /* Parse the declaration itself. */
12366 cp_parser_declaration (parser
);
12370 /* Parse a declaration.
12374 function-definition
12375 template-declaration
12376 explicit-instantiation
12377 explicit-specialization
12378 linkage-specification
12379 namespace-definition
12387 __extension__ declaration */
12390 cp_parser_declaration (cp_parser
* parser
)
12394 int saved_pedantic
;
12396 tree attributes
= NULL_TREE
;
12398 /* Check for the `__extension__' keyword. */
12399 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12401 /* Parse the qualified declaration. */
12402 cp_parser_declaration (parser
);
12403 /* Restore the PEDANTIC flag. */
12404 pedantic
= saved_pedantic
;
12409 /* Try to figure out what kind of declaration is present. */
12410 token1
= *cp_lexer_peek_token (parser
->lexer
);
12412 if (token1
.type
!= CPP_EOF
)
12413 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12416 token2
.type
= CPP_EOF
;
12417 token2
.keyword
= RID_MAX
;
12420 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12421 p
= obstack_alloc (&declarator_obstack
, 0);
12423 /* If the next token is `extern' and the following token is a string
12424 literal, then we have a linkage specification. */
12425 if (token1
.keyword
== RID_EXTERN
12426 && cp_parser_is_pure_string_literal (&token2
))
12427 cp_parser_linkage_specification (parser
);
12428 /* If the next token is `template', then we have either a template
12429 declaration, an explicit instantiation, or an explicit
12431 else if (token1
.keyword
== RID_TEMPLATE
)
12433 /* `template <>' indicates a template specialization. */
12434 if (token2
.type
== CPP_LESS
12435 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12436 cp_parser_explicit_specialization (parser
);
12437 /* `template <' indicates a template declaration. */
12438 else if (token2
.type
== CPP_LESS
)
12439 cp_parser_template_declaration (parser
, /*member_p=*/false);
12440 /* Anything else must be an explicit instantiation. */
12442 cp_parser_explicit_instantiation (parser
);
12444 /* If the next token is `export', then we have a template
12446 else if (token1
.keyword
== RID_EXPORT
)
12447 cp_parser_template_declaration (parser
, /*member_p=*/false);
12448 /* If the next token is `extern', 'static' or 'inline' and the one
12449 after that is `template', we have a GNU extended explicit
12450 instantiation directive. */
12451 else if (cp_parser_allow_gnu_extensions_p (parser
)
12452 && (token1
.keyword
== RID_EXTERN
12453 || token1
.keyword
== RID_STATIC
12454 || token1
.keyword
== RID_INLINE
)
12455 && token2
.keyword
== RID_TEMPLATE
)
12456 cp_parser_explicit_instantiation (parser
);
12457 /* If the next token is `namespace', check for a named or unnamed
12458 namespace definition. */
12459 else if (token1
.keyword
== RID_NAMESPACE
12460 && (/* A named namespace definition. */
12461 (token2
.type
== CPP_NAME
12462 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12464 || (token2
.type
== CPP_OPEN_SQUARE
12465 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12466 == CPP_OPEN_SQUARE
)
12467 /* An unnamed namespace definition. */
12468 || token2
.type
== CPP_OPEN_BRACE
12469 || token2
.keyword
== RID_ATTRIBUTE
))
12470 cp_parser_namespace_definition (parser
);
12471 /* An inline (associated) namespace definition. */
12472 else if (token1
.keyword
== RID_INLINE
12473 && token2
.keyword
== RID_NAMESPACE
)
12474 cp_parser_namespace_definition (parser
);
12475 /* Objective-C++ declaration/definition. */
12476 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12477 cp_parser_objc_declaration (parser
, NULL_TREE
);
12478 else if (c_dialect_objc ()
12479 && token1
.keyword
== RID_ATTRIBUTE
12480 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12481 cp_parser_objc_declaration (parser
, attributes
);
12482 /* At this point we may have a template declared by a concept
12484 else if (flag_concepts
12485 && cp_parser_template_declaration_after_export (parser
,
12486 /*member_p=*/false))
12489 /* Try to parse a block-declaration, or a function-definition. */
12490 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12492 /* Free any declarators allocated. */
12493 obstack_free (&declarator_obstack
, p
);
12496 /* Parse a block-declaration.
12501 namespace-alias-definition
12508 __extension__ block-declaration
12513 static_assert-declaration
12515 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12516 part of a declaration-statement. */
12519 cp_parser_block_declaration (cp_parser
*parser
,
12523 int saved_pedantic
;
12525 /* Check for the `__extension__' keyword. */
12526 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12528 /* Parse the qualified declaration. */
12529 cp_parser_block_declaration (parser
, statement_p
);
12530 /* Restore the PEDANTIC flag. */
12531 pedantic
= saved_pedantic
;
12536 /* Peek at the next token to figure out which kind of declaration is
12538 token1
= cp_lexer_peek_token (parser
->lexer
);
12540 /* If the next keyword is `asm', we have an asm-definition. */
12541 if (token1
->keyword
== RID_ASM
)
12544 cp_parser_commit_to_tentative_parse (parser
);
12545 cp_parser_asm_definition (parser
);
12547 /* If the next keyword is `namespace', we have a
12548 namespace-alias-definition. */
12549 else if (token1
->keyword
== RID_NAMESPACE
)
12550 cp_parser_namespace_alias_definition (parser
);
12551 /* If the next keyword is `using', we have a
12552 using-declaration, a using-directive, or an alias-declaration. */
12553 else if (token1
->keyword
== RID_USING
)
12558 cp_parser_commit_to_tentative_parse (parser
);
12559 /* If the token after `using' is `namespace', then we have a
12560 using-directive. */
12561 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12562 if (token2
->keyword
== RID_NAMESPACE
)
12563 cp_parser_using_directive (parser
);
12564 /* If the second token after 'using' is '=', then we have an
12565 alias-declaration. */
12566 else if (cxx_dialect
>= cxx11
12567 && token2
->type
== CPP_NAME
12568 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12569 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12570 cp_parser_alias_declaration (parser
);
12571 /* Otherwise, it's a using-declaration. */
12573 cp_parser_using_declaration (parser
,
12574 /*access_declaration_p=*/false);
12576 /* If the next keyword is `__label__' we have a misplaced label
12578 else if (token1
->keyword
== RID_LABEL
)
12580 cp_lexer_consume_token (parser
->lexer
);
12581 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12582 cp_parser_skip_to_end_of_statement (parser
);
12583 /* If the next token is now a `;', consume it. */
12584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12585 cp_lexer_consume_token (parser
->lexer
);
12587 /* If the next token is `static_assert' we have a static assertion. */
12588 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12589 cp_parser_static_assert (parser
, /*member_p=*/false);
12590 /* Anything else must be a simple-declaration. */
12592 cp_parser_simple_declaration (parser
, !statement_p
,
12593 /*maybe_range_for_decl*/NULL
);
12596 /* Parse a simple-declaration.
12598 simple-declaration:
12599 decl-specifier-seq [opt] init-declarator-list [opt] ;
12600 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12601 brace-or-equal-initializer ;
12603 init-declarator-list:
12605 init-declarator-list , init-declarator
12607 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12608 function-definition as a simple-declaration.
12610 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12611 parsed declaration if it is an uninitialized single declarator not followed
12612 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12613 if present, will not be consumed. */
12616 cp_parser_simple_declaration (cp_parser
* parser
,
12617 bool function_definition_allowed_p
,
12618 tree
*maybe_range_for_decl
)
12620 cp_decl_specifier_seq decl_specifiers
;
12621 int declares_class_or_enum
;
12622 bool saw_declarator
;
12623 location_t comma_loc
= UNKNOWN_LOCATION
;
12624 location_t init_loc
= UNKNOWN_LOCATION
;
12626 if (maybe_range_for_decl
)
12627 *maybe_range_for_decl
= NULL_TREE
;
12629 /* Defer access checks until we know what is being declared; the
12630 checks for names appearing in the decl-specifier-seq should be
12631 done as if we were in the scope of the thing being declared. */
12632 push_deferring_access_checks (dk_deferred
);
12634 /* Parse the decl-specifier-seq. We have to keep track of whether
12635 or not the decl-specifier-seq declares a named class or
12636 enumeration type, since that is the only case in which the
12637 init-declarator-list is allowed to be empty.
12641 In a simple-declaration, the optional init-declarator-list can be
12642 omitted only when declaring a class or enumeration, that is when
12643 the decl-specifier-seq contains either a class-specifier, an
12644 elaborated-type-specifier, or an enum-specifier. */
12645 cp_parser_decl_specifier_seq (parser
,
12646 CP_PARSER_FLAGS_OPTIONAL
,
12648 &declares_class_or_enum
);
12649 /* We no longer need to defer access checks. */
12650 stop_deferring_access_checks ();
12652 /* In a block scope, a valid declaration must always have a
12653 decl-specifier-seq. By not trying to parse declarators, we can
12654 resolve the declaration/expression ambiguity more quickly. */
12655 if (!function_definition_allowed_p
12656 && !decl_specifiers
.any_specifiers_p
)
12658 cp_parser_error (parser
, "expected declaration");
12662 /* If the next two tokens are both identifiers, the code is
12663 erroneous. The usual cause of this situation is code like:
12667 where "T" should name a type -- but does not. */
12668 if (!decl_specifiers
.any_type_specifiers_p
12669 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12671 /* If parsing tentatively, we should commit; we really are
12672 looking at a declaration. */
12673 cp_parser_commit_to_tentative_parse (parser
);
12678 /* If we have seen at least one decl-specifier, and the next token
12679 is not a parenthesis, then we must be looking at a declaration.
12680 (After "int (" we might be looking at a functional cast.) */
12681 if (decl_specifiers
.any_specifiers_p
12682 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12683 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12684 && !cp_parser_error_occurred (parser
))
12685 cp_parser_commit_to_tentative_parse (parser
);
12687 /* Look for C++17 decomposition declaration. */
12688 for (size_t n
= 1; ; n
++)
12689 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12690 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12692 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12693 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12694 && decl_specifiers
.any_specifiers_p
)
12697 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12698 maybe_range_for_decl
,
12701 /* The next token should be either a `,' or a `;'. */
12702 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12703 /* If it's a `;', we are done. */
12704 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12706 /* Anything else is an error. */
12709 /* If we have already issued an error message we don't need
12710 to issue another one. */
12711 if ((decl
!= error_mark_node
12712 && DECL_INITIAL (decl
) != error_mark_node
)
12713 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12714 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12715 /* Skip tokens until we reach the end of the statement. */
12716 cp_parser_skip_to_end_of_statement (parser
);
12717 /* If the next token is now a `;', consume it. */
12718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12719 cp_lexer_consume_token (parser
->lexer
);
12727 bool auto_specifier_p
;
12728 /* NULL_TREE if both variable and function declaration are allowed,
12729 error_mark_node if function declaration are not allowed and
12730 a FUNCTION_DECL that should be diagnosed if it is followed by
12731 variable declarations. */
12732 tree auto_function_declaration
;
12734 last_type
= NULL_TREE
;
12736 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12737 auto_function_declaration
= NULL_TREE
;
12739 /* Keep going until we hit the `;' at the end of the simple
12741 saw_declarator
= false;
12742 while (cp_lexer_next_token_is_not (parser
->lexer
,
12746 bool function_definition_p
;
12748 tree auto_result
= NULL_TREE
;
12750 if (saw_declarator
)
12752 /* If we are processing next declarator, comma is expected */
12753 token
= cp_lexer_peek_token (parser
->lexer
);
12754 gcc_assert (token
->type
== CPP_COMMA
);
12755 cp_lexer_consume_token (parser
->lexer
);
12756 if (maybe_range_for_decl
)
12758 *maybe_range_for_decl
= error_mark_node
;
12759 if (comma_loc
== UNKNOWN_LOCATION
)
12760 comma_loc
= token
->location
;
12764 saw_declarator
= true;
12766 /* Parse the init-declarator. */
12767 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12769 function_definition_allowed_p
,
12770 /*member_p=*/false,
12771 declares_class_or_enum
,
12772 &function_definition_p
,
12773 maybe_range_for_decl
,
12776 /* If an error occurred while parsing tentatively, exit quickly.
12777 (That usually happens when in the body of a function; each
12778 statement is treated as a declaration-statement until proven
12780 if (cp_parser_error_occurred (parser
))
12783 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
12785 /* If the init-declarator-list contains more than one
12786 init-declarator, they shall all form declarations of
12788 if (auto_function_declaration
== NULL_TREE
)
12789 auto_function_declaration
12790 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
12791 else if (TREE_CODE (decl
) == FUNCTION_DECL
12792 || auto_function_declaration
!= error_mark_node
)
12794 error_at (decl_specifiers
.locations
[ds_type_spec
],
12795 "non-variable %qD in declaration with more than one "
12796 "declarator with placeholder type",
12797 TREE_CODE (decl
) == FUNCTION_DECL
12798 ? decl
: auto_function_declaration
);
12799 auto_function_declaration
= error_mark_node
;
12804 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
12807 && last_type
!= error_mark_node
12808 && !same_type_p (auto_result
, last_type
))
12810 /* If the list of declarators contains more than one declarator,
12811 the type of each declared variable is determined as described
12812 above. If the type deduced for the template parameter U is not
12813 the same in each deduction, the program is ill-formed. */
12814 error_at (decl_specifiers
.locations
[ds_type_spec
],
12815 "inconsistent deduction for %qT: %qT and then %qT",
12816 decl_specifiers
.type
, last_type
, auto_result
);
12817 last_type
= error_mark_node
;
12820 last_type
= auto_result
;
12823 /* Handle function definitions specially. */
12824 if (function_definition_p
)
12826 /* If the next token is a `,', then we are probably
12827 processing something like:
12831 which is erroneous. */
12832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12835 error_at (token
->location
,
12837 " declarations and function-definitions is forbidden");
12839 /* Otherwise, we're done with the list of declarators. */
12842 pop_deferring_access_checks ();
12846 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12847 *maybe_range_for_decl
= decl
;
12848 /* The next token should be either a `,' or a `;'. */
12849 token
= cp_lexer_peek_token (parser
->lexer
);
12850 /* If it's a `,', there are more declarators to come. */
12851 if (token
->type
== CPP_COMMA
)
12852 /* will be consumed next time around */;
12853 /* If it's a `;', we are done. */
12854 else if (token
->type
== CPP_SEMICOLON
)
12856 else if (maybe_range_for_decl
)
12858 if (declares_class_or_enum
&& token
->type
== CPP_COLON
)
12859 permerror (decl_specifiers
.locations
[ds_type_spec
],
12860 "types may not be defined in a for-range-declaration");
12863 /* Anything else is an error. */
12866 /* If we have already issued an error message we don't need
12867 to issue another one. */
12868 if ((decl
!= error_mark_node
12869 && DECL_INITIAL (decl
) != error_mark_node
)
12870 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12871 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12872 /* Skip tokens until we reach the end of the statement. */
12873 cp_parser_skip_to_end_of_statement (parser
);
12874 /* If the next token is now a `;', consume it. */
12875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12876 cp_lexer_consume_token (parser
->lexer
);
12879 /* After the first time around, a function-definition is not
12880 allowed -- even if it was OK at first. For example:
12885 function_definition_allowed_p
= false;
12888 /* Issue an error message if no declarators are present, and the
12889 decl-specifier-seq does not itself declare a class or
12890 enumeration: [dcl.dcl]/3. */
12891 if (!saw_declarator
)
12893 if (cp_parser_declares_only_class_p (parser
))
12895 if (!declares_class_or_enum
12896 && decl_specifiers
.type
12897 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12898 /* Ensure an error is issued anyway when finish_decltype_type,
12899 called via cp_parser_decl_specifier_seq, returns a class or
12900 an enumeration (c++/51786). */
12901 decl_specifiers
.type
= NULL_TREE
;
12902 shadow_tag (&decl_specifiers
);
12904 /* Perform any deferred access checks. */
12905 perform_deferred_access_checks (tf_warning_or_error
);
12908 /* Consume the `;'. */
12910 if (!maybe_range_for_decl
)
12911 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12912 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12914 if (init_loc
!= UNKNOWN_LOCATION
)
12915 error_at (init_loc
, "initializer in range-based %<for%> loop");
12916 if (comma_loc
!= UNKNOWN_LOCATION
)
12917 error_at (comma_loc
,
12918 "multiple declarations in range-based %<for%> loop");
12922 pop_deferring_access_checks ();
12925 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12926 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12930 cp_parser_decomposition_declaration (cp_parser
*parser
,
12931 cp_decl_specifier_seq
*decl_specifiers
,
12932 tree
*maybe_range_for_decl
,
12933 location_t
*init_loc
)
12935 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
12936 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12937 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
12939 /* Parse the identifier-list. */
12940 auto_vec
<cp_expr
, 10> v
;
12941 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12944 cp_expr e
= cp_parser_identifier (parser
);
12945 if (e
.get_value () == error_mark_node
)
12948 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12950 cp_lexer_consume_token (parser
->lexer
);
12953 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12954 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
12956 end_loc
= UNKNOWN_LOCATION
;
12957 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
12959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
12960 cp_lexer_consume_token (parser
->lexer
);
12963 cp_parser_skip_to_end_of_statement (parser
);
12964 return error_mark_node
;
12968 if (cxx_dialect
< cxx1z
)
12969 pedwarn (loc
, 0, "decomposition declaration only available with "
12970 "-std=c++1z or -std=gnu++1z");
12973 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
12974 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
12975 declarator
->id_loc
= loc
;
12976 if (ref_qual
!= REF_QUAL_NONE
)
12977 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
12978 ref_qual
== REF_QUAL_RVALUE
,
12980 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
12981 NULL_TREE
, decl_specifiers
->attributes
,
12983 tree orig_decl
= decl
;
12987 cp_decl_specifier_seq decl_specs
;
12988 clear_decl_specs (&decl_specs
);
12989 decl_specs
.type
= make_auto ();
12991 FOR_EACH_VEC_ELT (v
, i
, e
)
12994 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
12996 declarator
->u
.id
.unqualified_name
= e
.get_value ();
12997 declarator
->id_loc
= e
.get_location ();
12998 tree elt_pushed_scope
;
12999 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13000 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13001 if (decl2
== error_mark_node
)
13002 decl
= error_mark_node
;
13003 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13005 /* Ensure we've diagnosed redeclaration if we aren't creating
13007 gcc_assert (errorcount
);
13008 decl
= error_mark_node
;
13012 if (elt_pushed_scope
)
13013 pop_scope (elt_pushed_scope
);
13018 error_at (loc
, "empty decomposition declaration");
13019 decl
= error_mark_node
;
13022 if (maybe_range_for_decl
== NULL
13023 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13025 bool non_constant_p
= false, is_direct_init
= false;
13026 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13027 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13030 if (decl
!= error_mark_node
)
13032 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13033 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13034 cp_finish_decomp (decl
, prev
, v
.length ());
13037 else if (decl
!= error_mark_node
)
13039 *maybe_range_for_decl
= prev
;
13040 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13041 the underlying DECL. */
13042 cp_finish_decomp (decl
, prev
, v
.length ());
13046 pop_scope (pushed_scope
);
13048 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13050 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13051 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13057 /* Parse a decl-specifier-seq.
13059 decl-specifier-seq:
13060 decl-specifier-seq [opt] decl-specifier
13061 decl-specifier attribute-specifier-seq [opt] (C++11)
13064 storage-class-specifier
13075 Concepts Extension:
13080 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13082 The parser flags FLAGS is used to control type-specifier parsing.
13084 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13087 1: one of the decl-specifiers is an elaborated-type-specifier
13088 (i.e., a type declaration)
13089 2: one of the decl-specifiers is an enum-specifier or a
13090 class-specifier (i.e., a type definition)
13095 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13096 cp_parser_flags flags
,
13097 cp_decl_specifier_seq
*decl_specs
,
13098 int* declares_class_or_enum
)
13100 bool constructor_possible_p
= !parser
->in_declarator_p
;
13101 bool found_decl_spec
= false;
13102 cp_token
*start_token
= NULL
;
13105 /* Clear DECL_SPECS. */
13106 clear_decl_specs (decl_specs
);
13108 /* Assume no class or enumeration type is declared. */
13109 *declares_class_or_enum
= 0;
13111 /* Keep reading specifiers until there are no more to read. */
13114 bool constructor_p
;
13118 /* Peek at the next token. */
13119 token
= cp_lexer_peek_token (parser
->lexer
);
13121 /* Save the first token of the decl spec list for error
13124 start_token
= token
;
13125 /* Handle attributes. */
13126 if (cp_next_tokens_can_be_attribute_p (parser
))
13128 /* Parse the attributes. */
13129 tree attrs
= cp_parser_attributes_opt (parser
);
13131 /* In a sequence of declaration specifiers, c++11 attributes
13132 appertain to the type that precede them. In that case
13135 The attribute-specifier-seq affects the type only for
13136 the declaration it appears in, not other declarations
13137 involving the same type.
13139 But for now let's force the user to position the
13140 attribute either at the beginning of the declaration or
13141 after the declarator-id, which would clearly mean that it
13142 applies to the declarator. */
13143 if (cxx11_attribute_p (attrs
))
13145 if (!found_decl_spec
)
13146 /* The c++11 attribute is at the beginning of the
13147 declaration. It appertains to the entity being
13151 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13153 /* This is an attribute following a
13154 class-specifier. */
13155 if (decl_specs
->type_definition_p
)
13156 warn_misplaced_attr_for_class_type (token
->location
,
13162 decl_specs
->std_attributes
13163 = chainon (decl_specs
->std_attributes
,
13165 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13166 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13172 decl_specs
->attributes
13173 = chainon (decl_specs
->attributes
,
13175 if (decl_specs
->locations
[ds_attribute
] == 0)
13176 decl_specs
->locations
[ds_attribute
] = token
->location
;
13179 /* Assume we will find a decl-specifier keyword. */
13180 found_decl_spec
= true;
13181 /* If the next token is an appropriate keyword, we can simply
13182 add it to the list. */
13183 switch (token
->keyword
)
13189 if (!at_class_scope_p ())
13191 error_at (token
->location
, "%<friend%> used outside of class");
13192 cp_lexer_purge_token (parser
->lexer
);
13197 /* Consume the token. */
13198 cp_lexer_consume_token (parser
->lexer
);
13202 case RID_CONSTEXPR
:
13204 cp_lexer_consume_token (parser
->lexer
);
13209 cp_lexer_consume_token (parser
->lexer
);
13212 /* function-specifier:
13219 cp_parser_function_specifier_opt (parser
, decl_specs
);
13226 /* Consume the token. */
13227 cp_lexer_consume_token (parser
->lexer
);
13228 /* A constructor declarator cannot appear in a typedef. */
13229 constructor_possible_p
= false;
13230 /* The "typedef" keyword can only occur in a declaration; we
13231 may as well commit at this point. */
13232 cp_parser_commit_to_tentative_parse (parser
);
13234 if (decl_specs
->storage_class
!= sc_none
)
13235 decl_specs
->conflicting_specifiers_p
= true;
13238 /* storage-class-specifier:
13248 if (cxx_dialect
== cxx98
)
13250 /* Consume the token. */
13251 cp_lexer_consume_token (parser
->lexer
);
13253 /* Complain about `auto' as a storage specifier, if
13254 we're complaining about C++0x compatibility. */
13255 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
13256 " changes meaning in C++11; please remove it");
13258 /* Set the storage class anyway. */
13259 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13263 /* C++0x auto type-specifier. */
13264 found_decl_spec
= false;
13271 /* Consume the token. */
13272 cp_lexer_consume_token (parser
->lexer
);
13273 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13277 /* Consume the token. */
13279 cp_lexer_consume_token (parser
->lexer
);
13283 /* We did not yet find a decl-specifier yet. */
13284 found_decl_spec
= false;
13288 if (found_decl_spec
13289 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13290 && token
->keyword
!= RID_CONSTEXPR
)
13291 error ("decl-specifier invalid in condition");
13293 if (found_decl_spec
13294 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13295 && token
->keyword
!= RID_MUTABLE
13296 && token
->keyword
!= RID_CONSTEXPR
)
13297 error_at (token
->location
, "%qD invalid in lambda",
13298 ridpointers
[token
->keyword
]);
13301 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13303 /* Constructors are a special case. The `S' in `S()' is not a
13304 decl-specifier; it is the beginning of the declarator. */
13306 = (!found_decl_spec
13307 && constructor_possible_p
13308 && (cp_parser_constructor_declarator_p
13309 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13311 /* If we don't have a DECL_SPEC yet, then we must be looking at
13312 a type-specifier. */
13313 if (!found_decl_spec
&& !constructor_p
)
13315 int decl_spec_declares_class_or_enum
;
13316 bool is_cv_qualifier
;
13320 = cp_parser_type_specifier (parser
, flags
,
13322 /*is_declaration=*/true,
13323 &decl_spec_declares_class_or_enum
,
13325 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13327 /* If this type-specifier referenced a user-defined type
13328 (a typedef, class-name, etc.), then we can't allow any
13329 more such type-specifiers henceforth.
13333 The longest sequence of decl-specifiers that could
13334 possibly be a type name is taken as the
13335 decl-specifier-seq of a declaration. The sequence shall
13336 be self-consistent as described below.
13340 As a general rule, at most one type-specifier is allowed
13341 in the complete decl-specifier-seq of a declaration. The
13342 only exceptions are the following:
13344 -- const or volatile can be combined with any other
13347 -- signed or unsigned can be combined with char, long,
13355 void g (const int Pc);
13357 Here, Pc is *not* part of the decl-specifier seq; it's
13358 the declarator. Therefore, once we see a type-specifier
13359 (other than a cv-qualifier), we forbid any additional
13360 user-defined types. We *do* still allow things like `int
13361 int' to be considered a decl-specifier-seq, and issue the
13362 error message later. */
13363 if (type_spec
&& !is_cv_qualifier
)
13364 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13365 /* A constructor declarator cannot follow a type-specifier. */
13368 constructor_possible_p
= false;
13369 found_decl_spec
= true;
13370 if (!is_cv_qualifier
)
13371 decl_specs
->any_type_specifiers_p
= true;
13375 /* If we still do not have a DECL_SPEC, then there are no more
13376 decl-specifiers. */
13377 if (!found_decl_spec
)
13380 decl_specs
->any_specifiers_p
= true;
13381 /* After we see one decl-specifier, further decl-specifiers are
13382 always optional. */
13383 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13386 /* Don't allow a friend specifier with a class definition. */
13387 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13388 && (*declares_class_or_enum
& 2))
13389 error_at (decl_specs
->locations
[ds_friend
],
13390 "class definition may not be declared a friend");
13393 /* Parse an (optional) storage-class-specifier.
13395 storage-class-specifier:
13404 storage-class-specifier:
13407 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13410 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13412 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13415 if (cxx_dialect
!= cxx98
)
13417 /* Fall through for C++98. */
13418 gcc_fallthrough ();
13425 /* Consume the token. */
13426 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13433 /* Parse an (optional) function-specifier.
13435 function-specifier:
13440 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13441 Updates DECL_SPECS, if it is non-NULL. */
13444 cp_parser_function_specifier_opt (cp_parser
* parser
,
13445 cp_decl_specifier_seq
*decl_specs
)
13447 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13448 switch (token
->keyword
)
13451 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13455 /* 14.5.2.3 [temp.mem]
13457 A member function template shall not be virtual. */
13458 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13459 && current_class_type
)
13460 error_at (token
->location
, "templates may not be %<virtual%>");
13462 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13466 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13473 /* Consume the token. */
13474 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13477 /* Parse a linkage-specification.
13479 linkage-specification:
13480 extern string-literal { declaration-seq [opt] }
13481 extern string-literal declaration */
13484 cp_parser_linkage_specification (cp_parser
* parser
)
13488 /* Look for the `extern' keyword. */
13489 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13491 /* Look for the string-literal. */
13492 linkage
= cp_parser_string_literal (parser
, false, false);
13494 /* Transform the literal into an identifier. If the literal is a
13495 wide-character string, or contains embedded NULs, then we can't
13496 handle it as the user wants. */
13497 if (strlen (TREE_STRING_POINTER (linkage
))
13498 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13500 cp_parser_error (parser
, "invalid linkage-specification");
13501 /* Assume C++ linkage. */
13502 linkage
= lang_name_cplusplus
;
13505 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13507 /* We're now using the new linkage. */
13508 push_lang_context (linkage
);
13510 /* If the next token is a `{', then we're using the first
13512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13514 cp_ensure_no_omp_declare_simd (parser
);
13515 cp_ensure_no_oacc_routine (parser
);
13517 /* Consume the `{' token. */
13518 cp_lexer_consume_token (parser
->lexer
);
13519 /* Parse the declarations. */
13520 cp_parser_declaration_seq_opt (parser
);
13521 /* Look for the closing `}'. */
13522 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13524 /* Otherwise, there's just one declaration. */
13527 bool saved_in_unbraced_linkage_specification_p
;
13529 saved_in_unbraced_linkage_specification_p
13530 = parser
->in_unbraced_linkage_specification_p
;
13531 parser
->in_unbraced_linkage_specification_p
= true;
13532 cp_parser_declaration (parser
);
13533 parser
->in_unbraced_linkage_specification_p
13534 = saved_in_unbraced_linkage_specification_p
;
13537 /* We're done with the linkage-specification. */
13538 pop_lang_context ();
13541 /* Parse a static_assert-declaration.
13543 static_assert-declaration:
13544 static_assert ( constant-expression , string-literal ) ;
13545 static_assert ( constant-expression ) ; (C++1Z)
13547 If MEMBER_P, this static_assert is a class member. */
13550 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13555 location_t saved_loc
;
13558 /* Peek at the `static_assert' token so we can keep track of exactly
13559 where the static assertion started. */
13560 token
= cp_lexer_peek_token (parser
->lexer
);
13561 saved_loc
= token
->location
;
13563 /* Look for the `static_assert' keyword. */
13564 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13568 /* We know we are in a static assertion; commit to any tentative
13570 if (cp_parser_parsing_tentatively (parser
))
13571 cp_parser_commit_to_tentative_parse (parser
);
13573 /* Parse the `(' starting the static assertion condition. */
13574 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
13576 /* Parse the constant-expression. Allow a non-constant expression
13577 here in order to give better diagnostics in finish_static_assert. */
13579 cp_parser_constant_expression (parser
,
13580 /*allow_non_constant_p=*/true,
13581 /*non_constant_p=*/&dummy
);
13583 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13585 if (cxx_dialect
< cxx1z
)
13586 pedwarn (input_location
, OPT_Wpedantic
,
13587 "static_assert without a message "
13588 "only available with -std=c++1z or -std=gnu++1z");
13590 cp_lexer_consume_token (parser
->lexer
);
13591 message
= build_string (1, "");
13592 TREE_TYPE (message
) = char_array_type_node
;
13593 fix_string_type (message
);
13597 /* Parse the separating `,'. */
13598 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13600 /* Parse the string-literal message. */
13601 message
= cp_parser_string_literal (parser
,
13602 /*translate=*/false,
13605 /* A `)' completes the static assertion. */
13606 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13607 cp_parser_skip_to_closing_parenthesis (parser
,
13608 /*recovering=*/true,
13609 /*or_comma=*/false,
13610 /*consume_paren=*/true);
13613 /* A semicolon terminates the declaration. */
13614 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13616 /* Complete the static assertion, which may mean either processing
13617 the static assert now or saving it for template instantiation. */
13618 finish_static_assert (condition
, message
, saved_loc
, member_p
);
13621 /* Parse the expression in decltype ( expression ). */
13624 cp_parser_decltype_expr (cp_parser
*parser
,
13625 bool &id_expression_or_member_access_p
)
13627 cp_token
*id_expr_start_token
;
13630 /* Since we're going to preserve any side-effects from this parse, set up a
13631 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13632 in the expression. */
13633 tentative_firewall
firewall (parser
);
13635 /* First, try parsing an id-expression. */
13636 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13637 cp_parser_parse_tentatively (parser
);
13638 expr
= cp_parser_id_expression (parser
,
13639 /*template_keyword_p=*/false,
13640 /*check_dependency_p=*/true,
13641 /*template_p=*/NULL
,
13642 /*declarator_p=*/false,
13643 /*optional_p=*/false);
13645 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13647 bool non_integral_constant_expression_p
= false;
13648 tree id_expression
= expr
;
13650 const char *error_msg
;
13652 if (identifier_p (expr
))
13653 /* Lookup the name we got back from the id-expression. */
13654 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13655 id_expr_start_token
->location
);
13658 && expr
!= error_mark_node
13659 && TREE_CODE (expr
) != TYPE_DECL
13660 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13661 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13662 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13664 /* Complete lookup of the id-expression. */
13665 expr
= (finish_id_expression
13666 (id_expression
, expr
, parser
->scope
, &idk
,
13667 /*integral_constant_expression_p=*/false,
13668 /*allow_non_integral_constant_expression_p=*/true,
13669 &non_integral_constant_expression_p
,
13670 /*template_p=*/false,
13672 /*address_p=*/false,
13673 /*template_arg_p=*/false,
13675 id_expr_start_token
->location
));
13677 if (expr
== error_mark_node
)
13678 /* We found an id-expression, but it was something that we
13679 should not have found. This is an error, not something
13680 we can recover from, so note that we found an
13681 id-expression and we'll recover as gracefully as
13683 id_expression_or_member_access_p
= true;
13687 && expr
!= error_mark_node
13688 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13689 /* We have an id-expression. */
13690 id_expression_or_member_access_p
= true;
13693 if (!id_expression_or_member_access_p
)
13695 /* Abort the id-expression parse. */
13696 cp_parser_abort_tentative_parse (parser
);
13698 /* Parsing tentatively, again. */
13699 cp_parser_parse_tentatively (parser
);
13701 /* Parse a class member access. */
13702 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13703 /*cast_p=*/false, /*decltype*/true,
13704 /*member_access_only_p=*/true, NULL
);
13707 && expr
!= error_mark_node
13708 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13709 /* We have an id-expression. */
13710 id_expression_or_member_access_p
= true;
13713 if (id_expression_or_member_access_p
)
13714 /* We have parsed the complete id-expression or member access. */
13715 cp_parser_parse_definitely (parser
);
13718 /* Abort our attempt to parse an id-expression or member access
13720 cp_parser_abort_tentative_parse (parser
);
13722 /* Parse a full expression. */
13723 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13724 /*decltype_p=*/true);
13730 /* Parse a `decltype' type. Returns the type.
13732 simple-type-specifier:
13733 decltype ( expression )
13735 decltype ( auto ) */
13738 cp_parser_decltype (cp_parser
*parser
)
13741 bool id_expression_or_member_access_p
= false;
13742 const char *saved_message
;
13743 bool saved_integral_constant_expression_p
;
13744 bool saved_non_integral_constant_expression_p
;
13745 bool saved_greater_than_is_operator_p
;
13746 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13748 if (start_token
->type
== CPP_DECLTYPE
)
13750 /* Already parsed. */
13751 cp_lexer_consume_token (parser
->lexer
);
13752 return saved_checks_value (start_token
->u
.tree_check_value
);
13755 /* Look for the `decltype' token. */
13756 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
13757 return error_mark_node
;
13759 /* Parse the opening `('. */
13760 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13761 return error_mark_node
;
13763 /* decltype (auto) */
13764 if (cxx_dialect
>= cxx14
13765 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
13767 cp_lexer_consume_token (parser
->lexer
);
13768 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13769 return error_mark_node
;
13770 expr
= make_decltype_auto ();
13771 AUTO_IS_DECLTYPE (expr
) = true;
13775 /* Types cannot be defined in a `decltype' expression. Save away the
13777 saved_message
= parser
->type_definition_forbidden_message
;
13779 /* And create the new one. */
13780 parser
->type_definition_forbidden_message
13781 = G_("types may not be defined in %<decltype%> expressions");
13783 /* The restrictions on constant-expressions do not apply inside
13784 decltype expressions. */
13785 saved_integral_constant_expression_p
13786 = parser
->integral_constant_expression_p
;
13787 saved_non_integral_constant_expression_p
13788 = parser
->non_integral_constant_expression_p
;
13789 parser
->integral_constant_expression_p
= false;
13791 /* Within a parenthesized expression, a `>' token is always
13792 the greater-than operator. */
13793 saved_greater_than_is_operator_p
13794 = parser
->greater_than_is_operator_p
;
13795 parser
->greater_than_is_operator_p
= true;
13797 /* Do not actually evaluate the expression. */
13798 ++cp_unevaluated_operand
;
13800 /* Do not warn about problems with the expression. */
13801 ++c_inhibit_evaluation_warnings
;
13803 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
13805 /* Go back to evaluating expressions. */
13806 --cp_unevaluated_operand
;
13807 --c_inhibit_evaluation_warnings
;
13809 /* The `>' token might be the end of a template-id or
13810 template-parameter-list now. */
13811 parser
->greater_than_is_operator_p
13812 = saved_greater_than_is_operator_p
;
13814 /* Restore the old message and the integral constant expression
13816 parser
->type_definition_forbidden_message
= saved_message
;
13817 parser
->integral_constant_expression_p
13818 = saved_integral_constant_expression_p
;
13819 parser
->non_integral_constant_expression_p
13820 = saved_non_integral_constant_expression_p
;
13822 /* Parse to the closing `)'. */
13823 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13825 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13826 /*consume_paren=*/true);
13827 return error_mark_node
;
13830 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
13831 tf_warning_or_error
);
13834 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13836 start_token
->type
= CPP_DECLTYPE
;
13837 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13838 start_token
->u
.tree_check_value
->value
= expr
;
13839 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13840 start_token
->keyword
= RID_MAX
;
13841 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
13846 /* Special member functions [gram.special] */
13848 /* Parse a conversion-function-id.
13850 conversion-function-id:
13851 operator conversion-type-id
13853 Returns an IDENTIFIER_NODE representing the operator. */
13856 cp_parser_conversion_function_id (cp_parser
* parser
)
13860 tree saved_qualifying_scope
;
13861 tree saved_object_scope
;
13862 tree pushed_scope
= NULL_TREE
;
13864 /* Look for the `operator' token. */
13865 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13866 return error_mark_node
;
13867 /* When we parse the conversion-type-id, the current scope will be
13868 reset. However, we need that information in able to look up the
13869 conversion function later, so we save it here. */
13870 saved_scope
= parser
->scope
;
13871 saved_qualifying_scope
= parser
->qualifying_scope
;
13872 saved_object_scope
= parser
->object_scope
;
13873 /* We must enter the scope of the class so that the names of
13874 entities declared within the class are available in the
13875 conversion-type-id. For example, consider:
13882 S::operator I() { ... }
13884 In order to see that `I' is a type-name in the definition, we
13885 must be in the scope of `S'. */
13887 pushed_scope
= push_scope (saved_scope
);
13888 /* Parse the conversion-type-id. */
13889 type
= cp_parser_conversion_type_id (parser
);
13890 /* Leave the scope of the class, if any. */
13892 pop_scope (pushed_scope
);
13893 /* Restore the saved scope. */
13894 parser
->scope
= saved_scope
;
13895 parser
->qualifying_scope
= saved_qualifying_scope
;
13896 parser
->object_scope
= saved_object_scope
;
13897 /* If the TYPE is invalid, indicate failure. */
13898 if (type
== error_mark_node
)
13899 return error_mark_node
;
13900 return mangle_conv_op_name_for_type (type
);
13903 /* Parse a conversion-type-id:
13905 conversion-type-id:
13906 type-specifier-seq conversion-declarator [opt]
13908 Returns the TYPE specified. */
13911 cp_parser_conversion_type_id (cp_parser
* parser
)
13914 cp_decl_specifier_seq type_specifiers
;
13915 cp_declarator
*declarator
;
13916 tree type_specified
;
13917 const char *saved_message
;
13919 /* Parse the attributes. */
13920 attributes
= cp_parser_attributes_opt (parser
);
13922 saved_message
= parser
->type_definition_forbidden_message
;
13923 parser
->type_definition_forbidden_message
13924 = G_("types may not be defined in a conversion-type-id");
13926 /* Parse the type-specifiers. */
13927 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13928 /*is_trailing_return=*/false,
13931 parser
->type_definition_forbidden_message
= saved_message
;
13933 /* If that didn't work, stop. */
13934 if (type_specifiers
.type
== error_mark_node
)
13935 return error_mark_node
;
13936 /* Parse the conversion-declarator. */
13937 declarator
= cp_parser_conversion_declarator_opt (parser
);
13939 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
13940 /*initialized=*/0, &attributes
);
13942 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
13944 /* Don't give this error when parsing tentatively. This happens to
13945 work because we always parse this definitively once. */
13946 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
13947 && type_uses_auto (type_specified
))
13949 if (cxx_dialect
< cxx14
)
13951 error ("invalid use of %<auto%> in conversion operator");
13952 return error_mark_node
;
13954 else if (template_parm_scope_p ())
13955 warning (0, "use of %<auto%> in member template "
13956 "conversion operator can never be deduced");
13959 return type_specified
;
13962 /* Parse an (optional) conversion-declarator.
13964 conversion-declarator:
13965 ptr-operator conversion-declarator [opt]
13969 static cp_declarator
*
13970 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
13972 enum tree_code code
;
13973 tree class_type
, std_attributes
= NULL_TREE
;
13974 cp_cv_quals cv_quals
;
13976 /* We don't know if there's a ptr-operator next, or not. */
13977 cp_parser_parse_tentatively (parser
);
13978 /* Try the ptr-operator. */
13979 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
13981 /* If it worked, look for more conversion-declarators. */
13982 if (cp_parser_parse_definitely (parser
))
13984 cp_declarator
*declarator
;
13986 /* Parse another optional declarator. */
13987 declarator
= cp_parser_conversion_declarator_opt (parser
);
13989 declarator
= cp_parser_make_indirect_declarator
13990 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
13998 /* Parse an (optional) ctor-initializer.
14001 : mem-initializer-list
14003 Returns TRUE iff the ctor-initializer was actually present. */
14006 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14008 /* If the next token is not a `:', then there is no
14009 ctor-initializer. */
14010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14012 /* Do default initialization of any bases and members. */
14013 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14014 finish_mem_initializers (NULL_TREE
);
14019 /* Consume the `:' token. */
14020 cp_lexer_consume_token (parser
->lexer
);
14021 /* And the mem-initializer-list. */
14022 cp_parser_mem_initializer_list (parser
);
14027 /* Parse a mem-initializer-list.
14029 mem-initializer-list:
14030 mem-initializer ... [opt]
14031 mem-initializer ... [opt] , mem-initializer-list */
14034 cp_parser_mem_initializer_list (cp_parser
* parser
)
14036 tree mem_initializer_list
= NULL_TREE
;
14037 tree target_ctor
= error_mark_node
;
14038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14040 /* Let the semantic analysis code know that we are starting the
14041 mem-initializer-list. */
14042 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14043 error_at (token
->location
,
14044 "only constructors take member initializers");
14046 /* Loop through the list. */
14049 tree mem_initializer
;
14051 token
= cp_lexer_peek_token (parser
->lexer
);
14052 /* Parse the mem-initializer. */
14053 mem_initializer
= cp_parser_mem_initializer (parser
);
14054 /* If the next token is a `...', we're expanding member initializers. */
14055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14057 /* Consume the `...'. */
14058 cp_lexer_consume_token (parser
->lexer
);
14060 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14061 can be expanded but members cannot. */
14062 if (mem_initializer
!= error_mark_node
14063 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14065 error_at (token
->location
,
14066 "cannot expand initializer for member %<%D%>",
14067 TREE_PURPOSE (mem_initializer
));
14068 mem_initializer
= error_mark_node
;
14071 /* Construct the pack expansion type. */
14072 if (mem_initializer
!= error_mark_node
)
14073 mem_initializer
= make_pack_expansion (mem_initializer
);
14075 if (target_ctor
!= error_mark_node
14076 && mem_initializer
!= error_mark_node
)
14078 error ("mem-initializer for %qD follows constructor delegation",
14079 TREE_PURPOSE (mem_initializer
));
14080 mem_initializer
= error_mark_node
;
14082 /* Look for a target constructor. */
14083 if (mem_initializer
!= error_mark_node
14084 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14085 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14087 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14088 if (mem_initializer_list
)
14090 error ("constructor delegation follows mem-initializer for %qD",
14091 TREE_PURPOSE (mem_initializer_list
));
14092 mem_initializer
= error_mark_node
;
14094 target_ctor
= mem_initializer
;
14096 /* Add it to the list, unless it was erroneous. */
14097 if (mem_initializer
!= error_mark_node
)
14099 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14100 mem_initializer_list
= mem_initializer
;
14102 /* If the next token is not a `,', we're done. */
14103 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14105 /* Consume the `,' token. */
14106 cp_lexer_consume_token (parser
->lexer
);
14109 /* Perform semantic analysis. */
14110 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14111 finish_mem_initializers (mem_initializer_list
);
14114 /* Parse a mem-initializer.
14117 mem-initializer-id ( expression-list [opt] )
14118 mem-initializer-id braced-init-list
14123 ( expression-list [opt] )
14125 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14126 class) or FIELD_DECL (for a non-static data member) to initialize;
14127 the TREE_VALUE is the expression-list. An empty initialization
14128 list is represented by void_list_node. */
14131 cp_parser_mem_initializer (cp_parser
* parser
)
14133 tree mem_initializer_id
;
14134 tree expression_list
;
14136 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14138 /* Find out what is being initialized. */
14139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14141 permerror (token
->location
,
14142 "anachronistic old-style base class initializer");
14143 mem_initializer_id
= NULL_TREE
;
14147 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14148 if (mem_initializer_id
== error_mark_node
)
14149 return mem_initializer_id
;
14151 member
= expand_member_init (mem_initializer_id
);
14152 if (member
&& !DECL_P (member
))
14153 in_base_initializer
= 1;
14155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14157 bool expr_non_constant_p
;
14158 cp_lexer_set_source_position (parser
->lexer
);
14159 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14160 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14161 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14162 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14166 vec
<tree
, va_gc
> *vec
;
14167 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14169 /*allow_expansion_p=*/true,
14170 /*non_constant_p=*/NULL
);
14172 return error_mark_node
;
14173 expression_list
= build_tree_list_vec (vec
);
14174 release_tree_vector (vec
);
14177 if (expression_list
== error_mark_node
)
14178 return error_mark_node
;
14179 if (!expression_list
)
14180 expression_list
= void_type_node
;
14182 in_base_initializer
= 0;
14184 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14187 /* Parse a mem-initializer-id.
14189 mem-initializer-id:
14190 :: [opt] nested-name-specifier [opt] class-name
14191 decltype-specifier (C++11)
14194 Returns a TYPE indicating the class to be initialized for the first
14195 production (and the second in C++11). Returns an IDENTIFIER_NODE
14196 indicating the data member to be initialized for the last production. */
14199 cp_parser_mem_initializer_id (cp_parser
* parser
)
14201 bool global_scope_p
;
14202 bool nested_name_specifier_p
;
14203 bool template_p
= false;
14206 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14208 /* `typename' is not allowed in this context ([temp.res]). */
14209 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14211 error_at (token
->location
,
14212 "keyword %<typename%> not allowed in this context (a qualified "
14213 "member initializer is implicitly a type)");
14214 cp_lexer_consume_token (parser
->lexer
);
14216 /* Look for the optional `::' operator. */
14218 = (cp_parser_global_scope_opt (parser
,
14219 /*current_scope_valid_p=*/false)
14221 /* Look for the optional nested-name-specifier. The simplest way to
14226 The keyword `typename' is not permitted in a base-specifier or
14227 mem-initializer; in these contexts a qualified name that
14228 depends on a template-parameter is implicitly assumed to be a
14231 is to assume that we have seen the `typename' keyword at this
14233 nested_name_specifier_p
14234 = (cp_parser_nested_name_specifier_opt (parser
,
14235 /*typename_keyword_p=*/true,
14236 /*check_dependency_p=*/true,
14238 /*is_declaration=*/true)
14240 if (nested_name_specifier_p
)
14241 template_p
= cp_parser_optional_template_keyword (parser
);
14242 /* If there is a `::' operator or a nested-name-specifier, then we
14243 are definitely looking for a class-name. */
14244 if (global_scope_p
|| nested_name_specifier_p
)
14245 return cp_parser_class_name (parser
,
14246 /*typename_keyword_p=*/true,
14247 /*template_keyword_p=*/template_p
,
14249 /*check_dependency_p=*/true,
14250 /*class_head_p=*/false,
14251 /*is_declaration=*/true);
14252 /* Otherwise, we could also be looking for an ordinary identifier. */
14253 cp_parser_parse_tentatively (parser
);
14254 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14255 /* Try a decltype-specifier. */
14256 id
= cp_parser_decltype (parser
);
14258 /* Otherwise, try a class-name. */
14259 id
= cp_parser_class_name (parser
,
14260 /*typename_keyword_p=*/true,
14261 /*template_keyword_p=*/false,
14263 /*check_dependency_p=*/true,
14264 /*class_head_p=*/false,
14265 /*is_declaration=*/true);
14266 /* If we found one, we're done. */
14267 if (cp_parser_parse_definitely (parser
))
14269 /* Otherwise, look for an ordinary identifier. */
14270 return cp_parser_identifier (parser
);
14273 /* Overloading [gram.over] */
14275 /* Parse an operator-function-id.
14277 operator-function-id:
14280 Returns an IDENTIFIER_NODE for the operator which is a
14281 human-readable spelling of the identifier, e.g., `operator +'. */
14284 cp_parser_operator_function_id (cp_parser
* parser
)
14286 /* Look for the `operator' keyword. */
14287 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14288 return error_mark_node
;
14289 /* And then the name of the operator itself. */
14290 return cp_parser_operator (parser
);
14293 /* Return an identifier node for a user-defined literal operator.
14294 The suffix identifier is chained to the operator name identifier. */
14297 cp_literal_operator_id (const char* name
)
14300 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14301 + strlen (name
) + 10);
14302 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14303 identifier
= get_identifier (buffer
);
14308 /* Parse an operator.
14311 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14312 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14313 || ++ -- , ->* -> () []
14320 Returns an IDENTIFIER_NODE for the operator which is a
14321 human-readable spelling of the identifier, e.g., `operator +'. */
14324 cp_parser_operator (cp_parser
* parser
)
14326 tree id
= NULL_TREE
;
14330 /* Peek at the next token. */
14331 token
= cp_lexer_peek_token (parser
->lexer
);
14333 location_t start_loc
= token
->location
;
14335 /* Figure out which operator we have. */
14336 switch (token
->type
)
14342 /* The keyword should be either `new' or `delete'. */
14343 if (token
->keyword
== RID_NEW
)
14345 else if (token
->keyword
== RID_DELETE
)
14350 /* Consume the `new' or `delete' token. */
14351 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14353 /* Peek at the next token. */
14354 token
= cp_lexer_peek_token (parser
->lexer
);
14355 /* If it's a `[' token then this is the array variant of the
14357 if (token
->type
== CPP_OPEN_SQUARE
)
14359 /* Consume the `[' token. */
14360 cp_lexer_consume_token (parser
->lexer
);
14361 /* Look for the `]' token. */
14362 if (cp_token
*close_token
14363 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14364 end_loc
= close_token
->location
;
14365 id
= ansi_opname (op
== NEW_EXPR
14366 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
14368 /* Otherwise, we have the non-array variant. */
14370 id
= ansi_opname (op
);
14372 location_t loc
= make_location (start_loc
, start_loc
, end_loc
);
14374 return cp_expr (id
, loc
);
14378 id
= ansi_opname (PLUS_EXPR
);
14382 id
= ansi_opname (MINUS_EXPR
);
14386 id
= ansi_opname (MULT_EXPR
);
14390 id
= ansi_opname (TRUNC_DIV_EXPR
);
14394 id
= ansi_opname (TRUNC_MOD_EXPR
);
14398 id
= ansi_opname (BIT_XOR_EXPR
);
14402 id
= ansi_opname (BIT_AND_EXPR
);
14406 id
= ansi_opname (BIT_IOR_EXPR
);
14410 id
= ansi_opname (BIT_NOT_EXPR
);
14414 id
= ansi_opname (TRUTH_NOT_EXPR
);
14418 id
= ansi_assopname (NOP_EXPR
);
14422 id
= ansi_opname (LT_EXPR
);
14426 id
= ansi_opname (GT_EXPR
);
14430 id
= ansi_assopname (PLUS_EXPR
);
14434 id
= ansi_assopname (MINUS_EXPR
);
14438 id
= ansi_assopname (MULT_EXPR
);
14442 id
= ansi_assopname (TRUNC_DIV_EXPR
);
14446 id
= ansi_assopname (TRUNC_MOD_EXPR
);
14450 id
= ansi_assopname (BIT_XOR_EXPR
);
14454 id
= ansi_assopname (BIT_AND_EXPR
);
14458 id
= ansi_assopname (BIT_IOR_EXPR
);
14462 id
= ansi_opname (LSHIFT_EXPR
);
14466 id
= ansi_opname (RSHIFT_EXPR
);
14469 case CPP_LSHIFT_EQ
:
14470 id
= ansi_assopname (LSHIFT_EXPR
);
14473 case CPP_RSHIFT_EQ
:
14474 id
= ansi_assopname (RSHIFT_EXPR
);
14478 id
= ansi_opname (EQ_EXPR
);
14482 id
= ansi_opname (NE_EXPR
);
14486 id
= ansi_opname (LE_EXPR
);
14489 case CPP_GREATER_EQ
:
14490 id
= ansi_opname (GE_EXPR
);
14494 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
14498 id
= ansi_opname (TRUTH_ORIF_EXPR
);
14501 case CPP_PLUS_PLUS
:
14502 id
= ansi_opname (POSTINCREMENT_EXPR
);
14505 case CPP_MINUS_MINUS
:
14506 id
= ansi_opname (PREDECREMENT_EXPR
);
14510 id
= ansi_opname (COMPOUND_EXPR
);
14513 case CPP_DEREF_STAR
:
14514 id
= ansi_opname (MEMBER_REF
);
14518 id
= ansi_opname (COMPONENT_REF
);
14521 case CPP_OPEN_PAREN
:
14522 /* Consume the `('. */
14523 cp_lexer_consume_token (parser
->lexer
);
14524 /* Look for the matching `)'. */
14525 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14526 return ansi_opname (CALL_EXPR
);
14528 case CPP_OPEN_SQUARE
:
14529 /* Consume the `['. */
14530 cp_lexer_consume_token (parser
->lexer
);
14531 /* Look for the matching `]'. */
14532 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14533 return ansi_opname (ARRAY_REF
);
14535 case CPP_UTF8STRING
:
14536 case CPP_UTF8STRING_USERDEF
:
14543 case CPP_STRING_USERDEF
:
14544 case CPP_WSTRING_USERDEF
:
14545 case CPP_STRING16_USERDEF
:
14546 case CPP_STRING32_USERDEF
:
14548 tree str
, string_tree
;
14551 if (cxx_dialect
== cxx98
)
14552 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14554 /* Consume the string. */
14555 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14556 /*wide_ok=*/true, /*lookup_udlit=*/false);
14557 if (str
== error_mark_node
)
14558 return error_mark_node
;
14559 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14561 string_tree
= USERDEF_LITERAL_VALUE (str
);
14562 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14567 /* Look for the suffix identifier. */
14568 token
= cp_lexer_peek_token (parser
->lexer
);
14569 if (token
->type
== CPP_NAME
)
14570 id
= cp_parser_identifier (parser
);
14571 else if (token
->type
== CPP_KEYWORD
)
14573 error ("unexpected keyword;"
14574 " remove space between quotes and suffix identifier");
14575 return error_mark_node
;
14579 error ("expected suffix identifier");
14580 return error_mark_node
;
14583 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14584 (TREE_TYPE (TREE_TYPE (string_tree
))));
14585 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14588 error ("expected empty string after %<operator%> keyword");
14589 return error_mark_node
;
14591 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14594 error ("invalid encoding prefix in literal operator");
14595 return error_mark_node
;
14597 if (id
!= error_mark_node
)
14599 const char *name
= IDENTIFIER_POINTER (id
);
14600 id
= cp_literal_operator_id (name
);
14606 /* Anything else is an error. */
14610 /* If we have selected an identifier, we need to consume the
14613 cp_lexer_consume_token (parser
->lexer
);
14614 /* Otherwise, no valid operator name was present. */
14617 cp_parser_error (parser
, "expected operator");
14618 id
= error_mark_node
;
14621 return cp_expr (id
, start_loc
);
14624 /* Parse a template-declaration.
14626 template-declaration:
14627 export [opt] template < template-parameter-list > declaration
14629 If MEMBER_P is TRUE, this template-declaration occurs within a
14632 The grammar rule given by the standard isn't correct. What
14633 is really meant is:
14635 template-declaration:
14636 export [opt] template-parameter-list-seq
14637 decl-specifier-seq [opt] init-declarator [opt] ;
14638 export [opt] template-parameter-list-seq
14639 function-definition
14641 template-parameter-list-seq:
14642 template-parameter-list-seq [opt]
14643 template < template-parameter-list >
14645 Concept Extensions:
14647 template-parameter-list-seq:
14648 template < template-parameter-list > requires-clause [opt]
14651 requires logical-or-expression */
14654 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14656 /* Check for `export'. */
14657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14659 /* Consume the `export' token. */
14660 cp_lexer_consume_token (parser
->lexer
);
14661 /* Warn that we do not support `export'. */
14662 warning (0, "keyword %<export%> not implemented, and will be ignored");
14665 cp_parser_template_declaration_after_export (parser
, member_p
);
14668 /* Parse a template-parameter-list.
14670 template-parameter-list:
14672 template-parameter-list , template-parameter
14674 Returns a TREE_LIST. Each node represents a template parameter.
14675 The nodes are connected via their TREE_CHAINs. */
14678 cp_parser_template_parameter_list (cp_parser
* parser
)
14680 tree parameter_list
= NULL_TREE
;
14682 begin_template_parm_list ();
14684 /* The loop below parses the template parms. We first need to know
14685 the total number of template parms to be able to compute proper
14686 canonical types of each dependent type. So after the loop, when
14687 we know the total number of template parms,
14688 end_template_parm_list computes the proper canonical types and
14689 fixes up the dependent types accordingly. */
14694 bool is_parameter_pack
;
14695 location_t parm_loc
;
14697 /* Parse the template-parameter. */
14698 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14699 parameter
= cp_parser_template_parameter (parser
,
14701 &is_parameter_pack
);
14702 /* Add it to the list. */
14703 if (parameter
!= error_mark_node
)
14704 parameter_list
= process_template_parm (parameter_list
,
14708 is_parameter_pack
);
14711 tree err_parm
= build_tree_list (parameter
, parameter
);
14712 parameter_list
= chainon (parameter_list
, err_parm
);
14715 /* If the next token is not a `,', we're done. */
14716 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14718 /* Otherwise, consume the `,' token. */
14719 cp_lexer_consume_token (parser
->lexer
);
14722 return end_template_parm_list (parameter_list
);
14725 /* Parse a introduction-list.
14728 introduced-parameter
14729 introduction-list , introduced-parameter
14731 introduced-parameter:
14732 ...[opt] identifier
14734 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14735 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14736 WILDCARD_DECL will also have DECL_NAME set and token location in
14737 DECL_SOURCE_LOCATION. */
14740 cp_parser_introduction_list (cp_parser
*parser
)
14742 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
14746 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14748 cp_lexer_consume_token (parser
->lexer
);
14750 /* Build placeholder. */
14751 tree parm
= build_nt (WILDCARD_DECL
);
14752 DECL_SOURCE_LOCATION (parm
)
14753 = cp_lexer_peek_token (parser
->lexer
)->location
;
14754 DECL_NAME (parm
) = cp_parser_identifier (parser
);
14755 WILDCARD_PACK_P (parm
) = is_pack
;
14756 vec_safe_push (introduction_vec
, parm
);
14758 /* If the next token is not a `,', we're done. */
14759 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14761 /* Otherwise, consume the `,' token. */
14762 cp_lexer_consume_token (parser
->lexer
);
14765 /* Convert the vec into a TREE_VEC. */
14766 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
14769 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
14770 TREE_VEC_ELT (introduction_list
, n
) = parm
;
14772 release_tree_vector (introduction_vec
);
14773 return introduction_list
;
14776 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14777 is an abstract declarator. */
14779 static inline cp_declarator
*
14780 get_id_declarator (cp_declarator
*declarator
)
14782 cp_declarator
*d
= declarator
;
14783 while (d
&& d
->kind
!= cdk_id
)
14788 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14789 is an abstract declarator. */
14792 get_unqualified_id (cp_declarator
*declarator
)
14794 declarator
= get_id_declarator (declarator
);
14796 return declarator
->u
.id
.unqualified_name
;
14801 /* Returns true if DECL represents a constrained-parameter. */
14804 is_constrained_parameter (tree decl
)
14807 && TREE_CODE (decl
) == TYPE_DECL
14808 && CONSTRAINED_PARM_CONCEPT (decl
)
14809 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
14812 /* Returns true if PARM declares a constrained-parameter. */
14815 is_constrained_parameter (cp_parameter_declarator
*parm
)
14817 return is_constrained_parameter (parm
->decl_specifiers
.type
);
14820 /* Check that the type parameter is only a declarator-id, and that its
14821 type is not cv-qualified. */
14824 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
14825 cp_parameter_declarator
*parm
)
14827 if (!parm
->declarator
)
14830 if (parm
->declarator
->kind
!= cdk_id
)
14832 cp_parser_error (parser
, "invalid constrained type parameter");
14836 /* Don't allow cv-qualified type parameters. */
14837 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
14838 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
14840 cp_parser_error (parser
, "cv-qualified type parameter");
14847 /* Finish parsing/processing a template type parameter and checking
14848 various restrictions. */
14851 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
14853 cp_parameter_declarator
* parmdecl
)
14855 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14856 return finish_template_type_parm (class_type_node
, id
);
14858 return error_mark_node
;
14862 finish_constrained_template_template_parm (tree proto
, tree id
)
14864 /* FIXME: This should probably be copied, and we may need to adjust
14865 the template parameter depths. */
14866 tree saved_parms
= current_template_parms
;
14867 begin_template_parm_list ();
14868 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
14869 end_template_parm_list ();
14871 tree parm
= finish_template_template_parm (class_type_node
, id
);
14872 current_template_parms
= saved_parms
;
14877 /* Finish parsing/processing a template template parameter by borrowing
14878 the template parameter list from the prototype parameter. */
14881 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
14884 cp_parameter_declarator
*parmdecl
)
14886 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14887 return error_mark_node
;
14888 return finish_constrained_template_template_parm (proto
, id
);
14891 /* Create a new non-type template parameter from the given PARM
14895 constrained_non_type_template_parm (bool *is_non_type
,
14896 cp_parameter_declarator
*parm
)
14898 *is_non_type
= true;
14899 cp_declarator
*decl
= parm
->declarator
;
14900 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
14901 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
14902 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
14905 /* Build a constrained template parameter based on the PARMDECL
14906 declarator. The type of PARMDECL is the constrained type, which
14907 refers to the prototype template parameter that ultimately
14908 specifies the type of the declared parameter. */
14911 finish_constrained_parameter (cp_parser
*parser
,
14912 cp_parameter_declarator
*parmdecl
,
14914 bool *is_parameter_pack
)
14916 tree decl
= parmdecl
->decl_specifiers
.type
;
14917 tree id
= get_unqualified_id (parmdecl
->declarator
);
14918 tree def
= parmdecl
->default_argument
;
14919 tree proto
= DECL_INITIAL (decl
);
14921 /* A template parameter constrained by a variadic concept shall also
14922 be declared as a template parameter pack. */
14923 bool is_variadic
= template_parameter_pack_p (proto
);
14924 if (is_variadic
&& !*is_parameter_pack
)
14925 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
14927 /* Build the parameter. Return an error if the declarator was invalid. */
14929 if (TREE_CODE (proto
) == TYPE_DECL
)
14930 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
14931 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
14932 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
14935 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
14936 if (parm
== error_mark_node
)
14937 return error_mark_node
;
14939 /* Finish the parameter decl and create a node attaching the
14940 default argument and constraint. */
14941 parm
= build_tree_list (def
, parm
);
14942 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
14947 /* Returns true if the parsed type actually represents the declaration
14948 of a type template-parameter. */
14951 declares_constrained_type_template_parameter (tree type
)
14953 return (is_constrained_parameter (type
)
14954 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
14958 /* Returns true if the parsed type actually represents the declaration of
14959 a template template-parameter. */
14962 declares_constrained_template_template_parameter (tree type
)
14964 return (is_constrained_parameter (type
)
14965 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
14968 /* Parse a default argument for a type template-parameter.
14969 Note that diagnostics are handled in cp_parser_template_parameter. */
14972 cp_parser_default_type_template_argument (cp_parser
*parser
)
14974 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14976 /* Consume the `=' token. */
14977 cp_lexer_consume_token (parser
->lexer
);
14979 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14981 /* Parse the default-argument. */
14982 push_deferring_access_checks (dk_no_deferred
);
14983 tree default_argument
= cp_parser_type_id (parser
);
14984 pop_deferring_access_checks ();
14986 if (flag_concepts
&& type_uses_auto (default_argument
))
14988 error_at (token
->location
,
14989 "invalid use of %<auto%> in default template argument");
14990 return error_mark_node
;
14993 return default_argument
;
14996 /* Parse a default argument for a template template-parameter. */
14999 cp_parser_default_template_template_argument (cp_parser
*parser
)
15001 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15005 /* Consume the `='. */
15006 cp_lexer_consume_token (parser
->lexer
);
15007 /* Parse the id-expression. */
15008 push_deferring_access_checks (dk_no_deferred
);
15009 /* save token before parsing the id-expression, for error
15011 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15012 tree default_argument
15013 = cp_parser_id_expression (parser
,
15014 /*template_keyword_p=*/false,
15015 /*check_dependency_p=*/true,
15016 /*template_p=*/&is_template
,
15017 /*declarator_p=*/false,
15018 /*optional_p=*/false);
15019 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15020 /* If the id-expression was a template-id that refers to
15021 a template-class, we already have the declaration here,
15022 so no further lookup is needed. */
15025 /* Look up the name. */
15027 = cp_parser_lookup_name (parser
, default_argument
,
15029 /*is_template=*/is_template
,
15030 /*is_namespace=*/false,
15031 /*check_dependency=*/true,
15032 /*ambiguous_decls=*/NULL
,
15034 /* See if the default argument is valid. */
15035 default_argument
= check_template_template_default_arg (default_argument
);
15036 pop_deferring_access_checks ();
15037 return default_argument
;
15040 /* Parse a template-parameter.
15042 template-parameter:
15044 parameter-declaration
15046 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15047 the parameter. The TREE_PURPOSE is the default value, if any.
15048 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15049 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15050 set to true iff this parameter is a parameter pack. */
15053 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15054 bool *is_parameter_pack
)
15057 cp_parameter_declarator
*parameter_declarator
;
15060 /* Assume it is a type parameter or a template parameter. */
15061 *is_non_type
= false;
15062 /* Assume it not a parameter pack. */
15063 *is_parameter_pack
= false;
15064 /* Peek at the next token. */
15065 token
= cp_lexer_peek_token (parser
->lexer
);
15066 /* If it is `class' or `template', we have a type-parameter. */
15067 if (token
->keyword
== RID_TEMPLATE
)
15068 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15069 /* If it is `class' or `typename' we do not know yet whether it is a
15070 type parameter or a non-type parameter. Consider:
15072 template <typename T, typename T::X X> ...
15076 template <class C, class D*> ...
15078 Here, the first parameter is a type parameter, and the second is
15079 a non-type parameter. We can tell by looking at the token after
15080 the identifier -- if it is a `,', `=', or `>' then we have a type
15082 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15084 /* Peek at the token after `class' or `typename'. */
15085 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15086 /* If it's an ellipsis, we have a template type parameter
15088 if (token
->type
== CPP_ELLIPSIS
)
15089 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15090 /* If it's an identifier, skip it. */
15091 if (token
->type
== CPP_NAME
)
15092 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15093 /* Now, see if the token looks like the end of a template
15095 if (token
->type
== CPP_COMMA
15096 || token
->type
== CPP_EQ
15097 || token
->type
== CPP_GREATER
)
15098 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15101 /* Otherwise, it is a non-type parameter or a constrained parameter.
15105 When parsing a default template-argument for a non-type
15106 template-parameter, the first non-nested `>' is taken as the end
15107 of the template parameter-list rather than a greater-than
15109 parameter_declarator
15110 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15111 /*parenthesized_p=*/NULL
);
15113 if (!parameter_declarator
)
15114 return error_mark_node
;
15116 /* If the parameter declaration is marked as a parameter pack, set
15117 *IS_PARAMETER_PACK to notify the caller. */
15118 if (parameter_declarator
->template_parameter_pack_p
)
15119 *is_parameter_pack
= true;
15121 if (parameter_declarator
->default_argument
)
15123 /* Can happen in some cases of erroneous input (c++/34892). */
15124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15125 /* Consume the `...' for better error recovery. */
15126 cp_lexer_consume_token (parser
->lexer
);
15129 // The parameter may have been constrained.
15130 if (is_constrained_parameter (parameter_declarator
))
15131 return finish_constrained_parameter (parser
,
15132 parameter_declarator
,
15134 is_parameter_pack
);
15136 // Now we're sure that the parameter is a non-type parameter.
15137 *is_non_type
= true;
15139 parm
= grokdeclarator (parameter_declarator
->declarator
,
15140 ¶meter_declarator
->decl_specifiers
,
15141 TPARM
, /*initialized=*/0,
15142 /*attrlist=*/NULL
);
15143 if (parm
== error_mark_node
)
15144 return error_mark_node
;
15146 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15149 /* Parse a type-parameter.
15152 class identifier [opt]
15153 class identifier [opt] = type-id
15154 typename identifier [opt]
15155 typename identifier [opt] = type-id
15156 template < template-parameter-list > class identifier [opt]
15157 template < template-parameter-list > class identifier [opt]
15160 GNU Extension (variadic templates):
15163 class ... identifier [opt]
15164 typename ... identifier [opt]
15166 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15167 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15168 the declaration of the parameter.
15170 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15173 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15178 /* Look for a keyword to tell us what kind of parameter this is. */
15179 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15181 return error_mark_node
;
15183 switch (token
->keyword
)
15189 tree default_argument
;
15191 /* If the next token is an ellipsis, we have a template
15193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15195 /* Consume the `...' token. */
15196 cp_lexer_consume_token (parser
->lexer
);
15197 maybe_warn_variadic_templates ();
15199 *is_parameter_pack
= true;
15202 /* If the next token is an identifier, then it names the
15204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15205 identifier
= cp_parser_identifier (parser
);
15207 identifier
= NULL_TREE
;
15209 /* Create the parameter. */
15210 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15212 /* If the next token is an `=', we have a default argument. */
15213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15216 = cp_parser_default_type_template_argument (parser
);
15218 /* Template parameter packs cannot have default
15220 if (*is_parameter_pack
)
15223 error_at (token
->location
,
15224 "template parameter pack %qD cannot have a "
15225 "default argument", identifier
);
15227 error_at (token
->location
,
15228 "template parameter packs cannot have "
15229 "default arguments");
15230 default_argument
= NULL_TREE
;
15232 else if (check_for_bare_parameter_packs (default_argument
))
15233 default_argument
= error_mark_node
;
15236 default_argument
= NULL_TREE
;
15238 /* Create the combined representation of the parameter and the
15239 default argument. */
15240 parameter
= build_tree_list (default_argument
, parameter
);
15247 tree default_argument
;
15249 /* Look for the `<'. */
15250 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15251 /* Parse the template-parameter-list. */
15252 cp_parser_template_parameter_list (parser
);
15253 /* Look for the `>'. */
15254 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15256 // If template requirements are present, parse them.
15259 tree reqs
= get_shorthand_constraints (current_template_parms
);
15260 if (tree r
= cp_parser_requires_clause_opt (parser
))
15261 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15262 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15265 /* Look for the `class' or 'typename' keywords. */
15266 cp_parser_type_parameter_key (parser
);
15267 /* If the next token is an ellipsis, we have a template
15269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15271 /* Consume the `...' token. */
15272 cp_lexer_consume_token (parser
->lexer
);
15273 maybe_warn_variadic_templates ();
15275 *is_parameter_pack
= true;
15277 /* If the next token is an `=', then there is a
15278 default-argument. If the next token is a `>', we are at
15279 the end of the parameter-list. If the next token is a `,',
15280 then we are at the end of this parameter. */
15281 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15282 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15283 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15285 identifier
= cp_parser_identifier (parser
);
15286 /* Treat invalid names as if the parameter were nameless. */
15287 if (identifier
== error_mark_node
)
15288 identifier
= NULL_TREE
;
15291 identifier
= NULL_TREE
;
15293 /* Create the template parameter. */
15294 parameter
= finish_template_template_parm (class_type_node
,
15297 /* If the next token is an `=', then there is a
15298 default-argument. */
15299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15302 = cp_parser_default_template_template_argument (parser
);
15304 /* Template parameter packs cannot have default
15306 if (*is_parameter_pack
)
15309 error_at (token
->location
,
15310 "template parameter pack %qD cannot "
15311 "have a default argument",
15314 error_at (token
->location
, "template parameter packs cannot "
15315 "have default arguments");
15316 default_argument
= NULL_TREE
;
15320 default_argument
= NULL_TREE
;
15322 /* Create the combined representation of the parameter and the
15323 default argument. */
15324 parameter
= build_tree_list (default_argument
, parameter
);
15329 gcc_unreachable ();
15336 /* Parse a template-id.
15339 template-name < template-argument-list [opt] >
15341 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15342 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15343 returned. Otherwise, if the template-name names a function, or set
15344 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15345 names a class, returns a TYPE_DECL for the specialization.
15347 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15348 uninstantiated templates. */
15351 cp_parser_template_id (cp_parser
*parser
,
15352 bool template_keyword_p
,
15353 bool check_dependency_p
,
15354 enum tag_types tag_type
,
15355 bool is_declaration
)
15360 cp_token_position start_of_id
= 0;
15361 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15362 bool is_identifier
;
15364 /* If the next token corresponds to a template-id, there is no need
15366 next_token
= cp_lexer_peek_token (parser
->lexer
);
15367 if (next_token
->type
== CPP_TEMPLATE_ID
)
15369 cp_lexer_consume_token (parser
->lexer
);
15370 return saved_checks_value (next_token
->u
.tree_check_value
);
15373 /* Avoid performing name lookup if there is no possibility of
15374 finding a template-id. */
15375 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
15376 || (next_token
->type
== CPP_NAME
15377 && !cp_parser_nth_token_starts_template_argument_list_p
15380 cp_parser_error (parser
, "expected template-id");
15381 return error_mark_node
;
15384 /* Remember where the template-id starts. */
15385 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15386 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15388 push_deferring_access_checks (dk_deferred
);
15390 /* Parse the template-name. */
15391 is_identifier
= false;
15392 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15393 check_dependency_p
,
15397 if (templ
== error_mark_node
|| is_identifier
)
15399 pop_deferring_access_checks ();
15403 /* Since we're going to preserve any side-effects from this parse, set up a
15404 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15405 in the template arguments. */
15406 tentative_firewall
firewall (parser
);
15408 /* If we find the sequence `[:' after a template-name, it's probably
15409 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15410 parse correctly the argument list. */
15411 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15412 == CPP_OPEN_SQUARE
)
15413 && next_token
->flags
& DIGRAPH
15414 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15416 && !(next_token_2
->flags
& PREV_WHITE
))
15418 cp_parser_parse_tentatively (parser
);
15419 /* Change `:' into `::'. */
15420 next_token_2
->type
= CPP_SCOPE
;
15421 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15423 cp_lexer_consume_token (parser
->lexer
);
15425 /* Parse the arguments. */
15426 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15427 if (!cp_parser_parse_definitely (parser
))
15429 /* If we couldn't parse an argument list, then we revert our changes
15430 and return simply an error. Maybe this is not a template-id
15432 next_token_2
->type
= CPP_COLON
;
15433 cp_parser_error (parser
, "expected %<<%>");
15434 pop_deferring_access_checks ();
15435 return error_mark_node
;
15437 /* Otherwise, emit an error about the invalid digraph, but continue
15438 parsing because we got our argument list. */
15439 if (permerror (next_token
->location
,
15440 "%<<::%> cannot begin a template-argument list"))
15442 static bool hint
= false;
15443 inform (next_token
->location
,
15444 "%<<:%> is an alternate spelling for %<[%>."
15445 " Insert whitespace between %<<%> and %<::%>");
15446 if (!hint
&& !flag_permissive
)
15448 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15449 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15450 "accept your code)");
15457 /* Look for the `<' that starts the template-argument-list. */
15458 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15460 pop_deferring_access_checks ();
15461 return error_mark_node
;
15463 /* Parse the arguments. */
15464 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15467 /* Build a representation of the specialization. */
15468 if (identifier_p (templ
))
15469 template_id
= build_min_nt_loc (next_token
->location
,
15472 else if (DECL_TYPE_TEMPLATE_P (templ
)
15473 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15475 bool entering_scope
;
15476 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15477 template (rather than some instantiation thereof) only if
15478 is not nested within some other construct. For example, in
15479 "template <typename T> void f(T) { A<T>::", A<T> is just an
15480 instantiation of A. */
15481 entering_scope
= (template_parm_scope_p ()
15482 && cp_lexer_next_token_is (parser
->lexer
,
15485 = finish_template_type (templ
, arguments
, entering_scope
);
15487 /* A template-like identifier may be a partial concept id. */
15488 else if (flag_concepts
15489 && (template_id
= (cp_parser_maybe_partial_concept_id
15490 (parser
, templ
, arguments
))))
15491 return template_id
;
15492 else if (variable_template_p (templ
))
15494 template_id
= lookup_template_variable (templ
, arguments
);
15495 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15496 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15500 /* If it's not a class-template or a template-template, it should be
15501 a function-template. */
15502 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15503 || TREE_CODE (templ
) == OVERLOAD
15504 || BASELINK_P (templ
)));
15506 template_id
= lookup_template_function (templ
, arguments
);
15507 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15508 SET_EXPR_LOCATION (template_id
, next_token
->location
);
15511 /* If parsing tentatively, replace the sequence of tokens that makes
15512 up the template-id with a CPP_TEMPLATE_ID token. That way,
15513 should we re-parse the token stream, we will not have to repeat
15514 the effort required to do the parse, nor will we issue duplicate
15515 error messages about problems during instantiation of the
15518 /* Don't do this if we had a parse error in a declarator; re-parsing
15519 might succeed if a name changes meaning (60361). */
15520 && !(cp_parser_error_occurred (parser
)
15521 && cp_parser_parsing_tentatively (parser
)
15522 && parser
->in_declarator_p
))
15524 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
15526 /* Reset the contents of the START_OF_ID token. */
15527 token
->type
= CPP_TEMPLATE_ID
;
15529 /* Update the location to be of the form:
15530 template-name < template-argument-list [opt] >
15531 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15532 with caret == start at the start of the template-name,
15533 ranging until the closing '>'. */
15534 location_t finish_loc
15535 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15536 location_t combined_loc
15537 = make_location (token
->location
, token
->location
, finish_loc
);
15538 token
->location
= combined_loc
;
15540 /* Retrieve any deferred checks. Do not pop this access checks yet
15541 so the memory will not be reclaimed during token replacing below. */
15542 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15543 token
->u
.tree_check_value
->value
= template_id
;
15544 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15545 token
->keyword
= RID_MAX
;
15547 /* Purge all subsequent tokens. */
15548 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15550 /* ??? Can we actually assume that, if template_id ==
15551 error_mark_node, we will have issued a diagnostic to the
15552 user, as opposed to simply marking the tentative parse as
15554 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15555 error_at (token
->location
, "parse error in template argument list");
15558 pop_to_parent_deferring_access_checks ();
15559 return template_id
;
15562 /* Parse a template-name.
15567 The standard should actually say:
15571 operator-function-id
15573 A defect report has been filed about this issue.
15575 A conversion-function-id cannot be a template name because they cannot
15576 be part of a template-id. In fact, looking at this code:
15578 a.operator K<int>()
15580 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15581 It is impossible to call a templated conversion-function-id with an
15582 explicit argument list, since the only allowed template parameter is
15583 the type to which it is converting.
15585 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15586 `template' keyword, in a construction like:
15590 In that case `f' is taken to be a template-name, even though there
15591 is no way of knowing for sure.
15593 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15594 name refers to a set of overloaded functions, at least one of which
15595 is a template, or an IDENTIFIER_NODE with the name of the template,
15596 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15597 names are looked up inside uninstantiated templates. */
15600 cp_parser_template_name (cp_parser
* parser
,
15601 bool template_keyword_p
,
15602 bool check_dependency_p
,
15603 bool is_declaration
,
15604 enum tag_types tag_type
,
15605 bool *is_identifier
)
15610 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15612 /* If the next token is `operator', then we have either an
15613 operator-function-id or a conversion-function-id. */
15614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15616 /* We don't know whether we're looking at an
15617 operator-function-id or a conversion-function-id. */
15618 cp_parser_parse_tentatively (parser
);
15619 /* Try an operator-function-id. */
15620 identifier
= cp_parser_operator_function_id (parser
);
15621 /* If that didn't work, try a conversion-function-id. */
15622 if (!cp_parser_parse_definitely (parser
))
15624 cp_parser_error (parser
, "expected template-name");
15625 return error_mark_node
;
15628 /* Look for the identifier. */
15630 identifier
= cp_parser_identifier (parser
);
15632 /* If we didn't find an identifier, we don't have a template-id. */
15633 if (identifier
== error_mark_node
)
15634 return error_mark_node
;
15636 /* If the name immediately followed the `template' keyword, then it
15637 is a template-name. However, if the next token is not `<', then
15638 we do not treat it as a template-name, since it is not being used
15639 as part of a template-id. This enables us to handle constructs
15642 template <typename T> struct S { S(); };
15643 template <typename T> S<T>::S();
15645 correctly. We would treat `S' as a template -- if it were `S<T>'
15646 -- but we do not if there is no `<'. */
15648 if (processing_template_decl
15649 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15651 /* In a declaration, in a dependent context, we pretend that the
15652 "template" keyword was present in order to improve error
15653 recovery. For example, given:
15655 template <typename T> void f(T::X<int>);
15657 we want to treat "X<int>" as a template-id. */
15659 && !template_keyword_p
15660 && parser
->scope
&& TYPE_P (parser
->scope
)
15661 && check_dependency_p
15662 && dependent_scope_p (parser
->scope
)
15663 /* Do not do this for dtors (or ctors), since they never
15664 need the template keyword before their name. */
15665 && !constructor_name_p (identifier
, parser
->scope
))
15667 cp_token_position start
= 0;
15669 /* Explain what went wrong. */
15670 error_at (token
->location
, "non-template %qD used as template",
15672 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15673 parser
->scope
, identifier
);
15674 /* If parsing tentatively, find the location of the "<" token. */
15675 if (cp_parser_simulate_error (parser
))
15676 start
= cp_lexer_token_position (parser
->lexer
, true);
15677 /* Parse the template arguments so that we can issue error
15678 messages about them. */
15679 cp_lexer_consume_token (parser
->lexer
);
15680 cp_parser_enclosed_template_argument_list (parser
);
15681 /* Skip tokens until we find a good place from which to
15682 continue parsing. */
15683 cp_parser_skip_to_closing_parenthesis (parser
,
15684 /*recovering=*/true,
15686 /*consume_paren=*/false);
15687 /* If parsing tentatively, permanently remove the
15688 template argument list. That will prevent duplicate
15689 error messages from being issued about the missing
15690 "template" keyword. */
15692 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15694 *is_identifier
= true;
15698 /* If the "template" keyword is present, then there is generally
15699 no point in doing name-lookup, so we just return IDENTIFIER.
15700 But, if the qualifying scope is non-dependent then we can
15701 (and must) do name-lookup normally. */
15702 if (template_keyword_p
15704 || (TYPE_P (parser
->scope
)
15705 && dependent_type_p (parser
->scope
))))
15709 /* Look up the name. */
15710 decl
= cp_parser_lookup_name (parser
, identifier
,
15712 /*is_template=*/true,
15713 /*is_namespace=*/false,
15714 check_dependency_p
,
15715 /*ambiguous_decls=*/NULL
,
15718 decl
= strip_using_decl (decl
);
15720 /* If DECL is a template, then the name was a template-name. */
15721 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
15723 if (TREE_DEPRECATED (decl
)
15724 && deprecated_state
!= DEPRECATED_SUPPRESS
)
15725 warn_deprecated_use (decl
, NULL_TREE
);
15729 tree fn
= NULL_TREE
;
15731 /* The standard does not explicitly indicate whether a name that
15732 names a set of overloaded declarations, some of which are
15733 templates, is a template-name. However, such a name should
15734 be a template-name; otherwise, there is no way to form a
15735 template-id for the overloaded templates. */
15736 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
15737 if (TREE_CODE (fns
) == OVERLOAD
)
15738 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
15739 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
15744 /* The name does not name a template. */
15745 cp_parser_error (parser
, "expected template-name");
15746 return error_mark_node
;
15750 /* If DECL is dependent, and refers to a function, then just return
15751 its name; we will look it up again during template instantiation. */
15752 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
15754 tree scope
= ovl_scope (decl
);
15755 if (TYPE_P (scope
) && dependent_type_p (scope
))
15762 /* Parse a template-argument-list.
15764 template-argument-list:
15765 template-argument ... [opt]
15766 template-argument-list , template-argument ... [opt]
15768 Returns a TREE_VEC containing the arguments. */
15771 cp_parser_template_argument_list (cp_parser
* parser
)
15773 tree fixed_args
[10];
15774 unsigned n_args
= 0;
15775 unsigned alloced
= 10;
15776 tree
*arg_ary
= fixed_args
;
15778 bool saved_in_template_argument_list_p
;
15780 bool saved_non_ice_p
;
15782 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
15783 parser
->in_template_argument_list_p
= true;
15784 /* Even if the template-id appears in an integral
15785 constant-expression, the contents of the argument list do
15787 saved_ice_p
= parser
->integral_constant_expression_p
;
15788 parser
->integral_constant_expression_p
= false;
15789 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
15790 parser
->non_integral_constant_expression_p
= false;
15792 /* Parse the arguments. */
15798 /* Consume the comma. */
15799 cp_lexer_consume_token (parser
->lexer
);
15801 /* Parse the template-argument. */
15802 argument
= cp_parser_template_argument (parser
);
15804 /* If the next token is an ellipsis, we're expanding a template
15806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15808 if (argument
== error_mark_node
)
15810 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15811 error_at (token
->location
,
15812 "expected parameter pack before %<...%>");
15814 /* Consume the `...' token. */
15815 cp_lexer_consume_token (parser
->lexer
);
15817 /* Make the argument into a TYPE_PACK_EXPANSION or
15818 EXPR_PACK_EXPANSION. */
15819 argument
= make_pack_expansion (argument
);
15822 if (n_args
== alloced
)
15826 if (arg_ary
== fixed_args
)
15828 arg_ary
= XNEWVEC (tree
, alloced
);
15829 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
15832 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
15834 arg_ary
[n_args
++] = argument
;
15836 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15838 vec
= make_tree_vec (n_args
);
15841 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
15843 if (arg_ary
!= fixed_args
)
15845 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
15846 parser
->integral_constant_expression_p
= saved_ice_p
;
15847 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
15849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
15853 /* Parse a template-argument.
15856 assignment-expression
15860 The representation is that of an assignment-expression, type-id, or
15861 id-expression -- except that the qualified id-expression is
15862 evaluated, so that the value returned is either a DECL or an
15865 Although the standard says "assignment-expression", it forbids
15866 throw-expressions or assignments in the template argument.
15867 Therefore, we use "conditional-expression" instead. */
15870 cp_parser_template_argument (cp_parser
* parser
)
15875 bool maybe_type_id
= false;
15876 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
15877 location_t loc
= 0;
15880 /* There's really no way to know what we're looking at, so we just
15881 try each alternative in order.
15885 In a template-argument, an ambiguity between a type-id and an
15886 expression is resolved to a type-id, regardless of the form of
15887 the corresponding template-parameter.
15889 Therefore, we try a type-id first. */
15890 cp_parser_parse_tentatively (parser
);
15891 argument
= cp_parser_template_type_arg (parser
);
15892 /* If there was no error parsing the type-id but the next token is a
15893 '>>', our behavior depends on which dialect of C++ we're
15894 parsing. In C++98, we probably found a typo for '> >'. But there
15895 are type-id which are also valid expressions. For instance:
15897 struct X { int operator >> (int); };
15898 template <int V> struct Foo {};
15901 Here 'X()' is a valid type-id of a function type, but the user just
15902 wanted to write the expression "X() >> 5". Thus, we remember that we
15903 found a valid type-id, but we still try to parse the argument as an
15904 expression to see what happens.
15906 In C++0x, the '>>' will be considered two separate '>'
15908 if (!cp_parser_error_occurred (parser
)
15909 && cxx_dialect
== cxx98
15910 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15912 maybe_type_id
= true;
15913 cp_parser_abort_tentative_parse (parser
);
15917 /* If the next token isn't a `,' or a `>', then this argument wasn't
15918 really finished. This means that the argument is not a valid
15920 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15921 cp_parser_error (parser
, "expected template-argument");
15922 /* If that worked, we're done. */
15923 if (cp_parser_parse_definitely (parser
))
15926 /* We're still not sure what the argument will be. */
15927 cp_parser_parse_tentatively (parser
);
15928 /* Try a template. */
15929 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
15930 argument
= cp_parser_id_expression (parser
,
15931 /*template_keyword_p=*/false,
15932 /*check_dependency_p=*/true,
15934 /*declarator_p=*/false,
15935 /*optional_p=*/false);
15936 /* If the next token isn't a `,' or a `>', then this argument wasn't
15937 really finished. */
15938 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15939 cp_parser_error (parser
, "expected template-argument");
15940 if (!cp_parser_error_occurred (parser
))
15942 /* Figure out what is being referred to. If the id-expression
15943 was for a class template specialization, then we will have a
15944 TYPE_DECL at this point. There is no need to do name lookup
15945 at this point in that case. */
15946 if (TREE_CODE (argument
) != TYPE_DECL
)
15947 argument
= cp_parser_lookup_name (parser
, argument
,
15949 /*is_template=*/template_p
,
15950 /*is_namespace=*/false,
15951 /*check_dependency=*/true,
15952 /*ambiguous_decls=*/NULL
,
15953 argument_start_token
->location
);
15954 /* Handle a constrained-type-specifier for a non-type template
15956 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
15958 else if (TREE_CODE (argument
) != TEMPLATE_DECL
15959 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
15960 cp_parser_error (parser
, "expected template-name");
15962 if (cp_parser_parse_definitely (parser
))
15964 if (TREE_DEPRECATED (argument
))
15965 warn_deprecated_use (argument
, NULL_TREE
);
15968 /* It must be a non-type argument. In C++17 any constant-expression is
15970 if (cxx_dialect
> cxx14
)
15973 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15975 -- an integral constant-expression of integral or enumeration
15978 -- the name of a non-type template-parameter; or
15980 -- the name of an object or function with external linkage...
15982 -- the address of an object or function with external linkage...
15984 -- a pointer to member... */
15985 /* Look for a non-type template parameter. */
15986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15988 cp_parser_parse_tentatively (parser
);
15989 argument
= cp_parser_primary_expression (parser
,
15990 /*address_p=*/false,
15992 /*template_arg_p=*/true,
15994 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
15995 || !cp_parser_next_token_ends_template_argument_p (parser
))
15996 cp_parser_simulate_error (parser
);
15997 if (cp_parser_parse_definitely (parser
))
16001 /* If the next token is "&", the argument must be the address of an
16002 object or function with external linkage. */
16003 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16006 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16007 cp_lexer_consume_token (parser
->lexer
);
16009 /* See if we might have an id-expression. */
16010 token
= cp_lexer_peek_token (parser
->lexer
);
16011 if (token
->type
== CPP_NAME
16012 || token
->keyword
== RID_OPERATOR
16013 || token
->type
== CPP_SCOPE
16014 || token
->type
== CPP_TEMPLATE_ID
16015 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16017 cp_parser_parse_tentatively (parser
);
16018 argument
= cp_parser_primary_expression (parser
,
16021 /*template_arg_p=*/true,
16023 if (cp_parser_error_occurred (parser
)
16024 || !cp_parser_next_token_ends_template_argument_p (parser
))
16025 cp_parser_abort_tentative_parse (parser
);
16030 if (INDIRECT_REF_P (argument
))
16032 /* Strip the dereference temporarily. */
16033 gcc_assert (REFERENCE_REF_P (argument
));
16034 argument
= TREE_OPERAND (argument
, 0);
16037 /* If we're in a template, we represent a qualified-id referring
16038 to a static data member as a SCOPE_REF even if the scope isn't
16039 dependent so that we can check access control later. */
16041 if (TREE_CODE (probe
) == SCOPE_REF
)
16042 probe
= TREE_OPERAND (probe
, 1);
16045 /* A variable without external linkage might still be a
16046 valid constant-expression, so no error is issued here
16047 if the external-linkage check fails. */
16048 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16049 cp_parser_simulate_error (parser
);
16051 else if (is_overloaded_fn (argument
))
16052 /* All overloaded functions are allowed; if the external
16053 linkage test does not pass, an error will be issued
16057 && (TREE_CODE (argument
) == OFFSET_REF
16058 || TREE_CODE (argument
) == SCOPE_REF
))
16059 /* A pointer-to-member. */
16061 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16064 cp_parser_simulate_error (parser
);
16066 if (cp_parser_parse_definitely (parser
))
16069 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16070 tf_warning_or_error
);
16072 argument
= convert_from_reference (argument
);
16077 /* If the argument started with "&", there are no other valid
16078 alternatives at this point. */
16081 cp_parser_error (parser
, "invalid non-type template argument");
16082 return error_mark_node
;
16086 /* If the argument wasn't successfully parsed as a type-id followed
16087 by '>>', the argument can only be a constant expression now.
16088 Otherwise, we try parsing the constant-expression tentatively,
16089 because the argument could really be a type-id. */
16091 cp_parser_parse_tentatively (parser
);
16093 if (cxx_dialect
<= cxx14
)
16094 argument
= cp_parser_constant_expression (parser
);
16097 /* With C++17 generalized non-type template arguments we need to handle
16098 lvalue constant expressions, too. */
16099 argument
= cp_parser_assignment_expression (parser
);
16100 require_potential_constant_expression (argument
);
16103 if (!maybe_type_id
)
16105 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16106 cp_parser_error (parser
, "expected template-argument");
16107 if (cp_parser_parse_definitely (parser
))
16109 /* We did our best to parse the argument as a non type-id, but that
16110 was the only alternative that matched (albeit with a '>' after
16111 it). We can assume it's just a typo from the user, and a
16112 diagnostic will then be issued. */
16113 return cp_parser_template_type_arg (parser
);
16116 /* Parse an explicit-instantiation.
16118 explicit-instantiation:
16119 template declaration
16121 Although the standard says `declaration', what it really means is:
16123 explicit-instantiation:
16124 template decl-specifier-seq [opt] declarator [opt] ;
16126 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16127 supposed to be allowed. A defect report has been filed about this
16132 explicit-instantiation:
16133 storage-class-specifier template
16134 decl-specifier-seq [opt] declarator [opt] ;
16135 function-specifier template
16136 decl-specifier-seq [opt] declarator [opt] ; */
16139 cp_parser_explicit_instantiation (cp_parser
* parser
)
16141 int declares_class_or_enum
;
16142 cp_decl_specifier_seq decl_specifiers
;
16143 tree extension_specifier
= NULL_TREE
;
16145 timevar_push (TV_TEMPLATE_INST
);
16147 /* Look for an (optional) storage-class-specifier or
16148 function-specifier. */
16149 if (cp_parser_allow_gnu_extensions_p (parser
))
16151 extension_specifier
16152 = cp_parser_storage_class_specifier_opt (parser
);
16153 if (!extension_specifier
)
16154 extension_specifier
16155 = cp_parser_function_specifier_opt (parser
,
16156 /*decl_specs=*/NULL
);
16159 /* Look for the `template' keyword. */
16160 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16161 /* Let the front end know that we are processing an explicit
16163 begin_explicit_instantiation ();
16164 /* [temp.explicit] says that we are supposed to ignore access
16165 control while processing explicit instantiation directives. */
16166 push_deferring_access_checks (dk_no_check
);
16167 /* Parse a decl-specifier-seq. */
16168 cp_parser_decl_specifier_seq (parser
,
16169 CP_PARSER_FLAGS_OPTIONAL
,
16171 &declares_class_or_enum
);
16172 /* If there was exactly one decl-specifier, and it declared a class,
16173 and there's no declarator, then we have an explicit type
16175 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16179 type
= check_tag_decl (&decl_specifiers
,
16180 /*explicit_type_instantiation_p=*/true);
16181 /* Turn access control back on for names used during
16182 template instantiation. */
16183 pop_deferring_access_checks ();
16185 do_type_instantiation (type
, extension_specifier
,
16186 /*complain=*/tf_error
);
16190 cp_declarator
*declarator
;
16193 /* Parse the declarator. */
16195 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16196 /*ctor_dtor_or_conv_p=*/NULL
,
16197 /*parenthesized_p=*/NULL
,
16198 /*member_p=*/false,
16199 /*friend_p=*/false);
16200 if (declares_class_or_enum
& 2)
16201 cp_parser_check_for_definition_in_return_type (declarator
,
16202 decl_specifiers
.type
,
16203 decl_specifiers
.locations
[ds_type_spec
]);
16204 if (declarator
!= cp_error_declarator
)
16206 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16207 permerror (decl_specifiers
.locations
[ds_inline
],
16208 "explicit instantiation shall not use"
16209 " %<inline%> specifier");
16210 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16211 permerror (decl_specifiers
.locations
[ds_constexpr
],
16212 "explicit instantiation shall not use"
16213 " %<constexpr%> specifier");
16215 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16216 NORMAL
, 0, &decl_specifiers
.attributes
);
16217 /* Turn access control back on for names used during
16218 template instantiation. */
16219 pop_deferring_access_checks ();
16220 /* Do the explicit instantiation. */
16221 do_decl_instantiation (decl
, extension_specifier
);
16225 pop_deferring_access_checks ();
16226 /* Skip the body of the explicit instantiation. */
16227 cp_parser_skip_to_end_of_statement (parser
);
16230 /* We're done with the instantiation. */
16231 end_explicit_instantiation ();
16233 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16235 timevar_pop (TV_TEMPLATE_INST
);
16238 /* Parse an explicit-specialization.
16240 explicit-specialization:
16241 template < > declaration
16243 Although the standard says `declaration', what it really means is:
16245 explicit-specialization:
16246 template <> decl-specifier [opt] init-declarator [opt] ;
16247 template <> function-definition
16248 template <> explicit-specialization
16249 template <> template-declaration */
16252 cp_parser_explicit_specialization (cp_parser
* parser
)
16254 bool need_lang_pop
;
16255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16257 /* Look for the `template' keyword. */
16258 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16259 /* Look for the `<'. */
16260 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16261 /* Look for the `>'. */
16262 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16263 /* We have processed another parameter list. */
16264 ++parser
->num_template_parameter_lists
;
16267 A template ... explicit specialization ... shall not have C
16269 if (current_lang_name
== lang_name_c
)
16271 error_at (token
->location
, "template specialization with C linkage");
16272 /* Give it C++ linkage to avoid confusing other parts of the
16274 push_lang_context (lang_name_cplusplus
);
16275 need_lang_pop
= true;
16278 need_lang_pop
= false;
16279 /* Let the front end know that we are beginning a specialization. */
16280 if (!begin_specialization ())
16282 end_specialization ();
16286 /* If the next keyword is `template', we need to figure out whether
16287 or not we're looking a template-declaration. */
16288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16290 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16291 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16292 cp_parser_template_declaration_after_export (parser
,
16293 /*member_p=*/false);
16295 cp_parser_explicit_specialization (parser
);
16298 /* Parse the dependent declaration. */
16299 cp_parser_single_declaration (parser
,
16301 /*member_p=*/false,
16302 /*explicit_specialization_p=*/true,
16303 /*friend_p=*/NULL
);
16304 /* We're done with the specialization. */
16305 end_specialization ();
16306 /* For the erroneous case of a template with C linkage, we pushed an
16307 implicit C++ linkage scope; exit that scope now. */
16309 pop_lang_context ();
16310 /* We're done with this parameter list. */
16311 --parser
->num_template_parameter_lists
;
16314 /* Parse a type-specifier.
16317 simple-type-specifier
16320 elaborated-type-specifier
16328 Returns a representation of the type-specifier. For a
16329 class-specifier, enum-specifier, or elaborated-type-specifier, a
16330 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16332 The parser flags FLAGS is used to control type-specifier parsing.
16334 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16335 in a decl-specifier-seq.
16337 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16338 class-specifier, enum-specifier, or elaborated-type-specifier, then
16339 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16340 if a type is declared; 2 if it is defined. Otherwise, it is set to
16343 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16344 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16345 is set to FALSE. */
16348 cp_parser_type_specifier (cp_parser
* parser
,
16349 cp_parser_flags flags
,
16350 cp_decl_specifier_seq
*decl_specs
,
16351 bool is_declaration
,
16352 int* declares_class_or_enum
,
16353 bool* is_cv_qualifier
)
16355 tree type_spec
= NULL_TREE
;
16358 cp_decl_spec ds
= ds_last
;
16360 /* Assume this type-specifier does not declare a new type. */
16361 if (declares_class_or_enum
)
16362 *declares_class_or_enum
= 0;
16363 /* And that it does not specify a cv-qualifier. */
16364 if (is_cv_qualifier
)
16365 *is_cv_qualifier
= false;
16366 /* Peek at the next token. */
16367 token
= cp_lexer_peek_token (parser
->lexer
);
16369 /* If we're looking at a keyword, we can use that to guide the
16370 production we choose. */
16371 keyword
= token
->keyword
;
16375 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16376 goto elaborated_type_specifier
;
16378 /* Look for the enum-specifier. */
16379 type_spec
= cp_parser_enum_specifier (parser
);
16380 /* If that worked, we're done. */
16383 if (declares_class_or_enum
)
16384 *declares_class_or_enum
= 2;
16386 cp_parser_set_decl_spec_type (decl_specs
,
16389 /*type_definition_p=*/true);
16393 goto elaborated_type_specifier
;
16395 /* Any of these indicate either a class-specifier, or an
16396 elaborated-type-specifier. */
16400 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16401 goto elaborated_type_specifier
;
16403 /* Parse tentatively so that we can back up if we don't find a
16404 class-specifier. */
16405 cp_parser_parse_tentatively (parser
);
16406 /* Look for the class-specifier. */
16407 type_spec
= cp_parser_class_specifier (parser
);
16408 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16409 /* If that worked, we're done. */
16410 if (cp_parser_parse_definitely (parser
))
16412 if (declares_class_or_enum
)
16413 *declares_class_or_enum
= 2;
16415 cp_parser_set_decl_spec_type (decl_specs
,
16418 /*type_definition_p=*/true);
16422 /* Fall through. */
16423 elaborated_type_specifier
:
16424 /* We're declaring (not defining) a class or enum. */
16425 if (declares_class_or_enum
)
16426 *declares_class_or_enum
= 1;
16428 /* Fall through. */
16430 /* Look for an elaborated-type-specifier. */
16432 = (cp_parser_elaborated_type_specifier
16434 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16437 cp_parser_set_decl_spec_type (decl_specs
,
16440 /*type_definition_p=*/false);
16445 if (is_cv_qualifier
)
16446 *is_cv_qualifier
= true;
16451 if (is_cv_qualifier
)
16452 *is_cv_qualifier
= true;
16457 if (is_cv_qualifier
)
16458 *is_cv_qualifier
= true;
16462 /* The `__complex__' keyword is a GNU extension. */
16470 /* Handle simple keywords. */
16475 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16476 decl_specs
->any_specifiers_p
= true;
16478 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16481 /* If we do not already have a type-specifier, assume we are looking
16482 at a simple-type-specifier. */
16483 type_spec
= cp_parser_simple_type_specifier (parser
,
16487 /* If we didn't find a type-specifier, and a type-specifier was not
16488 optional in this context, issue an error message. */
16489 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16491 cp_parser_error (parser
, "expected type specifier");
16492 return error_mark_node
;
16498 /* Parse a simple-type-specifier.
16500 simple-type-specifier:
16501 :: [opt] nested-name-specifier [opt] type-name
16502 :: [opt] nested-name-specifier template template-id
16517 simple-type-specifier:
16519 decltype ( expression )
16522 __underlying_type ( type-id )
16526 nested-name-specifier(opt) template-name
16530 simple-type-specifier:
16532 __typeof__ unary-expression
16533 __typeof__ ( type-id )
16534 __typeof__ ( type-id ) { initializer-list , [opt] }
16536 Concepts Extension:
16538 simple-type-specifier:
16539 constrained-type-specifier
16541 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16542 appropriately updated. */
16545 cp_parser_simple_type_specifier (cp_parser
* parser
,
16546 cp_decl_specifier_seq
*decl_specs
,
16547 cp_parser_flags flags
)
16549 tree type
= NULL_TREE
;
16553 /* Peek at the next token. */
16554 token
= cp_lexer_peek_token (parser
->lexer
);
16556 /* If we're looking at a keyword, things are easy. */
16557 switch (token
->keyword
)
16561 decl_specs
->explicit_char_p
= true;
16562 type
= char_type_node
;
16565 type
= char16_type_node
;
16568 type
= char32_type_node
;
16571 type
= wchar_type_node
;
16574 type
= boolean_type_node
;
16577 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16578 type
= short_integer_type_node
;
16582 decl_specs
->explicit_int_p
= true;
16583 type
= integer_type_node
;
16589 idx
= token
->keyword
- RID_INT_N_0
;
16590 if (! int_n_enabled_p
[idx
])
16594 decl_specs
->explicit_intN_p
= true;
16595 decl_specs
->int_n_idx
= idx
;
16597 type
= int_n_trees
[idx
].signed_type
;
16601 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16602 type
= long_integer_type_node
;
16605 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16606 type
= integer_type_node
;
16609 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16610 type
= unsigned_type_node
;
16613 type
= float_type_node
;
16616 type
= double_type_node
;
16619 type
= void_type_node
;
16623 maybe_warn_cpp0x (CPP0X_AUTO
);
16624 if (parser
->auto_is_implicit_function_template_parm_p
)
16626 /* The 'auto' might be the placeholder return type for a function decl
16627 with trailing return type. */
16628 bool have_trailing_return_fn_decl
= false;
16630 cp_parser_parse_tentatively (parser
);
16631 cp_lexer_consume_token (parser
->lexer
);
16632 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16633 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16634 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16635 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16639 cp_lexer_consume_token (parser
->lexer
);
16640 cp_parser_skip_to_closing_parenthesis (parser
,
16641 /*recovering*/false,
16643 /*consume_paren*/true);
16647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16649 have_trailing_return_fn_decl
= true;
16653 cp_lexer_consume_token (parser
->lexer
);
16655 cp_parser_abort_tentative_parse (parser
);
16657 if (have_trailing_return_fn_decl
)
16659 type
= make_auto ();
16663 if (cxx_dialect
>= cxx14
)
16665 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16666 type
= TREE_TYPE (type
);
16669 type
= error_mark_node
;
16671 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16673 if (cxx_dialect
< cxx14
)
16674 error_at (token
->location
,
16675 "use of %<auto%> in lambda parameter declaration "
16676 "only available with "
16677 "-std=c++14 or -std=gnu++14");
16679 else if (cxx_dialect
< cxx14
)
16680 error_at (token
->location
,
16681 "use of %<auto%> in parameter declaration "
16682 "only available with "
16683 "-std=c++14 or -std=gnu++14");
16684 else if (!flag_concepts
)
16685 pedwarn (token
->location
, OPT_Wpedantic
,
16686 "ISO C++ forbids use of %<auto%> in parameter "
16690 type
= make_auto ();
16694 /* Since DR 743, decltype can either be a simple-type-specifier by
16695 itself or begin a nested-name-specifier. Parsing it will replace
16696 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16697 handling below decide what to do. */
16698 cp_parser_decltype (parser
);
16699 cp_lexer_set_token_position (parser
->lexer
, token
);
16703 /* Consume the `typeof' token. */
16704 cp_lexer_consume_token (parser
->lexer
);
16705 /* Parse the operand to `typeof'. */
16706 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16707 /* If it is not already a TYPE, take its type. */
16708 if (!TYPE_P (type
))
16709 type
= finish_typeof (type
);
16712 cp_parser_set_decl_spec_type (decl_specs
, type
,
16714 /*type_definition_p=*/false);
16718 case RID_UNDERLYING_TYPE
:
16719 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
16721 cp_parser_set_decl_spec_type (decl_specs
, type
,
16723 /*type_definition_p=*/false);
16728 case RID_DIRECT_BASES
:
16729 type
= cp_parser_trait_expr (parser
, token
->keyword
);
16731 cp_parser_set_decl_spec_type (decl_specs
, type
,
16733 /*type_definition_p=*/false);
16739 /* If token is an already-parsed decltype not followed by ::,
16740 it's a simple-type-specifier. */
16741 if (token
->type
== CPP_DECLTYPE
16742 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
16744 type
= saved_checks_value (token
->u
.tree_check_value
);
16747 cp_parser_set_decl_spec_type (decl_specs
, type
,
16749 /*type_definition_p=*/false);
16750 /* Remember that we are handling a decltype in order to
16751 implement the resolution of DR 1510 when the argument
16752 isn't instantiation dependent. */
16753 decl_specs
->decltype_p
= true;
16755 cp_lexer_consume_token (parser
->lexer
);
16759 /* If the type-specifier was for a built-in type, we're done. */
16762 /* Record the type. */
16764 && (token
->keyword
!= RID_SIGNED
16765 && token
->keyword
!= RID_UNSIGNED
16766 && token
->keyword
!= RID_SHORT
16767 && token
->keyword
!= RID_LONG
))
16768 cp_parser_set_decl_spec_type (decl_specs
,
16771 /*type_definition_p=*/false);
16773 decl_specs
->any_specifiers_p
= true;
16775 /* Consume the token. */
16776 cp_lexer_consume_token (parser
->lexer
);
16778 if (type
== error_mark_node
)
16779 return error_mark_node
;
16781 /* There is no valid C++ program where a non-template type is
16782 followed by a "<". That usually indicates that the user thought
16783 that the type was a template. */
16784 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
16787 return TYPE_NAME (type
);
16790 /* The type-specifier must be a user-defined type. */
16791 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
16796 /* Don't gobble tokens or issue error messages if this is an
16797 optional type-specifier. */
16798 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16799 cp_parser_parse_tentatively (parser
);
16801 token
= cp_lexer_peek_token (parser
->lexer
);
16803 /* Look for the optional `::' operator. */
16805 = (cp_parser_global_scope_opt (parser
,
16806 /*current_scope_valid_p=*/false)
16808 /* Look for the nested-name specifier. */
16810 = (cp_parser_nested_name_specifier_opt (parser
,
16811 /*typename_keyword_p=*/false,
16812 /*check_dependency_p=*/true,
16814 /*is_declaration=*/false)
16816 /* If we have seen a nested-name-specifier, and the next token
16817 is `template', then we are using the template-id production. */
16819 && cp_parser_optional_template_keyword (parser
))
16821 /* Look for the template-id. */
16822 type
= cp_parser_template_id (parser
,
16823 /*template_keyword_p=*/true,
16824 /*check_dependency_p=*/true,
16826 /*is_declaration=*/false);
16827 /* If the template-id did not name a type, we are out of
16829 if (TREE_CODE (type
) != TYPE_DECL
)
16831 cp_parser_error (parser
, "expected template-id for type");
16835 /* Otherwise, look for a type-name. */
16837 type
= cp_parser_type_name (parser
);
16838 /* Keep track of all name-lookups performed in class scopes. */
16842 && TREE_CODE (type
) == TYPE_DECL
16843 && identifier_p (DECL_NAME (type
)))
16844 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
16845 /* If it didn't work out, we don't have a TYPE. */
16846 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx1z
)
16847 && !cp_parser_parse_definitely (parser
))
16849 if (!type
&& cxx_dialect
>= cxx1z
)
16851 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
16852 cp_parser_parse_tentatively (parser
);
16854 cp_parser_global_scope_opt (parser
,
16855 /*current_scope_valid_p=*/false);
16856 cp_parser_nested_name_specifier_opt (parser
,
16857 /*typename_keyword_p=*/false,
16858 /*check_dependency_p=*/true,
16860 /*is_declaration=*/false);
16861 tree name
= cp_parser_identifier (parser
);
16862 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
16863 && parser
->scope
!= error_mark_node
)
16865 tree tmpl
= cp_parser_lookup_name (parser
, name
,
16867 /*is_template=*/false,
16868 /*is_namespace=*/false,
16869 /*check_dependency=*/true,
16870 /*ambiguous_decls=*/NULL
,
16872 if (tmpl
&& tmpl
!= error_mark_node
16873 && (DECL_CLASS_TEMPLATE_P (tmpl
)
16874 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
16875 type
= make_template_placeholder (tmpl
);
16878 type
= error_mark_node
;
16879 if (!cp_parser_simulate_error (parser
))
16880 cp_parser_name_lookup_error (parser
, name
, tmpl
,
16881 NLE_TYPE
, token
->location
);
16885 type
= error_mark_node
;
16887 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
16888 && !cp_parser_parse_definitely (parser
))
16891 if (type
&& decl_specs
)
16892 cp_parser_set_decl_spec_type (decl_specs
, type
,
16894 /*type_definition_p=*/false);
16897 /* If we didn't get a type-name, issue an error message. */
16898 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16900 cp_parser_error (parser
, "expected type-name");
16901 return error_mark_node
;
16904 if (type
&& type
!= error_mark_node
)
16906 /* See if TYPE is an Objective-C type, and if so, parse and
16907 accept any protocol references following it. Do this before
16908 the cp_parser_check_for_invalid_template_id() call, because
16909 Objective-C types can be followed by '<...>' which would
16910 enclose protocol names rather than template arguments, and so
16911 everything is fine. */
16912 if (c_dialect_objc () && !parser
->scope
16913 && (objc_is_id (type
) || objc_is_class_name (type
)))
16915 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16916 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
16918 /* Clobber the "unqualified" type previously entered into
16919 DECL_SPECS with the new, improved protocol-qualified version. */
16921 decl_specs
->type
= qual_type
;
16926 /* There is no valid C++ program where a non-template type is
16927 followed by a "<". That usually indicates that the user
16928 thought that the type was a template. */
16929 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
16937 /* Parse a type-name.
16943 simple-template-id [in c++0x]
16960 Returns a TYPE_DECL for the type. */
16963 cp_parser_type_name (cp_parser
* parser
)
16965 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
16970 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
16974 /* We can't know yet whether it is a class-name or not. */
16975 cp_parser_parse_tentatively (parser
);
16976 /* Try a class-name. */
16977 type_decl
= cp_parser_class_name (parser
,
16978 typename_keyword_p
,
16979 /*template_keyword_p=*/false,
16981 /*check_dependency_p=*/true,
16982 /*class_head_p=*/false,
16983 /*is_declaration=*/false);
16984 /* If it's not a class-name, keep looking. */
16985 if (!cp_parser_parse_definitely (parser
))
16987 if (cxx_dialect
< cxx11
)
16988 /* It must be a typedef-name or an enum-name. */
16989 return cp_parser_nonclass_name (parser
);
16991 cp_parser_parse_tentatively (parser
);
16992 /* It is either a simple-template-id representing an
16993 instantiation of an alias template... */
16994 type_decl
= cp_parser_template_id (parser
,
16995 /*template_keyword_p=*/false,
16996 /*check_dependency_p=*/true,
16998 /*is_declaration=*/false);
16999 /* Note that this must be an instantiation of an alias template
17000 because [temp.names]/6 says:
17002 A template-id that names an alias template specialization
17005 Whereas [temp.names]/7 says:
17007 A simple-template-id that names a class template
17008 specialization is a class-name.
17010 With concepts, this could also be a partial-concept-id that
17011 declares a non-type template parameter. */
17012 if (type_decl
!= NULL_TREE
17013 && TREE_CODE (type_decl
) == TYPE_DECL
17014 && TYPE_DECL_ALIAS_P (type_decl
))
17015 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17016 else if (is_constrained_parameter (type_decl
))
17017 /* Don't do anything. */ ;
17019 cp_parser_simulate_error (parser
);
17021 if (!cp_parser_parse_definitely (parser
))
17022 /* ... Or a typedef-name or an enum-name. */
17023 return cp_parser_nonclass_name (parser
);
17029 /* Check if DECL and ARGS can form a constrained-type-specifier.
17030 If ARGS is non-null, we try to form a concept check of the
17031 form DECL<?, ARGS> where ? is a wildcard that matches any
17032 kind of template argument. If ARGS is NULL, then we try to
17033 form a concept check of the form DECL<?>. */
17036 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17037 tree decl
, tree args
)
17039 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17041 /* If we a constrained-type-specifier cannot be deduced. */
17042 if (parser
->prevent_constrained_type_specifiers
)
17045 /* A constrained type specifier can only be found in an
17046 overload set or as a reference to a template declaration.
17048 FIXME: This might be masking a bug. It's possible that
17049 that the deduction below is causing template specializations
17050 to be formed with the wildcard as an argument. */
17051 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17054 /* Try to build a call expression that evaluates the
17055 concept. This can fail if the overload set refers
17056 only to non-templates. */
17057 tree placeholder
= build_nt (WILDCARD_DECL
);
17058 tree check
= build_concept_check (decl
, placeholder
, args
);
17059 if (check
== error_mark_node
)
17062 /* Deduce the checked constraint and the prototype parameter.
17064 FIXME: In certain cases, failure to deduce should be a
17065 diagnosable error. */
17068 if (!deduce_constrained_parameter (check
, conc
, proto
))
17071 /* In template parameter scope, this results in a constrained
17072 parameter. Return a descriptor of that parm. */
17073 if (processing_template_parmlist
)
17074 return build_constrained_parameter (conc
, proto
, args
);
17076 /* In a parameter-declaration-clause, constrained-type
17077 specifiers result in invented template parameters. */
17078 if (parser
->auto_is_implicit_function_template_parm_p
)
17080 tree x
= build_constrained_parameter (conc
, proto
, args
);
17081 return synthesize_implicit_template_parm (parser
, x
);
17085 /* Otherwise, we're in a context where the constrained
17086 type name is deduced and the constraint applies
17087 after deduction. */
17088 return make_constrained_auto (conc
, args
);
17094 /* If DECL refers to a concept, return a TYPE_DECL representing
17095 the result of using the constrained type specifier in the
17096 current context. DECL refers to a concept if
17098 - it is an overload set containing a function concept taking a single
17101 - it is a variable concept taking a single type argument. */
17104 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17107 && (TREE_CODE (decl
) == OVERLOAD
17108 || BASELINK_P (decl
)
17109 || variable_concept_p (decl
)))
17110 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17115 /* Check if DECL and ARGS form a partial-concept-id. If so,
17116 assign ID to the resulting constrained placeholder.
17118 Returns true if the partial-concept-id designates a placeholder
17119 and false otherwise. Note that *id is set to NULL_TREE in
17123 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17125 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17128 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17140 Returns a TYPE_DECL for the type. */
17143 cp_parser_nonclass_name (cp_parser
* parser
)
17148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17149 identifier
= cp_parser_identifier (parser
);
17150 if (identifier
== error_mark_node
)
17151 return error_mark_node
;
17153 /* Look up the type-name. */
17154 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17156 type_decl
= strip_using_decl (type_decl
);
17158 /* If we found an overload set, then it may refer to a concept-name. */
17159 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17162 if (TREE_CODE (type_decl
) != TYPE_DECL
17163 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17165 /* See if this is an Objective-C type. */
17166 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17167 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17169 type_decl
= TYPE_NAME (type
);
17172 /* Issue an error if we did not find a type-name. */
17173 if (TREE_CODE (type_decl
) != TYPE_DECL
17174 /* In Objective-C, we have the complication that class names are
17175 normally type names and start declarations (eg, the
17176 "NSObject" in "NSObject *object;"), but can be used in an
17177 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17178 is an expression. So, a classname followed by a dot is not a
17179 valid type-name. */
17180 || (objc_is_class_name (TREE_TYPE (type_decl
))
17181 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17183 if (!cp_parser_simulate_error (parser
))
17184 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17185 NLE_TYPE
, token
->location
);
17186 return error_mark_node
;
17188 /* Remember that the name was used in the definition of the
17189 current class so that we can check later to see if the
17190 meaning would have been different after the class was
17191 entirely defined. */
17192 else if (type_decl
!= error_mark_node
17194 maybe_note_name_used_in_class (identifier
, type_decl
);
17199 /* Parse an elaborated-type-specifier. Note that the grammar given
17200 here incorporates the resolution to DR68.
17202 elaborated-type-specifier:
17203 class-key :: [opt] nested-name-specifier [opt] identifier
17204 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17205 enum-key :: [opt] nested-name-specifier [opt] identifier
17206 typename :: [opt] nested-name-specifier identifier
17207 typename :: [opt] nested-name-specifier template [opt]
17212 elaborated-type-specifier:
17213 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17214 class-key attributes :: [opt] nested-name-specifier [opt]
17215 template [opt] template-id
17216 enum attributes :: [opt] nested-name-specifier [opt] identifier
17218 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17219 declared `friend'. If IS_DECLARATION is TRUE, then this
17220 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17221 something is being declared.
17223 Returns the TYPE specified. */
17226 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17228 bool is_declaration
)
17230 enum tag_types tag_type
;
17232 tree type
= NULL_TREE
;
17233 tree attributes
= NULL_TREE
;
17235 cp_token
*token
= NULL
;
17237 /* See if we're looking at the `enum' keyword. */
17238 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17240 /* Consume the `enum' token. */
17241 cp_lexer_consume_token (parser
->lexer
);
17242 /* Remember that it's an enumeration type. */
17243 tag_type
= enum_type
;
17244 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17245 enums) is used here. */
17246 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17247 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17249 pedwarn (input_location
, 0, "elaborated-type-specifier "
17250 "for a scoped enum must not use the %<%D%> keyword",
17251 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
17252 /* Consume the `struct' or `class' and parse it anyway. */
17253 cp_lexer_consume_token (parser
->lexer
);
17255 /* Parse the attributes. */
17256 attributes
= cp_parser_attributes_opt (parser
);
17258 /* Or, it might be `typename'. */
17259 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17262 /* Consume the `typename' token. */
17263 cp_lexer_consume_token (parser
->lexer
);
17264 /* Remember that it's a `typename' type. */
17265 tag_type
= typename_type
;
17267 /* Otherwise it must be a class-key. */
17270 tag_type
= cp_parser_class_key (parser
);
17271 if (tag_type
== none_type
)
17272 return error_mark_node
;
17273 /* Parse the attributes. */
17274 attributes
= cp_parser_attributes_opt (parser
);
17277 /* Look for the `::' operator. */
17278 globalscope
= cp_parser_global_scope_opt (parser
,
17279 /*current_scope_valid_p=*/false);
17280 /* Look for the nested-name-specifier. */
17281 tree nested_name_specifier
;
17282 if (tag_type
== typename_type
&& !globalscope
)
17284 nested_name_specifier
17285 = cp_parser_nested_name_specifier (parser
,
17286 /*typename_keyword_p=*/true,
17287 /*check_dependency_p=*/true,
17290 if (!nested_name_specifier
)
17291 return error_mark_node
;
17294 /* Even though `typename' is not present, the proposed resolution
17295 to Core Issue 180 says that in `class A<T>::B', `B' should be
17296 considered a type-name, even if `A<T>' is dependent. */
17297 nested_name_specifier
17298 = cp_parser_nested_name_specifier_opt (parser
,
17299 /*typename_keyword_p=*/true,
17300 /*check_dependency_p=*/true,
17303 /* For everything but enumeration types, consider a template-id.
17304 For an enumeration type, consider only a plain identifier. */
17305 if (tag_type
!= enum_type
)
17307 bool template_p
= false;
17310 /* Allow the `template' keyword. */
17311 template_p
= cp_parser_optional_template_keyword (parser
);
17312 /* If we didn't see `template', we don't know if there's a
17313 template-id or not. */
17315 cp_parser_parse_tentatively (parser
);
17316 /* Parse the template-id. */
17317 token
= cp_lexer_peek_token (parser
->lexer
);
17318 decl
= cp_parser_template_id (parser
, template_p
,
17319 /*check_dependency_p=*/true,
17322 /* If we didn't find a template-id, look for an ordinary
17324 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17326 /* We can get here when cp_parser_template_id, called by
17327 cp_parser_class_name with tag_type == none_type, succeeds
17328 and caches a BASELINK. Then, when called again here,
17329 instead of failing and returning an error_mark_node
17330 returns it (see template/typename17.C in C++11).
17331 ??? Could we diagnose this earlier? */
17332 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17334 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17335 type
= error_mark_node
;
17337 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17338 in effect, then we must assume that, upon instantiation, the
17339 template will correspond to a class. */
17340 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17341 && tag_type
== typename_type
)
17342 type
= make_typename_type (parser
->scope
, decl
,
17344 /*complain=*/tf_error
);
17345 /* If the `typename' keyword is in effect and DECL is not a type
17346 decl, then type is non existent. */
17347 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17349 else if (TREE_CODE (decl
) == TYPE_DECL
)
17351 type
= check_elaborated_type_specifier (tag_type
, decl
,
17352 /*allow_template_p=*/true);
17354 /* If the next token is a semicolon, this must be a specialization,
17355 instantiation, or friend declaration. Check the scope while we
17356 still know whether or not we had a nested-name-specifier. */
17357 if (type
!= error_mark_node
17358 && !nested_name_specifier
&& !is_friend
17359 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17360 check_unqualified_spec_or_inst (type
, token
->location
);
17362 else if (decl
== error_mark_node
)
17363 type
= error_mark_node
;
17368 token
= cp_lexer_peek_token (parser
->lexer
);
17369 identifier
= cp_parser_identifier (parser
);
17371 if (identifier
== error_mark_node
)
17373 parser
->scope
= NULL_TREE
;
17374 return error_mark_node
;
17377 /* For a `typename', we needn't call xref_tag. */
17378 if (tag_type
== typename_type
17379 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17380 return cp_parser_make_typename_type (parser
, identifier
,
17383 /* Template parameter lists apply only if we are not within a
17384 function parameter list. */
17385 bool template_parm_lists_apply
17386 = parser
->num_template_parameter_lists
;
17387 if (template_parm_lists_apply
)
17388 for (cp_binding_level
*s
= current_binding_level
;
17389 s
&& s
->kind
!= sk_template_parms
;
17390 s
= s
->level_chain
)
17391 if (s
->kind
== sk_function_parms
)
17392 template_parm_lists_apply
= false;
17394 /* Look up a qualified name in the usual way. */
17398 tree ambiguous_decls
;
17400 decl
= cp_parser_lookup_name (parser
, identifier
,
17402 /*is_template=*/false,
17403 /*is_namespace=*/false,
17404 /*check_dependency=*/true,
17408 /* If the lookup was ambiguous, an error will already have been
17410 if (ambiguous_decls
)
17411 return error_mark_node
;
17413 /* If we are parsing friend declaration, DECL may be a
17414 TEMPLATE_DECL tree node here. However, we need to check
17415 whether this TEMPLATE_DECL results in valid code. Consider
17416 the following example:
17419 template <class T> class C {};
17422 template <class T> friend class N::C; // #1, valid code
17424 template <class T> class Y {
17425 friend class N::C; // #2, invalid code
17428 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17429 name lookup of `N::C'. We see that friend declaration must
17430 be template for the code to be valid. Note that
17431 processing_template_decl does not work here since it is
17432 always 1 for the above two cases. */
17434 decl
= (cp_parser_maybe_treat_template_as_class
17435 (decl
, /*tag_name_p=*/is_friend
17436 && template_parm_lists_apply
));
17438 if (TREE_CODE (decl
) != TYPE_DECL
)
17440 cp_parser_diagnose_invalid_type_name (parser
,
17443 return error_mark_node
;
17446 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17448 bool allow_template
= (template_parm_lists_apply
17449 || DECL_SELF_REFERENCE_P (decl
));
17450 type
= check_elaborated_type_specifier (tag_type
, decl
,
17453 if (type
== error_mark_node
)
17454 return error_mark_node
;
17457 /* Forward declarations of nested types, such as
17462 are invalid unless all components preceding the final '::'
17463 are complete. If all enclosing types are complete, these
17464 declarations become merely pointless.
17466 Invalid forward declarations of nested types are errors
17467 caught elsewhere in parsing. Those that are pointless arrive
17470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17471 && !is_friend
&& !processing_explicit_instantiation
)
17472 warning (0, "declaration %qD does not declare anything", decl
);
17474 type
= TREE_TYPE (decl
);
17478 /* An elaborated-type-specifier sometimes introduces a new type and
17479 sometimes names an existing type. Normally, the rule is that it
17480 introduces a new type only if there is not an existing type of
17481 the same name already in scope. For example, given:
17484 void f() { struct S s; }
17486 the `struct S' in the body of `f' is the same `struct S' as in
17487 the global scope; the existing definition is used. However, if
17488 there were no global declaration, this would introduce a new
17489 local class named `S'.
17491 An exception to this rule applies to the following code:
17493 namespace N { struct S; }
17495 Here, the elaborated-type-specifier names a new type
17496 unconditionally; even if there is already an `S' in the
17497 containing scope this declaration names a new type.
17498 This exception only applies if the elaborated-type-specifier
17499 forms the complete declaration:
17503 A declaration consisting solely of `class-key identifier ;' is
17504 either a redeclaration of the name in the current scope or a
17505 forward declaration of the identifier as a class name. It
17506 introduces the name into the current scope.
17508 We are in this situation precisely when the next token is a `;'.
17510 An exception to the exception is that a `friend' declaration does
17511 *not* name a new type; i.e., given:
17513 struct S { friend struct T; };
17515 `T' is not a new type in the scope of `S'.
17517 Also, `new struct S' or `sizeof (struct S)' never results in the
17518 definition of a new type; a new type can only be declared in a
17519 declaration context. */
17525 /* Friends have special name lookup rules. */
17526 ts
= ts_within_enclosing_non_class
;
17527 else if (is_declaration
17528 && cp_lexer_next_token_is (parser
->lexer
,
17530 /* This is a `class-key identifier ;' */
17536 (template_parm_lists_apply
17537 && (cp_parser_next_token_starts_class_definition_p (parser
)
17538 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17539 /* An unqualified name was used to reference this type, so
17540 there were no qualifying templates. */
17541 if (template_parm_lists_apply
17542 && !cp_parser_check_template_parameters (parser
,
17543 /*num_templates=*/0,
17545 /*declarator=*/NULL
))
17546 return error_mark_node
;
17547 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17551 if (type
== error_mark_node
)
17552 return error_mark_node
;
17554 /* Allow attributes on forward declarations of classes. */
17557 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17558 warning (OPT_Wattributes
,
17559 "attributes ignored on uninstantiated type");
17560 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17561 && ! processing_explicit_instantiation
)
17562 warning (OPT_Wattributes
,
17563 "attributes ignored on template instantiation");
17564 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17565 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17567 warning (OPT_Wattributes
,
17568 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17571 if (tag_type
!= enum_type
)
17573 /* Indicate whether this class was declared as a `class' or as a
17575 if (CLASS_TYPE_P (type
))
17576 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17577 cp_parser_check_class_key (tag_type
, type
);
17580 /* A "<" cannot follow an elaborated type specifier. If that
17581 happens, the user was probably trying to form a template-id. */
17582 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17588 /* Parse an enum-specifier.
17591 enum-head { enumerator-list [opt] }
17592 enum-head { enumerator-list , } [C++0x]
17595 enum-key identifier [opt] enum-base [opt]
17596 enum-key nested-name-specifier identifier enum-base [opt]
17601 enum struct [C++0x]
17604 : type-specifier-seq
17606 opaque-enum-specifier:
17607 enum-key identifier enum-base [opt] ;
17610 enum-key attributes[opt] identifier [opt] enum-base [opt]
17611 { enumerator-list [opt] }attributes[opt]
17612 enum-key attributes[opt] identifier [opt] enum-base [opt]
17613 { enumerator-list, }attributes[opt] [C++0x]
17615 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17616 if the token stream isn't an enum-specifier after all. */
17619 cp_parser_enum_specifier (cp_parser
* parser
)
17622 tree type
= NULL_TREE
;
17624 tree nested_name_specifier
= NULL_TREE
;
17626 bool scoped_enum_p
= false;
17627 bool has_underlying_type
= false;
17628 bool nested_being_defined
= false;
17629 bool new_value_list
= false;
17630 bool is_new_type
= false;
17631 bool is_unnamed
= false;
17632 tree underlying_type
= NULL_TREE
;
17633 cp_token
*type_start_token
= NULL
;
17634 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17636 parser
->colon_corrects_to_scope_p
= false;
17638 /* Parse tentatively so that we can back up if we don't find a
17640 cp_parser_parse_tentatively (parser
);
17642 /* Caller guarantees that the current token is 'enum', an identifier
17643 possibly follows, and the token after that is an opening brace.
17644 If we don't have an identifier, fabricate an anonymous name for
17645 the enumeration being defined. */
17646 cp_lexer_consume_token (parser
->lexer
);
17648 /* Parse the "class" or "struct", which indicates a scoped
17649 enumeration type in C++0x. */
17650 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17651 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17653 if (cxx_dialect
< cxx11
)
17654 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17656 /* Consume the `struct' or `class' token. */
17657 cp_lexer_consume_token (parser
->lexer
);
17659 scoped_enum_p
= true;
17662 attributes
= cp_parser_attributes_opt (parser
);
17664 /* Clear the qualification. */
17665 parser
->scope
= NULL_TREE
;
17666 parser
->qualifying_scope
= NULL_TREE
;
17667 parser
->object_scope
= NULL_TREE
;
17669 /* Figure out in what scope the declaration is being placed. */
17670 prev_scope
= current_scope ();
17672 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17674 push_deferring_access_checks (dk_no_check
);
17675 nested_name_specifier
17676 = cp_parser_nested_name_specifier_opt (parser
,
17677 /*typename_keyword_p=*/true,
17678 /*check_dependency_p=*/false,
17680 /*is_declaration=*/false);
17682 if (nested_name_specifier
)
17686 identifier
= cp_parser_identifier (parser
);
17687 name
= cp_parser_lookup_name (parser
, identifier
,
17689 /*is_template=*/false,
17690 /*is_namespace=*/false,
17691 /*check_dependency=*/true,
17692 /*ambiguous_decls=*/NULL
,
17694 if (name
&& name
!= error_mark_node
)
17696 type
= TREE_TYPE (name
);
17697 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17699 /* Are template enums allowed in ISO? */
17700 if (template_parm_scope_p ())
17701 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17702 "%qD is an enumeration template", name
);
17703 /* ignore a typename reference, for it will be solved by name
17708 else if (nested_name_specifier
== error_mark_node
)
17709 /* We already issued an error. */;
17712 error_at (type_start_token
->location
,
17713 "%qD does not name an enumeration in %qT",
17714 identifier
, nested_name_specifier
);
17715 nested_name_specifier
= error_mark_node
;
17720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17721 identifier
= cp_parser_identifier (parser
);
17724 identifier
= make_anon_name ();
17727 error_at (type_start_token
->location
,
17728 "unnamed scoped enum is not allowed");
17731 pop_deferring_access_checks ();
17733 /* Check for the `:' that denotes a specified underlying type in C++0x.
17734 Note that a ':' could also indicate a bitfield width, however. */
17735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17737 cp_decl_specifier_seq type_specifiers
;
17739 /* Consume the `:'. */
17740 cp_lexer_consume_token (parser
->lexer
);
17742 /* Parse the type-specifier-seq. */
17743 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17744 /*is_trailing_return=*/false,
17747 /* At this point this is surely not elaborated type specifier. */
17748 if (!cp_parser_parse_definitely (parser
))
17751 if (cxx_dialect
< cxx11
)
17752 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17754 has_underlying_type
= true;
17756 /* If that didn't work, stop. */
17757 if (type_specifiers
.type
!= error_mark_node
)
17759 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
17760 /*initialized=*/0, NULL
);
17761 if (underlying_type
== error_mark_node
17762 || check_for_bare_parameter_packs (underlying_type
))
17763 underlying_type
= NULL_TREE
;
17767 /* Look for the `{' but don't consume it yet. */
17768 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17770 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
17772 cp_parser_error (parser
, "expected %<{%>");
17773 if (has_underlying_type
)
17779 /* An opaque-enum-specifier must have a ';' here. */
17780 if ((scoped_enum_p
|| underlying_type
)
17781 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17783 cp_parser_error (parser
, "expected %<;%> or %<{%>");
17784 if (has_underlying_type
)
17792 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
17795 if (nested_name_specifier
)
17797 if (CLASS_TYPE_P (nested_name_specifier
))
17799 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
17800 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
17801 push_scope (nested_name_specifier
);
17803 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17805 push_nested_namespace (nested_name_specifier
);
17809 /* Issue an error message if type-definitions are forbidden here. */
17810 if (!cp_parser_check_type_definition (parser
))
17811 type
= error_mark_node
;
17813 /* Create the new type. We do this before consuming the opening
17814 brace so the enum will be recorded as being on the line of its
17815 tag (or the 'enum' keyword, if there is no tag). */
17816 type
= start_enum (identifier
, type
, underlying_type
,
17817 attributes
, scoped_enum_p
, &is_new_type
);
17819 /* If the next token is not '{' it is an opaque-enum-specifier or an
17820 elaborated-type-specifier. */
17821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17823 timevar_push (TV_PARSE_ENUM
);
17824 if (nested_name_specifier
17825 && nested_name_specifier
!= error_mark_node
)
17827 /* The following catches invalid code such as:
17828 enum class S<int>::E { A, B, C }; */
17829 if (!processing_specialization
17830 && CLASS_TYPE_P (nested_name_specifier
)
17831 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
17832 error_at (type_start_token
->location
, "cannot add an enumerator "
17833 "list to a template instantiation");
17835 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
17837 error_at (type_start_token
->location
,
17838 "%<%T::%E%> has not been declared",
17839 TYPE_CONTEXT (nested_name_specifier
),
17840 nested_name_specifier
);
17841 type
= error_mark_node
;
17843 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
17844 && !CLASS_TYPE_P (nested_name_specifier
))
17846 error_at (type_start_token
->location
, "nested name specifier "
17847 "%qT for enum declaration does not name a class "
17848 "or namespace", nested_name_specifier
);
17849 type
= error_mark_node
;
17851 /* If that scope does not contain the scope in which the
17852 class was originally declared, the program is invalid. */
17853 else if (prev_scope
&& !is_ancestor (prev_scope
,
17854 nested_name_specifier
))
17856 if (at_namespace_scope_p ())
17857 error_at (type_start_token
->location
,
17858 "declaration of %qD in namespace %qD which does not "
17860 type
, prev_scope
, nested_name_specifier
);
17862 error_at (type_start_token
->location
,
17863 "declaration of %qD in %qD which does not "
17865 type
, prev_scope
, nested_name_specifier
);
17866 type
= error_mark_node
;
17868 /* If that scope is the scope where the declaration is being placed
17869 the program is invalid. */
17870 else if (CLASS_TYPE_P (nested_name_specifier
)
17871 && CLASS_TYPE_P (prev_scope
)
17872 && same_type_p (nested_name_specifier
, prev_scope
))
17874 permerror (type_start_token
->location
,
17875 "extra qualification not allowed");
17876 nested_name_specifier
= NULL_TREE
;
17881 begin_scope (sk_scoped_enum
, type
);
17883 /* Consume the opening brace. */
17884 cp_lexer_consume_token (parser
->lexer
);
17886 if (type
== error_mark_node
)
17887 ; /* Nothing to add */
17888 else if (OPAQUE_ENUM_P (type
)
17889 || (cxx_dialect
> cxx98
&& processing_specialization
))
17891 new_value_list
= true;
17892 SET_OPAQUE_ENUM_P (type
, false);
17893 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17897 error_at (type_start_token
->location
,
17898 "multiple definition of %q#T", type
);
17899 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
17900 "previous definition here");
17901 type
= error_mark_node
;
17904 if (type
== error_mark_node
)
17905 cp_parser_skip_to_end_of_block_or_statement (parser
);
17906 /* If the next token is not '}', then there are some enumerators. */
17907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17909 if (is_unnamed
&& !scoped_enum_p
)
17910 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17911 "ISO C++ forbids empty unnamed enum");
17914 cp_parser_enumerator_list (parser
, type
);
17916 /* Consume the final '}'. */
17917 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17921 timevar_pop (TV_PARSE_ENUM
);
17925 /* If a ';' follows, then it is an opaque-enum-specifier
17926 and additional restrictions apply. */
17927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17930 error_at (type_start_token
->location
,
17931 "opaque-enum-specifier without name");
17932 else if (nested_name_specifier
)
17933 error_at (type_start_token
->location
,
17934 "opaque-enum-specifier must use a simple identifier");
17938 /* Look for trailing attributes to apply to this enumeration, and
17939 apply them if appropriate. */
17940 if (cp_parser_allow_gnu_extensions_p (parser
))
17942 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
17943 cplus_decl_attributes (&type
,
17945 (int) ATTR_FLAG_TYPE_IN_PLACE
);
17948 /* Finish up the enumeration. */
17949 if (type
!= error_mark_node
)
17951 if (new_value_list
)
17952 finish_enum_value_list (type
);
17954 finish_enum (type
);
17957 if (nested_name_specifier
)
17959 if (CLASS_TYPE_P (nested_name_specifier
))
17961 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
17962 pop_scope (nested_name_specifier
);
17964 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17966 pop_nested_namespace (nested_name_specifier
);
17970 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17974 /* Parse an enumerator-list. The enumerators all have the indicated
17978 enumerator-definition
17979 enumerator-list , enumerator-definition */
17982 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
17986 /* Parse an enumerator-definition. */
17987 cp_parser_enumerator_definition (parser
, type
);
17989 /* If the next token is not a ',', we've reached the end of
17991 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17993 /* Otherwise, consume the `,' and keep going. */
17994 cp_lexer_consume_token (parser
->lexer
);
17995 /* If the next token is a `}', there is a trailing comma. */
17996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17998 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
17999 pedwarn (input_location
, OPT_Wpedantic
,
18000 "comma at end of enumerator list");
18006 /* Parse an enumerator-definition. The enumerator has the indicated
18009 enumerator-definition:
18011 enumerator = constant-expression
18018 enumerator-definition:
18019 enumerator attributes [opt]
18020 enumerator attributes [opt] = constant-expression */
18023 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18029 /* Save the input location because we are interested in the location
18030 of the identifier and not the location of the explicit value. */
18031 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18033 /* Look for the identifier. */
18034 identifier
= cp_parser_identifier (parser
);
18035 if (identifier
== error_mark_node
)
18038 /* Parse any specified attributes. */
18039 tree attrs
= cp_parser_attributes_opt (parser
);
18041 /* If the next token is an '=', then there is an explicit value. */
18042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18044 /* Consume the `=' token. */
18045 cp_lexer_consume_token (parser
->lexer
);
18046 /* Parse the value. */
18047 value
= cp_parser_constant_expression (parser
);
18052 /* If we are processing a template, make sure the initializer of the
18053 enumerator doesn't contain any bare template parameter pack. */
18054 if (check_for_bare_parameter_packs (value
))
18055 value
= error_mark_node
;
18057 /* Create the enumerator. */
18058 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18061 /* Parse a namespace-name.
18064 original-namespace-name
18067 Returns the NAMESPACE_DECL for the namespace. */
18070 cp_parser_namespace_name (cp_parser
* parser
)
18073 tree namespace_decl
;
18075 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18077 /* Get the name of the namespace. */
18078 identifier
= cp_parser_identifier (parser
);
18079 if (identifier
== error_mark_node
)
18080 return error_mark_node
;
18082 /* Look up the identifier in the currently active scope. Look only
18083 for namespaces, due to:
18085 [basic.lookup.udir]
18087 When looking up a namespace-name in a using-directive or alias
18088 definition, only namespace names are considered.
18092 [basic.lookup.qual]
18094 During the lookup of a name preceding the :: scope resolution
18095 operator, object, function, and enumerator names are ignored.
18097 (Note that cp_parser_qualifying_entity only calls this
18098 function if the token after the name is the scope resolution
18100 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18102 /*is_template=*/false,
18103 /*is_namespace=*/true,
18104 /*check_dependency=*/true,
18105 /*ambiguous_decls=*/NULL
,
18107 /* If it's not a namespace, issue an error. */
18108 if (namespace_decl
== error_mark_node
18109 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18111 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18112 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18113 cp_parser_error (parser
, "expected namespace-name");
18114 namespace_decl
= error_mark_node
;
18117 return namespace_decl
;
18120 /* Parse a namespace-definition.
18122 namespace-definition:
18123 named-namespace-definition
18124 unnamed-namespace-definition
18126 named-namespace-definition:
18127 original-namespace-definition
18128 extension-namespace-definition
18130 original-namespace-definition:
18131 namespace identifier { namespace-body }
18133 extension-namespace-definition:
18134 namespace original-namespace-name { namespace-body }
18136 unnamed-namespace-definition:
18137 namespace { namespace-body } */
18140 cp_parser_namespace_definition (cp_parser
* parser
)
18142 tree identifier
, attribs
;
18143 bool has_visibility
;
18146 int nested_definition_count
= 0;
18148 cp_ensure_no_omp_declare_simd (parser
);
18149 cp_ensure_no_oacc_routine (parser
);
18150 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
18152 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18154 cp_lexer_consume_token (parser
->lexer
);
18159 /* Look for the `namespace' keyword. */
18160 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18162 /* Parse any specified attributes before the identifier. */
18163 attribs
= cp_parser_attributes_opt (parser
);
18165 /* Get the name of the namespace. We do not attempt to distinguish
18166 between an original-namespace-definition and an
18167 extension-namespace-definition at this point. The semantic
18168 analysis routines are responsible for that. */
18169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18170 identifier
= cp_parser_identifier (parser
);
18172 identifier
= NULL_TREE
;
18174 /* Parse any specified attributes after the identifier. */
18175 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
18176 if (post_ident_attribs
)
18179 attribs
= chainon (attribs
, post_ident_attribs
);
18181 attribs
= post_ident_attribs
;
18184 /* Start the namespace. */
18185 bool ok
= push_namespace (identifier
);
18187 /* Parse any nested namespace definition. */
18188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18191 error_at (token
->location
, "a nested namespace definition cannot have attributes");
18192 if (cxx_dialect
< cxx1z
)
18193 pedwarn (input_location
, OPT_Wpedantic
,
18194 "nested namespace definitions only available with "
18195 "-std=c++1z or -std=gnu++1z");
18197 error_at (token
->location
, "a nested namespace definition cannot be inline");
18198 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18200 cp_lexer_consume_token (parser
->lexer
);
18201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18202 identifier
= cp_parser_identifier (parser
);
18205 cp_parser_error (parser
, "nested identifier required");
18208 if (push_namespace (identifier
))
18209 ++nested_definition_count
;
18213 /* Look for the `{' to validate starting the namespace. */
18214 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
18216 /* "inline namespace" is equivalent to a stub namespace definition
18217 followed by a strong using directive. */
18218 if (is_inline
&& ok
)
18220 tree name_space
= current_namespace
;
18221 /* Set up namespace association. */
18222 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
18223 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
18224 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
18225 /* Import the contents of the inline namespace. */
18227 do_using_directive (name_space
);
18228 push_namespace (identifier
);
18231 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18233 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18235 /* Parse the body of the namespace. */
18236 cp_parser_namespace_body (parser
);
18238 if (has_visibility
)
18239 pop_visibility (1);
18241 /* Finish the nested namespace definitions. */
18242 while (nested_definition_count
--)
18245 /* Finish the namespace. */
18248 /* Look for the final `}'. */
18249 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18252 /* Parse a namespace-body.
18255 declaration-seq [opt] */
18258 cp_parser_namespace_body (cp_parser
* parser
)
18260 cp_parser_declaration_seq_opt (parser
);
18263 /* Parse a namespace-alias-definition.
18265 namespace-alias-definition:
18266 namespace identifier = qualified-namespace-specifier ; */
18269 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18272 tree namespace_specifier
;
18274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18276 /* Look for the `namespace' keyword. */
18277 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18278 /* Look for the identifier. */
18279 identifier
= cp_parser_identifier (parser
);
18280 if (identifier
== error_mark_node
)
18282 /* Look for the `=' token. */
18283 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18284 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18286 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18287 /* Skip the definition. */
18288 cp_lexer_consume_token (parser
->lexer
);
18289 if (cp_parser_skip_to_closing_brace (parser
))
18290 cp_lexer_consume_token (parser
->lexer
);
18293 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18294 /* Look for the qualified-namespace-specifier. */
18295 namespace_specifier
18296 = cp_parser_qualified_namespace_specifier (parser
);
18297 /* Look for the `;' token. */
18298 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18300 /* Register the alias in the symbol table. */
18301 do_namespace_alias (identifier
, namespace_specifier
);
18304 /* Parse a qualified-namespace-specifier.
18306 qualified-namespace-specifier:
18307 :: [opt] nested-name-specifier [opt] namespace-name
18309 Returns a NAMESPACE_DECL corresponding to the specified
18313 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18315 /* Look for the optional `::'. */
18316 cp_parser_global_scope_opt (parser
,
18317 /*current_scope_valid_p=*/false);
18319 /* Look for the optional nested-name-specifier. */
18320 cp_parser_nested_name_specifier_opt (parser
,
18321 /*typename_keyword_p=*/false,
18322 /*check_dependency_p=*/true,
18324 /*is_declaration=*/true);
18326 return cp_parser_namespace_name (parser
);
18329 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18330 access declaration.
18333 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18334 using :: unqualified-id ;
18336 access-declaration:
18342 cp_parser_using_declaration (cp_parser
* parser
,
18343 bool access_declaration_p
)
18346 bool typename_p
= false;
18347 bool global_scope_p
;
18351 int oldcount
= errorcount
;
18352 cp_token
*diag_token
= NULL
;
18354 if (access_declaration_p
)
18356 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18357 cp_parser_parse_tentatively (parser
);
18361 /* Look for the `using' keyword. */
18362 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18365 /* Peek at the next token. */
18366 token
= cp_lexer_peek_token (parser
->lexer
);
18367 /* See if it's `typename'. */
18368 if (token
->keyword
== RID_TYPENAME
)
18370 /* Remember that we've seen it. */
18372 /* Consume the `typename' token. */
18373 cp_lexer_consume_token (parser
->lexer
);
18377 /* Look for the optional global scope qualification. */
18379 = (cp_parser_global_scope_opt (parser
,
18380 /*current_scope_valid_p=*/false)
18383 /* If we saw `typename', or didn't see `::', then there must be a
18384 nested-name-specifier present. */
18385 if (typename_p
|| !global_scope_p
)
18387 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18388 /*check_dependency_p=*/true,
18390 /*is_declaration=*/true);
18391 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18393 cp_parser_skip_to_end_of_block_or_statement (parser
);
18397 /* Otherwise, we could be in either of the two productions. In that
18398 case, treat the nested-name-specifier as optional. */
18400 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18401 /*typename_keyword_p=*/false,
18402 /*check_dependency_p=*/true,
18404 /*is_declaration=*/true);
18406 qscope
= global_namespace
;
18407 else if (UNSCOPED_ENUM_P (qscope
))
18408 qscope
= CP_TYPE_CONTEXT (qscope
);
18410 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18411 /* Something has already gone wrong; there's no need to parse
18412 further. Since an error has occurred, the return value of
18413 cp_parser_parse_definitely will be false, as required. */
18414 return cp_parser_parse_definitely (parser
);
18416 token
= cp_lexer_peek_token (parser
->lexer
);
18417 /* Parse the unqualified-id. */
18418 identifier
= cp_parser_unqualified_id (parser
,
18419 /*template_keyword_p=*/false,
18420 /*check_dependency_p=*/true,
18421 /*declarator_p=*/true,
18422 /*optional_p=*/false);
18424 if (access_declaration_p
)
18426 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18427 cp_parser_simulate_error (parser
);
18428 if (!cp_parser_parse_definitely (parser
))
18431 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18433 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18434 if (cxx_dialect
< cxx1z
18435 && !in_system_header_at (ell
->location
))
18436 pedwarn (ell
->location
, 0,
18437 "pack expansion in using-declaration only available "
18438 "with -std=c++1z or -std=gnu++1z");
18439 qscope
= make_pack_expansion (qscope
);
18442 /* The function we call to handle a using-declaration is different
18443 depending on what scope we are in. */
18444 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18446 else if (!identifier_p (identifier
)
18447 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18448 /* [namespace.udecl]
18450 A using declaration shall not name a template-id. */
18451 error_at (token
->location
,
18452 "a template-id may not appear in a using-declaration");
18455 if (at_class_scope_p ())
18457 /* Create the USING_DECL. */
18458 decl
= do_class_using_decl (qscope
, identifier
);
18460 if (decl
&& typename_p
)
18461 USING_DECL_TYPENAME_P (decl
) = 1;
18463 if (check_for_bare_parameter_packs (decl
))
18465 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18469 /* Add it to the list of members in this class. */
18470 finish_member_declaration (decl
);
18474 decl
= cp_parser_lookup_name_simple (parser
,
18477 if (decl
== error_mark_node
)
18478 cp_parser_name_lookup_error (parser
, identifier
,
18481 else if (check_for_bare_parameter_packs (decl
))
18483 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18486 else if (!at_namespace_scope_p ())
18487 do_local_using_decl (decl
, qscope
, identifier
);
18489 do_toplevel_using_decl (decl
, qscope
, identifier
);
18493 if (!access_declaration_p
18494 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18496 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18497 if (cxx_dialect
< cxx1z
)
18498 pedwarn (comma
->location
, 0,
18499 "comma-separated list in using-declaration only available "
18500 "with -std=c++1z or -std=gnu++1z");
18504 /* Look for the final `;'. */
18505 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18507 if (access_declaration_p
&& errorcount
== oldcount
)
18508 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18509 "access declarations are deprecated "
18510 "in favour of using-declarations; "
18511 "suggestion: add the %<using%> keyword");
18516 /* Parse an alias-declaration.
18519 using identifier attribute-specifier-seq [opt] = type-id */
18522 cp_parser_alias_declaration (cp_parser
* parser
)
18524 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18525 location_t id_location
;
18526 cp_declarator
*declarator
;
18527 cp_decl_specifier_seq decl_specs
;
18529 const char *saved_message
= NULL
;
18531 /* Look for the `using' keyword. */
18532 cp_token
*using_token
18533 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18534 if (using_token
== NULL
)
18535 return error_mark_node
;
18537 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18538 id
= cp_parser_identifier (parser
);
18539 if (id
== error_mark_node
)
18540 return error_mark_node
;
18542 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18543 attributes
= cp_parser_attributes_opt (parser
);
18544 if (attributes
== error_mark_node
)
18545 return error_mark_node
;
18547 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18549 if (cp_parser_error_occurred (parser
))
18550 return error_mark_node
;
18552 cp_parser_commit_to_tentative_parse (parser
);
18554 /* Now we are going to parse the type-id of the declaration. */
18559 "A type-specifier-seq shall not define a class or enumeration
18560 unless it appears in the type-id of an alias-declaration (7.1.3) that
18561 is not the declaration of a template-declaration."
18563 In other words, if we currently are in an alias template, the
18564 type-id should not define a type.
18566 So let's set parser->type_definition_forbidden_message in that
18567 case; cp_parser_check_type_definition (called by
18568 cp_parser_class_specifier) will then emit an error if a type is
18569 defined in the type-id. */
18570 if (parser
->num_template_parameter_lists
)
18572 saved_message
= parser
->type_definition_forbidden_message
;
18573 parser
->type_definition_forbidden_message
=
18574 G_("types may not be defined in alias template declarations");
18577 type
= cp_parser_type_id (parser
);
18579 /* Restore the error message if need be. */
18580 if (parser
->num_template_parameter_lists
)
18581 parser
->type_definition_forbidden_message
= saved_message
;
18583 if (type
== error_mark_node
18584 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18586 cp_parser_skip_to_end_of_block_or_statement (parser
);
18587 return error_mark_node
;
18590 /* A typedef-name can also be introduced by an alias-declaration. The
18591 identifier following the using keyword becomes a typedef-name. It has
18592 the same semantics as if it were introduced by the typedef
18593 specifier. In particular, it does not define a new type and it shall
18594 not appear in the type-id. */
18596 clear_decl_specs (&decl_specs
);
18597 decl_specs
.type
= type
;
18598 if (attributes
!= NULL_TREE
)
18600 decl_specs
.attributes
= attributes
;
18601 set_and_check_decl_spec_loc (&decl_specs
,
18605 set_and_check_decl_spec_loc (&decl_specs
,
18608 set_and_check_decl_spec_loc (&decl_specs
,
18612 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18613 declarator
->id_loc
= id_location
;
18615 member_p
= at_class_scope_p ();
18617 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18618 NULL_TREE
, attributes
);
18620 decl
= start_decl (declarator
, &decl_specs
, 0,
18621 attributes
, NULL_TREE
, &pushed_scope
);
18622 if (decl
== error_mark_node
)
18625 // Attach constraints to the alias declaration.
18626 if (flag_concepts
&& current_template_parms
)
18628 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18629 tree constr
= build_constraints (reqs
, NULL_TREE
);
18630 set_constraints (decl
, constr
);
18633 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18636 pop_scope (pushed_scope
);
18638 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18639 added into the symbol table; otherwise, return the TYPE_DECL. */
18640 if (DECL_LANG_SPECIFIC (decl
)
18641 && DECL_TEMPLATE_INFO (decl
)
18642 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18644 decl
= DECL_TI_TEMPLATE (decl
);
18646 check_member_template (decl
);
18652 /* Parse a using-directive.
18655 using namespace :: [opt] nested-name-specifier [opt]
18656 namespace-name ; */
18659 cp_parser_using_directive (cp_parser
* parser
)
18661 tree namespace_decl
;
18664 /* Look for the `using' keyword. */
18665 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18666 /* And the `namespace' keyword. */
18667 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18668 /* Look for the optional `::' operator. */
18669 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18670 /* And the optional nested-name-specifier. */
18671 cp_parser_nested_name_specifier_opt (parser
,
18672 /*typename_keyword_p=*/false,
18673 /*check_dependency_p=*/true,
18675 /*is_declaration=*/true);
18676 /* Get the namespace being used. */
18677 namespace_decl
= cp_parser_namespace_name (parser
);
18678 /* And any specified attributes. */
18679 attribs
= cp_parser_attributes_opt (parser
);
18680 /* Update the symbol table. */
18681 parse_using_directive (namespace_decl
, attribs
);
18682 /* Look for the final `;'. */
18683 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18686 /* Parse an asm-definition.
18689 asm ( string-literal ) ;
18694 asm volatile [opt] ( string-literal ) ;
18695 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18696 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18697 : asm-operand-list [opt] ) ;
18698 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18699 : asm-operand-list [opt]
18700 : asm-clobber-list [opt] ) ;
18701 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18702 : asm-clobber-list [opt]
18703 : asm-goto-list ) ; */
18706 cp_parser_asm_definition (cp_parser
* parser
)
18709 tree outputs
= NULL_TREE
;
18710 tree inputs
= NULL_TREE
;
18711 tree clobbers
= NULL_TREE
;
18712 tree labels
= NULL_TREE
;
18714 bool volatile_p
= false;
18715 bool extended_p
= false;
18716 bool invalid_inputs_p
= false;
18717 bool invalid_outputs_p
= false;
18718 bool goto_p
= false;
18719 required_token missing
= RT_NONE
;
18721 /* Look for the `asm' keyword. */
18722 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
18724 if (parser
->in_function_body
18725 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
18727 error ("%<asm%> in %<constexpr%> function");
18728 cp_function_chain
->invalid_constexpr
= true;
18731 /* See if the next token is `volatile'. */
18732 if (cp_parser_allow_gnu_extensions_p (parser
)
18733 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
18735 /* Remember that we saw the `volatile' keyword. */
18737 /* Consume the token. */
18738 cp_lexer_consume_token (parser
->lexer
);
18740 if (cp_parser_allow_gnu_extensions_p (parser
)
18741 && parser
->in_function_body
18742 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
18744 /* Remember that we saw the `goto' keyword. */
18746 /* Consume the token. */
18747 cp_lexer_consume_token (parser
->lexer
);
18749 /* Look for the opening `('. */
18750 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
18752 /* Look for the string. */
18753 string
= cp_parser_string_literal (parser
, false, false);
18754 if (string
== error_mark_node
)
18756 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18757 /*consume_paren=*/true);
18761 /* If we're allowing GNU extensions, check for the extended assembly
18762 syntax. Unfortunately, the `:' tokens need not be separated by
18763 a space in C, and so, for compatibility, we tolerate that here
18764 too. Doing that means that we have to treat the `::' operator as
18766 if (cp_parser_allow_gnu_extensions_p (parser
)
18767 && parser
->in_function_body
18768 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
18769 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
18771 bool inputs_p
= false;
18772 bool clobbers_p
= false;
18773 bool labels_p
= false;
18775 /* The extended syntax was used. */
18778 /* Look for outputs. */
18779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18781 /* Consume the `:'. */
18782 cp_lexer_consume_token (parser
->lexer
);
18783 /* Parse the output-operands. */
18784 if (cp_lexer_next_token_is_not (parser
->lexer
,
18786 && cp_lexer_next_token_is_not (parser
->lexer
,
18788 && cp_lexer_next_token_is_not (parser
->lexer
,
18792 outputs
= cp_parser_asm_operand_list (parser
);
18793 if (outputs
== error_mark_node
)
18794 invalid_outputs_p
= true;
18797 /* If the next token is `::', there are no outputs, and the
18798 next token is the beginning of the inputs. */
18799 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18800 /* The inputs are coming next. */
18803 /* Look for inputs. */
18805 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18807 /* Consume the `:' or `::'. */
18808 cp_lexer_consume_token (parser
->lexer
);
18809 /* Parse the output-operands. */
18810 if (cp_lexer_next_token_is_not (parser
->lexer
,
18812 && cp_lexer_next_token_is_not (parser
->lexer
,
18814 && cp_lexer_next_token_is_not (parser
->lexer
,
18817 inputs
= cp_parser_asm_operand_list (parser
);
18818 if (inputs
== error_mark_node
)
18819 invalid_inputs_p
= true;
18822 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18823 /* The clobbers are coming next. */
18826 /* Look for clobbers. */
18828 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18831 /* Consume the `:' or `::'. */
18832 cp_lexer_consume_token (parser
->lexer
);
18833 /* Parse the clobbers. */
18834 if (cp_lexer_next_token_is_not (parser
->lexer
,
18836 && cp_lexer_next_token_is_not (parser
->lexer
,
18838 clobbers
= cp_parser_asm_clobber_list (parser
);
18841 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18842 /* The labels are coming next. */
18845 /* Look for labels. */
18847 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
18850 /* Consume the `:' or `::'. */
18851 cp_lexer_consume_token (parser
->lexer
);
18852 /* Parse the labels. */
18853 labels
= cp_parser_asm_label_list (parser
);
18856 if (goto_p
&& !labels_p
)
18857 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
18860 missing
= RT_COLON_SCOPE
;
18862 /* Look for the closing `)'. */
18863 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
18864 missing
? missing
: RT_CLOSE_PAREN
))
18865 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18866 /*consume_paren=*/true);
18867 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18869 if (!invalid_inputs_p
&& !invalid_outputs_p
)
18871 /* Create the ASM_EXPR. */
18872 if (parser
->in_function_body
)
18874 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
18875 inputs
, clobbers
, labels
);
18876 /* If the extended syntax was not used, mark the ASM_EXPR. */
18879 tree temp
= asm_stmt
;
18880 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
18881 temp
= TREE_OPERAND (temp
, 0);
18883 ASM_INPUT_P (temp
) = 1;
18887 symtab
->finalize_toplevel_asm (string
);
18891 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18892 type that comes from the decl-specifier-seq. */
18895 strip_declarator_types (tree type
, cp_declarator
*declarator
)
18897 for (cp_declarator
*d
= declarator
; d
;)
18907 if (TYPE_PTRMEMFUNC_P (type
))
18908 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
18909 type
= TREE_TYPE (type
);
18917 /* Declarators [gram.dcl.decl] */
18919 /* Parse an init-declarator.
18922 declarator initializer [opt]
18927 declarator asm-specification [opt] attributes [opt] initializer [opt]
18929 function-definition:
18930 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18932 decl-specifier-seq [opt] declarator function-try-block
18936 function-definition:
18937 __extension__ function-definition
18941 function-definition:
18942 decl-specifier-seq [opt] declarator function-transaction-block
18944 The DECL_SPECIFIERS apply to this declarator. Returns a
18945 representation of the entity declared. If MEMBER_P is TRUE, then
18946 this declarator appears in a class scope. The new DECL created by
18947 this declarator is returned.
18949 The CHECKS are access checks that should be performed once we know
18950 what entity is being declared (and, therefore, what classes have
18953 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18954 for a function-definition here as well. If the declarator is a
18955 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18956 be TRUE upon return. By that point, the function-definition will
18957 have been completely parsed.
18959 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18962 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18963 parsed declaration if it is an uninitialized single declarator not followed
18964 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18965 if present, will not be consumed. If returned, this declarator will be
18966 created with SD_INITIALIZED but will not call cp_finish_decl.
18968 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18969 and there is an initializer, the pointed location_t is set to the
18970 location of the '=' or `(', or '{' in C++11 token introducing the
18974 cp_parser_init_declarator (cp_parser
* parser
,
18975 cp_decl_specifier_seq
*decl_specifiers
,
18976 vec
<deferred_access_check
, va_gc
> *checks
,
18977 bool function_definition_allowed_p
,
18979 int declares_class_or_enum
,
18980 bool* function_definition_p
,
18981 tree
* maybe_range_for_decl
,
18982 location_t
* init_loc
,
18985 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
18986 *attributes_start_token
= NULL
;
18987 cp_declarator
*declarator
;
18988 tree prefix_attributes
;
18989 tree attributes
= NULL
;
18990 tree asm_specification
;
18992 tree decl
= NULL_TREE
;
18994 int is_initialized
;
18995 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18996 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18998 enum cpp_ttype initialization_kind
;
18999 bool is_direct_init
= false;
19000 bool is_non_constant_init
;
19001 int ctor_dtor_or_conv_p
;
19002 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19003 tree pushed_scope
= NULL_TREE
;
19004 bool range_for_decl_p
= false;
19005 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19006 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19008 /* Gather the attributes that were provided with the
19009 decl-specifiers. */
19010 prefix_attributes
= decl_specifiers
->attributes
;
19012 /* Assume that this is not the declarator for a function
19014 if (function_definition_p
)
19015 *function_definition_p
= false;
19017 /* Default arguments are only permitted for function parameters. */
19018 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19019 parser
->default_arg_ok_p
= false;
19021 /* Defer access checks while parsing the declarator; we cannot know
19022 what names are accessible until we know what is being
19024 resume_deferring_access_checks ();
19026 token
= cp_lexer_peek_token (parser
->lexer
);
19028 cp_parser_declarator_kind cdk
= CP_PARSER_DECLARATOR_NAMED
;
19029 if (token
->type
== CPP_OPEN_PAREN
19030 && decl_specifiers
->type
19031 && is_auto (decl_specifiers
->type
)
19032 && CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers
->type
))
19034 // C++17 deduction guide.
19035 cdk
= CP_PARSER_DECLARATOR_ABSTRACT
;
19037 for (int i
= 0; i
< ds_last
; ++i
)
19038 if (i
!= ds_type_spec
19039 && decl_specifiers
->locations
[i
]
19040 && !cp_parser_simulate_error (parser
))
19041 error_at (decl_specifiers
->locations
[i
],
19042 "decl-specifier in declaration of deduction guide");
19045 /* Parse the declarator. */
19047 = cp_parser_declarator (parser
, cdk
,
19048 &ctor_dtor_or_conv_p
,
19049 /*parenthesized_p=*/NULL
,
19050 member_p
, friend_p
);
19051 /* Gather up the deferred checks. */
19052 stop_deferring_access_checks ();
19054 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19056 /* If the DECLARATOR was erroneous, there's no need to go
19058 if (declarator
== cp_error_declarator
)
19059 return error_mark_node
;
19061 if (cdk
== CP_PARSER_DECLARATOR_ABSTRACT
)
19063 gcc_assert (declarator
->kind
== cdk_function
19064 && !declarator
->declarator
);
19065 tree t
= CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers
->type
);
19066 declarator
->declarator
= make_id_declarator (NULL_TREE
, dguide_name (t
),
19068 declarator
->declarator
->id_loc
19069 = decl_specifiers
->locations
[ds_type_spec
];
19072 /* Check that the number of template-parameter-lists is OK. */
19073 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19075 return error_mark_node
;
19077 if (declares_class_or_enum
& 2)
19078 cp_parser_check_for_definition_in_return_type (declarator
,
19079 decl_specifiers
->type
,
19080 decl_specifiers
->locations
[ds_type_spec
]);
19082 /* Figure out what scope the entity declared by the DECLARATOR is
19083 located in. `grokdeclarator' sometimes changes the scope, so
19084 we compute it now. */
19085 scope
= get_scope_of_declarator (declarator
);
19087 /* Perform any lookups in the declared type which were thought to be
19088 dependent, but are not in the scope of the declarator. */
19089 decl_specifiers
->type
19090 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19092 /* If we're allowing GNU extensions, look for an
19093 asm-specification. */
19094 if (cp_parser_allow_gnu_extensions_p (parser
))
19096 /* Look for an asm-specification. */
19097 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19098 asm_specification
= cp_parser_asm_specification_opt (parser
);
19101 asm_specification
= NULL_TREE
;
19103 /* Look for attributes. */
19104 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19105 attributes
= cp_parser_attributes_opt (parser
);
19107 /* Peek at the next token. */
19108 token
= cp_lexer_peek_token (parser
->lexer
);
19110 bool bogus_implicit_tmpl
= false;
19112 if (function_declarator_p (declarator
))
19114 /* Check to see if the token indicates the start of a
19115 function-definition. */
19116 if (cp_parser_token_starts_function_definition_p (token
))
19118 if (!function_definition_allowed_p
)
19120 /* If a function-definition should not appear here, issue an
19122 cp_parser_error (parser
,
19123 "a function-definition is not allowed here");
19124 return error_mark_node
;
19127 location_t func_brace_location
19128 = cp_lexer_peek_token (parser
->lexer
)->location
;
19130 /* Neither attributes nor an asm-specification are allowed
19131 on a function-definition. */
19132 if (asm_specification
)
19133 error_at (asm_spec_start_token
->location
,
19134 "an asm-specification is not allowed "
19135 "on a function-definition");
19137 error_at (attributes_start_token
->location
,
19138 "attributes are not allowed "
19139 "on a function-definition");
19140 /* This is a function-definition. */
19141 *function_definition_p
= true;
19143 /* Parse the function definition. */
19145 decl
= cp_parser_save_member_function_body (parser
,
19148 prefix_attributes
);
19151 (cp_parser_function_definition_from_specifiers_and_declarator
19152 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19154 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19156 /* This is where the prologue starts... */
19157 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19158 = func_brace_location
;
19164 else if (parser
->fully_implicit_function_template_p
)
19166 /* A non-template declaration involving a function parameter list
19167 containing an implicit template parameter will be made into a
19168 template. If the resulting declaration is not going to be an
19169 actual function then finish the template scope here to prevent it.
19170 An error message will be issued once we have a decl to talk about.
19172 FIXME probably we should do type deduction rather than create an
19173 implicit template, but the standard currently doesn't allow it. */
19174 bogus_implicit_tmpl
= true;
19175 finish_fully_implicit_template (parser
, NULL_TREE
);
19180 Only in function declarations for constructors, destructors, and
19181 type conversions can the decl-specifier-seq be omitted.
19183 We explicitly postpone this check past the point where we handle
19184 function-definitions because we tolerate function-definitions
19185 that are missing their return types in some modes. */
19186 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19188 cp_parser_error (parser
,
19189 "expected constructor, destructor, or type conversion");
19190 return error_mark_node
;
19193 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19194 if (token
->type
== CPP_EQ
19195 || token
->type
== CPP_OPEN_PAREN
19196 || token
->type
== CPP_OPEN_BRACE
)
19198 is_initialized
= SD_INITIALIZED
;
19199 initialization_kind
= token
->type
;
19200 if (maybe_range_for_decl
)
19201 *maybe_range_for_decl
= error_mark_node
;
19202 tmp_init_loc
= token
->location
;
19203 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19204 *init_loc
= tmp_init_loc
;
19206 if (token
->type
== CPP_EQ
19207 && function_declarator_p (declarator
))
19209 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19210 if (t2
->keyword
== RID_DEFAULT
)
19211 is_initialized
= SD_DEFAULTED
;
19212 else if (t2
->keyword
== RID_DELETE
)
19213 is_initialized
= SD_DELETED
;
19218 /* If the init-declarator isn't initialized and isn't followed by a
19219 `,' or `;', it's not a valid init-declarator. */
19220 if (token
->type
!= CPP_COMMA
19221 && token
->type
!= CPP_SEMICOLON
)
19223 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19224 range_for_decl_p
= true;
19227 if (!maybe_range_for_decl
)
19228 cp_parser_error (parser
, "expected initializer");
19229 return error_mark_node
;
19232 is_initialized
= SD_UNINITIALIZED
;
19233 initialization_kind
= CPP_EOF
;
19236 /* Because start_decl has side-effects, we should only call it if we
19237 know we're going ahead. By this point, we know that we cannot
19238 possibly be looking at any other construct. */
19239 cp_parser_commit_to_tentative_parse (parser
);
19241 /* Enter the newly declared entry in the symbol table. If we're
19242 processing a declaration in a class-specifier, we wait until
19243 after processing the initializer. */
19246 if (parser
->in_unbraced_linkage_specification_p
)
19247 decl_specifiers
->storage_class
= sc_extern
;
19248 decl
= start_decl (declarator
, decl_specifiers
,
19249 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19250 attributes
, prefix_attributes
, &pushed_scope
);
19251 cp_finalize_omp_declare_simd (parser
, decl
);
19252 cp_finalize_oacc_routine (parser
, decl
, false);
19253 /* Adjust location of decl if declarator->id_loc is more appropriate:
19254 set, and decl wasn't merged with another decl, in which case its
19255 location would be different from input_location, and more accurate. */
19257 && declarator
->id_loc
!= UNKNOWN_LOCATION
19258 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19259 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19262 /* Enter the SCOPE. That way unqualified names appearing in the
19263 initializer will be looked up in SCOPE. */
19264 pushed_scope
= push_scope (scope
);
19266 /* Perform deferred access control checks, now that we know in which
19267 SCOPE the declared entity resides. */
19268 if (!member_p
&& decl
)
19270 tree saved_current_function_decl
= NULL_TREE
;
19272 /* If the entity being declared is a function, pretend that we
19273 are in its scope. If it is a `friend', it may have access to
19274 things that would not otherwise be accessible. */
19275 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19277 saved_current_function_decl
= current_function_decl
;
19278 current_function_decl
= decl
;
19281 /* Perform access checks for template parameters. */
19282 cp_parser_perform_template_parameter_access_checks (checks
);
19284 /* Perform the access control checks for the declarator and the
19285 decl-specifiers. */
19286 perform_deferred_access_checks (tf_warning_or_error
);
19288 /* Restore the saved value. */
19289 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19290 current_function_decl
= saved_current_function_decl
;
19293 /* Parse the initializer. */
19294 initializer
= NULL_TREE
;
19295 is_direct_init
= false;
19296 is_non_constant_init
= true;
19297 if (is_initialized
)
19299 if (function_declarator_p (declarator
))
19301 if (initialization_kind
== CPP_EQ
)
19302 initializer
= cp_parser_pure_specifier (parser
);
19305 /* If the declaration was erroneous, we don't really
19306 know what the user intended, so just silently
19307 consume the initializer. */
19308 if (decl
!= error_mark_node
)
19309 error_at (tmp_init_loc
, "initializer provided for function");
19310 cp_parser_skip_to_closing_parenthesis (parser
,
19311 /*recovering=*/true,
19312 /*or_comma=*/false,
19313 /*consume_paren=*/true);
19318 /* We want to record the extra mangling scope for in-class
19319 initializers of class members and initializers of static data
19320 member templates. The former involves deferring
19321 parsing of the initializer until end of class as with default
19322 arguments. So right here we only handle the latter. */
19323 if (!member_p
&& processing_template_decl
)
19324 start_lambda_scope (decl
);
19325 initializer
= cp_parser_initializer (parser
,
19327 &is_non_constant_init
);
19328 if (!member_p
&& processing_template_decl
)
19329 finish_lambda_scope ();
19330 if (initializer
== error_mark_node
)
19331 cp_parser_skip_to_end_of_statement (parser
);
19335 /* The old parser allows attributes to appear after a parenthesized
19336 initializer. Mark Mitchell proposed removing this functionality
19337 on the GCC mailing lists on 2002-08-13. This parser accepts the
19338 attributes -- but ignores them. */
19339 if (cp_parser_allow_gnu_extensions_p (parser
)
19340 && initialization_kind
== CPP_OPEN_PAREN
)
19341 if (cp_parser_attributes_opt (parser
))
19342 warning (OPT_Wattributes
,
19343 "attributes after parenthesized initializer ignored");
19345 /* And now complain about a non-function implicit template. */
19346 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19347 error_at (DECL_SOURCE_LOCATION (decl
),
19348 "non-function %qD declared as implicit template", decl
);
19350 /* For an in-class declaration, use `grokfield' to create the
19356 pop_scope (pushed_scope
);
19357 pushed_scope
= NULL_TREE
;
19359 decl
= grokfield (declarator
, decl_specifiers
,
19360 initializer
, !is_non_constant_init
,
19361 /*asmspec=*/NULL_TREE
,
19362 chainon (attributes
, prefix_attributes
));
19363 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19364 cp_parser_save_default_args (parser
, decl
);
19365 cp_finalize_omp_declare_simd (parser
, decl
);
19366 cp_finalize_oacc_routine (parser
, decl
, false);
19369 /* Finish processing the declaration. But, skip member
19371 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19373 cp_finish_decl (decl
,
19374 initializer
, !is_non_constant_init
,
19376 /* If the initializer is in parentheses, then this is
19377 a direct-initialization, which means that an
19378 `explicit' constructor is OK. Otherwise, an
19379 `explicit' constructor cannot be used. */
19380 ((is_direct_init
|| !is_initialized
)
19381 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19383 else if ((cxx_dialect
!= cxx98
) && friend_p
19384 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19385 /* Core issue #226 (C++0x only): A default template-argument
19386 shall not be specified in a friend class template
19388 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19389 /*is_partial=*/false, /*is_friend_decl=*/1);
19391 if (!friend_p
&& pushed_scope
)
19392 pop_scope (pushed_scope
);
19394 if (function_declarator_p (declarator
)
19395 && parser
->fully_implicit_function_template_p
)
19398 decl
= finish_fully_implicit_template (parser
, decl
);
19400 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19403 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19404 && type_uses_auto (decl_specifiers
->type
))
19405 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19410 /* Parse a declarator.
19414 ptr-operator declarator
19416 abstract-declarator:
19417 ptr-operator abstract-declarator [opt]
19418 direct-abstract-declarator
19423 attributes [opt] direct-declarator
19424 attributes [opt] ptr-operator declarator
19426 abstract-declarator:
19427 attributes [opt] ptr-operator abstract-declarator [opt]
19428 attributes [opt] direct-abstract-declarator
19430 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19431 detect constructor, destructor or conversion operators. It is set
19432 to -1 if the declarator is a name, and +1 if it is a
19433 function. Otherwise it is set to zero. Usually you just want to
19434 test for >0, but internally the negative value is used.
19436 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19437 a decl-specifier-seq unless it declares a constructor, destructor,
19438 or conversion. It might seem that we could check this condition in
19439 semantic analysis, rather than parsing, but that makes it difficult
19440 to handle something like `f()'. We want to notice that there are
19441 no decl-specifiers, and therefore realize that this is an
19442 expression, not a declaration.)
19444 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19445 the declarator is a direct-declarator of the form "(...)".
19447 MEMBER_P is true iff this declarator is a member-declarator.
19449 FRIEND_P is true iff this declarator is a friend. */
19451 static cp_declarator
*
19452 cp_parser_declarator (cp_parser
* parser
,
19453 cp_parser_declarator_kind dcl_kind
,
19454 int* ctor_dtor_or_conv_p
,
19455 bool* parenthesized_p
,
19456 bool member_p
, bool friend_p
)
19458 cp_declarator
*declarator
;
19459 enum tree_code code
;
19460 cp_cv_quals cv_quals
;
19462 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19464 /* Assume this is not a constructor, destructor, or type-conversion
19466 if (ctor_dtor_or_conv_p
)
19467 *ctor_dtor_or_conv_p
= 0;
19469 if (cp_parser_allow_gnu_extensions_p (parser
))
19470 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19472 /* Check for the ptr-operator production. */
19473 cp_parser_parse_tentatively (parser
);
19474 /* Parse the ptr-operator. */
19475 code
= cp_parser_ptr_operator (parser
,
19480 /* If that worked, then we have a ptr-operator. */
19481 if (cp_parser_parse_definitely (parser
))
19483 /* If a ptr-operator was found, then this declarator was not
19485 if (parenthesized_p
)
19486 *parenthesized_p
= true;
19487 /* The dependent declarator is optional if we are parsing an
19488 abstract-declarator. */
19489 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19490 cp_parser_parse_tentatively (parser
);
19492 /* Parse the dependent declarator. */
19493 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19494 /*ctor_dtor_or_conv_p=*/NULL
,
19495 /*parenthesized_p=*/NULL
,
19496 /*member_p=*/false,
19499 /* If we are parsing an abstract-declarator, we must handle the
19500 case where the dependent declarator is absent. */
19501 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19502 && !cp_parser_parse_definitely (parser
))
19505 declarator
= cp_parser_make_indirect_declarator
19506 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19508 /* Everything else is a direct-declarator. */
19511 if (parenthesized_p
)
19512 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19514 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19515 ctor_dtor_or_conv_p
,
19516 member_p
, friend_p
);
19519 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19520 declarator
->attributes
= gnu_attributes
;
19524 /* Parse a direct-declarator or direct-abstract-declarator.
19528 direct-declarator ( parameter-declaration-clause )
19529 cv-qualifier-seq [opt]
19530 ref-qualifier [opt]
19531 exception-specification [opt]
19532 direct-declarator [ constant-expression [opt] ]
19535 direct-abstract-declarator:
19536 direct-abstract-declarator [opt]
19537 ( parameter-declaration-clause )
19538 cv-qualifier-seq [opt]
19539 ref-qualifier [opt]
19540 exception-specification [opt]
19541 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19542 ( abstract-declarator )
19544 Returns a representation of the declarator. DCL_KIND is
19545 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19546 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19547 we are parsing a direct-declarator. It is
19548 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19549 of ambiguity we prefer an abstract declarator, as per
19550 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19551 as for cp_parser_declarator. */
19553 static cp_declarator
*
19554 cp_parser_direct_declarator (cp_parser
* parser
,
19555 cp_parser_declarator_kind dcl_kind
,
19556 int* ctor_dtor_or_conv_p
,
19557 bool member_p
, bool friend_p
)
19560 cp_declarator
*declarator
= NULL
;
19561 tree scope
= NULL_TREE
;
19562 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19563 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19565 tree pushed_scope
= NULL_TREE
;
19569 /* Peek at the next token. */
19570 token
= cp_lexer_peek_token (parser
->lexer
);
19571 if (token
->type
== CPP_OPEN_PAREN
)
19573 /* This is either a parameter-declaration-clause, or a
19574 parenthesized declarator. When we know we are parsing a
19575 named declarator, it must be a parenthesized declarator
19576 if FIRST is true. For instance, `(int)' is a
19577 parameter-declaration-clause, with an omitted
19578 direct-abstract-declarator. But `((*))', is a
19579 parenthesized abstract declarator. Finally, when T is a
19580 template parameter `(T)' is a
19581 parameter-declaration-clause, and not a parenthesized
19584 We first try and parse a parameter-declaration-clause,
19585 and then try a nested declarator (if FIRST is true).
19587 It is not an error for it not to be a
19588 parameter-declaration-clause, even when FIRST is
19594 The first is the declaration of a function while the
19595 second is the definition of a variable, including its
19598 Having seen only the parenthesis, we cannot know which of
19599 these two alternatives should be selected. Even more
19600 complex are examples like:
19605 The former is a function-declaration; the latter is a
19606 variable initialization.
19608 Thus again, we try a parameter-declaration-clause, and if
19609 that fails, we back out and return. */
19611 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19614 bool is_declarator
= false;
19616 /* In a member-declarator, the only valid interpretation
19617 of a parenthesis is the start of a
19618 parameter-declaration-clause. (It is invalid to
19619 initialize a static data member with a parenthesized
19620 initializer; only the "=" form of initialization is
19623 cp_parser_parse_tentatively (parser
);
19625 /* Consume the `('. */
19626 cp_lexer_consume_token (parser
->lexer
);
19629 /* If this is going to be an abstract declarator, we're
19630 in a declarator and we can't have default args. */
19631 parser
->default_arg_ok_p
= false;
19632 parser
->in_declarator_p
= true;
19635 begin_scope (sk_function_parms
, NULL_TREE
);
19637 /* Parse the parameter-declaration-clause. */
19638 params
= cp_parser_parameter_declaration_clause (parser
);
19640 /* Consume the `)'. */
19641 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19643 /* If all went well, parse the cv-qualifier-seq,
19644 ref-qualifier and the exception-specification. */
19645 if (member_p
|| cp_parser_parse_definitely (parser
))
19647 cp_cv_quals cv_quals
;
19648 cp_virt_specifiers virt_specifiers
;
19649 cp_ref_qualifier ref_qual
;
19650 tree exception_specification
;
19653 bool memfn
= (member_p
|| (pushed_scope
19654 && CLASS_TYPE_P (pushed_scope
)));
19656 is_declarator
= true;
19658 if (ctor_dtor_or_conv_p
)
19659 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19662 /* Parse the cv-qualifier-seq. */
19663 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19664 /* Parse the ref-qualifier. */
19665 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19666 /* Parse the tx-qualifier. */
19667 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19668 /* And the exception-specification. */
19669 exception_specification
19670 = cp_parser_exception_specification_opt (parser
);
19672 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19674 /* In here, we handle cases where attribute is used after
19675 the function declaration. For example:
19676 void func (int x) __attribute__((vector(..))); */
19677 tree gnu_attrs
= NULL_TREE
;
19679 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
19681 cp_parser_parse_tentatively (parser
);
19682 tree attr
= cp_parser_gnu_attributes_opt (parser
);
19683 if (cp_lexer_next_token_is_not (parser
->lexer
,
19685 && cp_lexer_next_token_is_not (parser
->lexer
,
19687 cp_parser_abort_tentative_parse (parser
);
19688 else if (!cp_parser_parse_definitely (parser
))
19693 tree requires_clause
= NULL_TREE
;
19694 late_return
= (cp_parser_late_return_type_opt
19695 (parser
, declarator
, requires_clause
,
19696 memfn
? cv_quals
: -1));
19698 /* Parse the virt-specifier-seq. */
19699 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19701 /* Create the function-declarator. */
19702 declarator
= make_call_declarator (declarator
,
19708 exception_specification
,
19711 declarator
->std_attributes
= attrs
;
19712 declarator
->attributes
= gnu_attrs
;
19713 /* Any subsequent parameter lists are to do with
19714 return type, so are not those of the declared
19716 parser
->default_arg_ok_p
= false;
19719 /* Remove the function parms from scope. */
19720 pop_bindings_and_leave_scope ();
19723 /* Repeat the main loop. */
19727 /* If this is the first, we can try a parenthesized
19731 bool saved_in_type_id_in_expr_p
;
19733 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19734 parser
->in_declarator_p
= saved_in_declarator_p
;
19736 /* Consume the `('. */
19737 cp_lexer_consume_token (parser
->lexer
);
19738 /* Parse the nested declarator. */
19739 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19740 parser
->in_type_id_in_expr_p
= true;
19742 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
19743 /*parenthesized_p=*/NULL
,
19744 member_p
, friend_p
);
19745 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19747 /* Expect a `)'. */
19748 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
19749 declarator
= cp_error_declarator
;
19750 if (declarator
== cp_error_declarator
)
19753 goto handle_declarator
;
19755 /* Otherwise, we must be done. */
19759 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19760 && token
->type
== CPP_OPEN_SQUARE
19761 && !cp_next_tokens_can_be_attribute_p (parser
))
19763 /* Parse an array-declarator. */
19764 tree bounds
, attrs
;
19766 if (ctor_dtor_or_conv_p
)
19767 *ctor_dtor_or_conv_p
= 0;
19770 parser
->default_arg_ok_p
= false;
19771 parser
->in_declarator_p
= true;
19772 /* Consume the `['. */
19773 cp_lexer_consume_token (parser
->lexer
);
19774 /* Peek at the next token. */
19775 token
= cp_lexer_peek_token (parser
->lexer
);
19776 /* If the next token is `]', then there is no
19777 constant-expression. */
19778 if (token
->type
!= CPP_CLOSE_SQUARE
)
19780 bool non_constant_p
;
19782 = cp_parser_constant_expression (parser
,
19783 /*allow_non_constant=*/true,
19785 if (!non_constant_p
)
19787 else if (error_operand_p (bounds
))
19788 /* Already gave an error. */;
19789 else if (!parser
->in_function_body
19790 || current_binding_level
->kind
== sk_function_parms
)
19792 /* Normally, the array bound must be an integral constant
19793 expression. However, as an extension, we allow VLAs
19794 in function scopes as long as they aren't part of a
19795 parameter declaration. */
19796 cp_parser_error (parser
,
19797 "array bound is not an integer constant");
19798 bounds
= error_mark_node
;
19800 else if (processing_template_decl
19801 && !type_dependent_expression_p (bounds
))
19803 /* Remember this wasn't a constant-expression. */
19804 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
19805 TREE_SIDE_EFFECTS (bounds
) = 1;
19809 bounds
= NULL_TREE
;
19810 /* Look for the closing `]'. */
19811 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
19813 declarator
= cp_error_declarator
;
19817 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19818 declarator
= make_array_declarator (declarator
, bounds
);
19819 declarator
->std_attributes
= attrs
;
19821 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
19824 tree qualifying_scope
;
19825 tree unqualified_name
;
19827 special_function_kind sfk
;
19829 bool pack_expansion_p
= false;
19830 cp_token
*declarator_id_start_token
;
19832 /* Parse a declarator-id */
19833 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
19836 cp_parser_parse_tentatively (parser
);
19838 /* If we see an ellipsis, we should be looking at a
19840 if (token
->type
== CPP_ELLIPSIS
)
19842 /* Consume the `...' */
19843 cp_lexer_consume_token (parser
->lexer
);
19845 pack_expansion_p
= true;
19849 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
19851 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
19852 qualifying_scope
= parser
->scope
;
19857 if (!unqualified_name
&& pack_expansion_p
)
19859 /* Check whether an error occurred. */
19860 okay
= !cp_parser_error_occurred (parser
);
19862 /* We already consumed the ellipsis to mark a
19863 parameter pack, but we have no way to report it,
19864 so abort the tentative parse. We will be exiting
19865 immediately anyway. */
19866 cp_parser_abort_tentative_parse (parser
);
19869 okay
= cp_parser_parse_definitely (parser
);
19872 unqualified_name
= error_mark_node
;
19873 else if (unqualified_name
19874 && (qualifying_scope
19875 || (!identifier_p (unqualified_name
))))
19877 cp_parser_error (parser
, "expected unqualified-id");
19878 unqualified_name
= error_mark_node
;
19882 if (!unqualified_name
)
19884 if (unqualified_name
== error_mark_node
)
19886 declarator
= cp_error_declarator
;
19887 pack_expansion_p
= false;
19888 declarator
->parameter_pack_p
= false;
19892 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19894 if (qualifying_scope
&& at_namespace_scope_p ()
19895 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
19897 /* In the declaration of a member of a template class
19898 outside of the class itself, the SCOPE will sometimes
19899 be a TYPENAME_TYPE. For example, given:
19901 template <typename T>
19902 int S<T>::R::i = 3;
19904 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19905 this context, we must resolve S<T>::R to an ordinary
19906 type, rather than a typename type.
19908 The reason we normally avoid resolving TYPENAME_TYPEs
19909 is that a specialization of `S' might render
19910 `S<T>::R' not a type. However, if `S' is
19911 specialized, then this `i' will not be used, so there
19912 is no harm in resolving the types here. */
19915 /* Resolve the TYPENAME_TYPE. */
19916 type
= resolve_typename_type (qualifying_scope
,
19917 /*only_current_p=*/false);
19918 /* If that failed, the declarator is invalid. */
19919 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19921 if (typedef_variant_p (type
))
19922 error_at (declarator_id_start_token
->location
,
19923 "cannot define member of dependent typedef "
19926 error_at (declarator_id_start_token
->location
,
19927 "%<%T::%E%> is not a type",
19928 TYPE_CONTEXT (qualifying_scope
),
19929 TYPE_IDENTIFIER (qualifying_scope
));
19931 qualifying_scope
= type
;
19936 if (unqualified_name
)
19940 if (qualifying_scope
19941 && CLASS_TYPE_P (qualifying_scope
))
19942 class_type
= qualifying_scope
;
19944 class_type
= current_class_type
;
19946 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
19948 tree name_type
= TREE_TYPE (unqualified_name
);
19949 if (class_type
&& same_type_p (name_type
, class_type
))
19951 if (qualifying_scope
19952 && CLASSTYPE_USE_TEMPLATE (name_type
))
19954 error_at (declarator_id_start_token
->location
,
19955 "invalid use of constructor as a template");
19956 inform (declarator_id_start_token
->location
,
19957 "use %<%T::%D%> instead of %<%T::%D%> to "
19958 "name the constructor in a qualified name",
19960 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
19961 class_type
, name_type
);
19962 declarator
= cp_error_declarator
;
19966 unqualified_name
= constructor_name (class_type
);
19970 /* We do not attempt to print the declarator
19971 here because we do not have enough
19972 information about its original syntactic
19974 cp_parser_error (parser
, "invalid declarator");
19975 declarator
= cp_error_declarator
;
19982 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
19983 sfk
= sfk_destructor
;
19984 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
19985 sfk
= sfk_conversion
;
19986 else if (/* There's no way to declare a constructor
19987 for an unnamed type, even if the type
19988 got a name for linkage purposes. */
19989 !TYPE_WAS_UNNAMED (class_type
)
19990 /* Handle correctly (c++/19200):
20004 friend void N::S();
20007 && class_type
!= qualifying_scope
)
20008 && constructor_name_p (unqualified_name
,
20011 unqualified_name
= constructor_name (class_type
);
20012 sfk
= sfk_constructor
;
20014 else if (is_overloaded_fn (unqualified_name
)
20015 && DECL_CONSTRUCTOR_P (get_first_fn
20016 (unqualified_name
)))
20017 sfk
= sfk_constructor
;
20019 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20020 *ctor_dtor_or_conv_p
= -1;
20023 declarator
= make_id_declarator (qualifying_scope
,
20026 declarator
->std_attributes
= attrs
;
20027 declarator
->id_loc
= token
->location
;
20028 declarator
->parameter_pack_p
= pack_expansion_p
;
20030 if (pack_expansion_p
)
20031 maybe_warn_variadic_templates ();
20034 handle_declarator
:;
20035 scope
= get_scope_of_declarator (declarator
);
20038 /* Any names that appear after the declarator-id for a
20039 member are looked up in the containing scope. */
20040 if (at_function_scope_p ())
20042 /* But declarations with qualified-ids can't appear in a
20044 cp_parser_error (parser
, "qualified-id in declaration");
20045 declarator
= cp_error_declarator
;
20048 pushed_scope
= push_scope (scope
);
20050 parser
->in_declarator_p
= true;
20051 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20052 || (declarator
&& declarator
->kind
== cdk_id
))
20053 /* Default args are only allowed on function
20055 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20057 parser
->default_arg_ok_p
= false;
20066 /* For an abstract declarator, we might wind up with nothing at this
20067 point. That's an error; the declarator is not optional. */
20069 cp_parser_error (parser
, "expected declarator");
20071 /* If we entered a scope, we must exit it now. */
20073 pop_scope (pushed_scope
);
20075 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20076 parser
->in_declarator_p
= saved_in_declarator_p
;
20081 /* Parse a ptr-operator.
20084 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20085 * cv-qualifier-seq [opt]
20087 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20088 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20093 & cv-qualifier-seq [opt]
20095 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20096 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20097 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20098 filled in with the TYPE containing the member. *CV_QUALS is
20099 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20100 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20101 Note that the tree codes returned by this function have nothing
20102 to do with the types of trees that will be eventually be created
20103 to represent the pointer or reference type being parsed. They are
20104 just constants with suggestive names. */
20105 static enum tree_code
20106 cp_parser_ptr_operator (cp_parser
* parser
,
20108 cp_cv_quals
*cv_quals
,
20111 enum tree_code code
= ERROR_MARK
;
20113 tree attrs
= NULL_TREE
;
20115 /* Assume that it's not a pointer-to-member. */
20117 /* And that there are no cv-qualifiers. */
20118 *cv_quals
= TYPE_UNQUALIFIED
;
20120 /* Peek at the next token. */
20121 token
= cp_lexer_peek_token (parser
->lexer
);
20123 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20124 if (token
->type
== CPP_MULT
)
20125 code
= INDIRECT_REF
;
20126 else if (token
->type
== CPP_AND
)
20128 else if ((cxx_dialect
!= cxx98
) &&
20129 token
->type
== CPP_AND_AND
) /* C++0x only */
20130 code
= NON_LVALUE_EXPR
;
20132 if (code
!= ERROR_MARK
)
20134 /* Consume the `*', `&' or `&&'. */
20135 cp_lexer_consume_token (parser
->lexer
);
20137 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20138 `&', if we are allowing GNU extensions. (The only qualifier
20139 that can legally appear after `&' is `restrict', but that is
20140 enforced during semantic analysis. */
20141 if (code
== INDIRECT_REF
20142 || cp_parser_allow_gnu_extensions_p (parser
))
20143 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20145 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20146 if (attributes
!= NULL
)
20147 *attributes
= attrs
;
20151 /* Try the pointer-to-member case. */
20152 cp_parser_parse_tentatively (parser
);
20153 /* Look for the optional `::' operator. */
20154 cp_parser_global_scope_opt (parser
,
20155 /*current_scope_valid_p=*/false);
20156 /* Look for the nested-name specifier. */
20157 token
= cp_lexer_peek_token (parser
->lexer
);
20158 cp_parser_nested_name_specifier (parser
,
20159 /*typename_keyword_p=*/false,
20160 /*check_dependency_p=*/true,
20162 /*is_declaration=*/false);
20163 /* If we found it, and the next token is a `*', then we are
20164 indeed looking at a pointer-to-member operator. */
20165 if (!cp_parser_error_occurred (parser
)
20166 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20168 /* Indicate that the `*' operator was used. */
20169 code
= INDIRECT_REF
;
20171 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20172 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20173 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20174 error_at (token
->location
, "cannot form pointer to member of "
20175 "non-class %q#T", parser
->scope
);
20178 /* The type of which the member is a member is given by the
20180 *type
= parser
->scope
;
20181 /* The next name will not be qualified. */
20182 parser
->scope
= NULL_TREE
;
20183 parser
->qualifying_scope
= NULL_TREE
;
20184 parser
->object_scope
= NULL_TREE
;
20185 /* Look for optional c++11 attributes. */
20186 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20187 if (attributes
!= NULL
)
20188 *attributes
= attrs
;
20189 /* Look for the optional cv-qualifier-seq. */
20190 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20193 /* If that didn't work we don't have a ptr-operator. */
20194 if (!cp_parser_parse_definitely (parser
))
20195 cp_parser_error (parser
, "expected ptr-operator");
20201 /* Parse an (optional) cv-qualifier-seq.
20204 cv-qualifier cv-qualifier-seq [opt]
20215 Returns a bitmask representing the cv-qualifiers. */
20218 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20220 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20225 cp_cv_quals cv_qualifier
;
20227 /* Peek at the next token. */
20228 token
= cp_lexer_peek_token (parser
->lexer
);
20229 /* See if it's a cv-qualifier. */
20230 switch (token
->keyword
)
20233 cv_qualifier
= TYPE_QUAL_CONST
;
20237 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20241 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20245 cv_qualifier
= TYPE_UNQUALIFIED
;
20252 if (cv_quals
& cv_qualifier
)
20254 error_at (token
->location
, "duplicate cv-qualifier");
20255 cp_lexer_purge_token (parser
->lexer
);
20259 cp_lexer_consume_token (parser
->lexer
);
20260 cv_quals
|= cv_qualifier
;
20267 /* Parse an (optional) ref-qualifier
20273 Returns cp_ref_qualifier representing ref-qualifier. */
20275 static cp_ref_qualifier
20276 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20278 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20280 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20281 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20286 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20287 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20289 switch (token
->type
)
20292 curr_ref_qual
= REF_QUAL_LVALUE
;
20296 curr_ref_qual
= REF_QUAL_RVALUE
;
20300 curr_ref_qual
= REF_QUAL_NONE
;
20304 if (!curr_ref_qual
)
20308 error_at (token
->location
, "multiple ref-qualifiers");
20309 cp_lexer_purge_token (parser
->lexer
);
20313 ref_qual
= curr_ref_qual
;
20314 cp_lexer_consume_token (parser
->lexer
);
20321 /* Parse an optional tx-qualifier.
20325 transaction_safe_dynamic */
20328 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20331 if (token
->type
== CPP_NAME
)
20333 tree name
= token
->u
.value
;
20334 const char *p
= IDENTIFIER_POINTER (name
);
20335 const int len
= strlen ("transaction_safe");
20336 if (!strncmp (p
, "transaction_safe", len
))
20340 || !strcmp (p
, "_dynamic"))
20342 cp_lexer_consume_token (parser
->lexer
);
20345 error ("%E requires %<-fgnu-tm%>", name
);
20356 /* Parse an (optional) virt-specifier-seq.
20358 virt-specifier-seq:
20359 virt-specifier virt-specifier-seq [opt]
20365 Returns a bitmask representing the virt-specifiers. */
20367 static cp_virt_specifiers
20368 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20370 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20375 cp_virt_specifiers virt_specifier
;
20377 /* Peek at the next token. */
20378 token
= cp_lexer_peek_token (parser
->lexer
);
20379 /* See if it's a virt-specifier-qualifier. */
20380 if (token
->type
!= CPP_NAME
)
20382 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
20384 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20385 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20387 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
20389 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20390 virt_specifier
= VIRT_SPEC_FINAL
;
20392 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
20394 virt_specifier
= VIRT_SPEC_FINAL
;
20399 if (virt_specifiers
& virt_specifier
)
20401 error_at (token
->location
, "duplicate virt-specifier");
20402 cp_lexer_purge_token (parser
->lexer
);
20406 cp_lexer_consume_token (parser
->lexer
);
20407 virt_specifiers
|= virt_specifier
;
20410 return virt_specifiers
;
20413 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20414 is in scope even though it isn't real. */
20417 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20421 if (current_class_ptr
)
20423 /* We don't clear this between NSDMIs. Is it already what we want? */
20424 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20425 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20426 && cp_type_quals (type
) == quals
)
20430 this_parm
= build_this_parm (ctype
, quals
);
20431 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20432 current_class_ptr
= NULL_TREE
;
20434 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
20435 current_class_ptr
= this_parm
;
20438 /* Return true iff our current scope is a non-static data member
20442 parsing_nsdmi (void)
20444 /* We recognize NSDMI context by the context-less 'this' pointer set up
20445 by the function above. */
20446 if (current_class_ptr
20447 && TREE_CODE (current_class_ptr
) == PARM_DECL
20448 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20453 /* Parse a late-specified return type, if any. This is not a separate
20454 non-terminal, but part of a function declarator, which looks like
20456 -> trailing-type-specifier-seq abstract-declarator(opt)
20458 Returns the type indicated by the type-id.
20460 In addition to this, parse any queued up #pragma omp declare simd
20461 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20462 #pragma acc routine clauses.
20464 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20468 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20469 tree
& requires_clause
, cp_cv_quals quals
)
20472 tree type
= NULL_TREE
;
20473 bool declare_simd_p
= (parser
->omp_declare_simd
20475 && declarator
->kind
== cdk_id
);
20477 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
20478 && declarator
&& declarator
->kind
== cdk_id
);
20480 bool oacc_routine_p
= (parser
->oacc_routine
20482 && declarator
->kind
== cdk_id
);
20484 /* Peek at the next token. */
20485 token
= cp_lexer_peek_token (parser
->lexer
);
20486 /* A late-specified return type is indicated by an initial '->'. */
20487 if (token
->type
!= CPP_DEREF
20488 && token
->keyword
!= RID_REQUIRES
20489 && !(token
->type
== CPP_NAME
20490 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20491 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
20494 tree save_ccp
= current_class_ptr
;
20495 tree save_ccr
= current_class_ref
;
20498 /* DR 1207: 'this' is in scope in the trailing return type. */
20499 inject_this_parameter (current_class_type
, quals
);
20502 if (token
->type
== CPP_DEREF
)
20504 /* Consume the ->. */
20505 cp_lexer_consume_token (parser
->lexer
);
20507 type
= cp_parser_trailing_type_id (parser
);
20510 /* Function declarations may be followed by a trailing
20511 requires-clause. */
20512 requires_clause
= cp_parser_requires_clause_opt (parser
);
20514 if (cilk_simd_fn_vector_p
)
20515 declarator
->attributes
20516 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
20517 declarator
->attributes
);
20518 if (declare_simd_p
)
20519 declarator
->attributes
20520 = cp_parser_late_parsing_omp_declare_simd (parser
,
20521 declarator
->attributes
);
20522 if (oacc_routine_p
)
20523 declarator
->attributes
20524 = cp_parser_late_parsing_oacc_routine (parser
,
20525 declarator
->attributes
);
20529 current_class_ptr
= save_ccp
;
20530 current_class_ref
= save_ccr
;
20536 /* Parse a declarator-id.
20540 :: [opt] nested-name-specifier [opt] type-name
20542 In the `id-expression' case, the value returned is as for
20543 cp_parser_id_expression if the id-expression was an unqualified-id.
20544 If the id-expression was a qualified-id, then a SCOPE_REF is
20545 returned. The first operand is the scope (either a NAMESPACE_DECL
20546 or TREE_TYPE), but the second is still just a representation of an
20550 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20553 /* The expression must be an id-expression. Assume that qualified
20554 names are the names of types so that:
20557 int S<T>::R::i = 3;
20559 will work; we must treat `S<T>::R' as the name of a type.
20560 Similarly, assume that qualified names are templates, where
20564 int S<T>::R<T>::i = 3;
20567 id
= cp_parser_id_expression (parser
,
20568 /*template_keyword_p=*/false,
20569 /*check_dependency_p=*/false,
20570 /*template_p=*/NULL
,
20571 /*declarator_p=*/true,
20573 if (id
&& BASELINK_P (id
))
20574 id
= BASELINK_FUNCTIONS (id
);
20578 /* Parse a type-id.
20581 type-specifier-seq abstract-declarator [opt]
20583 Returns the TYPE specified. */
20586 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20587 bool is_trailing_return
)
20589 cp_decl_specifier_seq type_specifier_seq
;
20590 cp_declarator
*abstract_declarator
;
20592 /* Parse the type-specifier-seq. */
20593 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20594 is_trailing_return
,
20595 &type_specifier_seq
);
20596 if (is_template_arg
&& type_specifier_seq
.type
20597 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20598 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20599 /* A bare template name as a template argument is a template template
20600 argument, not a placeholder, so fail parsing it as a type argument. */
20602 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20603 cp_parser_simulate_error (parser
);
20604 return error_mark_node
;
20606 if (type_specifier_seq
.type
== error_mark_node
)
20607 return error_mark_node
;
20609 /* There might or might not be an abstract declarator. */
20610 cp_parser_parse_tentatively (parser
);
20611 /* Look for the declarator. */
20612 abstract_declarator
20613 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20614 /*parenthesized_p=*/NULL
,
20615 /*member_p=*/false,
20616 /*friend_p=*/false);
20617 /* Check to see if there really was a declarator. */
20618 if (!cp_parser_parse_definitely (parser
))
20619 abstract_declarator
= NULL
;
20621 if (type_specifier_seq
.type
20622 /* The concepts TS allows 'auto' as a type-id. */
20623 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20624 /* None of the valid uses of 'auto' in C++14 involve the type-id
20625 nonterminal, but it is valid in a trailing-return-type. */
20626 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20627 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20629 /* A type-id with type 'auto' is only ok if the abstract declarator
20630 is a function declarator with a late-specified return type.
20632 A type-id with 'auto' is also valid in a trailing-return-type
20633 in a compound-requirement. */
20634 if (abstract_declarator
20635 && abstract_declarator
->kind
== cdk_function
20636 && abstract_declarator
->u
.function
.late_return_type
)
20638 else if (parser
->in_result_type_constraint_p
)
20642 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20643 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20645 error_at (loc
, "missing template arguments after %qT",
20647 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20651 error_at (loc
, "invalid use of %qT", auto_node
);
20652 return error_mark_node
;
20656 return groktypename (&type_specifier_seq
, abstract_declarator
,
20661 cp_parser_type_id (cp_parser
*parser
)
20663 return cp_parser_type_id_1 (parser
, false, false);
20667 cp_parser_template_type_arg (cp_parser
*parser
)
20670 const char *saved_message
= parser
->type_definition_forbidden_message
;
20671 parser
->type_definition_forbidden_message
20672 = G_("types may not be defined in template arguments");
20673 r
= cp_parser_type_id_1 (parser
, true, false);
20674 parser
->type_definition_forbidden_message
= saved_message
;
20675 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20677 error ("invalid use of %<auto%> in template argument");
20678 r
= error_mark_node
;
20684 cp_parser_trailing_type_id (cp_parser
*parser
)
20686 return cp_parser_type_id_1 (parser
, false, true);
20689 /* Parse a type-specifier-seq.
20691 type-specifier-seq:
20692 type-specifier type-specifier-seq [opt]
20696 type-specifier-seq:
20697 attributes type-specifier-seq [opt]
20699 If IS_DECLARATION is true, we are at the start of a "condition" or
20700 exception-declaration, so we might be followed by a declarator-id.
20702 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20703 i.e. we've just seen "->".
20705 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20708 cp_parser_type_specifier_seq (cp_parser
* parser
,
20709 bool is_declaration
,
20710 bool is_trailing_return
,
20711 cp_decl_specifier_seq
*type_specifier_seq
)
20713 bool seen_type_specifier
= false;
20714 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20715 cp_token
*start_token
= NULL
;
20717 /* Clear the TYPE_SPECIFIER_SEQ. */
20718 clear_decl_specs (type_specifier_seq
);
20720 /* In the context of a trailing return type, enum E { } is an
20721 elaborated-type-specifier followed by a function-body, not an
20723 if (is_trailing_return
)
20724 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
20726 /* Parse the type-specifiers and attributes. */
20729 tree type_specifier
;
20730 bool is_cv_qualifier
;
20732 /* Check for attributes first. */
20733 if (cp_next_tokens_can_be_attribute_p (parser
))
20735 type_specifier_seq
->attributes
=
20736 chainon (type_specifier_seq
->attributes
,
20737 cp_parser_attributes_opt (parser
));
20741 /* record the token of the beginning of the type specifier seq,
20742 for error reporting purposes*/
20744 start_token
= cp_lexer_peek_token (parser
->lexer
);
20746 /* Look for the type-specifier. */
20747 type_specifier
= cp_parser_type_specifier (parser
,
20749 type_specifier_seq
,
20750 /*is_declaration=*/false,
20753 if (!type_specifier
)
20755 /* If the first type-specifier could not be found, this is not a
20756 type-specifier-seq at all. */
20757 if (!seen_type_specifier
)
20759 /* Set in_declarator_p to avoid skipping to the semicolon. */
20760 int in_decl
= parser
->in_declarator_p
;
20761 parser
->in_declarator_p
= true;
20763 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
20764 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20765 cp_parser_error (parser
, "expected type-specifier");
20767 parser
->in_declarator_p
= in_decl
;
20769 type_specifier_seq
->type
= error_mark_node
;
20772 /* If subsequent type-specifiers could not be found, the
20773 type-specifier-seq is complete. */
20777 seen_type_specifier
= true;
20778 /* The standard says that a condition can be:
20780 type-specifier-seq declarator = assignment-expression
20787 we should treat the "S" as a declarator, not as a
20788 type-specifier. The standard doesn't say that explicitly for
20789 type-specifier-seq, but it does say that for
20790 decl-specifier-seq in an ordinary declaration. Perhaps it
20791 would be clearer just to allow a decl-specifier-seq here, and
20792 then add a semantic restriction that if any decl-specifiers
20793 that are not type-specifiers appear, the program is invalid. */
20794 if (is_declaration
&& !is_cv_qualifier
)
20795 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
20799 /* Return whether the function currently being declared has an associated
20800 template parameter list. */
20803 function_being_declared_is_template_p (cp_parser
* parser
)
20805 if (!current_template_parms
|| processing_template_parmlist
)
20808 if (parser
->implicit_template_scope
)
20811 if (at_class_scope_p ()
20812 && TYPE_BEING_DEFINED (current_class_type
))
20813 return parser
->num_template_parameter_lists
!= 0;
20815 return ((int) parser
->num_template_parameter_lists
> template_class_depth
20816 (current_class_type
));
20819 /* Parse a parameter-declaration-clause.
20821 parameter-declaration-clause:
20822 parameter-declaration-list [opt] ... [opt]
20823 parameter-declaration-list , ...
20825 Returns a representation for the parameter declarations. A return
20826 value of NULL indicates a parameter-declaration-clause consisting
20827 only of an ellipsis. */
20830 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
20839 int auto_is_implicit_function_template_parm_p
;
20841 parser
->auto_is_implicit_function_template_parm_p
20842 = auto_is_implicit_function_template_parm_p
;
20844 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
20848 if (!processing_specialization
20849 && !processing_template_parmlist
20850 && !processing_explicit_instantiation
)
20851 if (!current_function_decl
20852 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
20853 parser
->auto_is_implicit_function_template_parm_p
= true;
20855 /* Peek at the next token. */
20856 token
= cp_lexer_peek_token (parser
->lexer
);
20857 /* Check for trivial parameter-declaration-clauses. */
20858 if (token
->type
== CPP_ELLIPSIS
)
20860 /* Consume the `...' token. */
20861 cp_lexer_consume_token (parser
->lexer
);
20864 else if (token
->type
== CPP_CLOSE_PAREN
)
20865 /* There are no parameters. */
20867 #ifndef NO_IMPLICIT_EXTERN_C
20868 if (in_system_header_at (input_location
)
20869 && current_class_type
== NULL
20870 && current_lang_name
== lang_name_c
)
20874 return void_list_node
;
20876 /* Check for `(void)', too, which is a special case. */
20877 else if (token
->keyword
== RID_VOID
20878 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20879 == CPP_CLOSE_PAREN
))
20881 /* Consume the `void' token. */
20882 cp_lexer_consume_token (parser
->lexer
);
20883 /* There are no parameters. */
20884 return void_list_node
;
20887 /* Parse the parameter-declaration-list. */
20888 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
20889 /* If a parse error occurred while parsing the
20890 parameter-declaration-list, then the entire
20891 parameter-declaration-clause is erroneous. */
20895 /* Peek at the next token. */
20896 token
= cp_lexer_peek_token (parser
->lexer
);
20897 /* If it's a `,', the clause should terminate with an ellipsis. */
20898 if (token
->type
== CPP_COMMA
)
20900 /* Consume the `,'. */
20901 cp_lexer_consume_token (parser
->lexer
);
20902 /* Expect an ellipsis. */
20904 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
20906 /* It might also be `...' if the optional trailing `,' was
20908 else if (token
->type
== CPP_ELLIPSIS
)
20910 /* Consume the `...' token. */
20911 cp_lexer_consume_token (parser
->lexer
);
20912 /* And remember that we saw it. */
20916 ellipsis_p
= false;
20918 /* Finish the parameter list. */
20920 parameters
= chainon (parameters
, void_list_node
);
20925 /* Parse a parameter-declaration-list.
20927 parameter-declaration-list:
20928 parameter-declaration
20929 parameter-declaration-list , parameter-declaration
20931 Returns a representation of the parameter-declaration-list, as for
20932 cp_parser_parameter_declaration_clause. However, the
20933 `void_list_node' is never appended to the list. Upon return,
20934 *IS_ERROR will be true iff an error occurred. */
20937 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
20939 tree parameters
= NULL_TREE
;
20940 tree
*tail
= ¶meters
;
20941 bool saved_in_unbraced_linkage_specification_p
;
20944 /* Assume all will go well. */
20946 /* The special considerations that apply to a function within an
20947 unbraced linkage specifications do not apply to the parameters
20948 to the function. */
20949 saved_in_unbraced_linkage_specification_p
20950 = parser
->in_unbraced_linkage_specification_p
;
20951 parser
->in_unbraced_linkage_specification_p
= false;
20953 /* Look for more parameters. */
20956 cp_parameter_declarator
*parameter
;
20957 tree decl
= error_mark_node
;
20958 bool parenthesized_p
= false;
20959 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
20960 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20961 (current_template_parms
)) : 0);
20963 /* Parse the parameter. */
20965 = cp_parser_parameter_declaration (parser
,
20966 /*template_parm_p=*/false,
20969 /* We don't know yet if the enclosing context is deprecated, so wait
20970 and warn in grokparms if appropriate. */
20971 deprecated_state
= DEPRECATED_SUPPRESS
;
20975 /* If a function parameter pack was specified and an implicit template
20976 parameter was introduced during cp_parser_parameter_declaration,
20977 change any implicit parameters introduced into packs. */
20978 if (parser
->implicit_template_parms
20979 && parameter
->declarator
20980 && parameter
->declarator
->parameter_pack_p
)
20982 int latest_template_parm_idx
= TREE_VEC_LENGTH
20983 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
20985 if (latest_template_parm_idx
!= template_parm_idx
)
20986 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
20987 (parameter
->decl_specifiers
.type
,
20988 template_parm_idx
, latest_template_parm_idx
);
20991 decl
= grokdeclarator (parameter
->declarator
,
20992 ¶meter
->decl_specifiers
,
20994 parameter
->default_argument
!= NULL_TREE
,
20995 ¶meter
->decl_specifiers
.attributes
);
20998 deprecated_state
= DEPRECATED_NORMAL
;
21000 /* If a parse error occurred parsing the parameter declaration,
21001 then the entire parameter-declaration-list is erroneous. */
21002 if (decl
== error_mark_node
)
21005 parameters
= error_mark_node
;
21009 if (parameter
->decl_specifiers
.attributes
)
21010 cplus_decl_attributes (&decl
,
21011 parameter
->decl_specifiers
.attributes
,
21013 if (DECL_NAME (decl
))
21014 decl
= pushdecl (decl
);
21016 if (decl
!= error_mark_node
)
21018 retrofit_lang_decl (decl
);
21019 DECL_PARM_INDEX (decl
) = ++index
;
21020 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21023 /* Add the new parameter to the list. */
21024 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21025 tail
= &TREE_CHAIN (*tail
);
21027 /* Peek at the next token. */
21028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21029 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21030 /* These are for Objective-C++ */
21031 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21032 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21033 /* The parameter-declaration-list is complete. */
21035 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21039 /* Peek at the next token. */
21040 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21041 /* If it's an ellipsis, then the list is complete. */
21042 if (token
->type
== CPP_ELLIPSIS
)
21044 /* Otherwise, there must be more parameters. Consume the
21046 cp_lexer_consume_token (parser
->lexer
);
21047 /* When parsing something like:
21049 int i(float f, double d)
21051 we can tell after seeing the declaration for "f" that we
21052 are not looking at an initialization of a variable "i",
21053 but rather at the declaration of a function "i".
21055 Due to the fact that the parsing of template arguments
21056 (as specified to a template-id) requires backtracking we
21057 cannot use this technique when inside a template argument
21059 if (!parser
->in_template_argument_list_p
21060 && !parser
->in_type_id_in_expr_p
21061 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21062 /* However, a parameter-declaration of the form
21063 "float(f)" (which is a valid declaration of a
21064 parameter "f") can also be interpreted as an
21065 expression (the conversion of "f" to "float"). */
21066 && !parenthesized_p
)
21067 cp_parser_commit_to_tentative_parse (parser
);
21071 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21073 cp_parser_skip_to_closing_parenthesis (parser
,
21074 /*recovering=*/true,
21075 /*or_comma=*/false,
21076 /*consume_paren=*/false);
21081 parser
->in_unbraced_linkage_specification_p
21082 = saved_in_unbraced_linkage_specification_p
;
21084 /* Reset implicit_template_scope if we are about to leave the function
21085 parameter list that introduced it. Note that for out-of-line member
21086 definitions, there will be one or more class scopes before we get to
21087 the template parameter scope. */
21089 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21090 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21092 while (maybe_its
->kind
== sk_class
)
21093 maybe_its
= maybe_its
->level_chain
;
21094 if (maybe_its
== its
)
21096 parser
->implicit_template_parms
= 0;
21097 parser
->implicit_template_scope
= 0;
21104 /* Parse a parameter declaration.
21106 parameter-declaration:
21107 decl-specifier-seq ... [opt] declarator
21108 decl-specifier-seq declarator = assignment-expression
21109 decl-specifier-seq ... [opt] abstract-declarator [opt]
21110 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21112 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21113 declares a template parameter. (In that case, a non-nested `>'
21114 token encountered during the parsing of the assignment-expression
21115 is not interpreted as a greater-than operator.)
21117 Returns a representation of the parameter, or NULL if an error
21118 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21119 true iff the declarator is of the form "(p)". */
21121 static cp_parameter_declarator
*
21122 cp_parser_parameter_declaration (cp_parser
*parser
,
21123 bool template_parm_p
,
21124 bool *parenthesized_p
)
21126 int declares_class_or_enum
;
21127 cp_decl_specifier_seq decl_specifiers
;
21128 cp_declarator
*declarator
;
21129 tree default_argument
;
21130 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21131 const char *saved_message
;
21132 bool template_parameter_pack_p
= false;
21134 /* In a template parameter, `>' is not an operator.
21138 When parsing a default template-argument for a non-type
21139 template-parameter, the first non-nested `>' is taken as the end
21140 of the template parameter-list rather than a greater-than
21143 /* Type definitions may not appear in parameter types. */
21144 saved_message
= parser
->type_definition_forbidden_message
;
21145 parser
->type_definition_forbidden_message
21146 = G_("types may not be defined in parameter types");
21148 /* Parse the declaration-specifiers. */
21149 cp_parser_decl_specifier_seq (parser
,
21150 CP_PARSER_FLAGS_NONE
,
21152 &declares_class_or_enum
);
21154 /* Complain about missing 'typename' or other invalid type names. */
21155 if (!decl_specifiers
.any_type_specifiers_p
21156 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21157 decl_specifiers
.type
= error_mark_node
;
21159 /* If an error occurred, there's no reason to attempt to parse the
21160 rest of the declaration. */
21161 if (cp_parser_error_occurred (parser
))
21163 parser
->type_definition_forbidden_message
= saved_message
;
21167 /* Peek at the next token. */
21168 token
= cp_lexer_peek_token (parser
->lexer
);
21170 /* If the next token is a `)', `,', `=', `>', or `...', then there
21171 is no declarator. However, when variadic templates are enabled,
21172 there may be a declarator following `...'. */
21173 if (token
->type
== CPP_CLOSE_PAREN
21174 || token
->type
== CPP_COMMA
21175 || token
->type
== CPP_EQ
21176 || token
->type
== CPP_GREATER
)
21179 if (parenthesized_p
)
21180 *parenthesized_p
= false;
21182 /* Otherwise, there should be a declarator. */
21185 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21186 parser
->default_arg_ok_p
= false;
21188 /* After seeing a decl-specifier-seq, if the next token is not a
21189 "(", there is no possibility that the code is a valid
21190 expression. Therefore, if parsing tentatively, we commit at
21192 if (!parser
->in_template_argument_list_p
21193 /* In an expression context, having seen:
21197 we cannot be sure whether we are looking at a
21198 function-type (taking a "char" as a parameter) or a cast
21199 of some object of type "char" to "int". */
21200 && !parser
->in_type_id_in_expr_p
21201 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21202 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21203 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21204 cp_parser_commit_to_tentative_parse (parser
);
21205 /* Parse the declarator. */
21206 declarator_token_start
= token
;
21207 declarator
= cp_parser_declarator (parser
,
21208 CP_PARSER_DECLARATOR_EITHER
,
21209 /*ctor_dtor_or_conv_p=*/NULL
,
21211 /*member_p=*/false,
21212 /*friend_p=*/false);
21213 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21214 /* After the declarator, allow more attributes. */
21215 decl_specifiers
.attributes
21216 = chainon (decl_specifiers
.attributes
,
21217 cp_parser_attributes_opt (parser
));
21219 /* If the declarator is a template parameter pack, remember that and
21220 clear the flag in the declarator itself so we don't get errors
21221 from grokdeclarator. */
21222 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21224 declarator
->parameter_pack_p
= false;
21225 template_parameter_pack_p
= true;
21229 /* If the next token is an ellipsis, and we have not seen a declarator
21230 name, and if either the type of the declarator contains parameter
21231 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21232 for, eg, abbreviated integral type names), then we actually have a
21233 parameter pack expansion expression. Otherwise, leave the ellipsis
21234 for a C-style variadic function. */
21235 token
= cp_lexer_peek_token (parser
->lexer
);
21236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21238 tree type
= decl_specifiers
.type
;
21240 if (type
&& DECL_P (type
))
21241 type
= TREE_TYPE (type
);
21244 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21245 && (template_parm_p
|| uses_parameter_packs (type
)))
21246 || (!type
&& template_parm_p
))
21247 && declarator_can_be_parameter_pack (declarator
))
21249 /* Consume the `...'. */
21250 cp_lexer_consume_token (parser
->lexer
);
21251 maybe_warn_variadic_templates ();
21253 /* Build a pack expansion type */
21254 if (template_parm_p
)
21255 template_parameter_pack_p
= true;
21256 else if (declarator
)
21257 declarator
->parameter_pack_p
= true;
21259 decl_specifiers
.type
= make_pack_expansion (type
);
21263 /* The restriction on defining new types applies only to the type
21264 of the parameter, not to the default argument. */
21265 parser
->type_definition_forbidden_message
= saved_message
;
21267 /* If the next token is `=', then process a default argument. */
21268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21270 tree type
= decl_specifiers
.type
;
21271 token
= cp_lexer_peek_token (parser
->lexer
);
21272 /* If we are defining a class, then the tokens that make up the
21273 default argument must be saved and processed later. */
21274 if (!template_parm_p
&& at_class_scope_p ()
21275 && TYPE_BEING_DEFINED (current_class_type
)
21276 && !LAMBDA_TYPE_P (current_class_type
))
21277 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21279 // A constrained-type-specifier may declare a type template-parameter.
21280 else if (declares_constrained_type_template_parameter (type
))
21282 = cp_parser_default_type_template_argument (parser
);
21284 // A constrained-type-specifier may declare a template-template-parameter.
21285 else if (declares_constrained_template_template_parameter (type
))
21287 = cp_parser_default_template_template_argument (parser
);
21289 /* Outside of a class definition, we can just parse the
21290 assignment-expression. */
21293 = cp_parser_default_argument (parser
, template_parm_p
);
21295 if (!parser
->default_arg_ok_p
)
21297 permerror (token
->location
,
21298 "default arguments are only "
21299 "permitted for function parameters");
21301 else if ((declarator
&& declarator
->parameter_pack_p
)
21302 || template_parameter_pack_p
21303 || (decl_specifiers
.type
21304 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21306 /* Find the name of the parameter pack. */
21307 cp_declarator
*id_declarator
= declarator
;
21308 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21309 id_declarator
= id_declarator
->declarator
;
21311 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21312 error_at (declarator_token_start
->location
,
21314 ? G_("template parameter pack %qD "
21315 "cannot have a default argument")
21316 : G_("parameter pack %qD cannot have "
21317 "a default argument"),
21318 id_declarator
->u
.id
.unqualified_name
);
21320 error_at (declarator_token_start
->location
,
21322 ? G_("template parameter pack cannot have "
21323 "a default argument")
21324 : G_("parameter pack cannot have a "
21325 "default argument"));
21327 default_argument
= NULL_TREE
;
21331 default_argument
= NULL_TREE
;
21333 return make_parameter_declarator (&decl_specifiers
,
21336 template_parameter_pack_p
);
21339 /* Parse a default argument and return it.
21341 TEMPLATE_PARM_P is true if this is a default argument for a
21342 non-type template parameter. */
21344 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21346 tree default_argument
= NULL_TREE
;
21347 bool saved_greater_than_is_operator_p
;
21348 bool saved_local_variables_forbidden_p
;
21349 bool non_constant_p
, is_direct_init
;
21351 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21353 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21354 parser
->greater_than_is_operator_p
= !template_parm_p
;
21355 /* Local variable names (and the `this' keyword) may not
21356 appear in a default argument. */
21357 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21358 parser
->local_variables_forbidden_p
= true;
21359 /* Parse the assignment-expression. */
21360 if (template_parm_p
)
21361 push_deferring_access_checks (dk_no_deferred
);
21362 tree saved_class_ptr
= NULL_TREE
;
21363 tree saved_class_ref
= NULL_TREE
;
21364 /* The "this" pointer is not valid in a default argument. */
21367 saved_class_ptr
= current_class_ptr
;
21368 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21369 saved_class_ref
= current_class_ref
;
21370 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21373 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21374 /* Restore the "this" pointer. */
21377 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21378 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21380 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21381 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21382 if (template_parm_p
)
21383 pop_deferring_access_checks ();
21384 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21385 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21387 return default_argument
;
21390 /* Parse a function-body.
21393 compound_statement */
21396 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21398 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21399 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21403 /* Parse a ctor-initializer-opt followed by a function-body. Return
21404 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21405 is true we are parsing a function-try-block. */
21408 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21409 bool in_function_try_block
)
21412 bool ctor_initializer_p
;
21413 const bool check_body_p
=
21414 DECL_CONSTRUCTOR_P (current_function_decl
)
21415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21418 /* Begin the function body. */
21419 body
= begin_function_body ();
21420 /* Parse the optional ctor-initializer. */
21421 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
21423 /* If we're parsing a constexpr constructor definition, we need
21424 to check that the constructor body is indeed empty. However,
21425 before we get to cp_parser_function_body lot of junk has been
21426 generated, so we can't just check that we have an empty block.
21427 Rather we take a snapshot of the outermost block, and check whether
21428 cp_parser_function_body changed its state. */
21431 list
= cur_stmt_list
;
21432 if (STATEMENT_LIST_TAIL (list
))
21433 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21435 /* Parse the function-body. */
21436 cp_parser_function_body (parser
, in_function_try_block
);
21438 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21439 /* Finish the function body. */
21440 finish_function_body (body
);
21442 return ctor_initializer_p
;
21445 /* Parse an initializer.
21448 = initializer-clause
21449 ( expression-list )
21451 Returns an expression representing the initializer. If no
21452 initializer is present, NULL_TREE is returned.
21454 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21455 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21456 set to TRUE if there is no initializer present. If there is an
21457 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21458 is set to true; otherwise it is set to false. */
21461 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21462 bool* non_constant_p
)
21467 /* Peek at the next token. */
21468 token
= cp_lexer_peek_token (parser
->lexer
);
21470 /* Let our caller know whether or not this initializer was
21472 *is_direct_init
= (token
->type
!= CPP_EQ
);
21473 /* Assume that the initializer is constant. */
21474 *non_constant_p
= false;
21476 if (token
->type
== CPP_EQ
)
21478 /* Consume the `='. */
21479 cp_lexer_consume_token (parser
->lexer
);
21480 /* Parse the initializer-clause. */
21481 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21483 else if (token
->type
== CPP_OPEN_PAREN
)
21485 vec
<tree
, va_gc
> *vec
;
21486 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21488 /*allow_expansion_p=*/true,
21491 return error_mark_node
;
21492 init
= build_tree_list_vec (vec
);
21493 release_tree_vector (vec
);
21495 else if (token
->type
== CPP_OPEN_BRACE
)
21497 cp_lexer_set_source_position (parser
->lexer
);
21498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21499 init
= cp_parser_braced_list (parser
, non_constant_p
);
21500 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21504 /* Anything else is an error. */
21505 cp_parser_error (parser
, "expected initializer");
21506 init
= error_mark_node
;
21509 if (check_for_bare_parameter_packs (init
))
21510 init
= error_mark_node
;
21515 /* Parse an initializer-clause.
21517 initializer-clause:
21518 assignment-expression
21521 Returns an expression representing the initializer.
21523 If the `assignment-expression' production is used the value
21524 returned is simply a representation for the expression.
21526 Otherwise, calls cp_parser_braced_list. */
21529 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21531 cp_expr initializer
;
21533 /* Assume the expression is constant. */
21534 *non_constant_p
= false;
21536 /* If it is not a `{', then we are looking at an
21537 assignment-expression. */
21538 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21541 = cp_parser_constant_expression (parser
,
21542 /*allow_non_constant_p=*/true,
21546 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21548 return initializer
;
21551 /* Parse a brace-enclosed initializer list.
21554 { initializer-list , [opt] }
21557 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21558 the elements of the initializer-list (or NULL, if the last
21559 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21560 NULL_TREE. There is no way to detect whether or not the optional
21561 trailing `,' was provided. NON_CONSTANT_P is as for
21562 cp_parser_initializer. */
21565 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21568 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21570 /* Consume the `{' token. */
21571 cp_lexer_consume_token (parser
->lexer
);
21572 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21573 initializer
= make_node (CONSTRUCTOR
);
21574 /* If it's not a `}', then there is a non-trivial initializer. */
21575 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21577 /* Parse the initializer list. */
21578 CONSTRUCTOR_ELTS (initializer
)
21579 = cp_parser_initializer_list (parser
, non_constant_p
);
21580 /* A trailing `,' token is allowed. */
21581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21582 cp_lexer_consume_token (parser
->lexer
);
21585 *non_constant_p
= false;
21586 /* Now, there should be a trailing `}'. */
21587 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21588 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21589 TREE_TYPE (initializer
) = init_list_type_node
;
21591 cp_expr
result (initializer
);
21592 /* Build a location of the form:
21595 with caret==start at the open brace, finish at the close brace. */
21596 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21597 result
.set_location (combined_loc
);
21601 /* Consume tokens up to, and including, the next non-nested closing `]'.
21602 Returns true iff we found a closing `]'. */
21605 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21607 unsigned square_depth
= 0;
21611 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21613 switch (token
->type
)
21616 case CPP_PRAGMA_EOL
:
21617 /* If we've run out of tokens, then there is no closing `]'. */
21620 case CPP_OPEN_SQUARE
:
21624 case CPP_CLOSE_SQUARE
:
21625 if (!square_depth
--)
21627 cp_lexer_consume_token (parser
->lexer
);
21636 /* Consume the token. */
21637 cp_lexer_consume_token (parser
->lexer
);
21641 /* Return true if we are looking at an array-designator, false otherwise. */
21644 cp_parser_array_designator_p (cp_parser
*parser
)
21646 /* Consume the `['. */
21647 cp_lexer_consume_token (parser
->lexer
);
21649 cp_lexer_save_tokens (parser
->lexer
);
21651 /* Skip tokens until the next token is a closing square bracket.
21652 If we find the closing `]', and the next token is a `=', then
21653 we are looking at an array designator. */
21654 bool array_designator_p
21655 = (cp_parser_skip_to_closing_square_bracket (parser
)
21656 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21658 /* Roll back the tokens we skipped. */
21659 cp_lexer_rollback_tokens (parser
->lexer
);
21661 return array_designator_p
;
21664 /* Parse an initializer-list.
21667 initializer-clause ... [opt]
21668 initializer-list , initializer-clause ... [opt]
21673 designation initializer-clause ...[opt]
21674 initializer-list , designation initializer-clause ...[opt]
21679 [ constant-expression ] =
21681 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21682 for the initializer. If the INDEX of the elt is non-NULL, it is the
21683 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21684 as for cp_parser_initializer. */
21686 static vec
<constructor_elt
, va_gc
> *
21687 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
21689 vec
<constructor_elt
, va_gc
> *v
= NULL
;
21691 /* Assume all of the expressions are constant. */
21692 *non_constant_p
= false;
21694 /* Parse the rest of the list. */
21700 bool clause_non_constant_p
;
21702 /* If the next token is an identifier and the following one is a
21703 colon, we are looking at the GNU designated-initializer
21705 if (cp_parser_allow_gnu_extensions_p (parser
)
21706 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
21707 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
21709 /* Warn the user that they are using an extension. */
21710 pedwarn (input_location
, OPT_Wpedantic
,
21711 "ISO C++ does not allow designated initializers");
21712 /* Consume the identifier. */
21713 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21714 /* Consume the `:'. */
21715 cp_lexer_consume_token (parser
->lexer
);
21717 /* Also handle the C99 syntax, '. id ='. */
21718 else if (cp_parser_allow_gnu_extensions_p (parser
)
21719 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
21720 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
21721 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
21723 /* Warn the user that they are using an extension. */
21724 pedwarn (input_location
, OPT_Wpedantic
,
21725 "ISO C++ does not allow C99 designated initializers");
21726 /* Consume the `.'. */
21727 cp_lexer_consume_token (parser
->lexer
);
21728 /* Consume the identifier. */
21729 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21730 /* Consume the `='. */
21731 cp_lexer_consume_token (parser
->lexer
);
21733 /* Also handle C99 array designators, '[ const ] ='. */
21734 else if (cp_parser_allow_gnu_extensions_p (parser
)
21735 && !c_dialect_objc ()
21736 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21738 /* In C++11, [ could start a lambda-introducer. */
21739 bool non_const
= false;
21741 cp_parser_parse_tentatively (parser
);
21743 if (!cp_parser_array_designator_p (parser
))
21745 cp_parser_simulate_error (parser
);
21746 designator
= NULL_TREE
;
21750 designator
= cp_parser_constant_expression (parser
, true,
21752 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21753 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21756 if (!cp_parser_parse_definitely (parser
))
21757 designator
= NULL_TREE
;
21758 else if (non_const
)
21759 require_potential_rvalue_constant_expression (designator
);
21762 designator
= NULL_TREE
;
21764 /* Parse the initializer. */
21765 initializer
= cp_parser_initializer_clause (parser
,
21766 &clause_non_constant_p
);
21767 /* If any clause is non-constant, so is the entire initializer. */
21768 if (clause_non_constant_p
)
21769 *non_constant_p
= true;
21771 /* If we have an ellipsis, this is an initializer pack
21773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21775 /* Consume the `...'. */
21776 cp_lexer_consume_token (parser
->lexer
);
21778 /* Turn the initializer into an initializer expansion. */
21779 initializer
= make_pack_expansion (initializer
);
21782 /* Add it to the vector. */
21783 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
21785 /* If the next token is not a comma, we have reached the end of
21787 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21790 /* Peek at the next token. */
21791 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21792 /* If the next token is a `}', then we're still done. An
21793 initializer-clause can have a trailing `,' after the
21794 initializer-list and before the closing `}'. */
21795 if (token
->type
== CPP_CLOSE_BRACE
)
21798 /* Consume the `,' token. */
21799 cp_lexer_consume_token (parser
->lexer
);
21805 /* Classes [gram.class] */
21807 /* Parse a class-name.
21813 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21814 to indicate that names looked up in dependent types should be
21815 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21816 keyword has been used to indicate that the name that appears next
21817 is a template. TAG_TYPE indicates the explicit tag given before
21818 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21819 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21820 is the class being defined in a class-head. If ENUM_OK is TRUE,
21821 enum-names are also accepted.
21823 Returns the TYPE_DECL representing the class. */
21826 cp_parser_class_name (cp_parser
*parser
,
21827 bool typename_keyword_p
,
21828 bool template_keyword_p
,
21829 enum tag_types tag_type
,
21830 bool check_dependency_p
,
21832 bool is_declaration
,
21839 tree identifier
= NULL_TREE
;
21841 /* All class-names start with an identifier. */
21842 token
= cp_lexer_peek_token (parser
->lexer
);
21843 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
21845 cp_parser_error (parser
, "expected class-name");
21846 return error_mark_node
;
21849 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21850 to a template-id, so we save it here. */
21851 scope
= parser
->scope
;
21852 if (scope
== error_mark_node
)
21853 return error_mark_node
;
21855 /* Any name names a type if we're following the `typename' keyword
21856 in a qualified name where the enclosing scope is type-dependent. */
21857 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
21858 && dependent_type_p (scope
));
21859 /* Handle the common case (an identifier, but not a template-id)
21861 if (token
->type
== CPP_NAME
21862 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
21864 cp_token
*identifier_token
;
21867 /* Look for the identifier. */
21868 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
21869 ambiguous_p
= identifier_token
->error_reported
;
21870 identifier
= cp_parser_identifier (parser
);
21871 /* If the next token isn't an identifier, we are certainly not
21872 looking at a class-name. */
21873 if (identifier
== error_mark_node
)
21874 decl
= error_mark_node
;
21875 /* If we know this is a type-name, there's no need to look it
21877 else if (typename_p
)
21881 tree ambiguous_decls
;
21882 /* If we already know that this lookup is ambiguous, then
21883 we've already issued an error message; there's no reason
21887 cp_parser_simulate_error (parser
);
21888 return error_mark_node
;
21890 /* If the next token is a `::', then the name must be a type
21893 [basic.lookup.qual]
21895 During the lookup for a name preceding the :: scope
21896 resolution operator, object, function, and enumerator
21897 names are ignored. */
21898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21899 tag_type
= scope_type
;
21900 /* Look up the name. */
21901 decl
= cp_parser_lookup_name (parser
, identifier
,
21903 /*is_template=*/false,
21904 /*is_namespace=*/false,
21905 check_dependency_p
,
21907 identifier_token
->location
);
21908 if (ambiguous_decls
)
21910 if (cp_parser_parsing_tentatively (parser
))
21911 cp_parser_simulate_error (parser
);
21912 return error_mark_node
;
21918 /* Try a template-id. */
21919 decl
= cp_parser_template_id (parser
, template_keyword_p
,
21920 check_dependency_p
,
21923 if (decl
== error_mark_node
)
21924 return error_mark_node
;
21927 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
21929 /* If this is a typename, create a TYPENAME_TYPE. */
21930 if (typename_p
&& decl
!= error_mark_node
)
21932 decl
= make_typename_type (scope
, decl
, typename_type
,
21933 /*complain=*/tf_error
);
21934 if (decl
!= error_mark_node
)
21935 decl
= TYPE_NAME (decl
);
21938 decl
= strip_using_decl (decl
);
21940 /* Check to see that it is really the name of a class. */
21941 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
21942 && identifier_p (TREE_OPERAND (decl
, 0))
21943 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21944 /* Situations like this:
21946 template <typename T> struct A {
21947 typename T::template X<int>::I i;
21950 are problematic. Is `T::template X<int>' a class-name? The
21951 standard does not seem to be definitive, but there is no other
21952 valid interpretation of the following `::'. Therefore, those
21953 names are considered class-names. */
21955 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
21956 if (decl
!= error_mark_node
)
21957 decl
= TYPE_NAME (decl
);
21959 else if (TREE_CODE (decl
) != TYPE_DECL
21960 || TREE_TYPE (decl
) == error_mark_node
21961 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
21962 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
21963 /* In Objective-C 2.0, a classname followed by '.' starts a
21964 dot-syntax expression, and it's not a type-name. */
21965 || (c_dialect_objc ()
21966 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
21967 && objc_is_class_name (decl
)))
21968 decl
= error_mark_node
;
21970 if (decl
== error_mark_node
)
21971 cp_parser_error (parser
, "expected class-name");
21972 else if (identifier
&& !parser
->scope
)
21973 maybe_note_name_used_in_class (identifier
, decl
);
21978 /* Parse a class-specifier.
21981 class-head { member-specification [opt] }
21983 Returns the TREE_TYPE representing the class. */
21986 cp_parser_class_specifier_1 (cp_parser
* parser
)
21989 tree attributes
= NULL_TREE
;
21990 bool nested_name_specifier_p
;
21991 unsigned saved_num_template_parameter_lists
;
21992 bool saved_in_function_body
;
21993 unsigned char in_statement
;
21994 bool in_switch_statement_p
;
21995 bool saved_in_unbraced_linkage_specification_p
;
21996 tree old_scope
= NULL_TREE
;
21997 tree scope
= NULL_TREE
;
21998 cp_token
*closing_brace
;
22000 push_deferring_access_checks (dk_no_deferred
);
22002 /* Parse the class-head. */
22003 type
= cp_parser_class_head (parser
,
22004 &nested_name_specifier_p
);
22005 /* If the class-head was a semantic disaster, skip the entire body
22009 cp_parser_skip_to_end_of_block_or_statement (parser
);
22010 pop_deferring_access_checks ();
22011 return error_mark_node
;
22014 /* Look for the `{'. */
22015 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
22017 pop_deferring_access_checks ();
22018 return error_mark_node
;
22021 cp_ensure_no_omp_declare_simd (parser
);
22022 cp_ensure_no_oacc_routine (parser
);
22024 /* Issue an error message if type-definitions are forbidden here. */
22025 cp_parser_check_type_definition (parser
);
22026 /* Remember that we are defining one more class. */
22027 ++parser
->num_classes_being_defined
;
22028 /* Inside the class, surrounding template-parameter-lists do not
22030 saved_num_template_parameter_lists
22031 = parser
->num_template_parameter_lists
;
22032 parser
->num_template_parameter_lists
= 0;
22033 /* We are not in a function body. */
22034 saved_in_function_body
= parser
->in_function_body
;
22035 parser
->in_function_body
= false;
22036 /* Or in a loop. */
22037 in_statement
= parser
->in_statement
;
22038 parser
->in_statement
= 0;
22039 /* Or in a switch. */
22040 in_switch_statement_p
= parser
->in_switch_statement_p
;
22041 parser
->in_switch_statement_p
= false;
22042 /* We are not immediately inside an extern "lang" block. */
22043 saved_in_unbraced_linkage_specification_p
22044 = parser
->in_unbraced_linkage_specification_p
;
22045 parser
->in_unbraced_linkage_specification_p
= false;
22047 // Associate constraints with the type.
22049 type
= associate_classtype_constraints (type
);
22051 /* Start the class. */
22052 if (nested_name_specifier_p
)
22054 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22055 old_scope
= push_inner_scope (scope
);
22057 type
= begin_class_definition (type
);
22059 if (type
== error_mark_node
)
22060 /* If the type is erroneous, skip the entire body of the class. */
22061 cp_parser_skip_to_closing_brace (parser
);
22063 /* Parse the member-specification. */
22064 cp_parser_member_specification_opt (parser
);
22066 /* Look for the trailing `}'. */
22067 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22068 /* Look for trailing attributes to apply to this class. */
22069 if (cp_parser_allow_gnu_extensions_p (parser
))
22070 attributes
= cp_parser_gnu_attributes_opt (parser
);
22071 if (type
!= error_mark_node
)
22072 type
= finish_struct (type
, attributes
);
22073 if (nested_name_specifier_p
)
22074 pop_inner_scope (old_scope
, scope
);
22076 /* We've finished a type definition. Check for the common syntax
22077 error of forgetting a semicolon after the definition. We need to
22078 be careful, as we can't just check for not-a-semicolon and be done
22079 with it; the user might have typed:
22081 class X { } c = ...;
22082 class X { } *p = ...;
22084 and so forth. Instead, enumerate all the possible tokens that
22085 might follow this production; if we don't see one of them, then
22086 complain and silently insert the semicolon. */
22088 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22089 bool want_semicolon
= true;
22091 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22092 /* Don't try to parse c++11 attributes here. As per the
22093 grammar, that should be a task for
22094 cp_parser_decl_specifier_seq. */
22095 want_semicolon
= false;
22097 switch (token
->type
)
22100 case CPP_SEMICOLON
:
22103 case CPP_OPEN_PAREN
:
22104 case CPP_CLOSE_PAREN
:
22106 want_semicolon
= false;
22109 /* While it's legal for type qualifiers and storage class
22110 specifiers to follow type definitions in the grammar, only
22111 compiler testsuites contain code like that. Assume that if
22112 we see such code, then what we're really seeing is a case
22116 const <type> var = ...;
22121 static <type> func (...) ...
22123 i.e. the qualifier or specifier applies to the next
22124 declaration. To do so, however, we need to look ahead one
22125 more token to see if *that* token is a type specifier.
22127 This code could be improved to handle:
22130 static const <type> var = ...; */
22132 if (keyword_is_decl_specifier (token
->keyword
))
22134 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22136 /* Handling user-defined types here would be nice, but very
22139 = (lookahead
->type
== CPP_KEYWORD
22140 && keyword_begins_type_specifier (lookahead
->keyword
));
22147 /* If we don't have a type, then something is very wrong and we
22148 shouldn't try to do anything clever. Likewise for not seeing the
22150 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22152 /* Locate the closing brace. */
22153 cp_token_position prev
22154 = cp_lexer_previous_token_position (parser
->lexer
);
22155 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22156 location_t loc
= prev_token
->location
;
22158 /* We want to suggest insertion of a ';' immediately *after* the
22159 closing brace, so, if we can, offset the location by 1 column. */
22160 location_t next_loc
= loc
;
22161 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22162 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22164 rich_location
richloc (line_table
, next_loc
);
22166 /* If we successfully offset the location, suggest the fix-it. */
22167 if (next_loc
!= loc
)
22168 richloc
.add_fixit_insert_before (next_loc
, ";");
22170 if (CLASSTYPE_DECLARED_CLASS (type
))
22171 error_at_rich_loc (&richloc
,
22172 "expected %<;%> after class definition");
22173 else if (TREE_CODE (type
) == RECORD_TYPE
)
22174 error_at_rich_loc (&richloc
,
22175 "expected %<;%> after struct definition");
22176 else if (TREE_CODE (type
) == UNION_TYPE
)
22177 error_at_rich_loc (&richloc
,
22178 "expected %<;%> after union definition");
22180 gcc_unreachable ();
22182 /* Unget one token and smash it to look as though we encountered
22183 a semicolon in the input stream. */
22184 cp_lexer_set_token_position (parser
->lexer
, prev
);
22185 token
= cp_lexer_peek_token (parser
->lexer
);
22186 token
->type
= CPP_SEMICOLON
;
22187 token
->keyword
= RID_MAX
;
22191 /* If this class is not itself within the scope of another class,
22192 then we need to parse the bodies of all of the queued function
22193 definitions. Note that the queued functions defined in a class
22194 are not always processed immediately following the
22195 class-specifier for that class. Consider:
22198 struct B { void f() { sizeof (A); } };
22201 If `f' were processed before the processing of `A' were
22202 completed, there would be no way to compute the size of `A'.
22203 Note that the nesting we are interested in here is lexical --
22204 not the semantic nesting given by TYPE_CONTEXT. In particular,
22207 struct A { struct B; };
22208 struct A::B { void f() { } };
22210 there is no need to delay the parsing of `A::B::f'. */
22211 if (--parser
->num_classes_being_defined
== 0)
22214 tree class_type
= NULL_TREE
;
22215 tree pushed_scope
= NULL_TREE
;
22217 cp_default_arg_entry
*e
;
22218 tree save_ccp
, save_ccr
;
22220 /* In a first pass, parse default arguments to the functions.
22221 Then, in a second pass, parse the bodies of the functions.
22222 This two-phased approach handles cases like:
22230 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22233 /* If there are default arguments that have not yet been processed,
22234 take care of them now. */
22235 if (class_type
!= e
->class_type
)
22238 pop_scope (pushed_scope
);
22239 class_type
= e
->class_type
;
22240 pushed_scope
= push_scope (class_type
);
22242 /* Make sure that any template parameters are in scope. */
22243 maybe_begin_member_template_processing (decl
);
22244 /* Parse the default argument expressions. */
22245 cp_parser_late_parsing_default_args (parser
, decl
);
22246 /* Remove any template parameters from the symbol table. */
22247 maybe_end_member_template_processing ();
22249 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22250 /* Now parse any NSDMIs. */
22251 save_ccp
= current_class_ptr
;
22252 save_ccr
= current_class_ref
;
22253 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22255 if (class_type
!= DECL_CONTEXT (decl
))
22258 pop_scope (pushed_scope
);
22259 class_type
= DECL_CONTEXT (decl
);
22260 pushed_scope
= push_scope (class_type
);
22262 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22263 cp_parser_late_parsing_nsdmi (parser
, decl
);
22265 vec_safe_truncate (unparsed_nsdmis
, 0);
22266 current_class_ptr
= save_ccp
;
22267 current_class_ref
= save_ccr
;
22269 pop_scope (pushed_scope
);
22271 /* Now do some post-NSDMI bookkeeping. */
22272 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22273 after_nsdmi_defaulted_late_checks (class_type
);
22274 vec_safe_truncate (unparsed_classes
, 0);
22275 after_nsdmi_defaulted_late_checks (type
);
22277 /* Now parse the body of the functions. */
22280 /* OpenMP UDRs need to be parsed before all other functions. */
22281 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22282 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22283 cp_parser_late_parsing_for_member (parser
, decl
);
22284 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22285 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22286 cp_parser_late_parsing_for_member (parser
, decl
);
22289 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22290 cp_parser_late_parsing_for_member (parser
, decl
);
22291 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22294 vec_safe_push (unparsed_classes
, type
);
22296 /* Put back any saved access checks. */
22297 pop_deferring_access_checks ();
22299 /* Restore saved state. */
22300 parser
->in_switch_statement_p
= in_switch_statement_p
;
22301 parser
->in_statement
= in_statement
;
22302 parser
->in_function_body
= saved_in_function_body
;
22303 parser
->num_template_parameter_lists
22304 = saved_num_template_parameter_lists
;
22305 parser
->in_unbraced_linkage_specification_p
22306 = saved_in_unbraced_linkage_specification_p
;
22312 cp_parser_class_specifier (cp_parser
* parser
)
22315 timevar_push (TV_PARSE_STRUCT
);
22316 ret
= cp_parser_class_specifier_1 (parser
);
22317 timevar_pop (TV_PARSE_STRUCT
);
22321 /* Parse a class-head.
22324 class-key identifier [opt] base-clause [opt]
22325 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22326 class-key nested-name-specifier [opt] template-id
22329 class-virt-specifier:
22333 class-key attributes identifier [opt] base-clause [opt]
22334 class-key attributes nested-name-specifier identifier base-clause [opt]
22335 class-key attributes nested-name-specifier [opt] template-id
22338 Upon return BASES is initialized to the list of base classes (or
22339 NULL, if there are none) in the same form returned by
22340 cp_parser_base_clause.
22342 Returns the TYPE of the indicated class. Sets
22343 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22344 involving a nested-name-specifier was used, and FALSE otherwise.
22346 Returns error_mark_node if this is not a class-head.
22348 Returns NULL_TREE if the class-head is syntactically valid, but
22349 semantically invalid in a way that means we should skip the entire
22350 body of the class. */
22353 cp_parser_class_head (cp_parser
* parser
,
22354 bool* nested_name_specifier_p
)
22356 tree nested_name_specifier
;
22357 enum tag_types class_key
;
22358 tree id
= NULL_TREE
;
22359 tree type
= NULL_TREE
;
22362 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22363 bool template_id_p
= false;
22364 bool qualified_p
= false;
22365 bool invalid_nested_name_p
= false;
22366 bool invalid_explicit_specialization_p
= false;
22367 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22368 tree pushed_scope
= NULL_TREE
;
22369 unsigned num_templates
;
22370 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22371 /* Assume no nested-name-specifier will be present. */
22372 *nested_name_specifier_p
= false;
22373 /* Assume no template parameter lists will be used in defining the
22376 parser
->colon_corrects_to_scope_p
= false;
22378 /* Look for the class-key. */
22379 class_key
= cp_parser_class_key (parser
);
22380 if (class_key
== none_type
)
22381 return error_mark_node
;
22383 location_t class_head_start_location
= input_location
;
22385 /* Parse the attributes. */
22386 attributes
= cp_parser_attributes_opt (parser
);
22388 /* If the next token is `::', that is invalid -- but sometimes
22389 people do try to write:
22393 Handle this gracefully by accepting the extra qualifier, and then
22394 issuing an error about it later if this really is a
22395 class-head. If it turns out just to be an elaborated type
22396 specifier, remain silent. */
22397 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22398 qualified_p
= true;
22400 push_deferring_access_checks (dk_no_check
);
22402 /* Determine the name of the class. Begin by looking for an
22403 optional nested-name-specifier. */
22404 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22405 nested_name_specifier
22406 = cp_parser_nested_name_specifier_opt (parser
,
22407 /*typename_keyword_p=*/false,
22408 /*check_dependency_p=*/false,
22410 /*is_declaration=*/false);
22411 /* If there was a nested-name-specifier, then there *must* be an
22413 if (nested_name_specifier
)
22415 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22416 /* Although the grammar says `identifier', it really means
22417 `class-name' or `template-name'. You are only allowed to
22418 define a class that has already been declared with this
22421 The proposed resolution for Core Issue 180 says that wherever
22422 you see `class T::X' you should treat `X' as a type-name.
22424 It is OK to define an inaccessible class; for example:
22426 class A { class B; };
22429 We do not know if we will see a class-name, or a
22430 template-name. We look for a class-name first, in case the
22431 class-name is a template-id; if we looked for the
22432 template-name first we would stop after the template-name. */
22433 cp_parser_parse_tentatively (parser
);
22434 type
= cp_parser_class_name (parser
,
22435 /*typename_keyword_p=*/false,
22436 /*template_keyword_p=*/false,
22438 /*check_dependency_p=*/false,
22439 /*class_head_p=*/true,
22440 /*is_declaration=*/false);
22441 /* If that didn't work, ignore the nested-name-specifier. */
22442 if (!cp_parser_parse_definitely (parser
))
22444 invalid_nested_name_p
= true;
22445 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22446 id
= cp_parser_identifier (parser
);
22447 if (id
== error_mark_node
)
22450 /* If we could not find a corresponding TYPE, treat this
22451 declaration like an unqualified declaration. */
22452 if (type
== error_mark_node
)
22453 nested_name_specifier
= NULL_TREE
;
22454 /* Otherwise, count the number of templates used in TYPE and its
22455 containing scopes. */
22460 for (scope
= TREE_TYPE (type
);
22461 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22462 scope
= get_containing_scope (scope
))
22464 && CLASS_TYPE_P (scope
)
22465 && CLASSTYPE_TEMPLATE_INFO (scope
)
22466 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22467 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22468 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22472 /* Otherwise, the identifier is optional. */
22475 /* We don't know whether what comes next is a template-id,
22476 an identifier, or nothing at all. */
22477 cp_parser_parse_tentatively (parser
);
22478 /* Check for a template-id. */
22479 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22480 id
= cp_parser_template_id (parser
,
22481 /*template_keyword_p=*/false,
22482 /*check_dependency_p=*/true,
22484 /*is_declaration=*/true);
22485 /* If that didn't work, it could still be an identifier. */
22486 if (!cp_parser_parse_definitely (parser
))
22488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22490 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22491 id
= cp_parser_identifier (parser
);
22498 template_id_p
= true;
22503 pop_deferring_access_checks ();
22507 cp_parser_check_for_invalid_template_id (parser
, id
,
22509 type_start_token
->location
);
22511 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22513 /* If it's not a `:' or a `{' then we can't really be looking at a
22514 class-head, since a class-head only appears as part of a
22515 class-specifier. We have to detect this situation before calling
22516 xref_tag, since that has irreversible side-effects. */
22517 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22519 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22520 type
= error_mark_node
;
22524 /* At this point, we're going ahead with the class-specifier, even
22525 if some other problem occurs. */
22526 cp_parser_commit_to_tentative_parse (parser
);
22527 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22529 cp_parser_error (parser
,
22530 "cannot specify %<override%> for a class");
22531 type
= error_mark_node
;
22534 /* Issue the error about the overly-qualified name now. */
22537 cp_parser_error (parser
,
22538 "global qualification of class name is invalid");
22539 type
= error_mark_node
;
22542 else if (invalid_nested_name_p
)
22544 cp_parser_error (parser
,
22545 "qualified name does not name a class");
22546 type
= error_mark_node
;
22549 else if (nested_name_specifier
)
22553 /* Reject typedef-names in class heads. */
22554 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22556 error_at (type_start_token
->location
,
22557 "invalid class name in declaration of %qD",
22563 /* Figure out in what scope the declaration is being placed. */
22564 scope
= current_scope ();
22565 /* If that scope does not contain the scope in which the
22566 class was originally declared, the program is invalid. */
22567 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22569 if (at_namespace_scope_p ())
22570 error_at (type_start_token
->location
,
22571 "declaration of %qD in namespace %qD which does not "
22573 type
, scope
, nested_name_specifier
);
22575 error_at (type_start_token
->location
,
22576 "declaration of %qD in %qD which does not enclose %qD",
22577 type
, scope
, nested_name_specifier
);
22583 A declarator-id shall not be qualified except for the
22584 definition of a ... nested class outside of its class
22585 ... [or] the definition or explicit instantiation of a
22586 class member of a namespace outside of its namespace. */
22587 if (scope
== nested_name_specifier
)
22589 permerror (nested_name_specifier_token_start
->location
,
22590 "extra qualification not allowed");
22591 nested_name_specifier
= NULL_TREE
;
22595 /* An explicit-specialization must be preceded by "template <>". If
22596 it is not, try to recover gracefully. */
22597 if (at_namespace_scope_p ()
22598 && parser
->num_template_parameter_lists
== 0
22599 && !processing_template_parmlist
22602 /* Build a location of this form:
22603 struct typename <ARGS>
22604 ^~~~~~~~~~~~~~~~~~~~~~
22605 with caret==start at the start token, and
22606 finishing at the end of the type. */
22607 location_t reported_loc
22608 = make_location (class_head_start_location
,
22609 class_head_start_location
,
22610 get_finish (type_start_token
->location
));
22611 rich_location
richloc (line_table
, reported_loc
);
22612 richloc
.add_fixit_insert_before (class_head_start_location
,
22616 "an explicit specialization must be preceded by %<template <>%>");
22617 invalid_explicit_specialization_p
= true;
22618 /* Take the same action that would have been taken by
22619 cp_parser_explicit_specialization. */
22620 ++parser
->num_template_parameter_lists
;
22621 begin_specialization ();
22623 /* There must be no "return" statements between this point and the
22624 end of this function; set "type "to the correct return value and
22625 use "goto done;" to return. */
22626 /* Make sure that the right number of template parameters were
22628 if (!cp_parser_check_template_parameters (parser
, num_templates
,
22629 type_start_token
->location
,
22630 /*declarator=*/NULL
))
22632 /* If something went wrong, there is no point in even trying to
22633 process the class-definition. */
22638 /* Look up the type. */
22641 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
22642 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
22643 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
22645 error_at (type_start_token
->location
,
22646 "function template %qD redeclared as a class template", id
);
22647 type
= error_mark_node
;
22651 type
= TREE_TYPE (id
);
22652 type
= maybe_process_partial_specialization (type
);
22654 /* Check the scope while we still know whether or not we had a
22655 nested-name-specifier. */
22656 if (type
!= error_mark_node
)
22657 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
22659 if (nested_name_specifier
)
22660 pushed_scope
= push_scope (nested_name_specifier
);
22662 else if (nested_name_specifier
)
22668 template <typename T> struct S { struct T };
22669 template <typename T> struct S<T>::T { };
22671 we will get a TYPENAME_TYPE when processing the definition of
22672 `S::T'. We need to resolve it to the actual type before we
22673 try to define it. */
22674 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
22676 class_type
= resolve_typename_type (TREE_TYPE (type
),
22677 /*only_current_p=*/false);
22678 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
22679 type
= TYPE_NAME (class_type
);
22682 cp_parser_error (parser
, "could not resolve typename type");
22683 type
= error_mark_node
;
22687 if (maybe_process_partial_specialization (TREE_TYPE (type
))
22688 == error_mark_node
)
22694 class_type
= current_class_type
;
22695 /* Enter the scope indicated by the nested-name-specifier. */
22696 pushed_scope
= push_scope (nested_name_specifier
);
22697 /* Get the canonical version of this type. */
22698 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
22699 /* Call push_template_decl if it seems like we should be defining a
22700 template either from the template headers or the type we're
22701 defining, so that we diagnose both extra and missing headers. */
22702 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22703 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
22704 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
22706 type
= push_template_decl (type
);
22707 if (type
== error_mark_node
)
22714 type
= TREE_TYPE (type
);
22715 *nested_name_specifier_p
= true;
22717 else /* The name is not a nested name. */
22719 /* If the class was unnamed, create a dummy name. */
22721 id
= make_anon_name ();
22722 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
22723 parser
->num_template_parameter_lists
);
22726 /* Indicate whether this class was declared as a `class' or as a
22728 if (TREE_CODE (type
) == RECORD_TYPE
)
22729 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
22730 cp_parser_check_class_key (class_key
, type
);
22732 /* If this type was already complete, and we see another definition,
22733 that's an error. */
22734 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
22736 error_at (type_start_token
->location
, "redefinition of %q#T",
22738 inform (location_of (type
), "previous definition of %q#T",
22743 else if (type
== error_mark_node
)
22748 /* Apply attributes now, before any use of the class as a template
22749 argument in its base list. */
22750 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
22751 fixup_attribute_variants (type
);
22754 /* We will have entered the scope containing the class; the names of
22755 base classes should be looked up in that context. For example:
22757 struct A { struct B {}; struct C; };
22758 struct A::C : B {};
22762 /* Get the list of base-classes, if there is one. */
22763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22765 /* PR59482: enter the class scope so that base-specifiers are looked
22769 bases
= cp_parser_base_clause (parser
);
22770 /* PR59482: get out of the previously pushed class scope so that the
22771 subsequent pops pop the right thing. */
22778 /* If we're really defining a class, process the base classes.
22779 If they're invalid, fail. */
22780 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22781 xref_basetypes (type
, bases
);
22784 /* Leave the scope given by the nested-name-specifier. We will
22785 enter the class scope itself while processing the members. */
22787 pop_scope (pushed_scope
);
22789 if (invalid_explicit_specialization_p
)
22791 end_specialization ();
22792 --parser
->num_template_parameter_lists
;
22796 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
22797 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
22798 CLASSTYPE_FINAL (type
) = 1;
22800 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22804 /* Parse a class-key.
22811 Returns the kind of class-key specified, or none_type to indicate
22814 static enum tag_types
22815 cp_parser_class_key (cp_parser
* parser
)
22818 enum tag_types tag_type
;
22820 /* Look for the class-key. */
22821 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
22825 /* Check to see if the TOKEN is a class-key. */
22826 tag_type
= cp_parser_token_is_class_key (token
);
22828 cp_parser_error (parser
, "expected class-key");
22832 /* Parse a type-parameter-key.
22834 type-parameter-key:
22840 cp_parser_type_parameter_key (cp_parser
* parser
)
22842 /* Look for the type-parameter-key. */
22843 enum tag_types tag_type
= none_type
;
22844 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22845 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
22847 cp_lexer_consume_token (parser
->lexer
);
22848 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
22849 /* typename is not allowed in a template template parameter
22850 by the standard until C++1Z. */
22851 pedwarn (token
->location
, OPT_Wpedantic
,
22852 "ISO C++ forbids typename key in template template parameter;"
22853 " use -std=c++1z or -std=gnu++1z");
22856 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
22861 /* Parse an (optional) member-specification.
22863 member-specification:
22864 member-declaration member-specification [opt]
22865 access-specifier : member-specification [opt] */
22868 cp_parser_member_specification_opt (cp_parser
* parser
)
22875 /* Peek at the next token. */
22876 token
= cp_lexer_peek_token (parser
->lexer
);
22877 /* If it's a `}', or EOF then we've seen all the members. */
22878 if (token
->type
== CPP_CLOSE_BRACE
22879 || token
->type
== CPP_EOF
22880 || token
->type
== CPP_PRAGMA_EOL
)
22883 /* See if this token is a keyword. */
22884 keyword
= token
->keyword
;
22888 case RID_PROTECTED
:
22890 /* Consume the access-specifier. */
22891 cp_lexer_consume_token (parser
->lexer
);
22892 /* Remember which access-specifier is active. */
22893 current_access_specifier
= token
->u
.value
;
22894 /* Look for the `:'. */
22895 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22899 /* Accept #pragmas at class scope. */
22900 if (token
->type
== CPP_PRAGMA
)
22902 cp_parser_pragma (parser
, pragma_member
, NULL
);
22906 /* Otherwise, the next construction must be a
22907 member-declaration. */
22908 cp_parser_member_declaration (parser
);
22913 /* Parse a member-declaration.
22915 member-declaration:
22916 decl-specifier-seq [opt] member-declarator-list [opt] ;
22917 function-definition ; [opt]
22918 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22920 template-declaration
22923 member-declarator-list:
22925 member-declarator-list , member-declarator
22928 declarator pure-specifier [opt]
22929 declarator constant-initializer [opt]
22930 identifier [opt] : constant-expression
22934 member-declaration:
22935 __extension__ member-declaration
22938 declarator attributes [opt] pure-specifier [opt]
22939 declarator attributes [opt] constant-initializer [opt]
22940 identifier [opt] attributes [opt] : constant-expression
22944 member-declaration:
22945 static_assert-declaration */
22948 cp_parser_member_declaration (cp_parser
* parser
)
22950 cp_decl_specifier_seq decl_specifiers
;
22951 tree prefix_attributes
;
22953 int declares_class_or_enum
;
22955 cp_token
*token
= NULL
;
22956 cp_token
*decl_spec_token_start
= NULL
;
22957 cp_token
*initializer_token_start
= NULL
;
22958 int saved_pedantic
;
22959 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22961 /* Check for the `__extension__' keyword. */
22962 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
22965 cp_parser_member_declaration (parser
);
22966 /* Restore the old value of the PEDANTIC flag. */
22967 pedantic
= saved_pedantic
;
22972 /* Check for a template-declaration. */
22973 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22975 /* An explicit specialization here is an error condition, and we
22976 expect the specialization handler to detect and report this. */
22977 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
22978 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
22979 cp_parser_explicit_specialization (parser
);
22981 cp_parser_template_declaration (parser
, /*member_p=*/true);
22985 /* Check for a template introduction. */
22986 else if (cp_parser_template_declaration_after_export (parser
, true))
22989 /* Check for a using-declaration. */
22990 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
22992 if (cxx_dialect
< cxx11
)
22994 /* Parse the using-declaration. */
22995 cp_parser_using_declaration (parser
,
22996 /*access_declaration_p=*/false);
23002 bool alias_decl_expected
;
23003 cp_parser_parse_tentatively (parser
);
23004 decl
= cp_parser_alias_declaration (parser
);
23005 /* Note that if we actually see the '=' token after the
23006 identifier, cp_parser_alias_declaration commits the
23007 tentative parse. In that case, we really expect an
23008 alias-declaration. Otherwise, we expect a using
23010 alias_decl_expected
=
23011 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23012 cp_parser_parse_definitely (parser
);
23014 if (alias_decl_expected
)
23015 finish_member_declaration (decl
);
23017 cp_parser_using_declaration (parser
,
23018 /*access_declaration_p=*/false);
23023 /* Check for @defs. */
23024 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23027 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23028 ivar
= ivar_chains
;
23032 ivar
= TREE_CHAIN (member
);
23033 TREE_CHAIN (member
) = NULL_TREE
;
23034 finish_member_declaration (member
);
23039 /* If the next token is `static_assert' we have a static assertion. */
23040 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23042 cp_parser_static_assert (parser
, /*member_p=*/true);
23046 parser
->colon_corrects_to_scope_p
= false;
23048 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23051 /* Parse the decl-specifier-seq. */
23052 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23053 cp_parser_decl_specifier_seq (parser
,
23054 CP_PARSER_FLAGS_OPTIONAL
,
23056 &declares_class_or_enum
);
23057 /* Check for an invalid type-name. */
23058 if (!decl_specifiers
.any_type_specifiers_p
23059 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23061 /* If there is no declarator, then the decl-specifier-seq should
23063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23065 /* If there was no decl-specifier-seq, and the next token is a
23066 `;', then we have something like:
23072 Each member-declaration shall declare at least one member
23073 name of the class. */
23074 if (!decl_specifiers
.any_specifiers_p
)
23076 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23077 if (!in_system_header_at (token
->location
))
23078 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
23084 /* See if this declaration is a friend. */
23085 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23086 /* If there were decl-specifiers, check to see if there was
23087 a class-declaration. */
23088 type
= check_tag_decl (&decl_specifiers
,
23089 /*explicit_type_instantiation_p=*/false);
23090 /* Nested classes have already been added to the class, but
23091 a `friend' needs to be explicitly registered. */
23094 /* If the `friend' keyword was present, the friend must
23095 be introduced with a class-key. */
23096 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23097 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23098 "in C++03 a class-key must be used "
23099 "when declaring a friend");
23102 template <typename T> struct A {
23103 friend struct A<T>::B;
23106 A<T>::B will be represented by a TYPENAME_TYPE, and
23107 therefore not recognized by check_tag_decl. */
23110 type
= decl_specifiers
.type
;
23111 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23112 type
= TREE_TYPE (type
);
23114 if (!type
|| !TYPE_P (type
))
23115 error_at (decl_spec_token_start
->location
,
23116 "friend declaration does not name a class or "
23119 make_friend_class (current_class_type
, type
,
23120 /*complain=*/true);
23122 /* If there is no TYPE, an error message will already have
23124 else if (!type
|| type
== error_mark_node
)
23126 /* An anonymous aggregate has to be handled specially; such
23127 a declaration really declares a data member (with a
23128 particular type), as opposed to a nested class. */
23129 else if (ANON_AGGR_TYPE_P (type
))
23132 if (decl_specifiers
.storage_class
!= sc_none
)
23133 error_at (decl_spec_token_start
->location
,
23134 "a storage class on an anonymous aggregate "
23135 "in class scope is not allowed");
23137 /* Remove constructors and such from TYPE, now that we
23138 know it is an anonymous aggregate. */
23139 fixup_anonymous_aggr (type
);
23140 /* And make the corresponding data member. */
23141 decl
= build_decl (decl_spec_token_start
->location
,
23142 FIELD_DECL
, NULL_TREE
, type
);
23143 /* Add it to the class. */
23144 finish_member_declaration (decl
);
23147 cp_parser_check_access_in_redeclaration
23149 decl_spec_token_start
->location
);
23154 bool assume_semicolon
= false;
23156 /* Clear attributes from the decl_specifiers but keep them
23157 around as prefix attributes that apply them to the entity
23159 prefix_attributes
= decl_specifiers
.attributes
;
23160 decl_specifiers
.attributes
= NULL_TREE
;
23162 /* See if these declarations will be friends. */
23163 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23165 /* Keep going until we hit the `;' at the end of the
23167 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23169 tree attributes
= NULL_TREE
;
23170 tree first_attribute
;
23172 /* Peek at the next token. */
23173 token
= cp_lexer_peek_token (parser
->lexer
);
23175 /* Check for a bitfield declaration. */
23176 if (token
->type
== CPP_COLON
23177 || (token
->type
== CPP_NAME
23178 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23184 /* Get the name of the bitfield. Note that we cannot just
23185 check TOKEN here because it may have been invalidated by
23186 the call to cp_lexer_peek_nth_token above. */
23187 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
23188 identifier
= cp_parser_identifier (parser
);
23190 identifier
= NULL_TREE
;
23192 /* Consume the `:' token. */
23193 cp_lexer_consume_token (parser
->lexer
);
23194 /* Get the width of the bitfield. */
23196 = cp_parser_constant_expression (parser
);
23198 /* Look for attributes that apply to the bitfield. */
23199 attributes
= cp_parser_attributes_opt (parser
);
23200 /* Remember which attributes are prefix attributes and
23202 first_attribute
= attributes
;
23203 /* Combine the attributes. */
23204 attributes
= chainon (prefix_attributes
, attributes
);
23206 /* Create the bitfield declaration. */
23207 decl
= grokbitfield (identifier
23208 ? make_id_declarator (NULL_TREE
,
23218 cp_declarator
*declarator
;
23220 tree asm_specification
;
23221 int ctor_dtor_or_conv_p
;
23223 /* Parse the declarator. */
23225 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23226 &ctor_dtor_or_conv_p
,
23227 /*parenthesized_p=*/NULL
,
23231 /* If something went wrong parsing the declarator, make sure
23232 that we at least consume some tokens. */
23233 if (declarator
== cp_error_declarator
)
23235 /* Skip to the end of the statement. */
23236 cp_parser_skip_to_end_of_statement (parser
);
23237 /* If the next token is not a semicolon, that is
23238 probably because we just skipped over the body of
23239 a function. So, we consume a semicolon if
23240 present, but do not issue an error message if it
23242 if (cp_lexer_next_token_is (parser
->lexer
,
23244 cp_lexer_consume_token (parser
->lexer
);
23248 if (declares_class_or_enum
& 2)
23249 cp_parser_check_for_definition_in_return_type
23250 (declarator
, decl_specifiers
.type
,
23251 decl_specifiers
.locations
[ds_type_spec
]);
23253 /* Look for an asm-specification. */
23254 asm_specification
= cp_parser_asm_specification_opt (parser
);
23255 /* Look for attributes that apply to the declaration. */
23256 attributes
= cp_parser_attributes_opt (parser
);
23257 /* Remember which attributes are prefix attributes and
23259 first_attribute
= attributes
;
23260 /* Combine the attributes. */
23261 attributes
= chainon (prefix_attributes
, attributes
);
23263 /* If it's an `=', then we have a constant-initializer or a
23264 pure-specifier. It is not correct to parse the
23265 initializer before registering the member declaration
23266 since the member declaration should be in scope while
23267 its initializer is processed. However, the rest of the
23268 front end does not yet provide an interface that allows
23269 us to handle this correctly. */
23270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23274 A pure-specifier shall be used only in the declaration of
23275 a virtual function.
23277 A member-declarator can contain a constant-initializer
23278 only if it declares a static member of integral or
23281 Therefore, if the DECLARATOR is for a function, we look
23282 for a pure-specifier; otherwise, we look for a
23283 constant-initializer. When we call `grokfield', it will
23284 perform more stringent semantics checks. */
23285 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23286 if (function_declarator_p (declarator
)
23287 || (decl_specifiers
.type
23288 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23289 && declarator
->kind
== cdk_id
23290 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23291 == FUNCTION_TYPE
)))
23292 initializer
= cp_parser_pure_specifier (parser
);
23293 else if (decl_specifiers
.storage_class
!= sc_static
)
23294 initializer
= cp_parser_save_nsdmi (parser
);
23295 else if (cxx_dialect
>= cxx11
)
23298 /* Don't require a constant rvalue in C++11, since we
23299 might want a reference constant. We'll enforce
23300 constancy later. */
23301 cp_lexer_consume_token (parser
->lexer
);
23302 /* Parse the initializer. */
23303 initializer
= cp_parser_initializer_clause (parser
,
23307 /* Parse the initializer. */
23308 initializer
= cp_parser_constant_initializer (parser
);
23310 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23311 && !function_declarator_p (declarator
))
23314 if (decl_specifiers
.storage_class
!= sc_static
)
23315 initializer
= cp_parser_save_nsdmi (parser
);
23317 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23319 /* Otherwise, there is no initializer. */
23321 initializer
= NULL_TREE
;
23323 /* See if we are probably looking at a function
23324 definition. We are certainly not looking at a
23325 member-declarator. Calling `grokfield' has
23326 side-effects, so we must not do it unless we are sure
23327 that we are looking at a member-declarator. */
23328 if (cp_parser_token_starts_function_definition_p
23329 (cp_lexer_peek_token (parser
->lexer
)))
23331 /* The grammar does not allow a pure-specifier to be
23332 used when a member function is defined. (It is
23333 possible that this fact is an oversight in the
23334 standard, since a pure function may be defined
23335 outside of the class-specifier. */
23336 if (initializer
&& initializer_token_start
)
23337 error_at (initializer_token_start
->location
,
23338 "pure-specifier on function-definition");
23339 decl
= cp_parser_save_member_function_body (parser
,
23343 if (parser
->fully_implicit_function_template_p
)
23344 decl
= finish_fully_implicit_template (parser
, decl
);
23345 /* If the member was not a friend, declare it here. */
23347 finish_member_declaration (decl
);
23348 /* Peek at the next token. */
23349 token
= cp_lexer_peek_token (parser
->lexer
);
23350 /* If the next token is a semicolon, consume it. */
23351 if (token
->type
== CPP_SEMICOLON
)
23352 cp_lexer_consume_token (parser
->lexer
);
23356 if (declarator
->kind
== cdk_function
)
23357 declarator
->id_loc
= token
->location
;
23358 /* Create the declaration. */
23359 decl
= grokfield (declarator
, &decl_specifiers
,
23360 initializer
, /*init_const_expr_p=*/true,
23361 asm_specification
, attributes
);
23362 if (parser
->fully_implicit_function_template_p
)
23365 finish_fully_implicit_template (parser
, 0);
23367 decl
= finish_fully_implicit_template (parser
, decl
);
23371 cp_finalize_omp_declare_simd (parser
, decl
);
23372 cp_finalize_oacc_routine (parser
, decl
, false);
23374 /* Reset PREFIX_ATTRIBUTES. */
23375 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23376 attributes
= TREE_CHAIN (attributes
);
23378 TREE_CHAIN (attributes
) = NULL_TREE
;
23380 /* If there is any qualification still in effect, clear it
23381 now; we will be starting fresh with the next declarator. */
23382 parser
->scope
= NULL_TREE
;
23383 parser
->qualifying_scope
= NULL_TREE
;
23384 parser
->object_scope
= NULL_TREE
;
23385 /* If it's a `,', then there are more declarators. */
23386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23388 cp_lexer_consume_token (parser
->lexer
);
23389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23391 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23392 error_at (token
->location
,
23393 "stray %<,%> at end of member declaration");
23396 /* If the next token isn't a `;', then we have a parse error. */
23397 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23400 /* The next token might be a ways away from where the
23401 actual semicolon is missing. Find the previous token
23402 and use that for our error position. */
23403 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23404 error_at (token
->location
,
23405 "expected %<;%> at end of member declaration");
23407 /* Assume that the user meant to provide a semicolon. If
23408 we were to cp_parser_skip_to_end_of_statement, we might
23409 skip to a semicolon inside a member function definition
23410 and issue nonsensical error messages. */
23411 assume_semicolon
= true;
23416 /* Add DECL to the list of members. */
23418 /* Explicitly include, eg, NSDMIs, for better error
23419 recovery (c++/58650). */
23420 || !DECL_DECLARES_FUNCTION_P (decl
))
23421 finish_member_declaration (decl
);
23423 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23424 cp_parser_save_default_args (parser
, decl
);
23425 else if (TREE_CODE (decl
) == FIELD_DECL
23426 && !DECL_C_BIT_FIELD (decl
)
23427 && DECL_INITIAL (decl
))
23428 /* Add DECL to the queue of NSDMI to be parsed later. */
23429 vec_safe_push (unparsed_nsdmis
, decl
);
23432 if (assume_semicolon
)
23437 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23439 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23442 /* Parse a pure-specifier.
23447 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23448 Otherwise, ERROR_MARK_NODE is returned. */
23451 cp_parser_pure_specifier (cp_parser
* parser
)
23455 /* Look for the `=' token. */
23456 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23457 return error_mark_node
;
23458 /* Look for the `0' token. */
23459 token
= cp_lexer_peek_token (parser
->lexer
);
23461 if (token
->type
== CPP_EOF
23462 || token
->type
== CPP_PRAGMA_EOL
)
23463 return error_mark_node
;
23465 cp_lexer_consume_token (parser
->lexer
);
23467 /* Accept = default or = delete in c++0x mode. */
23468 if (token
->keyword
== RID_DEFAULT
23469 || token
->keyword
== RID_DELETE
)
23471 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23472 return token
->u
.value
;
23475 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23476 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23478 cp_parser_error (parser
,
23479 "invalid pure specifier (only %<= 0%> is allowed)");
23480 cp_parser_skip_to_end_of_statement (parser
);
23481 return error_mark_node
;
23483 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23485 error_at (token
->location
, "templates may not be %<virtual%>");
23486 return error_mark_node
;
23489 return integer_zero_node
;
23492 /* Parse a constant-initializer.
23494 constant-initializer:
23495 = constant-expression
23497 Returns a representation of the constant-expression. */
23500 cp_parser_constant_initializer (cp_parser
* parser
)
23502 /* Look for the `=' token. */
23503 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23504 return error_mark_node
;
23506 /* It is invalid to write:
23508 struct S { static const int i = { 7 }; };
23511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23513 cp_parser_error (parser
,
23514 "a brace-enclosed initializer is not allowed here");
23515 /* Consume the opening brace. */
23516 cp_lexer_consume_token (parser
->lexer
);
23517 /* Skip the initializer. */
23518 cp_parser_skip_to_closing_brace (parser
);
23519 /* Look for the trailing `}'. */
23520 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
23522 return error_mark_node
;
23525 return cp_parser_constant_expression (parser
);
23528 /* Derived classes [gram.class.derived] */
23530 /* Parse a base-clause.
23533 : base-specifier-list
23535 base-specifier-list:
23536 base-specifier ... [opt]
23537 base-specifier-list , base-specifier ... [opt]
23539 Returns a TREE_LIST representing the base-classes, in the order in
23540 which they were declared. The representation of each node is as
23541 described by cp_parser_base_specifier.
23543 In the case that no bases are specified, this function will return
23544 NULL_TREE, not ERROR_MARK_NODE. */
23547 cp_parser_base_clause (cp_parser
* parser
)
23549 tree bases
= NULL_TREE
;
23551 /* Look for the `:' that begins the list. */
23552 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23554 /* Scan the base-specifier-list. */
23559 bool pack_expansion_p
= false;
23561 /* Look for the base-specifier. */
23562 base
= cp_parser_base_specifier (parser
);
23563 /* Look for the (optional) ellipsis. */
23564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23566 /* Consume the `...'. */
23567 cp_lexer_consume_token (parser
->lexer
);
23569 pack_expansion_p
= true;
23572 /* Add BASE to the front of the list. */
23573 if (base
&& base
!= error_mark_node
)
23575 if (pack_expansion_p
)
23576 /* Make this a pack expansion type. */
23577 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
23579 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
23581 TREE_CHAIN (base
) = bases
;
23585 /* Peek at the next token. */
23586 token
= cp_lexer_peek_token (parser
->lexer
);
23587 /* If it's not a comma, then the list is complete. */
23588 if (token
->type
!= CPP_COMMA
)
23590 /* Consume the `,'. */
23591 cp_lexer_consume_token (parser
->lexer
);
23594 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23595 base class had a qualified name. However, the next name that
23596 appears is certainly not qualified. */
23597 parser
->scope
= NULL_TREE
;
23598 parser
->qualifying_scope
= NULL_TREE
;
23599 parser
->object_scope
= NULL_TREE
;
23601 return nreverse (bases
);
23604 /* Parse a base-specifier.
23607 :: [opt] nested-name-specifier [opt] class-name
23608 virtual access-specifier [opt] :: [opt] nested-name-specifier
23610 access-specifier virtual [opt] :: [opt] nested-name-specifier
23613 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23614 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23615 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23616 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23619 cp_parser_base_specifier (cp_parser
* parser
)
23623 bool virtual_p
= false;
23624 bool duplicate_virtual_error_issued_p
= false;
23625 bool duplicate_access_error_issued_p
= false;
23626 bool class_scope_p
, template_p
;
23627 tree access
= access_default_node
;
23630 /* Process the optional `virtual' and `access-specifier'. */
23633 /* Peek at the next token. */
23634 token
= cp_lexer_peek_token (parser
->lexer
);
23635 /* Process `virtual'. */
23636 switch (token
->keyword
)
23639 /* If `virtual' appears more than once, issue an error. */
23640 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
23642 cp_parser_error (parser
,
23643 "%<virtual%> specified more than once in base-specified");
23644 duplicate_virtual_error_issued_p
= true;
23649 /* Consume the `virtual' token. */
23650 cp_lexer_consume_token (parser
->lexer
);
23655 case RID_PROTECTED
:
23657 /* If more than one access specifier appears, issue an
23659 if (access
!= access_default_node
23660 && !duplicate_access_error_issued_p
)
23662 cp_parser_error (parser
,
23663 "more than one access specifier in base-specified");
23664 duplicate_access_error_issued_p
= true;
23667 access
= ridpointers
[(int) token
->keyword
];
23669 /* Consume the access-specifier. */
23670 cp_lexer_consume_token (parser
->lexer
);
23679 /* It is not uncommon to see programs mechanically, erroneously, use
23680 the 'typename' keyword to denote (dependent) qualified types
23681 as base classes. */
23682 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23684 token
= cp_lexer_peek_token (parser
->lexer
);
23685 if (!processing_template_decl
)
23686 error_at (token
->location
,
23687 "keyword %<typename%> not allowed outside of templates");
23689 error_at (token
->location
,
23690 "keyword %<typename%> not allowed in this context "
23691 "(the base class is implicitly a type)");
23692 cp_lexer_consume_token (parser
->lexer
);
23695 /* Look for the optional `::' operator. */
23696 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
23697 /* Look for the nested-name-specifier. The simplest way to
23702 The keyword `typename' is not permitted in a base-specifier or
23703 mem-initializer; in these contexts a qualified name that
23704 depends on a template-parameter is implicitly assumed to be a
23707 is to pretend that we have seen the `typename' keyword at this
23709 cp_parser_nested_name_specifier_opt (parser
,
23710 /*typename_keyword_p=*/true,
23711 /*check_dependency_p=*/true,
23713 /*is_declaration=*/true);
23714 /* If the base class is given by a qualified name, assume that names
23715 we see are type names or templates, as appropriate. */
23716 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
23717 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
23720 && cp_lexer_next_token_is_decltype (parser
->lexer
))
23721 /* DR 950 allows decltype as a base-specifier. */
23722 type
= cp_parser_decltype (parser
);
23725 /* Otherwise, look for the class-name. */
23726 type
= cp_parser_class_name (parser
,
23730 /*check_dependency_p=*/true,
23731 /*class_head_p=*/false,
23732 /*is_declaration=*/true);
23733 type
= TREE_TYPE (type
);
23736 if (type
== error_mark_node
)
23737 return error_mark_node
;
23739 return finish_base_specifier (type
, access
, virtual_p
);
23742 /* Exception handling [gram.exception] */
23744 /* Parse an (optional) noexcept-specification.
23746 noexcept-specification:
23747 noexcept ( constant-expression ) [opt]
23749 If no noexcept-specification is present, returns NULL_TREE.
23750 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23751 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23752 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23753 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23754 in which case a boolean condition is returned instead. */
23757 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
23758 bool require_constexpr
,
23759 bool* consumed_expr
,
23763 const char *saved_message
;
23765 /* Peek at the next token. */
23766 token
= cp_lexer_peek_token (parser
->lexer
);
23768 /* Is it a noexcept-specification? */
23769 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
23772 cp_lexer_consume_token (parser
->lexer
);
23774 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
23776 cp_lexer_consume_token (parser
->lexer
);
23778 if (require_constexpr
)
23780 /* Types may not be defined in an exception-specification. */
23781 saved_message
= parser
->type_definition_forbidden_message
;
23782 parser
->type_definition_forbidden_message
23783 = G_("types may not be defined in an exception-specification");
23785 expr
= cp_parser_constant_expression (parser
);
23787 /* Restore the saved message. */
23788 parser
->type_definition_forbidden_message
= saved_message
;
23792 expr
= cp_parser_expression (parser
);
23793 *consumed_expr
= true;
23796 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23800 expr
= boolean_true_node
;
23801 if (!require_constexpr
)
23802 *consumed_expr
= false;
23805 /* We cannot build a noexcept-spec right away because this will check
23806 that expr is a constexpr. */
23808 return build_noexcept_spec (expr
, tf_warning_or_error
);
23816 /* Parse an (optional) exception-specification.
23818 exception-specification:
23819 throw ( type-id-list [opt] )
23821 Returns a TREE_LIST representing the exception-specification. The
23822 TREE_VALUE of each node is a type. */
23825 cp_parser_exception_specification_opt (cp_parser
* parser
)
23829 const char *saved_message
;
23831 /* Peek at the next token. */
23832 token
= cp_lexer_peek_token (parser
->lexer
);
23834 /* Is it a noexcept-specification? */
23835 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
23837 if (type_id_list
!= NULL_TREE
)
23838 return type_id_list
;
23840 /* If it's not `throw', then there's no exception-specification. */
23841 if (!cp_parser_is_keyword (token
, RID_THROW
))
23844 location_t loc
= token
->location
;
23846 /* Consume the `throw'. */
23847 cp_lexer_consume_token (parser
->lexer
);
23849 /* Look for the `('. */
23850 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23852 /* Peek at the next token. */
23853 token
= cp_lexer_peek_token (parser
->lexer
);
23854 /* If it's not a `)', then there is a type-id-list. */
23855 if (token
->type
!= CPP_CLOSE_PAREN
)
23857 /* Types may not be defined in an exception-specification. */
23858 saved_message
= parser
->type_definition_forbidden_message
;
23859 parser
->type_definition_forbidden_message
23860 = G_("types may not be defined in an exception-specification");
23861 /* Parse the type-id-list. */
23862 type_id_list
= cp_parser_type_id_list (parser
);
23863 /* Restore the saved message. */
23864 parser
->type_definition_forbidden_message
= saved_message
;
23866 if (cxx_dialect
>= cxx1z
)
23868 error_at (loc
, "ISO C++1z does not allow dynamic exception "
23870 type_id_list
= NULL_TREE
;
23872 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
23873 warning_at (loc
, OPT_Wdeprecated
,
23874 "dynamic exception specifications are deprecated in C++11;"
23875 " use %<noexcept%> instead");
23877 /* In C++17, throw() is equivalent to noexcept (true). throw()
23878 is deprecated in C++11 and above as well, but is still widely used,
23879 so don't warn about it yet. */
23880 else if (cxx_dialect
>= cxx1z
)
23881 type_id_list
= noexcept_true_spec
;
23883 type_id_list
= empty_except_spec
;
23885 /* Look for the `)'. */
23886 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23888 return type_id_list
;
23891 /* Parse an (optional) type-id-list.
23895 type-id-list , type-id ... [opt]
23897 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23898 in the order that the types were presented. */
23901 cp_parser_type_id_list (cp_parser
* parser
)
23903 tree types
= NULL_TREE
;
23910 token
= cp_lexer_peek_token (parser
->lexer
);
23912 /* Get the next type-id. */
23913 type
= cp_parser_type_id (parser
);
23914 /* Check for invalid 'auto'. */
23915 if (flag_concepts
&& type_uses_auto (type
))
23917 error_at (token
->location
,
23918 "invalid use of %<auto%> in exception-specification");
23919 type
= error_mark_node
;
23921 /* Parse the optional ellipsis. */
23922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23924 /* Consume the `...'. */
23925 cp_lexer_consume_token (parser
->lexer
);
23927 /* Turn the type into a pack expansion expression. */
23928 type
= make_pack_expansion (type
);
23930 /* Add it to the list. */
23931 types
= add_exception_specifier (types
, type
, /*complain=*/1);
23932 /* Peek at the next token. */
23933 token
= cp_lexer_peek_token (parser
->lexer
);
23934 /* If it is not a `,', we are done. */
23935 if (token
->type
!= CPP_COMMA
)
23937 /* Consume the `,'. */
23938 cp_lexer_consume_token (parser
->lexer
);
23941 return nreverse (types
);
23944 /* Parse a try-block.
23947 try compound-statement handler-seq */
23950 cp_parser_try_block (cp_parser
* parser
)
23954 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
23955 if (parser
->in_function_body
23956 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
23957 error ("%<try%> in %<constexpr%> function");
23959 try_block
= begin_try_block ();
23960 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
23961 finish_try_block (try_block
);
23962 cp_parser_handler_seq (parser
);
23963 finish_handler_sequence (try_block
);
23968 /* Parse a function-try-block.
23970 function-try-block:
23971 try ctor-initializer [opt] function-body handler-seq */
23974 cp_parser_function_try_block (cp_parser
* parser
)
23976 tree compound_stmt
;
23978 bool ctor_initializer_p
;
23980 /* Look for the `try' keyword. */
23981 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
23983 /* Let the rest of the front end know where we are. */
23984 try_block
= begin_function_try_block (&compound_stmt
);
23985 /* Parse the function-body. */
23986 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23987 (parser
, /*in_function_try_block=*/true);
23988 /* We're done with the `try' part. */
23989 finish_function_try_block (try_block
);
23990 /* Parse the handlers. */
23991 cp_parser_handler_seq (parser
);
23992 /* We're done with the handlers. */
23993 finish_function_handler_sequence (try_block
, compound_stmt
);
23995 return ctor_initializer_p
;
23998 /* Parse a handler-seq.
24001 handler handler-seq [opt] */
24004 cp_parser_handler_seq (cp_parser
* parser
)
24010 /* Parse the handler. */
24011 cp_parser_handler (parser
);
24012 /* Peek at the next token. */
24013 token
= cp_lexer_peek_token (parser
->lexer
);
24014 /* If it's not `catch' then there are no more handlers. */
24015 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24020 /* Parse a handler.
24023 catch ( exception-declaration ) compound-statement */
24026 cp_parser_handler (cp_parser
* parser
)
24031 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24032 handler
= begin_handler ();
24033 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24034 declaration
= cp_parser_exception_declaration (parser
);
24035 finish_handler_parms (declaration
, handler
);
24036 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24037 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24038 finish_handler (handler
);
24041 /* Parse an exception-declaration.
24043 exception-declaration:
24044 type-specifier-seq declarator
24045 type-specifier-seq abstract-declarator
24049 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24050 ellipsis variant is used. */
24053 cp_parser_exception_declaration (cp_parser
* parser
)
24055 cp_decl_specifier_seq type_specifiers
;
24056 cp_declarator
*declarator
;
24057 const char *saved_message
;
24059 /* If it's an ellipsis, it's easy to handle. */
24060 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24062 /* Consume the `...' token. */
24063 cp_lexer_consume_token (parser
->lexer
);
24067 /* Types may not be defined in exception-declarations. */
24068 saved_message
= parser
->type_definition_forbidden_message
;
24069 parser
->type_definition_forbidden_message
24070 = G_("types may not be defined in exception-declarations");
24072 /* Parse the type-specifier-seq. */
24073 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24074 /*is_trailing_return=*/false,
24076 /* If it's a `)', then there is no declarator. */
24077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24080 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24081 /*ctor_dtor_or_conv_p=*/NULL
,
24082 /*parenthesized_p=*/NULL
,
24083 /*member_p=*/false,
24084 /*friend_p=*/false);
24086 /* Restore the saved message. */
24087 parser
->type_definition_forbidden_message
= saved_message
;
24089 if (!type_specifiers
.any_specifiers_p
)
24090 return error_mark_node
;
24092 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24095 /* Parse a throw-expression.
24098 throw assignment-expression [opt]
24100 Returns a THROW_EXPR representing the throw-expression. */
24103 cp_parser_throw_expression (cp_parser
* parser
)
24108 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24109 token
= cp_lexer_peek_token (parser
->lexer
);
24110 /* Figure out whether or not there is an assignment-expression
24111 following the "throw" keyword. */
24112 if (token
->type
== CPP_COMMA
24113 || token
->type
== CPP_SEMICOLON
24114 || token
->type
== CPP_CLOSE_PAREN
24115 || token
->type
== CPP_CLOSE_SQUARE
24116 || token
->type
== CPP_CLOSE_BRACE
24117 || token
->type
== CPP_COLON
)
24118 expression
= NULL_TREE
;
24120 expression
= cp_parser_assignment_expression (parser
);
24122 return build_throw (expression
);
24125 /* GNU Extensions */
24127 /* Parse an (optional) asm-specification.
24130 asm ( string-literal )
24132 If the asm-specification is present, returns a STRING_CST
24133 corresponding to the string-literal. Otherwise, returns
24137 cp_parser_asm_specification_opt (cp_parser
* parser
)
24140 tree asm_specification
;
24142 /* Peek at the next token. */
24143 token
= cp_lexer_peek_token (parser
->lexer
);
24144 /* If the next token isn't the `asm' keyword, then there's no
24145 asm-specification. */
24146 if (!cp_parser_is_keyword (token
, RID_ASM
))
24149 /* Consume the `asm' token. */
24150 cp_lexer_consume_token (parser
->lexer
);
24151 /* Look for the `('. */
24152 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24154 /* Look for the string-literal. */
24155 asm_specification
= cp_parser_string_literal (parser
, false, false);
24157 /* Look for the `)'. */
24158 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24160 return asm_specification
;
24163 /* Parse an asm-operand-list.
24167 asm-operand-list , asm-operand
24170 string-literal ( expression )
24171 [ string-literal ] string-literal ( expression )
24173 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24174 each node is the expression. The TREE_PURPOSE is itself a
24175 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24176 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24177 is a STRING_CST for the string literal before the parenthesis. Returns
24178 ERROR_MARK_NODE if any of the operands are invalid. */
24181 cp_parser_asm_operand_list (cp_parser
* parser
)
24183 tree asm_operands
= NULL_TREE
;
24184 bool invalid_operands
= false;
24188 tree string_literal
;
24192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24194 /* Consume the `[' token. */
24195 cp_lexer_consume_token (parser
->lexer
);
24196 /* Read the operand name. */
24197 name
= cp_parser_identifier (parser
);
24198 if (name
!= error_mark_node
)
24199 name
= build_string (IDENTIFIER_LENGTH (name
),
24200 IDENTIFIER_POINTER (name
));
24201 /* Look for the closing `]'. */
24202 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24206 /* Look for the string-literal. */
24207 string_literal
= cp_parser_string_literal (parser
, false, false);
24209 /* Look for the `('. */
24210 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24211 /* Parse the expression. */
24212 expression
= cp_parser_expression (parser
);
24213 /* Look for the `)'. */
24214 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24216 if (name
== error_mark_node
24217 || string_literal
== error_mark_node
24218 || expression
== error_mark_node
)
24219 invalid_operands
= true;
24221 /* Add this operand to the list. */
24222 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24225 /* If the next token is not a `,', there are no more
24227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24229 /* Consume the `,'. */
24230 cp_lexer_consume_token (parser
->lexer
);
24233 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24236 /* Parse an asm-clobber-list.
24240 asm-clobber-list , string-literal
24242 Returns a TREE_LIST, indicating the clobbers in the order that they
24243 appeared. The TREE_VALUE of each node is a STRING_CST. */
24246 cp_parser_asm_clobber_list (cp_parser
* parser
)
24248 tree clobbers
= NULL_TREE
;
24252 tree string_literal
;
24254 /* Look for the string literal. */
24255 string_literal
= cp_parser_string_literal (parser
, false, false);
24256 /* Add it to the list. */
24257 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24258 /* If the next token is not a `,', then the list is
24260 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24262 /* Consume the `,' token. */
24263 cp_lexer_consume_token (parser
->lexer
);
24269 /* Parse an asm-label-list.
24273 asm-label-list , identifier
24275 Returns a TREE_LIST, indicating the labels in the order that they
24276 appeared. The TREE_VALUE of each node is a label. */
24279 cp_parser_asm_label_list (cp_parser
* parser
)
24281 tree labels
= NULL_TREE
;
24285 tree identifier
, label
, name
;
24287 /* Look for the identifier. */
24288 identifier
= cp_parser_identifier (parser
);
24289 if (!error_operand_p (identifier
))
24291 label
= lookup_label (identifier
);
24292 if (TREE_CODE (label
) == LABEL_DECL
)
24294 TREE_USED (label
) = 1;
24295 check_goto (label
);
24296 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24297 IDENTIFIER_POINTER (identifier
));
24298 labels
= tree_cons (name
, label
, labels
);
24301 /* If the next token is not a `,', then the list is
24303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24305 /* Consume the `,' token. */
24306 cp_lexer_consume_token (parser
->lexer
);
24309 return nreverse (labels
);
24312 /* Return TRUE iff the next tokens in the stream are possibly the
24313 beginning of a GNU extension attribute. */
24316 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24318 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24321 /* Return TRUE iff the next tokens in the stream are possibly the
24322 beginning of a standard C++-11 attribute specifier. */
24325 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24327 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24330 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24331 beginning of a standard C++-11 attribute specifier. */
24334 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24336 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24338 return (cxx_dialect
>= cxx11
24339 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24340 || (token
->type
== CPP_OPEN_SQUARE
24341 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24342 && token
->type
== CPP_OPEN_SQUARE
)));
24345 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24346 beginning of a GNU extension attribute. */
24349 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24351 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24353 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24356 /* Return true iff the next tokens can be the beginning of either a
24357 GNU attribute list, or a standard C++11 attribute sequence. */
24360 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24362 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24363 || cp_next_tokens_can_be_std_attribute_p (parser
));
24366 /* Return true iff the next Nth tokens can be the beginning of either
24367 a GNU attribute list, or a standard C++11 attribute sequence. */
24370 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24372 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24373 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24376 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24377 of GNU attributes, or return NULL. */
24380 cp_parser_attributes_opt (cp_parser
*parser
)
24382 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24383 return cp_parser_gnu_attributes_opt (parser
);
24384 return cp_parser_std_attribute_spec_seq (parser
);
24387 #define CILK_SIMD_FN_CLAUSE_MASK \
24388 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24389 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24390 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24391 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24392 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24394 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24395 vector [(<clauses>)] */
24398 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
24400 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
24401 cp_token
*token
= v_token
;
24404 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
24405 parser
->cilk_simd_fn_info
->error_seen
= false;
24406 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
24407 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
24408 parser
->cilk_simd_fn_info
->clauses
= NULL_TREE
;
24410 int paren_scope
= 0;
24411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24413 cp_lexer_consume_token (parser
->lexer
);
24414 v_token
= cp_lexer_peek_token (parser
->lexer
);
24417 while (paren_scope
> 0)
24419 token
= cp_lexer_peek_token (parser
->lexer
);
24420 if (token
->type
== CPP_OPEN_PAREN
)
24422 else if (token
->type
== CPP_CLOSE_PAREN
)
24424 /* Do not push the last ')' */
24425 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
24426 cp_lexer_consume_token (parser
->lexer
);
24429 token
->type
= CPP_PRAGMA_EOL
;
24430 parser
->lexer
->next_token
= token
;
24431 cp_lexer_consume_token (parser
->lexer
);
24433 struct cp_token_cache
*cp
24434 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
24435 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
24438 /* Parse an (optional) series of attributes.
24441 attributes attribute
24444 __attribute__ (( attribute-list [opt] ))
24446 The return value is as for cp_parser_gnu_attribute_list. */
24449 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24451 tree attributes
= NULL_TREE
;
24456 tree attribute_list
;
24459 /* Peek at the next token. */
24460 token
= cp_lexer_peek_token (parser
->lexer
);
24461 /* If it's not `__attribute__', then we're done. */
24462 if (token
->keyword
!= RID_ATTRIBUTE
)
24465 /* Consume the `__attribute__' keyword. */
24466 cp_lexer_consume_token (parser
->lexer
);
24467 /* Look for the two `(' tokens. */
24468 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24469 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24471 /* Peek at the next token. */
24472 token
= cp_lexer_peek_token (parser
->lexer
);
24473 if (token
->type
!= CPP_CLOSE_PAREN
)
24474 /* Parse the attribute-list. */
24475 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24477 /* If the next token is a `)', then there is no attribute
24479 attribute_list
= NULL
;
24481 /* Look for the two `)' tokens. */
24482 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24484 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24487 cp_parser_skip_to_end_of_statement (parser
);
24489 /* Add these new attributes to the list. */
24490 attributes
= chainon (attributes
, attribute_list
);
24496 /* Parse a GNU attribute-list.
24500 attribute-list , attribute
24504 identifier ( identifier )
24505 identifier ( identifier , expression-list )
24506 identifier ( expression-list )
24508 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24509 to an attribute. The TREE_PURPOSE of each node is the identifier
24510 indicating which attribute is in use. The TREE_VALUE represents
24511 the arguments, if any. */
24514 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24516 tree attribute_list
= NULL_TREE
;
24517 bool save_translate_strings_p
= parser
->translate_strings_p
;
24519 parser
->translate_strings_p
= false;
24526 /* Look for the identifier. We also allow keywords here; for
24527 example `__attribute__ ((const))' is legal. */
24528 token
= cp_lexer_peek_token (parser
->lexer
);
24529 if (token
->type
== CPP_NAME
24530 || token
->type
== CPP_KEYWORD
)
24532 tree arguments
= NULL_TREE
;
24534 /* Consume the token, but save it since we need it for the
24535 SIMD enabled function parsing. */
24536 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24538 /* Save away the identifier that indicates which attribute
24540 identifier
= (token
->type
== CPP_KEYWORD
)
24541 /* For keywords, use the canonical spelling, not the
24542 parsed identifier. */
24543 ? ridpointers
[(int) token
->keyword
]
24544 : id_token
->u
.value
;
24546 attribute
= build_tree_list (identifier
, NULL_TREE
);
24548 /* Peek at the next token. */
24549 token
= cp_lexer_peek_token (parser
->lexer
);
24550 /* If it's an `(', then parse the attribute arguments. */
24551 if (token
->type
== CPP_OPEN_PAREN
)
24553 vec
<tree
, va_gc
> *vec
;
24554 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24555 ? id_attr
: normal_attr
);
24556 if (is_cilkplus_vector_p (identifier
))
24558 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24562 vec
= cp_parser_parenthesized_expression_list
24563 (parser
, attr_flag
, /*cast_p=*/false,
24564 /*allow_expansion_p=*/false,
24565 /*non_constant_p=*/NULL
);
24567 arguments
= error_mark_node
;
24570 arguments
= build_tree_list_vec (vec
);
24571 release_tree_vector (vec
);
24573 /* Save the arguments away. */
24574 TREE_VALUE (attribute
) = arguments
;
24576 else if (is_cilkplus_vector_p (identifier
))
24578 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
24582 if (arguments
!= error_mark_node
)
24584 /* Add this attribute to the list. */
24585 TREE_CHAIN (attribute
) = attribute_list
;
24586 attribute_list
= attribute
;
24589 token
= cp_lexer_peek_token (parser
->lexer
);
24591 /* Now, look for more attributes. If the next token isn't a
24592 `,', we're done. */
24593 if (token
->type
!= CPP_COMMA
)
24596 /* Consume the comma and keep going. */
24597 cp_lexer_consume_token (parser
->lexer
);
24599 parser
->translate_strings_p
= save_translate_strings_p
;
24601 /* We built up the list in reverse order. */
24602 return nreverse (attribute_list
);
24605 /* Parse a standard C++11 attribute.
24607 The returned representation is a TREE_LIST which TREE_PURPOSE is
24608 the scoped name of the attribute, and the TREE_VALUE is its
24611 Note that the scoped name of the attribute is itself a TREE_LIST
24612 which TREE_PURPOSE is the namespace of the attribute, and
24613 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24614 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24615 and which TREE_PURPOSE is directly the attribute name.
24617 Clients of the attribute code should use get_attribute_namespace
24618 and get_attribute_name to get the actual namespace and name of
24619 attributes, regardless of their being GNU or C++11 attributes.
24622 attribute-token attribute-argument-clause [opt]
24626 attribute-scoped-token
24628 attribute-scoped-token:
24629 attribute-namespace :: identifier
24631 attribute-namespace:
24634 attribute-argument-clause:
24635 ( balanced-token-seq )
24637 balanced-token-seq:
24638 balanced-token [opt]
24639 balanced-token-seq balanced-token
24642 ( balanced-token-seq )
24643 [ balanced-token-seq ]
24644 { balanced-token-seq }. */
24647 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
24649 tree attribute
, attr_id
= NULL_TREE
, arguments
;
24652 /* First, parse name of the attribute, a.k.a attribute-token. */
24654 token
= cp_lexer_peek_token (parser
->lexer
);
24655 if (token
->type
== CPP_NAME
)
24656 attr_id
= token
->u
.value
;
24657 else if (token
->type
== CPP_KEYWORD
)
24658 attr_id
= ridpointers
[(int) token
->keyword
];
24659 else if (token
->flags
& NAMED_OP
)
24660 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24662 if (attr_id
== NULL_TREE
)
24665 cp_lexer_consume_token (parser
->lexer
);
24667 token
= cp_lexer_peek_token (parser
->lexer
);
24668 if (token
->type
== CPP_SCOPE
)
24670 /* We are seeing a scoped attribute token. */
24672 cp_lexer_consume_token (parser
->lexer
);
24674 error_at (token
->location
, "attribute using prefix used together "
24675 "with scoped attribute token");
24678 token
= cp_lexer_consume_token (parser
->lexer
);
24679 if (token
->type
== CPP_NAME
)
24680 attr_id
= token
->u
.value
;
24681 else if (token
->type
== CPP_KEYWORD
)
24682 attr_id
= ridpointers
[(int) token
->keyword
];
24683 else if (token
->flags
& NAMED_OP
)
24684 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
24687 error_at (token
->location
,
24688 "expected an identifier for the attribute name");
24689 return error_mark_node
;
24691 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24693 token
= cp_lexer_peek_token (parser
->lexer
);
24696 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
24700 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
24702 /* C++11 noreturn attribute is equivalent to GNU's. */
24703 if (is_attribute_p ("noreturn", attr_id
))
24704 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24705 /* C++14 deprecated attribute is equivalent to GNU's. */
24706 else if (is_attribute_p ("deprecated", attr_id
))
24708 if (cxx_dialect
== cxx11
)
24709 pedwarn (token
->location
, OPT_Wpedantic
,
24710 "%<deprecated%> is a C++14 feature;"
24711 " use %<gnu::deprecated%>");
24712 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24714 /* C++17 fallthrough attribute is equivalent to GNU's. */
24715 else if (is_attribute_p ("fallthrough", attr_id
))
24717 if (cxx_dialect
< cxx1z
)
24718 pedwarn (token
->location
, OPT_Wpedantic
,
24719 "%<fallthrough%> is a C++17 feature;"
24720 " use %<gnu::fallthrough%>");
24721 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
24723 /* Transactional Memory TS optimize_for_synchronized attribute is
24724 equivalent to GNU transaction_callable. */
24725 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
24726 TREE_PURPOSE (attribute
)
24727 = get_identifier ("transaction_callable");
24728 /* Transactional Memory attributes are GNU attributes. */
24729 else if (tm_attr_to_mask (attr_id
))
24730 TREE_PURPOSE (attribute
) = attr_id
;
24733 /* Now parse the optional argument clause of the attribute. */
24735 if (token
->type
!= CPP_OPEN_PAREN
)
24739 vec
<tree
, va_gc
> *vec
;
24740 int attr_flag
= normal_attr
;
24742 if (attr_ns
== get_identifier ("gnu")
24743 && attribute_takes_identifier_p (attr_id
))
24744 /* A GNU attribute that takes an identifier in parameter. */
24745 attr_flag
= id_attr
;
24747 vec
= cp_parser_parenthesized_expression_list
24748 (parser
, attr_flag
, /*cast_p=*/false,
24749 /*allow_expansion_p=*/true,
24750 /*non_constant_p=*/NULL
);
24752 arguments
= error_mark_node
;
24755 arguments
= build_tree_list_vec (vec
);
24756 release_tree_vector (vec
);
24759 if (arguments
== error_mark_node
)
24760 attribute
= error_mark_node
;
24762 TREE_VALUE (attribute
) = arguments
;
24768 /* Check that the attribute ATTRIBUTE appears at most once in the
24769 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24770 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24771 isn't implemented yet in GCC. */
24774 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
24778 tree name
= get_attribute_name (attribute
);
24779 if (is_attribute_p ("noreturn", name
)
24780 && lookup_attribute ("noreturn", attributes
))
24781 error ("attribute %<noreturn%> can appear at most once "
24782 "in an attribute-list");
24783 else if (is_attribute_p ("deprecated", name
)
24784 && lookup_attribute ("deprecated", attributes
))
24785 error ("attribute %<deprecated%> can appear at most once "
24786 "in an attribute-list");
24790 /* Parse a list of standard C++-11 attributes.
24794 attribute-list , attribute[opt]
24796 attribute-list , attribute ...
24800 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
24802 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
24803 cp_token
*token
= NULL
;
24807 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
24808 if (attribute
== error_mark_node
)
24810 if (attribute
!= NULL_TREE
)
24812 cp_parser_check_std_attribute (attributes
, attribute
);
24813 TREE_CHAIN (attribute
) = attributes
;
24814 attributes
= attribute
;
24816 token
= cp_lexer_peek_token (parser
->lexer
);
24817 if (token
->type
== CPP_ELLIPSIS
)
24819 cp_lexer_consume_token (parser
->lexer
);
24820 if (attribute
== NULL_TREE
)
24821 error_at (token
->location
,
24822 "expected attribute before %<...%>");
24824 TREE_VALUE (attribute
)
24825 = make_pack_expansion (TREE_VALUE (attribute
));
24826 token
= cp_lexer_peek_token (parser
->lexer
);
24828 if (token
->type
!= CPP_COMMA
)
24830 cp_lexer_consume_token (parser
->lexer
);
24832 attributes
= nreverse (attributes
);
24836 /* Parse a standard C++-11 attribute specifier.
24838 attribute-specifier:
24839 [ [ attribute-using-prefix [opt] attribute-list ] ]
24840 alignment-specifier
24842 attribute-using-prefix:
24843 using attribute-namespace :
24845 alignment-specifier:
24846 alignas ( type-id ... [opt] )
24847 alignas ( alignment-expression ... [opt] ). */
24850 cp_parser_std_attribute_spec (cp_parser
*parser
)
24852 tree attributes
= NULL_TREE
;
24853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24855 if (token
->type
== CPP_OPEN_SQUARE
24856 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
24858 tree attr_ns
= NULL_TREE
;
24860 cp_lexer_consume_token (parser
->lexer
);
24861 cp_lexer_consume_token (parser
->lexer
);
24863 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24865 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24866 if (token
->type
== CPP_NAME
)
24867 attr_ns
= token
->u
.value
;
24868 else if (token
->type
== CPP_KEYWORD
)
24869 attr_ns
= ridpointers
[(int) token
->keyword
];
24870 else if (token
->flags
& NAMED_OP
)
24871 attr_ns
= get_identifier (cpp_type2name (token
->type
,
24874 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
24876 if (cxx_dialect
< cxx1z
24877 && !in_system_header_at (input_location
))
24878 pedwarn (input_location
, 0,
24879 "attribute using prefix only available "
24880 "with -std=c++1z or -std=gnu++1z");
24882 cp_lexer_consume_token (parser
->lexer
);
24883 cp_lexer_consume_token (parser
->lexer
);
24884 cp_lexer_consume_token (parser
->lexer
);
24887 attr_ns
= NULL_TREE
;
24890 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
24892 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
24893 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
24894 cp_parser_skip_to_end_of_statement (parser
);
24896 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24897 when we are sure that we have actually parsed them. */
24898 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24904 /* Look for an alignment-specifier. */
24906 token
= cp_lexer_peek_token (parser
->lexer
);
24908 if (token
->type
!= CPP_KEYWORD
24909 || token
->keyword
!= RID_ALIGNAS
)
24912 cp_lexer_consume_token (parser
->lexer
);
24913 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24915 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
24917 cp_parser_error (parser
, "expected %<(%>");
24918 return error_mark_node
;
24921 cp_parser_parse_tentatively (parser
);
24922 alignas_expr
= cp_parser_type_id (parser
);
24924 if (!cp_parser_parse_definitely (parser
))
24926 alignas_expr
= cp_parser_assignment_expression (parser
);
24927 if (alignas_expr
== error_mark_node
)
24928 cp_parser_skip_to_end_of_statement (parser
);
24929 if (alignas_expr
== NULL_TREE
24930 || alignas_expr
== error_mark_node
)
24931 return alignas_expr
;
24934 alignas_expr
= cxx_alignas_expr (alignas_expr
);
24935 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
24937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24939 cp_lexer_consume_token (parser
->lexer
);
24940 alignas_expr
= make_pack_expansion (alignas_expr
);
24943 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
24945 cp_parser_error (parser
, "expected %<)%>");
24946 return error_mark_node
;
24949 /* Build the C++-11 representation of an 'aligned'
24952 build_tree_list (build_tree_list (get_identifier ("gnu"),
24953 get_identifier ("aligned")),
24960 /* Parse a standard C++-11 attribute-specifier-seq.
24962 attribute-specifier-seq:
24963 attribute-specifier-seq [opt] attribute-specifier
24967 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
24969 tree attr_specs
= NULL_TREE
;
24970 tree attr_last
= NULL_TREE
;
24974 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
24975 if (attr_spec
== NULL_TREE
)
24977 if (attr_spec
== error_mark_node
)
24978 return error_mark_node
;
24981 TREE_CHAIN (attr_last
) = attr_spec
;
24983 attr_specs
= attr_last
= attr_spec
;
24984 attr_last
= tree_last (attr_last
);
24990 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24991 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24992 current value of the PEDANTIC flag, regardless of whether or not
24993 the `__extension__' keyword is present. The caller is responsible
24994 for restoring the value of the PEDANTIC flag. */
24997 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
24999 /* Save the old value of the PEDANTIC flag. */
25000 *saved_pedantic
= pedantic
;
25002 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25004 /* Consume the `__extension__' token. */
25005 cp_lexer_consume_token (parser
->lexer
);
25006 /* We're not being pedantic while the `__extension__' keyword is
25016 /* Parse a label declaration.
25019 __label__ label-declarator-seq ;
25021 label-declarator-seq:
25022 identifier , label-declarator-seq
25026 cp_parser_label_declaration (cp_parser
* parser
)
25028 /* Look for the `__label__' keyword. */
25029 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25035 /* Look for an identifier. */
25036 identifier
= cp_parser_identifier (parser
);
25037 /* If we failed, stop. */
25038 if (identifier
== error_mark_node
)
25040 /* Declare it as a label. */
25041 finish_label_decl (identifier
);
25042 /* If the next token is a `;', stop. */
25043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25045 /* Look for the `,' separating the label declarations. */
25046 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25049 /* Look for the final `;'. */
25050 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25053 // -------------------------------------------------------------------------- //
25056 // Parse a requires clause.
25058 // requires-clause:
25059 // 'requires' logical-or-expression
25061 // The required logical-or-expression must be a constant expression. Note
25062 // that we don't check that the expression is constepxr here. We defer until
25063 // we analyze constraints and then, we only check atomic constraints.
25065 cp_parser_requires_clause (cp_parser
*parser
)
25067 // Parse the requires clause so that it is not automatically folded.
25068 ++processing_template_decl
;
25069 tree expr
= cp_parser_binary_expression (parser
, false, false,
25070 PREC_NOT_OPERATOR
, NULL
);
25071 if (check_for_bare_parameter_packs (expr
))
25072 expr
= error_mark_node
;
25073 --processing_template_decl
;
25077 // Optionally parse a requires clause:
25079 cp_parser_requires_clause_opt (cp_parser
*parser
)
25081 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25082 if (tok
->keyword
!= RID_REQUIRES
)
25084 if (!flag_concepts
&& tok
->type
== CPP_NAME
25085 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25087 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25088 "%<requires%> only available with -fconcepts");
25089 /* Parse and discard the requires-clause. */
25090 cp_lexer_consume_token (parser
->lexer
);
25091 cp_parser_requires_clause (parser
);
25095 cp_lexer_consume_token (parser
->lexer
);
25096 return cp_parser_requires_clause (parser
);
25100 /*---------------------------------------------------------------------------
25101 Requires expressions
25102 ---------------------------------------------------------------------------*/
25104 /* Parse a requires expression
25106 requirement-expression:
25107 'requires' requirement-parameter-list [opt] requirement-body */
25109 cp_parser_requires_expression (cp_parser
*parser
)
25111 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25112 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25114 /* A requires-expression shall appear only within a concept
25115 definition or a requires-clause.
25117 TODO: Implement this diagnostic correctly. */
25118 if (!processing_template_decl
)
25120 error_at (loc
, "a requires expression cannot appear outside a template");
25121 cp_parser_skip_to_end_of_statement (parser
);
25122 return error_mark_node
;
25127 /* Local parameters are delared as variables within the scope
25128 of the expression. They are not visible past the end of
25129 the expression. Expressions within the requires-expression
25130 are unevaluated. */
25131 struct scope_sentinel
25135 ++cp_unevaluated_operand
;
25136 begin_scope (sk_block
, NULL_TREE
);
25141 pop_bindings_and_leave_scope ();
25142 --cp_unevaluated_operand
;
25146 /* Parse the optional parameter list. */
25147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25149 parms
= cp_parser_requirement_parameter_list (parser
);
25150 if (parms
== error_mark_node
)
25151 return error_mark_node
;
25156 /* Parse the requirement body. */
25157 reqs
= cp_parser_requirement_body (parser
);
25158 if (reqs
== error_mark_node
)
25159 return error_mark_node
;
25162 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25164 grokparms (parms
, &parms
);
25165 return finish_requires_expr (parms
, reqs
);
25168 /* Parse a parameterized requirement.
25170 requirement-parameter-list:
25171 '(' parameter-declaration-clause ')' */
25173 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25175 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25176 return error_mark_node
;
25178 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25180 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25181 return error_mark_node
;
25186 /* Parse the body of a requirement.
25189 '{' requirement-list '}' */
25191 cp_parser_requirement_body (cp_parser
*parser
)
25193 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25194 return error_mark_node
;
25196 tree reqs
= cp_parser_requirement_list (parser
);
25198 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25199 return error_mark_node
;
25204 /* Parse a list of requirements.
25208 requirement-list ';' requirement[opt] */
25210 cp_parser_requirement_list (cp_parser
*parser
)
25212 tree result
= NULL_TREE
;
25215 tree req
= cp_parser_requirement (parser
);
25216 if (req
== error_mark_node
)
25217 return error_mark_node
;
25219 result
= tree_cons (NULL_TREE
, req
, result
);
25221 /* If we see a semi-colon, consume it. */
25222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25223 cp_lexer_consume_token (parser
->lexer
);
25225 /* Stop processing at the end of the list. */
25226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25230 /* Reverse the order of requirements so they are analyzed in
25231 declaration order. */
25232 return nreverse (result
);
25235 /* Parse a syntactic requirement or type requirement.
25239 compound-requirement
25241 nested-requirement */
25243 cp_parser_requirement (cp_parser
*parser
)
25245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25246 return cp_parser_compound_requirement (parser
);
25247 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25248 return cp_parser_type_requirement (parser
);
25249 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25250 return cp_parser_nested_requirement (parser
);
25252 return cp_parser_simple_requirement (parser
);
25255 /* Parse a simple requirement.
25257 simple-requirement:
25260 cp_parser_simple_requirement (cp_parser
*parser
)
25262 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25263 if (!expr
|| expr
== error_mark_node
)
25264 return error_mark_node
;
25266 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25267 return error_mark_node
;
25269 return finish_simple_requirement (expr
);
25272 /* Parse a type requirement
25275 nested-name-specifier [opt] required-type-name ';'
25277 required-type-name:
25279 'template' [opt] simple-template-id */
25281 cp_parser_type_requirement (cp_parser
*parser
)
25283 cp_lexer_consume_token (parser
->lexer
);
25285 // Save the scope before parsing name specifiers.
25286 tree saved_scope
= parser
->scope
;
25287 tree saved_object_scope
= parser
->object_scope
;
25288 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25289 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25290 cp_parser_nested_name_specifier_opt (parser
,
25291 /*typename_keyword_p=*/true,
25292 /*check_dependency_p=*/false,
25294 /*is_declaration=*/false);
25297 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25299 cp_lexer_consume_token (parser
->lexer
);
25300 type
= cp_parser_template_id (parser
,
25301 /*template_keyword_p=*/true,
25302 /*check_dependency=*/false,
25303 /*tag_type=*/none_type
,
25304 /*is_declaration=*/false);
25305 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25306 /*complain=*/tf_error
);
25309 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25311 if (TREE_CODE (type
) == TYPE_DECL
)
25312 type
= TREE_TYPE (type
);
25314 parser
->scope
= saved_scope
;
25315 parser
->object_scope
= saved_object_scope
;
25316 parser
->qualifying_scope
= saved_qualifying_scope
;
25318 if (type
== error_mark_node
)
25319 cp_parser_skip_to_end_of_statement (parser
);
25321 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25322 return error_mark_node
;
25323 if (type
== error_mark_node
)
25324 return error_mark_node
;
25326 return finish_type_requirement (type
);
25329 /* Parse a compound requirement
25331 compound-requirement:
25332 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25334 cp_parser_compound_requirement (cp_parser
*parser
)
25336 /* Parse an expression enclosed in '{ }'s. */
25337 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
25338 return error_mark_node
;
25340 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25341 if (!expr
|| expr
== error_mark_node
)
25342 return error_mark_node
;
25344 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25345 return error_mark_node
;
25347 /* Parse the optional noexcept. */
25348 bool noexcept_p
= false;
25349 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25351 cp_lexer_consume_token (parser
->lexer
);
25355 /* Parse the optional trailing return type. */
25356 tree type
= NULL_TREE
;
25357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25359 cp_lexer_consume_token (parser
->lexer
);
25360 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25361 parser
->in_result_type_constraint_p
= true;
25362 type
= cp_parser_trailing_type_id (parser
);
25363 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25364 if (type
== error_mark_node
)
25365 return error_mark_node
;
25368 return finish_compound_requirement (expr
, type
, noexcept_p
);
25371 /* Parse a nested requirement. This is the same as a requires clause.
25373 nested-requirement:
25376 cp_parser_nested_requirement (cp_parser
*parser
)
25378 cp_lexer_consume_token (parser
->lexer
);
25379 tree req
= cp_parser_requires_clause (parser
);
25380 if (req
== error_mark_node
)
25381 return error_mark_node
;
25382 return finish_nested_requirement (req
);
25385 /* Support Functions */
25387 /* Return the appropriate prefer_type argument for lookup_name_real based on
25388 tag_type and template_mem_access. */
25391 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25393 /* DR 141: When looking in the current enclosing context for a template-name
25394 after -> or ., only consider class templates. */
25395 if (template_mem_access
)
25399 case none_type
: return 0; // No preference.
25400 case scope_type
: return 1; // Type or namespace.
25401 default: return 2; // Type only.
25405 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25406 NAME should have one of the representations used for an
25407 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25408 is returned. If PARSER->SCOPE is a dependent type, then a
25409 SCOPE_REF is returned.
25411 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25412 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25413 was formed. Abstractly, such entities should not be passed to this
25414 function, because they do not need to be looked up, but it is
25415 simpler to check for this special case here, rather than at the
25418 In cases not explicitly covered above, this function returns a
25419 DECL, OVERLOAD, or baselink representing the result of the lookup.
25420 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25423 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25424 (e.g., "struct") that was used. In that case bindings that do not
25425 refer to types are ignored.
25427 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25430 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25433 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25436 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25437 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25438 NULL_TREE otherwise. */
25441 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25442 enum tag_types tag_type
,
25445 bool check_dependency
,
25446 tree
*ambiguous_decls
,
25447 location_t name_location
)
25450 tree object_type
= parser
->context
->object_type
;
25452 /* Assume that the lookup will be unambiguous. */
25453 if (ambiguous_decls
)
25454 *ambiguous_decls
= NULL_TREE
;
25456 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25457 no longer valid. Note that if we are parsing tentatively, and
25458 the parse fails, OBJECT_TYPE will be automatically restored. */
25459 parser
->context
->object_type
= NULL_TREE
;
25461 if (name
== error_mark_node
)
25462 return error_mark_node
;
25464 /* A template-id has already been resolved; there is no lookup to
25466 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25468 if (BASELINK_P (name
))
25470 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25471 == TEMPLATE_ID_EXPR
);
25475 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25476 it should already have been checked to make sure that the name
25477 used matches the type being destroyed. */
25478 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25482 /* Figure out to which type this destructor applies. */
25484 type
= parser
->scope
;
25485 else if (object_type
)
25486 type
= object_type
;
25488 type
= current_class_type
;
25489 /* If that's not a class type, there is no destructor. */
25490 if (!type
|| !CLASS_TYPE_P (type
))
25491 return error_mark_node
;
25492 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25493 lazily_declare_fn (sfk_destructor
, type
);
25494 if (!CLASSTYPE_DESTRUCTORS (type
))
25495 return error_mark_node
;
25496 /* If it was a class type, return the destructor. */
25497 return CLASSTYPE_DESTRUCTORS (type
);
25500 /* By this point, the NAME should be an ordinary identifier. If
25501 the id-expression was a qualified name, the qualifying scope is
25502 stored in PARSER->SCOPE at this point. */
25503 gcc_assert (identifier_p (name
));
25505 /* Perform the lookup. */
25510 if (parser
->scope
== error_mark_node
)
25511 return error_mark_node
;
25513 /* If the SCOPE is dependent, the lookup must be deferred until
25514 the template is instantiated -- unless we are explicitly
25515 looking up names in uninstantiated templates. Even then, we
25516 cannot look up the name if the scope is not a class type; it
25517 might, for example, be a template type parameter. */
25518 dependent_p
= (TYPE_P (parser
->scope
)
25519 && dependent_scope_p (parser
->scope
));
25520 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25522 /* Defer lookup. */
25523 decl
= error_mark_node
;
25526 tree pushed_scope
= NULL_TREE
;
25528 /* If PARSER->SCOPE is a dependent type, then it must be a
25529 class type, and we must not be checking dependencies;
25530 otherwise, we would have processed this lookup above. So
25531 that PARSER->SCOPE is not considered a dependent base by
25532 lookup_member, we must enter the scope here. */
25534 pushed_scope
= push_scope (parser
->scope
);
25536 /* If the PARSER->SCOPE is a template specialization, it
25537 may be instantiated during name lookup. In that case,
25538 errors may be issued. Even if we rollback the current
25539 tentative parse, those errors are valid. */
25540 decl
= lookup_qualified_name (parser
->scope
, name
,
25541 prefer_type_arg (tag_type
),
25542 /*complain=*/true);
25544 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25545 lookup result and the nested-name-specifier nominates a class C:
25546 * if the name specified after the nested-name-specifier, when
25547 looked up in C, is the injected-class-name of C (Clause 9), or
25548 * if the name specified after the nested-name-specifier is the
25549 same as the identifier or the simple-template-id's template-
25550 name in the last component of the nested-name-specifier,
25551 the name is instead considered to name the constructor of
25552 class C. [ Note: for example, the constructor is not an
25553 acceptable lookup result in an elaborated-type-specifier so
25554 the constructor would not be used in place of the
25555 injected-class-name. --end note ] Such a constructor name
25556 shall be used only in the declarator-id of a declaration that
25557 names a constructor or in a using-declaration. */
25558 if (tag_type
== none_type
25559 && DECL_SELF_REFERENCE_P (decl
)
25560 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25561 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25562 prefer_type_arg (tag_type
),
25563 /*complain=*/true);
25565 /* If we have a single function from a using decl, pull it out. */
25566 if (TREE_CODE (decl
) == OVERLOAD
25567 && !really_overloaded_fn (decl
))
25568 decl
= OVL_FUNCTION (decl
);
25571 pop_scope (pushed_scope
);
25574 /* If the scope is a dependent type and either we deferred lookup or
25575 we did lookup but didn't find the name, rememeber the name. */
25576 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25577 && dependent_type_p (parser
->scope
))
25583 /* The resolution to Core Issue 180 says that `struct
25584 A::B' should be considered a type-name, even if `A'
25586 type
= make_typename_type (parser
->scope
, name
, tag_type
,
25587 /*complain=*/tf_error
);
25588 if (type
!= error_mark_node
)
25589 decl
= TYPE_NAME (type
);
25591 else if (is_template
25592 && (cp_parser_next_token_ends_template_argument_p (parser
)
25593 || cp_lexer_next_token_is (parser
->lexer
,
25595 decl
= make_unbound_class_template (parser
->scope
,
25597 /*complain=*/tf_error
);
25599 decl
= build_qualified_name (/*type=*/NULL_TREE
,
25600 parser
->scope
, name
,
25603 parser
->qualifying_scope
= parser
->scope
;
25604 parser
->object_scope
= NULL_TREE
;
25606 else if (object_type
)
25608 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25609 OBJECT_TYPE is not a class. */
25610 if (CLASS_TYPE_P (object_type
))
25611 /* If the OBJECT_TYPE is a template specialization, it may
25612 be instantiated during name lookup. In that case, errors
25613 may be issued. Even if we rollback the current tentative
25614 parse, those errors are valid. */
25615 decl
= lookup_member (object_type
,
25618 prefer_type_arg (tag_type
),
25619 tf_warning_or_error
);
25624 /* Look it up in the enclosing context. DR 141: When looking for a
25625 template-name after -> or ., only consider class templates. */
25626 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
25628 /*block_p=*/true, is_namespace
, 0);
25629 if (object_type
== unknown_type_node
)
25630 /* The object is type-dependent, so we can't look anything up; we used
25631 this to get the DR 141 behavior. */
25632 object_type
= NULL_TREE
;
25633 parser
->object_scope
= object_type
;
25634 parser
->qualifying_scope
= NULL_TREE
;
25638 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
25640 /*block_p=*/true, is_namespace
, 0);
25641 parser
->qualifying_scope
= NULL_TREE
;
25642 parser
->object_scope
= NULL_TREE
;
25645 /* If the lookup failed, let our caller know. */
25646 if (!decl
|| decl
== error_mark_node
)
25647 return error_mark_node
;
25649 /* Pull out the template from an injected-class-name (or multiple). */
25651 decl
= maybe_get_template_decl_from_type_decl (decl
);
25653 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25654 if (TREE_CODE (decl
) == TREE_LIST
)
25656 if (ambiguous_decls
)
25657 *ambiguous_decls
= decl
;
25658 /* The error message we have to print is too complicated for
25659 cp_parser_error, so we incorporate its actions directly. */
25660 if (!cp_parser_simulate_error (parser
))
25662 error_at (name_location
, "reference to %qD is ambiguous",
25664 print_candidates (decl
);
25666 return error_mark_node
;
25669 gcc_assert (DECL_P (decl
)
25670 || TREE_CODE (decl
) == OVERLOAD
25671 || TREE_CODE (decl
) == SCOPE_REF
25672 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
25673 || BASELINK_P (decl
));
25675 /* If we have resolved the name of a member declaration, check to
25676 see if the declaration is accessible. When the name resolves to
25677 set of overloaded functions, accessibility is checked when
25678 overload resolution is done.
25680 During an explicit instantiation, access is not checked at all,
25681 as per [temp.explicit]. */
25683 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
25685 maybe_record_typedef_use (decl
);
25687 return cp_expr (decl
, name_location
);
25690 /* Like cp_parser_lookup_name, but for use in the typical case where
25691 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25692 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25695 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
25697 return cp_parser_lookup_name (parser
, name
,
25699 /*is_template=*/false,
25700 /*is_namespace=*/false,
25701 /*check_dependency=*/true,
25702 /*ambiguous_decls=*/NULL
,
25706 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25707 the current context, return the TYPE_DECL. If TAG_NAME_P is
25708 true, the DECL indicates the class being defined in a class-head,
25709 or declared in an elaborated-type-specifier.
25711 Otherwise, return DECL. */
25714 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
25716 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25717 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25720 template <typename T> struct B;
25723 template <typename T> struct A::B {};
25725 Similarly, in an elaborated-type-specifier:
25727 namespace N { struct X{}; }
25730 template <typename T> friend struct N::X;
25733 However, if the DECL refers to a class type, and we are in
25734 the scope of the class, then the name lookup automatically
25735 finds the TYPE_DECL created by build_self_reference rather
25736 than a TEMPLATE_DECL. For example, in:
25738 template <class T> struct S {
25742 there is no need to handle such case. */
25744 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
25745 return DECL_TEMPLATE_RESULT (decl
);
25750 /* If too many, or too few, template-parameter lists apply to the
25751 declarator, issue an error message. Returns TRUE if all went well,
25752 and FALSE otherwise. */
25755 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
25756 cp_declarator
*declarator
,
25757 location_t declarator_location
)
25759 switch (declarator
->kind
)
25763 unsigned num_templates
= 0;
25764 tree scope
= declarator
->u
.id
.qualifying_scope
;
25767 num_templates
= num_template_headers_for_class (scope
);
25768 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
25769 == TEMPLATE_ID_EXPR
)
25770 /* If the DECLARATOR has the form `X<y>' then it uses one
25771 additional level of template parameters. */
25774 return cp_parser_check_template_parameters
25775 (parser
, num_templates
, declarator_location
, declarator
);
25781 case cdk_reference
:
25783 return (cp_parser_check_declarator_template_parameters
25784 (parser
, declarator
->declarator
, declarator_location
));
25791 gcc_unreachable ();
25796 /* NUM_TEMPLATES were used in the current declaration. If that is
25797 invalid, return FALSE and issue an error messages. Otherwise,
25798 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25799 declarator and we can print more accurate diagnostics. */
25802 cp_parser_check_template_parameters (cp_parser
* parser
,
25803 unsigned num_templates
,
25804 location_t location
,
25805 cp_declarator
*declarator
)
25807 /* If there are the same number of template classes and parameter
25808 lists, that's OK. */
25809 if (parser
->num_template_parameter_lists
== num_templates
)
25811 /* If there are more, but only one more, then we are referring to a
25812 member template. That's OK too. */
25813 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
25815 /* If there are more template classes than parameter lists, we have
25818 template <class T> void S<T>::R<T>::f (); */
25819 if (parser
->num_template_parameter_lists
< num_templates
)
25821 if (declarator
&& !current_function_decl
)
25822 error_at (location
, "specializing member %<%T::%E%> "
25823 "requires %<template<>%> syntax",
25824 declarator
->u
.id
.qualifying_scope
,
25825 declarator
->u
.id
.unqualified_name
);
25826 else if (declarator
)
25827 error_at (location
, "invalid declaration of %<%T::%E%>",
25828 declarator
->u
.id
.qualifying_scope
,
25829 declarator
->u
.id
.unqualified_name
);
25831 error_at (location
, "too few template-parameter-lists");
25834 /* Otherwise, there are too many template parameter lists. We have
25837 template <class T> template <class U> void S::f(); */
25838 error_at (location
, "too many template-parameter-lists");
25842 /* Parse an optional `::' token indicating that the following name is
25843 from the global namespace. If so, PARSER->SCOPE is set to the
25844 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25845 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25846 Returns the new value of PARSER->SCOPE, if the `::' token is
25847 present, and NULL_TREE otherwise. */
25850 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
25854 /* Peek at the next token. */
25855 token
= cp_lexer_peek_token (parser
->lexer
);
25856 /* If we're looking at a `::' token then we're starting from the
25857 global namespace, not our current location. */
25858 if (token
->type
== CPP_SCOPE
)
25860 /* Consume the `::' token. */
25861 cp_lexer_consume_token (parser
->lexer
);
25862 /* Set the SCOPE so that we know where to start the lookup. */
25863 parser
->scope
= global_namespace
;
25864 parser
->qualifying_scope
= global_namespace
;
25865 parser
->object_scope
= NULL_TREE
;
25867 return parser
->scope
;
25869 else if (!current_scope_valid_p
)
25871 parser
->scope
= NULL_TREE
;
25872 parser
->qualifying_scope
= NULL_TREE
;
25873 parser
->object_scope
= NULL_TREE
;
25879 /* Returns TRUE if the upcoming token sequence is the start of a
25880 constructor declarator. If FRIEND_P is true, the declarator is
25881 preceded by the `friend' specifier. */
25884 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
25886 bool constructor_p
;
25887 bool outside_class_specifier_p
;
25888 tree nested_name_specifier
;
25889 cp_token
*next_token
;
25891 /* The common case is that this is not a constructor declarator, so
25892 try to avoid doing lots of work if at all possible. It's not
25893 valid declare a constructor at function scope. */
25894 if (parser
->in_function_body
)
25896 /* And only certain tokens can begin a constructor declarator. */
25897 next_token
= cp_lexer_peek_token (parser
->lexer
);
25898 if (next_token
->type
!= CPP_NAME
25899 && next_token
->type
!= CPP_SCOPE
25900 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
25901 && next_token
->type
!= CPP_TEMPLATE_ID
)
25904 /* Parse tentatively; we are going to roll back all of the tokens
25906 cp_parser_parse_tentatively (parser
);
25907 /* Assume that we are looking at a constructor declarator. */
25908 constructor_p
= true;
25910 /* Look for the optional `::' operator. */
25911 cp_parser_global_scope_opt (parser
,
25912 /*current_scope_valid_p=*/false);
25913 /* Look for the nested-name-specifier. */
25914 nested_name_specifier
25915 = (cp_parser_nested_name_specifier_opt (parser
,
25916 /*typename_keyword_p=*/false,
25917 /*check_dependency_p=*/false,
25919 /*is_declaration=*/false));
25921 outside_class_specifier_p
= (!at_class_scope_p ()
25922 || !TYPE_BEING_DEFINED (current_class_type
)
25925 /* Outside of a class-specifier, there must be a
25926 nested-name-specifier. */
25927 if (!nested_name_specifier
&& outside_class_specifier_p
)
25928 constructor_p
= false;
25929 else if (nested_name_specifier
== error_mark_node
)
25930 constructor_p
= false;
25932 /* If we have a class scope, this is easy; DR 147 says that S::S always
25933 names the constructor, and no other qualified name could. */
25934 if (constructor_p
&& nested_name_specifier
25935 && CLASS_TYPE_P (nested_name_specifier
))
25937 tree id
= cp_parser_unqualified_id (parser
,
25938 /*template_keyword_p=*/false,
25939 /*check_dependency_p=*/false,
25940 /*declarator_p=*/true,
25941 /*optional_p=*/false);
25942 if (is_overloaded_fn (id
))
25943 id
= DECL_NAME (get_first_fn (id
));
25944 if (!constructor_name_p (id
, nested_name_specifier
))
25945 constructor_p
= false;
25947 /* If we still think that this might be a constructor-declarator,
25948 look for a class-name. */
25949 else if (constructor_p
)
25953 template <typename T> struct S {
25957 we must recognize that the nested `S' names a class. */
25959 type_decl
= cp_parser_class_name (parser
,
25960 /*typename_keyword_p=*/false,
25961 /*template_keyword_p=*/false,
25963 /*check_dependency_p=*/false,
25964 /*class_head_p=*/false,
25965 /*is_declaration=*/false);
25966 /* If there was no class-name, then this is not a constructor.
25967 Otherwise, if we are in a class-specifier and we aren't
25968 handling a friend declaration, check that its type matches
25969 current_class_type (c++/38313). Note: error_mark_node
25970 is left alone for error recovery purposes. */
25971 constructor_p
= (!cp_parser_error_occurred (parser
)
25972 && (outside_class_specifier_p
25973 || type_decl
== error_mark_node
25974 || same_type_p (current_class_type
,
25975 TREE_TYPE (type_decl
))));
25977 /* If we're still considering a constructor, we have to see a `(',
25978 to begin the parameter-declaration-clause, followed by either a
25979 `)', an `...', or a decl-specifier. We need to check for a
25980 type-specifier to avoid being fooled into thinking that:
25984 is a constructor. (It is actually a function named `f' that
25985 takes one parameter (of type `int') and returns a value of type
25988 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25989 constructor_p
= false;
25992 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
25993 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
25994 /* A parameter declaration begins with a decl-specifier,
25995 which is either the "attribute" keyword, a storage class
25996 specifier, or (usually) a type-specifier. */
25997 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26000 tree pushed_scope
= NULL_TREE
;
26001 unsigned saved_num_template_parameter_lists
;
26003 /* Names appearing in the type-specifier should be looked up
26004 in the scope of the class. */
26005 if (current_class_type
)
26009 type
= TREE_TYPE (type_decl
);
26010 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26012 type
= resolve_typename_type (type
,
26013 /*only_current_p=*/false);
26014 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26016 cp_parser_abort_tentative_parse (parser
);
26020 pushed_scope
= push_scope (type
);
26023 /* Inside the constructor parameter list, surrounding
26024 template-parameter-lists do not apply. */
26025 saved_num_template_parameter_lists
26026 = parser
->num_template_parameter_lists
;
26027 parser
->num_template_parameter_lists
= 0;
26029 /* Look for the type-specifier. */
26030 cp_parser_type_specifier (parser
,
26031 CP_PARSER_FLAGS_NONE
,
26032 /*decl_specs=*/NULL
,
26033 /*is_declarator=*/true,
26034 /*declares_class_or_enum=*/NULL
,
26035 /*is_cv_qualifier=*/NULL
);
26037 parser
->num_template_parameter_lists
26038 = saved_num_template_parameter_lists
;
26040 /* Leave the scope of the class. */
26042 pop_scope (pushed_scope
);
26044 constructor_p
= !cp_parser_error_occurred (parser
);
26048 /* We did not really want to consume any tokens. */
26049 cp_parser_abort_tentative_parse (parser
);
26051 return constructor_p
;
26054 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26055 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26056 they must be performed once we are in the scope of the function.
26058 Returns the function defined. */
26061 cp_parser_function_definition_from_specifiers_and_declarator
26062 (cp_parser
* parser
,
26063 cp_decl_specifier_seq
*decl_specifiers
,
26065 const cp_declarator
*declarator
)
26070 /* Begin the function-definition. */
26071 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26073 /* The things we're about to see are not directly qualified by any
26074 template headers we've seen thus far. */
26075 reset_specialization ();
26077 /* If there were names looked up in the decl-specifier-seq that we
26078 did not check, check them now. We must wait until we are in the
26079 scope of the function to perform the checks, since the function
26080 might be a friend. */
26081 perform_deferred_access_checks (tf_warning_or_error
);
26085 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26086 parser
->omp_declare_simd
= NULL
;
26087 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26088 parser
->oacc_routine
= NULL
;
26093 /* Skip the entire function. */
26094 cp_parser_skip_to_end_of_block_or_statement (parser
);
26095 fn
= error_mark_node
;
26097 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26099 /* Seen already, skip it. An error message has already been output. */
26100 cp_parser_skip_to_end_of_block_or_statement (parser
);
26101 fn
= current_function_decl
;
26102 current_function_decl
= NULL_TREE
;
26103 /* If this is a function from a class, pop the nested class. */
26104 if (current_class_name
)
26105 pop_nested_class ();
26110 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26111 tv
= TV_PARSE_INLINE
;
26113 tv
= TV_PARSE_FUNC
;
26115 fn
= cp_parser_function_definition_after_declarator (parser
,
26116 /*inline_p=*/false);
26123 /* Parse the part of a function-definition that follows the
26124 declarator. INLINE_P is TRUE iff this function is an inline
26125 function defined within a class-specifier.
26127 Returns the function defined. */
26130 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26134 bool ctor_initializer_p
= false;
26135 bool saved_in_unbraced_linkage_specification_p
;
26136 bool saved_in_function_body
;
26137 unsigned saved_num_template_parameter_lists
;
26139 bool fully_implicit_function_template_p
26140 = parser
->fully_implicit_function_template_p
;
26141 parser
->fully_implicit_function_template_p
= false;
26142 tree implicit_template_parms
26143 = parser
->implicit_template_parms
;
26144 parser
->implicit_template_parms
= 0;
26145 cp_binding_level
* implicit_template_scope
26146 = parser
->implicit_template_scope
;
26147 parser
->implicit_template_scope
= 0;
26149 saved_in_function_body
= parser
->in_function_body
;
26150 parser
->in_function_body
= true;
26151 /* If the next token is `return', then the code may be trying to
26152 make use of the "named return value" extension that G++ used to
26154 token
= cp_lexer_peek_token (parser
->lexer
);
26155 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26157 /* Consume the `return' keyword. */
26158 cp_lexer_consume_token (parser
->lexer
);
26159 /* Look for the identifier that indicates what value is to be
26161 cp_parser_identifier (parser
);
26162 /* Issue an error message. */
26163 error_at (token
->location
,
26164 "named return values are no longer supported");
26165 /* Skip tokens until we reach the start of the function body. */
26168 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26169 if (token
->type
== CPP_OPEN_BRACE
26170 || token
->type
== CPP_EOF
26171 || token
->type
== CPP_PRAGMA_EOL
)
26173 cp_lexer_consume_token (parser
->lexer
);
26176 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26177 anything declared inside `f'. */
26178 saved_in_unbraced_linkage_specification_p
26179 = parser
->in_unbraced_linkage_specification_p
;
26180 parser
->in_unbraced_linkage_specification_p
= false;
26181 /* Inside the function, surrounding template-parameter-lists do not
26183 saved_num_template_parameter_lists
26184 = parser
->num_template_parameter_lists
;
26185 parser
->num_template_parameter_lists
= 0;
26187 start_lambda_scope (current_function_decl
);
26189 /* If the next token is `try', `__transaction_atomic', or
26190 `__transaction_relaxed`, then we are looking at either function-try-block
26191 or function-transaction-block. Note that all of these include the
26193 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26194 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26195 RID_TRANSACTION_ATOMIC
);
26196 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26197 RID_TRANSACTION_RELAXED
))
26198 ctor_initializer_p
= cp_parser_function_transaction (parser
,
26199 RID_TRANSACTION_RELAXED
);
26200 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26201 ctor_initializer_p
= cp_parser_function_try_block (parser
);
26203 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
26204 (parser
, /*in_function_try_block=*/false);
26206 finish_lambda_scope ();
26208 /* Finish the function. */
26209 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
26210 (inline_p
? 2 : 0));
26211 /* Generate code for it, if necessary. */
26212 expand_or_defer_fn (fn
);
26213 /* Restore the saved values. */
26214 parser
->in_unbraced_linkage_specification_p
26215 = saved_in_unbraced_linkage_specification_p
;
26216 parser
->num_template_parameter_lists
26217 = saved_num_template_parameter_lists
;
26218 parser
->in_function_body
= saved_in_function_body
;
26220 parser
->fully_implicit_function_template_p
26221 = fully_implicit_function_template_p
;
26222 parser
->implicit_template_parms
26223 = implicit_template_parms
;
26224 parser
->implicit_template_scope
26225 = implicit_template_scope
;
26227 if (parser
->fully_implicit_function_template_p
)
26228 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26233 /* Parse a template-declaration body (following argument list). */
26236 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26237 tree parameter_list
,
26240 tree decl
= NULL_TREE
;
26241 bool friend_p
= false;
26243 /* We just processed one more parameter list. */
26244 ++parser
->num_template_parameter_lists
;
26246 /* Get the deferred access checks from the parameter list. These
26247 will be checked once we know what is being declared, as for a
26248 member template the checks must be performed in the scope of the
26249 class containing the member. */
26250 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26252 /* Tentatively parse for a new template parameter list, which can either be
26253 the template keyword or a template introduction. */
26254 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26256 else if (cxx_dialect
>= cxx11
26257 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26258 decl
= cp_parser_alias_declaration (parser
);
26261 /* There are no access checks when parsing a template, as we do not
26262 know if a specialization will be a friend. */
26263 push_deferring_access_checks (dk_no_check
);
26264 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26265 decl
= cp_parser_single_declaration (parser
,
26268 /*explicit_specialization_p=*/false,
26270 pop_deferring_access_checks ();
26272 /* If this is a member template declaration, let the front
26274 if (member_p
&& !friend_p
&& decl
)
26276 if (TREE_CODE (decl
) == TYPE_DECL
)
26277 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26279 decl
= finish_member_template_decl (decl
);
26281 else if (friend_p
&& decl
26282 && DECL_DECLARES_TYPE_P (decl
))
26283 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26284 /*complain=*/true);
26286 /* We are done with the current parameter list. */
26287 --parser
->num_template_parameter_lists
;
26289 pop_deferring_access_checks ();
26292 finish_template_decl (parameter_list
);
26294 /* Check the template arguments for a literal operator template. */
26296 && DECL_DECLARES_FUNCTION_P (decl
)
26297 && UDLIT_OPER_P (DECL_NAME (decl
)))
26300 if (parameter_list
== NULL_TREE
)
26304 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26305 if (num_parms
== 1)
26307 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26308 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26309 if (TREE_TYPE (parm
) != char_type_node
26310 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26313 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26315 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26316 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26317 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26318 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26319 if (parm
== error_mark_node
26320 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26321 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26329 if (cxx_dialect
>= cxx14
)
26330 error ("literal operator template %qD has invalid parameter list."
26331 " Expected non-type template argument pack <char...>"
26332 " or <typename CharT, CharT...>",
26335 error ("literal operator template %qD has invalid parameter list."
26336 " Expected non-type template argument pack <char...>",
26341 /* Register member declarations. */
26342 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26343 finish_member_declaration (decl
);
26344 /* If DECL is a function template, we must return to parse it later.
26345 (Even though there is no definition, there might be default
26346 arguments that need handling.) */
26347 if (member_p
&& decl
26348 && DECL_DECLARES_FUNCTION_P (decl
))
26349 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26352 /* Parse a template introduction header for a template-declaration. Returns
26353 false if tentative parse fails. */
26356 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26358 cp_parser_parse_tentatively (parser
);
26360 tree saved_scope
= parser
->scope
;
26361 tree saved_object_scope
= parser
->object_scope
;
26362 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26364 /* Look for the optional `::' operator. */
26365 cp_parser_global_scope_opt (parser
,
26366 /*current_scope_valid_p=*/false);
26367 /* Look for the nested-name-specifier. */
26368 cp_parser_nested_name_specifier_opt (parser
,
26369 /*typename_keyword_p=*/false,
26370 /*check_dependency_p=*/true,
26372 /*is_declaration=*/false);
26374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26375 tree concept_name
= cp_parser_identifier (parser
);
26377 /* Look up the concept for which we will be matching
26378 template parameters. */
26379 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26381 parser
->scope
= saved_scope
;
26382 parser
->object_scope
= saved_object_scope
;
26383 parser
->qualifying_scope
= saved_qualifying_scope
;
26385 if (concept_name
== error_mark_node
)
26386 cp_parser_simulate_error (parser
);
26388 /* Look for opening brace for introduction. */
26389 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
26391 if (!cp_parser_parse_definitely (parser
))
26394 push_deferring_access_checks (dk_deferred
);
26396 /* Build vector of placeholder parameters and grab
26397 matching identifiers. */
26398 tree introduction_list
= cp_parser_introduction_list (parser
);
26400 /* The introduction-list shall not be empty. */
26401 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26404 error ("empty introduction-list");
26408 /* Look for closing brace for introduction. */
26409 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
26412 if (tmpl_decl
== error_mark_node
)
26414 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26419 /* Build and associate the constraint. */
26420 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26421 if (parms
&& parms
!= error_mark_node
)
26423 cp_parser_template_declaration_after_parameters (parser
, parms
,
26428 error_at (token
->location
, "no matching concept for template-introduction");
26432 /* Parse a normal template-declaration following the template keyword. */
26435 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26437 tree parameter_list
;
26438 bool need_lang_pop
;
26439 location_t location
= input_location
;
26441 /* Look for the `<' token. */
26442 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26444 if (at_class_scope_p () && current_function_decl
)
26446 /* 14.5.2.2 [temp.mem]
26448 A local class shall not have member templates. */
26449 error_at (location
,
26450 "invalid declaration of member template in local class");
26451 cp_parser_skip_to_end_of_block_or_statement (parser
);
26456 A template ... shall not have C linkage. */
26457 if (current_lang_name
== lang_name_c
)
26459 error_at (location
, "template with C linkage");
26460 /* Give it C++ linkage to avoid confusing other parts of the
26462 push_lang_context (lang_name_cplusplus
);
26463 need_lang_pop
= true;
26466 need_lang_pop
= false;
26468 /* We cannot perform access checks on the template parameter
26469 declarations until we know what is being declared, just as we
26470 cannot check the decl-specifier list. */
26471 push_deferring_access_checks (dk_deferred
);
26473 /* If the next token is `>', then we have an invalid
26474 specialization. Rather than complain about an invalid template
26475 parameter, issue an error message here. */
26476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26478 cp_parser_error (parser
, "invalid explicit specialization");
26479 begin_specialization ();
26480 parameter_list
= NULL_TREE
;
26484 /* Parse the template parameters. */
26485 parameter_list
= cp_parser_template_parameter_list (parser
);
26488 /* Look for the `>'. */
26489 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26491 /* Manage template requirements */
26494 tree reqs
= get_shorthand_constraints (current_template_parms
);
26495 if (tree r
= cp_parser_requires_clause_opt (parser
))
26496 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26497 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26500 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26503 /* For the erroneous case of a template with C linkage, we pushed an
26504 implicit C++ linkage scope; exit that scope now. */
26506 pop_lang_context ();
26509 /* Parse a template-declaration, assuming that the `export' (and
26510 `extern') keywords, if present, has already been scanned. MEMBER_P
26511 is as for cp_parser_template_declaration. */
26514 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26516 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26518 cp_lexer_consume_token (parser
->lexer
);
26519 cp_parser_explicit_template_declaration (parser
, member_p
);
26522 else if (flag_concepts
)
26523 return cp_parser_template_introduction (parser
, member_p
);
26528 /* Perform the deferred access checks from a template-parameter-list.
26529 CHECKS is a TREE_LIST of access checks, as returned by
26530 get_deferred_access_checks. */
26533 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26535 ++processing_template_parmlist
;
26536 perform_access_checks (checks
, tf_warning_or_error
);
26537 --processing_template_parmlist
;
26540 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26541 `function-definition' sequence that follows a template header.
26542 If MEMBER_P is true, this declaration appears in a class scope.
26544 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26545 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26548 cp_parser_single_declaration (cp_parser
* parser
,
26549 vec
<deferred_access_check
, va_gc
> *checks
,
26551 bool explicit_specialization_p
,
26554 int declares_class_or_enum
;
26555 tree decl
= NULL_TREE
;
26556 cp_decl_specifier_seq decl_specifiers
;
26557 bool function_definition_p
= false;
26558 cp_token
*decl_spec_token_start
;
26560 /* This function is only used when processing a template
26562 gcc_assert (innermost_scope_kind () == sk_template_parms
26563 || innermost_scope_kind () == sk_template_spec
);
26565 /* Defer access checks until we know what is being declared. */
26566 push_deferring_access_checks (dk_deferred
);
26568 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26570 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
26571 cp_parser_decl_specifier_seq (parser
,
26572 CP_PARSER_FLAGS_OPTIONAL
,
26574 &declares_class_or_enum
);
26576 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
26578 /* There are no template typedefs. */
26579 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
26581 error_at (decl_spec_token_start
->location
,
26582 "template declaration of %<typedef%>");
26583 decl
= error_mark_node
;
26586 /* Gather up the access checks that occurred the
26587 decl-specifier-seq. */
26588 stop_deferring_access_checks ();
26590 /* Check for the declaration of a template class. */
26591 if (declares_class_or_enum
)
26593 if (cp_parser_declares_only_class_p (parser
)
26594 || (declares_class_or_enum
& 2))
26596 // If this is a declaration, but not a definition, associate
26597 // any constraints with the type declaration. Constraints
26598 // are associated with definitions in cp_parser_class_specifier.
26599 if (declares_class_or_enum
== 1)
26600 associate_classtype_constraints (decl_specifiers
.type
);
26602 decl
= shadow_tag (&decl_specifiers
);
26607 friend template <typename T> struct A<T>::B;
26610 A<T>::B will be represented by a TYPENAME_TYPE, and
26611 therefore not recognized by shadow_tag. */
26612 if (friend_p
&& *friend_p
26614 && decl_specifiers
.type
26615 && TYPE_P (decl_specifiers
.type
))
26616 decl
= decl_specifiers
.type
;
26618 if (decl
&& decl
!= error_mark_node
)
26619 decl
= TYPE_NAME (decl
);
26621 decl
= error_mark_node
;
26623 /* Perform access checks for template parameters. */
26624 cp_parser_perform_template_parameter_access_checks (checks
);
26626 /* Give a helpful diagnostic for
26627 template <class T> struct A { } a;
26628 if we aren't already recovering from an error. */
26629 if (!cp_parser_declares_only_class_p (parser
)
26632 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26633 "a class template declaration must not declare "
26635 cp_parser_skip_to_end_of_block_or_statement (parser
);
26641 /* Complain about missing 'typename' or other invalid type names. */
26642 if (!decl_specifiers
.any_type_specifiers_p
26643 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
26645 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26646 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26647 the rest of this declaration. */
26648 decl
= error_mark_node
;
26652 /* If it's not a template class, try for a template function. If
26653 the next token is a `;', then this declaration does not declare
26654 anything. But, if there were errors in the decl-specifiers, then
26655 the error might well have come from an attempted class-specifier.
26656 In that case, there's no need to warn about a missing declarator. */
26658 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
26659 || decl_specifiers
.type
!= error_mark_node
))
26661 decl
= cp_parser_init_declarator (parser
,
26664 /*function_definition_allowed_p=*/true,
26666 declares_class_or_enum
,
26667 &function_definition_p
,
26670 /* 7.1.1-1 [dcl.stc]
26672 A storage-class-specifier shall not be specified in an explicit
26673 specialization... */
26675 && explicit_specialization_p
26676 && decl_specifiers
.storage_class
!= sc_none
)
26678 error_at (decl_spec_token_start
->location
,
26679 "explicit template specialization cannot have a storage class");
26680 decl
= error_mark_node
;
26683 if (decl
&& VAR_P (decl
))
26684 check_template_variable (decl
);
26687 /* Look for a trailing `;' after the declaration. */
26688 if (!function_definition_p
26689 && (decl
== error_mark_node
26690 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
26691 cp_parser_skip_to_end_of_block_or_statement (parser
);
26694 pop_deferring_access_checks ();
26696 /* Clear any current qualification; whatever comes next is the start
26697 of something new. */
26698 parser
->scope
= NULL_TREE
;
26699 parser
->qualifying_scope
= NULL_TREE
;
26700 parser
->object_scope
= NULL_TREE
;
26705 /* Parse a cast-expression that is not the operand of a unary "&". */
26708 cp_parser_simple_cast_expression (cp_parser
*parser
)
26710 return cp_parser_cast_expression (parser
, /*address_p=*/false,
26711 /*cast_p=*/false, /*decltype*/false, NULL
);
26714 /* Parse a functional cast to TYPE. Returns an expression
26715 representing the cast. */
26718 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
26720 vec
<tree
, va_gc
> *vec
;
26721 tree expression_list
;
26725 location_t start_loc
= input_location
;
26728 type
= error_mark_node
;
26730 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26732 cp_lexer_set_source_position (parser
->lexer
);
26733 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26734 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
26735 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
26736 if (TREE_CODE (type
) == TYPE_DECL
)
26737 type
= TREE_TYPE (type
);
26739 cast
= finish_compound_literal (type
, expression_list
,
26740 tf_warning_or_error
);
26741 /* Create a location of the form:
26744 with caret == start at the start of the type name,
26745 finishing at the closing brace. */
26746 location_t finish_loc
26747 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26748 location_t combined_loc
= make_location (start_loc
, start_loc
,
26750 cast
.set_location (combined_loc
);
26755 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
26757 /*allow_expansion_p=*/true,
26758 /*non_constant_p=*/NULL
);
26760 expression_list
= error_mark_node
;
26763 expression_list
= build_tree_list_vec (vec
);
26764 release_tree_vector (vec
);
26767 cast
= build_functional_cast (type
, expression_list
,
26768 tf_warning_or_error
);
26769 /* [expr.const]/1: In an integral constant expression "only type
26770 conversions to integral or enumeration type can be used". */
26771 if (TREE_CODE (type
) == TYPE_DECL
)
26772 type
= TREE_TYPE (type
);
26773 if (cast
!= error_mark_node
26774 && !cast_valid_in_integral_constant_expression_p (type
)
26775 && cp_parser_non_integral_constant_expression (parser
,
26777 return error_mark_node
;
26779 /* Create a location of the form:
26782 with caret == start at the start of the type name,
26783 finishing at the closing paren. */
26784 location_t finish_loc
26785 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
26786 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
26787 cast
.set_location (combined_loc
);
26791 /* Save the tokens that make up the body of a member function defined
26792 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26793 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26794 specifiers applied to the declaration. Returns the FUNCTION_DECL
26795 for the member function. */
26798 cp_parser_save_member_function_body (cp_parser
* parser
,
26799 cp_decl_specifier_seq
*decl_specifiers
,
26800 cp_declarator
*declarator
,
26806 bool function_try_block
= false;
26808 /* Create the FUNCTION_DECL. */
26809 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
26810 cp_finalize_omp_declare_simd (parser
, fn
);
26811 cp_finalize_oacc_routine (parser
, fn
, true);
26812 /* If something went badly wrong, bail out now. */
26813 if (fn
== error_mark_node
)
26815 /* If there's a function-body, skip it. */
26816 if (cp_parser_token_starts_function_definition_p
26817 (cp_lexer_peek_token (parser
->lexer
)))
26818 cp_parser_skip_to_end_of_block_or_statement (parser
);
26819 return error_mark_node
;
26822 /* Remember it, if there default args to post process. */
26823 cp_parser_save_default_args (parser
, fn
);
26825 /* Save away the tokens that make up the body of the
26827 first
= parser
->lexer
->next_token
;
26829 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
26830 cp_lexer_consume_token (parser
->lexer
);
26831 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26832 RID_TRANSACTION_ATOMIC
))
26834 cp_lexer_consume_token (parser
->lexer
);
26835 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
26837 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
26838 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
26839 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
26840 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
26841 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
26843 cp_lexer_consume_token (parser
->lexer
);
26844 cp_lexer_consume_token (parser
->lexer
);
26845 cp_lexer_consume_token (parser
->lexer
);
26846 cp_lexer_consume_token (parser
->lexer
);
26847 cp_lexer_consume_token (parser
->lexer
);
26850 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26851 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
26853 cp_lexer_consume_token (parser
->lexer
);
26854 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26859 /* Handle function try blocks. */
26860 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26862 cp_lexer_consume_token (parser
->lexer
);
26863 function_try_block
= true;
26865 /* We can have braced-init-list mem-initializers before the fn body. */
26866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26868 cp_lexer_consume_token (parser
->lexer
);
26869 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
26871 /* cache_group will stop after an un-nested { } pair, too. */
26872 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26875 /* variadic mem-inits have ... after the ')'. */
26876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26877 cp_lexer_consume_token (parser
->lexer
);
26880 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26881 /* Handle function try blocks. */
26882 if (function_try_block
)
26883 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
26884 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26885 last
= parser
->lexer
->next_token
;
26887 /* Save away the inline definition; we will process it when the
26888 class is complete. */
26889 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
26890 DECL_PENDING_INLINE_P (fn
) = 1;
26892 /* We need to know that this was defined in the class, so that
26893 friend templates are handled correctly. */
26894 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
26896 /* Add FN to the queue of functions to be parsed later. */
26897 vec_safe_push (unparsed_funs_with_definitions
, fn
);
26902 /* Save the tokens that make up the in-class initializer for a non-static
26903 data member. Returns a DEFAULT_ARG. */
26906 cp_parser_save_nsdmi (cp_parser
* parser
)
26908 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
26911 /* Parse a template-argument-list, as well as the trailing ">" (but
26912 not the opening "<"). See cp_parser_template_argument_list for the
26916 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
26920 tree saved_qualifying_scope
;
26921 tree saved_object_scope
;
26922 bool saved_greater_than_is_operator_p
;
26923 int saved_unevaluated_operand
;
26924 int saved_inhibit_evaluation_warnings
;
26928 When parsing a template-id, the first non-nested `>' is taken as
26929 the end of the template-argument-list rather than a greater-than
26931 saved_greater_than_is_operator_p
26932 = parser
->greater_than_is_operator_p
;
26933 parser
->greater_than_is_operator_p
= false;
26934 /* Parsing the argument list may modify SCOPE, so we save it
26936 saved_scope
= parser
->scope
;
26937 saved_qualifying_scope
= parser
->qualifying_scope
;
26938 saved_object_scope
= parser
->object_scope
;
26939 /* We need to evaluate the template arguments, even though this
26940 template-id may be nested within a "sizeof". */
26941 saved_unevaluated_operand
= cp_unevaluated_operand
;
26942 cp_unevaluated_operand
= 0;
26943 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26944 c_inhibit_evaluation_warnings
= 0;
26945 /* Parse the template-argument-list itself. */
26946 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
26947 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
26948 arguments
= NULL_TREE
;
26950 arguments
= cp_parser_template_argument_list (parser
);
26951 /* Look for the `>' that ends the template-argument-list. If we find
26952 a '>>' instead, it's probably just a typo. */
26953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
26955 if (cxx_dialect
!= cxx98
)
26957 /* In C++0x, a `>>' in a template argument list or cast
26958 expression is considered to be two separate `>'
26959 tokens. So, change the current token to a `>', but don't
26960 consume it: it will be consumed later when the outer
26961 template argument list (or cast expression) is parsed.
26962 Note that this replacement of `>' for `>>' is necessary
26963 even if we are parsing tentatively: in the tentative
26964 case, after calling
26965 cp_parser_enclosed_template_argument_list we will always
26966 throw away all of the template arguments and the first
26967 closing `>', either because the template argument list
26968 was erroneous or because we are replacing those tokens
26969 with a CPP_TEMPLATE_ID token. The second `>' (which will
26970 not have been thrown away) is needed either to close an
26971 outer template argument list or to complete a new-style
26973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26974 token
->type
= CPP_GREATER
;
26976 else if (!saved_greater_than_is_operator_p
)
26978 /* If we're in a nested template argument list, the '>>' has
26979 to be a typo for '> >'. We emit the error message, but we
26980 continue parsing and we push a '>' as next token, so that
26981 the argument list will be parsed correctly. Note that the
26982 global source location is still on the token before the
26983 '>>', so we need to say explicitly where we want it. */
26984 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26985 gcc_rich_location
richloc (token
->location
);
26986 richloc
.add_fixit_replace ("> >");
26987 error_at_rich_loc (&richloc
, "%<>>%> should be %<> >%> "
26988 "within a nested template argument list");
26990 token
->type
= CPP_GREATER
;
26994 /* If this is not a nested template argument list, the '>>'
26995 is a typo for '>'. Emit an error message and continue.
26996 Same deal about the token location, but here we can get it
26997 right by consuming the '>>' before issuing the diagnostic. */
26998 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26999 error_at (token
->location
,
27000 "spurious %<>>%>, use %<>%> to terminate "
27001 "a template argument list");
27005 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27006 /* The `>' token might be a greater-than operator again now. */
27007 parser
->greater_than_is_operator_p
27008 = saved_greater_than_is_operator_p
;
27009 /* Restore the SAVED_SCOPE. */
27010 parser
->scope
= saved_scope
;
27011 parser
->qualifying_scope
= saved_qualifying_scope
;
27012 parser
->object_scope
= saved_object_scope
;
27013 cp_unevaluated_operand
= saved_unevaluated_operand
;
27014 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27019 /* MEMBER_FUNCTION is a member function, or a friend. If default
27020 arguments, or the body of the function have not yet been parsed,
27024 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27026 timevar_push (TV_PARSE_INMETH
);
27027 /* If this member is a template, get the underlying
27029 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27030 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27032 /* There should not be any class definitions in progress at this
27033 point; the bodies of members are only parsed outside of all class
27035 gcc_assert (parser
->num_classes_being_defined
== 0);
27036 /* While we're parsing the member functions we might encounter more
27037 classes. We want to handle them right away, but we don't want
27038 them getting mixed up with functions that are currently in the
27040 push_unparsed_function_queues (parser
);
27042 /* Make sure that any template parameters are in scope. */
27043 maybe_begin_member_template_processing (member_function
);
27045 /* If the body of the function has not yet been parsed, parse it
27047 if (DECL_PENDING_INLINE_P (member_function
))
27049 tree function_scope
;
27050 cp_token_cache
*tokens
;
27052 /* The function is no longer pending; we are processing it. */
27053 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27054 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27055 DECL_PENDING_INLINE_P (member_function
) = 0;
27057 /* If this is a local class, enter the scope of the containing
27059 function_scope
= current_function_decl
;
27060 if (function_scope
)
27061 push_function_context ();
27063 /* Push the body of the function onto the lexer stack. */
27064 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27066 /* Let the front end know that we going to be defining this
27068 start_preparsed_function (member_function
, NULL_TREE
,
27069 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27071 /* Don't do access checking if it is a templated function. */
27072 if (processing_template_decl
)
27073 push_deferring_access_checks (dk_no_check
);
27075 /* #pragma omp declare reduction needs special parsing. */
27076 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27078 parser
->lexer
->in_pragma
= true;
27079 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27080 finish_function (/*inline*/2);
27081 cp_check_omp_declare_reduction (member_function
);
27084 /* Now, parse the body of the function. */
27085 cp_parser_function_definition_after_declarator (parser
,
27086 /*inline_p=*/true);
27088 if (processing_template_decl
)
27089 pop_deferring_access_checks ();
27091 /* Leave the scope of the containing function. */
27092 if (function_scope
)
27093 pop_function_context ();
27094 cp_parser_pop_lexer (parser
);
27097 /* Remove any template parameters from the symbol table. */
27098 maybe_end_member_template_processing ();
27100 /* Restore the queue. */
27101 pop_unparsed_function_queues (parser
);
27102 timevar_pop (TV_PARSE_INMETH
);
27105 /* If DECL contains any default args, remember it on the unparsed
27106 functions queue. */
27109 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27113 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27115 probe
= TREE_CHAIN (probe
))
27116 if (TREE_PURPOSE (probe
))
27118 cp_default_arg_entry entry
= {current_class_type
, decl
};
27119 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27124 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27125 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27126 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27127 from the parameter-type-list. */
27130 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27131 tree default_arg
, tree parmtype
)
27133 cp_token_cache
*tokens
;
27137 if (default_arg
== error_mark_node
)
27138 return error_mark_node
;
27140 /* Push the saved tokens for the default argument onto the parser's
27142 tokens
= DEFARG_TOKENS (default_arg
);
27143 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27145 start_lambda_scope (decl
);
27147 /* Parse the default argument. */
27148 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27149 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27150 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27152 finish_lambda_scope ();
27154 if (parsed_arg
== error_mark_node
)
27155 cp_parser_skip_to_end_of_statement (parser
);
27157 if (!processing_template_decl
)
27159 /* In a non-template class, check conversions now. In a template,
27160 we'll wait and instantiate these as needed. */
27161 if (TREE_CODE (decl
) == PARM_DECL
)
27162 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27163 tf_warning_or_error
);
27165 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
27168 /* If the token stream has not been completely used up, then
27169 there was extra junk after the end of the default
27171 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27173 if (TREE_CODE (decl
) == PARM_DECL
)
27174 cp_parser_error (parser
, "expected %<,%>");
27176 cp_parser_error (parser
, "expected %<;%>");
27179 /* Revert to the main lexer. */
27180 cp_parser_pop_lexer (parser
);
27185 /* FIELD is a non-static data member with an initializer which we saved for
27186 later; parse it now. */
27189 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27193 maybe_begin_member_template_processing (field
);
27195 push_unparsed_function_queues (parser
);
27196 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27197 DECL_INITIAL (field
),
27199 pop_unparsed_function_queues (parser
);
27201 maybe_end_member_template_processing ();
27203 DECL_INITIAL (field
) = def
;
27206 /* FN is a FUNCTION_DECL which may contains a parameter with an
27207 unparsed DEFAULT_ARG. Parse the default args now. This function
27208 assumes that the current scope is the scope in which the default
27209 argument should be processed. */
27212 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27214 bool saved_local_variables_forbidden_p
;
27215 tree parm
, parmdecl
;
27217 /* While we're parsing the default args, we might (due to the
27218 statement expression extension) encounter more classes. We want
27219 to handle them right away, but we don't want them getting mixed
27220 up with default args that are currently in the queue. */
27221 push_unparsed_function_queues (parser
);
27223 /* Local variable names (and the `this' keyword) may not appear
27224 in a default argument. */
27225 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27226 parser
->local_variables_forbidden_p
= true;
27228 push_defarg_context (fn
);
27230 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27231 parmdecl
= DECL_ARGUMENTS (fn
);
27232 parm
&& parm
!= void_list_node
;
27233 parm
= TREE_CHAIN (parm
),
27234 parmdecl
= DECL_CHAIN (parmdecl
))
27236 tree default_arg
= TREE_PURPOSE (parm
);
27238 vec
<tree
, va_gc
> *insts
;
27245 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27246 /* This can happen for a friend declaration for a function
27247 already declared with default arguments. */
27251 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27253 TREE_VALUE (parm
));
27254 if (parsed_arg
== error_mark_node
)
27259 TREE_PURPOSE (parm
) = parsed_arg
;
27261 /* Update any instantiations we've already created. */
27262 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27263 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27264 TREE_PURPOSE (copy
) = parsed_arg
;
27267 pop_defarg_context ();
27269 /* Make sure no default arg is missing. */
27270 check_default_args (fn
);
27272 /* Restore the state of local_variables_forbidden_p. */
27273 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27275 /* Restore the queue. */
27276 pop_unparsed_function_queues (parser
);
27279 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27281 sizeof ... ( identifier )
27283 where the 'sizeof' token has already been consumed. */
27286 cp_parser_sizeof_pack (cp_parser
*parser
)
27288 /* Consume the `...'. */
27289 cp_lexer_consume_token (parser
->lexer
);
27290 maybe_warn_variadic_templates ();
27292 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27294 cp_lexer_consume_token (parser
->lexer
);
27296 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27297 "%<sizeof...%> argument must be surrounded by parentheses");
27299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27300 tree name
= cp_parser_identifier (parser
);
27301 if (name
== error_mark_node
)
27302 return error_mark_node
;
27303 /* The name is not qualified. */
27304 parser
->scope
= NULL_TREE
;
27305 parser
->qualifying_scope
= NULL_TREE
;
27306 parser
->object_scope
= NULL_TREE
;
27307 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27308 if (expr
== error_mark_node
)
27309 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27311 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27312 expr
= TREE_TYPE (expr
);
27313 else if (TREE_CODE (expr
) == CONST_DECL
)
27314 expr
= DECL_INITIAL (expr
);
27315 expr
= make_pack_expansion (expr
);
27316 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27324 /* Parse the operand of `sizeof' (or a similar operator). Returns
27325 either a TYPE or an expression, depending on the form of the
27326 input. The KEYWORD indicates which kind of expression we have
27330 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27332 tree expr
= NULL_TREE
;
27333 const char *saved_message
;
27335 bool saved_integral_constant_expression_p
;
27336 bool saved_non_integral_constant_expression_p
;
27338 /* If it's a `...', then we are computing the length of a parameter
27340 if (keyword
== RID_SIZEOF
27341 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27342 return cp_parser_sizeof_pack (parser
);
27344 /* Types cannot be defined in a `sizeof' expression. Save away the
27346 saved_message
= parser
->type_definition_forbidden_message
;
27347 /* And create the new one. */
27348 tmp
= concat ("types may not be defined in %<",
27349 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27350 "%> expressions", NULL
);
27351 parser
->type_definition_forbidden_message
= tmp
;
27353 /* The restrictions on constant-expressions do not apply inside
27354 sizeof expressions. */
27355 saved_integral_constant_expression_p
27356 = parser
->integral_constant_expression_p
;
27357 saved_non_integral_constant_expression_p
27358 = parser
->non_integral_constant_expression_p
;
27359 parser
->integral_constant_expression_p
= false;
27361 /* Do not actually evaluate the expression. */
27362 ++cp_unevaluated_operand
;
27363 ++c_inhibit_evaluation_warnings
;
27364 /* If it's a `(', then we might be looking at the type-id
27366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27368 tree type
= NULL_TREE
;
27370 /* We can't be sure yet whether we're looking at a type-id or an
27372 cp_parser_parse_tentatively (parser
);
27373 /* Note: as a GNU Extension, compound literals are considered
27374 postfix-expressions as they are in C99, so they are valid
27375 arguments to sizeof. See comment in cp_parser_cast_expression
27377 if (cp_parser_compound_literal_p (parser
))
27378 cp_parser_simulate_error (parser
);
27381 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27382 parser
->in_type_id_in_expr_p
= true;
27383 /* Look for the type-id. */
27384 type
= cp_parser_type_id (parser
);
27385 /* Look for the closing `)'. */
27386 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27387 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27390 /* If all went well, then we're done. */
27391 if (cp_parser_parse_definitely (parser
))
27393 cp_decl_specifier_seq decl_specs
;
27395 /* Build a trivial decl-specifier-seq. */
27396 clear_decl_specs (&decl_specs
);
27397 decl_specs
.type
= type
;
27399 /* Call grokdeclarator to figure out what type this is. */
27400 expr
= grokdeclarator (NULL
,
27404 /*attrlist=*/NULL
);
27408 /* If the type-id production did not work out, then we must be
27409 looking at the unary-expression production. */
27411 expr
= cp_parser_unary_expression (parser
);
27413 /* Go back to evaluating expressions. */
27414 --cp_unevaluated_operand
;
27415 --c_inhibit_evaluation_warnings
;
27417 /* Free the message we created. */
27419 /* And restore the old one. */
27420 parser
->type_definition_forbidden_message
= saved_message
;
27421 parser
->integral_constant_expression_p
27422 = saved_integral_constant_expression_p
;
27423 parser
->non_integral_constant_expression_p
27424 = saved_non_integral_constant_expression_p
;
27429 /* If the current declaration has no declarator, return true. */
27432 cp_parser_declares_only_class_p (cp_parser
*parser
)
27434 /* If the next token is a `;' or a `,' then there is no
27436 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27437 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27440 /* Update the DECL_SPECS to reflect the storage class indicated by
27444 cp_parser_set_storage_class (cp_parser
*parser
,
27445 cp_decl_specifier_seq
*decl_specs
,
27449 cp_storage_class storage_class
;
27451 if (parser
->in_unbraced_linkage_specification_p
)
27453 error_at (token
->location
, "invalid use of %qD in linkage specification",
27454 ridpointers
[keyword
]);
27457 else if (decl_specs
->storage_class
!= sc_none
)
27459 decl_specs
->conflicting_specifiers_p
= true;
27463 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27464 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27465 && decl_specs
->gnu_thread_keyword_p
)
27467 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27468 "%<__thread%> before %qD", ridpointers
[keyword
]);
27474 storage_class
= sc_auto
;
27477 storage_class
= sc_register
;
27480 storage_class
= sc_static
;
27483 storage_class
= sc_extern
;
27486 storage_class
= sc_mutable
;
27489 gcc_unreachable ();
27491 decl_specs
->storage_class
= storage_class
;
27492 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27494 /* A storage class specifier cannot be applied alongside a typedef
27495 specifier. If there is a typedef specifier present then set
27496 conflicting_specifiers_p which will trigger an error later
27497 on in grokdeclarator. */
27498 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27499 decl_specs
->conflicting_specifiers_p
= true;
27502 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27503 is true, the type is a class or enum definition. */
27506 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27509 bool type_definition_p
)
27511 decl_specs
->any_specifiers_p
= true;
27513 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27514 (with, for example, in "typedef int wchar_t;") we remember that
27515 this is what happened. In system headers, we ignore these
27516 declarations so that G++ can work with system headers that are not
27518 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27519 && !type_definition_p
27520 && (type_spec
== boolean_type_node
27521 || type_spec
== char16_type_node
27522 || type_spec
== char32_type_node
27523 || type_spec
== wchar_type_node
)
27524 && (decl_specs
->type
27525 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27526 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27527 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27528 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27530 decl_specs
->redefined_builtin_type
= type_spec
;
27531 set_and_check_decl_spec_loc (decl_specs
,
27532 ds_redefined_builtin_type_spec
,
27534 if (!decl_specs
->type
)
27536 decl_specs
->type
= type_spec
;
27537 decl_specs
->type_definition_p
= false;
27538 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27541 else if (decl_specs
->type
)
27542 decl_specs
->multiple_types_p
= true;
27545 decl_specs
->type
= type_spec
;
27546 decl_specs
->type_definition_p
= type_definition_p
;
27547 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27548 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27552 /* True iff TOKEN is the GNU keyword __thread. */
27555 token_is__thread (cp_token
*token
)
27557 gcc_assert (token
->keyword
== RID_THREAD
);
27558 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
27561 /* Set the location for a declarator specifier and check if it is
27564 DECL_SPECS is the sequence of declarator specifiers onto which to
27567 DS is the single declarator specifier to set which location is to
27568 be set onto the existing sequence of declarators.
27570 LOCATION is the location for the declarator specifier to
27574 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
27575 cp_decl_spec ds
, cp_token
*token
)
27577 gcc_assert (ds
< ds_last
);
27579 if (decl_specs
== NULL
)
27582 source_location location
= token
->location
;
27584 if (decl_specs
->locations
[ds
] == 0)
27586 decl_specs
->locations
[ds
] = location
;
27587 if (ds
== ds_thread
)
27588 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
27594 if (decl_specs
->locations
[ds_long_long
] != 0)
27595 error_at (location
,
27596 "%<long long long%> is too long for GCC");
27599 decl_specs
->locations
[ds_long_long
] = location
;
27600 pedwarn_cxx98 (location
,
27602 "ISO C++ 1998 does not support %<long long%>");
27605 else if (ds
== ds_thread
)
27607 bool gnu
= token_is__thread (token
);
27608 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
27609 error_at (location
,
27610 "both %<__thread%> and %<thread_local%> specified");
27612 error_at (location
, "duplicate %qD", token
->u
.value
);
27616 static const char *const decl_spec_names
[] = {
27633 error_at (location
,
27634 "duplicate %qs", decl_spec_names
[ds
]);
27639 /* Return true iff the declarator specifier DS is present in the
27640 sequence of declarator specifiers DECL_SPECS. */
27643 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
27646 gcc_assert (ds
< ds_last
);
27648 if (decl_specs
== NULL
)
27651 return decl_specs
->locations
[ds
] != 0;
27654 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27655 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27658 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
27660 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
27663 /* Issue an error message indicating that TOKEN_DESC was expected.
27664 If KEYWORD is true, it indicated this function is called by
27665 cp_parser_require_keword and the required token can only be
27666 a indicated keyword. */
27669 cp_parser_required_error (cp_parser
*parser
,
27670 required_token token_desc
,
27673 switch (token_desc
)
27676 cp_parser_error (parser
, "expected %<new%>");
27679 cp_parser_error (parser
, "expected %<delete%>");
27682 cp_parser_error (parser
, "expected %<return%>");
27685 cp_parser_error (parser
, "expected %<while%>");
27688 cp_parser_error (parser
, "expected %<extern%>");
27690 case RT_STATIC_ASSERT
:
27691 cp_parser_error (parser
, "expected %<static_assert%>");
27694 cp_parser_error (parser
, "expected %<decltype%>");
27697 cp_parser_error (parser
, "expected %<operator%>");
27700 cp_parser_error (parser
, "expected %<class%>");
27703 cp_parser_error (parser
, "expected %<template%>");
27706 cp_parser_error (parser
, "expected %<namespace%>");
27709 cp_parser_error (parser
, "expected %<using%>");
27712 cp_parser_error (parser
, "expected %<asm%>");
27715 cp_parser_error (parser
, "expected %<try%>");
27718 cp_parser_error (parser
, "expected %<catch%>");
27721 cp_parser_error (parser
, "expected %<throw%>");
27724 cp_parser_error (parser
, "expected %<__label__%>");
27727 cp_parser_error (parser
, "expected %<@try%>");
27729 case RT_AT_SYNCHRONIZED
:
27730 cp_parser_error (parser
, "expected %<@synchronized%>");
27733 cp_parser_error (parser
, "expected %<@throw%>");
27735 case RT_TRANSACTION_ATOMIC
:
27736 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
27738 case RT_TRANSACTION_RELAXED
:
27739 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
27746 switch (token_desc
)
27749 cp_parser_error (parser
, "expected %<;%>");
27751 case RT_OPEN_PAREN
:
27752 cp_parser_error (parser
, "expected %<(%>");
27754 case RT_CLOSE_BRACE
:
27755 cp_parser_error (parser
, "expected %<}%>");
27757 case RT_OPEN_BRACE
:
27758 cp_parser_error (parser
, "expected %<{%>");
27760 case RT_CLOSE_SQUARE
:
27761 cp_parser_error (parser
, "expected %<]%>");
27763 case RT_OPEN_SQUARE
:
27764 cp_parser_error (parser
, "expected %<[%>");
27767 cp_parser_error (parser
, "expected %<,%>");
27770 cp_parser_error (parser
, "expected %<::%>");
27773 cp_parser_error (parser
, "expected %<<%>");
27776 cp_parser_error (parser
, "expected %<>%>");
27779 cp_parser_error (parser
, "expected %<=%>");
27782 cp_parser_error (parser
, "expected %<...%>");
27785 cp_parser_error (parser
, "expected %<*%>");
27788 cp_parser_error (parser
, "expected %<~%>");
27791 cp_parser_error (parser
, "expected %<:%>");
27793 case RT_COLON_SCOPE
:
27794 cp_parser_error (parser
, "expected %<:%> or %<::%>");
27796 case RT_CLOSE_PAREN
:
27797 cp_parser_error (parser
, "expected %<)%>");
27799 case RT_COMMA_CLOSE_PAREN
:
27800 cp_parser_error (parser
, "expected %<,%> or %<)%>");
27802 case RT_PRAGMA_EOL
:
27803 cp_parser_error (parser
, "expected end of line");
27806 cp_parser_error (parser
, "expected identifier");
27809 cp_parser_error (parser
, "expected selection-statement");
27811 case RT_INTERATION
:
27812 cp_parser_error (parser
, "expected iteration-statement");
27815 cp_parser_error (parser
, "expected jump-statement");
27818 cp_parser_error (parser
, "expected class-key");
27820 case RT_CLASS_TYPENAME_TEMPLATE
:
27821 cp_parser_error (parser
,
27822 "expected %<class%>, %<typename%>, or %<template%>");
27825 gcc_unreachable ();
27829 gcc_unreachable ();
27834 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27835 issue an error message indicating that TOKEN_DESC was expected.
27837 Returns the token consumed, if the token had the appropriate type.
27838 Otherwise, returns NULL. */
27841 cp_parser_require (cp_parser
* parser
,
27842 enum cpp_ttype type
,
27843 required_token token_desc
)
27845 if (cp_lexer_next_token_is (parser
->lexer
, type
))
27846 return cp_lexer_consume_token (parser
->lexer
);
27849 /* Output the MESSAGE -- unless we're parsing tentatively. */
27850 if (!cp_parser_simulate_error (parser
))
27851 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
27856 /* An error message is produced if the next token is not '>'.
27857 All further tokens are skipped until the desired token is
27858 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27861 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
27863 /* Current level of '< ... >'. */
27864 unsigned level
= 0;
27865 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27866 unsigned nesting_depth
= 0;
27868 /* Are we ready, yet? If not, issue error message. */
27869 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
27872 /* Skip tokens until the desired token is found. */
27875 /* Peek at the next token. */
27876 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27879 if (!nesting_depth
)
27884 if (cxx_dialect
== cxx98
)
27885 /* C++0x views the `>>' operator as two `>' tokens, but
27888 else if (!nesting_depth
&& level
-- == 0)
27890 /* We've hit a `>>' where the first `>' closes the
27891 template argument list, and the second `>' is
27892 spurious. Just consume the `>>' and stop; we've
27893 already produced at least one error. */
27894 cp_lexer_consume_token (parser
->lexer
);
27897 /* Fall through for C++0x, so we handle the second `>' in
27899 gcc_fallthrough ();
27902 if (!nesting_depth
&& level
-- == 0)
27904 /* We've reached the token we want, consume it and stop. */
27905 cp_lexer_consume_token (parser
->lexer
);
27910 case CPP_OPEN_PAREN
:
27911 case CPP_OPEN_SQUARE
:
27915 case CPP_CLOSE_PAREN
:
27916 case CPP_CLOSE_SQUARE
:
27917 if (nesting_depth
-- == 0)
27922 case CPP_PRAGMA_EOL
:
27923 case CPP_SEMICOLON
:
27924 case CPP_OPEN_BRACE
:
27925 case CPP_CLOSE_BRACE
:
27926 /* The '>' was probably forgotten, don't look further. */
27933 /* Consume this token. */
27934 cp_lexer_consume_token (parser
->lexer
);
27938 /* If the next token is the indicated keyword, consume it. Otherwise,
27939 issue an error message indicating that TOKEN_DESC was expected.
27941 Returns the token consumed, if the token had the appropriate type.
27942 Otherwise, returns NULL. */
27945 cp_parser_require_keyword (cp_parser
* parser
,
27947 required_token token_desc
)
27949 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
27951 if (token
&& token
->keyword
!= keyword
)
27953 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
27960 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27961 function-definition. */
27964 cp_parser_token_starts_function_definition_p (cp_token
* token
)
27966 return (/* An ordinary function-body begins with an `{'. */
27967 token
->type
== CPP_OPEN_BRACE
27968 /* A ctor-initializer begins with a `:'. */
27969 || token
->type
== CPP_COLON
27970 /* A function-try-block begins with `try'. */
27971 || token
->keyword
== RID_TRY
27972 /* A function-transaction-block begins with `__transaction_atomic'
27973 or `__transaction_relaxed'. */
27974 || token
->keyword
== RID_TRANSACTION_ATOMIC
27975 || token
->keyword
== RID_TRANSACTION_RELAXED
27976 /* The named return value extension begins with `return'. */
27977 || token
->keyword
== RID_RETURN
);
27980 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27984 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
27988 token
= cp_lexer_peek_token (parser
->lexer
);
27989 return (token
->type
== CPP_OPEN_BRACE
27990 || (token
->type
== CPP_COLON
27991 && !parser
->colon_doesnt_start_class_def_p
));
27994 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27995 C++0x) ending a template-argument. */
27998 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28002 token
= cp_lexer_peek_token (parser
->lexer
);
28003 return (token
->type
== CPP_COMMA
28004 || token
->type
== CPP_GREATER
28005 || token
->type
== CPP_ELLIPSIS
28006 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28009 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28010 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28013 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28018 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28019 if (token
->type
== CPP_LESS
)
28021 /* Check for the sequence `<::' in the original code. It would be lexed as
28022 `[:', where `[' is a digraph, and there is no whitespace before
28024 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28027 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28028 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28034 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28035 or none_type otherwise. */
28037 static enum tag_types
28038 cp_parser_token_is_class_key (cp_token
* token
)
28040 switch (token
->keyword
)
28045 return record_type
;
28054 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28055 or none_type otherwise or if the token is null. */
28057 static enum tag_types
28058 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28063 switch (token
->keyword
)
28068 return typename_type
;
28075 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28078 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28080 if (type
== error_mark_node
)
28082 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28084 if (permerror (input_location
, "%qs tag used in naming %q#T",
28085 class_key
== union_type
? "union"
28086 : class_key
== record_type
? "struct" : "class",
28088 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28089 "%q#T was previously declared here", type
);
28093 /* Issue an error message if DECL is redeclared with different
28094 access than its original declaration [class.access.spec/3].
28095 This applies to nested classes, nested class templates and
28096 enumerations [class.mem/1]. */
28099 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28102 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28103 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28106 if ((TREE_PRIVATE (decl
)
28107 != (current_access_specifier
== access_private_node
))
28108 || (TREE_PROTECTED (decl
)
28109 != (current_access_specifier
== access_protected_node
)))
28110 error_at (location
, "%qD redeclared with different access", decl
);
28113 /* Look for the `template' keyword, as a syntactic disambiguator.
28114 Return TRUE iff it is present, in which case it will be
28118 cp_parser_optional_template_keyword (cp_parser
*parser
)
28120 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28122 /* In C++98 the `template' keyword can only be used within templates;
28123 outside templates the parser can always figure out what is a
28124 template and what is not. In C++11, per the resolution of DR 468,
28125 `template' is allowed in cases where it is not strictly necessary. */
28126 if (!processing_template_decl
28127 && pedantic
&& cxx_dialect
== cxx98
)
28129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28130 pedwarn (token
->location
, OPT_Wpedantic
,
28131 "in C++98 %<template%> (as a disambiguator) is only "
28132 "allowed within templates");
28133 /* If this part of the token stream is rescanned, the same
28134 error message would be generated. So, we purge the token
28135 from the stream. */
28136 cp_lexer_purge_token (parser
->lexer
);
28141 /* Consume the `template' keyword. */
28142 cp_lexer_consume_token (parser
->lexer
);
28149 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28150 set PARSER->SCOPE, and perform other related actions. */
28153 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28155 struct tree_check
*check_value
;
28157 /* Get the stored value. */
28158 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28159 /* Set the scope from the stored value. */
28160 parser
->scope
= saved_checks_value (check_value
);
28161 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28162 parser
->object_scope
= NULL_TREE
;
28165 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28166 encounter the end of a block before what we were looking for. */
28169 cp_parser_cache_group (cp_parser
*parser
,
28170 enum cpp_ttype end
,
28175 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28177 /* Abort a parenthesized expression if we encounter a semicolon. */
28178 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28179 && token
->type
== CPP_SEMICOLON
)
28181 /* If we've reached the end of the file, stop. */
28182 if (token
->type
== CPP_EOF
28183 || (end
!= CPP_PRAGMA_EOL
28184 && token
->type
== CPP_PRAGMA_EOL
))
28186 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28187 /* We've hit the end of an enclosing block, so there's been some
28188 kind of syntax error. */
28191 /* Consume the token. */
28192 cp_lexer_consume_token (parser
->lexer
);
28193 /* See if it starts a new group. */
28194 if (token
->type
== CPP_OPEN_BRACE
)
28196 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28197 /* In theory this should probably check end == '}', but
28198 cp_parser_save_member_function_body needs it to exit
28199 after either '}' or ')' when called with ')'. */
28203 else if (token
->type
== CPP_OPEN_PAREN
)
28205 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28206 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28209 else if (token
->type
== CPP_PRAGMA
)
28210 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28211 else if (token
->type
== end
)
28216 /* Like above, for caching a default argument or NSDMI. Both of these are
28217 terminated by a non-nested comma, but it can be unclear whether or not a
28218 comma is nested in a template argument list unless we do more parsing.
28219 In order to handle this ambiguity, when we encounter a ',' after a '<'
28220 we try to parse what follows as a parameter-declaration-list (in the
28221 case of a default argument) or a member-declarator (in the case of an
28222 NSDMI). If that succeeds, then we stop caching. */
28225 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28227 unsigned depth
= 0;
28228 int maybe_template_id
= 0;
28229 cp_token
*first_token
;
28231 tree default_argument
;
28233 /* Add tokens until we have processed the entire default
28234 argument. We add the range [first_token, token). */
28235 first_token
= cp_lexer_peek_token (parser
->lexer
);
28236 if (first_token
->type
== CPP_OPEN_BRACE
)
28238 /* For list-initialization, this is straightforward. */
28239 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28240 token
= cp_lexer_peek_token (parser
->lexer
);
28246 /* Peek at the next token. */
28247 token
= cp_lexer_peek_token (parser
->lexer
);
28248 /* What we do depends on what token we have. */
28249 switch (token
->type
)
28251 /* In valid code, a default argument must be
28252 immediately followed by a `,' `)', or `...'. */
28254 if (depth
== 0 && maybe_template_id
)
28256 /* If we've seen a '<', we might be in a
28257 template-argument-list. Until Core issue 325 is
28258 resolved, we don't know how this situation ought
28259 to be handled, so try to DTRT. We check whether
28260 what comes after the comma is a valid parameter
28261 declaration list. If it is, then the comma ends
28262 the default argument; otherwise the default
28263 argument continues. */
28264 bool error
= false;
28267 /* Set ITALP so cp_parser_parameter_declaration_list
28268 doesn't decide to commit to this parse. */
28269 bool saved_italp
= parser
->in_template_argument_list_p
;
28270 parser
->in_template_argument_list_p
= true;
28272 cp_parser_parse_tentatively (parser
);
28276 /* Parse declarators until we reach a non-comma or
28277 somthing that cannot be an initializer.
28278 Just checking whether we're looking at a single
28279 declarator is insufficient. Consider:
28280 int var = tuple<T,U>::x;
28281 The template parameter 'U' looks exactly like a
28285 int ctor_dtor_or_conv_p
;
28286 cp_lexer_consume_token (parser
->lexer
);
28287 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28288 &ctor_dtor_or_conv_p
,
28289 /*parenthesized_p=*/NULL
,
28291 /*friend_p=*/false);
28292 peek
= cp_lexer_peek_token (parser
->lexer
);
28293 if (cp_parser_error_occurred (parser
))
28296 while (peek
->type
== CPP_COMMA
);
28297 /* If we met an '=' or ';' then the original comma
28298 was the end of the NSDMI. Otherwise assume
28299 we're still in the NSDMI. */
28300 error
= (peek
->type
!= CPP_EQ
28301 && peek
->type
!= CPP_SEMICOLON
);
28305 cp_lexer_consume_token (parser
->lexer
);
28306 begin_scope (sk_function_parms
, NULL_TREE
);
28307 cp_parser_parameter_declaration_list (parser
, &error
);
28308 pop_bindings_and_leave_scope ();
28310 if (!cp_parser_error_occurred (parser
) && !error
)
28312 cp_parser_abort_tentative_parse (parser
);
28314 parser
->in_template_argument_list_p
= saved_italp
;
28318 case CPP_CLOSE_PAREN
:
28320 /* If we run into a non-nested `;', `}', or `]',
28321 then the code is invalid -- but the default
28322 argument is certainly over. */
28323 case CPP_SEMICOLON
:
28324 case CPP_CLOSE_BRACE
:
28325 case CPP_CLOSE_SQUARE
:
28327 /* Handle correctly int n = sizeof ... ( p ); */
28328 && token
->type
!= CPP_ELLIPSIS
)
28330 /* Update DEPTH, if necessary. */
28331 else if (token
->type
== CPP_CLOSE_PAREN
28332 || token
->type
== CPP_CLOSE_BRACE
28333 || token
->type
== CPP_CLOSE_SQUARE
)
28337 case CPP_OPEN_PAREN
:
28338 case CPP_OPEN_SQUARE
:
28339 case CPP_OPEN_BRACE
:
28345 /* This might be the comparison operator, or it might
28346 start a template argument list. */
28347 ++maybe_template_id
;
28351 if (cxx_dialect
== cxx98
)
28353 /* Fall through for C++0x, which treats the `>>'
28354 operator like two `>' tokens in certain
28356 gcc_fallthrough ();
28361 /* This might be an operator, or it might close a
28362 template argument list. But if a previous '<'
28363 started a template argument list, this will have
28364 closed it, so we can't be in one anymore. */
28365 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28366 if (maybe_template_id
< 0)
28367 maybe_template_id
= 0;
28371 /* If we run out of tokens, issue an error message. */
28373 case CPP_PRAGMA_EOL
:
28374 error_at (token
->location
, "file ends in default argument");
28375 return error_mark_node
;
28379 /* In these cases, we should look for template-ids.
28380 For example, if the default argument is
28381 `X<int, double>()', we need to do name lookup to
28382 figure out whether or not `X' is a template; if
28383 so, the `,' does not end the default argument.
28385 That is not yet done. */
28392 /* If we've reached the end, stop. */
28396 /* Add the token to the token block. */
28397 token
= cp_lexer_consume_token (parser
->lexer
);
28400 /* Create a DEFAULT_ARG to represent the unparsed default
28402 default_argument
= make_node (DEFAULT_ARG
);
28403 DEFARG_TOKENS (default_argument
)
28404 = cp_token_cache_new (first_token
, token
);
28405 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28407 return default_argument
;
28410 /* Begin parsing tentatively. We always save tokens while parsing
28411 tentatively so that if the tentative parsing fails we can restore the
28415 cp_parser_parse_tentatively (cp_parser
* parser
)
28417 /* Enter a new parsing context. */
28418 parser
->context
= cp_parser_context_new (parser
->context
);
28419 /* Begin saving tokens. */
28420 cp_lexer_save_tokens (parser
->lexer
);
28421 /* In order to avoid repetitive access control error messages,
28422 access checks are queued up until we are no longer parsing
28424 push_deferring_access_checks (dk_deferred
);
28427 /* Commit to the currently active tentative parse. */
28430 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28432 cp_parser_context
*context
;
28435 /* Mark all of the levels as committed. */
28436 lexer
= parser
->lexer
;
28437 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28439 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28441 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28442 while (!cp_lexer_saving_tokens (lexer
))
28443 lexer
= lexer
->next
;
28444 cp_lexer_commit_tokens (lexer
);
28448 /* Commit to the topmost currently active tentative parse.
28450 Note that this function shouldn't be called when there are
28451 irreversible side-effects while in a tentative state. For
28452 example, we shouldn't create a permanent entry in the symbol
28453 table, or issue an error message that might not apply if the
28454 tentative parse is aborted. */
28457 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28459 cp_parser_context
*context
= parser
->context
;
28460 cp_lexer
*lexer
= parser
->lexer
;
28464 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28466 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28468 while (!cp_lexer_saving_tokens (lexer
))
28469 lexer
= lexer
->next
;
28470 cp_lexer_commit_tokens (lexer
);
28474 /* Abort the currently active tentative parse. All consumed tokens
28475 will be rolled back, and no diagnostics will be issued. */
28478 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28480 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28481 || errorcount
> 0);
28482 cp_parser_simulate_error (parser
);
28483 /* Now, pretend that we want to see if the construct was
28484 successfully parsed. */
28485 cp_parser_parse_definitely (parser
);
28488 /* Stop parsing tentatively. If a parse error has occurred, restore the
28489 token stream. Otherwise, commit to the tokens we have consumed.
28490 Returns true if no error occurred; false otherwise. */
28493 cp_parser_parse_definitely (cp_parser
* parser
)
28495 bool error_occurred
;
28496 cp_parser_context
*context
;
28498 /* Remember whether or not an error occurred, since we are about to
28499 destroy that information. */
28500 error_occurred
= cp_parser_error_occurred (parser
);
28501 /* Remove the topmost context from the stack. */
28502 context
= parser
->context
;
28503 parser
->context
= context
->next
;
28504 /* If no parse errors occurred, commit to the tentative parse. */
28505 if (!error_occurred
)
28507 /* Commit to the tokens read tentatively, unless that was
28509 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28510 cp_lexer_commit_tokens (parser
->lexer
);
28512 pop_to_parent_deferring_access_checks ();
28514 /* Otherwise, if errors occurred, roll back our state so that things
28515 are just as they were before we began the tentative parse. */
28518 cp_lexer_rollback_tokens (parser
->lexer
);
28519 pop_deferring_access_checks ();
28521 /* Add the context to the front of the free list. */
28522 context
->next
= cp_parser_context_free_list
;
28523 cp_parser_context_free_list
= context
;
28525 return !error_occurred
;
28528 /* Returns true if we are parsing tentatively and are not committed to
28529 this tentative parse. */
28532 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
28534 return (cp_parser_parsing_tentatively (parser
)
28535 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
28538 /* Returns nonzero iff an error has occurred during the most recent
28539 tentative parse. */
28542 cp_parser_error_occurred (cp_parser
* parser
)
28544 return (cp_parser_parsing_tentatively (parser
)
28545 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
28548 /* Returns nonzero if GNU extensions are allowed. */
28551 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
28553 return parser
->allow_gnu_extensions_p
;
28556 /* Objective-C++ Productions */
28559 /* Parse an Objective-C expression, which feeds into a primary-expression
28563 objc-message-expression
28564 objc-string-literal
28565 objc-encode-expression
28566 objc-protocol-expression
28567 objc-selector-expression
28569 Returns a tree representation of the expression. */
28572 cp_parser_objc_expression (cp_parser
* parser
)
28574 /* Try to figure out what kind of declaration is present. */
28575 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28579 case CPP_OPEN_SQUARE
:
28580 return cp_parser_objc_message_expression (parser
);
28582 case CPP_OBJC_STRING
:
28583 kwd
= cp_lexer_consume_token (parser
->lexer
);
28584 return objc_build_string_object (kwd
->u
.value
);
28587 switch (kwd
->keyword
)
28589 case RID_AT_ENCODE
:
28590 return cp_parser_objc_encode_expression (parser
);
28592 case RID_AT_PROTOCOL
:
28593 return cp_parser_objc_protocol_expression (parser
);
28595 case RID_AT_SELECTOR
:
28596 return cp_parser_objc_selector_expression (parser
);
28602 error_at (kwd
->location
,
28603 "misplaced %<@%D%> Objective-C++ construct",
28605 cp_parser_skip_to_end_of_block_or_statement (parser
);
28608 return error_mark_node
;
28611 /* Parse an Objective-C message expression.
28613 objc-message-expression:
28614 [ objc-message-receiver objc-message-args ]
28616 Returns a representation of an Objective-C message. */
28619 cp_parser_objc_message_expression (cp_parser
* parser
)
28621 tree receiver
, messageargs
;
28623 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28624 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
28625 receiver
= cp_parser_objc_message_receiver (parser
);
28626 messageargs
= cp_parser_objc_message_args (parser
);
28627 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28628 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28630 tree result
= objc_build_message_expr (receiver
, messageargs
);
28632 /* Construct a location e.g.
28635 ranging from the '[' to the ']', with the caret at the start. */
28636 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
28637 protected_set_expr_location (result
, combined_loc
);
28642 /* Parse an objc-message-receiver.
28644 objc-message-receiver:
28646 simple-type-specifier
28648 Returns a representation of the type or expression. */
28651 cp_parser_objc_message_receiver (cp_parser
* parser
)
28655 /* An Objective-C message receiver may be either (1) a type
28656 or (2) an expression. */
28657 cp_parser_parse_tentatively (parser
);
28658 rcv
= cp_parser_expression (parser
);
28660 /* If that worked out, fine. */
28661 if (cp_parser_parse_definitely (parser
))
28664 cp_parser_parse_tentatively (parser
);
28665 rcv
= cp_parser_simple_type_specifier (parser
,
28666 /*decl_specs=*/NULL
,
28667 CP_PARSER_FLAGS_NONE
);
28669 if (cp_parser_parse_definitely (parser
))
28670 return objc_get_class_reference (rcv
);
28672 cp_parser_error (parser
, "objective-c++ message receiver expected");
28673 return error_mark_node
;
28676 /* Parse the arguments and selectors comprising an Objective-C message.
28681 objc-selector-args , objc-comma-args
28683 objc-selector-args:
28684 objc-selector [opt] : assignment-expression
28685 objc-selector-args objc-selector [opt] : assignment-expression
28688 assignment-expression
28689 objc-comma-args , assignment-expression
28691 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28692 selector arguments and TREE_VALUE containing a list of comma
28696 cp_parser_objc_message_args (cp_parser
* parser
)
28698 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
28699 bool maybe_unary_selector_p
= true;
28700 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28702 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
28704 tree selector
= NULL_TREE
, arg
;
28706 if (token
->type
!= CPP_COLON
)
28707 selector
= cp_parser_objc_selector (parser
);
28709 /* Detect if we have a unary selector. */
28710 if (maybe_unary_selector_p
28711 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28712 return build_tree_list (selector
, NULL_TREE
);
28714 maybe_unary_selector_p
= false;
28715 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
28716 arg
= cp_parser_assignment_expression (parser
);
28719 = chainon (sel_args
,
28720 build_tree_list (selector
, arg
));
28722 token
= cp_lexer_peek_token (parser
->lexer
);
28725 /* Handle non-selector arguments, if any. */
28726 while (token
->type
== CPP_COMMA
)
28730 cp_lexer_consume_token (parser
->lexer
);
28731 arg
= cp_parser_assignment_expression (parser
);
28734 = chainon (addl_args
,
28735 build_tree_list (NULL_TREE
, arg
));
28737 token
= cp_lexer_peek_token (parser
->lexer
);
28740 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
28742 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
28743 return build_tree_list (error_mark_node
, error_mark_node
);
28746 return build_tree_list (sel_args
, addl_args
);
28749 /* Parse an Objective-C encode expression.
28751 objc-encode-expression:
28752 @encode objc-typename
28754 Returns an encoded representation of the type argument. */
28757 cp_parser_objc_encode_expression (cp_parser
* parser
)
28761 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28763 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
28764 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28765 token
= cp_lexer_peek_token (parser
->lexer
);
28766 type
= complete_type (cp_parser_type_id (parser
));
28767 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28771 error_at (token
->location
,
28772 "%<@encode%> must specify a type as an argument");
28773 return error_mark_node
;
28776 /* This happens if we find @encode(T) (where T is a template
28777 typename or something dependent on a template typename) when
28778 parsing a template. In that case, we can't compile it
28779 immediately, but we rather create an AT_ENCODE_EXPR which will
28780 need to be instantiated when the template is used.
28782 if (dependent_type_p (type
))
28784 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
28785 TREE_READONLY (value
) = 1;
28790 /* Build a location of the form:
28793 with caret==start at the @ token, finishing at the close paren. */
28794 location_t combined_loc
28795 = make_location (start_loc
, start_loc
,
28796 cp_lexer_previous_token (parser
->lexer
)->location
);
28798 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
28801 /* Parse an Objective-C @defs expression. */
28804 cp_parser_objc_defs_expression (cp_parser
*parser
)
28808 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
28809 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28810 name
= cp_parser_identifier (parser
);
28811 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28813 return objc_get_class_ivars (name
);
28816 /* Parse an Objective-C protocol expression.
28818 objc-protocol-expression:
28819 @protocol ( identifier )
28821 Returns a representation of the protocol expression. */
28824 cp_parser_objc_protocol_expression (cp_parser
* parser
)
28827 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28829 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28830 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28831 proto
= cp_parser_identifier (parser
);
28832 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28834 /* Build a location of the form:
28837 with caret==start at the @ token, finishing at the close paren. */
28838 location_t combined_loc
28839 = make_location (start_loc
, start_loc
,
28840 cp_lexer_previous_token (parser
->lexer
)->location
);
28841 tree result
= objc_build_protocol_expr (proto
);
28842 protected_set_expr_location (result
, combined_loc
);
28846 /* Parse an Objective-C selector expression.
28848 objc-selector-expression:
28849 @selector ( objc-method-signature )
28851 objc-method-signature:
28857 objc-selector-seq objc-selector :
28859 Returns a representation of the method selector. */
28862 cp_parser_objc_selector_expression (cp_parser
* parser
)
28864 tree sel_seq
= NULL_TREE
;
28865 bool maybe_unary_selector_p
= true;
28867 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28869 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
28870 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28871 token
= cp_lexer_peek_token (parser
->lexer
);
28873 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
28874 || token
->type
== CPP_SCOPE
)
28876 tree selector
= NULL_TREE
;
28878 if (token
->type
!= CPP_COLON
28879 || token
->type
== CPP_SCOPE
)
28880 selector
= cp_parser_objc_selector (parser
);
28882 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
28883 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
28885 /* Detect if we have a unary selector. */
28886 if (maybe_unary_selector_p
)
28888 sel_seq
= selector
;
28889 goto finish_selector
;
28893 cp_parser_error (parser
, "expected %<:%>");
28896 maybe_unary_selector_p
= false;
28897 token
= cp_lexer_consume_token (parser
->lexer
);
28899 if (token
->type
== CPP_SCOPE
)
28902 = chainon (sel_seq
,
28903 build_tree_list (selector
, NULL_TREE
));
28905 = chainon (sel_seq
,
28906 build_tree_list (NULL_TREE
, NULL_TREE
));
28910 = chainon (sel_seq
,
28911 build_tree_list (selector
, NULL_TREE
));
28913 token
= cp_lexer_peek_token (parser
->lexer
);
28917 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28920 /* Build a location of the form:
28923 with caret==start at the @ token, finishing at the close paren. */
28924 location_t combined_loc
28925 = make_location (loc
, loc
,
28926 cp_lexer_previous_token (parser
->lexer
)->location
);
28927 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
28928 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28929 protected_set_expr_location (result
, combined_loc
);
28933 /* Parse a list of identifiers.
28935 objc-identifier-list:
28937 objc-identifier-list , identifier
28939 Returns a TREE_LIST of identifier nodes. */
28942 cp_parser_objc_identifier_list (cp_parser
* parser
)
28948 identifier
= cp_parser_identifier (parser
);
28949 if (identifier
== error_mark_node
)
28950 return error_mark_node
;
28952 list
= build_tree_list (NULL_TREE
, identifier
);
28953 sep
= cp_lexer_peek_token (parser
->lexer
);
28955 while (sep
->type
== CPP_COMMA
)
28957 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28958 identifier
= cp_parser_identifier (parser
);
28959 if (identifier
== error_mark_node
)
28962 list
= chainon (list
, build_tree_list (NULL_TREE
,
28964 sep
= cp_lexer_peek_token (parser
->lexer
);
28970 /* Parse an Objective-C alias declaration.
28972 objc-alias-declaration:
28973 @compatibility_alias identifier identifier ;
28975 This function registers the alias mapping with the Objective-C front end.
28976 It returns nothing. */
28979 cp_parser_objc_alias_declaration (cp_parser
* parser
)
28983 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
28984 alias
= cp_parser_identifier (parser
);
28985 orig
= cp_parser_identifier (parser
);
28986 objc_declare_alias (alias
, orig
);
28987 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28990 /* Parse an Objective-C class forward-declaration.
28992 objc-class-declaration:
28993 @class objc-identifier-list ;
28995 The function registers the forward declarations with the Objective-C
28996 front end. It returns nothing. */
28999 cp_parser_objc_class_declaration (cp_parser
* parser
)
29001 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29006 id
= cp_parser_identifier (parser
);
29007 if (id
== error_mark_node
)
29010 objc_declare_class (id
);
29012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29013 cp_lexer_consume_token (parser
->lexer
);
29017 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29020 /* Parse a list of Objective-C protocol references.
29022 objc-protocol-refs-opt:
29023 objc-protocol-refs [opt]
29025 objc-protocol-refs:
29026 < objc-identifier-list >
29028 Returns a TREE_LIST of identifiers, if any. */
29031 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29033 tree protorefs
= NULL_TREE
;
29035 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29037 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29038 protorefs
= cp_parser_objc_identifier_list (parser
);
29039 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29045 /* Parse a Objective-C visibility specification. */
29048 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29050 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29052 switch (vis
->keyword
)
29054 case RID_AT_PRIVATE
:
29055 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29057 case RID_AT_PROTECTED
:
29058 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29060 case RID_AT_PUBLIC
:
29061 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29063 case RID_AT_PACKAGE
:
29064 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29070 /* Eat '@private'/'@protected'/'@public'. */
29071 cp_lexer_consume_token (parser
->lexer
);
29074 /* Parse an Objective-C method type. Return 'true' if it is a class
29075 (+) method, and 'false' if it is an instance (-) method. */
29078 cp_parser_objc_method_type (cp_parser
* parser
)
29080 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29086 /* Parse an Objective-C protocol qualifier. */
29089 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29091 tree quals
= NULL_TREE
, node
;
29092 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29094 node
= token
->u
.value
;
29096 while (node
&& identifier_p (node
)
29097 && (node
== ridpointers
[(int) RID_IN
]
29098 || node
== ridpointers
[(int) RID_OUT
]
29099 || node
== ridpointers
[(int) RID_INOUT
]
29100 || node
== ridpointers
[(int) RID_BYCOPY
]
29101 || node
== ridpointers
[(int) RID_BYREF
]
29102 || node
== ridpointers
[(int) RID_ONEWAY
]))
29104 quals
= tree_cons (NULL_TREE
, node
, quals
);
29105 cp_lexer_consume_token (parser
->lexer
);
29106 token
= cp_lexer_peek_token (parser
->lexer
);
29107 node
= token
->u
.value
;
29113 /* Parse an Objective-C typename. */
29116 cp_parser_objc_typename (cp_parser
* parser
)
29118 tree type_name
= NULL_TREE
;
29120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29122 tree proto_quals
, cp_type
= NULL_TREE
;
29124 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29125 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29127 /* An ObjC type name may consist of just protocol qualifiers, in which
29128 case the type shall default to 'id'. */
29129 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29131 cp_type
= cp_parser_type_id (parser
);
29133 /* If the type could not be parsed, an error has already
29134 been produced. For error recovery, behave as if it had
29135 not been specified, which will use the default type
29137 if (cp_type
== error_mark_node
)
29139 cp_type
= NULL_TREE
;
29140 /* We need to skip to the closing parenthesis as
29141 cp_parser_type_id() does not seem to do it for
29143 cp_parser_skip_to_closing_parenthesis (parser
,
29144 /*recovering=*/true,
29145 /*or_comma=*/false,
29146 /*consume_paren=*/false);
29150 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29151 type_name
= build_tree_list (proto_quals
, cp_type
);
29157 /* Check to see if TYPE refers to an Objective-C selector name. */
29160 cp_parser_objc_selector_p (enum cpp_ttype type
)
29162 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29163 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29164 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29165 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29166 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29169 /* Parse an Objective-C selector. */
29172 cp_parser_objc_selector (cp_parser
* parser
)
29174 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29176 if (!cp_parser_objc_selector_p (token
->type
))
29178 error_at (token
->location
, "invalid Objective-C++ selector name");
29179 return error_mark_node
;
29182 /* C++ operator names are allowed to appear in ObjC selectors. */
29183 switch (token
->type
)
29185 case CPP_AND_AND
: return get_identifier ("and");
29186 case CPP_AND_EQ
: return get_identifier ("and_eq");
29187 case CPP_AND
: return get_identifier ("bitand");
29188 case CPP_OR
: return get_identifier ("bitor");
29189 case CPP_COMPL
: return get_identifier ("compl");
29190 case CPP_NOT
: return get_identifier ("not");
29191 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29192 case CPP_OR_OR
: return get_identifier ("or");
29193 case CPP_OR_EQ
: return get_identifier ("or_eq");
29194 case CPP_XOR
: return get_identifier ("xor");
29195 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29196 default: return token
->u
.value
;
29200 /* Parse an Objective-C params list. */
29203 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29205 tree params
= NULL_TREE
;
29206 bool maybe_unary_selector_p
= true;
29207 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29209 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29211 tree selector
= NULL_TREE
, type_name
, identifier
;
29212 tree parm_attr
= NULL_TREE
;
29214 if (token
->keyword
== RID_ATTRIBUTE
)
29217 if (token
->type
!= CPP_COLON
)
29218 selector
= cp_parser_objc_selector (parser
);
29220 /* Detect if we have a unary selector. */
29221 if (maybe_unary_selector_p
29222 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29224 params
= selector
; /* Might be followed by attributes. */
29228 maybe_unary_selector_p
= false;
29229 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29231 /* Something went quite wrong. There should be a colon
29232 here, but there is not. Stop parsing parameters. */
29235 type_name
= cp_parser_objc_typename (parser
);
29236 /* New ObjC allows attributes on parameters too. */
29237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29238 parm_attr
= cp_parser_attributes_opt (parser
);
29239 identifier
= cp_parser_identifier (parser
);
29243 objc_build_keyword_decl (selector
,
29248 token
= cp_lexer_peek_token (parser
->lexer
);
29251 if (params
== NULL_TREE
)
29253 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29254 return error_mark_node
;
29257 /* We allow tail attributes for the method. */
29258 if (token
->keyword
== RID_ATTRIBUTE
)
29260 *attributes
= cp_parser_attributes_opt (parser
);
29261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29262 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29264 cp_parser_error (parser
,
29265 "method attributes must be specified at the end");
29266 return error_mark_node
;
29269 if (params
== NULL_TREE
)
29271 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29272 return error_mark_node
;
29277 /* Parse the non-keyword Objective-C params. */
29280 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29283 tree params
= make_node (TREE_LIST
);
29284 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29285 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29287 while (token
->type
== CPP_COMMA
)
29289 cp_parameter_declarator
*parmdecl
;
29292 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29293 token
= cp_lexer_peek_token (parser
->lexer
);
29295 if (token
->type
== CPP_ELLIPSIS
)
29297 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29299 token
= cp_lexer_peek_token (parser
->lexer
);
29303 /* TODO: parse attributes for tail parameters. */
29304 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29305 parm
= grokdeclarator (parmdecl
->declarator
,
29306 &parmdecl
->decl_specifiers
,
29307 PARM
, /*initialized=*/0,
29308 /*attrlist=*/NULL
);
29310 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29311 token
= cp_lexer_peek_token (parser
->lexer
);
29314 /* We allow tail attributes for the method. */
29315 if (token
->keyword
== RID_ATTRIBUTE
)
29317 if (*attributes
== NULL_TREE
)
29319 *attributes
= cp_parser_attributes_opt (parser
);
29320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29321 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29325 /* We have an error, but parse the attributes, so that we can
29327 *attributes
= cp_parser_attributes_opt (parser
);
29329 cp_parser_error (parser
,
29330 "method attributes must be specified at the end");
29331 return error_mark_node
;
29337 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29340 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29342 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29344 /* If the next token is `extern' and the following token is a string
29345 literal, then we have a linkage specification. */
29346 if (token
->keyword
== RID_EXTERN
29347 && cp_parser_is_pure_string_literal
29348 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29349 cp_parser_linkage_specification (parser
);
29350 /* Handle #pragma, if any. */
29351 else if (token
->type
== CPP_PRAGMA
)
29352 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29353 /* Allow stray semicolons. */
29354 else if (token
->type
== CPP_SEMICOLON
)
29355 cp_lexer_consume_token (parser
->lexer
);
29356 /* Mark methods as optional or required, when building protocols. */
29357 else if (token
->keyword
== RID_AT_OPTIONAL
)
29359 cp_lexer_consume_token (parser
->lexer
);
29360 objc_set_method_opt (true);
29362 else if (token
->keyword
== RID_AT_REQUIRED
)
29364 cp_lexer_consume_token (parser
->lexer
);
29365 objc_set_method_opt (false);
29367 else if (token
->keyword
== RID_NAMESPACE
)
29368 cp_parser_namespace_definition (parser
);
29369 /* Other stray characters must generate errors. */
29370 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29372 cp_lexer_consume_token (parser
->lexer
);
29373 error ("stray %qs between Objective-C++ methods",
29374 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29376 /* Finally, try to parse a block-declaration, or a function-definition. */
29378 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29381 /* Parse a method signature. */
29384 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29386 tree rettype
, kwdparms
, optparms
;
29387 bool ellipsis
= false;
29388 bool is_class_method
;
29390 is_class_method
= cp_parser_objc_method_type (parser
);
29391 rettype
= cp_parser_objc_typename (parser
);
29392 *attributes
= NULL_TREE
;
29393 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29394 if (kwdparms
== error_mark_node
)
29395 return error_mark_node
;
29396 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29397 if (optparms
== error_mark_node
)
29398 return error_mark_node
;
29400 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29404 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29407 cp_lexer_save_tokens (parser
->lexer
);
29408 tattr
= cp_parser_attributes_opt (parser
);
29409 gcc_assert (tattr
) ;
29411 /* If the attributes are followed by a method introducer, this is not allowed.
29412 Dump the attributes and flag the situation. */
29413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29414 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29417 /* Otherwise, the attributes introduce some interstitial code, possibly so
29418 rewind to allow that check. */
29419 cp_lexer_rollback_tokens (parser
->lexer
);
29423 /* Parse an Objective-C method prototype list. */
29426 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29430 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29432 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29434 tree attributes
, sig
;
29435 bool is_class_method
;
29436 if (token
->type
== CPP_PLUS
)
29437 is_class_method
= true;
29439 is_class_method
= false;
29440 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29441 if (sig
== error_mark_node
)
29443 cp_parser_skip_to_end_of_block_or_statement (parser
);
29444 token
= cp_lexer_peek_token (parser
->lexer
);
29447 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29448 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29450 else if (token
->keyword
== RID_AT_PROPERTY
)
29451 cp_parser_objc_at_property_declaration (parser
);
29452 else if (token
->keyword
== RID_ATTRIBUTE
29453 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29454 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29456 "prefix attributes are ignored for methods");
29458 /* Allow for interspersed non-ObjC++ code. */
29459 cp_parser_objc_interstitial_code (parser
);
29461 token
= cp_lexer_peek_token (parser
->lexer
);
29464 if (token
->type
!= CPP_EOF
)
29465 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29467 cp_parser_error (parser
, "expected %<@end%>");
29469 objc_finish_interface ();
29472 /* Parse an Objective-C method definition list. */
29475 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29477 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29479 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29483 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29486 tree sig
, attribute
;
29487 bool is_class_method
;
29488 if (token
->type
== CPP_PLUS
)
29489 is_class_method
= true;
29491 is_class_method
= false;
29492 push_deferring_access_checks (dk_deferred
);
29493 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29494 if (sig
== error_mark_node
)
29496 cp_parser_skip_to_end_of_block_or_statement (parser
);
29497 token
= cp_lexer_peek_token (parser
->lexer
);
29500 objc_start_method_definition (is_class_method
, sig
, attribute
,
29503 /* For historical reasons, we accept an optional semicolon. */
29504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29505 cp_lexer_consume_token (parser
->lexer
);
29507 ptk
= cp_lexer_peek_token (parser
->lexer
);
29508 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29509 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29511 perform_deferred_access_checks (tf_warning_or_error
);
29512 stop_deferring_access_checks ();
29513 meth
= cp_parser_function_definition_after_declarator (parser
,
29515 pop_deferring_access_checks ();
29516 objc_finish_method_definition (meth
);
29519 /* The following case will be removed once @synthesize is
29520 completely implemented. */
29521 else if (token
->keyword
== RID_AT_PROPERTY
)
29522 cp_parser_objc_at_property_declaration (parser
);
29523 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
29524 cp_parser_objc_at_synthesize_declaration (parser
);
29525 else if (token
->keyword
== RID_AT_DYNAMIC
)
29526 cp_parser_objc_at_dynamic_declaration (parser
);
29527 else if (token
->keyword
== RID_ATTRIBUTE
29528 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29529 warning_at (token
->location
, OPT_Wattributes
,
29530 "prefix attributes are ignored for methods");
29532 /* Allow for interspersed non-ObjC++ code. */
29533 cp_parser_objc_interstitial_code (parser
);
29535 token
= cp_lexer_peek_token (parser
->lexer
);
29538 if (token
->type
!= CPP_EOF
)
29539 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29541 cp_parser_error (parser
, "expected %<@end%>");
29543 objc_finish_implementation ();
29546 /* Parse Objective-C ivars. */
29549 cp_parser_objc_class_ivars (cp_parser
* parser
)
29551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29553 if (token
->type
!= CPP_OPEN_BRACE
)
29554 return; /* No ivars specified. */
29556 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
29557 token
= cp_lexer_peek_token (parser
->lexer
);
29559 while (token
->type
!= CPP_CLOSE_BRACE
29560 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29562 cp_decl_specifier_seq declspecs
;
29563 int decl_class_or_enum_p
;
29564 tree prefix_attributes
;
29566 cp_parser_objc_visibility_spec (parser
);
29568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29571 cp_parser_decl_specifier_seq (parser
,
29572 CP_PARSER_FLAGS_OPTIONAL
,
29574 &decl_class_or_enum_p
);
29576 /* auto, register, static, extern, mutable. */
29577 if (declspecs
.storage_class
!= sc_none
)
29579 cp_parser_error (parser
, "invalid type for instance variable");
29580 declspecs
.storage_class
= sc_none
;
29583 /* thread_local. */
29584 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
29586 cp_parser_error (parser
, "invalid type for instance variable");
29587 declspecs
.locations
[ds_thread
] = 0;
29591 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
29593 cp_parser_error (parser
, "invalid type for instance variable");
29594 declspecs
.locations
[ds_typedef
] = 0;
29597 prefix_attributes
= declspecs
.attributes
;
29598 declspecs
.attributes
= NULL_TREE
;
29600 /* Keep going until we hit the `;' at the end of the
29602 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29604 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
29605 cp_declarator
*declarator
= NULL
;
29606 int ctor_dtor_or_conv_p
;
29608 /* Check for a (possibly unnamed) bitfield declaration. */
29609 token
= cp_lexer_peek_token (parser
->lexer
);
29610 if (token
->type
== CPP_COLON
)
29613 if (token
->type
== CPP_NAME
29614 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
29617 /* Get the name of the bitfield. */
29618 declarator
= make_id_declarator (NULL_TREE
,
29619 cp_parser_identifier (parser
),
29623 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29624 /* Get the width of the bitfield. */
29626 = cp_parser_constant_expression (parser
);
29630 /* Parse the declarator. */
29632 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29633 &ctor_dtor_or_conv_p
,
29634 /*parenthesized_p=*/NULL
,
29635 /*member_p=*/false,
29636 /*friend_p=*/false);
29639 /* Look for attributes that apply to the ivar. */
29640 attributes
= cp_parser_attributes_opt (parser
);
29641 /* Remember which attributes are prefix attributes and
29643 first_attribute
= attributes
;
29644 /* Combine the attributes. */
29645 attributes
= chainon (prefix_attributes
, attributes
);
29648 /* Create the bitfield declaration. */
29649 decl
= grokbitfield (declarator
, &declspecs
,
29653 decl
= grokfield (declarator
, &declspecs
,
29654 NULL_TREE
, /*init_const_expr_p=*/false,
29655 NULL_TREE
, attributes
);
29657 /* Add the instance variable. */
29658 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
29659 objc_add_instance_variable (decl
);
29661 /* Reset PREFIX_ATTRIBUTES. */
29662 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
29663 attributes
= TREE_CHAIN (attributes
);
29665 TREE_CHAIN (attributes
) = NULL_TREE
;
29667 token
= cp_lexer_peek_token (parser
->lexer
);
29669 if (token
->type
== CPP_COMMA
)
29671 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29677 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29678 token
= cp_lexer_peek_token (parser
->lexer
);
29681 if (token
->keyword
== RID_AT_END
)
29682 cp_parser_error (parser
, "expected %<}%>");
29684 /* Do not consume the RID_AT_END, so it will be read again as terminating
29685 the @interface of @implementation. */
29686 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29687 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
29689 /* For historical reasons, we accept an optional semicolon. */
29690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29691 cp_lexer_consume_token (parser
->lexer
);
29694 /* Parse an Objective-C protocol declaration. */
29697 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
29699 tree proto
, protorefs
;
29702 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
29705 tok
= cp_lexer_peek_token (parser
->lexer
);
29706 error_at (tok
->location
, "identifier expected after %<@protocol%>");
29707 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29711 /* See if we have a forward declaration or a definition. */
29712 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
29714 /* Try a forward declaration first. */
29715 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
29721 id
= cp_parser_identifier (parser
);
29722 if (id
== error_mark_node
)
29725 objc_declare_protocol (id
, attributes
);
29727 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29728 cp_lexer_consume_token (parser
->lexer
);
29732 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29735 /* Ok, we got a full-fledged definition (or at least should). */
29738 proto
= cp_parser_identifier (parser
);
29739 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
29740 objc_start_protocol (proto
, protorefs
, attributes
);
29741 cp_parser_objc_method_prototype_list (parser
);
29745 /* Parse an Objective-C superclass or category. */
29748 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
29751 tree
*categ
, bool *is_class_extension
)
29753 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
29755 *super
= *categ
= NULL_TREE
;
29756 *is_class_extension
= false;
29757 if (next
->type
== CPP_COLON
)
29759 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
29760 *super
= cp_parser_identifier (parser
);
29762 else if (next
->type
== CPP_OPEN_PAREN
)
29764 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
29766 /* If there is no category name, and this is an @interface, we
29767 have a class extension. */
29768 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
29770 *categ
= NULL_TREE
;
29771 *is_class_extension
= true;
29774 *categ
= cp_parser_identifier (parser
);
29776 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29780 /* Parse an Objective-C class interface. */
29783 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
29785 tree name
, super
, categ
, protos
;
29786 bool is_class_extension
;
29788 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
29789 name
= cp_parser_identifier (parser
);
29790 if (name
== error_mark_node
)
29792 /* It's hard to recover because even if valid @interface stuff
29793 is to follow, we can't compile it (or validate it) if we
29794 don't even know which class it refers to. Let's assume this
29795 was a stray '@interface' token in the stream and skip it.
29799 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
29800 &is_class_extension
);
29801 protos
= cp_parser_objc_protocol_refs_opt (parser
);
29803 /* We have either a class or a category on our hands. */
29804 if (categ
|| is_class_extension
)
29805 objc_start_category_interface (name
, categ
, protos
, attributes
);
29808 objc_start_class_interface (name
, super
, protos
, attributes
);
29809 /* Handle instance variable declarations, if any. */
29810 cp_parser_objc_class_ivars (parser
);
29811 objc_continue_interface ();
29814 cp_parser_objc_method_prototype_list (parser
);
29817 /* Parse an Objective-C class implementation. */
29820 cp_parser_objc_class_implementation (cp_parser
* parser
)
29822 tree name
, super
, categ
;
29823 bool is_class_extension
;
29825 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
29826 name
= cp_parser_identifier (parser
);
29827 if (name
== error_mark_node
)
29829 /* It's hard to recover because even if valid @implementation
29830 stuff is to follow, we can't compile it (or validate it) if
29831 we don't even know which class it refers to. Let's assume
29832 this was a stray '@implementation' token in the stream and
29837 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
29838 &is_class_extension
);
29840 /* We have either a class or a category on our hands. */
29842 objc_start_category_implementation (name
, categ
);
29845 objc_start_class_implementation (name
, super
);
29846 /* Handle instance variable declarations, if any. */
29847 cp_parser_objc_class_ivars (parser
);
29848 objc_continue_implementation ();
29851 cp_parser_objc_method_definition_list (parser
);
29854 /* Consume the @end token and finish off the implementation. */
29857 cp_parser_objc_end_implementation (cp_parser
* parser
)
29859 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29860 objc_finish_implementation ();
29863 /* Parse an Objective-C declaration. */
29866 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
29868 /* Try to figure out what kind of declaration is present. */
29869 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29872 switch (kwd
->keyword
)
29877 error_at (kwd
->location
, "attributes may not be specified before"
29878 " the %<@%D%> Objective-C++ keyword",
29882 case RID_AT_IMPLEMENTATION
:
29883 warning_at (kwd
->location
, OPT_Wattributes
,
29884 "prefix attributes are ignored before %<@%D%>",
29891 switch (kwd
->keyword
)
29894 cp_parser_objc_alias_declaration (parser
);
29897 cp_parser_objc_class_declaration (parser
);
29899 case RID_AT_PROTOCOL
:
29900 cp_parser_objc_protocol_declaration (parser
, attributes
);
29902 case RID_AT_INTERFACE
:
29903 cp_parser_objc_class_interface (parser
, attributes
);
29905 case RID_AT_IMPLEMENTATION
:
29906 cp_parser_objc_class_implementation (parser
);
29909 cp_parser_objc_end_implementation (parser
);
29912 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
29914 cp_parser_skip_to_end_of_block_or_statement (parser
);
29918 /* Parse an Objective-C try-catch-finally statement.
29920 objc-try-catch-finally-stmt:
29921 @try compound-statement objc-catch-clause-seq [opt]
29922 objc-finally-clause [opt]
29924 objc-catch-clause-seq:
29925 objc-catch-clause objc-catch-clause-seq [opt]
29928 @catch ( objc-exception-declaration ) compound-statement
29930 objc-finally-clause:
29931 @finally compound-statement
29933 objc-exception-declaration:
29934 parameter-declaration
29937 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29941 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29942 for C. Keep them in sync. */
29945 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
29947 location_t location
;
29950 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
29951 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29952 objc_maybe_warn_exceptions (location
);
29953 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29954 node, lest it get absorbed into the surrounding block. */
29955 stmt
= push_stmt_list ();
29956 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
29957 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
29959 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
29961 cp_parameter_declarator
*parm
;
29962 tree parameter_declaration
= error_mark_node
;
29963 bool seen_open_paren
= false;
29965 cp_lexer_consume_token (parser
->lexer
);
29966 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29967 seen_open_paren
= true;
29968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29970 /* We have "@catch (...)" (where the '...' are literally
29971 what is in the code). Skip the '...'.
29972 parameter_declaration is set to NULL_TREE, and
29973 objc_being_catch_clauses() knows that that means
29975 cp_lexer_consume_token (parser
->lexer
);
29976 parameter_declaration
= NULL_TREE
;
29980 /* We have "@catch (NSException *exception)" or something
29981 like that. Parse the parameter declaration. */
29982 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
29984 parameter_declaration
= error_mark_node
;
29986 parameter_declaration
= grokdeclarator (parm
->declarator
,
29987 &parm
->decl_specifiers
,
29988 PARM
, /*initialized=*/0,
29989 /*attrlist=*/NULL
);
29991 if (seen_open_paren
)
29992 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29995 /* If there was no open parenthesis, we are recovering from
29996 an error, and we are trying to figure out what mistake
29997 the user has made. */
29999 /* If there is an immediate closing parenthesis, the user
30000 probably forgot the opening one (ie, they typed "@catch
30001 NSException *e)". Parse the closing parenthesis and keep
30003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30004 cp_lexer_consume_token (parser
->lexer
);
30006 /* If these is no immediate closing parenthesis, the user
30007 probably doesn't know that parenthesis are required at
30008 all (ie, they typed "@catch NSException *e"). So, just
30009 forget about the closing parenthesis and keep going. */
30011 objc_begin_catch_clause (parameter_declaration
);
30012 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30013 objc_finish_catch_clause ();
30015 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30017 cp_lexer_consume_token (parser
->lexer
);
30018 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30019 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30020 node, lest it get absorbed into the surrounding block. */
30021 stmt
= push_stmt_list ();
30022 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30023 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30026 return objc_finish_try_stmt ();
30029 /* Parse an Objective-C synchronized statement.
30031 objc-synchronized-stmt:
30032 @synchronized ( expression ) compound-statement
30034 Returns NULL_TREE. */
30037 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30039 location_t location
;
30042 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30044 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30045 objc_maybe_warn_exceptions (location
);
30046 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
30047 lock
= cp_parser_expression (parser
);
30048 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30050 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30051 node, lest it get absorbed into the surrounding block. */
30052 stmt
= push_stmt_list ();
30053 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30055 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30058 /* Parse an Objective-C throw statement.
30061 @throw assignment-expression [opt] ;
30063 Returns a constructed '@throw' statement. */
30066 cp_parser_objc_throw_statement (cp_parser
*parser
)
30068 tree expr
= NULL_TREE
;
30069 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30071 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30073 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30074 expr
= cp_parser_expression (parser
);
30076 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30078 return objc_build_throw_stmt (loc
, expr
);
30081 /* Parse an Objective-C statement. */
30084 cp_parser_objc_statement (cp_parser
* parser
)
30086 /* Try to figure out what kind of declaration is present. */
30087 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30089 switch (kwd
->keyword
)
30092 return cp_parser_objc_try_catch_finally_statement (parser
);
30093 case RID_AT_SYNCHRONIZED
:
30094 return cp_parser_objc_synchronized_statement (parser
);
30096 return cp_parser_objc_throw_statement (parser
);
30098 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30100 cp_parser_skip_to_end_of_block_or_statement (parser
);
30103 return error_mark_node
;
30106 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30107 look ahead to see if an objc keyword follows the attributes. This
30108 is to detect the use of prefix attributes on ObjC @interface and
30112 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30114 cp_lexer_save_tokens (parser
->lexer
);
30115 *attrib
= cp_parser_attributes_opt (parser
);
30116 gcc_assert (*attrib
);
30117 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30119 cp_lexer_commit_tokens (parser
->lexer
);
30122 cp_lexer_rollback_tokens (parser
->lexer
);
30126 /* This routine is a minimal replacement for
30127 c_parser_struct_declaration () used when parsing the list of
30128 types/names or ObjC++ properties. For example, when parsing the
30131 @property (readonly) int a, b, c;
30133 this function is responsible for parsing "int a, int b, int c" and
30134 returning the declarations as CHAIN of DECLs.
30136 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30137 similar parsing. */
30139 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30141 tree decls
= NULL_TREE
;
30142 cp_decl_specifier_seq declspecs
;
30143 int decl_class_or_enum_p
;
30144 tree prefix_attributes
;
30146 cp_parser_decl_specifier_seq (parser
,
30147 CP_PARSER_FLAGS_NONE
,
30149 &decl_class_or_enum_p
);
30151 if (declspecs
.type
== error_mark_node
)
30152 return error_mark_node
;
30154 /* auto, register, static, extern, mutable. */
30155 if (declspecs
.storage_class
!= sc_none
)
30157 cp_parser_error (parser
, "invalid type for property");
30158 declspecs
.storage_class
= sc_none
;
30161 /* thread_local. */
30162 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30164 cp_parser_error (parser
, "invalid type for property");
30165 declspecs
.locations
[ds_thread
] = 0;
30169 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30171 cp_parser_error (parser
, "invalid type for property");
30172 declspecs
.locations
[ds_typedef
] = 0;
30175 prefix_attributes
= declspecs
.attributes
;
30176 declspecs
.attributes
= NULL_TREE
;
30178 /* Keep going until we hit the `;' at the end of the declaration. */
30179 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30181 tree attributes
, first_attribute
, decl
;
30182 cp_declarator
*declarator
;
30185 /* Parse the declarator. */
30186 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30187 NULL
, NULL
, false, false);
30189 /* Look for attributes that apply to the ivar. */
30190 attributes
= cp_parser_attributes_opt (parser
);
30191 /* Remember which attributes are prefix attributes and
30193 first_attribute
= attributes
;
30194 /* Combine the attributes. */
30195 attributes
= chainon (prefix_attributes
, attributes
);
30197 decl
= grokfield (declarator
, &declspecs
,
30198 NULL_TREE
, /*init_const_expr_p=*/false,
30199 NULL_TREE
, attributes
);
30201 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30202 return error_mark_node
;
30204 /* Reset PREFIX_ATTRIBUTES. */
30205 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30206 attributes
= TREE_CHAIN (attributes
);
30208 TREE_CHAIN (attributes
) = NULL_TREE
;
30210 DECL_CHAIN (decl
) = decls
;
30213 token
= cp_lexer_peek_token (parser
->lexer
);
30214 if (token
->type
== CPP_COMMA
)
30216 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30225 /* Parse an Objective-C @property declaration. The syntax is:
30227 objc-property-declaration:
30228 '@property' objc-property-attributes[opt] struct-declaration ;
30230 objc-property-attributes:
30231 '(' objc-property-attribute-list ')'
30233 objc-property-attribute-list:
30234 objc-property-attribute
30235 objc-property-attribute-list, objc-property-attribute
30237 objc-property-attribute
30238 'getter' = identifier
30239 'setter' = identifier
30248 @property NSString *name;
30249 @property (readonly) id object;
30250 @property (retain, nonatomic, getter=getTheName) id name;
30251 @property int a, b, c;
30253 PS: This function is identical to
30254 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30256 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30258 /* The following variables hold the attributes of the properties as
30259 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30260 seen. When we see an attribute, we set them to 'true' (if they
30261 are boolean properties) or to the identifier (if they have an
30262 argument, ie, for getter and setter). Note that here we only
30263 parse the list of attributes, check the syntax and accumulate the
30264 attributes that we find. objc_add_property_declaration() will
30265 then process the information. */
30266 bool property_assign
= false;
30267 bool property_copy
= false;
30268 tree property_getter_ident
= NULL_TREE
;
30269 bool property_nonatomic
= false;
30270 bool property_readonly
= false;
30271 bool property_readwrite
= false;
30272 bool property_retain
= false;
30273 tree property_setter_ident
= NULL_TREE
;
30275 /* 'properties' is the list of properties that we read. Usually a
30276 single one, but maybe more (eg, in "@property int a, b, c;" there
30281 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30283 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30285 /* Parse the optional attribute list... */
30286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30289 cp_lexer_consume_token (parser
->lexer
);
30293 bool syntax_error
= false;
30294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30297 if (token
->type
!= CPP_NAME
)
30299 cp_parser_error (parser
, "expected identifier");
30302 keyword
= C_RID_CODE (token
->u
.value
);
30303 cp_lexer_consume_token (parser
->lexer
);
30306 case RID_ASSIGN
: property_assign
= true; break;
30307 case RID_COPY
: property_copy
= true; break;
30308 case RID_NONATOMIC
: property_nonatomic
= true; break;
30309 case RID_READONLY
: property_readonly
= true; break;
30310 case RID_READWRITE
: property_readwrite
= true; break;
30311 case RID_RETAIN
: property_retain
= true; break;
30315 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30317 if (keyword
== RID_GETTER
)
30318 cp_parser_error (parser
,
30319 "missing %<=%> (after %<getter%> attribute)");
30321 cp_parser_error (parser
,
30322 "missing %<=%> (after %<setter%> attribute)");
30323 syntax_error
= true;
30326 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30327 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30329 cp_parser_error (parser
, "expected identifier");
30330 syntax_error
= true;
30333 if (keyword
== RID_SETTER
)
30335 if (property_setter_ident
!= NULL_TREE
)
30337 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30338 cp_lexer_consume_token (parser
->lexer
);
30341 property_setter_ident
= cp_parser_objc_selector (parser
);
30342 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30343 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30345 cp_lexer_consume_token (parser
->lexer
);
30349 if (property_getter_ident
!= NULL_TREE
)
30351 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30352 cp_lexer_consume_token (parser
->lexer
);
30355 property_getter_ident
= cp_parser_objc_selector (parser
);
30359 cp_parser_error (parser
, "unknown property attribute");
30360 syntax_error
= true;
30367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30368 cp_lexer_consume_token (parser
->lexer
);
30373 /* FIXME: "@property (setter, assign);" will generate a spurious
30374 "error: expected ‘)’ before ‘,’ token". This is because
30375 cp_parser_require, unlike the C counterpart, will produce an
30376 error even if we are in error recovery. */
30377 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30379 cp_parser_skip_to_closing_parenthesis (parser
,
30380 /*recovering=*/true,
30381 /*or_comma=*/false,
30382 /*consume_paren=*/true);
30386 /* ... and the property declaration(s). */
30387 properties
= cp_parser_objc_struct_declaration (parser
);
30389 if (properties
== error_mark_node
)
30391 cp_parser_skip_to_end_of_statement (parser
);
30392 /* If the next token is now a `;', consume it. */
30393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30394 cp_lexer_consume_token (parser
->lexer
);
30398 if (properties
== NULL_TREE
)
30399 cp_parser_error (parser
, "expected identifier");
30402 /* Comma-separated properties are chained together in
30403 reverse order; add them one by one. */
30404 properties
= nreverse (properties
);
30406 for (; properties
; properties
= TREE_CHAIN (properties
))
30407 objc_add_property_declaration (loc
, copy_node (properties
),
30408 property_readonly
, property_readwrite
,
30409 property_assign
, property_retain
,
30410 property_copy
, property_nonatomic
,
30411 property_getter_ident
, property_setter_ident
);
30414 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30417 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30419 objc-synthesize-declaration:
30420 @synthesize objc-synthesize-identifier-list ;
30422 objc-synthesize-identifier-list:
30423 objc-synthesize-identifier
30424 objc-synthesize-identifier-list, objc-synthesize-identifier
30426 objc-synthesize-identifier
30428 identifier = identifier
30431 @synthesize MyProperty;
30432 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30434 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30435 for C. Keep them in sync.
30438 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30440 tree list
= NULL_TREE
;
30442 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30444 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30447 tree property
, ivar
;
30448 property
= cp_parser_identifier (parser
);
30449 if (property
== error_mark_node
)
30451 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30456 cp_lexer_consume_token (parser
->lexer
);
30457 ivar
= cp_parser_identifier (parser
);
30458 if (ivar
== error_mark_node
)
30460 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30466 list
= chainon (list
, build_tree_list (ivar
, property
));
30467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30468 cp_lexer_consume_token (parser
->lexer
);
30472 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30473 objc_add_synthesize_declaration (loc
, list
);
30476 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30478 objc-dynamic-declaration:
30479 @dynamic identifier-list ;
30482 @dynamic MyProperty;
30483 @dynamic MyProperty, AnotherProperty;
30485 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30486 for C. Keep them in sync.
30489 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30491 tree list
= NULL_TREE
;
30493 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30495 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30499 property
= cp_parser_identifier (parser
);
30500 if (property
== error_mark_node
)
30502 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30505 list
= chainon (list
, build_tree_list (NULL
, property
));
30506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30507 cp_lexer_consume_token (parser
->lexer
);
30511 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30512 objc_add_dynamic_declaration (loc
, list
);
30516 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30518 /* Returns name of the next clause.
30519 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30520 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30521 returned and the token is consumed. */
30523 static pragma_omp_clause
30524 cp_parser_omp_clause_name (cp_parser
*parser
)
30526 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
30528 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30529 result
= PRAGMA_OACC_CLAUSE_AUTO
;
30530 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
30531 result
= PRAGMA_OMP_CLAUSE_IF
;
30532 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
30533 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
30534 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
30535 result
= PRAGMA_OACC_CLAUSE_DELETE
;
30536 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
30537 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
30538 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30539 result
= PRAGMA_OMP_CLAUSE_FOR
;
30540 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30542 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30543 const char *p
= IDENTIFIER_POINTER (id
);
30548 if (!strcmp ("aligned", p
))
30549 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
30550 else if (!strcmp ("async", p
))
30551 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
30554 if (!strcmp ("collapse", p
))
30555 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
30556 else if (!strcmp ("copy", p
))
30557 result
= PRAGMA_OACC_CLAUSE_COPY
;
30558 else if (!strcmp ("copyin", p
))
30559 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
30560 else if (!strcmp ("copyout", p
))
30561 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
30562 else if (!strcmp ("copyprivate", p
))
30563 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
30564 else if (!strcmp ("create", p
))
30565 result
= PRAGMA_OACC_CLAUSE_CREATE
;
30568 if (!strcmp ("defaultmap", p
))
30569 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
30570 else if (!strcmp ("depend", p
))
30571 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
30572 else if (!strcmp ("device", p
))
30573 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
30574 else if (!strcmp ("deviceptr", p
))
30575 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
30576 else if (!strcmp ("device_resident", p
))
30577 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
30578 else if (!strcmp ("dist_schedule", p
))
30579 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
30582 if (!strcmp ("final", p
))
30583 result
= PRAGMA_OMP_CLAUSE_FINAL
;
30584 else if (!strcmp ("firstprivate", p
))
30585 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
30586 else if (!strcmp ("from", p
))
30587 result
= PRAGMA_OMP_CLAUSE_FROM
;
30590 if (!strcmp ("gang", p
))
30591 result
= PRAGMA_OACC_CLAUSE_GANG
;
30592 else if (!strcmp ("grainsize", p
))
30593 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
30596 if (!strcmp ("hint", p
))
30597 result
= PRAGMA_OMP_CLAUSE_HINT
;
30598 else if (!strcmp ("host", p
))
30599 result
= PRAGMA_OACC_CLAUSE_HOST
;
30602 if (!strcmp ("inbranch", p
))
30603 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
30604 else if (!strcmp ("independent", p
))
30605 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
30606 else if (!strcmp ("is_device_ptr", p
))
30607 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
30610 if (!strcmp ("lastprivate", p
))
30611 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
30612 else if (!strcmp ("linear", p
))
30613 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
30614 else if (!strcmp ("link", p
))
30615 result
= PRAGMA_OMP_CLAUSE_LINK
;
30618 if (!strcmp ("map", p
))
30619 result
= PRAGMA_OMP_CLAUSE_MAP
;
30620 else if (!strcmp ("mergeable", p
))
30621 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
30622 else if (flag_cilkplus
&& !strcmp ("mask", p
))
30623 result
= PRAGMA_CILK_CLAUSE_MASK
;
30626 if (!strcmp ("nogroup", p
))
30627 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
30628 else if (!strcmp ("notinbranch", p
))
30629 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
30630 else if (!strcmp ("nowait", p
))
30631 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
30632 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
30633 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
30634 else if (!strcmp ("num_gangs", p
))
30635 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
30636 else if (!strcmp ("num_tasks", p
))
30637 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
30638 else if (!strcmp ("num_teams", p
))
30639 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
30640 else if (!strcmp ("num_threads", p
))
30641 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
30642 else if (!strcmp ("num_workers", p
))
30643 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
30646 if (!strcmp ("ordered", p
))
30647 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
30650 if (!strcmp ("parallel", p
))
30651 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
30652 else if (!strcmp ("present", p
))
30653 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
30654 else if (!strcmp ("present_or_copy", p
)
30655 || !strcmp ("pcopy", p
))
30656 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
30657 else if (!strcmp ("present_or_copyin", p
)
30658 || !strcmp ("pcopyin", p
))
30659 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
30660 else if (!strcmp ("present_or_copyout", p
)
30661 || !strcmp ("pcopyout", p
))
30662 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
30663 else if (!strcmp ("present_or_create", p
)
30664 || !strcmp ("pcreate", p
))
30665 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
30666 else if (!strcmp ("priority", p
))
30667 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
30668 else if (!strcmp ("proc_bind", p
))
30669 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
30672 if (!strcmp ("reduction", p
))
30673 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
30676 if (!strcmp ("safelen", p
))
30677 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
30678 else if (!strcmp ("schedule", p
))
30679 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
30680 else if (!strcmp ("sections", p
))
30681 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
30682 else if (!strcmp ("self", p
))
30683 result
= PRAGMA_OACC_CLAUSE_SELF
;
30684 else if (!strcmp ("seq", p
))
30685 result
= PRAGMA_OACC_CLAUSE_SEQ
;
30686 else if (!strcmp ("shared", p
))
30687 result
= PRAGMA_OMP_CLAUSE_SHARED
;
30688 else if (!strcmp ("simd", p
))
30689 result
= PRAGMA_OMP_CLAUSE_SIMD
;
30690 else if (!strcmp ("simdlen", p
))
30691 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
30694 if (!strcmp ("taskgroup", p
))
30695 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
30696 else if (!strcmp ("thread_limit", p
))
30697 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
30698 else if (!strcmp ("threads", p
))
30699 result
= PRAGMA_OMP_CLAUSE_THREADS
;
30700 else if (!strcmp ("tile", p
))
30701 result
= PRAGMA_OACC_CLAUSE_TILE
;
30702 else if (!strcmp ("to", p
))
30703 result
= PRAGMA_OMP_CLAUSE_TO
;
30706 if (!strcmp ("uniform", p
))
30707 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
30708 else if (!strcmp ("untied", p
))
30709 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
30710 else if (!strcmp ("use_device", p
))
30711 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
30712 else if (!strcmp ("use_device_ptr", p
))
30713 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
30716 if (!strcmp ("vector", p
))
30717 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
30718 else if (!strcmp ("vector_length", p
))
30719 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
30720 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
30721 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
30724 if (!strcmp ("wait", p
))
30725 result
= PRAGMA_OACC_CLAUSE_WAIT
;
30726 else if (!strcmp ("worker", p
))
30727 result
= PRAGMA_OACC_CLAUSE_WORKER
;
30732 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
30733 cp_lexer_consume_token (parser
->lexer
);
30738 /* Validate that a clause of the given type does not already exist. */
30741 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
30742 const char *name
, location_t location
)
30746 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30747 if (OMP_CLAUSE_CODE (c
) == code
)
30749 error_at (location
, "too many %qs clauses", name
);
30757 variable-list , identifier
30759 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30760 colon). An opening parenthesis will have been consumed by the caller.
30762 If KIND is nonzero, create the appropriate node and install the decl
30763 in OMP_CLAUSE_DECL and add the node to the head of the list.
30765 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30766 return the list created.
30768 COLON can be NULL if only closing parenthesis should end the list,
30769 or pointer to bool which will receive false if the list is terminated
30770 by closing parenthesis or true if the list is terminated by colon. */
30773 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
30774 tree list
, bool *colon
)
30777 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30780 parser
->colon_corrects_to_scope_p
= false;
30787 token
= cp_lexer_peek_token (parser
->lexer
);
30789 && current_class_ptr
30790 && cp_parser_is_keyword (token
, RID_THIS
))
30792 decl
= finish_this_expr ();
30793 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
30794 || CONVERT_EXPR_P (decl
))
30795 decl
= TREE_OPERAND (decl
, 0);
30796 cp_lexer_consume_token (parser
->lexer
);
30800 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30801 /*check_dependency_p=*/true,
30802 /*template_p=*/NULL
,
30803 /*declarator_p=*/false,
30804 /*optional_p=*/false);
30805 if (name
== error_mark_node
)
30808 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
30809 if (decl
== error_mark_node
)
30810 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
30813 if (decl
== error_mark_node
)
30815 else if (kind
!= 0)
30819 case OMP_CLAUSE__CACHE_
:
30820 /* The OpenACC cache directive explicitly only allows "array
30821 elements or subarrays". */
30822 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
30824 error_at (token
->location
, "expected %<[%>");
30825 decl
= error_mark_node
;
30829 case OMP_CLAUSE_MAP
:
30830 case OMP_CLAUSE_FROM
:
30831 case OMP_CLAUSE_TO
:
30832 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
30835 = cp_lexer_peek_token (parser
->lexer
)->location
;
30836 cp_id_kind idk
= CP_ID_KIND_NONE
;
30837 cp_lexer_consume_token (parser
->lexer
);
30838 decl
= convert_from_reference (decl
);
30840 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
30845 case OMP_CLAUSE_DEPEND
:
30846 case OMP_CLAUSE_REDUCTION
:
30847 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
30849 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
30851 parser
->colon_corrects_to_scope_p
= false;
30852 cp_lexer_consume_token (parser
->lexer
);
30853 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30854 low_bound
= cp_parser_expression (parser
);
30856 parser
->colon_corrects_to_scope_p
30857 = saved_colon_corrects_to_scope_p
;
30858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
30859 length
= integer_one_node
;
30862 /* Look for `:'. */
30863 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30865 if (!cp_lexer_next_token_is (parser
->lexer
,
30867 length
= cp_parser_expression (parser
);
30869 /* Look for the closing `]'. */
30870 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
30874 decl
= tree_cons (low_bound
, length
, decl
);
30881 tree u
= build_omp_clause (token
->location
, kind
);
30882 OMP_CLAUSE_DECL (u
) = decl
;
30883 OMP_CLAUSE_CHAIN (u
) = list
;
30887 list
= tree_cons (decl
, NULL_TREE
, list
);
30890 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
30892 cp_lexer_consume_token (parser
->lexer
);
30896 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30898 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30901 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30905 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30909 /* Try to resync to an unnested comma. Copied from
30910 cp_parser_parenthesized_expression_list. */
30913 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30914 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
30915 /*recovering=*/true,
30917 /*consume_paren=*/true);
30925 /* Similarly, but expect leading and trailing parenthesis. This is a very
30926 common case for omp clauses. */
30929 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
30931 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30932 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
30937 copy ( variable-list )
30938 copyin ( variable-list )
30939 copyout ( variable-list )
30940 create ( variable-list )
30941 delete ( variable-list )
30942 present ( variable-list )
30943 present_or_copy ( variable-list )
30944 pcopy ( variable-list )
30945 present_or_copyin ( variable-list )
30946 pcopyin ( variable-list )
30947 present_or_copyout ( variable-list )
30948 pcopyout ( variable-list )
30949 present_or_create ( variable-list )
30950 pcreate ( variable-list ) */
30953 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
30956 enum gomp_map_kind kind
;
30959 case PRAGMA_OACC_CLAUSE_COPY
:
30960 kind
= GOMP_MAP_FORCE_TOFROM
;
30962 case PRAGMA_OACC_CLAUSE_COPYIN
:
30963 kind
= GOMP_MAP_FORCE_TO
;
30965 case PRAGMA_OACC_CLAUSE_COPYOUT
:
30966 kind
= GOMP_MAP_FORCE_FROM
;
30968 case PRAGMA_OACC_CLAUSE_CREATE
:
30969 kind
= GOMP_MAP_FORCE_ALLOC
;
30971 case PRAGMA_OACC_CLAUSE_DELETE
:
30972 kind
= GOMP_MAP_DELETE
;
30974 case PRAGMA_OACC_CLAUSE_DEVICE
:
30975 kind
= GOMP_MAP_FORCE_TO
;
30977 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
30978 kind
= GOMP_MAP_DEVICE_RESIDENT
;
30980 case PRAGMA_OACC_CLAUSE_HOST
:
30981 case PRAGMA_OACC_CLAUSE_SELF
:
30982 kind
= GOMP_MAP_FORCE_FROM
;
30984 case PRAGMA_OACC_CLAUSE_LINK
:
30985 kind
= GOMP_MAP_LINK
;
30987 case PRAGMA_OACC_CLAUSE_PRESENT
:
30988 kind
= GOMP_MAP_FORCE_PRESENT
;
30990 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
30991 kind
= GOMP_MAP_TOFROM
;
30993 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
30994 kind
= GOMP_MAP_TO
;
30996 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
30997 kind
= GOMP_MAP_FROM
;
30999 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31000 kind
= GOMP_MAP_ALLOC
;
31003 gcc_unreachable ();
31006 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31008 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31009 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31015 deviceptr ( variable-list ) */
31018 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31020 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31023 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31024 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31025 variable-list must only allow for pointer variables. */
31026 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31027 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31029 tree v
= TREE_PURPOSE (t
);
31030 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31031 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31032 OMP_CLAUSE_DECL (u
) = v
;
31033 OMP_CLAUSE_CHAIN (u
) = list
;
31047 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31048 enum omp_clause_code code
,
31049 tree list
, location_t location
)
31051 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31052 tree c
= build_omp_clause (location
, code
);
31053 OMP_CLAUSE_CHAIN (c
) = list
;
31058 num_gangs ( expression )
31059 num_workers ( expression )
31060 vector_length ( expression ) */
31063 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31064 const char *str
, tree list
)
31066 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31068 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31071 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31073 if (t
== error_mark_node
31074 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31076 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31077 /*or_comma=*/false,
31078 /*consume_paren=*/true);
31082 check_no_duplicate_clause (list
, code
, str
, loc
);
31084 tree c
= build_omp_clause (loc
, code
);
31085 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31086 OMP_CLAUSE_CHAIN (c
) = list
;
31092 gang [( gang-arg-list )]
31093 worker [( [num:] int-expr )]
31094 vector [( [length:] int-expr )]
31096 where gang-arg is one of:
31101 and size-expr may be:
31108 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31109 const char *str
, tree list
)
31111 const char *id
= "num";
31112 cp_lexer
*lexer
= parser
->lexer
;
31113 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31114 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31116 if (kind
== OMP_CLAUSE_VECTOR
)
31119 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31121 cp_lexer_consume_token (lexer
);
31125 cp_token
*next
= cp_lexer_peek_token (lexer
);
31128 /* Gang static argument. */
31129 if (kind
== OMP_CLAUSE_GANG
31130 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31132 cp_lexer_consume_token (lexer
);
31134 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31135 goto cleanup_error
;
31138 if (ops
[idx
] != NULL
)
31140 cp_parser_error (parser
, "too many %<static%> arguments");
31141 goto cleanup_error
;
31144 /* Check for the '*' argument. */
31145 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31146 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31147 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31150 cp_lexer_consume_token (lexer
);
31151 ops
[idx
] = integer_minus_one_node
;
31153 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31155 cp_lexer_consume_token (lexer
);
31161 /* Worker num: argument and vector length: arguments. */
31162 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31163 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
31164 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31166 cp_lexer_consume_token (lexer
); /* id */
31167 cp_lexer_consume_token (lexer
); /* ':' */
31170 /* Now collect the actual argument. */
31171 if (ops
[idx
] != NULL_TREE
)
31173 cp_parser_error (parser
, "unexpected argument");
31174 goto cleanup_error
;
31177 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31179 if (expr
== error_mark_node
)
31180 goto cleanup_error
;
31182 mark_exp_read (expr
);
31185 if (kind
== OMP_CLAUSE_GANG
31186 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31188 cp_lexer_consume_token (lexer
);
31195 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31196 goto cleanup_error
;
31199 check_no_duplicate_clause (list
, kind
, str
, loc
);
31201 c
= build_omp_clause (loc
, kind
);
31204 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31206 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31207 OMP_CLAUSE_CHAIN (c
) = list
;
31212 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31217 tile ( size-expr-list ) */
31220 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31222 tree c
, expr
= error_mark_node
;
31223 tree tile
= NULL_TREE
;
31225 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31227 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31233 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31234 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31236 cp_lexer_consume_token (parser
->lexer
);
31237 expr
= integer_minus_one_node
;
31240 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31242 if (expr
== error_mark_node
)
31245 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31248 cp_lexer_consume_token (parser
->lexer
);
31250 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31252 /* Consume the trailing ')'. */
31253 cp_lexer_consume_token (parser
->lexer
);
31255 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31256 tile
= nreverse (tile
);
31257 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31258 OMP_CLAUSE_CHAIN (c
) = list
;
31263 Parse wait clause or directive parameters. */
31266 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31268 vec
<tree
, va_gc
> *args
;
31271 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31273 /*allow_expansion_p=*/true,
31274 /*non_constant_p=*/NULL
);
31276 if (args
== NULL
|| args
->length () == 0)
31278 cp_parser_error (parser
, "expected integer expression before ')'");
31280 release_tree_vector (args
);
31284 args_tree
= build_tree_list_vec (args
);
31286 release_tree_vector (args
);
31288 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31290 tree targ
= TREE_VALUE (t
);
31292 if (targ
!= error_mark_node
)
31294 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31295 error ("%<wait%> expression must be integral");
31298 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31300 mark_rvalue_use (targ
);
31301 OMP_CLAUSE_DECL (c
) = targ
;
31302 OMP_CLAUSE_CHAIN (c
) = list
;
31312 wait ( int-expr-list ) */
31315 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31317 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31319 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31322 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31328 collapse ( constant-expression ) */
31331 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31337 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31338 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31341 num
= cp_parser_constant_expression (parser
);
31343 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31344 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31345 /*or_comma=*/false,
31346 /*consume_paren=*/true);
31348 if (num
== error_mark_node
)
31350 num
= fold_non_dependent_expr (num
);
31351 if (!tree_fits_shwi_p (num
)
31352 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31353 || (n
= tree_to_shwi (num
)) <= 0
31356 error_at (loc
, "collapse argument needs positive constant integer expression");
31360 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31361 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31362 OMP_CLAUSE_CHAIN (c
) = list
;
31363 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31369 default ( shared | none )
31375 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31376 location_t location
, bool is_oacc
)
31378 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31381 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31385 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31386 const char *p
= IDENTIFIER_POINTER (id
);
31391 if (strcmp ("none", p
) != 0)
31393 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31397 if (strcmp ("shared", p
) != 0 || is_oacc
)
31399 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31406 cp_lexer_consume_token (parser
->lexer
);
31412 cp_parser_error (parser
, "expected %<none%>");
31414 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31417 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31418 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31419 /*or_comma=*/false,
31420 /*consume_paren=*/true);
31422 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31425 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31426 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31427 OMP_CLAUSE_CHAIN (c
) = list
;
31428 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31434 final ( expression ) */
31437 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31441 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31444 t
= cp_parser_condition (parser
);
31446 if (t
== error_mark_node
31447 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31448 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31449 /*or_comma=*/false,
31450 /*consume_paren=*/true);
31452 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31454 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31455 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31456 OMP_CLAUSE_CHAIN (c
) = list
;
31465 if ( directive-name-modifier : expression )
31467 directive-name-modifier:
31468 parallel | task | taskloop | target data | target | target update
31469 | target enter data | target exit data */
31472 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31476 enum tree_code if_modifier
= ERROR_MARK
;
31478 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31481 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31483 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31484 const char *p
= IDENTIFIER_POINTER (id
);
31487 if (strcmp ("parallel", p
) == 0)
31488 if_modifier
= OMP_PARALLEL
;
31489 else if (strcmp ("task", p
) == 0)
31490 if_modifier
= OMP_TASK
;
31491 else if (strcmp ("taskloop", p
) == 0)
31492 if_modifier
= OMP_TASKLOOP
;
31493 else if (strcmp ("target", p
) == 0)
31495 if_modifier
= OMP_TARGET
;
31496 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31498 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31499 p
= IDENTIFIER_POINTER (id
);
31500 if (strcmp ("data", p
) == 0)
31501 if_modifier
= OMP_TARGET_DATA
;
31502 else if (strcmp ("update", p
) == 0)
31503 if_modifier
= OMP_TARGET_UPDATE
;
31504 else if (strcmp ("enter", p
) == 0)
31505 if_modifier
= OMP_TARGET_ENTER_DATA
;
31506 else if (strcmp ("exit", p
) == 0)
31507 if_modifier
= OMP_TARGET_EXIT_DATA
;
31508 if (if_modifier
!= OMP_TARGET
)
31513 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
31514 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
31516 if_modifier
= ERROR_MARK
;
31518 if (if_modifier
== OMP_TARGET_ENTER_DATA
31519 || if_modifier
== OMP_TARGET_EXIT_DATA
)
31521 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
31523 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
31524 p
= IDENTIFIER_POINTER (id
);
31525 if (strcmp ("data", p
) == 0)
31531 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
31532 error_at (loc
, "expected %<data%>");
31533 if_modifier
= ERROR_MARK
;
31538 if (if_modifier
!= ERROR_MARK
)
31540 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
31543 cp_lexer_consume_token (parser
->lexer
);
31550 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
31551 error_at (loc
, "expected %<:%>");
31553 if_modifier
= ERROR_MARK
;
31558 t
= cp_parser_condition (parser
);
31560 if (t
== error_mark_node
31561 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31562 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31563 /*or_comma=*/false,
31564 /*consume_paren=*/true);
31566 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31567 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
31569 if (if_modifier
!= ERROR_MARK
31570 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31572 const char *p
= NULL
;
31573 switch (if_modifier
)
31575 case OMP_PARALLEL
: p
= "parallel"; break;
31576 case OMP_TASK
: p
= "task"; break;
31577 case OMP_TASKLOOP
: p
= "taskloop"; break;
31578 case OMP_TARGET_DATA
: p
= "target data"; break;
31579 case OMP_TARGET
: p
= "target"; break;
31580 case OMP_TARGET_UPDATE
: p
= "target update"; break;
31581 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
31582 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
31583 default: gcc_unreachable ();
31585 error_at (location
, "too many %<if%> clauses with %qs modifier",
31589 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
31592 error_at (location
, "too many %<if%> clauses");
31594 error_at (location
, "too many %<if%> clauses without modifier");
31597 else if (if_modifier
== ERROR_MARK
31598 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
31600 error_at (location
, "if any %<if%> clause has modifier, then all "
31601 "%<if%> clauses have to use modifier");
31606 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
31607 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
31608 OMP_CLAUSE_IF_EXPR (c
) = t
;
31609 OMP_CLAUSE_CHAIN (c
) = list
;
31618 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
31619 tree list
, location_t location
)
31623 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
31626 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
31627 OMP_CLAUSE_CHAIN (c
) = list
;
31635 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
31636 tree list
, location_t location
)
31640 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
31642 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
31643 OMP_CLAUSE_CHAIN (c
) = list
;
31648 num_threads ( expression ) */
31651 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
31652 location_t location
)
31656 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31659 t
= cp_parser_expression (parser
);
31661 if (t
== error_mark_node
31662 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31663 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31664 /*or_comma=*/false,
31665 /*consume_paren=*/true);
31667 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
31668 "num_threads", location
);
31670 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
31671 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
31672 OMP_CLAUSE_CHAIN (c
) = list
;
31678 num_tasks ( expression ) */
31681 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
31682 location_t location
)
31686 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31689 t
= cp_parser_expression (parser
);
31691 if (t
== error_mark_node
31692 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31693 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31694 /*or_comma=*/false,
31695 /*consume_paren=*/true);
31697 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
31698 "num_tasks", location
);
31700 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
31701 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
31702 OMP_CLAUSE_CHAIN (c
) = list
;
31708 grainsize ( expression ) */
31711 cp_parser_omp_clause_grainsize (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_GRAINSIZE
,
31728 "grainsize", location
);
31730 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
31731 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
31732 OMP_CLAUSE_CHAIN (c
) = list
;
31738 priority ( expression ) */
31741 cp_parser_omp_clause_priority (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_PRIORITY
,
31758 "priority", location
);
31760 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
31761 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
31762 OMP_CLAUSE_CHAIN (c
) = list
;
31768 hint ( expression ) */
31771 cp_parser_omp_clause_hint (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_HINT
, "hint", location
);
31789 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
31790 OMP_CLAUSE_HINT_EXPR (c
) = t
;
31791 OMP_CLAUSE_CHAIN (c
) = list
;
31797 defaultmap ( tofrom : scalar ) */
31800 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
31801 location_t location
)
31806 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31809 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31811 cp_parser_error (parser
, "expected %<tofrom%>");
31814 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31815 p
= IDENTIFIER_POINTER (id
);
31816 if (strcmp (p
, "tofrom") != 0)
31818 cp_parser_error (parser
, "expected %<tofrom%>");
31821 cp_lexer_consume_token (parser
->lexer
);
31822 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31825 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31827 cp_parser_error (parser
, "expected %<scalar%>");
31830 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31831 p
= IDENTIFIER_POINTER (id
);
31832 if (strcmp (p
, "scalar") != 0)
31834 cp_parser_error (parser
, "expected %<scalar%>");
31837 cp_lexer_consume_token (parser
->lexer
);
31838 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31841 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
31844 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
31845 OMP_CLAUSE_CHAIN (c
) = list
;
31849 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31850 /*or_comma=*/false,
31851 /*consume_paren=*/true);
31859 ordered ( constant-expression ) */
31862 cp_parser_omp_clause_ordered (cp_parser
*parser
,
31863 tree list
, location_t location
)
31865 tree c
, num
= NULL_TREE
;
31868 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
31869 "ordered", location
);
31871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31873 cp_lexer_consume_token (parser
->lexer
);
31875 num
= cp_parser_constant_expression (parser
);
31877 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31878 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31879 /*or_comma=*/false,
31880 /*consume_paren=*/true);
31882 if (num
== error_mark_node
)
31884 num
= fold_non_dependent_expr (num
);
31885 if (!tree_fits_shwi_p (num
)
31886 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31887 || (n
= tree_to_shwi (num
)) <= 0
31890 error_at (location
,
31891 "ordered argument needs positive constant integer "
31897 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
31898 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
31899 OMP_CLAUSE_CHAIN (c
) = list
;
31904 reduction ( reduction-operator : variable-list )
31906 reduction-operator:
31907 One of: + * - & ^ | && ||
31911 reduction-operator:
31912 One of: + * - & ^ | && || min max
31916 reduction-operator:
31917 One of: + * - & ^ | && ||
31921 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
31923 enum tree_code code
= ERROR_MARK
;
31924 tree nlist
, c
, id
= NULL_TREE
;
31926 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31929 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31931 case CPP_PLUS
: code
= PLUS_EXPR
; break;
31932 case CPP_MULT
: code
= MULT_EXPR
; break;
31933 case CPP_MINUS
: code
= MINUS_EXPR
; break;
31934 case CPP_AND
: code
= BIT_AND_EXPR
; break;
31935 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
31936 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
31937 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
31938 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
31942 if (code
!= ERROR_MARK
)
31943 cp_lexer_consume_token (parser
->lexer
);
31946 bool saved_colon_corrects_to_scope_p
;
31947 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31948 parser
->colon_corrects_to_scope_p
= false;
31949 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
31950 /*check_dependency_p=*/true,
31951 /*template_p=*/NULL
,
31952 /*declarator_p=*/false,
31953 /*optional_p=*/false);
31954 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31955 if (identifier_p (id
))
31957 const char *p
= IDENTIFIER_POINTER (id
);
31959 if (strcmp (p
, "min") == 0)
31961 else if (strcmp (p
, "max") == 0)
31963 else if (id
== ansi_opname (PLUS_EXPR
))
31965 else if (id
== ansi_opname (MULT_EXPR
))
31967 else if (id
== ansi_opname (MINUS_EXPR
))
31969 else if (id
== ansi_opname (BIT_AND_EXPR
))
31970 code
= BIT_AND_EXPR
;
31971 else if (id
== ansi_opname (BIT_IOR_EXPR
))
31972 code
= BIT_IOR_EXPR
;
31973 else if (id
== ansi_opname (BIT_XOR_EXPR
))
31974 code
= BIT_XOR_EXPR
;
31975 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
31976 code
= TRUTH_ANDIF_EXPR
;
31977 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
31978 code
= TRUTH_ORIF_EXPR
;
31979 id
= omp_reduction_id (code
, id
, NULL_TREE
);
31980 tree scope
= parser
->scope
;
31982 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
31983 parser
->scope
= NULL_TREE
;
31984 parser
->qualifying_scope
= NULL_TREE
;
31985 parser
->object_scope
= NULL_TREE
;
31989 error ("invalid reduction-identifier");
31991 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31992 /*or_comma=*/false,
31993 /*consume_paren=*/true);
31998 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32001 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32003 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32005 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32006 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32013 schedule ( schedule-kind )
32014 schedule ( schedule-kind , expression )
32017 static | dynamic | guided | runtime | auto
32020 schedule ( schedule-modifier : schedule-kind )
32021 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32029 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32032 int modifiers
= 0, nmodifiers
= 0;
32034 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32037 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32039 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32041 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32042 const char *p
= IDENTIFIER_POINTER (id
);
32043 if (strcmp ("simd", p
) == 0)
32044 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32045 else if (strcmp ("monotonic", p
) == 0)
32046 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32047 else if (strcmp ("nonmonotonic", p
) == 0)
32048 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32051 cp_lexer_consume_token (parser
->lexer
);
32052 if (nmodifiers
++ == 0
32053 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32054 cp_lexer_consume_token (parser
->lexer
);
32057 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32064 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32065 const char *p
= IDENTIFIER_POINTER (id
);
32070 if (strcmp ("dynamic", p
) != 0)
32072 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32076 if (strcmp ("guided", p
) != 0)
32078 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32082 if (strcmp ("runtime", p
) != 0)
32084 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32091 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32092 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32093 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32094 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32097 cp_lexer_consume_token (parser
->lexer
);
32099 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32100 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32101 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32102 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32104 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32112 cp_lexer_consume_token (parser
->lexer
);
32114 token
= cp_lexer_peek_token (parser
->lexer
);
32115 t
= cp_parser_assignment_expression (parser
);
32117 if (t
== error_mark_node
)
32119 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32120 error_at (token
->location
, "schedule %<runtime%> does not take "
32121 "a %<chunk_size%> parameter");
32122 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32123 error_at (token
->location
, "schedule %<auto%> does not take "
32124 "a %<chunk_size%> parameter");
32126 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32128 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32131 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32134 OMP_CLAUSE_SCHEDULE_KIND (c
)
32135 = (enum omp_clause_schedule_kind
)
32136 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32138 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32139 OMP_CLAUSE_CHAIN (c
) = list
;
32143 cp_parser_error (parser
, "invalid schedule kind");
32145 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32146 /*or_comma=*/false,
32147 /*consume_paren=*/true);
32155 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32156 tree list
, location_t location
)
32160 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32162 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32163 OMP_CLAUSE_CHAIN (c
) = list
;
32172 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32173 tree list
, location_t location
)
32175 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32176 tree c
= build_omp_clause (location
, code
);
32177 OMP_CLAUSE_CHAIN (c
) = list
;
32188 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32189 enum omp_clause_code code
,
32190 tree list
, location_t location
)
32192 tree c
= build_omp_clause (location
, code
);
32193 OMP_CLAUSE_CHAIN (c
) = list
;
32201 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32202 tree list
, location_t location
)
32204 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32205 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32206 OMP_CLAUSE_CHAIN (c
) = list
;
32215 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32216 enum omp_clause_code code
,
32217 tree list
, location_t location
)
32219 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32220 tree c
= build_omp_clause (location
, code
);
32221 OMP_CLAUSE_CHAIN (c
) = list
;
32226 num_teams ( expression ) */
32229 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32230 location_t location
)
32234 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32237 t
= cp_parser_expression (parser
);
32239 if (t
== error_mark_node
32240 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32241 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32242 /*or_comma=*/false,
32243 /*consume_paren=*/true);
32245 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32246 "num_teams", location
);
32248 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32249 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32250 OMP_CLAUSE_CHAIN (c
) = list
;
32256 thread_limit ( expression ) */
32259 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32260 location_t location
)
32264 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32267 t
= cp_parser_expression (parser
);
32269 if (t
== error_mark_node
32270 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32271 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32276 "thread_limit", location
);
32278 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32279 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32280 OMP_CLAUSE_CHAIN (c
) = list
;
32286 aligned ( variable-list )
32287 aligned ( variable-list : constant-expression ) */
32290 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32292 tree nlist
, c
, alignment
= NULL_TREE
;
32295 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32298 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32303 alignment
= cp_parser_constant_expression (parser
);
32305 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32306 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32307 /*or_comma=*/false,
32308 /*consume_paren=*/true);
32310 if (alignment
== error_mark_node
)
32311 alignment
= NULL_TREE
;
32314 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32315 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32321 linear ( variable-list )
32322 linear ( variable-list : expression )
32325 linear ( modifier ( variable-list ) )
32326 linear ( modifier ( variable-list ) : expression ) */
32329 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32330 bool is_cilk_simd_fn
, bool declare_simd
)
32332 tree nlist
, c
, step
= integer_one_node
;
32334 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32336 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32339 if (!is_cilk_simd_fn
32340 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32342 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32343 const char *p
= IDENTIFIER_POINTER (id
);
32345 if (strcmp ("ref", p
) == 0)
32346 kind
= OMP_CLAUSE_LINEAR_REF
;
32347 else if (strcmp ("val", p
) == 0)
32348 kind
= OMP_CLAUSE_LINEAR_VAL
;
32349 else if (strcmp ("uval", p
) == 0)
32350 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32351 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32352 cp_lexer_consume_token (parser
->lexer
);
32354 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32357 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32358 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32362 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32363 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32365 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32366 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32367 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32368 /*or_comma=*/false,
32369 /*consume_paren=*/true);
32376 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32377 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32380 cp_parser_parse_tentatively (parser
);
32381 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32382 /*check_dependency_p=*/true,
32383 /*template_p=*/NULL
,
32384 /*declarator_p=*/false,
32385 /*optional_p=*/false);
32386 if (step
!= error_mark_node
)
32387 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32388 if (step
== error_mark_node
)
32391 cp_parser_abort_tentative_parse (parser
);
32393 else if (!cp_parser_parse_definitely (parser
))
32397 step
= cp_parser_expression (parser
);
32399 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
32401 sorry ("using parameters for %<linear%> step is not supported yet");
32402 step
= integer_one_node
;
32404 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32405 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32406 /*or_comma=*/false,
32407 /*consume_paren=*/true);
32409 if (step
== error_mark_node
)
32413 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32415 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32416 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32423 safelen ( constant-expression ) */
32426 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32427 location_t location
)
32431 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32434 t
= cp_parser_constant_expression (parser
);
32436 if (t
== error_mark_node
32437 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32438 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32439 /*or_comma=*/false,
32440 /*consume_paren=*/true);
32442 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32444 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32445 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32446 OMP_CLAUSE_CHAIN (c
) = list
;
32452 simdlen ( constant-expression ) */
32455 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32456 location_t location
)
32460 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32463 t
= cp_parser_constant_expression (parser
);
32465 if (t
== error_mark_node
32466 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32467 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32468 /*or_comma=*/false,
32469 /*consume_paren=*/true);
32471 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32473 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32474 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32475 OMP_CLAUSE_CHAIN (c
) = list
;
32482 identifier [+/- integer]
32483 vec , identifier [+/- integer]
32487 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32492 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32494 cp_parser_error (parser
, "expected identifier");
32498 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32500 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32501 tree t
, identifier
= cp_parser_identifier (parser
);
32502 tree addend
= NULL
;
32504 if (identifier
== error_mark_node
)
32505 t
= error_mark_node
;
32508 t
= cp_parser_lookup_name_simple
32509 (parser
, identifier
,
32510 cp_lexer_peek_token (parser
->lexer
)->location
);
32511 if (t
== error_mark_node
)
32512 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
32517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
32519 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
32521 addend
= integer_zero_node
;
32522 goto add_to_vector
;
32524 cp_lexer_consume_token (parser
->lexer
);
32526 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
32528 cp_parser_error (parser
, "expected integer");
32532 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32533 if (TREE_CODE (addend
) != INTEGER_CST
)
32535 cp_parser_error (parser
, "expected integer");
32538 cp_lexer_consume_token (parser
->lexer
);
32541 if (t
!= error_mark_node
)
32543 vec
= tree_cons (addend
, t
, vec
);
32545 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
32548 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32551 cp_lexer_consume_token (parser
->lexer
);
32554 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
32556 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
32557 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
32558 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
32559 OMP_CLAUSE_CHAIN (u
) = list
;
32566 depend ( depend-kind : variable-list )
32574 depend ( sink : vec ) */
32577 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
32580 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
32582 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32587 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32588 const char *p
= IDENTIFIER_POINTER (id
);
32590 if (strcmp ("in", p
) == 0)
32591 kind
= OMP_CLAUSE_DEPEND_IN
;
32592 else if (strcmp ("inout", p
) == 0)
32593 kind
= OMP_CLAUSE_DEPEND_INOUT
;
32594 else if (strcmp ("out", p
) == 0)
32595 kind
= OMP_CLAUSE_DEPEND_OUT
;
32596 else if (strcmp ("source", p
) == 0)
32597 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
32598 else if (strcmp ("sink", p
) == 0)
32599 kind
= OMP_CLAUSE_DEPEND_SINK
;
32606 cp_lexer_consume_token (parser
->lexer
);
32608 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
32610 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
32611 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32612 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
32613 OMP_CLAUSE_CHAIN (c
) = list
;
32614 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32615 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32616 /*or_comma=*/false,
32617 /*consume_paren=*/true);
32621 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32624 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
32625 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
32628 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
32631 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32632 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
32637 cp_parser_error (parser
, "invalid depend kind");
32639 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32640 /*or_comma=*/false,
32641 /*consume_paren=*/true);
32646 map ( map-kind : variable-list )
32647 map ( variable-list )
32650 alloc | to | from | tofrom
32654 alloc | to | from | tofrom | release | delete
32656 map ( always [,] map-kind: variable-list ) */
32659 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
32662 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
32663 bool always
= false;
32665 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32670 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32671 const char *p
= IDENTIFIER_POINTER (id
);
32673 if (strcmp ("always", p
) == 0)
32676 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
32678 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
32679 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
32681 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
32685 cp_lexer_consume_token (parser
->lexer
);
32687 cp_lexer_consume_token (parser
->lexer
);
32692 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32693 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32695 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32696 const char *p
= IDENTIFIER_POINTER (id
);
32698 if (strcmp ("alloc", p
) == 0)
32699 kind
= GOMP_MAP_ALLOC
;
32700 else if (strcmp ("to", p
) == 0)
32701 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
32702 else if (strcmp ("from", p
) == 0)
32703 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
32704 else if (strcmp ("tofrom", p
) == 0)
32705 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
32706 else if (strcmp ("release", p
) == 0)
32707 kind
= GOMP_MAP_RELEASE
;
32710 cp_parser_error (parser
, "invalid map kind");
32711 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32712 /*or_comma=*/false,
32713 /*consume_paren=*/true);
32716 cp_lexer_consume_token (parser
->lexer
);
32717 cp_lexer_consume_token (parser
->lexer
);
32719 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
32720 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
32722 kind
= GOMP_MAP_DELETE
;
32723 cp_lexer_consume_token (parser
->lexer
);
32724 cp_lexer_consume_token (parser
->lexer
);
32727 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
32730 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32731 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32737 device ( expression ) */
32740 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
32741 location_t location
)
32745 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32748 t
= cp_parser_expression (parser
);
32750 if (t
== error_mark_node
32751 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32752 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32753 /*or_comma=*/false,
32754 /*consume_paren=*/true);
32756 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
32757 "device", location
);
32759 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
32760 OMP_CLAUSE_DEVICE_ID (c
) = t
;
32761 OMP_CLAUSE_CHAIN (c
) = list
;
32767 dist_schedule ( static )
32768 dist_schedule ( static , expression ) */
32771 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
32772 location_t location
)
32776 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32779 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
32781 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32783 cp_lexer_consume_token (parser
->lexer
);
32785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32787 cp_lexer_consume_token (parser
->lexer
);
32789 t
= cp_parser_assignment_expression (parser
);
32791 if (t
== error_mark_node
)
32793 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
32795 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32798 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32801 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
32803 OMP_CLAUSE_CHAIN (c
) = list
;
32807 cp_parser_error (parser
, "invalid dist_schedule kind");
32809 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32810 /*or_comma=*/false,
32811 /*consume_paren=*/true);
32816 proc_bind ( proc-bind-kind )
32819 master | close | spread */
32822 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
32823 location_t location
)
32826 enum omp_clause_proc_bind_kind kind
;
32828 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32833 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32834 const char *p
= IDENTIFIER_POINTER (id
);
32836 if (strcmp ("master", p
) == 0)
32837 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
32838 else if (strcmp ("close", p
) == 0)
32839 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
32840 else if (strcmp ("spread", p
) == 0)
32841 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
32848 cp_lexer_consume_token (parser
->lexer
);
32849 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32852 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
32853 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
32855 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
32856 OMP_CLAUSE_CHAIN (c
) = list
;
32860 cp_parser_error (parser
, "invalid depend kind");
32862 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32863 /*or_comma=*/false,
32864 /*consume_paren=*/true);
32869 async [( int-expr )] */
32872 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
32875 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32877 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
32879 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32881 cp_lexer_consume_token (parser
->lexer
);
32883 t
= cp_parser_expression (parser
);
32884 if (t
== error_mark_node
32885 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32886 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32887 /*or_comma=*/false,
32888 /*consume_paren=*/true);
32891 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
32893 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
32894 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
32895 OMP_CLAUSE_CHAIN (c
) = list
;
32901 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32902 is a bitmask in MASK. Return the list of clauses found. */
32905 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
32906 const char *where
, cp_token
*pragma_tok
,
32907 bool finish_p
= true)
32909 tree clauses
= NULL
;
32912 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32915 pragma_omp_clause c_kind
;
32916 omp_clause_code code
;
32917 const char *c_name
;
32918 tree prev
= clauses
;
32920 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32921 cp_lexer_consume_token (parser
->lexer
);
32923 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
32924 c_kind
= cp_parser_omp_clause_name (parser
);
32928 case PRAGMA_OACC_CLAUSE_ASYNC
:
32929 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
32932 case PRAGMA_OACC_CLAUSE_AUTO
:
32933 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
32937 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
32938 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
32939 c_name
= "collapse";
32941 case PRAGMA_OACC_CLAUSE_COPY
:
32942 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32945 case PRAGMA_OACC_CLAUSE_COPYIN
:
32946 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32949 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32950 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32951 c_name
= "copyout";
32953 case PRAGMA_OACC_CLAUSE_CREATE
:
32954 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32957 case PRAGMA_OACC_CLAUSE_DELETE
:
32958 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32961 case PRAGMA_OMP_CLAUSE_DEFAULT
:
32962 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
32963 c_name
= "default";
32965 case PRAGMA_OACC_CLAUSE_DEVICE
:
32966 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32969 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
32970 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
32971 c_name
= "deviceptr";
32973 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32974 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32975 c_name
= "device_resident";
32977 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
32978 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
32980 c_name
= "firstprivate";
32982 case PRAGMA_OACC_CLAUSE_GANG
:
32984 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
32987 case PRAGMA_OACC_CLAUSE_HOST
:
32988 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32991 case PRAGMA_OACC_CLAUSE_IF
:
32992 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
32995 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
32996 clauses
= cp_parser_oacc_simple_clause (parser
,
32997 OMP_CLAUSE_INDEPENDENT
,
32999 c_name
= "independent";
33001 case PRAGMA_OACC_CLAUSE_LINK
:
33002 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33005 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33006 code
= OMP_CLAUSE_NUM_GANGS
;
33007 c_name
= "num_gangs";
33008 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33011 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33012 c_name
= "num_workers";
33013 code
= OMP_CLAUSE_NUM_WORKERS
;
33014 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33017 case PRAGMA_OACC_CLAUSE_PRESENT
:
33018 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33019 c_name
= "present";
33021 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33022 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33023 c_name
= "present_or_copy";
33025 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33026 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33027 c_name
= "present_or_copyin";
33029 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33030 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33031 c_name
= "present_or_copyout";
33033 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33034 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33035 c_name
= "present_or_create";
33037 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33038 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33040 c_name
= "private";
33042 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33043 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33044 c_name
= "reduction";
33046 case PRAGMA_OACC_CLAUSE_SELF
:
33047 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33050 case PRAGMA_OACC_CLAUSE_SEQ
:
33051 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33055 case PRAGMA_OACC_CLAUSE_TILE
:
33056 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33059 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33060 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33062 c_name
= "use_device";
33064 case PRAGMA_OACC_CLAUSE_VECTOR
:
33066 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33069 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33070 c_name
= "vector_length";
33071 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33072 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33075 case PRAGMA_OACC_CLAUSE_WAIT
:
33076 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33079 case PRAGMA_OACC_CLAUSE_WORKER
:
33081 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33085 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33091 if (((mask
>> c_kind
) & 1) == 0)
33093 /* Remove the invalid clause(s) from the list to avoid
33094 confusing the rest of the compiler. */
33096 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33101 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33104 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33109 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33110 is a bitmask in MASK. Return the list of clauses found; the result
33111 of clause default goes in *pdefault. */
33114 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33115 const char *where
, cp_token
*pragma_tok
,
33116 bool finish_p
= true)
33118 tree clauses
= NULL
;
33120 cp_token
*token
= NULL
;
33122 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33124 pragma_omp_clause c_kind
;
33125 const char *c_name
;
33126 tree prev
= clauses
;
33128 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33129 cp_lexer_consume_token (parser
->lexer
);
33131 token
= cp_lexer_peek_token (parser
->lexer
);
33132 c_kind
= cp_parser_omp_clause_name (parser
);
33136 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33137 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33139 c_name
= "collapse";
33141 case PRAGMA_OMP_CLAUSE_COPYIN
:
33142 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33145 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33146 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33148 c_name
= "copyprivate";
33150 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33151 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33152 token
->location
, false);
33153 c_name
= "default";
33155 case PRAGMA_OMP_CLAUSE_FINAL
:
33156 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33159 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33160 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33162 c_name
= "firstprivate";
33164 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33165 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33167 c_name
= "grainsize";
33169 case PRAGMA_OMP_CLAUSE_HINT
:
33170 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33174 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33175 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33177 c_name
= "defaultmap";
33179 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33180 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33182 c_name
= "use_device_ptr";
33184 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33185 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33187 c_name
= "is_device_ptr";
33189 case PRAGMA_OMP_CLAUSE_IF
:
33190 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33194 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33195 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33197 c_name
= "lastprivate";
33199 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33200 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33202 c_name
= "mergeable";
33204 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33205 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33208 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33209 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33211 c_name
= "num_tasks";
33213 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33214 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33216 c_name
= "num_threads";
33218 case PRAGMA_OMP_CLAUSE_ORDERED
:
33219 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33221 c_name
= "ordered";
33223 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33224 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33226 c_name
= "priority";
33228 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33229 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33231 c_name
= "private";
33233 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33234 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33235 c_name
= "reduction";
33237 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33238 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33240 c_name
= "schedule";
33242 case PRAGMA_OMP_CLAUSE_SHARED
:
33243 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33247 case PRAGMA_OMP_CLAUSE_UNTIED
:
33248 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33252 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33253 case PRAGMA_CILK_CLAUSE_MASK
:
33254 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33255 clauses
, token
->location
);
33256 c_name
= "inbranch";
33258 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33259 case PRAGMA_CILK_CLAUSE_NOMASK
:
33260 clauses
= cp_parser_omp_clause_branch (parser
,
33261 OMP_CLAUSE_NOTINBRANCH
,
33262 clauses
, token
->location
);
33263 c_name
= "notinbranch";
33265 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33266 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33267 clauses
, token
->location
);
33268 c_name
= "parallel";
33272 error_at (token
->location
, "%qs must be the first clause of %qs",
33277 case PRAGMA_OMP_CLAUSE_FOR
:
33278 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33279 clauses
, token
->location
);
33282 goto clause_not_first
;
33284 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33285 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33286 clauses
, token
->location
);
33287 c_name
= "sections";
33289 goto clause_not_first
;
33291 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33292 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33293 clauses
, token
->location
);
33294 c_name
= "taskgroup";
33296 goto clause_not_first
;
33298 case PRAGMA_OMP_CLAUSE_LINK
:
33299 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33302 case PRAGMA_OMP_CLAUSE_TO
:
33303 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33304 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33307 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33310 case PRAGMA_OMP_CLAUSE_FROM
:
33311 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33314 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33315 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33317 c_name
= "uniform";
33319 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33320 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33322 c_name
= "num_teams";
33324 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33325 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33327 c_name
= "thread_limit";
33329 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33330 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33331 c_name
= "aligned";
33333 case PRAGMA_OMP_CLAUSE_LINEAR
:
33335 bool cilk_simd_fn
= false, declare_simd
= false;
33336 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
33337 cilk_simd_fn
= true;
33338 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33339 declare_simd
= true;
33340 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
33341 cilk_simd_fn
, declare_simd
);
33345 case PRAGMA_OMP_CLAUSE_DEPEND
:
33346 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33350 case PRAGMA_OMP_CLAUSE_MAP
:
33351 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33354 case PRAGMA_OMP_CLAUSE_DEVICE
:
33355 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33359 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33360 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33362 c_name
= "dist_schedule";
33364 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33365 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33367 c_name
= "proc_bind";
33369 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33370 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33372 c_name
= "safelen";
33374 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33375 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33377 c_name
= "simdlen";
33379 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33380 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33382 c_name
= "nogroup";
33384 case PRAGMA_OMP_CLAUSE_THREADS
:
33386 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33387 clauses
, token
->location
);
33388 c_name
= "threads";
33390 case PRAGMA_OMP_CLAUSE_SIMD
:
33392 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33393 clauses
, token
->location
);
33396 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
33397 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
33398 c_name
= "simdlen";
33401 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33407 if (((mask
>> c_kind
) & 1) == 0)
33409 /* Remove the invalid clause(s) from the list to avoid
33410 confusing the rest of the compiler. */
33412 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33416 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33417 no reason to skip to the end. */
33418 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
33419 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33422 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33423 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33425 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33434 In practice, we're also interested in adding the statement to an
33435 outer node. So it is convenient if we work around the fact that
33436 cp_parser_statement calls add_stmt. */
33439 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33441 unsigned save
= parser
->in_statement
;
33443 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33444 This preserves the "not within loop or switch" style error messages
33445 for nonsense cases like
33451 if (parser
->in_statement
)
33452 parser
->in_statement
= IN_OMP_BLOCK
;
33458 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33460 parser
->in_statement
= save
;
33464 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33466 tree stmt
= begin_omp_structured_block ();
33467 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33469 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33471 cp_parser_end_omp_structured_block (parser
, save
);
33472 return finish_omp_structured_block (stmt
);
33476 # pragma omp atomic new-line
33480 x binop= expr | x++ | ++x | x-- | --x
33482 +, *, -, /, &, ^, |, <<, >>
33484 where x is an lvalue expression with scalar type.
33487 # pragma omp atomic new-line
33490 # pragma omp atomic read new-line
33493 # pragma omp atomic write new-line
33496 # pragma omp atomic update new-line
33499 # pragma omp atomic capture new-line
33502 # pragma omp atomic capture new-line
33510 expression-stmt | x = x binop expr
33512 v = expression-stmt
33514 { v = x; update-stmt; } | { update-stmt; v = x; }
33518 expression-stmt | x = x binop expr | x = expr binop x
33522 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33524 where x and v are lvalue expressions with scalar type. */
33527 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
33529 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
33530 tree rhs1
= NULL_TREE
, orig_lhs
;
33531 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
33532 bool structured_block
= false;
33533 bool seq_cst
= false;
33535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33537 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33538 const char *p
= IDENTIFIER_POINTER (id
);
33540 if (!strcmp (p
, "seq_cst"))
33543 cp_lexer_consume_token (parser
->lexer
);
33544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33545 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33546 cp_lexer_consume_token (parser
->lexer
);
33549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33551 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33552 const char *p
= IDENTIFIER_POINTER (id
);
33554 if (!strcmp (p
, "read"))
33555 code
= OMP_ATOMIC_READ
;
33556 else if (!strcmp (p
, "write"))
33558 else if (!strcmp (p
, "update"))
33560 else if (!strcmp (p
, "capture"))
33561 code
= OMP_ATOMIC_CAPTURE_NEW
;
33565 cp_lexer_consume_token (parser
->lexer
);
33569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
33570 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
33571 cp_lexer_consume_token (parser
->lexer
);
33573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33575 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33576 const char *p
= IDENTIFIER_POINTER (id
);
33578 if (!strcmp (p
, "seq_cst"))
33581 cp_lexer_consume_token (parser
->lexer
);
33585 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33589 case OMP_ATOMIC_READ
:
33590 case NOP_EXPR
: /* atomic write */
33591 v
= cp_parser_unary_expression (parser
);
33592 if (v
== error_mark_node
)
33594 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33596 if (code
== NOP_EXPR
)
33597 lhs
= cp_parser_expression (parser
);
33599 lhs
= cp_parser_unary_expression (parser
);
33600 if (lhs
== error_mark_node
)
33602 if (code
== NOP_EXPR
)
33604 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33612 case OMP_ATOMIC_CAPTURE_NEW
:
33613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33615 cp_lexer_consume_token (parser
->lexer
);
33616 structured_block
= true;
33620 v
= cp_parser_unary_expression (parser
);
33621 if (v
== error_mark_node
)
33623 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33631 lhs
= cp_parser_unary_expression (parser
);
33633 switch (TREE_CODE (lhs
))
33638 case POSTINCREMENT_EXPR
:
33639 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33640 code
= OMP_ATOMIC_CAPTURE_OLD
;
33642 case PREINCREMENT_EXPR
:
33643 lhs
= TREE_OPERAND (lhs
, 0);
33644 opcode
= PLUS_EXPR
;
33645 rhs
= integer_one_node
;
33648 case POSTDECREMENT_EXPR
:
33649 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
33650 code
= OMP_ATOMIC_CAPTURE_OLD
;
33652 case PREDECREMENT_EXPR
:
33653 lhs
= TREE_OPERAND (lhs
, 0);
33654 opcode
= MINUS_EXPR
;
33655 rhs
= integer_one_node
;
33658 case COMPOUND_EXPR
:
33659 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
33660 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
33661 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
33662 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
33663 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33664 (TREE_OPERAND (lhs
, 1), 0), 0)))
33666 /* Undo effects of boolean_increment for post {in,de}crement. */
33667 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
33670 if (TREE_CODE (lhs
) == MODIFY_EXPR
33671 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
33673 /* Undo effects of boolean_increment. */
33674 if (integer_onep (TREE_OPERAND (lhs
, 1)))
33676 /* This is pre or post increment. */
33677 rhs
= TREE_OPERAND (lhs
, 1);
33678 lhs
= TREE_OPERAND (lhs
, 0);
33680 if (code
== OMP_ATOMIC_CAPTURE_NEW
33681 && !structured_block
33682 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
33683 code
= OMP_ATOMIC_CAPTURE_OLD
;
33689 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33692 opcode
= MULT_EXPR
;
33695 opcode
= TRUNC_DIV_EXPR
;
33698 opcode
= PLUS_EXPR
;
33701 opcode
= MINUS_EXPR
;
33703 case CPP_LSHIFT_EQ
:
33704 opcode
= LSHIFT_EXPR
;
33706 case CPP_RSHIFT_EQ
:
33707 opcode
= RSHIFT_EXPR
;
33710 opcode
= BIT_AND_EXPR
;
33713 opcode
= BIT_IOR_EXPR
;
33716 opcode
= BIT_XOR_EXPR
;
33719 enum cp_parser_prec oprec
;
33721 cp_lexer_consume_token (parser
->lexer
);
33722 cp_parser_parse_tentatively (parser
);
33723 rhs1
= cp_parser_simple_cast_expression (parser
);
33724 if (rhs1
== error_mark_node
)
33726 cp_parser_abort_tentative_parse (parser
);
33727 cp_parser_simple_cast_expression (parser
);
33730 token
= cp_lexer_peek_token (parser
->lexer
);
33731 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
33733 cp_parser_abort_tentative_parse (parser
);
33734 cp_parser_parse_tentatively (parser
);
33735 rhs
= cp_parser_binary_expression (parser
, false, true,
33736 PREC_NOT_OPERATOR
, NULL
);
33737 if (rhs
== error_mark_node
)
33739 cp_parser_abort_tentative_parse (parser
);
33740 cp_parser_binary_expression (parser
, false, true,
33741 PREC_NOT_OPERATOR
, NULL
);
33744 switch (TREE_CODE (rhs
))
33747 case TRUNC_DIV_EXPR
:
33756 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
33758 if (cp_parser_parse_definitely (parser
))
33760 opcode
= TREE_CODE (rhs
);
33761 rhs1
= TREE_OPERAND (rhs
, 0);
33762 rhs
= TREE_OPERAND (rhs
, 1);
33772 cp_parser_abort_tentative_parse (parser
);
33773 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
33775 rhs
= cp_parser_expression (parser
);
33776 if (rhs
== error_mark_node
)
33782 cp_parser_error (parser
,
33783 "invalid form of %<#pragma omp atomic%>");
33786 if (!cp_parser_parse_definitely (parser
))
33788 switch (token
->type
)
33790 case CPP_SEMICOLON
:
33791 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33793 code
= OMP_ATOMIC_CAPTURE_OLD
;
33798 cp_lexer_consume_token (parser
->lexer
);
33801 else if (structured_block
)
33808 cp_parser_error (parser
,
33809 "invalid form of %<#pragma omp atomic%>");
33812 opcode
= MULT_EXPR
;
33815 opcode
= TRUNC_DIV_EXPR
;
33818 opcode
= PLUS_EXPR
;
33821 opcode
= MINUS_EXPR
;
33824 opcode
= LSHIFT_EXPR
;
33827 opcode
= RSHIFT_EXPR
;
33830 opcode
= BIT_AND_EXPR
;
33833 opcode
= BIT_IOR_EXPR
;
33836 opcode
= BIT_XOR_EXPR
;
33839 cp_parser_error (parser
,
33840 "invalid operator for %<#pragma omp atomic%>");
33843 oprec
= TOKEN_PRECEDENCE (token
);
33844 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
33845 if (commutative_tree_code (opcode
))
33846 oprec
= (enum cp_parser_prec
) (oprec
- 1);
33847 cp_lexer_consume_token (parser
->lexer
);
33848 rhs
= cp_parser_binary_expression (parser
, false, false,
33850 if (rhs
== error_mark_node
)
33855 cp_parser_error (parser
,
33856 "invalid operator for %<#pragma omp atomic%>");
33859 cp_lexer_consume_token (parser
->lexer
);
33861 rhs
= cp_parser_expression (parser
);
33862 if (rhs
== error_mark_node
)
33867 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33869 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
33871 v
= cp_parser_unary_expression (parser
);
33872 if (v
== error_mark_node
)
33874 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33876 lhs1
= cp_parser_unary_expression (parser
);
33877 if (lhs1
== error_mark_node
)
33880 if (structured_block
)
33882 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33883 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33886 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
33887 if (!structured_block
)
33888 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33892 cp_parser_skip_to_end_of_block_or_statement (parser
);
33893 if (structured_block
)
33895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33896 cp_lexer_consume_token (parser
->lexer
);
33897 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
33899 cp_parser_skip_to_end_of_block_or_statement (parser
);
33900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33901 cp_lexer_consume_token (parser
->lexer
);
33908 # pragma omp barrier new-line */
33911 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
33913 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33914 finish_omp_barrier ();
33918 # pragma omp critical [(name)] new-line
33922 # pragma omp critical [(name) [hint(expression)]] new-line
33923 structured-block */
33925 #define OMP_CRITICAL_CLAUSE_MASK \
33926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33929 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
33931 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
33933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33935 cp_lexer_consume_token (parser
->lexer
);
33937 name
= cp_parser_identifier (parser
);
33939 if (name
== error_mark_node
33940 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33941 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33942 /*or_comma=*/false,
33943 /*consume_paren=*/true);
33944 if (name
== error_mark_node
)
33947 clauses
= cp_parser_omp_all_clauses (parser
,
33948 OMP_CRITICAL_CLAUSE_MASK
,
33949 "#pragma omp critical", pragma_tok
);
33952 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33954 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
33955 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
33959 # pragma omp flush flush-vars[opt] new-line
33962 ( variable-list ) */
33965 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
33967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33968 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
33969 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33971 finish_omp_flush ();
33974 /* Helper function, to parse omp for increment expression. */
33977 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
33979 tree cond
= cp_parser_binary_expression (parser
, false, true,
33980 PREC_NOT_OPERATOR
, NULL
);
33981 if (cond
== error_mark_node
33982 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33984 cp_parser_skip_to_end_of_statement (parser
);
33985 return error_mark_node
;
33988 switch (TREE_CODE (cond
))
33996 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
33998 /* Fall through: OpenMP disallows NE_EXPR. */
33999 gcc_fallthrough ();
34001 return error_mark_node
;
34004 /* If decl is an iterator, preserve LHS and RHS of the relational
34005 expr until finish_omp_for. */
34007 && (type_dependent_expression_p (decl
)
34008 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34011 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34013 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34014 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34015 /*overload=*/NULL
, tf_warning_or_error
);
34018 /* Helper function, to parse omp for increment expression. */
34021 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34023 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34029 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34031 op
= (token
->type
== CPP_PLUS_PLUS
34032 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34033 cp_lexer_consume_token (parser
->lexer
);
34034 lhs
= cp_parser_simple_cast_expression (parser
);
34036 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34037 return error_mark_node
;
34038 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34041 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34043 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34044 return error_mark_node
;
34046 token
= cp_lexer_peek_token (parser
->lexer
);
34047 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34049 op
= (token
->type
== CPP_PLUS_PLUS
34050 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34051 cp_lexer_consume_token (parser
->lexer
);
34052 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34055 op
= cp_parser_assignment_operator_opt (parser
);
34056 if (op
== ERROR_MARK
)
34057 return error_mark_node
;
34059 if (op
!= NOP_EXPR
)
34061 rhs
= cp_parser_assignment_expression (parser
);
34062 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34063 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34066 lhs
= cp_parser_binary_expression (parser
, false, false,
34067 PREC_ADDITIVE_EXPRESSION
, NULL
);
34068 token
= cp_lexer_peek_token (parser
->lexer
);
34069 decl_first
= (lhs
== decl
34070 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34073 if (token
->type
!= CPP_PLUS
34074 && token
->type
!= CPP_MINUS
)
34075 return error_mark_node
;
34079 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34080 cp_lexer_consume_token (parser
->lexer
);
34081 rhs
= cp_parser_binary_expression (parser
, false, false,
34082 PREC_ADDITIVE_EXPRESSION
, NULL
);
34083 token
= cp_lexer_peek_token (parser
->lexer
);
34084 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34086 if (lhs
== NULL_TREE
)
34088 if (op
== PLUS_EXPR
)
34091 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34092 tf_warning_or_error
);
34095 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34096 ERROR_MARK
, NULL
, tf_warning_or_error
);
34099 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34104 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34105 || op
== MINUS_EXPR
)
34106 return error_mark_node
;
34107 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34110 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34112 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34115 /* Parse the initialization statement of either an OpenMP for loop or
34116 a Cilk Plus for loop.
34118 Return true if the resulting construct should have an
34119 OMP_CLAUSE_PRIVATE added to it. */
34122 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34123 enum tree_code code
,
34124 tree
&this_pre_body
,
34125 vec
<tree
, va_gc
> *for_block
,
34131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34134 tree add_private_clause
= NULL_TREE
;
34136 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34140 integer-type var = lb
34141 random-access-iterator-type var = lb
34142 pointer-type var = lb
34144 cp_decl_specifier_seq type_specifiers
;
34146 /* First, try to parse as an initialized declaration. See
34147 cp_parser_condition, from whence the bulk of this is copied. */
34149 cp_parser_parse_tentatively (parser
);
34150 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34151 /*is_trailing_return=*/false,
34153 if (cp_parser_parse_definitely (parser
))
34155 /* If parsing a type specifier seq succeeded, then this
34156 MUST be a initialized declaration. */
34157 tree asm_specification
, attributes
;
34158 cp_declarator
*declarator
;
34160 declarator
= cp_parser_declarator (parser
,
34161 CP_PARSER_DECLARATOR_NAMED
,
34162 /*ctor_dtor_or_conv_p=*/NULL
,
34163 /*parenthesized_p=*/NULL
,
34164 /*member_p=*/false,
34165 /*friend_p=*/false);
34166 attributes
= cp_parser_attributes_opt (parser
);
34167 asm_specification
= cp_parser_asm_specification_opt (parser
);
34169 if (declarator
== cp_error_declarator
)
34170 cp_parser_skip_to_end_of_statement (parser
);
34174 tree pushed_scope
, auto_node
;
34176 decl
= start_decl (declarator
, &type_specifiers
,
34177 SD_INITIALIZED
, attributes
,
34178 /*prefix_attributes=*/NULL_TREE
,
34181 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34182 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34184 if (cp_lexer_next_token_is (parser
->lexer
,
34187 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
34188 error ("parenthesized initialization is not allowed in "
34189 "OpenMP %<for%> loop");
34191 error ("parenthesized initialization is "
34192 "not allowed in for-loop");
34195 /* Trigger an error. */
34196 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34198 init
= error_mark_node
;
34199 cp_parser_skip_to_end_of_statement (parser
);
34201 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34202 || type_dependent_expression_p (decl
)
34205 bool is_direct_init
, is_non_constant_init
;
34207 init
= cp_parser_initializer (parser
,
34209 &is_non_constant_init
);
34214 = do_auto_deduction (TREE_TYPE (decl
), init
,
34217 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34218 && !type_dependent_expression_p (decl
))
34222 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34224 LOOKUP_ONLYCONVERTING
);
34226 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34228 vec_safe_push (for_block
, this_pre_body
);
34233 init
= pop_stmt_list (this_pre_body
);
34234 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34236 tree_stmt_iterator i
= tsi_start (init
);
34237 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34238 while (!tsi_end_p (i
))
34240 tree t
= tsi_stmt (i
);
34241 if (TREE_CODE (t
) == DECL_EXPR
34242 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34245 vec_safe_push (for_block
, t
);
34250 if (tsi_one_before_end_p (i
))
34252 tree t
= tsi_stmt (i
);
34254 free_stmt_list (init
);
34259 this_pre_body
= NULL_TREE
;
34264 cp_lexer_consume_token (parser
->lexer
);
34265 init
= cp_parser_assignment_expression (parser
);
34268 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34269 init
= error_mark_node
;
34271 cp_finish_decl (decl
, NULL_TREE
,
34272 /*init_const_expr_p=*/false,
34274 LOOKUP_ONLYCONVERTING
);
34278 pop_scope (pushed_scope
);
34284 /* If parsing a type specifier sequence failed, then
34285 this MUST be a simple expression. */
34286 if (code
== CILK_FOR
)
34287 error ("%<_Cilk_for%> allows expression instead of declaration only "
34288 "in C, not in C++");
34289 cp_parser_parse_tentatively (parser
);
34290 decl
= cp_parser_primary_expression (parser
, false, false,
34292 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34293 if (!cp_parser_error_occurred (parser
)
34295 && (TREE_CODE (decl
) == COMPONENT_REF
34296 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34298 cp_parser_abort_tentative_parse (parser
);
34299 cp_parser_parse_tentatively (parser
);
34300 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34301 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34302 /*check_dependency_p=*/true,
34303 /*template_p=*/NULL
,
34304 /*declarator_p=*/false,
34305 /*optional_p=*/false);
34306 if (name
!= error_mark_node
34307 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34309 decl
= cp_parser_lookup_name_simple (parser
, name
,
34311 if (TREE_CODE (decl
) == FIELD_DECL
)
34312 add_private_clause
= omp_privatize_field (decl
, false);
34314 cp_parser_abort_tentative_parse (parser
);
34315 cp_parser_parse_tentatively (parser
);
34316 decl
= cp_parser_primary_expression (parser
, false, false,
34319 if (!cp_parser_error_occurred (parser
)
34322 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34326 cp_parser_parse_definitely (parser
);
34327 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34328 rhs
= cp_parser_assignment_expression (parser
);
34330 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34333 tf_warning_or_error
));
34334 if (!add_private_clause
)
34335 add_private_clause
= decl
;
34340 cp_parser_abort_tentative_parse (parser
);
34341 init
= cp_parser_expression (parser
);
34344 if (TREE_CODE (init
) == MODIFY_EXPR
34345 || TREE_CODE (init
) == MODOP_EXPR
)
34346 real_decl
= TREE_OPERAND (init
, 0);
34350 return add_private_clause
;
34353 /* Parse the restricted form of the for statement allowed by OpenMP. */
34356 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34357 tree
*cclauses
, bool *if_p
)
34359 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34360 tree real_decl
, initv
, condv
, incrv
, declv
;
34361 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34362 location_t loc_first
;
34363 bool collapse_err
= false;
34364 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34365 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34366 auto_vec
<tree
, 4> orig_inits
;
34368 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34369 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34370 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34371 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34372 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34375 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34378 if (ordered
&& ordered
< collapse
)
34380 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34381 "%<ordered%> clause parameter is less than %<collapse%>");
34382 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34383 = build_int_cst (NULL_TREE
, collapse
);
34384 ordered
= collapse
;
34388 for (tree
*pc
= &clauses
; *pc
; )
34389 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34391 error_at (OMP_CLAUSE_LOCATION (*pc
),
34392 "%<linear%> clause may not be specified together "
34393 "with %<ordered%> clause with a parameter");
34394 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34397 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34400 gcc_assert (collapse
>= 1 && ordered
>= 0);
34401 count
= ordered
? ordered
: collapse
;
34403 declv
= make_tree_vec (count
);
34404 initv
= make_tree_vec (count
);
34405 condv
= make_tree_vec (count
);
34406 incrv
= make_tree_vec (count
);
34408 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34410 for (i
= 0; i
< count
; i
++)
34412 int bracecount
= 0;
34413 tree add_private_clause
= NULL_TREE
;
34416 if (code
!= CILK_FOR
34417 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34419 cp_parser_error (parser
, "for statement expected");
34422 if (code
== CILK_FOR
34423 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
34425 cp_parser_error (parser
, "_Cilk_for statement expected");
34428 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34430 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34433 init
= orig_init
= decl
= real_decl
= NULL
;
34434 this_pre_body
= push_stmt_list ();
34437 = cp_parser_omp_for_loop_init (parser
, code
,
34438 this_pre_body
, for_block
,
34439 init
, orig_init
, decl
, real_decl
);
34441 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34444 this_pre_body
= pop_stmt_list (this_pre_body
);
34448 pre_body
= push_stmt_list ();
34450 add_stmt (this_pre_body
);
34451 pre_body
= pop_stmt_list (pre_body
);
34454 pre_body
= this_pre_body
;
34459 if (cclauses
!= NULL
34460 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34461 && real_decl
!= NULL_TREE
)
34464 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34465 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34466 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34468 error_at (loc
, "iteration variable %qD"
34469 " should not be firstprivate", real_decl
);
34470 *c
= OMP_CLAUSE_CHAIN (*c
);
34472 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34473 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34475 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34477 *c
= OMP_CLAUSE_CHAIN (*c
);
34478 if (code
== OMP_SIMD
)
34480 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34481 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34485 OMP_CLAUSE_CHAIN (l
) = clauses
;
34488 add_private_clause
= NULL_TREE
;
34492 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34493 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34494 add_private_clause
= NULL_TREE
;
34495 c
= &OMP_CLAUSE_CHAIN (*c
);
34499 if (add_private_clause
)
34502 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34504 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34505 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34506 && OMP_CLAUSE_DECL (c
) == decl
)
34508 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34509 && OMP_CLAUSE_DECL (c
) == decl
)
34510 error_at (loc
, "iteration variable %qD "
34511 "should not be firstprivate",
34513 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34514 && OMP_CLAUSE_DECL (c
) == decl
)
34515 error_at (loc
, "iteration variable %qD should not be reduction",
34520 if (code
!= OMP_SIMD
)
34521 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
34522 else if (collapse
== 1)
34523 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
34525 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
34526 OMP_CLAUSE_DECL (c
) = add_private_clause
;
34527 c
= finish_omp_clauses (c
, C_ORT_OMP
);
34530 OMP_CLAUSE_CHAIN (c
) = clauses
;
34532 /* For linear, signal that we need to fill up
34533 the so far unknown linear step. */
34534 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
34535 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
34541 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34542 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
34543 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34546 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
34548 /* If decl is an iterator, preserve the operator on decl
34549 until finish_omp_for. */
34551 && ((processing_template_decl
34552 && (TREE_TYPE (real_decl
) == NULL_TREE
34553 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
34554 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
34555 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
34557 incr
= cp_parser_expression (parser
);
34558 if (!EXPR_HAS_LOCATION (incr
))
34559 protected_set_expr_location (incr
, input_location
);
34562 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34563 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34564 /*or_comma=*/false,
34565 /*consume_paren=*/true);
34567 TREE_VEC_ELT (declv
, i
) = decl
;
34568 TREE_VEC_ELT (initv
, i
) = init
;
34569 TREE_VEC_ELT (condv
, i
) = cond
;
34570 TREE_VEC_ELT (incrv
, i
) = incr
;
34573 orig_inits
.safe_grow_cleared (i
+ 1);
34574 orig_inits
[i
] = orig_init
;
34577 if (i
== count
- 1)
34580 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34581 in between the collapsed for loops to be still considered perfectly
34582 nested. Hopefully the final version clarifies this.
34583 For now handle (multiple) {'s and empty statements. */
34584 cp_parser_parse_tentatively (parser
);
34587 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34589 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34591 cp_lexer_consume_token (parser
->lexer
);
34594 else if (bracecount
34595 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34596 cp_lexer_consume_token (parser
->lexer
);
34599 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34600 error_at (loc
, "not enough collapsed for loops");
34601 collapse_err
= true;
34602 cp_parser_abort_tentative_parse (parser
);
34611 cp_parser_parse_definitely (parser
);
34612 nbraces
+= bracecount
;
34619 /* Note that we saved the original contents of this flag when we entered
34620 the structured block, and so we don't need to re-save it here. */
34621 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
34622 parser
->in_statement
= IN_CILK_SIMD_FOR
;
34624 parser
->in_statement
= IN_OMP_FOR
;
34626 /* Note that the grammar doesn't call for a structured block here,
34627 though the loop as a whole is a structured block. */
34628 body
= push_stmt_list ();
34629 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34630 body
= pop_stmt_list (body
);
34632 if (declv
== NULL_TREE
)
34635 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
34636 body
, pre_body
, &orig_inits
, clauses
);
34640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34642 cp_lexer_consume_token (parser
->lexer
);
34645 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34646 cp_lexer_consume_token (parser
->lexer
);
34651 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
34652 "collapsed loops not perfectly nested");
34654 collapse_err
= true;
34655 cp_parser_statement_seq_opt (parser
, NULL
);
34656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
34661 while (!for_block
->is_empty ())
34663 tree t
= for_block
->pop ();
34664 if (TREE_CODE (t
) == STATEMENT_LIST
)
34665 add_stmt (pop_stmt_list (t
));
34669 release_tree_vector (for_block
);
34674 /* Helper function for OpenMP parsing, split clauses and call
34675 finish_omp_clauses on each of the set of clauses afterwards. */
34678 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
34679 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
34682 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
34683 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
34685 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
34689 #pragma omp simd simd-clause[optseq] new-line
34692 #define OMP_SIMD_CLAUSE_MASK \
34693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34703 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
34704 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34707 tree clauses
, sb
, ret
;
34709 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34711 strcat (p_name
, " simd");
34712 mask
|= OMP_SIMD_CLAUSE_MASK
;
34714 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34718 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
34719 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
34720 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
34721 OMP_CLAUSE_ORDERED
);
34722 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
34724 error_at (OMP_CLAUSE_LOCATION (c
),
34725 "%<ordered%> clause with parameter may not be specified "
34726 "on %qs construct", p_name
);
34727 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
34731 sb
= begin_omp_structured_block ();
34732 save
= cp_parser_begin_omp_structured_block (parser
);
34734 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
34736 cp_parser_end_omp_structured_block (parser
, save
);
34737 add_stmt (finish_omp_structured_block (sb
));
34743 #pragma omp for for-clause[optseq] new-line
34747 #pragma omp for simd for-simd-clause[optseq] new-line
34750 #define OMP_FOR_CLAUSE_MASK \
34751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34762 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
34763 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34766 tree clauses
, sb
, ret
;
34768 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34770 strcat (p_name
, " for");
34771 mask
|= OMP_FOR_CLAUSE_MASK
;
34772 /* parallel for{, simd} disallows nowait clause, but for
34773 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34774 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
34775 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
34776 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34777 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34778 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
34780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34782 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34783 const char *p
= IDENTIFIER_POINTER (id
);
34785 if (strcmp (p
, "simd") == 0)
34787 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34788 if (cclauses
== NULL
)
34789 cclauses
= cclauses_buf
;
34791 cp_lexer_consume_token (parser
->lexer
);
34792 if (!flag_openmp
) /* flag_openmp_simd */
34793 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34795 sb
= begin_omp_structured_block ();
34796 save
= cp_parser_begin_omp_structured_block (parser
);
34797 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34799 cp_parser_end_omp_structured_block (parser
, save
);
34800 tree body
= finish_omp_structured_block (sb
);
34803 ret
= make_node (OMP_FOR
);
34804 TREE_TYPE (ret
) = void_type_node
;
34805 OMP_FOR_BODY (ret
) = body
;
34806 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34807 SET_EXPR_LOCATION (ret
, loc
);
34812 if (!flag_openmp
) /* flag_openmp_simd */
34814 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34818 /* Composite distribute parallel for disallows linear clause. */
34819 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34820 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
34822 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34826 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
34827 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34830 sb
= begin_omp_structured_block ();
34831 save
= cp_parser_begin_omp_structured_block (parser
);
34833 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
34835 cp_parser_end_omp_structured_block (parser
, save
);
34836 add_stmt (finish_omp_structured_block (sb
));
34842 # pragma omp master new-line
34843 structured-block */
34846 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34848 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34849 return c_finish_omp_master (input_location
,
34850 cp_parser_omp_structured_block (parser
, if_p
));
34854 # pragma omp ordered new-line
34858 # pragma omp ordered ordered-clauses new-line
34859 structured-block */
34861 #define OMP_ORDERED_CLAUSE_MASK \
34862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34865 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34866 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34869 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
34870 enum pragma_context context
, bool *if_p
)
34872 location_t loc
= pragma_tok
->location
;
34874 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
34876 cp_parser_error (parser
, "expected declaration specifiers");
34877 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34883 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34884 const char *p
= IDENTIFIER_POINTER (id
);
34886 if (strcmp (p
, "depend") == 0)
34888 if (context
== pragma_stmt
)
34890 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
34891 "%<depend%> clause may only be used in compound "
34893 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34897 = cp_parser_omp_all_clauses (parser
,
34898 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
34899 "#pragma omp ordered", pragma_tok
);
34900 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
34906 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
34907 "#pragma omp ordered", pragma_tok
);
34908 c_finish_omp_ordered (loc
, clauses
,
34909 cp_parser_omp_structured_block (parser
, if_p
));
34916 { section-sequence }
34919 section-directive[opt] structured-block
34920 section-sequence section-directive structured-block */
34923 cp_parser_omp_sections_scope (cp_parser
*parser
)
34925 tree stmt
, substmt
;
34926 bool error_suppress
= false;
34929 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
34932 stmt
= push_stmt_list ();
34934 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
34935 != PRAGMA_OMP_SECTION
)
34937 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34938 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34939 add_stmt (substmt
);
34944 tok
= cp_lexer_peek_token (parser
->lexer
);
34945 if (tok
->type
== CPP_CLOSE_BRACE
)
34947 if (tok
->type
== CPP_EOF
)
34950 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
34952 cp_lexer_consume_token (parser
->lexer
);
34953 cp_parser_require_pragma_eol (parser
, tok
);
34954 error_suppress
= false;
34956 else if (!error_suppress
)
34958 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
34959 error_suppress
= true;
34962 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34963 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34964 add_stmt (substmt
);
34966 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34968 substmt
= pop_stmt_list (stmt
);
34970 stmt
= make_node (OMP_SECTIONS
);
34971 TREE_TYPE (stmt
) = void_type_node
;
34972 OMP_SECTIONS_BODY (stmt
) = substmt
;
34979 # pragma omp sections sections-clause[optseq] newline
34982 #define OMP_SECTIONS_CLAUSE_MASK \
34983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34990 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
34991 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
34994 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34996 strcat (p_name
, " sections");
34997 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
34999 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35001 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35005 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35006 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35009 ret
= cp_parser_omp_sections_scope (parser
);
35011 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35017 # pragma omp parallel parallel-clause[optseq] new-line
35019 # pragma omp parallel for parallel-for-clause[optseq] new-line
35021 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35025 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35026 structured-block */
35028 #define OMP_PARALLEL_CLAUSE_MASK \
35029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35040 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35041 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35044 tree stmt
, clauses
, block
;
35046 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35048 strcat (p_name
, " parallel");
35049 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35050 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35051 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35052 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35053 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35055 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35057 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35058 if (cclauses
== NULL
)
35059 cclauses
= cclauses_buf
;
35061 cp_lexer_consume_token (parser
->lexer
);
35062 if (!flag_openmp
) /* flag_openmp_simd */
35063 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35065 block
= begin_omp_parallel ();
35066 save
= cp_parser_begin_omp_structured_block (parser
);
35067 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35069 cp_parser_end_omp_structured_block (parser
, save
);
35070 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35072 if (ret
== NULL_TREE
)
35074 OMP_PARALLEL_COMBINED (stmt
) = 1;
35077 /* When combined with distribute, parallel has to be followed by for.
35078 #pragma omp target parallel is allowed though. */
35080 && (mask
& (OMP_CLAUSE_MASK_1
35081 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35083 error_at (loc
, "expected %<for%> after %qs", p_name
);
35084 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35087 else if (!flag_openmp
) /* flag_openmp_simd */
35089 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35092 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35094 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35095 const char *p
= IDENTIFIER_POINTER (id
);
35096 if (strcmp (p
, "sections") == 0)
35098 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35099 cclauses
= cclauses_buf
;
35101 cp_lexer_consume_token (parser
->lexer
);
35102 block
= begin_omp_parallel ();
35103 save
= cp_parser_begin_omp_structured_block (parser
);
35104 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35105 cp_parser_end_omp_structured_block (parser
, save
);
35106 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35108 OMP_PARALLEL_COMBINED (stmt
) = 1;
35113 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35117 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35118 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35121 block
= begin_omp_parallel ();
35122 save
= cp_parser_begin_omp_structured_block (parser
);
35123 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35124 cp_parser_end_omp_structured_block (parser
, save
);
35125 stmt
= finish_omp_parallel (clauses
, block
);
35130 # pragma omp single single-clause[optseq] new-line
35131 structured-block */
35133 #define OMP_SINGLE_CLAUSE_MASK \
35134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35140 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35142 tree stmt
= make_node (OMP_SINGLE
);
35143 TREE_TYPE (stmt
) = void_type_node
;
35145 OMP_SINGLE_CLAUSES (stmt
)
35146 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35147 "#pragma omp single", pragma_tok
);
35148 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35150 return add_stmt (stmt
);
35154 # pragma omp task task-clause[optseq] new-line
35155 structured-block */
35157 #define OMP_TASK_CLAUSE_MASK \
35158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35170 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35172 tree clauses
, block
;
35175 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35176 "#pragma omp task", pragma_tok
);
35177 block
= begin_omp_task ();
35178 save
= cp_parser_begin_omp_structured_block (parser
);
35179 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35180 cp_parser_end_omp_structured_block (parser
, save
);
35181 return finish_omp_task (clauses
, block
);
35185 # pragma omp taskwait new-line */
35188 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35190 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35191 finish_omp_taskwait ();
35195 # pragma omp taskyield new-line */
35198 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35200 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35201 finish_omp_taskyield ();
35205 # pragma omp taskgroup new-line
35206 structured-block */
35209 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35211 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35212 return c_finish_omp_taskgroup (input_location
,
35213 cp_parser_omp_structured_block (parser
,
35219 # pragma omp threadprivate (variable-list) */
35222 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35226 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35227 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35229 finish_omp_threadprivate (vars
);
35233 # pragma omp cancel cancel-clause[optseq] new-line */
35235 #define OMP_CANCEL_CLAUSE_MASK \
35236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35243 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35245 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35246 "#pragma omp cancel", pragma_tok
);
35247 finish_omp_cancel (clauses
);
35251 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35253 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35260 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35261 enum pragma_context context
)
35264 bool point_seen
= false;
35266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35268 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35269 const char *p
= IDENTIFIER_POINTER (id
);
35271 if (strcmp (p
, "point") == 0)
35273 cp_lexer_consume_token (parser
->lexer
);
35279 cp_parser_error (parser
, "expected %<point%>");
35280 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35284 if (context
!= pragma_compound
)
35286 if (context
== pragma_stmt
)
35287 error_at (pragma_tok
->location
,
35288 "%<#pragma omp cancellation point%> may only be used in"
35289 " compound statements");
35291 cp_parser_error (parser
, "expected declaration specifiers");
35292 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35296 clauses
= cp_parser_omp_all_clauses (parser
,
35297 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35298 "#pragma omp cancellation point",
35300 finish_omp_cancellation_point (clauses
);
35304 #pragma omp distribute distribute-clause[optseq] new-line
35307 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35315 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35316 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35319 tree clauses
, sb
, ret
;
35321 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35323 strcat (p_name
, " distribute");
35324 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
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 bool parallel
= false;
35333 if (strcmp (p
, "simd") == 0)
35336 parallel
= strcmp (p
, "parallel") == 0;
35337 if (parallel
|| simd
)
35339 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35340 if (cclauses
== NULL
)
35341 cclauses
= cclauses_buf
;
35342 cp_lexer_consume_token (parser
->lexer
);
35343 if (!flag_openmp
) /* flag_openmp_simd */
35346 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35349 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35352 sb
= begin_omp_structured_block ();
35353 save
= cp_parser_begin_omp_structured_block (parser
);
35355 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35358 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35360 cp_parser_end_omp_structured_block (parser
, save
);
35361 tree body
= finish_omp_structured_block (sb
);
35364 ret
= make_node (OMP_DISTRIBUTE
);
35365 TREE_TYPE (ret
) = void_type_node
;
35366 OMP_FOR_BODY (ret
) = body
;
35367 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35368 SET_EXPR_LOCATION (ret
, loc
);
35373 if (!flag_openmp
) /* flag_openmp_simd */
35375 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35379 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35383 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35384 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35387 sb
= begin_omp_structured_block ();
35388 save
= cp_parser_begin_omp_structured_block (parser
);
35390 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35392 cp_parser_end_omp_structured_block (parser
, save
);
35393 add_stmt (finish_omp_structured_block (sb
));
35399 # pragma omp teams teams-clause[optseq] new-line
35400 structured-block */
35402 #define OMP_TEAMS_CLAUSE_MASK \
35403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35412 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35413 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35416 tree clauses
, sb
, ret
;
35418 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35420 strcat (p_name
, " teams");
35421 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35425 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35426 const char *p
= IDENTIFIER_POINTER (id
);
35427 if (strcmp (p
, "distribute") == 0)
35429 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35430 if (cclauses
== NULL
)
35431 cclauses
= cclauses_buf
;
35433 cp_lexer_consume_token (parser
->lexer
);
35434 if (!flag_openmp
) /* flag_openmp_simd */
35435 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35437 sb
= begin_omp_structured_block ();
35438 save
= cp_parser_begin_omp_structured_block (parser
);
35439 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35441 cp_parser_end_omp_structured_block (parser
, save
);
35442 tree body
= finish_omp_structured_block (sb
);
35445 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35446 ret
= make_node (OMP_TEAMS
);
35447 TREE_TYPE (ret
) = void_type_node
;
35448 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35449 OMP_TEAMS_BODY (ret
) = body
;
35450 OMP_TEAMS_COMBINED (ret
) = 1;
35451 return add_stmt (ret
);
35454 if (!flag_openmp
) /* flag_openmp_simd */
35456 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35460 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35464 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35465 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35468 tree stmt
= make_node (OMP_TEAMS
);
35469 TREE_TYPE (stmt
) = void_type_node
;
35470 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35471 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35473 return add_stmt (stmt
);
35477 # pragma omp target data target-data-clause[optseq] new-line
35478 structured-block */
35480 #define OMP_TARGET_DATA_CLAUSE_MASK \
35481 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35487 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35490 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35491 "#pragma omp target data", pragma_tok
);
35493 for (tree
*pc
= &clauses
; *pc
;)
35495 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35496 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35499 case GOMP_MAP_ALWAYS_TO
:
35500 case GOMP_MAP_FROM
:
35501 case GOMP_MAP_ALWAYS_FROM
:
35502 case GOMP_MAP_TOFROM
:
35503 case GOMP_MAP_ALWAYS_TOFROM
:
35504 case GOMP_MAP_ALLOC
:
35507 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35508 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35509 case GOMP_MAP_ALWAYS_POINTER
:
35513 error_at (OMP_CLAUSE_LOCATION (*pc
),
35514 "%<#pragma omp target data%> with map-type other "
35515 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35516 "on %<map%> clause");
35517 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35520 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35526 error_at (pragma_tok
->location
,
35527 "%<#pragma omp target data%> must contain at least "
35528 "one %<map%> clause");
35532 tree stmt
= make_node (OMP_TARGET_DATA
);
35533 TREE_TYPE (stmt
) = void_type_node
;
35534 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
35536 keep_next_level (true);
35537 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35539 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35540 return add_stmt (stmt
);
35544 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35545 structured-block */
35547 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35555 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35556 enum pragma_context context
)
35558 bool data_seen
= false;
35559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35561 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35562 const char *p
= IDENTIFIER_POINTER (id
);
35564 if (strcmp (p
, "data") == 0)
35566 cp_lexer_consume_token (parser
->lexer
);
35572 cp_parser_error (parser
, "expected %<data%>");
35573 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35577 if (context
== pragma_stmt
)
35579 error_at (pragma_tok
->location
,
35580 "%<#pragma omp target enter data%> may only be "
35581 "used in compound statements");
35582 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35587 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
35588 "#pragma omp target enter data", pragma_tok
);
35590 for (tree
*pc
= &clauses
; *pc
;)
35592 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35593 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35596 case GOMP_MAP_ALWAYS_TO
:
35597 case GOMP_MAP_ALLOC
:
35600 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35601 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35602 case GOMP_MAP_ALWAYS_POINTER
:
35606 error_at (OMP_CLAUSE_LOCATION (*pc
),
35607 "%<#pragma omp target enter data%> with map-type other "
35608 "than %<to%> or %<alloc%> on %<map%> clause");
35609 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35612 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35618 error_at (pragma_tok
->location
,
35619 "%<#pragma omp target enter data%> must contain at least "
35620 "one %<map%> clause");
35624 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
35625 TREE_TYPE (stmt
) = void_type_node
;
35626 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
35627 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35628 return add_stmt (stmt
);
35632 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35633 structured-block */
35635 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35643 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35644 enum pragma_context context
)
35646 bool data_seen
= false;
35647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35649 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35650 const char *p
= IDENTIFIER_POINTER (id
);
35652 if (strcmp (p
, "data") == 0)
35654 cp_lexer_consume_token (parser
->lexer
);
35660 cp_parser_error (parser
, "expected %<data%>");
35661 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35665 if (context
== pragma_stmt
)
35667 error_at (pragma_tok
->location
,
35668 "%<#pragma omp target exit data%> may only be "
35669 "used in compound statements");
35670 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35675 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
35676 "#pragma omp target exit data", pragma_tok
);
35678 for (tree
*pc
= &clauses
; *pc
;)
35680 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35681 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35683 case GOMP_MAP_FROM
:
35684 case GOMP_MAP_ALWAYS_FROM
:
35685 case GOMP_MAP_RELEASE
:
35686 case GOMP_MAP_DELETE
:
35689 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35690 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35691 case GOMP_MAP_ALWAYS_POINTER
:
35695 error_at (OMP_CLAUSE_LOCATION (*pc
),
35696 "%<#pragma omp target exit data%> with map-type other "
35697 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35699 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35702 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35708 error_at (pragma_tok
->location
,
35709 "%<#pragma omp target exit data%> must contain at least "
35710 "one %<map%> clause");
35714 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
35715 TREE_TYPE (stmt
) = void_type_node
;
35716 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
35717 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35718 return add_stmt (stmt
);
35722 # pragma omp target update target-update-clause[optseq] new-line */
35724 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35733 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
35734 enum pragma_context context
)
35736 if (context
== pragma_stmt
)
35738 error_at (pragma_tok
->location
,
35739 "%<#pragma omp target update%> may only be "
35740 "used in compound statements");
35741 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35746 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
35747 "#pragma omp target update", pragma_tok
);
35748 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
35749 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
35751 error_at (pragma_tok
->location
,
35752 "%<#pragma omp target update%> must contain at least one "
35753 "%<from%> or %<to%> clauses");
35757 tree stmt
= make_node (OMP_TARGET_UPDATE
);
35758 TREE_TYPE (stmt
) = void_type_node
;
35759 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
35760 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35766 # pragma omp target target-clause[optseq] new-line
35767 structured-block */
35769 #define OMP_TARGET_CLAUSE_MASK \
35770 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35781 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
35782 enum pragma_context context
, bool *if_p
)
35784 tree
*pc
= NULL
, stmt
;
35786 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
35788 cp_parser_error (parser
, "expected declaration specifiers");
35789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35793 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35795 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35796 const char *p
= IDENTIFIER_POINTER (id
);
35797 enum tree_code ccode
= ERROR_MARK
;
35799 if (strcmp (p
, "teams") == 0)
35801 else if (strcmp (p
, "parallel") == 0)
35802 ccode
= OMP_PARALLEL
;
35803 else if (strcmp (p
, "simd") == 0)
35805 if (ccode
!= ERROR_MARK
)
35807 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
35808 char p_name
[sizeof ("#pragma omp target teams distribute "
35809 "parallel for simd")];
35811 cp_lexer_consume_token (parser
->lexer
);
35812 strcpy (p_name
, "#pragma omp target");
35813 if (!flag_openmp
) /* flag_openmp_simd */
35819 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35820 OMP_TARGET_CLAUSE_MASK
,
35824 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35825 OMP_TARGET_CLAUSE_MASK
,
35829 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35830 OMP_TARGET_CLAUSE_MASK
,
35834 gcc_unreachable ();
35836 return stmt
!= NULL_TREE
;
35838 keep_next_level (true);
35839 tree sb
= begin_omp_structured_block (), ret
;
35840 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
35844 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
35845 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35849 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
35850 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35854 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
35855 OMP_TARGET_CLAUSE_MASK
, cclauses
,
35859 gcc_unreachable ();
35861 cp_parser_end_omp_structured_block (parser
, save
);
35862 tree body
= finish_omp_structured_block (sb
);
35863 if (ret
== NULL_TREE
)
35865 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
35867 /* For combined target teams, ensure the num_teams and
35868 thread_limit clause expressions are evaluated on the host,
35869 before entering the target construct. */
35871 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35872 c
; c
= OMP_CLAUSE_CHAIN (c
))
35873 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
35874 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
35875 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
35877 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
35878 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
35879 if (expr
== error_mark_node
)
35881 tree tmp
= TARGET_EXPR_SLOT (expr
);
35883 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
35884 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
35885 OMP_CLAUSE_FIRSTPRIVATE
);
35886 OMP_CLAUSE_DECL (tc
) = tmp
;
35887 OMP_CLAUSE_CHAIN (tc
)
35888 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35889 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
35892 tree stmt
= make_node (OMP_TARGET
);
35893 TREE_TYPE (stmt
) = void_type_node
;
35894 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35895 OMP_TARGET_BODY (stmt
) = body
;
35896 OMP_TARGET_COMBINED (stmt
) = 1;
35898 pc
= &OMP_TARGET_CLAUSES (stmt
);
35899 goto check_clauses
;
35901 else if (!flag_openmp
) /* flag_openmp_simd */
35903 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35906 else if (strcmp (p
, "data") == 0)
35908 cp_lexer_consume_token (parser
->lexer
);
35909 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
35912 else if (strcmp (p
, "enter") == 0)
35914 cp_lexer_consume_token (parser
->lexer
);
35915 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
35918 else if (strcmp (p
, "exit") == 0)
35920 cp_lexer_consume_token (parser
->lexer
);
35921 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
35924 else if (strcmp (p
, "update") == 0)
35926 cp_lexer_consume_token (parser
->lexer
);
35927 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
35931 stmt
= make_node (OMP_TARGET
);
35932 TREE_TYPE (stmt
) = void_type_node
;
35934 OMP_TARGET_CLAUSES (stmt
)
35935 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
35936 "#pragma omp target", pragma_tok
);
35937 pc
= &OMP_TARGET_CLAUSES (stmt
);
35938 keep_next_level (true);
35939 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35941 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35947 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35948 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35951 case GOMP_MAP_ALWAYS_TO
:
35952 case GOMP_MAP_FROM
:
35953 case GOMP_MAP_ALWAYS_FROM
:
35954 case GOMP_MAP_TOFROM
:
35955 case GOMP_MAP_ALWAYS_TOFROM
:
35956 case GOMP_MAP_ALLOC
:
35957 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35958 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35959 case GOMP_MAP_ALWAYS_POINTER
:
35962 error_at (OMP_CLAUSE_LOCATION (*pc
),
35963 "%<#pragma omp target%> with map-type other "
35964 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35965 "on %<map%> clause");
35966 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35969 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35975 # pragma acc cache (variable-list) new-line
35979 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
35981 tree stmt
, clauses
;
35983 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
35984 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
35986 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
35988 stmt
= make_node (OACC_CACHE
);
35989 TREE_TYPE (stmt
) = void_type_node
;
35990 OACC_CACHE_CLAUSES (stmt
) = clauses
;
35991 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35998 # pragma acc data oacc-data-clause[optseq] new-line
35999 structured-block */
36001 #define OACC_DATA_CLAUSE_MASK \
36002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36015 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36017 tree stmt
, clauses
, block
;
36020 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36021 "#pragma acc data", pragma_tok
);
36023 block
= begin_omp_parallel ();
36024 save
= cp_parser_begin_omp_structured_block (parser
);
36025 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36026 cp_parser_end_omp_structured_block (parser
, save
);
36027 stmt
= finish_oacc_data (clauses
, block
);
36032 # pragma acc host_data <clauses> new-line
36033 structured-block */
36035 #define OACC_HOST_DATA_CLAUSE_MASK \
36036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36039 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36041 tree stmt
, clauses
, block
;
36044 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36045 "#pragma acc host_data", pragma_tok
);
36047 block
= begin_omp_parallel ();
36048 save
= cp_parser_begin_omp_structured_block (parser
);
36049 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36050 cp_parser_end_omp_structured_block (parser
, save
);
36051 stmt
= finish_oacc_host_data (clauses
, block
);
36056 # pragma acc declare oacc-data-clause[optseq] new-line
36059 #define OACC_DECLARE_CLAUSE_MASK \
36060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36074 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36076 tree clauses
, stmt
;
36077 bool error
= false;
36079 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36080 "#pragma acc declare", pragma_tok
, true);
36083 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36085 error_at (pragma_tok
->location
,
36086 "no valid clauses specified in %<#pragma acc declare%>");
36090 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36092 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36093 tree decl
= OMP_CLAUSE_DECL (t
);
36094 if (!DECL_P (decl
))
36096 error_at (loc
, "array section in %<#pragma acc declare%>");
36100 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36101 switch (OMP_CLAUSE_MAP_KIND (t
))
36103 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36104 case GOMP_MAP_FORCE_ALLOC
:
36105 case GOMP_MAP_FORCE_TO
:
36106 case GOMP_MAP_FORCE_DEVICEPTR
:
36107 case GOMP_MAP_DEVICE_RESIDENT
:
36110 case GOMP_MAP_LINK
:
36111 if (!global_bindings_p ()
36112 && (TREE_STATIC (decl
)
36113 || !DECL_EXTERNAL (decl
)))
36116 "%qD must be a global variable in"
36117 "%<#pragma acc declare link%>",
36125 if (global_bindings_p ())
36127 error_at (loc
, "invalid OpenACC clause at file scope");
36131 if (DECL_EXTERNAL (decl
))
36134 "invalid use of %<extern%> variable %qD "
36135 "in %<#pragma acc declare%>", decl
);
36139 else if (TREE_PUBLIC (decl
))
36142 "invalid use of %<global%> variable %qD "
36143 "in %<#pragma acc declare%>", decl
);
36150 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36151 || lookup_attribute ("omp declare target link",
36152 DECL_ATTRIBUTES (decl
)))
36154 error_at (loc
, "variable %qD used more than once with "
36155 "%<#pragma acc declare%>", decl
);
36164 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36165 id
= get_identifier ("omp declare target link");
36167 id
= get_identifier ("omp declare target");
36169 DECL_ATTRIBUTES (decl
)
36170 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36171 if (global_bindings_p ())
36173 symtab_node
*node
= symtab_node::get (decl
);
36176 node
->offloadable
= 1;
36177 if (ENABLE_OFFLOADING
)
36179 g
->have_offload
= true;
36180 if (is_a
<varpool_node
*> (node
))
36181 vec_safe_push (offload_vars
, decl
);
36188 if (error
|| global_bindings_p ())
36191 stmt
= make_node (OACC_DECLARE
);
36192 TREE_TYPE (stmt
) = void_type_node
;
36193 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36194 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36202 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36206 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36208 LOC is the location of the #pragma token.
36211 #define OACC_ENTER_DATA_CLAUSE_MASK \
36212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36220 #define OACC_EXIT_DATA_CLAUSE_MASK \
36221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36228 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36231 location_t loc
= pragma_tok
->location
;
36232 tree stmt
, clauses
;
36233 const char *p
= "";
36235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36236 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36238 if (strcmp (p
, "data") != 0)
36240 error_at (loc
, enter
36241 ? "expected %<data%> after %<#pragma acc enter%>"
36242 : "expected %<data%> after %<#pragma acc exit%>");
36243 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36247 cp_lexer_consume_token (parser
->lexer
);
36250 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36251 "#pragma acc enter data", pragma_tok
);
36253 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36254 "#pragma acc exit data", pragma_tok
);
36256 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36258 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36259 enter
? "enter" : "exit");
36263 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36264 TREE_TYPE (stmt
) = void_type_node
;
36265 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36266 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36272 # pragma acc loop oacc-loop-clause[optseq] new-line
36273 structured-block */
36275 #define OACC_LOOP_CLAUSE_MASK \
36276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36288 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36289 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36291 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36293 strcat (p_name
, " loop");
36294 mask
|= OACC_LOOP_CLAUSE_MASK
;
36296 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36300 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36302 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36304 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36307 tree block
= begin_omp_structured_block ();
36308 int save
= cp_parser_begin_omp_structured_block (parser
);
36309 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36310 cp_parser_end_omp_structured_block (parser
, save
);
36311 add_stmt (finish_omp_structured_block (block
));
36317 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36322 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36326 #define OACC_KERNELS_CLAUSE_MASK \
36327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36342 #define OACC_PARALLEL_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36365 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36366 char *p_name
, bool *if_p
)
36368 omp_clause_mask mask
;
36369 enum tree_code code
;
36370 switch (cp_parser_pragma_kind (pragma_tok
))
36372 case PRAGMA_OACC_KERNELS
:
36373 strcat (p_name
, " kernels");
36374 mask
= OACC_KERNELS_CLAUSE_MASK
;
36375 code
= OACC_KERNELS
;
36377 case PRAGMA_OACC_PARALLEL
:
36378 strcat (p_name
, " parallel");
36379 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36380 code
= OACC_PARALLEL
;
36383 gcc_unreachable ();
36386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36389 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36390 if (strcmp (p
, "loop") == 0)
36392 cp_lexer_consume_token (parser
->lexer
);
36393 tree block
= begin_omp_parallel ();
36395 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36397 return finish_omp_construct (code
, block
, clauses
);
36401 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36403 tree block
= begin_omp_parallel ();
36404 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36405 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36406 cp_parser_end_omp_structured_block (parser
, save
);
36407 return finish_omp_construct (code
, block
, clauses
);
36411 # pragma acc update oacc-update-clause[optseq] new-line
36414 #define OACC_UPDATE_CLAUSE_MASK \
36415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36423 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36425 tree stmt
, clauses
;
36427 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36428 "#pragma acc update", pragma_tok
);
36430 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36432 error_at (pragma_tok
->location
,
36433 "%<#pragma acc update%> must contain at least one "
36434 "%<device%> or %<host%> or %<self%> clause");
36438 stmt
= make_node (OACC_UPDATE
);
36439 TREE_TYPE (stmt
) = void_type_node
;
36440 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36441 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36447 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36449 LOC is the location of the #pragma token.
36452 #define OACC_WAIT_CLAUSE_MASK \
36453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36456 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36458 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36459 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36461 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36462 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36464 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36465 "#pragma acc wait", pragma_tok
);
36467 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36468 stmt
= finish_expr_stmt (stmt
);
36474 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36476 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36485 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36486 enum pragma_context context
)
36488 bool first_p
= parser
->omp_declare_simd
== NULL
;
36489 cp_omp_declare_simd_data data
;
36492 data
.error_seen
= false;
36493 data
.fndecl_seen
= false;
36494 data
.tokens
= vNULL
;
36495 data
.clauses
= NULL_TREE
;
36496 /* It is safe to take the address of a local variable; it will only be
36497 used while this scope is live. */
36498 parser
->omp_declare_simd
= &data
;
36501 /* Store away all pragma tokens. */
36502 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36503 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36504 cp_lexer_consume_token (parser
->lexer
);
36505 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36506 parser
->omp_declare_simd
->error_seen
= true;
36507 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36508 struct cp_token_cache
*cp
36509 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36510 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36514 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36515 cp_parser_pragma (parser
, context
, NULL
);
36518 case pragma_external
:
36519 cp_parser_declaration (parser
);
36521 case pragma_member
:
36522 cp_parser_member_declaration (parser
);
36524 case pragma_objc_icode
:
36525 cp_parser_block_declaration (parser
, /*statement_p=*/false);
36528 cp_parser_declaration_statement (parser
);
36531 if (parser
->omp_declare_simd
36532 && !parser
->omp_declare_simd
->error_seen
36533 && !parser
->omp_declare_simd
->fndecl_seen
)
36534 error_at (pragma_tok
->location
,
36535 "%<#pragma omp declare simd%> not immediately followed by "
36536 "function declaration or definition");
36537 data
.tokens
.release ();
36538 parser
->omp_declare_simd
= NULL
;
36542 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36543 This function is modelled similar to the late parsing of omp declare
36547 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
36549 struct cp_token_cache
*ce
;
36550 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
36553 if (parser
->omp_declare_simd
!= NULL
36554 || lookup_attribute ("simd", attrs
))
36556 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36557 "used in the same function marked as a Cilk Plus SIMD-enabled "
36559 parser
->cilk_simd_fn_info
->tokens
.release ();
36560 XDELETE (parser
->cilk_simd_fn_info
);
36561 parser
->cilk_simd_fn_info
= NULL
;
36564 if (!info
->error_seen
&& info
->fndecl_seen
)
36566 error ("vector attribute not immediately followed by a single function"
36567 " declaration or definition");
36568 info
->error_seen
= true;
36570 if (info
->error_seen
)
36573 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
36577 cp_parser_push_lexer_for_tokens (parser
, ce
);
36578 parser
->lexer
->in_pragma
= true;
36579 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
36580 "SIMD-enabled functions attribute",
36582 cp_parser_pop_lexer (parser
);
36584 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36586 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
36587 TREE_CHAIN (c
) = attrs
;
36590 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36591 TREE_CHAIN (c
) = attrs
;
36592 if (processing_template_decl
)
36593 ATTR_IS_DEPENDENT (c
) = 1;
36596 info
->fndecl_seen
= true;
36597 parser
->cilk_simd_fn_info
->tokens
.release ();
36598 XDELETE (parser
->cilk_simd_fn_info
);
36599 parser
->cilk_simd_fn_info
= NULL
;
36603 /* Finalize #pragma omp declare simd clauses after direct declarator has
36604 been parsed, and put that into "omp declare simd" attribute. */
36607 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
36609 struct cp_token_cache
*ce
;
36610 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
36613 if (!data
->error_seen
&& data
->fndecl_seen
)
36615 error ("%<#pragma omp declare simd%> not immediately followed by "
36616 "a single function declaration or definition");
36617 data
->error_seen
= true;
36619 if (data
->error_seen
)
36622 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
36626 cp_parser_push_lexer_for_tokens (parser
, ce
);
36627 parser
->lexer
->in_pragma
= true;
36628 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
36629 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36630 cp_lexer_consume_token (parser
->lexer
);
36631 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
36632 "#pragma omp declare simd", pragma_tok
);
36633 cp_parser_pop_lexer (parser
);
36635 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
36636 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
36637 TREE_CHAIN (c
) = attrs
;
36638 if (processing_template_decl
)
36639 ATTR_IS_DEPENDENT (c
) = 1;
36643 data
->fndecl_seen
= true;
36649 # pragma omp declare target new-line
36650 declarations and definitions
36651 # pragma omp end declare target new-line
36654 # pragma omp declare target ( extended-list ) new-line
36656 # pragma omp declare target declare-target-clauses[seq] new-line */
36658 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36663 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36665 tree clauses
= NULL_TREE
;
36666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36668 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
36669 "#pragma omp declare target", pragma_tok
);
36670 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36672 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
36674 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36675 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36679 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36680 scope_chain
->omp_declare_target_attribute
++;
36683 if (scope_chain
->omp_declare_target_attribute
)
36684 error_at (pragma_tok
->location
,
36685 "%<#pragma omp declare target%> with clauses in between "
36686 "%<#pragma omp declare target%> without clauses and "
36687 "%<#pragma omp end declare target%>");
36688 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36690 tree t
= OMP_CLAUSE_DECL (c
), id
;
36691 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
36692 tree at2
= lookup_attribute ("omp declare target link",
36693 DECL_ATTRIBUTES (t
));
36694 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
36696 id
= get_identifier ("omp declare target link");
36697 std::swap (at1
, at2
);
36700 id
= get_identifier ("omp declare target");
36703 error_at (OMP_CLAUSE_LOCATION (c
),
36704 "%qD specified both in declare target %<link%> and %<to%>"
36710 symtab_node
*node
= symtab_node::get (t
);
36711 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
36714 node
->offloadable
= 1;
36715 if (ENABLE_OFFLOADING
)
36717 g
->have_offload
= true;
36718 if (is_a
<varpool_node
*> (node
))
36719 vec_safe_push (offload_vars
, t
);
36727 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
36729 const char *p
= "";
36730 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36732 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36733 p
= IDENTIFIER_POINTER (id
);
36735 if (strcmp (p
, "declare") == 0)
36737 cp_lexer_consume_token (parser
->lexer
);
36739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36741 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36742 p
= IDENTIFIER_POINTER (id
);
36744 if (strcmp (p
, "target") == 0)
36745 cp_lexer_consume_token (parser
->lexer
);
36748 cp_parser_error (parser
, "expected %<target%>");
36749 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36755 cp_parser_error (parser
, "expected %<declare%>");
36756 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36759 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36760 if (!scope_chain
->omp_declare_target_attribute
)
36761 error_at (pragma_tok
->location
,
36762 "%<#pragma omp end declare target%> without corresponding "
36763 "%<#pragma omp declare target%>");
36765 scope_chain
->omp_declare_target_attribute
--;
36768 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36769 expression and optional initializer clause of
36770 #pragma omp declare reduction. We store the expression(s) as
36771 either 3, 6 or 7 special statements inside of the artificial function's
36772 body. The first two statements are DECL_EXPRs for the artificial
36773 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36774 expression that uses those variables.
36775 If there was any INITIALIZER clause, this is followed by further statements,
36776 the fourth and fifth statements are DECL_EXPRs for the artificial
36777 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36778 constructor variant (first token after open paren is not omp_priv),
36779 then the sixth statement is a statement with the function call expression
36780 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36781 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36782 to initialize the OMP_PRIV artificial variable and there is seventh
36783 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36786 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
36788 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
36789 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
36790 type
= TREE_TYPE (type
);
36791 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
36792 DECL_ARTIFICIAL (omp_out
) = 1;
36793 pushdecl (omp_out
);
36794 add_decl_expr (omp_out
);
36795 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
36796 DECL_ARTIFICIAL (omp_in
) = 1;
36798 add_decl_expr (omp_in
);
36800 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
36802 keep_next_level (true);
36803 tree block
= begin_omp_structured_block ();
36804 combiner
= cp_parser_expression (parser
);
36805 finish_expr_stmt (combiner
);
36806 block
= finish_omp_structured_block (block
);
36809 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36812 const char *p
= "";
36813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36815 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36816 p
= IDENTIFIER_POINTER (id
);
36819 if (strcmp (p
, "initializer") == 0)
36821 cp_lexer_consume_token (parser
->lexer
);
36822 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36826 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36828 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36829 p
= IDENTIFIER_POINTER (id
);
36832 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
36833 DECL_ARTIFICIAL (omp_priv
) = 1;
36834 pushdecl (omp_priv
);
36835 add_decl_expr (omp_priv
);
36836 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
36837 DECL_ARTIFICIAL (omp_orig
) = 1;
36838 pushdecl (omp_orig
);
36839 add_decl_expr (omp_orig
);
36841 keep_next_level (true);
36842 block
= begin_omp_structured_block ();
36845 if (strcmp (p
, "omp_priv") == 0)
36847 bool is_direct_init
, is_non_constant_init
;
36849 cp_lexer_consume_token (parser
->lexer
);
36850 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
36852 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36853 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
36855 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
36856 == CPP_CLOSE_PAREN
))
36858 finish_omp_structured_block (block
);
36859 error ("invalid initializer clause");
36862 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
36863 &is_non_constant_init
);
36864 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
36865 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
36869 cp_parser_parse_tentatively (parser
);
36870 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36871 /*check_dependency_p=*/true,
36872 /*template_p=*/NULL
,
36873 /*declarator_p=*/false,
36874 /*optional_p=*/false);
36875 vec
<tree
, va_gc
> *args
;
36876 if (fn_name
== error_mark_node
36877 || cp_parser_error_occurred (parser
)
36878 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36879 || ((args
= cp_parser_parenthesized_expression_list
36880 (parser
, non_attr
, /*cast_p=*/false,
36881 /*allow_expansion_p=*/true,
36882 /*non_constant_p=*/NULL
)),
36883 cp_parser_error_occurred (parser
)))
36885 finish_omp_structured_block (block
);
36886 cp_parser_abort_tentative_parse (parser
);
36887 cp_parser_error (parser
, "expected id-expression (arguments)");
36892 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
36893 if (arg
== omp_priv
36894 || (TREE_CODE (arg
) == ADDR_EXPR
36895 && TREE_OPERAND (arg
, 0) == omp_priv
))
36897 cp_parser_abort_tentative_parse (parser
);
36898 if (arg
== NULL_TREE
)
36899 error ("one of the initializer call arguments should be %<omp_priv%>"
36900 " or %<&omp_priv%>");
36901 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
36903 finish_expr_stmt (initializer
);
36906 block
= finish_omp_structured_block (block
);
36907 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
36911 add_decl_expr (omp_orig
);
36913 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36917 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
36918 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
36924 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36925 initializer-clause[opt] new-line
36927 initializer-clause:
36928 initializer (omp_priv initializer)
36929 initializer (function-name (argument-list)) */
36932 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
36933 enum pragma_context
)
36935 auto_vec
<tree
> types
;
36936 enum tree_code reduc_code
= ERROR_MARK
;
36937 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
36939 cp_token
*first_token
;
36940 cp_token_cache
*cp
;
36944 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36945 p
= obstack_alloc (&declarator_obstack
, 0);
36947 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36950 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
36953 reduc_code
= PLUS_EXPR
;
36956 reduc_code
= MULT_EXPR
;
36959 reduc_code
= MINUS_EXPR
;
36962 reduc_code
= BIT_AND_EXPR
;
36965 reduc_code
= BIT_XOR_EXPR
;
36968 reduc_code
= BIT_IOR_EXPR
;
36971 reduc_code
= TRUTH_ANDIF_EXPR
;
36974 reduc_code
= TRUTH_ORIF_EXPR
;
36977 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
36980 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36981 "%<|%>, %<&&%>, %<||%> or identifier");
36985 if (reduc_code
!= ERROR_MARK
)
36986 cp_lexer_consume_token (parser
->lexer
);
36988 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
36989 if (reduc_id
== error_mark_node
)
36992 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36995 /* Types may not be defined in declare reduction type list. */
36996 const char *saved_message
;
36997 saved_message
= parser
->type_definition_forbidden_message
;
36998 parser
->type_definition_forbidden_message
36999 = G_("types may not be defined in declare reduction type list");
37000 bool saved_colon_corrects_to_scope_p
;
37001 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37002 parser
->colon_corrects_to_scope_p
= false;
37003 bool saved_colon_doesnt_start_class_def_p
;
37004 saved_colon_doesnt_start_class_def_p
37005 = parser
->colon_doesnt_start_class_def_p
;
37006 parser
->colon_doesnt_start_class_def_p
= true;
37010 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37011 type
= cp_parser_type_id (parser
);
37012 if (type
== error_mark_node
)
37014 else if (ARITHMETIC_TYPE_P (type
)
37015 && (orig_reduc_id
== NULL_TREE
37016 || (TREE_CODE (type
) != COMPLEX_TYPE
37017 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37019 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
37021 error_at (loc
, "predeclared arithmetic type %qT in "
37022 "%<#pragma omp declare reduction%>", type
);
37023 else if (TREE_CODE (type
) == FUNCTION_TYPE
37024 || TREE_CODE (type
) == METHOD_TYPE
37025 || TREE_CODE (type
) == ARRAY_TYPE
)
37026 error_at (loc
, "function or array type %qT in "
37027 "%<#pragma omp declare reduction%>", type
);
37028 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37029 error_at (loc
, "reference type %qT in "
37030 "%<#pragma omp declare reduction%>", type
);
37031 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37032 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37033 "%<#pragma omp declare reduction%>", type
);
37035 types
.safe_push (type
);
37037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37038 cp_lexer_consume_token (parser
->lexer
);
37043 /* Restore the saved message. */
37044 parser
->type_definition_forbidden_message
= saved_message
;
37045 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37046 parser
->colon_doesnt_start_class_def_p
37047 = saved_colon_doesnt_start_class_def_p
;
37049 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37050 || types
.is_empty ())
37053 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37057 first_token
= cp_lexer_peek_token (parser
->lexer
);
37060 FOR_EACH_VEC_ELT (types
, i
, type
)
37063 = build_function_type_list (void_type_node
,
37064 cp_build_reference_type (type
, false),
37066 tree this_reduc_id
= reduc_id
;
37067 if (!dependent_type_p (type
))
37068 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37069 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37070 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37071 DECL_ARTIFICIAL (fndecl
) = 1;
37072 DECL_EXTERNAL (fndecl
) = 1;
37073 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37074 DECL_IGNORED_P (fndecl
) = 1;
37075 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37076 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37077 DECL_ATTRIBUTES (fndecl
)
37078 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37079 DECL_ATTRIBUTES (fndecl
));
37080 if (processing_template_decl
)
37081 fndecl
= push_template_decl (fndecl
);
37082 bool block_scope
= false;
37083 tree block
= NULL_TREE
;
37084 if (current_function_decl
)
37086 block_scope
= true;
37087 DECL_CONTEXT (fndecl
) = global_namespace
;
37088 if (!processing_template_decl
)
37091 else if (current_class_type
)
37095 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37096 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37097 cp_lexer_consume_token (parser
->lexer
);
37098 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37100 cp
= cp_token_cache_new (first_token
,
37101 cp_lexer_peek_nth_token (parser
->lexer
,
37104 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37105 finish_member_declaration (fndecl
);
37106 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37107 DECL_PENDING_INLINE_P (fndecl
) = 1;
37108 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37113 DECL_CONTEXT (fndecl
) = current_namespace
;
37117 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37119 block
= begin_omp_structured_block ();
37122 cp_parser_push_lexer_for_tokens (parser
, cp
);
37123 parser
->lexer
->in_pragma
= true;
37125 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37128 finish_function (0);
37130 DECL_CONTEXT (fndecl
) = current_function_decl
;
37132 cp_parser_pop_lexer (parser
);
37136 cp_parser_pop_lexer (parser
);
37138 finish_function (0);
37141 DECL_CONTEXT (fndecl
) = current_function_decl
;
37142 block
= finish_omp_structured_block (block
);
37143 if (TREE_CODE (block
) == BIND_EXPR
)
37144 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37145 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37146 DECL_SAVED_TREE (fndecl
) = block
;
37147 if (processing_template_decl
)
37148 add_decl_expr (fndecl
);
37150 cp_check_omp_declare_reduction (fndecl
);
37151 if (cp
== NULL
&& types
.length () > 1)
37152 cp
= cp_token_cache_new (first_token
,
37153 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37154 if (errs
!= errorcount
)
37158 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37161 /* Free any declarators allocated. */
37162 obstack_free (&declarator_obstack
, p
);
37166 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37167 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37168 initializer-clause[opt] new-line
37169 #pragma omp declare target new-line */
37172 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37173 enum pragma_context context
)
37175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37177 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37178 const char *p
= IDENTIFIER_POINTER (id
);
37180 if (strcmp (p
, "simd") == 0)
37182 cp_lexer_consume_token (parser
->lexer
);
37183 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37187 cp_ensure_no_omp_declare_simd (parser
);
37188 if (strcmp (p
, "reduction") == 0)
37190 cp_lexer_consume_token (parser
->lexer
);
37191 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37195 if (!flag_openmp
) /* flag_openmp_simd */
37197 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37200 if (strcmp (p
, "target") == 0)
37202 cp_lexer_consume_token (parser
->lexer
);
37203 cp_parser_omp_declare_target (parser
, pragma_tok
);
37207 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37209 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37213 #pragma omp taskloop taskloop-clause[optseq] new-line
37216 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37219 #define OMP_TASKLOOP_CLAUSE_MASK \
37220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37236 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37237 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37240 tree clauses
, sb
, ret
;
37242 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37244 strcat (p_name
, " taskloop");
37245 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37249 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37250 const char *p
= IDENTIFIER_POINTER (id
);
37252 if (strcmp (p
, "simd") == 0)
37254 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37255 if (cclauses
== NULL
)
37256 cclauses
= cclauses_buf
;
37258 cp_lexer_consume_token (parser
->lexer
);
37259 if (!flag_openmp
) /* flag_openmp_simd */
37260 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37262 sb
= begin_omp_structured_block ();
37263 save
= cp_parser_begin_omp_structured_block (parser
);
37264 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37266 cp_parser_end_omp_structured_block (parser
, save
);
37267 tree body
= finish_omp_structured_block (sb
);
37270 ret
= make_node (OMP_TASKLOOP
);
37271 TREE_TYPE (ret
) = void_type_node
;
37272 OMP_FOR_BODY (ret
) = body
;
37273 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37274 SET_EXPR_LOCATION (ret
, loc
);
37279 if (!flag_openmp
) /* flag_openmp_simd */
37281 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37285 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37289 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37290 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37293 sb
= begin_omp_structured_block ();
37294 save
= cp_parser_begin_omp_structured_block (parser
);
37296 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37299 cp_parser_end_omp_structured_block (parser
, save
);
37300 add_stmt (finish_omp_structured_block (sb
));
37307 # pragma acc routine oacc-routine-clause[optseq] new-line
37308 function-definition
37310 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37313 #define OACC_ROUTINE_CLAUSE_MASK \
37314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37320 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37321 component, which must resolve to a declared namespace-scope
37322 function. The clauses are either processed directly (for a named
37323 function), or defered until the immediatley following declaration
37327 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37328 enum pragma_context context
)
37330 gcc_checking_assert (context
== pragma_external
);
37331 /* The checking for "another pragma following this one" in the "no optional
37332 '( name )'" case makes sure that we dont re-enter. */
37333 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37335 cp_oacc_routine_data data
;
37336 data
.error_seen
= false;
37337 data
.fndecl_seen
= false;
37338 data
.tokens
= vNULL
;
37339 data
.clauses
= NULL_TREE
;
37340 data
.loc
= pragma_tok
->location
;
37341 /* It is safe to take the address of a local variable; it will only be
37342 used while this scope is live. */
37343 parser
->oacc_routine
= &data
;
37345 /* Look for optional '( name )'. */
37346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37348 cp_lexer_consume_token (parser
->lexer
); /* '(' */
37350 /* We parse the name as an id-expression. If it resolves to
37351 anything other than a non-overloaded function at namespace
37352 scope, it's an error. */
37353 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37354 tree name
= cp_parser_id_expression (parser
,
37355 /*template_keyword_p=*/false,
37356 /*check_dependency_p=*/false,
37357 /*template_p=*/NULL
,
37358 /*declarator_p=*/false,
37359 /*optional_p=*/false);
37360 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37361 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37362 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37364 if (decl
== error_mark_node
37365 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37367 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37368 parser
->oacc_routine
= NULL
;
37373 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37374 "#pragma acc routine",
37375 cp_lexer_peek_token (parser
->lexer
));
37377 if (decl
&& is_overloaded_fn (decl
)
37378 && (TREE_CODE (decl
) != FUNCTION_DECL
37379 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37381 error_at (name_loc
,
37382 "%<#pragma acc routine%> names a set of overloads");
37383 parser
->oacc_routine
= NULL
;
37387 /* Perhaps we should use the same rule as declarations in different
37389 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37391 error_at (name_loc
,
37392 "%qD does not refer to a namespace scope function", decl
);
37393 parser
->oacc_routine
= NULL
;
37397 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37399 error_at (name_loc
, "%qD does not refer to a function", decl
);
37400 parser
->oacc_routine
= NULL
;
37404 cp_finalize_oacc_routine (parser
, decl
, false);
37405 parser
->oacc_routine
= NULL
;
37407 else /* No optional '( name )'. */
37409 /* Store away all pragma tokens. */
37410 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37411 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37412 cp_lexer_consume_token (parser
->lexer
);
37413 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37414 parser
->oacc_routine
->error_seen
= true;
37415 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37416 struct cp_token_cache
*cp
37417 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37418 parser
->oacc_routine
->tokens
.safe_push (cp
);
37420 /* Emit a helpful diagnostic if there's another pragma following this
37422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37424 cp_ensure_no_oacc_routine (parser
);
37425 data
.tokens
.release ();
37426 /* ..., and then just keep going. */
37430 /* We only have to consider the pragma_external case here. */
37431 cp_parser_declaration (parser
);
37432 if (parser
->oacc_routine
37433 && !parser
->oacc_routine
->fndecl_seen
)
37434 cp_ensure_no_oacc_routine (parser
);
37436 parser
->oacc_routine
= NULL
;
37437 data
.tokens
.release ();
37441 /* Finalize #pragma acc routine clauses after direct declarator has
37445 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37447 struct cp_token_cache
*ce
;
37448 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37450 if (!data
->error_seen
&& data
->fndecl_seen
)
37452 error_at (data
->loc
,
37453 "%<#pragma acc routine%> not immediately followed by "
37454 "a single function declaration or definition");
37455 data
->error_seen
= true;
37457 if (data
->error_seen
)
37460 gcc_checking_assert (data
->tokens
.length () == 1);
37461 ce
= data
->tokens
[0];
37463 cp_parser_push_lexer_for_tokens (parser
, ce
);
37464 parser
->lexer
->in_pragma
= true;
37465 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37467 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37468 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37469 parser
->oacc_routine
->clauses
37470 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37471 "#pragma acc routine", pragma_tok
);
37472 cp_parser_pop_lexer (parser
);
37473 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37479 /* Apply any saved OpenACC routine clauses to a just-parsed
37483 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37485 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37487 /* Keep going if we're in error reporting mode. */
37488 if (parser
->oacc_routine
->error_seen
37489 || fndecl
== error_mark_node
)
37492 if (parser
->oacc_routine
->fndecl_seen
)
37494 error_at (parser
->oacc_routine
->loc
,
37495 "%<#pragma acc routine%> not immediately followed by"
37496 " a single function declaration or definition");
37497 parser
->oacc_routine
= NULL
;
37500 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37502 cp_ensure_no_oacc_routine (parser
);
37506 if (oacc_get_fn_attrib (fndecl
))
37508 error_at (parser
->oacc_routine
->loc
,
37509 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37510 parser
->oacc_routine
= NULL
;
37514 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37516 error_at (parser
->oacc_routine
->loc
,
37517 "%<#pragma acc routine%> must be applied before %s",
37518 TREE_USED (fndecl
) ? "use" : "definition");
37519 parser
->oacc_routine
= NULL
;
37523 /* Process the routine's dimension clauses. */
37524 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37525 oacc_replace_fn_attrib (fndecl
, dims
);
37527 /* Add an "omp declare target" attribute. */
37528 DECL_ATTRIBUTES (fndecl
)
37529 = tree_cons (get_identifier ("omp declare target"),
37530 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37532 /* Don't unset parser->oacc_routine here: we may still need it to
37533 diagnose wrong usage. But, remember that we've used this "#pragma acc
37535 parser
->oacc_routine
->fndecl_seen
= true;
37539 /* Main entry point to OpenMP statement pragmas. */
37542 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37545 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37546 omp_clause_mask
mask (0);
37548 switch (cp_parser_pragma_kind (pragma_tok
))
37550 case PRAGMA_OACC_ATOMIC
:
37551 cp_parser_omp_atomic (parser
, pragma_tok
);
37553 case PRAGMA_OACC_CACHE
:
37554 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37556 case PRAGMA_OACC_DATA
:
37557 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37559 case PRAGMA_OACC_ENTER_DATA
:
37560 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37562 case PRAGMA_OACC_EXIT_DATA
:
37563 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
37565 case PRAGMA_OACC_HOST_DATA
:
37566 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
37568 case PRAGMA_OACC_KERNELS
:
37569 case PRAGMA_OACC_PARALLEL
:
37570 strcpy (p_name
, "#pragma acc");
37571 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
37574 case PRAGMA_OACC_LOOP
:
37575 strcpy (p_name
, "#pragma acc");
37576 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37579 case PRAGMA_OACC_UPDATE
:
37580 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
37582 case PRAGMA_OACC_WAIT
:
37583 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
37585 case PRAGMA_OMP_ATOMIC
:
37586 cp_parser_omp_atomic (parser
, pragma_tok
);
37588 case PRAGMA_OMP_CRITICAL
:
37589 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
37591 case PRAGMA_OMP_DISTRIBUTE
:
37592 strcpy (p_name
, "#pragma omp");
37593 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
37596 case PRAGMA_OMP_FOR
:
37597 strcpy (p_name
, "#pragma omp");
37598 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
37601 case PRAGMA_OMP_MASTER
:
37602 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
37604 case PRAGMA_OMP_PARALLEL
:
37605 strcpy (p_name
, "#pragma omp");
37606 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
37609 case PRAGMA_OMP_SECTIONS
:
37610 strcpy (p_name
, "#pragma omp");
37611 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
37613 case PRAGMA_OMP_SIMD
:
37614 strcpy (p_name
, "#pragma omp");
37615 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
37618 case PRAGMA_OMP_SINGLE
:
37619 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
37621 case PRAGMA_OMP_TASK
:
37622 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
37624 case PRAGMA_OMP_TASKGROUP
:
37625 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
37627 case PRAGMA_OMP_TASKLOOP
:
37628 strcpy (p_name
, "#pragma omp");
37629 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
37632 case PRAGMA_OMP_TEAMS
:
37633 strcpy (p_name
, "#pragma omp");
37634 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
37638 gcc_unreachable ();
37641 protected_set_expr_location (stmt
, pragma_tok
->location
);
37644 /* Transactional Memory parsing routines. */
37646 /* Parse a transaction attribute.
37652 We use this instead of cp_parser_attributes_opt for transactions to avoid
37653 the pedwarn in C++98 mode. */
37656 cp_parser_txn_attribute_opt (cp_parser
*parser
)
37659 tree attr_name
, attr
= NULL
;
37661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
37662 return cp_parser_attributes_opt (parser
);
37664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
37666 cp_lexer_consume_token (parser
->lexer
);
37667 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
37670 token
= cp_lexer_peek_token (parser
->lexer
);
37671 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
37673 token
= cp_lexer_consume_token (parser
->lexer
);
37675 attr_name
= (token
->type
== CPP_KEYWORD
37676 /* For keywords, use the canonical spelling,
37677 not the parsed identifier. */
37678 ? ridpointers
[(int) token
->keyword
]
37680 attr
= build_tree_list (attr_name
, NULL_TREE
);
37683 cp_parser_error (parser
, "expected identifier");
37685 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37687 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
37691 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37693 transaction-statement:
37694 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37696 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37700 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
37702 unsigned char old_in
= parser
->in_transaction
;
37703 unsigned char this_in
= 1, new_in
;
37704 enum rid keyword
= token
->keyword
;
37705 tree stmt
, attrs
, noex
;
37707 cp_lexer_consume_token (parser
->lexer
);
37709 if (keyword
== RID_TRANSACTION_RELAXED
37710 || keyword
== RID_SYNCHRONIZED
)
37711 this_in
|= TM_STMT_ATTR_RELAXED
;
37714 attrs
= cp_parser_txn_attribute_opt (parser
);
37716 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37719 /* Parse a noexcept specification. */
37720 if (keyword
== RID_ATOMIC_NOEXCEPT
)
37721 noex
= boolean_true_node
;
37722 else if (keyword
== RID_ATOMIC_CANCEL
)
37724 /* cancel-and-throw is unimplemented. */
37725 sorry ("atomic_cancel");
37729 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
37731 /* Keep track if we're in the lexical scope of an outer transaction. */
37732 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
37734 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
37736 parser
->in_transaction
= new_in
;
37737 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
37738 parser
->in_transaction
= old_in
;
37740 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
37745 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37747 transaction-expression:
37748 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37749 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37753 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
37755 unsigned char old_in
= parser
->in_transaction
;
37756 unsigned char this_in
= 1;
37760 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37762 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37763 || keyword
== RID_TRANSACTION_RELAXED
);
37767 keyword
== RID_TRANSACTION_RELAXED
37768 ? G_("%<__transaction_relaxed%> without transactional memory "
37770 : G_("%<__transaction_atomic%> without transactional memory "
37771 "support enabled"));
37773 token
= cp_parser_require_keyword (parser
, keyword
,
37774 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37775 : RT_TRANSACTION_RELAXED
));
37776 gcc_assert (token
!= NULL
);
37778 if (keyword
== RID_TRANSACTION_RELAXED
)
37779 this_in
|= TM_STMT_ATTR_RELAXED
;
37781 /* Set this early. This might mean that we allow transaction_cancel in
37782 an expression that we find out later actually has to be a constexpr.
37783 However, we expect that cxx_constant_value will be able to deal with
37784 this; also, if the noexcept has no constexpr, then what we parse next
37785 really is a transaction's body. */
37786 parser
->in_transaction
= this_in
;
37788 /* Parse a noexcept specification. */
37789 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
37792 if (!noex
|| !noex_expr
37793 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37795 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
37797 expr
= cp_parser_expression (parser
);
37798 expr
= finish_parenthesized_expr (expr
);
37800 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
37804 /* The only expression that is available got parsed for the noexcept
37805 already. noexcept is true then. */
37807 noex
= boolean_true_node
;
37810 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
37811 parser
->in_transaction
= old_in
;
37813 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
37814 return error_mark_node
;
37816 return (flag_tm
? expr
: error_mark_node
);
37819 /* Parse a function-transaction-block.
37821 function-transaction-block:
37822 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37824 __transaction_atomic txn-attribute[opt] function-try-block
37825 __transaction_relaxed ctor-initializer[opt] function-body
37826 __transaction_relaxed function-try-block
37830 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
37832 unsigned char old_in
= parser
->in_transaction
;
37833 unsigned char new_in
= 1;
37834 tree compound_stmt
, stmt
, attrs
;
37835 bool ctor_initializer_p
;
37838 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37839 || keyword
== RID_TRANSACTION_RELAXED
);
37840 token
= cp_parser_require_keyword (parser
, keyword
,
37841 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37842 : RT_TRANSACTION_RELAXED
));
37843 gcc_assert (token
!= NULL
);
37845 if (keyword
== RID_TRANSACTION_RELAXED
)
37846 new_in
|= TM_STMT_ATTR_RELAXED
;
37849 attrs
= cp_parser_txn_attribute_opt (parser
);
37851 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37854 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
37856 parser
->in_transaction
= new_in
;
37858 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
37859 ctor_initializer_p
= cp_parser_function_try_block (parser
);
37861 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
37862 (parser
, /*in_function_try_block=*/false);
37864 parser
->in_transaction
= old_in
;
37866 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
37868 return ctor_initializer_p
;
37871 /* Parse a __transaction_cancel statement.
37874 __transaction_cancel txn-attribute[opt] ;
37875 __transaction_cancel txn-attribute[opt] throw-expression ;
37877 ??? Cancel and throw is not yet implemented. */
37880 cp_parser_transaction_cancel (cp_parser
*parser
)
37883 bool is_outer
= false;
37886 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
37887 RT_TRANSACTION_CANCEL
);
37888 gcc_assert (token
!= NULL
);
37890 attrs
= cp_parser_txn_attribute_opt (parser
);
37892 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
37894 /* ??? Parse cancel-and-throw here. */
37896 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37900 error_at (token
->location
, "%<__transaction_cancel%> without "
37901 "transactional memory support enabled");
37902 return error_mark_node
;
37904 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
37906 error_at (token
->location
, "%<__transaction_cancel%> within a "
37907 "%<__transaction_relaxed%>");
37908 return error_mark_node
;
37912 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
37913 && !is_tm_may_cancel_outer (current_function_decl
))
37915 error_at (token
->location
, "outer %<__transaction_cancel%> not "
37916 "within outer %<__transaction_atomic%>");
37917 error_at (token
->location
,
37918 " or a %<transaction_may_cancel_outer%> function");
37919 return error_mark_node
;
37922 else if (parser
->in_transaction
== 0)
37924 error_at (token
->location
, "%<__transaction_cancel%> not within "
37925 "%<__transaction_atomic%>");
37926 return error_mark_node
;
37929 stmt
= build_tm_abort_call (token
->location
, is_outer
);
37937 static GTY (()) cp_parser
*the_parser
;
37940 /* Special handling for the first token or line in the file. The first
37941 thing in the file might be #pragma GCC pch_preprocess, which loads a
37942 PCH file, which is a GC collection point. So we need to handle this
37943 first pragma without benefit of an existing lexer structure.
37945 Always returns one token to the caller in *FIRST_TOKEN. This is
37946 either the true first token of the file, or the first token after
37947 the initial pragma. */
37950 cp_parser_initial_pragma (cp_token
*first_token
)
37954 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37955 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
37958 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37959 if (first_token
->type
== CPP_STRING
)
37961 name
= first_token
->u
.value
;
37963 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37964 if (first_token
->type
!= CPP_PRAGMA_EOL
)
37965 error_at (first_token
->location
,
37966 "junk at end of %<#pragma GCC pch_preprocess%>");
37969 error_at (first_token
->location
, "expected string literal");
37971 /* Skip to the end of the pragma. */
37972 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
37973 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37975 /* Now actually load the PCH file. */
37977 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
37979 /* Read one more token to return to our caller. We have to do this
37980 after reading the PCH file in, since its pointers have to be
37982 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37985 /* Parses the grainsize pragma for the _Cilk_for statement.
37987 #pragma cilk grainsize = <VALUE>. */
37990 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37992 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37994 tree exp
= cp_parser_binary_expression (parser
, false, false,
37995 PREC_NOT_OPERATOR
, NULL
);
37996 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37997 if (!exp
|| exp
== error_mark_node
)
37999 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
38003 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38004 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
38005 cp_parser_cilk_for (parser
, exp
, if_p
);
38007 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
38008 "%<#pragma cilk grainsize%> is not followed by "
38012 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38015 /* Normal parsing of a pragma token. Here we can (and must) use the
38019 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38021 cp_token
*pragma_tok
;
38026 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38027 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38028 parser
->lexer
->in_pragma
= true;
38030 id
= cp_parser_pragma_kind (pragma_tok
);
38031 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38032 cp_ensure_no_omp_declare_simd (parser
);
38035 case PRAGMA_GCC_PCH_PREPROCESS
:
38036 error_at (pragma_tok
->location
,
38037 "%<#pragma GCC pch_preprocess%> must be first");
38040 case PRAGMA_OMP_BARRIER
:
38043 case pragma_compound
:
38044 cp_parser_omp_barrier (parser
, pragma_tok
);
38047 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
38048 "used in compound statements");
38055 case PRAGMA_OMP_FLUSH
:
38058 case pragma_compound
:
38059 cp_parser_omp_flush (parser
, pragma_tok
);
38062 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
38063 "used in compound statements");
38070 case PRAGMA_OMP_TASKWAIT
:
38073 case pragma_compound
:
38074 cp_parser_omp_taskwait (parser
, pragma_tok
);
38077 error_at (pragma_tok
->location
,
38078 "%<#pragma omp taskwait%> may only be "
38079 "used in compound statements");
38086 case PRAGMA_OMP_TASKYIELD
:
38089 case pragma_compound
:
38090 cp_parser_omp_taskyield (parser
, pragma_tok
);
38093 error_at (pragma_tok
->location
,
38094 "%<#pragma omp taskyield%> may only be "
38095 "used in compound statements");
38102 case PRAGMA_OMP_CANCEL
:
38105 case pragma_compound
:
38106 cp_parser_omp_cancel (parser
, pragma_tok
);
38109 error_at (pragma_tok
->location
,
38110 "%<#pragma omp cancel%> may only be "
38111 "used in compound statements");
38118 case PRAGMA_OMP_CANCELLATION_POINT
:
38119 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38122 case PRAGMA_OMP_THREADPRIVATE
:
38123 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38126 case PRAGMA_OMP_DECLARE
:
38127 cp_parser_omp_declare (parser
, pragma_tok
, context
);
38130 case PRAGMA_OACC_DECLARE
:
38131 cp_parser_oacc_declare (parser
, pragma_tok
);
38134 case PRAGMA_OACC_ENTER_DATA
:
38135 if (context
== pragma_stmt
)
38137 cp_parser_error (parser
, "%<#pragma acc enter data%> may only be "
38138 "used in compound statements");
38141 else if (context
!= pragma_compound
)
38143 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38146 case PRAGMA_OACC_EXIT_DATA
:
38147 if (context
== pragma_stmt
)
38149 cp_parser_error (parser
, "%<#pragma acc exit data%> may only be "
38150 "used in compound statements");
38153 else if (context
!= pragma_compound
)
38155 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38158 case PRAGMA_OACC_ROUTINE
:
38159 if (context
!= pragma_external
)
38161 error_at (pragma_tok
->location
,
38162 "%<#pragma acc routine%> must be at file scope");
38165 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38168 case PRAGMA_OACC_UPDATE
:
38169 if (context
== pragma_stmt
)
38171 cp_parser_error (parser
, "%<#pragma acc update%> may only be "
38172 "used in compound statements");
38175 else if (context
!= pragma_compound
)
38177 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38180 case PRAGMA_OACC_WAIT
:
38181 if (context
== pragma_stmt
)
38183 cp_parser_error (parser
, "%<#pragma acc wait%> may only be "
38184 "used in compound statements");
38187 else if (context
!= pragma_compound
)
38189 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38192 case PRAGMA_OACC_ATOMIC
:
38193 case PRAGMA_OACC_CACHE
:
38194 case PRAGMA_OACC_DATA
:
38195 case PRAGMA_OACC_HOST_DATA
:
38196 case PRAGMA_OACC_KERNELS
:
38197 case PRAGMA_OACC_PARALLEL
:
38198 case PRAGMA_OACC_LOOP
:
38199 case PRAGMA_OMP_ATOMIC
:
38200 case PRAGMA_OMP_CRITICAL
:
38201 case PRAGMA_OMP_DISTRIBUTE
:
38202 case PRAGMA_OMP_FOR
:
38203 case PRAGMA_OMP_MASTER
:
38204 case PRAGMA_OMP_PARALLEL
:
38205 case PRAGMA_OMP_SECTIONS
:
38206 case PRAGMA_OMP_SIMD
:
38207 case PRAGMA_OMP_SINGLE
:
38208 case PRAGMA_OMP_TASK
:
38209 case PRAGMA_OMP_TASKGROUP
:
38210 case PRAGMA_OMP_TASKLOOP
:
38211 case PRAGMA_OMP_TEAMS
:
38212 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38214 stmt
= push_omp_privatization_clauses (false);
38215 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38216 pop_omp_privatization_clauses (stmt
);
38219 case PRAGMA_OMP_ORDERED
:
38220 stmt
= push_omp_privatization_clauses (false);
38221 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38222 pop_omp_privatization_clauses (stmt
);
38225 case PRAGMA_OMP_TARGET
:
38226 stmt
= push_omp_privatization_clauses (false);
38227 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38228 pop_omp_privatization_clauses (stmt
);
38231 case PRAGMA_OMP_END_DECLARE_TARGET
:
38232 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38235 case PRAGMA_OMP_SECTION
:
38236 error_at (pragma_tok
->location
,
38237 "%<#pragma omp section%> may only be used in "
38238 "%<#pragma omp sections%> construct");
38243 if (context
== pragma_external
)
38245 error_at (pragma_tok
->location
,
38246 "%<#pragma GCC ivdep%> must be inside a function");
38249 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38251 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38252 if (tok
->type
!= CPP_KEYWORD
38253 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
38254 && tok
->keyword
!= RID_DO
))
38256 cp_parser_error (parser
, "for, while or do statement expected");
38259 cp_parser_iteration_statement (parser
, if_p
, true);
38263 case PRAGMA_CILK_SIMD
:
38264 if (context
== pragma_external
)
38266 error_at (pragma_tok
->location
,
38267 "%<#pragma simd%> must be inside a function");
38270 stmt
= push_omp_privatization_clauses (false);
38271 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
38272 pop_omp_privatization_clauses (stmt
);
38275 case PRAGMA_CILK_GRAINSIZE
:
38276 if (context
== pragma_external
)
38278 error_at (pragma_tok
->location
,
38279 "%<#pragma cilk grainsize%> must be inside a function");
38283 /* Ignore the pragma if Cilk Plus is not enabled. */
38286 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
38291 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
38292 "%<#pragma cilk grainsize%>");
38297 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38298 c_invoke_pragma_handler (id
);
38302 cp_parser_error (parser
, "expected declaration specifiers");
38306 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38310 /* The interface the pragma parsers have to the lexer. */
38313 pragma_lex (tree
*value
, location_t
*loc
)
38315 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38316 enum cpp_ttype ret
= tok
->type
;
38318 *value
= tok
->u
.value
;
38320 *loc
= tok
->location
;
38322 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38324 else if (ret
== CPP_STRING
)
38325 *value
= cp_parser_string_literal (the_parser
, false, false);
38328 if (ret
== CPP_KEYWORD
)
38330 cp_lexer_consume_token (the_parser
->lexer
);
38337 /* External interface. */
38339 /* Parse one entire translation unit. */
38342 c_parse_file (void)
38344 static bool already_called
= false;
38346 if (already_called
)
38347 fatal_error (input_location
,
38348 "inter-module optimizations not implemented for C++");
38349 already_called
= true;
38351 the_parser
= cp_parser_new ();
38352 push_deferring_access_checks (flag_access_control
38353 ? dk_no_deferred
: dk_no_check
);
38354 cp_parser_translation_unit (the_parser
);
38358 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38359 vectorlength clause:
38361 vectorlength ( constant-expression ) */
38364 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
38367 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38369 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38370 safelen clause. Thus, vectorlength is represented as OMP 4.0
38371 safelen. For SIMD-enabled function it is represented by OMP 4.0
38374 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
38377 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
38380 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38381 return error_mark_node
;
38383 expr
= cp_parser_constant_expression (parser
);
38384 expr
= maybe_constant_value (expr
);
38386 /* If expr == error_mark_node, then don't emit any errors nor
38387 create a clause. if any of the above functions returns
38388 error mark node then they would have emitted an error message. */
38389 if (expr
== error_mark_node
)
38391 else if (!TREE_TYPE (expr
)
38392 || !TREE_CONSTANT (expr
)
38393 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
38394 error_at (loc
, "vectorlength must be an integer constant");
38395 else if (TREE_CONSTANT (expr
)
38396 && !pow2p_hwi (TREE_INT_CST_LOW (expr
)))
38397 error_at (loc
, "vectorlength must be a power of 2");
38403 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
38404 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
38405 OMP_CLAUSE_CHAIN (c
) = clauses
;
38410 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
38411 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
38412 OMP_CLAUSE_CHAIN (c
) = clauses
;
38417 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
38418 return error_mark_node
;
38422 /* Handles the Cilk Plus #pragma simd linear clause.
38424 linear ( simd-linear-variable-list )
38426 simd-linear-variable-list:
38427 simd-linear-variable
38428 simd-linear-variable-list , simd-linear-variable
38430 simd-linear-variable:
38432 id-expression : simd-linear-step
38435 conditional-expression */
38438 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
38440 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38442 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
38444 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38446 cp_parser_error (parser
, "expected identifier");
38447 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38448 return error_mark_node
;
38451 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
38452 parser
->colon_corrects_to_scope_p
= false;
38455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38456 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
38458 cp_parser_error (parser
, "expected variable-name");
38459 clauses
= error_mark_node
;
38463 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
38465 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
38467 if (decl
== error_mark_node
)
38469 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
38471 clauses
= error_mark_node
;
38475 tree e
= NULL_TREE
;
38476 tree step_size
= integer_one_node
;
38478 /* If present, parse the linear step. Otherwise, assume the default
38480 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
38482 cp_lexer_consume_token (parser
->lexer
);
38484 e
= cp_parser_assignment_expression (parser
);
38485 e
= maybe_constant_value (e
);
38487 if (e
== error_mark_node
)
38489 /* If an error has occurred, then the whole pragma is
38490 considered ill-formed. Thus, no reason to keep
38492 clauses
= error_mark_node
;
38495 else if (type_dependent_expression_p (e
)
38496 || value_dependent_expression_p (e
)
38498 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
38499 && (TREE_CONSTANT (e
)
38503 cp_parser_error (parser
,
38504 "step size must be an integer constant "
38505 "expression or an integer variable");
38508 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38509 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
38510 OMP_CLAUSE_DECL (l
) = decl
;
38511 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
38512 OMP_CLAUSE_CHAIN (l
) = clauses
;
38515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38516 cp_lexer_consume_token (parser
->lexer
);
38517 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38521 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38522 "expected %<,%> or %<)%> after %qE", decl
);
38523 clauses
= error_mark_node
;
38527 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
38528 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
38532 /* Returns the name of the next clause. If the clause is not
38533 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38534 token is not consumed. Otherwise, the appropriate enum from the
38535 pragma_simd_clause is returned and the token is consumed. */
38537 static pragma_omp_clause
38538 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
38540 pragma_omp_clause clause_type
;
38541 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38543 if (token
->keyword
== RID_PRIVATE
)
38544 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
38545 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
38546 return PRAGMA_CILK_CLAUSE_NONE
;
38547 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
38548 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
38549 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
38550 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
38551 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
38552 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
38553 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
38554 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
38555 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
38556 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
38558 return PRAGMA_CILK_CLAUSE_NONE
;
38560 cp_lexer_consume_token (parser
->lexer
);
38561 return clause_type
;
38564 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38567 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
38569 tree clauses
= NULL_TREE
;
38571 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38572 && clauses
!= error_mark_node
)
38574 pragma_omp_clause c_kind
;
38575 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
38576 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
38577 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
38578 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
38579 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
38580 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
38581 /* Use the OpenMP 4.0 equivalent function. */
38582 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
38583 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
38584 /* Use the OpenMP 4.0 equivalent function. */
38585 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
38587 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
38588 /* Use the OMP 4.0 equivalent function. */
38589 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
38591 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
38592 /* Use the OMP 4.0 equivalent function. */
38593 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
38596 clauses
= error_mark_node
;
38597 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
38602 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
38604 if (clauses
== error_mark_node
)
38605 return error_mark_node
;
38607 return finish_omp_clauses (clauses
, C_ORT_CILK
);
38610 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38613 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
38615 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
38617 if (clauses
== error_mark_node
)
38620 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
38622 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38623 "for statement expected");
38627 tree sb
= begin_omp_structured_block ();
38628 int save
= cp_parser_begin_omp_structured_block (parser
);
38629 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
38631 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
38632 cp_parser_end_omp_structured_block (parser
, save
);
38633 add_stmt (finish_omp_structured_block (sb
));
38636 /* Main entry-point for parsing Cilk Plus _Cilk_for
38637 loops. The return value is error_mark_node
38638 when errors happen and CILK_FOR tree on success. */
38641 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
38643 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
38644 gcc_unreachable ();
38646 tree sb
= begin_omp_structured_block ();
38647 int save
= cp_parser_begin_omp_structured_block (parser
);
38649 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
38650 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
38651 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
38652 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
38654 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
38656 cpp_validate_cilk_plus_loop (ret
);
38658 ret
= error_mark_node
;
38660 cp_parser_end_omp_structured_block (parser
, save
);
38661 add_stmt (finish_omp_structured_block (sb
));
38665 /* Create an identifier for a generic parameter type (a synthesized
38666 template parameter implied by `auto' or a concept identifier). */
38668 static GTY(()) int generic_parm_count
;
38670 make_generic_type_name ()
38673 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38674 return get_identifier (buf
);
38677 /* Predicate that behaves as is_auto_or_concept but matches the parent
38678 node of the generic type rather than the generic type itself. This
38679 allows for type transformation in add_implicit_template_parms. */
38682 tree_type_is_auto_or_concept (const_tree t
)
38684 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
38687 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38688 (creating a new template parameter list if necessary). Returns the newly
38689 created template type parm. */
38692 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38694 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38696 /* Before committing to modifying any scope, if we're in an
38697 implicit template scope, and we're trying to synthesize a
38698 constrained parameter, try to find a previous parameter with
38699 the same name. This is the same-type rule for abbreviated
38700 function templates.
38702 NOTE: We can generate implicit parameters when tentatively
38703 parsing a nested name specifier, only to reject that parse
38704 later. However, matching the same template-id as part of a
38705 direct-declarator should generate an identical template
38706 parameter, so this rule will merge them. */
38707 if (parser
->implicit_template_scope
&& constr
)
38709 tree t
= parser
->implicit_template_parms
;
38712 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38714 tree d
= TREE_VALUE (t
);
38715 if (TREE_CODE (d
) == PARM_DECL
)
38716 /* Return the TEMPLATE_PARM_INDEX. */
38717 d
= DECL_INITIAL (d
);
38720 t
= TREE_CHAIN (t
);
38724 /* We are either continuing a function template that already contains implicit
38725 template parameters, creating a new fully-implicit function template, or
38726 extending an existing explicit function template with implicit template
38729 cp_binding_level
*const entry_scope
= current_binding_level
;
38731 bool become_template
= false;
38732 cp_binding_level
*parent_scope
= 0;
38734 if (parser
->implicit_template_scope
)
38736 gcc_assert (parser
->implicit_template_parms
);
38738 current_binding_level
= parser
->implicit_template_scope
;
38742 /* Roll back to the existing template parameter scope (in the case of
38743 extending an explicit function template) or introduce a new template
38744 parameter scope ahead of the function parameter scope (or class scope
38745 in the case of out-of-line member definitions). The function scope is
38746 added back after template parameter synthesis below. */
38748 cp_binding_level
*scope
= entry_scope
;
38750 while (scope
->kind
== sk_function_parms
)
38752 parent_scope
= scope
;
38753 scope
= scope
->level_chain
;
38755 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38757 /* If not defining a class, then any class scope is a scope level in
38758 an out-of-line member definition. In this case simply wind back
38759 beyond the first such scope to inject the template parameter list.
38760 Otherwise wind back to the class being defined. The latter can
38761 occur in class member friend declarations such as:
38767 friend void A::foo (auto);
38770 The template parameter list synthesized for the friend declaration
38771 must be injected in the scope of 'B'. This can also occur in
38772 erroneous cases such as:
38778 void B::foo (auto) {}
38781 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38782 but, nevertheless, the template parameter list synthesized for the
38783 declarator should be injected into the scope of 'A' as if the
38784 ill-formed template was specified explicitly. */
38786 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38788 parent_scope
= scope
;
38789 scope
= scope
->level_chain
;
38793 current_binding_level
= scope
;
38795 if (scope
->kind
!= sk_template_parms
38796 || !function_being_declared_is_template_p (parser
))
38798 /* Introduce a new template parameter list for implicit template
38801 become_template
= true;
38803 parser
->implicit_template_scope
38804 = begin_scope (sk_template_parms
, NULL
);
38806 ++processing_template_decl
;
38808 parser
->fully_implicit_function_template_p
= true;
38809 ++parser
->num_template_parameter_lists
;
38813 /* Synthesize implicit template parameters at the end of the explicit
38814 template parameter list. */
38816 gcc_assert (current_template_parms
);
38818 parser
->implicit_template_scope
= scope
;
38820 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38821 parser
->implicit_template_parms
38822 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38826 /* Synthesize a new template parameter and track the current template
38827 parameter chain with implicit_template_parms. */
38829 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38830 tree synth_id
= make_generic_type_name ();
38831 tree synth_tmpl_parm
;
38832 bool non_type
= false;
38834 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38836 = finish_template_type_parm (class_type_node
, synth_id
);
38837 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38839 = finish_constrained_template_template_parm (proto
, synth_id
);
38842 synth_tmpl_parm
= copy_decl (proto
);
38843 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38847 // Attach the constraint to the parm before processing.
38848 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38849 TREE_TYPE (node
) = constr
;
38851 = process_template_parm (parser
->implicit_template_parms
,
38854 /*non_type=*/non_type
,
38855 /*param_pack=*/false);
38857 // Chain the new parameter to the list of implicit parameters.
38858 if (parser
->implicit_template_parms
)
38859 parser
->implicit_template_parms
38860 = TREE_CHAIN (parser
->implicit_template_parms
);
38862 parser
->implicit_template_parms
= new_parm
;
38864 tree new_decl
= getdecls ();
38866 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38867 new_decl
= DECL_INITIAL (new_decl
);
38869 /* If creating a fully implicit function template, start the new implicit
38870 template parameter list with this synthesized type, otherwise grow the
38871 current template parameter list. */
38873 if (become_template
)
38875 parent_scope
->level_chain
= current_binding_level
;
38877 tree new_parms
= make_tree_vec (1);
38878 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
38879 current_template_parms
= tree_cons (size_int (processing_template_decl
),
38880 new_parms
, current_template_parms
);
38884 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38885 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
38886 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
38887 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
38890 // If the new parameter was constrained, we need to add that to the
38891 // constraints in the template parameter list.
38892 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
38894 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
38895 reqs
= conjoin_constraints (reqs
, req
);
38896 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
38899 current_binding_level
= entry_scope
;
38904 /* Finish the declaration of a fully implicit function template. Such a
38905 template has no explicit template parameter list so has not been through the
38906 normal template head and tail processing. synthesize_implicit_template_parm
38907 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38908 provided if the declaration is a class member such that its template
38909 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38910 form is returned. Otherwise NULL_TREE is returned. */
38913 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
38915 gcc_assert (parser
->fully_implicit_function_template_p
);
38917 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
38918 && DECL_VIRTUAL_P (member_decl_opt
))
38920 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
38921 "implicit templates may not be %<virtual%>");
38922 DECL_VIRTUAL_P (member_decl_opt
) = false;
38925 if (member_decl_opt
)
38926 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
38927 end_template_decl ();
38929 parser
->fully_implicit_function_template_p
= false;
38930 --parser
->num_template_parameter_lists
;
38932 return member_decl_opt
;
38935 #include "gt-cp-parser.h"