2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3496 unsigned condop_depth
= 0;
3498 if (recovering
&& or_ttype
== CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3504 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3508 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3511 switch (token
->type
)
3514 case CPP_PRAGMA_EOL
:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE
:
3522 case CPP_CLOSE_SQUARE
:
3523 if (!square_depth
--)
3528 /* This matches the processing in skip_to_end_of_statement. */
3533 case CPP_OPEN_BRACE
:
3536 case CPP_CLOSE_BRACE
:
3541 case CPP_OPEN_PAREN
:
3546 case CPP_CLOSE_PAREN
:
3547 if (!brace_depth
&& !paren_depth
--)
3550 cp_lexer_consume_token (parser
->lexer
);
3556 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3561 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser
->lexer
);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3580 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3585 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3587 ttype
, consume_paren
);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3595 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3597 unsigned nesting_depth
= 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser
->fully_implicit_function_template_p
)
3601 abort_fully_implicit_template (parser
);
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. */
3615 /* If the next token is a `;', we have reached the end of the
3621 case CPP_CLOSE_BRACE
:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth
== 0)
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3642 if (--nesting_depth
== 0)
3644 cp_lexer_consume_token (parser
->lexer
);
3649 case CPP_OPEN_BRACE
:
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser
->lexer
);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3672 /* If there is additional (erroneous) input, skip to the end of
3674 cp_parser_skip_to_end_of_statement (parser
);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3677 cp_lexer_consume_token (parser
->lexer
);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3687 int nesting_depth
= 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser
->fully_implicit_function_template_p
)
3691 abort_fully_implicit_template (parser
);
3693 while (nesting_depth
>= 0)
3695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3697 switch (token
->type
)
3700 case CPP_PRAGMA_EOL
:
3701 /* If we've run out of tokens, stop. */
3705 /* Stop if this is an unnested ';'. */
3710 case CPP_CLOSE_BRACE
:
3711 /* Stop if this is an unnested '}', or closes the outermost
3714 if (nesting_depth
< 0)
3720 case CPP_OPEN_BRACE
:
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser
->lexer
);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3739 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3741 unsigned nesting_depth
= 0;
3745 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3747 switch (token
->type
)
3750 case CPP_PRAGMA_EOL
:
3751 /* If we've run out of tokens, stop. */
3754 case CPP_CLOSE_BRACE
:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth
-- == 0)
3761 case CPP_OPEN_BRACE
:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3781 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3785 parser
->lexer
->in_pragma
= false;
3788 token
= cp_lexer_consume_token (parser
->lexer
);
3789 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3799 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3801 parser
->lexer
->in_pragma
= false;
3802 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3811 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3812 location_t id_location
)
3815 if (identifier_p (id
))
3817 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3818 /*complain=*/tf_none
);
3819 if (result
== error_mark_node
)
3820 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3823 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator
*
3834 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3835 cp_cv_quals cv_qualifiers
,
3836 cp_declarator
*target
,
3839 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3840 return cp_error_declarator
;
3842 if (code
== INDIRECT_REF
)
3843 if (class_type
== NULL_TREE
)
3844 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3846 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3847 target
, attributes
);
3848 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3849 return make_reference_declarator (cv_qualifiers
, target
,
3851 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3852 return make_reference_declarator (cv_qualifiers
, target
,
3857 /* Create a new C++ parser. */
3860 cp_parser_new (void)
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer
= cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3873 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3875 parser
= ggc_cleared_alloc
<cp_parser
> ();
3876 parser
->lexer
= lexer
;
3877 parser
->context
= cp_parser_context_new (NULL
);
3879 /* For now, we always accept GNU extensions. */
3880 parser
->allow_gnu_extensions_p
= 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3884 parser
->greater_than_is_operator_p
= true;
3886 parser
->default_arg_ok_p
= true;
3888 /* We are not parsing a constant-expression. */
3889 parser
->integral_constant_expression_p
= false;
3890 parser
->allow_non_integral_constant_expression_p
= false;
3891 parser
->non_integral_constant_expression_p
= false;
3893 /* Local variable names are not forbidden. */
3894 parser
->local_variables_forbidden_p
= false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser
->in_unbraced_linkage_specification_p
= false;
3899 /* We are not processing a declarator. */
3900 parser
->in_declarator_p
= false;
3902 /* We are not processing a template-argument-list. */
3903 parser
->in_template_argument_list_p
= false;
3905 /* We are not in an iteration statement. */
3906 parser
->in_statement
= 0;
3908 /* We are not in a switch statement. */
3909 parser
->in_switch_statement_p
= false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser
->in_type_id_in_expr_p
= false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser
->implicit_extern_c
= false;
3917 /* String literals should be translated to the execution character set. */
3918 parser
->translate_strings_p
= true;
3920 /* We are not parsing a function body. */
3921 parser
->in_function_body
= false;
3923 /* We can correct until told otherwise. */
3924 parser
->colon_corrects_to_scope_p
= true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser
);
3929 /* There are no classes being defined. */
3930 parser
->num_classes_being_defined
= 0;
3932 /* No template parameters apply. */
3933 parser
->num_template_parameter_lists
= 0;
3935 /* Special parsing data structures. */
3936 parser
->omp_declare_simd
= NULL
;
3937 parser
->oacc_routine
= NULL
;
3939 /* Not declaring an implicit function template. */
3940 parser
->auto_is_implicit_function_template_parm_p
= false;
3941 parser
->fully_implicit_function_template_p
= false;
3942 parser
->implicit_template_parms
= 0;
3943 parser
->implicit_template_scope
= 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser
->prevent_constrained_type_specifiers
= 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3959 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3961 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3962 lexer
->next
= parser
->lexer
;
3963 parser
->lexer
= lexer
;
3965 /* Move the current source position to that of the first token in the
3967 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3973 cp_parser_pop_lexer (cp_parser
*parser
)
3975 cp_lexer
*lexer
= parser
->lexer
;
3976 parser
->lexer
= lexer
->next
;
3977 cp_lexer_destroy (lexer
);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3990 cp_parser_identifier (cp_parser
* parser
)
3994 /* Look for the identifier. */
3995 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3996 /* Return the value. */
3998 return cp_expr (token
->u
.value
, token
->location
);
4000 return error_mark_node
;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4018 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4019 bool lookup_udlit
= true)
4023 struct obstack str_ob
;
4024 cpp_string str
, istr
, *strs
;
4026 enum cpp_ttype type
, curr_type
;
4027 int have_suffix_p
= 0;
4029 tree suffix_id
= NULL_TREE
;
4030 bool curr_tok_is_userdef_p
= false;
4032 tok
= cp_lexer_peek_token (parser
->lexer
);
4033 if (!cp_parser_is_string_literal (tok
))
4035 cp_parser_error (parser
, "expected string-literal");
4036 return error_mark_node
;
4039 location_t loc
= tok
->location
;
4041 if (cpp_userdef_string_p (tok
->type
))
4043 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4044 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4045 curr_tok_is_userdef_p
= true;
4049 string_tree
= tok
->u
.value
;
4050 curr_type
= tok
->type
;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4059 cp_lexer_consume_token (parser
->lexer
);
4061 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4062 str
.len
= TREE_STRING_LENGTH (string_tree
);
4065 if (curr_tok_is_userdef_p
)
4067 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4069 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4072 curr_type
= tok
->type
;
4078 location_t last_tok_loc
= tok
->location
;
4079 gcc_obstack_init (&str_ob
);
4084 cp_lexer_consume_token (parser
->lexer
);
4086 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4087 str
.len
= TREE_STRING_LENGTH (string_tree
);
4089 if (curr_tok_is_userdef_p
)
4091 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4092 if (have_suffix_p
== 0)
4094 suffix_id
= curr_suffix_id
;
4097 else if (have_suffix_p
== 1
4098 && curr_suffix_id
!= suffix_id
)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id
, curr_suffix_id
);
4105 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_type
= tok
->type
;
4110 if (type
!= curr_type
)
4112 if (type
== CPP_STRING
)
4114 else if (curr_type
!= CPP_STRING
)
4116 rich_location
rich_loc (line_table
, tok
->location
);
4117 rich_loc
.add_range (last_tok_loc
, false);
4118 error_at (&rich_loc
,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4126 last_tok_loc
= tok
->location
;
4128 tok
= cp_lexer_peek_token (parser
->lexer
);
4129 if (cpp_userdef_string_p (tok
->type
))
4131 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4133 curr_tok_is_userdef_p
= true;
4137 string_tree
= tok
->u
.value
;
4138 curr_type
= tok
->type
;
4139 curr_tok_is_userdef_p
= false;
4142 while (cp_parser_is_string_literal (tok
));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4149 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4152 if (type
!= CPP_STRING
&& !wide_ok
)
4154 cp_parser_error (parser
, "a wide string is invalid in this context");
4158 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4159 (parse_in
, strs
, count
, &istr
, type
))
4161 value
= build_string (istr
.len
, (const char *)istr
.text
);
4162 free (CONST_CAST (unsigned char *, istr
.text
));
4168 case CPP_UTF8STRING
:
4169 TREE_TYPE (value
) = char_array_type_node
;
4172 TREE_TYPE (value
) = char16_array_type_node
;
4175 TREE_TYPE (value
) = char32_array_type_node
;
4178 TREE_TYPE (value
) = wchar_array_type_node
;
4182 value
= fix_string_type (value
);
4186 tree literal
= build_userdef_literal (suffix_id
, value
,
4187 OT_NONE
, NULL_TREE
);
4189 value
= cp_parser_userdef_string_literal (literal
);
4195 /* cpp_interpret_string has issued an error. */
4196 value
= error_mark_node
;
4199 obstack_free (&str_ob
, 0);
4201 return cp_expr (value
, loc
);
4204 /* Look up a literal operator with the name and the exact arguments. */
4207 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4210 decl
= lookup_name (name
);
4211 if (!decl
|| !is_overloaded_fn (decl
))
4212 return error_mark_node
;
4214 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4219 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4220 if (parmtypes
!= NULL_TREE
)
4222 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4223 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4225 tree tparm
= TREE_VALUE (parmtypes
);
4226 tree targ
= TREE_TYPE ((*args
)[ix
]);
4227 bool ptr
= TYPE_PTR_P (tparm
);
4228 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4229 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4231 || !same_type_p (TREE_TYPE (tparm
),
4236 && ix
== vec_safe_length (args
)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes
== void_list_node
)
4246 return error_mark_node
;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4253 cp_parser_userdef_char_literal (cp_parser
*parser
)
4255 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4256 tree literal
= token
->u
.value
;
4257 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4258 tree value
= USERDEF_LITERAL_VALUE (literal
);
4259 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4265 vec_safe_push (args
, value
);
4266 decl
= lookup_literal_operator (name
, args
);
4267 if (!decl
|| decl
== error_mark_node
)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name
, TREE_TYPE (value
));
4271 release_tree_vector (args
);
4272 return error_mark_node
;
4274 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4275 release_tree_vector (args
);
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4283 make_char_string_pack (tree value
)
4286 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4287 const char *str
= TREE_STRING_POINTER (value
);
4288 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4289 tree argvec
= make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec
= make_tree_vec (len
);
4293 for (i
= 0; i
< len
; ++i
)
4294 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4299 TREE_VEC_ELT (argvec
, 0) = argpack
;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4308 make_string_pack (tree value
)
4311 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value
);
4314 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4315 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4316 tree argvec
= make_tree_vec (2);
4318 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4319 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec
= make_tree_vec (len
);
4326 for (int i
= 0; i
< len
; ++i
)
4327 TREE_VEC_ELT (charvec
, i
)
4328 = double_int_to_tree (str_char_type_node
,
4329 double_int::from_buffer (str
+ i
* sz
, sz
));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4334 TREE_VEC_ELT (argvec
, 1) = argpack
;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4343 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4345 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4346 tree literal
= token
->u
.value
;
4347 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4348 tree value
= USERDEF_LITERAL_VALUE (literal
);
4349 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4350 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4351 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4353 vec
<tree
, va_gc
> *args
;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args
= make_tree_vector ();
4358 vec_safe_push (args
, value
);
4359 decl
= lookup_literal_operator (name
, args
);
4360 if (decl
&& decl
!= error_mark_node
)
4362 result
= finish_call_expr (decl
, &args
, false, true,
4363 tf_warning_or_error
);
4365 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node
);
4374 warning_at (token
->location
, OPT_Woverflow
,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node
);
4377 else if (overflow
< 0)
4378 warning_at (token
->location
, OPT_Woverflow
,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args
= make_tree_vector ();
4391 vec_safe_push (args
, num_string
);
4392 decl
= lookup_literal_operator (name
, args
);
4393 if (decl
&& decl
!= error_mark_node
)
4395 result
= finish_call_expr (decl
, &args
, false, true,
4396 tf_warning_or_error
);
4397 release_tree_vector (args
);
4400 release_tree_vector (args
);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args
= make_tree_vector ();
4406 decl
= lookup_literal_operator (name
, args
);
4407 if (decl
&& decl
!= error_mark_node
)
4409 tree tmpl_args
= make_char_string_pack (num_string
);
4410 decl
= lookup_template_function (decl
, tmpl_args
);
4411 result
= finish_call_expr (decl
, &args
, false, true,
4412 tf_warning_or_error
);
4413 release_tree_vector (args
);
4417 release_tree_vector (args
);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4422 bool i14
= (cxx_dialect
> cxx11
4423 && (id_equal (suffix_id
, "i")
4424 || id_equal (suffix_id
, "if")
4425 || id_equal (suffix_id
, "il")));
4426 diagnostic_t kind
= DK_ERROR
;
4431 tree cxlit
= lookup_qualified_name (std_node
,
4432 get_identifier ("complex_literals"),
4434 if (cxlit
== error_mark_node
)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4438 opt
= OPT_Wpedantic
;
4443 = emit_diagnostic (kind
, input_location
, opt
,
4444 "unable to find numeric literal operator %qD", name
);
4447 /* Don't inform either. */;
4450 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4454 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4458 inform (token
->location
, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind
== DK_ERROR
)
4462 value
= error_mark_node
;
4465 /* Use the built-in semantics. */
4467 if (id_equal (suffix_id
, "i"))
4469 if (TREE_CODE (value
) == INTEGER_CST
)
4470 type
= integer_type_node
;
4472 type
= double_type_node
;
4474 else if (id_equal (suffix_id
, "if"))
4475 type
= float_type_node
;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type
= long_double_type_node
;
4479 value
= build_complex (build_complex_type (type
),
4480 fold_convert (type
, integer_zero_node
),
4481 fold_convert (type
, value
));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4485 /* Avoid repeated diagnostics. */
4486 token
->u
.value
= value
;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4495 cp_parser_userdef_string_literal (tree literal
)
4497 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4498 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4499 tree value
= USERDEF_LITERAL_VALUE (literal
);
4500 int len
= TREE_STRING_LENGTH (value
)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4503 vec
<tree
, va_gc
> *args
;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args
= make_tree_vector ();
4508 vec_safe_push (args
, value
);
4509 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4510 decl
= lookup_literal_operator (name
, args
);
4512 if (decl
&& decl
!= error_mark_node
)
4514 result
= finish_call_expr (decl
, &args
, false, true,
4515 tf_warning_or_error
);
4516 release_tree_vector (args
);
4519 release_tree_vector (args
);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args
= make_tree_vector ();
4525 decl
= lookup_literal_operator (name
, args
);
4526 if (decl
&& decl
!= error_mark_node
)
4528 tree tmpl_args
= make_string_pack (value
);
4529 decl
= lookup_template_function (decl
, tmpl_args
);
4530 result
= finish_call_expr (decl
, &args
, false, true,
4531 tf_warning_or_error
);
4532 release_tree_vector (args
);
4535 release_tree_vector (args
);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name
, TREE_TYPE (value
), size_type_node
);
4539 return error_mark_node
;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4553 cp_parser_translation_unit (cp_parser
* parser
)
4555 /* The address of the first non-permanent object on the declarator
4557 static void *declarator_obstack_base
;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator
)
4564 gcc_obstack_init (&declarator_obstack
);
4565 /* Create the error declarator. */
4566 cp_error_declarator
= make_declarator (cdk_error
);
4567 /* Create the empty parameter list. */
4568 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4574 cp_parser_declaration_seq_opt (parser
);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser
->lexer
);
4581 parser
->lexer
= NULL
;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser
->implicit_extern_c
)
4587 pop_lang_context ();
4588 parser
->implicit_extern_c
= false;
4592 finish_translation_unit ();
4598 cp_parser_error (parser
, "expected declaration");
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack
)
4604 == declarator_obstack_base
);
4606 /* All went well. */
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p
)
4616 tsubst_flags_t complain
= tf_warning_or_error
;
4618 complain
|= tf_decltype
;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4632 cp_parser_parse_tentatively (parser
);
4633 cp_parser_commit_to_topmost_tentative_parse (parser
);
4634 return cp_lexer_token_position (parser
->lexer
, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4641 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser
);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4651 token
->type
= CPP_PREPARSED_EXPR
;
4652 token
->u
.value
= expr
;
4653 token
->keyword
= RID_MAX
;
4654 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4667 tentative_firewall (cp_parser
*p
): parser(p
)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4673 cp_parser_parse_tentatively (parser
);
4674 cp_parser_commit_to_topmost_tentative_parse (parser
);
4675 cp_parser_parse_tentatively (parser
);
4679 ~tentative_firewall()
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err
= cp_parser_error_occurred (parser
);
4686 cp_parser_parse_definitely (parser
);
4687 cp_parser_parse_definitely (parser
);
4689 cp_parser_simulate_error (parser
);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4701 template <typename traits_t
>
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser
*parser
)
4715 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4716 return cp_parser_require (parser
, traits_t::open_token_type
,
4717 traits_t::required_token_open
);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token
* consume_open (cp_parser
*parser
)
4725 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4726 gcc_assert (tok
->type
== traits_t::open_token_type
);
4727 m_open_loc
= tok
->location
;
4731 /* If the next token is the closing symbol for this pair, consume it
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token
*require_close (cp_parser
*parser
) const
4738 return cp_parser_require (parser
, traits_t::close_token_type
,
4739 traits_t::required_token_close
,
4744 location_t m_open_loc
;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4752 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4754 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair
<matching_paren_traits
> matching_parens
;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4767 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4768 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4769 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4775 typedef token_pair
<matching_brace_traits
> matching_braces
;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4782 cp_parser_statement_expr (cp_parser
*parser
)
4784 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4786 /* Consume the '('. */
4787 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4788 matching_parens parens
;
4789 parens
.consume_open (parser
);
4790 /* Start the statement-expression. */
4791 tree expr
= begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4795 expr
= finish_stmt_expr (expr
, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4798 if (!parens
.require_close (parser
))
4799 cp_parser_skip_to_end_of_statement (parser
);
4801 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4802 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4803 return cp_expr (expr
, combined_loc
);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4821 cp_parser_fold_operator (cp_token
*token
)
4823 switch (token
->type
)
4825 case CPP_PLUS
: return PLUS_EXPR
;
4826 case CPP_MINUS
: return MINUS_EXPR
;
4827 case CPP_MULT
: return MULT_EXPR
;
4828 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4829 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4830 case CPP_XOR
: return BIT_XOR_EXPR
;
4831 case CPP_AND
: return BIT_AND_EXPR
;
4832 case CPP_OR
: return BIT_IOR_EXPR
;
4833 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4834 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4836 case CPP_EQ
: return -NOP_EXPR
;
4837 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4838 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4839 case CPP_MULT_EQ
: return -MULT_EXPR
;
4840 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4841 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4842 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4843 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4844 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4845 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4846 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4848 case CPP_EQ_EQ
: return EQ_EXPR
;
4849 case CPP_NOT_EQ
: return NE_EXPR
;
4850 case CPP_LESS
: return LT_EXPR
;
4851 case CPP_GREATER
: return GT_EXPR
;
4852 case CPP_LESS_EQ
: return LE_EXPR
;
4853 case CPP_GREATER_EQ
: return GE_EXPR
;
4855 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4856 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4858 case CPP_COMMA
: return COMPOUND_EXPR
;
4860 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4861 case CPP_DEREF_STAR
: return MEMBER_REF
;
4863 default: return ERROR_MARK
;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4872 is_binary_op (tree_code code
)
4877 case POINTER_PLUS_EXPR
:
4880 case TRUNC_DIV_EXPR
:
4881 case TRUNC_MOD_EXPR
:
4897 case TRUTH_ANDIF_EXPR
:
4898 case TRUTH_ORIF_EXPR
:
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4915 cp_parser_fold_operator (cp_parser
*parser
)
4917 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4918 int code
= cp_parser_fold_operator (token
);
4919 if (code
!= ERROR_MARK
)
4920 cp_lexer_consume_token (parser
->lexer
);
4924 /* Parse a fold-expression.
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4934 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4941 cp_lexer_consume_token (parser
->lexer
);
4942 int op
= cp_parser_fold_operator (parser
);
4943 if (op
== ERROR_MARK
)
4945 cp_parser_error (parser
, "expected binary operator");
4946 return error_mark_node
;
4949 tree expr
= cp_parser_cast_expression (parser
, false, false,
4951 if (expr
== error_mark_node
)
4952 return error_mark_node
;
4953 return finish_left_unary_fold_expr (expr
, op
);
4956 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4957 int op
= cp_parser_fold_operator (parser
);
4958 if (op
== ERROR_MARK
)
4960 cp_parser_error (parser
, "expected binary operator");
4961 return error_mark_node
;
4964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4966 cp_parser_error (parser
, "expected ...");
4967 return error_mark_node
;
4969 cp_lexer_consume_token (parser
->lexer
);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1
)))
4977 error_at (location_of (expr1
),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1
) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1
)
4981 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4982 error_at (location_of (expr1
),
4983 "conditional expression in operand of fold-expression");
4986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4987 return finish_right_unary_fold_expr (expr1
, op
);
4989 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4991 cp_parser_error (parser
, "mismatched operator in fold-expression");
4992 return error_mark_node
;
4994 cp_lexer_consume_token (parser
->lexer
);
4996 // Binary left or right fold.
4997 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4998 if (expr2
== error_mark_node
)
4999 return error_mark_node
;
5000 return finish_binary_fold_expr (expr1
, expr2
, op
);
5003 /* Parse a primary-expression.
5010 lambda-expression (C++11)
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5058 cp_parser_primary_expression (cp_parser
*parser
,
5061 bool template_arg_p
,
5065 cp_token
*token
= NULL
;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk
= CP_ID_KIND_NONE
;
5070 /* Peek at the next token. */
5071 token
= cp_lexer_peek_token (parser
->lexer
);
5072 switch ((int) token
->type
)
5081 user-defined-literal */
5088 case CPP_PREPARSED_EXPR
:
5089 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5090 return cp_parser_userdef_numeric_literal (parser
);
5091 token
= cp_lexer_consume_token (parser
->lexer
);
5092 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5094 error_at (token
->location
,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node
;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token
->u
.value
) == REAL_CST
5102 && parser
->integral_constant_expression_p
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5110 cp_token
*next_token
;
5112 next_token
= cp_lexer_peek_token (parser
->lexer
);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token
->type
!= CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token
->type
!= CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token
->type
!= CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token
->type
!= CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token
->type
!= CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token
->type
!= CPP_GREATER
5126 || parser
->greater_than_is_operator_p
)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token
->type
!= CPP_RSHIFT
5130 || (cxx_dialect
== cxx98
)
5131 || parser
->greater_than_is_operator_p
))
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5140 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5142 return cp_expr (token
->u
.value
, token
->location
);
5144 case CPP_CHAR_USERDEF
:
5145 case CPP_CHAR16_USERDEF
:
5146 case CPP_CHAR32_USERDEF
:
5147 case CPP_WCHAR_USERDEF
:
5148 case CPP_UTF8CHAR_USERDEF
:
5149 return cp_parser_userdef_char_literal (parser
);
5155 case CPP_UTF8STRING
:
5156 case CPP_STRING_USERDEF
:
5157 case CPP_STRING16_USERDEF
:
5158 case CPP_STRING32_USERDEF
:
5159 case CPP_WSTRING_USERDEF
:
5160 case CPP_UTF8STRING_USERDEF
:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser
,
5165 parser
->translate_strings_p
,
5168 case CPP_OPEN_PAREN
:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser
)
5172 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token
->location
, OPT_Wpedantic
,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser
->in_function_body
5185 || parser
->in_template_argument_list_p
)
5187 error_at (token
->location
,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser
);
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5192 cp_lexer_consume_token (parser
->lexer
);
5193 return error_mark_node
;
5196 return cp_parser_statement_expr (parser
);
5198 /* Otherwise it's a normal parenthesized expression. */
5201 bool saved_greater_than_is_operator_p
;
5203 location_t open_paren_loc
= token
->location
;
5205 /* Consume the `('. */
5206 matching_parens parens
;
5207 parens
.consume_open (parser
);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser
->greater_than_is_operator_p
;
5212 parser
->greater_than_is_operator_p
= true;
5214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5215 /* Left fold expression. */
5218 /* Parse the parenthesized expression. */
5219 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5221 token
= cp_lexer_peek_token (parser
->lexer
);
5222 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5224 expr
= cp_parser_fold_expression (parser
, expr
);
5225 if (expr
!= error_mark_node
5226 && cxx_dialect
< cxx17
5227 && !in_system_header_at (input_location
))
5228 pedwarn (input_location
, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5237 expr
= finish_parenthesized_expr (expr
);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5244 *idk
= CP_ID_KIND_NONE
;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser
->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p
;
5251 /* Consume the `)'. */
5252 token
= cp_lexer_peek_token (parser
->lexer
);
5253 location_t close_paren_loc
= token
->location
;
5254 expr
.set_range (open_paren_loc
, close_paren_loc
);
5255 if (!parens
.require_close (parser
)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5257 cp_parser_skip_to_end_of_statement (parser
);
5262 case CPP_OPEN_SQUARE
:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser
);
5268 tree msg
= cp_parser_objc_message_expression (parser
);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser
))
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam
= cp_parser_lambda_expression (parser
);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser
))
5277 return error_mark_node
;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5282 case CPP_OBJC_STRING
:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser
);
5286 cp_parser_error (parser
, "expected primary-expression");
5287 return error_mark_node
;
5290 switch (token
->keyword
)
5292 /* These two are the boolean literals. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 return cp_expr (boolean_true_node
, token
->location
);
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return cp_expr (boolean_false_node
, token
->location
);
5300 /* The `__null' literal. */
5302 cp_lexer_consume_token (parser
->lexer
);
5303 return cp_expr (null_node
, token
->location
);
5305 /* The `nullptr' literal. */
5307 cp_lexer_consume_token (parser
->lexer
);
5308 return cp_expr (nullptr_node
, token
->location
);
5310 /* Recognize the `this' keyword. */
5312 cp_lexer_consume_token (parser
->lexer
);
5313 if (parser
->local_variables_forbidden_p
)
5315 error_at (token
->location
,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node
;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5321 return error_mark_node
;
5322 return cp_expr (finish_this_expr (), token
->location
);
5324 /* The `operator' keyword can be the beginning of an
5329 case RID_FUNCTION_NAME
:
5330 case RID_PRETTY_FUNCTION_NAME
:
5331 case RID_C99_FUNCTION_NAME
:
5333 non_integral_constant name
;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token
= cp_lexer_consume_token (parser
->lexer
);
5344 switch (token
->keyword
)
5346 case RID_FUNCTION_NAME
:
5347 name
= NIC_FUNC_NAME
;
5349 case RID_PRETTY_FUNCTION_NAME
:
5350 name
= NIC_PRETTY_FUNC
;
5352 case RID_C99_FUNCTION_NAME
:
5353 name
= NIC_C99_FUNC
;
5359 if (cp_parser_non_integral_constant_expression (parser
, name
))
5360 return error_mark_node
;
5362 /* Look up the name. */
5363 return finish_fname (token
->u
.value
);
5370 source_location type_location
;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser
->lexer
);
5376 /* Look for the opening `('. */
5377 matching_parens parens
;
5378 parens
.require_open (parser
);
5379 /* Now, parse the assignment-expression. */
5380 expression
= cp_parser_assignment_expression (parser
);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5383 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel
s (parser
);
5387 type
= cp_parser_type_id (parser
);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser
->lexer
)->location
;
5392 parens
.require_close (parser
);
5393 /* Using `va_arg' in a constant-expression is not
5395 if (cp_parser_non_integral_constant_expression (parser
,
5397 return error_mark_node
;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location
, start_loc
, finish_loc
);
5405 return build_x_va_arg (combined_loc
, expression
, type
);
5409 return cp_parser_builtin_offsetof (parser
);
5411 case RID_HAS_NOTHROW_ASSIGN
:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5413 case RID_HAS_NOTHROW_COPY
:
5414 case RID_HAS_TRIVIAL_ASSIGN
:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5416 case RID_HAS_TRIVIAL_COPY
:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5420 case RID_IS_ABSTRACT
:
5421 case RID_IS_AGGREGATE
:
5422 case RID_IS_BASE_OF
:
5427 case RID_IS_LITERAL_TYPE
:
5429 case RID_IS_POLYMORPHIC
:
5430 case RID_IS_SAME_AS
:
5431 case RID_IS_STD_LAYOUT
:
5432 case RID_IS_TRIVIAL
:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5435 case RID_IS_TRIVIALLY_COPYABLE
:
5437 case RID_IS_ASSIGNABLE
:
5438 case RID_IS_CONSTRUCTIBLE
:
5439 return cp_parser_trait_expr (parser
, token
->keyword
);
5443 return cp_parser_requires_expression (parser
);
5445 /* Objective-C++ expressions. */
5447 case RID_AT_PROTOCOL
:
5448 case RID_AT_SELECTOR
:
5449 return cp_parser_objc_expression (parser
);
5452 if (parser
->in_function_body
5453 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5456 error_at (token
->location
,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser
);
5459 return error_mark_node
;
5463 cp_parser_error (parser
, "expected primary-expression");
5464 return error_mark_node
;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5472 case CPP_TEMPLATE_ID
:
5473 case CPP_NESTED_NAME_SPECIFIER
:
5476 cp_expr id_expression
;
5478 const char *error_msg
;
5481 cp_token
*id_expr_token
;
5483 /* Parse the id-expression. */
5485 = cp_parser_id_expression (parser
,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression
== error_mark_node
)
5492 return error_mark_node
;
5493 id_expr_token
= token
;
5494 token
= cp_lexer_peek_token (parser
->lexer
);
5495 done
= (token
->type
!= CPP_OPEN_SQUARE
5496 && token
->type
!= CPP_OPEN_PAREN
5497 && token
->type
!= CPP_DOT
5498 && token
->type
!= CPP_DEREF
5499 && token
->type
!= CPP_PLUS_PLUS
5500 && token
->type
!= CPP_MINUS_MINUS
);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression
) == TYPE_DECL
)
5506 decl
= id_expression
;
5507 /* Look up the name. */
5510 tree ambiguous_decls
;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5515 if (id_expr_token
->type
== CPP_NAME
5516 && id_expr_token
->error_reported
)
5518 cp_parser_simulate_error (parser
);
5519 return error_mark_node
;
5522 decl
= cp_parser_lookup_name (parser
, id_expression
,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5528 id_expr_token
->location
);
5529 /* If the lookup was ambiguous, an error will already have
5531 if (ambiguous_decls
)
5532 return error_mark_node
;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5538 && TREE_CODE (decl
) == TYPE_DECL
5539 && objc_is_class_name (decl
))
5542 cp_lexer_consume_token (parser
->lexer
);
5543 component
= cp_parser_identifier (parser
);
5544 if (component
== error_mark_node
)
5545 return error_mark_node
;
5547 tree result
= objc_build_class_component_ref (id_expression
,
5549 /* Build a location of the form:
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location
, id_expression
.get_start (),
5557 get_finish (input_location
));
5558 protected_set_expr_location (result
, combined_loc
);
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5575 if (decl_tree
!= decl
.get_value ())
5576 decl
= cp_expr (decl_tree
);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl
) == SCOPE_REF
)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser
->local_variables_forbidden_p
5597 && local_variable_p (decl
))
5599 error_at (id_expr_token
->location
,
5600 "local variable %qD may not appear in this context",
5602 return error_mark_node
;
5606 if (processing_template_decl
)
5607 if (tree fns
= maybe_get_fns (decl
))
5608 /* It's too difficult to mark ths in all the places where
5609 we know for sure we need to keep the lookup, so do it
5610 now. The cost is extra GC to recycle the lookups
5611 resolved at parse time. */
5614 decl
= (finish_id_expression
5615 (id_expression
, decl
, parser
->scope
,
5617 parser
->integral_constant_expression_p
,
5618 parser
->allow_non_integral_constant_expression_p
,
5619 &parser
->non_integral_constant_expression_p
,
5620 template_p
, done
, address_p
,
5623 id_expression
.get_location ()));
5625 cp_parser_error (parser
, error_msg
);
5626 decl
.set_location (id_expr_token
->location
);
5630 /* Anything else is an error. */
5632 cp_parser_error (parser
, "expected primary-expression");
5633 return error_mark_node
;
5637 static inline cp_expr
5638 cp_parser_primary_expression (cp_parser
*parser
,
5641 bool template_arg_p
,
5644 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5645 /*decltype*/false, idk
);
5648 /* Parse an id-expression.
5655 :: [opt] nested-name-specifier template [opt] unqualified-id
5657 :: operator-function-id
5660 Return a representation of the unqualified portion of the
5661 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5662 a `::' or nested-name-specifier.
5664 Often, if the id-expression was a qualified-id, the caller will
5665 want to make a SCOPE_REF to represent the qualified-id. This
5666 function does not do this in order to avoid wastefully creating
5667 SCOPE_REFs when they are not required.
5669 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5672 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5673 uninstantiated templates.
5675 If *TEMPLATE_P is non-NULL, it is set to true iff the
5676 `template' keyword is used to explicitly indicate that the entity
5677 named is a template.
5679 If DECLARATOR_P is true, the id-expression is appearing as part of
5680 a declarator, rather than as part of an expression. */
5683 cp_parser_id_expression (cp_parser
*parser
,
5684 bool template_keyword_p
,
5685 bool check_dependency_p
,
5690 bool global_scope_p
;
5691 bool nested_name_specifier_p
;
5693 /* Assume the `template' keyword was not used. */
5695 *template_p
= template_keyword_p
;
5697 /* Look for the optional `::' operator. */
5699 = (!template_keyword_p
5700 && (cp_parser_global_scope_opt (parser
,
5701 /*current_scope_valid_p=*/false)
5704 /* Look for the optional nested-name-specifier. */
5705 nested_name_specifier_p
5706 = (cp_parser_nested_name_specifier_opt (parser
,
5707 /*typename_keyword_p=*/false,
5714 /* If there is a nested-name-specifier, then we are looking at
5715 the first qualified-id production. */
5716 if (nested_name_specifier_p
)
5719 tree saved_object_scope
;
5720 tree saved_qualifying_scope
;
5721 cp_expr unqualified_id
;
5724 /* See if the next token is the `template' keyword. */
5726 template_p
= &is_template
;
5727 *template_p
= cp_parser_optional_template_keyword (parser
);
5728 /* Name lookup we do during the processing of the
5729 unqualified-id might obliterate SCOPE. */
5730 saved_scope
= parser
->scope
;
5731 saved_object_scope
= parser
->object_scope
;
5732 saved_qualifying_scope
= parser
->qualifying_scope
;
5733 /* Process the final unqualified-id. */
5734 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5737 /*optional_p=*/false);
5738 /* Restore the SAVED_SCOPE for our caller. */
5739 parser
->scope
= saved_scope
;
5740 parser
->object_scope
= saved_object_scope
;
5741 parser
->qualifying_scope
= saved_qualifying_scope
;
5743 return unqualified_id
;
5745 /* Otherwise, if we are in global scope, then we are looking at one
5746 of the other qualified-id productions. */
5747 else if (global_scope_p
)
5752 /* Peek at the next token. */
5753 token
= cp_lexer_peek_token (parser
->lexer
);
5755 /* If it's an identifier, and the next token is not a "<", then
5756 we can avoid the template-id case. This is an optimization
5757 for this common case. */
5758 if (token
->type
== CPP_NAME
5759 && !cp_parser_nth_token_starts_template_argument_list_p
5761 return cp_parser_identifier (parser
);
5763 cp_parser_parse_tentatively (parser
);
5764 /* Try a template-id. */
5765 id
= cp_parser_template_id (parser
,
5766 /*template_keyword_p=*/false,
5767 /*check_dependency_p=*/true,
5770 /* If that worked, we're done. */
5771 if (cp_parser_parse_definitely (parser
))
5774 /* Peek at the next token. (Changes in the token buffer may
5775 have invalidated the pointer obtained above.) */
5776 token
= cp_lexer_peek_token (parser
->lexer
);
5778 switch (token
->type
)
5781 return cp_parser_identifier (parser
);
5784 if (token
->keyword
== RID_OPERATOR
)
5785 return cp_parser_operator_function_id (parser
);
5789 cp_parser_error (parser
, "expected id-expression");
5790 return error_mark_node
;
5794 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5795 /*check_dependency_p=*/true,
5800 /* Parse an unqualified-id.
5804 operator-function-id
5805 conversion-function-id
5809 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5810 keyword, in a construct like `A::template ...'.
5812 Returns a representation of unqualified-id. For the `identifier'
5813 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5814 production a BIT_NOT_EXPR is returned; the operand of the
5815 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5816 other productions, see the documentation accompanying the
5817 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5818 names are looked up in uninstantiated templates. If DECLARATOR_P
5819 is true, the unqualified-id is appearing as part of a declarator,
5820 rather than as part of an expression. */
5823 cp_parser_unqualified_id (cp_parser
* parser
,
5824 bool template_keyword_p
,
5825 bool check_dependency_p
,
5831 /* Peek at the next token. */
5832 token
= cp_lexer_peek_token (parser
->lexer
);
5834 switch ((int) token
->type
)
5840 /* We don't know yet whether or not this will be a
5842 cp_parser_parse_tentatively (parser
);
5843 /* Try a template-id. */
5844 id
= cp_parser_template_id (parser
, template_keyword_p
,
5848 /* If it worked, we're done. */
5849 if (cp_parser_parse_definitely (parser
))
5851 /* Otherwise, it's an ordinary identifier. */
5852 return cp_parser_identifier (parser
);
5855 case CPP_TEMPLATE_ID
:
5856 return cp_parser_template_id (parser
, template_keyword_p
,
5864 tree qualifying_scope
;
5869 /* Consume the `~' token. */
5870 cp_lexer_consume_token (parser
->lexer
);
5871 /* Parse the class-name. The standard, as written, seems to
5874 template <typename T> struct S { ~S (); };
5875 template <typename T> S<T>::~S() {}
5877 is invalid, since `~' must be followed by a class-name, but
5878 `S<T>' is dependent, and so not known to be a class.
5879 That's not right; we need to look in uninstantiated
5880 templates. A further complication arises from:
5882 template <typename T> void f(T t) {
5886 Here, it is not possible to look up `T' in the scope of `T'
5887 itself. We must look in both the current scope, and the
5888 scope of the containing complete expression.
5890 Yet another issue is:
5899 The standard does not seem to say that the `S' in `~S'
5900 should refer to the type `S' and not the data member
5903 /* DR 244 says that we look up the name after the "~" in the
5904 same scope as we looked up the qualifying name. That idea
5905 isn't fully worked out; it's more complicated than that. */
5906 scope
= parser
->scope
;
5907 object_scope
= parser
->object_scope
;
5908 qualifying_scope
= parser
->qualifying_scope
;
5910 /* Check for invalid scopes. */
5911 if (scope
== error_mark_node
)
5913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5914 cp_lexer_consume_token (parser
->lexer
);
5915 return error_mark_node
;
5917 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5919 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5920 error_at (token
->location
,
5921 "scope %qT before %<~%> is not a class-name",
5923 cp_parser_simulate_error (parser
);
5924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5925 cp_lexer_consume_token (parser
->lexer
);
5926 return error_mark_node
;
5928 gcc_assert (!scope
|| TYPE_P (scope
));
5930 /* If the name is of the form "X::~X" it's OK even if X is a
5932 token
= cp_lexer_peek_token (parser
->lexer
);
5934 && token
->type
== CPP_NAME
5935 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5937 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5938 || (CLASS_TYPE_P (scope
)
5939 && constructor_name_p (token
->u
.value
, scope
))))
5941 cp_lexer_consume_token (parser
->lexer
);
5942 return build_nt (BIT_NOT_EXPR
, scope
);
5945 /* ~auto means the destructor of whatever the object is. */
5946 if (cp_parser_is_keyword (token
, RID_AUTO
))
5948 if (cxx_dialect
< cxx14
)
5949 pedwarn (input_location
, 0,
5950 "%<~auto%> only available with "
5951 "-std=c++14 or -std=gnu++14");
5952 cp_lexer_consume_token (parser
->lexer
);
5953 return build_nt (BIT_NOT_EXPR
, make_auto ());
5956 /* If there was an explicit qualification (S::~T), first look
5957 in the scope given by the qualification (i.e., S).
5959 Note: in the calls to cp_parser_class_name below we pass
5960 typename_type so that lookup finds the injected-class-name
5961 rather than the constructor. */
5963 type_decl
= NULL_TREE
;
5966 cp_parser_parse_tentatively (parser
);
5967 type_decl
= cp_parser_class_name (parser
,
5968 /*typename_keyword_p=*/false,
5969 /*template_keyword_p=*/false,
5971 /*check_dependency=*/false,
5972 /*class_head_p=*/false,
5974 if (cp_parser_parse_definitely (parser
))
5977 /* In "N::S::~S", look in "N" as well. */
5978 if (!done
&& scope
&& qualifying_scope
)
5980 cp_parser_parse_tentatively (parser
);
5981 parser
->scope
= qualifying_scope
;
5982 parser
->object_scope
= NULL_TREE
;
5983 parser
->qualifying_scope
= NULL_TREE
;
5985 = cp_parser_class_name (parser
,
5986 /*typename_keyword_p=*/false,
5987 /*template_keyword_p=*/false,
5989 /*check_dependency=*/false,
5990 /*class_head_p=*/false,
5992 if (cp_parser_parse_definitely (parser
))
5995 /* In "p->S::~T", look in the scope given by "*p" as well. */
5996 else if (!done
&& object_scope
)
5998 cp_parser_parse_tentatively (parser
);
5999 parser
->scope
= object_scope
;
6000 parser
->object_scope
= NULL_TREE
;
6001 parser
->qualifying_scope
= NULL_TREE
;
6003 = cp_parser_class_name (parser
,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6010 if (cp_parser_parse_definitely (parser
))
6013 /* Look in the surrounding context. */
6016 parser
->scope
= NULL_TREE
;
6017 parser
->object_scope
= NULL_TREE
;
6018 parser
->qualifying_scope
= NULL_TREE
;
6019 if (processing_template_decl
)
6020 cp_parser_parse_tentatively (parser
);
6022 = cp_parser_class_name (parser
,
6023 /*typename_keyword_p=*/false,
6024 /*template_keyword_p=*/false,
6026 /*check_dependency=*/false,
6027 /*class_head_p=*/false,
6029 if (processing_template_decl
6030 && ! cp_parser_parse_definitely (parser
))
6032 /* We couldn't find a type with this name. If we're parsing
6033 tentatively, fail and try something else. */
6034 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6036 cp_parser_simulate_error (parser
);
6037 return error_mark_node
;
6039 /* Otherwise, accept it and check for a match at instantiation
6041 type_decl
= cp_parser_identifier (parser
);
6042 if (type_decl
!= error_mark_node
)
6043 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6047 /* If an error occurred, assume that the name of the
6048 destructor is the same as the name of the qualifying
6049 class. That allows us to keep parsing after running
6050 into ill-formed destructor names. */
6051 if (type_decl
== error_mark_node
&& scope
)
6052 return build_nt (BIT_NOT_EXPR
, scope
);
6053 else if (type_decl
== error_mark_node
)
6054 return error_mark_node
;
6056 /* Check that destructor name and scope match. */
6057 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6059 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6060 error_at (token
->location
,
6061 "declaration of %<~%T%> as member of %qT",
6063 cp_parser_simulate_error (parser
);
6064 return error_mark_node
;
6069 A typedef-name that names a class shall not be used as the
6070 identifier in the declarator for a destructor declaration. */
6072 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6073 && !DECL_SELF_REFERENCE_P (type_decl
)
6074 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6075 error_at (token
->location
,
6076 "typedef-name %qD used as destructor declarator",
6079 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6083 if (token
->keyword
== RID_OPERATOR
)
6087 /* This could be a template-id, so we try that first. */
6088 cp_parser_parse_tentatively (parser
);
6089 /* Try a template-id. */
6090 id
= cp_parser_template_id (parser
, template_keyword_p
,
6091 /*check_dependency_p=*/true,
6094 /* If that worked, we're done. */
6095 if (cp_parser_parse_definitely (parser
))
6097 /* We still don't know whether we're looking at an
6098 operator-function-id or a conversion-function-id. */
6099 cp_parser_parse_tentatively (parser
);
6100 /* Try an operator-function-id. */
6101 id
= cp_parser_operator_function_id (parser
);
6102 /* If that didn't work, try a conversion-function-id. */
6103 if (!cp_parser_parse_definitely (parser
))
6104 id
= cp_parser_conversion_function_id (parser
);
6113 cp_parser_error (parser
, "expected unqualified-id");
6114 return error_mark_node
;
6118 /* Parse an (optional) nested-name-specifier.
6120 nested-name-specifier: [C++98]
6121 class-or-namespace-name :: nested-name-specifier [opt]
6122 class-or-namespace-name :: template nested-name-specifier [opt]
6124 nested-name-specifier: [C++0x]
6127 nested-name-specifier identifier ::
6128 nested-name-specifier template [opt] simple-template-id ::
6130 PARSER->SCOPE should be set appropriately before this function is
6131 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6132 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6135 Sets PARSER->SCOPE to the class (TYPE) or namespace
6136 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6137 it unchanged if there is no nested-name-specifier. Returns the new
6138 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6140 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6141 part of a declaration and/or decl-specifier. */
6144 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6145 bool typename_keyword_p
,
6146 bool check_dependency_p
,
6148 bool is_declaration
,
6149 bool template_keyword_p
/* = false */)
6151 bool success
= false;
6152 cp_token_position start
= 0;
6155 /* Remember where the nested-name-specifier starts. */
6156 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6158 start
= cp_lexer_token_position (parser
->lexer
, false);
6159 push_deferring_access_checks (dk_deferred
);
6166 tree saved_qualifying_scope
;
6168 /* Spot cases that cannot be the beginning of a
6169 nested-name-specifier. */
6170 token
= cp_lexer_peek_token (parser
->lexer
);
6172 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6173 the already parsed nested-name-specifier. */
6174 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6176 /* Grab the nested-name-specifier and continue the loop. */
6177 cp_parser_pre_parsed_nested_name_specifier (parser
);
6178 /* If we originally encountered this nested-name-specifier
6179 with IS_DECLARATION set to false, we will not have
6180 resolved TYPENAME_TYPEs, so we must do so here. */
6182 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6184 new_scope
= resolve_typename_type (parser
->scope
,
6185 /*only_current_p=*/false);
6186 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6187 parser
->scope
= new_scope
;
6193 /* Spot cases that cannot be the beginning of a
6194 nested-name-specifier. On the second and subsequent times
6195 through the loop, we look for the `template' keyword. */
6196 if (success
&& token
->keyword
== RID_TEMPLATE
)
6198 /* A template-id can start a nested-name-specifier. */
6199 else if (token
->type
== CPP_TEMPLATE_ID
)
6201 /* DR 743: decltype can be used in a nested-name-specifier. */
6202 else if (token_is_decltype (token
))
6206 /* If the next token is not an identifier, then it is
6207 definitely not a type-name or namespace-name. */
6208 if (token
->type
!= CPP_NAME
)
6210 /* If the following token is neither a `<' (to begin a
6211 template-id), nor a `::', then we are not looking at a
6212 nested-name-specifier. */
6213 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6215 if (token
->type
== CPP_COLON
6216 && parser
->colon_corrects_to_scope_p
6217 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6219 gcc_rich_location
richloc (token
->location
);
6220 richloc
.add_fixit_replace ("::");
6222 "found %<:%> in nested-name-specifier, "
6224 token
->type
= CPP_SCOPE
;
6227 if (token
->type
!= CPP_SCOPE
6228 && !cp_parser_nth_token_starts_template_argument_list_p
6233 /* The nested-name-specifier is optional, so we parse
6235 cp_parser_parse_tentatively (parser
);
6237 /* Look for the optional `template' keyword, if this isn't the
6238 first time through the loop. */
6240 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6242 /* Save the old scope since the name lookup we are about to do
6243 might destroy it. */
6244 old_scope
= parser
->scope
;
6245 saved_qualifying_scope
= parser
->qualifying_scope
;
6246 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6247 look up names in "X<T>::I" in order to determine that "Y" is
6248 a template. So, if we have a typename at this point, we make
6249 an effort to look through it. */
6251 && !typename_keyword_p
6253 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6254 parser
->scope
= resolve_typename_type (parser
->scope
,
6255 /*only_current_p=*/false);
6256 /* Parse the qualifying entity. */
6258 = cp_parser_qualifying_entity (parser
,
6264 /* Look for the `::' token. */
6265 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6267 /* If we found what we wanted, we keep going; otherwise, we're
6269 if (!cp_parser_parse_definitely (parser
))
6271 bool error_p
= false;
6273 /* Restore the OLD_SCOPE since it was valid before the
6274 failed attempt at finding the last
6275 class-or-namespace-name. */
6276 parser
->scope
= old_scope
;
6277 parser
->qualifying_scope
= saved_qualifying_scope
;
6279 /* If the next token is a decltype, and the one after that is a
6280 `::', then the decltype has failed to resolve to a class or
6281 enumeration type. Give this error even when parsing
6282 tentatively since it can't possibly be valid--and we're going
6283 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6284 won't get another chance.*/
6285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6286 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6289 token
= cp_lexer_consume_token (parser
->lexer
);
6290 error_at (token
->location
, "decltype evaluates to %qT, "
6291 "which is not a class or enumeration type",
6292 token
->u
.tree_check_value
->value
);
6293 parser
->scope
= error_mark_node
;
6297 cp_lexer_consume_token (parser
->lexer
);
6300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6301 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6303 /* If we have a non-type template-id followed by ::, it can't
6304 possibly be valid. */
6305 token
= cp_lexer_peek_token (parser
->lexer
);
6306 tree tid
= token
->u
.tree_check_value
->value
;
6307 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6308 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6310 tree tmpl
= NULL_TREE
;
6311 if (is_overloaded_fn (tid
))
6313 tree fns
= get_fns (tid
);
6314 if (OVL_SINGLE_P (fns
))
6315 tmpl
= OVL_FIRST (fns
);
6316 error_at (token
->location
, "function template-id %qD "
6317 "in nested-name-specifier", tid
);
6321 /* Variable template. */
6322 tmpl
= TREE_OPERAND (tid
, 0);
6323 gcc_assert (variable_template_p (tmpl
));
6324 error_at (token
->location
, "variable template-id %qD "
6325 "in nested-name-specifier", tid
);
6328 inform (DECL_SOURCE_LOCATION (tmpl
),
6329 "%qD declared here", tmpl
);
6331 parser
->scope
= error_mark_node
;
6335 cp_lexer_consume_token (parser
->lexer
);
6336 cp_lexer_consume_token (parser
->lexer
);
6340 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6342 /* If the next token is an identifier, and the one after
6343 that is a `::', then any valid interpretation would have
6344 found a class-or-namespace-name. */
6345 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6346 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6348 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6351 token
= cp_lexer_consume_token (parser
->lexer
);
6354 if (!token
->error_reported
)
6357 tree ambiguous_decls
;
6359 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6361 /*is_template=*/false,
6362 /*is_namespace=*/false,
6363 /*check_dependency=*/true,
6366 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6367 error_at (token
->location
,
6368 "%qD used without template arguments",
6370 else if (ambiguous_decls
)
6372 // cp_parser_lookup_name has the same diagnostic,
6373 // thus make sure to emit it at most once.
6374 if (cp_parser_uncommitted_to_tentative_parse_p
6377 error_at (token
->location
,
6378 "reference to %qD is ambiguous",
6380 print_candidates (ambiguous_decls
);
6382 decl
= error_mark_node
;
6386 if (cxx_dialect
!= cxx98
)
6387 cp_parser_name_lookup_error
6388 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6391 cp_parser_name_lookup_error
6392 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6396 parser
->scope
= error_mark_node
;
6398 /* Treat this as a successful nested-name-specifier
6403 If the name found is not a class-name (clause
6404 _class_) or namespace-name (_namespace.def_), the
6405 program is ill-formed. */
6408 cp_lexer_consume_token (parser
->lexer
);
6412 /* We've found one valid nested-name-specifier. */
6414 /* Name lookup always gives us a DECL. */
6415 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6416 new_scope
= TREE_TYPE (new_scope
);
6417 /* Uses of "template" must be followed by actual templates. */
6418 if (template_keyword_p
6419 && !(CLASS_TYPE_P (new_scope
)
6420 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6421 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6422 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6423 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6424 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6425 == TEMPLATE_ID_EXPR
)))
6426 permerror (input_location
, TYPE_P (new_scope
)
6427 ? G_("%qT is not a template")
6428 : G_("%qD is not a template"),
6430 /* If it is a class scope, try to complete it; we are about to
6431 be looking up names inside the class. */
6432 if (TYPE_P (new_scope
)
6433 /* Since checking types for dependency can be expensive,
6434 avoid doing it if the type is already complete. */
6435 && !COMPLETE_TYPE_P (new_scope
)
6436 /* Do not try to complete dependent types. */
6437 && !dependent_type_p (new_scope
))
6439 new_scope
= complete_type (new_scope
);
6440 /* If it is a typedef to current class, use the current
6441 class instead, as the typedef won't have any names inside
6443 if (!COMPLETE_TYPE_P (new_scope
)
6444 && currently_open_class (new_scope
))
6445 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6447 /* Make sure we look in the right scope the next time through
6449 parser
->scope
= new_scope
;
6452 /* If parsing tentatively, replace the sequence of tokens that makes
6453 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6454 token. That way, should we re-parse the token stream, we will
6455 not have to repeat the effort required to do the parse, nor will
6456 we issue duplicate error messages. */
6457 if (success
&& start
)
6461 token
= cp_lexer_token_at (parser
->lexer
, start
);
6462 /* Reset the contents of the START token. */
6463 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6464 /* Retrieve any deferred checks. Do not pop this access checks yet
6465 so the memory will not be reclaimed during token replacing below. */
6466 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6467 token
->u
.tree_check_value
->value
= parser
->scope
;
6468 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6469 token
->u
.tree_check_value
->qualifying_scope
=
6470 parser
->qualifying_scope
;
6471 token
->keyword
= RID_MAX
;
6473 /* Purge all subsequent tokens. */
6474 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6478 pop_to_parent_deferring_access_checks ();
6480 return success
? parser
->scope
: NULL_TREE
;
6483 /* Parse a nested-name-specifier. See
6484 cp_parser_nested_name_specifier_opt for details. This function
6485 behaves identically, except that it will an issue an error if no
6486 nested-name-specifier is present. */
6489 cp_parser_nested_name_specifier (cp_parser
*parser
,
6490 bool typename_keyword_p
,
6491 bool check_dependency_p
,
6493 bool is_declaration
)
6497 /* Look for the nested-name-specifier. */
6498 scope
= cp_parser_nested_name_specifier_opt (parser
,
6503 /* If it was not present, issue an error message. */
6506 cp_parser_error (parser
, "expected nested-name-specifier");
6507 parser
->scope
= NULL_TREE
;
6513 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6514 this is either a class-name or a namespace-name (which corresponds
6515 to the class-or-namespace-name production in the grammar). For
6516 C++0x, it can also be a type-name that refers to an enumeration
6517 type or a simple-template-id.
6519 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6520 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6521 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6522 TYPE_P is TRUE iff the next name should be taken as a class-name,
6523 even the same name is declared to be another entity in the same
6526 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6527 specified by the class-or-namespace-name. If neither is found the
6528 ERROR_MARK_NODE is returned. */
6531 cp_parser_qualifying_entity (cp_parser
*parser
,
6532 bool typename_keyword_p
,
6533 bool template_keyword_p
,
6534 bool check_dependency_p
,
6536 bool is_declaration
)
6539 tree saved_qualifying_scope
;
6540 tree saved_object_scope
;
6543 bool successful_parse_p
;
6545 /* DR 743: decltype can appear in a nested-name-specifier. */
6546 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6548 scope
= cp_parser_decltype (parser
);
6549 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6550 && !MAYBE_CLASS_TYPE_P (scope
))
6552 cp_parser_simulate_error (parser
);
6553 return error_mark_node
;
6555 if (TYPE_NAME (scope
))
6556 scope
= TYPE_NAME (scope
);
6560 /* Before we try to parse the class-name, we must save away the
6561 current PARSER->SCOPE since cp_parser_class_name will destroy
6563 saved_scope
= parser
->scope
;
6564 saved_qualifying_scope
= parser
->qualifying_scope
;
6565 saved_object_scope
= parser
->object_scope
;
6566 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6567 there is no need to look for a namespace-name. */
6568 only_class_p
= template_keyword_p
6569 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6571 cp_parser_parse_tentatively (parser
);
6572 scope
= cp_parser_class_name (parser
,
6575 type_p
? class_type
: none_type
,
6577 /*class_head_p=*/false,
6579 /*enum_ok=*/cxx_dialect
> cxx98
);
6580 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6581 /* If that didn't work, try for a namespace-name. */
6582 if (!only_class_p
&& !successful_parse_p
)
6584 /* Restore the saved scope. */
6585 parser
->scope
= saved_scope
;
6586 parser
->qualifying_scope
= saved_qualifying_scope
;
6587 parser
->object_scope
= saved_object_scope
;
6588 /* If we are not looking at an identifier followed by the scope
6589 resolution operator, then this is not part of a
6590 nested-name-specifier. (Note that this function is only used
6591 to parse the components of a nested-name-specifier.) */
6592 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6593 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6594 return error_mark_node
;
6595 scope
= cp_parser_namespace_name (parser
);
6601 /* Return true if we are looking at a compound-literal, false otherwise. */
6604 cp_parser_compound_literal_p (cp_parser
*parser
)
6606 cp_lexer_save_tokens (parser
->lexer
);
6608 /* Skip tokens until the next token is a closing parenthesis.
6609 If we find the closing `)', and the next token is a `{', then
6610 we are looking at a compound-literal. */
6611 bool compound_literal_p
6612 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6613 /*consume_paren=*/true)
6614 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6616 /* Roll back the tokens we skipped. */
6617 cp_lexer_rollback_tokens (parser
->lexer
);
6619 return compound_literal_p
;
6622 /* Return true if EXPR is the integer constant zero or a complex constant
6623 of zero, without any folding, but ignoring location wrappers. */
6626 literal_integer_zerop (const_tree expr
)
6628 return (location_wrapper_p (expr
)
6629 && integer_zerop (TREE_OPERAND (expr
, 0)));
6632 /* Parse a postfix-expression.
6636 postfix-expression [ expression ]
6637 postfix-expression ( expression-list [opt] )
6638 simple-type-specifier ( expression-list [opt] )
6639 typename :: [opt] nested-name-specifier identifier
6640 ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier template [opt] template-id
6642 ( expression-list [opt] )
6643 postfix-expression . template [opt] id-expression
6644 postfix-expression -> template [opt] id-expression
6645 postfix-expression . pseudo-destructor-name
6646 postfix-expression -> pseudo-destructor-name
6647 postfix-expression ++
6648 postfix-expression --
6649 dynamic_cast < type-id > ( expression )
6650 static_cast < type-id > ( expression )
6651 reinterpret_cast < type-id > ( expression )
6652 const_cast < type-id > ( expression )
6653 typeid ( expression )
6659 ( type-id ) { initializer-list , [opt] }
6661 This extension is a GNU version of the C99 compound-literal
6662 construct. (The C99 grammar uses `type-name' instead of `type-id',
6663 but they are essentially the same concept.)
6665 If ADDRESS_P is true, the postfix expression is the operand of the
6666 `&' operator. CAST_P is true if this expression is the target of a
6669 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6670 class member access expressions [expr.ref].
6672 Returns a representation of the expression. */
6675 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6676 bool member_access_only_p
, bool decltype_p
,
6677 cp_id_kind
* pidk_return
)
6682 cp_id_kind idk
= CP_ID_KIND_NONE
;
6683 cp_expr postfix_expression
= NULL_TREE
;
6684 bool is_member_access
= false;
6686 /* Peek at the next token. */
6687 token
= cp_lexer_peek_token (parser
->lexer
);
6688 loc
= token
->location
;
6689 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6691 /* Some of the productions are determined by keywords. */
6692 keyword
= token
->keyword
;
6702 const char *saved_message
;
6703 bool saved_in_type_id_in_expr_p
;
6705 /* All of these can be handled in the same way from the point
6706 of view of parsing. Begin by consuming the token
6707 identifying the cast. */
6708 cp_lexer_consume_token (parser
->lexer
);
6710 /* New types cannot be defined in the cast. */
6711 saved_message
= parser
->type_definition_forbidden_message
;
6712 parser
->type_definition_forbidden_message
6713 = G_("types may not be defined in casts");
6715 /* Look for the opening `<'. */
6716 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6717 /* Parse the type to which we are casting. */
6718 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6719 parser
->in_type_id_in_expr_p
= true;
6720 type
= cp_parser_type_id (parser
);
6721 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6722 /* Look for the closing `>'. */
6723 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6724 /* Restore the old message. */
6725 parser
->type_definition_forbidden_message
= saved_message
;
6727 bool saved_greater_than_is_operator_p
6728 = parser
->greater_than_is_operator_p
;
6729 parser
->greater_than_is_operator_p
= true;
6731 /* And the expression which is being cast. */
6732 matching_parens parens
;
6733 parens
.require_open (parser
);
6734 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6735 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6737 location_t end_loc
= close_paren
?
6738 close_paren
->location
: UNKNOWN_LOCATION
;
6740 parser
->greater_than_is_operator_p
6741 = saved_greater_than_is_operator_p
;
6743 /* Only type conversions to integral or enumeration types
6744 can be used in constant-expressions. */
6745 if (!cast_valid_in_integral_constant_expression_p (type
)
6746 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6748 postfix_expression
= error_mark_node
;
6756 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6760 = build_static_cast (type
, expression
, tf_warning_or_error
);
6764 = build_reinterpret_cast (type
, expression
,
6765 tf_warning_or_error
);
6769 = build_const_cast (type
, expression
, tf_warning_or_error
);
6775 /* Construct a location e.g. :
6776 reinterpret_cast <int *> (expr)
6777 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6778 ranging from the start of the "*_cast" token to the final closing
6779 paren, with the caret at the start. */
6780 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6781 postfix_expression
.set_location (cp_cast_loc
);
6788 const char *saved_message
;
6789 bool saved_in_type_id_in_expr_p
;
6791 /* Consume the `typeid' token. */
6792 cp_lexer_consume_token (parser
->lexer
);
6793 /* Look for the `(' token. */
6794 matching_parens parens
;
6795 parens
.require_open (parser
);
6796 /* Types cannot be defined in a `typeid' expression. */
6797 saved_message
= parser
->type_definition_forbidden_message
;
6798 parser
->type_definition_forbidden_message
6799 = G_("types may not be defined in a %<typeid%> expression");
6800 /* We can't be sure yet whether we're looking at a type-id or an
6802 cp_parser_parse_tentatively (parser
);
6803 /* Try a type-id first. */
6804 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6805 parser
->in_type_id_in_expr_p
= true;
6806 type
= cp_parser_type_id (parser
);
6807 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6808 /* Look for the `)' token. Otherwise, we can't be sure that
6809 we're not looking at an expression: consider `typeid (int
6810 (3))', for example. */
6811 cp_token
*close_paren
= parens
.require_close (parser
);
6812 /* If all went well, simply lookup the type-id. */
6813 if (cp_parser_parse_definitely (parser
))
6814 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6815 /* Otherwise, fall back to the expression variant. */
6820 /* Look for an expression. */
6821 expression
= cp_parser_expression (parser
, & idk
);
6822 /* Compute its typeid. */
6823 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6824 /* Look for the `)' token. */
6825 close_paren
= parens
.require_close (parser
);
6827 /* Restore the saved message. */
6828 parser
->type_definition_forbidden_message
= saved_message
;
6829 /* `typeid' may not appear in an integral constant expression. */
6830 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6831 postfix_expression
= error_mark_node
;
6833 /* Construct a location e.g. :
6836 ranging from the start of the "typeid" token to the final closing
6837 paren, with the caret at the start. */
6840 location_t typeid_loc
6841 = make_location (start_loc
, start_loc
, close_paren
->location
);
6842 postfix_expression
.set_location (typeid_loc
);
6843 postfix_expression
.maybe_add_location_wrapper ();
6851 /* The syntax permitted here is the same permitted for an
6852 elaborated-type-specifier. */
6853 ++parser
->prevent_constrained_type_specifiers
;
6854 type
= cp_parser_elaborated_type_specifier (parser
,
6855 /*is_friend=*/false,
6856 /*is_declaration=*/false);
6857 --parser
->prevent_constrained_type_specifiers
;
6858 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6863 case RID_BUILTIN_SHUFFLE
:
6864 case RID_BUILTIN_LAUNDER
:
6866 vec
<tree
, va_gc
> *vec
;
6870 cp_lexer_consume_token (parser
->lexer
);
6871 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6872 /*cast_p=*/false, /*allow_expansion_p=*/true,
6873 /*non_constant_p=*/NULL
);
6876 postfix_expression
= error_mark_node
;
6880 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6886 if (vec
->length () == 1)
6888 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6891 error_at (loc
, "wrong number of arguments to "
6892 "%<__builtin_addressof%>");
6893 postfix_expression
= error_mark_node
;
6897 case RID_BUILTIN_LAUNDER
:
6898 if (vec
->length () == 1)
6899 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6900 tf_warning_or_error
);
6903 error_at (loc
, "wrong number of arguments to "
6904 "%<__builtin_launder%>");
6905 postfix_expression
= error_mark_node
;
6909 case RID_BUILTIN_SHUFFLE
:
6910 if (vec
->length () == 2)
6912 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6913 (*vec
)[1], tf_warning_or_error
);
6914 else if (vec
->length () == 3)
6916 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6917 (*vec
)[2], tf_warning_or_error
);
6920 error_at (loc
, "wrong number of arguments to "
6921 "%<__builtin_shuffle%>");
6922 postfix_expression
= error_mark_node
;
6936 /* If the next thing is a simple-type-specifier, we may be
6937 looking at a functional cast. We could also be looking at
6938 an id-expression. So, we try the functional cast, and if
6939 that doesn't work we fall back to the primary-expression. */
6940 cp_parser_parse_tentatively (parser
);
6941 /* Look for the simple-type-specifier. */
6942 ++parser
->prevent_constrained_type_specifiers
;
6943 type
= cp_parser_simple_type_specifier (parser
,
6944 /*decl_specs=*/NULL
,
6945 CP_PARSER_FLAGS_NONE
);
6946 --parser
->prevent_constrained_type_specifiers
;
6947 /* Parse the cast itself. */
6948 if (!cp_parser_error_occurred (parser
))
6950 = cp_parser_functional_cast (parser
, type
);
6951 /* If that worked, we're done. */
6952 if (cp_parser_parse_definitely (parser
))
6955 /* If the functional-cast didn't work out, try a
6956 compound-literal. */
6957 if (cp_parser_allow_gnu_extensions_p (parser
)
6958 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6960 cp_expr initializer
= NULL_TREE
;
6962 cp_parser_parse_tentatively (parser
);
6964 matching_parens parens
;
6965 parens
.consume_open (parser
);
6967 /* Avoid calling cp_parser_type_id pointlessly, see comment
6968 in cp_parser_cast_expression about c++/29234. */
6969 if (!cp_parser_compound_literal_p (parser
))
6970 cp_parser_simulate_error (parser
);
6973 /* Parse the type. */
6974 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6975 parser
->in_type_id_in_expr_p
= true;
6976 type
= cp_parser_type_id (parser
);
6977 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6978 parens
.require_close (parser
);
6981 /* If things aren't going well, there's no need to
6983 if (!cp_parser_error_occurred (parser
))
6985 bool non_constant_p
;
6986 /* Parse the brace-enclosed initializer list. */
6987 initializer
= cp_parser_braced_list (parser
,
6990 /* If that worked, we're definitely looking at a
6991 compound-literal expression. */
6992 if (cp_parser_parse_definitely (parser
))
6994 /* Warn the user that a compound literal is not
6995 allowed in standard C++. */
6996 pedwarn (input_location
, OPT_Wpedantic
,
6997 "ISO C++ forbids compound-literals");
6998 /* For simplicity, we disallow compound literals in
6999 constant-expressions. We could
7000 allow compound literals of integer type, whose
7001 initializer was a constant, in constant
7002 expressions. Permitting that usage, as a further
7003 extension, would not change the meaning of any
7004 currently accepted programs. (Of course, as
7005 compound literals are not part of ISO C++, the
7006 standard has nothing to say.) */
7007 if (cp_parser_non_integral_constant_expression (parser
,
7010 postfix_expression
= error_mark_node
;
7013 /* Form the representation of the compound-literal. */
7015 = finish_compound_literal (type
, initializer
,
7016 tf_warning_or_error
, fcl_c99
);
7017 postfix_expression
.set_location (initializer
.get_location ());
7022 /* It must be a primary-expression. */
7024 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7025 /*template_arg_p=*/false,
7032 /* Note that we don't need to worry about calling build_cplus_new on a
7033 class-valued CALL_EXPR in decltype when it isn't the end of the
7034 postfix-expression; unary_complex_lvalue will take care of that for
7037 /* Keep looping until the postfix-expression is complete. */
7040 if (idk
== CP_ID_KIND_UNQUALIFIED
7041 && identifier_p (postfix_expression
)
7042 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7043 /* It is not a Koenig lookup function call. */
7045 = unqualified_name_lookup_error (postfix_expression
);
7047 /* Peek at the next token. */
7048 token
= cp_lexer_peek_token (parser
->lexer
);
7050 switch (token
->type
)
7052 case CPP_OPEN_SQUARE
:
7053 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7055 cp_parser_error (parser
,
7056 "two consecutive %<[%> shall "
7057 "only introduce an attribute");
7058 return error_mark_node
;
7061 = cp_parser_postfix_open_square_expression (parser
,
7065 postfix_expression
.set_range (start_loc
,
7066 postfix_expression
.get_location ());
7068 idk
= CP_ID_KIND_NONE
;
7069 is_member_access
= false;
7072 case CPP_OPEN_PAREN
:
7073 /* postfix-expression ( expression-list [opt] ) */
7076 bool is_builtin_constant_p
;
7077 bool saved_integral_constant_expression_p
= false;
7078 bool saved_non_integral_constant_expression_p
= false;
7079 tsubst_flags_t complain
= complain_flags (decltype_p
);
7080 vec
<tree
, va_gc
> *args
;
7081 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7083 is_member_access
= false;
7085 is_builtin_constant_p
7086 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7087 if (is_builtin_constant_p
)
7089 /* The whole point of __builtin_constant_p is to allow
7090 non-constant expressions to appear as arguments. */
7091 saved_integral_constant_expression_p
7092 = parser
->integral_constant_expression_p
;
7093 saved_non_integral_constant_expression_p
7094 = parser
->non_integral_constant_expression_p
;
7095 parser
->integral_constant_expression_p
= false;
7097 args
= (cp_parser_parenthesized_expression_list
7099 /*cast_p=*/false, /*allow_expansion_p=*/true,
7100 /*non_constant_p=*/NULL
,
7101 /*close_paren_loc=*/&close_paren_loc
,
7102 /*wrap_locations_p=*/true));
7103 if (is_builtin_constant_p
)
7105 parser
->integral_constant_expression_p
7106 = saved_integral_constant_expression_p
;
7107 parser
->non_integral_constant_expression_p
7108 = saved_non_integral_constant_expression_p
;
7113 postfix_expression
= error_mark_node
;
7117 /* Function calls are not permitted in
7118 constant-expressions. */
7119 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7120 && cp_parser_non_integral_constant_expression (parser
,
7123 postfix_expression
= error_mark_node
;
7124 release_tree_vector (args
);
7129 if (idk
== CP_ID_KIND_UNQUALIFIED
7130 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7132 if (identifier_p (postfix_expression
))
7134 if (!args
->is_empty ())
7137 if (!any_type_dependent_arguments_p (args
))
7139 = perform_koenig_lookup (postfix_expression
, args
,
7144 = unqualified_fn_lookup_error (postfix_expression
);
7146 /* We do not perform argument-dependent lookup if
7147 normal lookup finds a non-function, in accordance
7148 with the expected resolution of DR 218. */
7149 else if (!args
->is_empty ()
7150 && is_overloaded_fn (postfix_expression
))
7152 tree fn
= get_first_fn (postfix_expression
);
7153 fn
= STRIP_TEMPLATE (fn
);
7155 /* Do not do argument dependent lookup if regular
7156 lookup finds a member function or a block-scope
7157 function declaration. [basic.lookup.argdep]/3 */
7158 if (!DECL_FUNCTION_MEMBER_P (fn
)
7159 && !DECL_LOCAL_FUNCTION_P (fn
))
7162 if (!any_type_dependent_arguments_p (args
))
7164 = perform_koenig_lookup (postfix_expression
, args
,
7170 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7172 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7173 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7175 if (processing_template_decl
7176 && (type_dependent_object_expression_p (instance
)
7177 || (!BASELINK_P (fn
)
7178 && TREE_CODE (fn
) != FIELD_DECL
)
7179 || type_dependent_expression_p (fn
)
7180 || any_type_dependent_arguments_p (args
)))
7182 maybe_generic_this_capture (instance
, fn
);
7184 = build_min_nt_call_vec (postfix_expression
, args
);
7185 release_tree_vector (args
);
7189 if (BASELINK_P (fn
))
7192 = (build_new_method_call
7193 (instance
, fn
, &args
, NULL_TREE
,
7194 (idk
== CP_ID_KIND_QUALIFIED
7195 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7202 = finish_call_expr (postfix_expression
, &args
,
7203 /*disallow_virtual=*/false,
7207 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7208 || TREE_CODE (postfix_expression
) == MEMBER_REF
7209 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7210 postfix_expression
= (build_offset_ref_call_from_tree
7211 (postfix_expression
, &args
,
7213 else if (idk
== CP_ID_KIND_QUALIFIED
)
7214 /* A call to a static class member, or a namespace-scope
7217 = finish_call_expr (postfix_expression
, &args
,
7218 /*disallow_virtual=*/true,
7222 /* All other function calls. */
7224 = finish_call_expr (postfix_expression
, &args
,
7225 /*disallow_virtual=*/false,
7229 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7231 location_t combined_loc
= make_location (token
->location
,
7234 postfix_expression
.set_location (combined_loc
);
7237 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7238 idk
= CP_ID_KIND_NONE
;
7240 release_tree_vector (args
);
7246 /* postfix-expression . template [opt] id-expression
7247 postfix-expression . pseudo-destructor-name
7248 postfix-expression -> template [opt] id-expression
7249 postfix-expression -> pseudo-destructor-name */
7251 /* Consume the `.' or `->' operator. */
7252 cp_lexer_consume_token (parser
->lexer
);
7255 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7259 is_member_access
= true;
7263 /* postfix-expression ++ */
7264 /* Consume the `++' token. */
7265 cp_lexer_consume_token (parser
->lexer
);
7266 /* Generate a representation for the complete expression. */
7268 = finish_increment_expr (postfix_expression
,
7269 POSTINCREMENT_EXPR
);
7270 /* Increments may not appear in constant-expressions. */
7271 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7272 postfix_expression
= error_mark_node
;
7273 idk
= CP_ID_KIND_NONE
;
7274 is_member_access
= false;
7277 case CPP_MINUS_MINUS
:
7278 /* postfix-expression -- */
7279 /* Consume the `--' token. */
7280 cp_lexer_consume_token (parser
->lexer
);
7281 /* Generate a representation for the complete expression. */
7283 = finish_increment_expr (postfix_expression
,
7284 POSTDECREMENT_EXPR
);
7285 /* Decrements may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7287 postfix_expression
= error_mark_node
;
7288 idk
= CP_ID_KIND_NONE
;
7289 is_member_access
= false;
7293 if (pidk_return
!= NULL
)
7294 * pidk_return
= idk
;
7295 if (member_access_only_p
)
7296 return is_member_access
7297 ? postfix_expression
7298 : cp_expr (error_mark_node
);
7300 return postfix_expression
;
7304 /* We should never get here. */
7306 return error_mark_node
;
7309 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7310 by cp_parser_builtin_offsetof. We're looking for
7312 postfix-expression [ expression ]
7313 postfix-expression [ braced-init-list ] (C++11)
7315 FOR_OFFSETOF is set if we're being called in that context, which
7316 changes how we deal with integer constant expressions. */
7319 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7320 tree postfix_expression
,
7324 tree index
= NULL_TREE
;
7325 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7326 bool saved_greater_than_is_operator_p
;
7328 /* Consume the `[' token. */
7329 cp_lexer_consume_token (parser
->lexer
);
7331 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7332 parser
->greater_than_is_operator_p
= true;
7334 /* Parse the index expression. */
7335 /* ??? For offsetof, there is a question of what to allow here. If
7336 offsetof is not being used in an integral constant expression context,
7337 then we *could* get the right answer by computing the value at runtime.
7338 If we are in an integral constant expression context, then we might
7339 could accept any constant expression; hard to say without analysis.
7340 Rather than open the barn door too wide right away, allow only integer
7341 constant expressions here. */
7343 index
= cp_parser_constant_expression (parser
);
7346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7348 bool expr_nonconst_p
;
7349 cp_lexer_set_source_position (parser
->lexer
);
7350 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7351 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7354 index
= cp_parser_expression (parser
);
7357 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7359 /* Look for the closing `]'. */
7360 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7362 /* Build the ARRAY_REF. */
7363 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7366 /* When not doing offsetof, array references are not permitted in
7367 constant-expressions. */
7369 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7370 postfix_expression
= error_mark_node
;
7372 return postfix_expression
;
7375 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7376 dereference of incomplete type, returns true if error_mark_node should
7377 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7378 and *DEPENDENT_P. */
7381 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7384 /* In a template, be permissive by treating an object expression
7385 of incomplete type as dependent (after a pedwarn). */
7386 diagnostic_t kind
= (processing_template_decl
7387 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7389 switch (TREE_CODE (*postfix_expression
))
7392 case REINTERPRET_CAST_EXPR
:
7393 case CONST_CAST_EXPR
:
7394 case STATIC_CAST_EXPR
:
7395 case DYNAMIC_CAST_EXPR
:
7396 case IMPLICIT_CONV_EXPR
:
7397 case VIEW_CONVERT_EXPR
:
7398 case NON_LVALUE_EXPR
:
7402 /* Don't emit any diagnostic for OVERLOADs. */
7406 /* Avoid clobbering e.g. DECLs. */
7407 if (!EXPR_P (*postfix_expression
))
7412 if (kind
== DK_IGNORED
)
7415 location_t exploc
= location_of (*postfix_expression
);
7416 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7417 if (!MAYBE_CLASS_TYPE_P (*scope
))
7419 if (kind
== DK_ERROR
)
7420 *scope
= *postfix_expression
= error_mark_node
;
7421 else if (processing_template_decl
)
7423 *dependent_p
= true;
7424 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7429 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7430 by cp_parser_builtin_offsetof. We're looking for
7432 postfix-expression . template [opt] id-expression
7433 postfix-expression . pseudo-destructor-name
7434 postfix-expression -> template [opt] id-expression
7435 postfix-expression -> pseudo-destructor-name
7437 FOR_OFFSETOF is set if we're being called in that context. That sorta
7438 limits what of the above we'll actually accept, but nevermind.
7439 TOKEN_TYPE is the "." or "->" token, which will already have been
7440 removed from the stream. */
7443 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7444 enum cpp_ttype token_type
,
7445 cp_expr postfix_expression
,
7446 bool for_offsetof
, cp_id_kind
*idk
,
7447 location_t location
)
7451 bool pseudo_destructor_p
;
7452 tree scope
= NULL_TREE
;
7453 location_t start_loc
= postfix_expression
.get_start ();
7455 /* If this is a `->' operator, dereference the pointer. */
7456 if (token_type
== CPP_DEREF
)
7457 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7458 tf_warning_or_error
);
7459 /* Check to see whether or not the expression is type-dependent and
7460 not the current instantiation. */
7461 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7462 /* The identifier following the `->' or `.' is not qualified. */
7463 parser
->scope
= NULL_TREE
;
7464 parser
->qualifying_scope
= NULL_TREE
;
7465 parser
->object_scope
= NULL_TREE
;
7466 *idk
= CP_ID_KIND_NONE
;
7468 /* Enter the scope corresponding to the type of the object
7469 given by the POSTFIX_EXPRESSION. */
7472 scope
= TREE_TYPE (postfix_expression
);
7473 /* According to the standard, no expression should ever have
7474 reference type. Unfortunately, we do not currently match
7475 the standard in this respect in that our internal representation
7476 of an expression may have reference type even when the standard
7477 says it does not. Therefore, we have to manually obtain the
7478 underlying type here. */
7479 scope
= non_reference (scope
);
7480 /* The type of the POSTFIX_EXPRESSION must be complete. */
7481 /* Unlike the object expression in other contexts, *this is not
7482 required to be of complete type for purposes of class member
7483 access (5.2.5) outside the member function body. */
7484 if (postfix_expression
!= current_class_ref
7485 && scope
!= error_mark_node
7486 && !currently_open_class (scope
))
7488 scope
= complete_type (scope
);
7489 if (!COMPLETE_TYPE_P (scope
)
7490 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7492 return error_mark_node
;
7497 /* Let the name lookup machinery know that we are processing a
7498 class member access expression. */
7499 parser
->context
->object_type
= scope
;
7500 /* If something went wrong, we want to be able to discern that case,
7501 as opposed to the case where there was no SCOPE due to the type
7502 of expression being dependent. */
7504 scope
= error_mark_node
;
7505 /* If the SCOPE was erroneous, make the various semantic analysis
7506 functions exit quickly -- and without issuing additional error
7508 if (scope
== error_mark_node
)
7509 postfix_expression
= error_mark_node
;
7514 /* Tell cp_parser_lookup_name that there was an object, even though it's
7516 parser
->context
->object_type
= unknown_type_node
;
7518 /* Assume this expression is not a pseudo-destructor access. */
7519 pseudo_destructor_p
= false;
7521 /* If the SCOPE is a scalar type, then, if this is a valid program,
7522 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7523 is type dependent, it can be pseudo-destructor-name or something else.
7524 Try to parse it as pseudo-destructor-name first. */
7525 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7530 cp_parser_parse_tentatively (parser
);
7531 /* Parse the pseudo-destructor-name. */
7533 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7536 && (cp_parser_error_occurred (parser
)
7537 || !SCALAR_TYPE_P (type
)))
7538 cp_parser_abort_tentative_parse (parser
);
7539 else if (cp_parser_parse_definitely (parser
))
7541 pseudo_destructor_p
= true;
7543 = finish_pseudo_destructor_expr (postfix_expression
,
7548 if (!pseudo_destructor_p
)
7550 /* If the SCOPE is not a scalar type, we are looking at an
7551 ordinary class member access expression, rather than a
7552 pseudo-destructor-name. */
7554 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7555 /* Parse the id-expression. */
7556 name
= (cp_parser_id_expression
7558 cp_parser_optional_template_keyword (parser
),
7559 /*check_dependency_p=*/true,
7561 /*declarator_p=*/false,
7562 /*optional_p=*/false));
7563 /* In general, build a SCOPE_REF if the member name is qualified.
7564 However, if the name was not dependent and has already been
7565 resolved; there is no need to build the SCOPE_REF. For example;
7567 struct X { void f(); };
7568 template <typename T> void f(T* t) { t->X::f(); }
7570 Even though "t" is dependent, "X::f" is not and has been resolved
7571 to a BASELINK; there is no need to include scope information. */
7573 /* But we do need to remember that there was an explicit scope for
7574 virtual function calls. */
7576 *idk
= CP_ID_KIND_QUALIFIED
;
7578 /* If the name is a template-id that names a type, we will get a
7579 TYPE_DECL here. That is invalid code. */
7580 if (TREE_CODE (name
) == TYPE_DECL
)
7582 error_at (token
->location
, "invalid use of %qD", name
);
7583 postfix_expression
= error_mark_node
;
7587 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7589 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7591 error_at (token
->location
, "%<%D::%D%> is not a class member",
7592 parser
->scope
, name
);
7593 postfix_expression
= error_mark_node
;
7596 name
= build_qualified_name (/*type=*/NULL_TREE
,
7600 parser
->scope
= NULL_TREE
;
7601 parser
->qualifying_scope
= NULL_TREE
;
7602 parser
->object_scope
= NULL_TREE
;
7604 if (parser
->scope
&& name
&& BASELINK_P (name
))
7605 adjust_result_of_qualified_name_lookup
7606 (name
, parser
->scope
, scope
);
7608 = finish_class_member_access_expr (postfix_expression
, name
,
7610 tf_warning_or_error
);
7611 /* Build a location e.g.:
7614 where the caret is at the deref token, ranging from
7615 the start of postfix_expression to the end of the access expr. */
7617 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7618 location_t combined_loc
7619 = make_location (input_location
, start_loc
, end_loc
);
7620 protected_set_expr_location (postfix_expression
, combined_loc
);
7624 /* We no longer need to look up names in the scope of the object on
7625 the left-hand side of the `.' or `->' operator. */
7626 parser
->context
->object_type
= NULL_TREE
;
7628 /* Outside of offsetof, these operators may not appear in
7629 constant-expressions. */
7631 && (cp_parser_non_integral_constant_expression
7632 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7633 postfix_expression
= error_mark_node
;
7635 return postfix_expression
;
7638 /* Parse a parenthesized expression-list.
7641 assignment-expression
7642 expression-list, assignment-expression
7647 identifier, expression-list
7649 CAST_P is true if this expression is the target of a cast.
7651 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7654 WRAP_LOCATIONS_P is true if expressions within this list for which
7655 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7656 their source locations.
7658 Returns a vector of trees. Each element is a representation of an
7659 assignment-expression. NULL is returned if the ( and or ) are
7660 missing. An empty, but allocated, vector is returned on no
7661 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7662 if we are parsing an attribute list for an attribute that wants a
7663 plain identifier argument, normal_attr for an attribute that wants
7664 an expression, or non_attr if we aren't parsing an attribute list. If
7665 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7666 not all of the expressions in the list were constant.
7667 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7668 will be written to with the location of the closing parenthesis. If
7669 an error occurs, it may or may not be written to. */
7671 static vec
<tree
, va_gc
> *
7672 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7673 int is_attribute_list
,
7675 bool allow_expansion_p
,
7676 bool *non_constant_p
,
7677 location_t
*close_paren_loc
,
7678 bool wrap_locations_p
)
7680 vec
<tree
, va_gc
> *expression_list
;
7681 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7682 tree identifier
= NULL_TREE
;
7683 bool saved_greater_than_is_operator_p
;
7685 /* Assume all the expressions will be constant. */
7687 *non_constant_p
= false;
7689 matching_parens parens
;
7690 if (!parens
.require_open (parser
))
7693 expression_list
= make_tree_vector ();
7695 /* Within a parenthesized expression, a `>' token is always
7696 the greater-than operator. */
7697 saved_greater_than_is_operator_p
7698 = parser
->greater_than_is_operator_p
;
7699 parser
->greater_than_is_operator_p
= true;
7701 cp_expr
expr (NULL_TREE
);
7703 /* Consume expressions until there are no more. */
7704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7707 /* At the beginning of attribute lists, check to see if the
7708 next token is an identifier. */
7709 if (is_attribute_list
== id_attr
7710 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7714 /* Consume the identifier. */
7715 token
= cp_lexer_consume_token (parser
->lexer
);
7716 /* Save the identifier. */
7717 identifier
= token
->u
.value
;
7721 bool expr_non_constant_p
;
7723 /* Parse the next assignment-expression. */
7724 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7726 /* A braced-init-list. */
7727 cp_lexer_set_source_position (parser
->lexer
);
7728 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7729 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7730 if (non_constant_p
&& expr_non_constant_p
)
7731 *non_constant_p
= true;
7733 else if (non_constant_p
)
7735 expr
= (cp_parser_constant_expression
7736 (parser
, /*allow_non_constant_p=*/true,
7737 &expr_non_constant_p
));
7738 if (expr_non_constant_p
)
7739 *non_constant_p
= true;
7742 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7746 expr
= instantiate_non_dependent_expr (expr
);
7748 /* If we have an ellipsis, then this is an expression
7750 if (allow_expansion_p
7751 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7753 /* Consume the `...'. */
7754 cp_lexer_consume_token (parser
->lexer
);
7756 /* Build the argument pack. */
7757 expr
= make_pack_expansion (expr
);
7760 if (wrap_locations_p
)
7761 expr
.maybe_add_location_wrapper ();
7763 /* Add it to the list. We add error_mark_node
7764 expressions to the list, so that we can still tell if
7765 the correct form for a parenthesized expression-list
7766 is found. That gives better errors. */
7767 vec_safe_push (expression_list
, expr
.get_value ());
7769 if (expr
== error_mark_node
)
7773 /* After the first item, attribute lists look the same as
7774 expression lists. */
7775 is_attribute_list
= non_attr
;
7778 /* If the next token isn't a `,', then we are done. */
7779 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7782 /* Otherwise, consume the `,' and keep going. */
7783 cp_lexer_consume_token (parser
->lexer
);
7786 if (close_paren_loc
)
7787 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7789 if (!parens
.require_close (parser
))
7794 /* We try and resync to an unnested comma, as that will give the
7795 user better diagnostics. */
7796 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7797 /*recovering=*/true,
7799 /*consume_paren=*/true);
7804 parser
->greater_than_is_operator_p
7805 = saved_greater_than_is_operator_p
;
7810 parser
->greater_than_is_operator_p
7811 = saved_greater_than_is_operator_p
;
7814 vec_safe_insert (expression_list
, 0, identifier
);
7816 return expression_list
;
7819 /* Parse a pseudo-destructor-name.
7821 pseudo-destructor-name:
7822 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7823 :: [opt] nested-name-specifier template template-id :: ~ type-name
7824 :: [opt] nested-name-specifier [opt] ~ type-name
7826 If either of the first two productions is used, sets *SCOPE to the
7827 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7828 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7829 or ERROR_MARK_NODE if the parse fails. */
7832 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7837 bool nested_name_specifier_p
;
7840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7841 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7842 && !type_dependent_expression_p (object
))
7844 if (cxx_dialect
< cxx14
)
7845 pedwarn (input_location
, 0,
7846 "%<~auto%> only available with "
7847 "-std=c++14 or -std=gnu++14");
7848 cp_lexer_consume_token (parser
->lexer
);
7849 cp_lexer_consume_token (parser
->lexer
);
7851 *type
= TREE_TYPE (object
);
7855 /* Assume that things will not work out. */
7856 *type
= error_mark_node
;
7858 /* Look for the optional `::' operator. */
7859 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7860 /* Look for the optional nested-name-specifier. */
7861 nested_name_specifier_p
7862 = (cp_parser_nested_name_specifier_opt (parser
,
7863 /*typename_keyword_p=*/false,
7864 /*check_dependency_p=*/true,
7866 /*is_declaration=*/false)
7868 /* Now, if we saw a nested-name-specifier, we might be doing the
7869 second production. */
7870 if (nested_name_specifier_p
7871 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7873 /* Consume the `template' keyword. */
7874 cp_lexer_consume_token (parser
->lexer
);
7875 /* Parse the template-id. */
7876 cp_parser_template_id (parser
,
7877 /*template_keyword_p=*/true,
7878 /*check_dependency_p=*/false,
7880 /*is_declaration=*/true);
7881 /* Look for the `::' token. */
7882 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7884 /* If the next token is not a `~', then there might be some
7885 additional qualification. */
7886 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7888 /* At this point, we're looking for "type-name :: ~". The type-name
7889 must not be a class-name, since this is a pseudo-destructor. So,
7890 it must be either an enum-name, or a typedef-name -- both of which
7891 are just identifiers. So, we peek ahead to check that the "::"
7892 and "~" tokens are present; if they are not, then we can avoid
7893 calling type_name. */
7894 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7895 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7896 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7898 cp_parser_error (parser
, "non-scalar type");
7902 /* Look for the type-name. */
7903 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7904 if (*scope
== error_mark_node
)
7907 /* Look for the `::' token. */
7908 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7913 /* Look for the `~'. */
7914 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7916 /* Once we see the ~, this has to be a pseudo-destructor. */
7917 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7918 cp_parser_commit_to_topmost_tentative_parse (parser
);
7920 /* Look for the type-name again. We are not responsible for
7921 checking that it matches the first type-name. */
7922 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7925 /* Parse a unary-expression.
7931 unary-operator cast-expression
7932 sizeof unary-expression
7934 alignof ( type-id ) [C++0x]
7941 __extension__ cast-expression
7942 __alignof__ unary-expression
7943 __alignof__ ( type-id )
7944 alignof unary-expression [C++0x]
7945 __real__ cast-expression
7946 __imag__ cast-expression
7948 sizeof ( type-id ) { initializer-list , [opt] }
7949 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7950 __alignof__ ( type-id ) { initializer-list , [opt] }
7952 ADDRESS_P is true iff the unary-expression is appearing as the
7953 operand of the `&' operator. CAST_P is true if this expression is
7954 the target of a cast.
7956 Returns a representation of the expression. */
7959 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7960 bool address_p
, bool cast_p
, bool decltype_p
)
7963 enum tree_code unary_operator
;
7965 /* Peek at the next token. */
7966 token
= cp_lexer_peek_token (parser
->lexer
);
7967 /* Some keywords give away the kind of expression. */
7968 if (token
->type
== CPP_KEYWORD
)
7970 enum rid keyword
= token
->keyword
;
7979 location_t start_loc
= token
->location
;
7981 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7982 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
7984 /* Consume the token. */
7985 cp_lexer_consume_token (parser
->lexer
);
7986 /* Parse the operand. */
7987 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7989 if (TYPE_P (operand
))
7990 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
7994 /* ISO C++ defines alignof only with types, not with
7995 expressions. So pedwarn if alignof is used with a non-
7996 type expression. However, __alignof__ is ok. */
7998 pedwarn (token
->location
, OPT_Wpedantic
,
7999 "ISO C++ does not allow %<alignof%> "
8002 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8004 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8005 SIZEOF_EXPR with the original operand. */
8006 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8008 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8010 if (!processing_template_decl
&& TYPE_P (operand
))
8012 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8013 build1 (NOP_EXPR
, operand
,
8015 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8018 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8019 TREE_SIDE_EFFECTS (ret
) = 0;
8020 TREE_READONLY (ret
) = 1;
8024 /* Construct a location e.g. :
8027 with start == caret at the start of the "alignof"/"sizeof"
8028 token, with the endpoint at the final closing paren. */
8029 location_t finish_loc
8030 = cp_lexer_previous_token (parser
->lexer
)->location
;
8031 location_t compound_loc
8032 = make_location (start_loc
, start_loc
, finish_loc
);
8034 cp_expr
ret_expr (ret
);
8035 ret_expr
.set_location (compound_loc
);
8036 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8041 return cp_parser_new_expression (parser
);
8044 return cp_parser_delete_expression (parser
);
8048 /* The saved value of the PEDANTIC flag. */
8052 /* Save away the PEDANTIC flag. */
8053 cp_parser_extension_opt (parser
, &saved_pedantic
);
8054 /* Parse the cast-expression. */
8055 expr
= cp_parser_simple_cast_expression (parser
);
8056 /* Restore the PEDANTIC flag. */
8057 pedantic
= saved_pedantic
;
8067 /* Consume the `__real__' or `__imag__' token. */
8068 cp_lexer_consume_token (parser
->lexer
);
8069 /* Parse the cast-expression. */
8070 expression
= cp_parser_simple_cast_expression (parser
);
8071 /* Create the complete representation. */
8072 return build_x_unary_op (token
->location
,
8073 (keyword
== RID_REALPART
8074 ? REALPART_EXPR
: IMAGPART_EXPR
),
8076 tf_warning_or_error
);
8080 case RID_TRANSACTION_ATOMIC
:
8081 case RID_TRANSACTION_RELAXED
:
8082 return cp_parser_transaction_expression (parser
, keyword
);
8087 const char *saved_message
;
8088 bool saved_integral_constant_expression_p
;
8089 bool saved_non_integral_constant_expression_p
;
8090 bool saved_greater_than_is_operator_p
;
8092 location_t start_loc
= token
->location
;
8094 cp_lexer_consume_token (parser
->lexer
);
8095 matching_parens parens
;
8096 parens
.require_open (parser
);
8098 saved_message
= parser
->type_definition_forbidden_message
;
8099 parser
->type_definition_forbidden_message
8100 = G_("types may not be defined in %<noexcept%> expressions");
8102 saved_integral_constant_expression_p
8103 = parser
->integral_constant_expression_p
;
8104 saved_non_integral_constant_expression_p
8105 = parser
->non_integral_constant_expression_p
;
8106 parser
->integral_constant_expression_p
= false;
8108 saved_greater_than_is_operator_p
8109 = parser
->greater_than_is_operator_p
;
8110 parser
->greater_than_is_operator_p
= true;
8112 ++cp_unevaluated_operand
;
8113 ++c_inhibit_evaluation_warnings
;
8114 ++cp_noexcept_operand
;
8115 expr
= cp_parser_expression (parser
);
8116 --cp_noexcept_operand
;
8117 --c_inhibit_evaluation_warnings
;
8118 --cp_unevaluated_operand
;
8120 parser
->greater_than_is_operator_p
8121 = saved_greater_than_is_operator_p
;
8123 parser
->integral_constant_expression_p
8124 = saved_integral_constant_expression_p
;
8125 parser
->non_integral_constant_expression_p
8126 = saved_non_integral_constant_expression_p
;
8128 parser
->type_definition_forbidden_message
= saved_message
;
8130 location_t finish_loc
8131 = cp_lexer_peek_token (parser
->lexer
)->location
;
8132 parens
.require_close (parser
);
8134 /* Construct a location of the form:
8137 with start == caret, finishing at the close-paren. */
8138 location_t noexcept_loc
8139 = make_location (start_loc
, start_loc
, finish_loc
);
8141 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8150 /* Look for the `:: new' and `:: delete', which also signal the
8151 beginning of a new-expression, or delete-expression,
8152 respectively. If the next token is `::', then it might be one of
8154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8158 /* See if the token after the `::' is one of the keywords in
8159 which we're interested. */
8160 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8161 /* If it's `new', we have a new-expression. */
8162 if (keyword
== RID_NEW
)
8163 return cp_parser_new_expression (parser
);
8164 /* Similarly, for `delete'. */
8165 else if (keyword
== RID_DELETE
)
8166 return cp_parser_delete_expression (parser
);
8169 /* Look for a unary operator. */
8170 unary_operator
= cp_parser_unary_operator (token
);
8171 /* The `++' and `--' operators can be handled similarly, even though
8172 they are not technically unary-operators in the grammar. */
8173 if (unary_operator
== ERROR_MARK
)
8175 if (token
->type
== CPP_PLUS_PLUS
)
8176 unary_operator
= PREINCREMENT_EXPR
;
8177 else if (token
->type
== CPP_MINUS_MINUS
)
8178 unary_operator
= PREDECREMENT_EXPR
;
8179 /* Handle the GNU address-of-label extension. */
8180 else if (cp_parser_allow_gnu_extensions_p (parser
)
8181 && token
->type
== CPP_AND_AND
)
8185 location_t start_loc
= token
->location
;
8187 /* Consume the '&&' token. */
8188 cp_lexer_consume_token (parser
->lexer
);
8189 /* Look for the identifier. */
8190 location_t finish_loc
8191 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8192 identifier
= cp_parser_identifier (parser
);
8193 /* Construct a location of the form:
8196 with caret==start at the "&&", finish at the end of the label. */
8197 location_t combined_loc
8198 = make_location (start_loc
, start_loc
, finish_loc
);
8199 /* Create an expression representing the address. */
8200 expression
= finish_label_address_expr (identifier
, combined_loc
);
8201 if (cp_parser_non_integral_constant_expression (parser
,
8203 expression
= error_mark_node
;
8207 if (unary_operator
!= ERROR_MARK
)
8209 cp_expr cast_expression
;
8210 cp_expr expression
= error_mark_node
;
8211 non_integral_constant non_constant_p
= NIC_NONE
;
8212 location_t loc
= token
->location
;
8213 tsubst_flags_t complain
= complain_flags (decltype_p
);
8215 /* Consume the operator token. */
8216 token
= cp_lexer_consume_token (parser
->lexer
);
8217 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8219 /* Parse the cast-expression. */
8221 = cp_parser_cast_expression (parser
,
8222 unary_operator
== ADDR_EXPR
,
8228 OP_TOKEN CAST_EXPRESSION
8229 ^~~~~~~~~~~~~~~~~~~~~~~~~
8230 with start==caret at the operator token, and
8231 extending to the end of the cast_expression. */
8232 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8234 /* Now, build an appropriate representation. */
8235 switch (unary_operator
)
8238 non_constant_p
= NIC_STAR
;
8239 expression
= build_x_indirect_ref (loc
, cast_expression
,
8242 /* TODO: build_x_indirect_ref does not always honor the
8243 location, so ensure it is set. */
8244 expression
.set_location (loc
);
8248 non_constant_p
= NIC_ADDR
;
8251 expression
= build_x_unary_op (loc
, unary_operator
,
8254 /* TODO: build_x_unary_op does not always honor the location,
8255 so ensure it is set. */
8256 expression
.set_location (loc
);
8259 case PREINCREMENT_EXPR
:
8260 case PREDECREMENT_EXPR
:
8261 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8262 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8265 /* Immediately fold negation of a constant, unless the constant is 0
8266 (since -0 == 0) or it would overflow. */
8267 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8268 && CONSTANT_CLASS_P (cast_expression
)
8269 && !integer_zerop (cast_expression
)
8270 && !TREE_OVERFLOW (cast_expression
))
8272 tree folded
= fold_build1 (unary_operator
,
8273 TREE_TYPE (cast_expression
),
8275 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8277 expression
= cp_expr (folded
, loc
);
8282 case UNARY_PLUS_EXPR
:
8283 case TRUTH_NOT_EXPR
:
8284 expression
= finish_unary_op_expr (loc
, unary_operator
,
8285 cast_expression
, complain
);
8292 if (non_constant_p
!= NIC_NONE
8293 && cp_parser_non_integral_constant_expression (parser
,
8295 expression
= error_mark_node
;
8300 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8301 /*member_access_only_p=*/false,
8306 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8307 unary-operator, the corresponding tree code is returned. */
8309 static enum tree_code
8310 cp_parser_unary_operator (cp_token
* token
)
8312 switch (token
->type
)
8315 return INDIRECT_REF
;
8321 return UNARY_PLUS_EXPR
;
8327 return TRUTH_NOT_EXPR
;
8330 return BIT_NOT_EXPR
;
8337 /* Parse a new-expression.
8340 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8341 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8343 Returns a representation of the expression. */
8346 cp_parser_new_expression (cp_parser
* parser
)
8348 bool global_scope_p
;
8349 vec
<tree
, va_gc
> *placement
;
8351 vec
<tree
, va_gc
> *initializer
;
8352 tree nelts
= NULL_TREE
;
8355 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8357 /* Look for the optional `::' operator. */
8359 = (cp_parser_global_scope_opt (parser
,
8360 /*current_scope_valid_p=*/false)
8362 /* Look for the `new' operator. */
8363 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8364 /* There's no easy way to tell a new-placement from the
8365 `( type-id )' construct. */
8366 cp_parser_parse_tentatively (parser
);
8367 /* Look for a new-placement. */
8368 placement
= cp_parser_new_placement (parser
);
8369 /* If that didn't work out, there's no new-placement. */
8370 if (!cp_parser_parse_definitely (parser
))
8372 if (placement
!= NULL
)
8373 release_tree_vector (placement
);
8377 /* If the next token is a `(', then we have a parenthesized
8379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8382 const char *saved_message
= parser
->type_definition_forbidden_message
;
8384 /* Consume the `('. */
8385 matching_parens parens
;
8386 parens
.consume_open (parser
);
8388 /* Parse the type-id. */
8389 parser
->type_definition_forbidden_message
8390 = G_("types may not be defined in a new-expression");
8392 type_id_in_expr_sentinel
s (parser
);
8393 type
= cp_parser_type_id (parser
);
8395 parser
->type_definition_forbidden_message
= saved_message
;
8397 /* Look for the closing `)'. */
8398 parens
.require_close (parser
);
8399 token
= cp_lexer_peek_token (parser
->lexer
);
8400 /* There should not be a direct-new-declarator in this production,
8401 but GCC used to allowed this, so we check and emit a sensible error
8402 message for this case. */
8403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8405 error_at (token
->location
,
8406 "array bound forbidden after parenthesized type-id");
8407 inform (token
->location
,
8408 "try removing the parentheses around the type-id");
8409 cp_parser_direct_new_declarator (parser
);
8412 /* Otherwise, there must be a new-type-id. */
8414 type
= cp_parser_new_type_id (parser
, &nelts
);
8416 /* If the next token is a `(' or '{', then we have a new-initializer. */
8417 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8418 if (token
->type
== CPP_OPEN_PAREN
8419 || token
->type
== CPP_OPEN_BRACE
)
8420 initializer
= cp_parser_new_initializer (parser
);
8424 /* A new-expression may not appear in an integral constant
8426 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8427 ret
= error_mark_node
;
8428 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8429 of a new-type-id or type-id of a new-expression, the new-expression shall
8430 contain a new-initializer of the form ( assignment-expression )".
8431 Additionally, consistently with the spirit of DR 1467, we want to accept
8432 'new auto { 2 }' too. */
8433 else if ((ret
= type_uses_auto (type
))
8434 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8435 && (vec_safe_length (initializer
) != 1
8436 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8437 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8439 error_at (token
->location
,
8440 "initialization of new-expression for type %<auto%> "
8441 "requires exactly one element");
8442 ret
= error_mark_node
;
8446 /* Construct a location e.g.:
8449 with caret == start at the start of the "new" token, and the end
8450 at the end of the final token we consumed. */
8451 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8452 location_t end_loc
= get_finish (end_tok
->location
);
8453 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8455 /* Create a representation of the new-expression. */
8456 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8457 tf_warning_or_error
);
8458 protected_set_expr_location (ret
, combined_loc
);
8461 if (placement
!= NULL
)
8462 release_tree_vector (placement
);
8463 if (initializer
!= NULL
)
8464 release_tree_vector (initializer
);
8469 /* Parse a new-placement.
8474 Returns the same representation as for an expression-list. */
8476 static vec
<tree
, va_gc
> *
8477 cp_parser_new_placement (cp_parser
* parser
)
8479 vec
<tree
, va_gc
> *expression_list
;
8481 /* Parse the expression-list. */
8482 expression_list
= (cp_parser_parenthesized_expression_list
8483 (parser
, non_attr
, /*cast_p=*/false,
8484 /*allow_expansion_p=*/true,
8485 /*non_constant_p=*/NULL
));
8487 if (expression_list
&& expression_list
->is_empty ())
8488 error ("expected expression-list or type-id");
8490 return expression_list
;
8493 /* Parse a new-type-id.
8496 type-specifier-seq new-declarator [opt]
8498 Returns the TYPE allocated. If the new-type-id indicates an array
8499 type, *NELTS is set to the number of elements in the last array
8500 bound; the TYPE will not include the last array bound. */
8503 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8505 cp_decl_specifier_seq type_specifier_seq
;
8506 cp_declarator
*new_declarator
;
8507 cp_declarator
*declarator
;
8508 cp_declarator
*outer_declarator
;
8509 const char *saved_message
;
8511 /* The type-specifier sequence must not contain type definitions.
8512 (It cannot contain declarations of new types either, but if they
8513 are not definitions we will catch that because they are not
8515 saved_message
= parser
->type_definition_forbidden_message
;
8516 parser
->type_definition_forbidden_message
8517 = G_("types may not be defined in a new-type-id");
8518 /* Parse the type-specifier-seq. */
8519 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8520 /*is_trailing_return=*/false,
8521 &type_specifier_seq
);
8522 /* Restore the old message. */
8523 parser
->type_definition_forbidden_message
= saved_message
;
8525 if (type_specifier_seq
.type
== error_mark_node
)
8526 return error_mark_node
;
8528 /* Parse the new-declarator. */
8529 new_declarator
= cp_parser_new_declarator_opt (parser
);
8531 /* Determine the number of elements in the last array dimension, if
8534 /* Skip down to the last array dimension. */
8535 declarator
= new_declarator
;
8536 outer_declarator
= NULL
;
8537 while (declarator
&& (declarator
->kind
== cdk_pointer
8538 || declarator
->kind
== cdk_ptrmem
))
8540 outer_declarator
= declarator
;
8541 declarator
= declarator
->declarator
;
8544 && declarator
->kind
== cdk_array
8545 && declarator
->declarator
8546 && declarator
->declarator
->kind
== cdk_array
)
8548 outer_declarator
= declarator
;
8549 declarator
= declarator
->declarator
;
8552 if (declarator
&& declarator
->kind
== cdk_array
)
8554 *nelts
= declarator
->u
.array
.bounds
;
8555 if (*nelts
== error_mark_node
)
8556 *nelts
= integer_one_node
;
8558 if (outer_declarator
)
8559 outer_declarator
->declarator
= declarator
->declarator
;
8561 new_declarator
= NULL
;
8564 return groktypename (&type_specifier_seq
, new_declarator
, false);
8567 /* Parse an (optional) new-declarator.
8570 ptr-operator new-declarator [opt]
8571 direct-new-declarator
8573 Returns the declarator. */
8575 static cp_declarator
*
8576 cp_parser_new_declarator_opt (cp_parser
* parser
)
8578 enum tree_code code
;
8579 tree type
, std_attributes
= NULL_TREE
;
8580 cp_cv_quals cv_quals
;
8582 /* We don't know if there's a ptr-operator next, or not. */
8583 cp_parser_parse_tentatively (parser
);
8584 /* Look for a ptr-operator. */
8585 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8586 /* If that worked, look for more new-declarators. */
8587 if (cp_parser_parse_definitely (parser
))
8589 cp_declarator
*declarator
;
8591 /* Parse another optional declarator. */
8592 declarator
= cp_parser_new_declarator_opt (parser
);
8594 declarator
= cp_parser_make_indirect_declarator
8595 (code
, type
, cv_quals
, declarator
, std_attributes
);
8600 /* If the next token is a `[', there is a direct-new-declarator. */
8601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8602 return cp_parser_direct_new_declarator (parser
);
8607 /* Parse a direct-new-declarator.
8609 direct-new-declarator:
8611 direct-new-declarator [constant-expression]
8615 static cp_declarator
*
8616 cp_parser_direct_new_declarator (cp_parser
* parser
)
8618 cp_declarator
*declarator
= NULL
;
8625 /* Look for the opening `['. */
8626 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8628 token
= cp_lexer_peek_token (parser
->lexer
);
8629 expression
= cp_parser_expression (parser
);
8630 /* The standard requires that the expression have integral
8631 type. DR 74 adds enumeration types. We believe that the
8632 real intent is that these expressions be handled like the
8633 expression in a `switch' condition, which also allows
8634 classes with a single conversion to integral or
8635 enumeration type. */
8636 if (!processing_template_decl
)
8639 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8644 error_at (token
->location
,
8645 "expression in new-declarator must have integral "
8646 "or enumeration type");
8647 expression
= error_mark_node
;
8651 /* Look for the closing `]'. */
8652 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8654 /* Add this bound to the declarator. */
8655 declarator
= make_array_declarator (declarator
, expression
);
8657 /* If the next token is not a `[', then there are no more
8659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8666 /* Parse a new-initializer.
8669 ( expression-list [opt] )
8672 Returns a representation of the expression-list. */
8674 static vec
<tree
, va_gc
> *
8675 cp_parser_new_initializer (cp_parser
* parser
)
8677 vec
<tree
, va_gc
> *expression_list
;
8679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8682 bool expr_non_constant_p
;
8683 cp_lexer_set_source_position (parser
->lexer
);
8684 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8685 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8686 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8687 expression_list
= make_tree_vector_single (t
);
8690 expression_list
= (cp_parser_parenthesized_expression_list
8691 (parser
, non_attr
, /*cast_p=*/false,
8692 /*allow_expansion_p=*/true,
8693 /*non_constant_p=*/NULL
));
8695 return expression_list
;
8698 /* Parse a delete-expression.
8701 :: [opt] delete cast-expression
8702 :: [opt] delete [ ] cast-expression
8704 Returns a representation of the expression. */
8707 cp_parser_delete_expression (cp_parser
* parser
)
8709 bool global_scope_p
;
8713 /* Look for the optional `::' operator. */
8715 = (cp_parser_global_scope_opt (parser
,
8716 /*current_scope_valid_p=*/false)
8718 /* Look for the `delete' keyword. */
8719 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8720 /* See if the array syntax is in use. */
8721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8723 /* Consume the `[' token. */
8724 cp_lexer_consume_token (parser
->lexer
);
8725 /* Look for the `]' token. */
8726 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8727 /* Remember that this is the `[]' construct. */
8733 /* Parse the cast-expression. */
8734 expression
= cp_parser_simple_cast_expression (parser
);
8736 /* A delete-expression may not appear in an integral constant
8738 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8739 return error_mark_node
;
8741 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8742 tf_warning_or_error
);
8745 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8746 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8750 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8753 switch (token
->type
)
8759 case CPP_CLOSE_SQUARE
:
8760 case CPP_CLOSE_PAREN
:
8761 case CPP_CLOSE_BRACE
:
8762 case CPP_OPEN_BRACE
:
8766 case CPP_DEREF_STAR
:
8774 case CPP_GREATER_EQ
:
8795 case CPP_OPEN_PAREN
:
8796 /* In ((type ()) () the last () isn't a valid cast-expression,
8797 so the whole must be parsed as postfix-expression. */
8798 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8801 case CPP_OPEN_SQUARE
:
8802 /* '[' may start a primary-expression in obj-c++ and in C++11,
8803 as a lambda-expression, eg, '(void)[]{}'. */
8804 if (cxx_dialect
>= cxx11
)
8806 return c_dialect_objc ();
8809 case CPP_MINUS_MINUS
:
8810 /* '++' and '--' may or may not start a cast-expression:
8812 struct T { void operator++(int); };
8813 void f() { (T())++; }
8826 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8827 in the order: const_cast, static_cast, reinterpret_cast.
8829 Don't suggest dynamic_cast.
8831 Return the first legal cast kind found, or NULL otherwise. */
8834 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8838 /* Reuse the parser logic by attempting to build the various kinds of
8839 cast, with "complain" disabled.
8840 Identify the first such cast that is valid. */
8842 /* Don't attempt to run such logic within template processing. */
8843 if (processing_template_decl
)
8846 /* First try const_cast. */
8847 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8848 if (trial
!= error_mark_node
)
8849 return "const_cast";
8851 /* If that fails, try static_cast. */
8852 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8853 if (trial
!= error_mark_node
)
8854 return "static_cast";
8856 /* Finally, try reinterpret_cast. */
8857 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8858 if (trial
!= error_mark_node
)
8859 return "reinterpret_cast";
8861 /* No such cast possible. */
8865 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8866 suggesting how to convert a C-style cast of the form:
8870 to a C++-style cast.
8872 The primary range of RICHLOC is asssumed to be that of the original
8873 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8874 of the parens in the C-style cast. */
8877 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8878 location_t close_paren_loc
, tree orig_expr
,
8881 /* This function is non-trivial, so bail out now if the warning isn't
8882 going to be emitted. */
8883 if (!warn_old_style_cast
)
8886 /* Try to find a legal C++ cast, trying them in order:
8887 const_cast, static_cast, reinterpret_cast. */
8888 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8889 if (!cast_suggestion
)
8892 /* Replace the open paren with "CAST_SUGGESTION<". */
8894 pp_printf (&pp
, "%s<", cast_suggestion
);
8895 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8897 /* Replace the close paren with "> (". */
8898 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8900 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8901 rich_loc
->add_fixit_insert_after (")");
8905 /* Parse a cast-expression.
8909 ( type-id ) cast-expression
8911 ADDRESS_P is true iff the unary-expression is appearing as the
8912 operand of the `&' operator. CAST_P is true if this expression is
8913 the target of a cast.
8915 Returns a representation of the expression. */
8918 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8919 bool decltype_p
, cp_id_kind
* pidk
)
8921 /* If it's a `(', then we might be looking at a cast. */
8922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8924 tree type
= NULL_TREE
;
8925 cp_expr
expr (NULL_TREE
);
8926 int cast_expression
= 0;
8927 const char *saved_message
;
8929 /* There's no way to know yet whether or not this is a cast.
8930 For example, `(int (3))' is a unary-expression, while `(int)
8931 3' is a cast. So, we resort to parsing tentatively. */
8932 cp_parser_parse_tentatively (parser
);
8933 /* Types may not be defined in a cast. */
8934 saved_message
= parser
->type_definition_forbidden_message
;
8935 parser
->type_definition_forbidden_message
8936 = G_("types may not be defined in casts");
8937 /* Consume the `('. */
8938 matching_parens parens
;
8939 cp_token
*open_paren
= parens
.consume_open (parser
);
8940 location_t open_paren_loc
= open_paren
->location
;
8941 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8943 /* A very tricky bit is that `(struct S) { 3 }' is a
8944 compound-literal (which we permit in C++ as an extension).
8945 But, that construct is not a cast-expression -- it is a
8946 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8947 is legal; if the compound-literal were a cast-expression,
8948 you'd need an extra set of parentheses.) But, if we parse
8949 the type-id, and it happens to be a class-specifier, then we
8950 will commit to the parse at that point, because we cannot
8951 undo the action that is done when creating a new class. So,
8952 then we cannot back up and do a postfix-expression.
8954 Another tricky case is the following (c++/29234):
8956 struct S { void operator () (); };
8963 As a type-id we parse the parenthesized S()() as a function
8964 returning a function, groktypename complains and we cannot
8965 back up in this case either.
8967 Therefore, we scan ahead to the closing `)', and check to see
8968 if the tokens after the `)' can start a cast-expression. Otherwise
8969 we are dealing with an unary-expression, a postfix-expression
8972 Yet another tricky case, in C++11, is the following (c++/54891):
8976 The issue is that usually, besides the case of lambda-expressions,
8977 the parenthesized type-id cannot be followed by '[', and, eg, we
8978 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8979 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8980 we don't commit, we try a cast-expression, then an unary-expression.
8982 Save tokens so that we can put them back. */
8983 cp_lexer_save_tokens (parser
->lexer
);
8985 /* We may be looking at a cast-expression. */
8986 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8987 /*consume_paren=*/true))
8989 = cp_parser_tokens_start_cast_expression (parser
);
8991 /* Roll back the tokens we skipped. */
8992 cp_lexer_rollback_tokens (parser
->lexer
);
8993 /* If we aren't looking at a cast-expression, simulate an error so
8994 that the call to cp_parser_error_occurred below returns true. */
8995 if (!cast_expression
)
8996 cp_parser_simulate_error (parser
);
8999 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9000 parser
->in_type_id_in_expr_p
= true;
9001 /* Look for the type-id. */
9002 type
= cp_parser_type_id (parser
);
9003 /* Look for the closing `)'. */
9004 cp_token
*close_paren
= parens
.require_close (parser
);
9006 close_paren_loc
= close_paren
->location
;
9007 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9010 /* Restore the saved message. */
9011 parser
->type_definition_forbidden_message
= saved_message
;
9013 /* At this point this can only be either a cast or a
9014 parenthesized ctor such as `(T ())' that looks like a cast to
9015 function returning T. */
9016 if (!cp_parser_error_occurred (parser
))
9018 /* Only commit if the cast-expression doesn't start with
9019 '++', '--', or '[' in C++11. */
9020 if (cast_expression
> 0)
9021 cp_parser_commit_to_topmost_tentative_parse (parser
);
9023 expr
= cp_parser_cast_expression (parser
,
9024 /*address_p=*/false,
9026 /*decltype_p=*/false,
9029 if (cp_parser_parse_definitely (parser
))
9031 /* Warn about old-style casts, if so requested. */
9032 if (warn_old_style_cast
9033 && !in_system_header_at (input_location
)
9034 && !VOID_TYPE_P (type
)
9035 && current_lang_name
!= lang_name_c
)
9037 gcc_rich_location
rich_loc (input_location
);
9038 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9040 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9041 "use of old-style cast to %q#T", type
);
9044 /* Only type conversions to integral or enumeration types
9045 can be used in constant-expressions. */
9046 if (!cast_valid_in_integral_constant_expression_p (type
)
9047 && cp_parser_non_integral_constant_expression (parser
,
9049 return error_mark_node
;
9051 /* Perform the cast. */
9055 with start==caret at the open paren, extending to the
9057 location_t cast_loc
= make_location (open_paren_loc
,
9059 expr
.get_finish ());
9060 expr
= build_c_cast (cast_loc
, type
, expr
);
9065 cp_parser_abort_tentative_parse (parser
);
9068 /* If we get here, then it's not a cast, so it must be a
9069 unary-expression. */
9070 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9071 cast_p
, decltype_p
);
9074 /* Parse a binary expression of the general form:
9078 pm-expression .* cast-expression
9079 pm-expression ->* cast-expression
9081 multiplicative-expression:
9083 multiplicative-expression * pm-expression
9084 multiplicative-expression / pm-expression
9085 multiplicative-expression % pm-expression
9087 additive-expression:
9088 multiplicative-expression
9089 additive-expression + multiplicative-expression
9090 additive-expression - multiplicative-expression
9094 shift-expression << additive-expression
9095 shift-expression >> additive-expression
9097 relational-expression:
9099 relational-expression < shift-expression
9100 relational-expression > shift-expression
9101 relational-expression <= shift-expression
9102 relational-expression >= shift-expression
9106 relational-expression:
9107 relational-expression <? shift-expression
9108 relational-expression >? shift-expression
9110 equality-expression:
9111 relational-expression
9112 equality-expression == relational-expression
9113 equality-expression != relational-expression
9117 and-expression & equality-expression
9119 exclusive-or-expression:
9121 exclusive-or-expression ^ and-expression
9123 inclusive-or-expression:
9124 exclusive-or-expression
9125 inclusive-or-expression | exclusive-or-expression
9127 logical-and-expression:
9128 inclusive-or-expression
9129 logical-and-expression && inclusive-or-expression
9131 logical-or-expression:
9132 logical-and-expression
9133 logical-or-expression || logical-and-expression
9135 All these are implemented with a single function like:
9138 simple-cast-expression
9139 binary-expression <token> binary-expression
9141 CAST_P is true if this expression is the target of a cast.
9143 The binops_by_token map is used to get the tree codes for each <token> type.
9144 binary-expressions are associated according to a precedence table. */
9146 #define TOKEN_PRECEDENCE(token) \
9147 (((token->type == CPP_GREATER \
9148 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9149 && !parser->greater_than_is_operator_p) \
9150 ? PREC_NOT_OPERATOR \
9151 : binops_by_token[token->type].prec)
9154 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9155 bool no_toplevel_fold_p
,
9157 enum cp_parser_prec prec
,
9160 cp_parser_expression_stack stack
;
9161 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9162 cp_parser_expression_stack_entry current
;
9165 enum tree_code rhs_type
;
9166 enum cp_parser_prec new_prec
, lookahead_prec
;
9169 /* Parse the first expression. */
9170 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9171 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9172 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9173 cast_p
, decltype_p
, pidk
);
9174 current
.prec
= prec
;
9176 if (cp_parser_error_occurred (parser
))
9177 return error_mark_node
;
9181 /* Get an operator token. */
9182 token
= cp_lexer_peek_token (parser
->lexer
);
9184 if (warn_cxx11_compat
9185 && token
->type
== CPP_RSHIFT
9186 && !parser
->greater_than_is_operator_p
)
9188 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9189 "%<>>%> operator is treated"
9190 " as two right angle brackets in C++11"))
9191 inform (token
->location
,
9192 "suggest parentheses around %<>>%> expression");
9195 new_prec
= TOKEN_PRECEDENCE (token
);
9196 if (new_prec
!= PREC_NOT_OPERATOR
9197 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9198 /* This is a fold-expression; handle it later. */
9199 new_prec
= PREC_NOT_OPERATOR
;
9201 /* Popping an entry off the stack means we completed a subexpression:
9202 - either we found a token which is not an operator (`>' where it is not
9203 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9204 will happen repeatedly;
9205 - or, we found an operator which has lower priority. This is the case
9206 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9208 if (new_prec
<= current
.prec
)
9217 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9218 current
.loc
= token
->location
;
9220 /* We used the operator token. */
9221 cp_lexer_consume_token (parser
->lexer
);
9223 /* For "false && x" or "true || x", x will never be executed;
9224 disable warnings while evaluating it. */
9225 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9226 c_inhibit_evaluation_warnings
+=
9227 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9228 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9229 c_inhibit_evaluation_warnings
+=
9230 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9232 /* Extract another operand. It may be the RHS of this expression
9233 or the LHS of a new, higher priority expression. */
9234 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9235 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9236 rhs
= cp_parser_simple_cast_expression (parser
);
9238 /* Get another operator token. Look up its precedence to avoid
9239 building a useless (immediately popped) stack entry for common
9240 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9241 token
= cp_lexer_peek_token (parser
->lexer
);
9242 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9243 if (lookahead_prec
!= PREC_NOT_OPERATOR
9244 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9245 lookahead_prec
= PREC_NOT_OPERATOR
;
9246 if (lookahead_prec
> new_prec
)
9248 /* ... and prepare to parse the RHS of the new, higher priority
9249 expression. Since precedence levels on the stack are
9250 monotonically increasing, we do not have to care about
9255 current
.lhs_type
= rhs_type
;
9256 current
.prec
= new_prec
;
9257 new_prec
= lookahead_prec
;
9261 lookahead_prec
= new_prec
;
9262 /* If the stack is not empty, we have parsed into LHS the right side
9263 (`4' in the example above) of an expression we had suspended.
9264 We can use the information on the stack to recover the LHS (`3')
9265 from the stack together with the tree code (`MULT_EXPR'), and
9266 the precedence of the higher level subexpression
9267 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9268 which will be used to actually build the additive expression. */
9270 rhs_type
= current
.lhs_type
;
9275 /* Undo the disabling of warnings done above. */
9276 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9277 c_inhibit_evaluation_warnings
-=
9278 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9279 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9280 c_inhibit_evaluation_warnings
-=
9281 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9283 if (warn_logical_not_paren
9284 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9285 && current
.lhs_type
== TRUTH_NOT_EXPR
9286 /* Avoid warning for !!x == y. */
9287 && (TREE_CODE (current
.lhs
) != NE_EXPR
9288 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9289 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9290 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9291 /* Avoid warning for !b == y where b is boolean. */
9292 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9293 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9295 /* Avoid warning for !!b == y where b is boolean. */
9296 && (!DECL_P (current
.lhs
)
9297 || TREE_TYPE (current
.lhs
) == NULL_TREE
9298 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9299 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9300 current
.lhs
, maybe_constant_value (rhs
));
9304 location_t combined_loc
= make_location (current
.loc
,
9305 current
.lhs
.get_start (),
9308 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9309 ERROR_MARK for everything that is not a binary expression.
9310 This makes warn_about_parentheses miss some warnings that
9311 involve unary operators. For unary expressions we should
9312 pass the correct tree_code unless the unary expression was
9313 surrounded by parentheses.
9315 if (no_toplevel_fold_p
9316 && lookahead_prec
<= current
.prec
9319 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9320 current
.lhs
= error_mark_node
;
9324 = build_min (current
.tree_type
,
9325 TREE_CODE_CLASS (current
.tree_type
)
9327 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9328 current
.lhs
.get_value (), rhs
.get_value ());
9329 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9334 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9335 current
.lhs
, current
.lhs_type
,
9336 rhs
, rhs_type
, &overload
,
9337 complain_flags (decltype_p
));
9338 /* TODO: build_x_binary_op doesn't always honor the location. */
9339 current
.lhs
.set_location (combined_loc
);
9341 current
.lhs_type
= current
.tree_type
;
9343 /* If the binary operator required the use of an overloaded operator,
9344 then this expression cannot be an integral constant-expression.
9345 An overloaded operator can be used even if both operands are
9346 otherwise permissible in an integral constant-expression if at
9347 least one of the operands is of enumeration type. */
9350 && cp_parser_non_integral_constant_expression (parser
,
9352 return error_mark_node
;
9359 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9360 bool no_toplevel_fold_p
,
9361 enum cp_parser_prec prec
,
9364 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9365 /*decltype*/false, prec
, pidk
);
9368 /* Parse the `? expression : assignment-expression' part of a
9369 conditional-expression. The LOGICAL_OR_EXPR is the
9370 logical-or-expression that started the conditional-expression.
9371 Returns a representation of the entire conditional-expression.
9373 This routine is used by cp_parser_assignment_expression.
9375 ? expression : assignment-expression
9379 ? : assignment-expression */
9382 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9384 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9385 cp_expr assignment_expr
;
9386 struct cp_token
*token
;
9387 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9389 /* Consume the `?' token. */
9390 cp_lexer_consume_token (parser
->lexer
);
9391 token
= cp_lexer_peek_token (parser
->lexer
);
9392 if (cp_parser_allow_gnu_extensions_p (parser
)
9393 && token
->type
== CPP_COLON
)
9395 pedwarn (token
->location
, OPT_Wpedantic
,
9396 "ISO C++ does not allow ?: with omitted middle operand");
9397 /* Implicit true clause. */
9399 c_inhibit_evaluation_warnings
+=
9400 folded_logical_or_expr
== truthvalue_true_node
;
9401 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9405 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9406 parser
->colon_corrects_to_scope_p
= false;
9407 /* Parse the expression. */
9408 c_inhibit_evaluation_warnings
+=
9409 folded_logical_or_expr
== truthvalue_false_node
;
9410 expr
= cp_parser_expression (parser
);
9411 c_inhibit_evaluation_warnings
+=
9412 ((folded_logical_or_expr
== truthvalue_true_node
)
9413 - (folded_logical_or_expr
== truthvalue_false_node
));
9414 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9417 /* The next token should be a `:'. */
9418 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9419 /* Parse the assignment-expression. */
9420 assignment_expr
= cp_parser_assignment_expression (parser
);
9421 c_inhibit_evaluation_warnings
-=
9422 folded_logical_or_expr
== truthvalue_true_node
;
9425 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9426 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9427 with the caret at the "?", ranging from the start of
9428 the logical_or_expr to the end of the assignment_expr. */
9429 loc
= make_location (loc
,
9430 logical_or_expr
.get_start (),
9431 assignment_expr
.get_finish ());
9433 /* Build the conditional-expression. */
9434 return build_x_conditional_expr (loc
, logical_or_expr
,
9437 tf_warning_or_error
);
9440 /* Parse an assignment-expression.
9442 assignment-expression:
9443 conditional-expression
9444 logical-or-expression assignment-operator assignment_expression
9447 CAST_P is true if this expression is the target of a cast.
9448 DECLTYPE_P is true if this expression is the operand of decltype.
9450 Returns a representation for the expression. */
9453 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9454 bool cast_p
, bool decltype_p
)
9458 /* If the next token is the `throw' keyword, then we're looking at
9459 a throw-expression. */
9460 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9461 expr
= cp_parser_throw_expression (parser
);
9462 /* Otherwise, it must be that we are looking at a
9463 logical-or-expression. */
9466 /* Parse the binary expressions (logical-or-expression). */
9467 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9469 PREC_NOT_OPERATOR
, pidk
);
9470 /* If the next token is a `?' then we're actually looking at a
9471 conditional-expression. */
9472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9473 return cp_parser_question_colon_clause (parser
, expr
);
9476 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9478 /* If it's an assignment-operator, we're using the second
9480 enum tree_code assignment_operator
9481 = cp_parser_assignment_operator_opt (parser
);
9482 if (assignment_operator
!= ERROR_MARK
)
9484 bool non_constant_p
;
9486 /* Parse the right-hand side of the assignment. */
9487 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9490 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9491 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9493 /* An assignment may not appear in a
9494 constant-expression. */
9495 if (cp_parser_non_integral_constant_expression (parser
,
9497 return error_mark_node
;
9498 /* Build the assignment expression. Its default
9502 is the location of the '=' token as the
9503 caret, ranging from the start of the lhs to the
9505 loc
= make_location (loc
,
9508 expr
= build_x_modify_expr (loc
, expr
,
9509 assignment_operator
,
9511 complain_flags (decltype_p
));
9512 /* TODO: build_x_modify_expr doesn't honor the location,
9513 so we must set it here. */
9514 expr
.set_location (loc
);
9522 /* Parse an (optional) assignment-operator.
9524 assignment-operator: one of
9525 = *= /= %= += -= >>= <<= &= ^= |=
9529 assignment-operator: one of
9532 If the next token is an assignment operator, the corresponding tree
9533 code is returned, and the token is consumed. For example, for
9534 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9535 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9536 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9537 operator, ERROR_MARK is returned. */
9539 static enum tree_code
9540 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9545 /* Peek at the next token. */
9546 token
= cp_lexer_peek_token (parser
->lexer
);
9548 switch (token
->type
)
9559 op
= TRUNC_DIV_EXPR
;
9563 op
= TRUNC_MOD_EXPR
;
9595 /* Nothing else is an assignment operator. */
9599 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9600 if (op
!= ERROR_MARK
9601 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9604 /* If it was an assignment operator, consume it. */
9605 if (op
!= ERROR_MARK
)
9606 cp_lexer_consume_token (parser
->lexer
);
9611 /* Parse an expression.
9614 assignment-expression
9615 expression , assignment-expression
9617 CAST_P is true if this expression is the target of a cast.
9618 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9619 except possibly parenthesized or on the RHS of a comma (N3276).
9621 Returns a representation of the expression. */
9624 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9625 bool cast_p
, bool decltype_p
)
9627 cp_expr expression
= NULL_TREE
;
9628 location_t loc
= UNKNOWN_LOCATION
;
9632 cp_expr assignment_expression
;
9634 /* Parse the next assignment-expression. */
9635 assignment_expression
9636 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9638 /* We don't create a temporary for a call that is the immediate operand
9639 of decltype or on the RHS of a comma. But when we see a comma, we
9640 need to create a temporary for a call on the LHS. */
9641 if (decltype_p
&& !processing_template_decl
9642 && TREE_CODE (assignment_expression
) == CALL_EXPR
9643 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9644 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9645 assignment_expression
9646 = build_cplus_new (TREE_TYPE (assignment_expression
),
9647 assignment_expression
, tf_warning_or_error
);
9649 /* If this is the first assignment-expression, we can just
9652 expression
= assignment_expression
;
9655 /* Create a location with caret at the comma, ranging
9656 from the start of the LHS to the end of the RHS. */
9657 loc
= make_location (loc
,
9658 expression
.get_start (),
9659 assignment_expression
.get_finish ());
9660 expression
= build_x_compound_expr (loc
, expression
,
9661 assignment_expression
,
9662 complain_flags (decltype_p
));
9663 expression
.set_location (loc
);
9665 /* If the next token is not a comma, or we're in a fold-expression, then
9666 we are done with the expression. */
9667 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9668 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9670 /* Consume the `,'. */
9671 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9672 cp_lexer_consume_token (parser
->lexer
);
9673 /* A comma operator cannot appear in a constant-expression. */
9674 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9675 expression
= error_mark_node
;
9681 /* Parse a constant-expression.
9683 constant-expression:
9684 conditional-expression
9686 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9687 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9688 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9689 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9690 only parse a conditional-expression, otherwise parse an
9691 assignment-expression. See below for rationale. */
9694 cp_parser_constant_expression (cp_parser
* parser
,
9695 bool allow_non_constant_p
,
9696 bool *non_constant_p
,
9699 bool saved_integral_constant_expression_p
;
9700 bool saved_allow_non_integral_constant_expression_p
;
9701 bool saved_non_integral_constant_expression_p
;
9704 /* It might seem that we could simply parse the
9705 conditional-expression, and then check to see if it were
9706 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9707 one that the compiler can figure out is constant, possibly after
9708 doing some simplifications or optimizations. The standard has a
9709 precise definition of constant-expression, and we must honor
9710 that, even though it is somewhat more restrictive.
9716 is not a legal declaration, because `(2, 3)' is not a
9717 constant-expression. The `,' operator is forbidden in a
9718 constant-expression. However, GCC's constant-folding machinery
9719 will fold this operation to an INTEGER_CST for `3'. */
9721 /* Save the old settings. */
9722 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9723 saved_allow_non_integral_constant_expression_p
9724 = parser
->allow_non_integral_constant_expression_p
;
9725 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9726 /* We are now parsing a constant-expression. */
9727 parser
->integral_constant_expression_p
= true;
9728 parser
->allow_non_integral_constant_expression_p
9729 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9730 parser
->non_integral_constant_expression_p
= false;
9731 /* Although the grammar says "conditional-expression", when not STRICT_P,
9732 we parse an "assignment-expression", which also permits
9733 "throw-expression" and the use of assignment operators. In the case
9734 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9735 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9736 actually essential that we look for an assignment-expression.
9737 For example, cp_parser_initializer_clauses uses this function to
9738 determine whether a particular assignment-expression is in fact
9742 /* Parse the binary expressions (logical-or-expression). */
9743 expression
= cp_parser_binary_expression (parser
, false, false, false,
9744 PREC_NOT_OPERATOR
, NULL
);
9745 /* If the next token is a `?' then we're actually looking at
9746 a conditional-expression; otherwise we're done. */
9747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9748 expression
= cp_parser_question_colon_clause (parser
, expression
);
9751 expression
= cp_parser_assignment_expression (parser
);
9752 /* Restore the old settings. */
9753 parser
->integral_constant_expression_p
9754 = saved_integral_constant_expression_p
;
9755 parser
->allow_non_integral_constant_expression_p
9756 = saved_allow_non_integral_constant_expression_p
;
9757 if (cxx_dialect
>= cxx11
)
9759 /* Require an rvalue constant expression here; that's what our
9760 callers expect. Reference constant expressions are handled
9761 separately in e.g. cp_parser_template_argument. */
9762 tree decay
= expression
;
9763 if (TREE_TYPE (expression
)
9764 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9765 decay
= build_address (expression
);
9766 bool is_const
= potential_rvalue_constant_expression (decay
);
9767 parser
->non_integral_constant_expression_p
= !is_const
;
9768 if (!is_const
&& !allow_non_constant_p
)
9769 require_potential_rvalue_constant_expression (decay
);
9771 if (allow_non_constant_p
)
9772 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9773 parser
->non_integral_constant_expression_p
9774 = saved_non_integral_constant_expression_p
;
9779 /* Parse __builtin_offsetof.
9781 offsetof-expression:
9782 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9784 offsetof-member-designator:
9786 | offsetof-member-designator "." id-expression
9787 | offsetof-member-designator "[" expression "]"
9788 | offsetof-member-designator "->" id-expression */
9791 cp_parser_builtin_offsetof (cp_parser
*parser
)
9793 int save_ice_p
, save_non_ice_p
;
9798 location_t finish_loc
;
9800 /* We're about to accept non-integral-constant things, but will
9801 definitely yield an integral constant expression. Save and
9802 restore these values around our local parsing. */
9803 save_ice_p
= parser
->integral_constant_expression_p
;
9804 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9806 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9808 /* Consume the "__builtin_offsetof" token. */
9809 cp_lexer_consume_token (parser
->lexer
);
9810 /* Consume the opening `('. */
9811 matching_parens parens
;
9812 parens
.require_open (parser
);
9813 /* Parse the type-id. */
9814 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9816 const char *saved_message
= parser
->type_definition_forbidden_message
;
9817 parser
->type_definition_forbidden_message
9818 = G_("types may not be defined within __builtin_offsetof");
9819 type
= cp_parser_type_id (parser
);
9820 parser
->type_definition_forbidden_message
= saved_message
;
9822 /* Look for the `,'. */
9823 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9824 token
= cp_lexer_peek_token (parser
->lexer
);
9826 /* Build the (type *)null that begins the traditional offsetof macro. */
9828 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9829 tf_warning_or_error
);
9831 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9832 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9833 true, &dummy
, token
->location
);
9836 token
= cp_lexer_peek_token (parser
->lexer
);
9837 switch (token
->type
)
9839 case CPP_OPEN_SQUARE
:
9840 /* offsetof-member-designator "[" expression "]" */
9841 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9846 /* offsetof-member-designator "->" identifier */
9847 expr
= grok_array_decl (token
->location
, expr
,
9848 integer_zero_node
, false);
9852 /* offsetof-member-designator "." identifier */
9853 cp_lexer_consume_token (parser
->lexer
);
9854 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9859 case CPP_CLOSE_PAREN
:
9860 /* Consume the ")" token. */
9861 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9862 cp_lexer_consume_token (parser
->lexer
);
9866 /* Error. We know the following require will fail, but
9867 that gives the proper error message. */
9868 parens
.require_close (parser
);
9869 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9870 expr
= error_mark_node
;
9876 /* Make a location of the form:
9877 __builtin_offsetof (struct s, f)
9878 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9879 with caret at the type-id, ranging from the start of the
9880 "_builtin_offsetof" token to the close paren. */
9881 loc
= make_location (loc
, start_loc
, finish_loc
);
9882 /* The result will be an INTEGER_CST, so we need to explicitly
9883 preserve the location. */
9884 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9887 parser
->integral_constant_expression_p
= save_ice_p
;
9888 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9890 expr
= expr
.maybe_add_location_wrapper ();
9894 /* Parse a trait expression.
9896 Returns a representation of the expression, the underlying type
9897 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9900 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9903 tree type1
, type2
= NULL_TREE
;
9904 bool binary
= false;
9905 bool variadic
= false;
9909 case RID_HAS_NOTHROW_ASSIGN
:
9910 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9912 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9913 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9915 case RID_HAS_NOTHROW_COPY
:
9916 kind
= CPTK_HAS_NOTHROW_COPY
;
9918 case RID_HAS_TRIVIAL_ASSIGN
:
9919 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9921 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9922 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9924 case RID_HAS_TRIVIAL_COPY
:
9925 kind
= CPTK_HAS_TRIVIAL_COPY
;
9927 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9928 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9930 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9931 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9933 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9934 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9936 case RID_IS_ABSTRACT
:
9937 kind
= CPTK_IS_ABSTRACT
;
9939 case RID_IS_AGGREGATE
:
9940 kind
= CPTK_IS_AGGREGATE
;
9942 case RID_IS_BASE_OF
:
9943 kind
= CPTK_IS_BASE_OF
;
9947 kind
= CPTK_IS_CLASS
;
9950 kind
= CPTK_IS_EMPTY
;
9953 kind
= CPTK_IS_ENUM
;
9956 kind
= CPTK_IS_FINAL
;
9958 case RID_IS_LITERAL_TYPE
:
9959 kind
= CPTK_IS_LITERAL_TYPE
;
9964 case RID_IS_POLYMORPHIC
:
9965 kind
= CPTK_IS_POLYMORPHIC
;
9967 case RID_IS_SAME_AS
:
9968 kind
= CPTK_IS_SAME_AS
;
9971 case RID_IS_STD_LAYOUT
:
9972 kind
= CPTK_IS_STD_LAYOUT
;
9974 case RID_IS_TRIVIAL
:
9975 kind
= CPTK_IS_TRIVIAL
;
9977 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9978 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9981 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9982 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9985 case RID_IS_TRIVIALLY_COPYABLE
:
9986 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9989 kind
= CPTK_IS_UNION
;
9991 case RID_UNDERLYING_TYPE
:
9992 kind
= CPTK_UNDERLYING_TYPE
;
9997 case RID_DIRECT_BASES
:
9998 kind
= CPTK_DIRECT_BASES
;
10000 case RID_IS_ASSIGNABLE
:
10001 kind
= CPTK_IS_ASSIGNABLE
;
10004 case RID_IS_CONSTRUCTIBLE
:
10005 kind
= CPTK_IS_CONSTRUCTIBLE
;
10009 gcc_unreachable ();
10012 /* Get location of initial token. */
10013 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10015 /* Consume the token. */
10016 cp_lexer_consume_token (parser
->lexer
);
10018 matching_parens parens
;
10019 parens
.require_open (parser
);
10022 type_id_in_expr_sentinel
s (parser
);
10023 type1
= cp_parser_type_id (parser
);
10026 if (type1
== error_mark_node
)
10027 return error_mark_node
;
10031 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10034 type_id_in_expr_sentinel
s (parser
);
10035 type2
= cp_parser_type_id (parser
);
10038 if (type2
== error_mark_node
)
10039 return error_mark_node
;
10043 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10045 cp_lexer_consume_token (parser
->lexer
);
10046 tree elt
= cp_parser_type_id (parser
);
10047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10049 cp_lexer_consume_token (parser
->lexer
);
10050 elt
= make_pack_expansion (elt
);
10052 if (elt
== error_mark_node
)
10053 return error_mark_node
;
10054 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10058 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10059 parens
.require_close (parser
);
10061 /* Construct a location of the form:
10062 __is_trivially_copyable(_Tp)
10063 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10064 with start == caret, finishing at the close-paren. */
10065 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10067 /* Complete the trait expression, which may mean either processing
10068 the trait expr now or saving it for template instantiation. */
10071 case CPTK_UNDERLYING_TYPE
:
10072 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10074 return cp_expr (finish_bases (type1
, false), trait_loc
);
10075 case CPTK_DIRECT_BASES
:
10076 return cp_expr (finish_bases (type1
, true), trait_loc
);
10078 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10082 /* Parse a lambda expression.
10085 lambda-introducer lambda-declarator [opt] compound-statement
10087 Returns a representation of the expression. */
10090 cp_parser_lambda_expression (cp_parser
* parser
)
10092 tree lambda_expr
= build_lambda_expr ();
10095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10096 cp_token_position start
= 0;
10098 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10100 if (cp_unevaluated_operand
)
10102 if (!token
->error_reported
)
10104 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10105 "lambda-expression in unevaluated context");
10106 token
->error_reported
= true;
10110 else if (parser
->in_template_argument_list_p
)
10112 if (!token
->error_reported
)
10114 error_at (token
->location
, "lambda-expression in template-argument");
10115 token
->error_reported
= true;
10120 /* We may be in the middle of deferred access check. Disable
10122 push_deferring_access_checks (dk_no_deferred
);
10124 cp_parser_lambda_introducer (parser
, lambda_expr
);
10126 type
= begin_lambda_type (lambda_expr
);
10127 if (type
== error_mark_node
)
10128 return error_mark_node
;
10130 record_lambda_scope (lambda_expr
);
10132 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10133 determine_visibility (TYPE_NAME (type
));
10135 /* Now that we've started the type, add the capture fields for any
10136 explicit captures. */
10137 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10140 /* Inside the class, surrounding template-parameter-lists do not apply. */
10141 unsigned int saved_num_template_parameter_lists
10142 = parser
->num_template_parameter_lists
;
10143 unsigned char in_statement
= parser
->in_statement
;
10144 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10145 bool fully_implicit_function_template_p
10146 = parser
->fully_implicit_function_template_p
;
10147 tree implicit_template_parms
= parser
->implicit_template_parms
;
10148 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10149 bool auto_is_implicit_function_template_parm_p
10150 = parser
->auto_is_implicit_function_template_parm_p
;
10152 parser
->num_template_parameter_lists
= 0;
10153 parser
->in_statement
= 0;
10154 parser
->in_switch_statement_p
= false;
10155 parser
->fully_implicit_function_template_p
= false;
10156 parser
->implicit_template_parms
= 0;
10157 parser
->implicit_template_scope
= 0;
10158 parser
->auto_is_implicit_function_template_parm_p
= false;
10160 /* By virtue of defining a local class, a lambda expression has access to
10161 the private variables of enclosing classes. */
10163 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10165 if (ok
&& cp_parser_error_occurred (parser
))
10170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10171 && cp_parser_start_tentative_firewall (parser
))
10173 cp_parser_lambda_body (parser
, lambda_expr
);
10175 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10177 if (cp_parser_skip_to_closing_brace (parser
))
10178 cp_lexer_consume_token (parser
->lexer
);
10181 /* The capture list was built up in reverse order; fix that now. */
10182 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10183 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10186 maybe_add_lambda_conv_op (type
);
10188 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10190 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10191 parser
->in_statement
= in_statement
;
10192 parser
->in_switch_statement_p
= in_switch_statement_p
;
10193 parser
->fully_implicit_function_template_p
10194 = fully_implicit_function_template_p
;
10195 parser
->implicit_template_parms
= implicit_template_parms
;
10196 parser
->implicit_template_scope
= implicit_template_scope
;
10197 parser
->auto_is_implicit_function_template_parm_p
10198 = auto_is_implicit_function_template_parm_p
;
10201 /* This field is only used during parsing of the lambda. */
10202 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10204 /* This lambda shouldn't have any proxies left at this point. */
10205 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10206 /* And now that we're done, push proxies for an enclosing lambda. */
10207 insert_pending_capture_proxies ();
10209 /* Update the lambda expression to a range. */
10210 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10211 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10213 end_tok
->location
);
10216 lambda_expr
= build_lambda_object (lambda_expr
);
10218 lambda_expr
= error_mark_node
;
10220 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10222 pop_deferring_access_checks ();
10224 return lambda_expr
;
10227 /* Parse the beginning of a lambda expression.
10230 [ lambda-capture [opt] ]
10232 LAMBDA_EXPR is the current representation of the lambda expression. */
10235 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10237 /* Need commas after the first capture. */
10240 /* Eat the leading `['. */
10241 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10243 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10245 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10246 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10247 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10248 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10250 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10252 cp_lexer_consume_token (parser
->lexer
);
10256 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10258 cp_token
* capture_token
;
10260 tree capture_init_expr
;
10261 cp_id_kind idk
= CP_ID_KIND_NONE
;
10262 bool explicit_init_p
= false;
10264 enum capture_kind_type
10269 enum capture_kind_type capture_kind
= BY_COPY
;
10271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10273 error ("expected end of capture-list");
10280 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10282 /* Possibly capture `this'. */
10283 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10285 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10286 if (cxx_dialect
< cxx2a
10287 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10288 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10289 "with by-copy capture default");
10290 cp_lexer_consume_token (parser
->lexer
);
10291 add_capture (lambda_expr
,
10292 /*id=*/this_identifier
,
10293 /*initializer=*/finish_this_expr (),
10294 /*by_reference_p=*/true,
10299 /* Possibly capture `*this'. */
10300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10301 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10303 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10304 if (cxx_dialect
< cxx17
)
10305 pedwarn (loc
, 0, "%<*this%> capture only available with "
10306 "-std=c++17 or -std=gnu++17");
10307 cp_lexer_consume_token (parser
->lexer
);
10308 cp_lexer_consume_token (parser
->lexer
);
10309 add_capture (lambda_expr
,
10310 /*id=*/this_identifier
,
10311 /*initializer=*/finish_this_expr (),
10312 /*by_reference_p=*/false,
10317 /* Remember whether we want to capture as a reference or not. */
10318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10320 capture_kind
= BY_REFERENCE
;
10321 cp_lexer_consume_token (parser
->lexer
);
10324 /* Get the identifier. */
10325 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10326 capture_id
= cp_parser_identifier (parser
);
10328 if (capture_id
== error_mark_node
)
10329 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10330 delimiters, but I modified this to stop on unnested ']' as well. It
10331 was already changed to stop on unnested '}', so the
10332 "closing_parenthesis" name is no more misleading with my change. */
10334 cp_parser_skip_to_closing_parenthesis (parser
,
10335 /*recovering=*/true,
10337 /*consume_paren=*/true);
10341 /* Find the initializer for this capture. */
10342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10343 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10344 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10346 bool direct
, non_constant
;
10347 /* An explicit initializer exists. */
10348 if (cxx_dialect
< cxx14
)
10349 pedwarn (input_location
, 0,
10350 "lambda capture initializers "
10351 "only available with -std=c++14 or -std=gnu++14");
10352 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10353 &non_constant
, true);
10354 explicit_init_p
= true;
10355 if (capture_init_expr
== NULL_TREE
)
10357 error ("empty initializer for lambda init-capture");
10358 capture_init_expr
= error_mark_node
;
10363 const char* error_msg
;
10365 /* Turn the identifier into an id-expression. */
10367 = cp_parser_lookup_name_simple (parser
, capture_id
,
10368 capture_token
->location
);
10370 if (capture_init_expr
== error_mark_node
)
10372 unqualified_name_lookup_error (capture_id
);
10375 else if (!VAR_P (capture_init_expr
)
10376 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10378 error_at (capture_token
->location
,
10379 "capture of non-variable %qE",
10380 capture_init_expr
);
10381 if (DECL_P (capture_init_expr
))
10382 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10383 "%q#D declared here", capture_init_expr
);
10386 if (VAR_P (capture_init_expr
)
10387 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10389 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10390 "%qD with non-automatic storage duration",
10391 capture_init_expr
))
10392 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10393 "%q#D declared here", capture_init_expr
);
10398 = finish_id_expression
10403 /*integral_constant_expression_p=*/false,
10404 /*allow_non_integral_constant_expression_p=*/false,
10405 /*non_integral_constant_expression_p=*/NULL
,
10406 /*template_p=*/false,
10408 /*address_p=*/false,
10409 /*template_arg_p=*/false,
10411 capture_token
->location
);
10413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10415 cp_lexer_consume_token (parser
->lexer
);
10416 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10420 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10421 && !explicit_init_p
)
10423 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10424 && capture_kind
== BY_COPY
)
10425 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10426 "of %qD redundant with by-copy capture default",
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10429 && capture_kind
== BY_REFERENCE
)
10430 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10431 "capture of %qD redundant with by-reference capture "
10432 "default", capture_id
);
10435 add_capture (lambda_expr
,
10438 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10441 /* If there is any qualification still in effect, clear it
10442 now; we will be starting fresh with the next capture. */
10443 parser
->scope
= NULL_TREE
;
10444 parser
->qualifying_scope
= NULL_TREE
;
10445 parser
->object_scope
= NULL_TREE
;
10448 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10451 /* Parse the (optional) middle of a lambda expression.
10454 < template-parameter-list [opt] >
10455 ( parameter-declaration-clause [opt] )
10456 attribute-specifier [opt]
10457 decl-specifier-seq [opt]
10458 exception-specification [opt]
10459 lambda-return-type-clause [opt]
10461 LAMBDA_EXPR is the current representation of the lambda expression. */
10464 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10466 /* 5.1.1.4 of the standard says:
10467 If a lambda-expression does not include a lambda-declarator, it is as if
10468 the lambda-declarator were ().
10469 This means an empty parameter list, no attributes, and no exception
10471 tree param_list
= void_list_node
;
10472 tree attributes
= NULL_TREE
;
10473 tree exception_spec
= NULL_TREE
;
10474 tree template_param_list
= NULL_TREE
;
10475 tree tx_qual
= NULL_TREE
;
10476 tree return_type
= NULL_TREE
;
10477 cp_decl_specifier_seq lambda_specs
;
10478 clear_decl_specs (&lambda_specs
);
10480 /* The template-parameter-list is optional, but must begin with
10481 an opening angle if present. */
10482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10484 if (cxx_dialect
< cxx14
)
10485 pedwarn (parser
->lexer
->next_token
->location
, 0,
10486 "lambda templates are only available with "
10487 "-std=c++14 or -std=gnu++14");
10488 else if (cxx_dialect
< cxx2a
)
10489 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10490 "lambda templates are only available with "
10491 "-std=c++2a or -std=gnu++2a");
10493 cp_lexer_consume_token (parser
->lexer
);
10495 template_param_list
= cp_parser_template_parameter_list (parser
);
10497 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10499 /* We just processed one more parameter list. */
10500 ++parser
->num_template_parameter_lists
;
10503 /* The parameter-declaration-clause is optional (unless
10504 template-parameter-list was given), but must begin with an
10505 opening parenthesis if present. */
10506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10508 matching_parens parens
;
10509 parens
.consume_open (parser
);
10511 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10513 /* Parse parameters. */
10514 param_list
= cp_parser_parameter_declaration_clause (parser
);
10516 /* Default arguments shall not be specified in the
10517 parameter-declaration-clause of a lambda-declarator. */
10518 if (cxx_dialect
< cxx14
)
10519 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10520 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10521 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10522 "default argument specified for lambda parameter");
10524 parens
.require_close (parser
);
10526 attributes
= cp_parser_attributes_opt (parser
);
10528 /* In the decl-specifier-seq of the lambda-declarator, each
10529 decl-specifier shall either be mutable or constexpr. */
10530 int declares_class_or_enum
;
10531 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10532 cp_parser_decl_specifier_seq (parser
,
10533 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10534 &lambda_specs
, &declares_class_or_enum
);
10535 if (lambda_specs
.storage_class
== sc_mutable
)
10537 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10538 if (lambda_specs
.conflicting_specifiers_p
)
10539 error_at (lambda_specs
.locations
[ds_storage_class
],
10540 "duplicate %<mutable%>");
10543 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10545 /* Parse optional exception specification. */
10546 exception_spec
= cp_parser_exception_specification_opt (parser
);
10548 /* Parse optional trailing return type. */
10549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10551 cp_lexer_consume_token (parser
->lexer
);
10552 return_type
= cp_parser_trailing_type_id (parser
);
10555 /* The function parameters must be in scope all the way until after the
10556 trailing-return-type in case of decltype. */
10557 pop_bindings_and_leave_scope ();
10559 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10560 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10562 /* Create the function call operator.
10564 Messing with declarators like this is no uglier than building up the
10565 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10568 cp_decl_specifier_seq return_type_specs
;
10569 cp_declarator
* declarator
;
10574 clear_decl_specs (&return_type_specs
);
10575 return_type_specs
.type
= make_auto ();
10577 if (lambda_specs
.locations
[ds_constexpr
])
10579 if (cxx_dialect
>= cxx17
)
10580 return_type_specs
.locations
[ds_constexpr
]
10581 = lambda_specs
.locations
[ds_constexpr
];
10583 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10584 "lambda only available with -std=c++17 or -std=gnu++17");
10587 p
= obstack_alloc (&declarator_obstack
, 0);
10589 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10591 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10592 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10593 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10594 VIRT_SPEC_UNSPECIFIED
,
10598 /*late_return_type=*/NULL_TREE
,
10599 /*requires_clause*/NULL_TREE
);
10600 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10602 declarator
->u
.function
.late_return_type
= return_type
;
10604 fco
= grokmethod (&return_type_specs
,
10607 if (fco
!= error_mark_node
)
10609 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10610 DECL_ARTIFICIAL (fco
) = 1;
10611 /* Give the object parameter a different name. */
10612 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10614 if (template_param_list
)
10616 fco
= finish_member_template_decl (fco
);
10617 finish_template_decl (template_param_list
);
10618 --parser
->num_template_parameter_lists
;
10620 else if (parser
->fully_implicit_function_template_p
)
10621 fco
= finish_fully_implicit_template (parser
, fco
);
10623 finish_member_declaration (fco
);
10625 obstack_free (&declarator_obstack
, p
);
10627 return (fco
!= error_mark_node
);
10631 /* Parse the body of a lambda expression, which is simply
10635 but which requires special handling.
10636 LAMBDA_EXPR is the current representation of the lambda expression. */
10639 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10641 bool nested
= (current_function_decl
!= NULL_TREE
);
10642 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10643 bool in_function_body
= parser
->in_function_body
;
10646 push_function_context ();
10648 /* Still increment function_depth so that we don't GC in the
10649 middle of an expression. */
10652 vec
<tree
> omp_privatization_save
;
10653 save_omp_privatization_clauses (omp_privatization_save
);
10654 /* Clear this in case we're in the middle of a default argument. */
10655 parser
->local_variables_forbidden_p
= false;
10656 parser
->in_function_body
= true;
10659 local_specialization_stack
s (lss_copy
);
10660 tree fco
= lambda_function (lambda_expr
);
10661 tree body
= start_lambda_function (fco
, lambda_expr
);
10662 matching_braces braces
;
10664 if (braces
.require_open (parser
))
10666 tree compound_stmt
= begin_compound_stmt (0);
10668 /* Originally C++11 required us to peek for 'return expr'; and
10669 process it specially here to deduce the return type. N3638
10670 removed the need for that. */
10672 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10673 cp_parser_label_declaration (parser
);
10674 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10675 braces
.require_close (parser
);
10677 finish_compound_stmt (compound_stmt
);
10680 finish_lambda_function (body
);
10683 restore_omp_privatization_clauses (omp_privatization_save
);
10684 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10685 parser
->in_function_body
= in_function_body
;
10687 pop_function_context();
10692 /* Statements [gram.stmt.stmt] */
10694 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10697 add_debug_begin_stmt (location_t loc
)
10699 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10701 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10702 /* A concept is never expanded normally. */
10705 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10706 SET_EXPR_LOCATION (stmt
, loc
);
10710 /* Parse a statement.
10714 expression-statement
10716 selection-statement
10717 iteration-statement
10719 declaration-statement
10726 attribute-specifier-seq (opt) expression-statement
10727 attribute-specifier-seq (opt) compound-statement
10728 attribute-specifier-seq (opt) selection-statement
10729 attribute-specifier-seq (opt) iteration-statement
10730 attribute-specifier-seq (opt) jump-statement
10731 declaration-statement
10732 attribute-specifier-seq (opt) try-block
10735 expression-statement
10743 IN_COMPOUND is true when the statement is nested inside a
10744 cp_parser_compound_statement; this matters for certain pragmas.
10746 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10747 is a (possibly labeled) if statement which is not enclosed in braces
10748 and has an else clause. This is used to implement -Wparentheses.
10750 CHAIN is a vector of if-else-if conditions. */
10753 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10754 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10755 location_t
*loc_after_labels
)
10757 tree statement
, std_attrs
= NULL_TREE
;
10759 location_t statement_location
, attrs_location
;
10764 /* There is no statement yet. */
10765 statement
= NULL_TREE
;
10767 saved_token_sentinel
saved_tokens (parser
->lexer
);
10768 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10769 if (c_dialect_objc ())
10770 /* In obj-c++, seeing '[[' might be the either the beginning of
10771 c++11 attributes, or a nested objc-message-expression. So
10772 let's parse the c++11 attributes tentatively. */
10773 cp_parser_parse_tentatively (parser
);
10774 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10775 if (c_dialect_objc ())
10777 if (!cp_parser_parse_definitely (parser
))
10778 std_attrs
= NULL_TREE
;
10781 /* Peek at the next token. */
10782 token
= cp_lexer_peek_token (parser
->lexer
);
10783 /* Remember the location of the first token in the statement. */
10784 statement_location
= token
->location
;
10785 add_debug_begin_stmt (statement_location
);
10786 /* If this is a keyword, then that will often determine what kind of
10787 statement we have. */
10788 if (token
->type
== CPP_KEYWORD
)
10790 enum rid keyword
= token
->keyword
;
10796 /* Looks like a labeled-statement with a case label.
10797 Parse the label, and then use tail recursion to parse
10799 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10800 in_compound
= false;
10805 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10811 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10818 statement
= cp_parser_jump_statement (parser
);
10821 /* Objective-C++ exception-handling constructs. */
10824 case RID_AT_FINALLY
:
10825 case RID_AT_SYNCHRONIZED
:
10827 statement
= cp_parser_objc_statement (parser
);
10831 statement
= cp_parser_try_block (parser
);
10834 case RID_NAMESPACE
:
10835 /* This must be a namespace alias definition. */
10836 cp_parser_declaration_statement (parser
);
10839 case RID_TRANSACTION_ATOMIC
:
10840 case RID_TRANSACTION_RELAXED
:
10841 case RID_SYNCHRONIZED
:
10842 case RID_ATOMIC_NOEXCEPT
:
10843 case RID_ATOMIC_CANCEL
:
10844 statement
= cp_parser_transaction (parser
, token
);
10846 case RID_TRANSACTION_CANCEL
:
10847 statement
= cp_parser_transaction_cancel (parser
);
10851 /* It might be a keyword like `int' that can start a
10852 declaration-statement. */
10856 else if (token
->type
== CPP_NAME
)
10858 /* If the next token is a `:', then we are looking at a
10859 labeled-statement. */
10860 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10861 if (token
->type
== CPP_COLON
)
10863 /* Looks like a labeled-statement with an ordinary label.
10864 Parse the label, and then use tail recursion to parse
10867 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10868 in_compound
= false;
10872 /* Anything that starts with a `{' must be a compound-statement. */
10873 else if (token
->type
== CPP_OPEN_BRACE
)
10874 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10875 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10876 a statement all its own. */
10877 else if (token
->type
== CPP_PRAGMA
)
10879 /* Only certain OpenMP pragmas are attached to statements, and thus
10880 are considered statements themselves. All others are not. In
10881 the context of a compound, accept the pragma as a "statement" and
10882 return so that we can check for a close brace. Otherwise we
10883 require a real statement and must go back and read one. */
10885 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10886 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10890 else if (token
->type
== CPP_EOF
)
10892 cp_parser_error (parser
, "expected statement");
10896 /* Everything else must be a declaration-statement or an
10897 expression-statement. Try for the declaration-statement
10898 first, unless we are looking at a `;', in which case we know that
10899 we have an expression-statement. */
10902 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10904 if (std_attrs
!= NULL_TREE
)
10906 /* Attributes should be parsed as part of the the
10907 declaration, so let's un-parse them. */
10908 saved_tokens
.rollback();
10909 std_attrs
= NULL_TREE
;
10912 cp_parser_parse_tentatively (parser
);
10913 /* Try to parse the declaration-statement. */
10914 cp_parser_declaration_statement (parser
);
10915 /* If that worked, we're done. */
10916 if (cp_parser_parse_definitely (parser
))
10919 /* All preceding labels have been parsed at this point. */
10920 if (loc_after_labels
!= NULL
)
10921 *loc_after_labels
= statement_location
;
10923 /* Look for an expression-statement instead. */
10924 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10926 /* Handle [[fallthrough]];. */
10927 if (attribute_fallthrough_p (std_attrs
))
10929 /* The next token after the fallthrough attribute is ';'. */
10930 if (statement
== NULL_TREE
)
10932 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10933 statement
= build_call_expr_internal_loc (statement_location
,
10935 void_type_node
, 0);
10936 finish_expr_stmt (statement
);
10939 warning_at (statement_location
, OPT_Wattributes
,
10940 "%<fallthrough%> attribute not followed by %<;%>");
10941 std_attrs
= NULL_TREE
;
10945 /* Set the line number for the statement. */
10946 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10947 SET_EXPR_LOCATION (statement
, statement_location
);
10949 /* Allow "[[fallthrough]];", but warn otherwise. */
10950 if (std_attrs
!= NULL_TREE
)
10951 warning_at (attrs_location
,
10953 "attributes at the beginning of statement are ignored");
10956 /* Append ATTR to attribute list ATTRS. */
10959 attr_chainon (tree attrs
, tree attr
)
10961 if (attrs
== error_mark_node
)
10962 return error_mark_node
;
10963 if (attr
== error_mark_node
)
10964 return error_mark_node
;
10965 return chainon (attrs
, attr
);
10968 /* Parse the label for a labeled-statement, i.e.
10971 case constant-expression :
10975 case constant-expression ... constant-expression : statement
10977 When a label is parsed without errors, the label is added to the
10978 parse tree by the finish_* functions, so this function doesn't
10979 have to return the label. */
10982 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10985 tree label
= NULL_TREE
;
10986 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10988 /* The next token should be an identifier. */
10989 token
= cp_lexer_peek_token (parser
->lexer
);
10990 if (token
->type
!= CPP_NAME
10991 && token
->type
!= CPP_KEYWORD
)
10993 cp_parser_error (parser
, "expected labeled-statement");
10997 /* Remember whether this case or a user-defined label is allowed to fall
10999 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11001 parser
->colon_corrects_to_scope_p
= false;
11002 switch (token
->keyword
)
11006 tree expr
, expr_hi
;
11007 cp_token
*ellipsis
;
11009 /* Consume the `case' token. */
11010 cp_lexer_consume_token (parser
->lexer
);
11011 /* Parse the constant-expression. */
11012 expr
= cp_parser_constant_expression (parser
);
11013 if (check_for_bare_parameter_packs (expr
))
11014 expr
= error_mark_node
;
11016 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11017 if (ellipsis
->type
== CPP_ELLIPSIS
)
11019 /* Consume the `...' token. */
11020 cp_lexer_consume_token (parser
->lexer
);
11021 expr_hi
= cp_parser_constant_expression (parser
);
11022 if (check_for_bare_parameter_packs (expr_hi
))
11023 expr_hi
= error_mark_node
;
11025 /* We don't need to emit warnings here, as the common code
11026 will do this for us. */
11029 expr_hi
= NULL_TREE
;
11031 if (parser
->in_switch_statement_p
)
11033 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11034 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11035 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11038 error_at (token
->location
,
11039 "case label %qE not within a switch statement",
11045 /* Consume the `default' token. */
11046 cp_lexer_consume_token (parser
->lexer
);
11048 if (parser
->in_switch_statement_p
)
11050 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11051 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11052 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11055 error_at (token
->location
, "case label not within a switch statement");
11059 /* Anything else must be an ordinary label. */
11060 label
= finish_label_stmt (cp_parser_identifier (parser
));
11061 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11062 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11066 /* Require the `:' token. */
11067 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11069 /* An ordinary label may optionally be followed by attributes.
11070 However, this is only permitted if the attributes are then
11071 followed by a semicolon. This is because, for backward
11072 compatibility, when parsing
11073 lab: __attribute__ ((unused)) int i;
11074 we want the attribute to attach to "i", not "lab". */
11075 if (label
!= NULL_TREE
11076 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11079 cp_parser_parse_tentatively (parser
);
11080 attrs
= cp_parser_gnu_attributes_opt (parser
);
11081 if (attrs
== NULL_TREE
11082 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11083 cp_parser_abort_tentative_parse (parser
);
11084 else if (!cp_parser_parse_definitely (parser
))
11087 attributes
= attr_chainon (attributes
, attrs
);
11090 if (attributes
!= NULL_TREE
)
11091 cplus_decl_attributes (&label
, attributes
, 0);
11093 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11096 /* Parse an expression-statement.
11098 expression-statement:
11101 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11102 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11103 indicates whether this expression-statement is part of an
11104 expression statement. */
11107 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11109 tree statement
= NULL_TREE
;
11110 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11111 location_t loc
= token
->location
;
11113 /* There might be attribute fallthrough. */
11114 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11116 /* If the next token is a ';', then there is no expression
11118 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11120 statement
= cp_parser_expression (parser
);
11121 if (statement
== error_mark_node
11122 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11124 cp_parser_skip_to_end_of_block_or_statement (parser
);
11125 return error_mark_node
;
11129 /* Handle [[fallthrough]];. */
11130 if (attribute_fallthrough_p (attr
))
11132 /* The next token after the fallthrough attribute is ';'. */
11133 if (statement
== NULL_TREE
)
11134 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11135 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11136 void_type_node
, 0);
11138 warning_at (loc
, OPT_Wattributes
,
11139 "%<fallthrough%> attribute not followed by %<;%>");
11143 /* Allow "[[fallthrough]];", but warn otherwise. */
11144 if (attr
!= NULL_TREE
)
11145 warning_at (loc
, OPT_Wattributes
,
11146 "attributes at the beginning of statement are ignored");
11148 /* Give a helpful message for "A<T>::type t;" and the like. */
11149 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11150 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11152 if (TREE_CODE (statement
) == SCOPE_REF
)
11153 error_at (token
->location
, "need %<typename%> before %qE because "
11154 "%qT is a dependent scope",
11155 statement
, TREE_OPERAND (statement
, 0));
11156 else if (is_overloaded_fn (statement
)
11157 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11160 tree fn
= get_first_fn (statement
);
11161 error_at (token
->location
,
11162 "%<%T::%D%> names the constructor, not the type",
11163 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11167 /* Consume the final `;'. */
11168 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11170 if (in_statement_expr
11171 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11172 /* This is the final expression statement of a statement
11174 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11175 else if (statement
)
11176 statement
= finish_expr_stmt (statement
);
11181 /* Parse a compound-statement.
11183 compound-statement:
11184 { statement-seq [opt] }
11188 compound-statement:
11189 { label-declaration-seq [opt] statement-seq [opt] }
11191 label-declaration-seq:
11193 label-declaration-seq label-declaration
11195 Returns a tree representing the statement. */
11198 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11199 int bcs_flags
, bool function_body
)
11201 tree compound_stmt
;
11202 matching_braces braces
;
11204 /* Consume the `{'. */
11205 if (!braces
.require_open (parser
))
11206 return error_mark_node
;
11207 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11208 && !function_body
&& cxx_dialect
< cxx14
)
11209 pedwarn (input_location
, OPT_Wpedantic
,
11210 "compound-statement in %<constexpr%> function");
11211 /* Begin the compound-statement. */
11212 compound_stmt
= begin_compound_stmt (bcs_flags
);
11213 /* If the next keyword is `__label__' we have a label declaration. */
11214 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11215 cp_parser_label_declaration (parser
);
11216 /* Parse an (optional) statement-seq. */
11217 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11218 /* Finish the compound-statement. */
11219 finish_compound_stmt (compound_stmt
);
11220 /* Consume the `}'. */
11221 braces
.require_close (parser
);
11223 return compound_stmt
;
11226 /* Parse an (optional) statement-seq.
11230 statement-seq [opt] statement */
11233 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11235 /* Scan statements until there aren't any more. */
11238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11240 /* If we are looking at a `}', then we have run out of
11241 statements; the same is true if we have reached the end
11242 of file, or have stumbled upon a stray '@end'. */
11243 if (token
->type
== CPP_CLOSE_BRACE
11244 || token
->type
== CPP_EOF
11245 || token
->type
== CPP_PRAGMA_EOL
11246 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11249 /* If we are in a compound statement and find 'else' then
11250 something went wrong. */
11251 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11253 if (parser
->in_statement
& IN_IF_STMT
)
11257 token
= cp_lexer_consume_token (parser
->lexer
);
11258 error_at (token
->location
, "%<else%> without a previous %<if%>");
11262 /* Parse the statement. */
11263 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11267 /* Return true if this is the C++20 version of range-based-for with
11271 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11275 /* Save tokens so that we can put them back. */
11276 cp_lexer_save_tokens (parser
->lexer
);
11278 /* There has to be an unnested ; followed by an unnested :. */
11279 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11280 /*recovering=*/false,
11282 /*consume_paren=*/false) != -1)
11285 /* We found the semicolon, eat it now. */
11286 cp_lexer_consume_token (parser
->lexer
);
11288 /* Now look for ':' that is not nested in () or {}. */
11289 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11290 /*recovering=*/false,
11292 /*consume_paren=*/false) == -1);
11295 /* Roll back the tokens we skipped. */
11296 cp_lexer_rollback_tokens (parser
->lexer
);
11301 /* Return true if we're looking at (init; cond), false otherwise. */
11304 cp_parser_init_statement_p (cp_parser
*parser
)
11306 /* Save tokens so that we can put them back. */
11307 cp_lexer_save_tokens (parser
->lexer
);
11309 /* Look for ';' that is not nested in () or {}. */
11310 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11311 /*recovering=*/false,
11313 /*consume_paren=*/false);
11315 /* Roll back the tokens we skipped. */
11316 cp_lexer_rollback_tokens (parser
->lexer
);
11321 /* Parse a selection-statement.
11323 selection-statement:
11324 if ( init-statement [opt] condition ) statement
11325 if ( init-statement [opt] condition ) statement else statement
11326 switch ( init-statement [opt] condition ) statement
11328 Returns the new IF_STMT or SWITCH_STMT.
11330 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11331 is a (possibly labeled) if statement which is not enclosed in
11332 braces and has an else clause. This is used to implement
11335 CHAIN is a vector of if-else-if conditions. This is used to implement
11336 -Wduplicated-cond. */
11339 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11344 token_indent_info guard_tinfo
;
11349 /* Peek at the next token. */
11350 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11351 guard_tinfo
= get_token_indent_info (token
);
11353 /* See what kind of keyword it is. */
11354 keyword
= token
->keyword
;
11364 if (keyword
== RID_IF
11365 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11369 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11370 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11371 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11372 "with -std=c++17 or -std=gnu++17");
11375 /* Look for the `('. */
11376 matching_parens parens
;
11377 if (!parens
.require_open (parser
))
11379 cp_parser_skip_to_end_of_statement (parser
);
11380 return error_mark_node
;
11383 /* Begin the selection-statement. */
11384 if (keyword
== RID_IF
)
11386 statement
= begin_if_stmt ();
11387 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11390 statement
= begin_switch_stmt ();
11392 /* Parse the optional init-statement. */
11393 if (cp_parser_init_statement_p (parser
))
11396 if (cxx_dialect
< cxx17
)
11397 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11398 "init-statement in selection statements only available "
11399 "with -std=c++17 or -std=gnu++17");
11400 cp_parser_init_statement (parser
, &decl
);
11403 /* Parse the condition. */
11404 condition
= cp_parser_condition (parser
);
11405 /* Look for the `)'. */
11406 if (!parens
.require_close (parser
))
11407 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11408 /*consume_paren=*/true);
11410 if (keyword
== RID_IF
)
11413 unsigned char in_statement
;
11415 /* Add the condition. */
11416 condition
= finish_if_stmt_cond (condition
, statement
);
11418 if (warn_duplicated_cond
)
11419 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11422 /* Parse the then-clause. */
11423 in_statement
= parser
->in_statement
;
11424 parser
->in_statement
|= IN_IF_STMT
;
11426 /* Outside a template, the non-selected branch of a constexpr
11427 if is a 'discarded statement', i.e. unevaluated. */
11428 bool was_discarded
= in_discarded_stmt
;
11429 bool discard_then
= (cx
&& !processing_template_decl
11430 && integer_zerop (condition
));
11433 in_discarded_stmt
= true;
11434 ++c_inhibit_evaluation_warnings
;
11437 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11440 parser
->in_statement
= in_statement
;
11442 finish_then_clause (statement
);
11446 THEN_CLAUSE (statement
) = NULL_TREE
;
11447 in_discarded_stmt
= was_discarded
;
11448 --c_inhibit_evaluation_warnings
;
11451 /* If the next token is `else', parse the else-clause. */
11452 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11455 bool discard_else
= (cx
&& !processing_template_decl
11456 && integer_nonzerop (condition
));
11459 in_discarded_stmt
= true;
11460 ++c_inhibit_evaluation_warnings
;
11464 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11465 /* Consume the `else' keyword. */
11466 cp_lexer_consume_token (parser
->lexer
);
11467 if (warn_duplicated_cond
)
11469 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11473 /* We've got "if (COND) else if (COND2)". Start
11474 the condition chain and add COND as the first
11476 chain
= new vec
<tree
> ();
11477 if (!CONSTANT_CLASS_P (condition
)
11478 && !TREE_SIDE_EFFECTS (condition
))
11480 /* Wrap it in a NOP_EXPR so that we can set the
11481 location of the condition. */
11482 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11484 SET_EXPR_LOCATION (e
, token
->location
);
11485 chain
->safe_push (e
);
11488 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11491 /* This is if-else without subsequent if. Zap the
11492 condition chain; we would have already warned at
11498 begin_else_clause (statement
);
11499 /* Parse the else-clause. */
11500 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11501 guard_tinfo
, chain
);
11503 finish_else_clause (statement
);
11505 /* If we are currently parsing a then-clause, then
11506 IF_P will not be NULL. We set it to true to
11507 indicate that this if statement has an else clause.
11508 This may trigger the Wparentheses warning below
11509 when we get back up to the parent if statement. */
11515 ELSE_CLAUSE (statement
) = NULL_TREE
;
11516 in_discarded_stmt
= was_discarded
;
11517 --c_inhibit_evaluation_warnings
;
11522 /* This if statement does not have an else clause. If
11523 NESTED_IF is true, then the then-clause has an if
11524 statement which does have an else clause. We warn
11525 about the potential ambiguity. */
11527 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11528 "suggest explicit braces to avoid ambiguous"
11530 if (warn_duplicated_cond
)
11532 /* We don't need the condition chain anymore. */
11538 /* Now we're all done with the if-statement. */
11539 finish_if_stmt (statement
);
11543 bool in_switch_statement_p
;
11544 unsigned char in_statement
;
11546 /* Add the condition. */
11547 finish_switch_cond (condition
, statement
);
11549 /* Parse the body of the switch-statement. */
11550 in_switch_statement_p
= parser
->in_switch_statement_p
;
11551 in_statement
= parser
->in_statement
;
11552 parser
->in_switch_statement_p
= true;
11553 parser
->in_statement
|= IN_SWITCH_STMT
;
11554 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11556 parser
->in_switch_statement_p
= in_switch_statement_p
;
11557 parser
->in_statement
= in_statement
;
11559 /* Now we're all done with the switch-statement. */
11560 finish_switch_stmt (statement
);
11568 cp_parser_error (parser
, "expected selection-statement");
11569 return error_mark_node
;
11573 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11574 If we have seen at least one decl-specifier, and the next token
11575 is not a parenthesis, then we must be looking at a declaration.
11576 (After "int (" we might be looking at a functional cast.) */
11579 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11580 bool any_specifiers_p
)
11582 if (any_specifiers_p
11583 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11584 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11585 && !cp_parser_error_occurred (parser
))
11586 cp_parser_commit_to_tentative_parse (parser
);
11589 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11590 The declarator shall not specify a function or an array. Returns
11591 TRUE if the declarator is valid, FALSE otherwise. */
11594 cp_parser_check_condition_declarator (cp_parser
* parser
,
11595 cp_declarator
*declarator
,
11598 if (declarator
== cp_error_declarator
11599 || function_declarator_p (declarator
)
11600 || declarator
->kind
== cdk_array
)
11602 if (declarator
== cp_error_declarator
)
11603 /* Already complained. */;
11604 else if (declarator
->kind
== cdk_array
)
11605 error_at (loc
, "condition declares an array");
11607 error_at (loc
, "condition declares a function");
11608 if (parser
->fully_implicit_function_template_p
)
11609 abort_fully_implicit_template (parser
);
11610 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11611 /*or_comma=*/false,
11612 /*consume_paren=*/false);
11619 /* Parse a condition.
11623 type-specifier-seq declarator = initializer-clause
11624 type-specifier-seq declarator braced-init-list
11629 type-specifier-seq declarator asm-specification [opt]
11630 attributes [opt] = assignment-expression
11632 Returns the expression that should be tested. */
11635 cp_parser_condition (cp_parser
* parser
)
11637 cp_decl_specifier_seq type_specifiers
;
11638 const char *saved_message
;
11639 int declares_class_or_enum
;
11641 /* Try the declaration first. */
11642 cp_parser_parse_tentatively (parser
);
11643 /* New types are not allowed in the type-specifier-seq for a
11645 saved_message
= parser
->type_definition_forbidden_message
;
11646 parser
->type_definition_forbidden_message
11647 = G_("types may not be defined in conditions");
11648 /* Parse the type-specifier-seq. */
11649 cp_parser_decl_specifier_seq (parser
,
11650 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11652 &declares_class_or_enum
);
11653 /* Restore the saved message. */
11654 parser
->type_definition_forbidden_message
= saved_message
;
11656 cp_parser_maybe_commit_to_declaration (parser
,
11657 type_specifiers
.any_specifiers_p
);
11659 /* If all is well, we might be looking at a declaration. */
11660 if (!cp_parser_error_occurred (parser
))
11663 tree asm_specification
;
11665 cp_declarator
*declarator
;
11666 tree initializer
= NULL_TREE
;
11667 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11669 /* Parse the declarator. */
11670 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11671 /*ctor_dtor_or_conv_p=*/NULL
,
11672 /*parenthesized_p=*/NULL
,
11673 /*member_p=*/false,
11674 /*friend_p=*/false);
11675 /* Parse the attributes. */
11676 attributes
= cp_parser_attributes_opt (parser
);
11677 /* Parse the asm-specification. */
11678 asm_specification
= cp_parser_asm_specification_opt (parser
);
11679 /* If the next token is not an `=' or '{', then we might still be
11680 looking at an expression. For example:
11684 looks like a decl-specifier-seq and a declarator -- but then
11685 there is no `=', so this is an expression. */
11686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11687 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11688 cp_parser_simulate_error (parser
);
11690 /* If we did see an `=' or '{', then we are looking at a declaration
11692 if (cp_parser_parse_definitely (parser
))
11695 bool non_constant_p
;
11696 int flags
= LOOKUP_ONLYCONVERTING
;
11698 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11699 return error_mark_node
;
11701 /* Create the declaration. */
11702 decl
= start_decl (declarator
, &type_specifiers
,
11703 /*initialized_p=*/true,
11704 attributes
, /*prefix_attributes=*/NULL_TREE
,
11707 /* Parse the initializer. */
11708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11710 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11711 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11714 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11716 /* Consume the `='. */
11717 cp_lexer_consume_token (parser
->lexer
);
11718 initializer
= cp_parser_initializer_clause (parser
,
11723 cp_parser_error (parser
, "expected initializer");
11724 initializer
= error_mark_node
;
11726 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11727 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11729 /* Process the initializer. */
11730 cp_finish_decl (decl
,
11731 initializer
, !non_constant_p
,
11736 pop_scope (pushed_scope
);
11738 return convert_from_reference (decl
);
11741 /* If we didn't even get past the declarator successfully, we are
11742 definitely not looking at a declaration. */
11744 cp_parser_abort_tentative_parse (parser
);
11746 /* Otherwise, we are looking at an expression. */
11747 return cp_parser_expression (parser
);
11750 /* Parses a for-statement or range-for-statement until the closing ')',
11754 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11756 tree init
, scope
, decl
;
11759 /* Begin the for-statement. */
11760 scope
= begin_for_scope (&init
);
11762 /* Parse the initialization. */
11763 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11766 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11768 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11772 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11773 unsigned short unroll
)
11775 /* Normal for loop */
11776 tree condition
= NULL_TREE
;
11777 tree expression
= NULL_TREE
;
11780 stmt
= begin_for_stmt (scope
, init
);
11781 /* The init-statement has already been parsed in
11782 cp_parser_init_statement, so no work is needed here. */
11783 finish_init_stmt (stmt
);
11785 /* If there's a condition, process it. */
11786 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11787 condition
= cp_parser_condition (parser
);
11790 cp_parser_error (parser
, "missing loop condition in loop with "
11791 "%<GCC ivdep%> pragma");
11792 condition
= error_mark_node
;
11796 cp_parser_error (parser
, "missing loop condition in loop with "
11797 "%<GCC unroll%> pragma");
11798 condition
= error_mark_node
;
11800 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11801 /* Look for the `;'. */
11802 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11804 /* If there's an expression, process it. */
11805 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11806 expression
= cp_parser_expression (parser
);
11807 finish_for_expr (expression
, stmt
);
11812 /* Tries to parse a range-based for-statement:
11815 decl-specifier-seq declarator : expression
11817 The decl-specifier-seq declarator and the `:' are already parsed by
11818 cp_parser_init_statement. If processing_template_decl it returns a
11819 newly created RANGE_FOR_STMT; if not, it is converted to a
11820 regular FOR_STMT. */
11823 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11824 bool ivdep
, unsigned short unroll
)
11826 tree stmt
, range_expr
;
11827 auto_vec
<cxx_binding
*, 16> bindings
;
11828 auto_vec
<tree
, 16> names
;
11829 tree decomp_first_name
= NULL_TREE
;
11830 unsigned int decomp_cnt
= 0;
11832 /* Get the range declaration momentarily out of the way so that
11833 the range expression doesn't clash with it. */
11834 if (range_decl
!= error_mark_node
)
11836 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11838 tree v
= DECL_VALUE_EXPR (range_decl
);
11839 /* For decomposition declaration get all of the corresponding
11840 declarations out of the way. */
11841 if (TREE_CODE (v
) == ARRAY_REF
11842 && VAR_P (TREE_OPERAND (v
, 0))
11843 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11845 tree d
= range_decl
;
11846 range_decl
= TREE_OPERAND (v
, 0);
11847 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11848 decomp_first_name
= d
;
11849 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11851 tree name
= DECL_NAME (d
);
11852 names
.safe_push (name
);
11853 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11854 IDENTIFIER_BINDING (name
)
11855 = IDENTIFIER_BINDING (name
)->previous
;
11859 if (names
.is_empty ())
11861 tree name
= DECL_NAME (range_decl
);
11862 names
.safe_push (name
);
11863 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11864 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11870 bool expr_non_constant_p
;
11871 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11874 range_expr
= cp_parser_expression (parser
);
11876 /* Put the range declaration(s) back into scope. */
11877 for (unsigned int i
= 0; i
< names
.length (); i
++)
11879 cxx_binding
*binding
= bindings
[i
];
11880 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11881 IDENTIFIER_BINDING (names
[i
]) = binding
;
11884 /* If in template, STMT is converted to a normal for-statement
11885 at instantiation. If not, it is done just ahead. */
11886 if (processing_template_decl
)
11888 if (check_for_bare_parameter_packs (range_expr
))
11889 range_expr
= error_mark_node
;
11890 stmt
= begin_range_for_stmt (scope
, init
);
11892 RANGE_FOR_IVDEP (stmt
) = 1;
11894 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11895 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11896 if (!type_dependent_expression_p (range_expr
)
11897 /* do_auto_deduction doesn't mess with template init-lists. */
11898 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11899 do_range_for_auto_deduction (range_decl
, range_expr
);
11903 stmt
= begin_for_stmt (scope
, init
);
11904 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11905 decomp_first_name
, decomp_cnt
, ivdep
,
11911 /* Subroutine of cp_convert_range_for: given the initializer expression,
11912 builds up the range temporary. */
11915 build_range_temp (tree range_expr
)
11917 tree range_type
, range_temp
;
11919 /* Find out the type deduced by the declaration
11920 `auto &&__range = range_expr'. */
11921 range_type
= cp_build_reference_type (make_auto (), true);
11922 range_type
= do_auto_deduction (range_type
, range_expr
,
11923 type_uses_auto (range_type
));
11925 /* Create the __range variable. */
11926 range_temp
= build_decl (input_location
, VAR_DECL
,
11927 get_identifier ("__for_range"), range_type
);
11928 TREE_USED (range_temp
) = 1;
11929 DECL_ARTIFICIAL (range_temp
) = 1;
11934 /* Used by cp_parser_range_for in template context: we aren't going to
11935 do a full conversion yet, but we still need to resolve auto in the
11936 type of the for-range-declaration if present. This is basically
11937 a shortcut version of cp_convert_range_for. */
11940 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11942 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11945 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11946 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11947 iter_type
= (cp_parser_perform_range_for_lookup
11948 (range_temp
, &begin_dummy
, &end_dummy
));
11951 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11953 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11955 tf_warning_or_error
);
11956 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11957 iter_decl
, auto_node
);
11962 /* Converts a range-based for-statement into a normal
11963 for-statement, as per the definition.
11965 for (RANGE_DECL : RANGE_EXPR)
11968 should be equivalent to:
11971 auto &&__range = RANGE_EXPR;
11972 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11976 RANGE_DECL = *__begin;
11981 If RANGE_EXPR is an array:
11982 BEGIN_EXPR = __range
11983 END_EXPR = __range + ARRAY_SIZE(__range)
11984 Else if RANGE_EXPR has a member 'begin' or 'end':
11985 BEGIN_EXPR = __range.begin()
11986 END_EXPR = __range.end()
11988 BEGIN_EXPR = begin(__range)
11989 END_EXPR = end(__range);
11991 If __range has a member 'begin' but not 'end', or vice versa, we must
11992 still use the second alternative (it will surely fail, however).
11993 When calling begin()/end() in the third alternative we must use
11994 argument dependent lookup, but always considering 'std' as an associated
11998 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11999 tree decomp_first_name
, unsigned int decomp_cnt
,
12000 bool ivdep
, unsigned short unroll
)
12003 tree iter_type
, begin_expr
, end_expr
;
12004 tree condition
, expression
;
12006 range_expr
= mark_lvalue_use (range_expr
);
12008 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12009 /* If an error happened previously do nothing or else a lot of
12010 unhelpful errors would be issued. */
12011 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12016 if (VAR_P (range_expr
)
12017 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12018 /* Can't bind a reference to an array of runtime bound. */
12019 range_temp
= range_expr
;
12022 range_temp
= build_range_temp (range_expr
);
12023 pushdecl (range_temp
);
12024 cp_finish_decl (range_temp
, range_expr
,
12025 /*is_constant_init*/false, NULL_TREE
,
12026 LOOKUP_ONLYCONVERTING
);
12027 range_temp
= convert_from_reference (range_temp
);
12029 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12030 &begin_expr
, &end_expr
);
12033 /* The new for initialization statement. */
12034 begin
= build_decl (input_location
, VAR_DECL
,
12035 get_identifier ("__for_begin"), iter_type
);
12036 TREE_USED (begin
) = 1;
12037 DECL_ARTIFICIAL (begin
) = 1;
12039 cp_finish_decl (begin
, begin_expr
,
12040 /*is_constant_init*/false, NULL_TREE
,
12041 LOOKUP_ONLYCONVERTING
);
12043 if (cxx_dialect
>= cxx17
)
12044 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12045 end
= build_decl (input_location
, VAR_DECL
,
12046 get_identifier ("__for_end"), iter_type
);
12047 TREE_USED (end
) = 1;
12048 DECL_ARTIFICIAL (end
) = 1;
12050 cp_finish_decl (end
, end_expr
,
12051 /*is_constant_init*/false, NULL_TREE
,
12052 LOOKUP_ONLYCONVERTING
);
12054 finish_init_stmt (statement
);
12056 /* The new for condition. */
12057 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12060 NULL
, tf_warning_or_error
);
12061 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12063 /* The new increment expression. */
12064 expression
= finish_unary_op_expr (input_location
,
12065 PREINCREMENT_EXPR
, begin
,
12066 tf_warning_or_error
);
12067 finish_for_expr (expression
, statement
);
12069 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12070 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12072 /* The declaration is initialized with *__begin inside the loop body. */
12073 cp_finish_decl (range_decl
,
12074 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12075 tf_warning_or_error
),
12076 /*is_constant_init*/false, NULL_TREE
,
12077 LOOKUP_ONLYCONVERTING
);
12078 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12079 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12084 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12085 We need to solve both at the same time because the method used
12086 depends on the existence of members begin or end.
12087 Returns the type deduced for the iterator expression. */
12090 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12092 if (error_operand_p (range
))
12094 *begin
= *end
= error_mark_node
;
12095 return error_mark_node
;
12098 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12100 error ("range-based %<for%> expression of type %qT "
12101 "has incomplete type", TREE_TYPE (range
));
12102 *begin
= *end
= error_mark_node
;
12103 return error_mark_node
;
12105 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12107 /* If RANGE is an array, we will use pointer arithmetic. */
12108 *begin
= decay_conversion (range
, tf_warning_or_error
);
12109 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12111 array_type_nelts_top (TREE_TYPE (range
)),
12113 return TREE_TYPE (*begin
);
12117 /* If it is not an array, we must do a bit of magic. */
12118 tree id_begin
, id_end
;
12119 tree member_begin
, member_end
;
12121 *begin
= *end
= error_mark_node
;
12123 id_begin
= get_identifier ("begin");
12124 id_end
= get_identifier ("end");
12125 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12126 /*protect=*/2, /*want_type=*/false,
12127 tf_warning_or_error
);
12128 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12129 /*protect=*/2, /*want_type=*/false,
12130 tf_warning_or_error
);
12132 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12134 /* Use the member functions. */
12135 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12136 *end
= cp_parser_range_for_member_function (range
, id_end
);
12140 /* Use global functions with ADL. */
12141 vec
<tree
, va_gc
> *vec
;
12142 vec
= make_tree_vector ();
12144 vec_safe_push (vec
, range
);
12146 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12147 tf_warning_or_error
);
12148 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12149 tf_warning_or_error
);
12150 member_end
= perform_koenig_lookup (id_end
, vec
,
12151 tf_warning_or_error
);
12152 *end
= finish_call_expr (member_end
, &vec
, false, true,
12153 tf_warning_or_error
);
12155 release_tree_vector (vec
);
12158 /* Last common checks. */
12159 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12161 /* If one of the expressions is an error do no more checks. */
12162 *begin
= *end
= error_mark_node
;
12163 return error_mark_node
;
12165 else if (type_dependent_expression_p (*begin
)
12166 || type_dependent_expression_p (*end
))
12167 /* Can happen, when, eg, in a template context, Koenig lookup
12168 can't resolve begin/end (c++/58503). */
12172 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12173 /* The unqualified type of the __begin and __end temporaries should
12174 be the same, as required by the multiple auto declaration. */
12175 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12177 if (cxx_dialect
>= cxx17
12178 && (build_x_binary_op (input_location
, NE_EXPR
,
12179 *begin
, ERROR_MARK
,
12182 != error_mark_node
))
12183 /* P0184R0 allows __begin and __end to have different types,
12184 but make sure they are comparable so we can give a better
12187 error ("inconsistent begin/end types in range-based %<for%> "
12188 "statement: %qT and %qT",
12189 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12196 /* Helper function for cp_parser_perform_range_for_lookup.
12197 Builds a tree for RANGE.IDENTIFIER(). */
12200 cp_parser_range_for_member_function (tree range
, tree identifier
)
12203 vec
<tree
, va_gc
> *vec
;
12205 member
= finish_class_member_access_expr (range
, identifier
,
12206 false, tf_warning_or_error
);
12207 if (member
== error_mark_node
)
12208 return error_mark_node
;
12210 vec
= make_tree_vector ();
12211 res
= finish_call_expr (member
, &vec
,
12212 /*disallow_virtual=*/false,
12213 /*koenig_p=*/false,
12214 tf_warning_or_error
);
12215 release_tree_vector (vec
);
12219 /* Parse an iteration-statement.
12221 iteration-statement:
12222 while ( condition ) statement
12223 do statement while ( expression ) ;
12224 for ( init-statement condition [opt] ; expression [opt] )
12227 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12230 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12231 unsigned short unroll
)
12236 unsigned char in_statement
;
12237 token_indent_info guard_tinfo
;
12239 /* Peek at the next token. */
12240 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12242 return error_mark_node
;
12244 guard_tinfo
= get_token_indent_info (token
);
12246 /* Remember whether or not we are already within an iteration
12248 in_statement
= parser
->in_statement
;
12250 /* See what kind of keyword it is. */
12251 keyword
= token
->keyword
;
12258 /* Begin the while-statement. */
12259 statement
= begin_while_stmt ();
12260 /* Look for the `('. */
12261 matching_parens parens
;
12262 parens
.require_open (parser
);
12263 /* Parse the condition. */
12264 condition
= cp_parser_condition (parser
);
12265 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12266 /* Look for the `)'. */
12267 parens
.require_close (parser
);
12268 /* Parse the dependent statement. */
12269 parser
->in_statement
= IN_ITERATION_STMT
;
12270 bool prev
= note_iteration_stmt_body_start ();
12271 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12272 note_iteration_stmt_body_end (prev
);
12273 parser
->in_statement
= in_statement
;
12274 /* We're done with the while-statement. */
12275 finish_while_stmt (statement
);
12283 /* Begin the do-statement. */
12284 statement
= begin_do_stmt ();
12285 /* Parse the body of the do-statement. */
12286 parser
->in_statement
= IN_ITERATION_STMT
;
12287 bool prev
= note_iteration_stmt_body_start ();
12288 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12289 note_iteration_stmt_body_end (prev
);
12290 parser
->in_statement
= in_statement
;
12291 finish_do_body (statement
);
12292 /* Look for the `while' keyword. */
12293 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12294 /* Look for the `('. */
12295 matching_parens parens
;
12296 parens
.require_open (parser
);
12297 /* Parse the expression. */
12298 expression
= cp_parser_expression (parser
);
12299 /* We're done with the do-statement. */
12300 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12301 /* Look for the `)'. */
12302 parens
.require_close (parser
);
12303 /* Look for the `;'. */
12304 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12310 /* Look for the `('. */
12311 matching_parens parens
;
12312 parens
.require_open (parser
);
12314 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12316 /* Look for the `)'. */
12317 parens
.require_close (parser
);
12319 /* Parse the body of the for-statement. */
12320 parser
->in_statement
= IN_ITERATION_STMT
;
12321 bool prev
= note_iteration_stmt_body_start ();
12322 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12323 note_iteration_stmt_body_end (prev
);
12324 parser
->in_statement
= in_statement
;
12326 /* We're done with the for-statement. */
12327 finish_for_stmt (statement
);
12332 cp_parser_error (parser
, "expected iteration-statement");
12333 statement
= error_mark_node
;
12340 /* Parse a init-statement or the declarator of a range-based-for.
12341 Returns true if a range-based-for declaration is seen.
12344 expression-statement
12345 simple-declaration */
12348 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12350 /* If the next token is a `;', then we have an empty
12351 expression-statement. Grammatically, this is also a
12352 simple-declaration, but an invalid one, because it does not
12353 declare anything. Therefore, if we did not handle this case
12354 specially, we would issue an error message about an invalid
12356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12358 bool is_range_for
= false;
12359 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12361 /* Try to parse the init-statement. */
12362 if (cp_parser_range_based_for_with_init_p (parser
))
12365 cp_parser_parse_tentatively (parser
);
12366 /* Parse the declaration. */
12367 cp_parser_simple_declaration (parser
,
12368 /*function_definition_allowed_p=*/false,
12370 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12371 if (!cp_parser_parse_definitely (parser
))
12372 /* That didn't work, try to parse it as an expression-statement. */
12373 cp_parser_expression_statement (parser
, NULL_TREE
);
12375 if (cxx_dialect
< cxx2a
)
12377 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12378 "range-based %<for%> loops with initializer only "
12379 "available with -std=c++2a or -std=gnu++2a");
12380 *decl
= error_mark_node
;
12384 /* A colon is used in range-based for. */
12385 parser
->colon_corrects_to_scope_p
= false;
12387 /* We're going to speculatively look for a declaration, falling back
12388 to an expression, if necessary. */
12389 cp_parser_parse_tentatively (parser
);
12390 /* Parse the declaration. */
12391 cp_parser_simple_declaration (parser
,
12392 /*function_definition_allowed_p=*/false,
12394 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12397 /* It is a range-for, consume the ':'. */
12398 cp_lexer_consume_token (parser
->lexer
);
12399 is_range_for
= true;
12400 if (cxx_dialect
< cxx11
)
12401 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12402 "range-based %<for%> loops only available with "
12403 "-std=c++11 or -std=gnu++11");
12406 /* The ';' is not consumed yet because we told
12407 cp_parser_simple_declaration not to. */
12408 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12410 if (cp_parser_parse_definitely (parser
))
12411 return is_range_for
;
12412 /* If the tentative parse failed, then we shall need to look for an
12413 expression-statement. */
12415 /* If we are here, it is an expression-statement. */
12416 cp_parser_expression_statement (parser
, NULL_TREE
);
12420 /* Parse a jump-statement.
12425 return expression [opt] ;
12426 return braced-init-list ;
12432 goto * expression ;
12434 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12437 cp_parser_jump_statement (cp_parser
* parser
)
12439 tree statement
= error_mark_node
;
12442 unsigned char in_statement
;
12444 /* Peek at the next token. */
12445 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12447 return error_mark_node
;
12449 /* See what kind of keyword it is. */
12450 keyword
= token
->keyword
;
12454 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12455 switch (in_statement
)
12458 error_at (token
->location
, "break statement not within loop or switch");
12461 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12462 || in_statement
== IN_ITERATION_STMT
);
12463 statement
= finish_break_stmt ();
12464 if (in_statement
== IN_ITERATION_STMT
)
12465 break_maybe_infinite_loop ();
12468 error_at (token
->location
, "invalid exit from OpenMP structured block");
12471 error_at (token
->location
, "break statement used with OpenMP for loop");
12474 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12478 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12481 error_at (token
->location
, "continue statement not within a loop");
12483 /* Fall through. */
12484 case IN_ITERATION_STMT
:
12486 statement
= finish_continue_stmt ();
12489 error_at (token
->location
, "invalid exit from OpenMP structured block");
12492 gcc_unreachable ();
12494 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12500 bool expr_non_constant_p
;
12502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12504 cp_lexer_set_source_position (parser
->lexer
);
12505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12506 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12508 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12509 expr
= cp_parser_expression (parser
);
12511 /* If the next token is a `;', then there is no
12514 /* Build the return-statement. */
12515 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12516 /* Don't deduce from a discarded return statement. */;
12518 statement
= finish_return_stmt (expr
);
12519 /* Look for the final `;'. */
12520 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12525 if (parser
->in_function_body
12526 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12528 error ("%<goto%> in %<constexpr%> function");
12529 cp_function_chain
->invalid_constexpr
= true;
12532 /* Create the goto-statement. */
12533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12535 /* Issue a warning about this use of a GNU extension. */
12536 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12537 /* Consume the '*' token. */
12538 cp_lexer_consume_token (parser
->lexer
);
12539 /* Parse the dependent expression. */
12540 finish_goto_stmt (cp_parser_expression (parser
));
12543 finish_goto_stmt (cp_parser_identifier (parser
));
12544 /* Look for the final `;'. */
12545 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12549 cp_parser_error (parser
, "expected jump-statement");
12556 /* Parse a declaration-statement.
12558 declaration-statement:
12559 block-declaration */
12562 cp_parser_declaration_statement (cp_parser
* parser
)
12566 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12567 p
= obstack_alloc (&declarator_obstack
, 0);
12569 /* Parse the block-declaration. */
12570 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12572 /* Free any declarators allocated. */
12573 obstack_free (&declarator_obstack
, p
);
12576 /* Some dependent statements (like `if (cond) statement'), are
12577 implicitly in their own scope. In other words, if the statement is
12578 a single statement (as opposed to a compound-statement), it is
12579 none-the-less treated as if it were enclosed in braces. Any
12580 declarations appearing in the dependent statement are out of scope
12581 after control passes that point. This function parses a statement,
12582 but ensures that is in its own scope, even if it is not a
12583 compound-statement.
12585 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12586 is a (possibly labeled) if statement which is not enclosed in
12587 braces and has an else clause. This is used to implement
12590 CHAIN is a vector of if-else-if conditions. This is used to implement
12593 Returns the new statement. */
12596 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12597 const token_indent_info
&guard_tinfo
,
12601 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12602 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12603 token_indent_info body_tinfo
12604 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12609 /* Mark if () ; with a special NOP_EXPR. */
12610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12612 cp_lexer_consume_token (parser
->lexer
);
12613 statement
= add_stmt (build_empty_stmt (body_loc
));
12615 if (guard_tinfo
.keyword
== RID_IF
12616 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12617 warning_at (body_loc
, OPT_Wempty_body
,
12618 "suggest braces around empty body in an %<if%> statement");
12619 else if (guard_tinfo
.keyword
== RID_ELSE
)
12620 warning_at (body_loc
, OPT_Wempty_body
,
12621 "suggest braces around empty body in an %<else%> statement");
12623 /* if a compound is opened, we simply parse the statement directly. */
12624 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12625 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12626 /* If the token is not a `{', then we must take special action. */
12629 /* Create a compound-statement. */
12630 statement
= begin_compound_stmt (0);
12631 /* Parse the dependent-statement. */
12632 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12633 &body_loc_after_labels
);
12634 /* Finish the dummy compound-statement. */
12635 finish_compound_stmt (statement
);
12638 token_indent_info next_tinfo
12639 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12640 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12642 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12643 && next_tinfo
.type
!= CPP_SEMICOLON
)
12644 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12645 guard_tinfo
.location
, guard_tinfo
.keyword
);
12647 /* Return the statement. */
12651 /* For some dependent statements (like `while (cond) statement'), we
12652 have already created a scope. Therefore, even if the dependent
12653 statement is a compound-statement, we do not want to create another
12657 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12658 const token_indent_info
&guard_tinfo
)
12660 /* If the token is a `{', then we must take special action. */
12661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12663 token_indent_info body_tinfo
12664 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12665 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12667 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12668 &loc_after_labels
);
12669 token_indent_info next_tinfo
12670 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12671 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12673 if (loc_after_labels
!= UNKNOWN_LOCATION
12674 && next_tinfo
.type
!= CPP_SEMICOLON
)
12675 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12676 guard_tinfo
.location
,
12677 guard_tinfo
.keyword
);
12681 /* Avoid calling cp_parser_compound_statement, so that we
12682 don't create a new scope. Do everything else by hand. */
12683 matching_braces braces
;
12684 braces
.require_open (parser
);
12685 /* If the next keyword is `__label__' we have a label declaration. */
12686 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12687 cp_parser_label_declaration (parser
);
12688 /* Parse an (optional) statement-seq. */
12689 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12690 braces
.require_close (parser
);
12694 /* Declarations [gram.dcl.dcl] */
12696 /* Parse an optional declaration-sequence.
12700 declaration-seq declaration */
12703 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12709 token
= cp_lexer_peek_token (parser
->lexer
);
12711 if (token
->type
== CPP_CLOSE_BRACE
12712 || token
->type
== CPP_EOF
12713 || token
->type
== CPP_PRAGMA_EOL
)
12716 if (token
->type
== CPP_SEMICOLON
)
12718 /* A declaration consisting of a single semicolon is
12719 invalid. Allow it unless we're being pedantic. */
12720 cp_lexer_consume_token (parser
->lexer
);
12721 if (!in_system_header_at (input_location
))
12722 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12726 /* If we're entering or exiting a region that's implicitly
12727 extern "C", modify the lang context appropriately. */
12728 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12730 push_lang_context (lang_name_c
);
12731 parser
->implicit_extern_c
= true;
12733 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12735 pop_lang_context ();
12736 parser
->implicit_extern_c
= false;
12739 if (token
->type
== CPP_PRAGMA
)
12741 /* A top-level declaration can consist solely of a #pragma.
12742 A nested declaration cannot, so this is done here and not
12743 in cp_parser_declaration. (A #pragma at block scope is
12744 handled in cp_parser_statement.) */
12745 cp_parser_pragma (parser
, pragma_external
, NULL
);
12749 /* Parse the declaration itself. */
12750 cp_parser_declaration (parser
);
12754 /* Parse a declaration.
12758 function-definition
12759 template-declaration
12760 explicit-instantiation
12761 explicit-specialization
12762 linkage-specification
12763 namespace-definition
12771 __extension__ declaration */
12774 cp_parser_declaration (cp_parser
* parser
)
12778 int saved_pedantic
;
12780 tree attributes
= NULL_TREE
;
12782 /* Check for the `__extension__' keyword. */
12783 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12785 /* Parse the qualified declaration. */
12786 cp_parser_declaration (parser
);
12787 /* Restore the PEDANTIC flag. */
12788 pedantic
= saved_pedantic
;
12793 /* Try to figure out what kind of declaration is present. */
12794 token1
= *cp_lexer_peek_token (parser
->lexer
);
12796 if (token1
.type
!= CPP_EOF
)
12797 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12800 token2
.type
= CPP_EOF
;
12801 token2
.keyword
= RID_MAX
;
12804 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12805 p
= obstack_alloc (&declarator_obstack
, 0);
12807 /* If the next token is `extern' and the following token is a string
12808 literal, then we have a linkage specification. */
12809 if (token1
.keyword
== RID_EXTERN
12810 && cp_parser_is_pure_string_literal (&token2
))
12811 cp_parser_linkage_specification (parser
);
12812 /* If the next token is `template', then we have either a template
12813 declaration, an explicit instantiation, or an explicit
12815 else if (token1
.keyword
== RID_TEMPLATE
)
12817 /* `template <>' indicates a template specialization. */
12818 if (token2
.type
== CPP_LESS
12819 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12820 cp_parser_explicit_specialization (parser
);
12821 /* `template <' indicates a template declaration. */
12822 else if (token2
.type
== CPP_LESS
)
12823 cp_parser_template_declaration (parser
, /*member_p=*/false);
12824 /* Anything else must be an explicit instantiation. */
12826 cp_parser_explicit_instantiation (parser
);
12828 /* If the next token is `export', then we have a template
12830 else if (token1
.keyword
== RID_EXPORT
)
12831 cp_parser_template_declaration (parser
, /*member_p=*/false);
12832 /* If the next token is `extern', 'static' or 'inline' and the one
12833 after that is `template', we have a GNU extended explicit
12834 instantiation directive. */
12835 else if (cp_parser_allow_gnu_extensions_p (parser
)
12836 && (token1
.keyword
== RID_EXTERN
12837 || token1
.keyword
== RID_STATIC
12838 || token1
.keyword
== RID_INLINE
)
12839 && token2
.keyword
== RID_TEMPLATE
)
12840 cp_parser_explicit_instantiation (parser
);
12841 /* If the next token is `namespace', check for a named or unnamed
12842 namespace definition. */
12843 else if (token1
.keyword
== RID_NAMESPACE
12844 && (/* A named namespace definition. */
12845 (token2
.type
== CPP_NAME
12846 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12848 || (token2
.type
== CPP_OPEN_SQUARE
12849 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12850 == CPP_OPEN_SQUARE
)
12851 /* An unnamed namespace definition. */
12852 || token2
.type
== CPP_OPEN_BRACE
12853 || token2
.keyword
== RID_ATTRIBUTE
))
12854 cp_parser_namespace_definition (parser
);
12855 /* An inline (associated) namespace definition. */
12856 else if (token1
.keyword
== RID_INLINE
12857 && token2
.keyword
== RID_NAMESPACE
)
12858 cp_parser_namespace_definition (parser
);
12859 /* Objective-C++ declaration/definition. */
12860 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12861 cp_parser_objc_declaration (parser
, NULL_TREE
);
12862 else if (c_dialect_objc ()
12863 && token1
.keyword
== RID_ATTRIBUTE
12864 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12865 cp_parser_objc_declaration (parser
, attributes
);
12866 /* At this point we may have a template declared by a concept
12868 else if (flag_concepts
12869 && cp_parser_template_declaration_after_export (parser
,
12870 /*member_p=*/false))
12873 /* Try to parse a block-declaration, or a function-definition. */
12874 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12876 /* Free any declarators allocated. */
12877 obstack_free (&declarator_obstack
, p
);
12880 /* Parse a block-declaration.
12885 namespace-alias-definition
12892 __extension__ block-declaration
12897 static_assert-declaration
12899 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12900 part of a declaration-statement. */
12903 cp_parser_block_declaration (cp_parser
*parser
,
12907 int saved_pedantic
;
12909 /* Check for the `__extension__' keyword. */
12910 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12912 /* Parse the qualified declaration. */
12913 cp_parser_block_declaration (parser
, statement_p
);
12914 /* Restore the PEDANTIC flag. */
12915 pedantic
= saved_pedantic
;
12920 /* Peek at the next token to figure out which kind of declaration is
12922 token1
= cp_lexer_peek_token (parser
->lexer
);
12924 /* If the next keyword is `asm', we have an asm-definition. */
12925 if (token1
->keyword
== RID_ASM
)
12928 cp_parser_commit_to_tentative_parse (parser
);
12929 cp_parser_asm_definition (parser
);
12931 /* If the next keyword is `namespace', we have a
12932 namespace-alias-definition. */
12933 else if (token1
->keyword
== RID_NAMESPACE
)
12934 cp_parser_namespace_alias_definition (parser
);
12935 /* If the next keyword is `using', we have a
12936 using-declaration, a using-directive, or an alias-declaration. */
12937 else if (token1
->keyword
== RID_USING
)
12942 cp_parser_commit_to_tentative_parse (parser
);
12943 /* If the token after `using' is `namespace', then we have a
12944 using-directive. */
12945 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12946 if (token2
->keyword
== RID_NAMESPACE
)
12947 cp_parser_using_directive (parser
);
12948 /* If the second token after 'using' is '=', then we have an
12949 alias-declaration. */
12950 else if (cxx_dialect
>= cxx11
12951 && token2
->type
== CPP_NAME
12952 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12953 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12954 cp_parser_alias_declaration (parser
);
12955 /* Otherwise, it's a using-declaration. */
12957 cp_parser_using_declaration (parser
,
12958 /*access_declaration_p=*/false);
12960 /* If the next keyword is `__label__' we have a misplaced label
12962 else if (token1
->keyword
== RID_LABEL
)
12964 cp_lexer_consume_token (parser
->lexer
);
12965 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12966 cp_parser_skip_to_end_of_statement (parser
);
12967 /* If the next token is now a `;', consume it. */
12968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12969 cp_lexer_consume_token (parser
->lexer
);
12971 /* If the next token is `static_assert' we have a static assertion. */
12972 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12973 cp_parser_static_assert (parser
, /*member_p=*/false);
12974 /* Anything else must be a simple-declaration. */
12976 cp_parser_simple_declaration (parser
, !statement_p
,
12977 /*maybe_range_for_decl*/NULL
);
12980 /* Parse a simple-declaration.
12982 simple-declaration:
12983 decl-specifier-seq [opt] init-declarator-list [opt] ;
12984 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12985 brace-or-equal-initializer ;
12987 init-declarator-list:
12989 init-declarator-list , init-declarator
12991 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12992 function-definition as a simple-declaration.
12994 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12995 parsed declaration if it is an uninitialized single declarator not followed
12996 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12997 if present, will not be consumed. */
13000 cp_parser_simple_declaration (cp_parser
* parser
,
13001 bool function_definition_allowed_p
,
13002 tree
*maybe_range_for_decl
)
13004 cp_decl_specifier_seq decl_specifiers
;
13005 int declares_class_or_enum
;
13006 bool saw_declarator
;
13007 location_t comma_loc
= UNKNOWN_LOCATION
;
13008 location_t init_loc
= UNKNOWN_LOCATION
;
13010 if (maybe_range_for_decl
)
13011 *maybe_range_for_decl
= NULL_TREE
;
13013 /* Defer access checks until we know what is being declared; the
13014 checks for names appearing in the decl-specifier-seq should be
13015 done as if we were in the scope of the thing being declared. */
13016 push_deferring_access_checks (dk_deferred
);
13018 /* Parse the decl-specifier-seq. We have to keep track of whether
13019 or not the decl-specifier-seq declares a named class or
13020 enumeration type, since that is the only case in which the
13021 init-declarator-list is allowed to be empty.
13025 In a simple-declaration, the optional init-declarator-list can be
13026 omitted only when declaring a class or enumeration, that is when
13027 the decl-specifier-seq contains either a class-specifier, an
13028 elaborated-type-specifier, or an enum-specifier. */
13029 cp_parser_decl_specifier_seq (parser
,
13030 CP_PARSER_FLAGS_OPTIONAL
,
13032 &declares_class_or_enum
);
13033 /* We no longer need to defer access checks. */
13034 stop_deferring_access_checks ();
13036 /* In a block scope, a valid declaration must always have a
13037 decl-specifier-seq. By not trying to parse declarators, we can
13038 resolve the declaration/expression ambiguity more quickly. */
13039 if (!function_definition_allowed_p
13040 && !decl_specifiers
.any_specifiers_p
)
13042 cp_parser_error (parser
, "expected declaration");
13046 /* If the next two tokens are both identifiers, the code is
13047 erroneous. The usual cause of this situation is code like:
13051 where "T" should name a type -- but does not. */
13052 if (!decl_specifiers
.any_type_specifiers_p
13053 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13055 /* If parsing tentatively, we should commit; we really are
13056 looking at a declaration. */
13057 cp_parser_commit_to_tentative_parse (parser
);
13062 cp_parser_maybe_commit_to_declaration (parser
,
13063 decl_specifiers
.any_specifiers_p
);
13065 /* Look for C++17 decomposition declaration. */
13066 for (size_t n
= 1; ; n
++)
13067 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13068 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13070 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13071 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13072 && decl_specifiers
.any_specifiers_p
)
13075 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13076 maybe_range_for_decl
,
13079 /* The next token should be either a `,' or a `;'. */
13080 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13081 /* If it's a `;', we are done. */
13082 if (token
->type
== CPP_SEMICOLON
)
13084 else if (maybe_range_for_decl
)
13086 if (*maybe_range_for_decl
== NULL_TREE
)
13087 *maybe_range_for_decl
= error_mark_node
;
13090 /* Anything else is an error. */
13093 /* If we have already issued an error message we don't need
13094 to issue another one. */
13095 if ((decl
!= error_mark_node
13096 && DECL_INITIAL (decl
) != error_mark_node
)
13097 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13098 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13099 /* Skip tokens until we reach the end of the statement. */
13100 cp_parser_skip_to_end_of_statement (parser
);
13101 /* If the next token is now a `;', consume it. */
13102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13103 cp_lexer_consume_token (parser
->lexer
);
13111 bool auto_specifier_p
;
13112 /* NULL_TREE if both variable and function declaration are allowed,
13113 error_mark_node if function declaration are not allowed and
13114 a FUNCTION_DECL that should be diagnosed if it is followed by
13115 variable declarations. */
13116 tree auto_function_declaration
;
13118 last_type
= NULL_TREE
;
13120 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13121 auto_function_declaration
= NULL_TREE
;
13123 /* Keep going until we hit the `;' at the end of the simple
13125 saw_declarator
= false;
13126 while (cp_lexer_next_token_is_not (parser
->lexer
,
13130 bool function_definition_p
;
13132 tree auto_result
= NULL_TREE
;
13134 if (saw_declarator
)
13136 /* If we are processing next declarator, comma is expected */
13137 token
= cp_lexer_peek_token (parser
->lexer
);
13138 gcc_assert (token
->type
== CPP_COMMA
);
13139 cp_lexer_consume_token (parser
->lexer
);
13140 if (maybe_range_for_decl
)
13142 *maybe_range_for_decl
= error_mark_node
;
13143 if (comma_loc
== UNKNOWN_LOCATION
)
13144 comma_loc
= token
->location
;
13148 saw_declarator
= true;
13150 /* Parse the init-declarator. */
13151 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13153 function_definition_allowed_p
,
13154 /*member_p=*/false,
13155 declares_class_or_enum
,
13156 &function_definition_p
,
13157 maybe_range_for_decl
,
13160 /* If an error occurred while parsing tentatively, exit quickly.
13161 (That usually happens when in the body of a function; each
13162 statement is treated as a declaration-statement until proven
13164 if (cp_parser_error_occurred (parser
))
13167 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13169 /* If the init-declarator-list contains more than one
13170 init-declarator, they shall all form declarations of
13172 if (auto_function_declaration
== NULL_TREE
)
13173 auto_function_declaration
13174 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13175 else if (TREE_CODE (decl
) == FUNCTION_DECL
13176 || auto_function_declaration
!= error_mark_node
)
13178 error_at (decl_specifiers
.locations
[ds_type_spec
],
13179 "non-variable %qD in declaration with more than one "
13180 "declarator with placeholder type",
13181 TREE_CODE (decl
) == FUNCTION_DECL
13182 ? decl
: auto_function_declaration
);
13183 auto_function_declaration
= error_mark_node
;
13188 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13191 && last_type
!= error_mark_node
13192 && !same_type_p (auto_result
, last_type
))
13194 /* If the list of declarators contains more than one declarator,
13195 the type of each declared variable is determined as described
13196 above. If the type deduced for the template parameter U is not
13197 the same in each deduction, the program is ill-formed. */
13198 error_at (decl_specifiers
.locations
[ds_type_spec
],
13199 "inconsistent deduction for %qT: %qT and then %qT",
13200 decl_specifiers
.type
, last_type
, auto_result
);
13201 last_type
= error_mark_node
;
13204 last_type
= auto_result
;
13207 /* Handle function definitions specially. */
13208 if (function_definition_p
)
13210 /* If the next token is a `,', then we are probably
13211 processing something like:
13215 which is erroneous. */
13216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13218 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13219 error_at (token
->location
,
13221 " declarations and function-definitions is forbidden");
13223 /* Otherwise, we're done with the list of declarators. */
13226 pop_deferring_access_checks ();
13230 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13231 *maybe_range_for_decl
= decl
;
13232 /* The next token should be either a `,' or a `;'. */
13233 token
= cp_lexer_peek_token (parser
->lexer
);
13234 /* If it's a `,', there are more declarators to come. */
13235 if (token
->type
== CPP_COMMA
)
13236 /* will be consumed next time around */;
13237 /* If it's a `;', we are done. */
13238 else if (token
->type
== CPP_SEMICOLON
)
13240 else if (maybe_range_for_decl
)
13242 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13243 permerror (decl_specifiers
.locations
[ds_type_spec
],
13244 "types may not be defined in a for-range-declaration");
13247 /* Anything else is an error. */
13250 /* If we have already issued an error message we don't need
13251 to issue another one. */
13252 if ((decl
!= error_mark_node
13253 && DECL_INITIAL (decl
) != error_mark_node
)
13254 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13255 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13256 /* Skip tokens until we reach the end of the statement. */
13257 cp_parser_skip_to_end_of_statement (parser
);
13258 /* If the next token is now a `;', consume it. */
13259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13260 cp_lexer_consume_token (parser
->lexer
);
13263 /* After the first time around, a function-definition is not
13264 allowed -- even if it was OK at first. For example:
13269 function_definition_allowed_p
= false;
13272 /* Issue an error message if no declarators are present, and the
13273 decl-specifier-seq does not itself declare a class or
13274 enumeration: [dcl.dcl]/3. */
13275 if (!saw_declarator
)
13277 if (cp_parser_declares_only_class_p (parser
))
13279 if (!declares_class_or_enum
13280 && decl_specifiers
.type
13281 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13282 /* Ensure an error is issued anyway when finish_decltype_type,
13283 called via cp_parser_decl_specifier_seq, returns a class or
13284 an enumeration (c++/51786). */
13285 decl_specifiers
.type
= NULL_TREE
;
13286 shadow_tag (&decl_specifiers
);
13288 /* Perform any deferred access checks. */
13289 perform_deferred_access_checks (tf_warning_or_error
);
13292 /* Consume the `;'. */
13294 if (!maybe_range_for_decl
)
13295 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13296 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13298 if (init_loc
!= UNKNOWN_LOCATION
)
13299 error_at (init_loc
, "initializer in range-based %<for%> loop");
13300 if (comma_loc
!= UNKNOWN_LOCATION
)
13301 error_at (comma_loc
,
13302 "multiple declarations in range-based %<for%> loop");
13306 pop_deferring_access_checks ();
13309 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13310 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13314 cp_parser_decomposition_declaration (cp_parser
*parser
,
13315 cp_decl_specifier_seq
*decl_specifiers
,
13316 tree
*maybe_range_for_decl
,
13317 location_t
*init_loc
)
13319 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13320 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13321 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13323 /* Parse the identifier-list. */
13324 auto_vec
<cp_expr
, 10> v
;
13325 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13328 cp_expr e
= cp_parser_identifier (parser
);
13329 if (e
.get_value () == error_mark_node
)
13332 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13334 cp_lexer_consume_token (parser
->lexer
);
13337 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13338 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13340 end_loc
= UNKNOWN_LOCATION
;
13341 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13344 cp_lexer_consume_token (parser
->lexer
);
13347 cp_parser_skip_to_end_of_statement (parser
);
13348 return error_mark_node
;
13352 if (cxx_dialect
< cxx17
)
13353 pedwarn (loc
, 0, "structured bindings only available with "
13354 "-std=c++17 or -std=gnu++17");
13357 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13358 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13359 declarator
->id_loc
= loc
;
13360 if (ref_qual
!= REF_QUAL_NONE
)
13361 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13362 ref_qual
== REF_QUAL_RVALUE
,
13364 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13365 NULL_TREE
, decl_specifiers
->attributes
,
13367 tree orig_decl
= decl
;
13371 cp_decl_specifier_seq decl_specs
;
13372 clear_decl_specs (&decl_specs
);
13373 decl_specs
.type
= make_auto ();
13375 FOR_EACH_VEC_ELT (v
, i
, e
)
13378 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13380 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13381 declarator
->id_loc
= e
.get_location ();
13382 tree elt_pushed_scope
;
13383 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13384 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13385 if (decl2
== error_mark_node
)
13386 decl
= error_mark_node
;
13387 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13389 /* Ensure we've diagnosed redeclaration if we aren't creating
13391 gcc_assert (errorcount
);
13392 decl
= error_mark_node
;
13396 if (elt_pushed_scope
)
13397 pop_scope (elt_pushed_scope
);
13402 error_at (loc
, "empty structured binding declaration");
13403 decl
= error_mark_node
;
13406 if (maybe_range_for_decl
== NULL
13407 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13409 bool non_constant_p
= false, is_direct_init
= false;
13410 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13411 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13413 if (initializer
== NULL_TREE
13414 || (TREE_CODE (initializer
) == TREE_LIST
13415 && TREE_CHAIN (initializer
))
13417 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13418 && CONSTRUCTOR_NELTS (initializer
) != 1))
13420 error_at (loc
, "invalid initializer for structured binding "
13422 initializer
= error_mark_node
;
13425 if (decl
!= error_mark_node
)
13427 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13428 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13429 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13430 cp_finish_decomp (decl
, prev
, v
.length ());
13433 else if (decl
!= error_mark_node
)
13435 *maybe_range_for_decl
= prev
;
13436 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13437 the underlying DECL. */
13438 cp_finish_decomp (decl
, prev
, v
.length ());
13442 pop_scope (pushed_scope
);
13444 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13446 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13447 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13453 /* Parse a decl-specifier-seq.
13455 decl-specifier-seq:
13456 decl-specifier-seq [opt] decl-specifier
13457 decl-specifier attribute-specifier-seq [opt] (C++11)
13460 storage-class-specifier
13471 Concepts Extension:
13476 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13478 The parser flags FLAGS is used to control type-specifier parsing.
13480 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13483 1: one of the decl-specifiers is an elaborated-type-specifier
13484 (i.e., a type declaration)
13485 2: one of the decl-specifiers is an enum-specifier or a
13486 class-specifier (i.e., a type definition)
13491 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13492 cp_parser_flags flags
,
13493 cp_decl_specifier_seq
*decl_specs
,
13494 int* declares_class_or_enum
)
13496 bool constructor_possible_p
= !parser
->in_declarator_p
;
13497 bool found_decl_spec
= false;
13498 cp_token
*start_token
= NULL
;
13501 /* Clear DECL_SPECS. */
13502 clear_decl_specs (decl_specs
);
13504 /* Assume no class or enumeration type is declared. */
13505 *declares_class_or_enum
= 0;
13507 /* Keep reading specifiers until there are no more to read. */
13510 bool constructor_p
;
13514 /* Peek at the next token. */
13515 token
= cp_lexer_peek_token (parser
->lexer
);
13517 /* Save the first token of the decl spec list for error
13520 start_token
= token
;
13521 /* Handle attributes. */
13522 if (cp_next_tokens_can_be_attribute_p (parser
))
13524 /* Parse the attributes. */
13525 tree attrs
= cp_parser_attributes_opt (parser
);
13527 /* In a sequence of declaration specifiers, c++11 attributes
13528 appertain to the type that precede them. In that case
13531 The attribute-specifier-seq affects the type only for
13532 the declaration it appears in, not other declarations
13533 involving the same type.
13535 But for now let's force the user to position the
13536 attribute either at the beginning of the declaration or
13537 after the declarator-id, which would clearly mean that it
13538 applies to the declarator. */
13539 if (cxx11_attribute_p (attrs
))
13541 if (!found_decl_spec
)
13542 /* The c++11 attribute is at the beginning of the
13543 declaration. It appertains to the entity being
13547 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13549 /* This is an attribute following a
13550 class-specifier. */
13551 if (decl_specs
->type_definition_p
)
13552 warn_misplaced_attr_for_class_type (token
->location
,
13558 decl_specs
->std_attributes
13559 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13560 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13561 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13567 decl_specs
->attributes
13568 = attr_chainon (decl_specs
->attributes
, attrs
);
13569 if (decl_specs
->locations
[ds_attribute
] == 0)
13570 decl_specs
->locations
[ds_attribute
] = token
->location
;
13573 /* Assume we will find a decl-specifier keyword. */
13574 found_decl_spec
= true;
13575 /* If the next token is an appropriate keyword, we can simply
13576 add it to the list. */
13577 switch (token
->keyword
)
13583 if (!at_class_scope_p ())
13585 gcc_rich_location
richloc (token
->location
);
13586 richloc
.add_fixit_remove ();
13587 error_at (&richloc
, "%<friend%> used outside of class");
13588 cp_lexer_purge_token (parser
->lexer
);
13593 /* Consume the token. */
13594 cp_lexer_consume_token (parser
->lexer
);
13598 case RID_CONSTEXPR
:
13600 cp_lexer_consume_token (parser
->lexer
);
13605 cp_lexer_consume_token (parser
->lexer
);
13608 /* function-specifier:
13615 cp_parser_function_specifier_opt (parser
, decl_specs
);
13622 /* Consume the token. */
13623 cp_lexer_consume_token (parser
->lexer
);
13624 /* A constructor declarator cannot appear in a typedef. */
13625 constructor_possible_p
= false;
13626 /* The "typedef" keyword can only occur in a declaration; we
13627 may as well commit at this point. */
13628 cp_parser_commit_to_tentative_parse (parser
);
13630 if (decl_specs
->storage_class
!= sc_none
)
13631 decl_specs
->conflicting_specifiers_p
= true;
13634 /* storage-class-specifier:
13644 if (cxx_dialect
== cxx98
)
13646 /* Consume the token. */
13647 cp_lexer_consume_token (parser
->lexer
);
13649 /* Complain about `auto' as a storage specifier, if
13650 we're complaining about C++0x compatibility. */
13651 gcc_rich_location
richloc (token
->location
);
13652 richloc
.add_fixit_remove ();
13653 warning_at (&richloc
, OPT_Wc__11_compat
,
13654 "%<auto%> changes meaning in C++11; "
13655 "please remove it");
13657 /* Set the storage class anyway. */
13658 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13662 /* C++0x auto type-specifier. */
13663 found_decl_spec
= false;
13670 /* Consume the token. */
13671 cp_lexer_consume_token (parser
->lexer
);
13672 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13676 /* Consume the token. */
13678 cp_lexer_consume_token (parser
->lexer
);
13682 /* We did not yet find a decl-specifier yet. */
13683 found_decl_spec
= false;
13687 if (found_decl_spec
13688 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13689 && token
->keyword
!= RID_CONSTEXPR
)
13690 error ("decl-specifier invalid in condition");
13692 if (found_decl_spec
13693 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13694 && token
->keyword
!= RID_MUTABLE
13695 && token
->keyword
!= RID_CONSTEXPR
)
13696 error_at (token
->location
, "%qD invalid in lambda",
13697 ridpointers
[token
->keyword
]);
13700 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13702 /* Constructors are a special case. The `S' in `S()' is not a
13703 decl-specifier; it is the beginning of the declarator. */
13705 = (!found_decl_spec
13706 && constructor_possible_p
13707 && (cp_parser_constructor_declarator_p
13708 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13710 /* If we don't have a DECL_SPEC yet, then we must be looking at
13711 a type-specifier. */
13712 if (!found_decl_spec
&& !constructor_p
)
13714 int decl_spec_declares_class_or_enum
;
13715 bool is_cv_qualifier
;
13719 = cp_parser_type_specifier (parser
, flags
,
13721 /*is_declaration=*/true,
13722 &decl_spec_declares_class_or_enum
,
13724 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13726 /* If this type-specifier referenced a user-defined type
13727 (a typedef, class-name, etc.), then we can't allow any
13728 more such type-specifiers henceforth.
13732 The longest sequence of decl-specifiers that could
13733 possibly be a type name is taken as the
13734 decl-specifier-seq of a declaration. The sequence shall
13735 be self-consistent as described below.
13739 As a general rule, at most one type-specifier is allowed
13740 in the complete decl-specifier-seq of a declaration. The
13741 only exceptions are the following:
13743 -- const or volatile can be combined with any other
13746 -- signed or unsigned can be combined with char, long,
13754 void g (const int Pc);
13756 Here, Pc is *not* part of the decl-specifier seq; it's
13757 the declarator. Therefore, once we see a type-specifier
13758 (other than a cv-qualifier), we forbid any additional
13759 user-defined types. We *do* still allow things like `int
13760 int' to be considered a decl-specifier-seq, and issue the
13761 error message later. */
13762 if (type_spec
&& !is_cv_qualifier
)
13763 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13764 /* A constructor declarator cannot follow a type-specifier. */
13767 constructor_possible_p
= false;
13768 found_decl_spec
= true;
13769 if (!is_cv_qualifier
)
13770 decl_specs
->any_type_specifiers_p
= true;
13774 /* If we still do not have a DECL_SPEC, then there are no more
13775 decl-specifiers. */
13776 if (!found_decl_spec
)
13779 decl_specs
->any_specifiers_p
= true;
13780 /* After we see one decl-specifier, further decl-specifiers are
13781 always optional. */
13782 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13785 /* Don't allow a friend specifier with a class definition. */
13786 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13787 && (*declares_class_or_enum
& 2))
13788 error_at (decl_specs
->locations
[ds_friend
],
13789 "class definition may not be declared a friend");
13792 /* Parse an (optional) storage-class-specifier.
13794 storage-class-specifier:
13803 storage-class-specifier:
13806 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13809 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13811 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13814 if (cxx_dialect
!= cxx98
)
13816 /* Fall through for C++98. */
13817 gcc_fallthrough ();
13824 /* Consume the token. */
13825 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13832 /* Parse an (optional) function-specifier.
13834 function-specifier:
13839 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13840 Updates DECL_SPECS, if it is non-NULL. */
13843 cp_parser_function_specifier_opt (cp_parser
* parser
,
13844 cp_decl_specifier_seq
*decl_specs
)
13846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13847 switch (token
->keyword
)
13850 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13854 /* 14.5.2.3 [temp.mem]
13856 A member function template shall not be virtual. */
13857 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13858 && current_class_type
)
13859 error_at (token
->location
, "templates may not be %<virtual%>");
13861 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13865 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13872 /* Consume the token. */
13873 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13876 /* Parse a linkage-specification.
13878 linkage-specification:
13879 extern string-literal { declaration-seq [opt] }
13880 extern string-literal declaration */
13883 cp_parser_linkage_specification (cp_parser
* parser
)
13887 /* Look for the `extern' keyword. */
13888 cp_token
*extern_token
13889 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13891 /* Look for the string-literal. */
13892 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13893 linkage
= cp_parser_string_literal (parser
, false, false);
13895 /* Transform the literal into an identifier. If the literal is a
13896 wide-character string, or contains embedded NULs, then we can't
13897 handle it as the user wants. */
13898 if (strlen (TREE_STRING_POINTER (linkage
))
13899 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13901 cp_parser_error (parser
, "invalid linkage-specification");
13902 /* Assume C++ linkage. */
13903 linkage
= lang_name_cplusplus
;
13906 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13908 /* We're now using the new linkage. */
13909 push_lang_context (linkage
);
13911 /* Preserve the location of the the innermost linkage specification,
13912 tracking the locations of nested specifications via a local. */
13913 location_t saved_location
13914 = parser
->innermost_linkage_specification_location
;
13915 /* Construct a location ranging from the start of the "extern" to
13916 the end of the string-literal, with the caret at the start, e.g.:
13920 parser
->innermost_linkage_specification_location
13921 = make_location (extern_token
->location
,
13922 extern_token
->location
,
13923 get_finish (string_token
->location
));
13925 /* If the next token is a `{', then we're using the first
13927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13929 cp_ensure_no_omp_declare_simd (parser
);
13930 cp_ensure_no_oacc_routine (parser
);
13932 /* Consume the `{' token. */
13933 matching_braces braces
;
13934 braces
.consume_open (parser
)->location
;
13935 /* Parse the declarations. */
13936 cp_parser_declaration_seq_opt (parser
);
13937 /* Look for the closing `}'. */
13938 braces
.require_close (parser
);
13940 /* Otherwise, there's just one declaration. */
13943 bool saved_in_unbraced_linkage_specification_p
;
13945 saved_in_unbraced_linkage_specification_p
13946 = parser
->in_unbraced_linkage_specification_p
;
13947 parser
->in_unbraced_linkage_specification_p
= true;
13948 cp_parser_declaration (parser
);
13949 parser
->in_unbraced_linkage_specification_p
13950 = saved_in_unbraced_linkage_specification_p
;
13953 /* We're done with the linkage-specification. */
13954 pop_lang_context ();
13956 /* Restore location of parent linkage specification, if any. */
13957 parser
->innermost_linkage_specification_location
= saved_location
;
13960 /* Parse a static_assert-declaration.
13962 static_assert-declaration:
13963 static_assert ( constant-expression , string-literal ) ;
13964 static_assert ( constant-expression ) ; (C++17)
13966 If MEMBER_P, this static_assert is a class member. */
13969 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13972 location_t token_loc
;
13976 /* Peek at the `static_assert' token so we can keep track of exactly
13977 where the static assertion started. */
13978 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13980 /* Look for the `static_assert' keyword. */
13981 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13985 /* We know we are in a static assertion; commit to any tentative
13987 if (cp_parser_parsing_tentatively (parser
))
13988 cp_parser_commit_to_tentative_parse (parser
);
13990 /* Parse the `(' starting the static assertion condition. */
13991 matching_parens parens
;
13992 parens
.require_open (parser
);
13994 /* Parse the constant-expression. Allow a non-constant expression
13995 here in order to give better diagnostics in finish_static_assert. */
13997 cp_parser_constant_expression (parser
,
13998 /*allow_non_constant_p=*/true,
13999 /*non_constant_p=*/&dummy
);
14001 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14003 if (cxx_dialect
< cxx17
)
14004 pedwarn (input_location
, OPT_Wpedantic
,
14005 "static_assert without a message "
14006 "only available with -std=c++17 or -std=gnu++17");
14008 cp_lexer_consume_token (parser
->lexer
);
14009 message
= build_string (1, "");
14010 TREE_TYPE (message
) = char_array_type_node
;
14011 fix_string_type (message
);
14015 /* Parse the separating `,'. */
14016 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14018 /* Parse the string-literal message. */
14019 message
= cp_parser_string_literal (parser
,
14020 /*translate=*/false,
14023 /* A `)' completes the static assertion. */
14024 if (!parens
.require_close (parser
))
14025 cp_parser_skip_to_closing_parenthesis (parser
,
14026 /*recovering=*/true,
14027 /*or_comma=*/false,
14028 /*consume_paren=*/true);
14031 /* A semicolon terminates the declaration. */
14032 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14034 /* Get the location for the static assertion. Use that of the
14035 condition if available, otherwise, use that of the "static_assert"
14037 location_t assert_loc
= condition
.get_location ();
14038 if (assert_loc
== UNKNOWN_LOCATION
)
14039 assert_loc
= token_loc
;
14041 /* Complete the static assertion, which may mean either processing
14042 the static assert now or saving it for template instantiation. */
14043 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14046 /* Parse the expression in decltype ( expression ). */
14049 cp_parser_decltype_expr (cp_parser
*parser
,
14050 bool &id_expression_or_member_access_p
)
14052 cp_token
*id_expr_start_token
;
14055 /* Since we're going to preserve any side-effects from this parse, set up a
14056 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14057 in the expression. */
14058 tentative_firewall
firewall (parser
);
14060 /* First, try parsing an id-expression. */
14061 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14062 cp_parser_parse_tentatively (parser
);
14063 expr
= cp_parser_id_expression (parser
,
14064 /*template_keyword_p=*/false,
14065 /*check_dependency_p=*/true,
14066 /*template_p=*/NULL
,
14067 /*declarator_p=*/false,
14068 /*optional_p=*/false);
14070 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14072 bool non_integral_constant_expression_p
= false;
14073 tree id_expression
= expr
;
14075 const char *error_msg
;
14077 if (identifier_p (expr
))
14078 /* Lookup the name we got back from the id-expression. */
14079 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14080 id_expr_start_token
->location
);
14082 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14083 /* A template without args is not a complete id-expression. */
14084 expr
= error_mark_node
;
14087 && expr
!= error_mark_node
14088 && TREE_CODE (expr
) != TYPE_DECL
14089 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14090 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14091 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14093 /* Complete lookup of the id-expression. */
14094 expr
= (finish_id_expression
14095 (id_expression
, expr
, parser
->scope
, &idk
,
14096 /*integral_constant_expression_p=*/false,
14097 /*allow_non_integral_constant_expression_p=*/true,
14098 &non_integral_constant_expression_p
,
14099 /*template_p=*/false,
14101 /*address_p=*/false,
14102 /*template_arg_p=*/false,
14104 id_expr_start_token
->location
));
14106 if (expr
== error_mark_node
)
14107 /* We found an id-expression, but it was something that we
14108 should not have found. This is an error, not something
14109 we can recover from, so note that we found an
14110 id-expression and we'll recover as gracefully as
14112 id_expression_or_member_access_p
= true;
14116 && expr
!= error_mark_node
14117 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14118 /* We have an id-expression. */
14119 id_expression_or_member_access_p
= true;
14122 if (!id_expression_or_member_access_p
)
14124 /* Abort the id-expression parse. */
14125 cp_parser_abort_tentative_parse (parser
);
14127 /* Parsing tentatively, again. */
14128 cp_parser_parse_tentatively (parser
);
14130 /* Parse a class member access. */
14131 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14132 /*cast_p=*/false, /*decltype*/true,
14133 /*member_access_only_p=*/true, NULL
);
14136 && expr
!= error_mark_node
14137 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14138 /* We have an id-expression. */
14139 id_expression_or_member_access_p
= true;
14142 if (id_expression_or_member_access_p
)
14143 /* We have parsed the complete id-expression or member access. */
14144 cp_parser_parse_definitely (parser
);
14147 /* Abort our attempt to parse an id-expression or member access
14149 cp_parser_abort_tentative_parse (parser
);
14151 /* Commit to the tentative_firewall so we get syntax errors. */
14152 cp_parser_commit_to_tentative_parse (parser
);
14154 /* Parse a full expression. */
14155 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14156 /*decltype_p=*/true);
14162 /* Parse a `decltype' type. Returns the type.
14164 simple-type-specifier:
14165 decltype ( expression )
14167 decltype ( auto ) */
14170 cp_parser_decltype (cp_parser
*parser
)
14172 bool id_expression_or_member_access_p
= false;
14173 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14175 if (start_token
->type
== CPP_DECLTYPE
)
14177 /* Already parsed. */
14178 cp_lexer_consume_token (parser
->lexer
);
14179 return saved_checks_value (start_token
->u
.tree_check_value
);
14182 /* Look for the `decltype' token. */
14183 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14184 return error_mark_node
;
14186 /* Parse the opening `('. */
14187 matching_parens parens
;
14188 if (!parens
.require_open (parser
))
14189 return error_mark_node
;
14191 push_deferring_access_checks (dk_deferred
);
14193 tree expr
= NULL_TREE
;
14195 if (cxx_dialect
>= cxx14
14196 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14197 /* decltype (auto) */
14198 cp_lexer_consume_token (parser
->lexer
);
14201 /* decltype (expression) */
14203 /* Types cannot be defined in a `decltype' expression. Save away the
14204 old message and set the new one. */
14205 const char *saved_message
= parser
->type_definition_forbidden_message
;
14206 parser
->type_definition_forbidden_message
14207 = G_("types may not be defined in %<decltype%> expressions");
14209 /* The restrictions on constant-expressions do not apply inside
14210 decltype expressions. */
14211 bool saved_integral_constant_expression_p
14212 = parser
->integral_constant_expression_p
;
14213 bool saved_non_integral_constant_expression_p
14214 = parser
->non_integral_constant_expression_p
;
14215 parser
->integral_constant_expression_p
= false;
14217 /* Within a parenthesized expression, a `>' token is always
14218 the greater-than operator. */
14219 bool saved_greater_than_is_operator_p
14220 = parser
->greater_than_is_operator_p
;
14221 parser
->greater_than_is_operator_p
= true;
14223 /* Do not actually evaluate the expression. */
14224 ++cp_unevaluated_operand
;
14226 /* Do not warn about problems with the expression. */
14227 ++c_inhibit_evaluation_warnings
;
14229 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14231 /* Go back to evaluating expressions. */
14232 --cp_unevaluated_operand
;
14233 --c_inhibit_evaluation_warnings
;
14235 /* The `>' token might be the end of a template-id or
14236 template-parameter-list now. */
14237 parser
->greater_than_is_operator_p
14238 = saved_greater_than_is_operator_p
;
14240 /* Restore the old message and the integral constant expression
14242 parser
->type_definition_forbidden_message
= saved_message
;
14243 parser
->integral_constant_expression_p
14244 = saved_integral_constant_expression_p
;
14245 parser
->non_integral_constant_expression_p
14246 = saved_non_integral_constant_expression_p
;
14249 /* Parse to the closing `)'. */
14250 if (!parens
.require_close (parser
))
14252 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14253 /*consume_paren=*/true);
14254 pop_deferring_access_checks ();
14255 return error_mark_node
;
14261 expr
= make_decltype_auto ();
14262 AUTO_IS_DECLTYPE (expr
) = true;
14265 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14266 tf_warning_or_error
);
14268 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14270 start_token
->type
= CPP_DECLTYPE
;
14271 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14272 start_token
->u
.tree_check_value
->value
= expr
;
14273 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14274 start_token
->keyword
= RID_MAX
;
14275 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14277 pop_to_parent_deferring_access_checks ();
14282 /* Special member functions [gram.special] */
14284 /* Parse a conversion-function-id.
14286 conversion-function-id:
14287 operator conversion-type-id
14289 Returns an IDENTIFIER_NODE representing the operator. */
14292 cp_parser_conversion_function_id (cp_parser
* parser
)
14296 tree saved_qualifying_scope
;
14297 tree saved_object_scope
;
14298 tree pushed_scope
= NULL_TREE
;
14300 /* Look for the `operator' token. */
14301 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14302 return error_mark_node
;
14303 /* When we parse the conversion-type-id, the current scope will be
14304 reset. However, we need that information in able to look up the
14305 conversion function later, so we save it here. */
14306 saved_scope
= parser
->scope
;
14307 saved_qualifying_scope
= parser
->qualifying_scope
;
14308 saved_object_scope
= parser
->object_scope
;
14309 /* We must enter the scope of the class so that the names of
14310 entities declared within the class are available in the
14311 conversion-type-id. For example, consider:
14318 S::operator I() { ... }
14320 In order to see that `I' is a type-name in the definition, we
14321 must be in the scope of `S'. */
14323 pushed_scope
= push_scope (saved_scope
);
14324 /* Parse the conversion-type-id. */
14325 type
= cp_parser_conversion_type_id (parser
);
14326 /* Leave the scope of the class, if any. */
14328 pop_scope (pushed_scope
);
14329 /* Restore the saved scope. */
14330 parser
->scope
= saved_scope
;
14331 parser
->qualifying_scope
= saved_qualifying_scope
;
14332 parser
->object_scope
= saved_object_scope
;
14333 /* If the TYPE is invalid, indicate failure. */
14334 if (type
== error_mark_node
)
14335 return error_mark_node
;
14336 return make_conv_op_name (type
);
14339 /* Parse a conversion-type-id:
14341 conversion-type-id:
14342 type-specifier-seq conversion-declarator [opt]
14344 Returns the TYPE specified. */
14347 cp_parser_conversion_type_id (cp_parser
* parser
)
14350 cp_decl_specifier_seq type_specifiers
;
14351 cp_declarator
*declarator
;
14352 tree type_specified
;
14353 const char *saved_message
;
14355 /* Parse the attributes. */
14356 attributes
= cp_parser_attributes_opt (parser
);
14358 saved_message
= parser
->type_definition_forbidden_message
;
14359 parser
->type_definition_forbidden_message
14360 = G_("types may not be defined in a conversion-type-id");
14362 /* Parse the type-specifiers. */
14363 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14364 /*is_trailing_return=*/false,
14367 parser
->type_definition_forbidden_message
= saved_message
;
14369 /* If that didn't work, stop. */
14370 if (type_specifiers
.type
== error_mark_node
)
14371 return error_mark_node
;
14372 /* Parse the conversion-declarator. */
14373 declarator
= cp_parser_conversion_declarator_opt (parser
);
14375 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14376 /*initialized=*/0, &attributes
);
14378 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14380 /* Don't give this error when parsing tentatively. This happens to
14381 work because we always parse this definitively once. */
14382 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14383 && type_uses_auto (type_specified
))
14385 if (cxx_dialect
< cxx14
)
14387 error ("invalid use of %<auto%> in conversion operator");
14388 return error_mark_node
;
14390 else if (template_parm_scope_p ())
14391 warning (0, "use of %<auto%> in member template "
14392 "conversion operator can never be deduced");
14395 return type_specified
;
14398 /* Parse an (optional) conversion-declarator.
14400 conversion-declarator:
14401 ptr-operator conversion-declarator [opt]
14405 static cp_declarator
*
14406 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14408 enum tree_code code
;
14409 tree class_type
, std_attributes
= NULL_TREE
;
14410 cp_cv_quals cv_quals
;
14412 /* We don't know if there's a ptr-operator next, or not. */
14413 cp_parser_parse_tentatively (parser
);
14414 /* Try the ptr-operator. */
14415 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14417 /* If it worked, look for more conversion-declarators. */
14418 if (cp_parser_parse_definitely (parser
))
14420 cp_declarator
*declarator
;
14422 /* Parse another optional declarator. */
14423 declarator
= cp_parser_conversion_declarator_opt (parser
);
14425 declarator
= cp_parser_make_indirect_declarator
14426 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14434 /* Parse an (optional) ctor-initializer.
14437 : mem-initializer-list */
14440 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14442 /* If the next token is not a `:', then there is no
14443 ctor-initializer. */
14444 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14446 /* Do default initialization of any bases and members. */
14447 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14448 finish_mem_initializers (NULL_TREE
);
14452 /* Consume the `:' token. */
14453 cp_lexer_consume_token (parser
->lexer
);
14454 /* And the mem-initializer-list. */
14455 cp_parser_mem_initializer_list (parser
);
14458 /* Parse a mem-initializer-list.
14460 mem-initializer-list:
14461 mem-initializer ... [opt]
14462 mem-initializer ... [opt] , mem-initializer-list */
14465 cp_parser_mem_initializer_list (cp_parser
* parser
)
14467 tree mem_initializer_list
= NULL_TREE
;
14468 tree target_ctor
= error_mark_node
;
14469 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14471 /* Let the semantic analysis code know that we are starting the
14472 mem-initializer-list. */
14473 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14474 error_at (token
->location
,
14475 "only constructors take member initializers");
14477 /* Loop through the list. */
14480 tree mem_initializer
;
14482 token
= cp_lexer_peek_token (parser
->lexer
);
14483 /* Parse the mem-initializer. */
14484 mem_initializer
= cp_parser_mem_initializer (parser
);
14485 /* If the next token is a `...', we're expanding member initializers. */
14486 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14488 || (mem_initializer
!= error_mark_node
14489 && check_for_bare_parameter_packs (TREE_PURPOSE
14490 (mem_initializer
))))
14492 /* Consume the `...'. */
14494 cp_lexer_consume_token (parser
->lexer
);
14496 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14497 can be expanded but members cannot. */
14498 if (mem_initializer
!= error_mark_node
14499 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14501 error_at (token
->location
,
14502 "cannot expand initializer for member %qD",
14503 TREE_PURPOSE (mem_initializer
));
14504 mem_initializer
= error_mark_node
;
14507 /* Construct the pack expansion type. */
14508 if (mem_initializer
!= error_mark_node
)
14509 mem_initializer
= make_pack_expansion (mem_initializer
);
14511 if (target_ctor
!= error_mark_node
14512 && mem_initializer
!= error_mark_node
)
14514 error ("mem-initializer for %qD follows constructor delegation",
14515 TREE_PURPOSE (mem_initializer
));
14516 mem_initializer
= error_mark_node
;
14518 /* Look for a target constructor. */
14519 if (mem_initializer
!= error_mark_node
14520 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14521 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14523 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14524 if (mem_initializer_list
)
14526 error ("constructor delegation follows mem-initializer for %qD",
14527 TREE_PURPOSE (mem_initializer_list
));
14528 mem_initializer
= error_mark_node
;
14530 target_ctor
= mem_initializer
;
14532 /* Add it to the list, unless it was erroneous. */
14533 if (mem_initializer
!= error_mark_node
)
14535 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14536 mem_initializer_list
= mem_initializer
;
14538 /* If the next token is not a `,', we're done. */
14539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14541 /* Consume the `,' token. */
14542 cp_lexer_consume_token (parser
->lexer
);
14545 /* Perform semantic analysis. */
14546 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14547 finish_mem_initializers (mem_initializer_list
);
14550 /* Parse a mem-initializer.
14553 mem-initializer-id ( expression-list [opt] )
14554 mem-initializer-id braced-init-list
14559 ( expression-list [opt] )
14561 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14562 class) or FIELD_DECL (for a non-static data member) to initialize;
14563 the TREE_VALUE is the expression-list. An empty initialization
14564 list is represented by void_list_node. */
14567 cp_parser_mem_initializer (cp_parser
* parser
)
14569 tree mem_initializer_id
;
14570 tree expression_list
;
14572 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14574 /* Find out what is being initialized. */
14575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14577 permerror (token
->location
,
14578 "anachronistic old-style base class initializer");
14579 mem_initializer_id
= NULL_TREE
;
14583 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14584 if (mem_initializer_id
== error_mark_node
)
14585 return mem_initializer_id
;
14587 member
= expand_member_init (mem_initializer_id
);
14588 if (member
&& !DECL_P (member
))
14589 in_base_initializer
= 1;
14591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14593 bool expr_non_constant_p
;
14594 cp_lexer_set_source_position (parser
->lexer
);
14595 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14596 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14597 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14598 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14602 vec
<tree
, va_gc
> *vec
;
14603 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14605 /*allow_expansion_p=*/true,
14606 /*non_constant_p=*/NULL
);
14608 return error_mark_node
;
14609 expression_list
= build_tree_list_vec (vec
);
14610 release_tree_vector (vec
);
14613 if (expression_list
== error_mark_node
)
14614 return error_mark_node
;
14615 if (!expression_list
)
14616 expression_list
= void_type_node
;
14618 in_base_initializer
= 0;
14620 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14623 /* Parse a mem-initializer-id.
14625 mem-initializer-id:
14626 :: [opt] nested-name-specifier [opt] class-name
14627 decltype-specifier (C++11)
14630 Returns a TYPE indicating the class to be initialized for the first
14631 production (and the second in C++11). Returns an IDENTIFIER_NODE
14632 indicating the data member to be initialized for the last production. */
14635 cp_parser_mem_initializer_id (cp_parser
* parser
)
14637 bool global_scope_p
;
14638 bool nested_name_specifier_p
;
14639 bool template_p
= false;
14642 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14644 /* `typename' is not allowed in this context ([temp.res]). */
14645 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14647 error_at (token
->location
,
14648 "keyword %<typename%> not allowed in this context (a qualified "
14649 "member initializer is implicitly a type)");
14650 cp_lexer_consume_token (parser
->lexer
);
14652 /* Look for the optional `::' operator. */
14654 = (cp_parser_global_scope_opt (parser
,
14655 /*current_scope_valid_p=*/false)
14657 /* Look for the optional nested-name-specifier. The simplest way to
14662 The keyword `typename' is not permitted in a base-specifier or
14663 mem-initializer; in these contexts a qualified name that
14664 depends on a template-parameter is implicitly assumed to be a
14667 is to assume that we have seen the `typename' keyword at this
14669 nested_name_specifier_p
14670 = (cp_parser_nested_name_specifier_opt (parser
,
14671 /*typename_keyword_p=*/true,
14672 /*check_dependency_p=*/true,
14674 /*is_declaration=*/true)
14676 if (nested_name_specifier_p
)
14677 template_p
= cp_parser_optional_template_keyword (parser
);
14678 /* If there is a `::' operator or a nested-name-specifier, then we
14679 are definitely looking for a class-name. */
14680 if (global_scope_p
|| nested_name_specifier_p
)
14681 return cp_parser_class_name (parser
,
14682 /*typename_keyword_p=*/true,
14683 /*template_keyword_p=*/template_p
,
14685 /*check_dependency_p=*/true,
14686 /*class_head_p=*/false,
14687 /*is_declaration=*/true);
14688 /* Otherwise, we could also be looking for an ordinary identifier. */
14689 cp_parser_parse_tentatively (parser
);
14690 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14691 /* Try a decltype-specifier. */
14692 id
= cp_parser_decltype (parser
);
14694 /* Otherwise, try a class-name. */
14695 id
= cp_parser_class_name (parser
,
14696 /*typename_keyword_p=*/true,
14697 /*template_keyword_p=*/false,
14699 /*check_dependency_p=*/true,
14700 /*class_head_p=*/false,
14701 /*is_declaration=*/true);
14702 /* If we found one, we're done. */
14703 if (cp_parser_parse_definitely (parser
))
14705 /* Otherwise, look for an ordinary identifier. */
14706 return cp_parser_identifier (parser
);
14709 /* Overloading [gram.over] */
14711 /* Parse an operator-function-id.
14713 operator-function-id:
14716 Returns an IDENTIFIER_NODE for the operator which is a
14717 human-readable spelling of the identifier, e.g., `operator +'. */
14720 cp_parser_operator_function_id (cp_parser
* parser
)
14722 /* Look for the `operator' keyword. */
14723 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14724 return error_mark_node
;
14725 /* And then the name of the operator itself. */
14726 return cp_parser_operator (parser
);
14729 /* Return an identifier node for a user-defined literal operator.
14730 The suffix identifier is chained to the operator name identifier. */
14733 cp_literal_operator_id (const char* name
)
14736 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14737 + strlen (name
) + 10);
14738 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14739 identifier
= get_identifier (buffer
);
14744 /* Parse an operator.
14747 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14748 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14749 || ++ -- , ->* -> () []
14756 Returns an IDENTIFIER_NODE for the operator which is a
14757 human-readable spelling of the identifier, e.g., `operator +'. */
14760 cp_parser_operator (cp_parser
* parser
)
14762 tree id
= NULL_TREE
;
14766 /* Peek at the next token. */
14767 token
= cp_lexer_peek_token (parser
->lexer
);
14769 location_t start_loc
= token
->location
;
14771 /* Figure out which operator we have. */
14772 enum tree_code op
= ERROR_MARK
;
14773 bool assop
= false;
14774 bool consumed
= false;
14775 switch (token
->type
)
14779 /* The keyword should be either `new' or `delete'. */
14780 if (token
->keyword
== RID_NEW
)
14782 else if (token
->keyword
== RID_DELETE
)
14787 /* Consume the `new' or `delete' token. */
14788 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14790 /* Peek at the next token. */
14791 token
= cp_lexer_peek_token (parser
->lexer
);
14792 /* If it's a `[' token then this is the array variant of the
14794 if (token
->type
== CPP_OPEN_SQUARE
)
14796 /* Consume the `[' token. */
14797 cp_lexer_consume_token (parser
->lexer
);
14798 /* Look for the `]' token. */
14799 if (cp_token
*close_token
14800 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14801 end_loc
= close_token
->location
;
14802 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14804 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14822 op
= TRUNC_DIV_EXPR
;
14826 op
= TRUNC_MOD_EXPR
;
14846 op
= TRUTH_NOT_EXPR
;
14879 op
= TRUNC_DIV_EXPR
;
14884 op
= TRUNC_MOD_EXPR
;
14910 case CPP_LSHIFT_EQ
:
14915 case CPP_RSHIFT_EQ
:
14932 case CPP_GREATER_EQ
:
14937 op
= TRUTH_ANDIF_EXPR
;
14941 op
= TRUTH_ORIF_EXPR
;
14944 case CPP_PLUS_PLUS
:
14945 op
= POSTINCREMENT_EXPR
;
14948 case CPP_MINUS_MINUS
:
14949 op
= PREDECREMENT_EXPR
;
14953 op
= COMPOUND_EXPR
;
14956 case CPP_DEREF_STAR
:
14961 op
= COMPONENT_REF
;
14964 case CPP_OPEN_PAREN
:
14966 /* Consume the `('. */
14967 matching_parens parens
;
14968 parens
.consume_open (parser
);
14969 /* Look for the matching `)'. */
14970 parens
.require_close (parser
);
14976 case CPP_OPEN_SQUARE
:
14977 /* Consume the `['. */
14978 cp_lexer_consume_token (parser
->lexer
);
14979 /* Look for the matching `]'. */
14980 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14985 case CPP_UTF8STRING
:
14986 case CPP_UTF8STRING_USERDEF
:
14993 case CPP_STRING_USERDEF
:
14994 case CPP_WSTRING_USERDEF
:
14995 case CPP_STRING16_USERDEF
:
14996 case CPP_STRING32_USERDEF
:
14998 tree str
, string_tree
;
15001 if (cxx_dialect
== cxx98
)
15002 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15004 /* Consume the string. */
15005 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15006 /*wide_ok=*/true, /*lookup_udlit=*/false);
15007 if (str
== error_mark_node
)
15008 return error_mark_node
;
15009 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15011 string_tree
= USERDEF_LITERAL_VALUE (str
);
15012 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15017 /* Look for the suffix identifier. */
15018 token
= cp_lexer_peek_token (parser
->lexer
);
15019 if (token
->type
== CPP_NAME
)
15020 id
= cp_parser_identifier (parser
);
15021 else if (token
->type
== CPP_KEYWORD
)
15023 error ("unexpected keyword;"
15024 " remove space between quotes and suffix identifier");
15025 return error_mark_node
;
15029 error ("expected suffix identifier");
15030 return error_mark_node
;
15033 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15034 (TREE_TYPE (TREE_TYPE (string_tree
))));
15035 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15038 error ("expected empty string after %<operator%> keyword");
15039 return error_mark_node
;
15041 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15044 error ("invalid encoding prefix in literal operator");
15045 return error_mark_node
;
15047 if (id
!= error_mark_node
)
15049 const char *name
= IDENTIFIER_POINTER (id
);
15050 id
= cp_literal_operator_id (name
);
15056 /* Anything else is an error. */
15060 /* If we have selected an identifier, we need to consume the
15062 if (op
!= ERROR_MARK
)
15064 id
= ovl_op_identifier (assop
, op
);
15066 cp_lexer_consume_token (parser
->lexer
);
15068 /* Otherwise, no valid operator name was present. */
15071 cp_parser_error (parser
, "expected operator");
15072 id
= error_mark_node
;
15075 return cp_expr (id
, start_loc
);
15078 /* Parse a template-declaration.
15080 template-declaration:
15081 export [opt] template < template-parameter-list > declaration
15083 If MEMBER_P is TRUE, this template-declaration occurs within a
15086 The grammar rule given by the standard isn't correct. What
15087 is really meant is:
15089 template-declaration:
15090 export [opt] template-parameter-list-seq
15091 decl-specifier-seq [opt] init-declarator [opt] ;
15092 export [opt] template-parameter-list-seq
15093 function-definition
15095 template-parameter-list-seq:
15096 template-parameter-list-seq [opt]
15097 template < template-parameter-list >
15099 Concept Extensions:
15101 template-parameter-list-seq:
15102 template < template-parameter-list > requires-clause [opt]
15105 requires logical-or-expression */
15108 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15110 /* Check for `export'. */
15111 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15113 /* Consume the `export' token. */
15114 cp_lexer_consume_token (parser
->lexer
);
15115 /* Warn that we do not support `export'. */
15116 warning (0, "keyword %<export%> not implemented, and will be ignored");
15119 cp_parser_template_declaration_after_export (parser
, member_p
);
15122 /* Parse a template-parameter-list.
15124 template-parameter-list:
15126 template-parameter-list , template-parameter
15128 Returns a TREE_LIST. Each node represents a template parameter.
15129 The nodes are connected via their TREE_CHAINs. */
15132 cp_parser_template_parameter_list (cp_parser
* parser
)
15134 tree parameter_list
= NULL_TREE
;
15136 begin_template_parm_list ();
15138 /* The loop below parses the template parms. We first need to know
15139 the total number of template parms to be able to compute proper
15140 canonical types of each dependent type. So after the loop, when
15141 we know the total number of template parms,
15142 end_template_parm_list computes the proper canonical types and
15143 fixes up the dependent types accordingly. */
15148 bool is_parameter_pack
;
15149 location_t parm_loc
;
15151 /* Parse the template-parameter. */
15152 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15153 parameter
= cp_parser_template_parameter (parser
,
15155 &is_parameter_pack
);
15156 /* Add it to the list. */
15157 if (parameter
!= error_mark_node
)
15158 parameter_list
= process_template_parm (parameter_list
,
15162 is_parameter_pack
);
15165 tree err_parm
= build_tree_list (parameter
, parameter
);
15166 parameter_list
= chainon (parameter_list
, err_parm
);
15169 /* If the next token is not a `,', we're done. */
15170 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15172 /* Otherwise, consume the `,' token. */
15173 cp_lexer_consume_token (parser
->lexer
);
15176 return end_template_parm_list (parameter_list
);
15179 /* Parse a introduction-list.
15182 introduced-parameter
15183 introduction-list , introduced-parameter
15185 introduced-parameter:
15186 ...[opt] identifier
15188 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15189 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15190 WILDCARD_DECL will also have DECL_NAME set and token location in
15191 DECL_SOURCE_LOCATION. */
15194 cp_parser_introduction_list (cp_parser
*parser
)
15196 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15200 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15202 cp_lexer_consume_token (parser
->lexer
);
15204 /* Build placeholder. */
15205 tree parm
= build_nt (WILDCARD_DECL
);
15206 DECL_SOURCE_LOCATION (parm
)
15207 = cp_lexer_peek_token (parser
->lexer
)->location
;
15208 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15209 WILDCARD_PACK_P (parm
) = is_pack
;
15210 vec_safe_push (introduction_vec
, parm
);
15212 /* If the next token is not a `,', we're done. */
15213 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15215 /* Otherwise, consume the `,' token. */
15216 cp_lexer_consume_token (parser
->lexer
);
15219 /* Convert the vec into a TREE_VEC. */
15220 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15223 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15224 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15226 release_tree_vector (introduction_vec
);
15227 return introduction_list
;
15230 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15231 is an abstract declarator. */
15233 static inline cp_declarator
*
15234 get_id_declarator (cp_declarator
*declarator
)
15236 cp_declarator
*d
= declarator
;
15237 while (d
&& d
->kind
!= cdk_id
)
15242 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15243 is an abstract declarator. */
15246 get_unqualified_id (cp_declarator
*declarator
)
15248 declarator
= get_id_declarator (declarator
);
15250 return declarator
->u
.id
.unqualified_name
;
15255 /* Returns true if DECL represents a constrained-parameter. */
15258 is_constrained_parameter (tree decl
)
15261 && TREE_CODE (decl
) == TYPE_DECL
15262 && CONSTRAINED_PARM_CONCEPT (decl
)
15263 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15266 /* Returns true if PARM declares a constrained-parameter. */
15269 is_constrained_parameter (cp_parameter_declarator
*parm
)
15271 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15274 /* Check that the type parameter is only a declarator-id, and that its
15275 type is not cv-qualified. */
15278 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15279 cp_parameter_declarator
*parm
)
15281 if (!parm
->declarator
)
15284 if (parm
->declarator
->kind
!= cdk_id
)
15286 cp_parser_error (parser
, "invalid constrained type parameter");
15290 /* Don't allow cv-qualified type parameters. */
15291 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15292 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15294 cp_parser_error (parser
, "cv-qualified type parameter");
15301 /* Finish parsing/processing a template type parameter and checking
15302 various restrictions. */
15305 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15307 cp_parameter_declarator
* parmdecl
)
15309 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15310 return finish_template_type_parm (class_type_node
, id
);
15312 return error_mark_node
;
15316 finish_constrained_template_template_parm (tree proto
, tree id
)
15318 /* FIXME: This should probably be copied, and we may need to adjust
15319 the template parameter depths. */
15320 tree saved_parms
= current_template_parms
;
15321 begin_template_parm_list ();
15322 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15323 end_template_parm_list ();
15325 tree parm
= finish_template_template_parm (class_type_node
, id
);
15326 current_template_parms
= saved_parms
;
15331 /* Finish parsing/processing a template template parameter by borrowing
15332 the template parameter list from the prototype parameter. */
15335 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15338 cp_parameter_declarator
*parmdecl
)
15340 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15341 return error_mark_node
;
15342 return finish_constrained_template_template_parm (proto
, id
);
15345 /* Create a new non-type template parameter from the given PARM
15349 constrained_non_type_template_parm (bool *is_non_type
,
15350 cp_parameter_declarator
*parm
)
15352 *is_non_type
= true;
15353 cp_declarator
*decl
= parm
->declarator
;
15354 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15355 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15356 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15359 /* Build a constrained template parameter based on the PARMDECL
15360 declarator. The type of PARMDECL is the constrained type, which
15361 refers to the prototype template parameter that ultimately
15362 specifies the type of the declared parameter. */
15365 finish_constrained_parameter (cp_parser
*parser
,
15366 cp_parameter_declarator
*parmdecl
,
15368 bool *is_parameter_pack
)
15370 tree decl
= parmdecl
->decl_specifiers
.type
;
15371 tree id
= get_unqualified_id (parmdecl
->declarator
);
15372 tree def
= parmdecl
->default_argument
;
15373 tree proto
= DECL_INITIAL (decl
);
15375 /* A template parameter constrained by a variadic concept shall also
15376 be declared as a template parameter pack. */
15377 bool is_variadic
= template_parameter_pack_p (proto
);
15378 if (is_variadic
&& !*is_parameter_pack
)
15379 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15381 /* Build the parameter. Return an error if the declarator was invalid. */
15383 if (TREE_CODE (proto
) == TYPE_DECL
)
15384 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15385 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15386 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15389 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15390 if (parm
== error_mark_node
)
15391 return error_mark_node
;
15393 /* Finish the parameter decl and create a node attaching the
15394 default argument and constraint. */
15395 parm
= build_tree_list (def
, parm
);
15396 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15401 /* Returns true if the parsed type actually represents the declaration
15402 of a type template-parameter. */
15405 declares_constrained_type_template_parameter (tree type
)
15407 return (is_constrained_parameter (type
)
15408 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15412 /* Returns true if the parsed type actually represents the declaration of
15413 a template template-parameter. */
15416 declares_constrained_template_template_parameter (tree type
)
15418 return (is_constrained_parameter (type
)
15419 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15422 /* Parse a default argument for a type template-parameter.
15423 Note that diagnostics are handled in cp_parser_template_parameter. */
15426 cp_parser_default_type_template_argument (cp_parser
*parser
)
15428 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15430 /* Consume the `=' token. */
15431 cp_lexer_consume_token (parser
->lexer
);
15433 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15435 /* Parse the default-argument. */
15436 push_deferring_access_checks (dk_no_deferred
);
15437 tree default_argument
= cp_parser_type_id (parser
);
15438 pop_deferring_access_checks ();
15440 if (flag_concepts
&& type_uses_auto (default_argument
))
15442 error_at (token
->location
,
15443 "invalid use of %<auto%> in default template argument");
15444 return error_mark_node
;
15447 return default_argument
;
15450 /* Parse a default argument for a template template-parameter. */
15453 cp_parser_default_template_template_argument (cp_parser
*parser
)
15455 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15459 /* Consume the `='. */
15460 cp_lexer_consume_token (parser
->lexer
);
15461 /* Parse the id-expression. */
15462 push_deferring_access_checks (dk_no_deferred
);
15463 /* save token before parsing the id-expression, for error
15465 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15466 tree default_argument
15467 = cp_parser_id_expression (parser
,
15468 /*template_keyword_p=*/false,
15469 /*check_dependency_p=*/true,
15470 /*template_p=*/&is_template
,
15471 /*declarator_p=*/false,
15472 /*optional_p=*/false);
15473 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15474 /* If the id-expression was a template-id that refers to
15475 a template-class, we already have the declaration here,
15476 so no further lookup is needed. */
15479 /* Look up the name. */
15481 = cp_parser_lookup_name (parser
, default_argument
,
15483 /*is_template=*/is_template
,
15484 /*is_namespace=*/false,
15485 /*check_dependency=*/true,
15486 /*ambiguous_decls=*/NULL
,
15488 /* See if the default argument is valid. */
15489 default_argument
= check_template_template_default_arg (default_argument
);
15490 pop_deferring_access_checks ();
15491 return default_argument
;
15494 /* Parse a template-parameter.
15496 template-parameter:
15498 parameter-declaration
15500 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15501 the parameter. The TREE_PURPOSE is the default value, if any.
15502 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15503 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15504 set to true iff this parameter is a parameter pack. */
15507 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15508 bool *is_parameter_pack
)
15511 cp_parameter_declarator
*parameter_declarator
;
15514 /* Assume it is a type parameter or a template parameter. */
15515 *is_non_type
= false;
15516 /* Assume it not a parameter pack. */
15517 *is_parameter_pack
= false;
15518 /* Peek at the next token. */
15519 token
= cp_lexer_peek_token (parser
->lexer
);
15520 /* If it is `template', we have a type-parameter. */
15521 if (token
->keyword
== RID_TEMPLATE
)
15522 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15523 /* If it is `class' or `typename' we do not know yet whether it is a
15524 type parameter or a non-type parameter. Consider:
15526 template <typename T, typename T::X X> ...
15530 template <class C, class D*> ...
15532 Here, the first parameter is a type parameter, and the second is
15533 a non-type parameter. We can tell by looking at the token after
15534 the identifier -- if it is a `,', `=', or `>' then we have a type
15536 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15538 /* Peek at the token after `class' or `typename'. */
15539 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15540 /* If it's an ellipsis, we have a template type parameter
15542 if (token
->type
== CPP_ELLIPSIS
)
15543 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15544 /* If it's an identifier, skip it. */
15545 if (token
->type
== CPP_NAME
)
15546 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15547 /* Now, see if the token looks like the end of a template
15549 if (token
->type
== CPP_COMMA
15550 || token
->type
== CPP_EQ
15551 || token
->type
== CPP_GREATER
)
15552 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15555 /* Otherwise, it is a non-type parameter or a constrained parameter.
15559 When parsing a default template-argument for a non-type
15560 template-parameter, the first non-nested `>' is taken as the end
15561 of the template parameter-list rather than a greater-than
15563 parameter_declarator
15564 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15565 /*parenthesized_p=*/NULL
);
15567 if (!parameter_declarator
)
15568 return error_mark_node
;
15570 /* If the parameter declaration is marked as a parameter pack, set
15571 *IS_PARAMETER_PACK to notify the caller. */
15572 if (parameter_declarator
->template_parameter_pack_p
)
15573 *is_parameter_pack
= true;
15575 if (parameter_declarator
->default_argument
)
15577 /* Can happen in some cases of erroneous input (c++/34892). */
15578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15579 /* Consume the `...' for better error recovery. */
15580 cp_lexer_consume_token (parser
->lexer
);
15583 // The parameter may have been constrained.
15584 if (is_constrained_parameter (parameter_declarator
))
15585 return finish_constrained_parameter (parser
,
15586 parameter_declarator
,
15588 is_parameter_pack
);
15590 // Now we're sure that the parameter is a non-type parameter.
15591 *is_non_type
= true;
15593 parm
= grokdeclarator (parameter_declarator
->declarator
,
15594 ¶meter_declarator
->decl_specifiers
,
15595 TPARM
, /*initialized=*/0,
15596 /*attrlist=*/NULL
);
15597 if (parm
== error_mark_node
)
15598 return error_mark_node
;
15600 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15603 /* Parse a type-parameter.
15606 class identifier [opt]
15607 class identifier [opt] = type-id
15608 typename identifier [opt]
15609 typename identifier [opt] = type-id
15610 template < template-parameter-list > class identifier [opt]
15611 template < template-parameter-list > class identifier [opt]
15614 GNU Extension (variadic templates):
15617 class ... identifier [opt]
15618 typename ... identifier [opt]
15620 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15621 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15622 the declaration of the parameter.
15624 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15627 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15632 /* Look for a keyword to tell us what kind of parameter this is. */
15633 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15635 return error_mark_node
;
15637 switch (token
->keyword
)
15643 tree default_argument
;
15645 /* If the next token is an ellipsis, we have a template
15647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15649 /* Consume the `...' token. */
15650 cp_lexer_consume_token (parser
->lexer
);
15651 maybe_warn_variadic_templates ();
15653 *is_parameter_pack
= true;
15656 /* If the next token is an identifier, then it names the
15658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15659 identifier
= cp_parser_identifier (parser
);
15661 identifier
= NULL_TREE
;
15663 /* Create the parameter. */
15664 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15666 /* If the next token is an `=', we have a default argument. */
15667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15670 = cp_parser_default_type_template_argument (parser
);
15672 /* Template parameter packs cannot have default
15674 if (*is_parameter_pack
)
15677 error_at (token
->location
,
15678 "template parameter pack %qD cannot have a "
15679 "default argument", identifier
);
15681 error_at (token
->location
,
15682 "template parameter packs cannot have "
15683 "default arguments");
15684 default_argument
= NULL_TREE
;
15686 else if (check_for_bare_parameter_packs (default_argument
))
15687 default_argument
= error_mark_node
;
15690 default_argument
= NULL_TREE
;
15692 /* Create the combined representation of the parameter and the
15693 default argument. */
15694 parameter
= build_tree_list (default_argument
, parameter
);
15701 tree default_argument
;
15703 /* Look for the `<'. */
15704 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15705 /* Parse the template-parameter-list. */
15706 cp_parser_template_parameter_list (parser
);
15707 /* Look for the `>'. */
15708 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15710 // If template requirements are present, parse them.
15713 tree reqs
= get_shorthand_constraints (current_template_parms
);
15714 if (tree r
= cp_parser_requires_clause_opt (parser
))
15715 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15716 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15719 /* Look for the `class' or 'typename' keywords. */
15720 cp_parser_type_parameter_key (parser
);
15721 /* If the next token is an ellipsis, we have a template
15723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15725 /* Consume the `...' token. */
15726 cp_lexer_consume_token (parser
->lexer
);
15727 maybe_warn_variadic_templates ();
15729 *is_parameter_pack
= true;
15731 /* If the next token is an `=', then there is a
15732 default-argument. If the next token is a `>', we are at
15733 the end of the parameter-list. If the next token is a `,',
15734 then we are at the end of this parameter. */
15735 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15736 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15737 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15739 identifier
= cp_parser_identifier (parser
);
15740 /* Treat invalid names as if the parameter were nameless. */
15741 if (identifier
== error_mark_node
)
15742 identifier
= NULL_TREE
;
15745 identifier
= NULL_TREE
;
15747 /* Create the template parameter. */
15748 parameter
= finish_template_template_parm (class_type_node
,
15751 /* If the next token is an `=', then there is a
15752 default-argument. */
15753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15756 = cp_parser_default_template_template_argument (parser
);
15758 /* Template parameter packs cannot have default
15760 if (*is_parameter_pack
)
15763 error_at (token
->location
,
15764 "template parameter pack %qD cannot "
15765 "have a default argument",
15768 error_at (token
->location
, "template parameter packs cannot "
15769 "have default arguments");
15770 default_argument
= NULL_TREE
;
15774 default_argument
= NULL_TREE
;
15776 /* Create the combined representation of the parameter and the
15777 default argument. */
15778 parameter
= build_tree_list (default_argument
, parameter
);
15783 gcc_unreachable ();
15790 /* Parse a template-id.
15793 template-name < template-argument-list [opt] >
15795 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15796 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15797 returned. Otherwise, if the template-name names a function, or set
15798 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15799 names a class, returns a TYPE_DECL for the specialization.
15801 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15802 uninstantiated templates. */
15805 cp_parser_template_id (cp_parser
*parser
,
15806 bool template_keyword_p
,
15807 bool check_dependency_p
,
15808 enum tag_types tag_type
,
15809 bool is_declaration
)
15814 cp_token_position start_of_id
= 0;
15815 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15816 bool is_identifier
;
15818 /* If the next token corresponds to a template-id, there is no need
15820 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15821 if (token
->type
== CPP_TEMPLATE_ID
)
15823 cp_lexer_consume_token (parser
->lexer
);
15824 return saved_checks_value (token
->u
.tree_check_value
);
15827 /* Avoid performing name lookup if there is no possibility of
15828 finding a template-id. */
15829 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15830 || (token
->type
== CPP_NAME
15831 && !cp_parser_nth_token_starts_template_argument_list_p
15834 cp_parser_error (parser
, "expected template-id");
15835 return error_mark_node
;
15838 /* Remember where the template-id starts. */
15839 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15840 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15842 push_deferring_access_checks (dk_deferred
);
15844 /* Parse the template-name. */
15845 is_identifier
= false;
15846 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15847 check_dependency_p
,
15851 if (templ
== error_mark_node
|| is_identifier
)
15853 pop_deferring_access_checks ();
15857 /* Since we're going to preserve any side-effects from this parse, set up a
15858 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15859 in the template arguments. */
15860 tentative_firewall
firewall (parser
);
15862 /* If we find the sequence `[:' after a template-name, it's probably
15863 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15864 parse correctly the argument list. */
15865 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15866 == CPP_OPEN_SQUARE
)
15867 && next_token
->flags
& DIGRAPH
15868 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15870 && !(next_token_2
->flags
& PREV_WHITE
))
15872 cp_parser_parse_tentatively (parser
);
15873 /* Change `:' into `::'. */
15874 next_token_2
->type
= CPP_SCOPE
;
15875 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15877 cp_lexer_consume_token (parser
->lexer
);
15879 /* Parse the arguments. */
15880 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15881 if (!cp_parser_parse_definitely (parser
))
15883 /* If we couldn't parse an argument list, then we revert our changes
15884 and return simply an error. Maybe this is not a template-id
15886 next_token_2
->type
= CPP_COLON
;
15887 cp_parser_error (parser
, "expected %<<%>");
15888 pop_deferring_access_checks ();
15889 return error_mark_node
;
15891 /* Otherwise, emit an error about the invalid digraph, but continue
15892 parsing because we got our argument list. */
15893 if (permerror (next_token
->location
,
15894 "%<<::%> cannot begin a template-argument list"))
15896 static bool hint
= false;
15897 inform (next_token
->location
,
15898 "%<<:%> is an alternate spelling for %<[%>."
15899 " Insert whitespace between %<<%> and %<::%>");
15900 if (!hint
&& !flag_permissive
)
15902 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15903 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15904 "accept your code)");
15911 /* Look for the `<' that starts the template-argument-list. */
15912 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15914 pop_deferring_access_checks ();
15915 return error_mark_node
;
15917 /* Parse the arguments. */
15918 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15921 /* Set the location to be of the form:
15922 template-name < template-argument-list [opt] >
15923 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15924 with caret == start at the start of the template-name,
15925 ranging until the closing '>'. */
15926 location_t finish_loc
15927 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15928 location_t combined_loc
15929 = make_location (token
->location
, token
->location
, finish_loc
);
15931 /* Check for concepts autos where they don't belong. We could
15932 identify types in some cases of idnetifier TEMPL, looking ahead
15933 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15934 types. We reject them in functions, but if what we have is an
15935 identifier, even with none_type we can't conclude it's NOT a
15936 type, we have to wait for template substitution. */
15937 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15938 template_id
= error_mark_node
;
15939 /* Build a representation of the specialization. */
15940 else if (identifier_p (templ
))
15941 template_id
= build_min_nt_loc (combined_loc
,
15944 else if (DECL_TYPE_TEMPLATE_P (templ
)
15945 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15947 bool entering_scope
;
15948 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15949 template (rather than some instantiation thereof) only if
15950 is not nested within some other construct. For example, in
15951 "template <typename T> void f(T) { A<T>::", A<T> is just an
15952 instantiation of A. */
15953 entering_scope
= (template_parm_scope_p ()
15954 && cp_lexer_next_token_is (parser
->lexer
,
15957 = finish_template_type (templ
, arguments
, entering_scope
);
15959 /* A template-like identifier may be a partial concept id. */
15960 else if (flag_concepts
15961 && (template_id
= (cp_parser_maybe_partial_concept_id
15962 (parser
, templ
, arguments
))))
15963 return template_id
;
15964 else if (variable_template_p (templ
))
15966 template_id
= lookup_template_variable (templ
, arguments
);
15967 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15968 SET_EXPR_LOCATION (template_id
, combined_loc
);
15972 /* If it's not a class-template or a template-template, it should be
15973 a function-template. */
15974 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15975 || TREE_CODE (templ
) == OVERLOAD
15976 || BASELINK_P (templ
)));
15978 template_id
= lookup_template_function (templ
, arguments
);
15979 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15980 SET_EXPR_LOCATION (template_id
, combined_loc
);
15983 /* If parsing tentatively, replace the sequence of tokens that makes
15984 up the template-id with a CPP_TEMPLATE_ID token. That way,
15985 should we re-parse the token stream, we will not have to repeat
15986 the effort required to do the parse, nor will we issue duplicate
15987 error messages about problems during instantiation of the
15990 /* Don't do this if we had a parse error in a declarator; re-parsing
15991 might succeed if a name changes meaning (60361). */
15992 && !(cp_parser_error_occurred (parser
)
15993 && cp_parser_parsing_tentatively (parser
)
15994 && parser
->in_declarator_p
))
15996 /* Reset the contents of the START_OF_ID token. */
15997 token
->type
= CPP_TEMPLATE_ID
;
15998 token
->location
= combined_loc
;
16000 /* Retrieve any deferred checks. Do not pop this access checks yet
16001 so the memory will not be reclaimed during token replacing below. */
16002 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16003 token
->u
.tree_check_value
->value
= template_id
;
16004 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16005 token
->keyword
= RID_MAX
;
16007 /* Purge all subsequent tokens. */
16008 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16010 /* ??? Can we actually assume that, if template_id ==
16011 error_mark_node, we will have issued a diagnostic to the
16012 user, as opposed to simply marking the tentative parse as
16014 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16015 error_at (token
->location
, "parse error in template argument list");
16018 pop_to_parent_deferring_access_checks ();
16019 return template_id
;
16022 /* Parse a template-name.
16027 The standard should actually say:
16031 operator-function-id
16033 A defect report has been filed about this issue.
16035 A conversion-function-id cannot be a template name because they cannot
16036 be part of a template-id. In fact, looking at this code:
16038 a.operator K<int>()
16040 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16041 It is impossible to call a templated conversion-function-id with an
16042 explicit argument list, since the only allowed template parameter is
16043 the type to which it is converting.
16045 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16046 `template' keyword, in a construction like:
16050 In that case `f' is taken to be a template-name, even though there
16051 is no way of knowing for sure.
16053 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16054 name refers to a set of overloaded functions, at least one of which
16055 is a template, or an IDENTIFIER_NODE with the name of the template,
16056 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16057 names are looked up inside uninstantiated templates. */
16060 cp_parser_template_name (cp_parser
* parser
,
16061 bool template_keyword_p
,
16062 bool check_dependency_p
,
16063 bool is_declaration
,
16064 enum tag_types tag_type
,
16065 bool *is_identifier
)
16069 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16071 /* If the next token is `operator', then we have either an
16072 operator-function-id or a conversion-function-id. */
16073 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16075 /* We don't know whether we're looking at an
16076 operator-function-id or a conversion-function-id. */
16077 cp_parser_parse_tentatively (parser
);
16078 /* Try an operator-function-id. */
16079 identifier
= cp_parser_operator_function_id (parser
);
16080 /* If that didn't work, try a conversion-function-id. */
16081 if (!cp_parser_parse_definitely (parser
))
16083 cp_parser_error (parser
, "expected template-name");
16084 return error_mark_node
;
16087 /* Look for the identifier. */
16089 identifier
= cp_parser_identifier (parser
);
16091 /* If we didn't find an identifier, we don't have a template-id. */
16092 if (identifier
== error_mark_node
)
16093 return error_mark_node
;
16095 /* If the name immediately followed the `template' keyword, then it
16096 is a template-name. However, if the next token is not `<', then
16097 we do not treat it as a template-name, since it is not being used
16098 as part of a template-id. This enables us to handle constructs
16101 template <typename T> struct S { S(); };
16102 template <typename T> S<T>::S();
16104 correctly. We would treat `S' as a template -- if it were `S<T>'
16105 -- but we do not if there is no `<'. */
16107 if (processing_template_decl
16108 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16110 /* In a declaration, in a dependent context, we pretend that the
16111 "template" keyword was present in order to improve error
16112 recovery. For example, given:
16114 template <typename T> void f(T::X<int>);
16116 we want to treat "X<int>" as a template-id. */
16118 && !template_keyword_p
16119 && parser
->scope
&& TYPE_P (parser
->scope
)
16120 && check_dependency_p
16121 && dependent_scope_p (parser
->scope
)
16122 /* Do not do this for dtors (or ctors), since they never
16123 need the template keyword before their name. */
16124 && !constructor_name_p (identifier
, parser
->scope
))
16126 cp_token_position start
= 0;
16128 /* Explain what went wrong. */
16129 error_at (token
->location
, "non-template %qD used as template",
16131 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16132 parser
->scope
, identifier
);
16133 /* If parsing tentatively, find the location of the "<" token. */
16134 if (cp_parser_simulate_error (parser
))
16135 start
= cp_lexer_token_position (parser
->lexer
, true);
16136 /* Parse the template arguments so that we can issue error
16137 messages about them. */
16138 cp_lexer_consume_token (parser
->lexer
);
16139 cp_parser_enclosed_template_argument_list (parser
);
16140 /* Skip tokens until we find a good place from which to
16141 continue parsing. */
16142 cp_parser_skip_to_closing_parenthesis (parser
,
16143 /*recovering=*/true,
16145 /*consume_paren=*/false);
16146 /* If parsing tentatively, permanently remove the
16147 template argument list. That will prevent duplicate
16148 error messages from being issued about the missing
16149 "template" keyword. */
16151 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16153 *is_identifier
= true;
16154 parser
->context
->object_type
= NULL_TREE
;
16158 /* If the "template" keyword is present, then there is generally
16159 no point in doing name-lookup, so we just return IDENTIFIER.
16160 But, if the qualifying scope is non-dependent then we can
16161 (and must) do name-lookup normally. */
16162 if (template_keyword_p
)
16164 tree scope
= (parser
->scope
? parser
->scope
16165 : parser
->context
->object_type
);
16166 if (scope
&& TYPE_P (scope
)
16167 && (!CLASS_TYPE_P (scope
)
16168 || (check_dependency_p
&& dependent_type_p (scope
))))
16170 /* We're optimizing away the call to cp_parser_lookup_name, but
16171 we still need to do this. */
16172 parser
->context
->object_type
= NULL_TREE
;
16178 /* Look up the name. */
16179 decl
= cp_parser_lookup_name (parser
, identifier
,
16181 /*is_template=*/true,
16182 /*is_namespace=*/false,
16183 check_dependency_p
,
16184 /*ambiguous_decls=*/NULL
,
16187 decl
= strip_using_decl (decl
);
16189 /* If DECL is a template, then the name was a template-name. */
16190 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16192 if (TREE_DEPRECATED (decl
)
16193 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16194 warn_deprecated_use (decl
, NULL_TREE
);
16198 /* The standard does not explicitly indicate whether a name that
16199 names a set of overloaded declarations, some of which are
16200 templates, is a template-name. However, such a name should
16201 be a template-name; otherwise, there is no way to form a
16202 template-id for the overloaded templates. */
16203 bool found
= false;
16205 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16206 !found
&& iter
; ++iter
)
16207 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16212 /* The name does not name a template. */
16213 cp_parser_error (parser
, "expected template-name");
16214 return error_mark_node
;
16218 /* If DECL is dependent, and refers to a function, then just return
16219 its name; we will look it up again during template instantiation. */
16220 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16222 tree scope
= ovl_scope (decl
);
16223 if (TYPE_P (scope
) && dependent_type_p (scope
))
16230 /* Parse a template-argument-list.
16232 template-argument-list:
16233 template-argument ... [opt]
16234 template-argument-list , template-argument ... [opt]
16236 Returns a TREE_VEC containing the arguments. */
16239 cp_parser_template_argument_list (cp_parser
* parser
)
16241 tree fixed_args
[10];
16242 unsigned n_args
= 0;
16243 unsigned alloced
= 10;
16244 tree
*arg_ary
= fixed_args
;
16246 bool saved_in_template_argument_list_p
;
16248 bool saved_non_ice_p
;
16250 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16251 parser
->in_template_argument_list_p
= true;
16252 /* Even if the template-id appears in an integral
16253 constant-expression, the contents of the argument list do
16255 saved_ice_p
= parser
->integral_constant_expression_p
;
16256 parser
->integral_constant_expression_p
= false;
16257 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16258 parser
->non_integral_constant_expression_p
= false;
16260 /* Parse the arguments. */
16266 /* Consume the comma. */
16267 cp_lexer_consume_token (parser
->lexer
);
16269 /* Parse the template-argument. */
16270 argument
= cp_parser_template_argument (parser
);
16272 /* If the next token is an ellipsis, we're expanding a template
16274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16276 if (argument
== error_mark_node
)
16278 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16279 error_at (token
->location
,
16280 "expected parameter pack before %<...%>");
16282 /* Consume the `...' token. */
16283 cp_lexer_consume_token (parser
->lexer
);
16285 /* Make the argument into a TYPE_PACK_EXPANSION or
16286 EXPR_PACK_EXPANSION. */
16287 argument
= make_pack_expansion (argument
);
16290 if (n_args
== alloced
)
16294 if (arg_ary
== fixed_args
)
16296 arg_ary
= XNEWVEC (tree
, alloced
);
16297 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16300 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16302 arg_ary
[n_args
++] = argument
;
16304 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16306 vec
= make_tree_vec (n_args
);
16309 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16311 if (arg_ary
!= fixed_args
)
16313 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16314 parser
->integral_constant_expression_p
= saved_ice_p
;
16315 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16317 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16321 /* Parse a template-argument.
16324 assignment-expression
16328 The representation is that of an assignment-expression, type-id, or
16329 id-expression -- except that the qualified id-expression is
16330 evaluated, so that the value returned is either a DECL or an
16333 Although the standard says "assignment-expression", it forbids
16334 throw-expressions or assignments in the template argument.
16335 Therefore, we use "conditional-expression" instead. */
16338 cp_parser_template_argument (cp_parser
* parser
)
16343 bool maybe_type_id
= false;
16344 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16345 location_t loc
= 0;
16348 /* There's really no way to know what we're looking at, so we just
16349 try each alternative in order.
16353 In a template-argument, an ambiguity between a type-id and an
16354 expression is resolved to a type-id, regardless of the form of
16355 the corresponding template-parameter.
16357 Therefore, we try a type-id first. */
16358 cp_parser_parse_tentatively (parser
);
16359 argument
= cp_parser_template_type_arg (parser
);
16360 /* If there was no error parsing the type-id but the next token is a
16361 '>>', our behavior depends on which dialect of C++ we're
16362 parsing. In C++98, we probably found a typo for '> >'. But there
16363 are type-id which are also valid expressions. For instance:
16365 struct X { int operator >> (int); };
16366 template <int V> struct Foo {};
16369 Here 'X()' is a valid type-id of a function type, but the user just
16370 wanted to write the expression "X() >> 5". Thus, we remember that we
16371 found a valid type-id, but we still try to parse the argument as an
16372 expression to see what happens.
16374 In C++0x, the '>>' will be considered two separate '>'
16376 if (!cp_parser_error_occurred (parser
)
16377 && cxx_dialect
== cxx98
16378 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16380 maybe_type_id
= true;
16381 cp_parser_abort_tentative_parse (parser
);
16385 /* If the next token isn't a `,' or a `>', then this argument wasn't
16386 really finished. This means that the argument is not a valid
16388 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16389 cp_parser_error (parser
, "expected template-argument");
16390 /* If that worked, we're done. */
16391 if (cp_parser_parse_definitely (parser
))
16394 /* We're still not sure what the argument will be. */
16395 cp_parser_parse_tentatively (parser
);
16396 /* Try a template. */
16397 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16398 argument
= cp_parser_id_expression (parser
,
16399 /*template_keyword_p=*/false,
16400 /*check_dependency_p=*/true,
16402 /*declarator_p=*/false,
16403 /*optional_p=*/false);
16404 /* If the next token isn't a `,' or a `>', then this argument wasn't
16405 really finished. */
16406 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16407 cp_parser_error (parser
, "expected template-argument");
16408 if (!cp_parser_error_occurred (parser
))
16410 /* Figure out what is being referred to. If the id-expression
16411 was for a class template specialization, then we will have a
16412 TYPE_DECL at this point. There is no need to do name lookup
16413 at this point in that case. */
16414 if (TREE_CODE (argument
) != TYPE_DECL
)
16415 argument
= cp_parser_lookup_name (parser
, argument
,
16417 /*is_template=*/template_p
,
16418 /*is_namespace=*/false,
16419 /*check_dependency=*/true,
16420 /*ambiguous_decls=*/NULL
,
16421 argument_start_token
->location
);
16422 /* Handle a constrained-type-specifier for a non-type template
16424 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16426 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16427 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16428 cp_parser_error (parser
, "expected template-name");
16430 if (cp_parser_parse_definitely (parser
))
16432 if (TREE_DEPRECATED (argument
))
16433 warn_deprecated_use (argument
, NULL_TREE
);
16436 /* It must be a non-type argument. In C++17 any constant-expression is
16438 if (cxx_dialect
> cxx14
)
16441 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16443 -- an integral constant-expression of integral or enumeration
16446 -- the name of a non-type template-parameter; or
16448 -- the name of an object or function with external linkage...
16450 -- the address of an object or function with external linkage...
16452 -- a pointer to member... */
16453 /* Look for a non-type template parameter. */
16454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16456 cp_parser_parse_tentatively (parser
);
16457 argument
= cp_parser_primary_expression (parser
,
16458 /*address_p=*/false,
16460 /*template_arg_p=*/true,
16462 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16463 || !cp_parser_next_token_ends_template_argument_p (parser
))
16464 cp_parser_simulate_error (parser
);
16465 if (cp_parser_parse_definitely (parser
))
16469 /* If the next token is "&", the argument must be the address of an
16470 object or function with external linkage. */
16471 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16474 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16475 cp_lexer_consume_token (parser
->lexer
);
16477 /* See if we might have an id-expression. */
16478 token
= cp_lexer_peek_token (parser
->lexer
);
16479 if (token
->type
== CPP_NAME
16480 || token
->keyword
== RID_OPERATOR
16481 || token
->type
== CPP_SCOPE
16482 || token
->type
== CPP_TEMPLATE_ID
16483 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16485 cp_parser_parse_tentatively (parser
);
16486 argument
= cp_parser_primary_expression (parser
,
16489 /*template_arg_p=*/true,
16491 if (cp_parser_error_occurred (parser
)
16492 || !cp_parser_next_token_ends_template_argument_p (parser
))
16493 cp_parser_abort_tentative_parse (parser
);
16498 if (INDIRECT_REF_P (argument
))
16500 /* Strip the dereference temporarily. */
16501 gcc_assert (REFERENCE_REF_P (argument
));
16502 argument
= TREE_OPERAND (argument
, 0);
16505 /* If we're in a template, we represent a qualified-id referring
16506 to a static data member as a SCOPE_REF even if the scope isn't
16507 dependent so that we can check access control later. */
16509 if (TREE_CODE (probe
) == SCOPE_REF
)
16510 probe
= TREE_OPERAND (probe
, 1);
16513 /* A variable without external linkage might still be a
16514 valid constant-expression, so no error is issued here
16515 if the external-linkage check fails. */
16516 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16517 cp_parser_simulate_error (parser
);
16519 else if (is_overloaded_fn (argument
))
16520 /* All overloaded functions are allowed; if the external
16521 linkage test does not pass, an error will be issued
16525 && (TREE_CODE (argument
) == OFFSET_REF
16526 || TREE_CODE (argument
) == SCOPE_REF
))
16527 /* A pointer-to-member. */
16529 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16532 cp_parser_simulate_error (parser
);
16534 if (cp_parser_parse_definitely (parser
))
16537 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16538 tf_warning_or_error
);
16540 argument
= convert_from_reference (argument
);
16545 /* If the argument started with "&", there are no other valid
16546 alternatives at this point. */
16549 cp_parser_error (parser
, "invalid non-type template argument");
16550 return error_mark_node
;
16554 /* If the argument wasn't successfully parsed as a type-id followed
16555 by '>>', the argument can only be a constant expression now.
16556 Otherwise, we try parsing the constant-expression tentatively,
16557 because the argument could really be a type-id. */
16559 cp_parser_parse_tentatively (parser
);
16561 if (cxx_dialect
<= cxx14
)
16562 argument
= cp_parser_constant_expression (parser
);
16565 /* With C++17 generalized non-type template arguments we need to handle
16566 lvalue constant expressions, too. */
16567 argument
= cp_parser_assignment_expression (parser
);
16568 require_potential_constant_expression (argument
);
16571 if (!maybe_type_id
)
16573 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16574 cp_parser_error (parser
, "expected template-argument");
16575 if (cp_parser_parse_definitely (parser
))
16577 /* We did our best to parse the argument as a non type-id, but that
16578 was the only alternative that matched (albeit with a '>' after
16579 it). We can assume it's just a typo from the user, and a
16580 diagnostic will then be issued. */
16581 return cp_parser_template_type_arg (parser
);
16584 /* Parse an explicit-instantiation.
16586 explicit-instantiation:
16587 template declaration
16589 Although the standard says `declaration', what it really means is:
16591 explicit-instantiation:
16592 template decl-specifier-seq [opt] declarator [opt] ;
16594 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16595 supposed to be allowed. A defect report has been filed about this
16600 explicit-instantiation:
16601 storage-class-specifier template
16602 decl-specifier-seq [opt] declarator [opt] ;
16603 function-specifier template
16604 decl-specifier-seq [opt] declarator [opt] ; */
16607 cp_parser_explicit_instantiation (cp_parser
* parser
)
16609 int declares_class_or_enum
;
16610 cp_decl_specifier_seq decl_specifiers
;
16611 tree extension_specifier
= NULL_TREE
;
16613 timevar_push (TV_TEMPLATE_INST
);
16615 /* Look for an (optional) storage-class-specifier or
16616 function-specifier. */
16617 if (cp_parser_allow_gnu_extensions_p (parser
))
16619 extension_specifier
16620 = cp_parser_storage_class_specifier_opt (parser
);
16621 if (!extension_specifier
)
16622 extension_specifier
16623 = cp_parser_function_specifier_opt (parser
,
16624 /*decl_specs=*/NULL
);
16627 /* Look for the `template' keyword. */
16628 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16629 /* Let the front end know that we are processing an explicit
16631 begin_explicit_instantiation ();
16632 /* [temp.explicit] says that we are supposed to ignore access
16633 control while processing explicit instantiation directives. */
16634 push_deferring_access_checks (dk_no_check
);
16635 /* Parse a decl-specifier-seq. */
16636 cp_parser_decl_specifier_seq (parser
,
16637 CP_PARSER_FLAGS_OPTIONAL
,
16639 &declares_class_or_enum
);
16640 /* If there was exactly one decl-specifier, and it declared a class,
16641 and there's no declarator, then we have an explicit type
16643 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16647 type
= check_tag_decl (&decl_specifiers
,
16648 /*explicit_type_instantiation_p=*/true);
16649 /* Turn access control back on for names used during
16650 template instantiation. */
16651 pop_deferring_access_checks ();
16653 do_type_instantiation (type
, extension_specifier
,
16654 /*complain=*/tf_error
);
16658 cp_declarator
*declarator
;
16661 /* Parse the declarator. */
16663 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16664 /*ctor_dtor_or_conv_p=*/NULL
,
16665 /*parenthesized_p=*/NULL
,
16666 /*member_p=*/false,
16667 /*friend_p=*/false);
16668 if (declares_class_or_enum
& 2)
16669 cp_parser_check_for_definition_in_return_type (declarator
,
16670 decl_specifiers
.type
,
16671 decl_specifiers
.locations
[ds_type_spec
]);
16672 if (declarator
!= cp_error_declarator
)
16674 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16675 permerror (decl_specifiers
.locations
[ds_inline
],
16676 "explicit instantiation shall not use"
16677 " %<inline%> specifier");
16678 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16679 permerror (decl_specifiers
.locations
[ds_constexpr
],
16680 "explicit instantiation shall not use"
16681 " %<constexpr%> specifier");
16683 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16684 NORMAL
, 0, &decl_specifiers
.attributes
);
16685 /* Turn access control back on for names used during
16686 template instantiation. */
16687 pop_deferring_access_checks ();
16688 /* Do the explicit instantiation. */
16689 do_decl_instantiation (decl
, extension_specifier
);
16693 pop_deferring_access_checks ();
16694 /* Skip the body of the explicit instantiation. */
16695 cp_parser_skip_to_end_of_statement (parser
);
16698 /* We're done with the instantiation. */
16699 end_explicit_instantiation ();
16701 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16703 timevar_pop (TV_TEMPLATE_INST
);
16706 /* Parse an explicit-specialization.
16708 explicit-specialization:
16709 template < > declaration
16711 Although the standard says `declaration', what it really means is:
16713 explicit-specialization:
16714 template <> decl-specifier [opt] init-declarator [opt] ;
16715 template <> function-definition
16716 template <> explicit-specialization
16717 template <> template-declaration */
16720 cp_parser_explicit_specialization (cp_parser
* parser
)
16722 bool need_lang_pop
;
16723 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16725 /* Look for the `template' keyword. */
16726 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16727 /* Look for the `<'. */
16728 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16729 /* Look for the `>'. */
16730 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16731 /* We have processed another parameter list. */
16732 ++parser
->num_template_parameter_lists
;
16735 A template ... explicit specialization ... shall not have C
16737 if (current_lang_name
== lang_name_c
)
16739 error_at (token
->location
, "template specialization with C linkage");
16740 maybe_show_extern_c_location ();
16741 /* Give it C++ linkage to avoid confusing other parts of the
16743 push_lang_context (lang_name_cplusplus
);
16744 need_lang_pop
= true;
16747 need_lang_pop
= false;
16748 /* Let the front end know that we are beginning a specialization. */
16749 if (!begin_specialization ())
16751 end_specialization ();
16755 /* If the next keyword is `template', we need to figure out whether
16756 or not we're looking a template-declaration. */
16757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16759 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16760 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16761 cp_parser_template_declaration_after_export (parser
,
16762 /*member_p=*/false);
16764 cp_parser_explicit_specialization (parser
);
16767 /* Parse the dependent declaration. */
16768 cp_parser_single_declaration (parser
,
16770 /*member_p=*/false,
16771 /*explicit_specialization_p=*/true,
16772 /*friend_p=*/NULL
);
16773 /* We're done with the specialization. */
16774 end_specialization ();
16775 /* For the erroneous case of a template with C linkage, we pushed an
16776 implicit C++ linkage scope; exit that scope now. */
16778 pop_lang_context ();
16779 /* We're done with this parameter list. */
16780 --parser
->num_template_parameter_lists
;
16783 /* Parse a type-specifier.
16786 simple-type-specifier
16789 elaborated-type-specifier
16797 Returns a representation of the type-specifier. For a
16798 class-specifier, enum-specifier, or elaborated-type-specifier, a
16799 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16801 The parser flags FLAGS is used to control type-specifier parsing.
16803 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16804 in a decl-specifier-seq.
16806 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16807 class-specifier, enum-specifier, or elaborated-type-specifier, then
16808 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16809 if a type is declared; 2 if it is defined. Otherwise, it is set to
16812 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16813 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16814 is set to FALSE. */
16817 cp_parser_type_specifier (cp_parser
* parser
,
16818 cp_parser_flags flags
,
16819 cp_decl_specifier_seq
*decl_specs
,
16820 bool is_declaration
,
16821 int* declares_class_or_enum
,
16822 bool* is_cv_qualifier
)
16824 tree type_spec
= NULL_TREE
;
16827 cp_decl_spec ds
= ds_last
;
16829 /* Assume this type-specifier does not declare a new type. */
16830 if (declares_class_or_enum
)
16831 *declares_class_or_enum
= 0;
16832 /* And that it does not specify a cv-qualifier. */
16833 if (is_cv_qualifier
)
16834 *is_cv_qualifier
= false;
16835 /* Peek at the next token. */
16836 token
= cp_lexer_peek_token (parser
->lexer
);
16838 /* If we're looking at a keyword, we can use that to guide the
16839 production we choose. */
16840 keyword
= token
->keyword
;
16844 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16845 goto elaborated_type_specifier
;
16847 /* Look for the enum-specifier. */
16848 type_spec
= cp_parser_enum_specifier (parser
);
16849 /* If that worked, we're done. */
16852 if (declares_class_or_enum
)
16853 *declares_class_or_enum
= 2;
16855 cp_parser_set_decl_spec_type (decl_specs
,
16858 /*type_definition_p=*/true);
16862 goto elaborated_type_specifier
;
16864 /* Any of these indicate either a class-specifier, or an
16865 elaborated-type-specifier. */
16869 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16870 goto elaborated_type_specifier
;
16872 /* Parse tentatively so that we can back up if we don't find a
16873 class-specifier. */
16874 cp_parser_parse_tentatively (parser
);
16875 /* Look for the class-specifier. */
16876 type_spec
= cp_parser_class_specifier (parser
);
16877 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16878 /* If that worked, we're done. */
16879 if (cp_parser_parse_definitely (parser
))
16881 if (declares_class_or_enum
)
16882 *declares_class_or_enum
= 2;
16884 cp_parser_set_decl_spec_type (decl_specs
,
16887 /*type_definition_p=*/true);
16891 /* Fall through. */
16892 elaborated_type_specifier
:
16893 /* We're declaring (not defining) a class or enum. */
16894 if (declares_class_or_enum
)
16895 *declares_class_or_enum
= 1;
16897 /* Fall through. */
16899 /* Look for an elaborated-type-specifier. */
16901 = (cp_parser_elaborated_type_specifier
16903 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16906 cp_parser_set_decl_spec_type (decl_specs
,
16909 /*type_definition_p=*/false);
16914 if (is_cv_qualifier
)
16915 *is_cv_qualifier
= true;
16920 if (is_cv_qualifier
)
16921 *is_cv_qualifier
= true;
16926 if (is_cv_qualifier
)
16927 *is_cv_qualifier
= true;
16931 /* The `__complex__' keyword is a GNU extension. */
16939 /* Handle simple keywords. */
16944 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16945 decl_specs
->any_specifiers_p
= true;
16947 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16950 /* If we do not already have a type-specifier, assume we are looking
16951 at a simple-type-specifier. */
16952 type_spec
= cp_parser_simple_type_specifier (parser
,
16956 /* If we didn't find a type-specifier, and a type-specifier was not
16957 optional in this context, issue an error message. */
16958 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16960 cp_parser_error (parser
, "expected type specifier");
16961 return error_mark_node
;
16967 /* Parse a simple-type-specifier.
16969 simple-type-specifier:
16970 :: [opt] nested-name-specifier [opt] type-name
16971 :: [opt] nested-name-specifier template template-id
16986 simple-type-specifier:
16988 decltype ( expression )
16991 __underlying_type ( type-id )
16995 nested-name-specifier(opt) template-name
16999 simple-type-specifier:
17001 __typeof__ unary-expression
17002 __typeof__ ( type-id )
17003 __typeof__ ( type-id ) { initializer-list , [opt] }
17005 Concepts Extension:
17007 simple-type-specifier:
17008 constrained-type-specifier
17010 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17011 appropriately updated. */
17014 cp_parser_simple_type_specifier (cp_parser
* parser
,
17015 cp_decl_specifier_seq
*decl_specs
,
17016 cp_parser_flags flags
)
17018 tree type
= NULL_TREE
;
17022 /* Peek at the next token. */
17023 token
= cp_lexer_peek_token (parser
->lexer
);
17025 /* If we're looking at a keyword, things are easy. */
17026 switch (token
->keyword
)
17030 decl_specs
->explicit_char_p
= true;
17031 type
= char_type_node
;
17034 type
= char16_type_node
;
17037 type
= char32_type_node
;
17040 type
= wchar_type_node
;
17043 type
= boolean_type_node
;
17046 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17047 type
= short_integer_type_node
;
17051 decl_specs
->explicit_int_p
= true;
17052 type
= integer_type_node
;
17058 idx
= token
->keyword
- RID_INT_N_0
;
17059 if (! int_n_enabled_p
[idx
])
17063 decl_specs
->explicit_intN_p
= true;
17064 decl_specs
->int_n_idx
= idx
;
17066 type
= int_n_trees
[idx
].signed_type
;
17070 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17071 type
= long_integer_type_node
;
17074 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17075 type
= integer_type_node
;
17078 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17079 type
= unsigned_type_node
;
17082 type
= float_type_node
;
17085 type
= double_type_node
;
17088 type
= void_type_node
;
17092 maybe_warn_cpp0x (CPP0X_AUTO
);
17093 if (parser
->auto_is_implicit_function_template_parm_p
)
17095 /* The 'auto' might be the placeholder return type for a function decl
17096 with trailing return type. */
17097 bool have_trailing_return_fn_decl
= false;
17099 cp_parser_parse_tentatively (parser
);
17100 cp_lexer_consume_token (parser
->lexer
);
17101 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17102 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17103 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17104 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17108 cp_lexer_consume_token (parser
->lexer
);
17109 cp_parser_skip_to_closing_parenthesis (parser
,
17110 /*recovering*/false,
17112 /*consume_paren*/true);
17116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17118 have_trailing_return_fn_decl
= true;
17122 cp_lexer_consume_token (parser
->lexer
);
17124 cp_parser_abort_tentative_parse (parser
);
17126 if (have_trailing_return_fn_decl
)
17128 type
= make_auto ();
17132 if (cxx_dialect
>= cxx14
)
17134 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17135 type
= TREE_TYPE (type
);
17138 type
= error_mark_node
;
17140 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17142 if (cxx_dialect
< cxx14
)
17143 error_at (token
->location
,
17144 "use of %<auto%> in lambda parameter declaration "
17145 "only available with "
17146 "-std=c++14 or -std=gnu++14");
17148 else if (cxx_dialect
< cxx14
)
17149 error_at (token
->location
,
17150 "use of %<auto%> in parameter declaration "
17151 "only available with "
17152 "-std=c++14 or -std=gnu++14");
17153 else if (!flag_concepts
)
17154 pedwarn (token
->location
, 0,
17155 "use of %<auto%> in parameter declaration "
17156 "only available with -fconcepts");
17159 type
= make_auto ();
17163 /* Since DR 743, decltype can either be a simple-type-specifier by
17164 itself or begin a nested-name-specifier. Parsing it will replace
17165 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17166 handling below decide what to do. */
17167 cp_parser_decltype (parser
);
17168 cp_lexer_set_token_position (parser
->lexer
, token
);
17172 /* Consume the `typeof' token. */
17173 cp_lexer_consume_token (parser
->lexer
);
17174 /* Parse the operand to `typeof'. */
17175 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17176 /* If it is not already a TYPE, take its type. */
17177 if (!TYPE_P (type
))
17178 type
= finish_typeof (type
);
17181 cp_parser_set_decl_spec_type (decl_specs
, type
,
17183 /*type_definition_p=*/false);
17187 case RID_UNDERLYING_TYPE
:
17188 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17190 cp_parser_set_decl_spec_type (decl_specs
, type
,
17192 /*type_definition_p=*/false);
17197 case RID_DIRECT_BASES
:
17198 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17200 cp_parser_set_decl_spec_type (decl_specs
, type
,
17202 /*type_definition_p=*/false);
17208 /* If token is an already-parsed decltype not followed by ::,
17209 it's a simple-type-specifier. */
17210 if (token
->type
== CPP_DECLTYPE
17211 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17213 type
= saved_checks_value (token
->u
.tree_check_value
);
17216 cp_parser_set_decl_spec_type (decl_specs
, type
,
17218 /*type_definition_p=*/false);
17219 /* Remember that we are handling a decltype in order to
17220 implement the resolution of DR 1510 when the argument
17221 isn't instantiation dependent. */
17222 decl_specs
->decltype_p
= true;
17224 cp_lexer_consume_token (parser
->lexer
);
17228 /* If the type-specifier was for a built-in type, we're done. */
17231 /* Record the type. */
17233 && (token
->keyword
!= RID_SIGNED
17234 && token
->keyword
!= RID_UNSIGNED
17235 && token
->keyword
!= RID_SHORT
17236 && token
->keyword
!= RID_LONG
))
17237 cp_parser_set_decl_spec_type (decl_specs
,
17240 /*type_definition_p=*/false);
17242 decl_specs
->any_specifiers_p
= true;
17244 /* Consume the token. */
17245 cp_lexer_consume_token (parser
->lexer
);
17247 if (type
== error_mark_node
)
17248 return error_mark_node
;
17250 /* There is no valid C++ program where a non-template type is
17251 followed by a "<". That usually indicates that the user thought
17252 that the type was a template. */
17253 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17256 return TYPE_NAME (type
);
17259 /* The type-specifier must be a user-defined type. */
17260 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17265 /* Don't gobble tokens or issue error messages if this is an
17266 optional type-specifier. */
17267 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17268 cp_parser_parse_tentatively (parser
);
17270 token
= cp_lexer_peek_token (parser
->lexer
);
17272 /* Look for the optional `::' operator. */
17274 = (cp_parser_global_scope_opt (parser
,
17275 /*current_scope_valid_p=*/false)
17277 /* Look for the nested-name specifier. */
17279 = (cp_parser_nested_name_specifier_opt (parser
,
17280 /*typename_keyword_p=*/false,
17281 /*check_dependency_p=*/true,
17283 /*is_declaration=*/false)
17285 /* If we have seen a nested-name-specifier, and the next token
17286 is `template', then we are using the template-id production. */
17288 && cp_parser_optional_template_keyword (parser
))
17290 /* Look for the template-id. */
17291 type
= cp_parser_template_id (parser
,
17292 /*template_keyword_p=*/true,
17293 /*check_dependency_p=*/true,
17295 /*is_declaration=*/false);
17296 /* If the template-id did not name a type, we are out of
17298 if (TREE_CODE (type
) != TYPE_DECL
)
17300 cp_parser_error (parser
, "expected template-id for type");
17304 /* Otherwise, look for a type-name. */
17306 type
= cp_parser_type_name (parser
);
17307 /* Keep track of all name-lookups performed in class scopes. */
17311 && TREE_CODE (type
) == TYPE_DECL
17312 && identifier_p (DECL_NAME (type
)))
17313 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17314 /* If it didn't work out, we don't have a TYPE. */
17315 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17316 && !cp_parser_parse_definitely (parser
))
17318 if (!type
&& cxx_dialect
>= cxx17
)
17320 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17321 cp_parser_parse_tentatively (parser
);
17323 cp_parser_global_scope_opt (parser
,
17324 /*current_scope_valid_p=*/false);
17325 cp_parser_nested_name_specifier_opt (parser
,
17326 /*typename_keyword_p=*/false,
17327 /*check_dependency_p=*/true,
17329 /*is_declaration=*/false);
17330 tree name
= cp_parser_identifier (parser
);
17331 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17332 && parser
->scope
!= error_mark_node
)
17334 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17336 /*is_template=*/false,
17337 /*is_namespace=*/false,
17338 /*check_dependency=*/true,
17339 /*ambiguous_decls=*/NULL
,
17341 if (tmpl
&& tmpl
!= error_mark_node
17342 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17343 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17344 type
= make_template_placeholder (tmpl
);
17347 type
= error_mark_node
;
17348 if (!cp_parser_simulate_error (parser
))
17349 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17350 NLE_TYPE
, token
->location
);
17354 type
= error_mark_node
;
17356 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17357 && !cp_parser_parse_definitely (parser
))
17360 if (type
&& decl_specs
)
17361 cp_parser_set_decl_spec_type (decl_specs
, type
,
17363 /*type_definition_p=*/false);
17366 /* If we didn't get a type-name, issue an error message. */
17367 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17369 cp_parser_error (parser
, "expected type-name");
17370 return error_mark_node
;
17373 if (type
&& type
!= error_mark_node
)
17375 /* See if TYPE is an Objective-C type, and if so, parse and
17376 accept any protocol references following it. Do this before
17377 the cp_parser_check_for_invalid_template_id() call, because
17378 Objective-C types can be followed by '<...>' which would
17379 enclose protocol names rather than template arguments, and so
17380 everything is fine. */
17381 if (c_dialect_objc () && !parser
->scope
17382 && (objc_is_id (type
) || objc_is_class_name (type
)))
17384 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17385 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17387 /* Clobber the "unqualified" type previously entered into
17388 DECL_SPECS with the new, improved protocol-qualified version. */
17390 decl_specs
->type
= qual_type
;
17395 /* There is no valid C++ program where a non-template type is
17396 followed by a "<". That usually indicates that the user
17397 thought that the type was a template. */
17398 cp_parser_check_for_invalid_template_id (parser
, type
,
17406 /* Parse a type-name.
17412 simple-template-id [in c++0x]
17429 Returns a TYPE_DECL for the type. */
17432 cp_parser_type_name (cp_parser
* parser
)
17434 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17439 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17443 /* We can't know yet whether it is a class-name or not. */
17444 cp_parser_parse_tentatively (parser
);
17445 /* Try a class-name. */
17446 type_decl
= cp_parser_class_name (parser
,
17447 typename_keyword_p
,
17448 /*template_keyword_p=*/false,
17450 /*check_dependency_p=*/true,
17451 /*class_head_p=*/false,
17452 /*is_declaration=*/false);
17453 /* If it's not a class-name, keep looking. */
17454 if (!cp_parser_parse_definitely (parser
))
17456 if (cxx_dialect
< cxx11
)
17457 /* It must be a typedef-name or an enum-name. */
17458 return cp_parser_nonclass_name (parser
);
17460 cp_parser_parse_tentatively (parser
);
17461 /* It is either a simple-template-id representing an
17462 instantiation of an alias template... */
17463 type_decl
= cp_parser_template_id (parser
,
17464 /*template_keyword_p=*/false,
17465 /*check_dependency_p=*/true,
17467 /*is_declaration=*/false);
17468 /* Note that this must be an instantiation of an alias template
17469 because [temp.names]/6 says:
17471 A template-id that names an alias template specialization
17474 Whereas [temp.names]/7 says:
17476 A simple-template-id that names a class template
17477 specialization is a class-name.
17479 With concepts, this could also be a partial-concept-id that
17480 declares a non-type template parameter. */
17481 if (type_decl
!= NULL_TREE
17482 && TREE_CODE (type_decl
) == TYPE_DECL
17483 && TYPE_DECL_ALIAS_P (type_decl
))
17484 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17485 else if (is_constrained_parameter (type_decl
))
17486 /* Don't do anything. */ ;
17488 cp_parser_simulate_error (parser
);
17490 if (!cp_parser_parse_definitely (parser
))
17491 /* ... Or a typedef-name or an enum-name. */
17492 return cp_parser_nonclass_name (parser
);
17498 /* Check if DECL and ARGS can form a constrained-type-specifier.
17499 If ARGS is non-null, we try to form a concept check of the
17500 form DECL<?, ARGS> where ? is a wildcard that matches any
17501 kind of template argument. If ARGS is NULL, then we try to
17502 form a concept check of the form DECL<?>. */
17505 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17506 tree decl
, tree args
)
17508 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17510 /* If we a constrained-type-specifier cannot be deduced. */
17511 if (parser
->prevent_constrained_type_specifiers
)
17514 /* A constrained type specifier can only be found in an
17515 overload set or as a reference to a template declaration.
17517 FIXME: This might be masking a bug. It's possible that
17518 that the deduction below is causing template specializations
17519 to be formed with the wildcard as an argument. */
17520 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17523 /* Try to build a call expression that evaluates the
17524 concept. This can fail if the overload set refers
17525 only to non-templates. */
17526 tree placeholder
= build_nt (WILDCARD_DECL
);
17527 tree check
= build_concept_check (decl
, placeholder
, args
);
17528 if (check
== error_mark_node
)
17531 /* Deduce the checked constraint and the prototype parameter.
17533 FIXME: In certain cases, failure to deduce should be a
17534 diagnosable error. */
17537 if (!deduce_constrained_parameter (check
, conc
, proto
))
17540 /* In template parameter scope, this results in a constrained
17541 parameter. Return a descriptor of that parm. */
17542 if (processing_template_parmlist
)
17543 return build_constrained_parameter (conc
, proto
, args
);
17545 /* In a parameter-declaration-clause, constrained-type
17546 specifiers result in invented template parameters. */
17547 if (parser
->auto_is_implicit_function_template_parm_p
)
17549 tree x
= build_constrained_parameter (conc
, proto
, args
);
17550 return synthesize_implicit_template_parm (parser
, x
);
17554 /* Otherwise, we're in a context where the constrained
17555 type name is deduced and the constraint applies
17556 after deduction. */
17557 return make_constrained_auto (conc
, args
);
17563 /* If DECL refers to a concept, return a TYPE_DECL representing
17564 the result of using the constrained type specifier in the
17565 current context. DECL refers to a concept if
17567 - it is an overload set containing a function concept taking a single
17570 - it is a variable concept taking a single type argument. */
17573 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17576 && (TREE_CODE (decl
) == OVERLOAD
17577 || BASELINK_P (decl
)
17578 || variable_concept_p (decl
)))
17579 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17584 /* Check if DECL and ARGS form a partial-concept-id. If so,
17585 assign ID to the resulting constrained placeholder.
17587 Returns true if the partial-concept-id designates a placeholder
17588 and false otherwise. Note that *id is set to NULL_TREE in
17592 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17594 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17597 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17609 Returns a TYPE_DECL for the type. */
17612 cp_parser_nonclass_name (cp_parser
* parser
)
17617 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17618 identifier
= cp_parser_identifier (parser
);
17619 if (identifier
== error_mark_node
)
17620 return error_mark_node
;
17622 /* Look up the type-name. */
17623 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17625 type_decl
= strip_using_decl (type_decl
);
17627 /* If we found an overload set, then it may refer to a concept-name. */
17628 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17631 if (TREE_CODE (type_decl
) != TYPE_DECL
17632 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17634 /* See if this is an Objective-C type. */
17635 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17636 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17638 type_decl
= TYPE_NAME (type
);
17641 /* Issue an error if we did not find a type-name. */
17642 if (TREE_CODE (type_decl
) != TYPE_DECL
17643 /* In Objective-C, we have the complication that class names are
17644 normally type names and start declarations (eg, the
17645 "NSObject" in "NSObject *object;"), but can be used in an
17646 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17647 is an expression. So, a classname followed by a dot is not a
17648 valid type-name. */
17649 || (objc_is_class_name (TREE_TYPE (type_decl
))
17650 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17652 if (!cp_parser_simulate_error (parser
))
17653 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17654 NLE_TYPE
, token
->location
);
17655 return error_mark_node
;
17657 /* Remember that the name was used in the definition of the
17658 current class so that we can check later to see if the
17659 meaning would have been different after the class was
17660 entirely defined. */
17661 else if (type_decl
!= error_mark_node
17663 maybe_note_name_used_in_class (identifier
, type_decl
);
17668 /* Parse an elaborated-type-specifier. Note that the grammar given
17669 here incorporates the resolution to DR68.
17671 elaborated-type-specifier:
17672 class-key :: [opt] nested-name-specifier [opt] identifier
17673 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17674 enum-key :: [opt] nested-name-specifier [opt] identifier
17675 typename :: [opt] nested-name-specifier identifier
17676 typename :: [opt] nested-name-specifier template [opt]
17681 elaborated-type-specifier:
17682 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17683 class-key attributes :: [opt] nested-name-specifier [opt]
17684 template [opt] template-id
17685 enum attributes :: [opt] nested-name-specifier [opt] identifier
17687 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17688 declared `friend'. If IS_DECLARATION is TRUE, then this
17689 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17690 something is being declared.
17692 Returns the TYPE specified. */
17695 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17697 bool is_declaration
)
17699 enum tag_types tag_type
;
17701 tree type
= NULL_TREE
;
17702 tree attributes
= NULL_TREE
;
17704 cp_token
*token
= NULL
;
17706 /* See if we're looking at the `enum' keyword. */
17707 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17709 /* Consume the `enum' token. */
17710 cp_lexer_consume_token (parser
->lexer
);
17711 /* Remember that it's an enumeration type. */
17712 tag_type
= enum_type
;
17713 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17714 enums) is used here. */
17715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17716 if (cp_parser_is_keyword (token
, RID_CLASS
)
17717 || cp_parser_is_keyword (token
, RID_STRUCT
))
17719 gcc_rich_location
richloc (token
->location
);
17720 richloc
.add_range (input_location
, false);
17721 richloc
.add_fixit_remove ();
17722 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17723 "a scoped enum must not use the %qD keyword",
17725 /* Consume the `struct' or `class' and parse it anyway. */
17726 cp_lexer_consume_token (parser
->lexer
);
17728 /* Parse the attributes. */
17729 attributes
= cp_parser_attributes_opt (parser
);
17731 /* Or, it might be `typename'. */
17732 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17735 /* Consume the `typename' token. */
17736 cp_lexer_consume_token (parser
->lexer
);
17737 /* Remember that it's a `typename' type. */
17738 tag_type
= typename_type
;
17740 /* Otherwise it must be a class-key. */
17743 tag_type
= cp_parser_class_key (parser
);
17744 if (tag_type
== none_type
)
17745 return error_mark_node
;
17746 /* Parse the attributes. */
17747 attributes
= cp_parser_attributes_opt (parser
);
17750 /* Look for the `::' operator. */
17751 globalscope
= cp_parser_global_scope_opt (parser
,
17752 /*current_scope_valid_p=*/false);
17753 /* Look for the nested-name-specifier. */
17754 tree nested_name_specifier
;
17755 if (tag_type
== typename_type
&& !globalscope
)
17757 nested_name_specifier
17758 = cp_parser_nested_name_specifier (parser
,
17759 /*typename_keyword_p=*/true,
17760 /*check_dependency_p=*/true,
17763 if (!nested_name_specifier
)
17764 return error_mark_node
;
17767 /* Even though `typename' is not present, the proposed resolution
17768 to Core Issue 180 says that in `class A<T>::B', `B' should be
17769 considered a type-name, even if `A<T>' is dependent. */
17770 nested_name_specifier
17771 = cp_parser_nested_name_specifier_opt (parser
,
17772 /*typename_keyword_p=*/true,
17773 /*check_dependency_p=*/true,
17776 /* For everything but enumeration types, consider a template-id.
17777 For an enumeration type, consider only a plain identifier. */
17778 if (tag_type
!= enum_type
)
17780 bool template_p
= false;
17783 /* Allow the `template' keyword. */
17784 template_p
= cp_parser_optional_template_keyword (parser
);
17785 /* If we didn't see `template', we don't know if there's a
17786 template-id or not. */
17788 cp_parser_parse_tentatively (parser
);
17789 /* Parse the template-id. */
17790 token
= cp_lexer_peek_token (parser
->lexer
);
17791 decl
= cp_parser_template_id (parser
, template_p
,
17792 /*check_dependency_p=*/true,
17795 /* If we didn't find a template-id, look for an ordinary
17797 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17799 /* We can get here when cp_parser_template_id, called by
17800 cp_parser_class_name with tag_type == none_type, succeeds
17801 and caches a BASELINK. Then, when called again here,
17802 instead of failing and returning an error_mark_node
17803 returns it (see template/typename17.C in C++11).
17804 ??? Could we diagnose this earlier? */
17805 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17807 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17808 type
= error_mark_node
;
17810 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17811 in effect, then we must assume that, upon instantiation, the
17812 template will correspond to a class. */
17813 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17814 && tag_type
== typename_type
)
17815 type
= make_typename_type (parser
->scope
, decl
,
17817 /*complain=*/tf_error
);
17818 /* If the `typename' keyword is in effect and DECL is not a type
17819 decl, then type is non existent. */
17820 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17822 else if (TREE_CODE (decl
) == TYPE_DECL
)
17824 type
= check_elaborated_type_specifier (tag_type
, decl
,
17825 /*allow_template_p=*/true);
17827 /* If the next token is a semicolon, this must be a specialization,
17828 instantiation, or friend declaration. Check the scope while we
17829 still know whether or not we had a nested-name-specifier. */
17830 if (type
!= error_mark_node
17831 && !nested_name_specifier
&& !is_friend
17832 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17833 check_unqualified_spec_or_inst (type
, token
->location
);
17835 else if (decl
== error_mark_node
)
17836 type
= error_mark_node
;
17841 token
= cp_lexer_peek_token (parser
->lexer
);
17842 identifier
= cp_parser_identifier (parser
);
17844 if (identifier
== error_mark_node
)
17846 parser
->scope
= NULL_TREE
;
17847 return error_mark_node
;
17850 /* For a `typename', we needn't call xref_tag. */
17851 if (tag_type
== typename_type
17852 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17853 return cp_parser_make_typename_type (parser
, identifier
,
17856 /* Template parameter lists apply only if we are not within a
17857 function parameter list. */
17858 bool template_parm_lists_apply
17859 = parser
->num_template_parameter_lists
;
17860 if (template_parm_lists_apply
)
17861 for (cp_binding_level
*s
= current_binding_level
;
17862 s
&& s
->kind
!= sk_template_parms
;
17863 s
= s
->level_chain
)
17864 if (s
->kind
== sk_function_parms
)
17865 template_parm_lists_apply
= false;
17867 /* Look up a qualified name in the usual way. */
17871 tree ambiguous_decls
;
17873 decl
= cp_parser_lookup_name (parser
, identifier
,
17875 /*is_template=*/false,
17876 /*is_namespace=*/false,
17877 /*check_dependency=*/true,
17881 /* If the lookup was ambiguous, an error will already have been
17883 if (ambiguous_decls
)
17884 return error_mark_node
;
17886 /* If we are parsing friend declaration, DECL may be a
17887 TEMPLATE_DECL tree node here. However, we need to check
17888 whether this TEMPLATE_DECL results in valid code. Consider
17889 the following example:
17892 template <class T> class C {};
17895 template <class T> friend class N::C; // #1, valid code
17897 template <class T> class Y {
17898 friend class N::C; // #2, invalid code
17901 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17902 name lookup of `N::C'. We see that friend declaration must
17903 be template for the code to be valid. Note that
17904 processing_template_decl does not work here since it is
17905 always 1 for the above two cases. */
17907 decl
= (cp_parser_maybe_treat_template_as_class
17908 (decl
, /*tag_name_p=*/is_friend
17909 && template_parm_lists_apply
));
17911 if (TREE_CODE (decl
) != TYPE_DECL
)
17913 cp_parser_diagnose_invalid_type_name (parser
,
17916 return error_mark_node
;
17919 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17921 bool allow_template
= (template_parm_lists_apply
17922 || DECL_SELF_REFERENCE_P (decl
));
17923 type
= check_elaborated_type_specifier (tag_type
, decl
,
17926 if (type
== error_mark_node
)
17927 return error_mark_node
;
17930 /* Forward declarations of nested types, such as
17935 are invalid unless all components preceding the final '::'
17936 are complete. If all enclosing types are complete, these
17937 declarations become merely pointless.
17939 Invalid forward declarations of nested types are errors
17940 caught elsewhere in parsing. Those that are pointless arrive
17943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17944 && !is_friend
&& !processing_explicit_instantiation
)
17945 warning (0, "declaration %qD does not declare anything", decl
);
17947 type
= TREE_TYPE (decl
);
17951 /* An elaborated-type-specifier sometimes introduces a new type and
17952 sometimes names an existing type. Normally, the rule is that it
17953 introduces a new type only if there is not an existing type of
17954 the same name already in scope. For example, given:
17957 void f() { struct S s; }
17959 the `struct S' in the body of `f' is the same `struct S' as in
17960 the global scope; the existing definition is used. However, if
17961 there were no global declaration, this would introduce a new
17962 local class named `S'.
17964 An exception to this rule applies to the following code:
17966 namespace N { struct S; }
17968 Here, the elaborated-type-specifier names a new type
17969 unconditionally; even if there is already an `S' in the
17970 containing scope this declaration names a new type.
17971 This exception only applies if the elaborated-type-specifier
17972 forms the complete declaration:
17976 A declaration consisting solely of `class-key identifier ;' is
17977 either a redeclaration of the name in the current scope or a
17978 forward declaration of the identifier as a class name. It
17979 introduces the name into the current scope.
17981 We are in this situation precisely when the next token is a `;'.
17983 An exception to the exception is that a `friend' declaration does
17984 *not* name a new type; i.e., given:
17986 struct S { friend struct T; };
17988 `T' is not a new type in the scope of `S'.
17990 Also, `new struct S' or `sizeof (struct S)' never results in the
17991 definition of a new type; a new type can only be declared in a
17992 declaration context. */
17998 /* Friends have special name lookup rules. */
17999 ts
= ts_within_enclosing_non_class
;
18000 else if (is_declaration
18001 && cp_lexer_next_token_is (parser
->lexer
,
18003 /* This is a `class-key identifier ;' */
18009 (template_parm_lists_apply
18010 && (cp_parser_next_token_starts_class_definition_p (parser
)
18011 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18012 /* An unqualified name was used to reference this type, so
18013 there were no qualifying templates. */
18014 if (template_parm_lists_apply
18015 && !cp_parser_check_template_parameters (parser
,
18016 /*num_templates=*/0,
18017 /*template_id*/false,
18019 /*declarator=*/NULL
))
18020 return error_mark_node
;
18021 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18025 if (type
== error_mark_node
)
18026 return error_mark_node
;
18028 /* Allow attributes on forward declarations of classes. */
18031 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18032 warning (OPT_Wattributes
,
18033 "attributes ignored on uninstantiated type");
18034 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18035 && ! processing_explicit_instantiation
)
18036 warning (OPT_Wattributes
,
18037 "attributes ignored on template instantiation");
18038 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18039 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18041 warning (OPT_Wattributes
,
18042 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18045 if (tag_type
!= enum_type
)
18047 /* Indicate whether this class was declared as a `class' or as a
18049 if (CLASS_TYPE_P (type
))
18050 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18051 cp_parser_check_class_key (tag_type
, type
);
18054 /* A "<" cannot follow an elaborated type specifier. If that
18055 happens, the user was probably trying to form a template-id. */
18056 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18062 /* Parse an enum-specifier.
18065 enum-head { enumerator-list [opt] }
18066 enum-head { enumerator-list , } [C++0x]
18069 enum-key identifier [opt] enum-base [opt]
18070 enum-key nested-name-specifier identifier enum-base [opt]
18075 enum struct [C++0x]
18078 : type-specifier-seq
18080 opaque-enum-specifier:
18081 enum-key identifier enum-base [opt] ;
18084 enum-key attributes[opt] identifier [opt] enum-base [opt]
18085 { enumerator-list [opt] }attributes[opt]
18086 enum-key attributes[opt] identifier [opt] enum-base [opt]
18087 { enumerator-list, }attributes[opt] [C++0x]
18089 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18090 if the token stream isn't an enum-specifier after all. */
18093 cp_parser_enum_specifier (cp_parser
* parser
)
18096 tree type
= NULL_TREE
;
18098 tree nested_name_specifier
= NULL_TREE
;
18100 bool scoped_enum_p
= false;
18101 bool has_underlying_type
= false;
18102 bool nested_being_defined
= false;
18103 bool new_value_list
= false;
18104 bool is_new_type
= false;
18105 bool is_unnamed
= false;
18106 tree underlying_type
= NULL_TREE
;
18107 cp_token
*type_start_token
= NULL
;
18108 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18110 parser
->colon_corrects_to_scope_p
= false;
18112 /* Parse tentatively so that we can back up if we don't find a
18114 cp_parser_parse_tentatively (parser
);
18116 /* Caller guarantees that the current token is 'enum', an identifier
18117 possibly follows, and the token after that is an opening brace.
18118 If we don't have an identifier, fabricate an anonymous name for
18119 the enumeration being defined. */
18120 cp_lexer_consume_token (parser
->lexer
);
18122 /* Parse the "class" or "struct", which indicates a scoped
18123 enumeration type in C++0x. */
18124 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18125 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18127 if (cxx_dialect
< cxx11
)
18128 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18130 /* Consume the `struct' or `class' token. */
18131 cp_lexer_consume_token (parser
->lexer
);
18133 scoped_enum_p
= true;
18136 attributes
= cp_parser_attributes_opt (parser
);
18138 /* Clear the qualification. */
18139 parser
->scope
= NULL_TREE
;
18140 parser
->qualifying_scope
= NULL_TREE
;
18141 parser
->object_scope
= NULL_TREE
;
18143 /* Figure out in what scope the declaration is being placed. */
18144 prev_scope
= current_scope ();
18146 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18148 push_deferring_access_checks (dk_no_check
);
18149 nested_name_specifier
18150 = cp_parser_nested_name_specifier_opt (parser
,
18151 /*typename_keyword_p=*/true,
18152 /*check_dependency_p=*/false,
18154 /*is_declaration=*/false);
18156 if (nested_name_specifier
)
18160 identifier
= cp_parser_identifier (parser
);
18161 name
= cp_parser_lookup_name (parser
, identifier
,
18163 /*is_template=*/false,
18164 /*is_namespace=*/false,
18165 /*check_dependency=*/true,
18166 /*ambiguous_decls=*/NULL
,
18168 if (name
&& name
!= error_mark_node
)
18170 type
= TREE_TYPE (name
);
18171 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18173 /* Are template enums allowed in ISO? */
18174 if (template_parm_scope_p ())
18175 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18176 "%qD is an enumeration template", name
);
18177 /* ignore a typename reference, for it will be solved by name
18182 else if (nested_name_specifier
== error_mark_node
)
18183 /* We already issued an error. */;
18186 error_at (type_start_token
->location
,
18187 "%qD does not name an enumeration in %qT",
18188 identifier
, nested_name_specifier
);
18189 nested_name_specifier
= error_mark_node
;
18194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18195 identifier
= cp_parser_identifier (parser
);
18198 identifier
= make_anon_name ();
18201 error_at (type_start_token
->location
,
18202 "unnamed scoped enum is not allowed");
18205 pop_deferring_access_checks ();
18207 /* Check for the `:' that denotes a specified underlying type in C++0x.
18208 Note that a ':' could also indicate a bitfield width, however. */
18209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18211 cp_decl_specifier_seq type_specifiers
;
18213 /* Consume the `:'. */
18214 cp_lexer_consume_token (parser
->lexer
);
18216 /* Parse the type-specifier-seq. */
18217 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18218 /*is_trailing_return=*/false,
18221 /* At this point this is surely not elaborated type specifier. */
18222 if (!cp_parser_parse_definitely (parser
))
18225 if (cxx_dialect
< cxx11
)
18226 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18228 has_underlying_type
= true;
18230 /* If that didn't work, stop. */
18231 if (type_specifiers
.type
!= error_mark_node
)
18233 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18234 /*initialized=*/0, NULL
);
18235 if (underlying_type
== error_mark_node
18236 || check_for_bare_parameter_packs (underlying_type
))
18237 underlying_type
= NULL_TREE
;
18241 /* Look for the `{' but don't consume it yet. */
18242 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18244 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18246 cp_parser_error (parser
, "expected %<{%>");
18247 if (has_underlying_type
)
18253 /* An opaque-enum-specifier must have a ';' here. */
18254 if ((scoped_enum_p
|| underlying_type
)
18255 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18257 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18258 if (has_underlying_type
)
18266 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18269 if (nested_name_specifier
)
18271 if (CLASS_TYPE_P (nested_name_specifier
))
18273 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18274 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18275 push_scope (nested_name_specifier
);
18277 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18279 push_nested_namespace (nested_name_specifier
);
18283 /* Issue an error message if type-definitions are forbidden here. */
18284 if (!cp_parser_check_type_definition (parser
))
18285 type
= error_mark_node
;
18287 /* Create the new type. We do this before consuming the opening
18288 brace so the enum will be recorded as being on the line of its
18289 tag (or the 'enum' keyword, if there is no tag). */
18290 type
= start_enum (identifier
, type
, underlying_type
,
18291 attributes
, scoped_enum_p
, &is_new_type
);
18293 /* If the next token is not '{' it is an opaque-enum-specifier or an
18294 elaborated-type-specifier. */
18295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18297 timevar_push (TV_PARSE_ENUM
);
18298 if (nested_name_specifier
18299 && nested_name_specifier
!= error_mark_node
)
18301 /* The following catches invalid code such as:
18302 enum class S<int>::E { A, B, C }; */
18303 if (!processing_specialization
18304 && CLASS_TYPE_P (nested_name_specifier
)
18305 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18306 error_at (type_start_token
->location
, "cannot add an enumerator "
18307 "list to a template instantiation");
18309 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18311 error_at (type_start_token
->location
,
18312 "%<%T::%E%> has not been declared",
18313 TYPE_CONTEXT (nested_name_specifier
),
18314 nested_name_specifier
);
18315 type
= error_mark_node
;
18317 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18318 && !CLASS_TYPE_P (nested_name_specifier
))
18320 error_at (type_start_token
->location
, "nested name specifier "
18321 "%qT for enum declaration does not name a class "
18322 "or namespace", nested_name_specifier
);
18323 type
= error_mark_node
;
18325 /* If that scope does not contain the scope in which the
18326 class was originally declared, the program is invalid. */
18327 else if (prev_scope
&& !is_ancestor (prev_scope
,
18328 nested_name_specifier
))
18330 if (at_namespace_scope_p ())
18331 error_at (type_start_token
->location
,
18332 "declaration of %qD in namespace %qD which does not "
18334 type
, prev_scope
, nested_name_specifier
);
18336 error_at (type_start_token
->location
,
18337 "declaration of %qD in %qD which does not "
18339 type
, prev_scope
, nested_name_specifier
);
18340 type
= error_mark_node
;
18342 /* If that scope is the scope where the declaration is being placed
18343 the program is invalid. */
18344 else if (CLASS_TYPE_P (nested_name_specifier
)
18345 && CLASS_TYPE_P (prev_scope
)
18346 && same_type_p (nested_name_specifier
, prev_scope
))
18348 permerror (type_start_token
->location
,
18349 "extra qualification not allowed");
18350 nested_name_specifier
= NULL_TREE
;
18355 begin_scope (sk_scoped_enum
, type
);
18357 /* Consume the opening brace. */
18358 matching_braces braces
;
18359 braces
.consume_open (parser
);
18361 if (type
== error_mark_node
)
18362 ; /* Nothing to add */
18363 else if (OPAQUE_ENUM_P (type
)
18364 || (cxx_dialect
> cxx98
&& processing_specialization
))
18366 new_value_list
= true;
18367 SET_OPAQUE_ENUM_P (type
, false);
18368 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18372 error_at (type_start_token
->location
,
18373 "multiple definition of %q#T", type
);
18374 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18375 "previous definition here");
18376 type
= error_mark_node
;
18379 if (type
== error_mark_node
)
18380 cp_parser_skip_to_end_of_block_or_statement (parser
);
18381 /* If the next token is not '}', then there are some enumerators. */
18382 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18384 if (is_unnamed
&& !scoped_enum_p
)
18385 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18386 "ISO C++ forbids empty unnamed enum");
18389 cp_parser_enumerator_list (parser
, type
);
18391 /* Consume the final '}'. */
18392 braces
.require_close (parser
);
18396 timevar_pop (TV_PARSE_ENUM
);
18400 /* If a ';' follows, then it is an opaque-enum-specifier
18401 and additional restrictions apply. */
18402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18405 error_at (type_start_token
->location
,
18406 "opaque-enum-specifier without name");
18407 else if (nested_name_specifier
)
18408 error_at (type_start_token
->location
,
18409 "opaque-enum-specifier must use a simple identifier");
18413 /* Look for trailing attributes to apply to this enumeration, and
18414 apply them if appropriate. */
18415 if (cp_parser_allow_gnu_extensions_p (parser
))
18417 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18418 cplus_decl_attributes (&type
,
18420 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18423 /* Finish up the enumeration. */
18424 if (type
!= error_mark_node
)
18426 if (new_value_list
)
18427 finish_enum_value_list (type
);
18429 finish_enum (type
);
18432 if (nested_name_specifier
)
18434 if (CLASS_TYPE_P (nested_name_specifier
))
18436 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18437 pop_scope (nested_name_specifier
);
18439 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18441 pop_nested_namespace (nested_name_specifier
);
18445 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18449 /* Parse an enumerator-list. The enumerators all have the indicated
18453 enumerator-definition
18454 enumerator-list , enumerator-definition */
18457 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18461 /* Parse an enumerator-definition. */
18462 cp_parser_enumerator_definition (parser
, type
);
18464 /* If the next token is not a ',', we've reached the end of
18466 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18468 /* Otherwise, consume the `,' and keep going. */
18469 cp_lexer_consume_token (parser
->lexer
);
18470 /* If the next token is a `}', there is a trailing comma. */
18471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18473 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18474 pedwarn (input_location
, OPT_Wpedantic
,
18475 "comma at end of enumerator list");
18481 /* Parse an enumerator-definition. The enumerator has the indicated
18484 enumerator-definition:
18486 enumerator = constant-expression
18493 enumerator-definition:
18494 enumerator attributes [opt]
18495 enumerator attributes [opt] = constant-expression */
18498 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18504 /* Save the input location because we are interested in the location
18505 of the identifier and not the location of the explicit value. */
18506 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18508 /* Look for the identifier. */
18509 identifier
= cp_parser_identifier (parser
);
18510 if (identifier
== error_mark_node
)
18513 /* Parse any specified attributes. */
18514 tree attrs
= cp_parser_attributes_opt (parser
);
18516 /* If the next token is an '=', then there is an explicit value. */
18517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18519 /* Consume the `=' token. */
18520 cp_lexer_consume_token (parser
->lexer
);
18521 /* Parse the value. */
18522 value
= cp_parser_constant_expression (parser
);
18527 /* If we are processing a template, make sure the initializer of the
18528 enumerator doesn't contain any bare template parameter pack. */
18529 if (check_for_bare_parameter_packs (value
))
18530 value
= error_mark_node
;
18532 /* Create the enumerator. */
18533 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18536 /* Parse a namespace-name.
18539 original-namespace-name
18542 Returns the NAMESPACE_DECL for the namespace. */
18545 cp_parser_namespace_name (cp_parser
* parser
)
18548 tree namespace_decl
;
18550 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18552 /* Get the name of the namespace. */
18553 identifier
= cp_parser_identifier (parser
);
18554 if (identifier
== error_mark_node
)
18555 return error_mark_node
;
18557 /* Look up the identifier in the currently active scope. Look only
18558 for namespaces, due to:
18560 [basic.lookup.udir]
18562 When looking up a namespace-name in a using-directive or alias
18563 definition, only namespace names are considered.
18567 [basic.lookup.qual]
18569 During the lookup of a name preceding the :: scope resolution
18570 operator, object, function, and enumerator names are ignored.
18572 (Note that cp_parser_qualifying_entity only calls this
18573 function if the token after the name is the scope resolution
18575 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18577 /*is_template=*/false,
18578 /*is_namespace=*/true,
18579 /*check_dependency=*/true,
18580 /*ambiguous_decls=*/NULL
,
18582 /* If it's not a namespace, issue an error. */
18583 if (namespace_decl
== error_mark_node
18584 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18586 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18588 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18589 if (namespace_decl
== error_mark_node
18590 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18591 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18594 cp_parser_error (parser
, "expected namespace-name");
18595 namespace_decl
= error_mark_node
;
18598 return namespace_decl
;
18601 /* Parse a namespace-definition.
18603 namespace-definition:
18604 named-namespace-definition
18605 unnamed-namespace-definition
18607 named-namespace-definition:
18608 original-namespace-definition
18609 extension-namespace-definition
18611 original-namespace-definition:
18612 namespace identifier { namespace-body }
18614 extension-namespace-definition:
18615 namespace original-namespace-name { namespace-body }
18617 unnamed-namespace-definition:
18618 namespace { namespace-body } */
18621 cp_parser_namespace_definition (cp_parser
* parser
)
18624 int nested_definition_count
= 0;
18626 cp_ensure_no_omp_declare_simd (parser
);
18627 cp_ensure_no_oacc_routine (parser
);
18629 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18633 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18634 cp_lexer_consume_token (parser
->lexer
);
18637 /* Look for the `namespace' keyword. */
18639 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18641 /* Parse any specified attributes before the identifier. */
18642 tree attribs
= cp_parser_attributes_opt (parser
);
18646 identifier
= NULL_TREE
;
18648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18650 identifier
= cp_parser_identifier (parser
);
18652 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18653 pedwarn (input_location
, OPT_Wpedantic
,
18654 "standard attributes on namespaces must precede "
18655 "the namespace name");
18657 /* Parse any attributes specified after the identifier. */
18658 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18664 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18665 pedwarn (input_location
, OPT_Wpedantic
,
18666 "nested namespace definitions only available with "
18667 "-std=c++17 or -std=gnu++17");
18669 /* Nested namespace names can create new namespaces (unlike
18670 other qualified-ids). */
18671 if (int count
= identifier
? push_namespace (identifier
) : 0)
18672 nested_definition_count
+= count
;
18674 cp_parser_error (parser
, "nested namespace name required");
18675 cp_lexer_consume_token (parser
->lexer
);
18678 if (nested_definition_count
&& !identifier
)
18679 cp_parser_error (parser
, "namespace name required");
18681 if (nested_definition_count
&& attribs
)
18682 error_at (token
->location
,
18683 "a nested namespace definition cannot have attributes");
18684 if (nested_definition_count
&& is_inline
)
18685 error_at (token
->location
,
18686 "a nested namespace definition cannot be inline");
18688 /* Start the namespace. */
18689 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18691 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18693 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18695 /* Look for the `{' to validate starting the namespace. */
18696 matching_braces braces
;
18697 if (braces
.require_open (parser
))
18699 /* Parse the body of the namespace. */
18700 cp_parser_namespace_body (parser
);
18702 /* Look for the final `}'. */
18703 braces
.require_close (parser
);
18706 if (has_visibility
)
18707 pop_visibility (1);
18709 /* Pop the nested namespace definitions. */
18710 while (nested_definition_count
--)
18714 /* Parse a namespace-body.
18717 declaration-seq [opt] */
18720 cp_parser_namespace_body (cp_parser
* parser
)
18722 cp_parser_declaration_seq_opt (parser
);
18725 /* Parse a namespace-alias-definition.
18727 namespace-alias-definition:
18728 namespace identifier = qualified-namespace-specifier ; */
18731 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18734 tree namespace_specifier
;
18736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18738 /* Look for the `namespace' keyword. */
18739 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18740 /* Look for the identifier. */
18741 identifier
= cp_parser_identifier (parser
);
18742 if (identifier
== error_mark_node
)
18744 /* Look for the `=' token. */
18745 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18746 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18748 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18749 /* Skip the definition. */
18750 cp_lexer_consume_token (parser
->lexer
);
18751 if (cp_parser_skip_to_closing_brace (parser
))
18752 cp_lexer_consume_token (parser
->lexer
);
18755 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18756 /* Look for the qualified-namespace-specifier. */
18757 namespace_specifier
18758 = cp_parser_qualified_namespace_specifier (parser
);
18759 /* Look for the `;' token. */
18760 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18762 /* Register the alias in the symbol table. */
18763 do_namespace_alias (identifier
, namespace_specifier
);
18766 /* Parse a qualified-namespace-specifier.
18768 qualified-namespace-specifier:
18769 :: [opt] nested-name-specifier [opt] namespace-name
18771 Returns a NAMESPACE_DECL corresponding to the specified
18775 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18777 /* Look for the optional `::'. */
18778 cp_parser_global_scope_opt (parser
,
18779 /*current_scope_valid_p=*/false);
18781 /* Look for the optional nested-name-specifier. */
18782 cp_parser_nested_name_specifier_opt (parser
,
18783 /*typename_keyword_p=*/false,
18784 /*check_dependency_p=*/true,
18786 /*is_declaration=*/true);
18788 return cp_parser_namespace_name (parser
);
18791 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18792 access declaration.
18795 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18796 using :: unqualified-id ;
18798 access-declaration:
18804 cp_parser_using_declaration (cp_parser
* parser
,
18805 bool access_declaration_p
)
18808 bool typename_p
= false;
18809 bool global_scope_p
;
18813 int oldcount
= errorcount
;
18814 cp_token
*diag_token
= NULL
;
18816 if (access_declaration_p
)
18818 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18819 cp_parser_parse_tentatively (parser
);
18823 /* Look for the `using' keyword. */
18824 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18827 /* Peek at the next token. */
18828 token
= cp_lexer_peek_token (parser
->lexer
);
18829 /* See if it's `typename'. */
18830 if (token
->keyword
== RID_TYPENAME
)
18832 /* Remember that we've seen it. */
18834 /* Consume the `typename' token. */
18835 cp_lexer_consume_token (parser
->lexer
);
18839 /* Look for the optional global scope qualification. */
18841 = (cp_parser_global_scope_opt (parser
,
18842 /*current_scope_valid_p=*/false)
18845 /* If we saw `typename', or didn't see `::', then there must be a
18846 nested-name-specifier present. */
18847 if (typename_p
|| !global_scope_p
)
18849 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18850 /*check_dependency_p=*/true,
18852 /*is_declaration=*/true);
18853 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18855 cp_parser_skip_to_end_of_block_or_statement (parser
);
18859 /* Otherwise, we could be in either of the two productions. In that
18860 case, treat the nested-name-specifier as optional. */
18862 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18863 /*typename_keyword_p=*/false,
18864 /*check_dependency_p=*/true,
18866 /*is_declaration=*/true);
18868 qscope
= global_namespace
;
18869 else if (UNSCOPED_ENUM_P (qscope
))
18870 qscope
= CP_TYPE_CONTEXT (qscope
);
18872 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18873 /* Something has already gone wrong; there's no need to parse
18874 further. Since an error has occurred, the return value of
18875 cp_parser_parse_definitely will be false, as required. */
18876 return cp_parser_parse_definitely (parser
);
18878 token
= cp_lexer_peek_token (parser
->lexer
);
18879 /* Parse the unqualified-id. */
18880 identifier
= cp_parser_unqualified_id (parser
,
18881 /*template_keyword_p=*/false,
18882 /*check_dependency_p=*/true,
18883 /*declarator_p=*/true,
18884 /*optional_p=*/false);
18886 if (access_declaration_p
)
18888 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18889 cp_parser_simulate_error (parser
);
18890 if (!cp_parser_parse_definitely (parser
))
18893 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18895 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18896 if (cxx_dialect
< cxx17
18897 && !in_system_header_at (ell
->location
))
18898 pedwarn (ell
->location
, 0,
18899 "pack expansion in using-declaration only available "
18900 "with -std=c++17 or -std=gnu++17");
18901 qscope
= make_pack_expansion (qscope
);
18904 /* The function we call to handle a using-declaration is different
18905 depending on what scope we are in. */
18906 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18908 else if (!identifier_p (identifier
)
18909 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18910 /* [namespace.udecl]
18912 A using declaration shall not name a template-id. */
18913 error_at (token
->location
,
18914 "a template-id may not appear in a using-declaration");
18917 if (at_class_scope_p ())
18919 /* Create the USING_DECL. */
18920 decl
= do_class_using_decl (qscope
, identifier
);
18922 if (decl
&& typename_p
)
18923 USING_DECL_TYPENAME_P (decl
) = 1;
18925 if (check_for_bare_parameter_packs (decl
))
18927 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18931 /* Add it to the list of members in this class. */
18932 finish_member_declaration (decl
);
18936 decl
= cp_parser_lookup_name_simple (parser
,
18939 if (decl
== error_mark_node
)
18940 cp_parser_name_lookup_error (parser
, identifier
,
18943 else if (check_for_bare_parameter_packs (decl
))
18945 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18948 else if (!at_namespace_scope_p ())
18949 finish_local_using_decl (decl
, qscope
, identifier
);
18951 finish_namespace_using_decl (decl
, qscope
, identifier
);
18955 if (!access_declaration_p
18956 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18958 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18959 if (cxx_dialect
< cxx17
)
18960 pedwarn (comma
->location
, 0,
18961 "comma-separated list in using-declaration only available "
18962 "with -std=c++17 or -std=gnu++17");
18966 /* Look for the final `;'. */
18967 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18969 if (access_declaration_p
&& errorcount
== oldcount
)
18970 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18971 "access declarations are deprecated "
18972 "in favour of using-declarations; "
18973 "suggestion: add the %<using%> keyword");
18978 /* Parse an alias-declaration.
18981 using identifier attribute-specifier-seq [opt] = type-id */
18984 cp_parser_alias_declaration (cp_parser
* parser
)
18986 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18987 location_t id_location
;
18988 cp_declarator
*declarator
;
18989 cp_decl_specifier_seq decl_specs
;
18991 const char *saved_message
= NULL
;
18993 /* Look for the `using' keyword. */
18994 cp_token
*using_token
18995 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18996 if (using_token
== NULL
)
18997 return error_mark_node
;
18999 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19000 id
= cp_parser_identifier (parser
);
19001 if (id
== error_mark_node
)
19002 return error_mark_node
;
19004 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19005 attributes
= cp_parser_attributes_opt (parser
);
19006 if (attributes
== error_mark_node
)
19007 return error_mark_node
;
19009 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19011 if (cp_parser_error_occurred (parser
))
19012 return error_mark_node
;
19014 cp_parser_commit_to_tentative_parse (parser
);
19016 /* Now we are going to parse the type-id of the declaration. */
19021 "A type-specifier-seq shall not define a class or enumeration
19022 unless it appears in the type-id of an alias-declaration (7.1.3) that
19023 is not the declaration of a template-declaration."
19025 In other words, if we currently are in an alias template, the
19026 type-id should not define a type.
19028 So let's set parser->type_definition_forbidden_message in that
19029 case; cp_parser_check_type_definition (called by
19030 cp_parser_class_specifier) will then emit an error if a type is
19031 defined in the type-id. */
19032 if (parser
->num_template_parameter_lists
)
19034 saved_message
= parser
->type_definition_forbidden_message
;
19035 parser
->type_definition_forbidden_message
=
19036 G_("types may not be defined in alias template declarations");
19039 type
= cp_parser_type_id (parser
);
19041 /* Restore the error message if need be. */
19042 if (parser
->num_template_parameter_lists
)
19043 parser
->type_definition_forbidden_message
= saved_message
;
19045 if (type
== error_mark_node
19046 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19048 cp_parser_skip_to_end_of_block_or_statement (parser
);
19049 return error_mark_node
;
19052 /* A typedef-name can also be introduced by an alias-declaration. The
19053 identifier following the using keyword becomes a typedef-name. It has
19054 the same semantics as if it were introduced by the typedef
19055 specifier. In particular, it does not define a new type and it shall
19056 not appear in the type-id. */
19058 clear_decl_specs (&decl_specs
);
19059 decl_specs
.type
= type
;
19060 if (attributes
!= NULL_TREE
)
19062 decl_specs
.attributes
= attributes
;
19063 set_and_check_decl_spec_loc (&decl_specs
,
19067 set_and_check_decl_spec_loc (&decl_specs
,
19070 set_and_check_decl_spec_loc (&decl_specs
,
19074 if (parser
->num_template_parameter_lists
19075 && !cp_parser_check_template_parameters (parser
,
19076 /*num_templates=*/0,
19077 /*template_id*/false,
19079 /*declarator=*/NULL
))
19080 return error_mark_node
;
19082 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19083 declarator
->id_loc
= id_location
;
19085 member_p
= at_class_scope_p ();
19087 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19088 NULL_TREE
, attributes
);
19090 decl
= start_decl (declarator
, &decl_specs
, 0,
19091 attributes
, NULL_TREE
, &pushed_scope
);
19092 if (decl
== error_mark_node
)
19095 // Attach constraints to the alias declaration.
19096 if (flag_concepts
&& current_template_parms
)
19098 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19099 tree constr
= build_constraints (reqs
, NULL_TREE
);
19100 set_constraints (decl
, constr
);
19103 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19106 pop_scope (pushed_scope
);
19108 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19109 added into the symbol table; otherwise, return the TYPE_DECL. */
19110 if (DECL_LANG_SPECIFIC (decl
)
19111 && DECL_TEMPLATE_INFO (decl
)
19112 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19114 decl
= DECL_TI_TEMPLATE (decl
);
19116 check_member_template (decl
);
19122 /* Parse a using-directive.
19125 using namespace :: [opt] nested-name-specifier [opt]
19126 namespace-name ; */
19129 cp_parser_using_directive (cp_parser
* parser
)
19131 tree namespace_decl
;
19134 /* Look for the `using' keyword. */
19135 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19136 /* And the `namespace' keyword. */
19137 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19138 /* Look for the optional `::' operator. */
19139 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19140 /* And the optional nested-name-specifier. */
19141 cp_parser_nested_name_specifier_opt (parser
,
19142 /*typename_keyword_p=*/false,
19143 /*check_dependency_p=*/true,
19145 /*is_declaration=*/true);
19146 /* Get the namespace being used. */
19147 namespace_decl
= cp_parser_namespace_name (parser
);
19148 /* And any specified attributes. */
19149 attribs
= cp_parser_attributes_opt (parser
);
19151 /* Update the symbol table. */
19152 if (namespace_bindings_p ())
19153 finish_namespace_using_directive (namespace_decl
, attribs
);
19155 finish_local_using_directive (namespace_decl
, attribs
);
19157 /* Look for the final `;'. */
19158 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19161 /* Parse an asm-definition.
19164 asm ( string-literal ) ;
19169 asm volatile [opt] ( string-literal ) ;
19170 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19171 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19172 : asm-operand-list [opt] ) ;
19173 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19174 : asm-operand-list [opt]
19175 : asm-clobber-list [opt] ) ;
19176 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19177 : asm-clobber-list [opt]
19178 : asm-goto-list ) ; */
19181 cp_parser_asm_definition (cp_parser
* parser
)
19184 tree outputs
= NULL_TREE
;
19185 tree inputs
= NULL_TREE
;
19186 tree clobbers
= NULL_TREE
;
19187 tree labels
= NULL_TREE
;
19189 bool volatile_p
= false;
19190 bool extended_p
= false;
19191 bool invalid_inputs_p
= false;
19192 bool invalid_outputs_p
= false;
19193 bool goto_p
= false;
19194 required_token missing
= RT_NONE
;
19196 /* Look for the `asm' keyword. */
19197 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19199 if (parser
->in_function_body
19200 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19202 error ("%<asm%> in %<constexpr%> function");
19203 cp_function_chain
->invalid_constexpr
= true;
19206 /* See if the next token is `volatile'. */
19207 if (cp_parser_allow_gnu_extensions_p (parser
)
19208 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19210 /* Remember that we saw the `volatile' keyword. */
19212 /* Consume the token. */
19213 cp_lexer_consume_token (parser
->lexer
);
19215 if (cp_parser_allow_gnu_extensions_p (parser
)
19216 && parser
->in_function_body
19217 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19219 /* Remember that we saw the `goto' keyword. */
19221 /* Consume the token. */
19222 cp_lexer_consume_token (parser
->lexer
);
19224 /* Look for the opening `('. */
19225 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19227 /* Look for the string. */
19228 string
= cp_parser_string_literal (parser
, false, false);
19229 if (string
== error_mark_node
)
19231 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19232 /*consume_paren=*/true);
19236 /* If we're allowing GNU extensions, check for the extended assembly
19237 syntax. Unfortunately, the `:' tokens need not be separated by
19238 a space in C, and so, for compatibility, we tolerate that here
19239 too. Doing that means that we have to treat the `::' operator as
19241 if (cp_parser_allow_gnu_extensions_p (parser
)
19242 && parser
->in_function_body
19243 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19244 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19246 bool inputs_p
= false;
19247 bool clobbers_p
= false;
19248 bool labels_p
= false;
19250 /* The extended syntax was used. */
19253 /* Look for outputs. */
19254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19256 /* Consume the `:'. */
19257 cp_lexer_consume_token (parser
->lexer
);
19258 /* Parse the output-operands. */
19259 if (cp_lexer_next_token_is_not (parser
->lexer
,
19261 && cp_lexer_next_token_is_not (parser
->lexer
,
19263 && cp_lexer_next_token_is_not (parser
->lexer
,
19267 outputs
= cp_parser_asm_operand_list (parser
);
19268 if (outputs
== error_mark_node
)
19269 invalid_outputs_p
= true;
19272 /* If the next token is `::', there are no outputs, and the
19273 next token is the beginning of the inputs. */
19274 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19275 /* The inputs are coming next. */
19278 /* Look for inputs. */
19280 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19282 /* Consume the `:' or `::'. */
19283 cp_lexer_consume_token (parser
->lexer
);
19284 /* Parse the output-operands. */
19285 if (cp_lexer_next_token_is_not (parser
->lexer
,
19287 && cp_lexer_next_token_is_not (parser
->lexer
,
19289 && cp_lexer_next_token_is_not (parser
->lexer
,
19292 inputs
= cp_parser_asm_operand_list (parser
);
19293 if (inputs
== error_mark_node
)
19294 invalid_inputs_p
= true;
19297 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19298 /* The clobbers are coming next. */
19301 /* Look for clobbers. */
19303 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19306 /* Consume the `:' or `::'. */
19307 cp_lexer_consume_token (parser
->lexer
);
19308 /* Parse the clobbers. */
19309 if (cp_lexer_next_token_is_not (parser
->lexer
,
19311 && cp_lexer_next_token_is_not (parser
->lexer
,
19313 clobbers
= cp_parser_asm_clobber_list (parser
);
19316 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19317 /* The labels are coming next. */
19320 /* Look for labels. */
19322 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19325 /* Consume the `:' or `::'. */
19326 cp_lexer_consume_token (parser
->lexer
);
19327 /* Parse the labels. */
19328 labels
= cp_parser_asm_label_list (parser
);
19331 if (goto_p
&& !labels_p
)
19332 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19335 missing
= RT_COLON_SCOPE
;
19337 /* Look for the closing `)'. */
19338 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19339 missing
? missing
: RT_CLOSE_PAREN
))
19340 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19341 /*consume_paren=*/true);
19342 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19344 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19346 /* Create the ASM_EXPR. */
19347 if (parser
->in_function_body
)
19349 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19350 inputs
, clobbers
, labels
);
19351 /* If the extended syntax was not used, mark the ASM_EXPR. */
19354 tree temp
= asm_stmt
;
19355 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19356 temp
= TREE_OPERAND (temp
, 0);
19358 ASM_INPUT_P (temp
) = 1;
19362 symtab
->finalize_toplevel_asm (string
);
19366 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19367 type that comes from the decl-specifier-seq. */
19370 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19372 for (cp_declarator
*d
= declarator
; d
;)
19382 if (TYPE_PTRMEMFUNC_P (type
))
19383 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19384 type
= TREE_TYPE (type
);
19392 /* Declarators [gram.dcl.decl] */
19394 /* Parse an init-declarator.
19397 declarator initializer [opt]
19402 declarator asm-specification [opt] attributes [opt] initializer [opt]
19404 function-definition:
19405 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19407 decl-specifier-seq [opt] declarator function-try-block
19411 function-definition:
19412 __extension__ function-definition
19416 function-definition:
19417 decl-specifier-seq [opt] declarator function-transaction-block
19419 The DECL_SPECIFIERS apply to this declarator. Returns a
19420 representation of the entity declared. If MEMBER_P is TRUE, then
19421 this declarator appears in a class scope. The new DECL created by
19422 this declarator is returned.
19424 The CHECKS are access checks that should be performed once we know
19425 what entity is being declared (and, therefore, what classes have
19428 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19429 for a function-definition here as well. If the declarator is a
19430 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19431 be TRUE upon return. By that point, the function-definition will
19432 have been completely parsed.
19434 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19437 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19438 parsed declaration if it is an uninitialized single declarator not followed
19439 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19440 if present, will not be consumed. If returned, this declarator will be
19441 created with SD_INITIALIZED but will not call cp_finish_decl.
19443 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19444 and there is an initializer, the pointed location_t is set to the
19445 location of the '=' or `(', or '{' in C++11 token introducing the
19449 cp_parser_init_declarator (cp_parser
* parser
,
19450 cp_decl_specifier_seq
*decl_specifiers
,
19451 vec
<deferred_access_check
, va_gc
> *checks
,
19452 bool function_definition_allowed_p
,
19454 int declares_class_or_enum
,
19455 bool* function_definition_p
,
19456 tree
* maybe_range_for_decl
,
19457 location_t
* init_loc
,
19460 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19461 *attributes_start_token
= NULL
;
19462 cp_declarator
*declarator
;
19463 tree prefix_attributes
;
19464 tree attributes
= NULL
;
19465 tree asm_specification
;
19467 tree decl
= NULL_TREE
;
19469 int is_initialized
;
19470 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19471 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19473 enum cpp_ttype initialization_kind
;
19474 bool is_direct_init
= false;
19475 bool is_non_constant_init
;
19476 int ctor_dtor_or_conv_p
;
19477 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19478 tree pushed_scope
= NULL_TREE
;
19479 bool range_for_decl_p
= false;
19480 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19481 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19483 /* Gather the attributes that were provided with the
19484 decl-specifiers. */
19485 prefix_attributes
= decl_specifiers
->attributes
;
19487 /* Assume that this is not the declarator for a function
19489 if (function_definition_p
)
19490 *function_definition_p
= false;
19492 /* Default arguments are only permitted for function parameters. */
19493 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19494 parser
->default_arg_ok_p
= false;
19496 /* Defer access checks while parsing the declarator; we cannot know
19497 what names are accessible until we know what is being
19499 resume_deferring_access_checks ();
19501 token
= cp_lexer_peek_token (parser
->lexer
);
19503 /* Parse the declarator. */
19505 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19506 &ctor_dtor_or_conv_p
,
19507 /*parenthesized_p=*/NULL
,
19508 member_p
, friend_p
);
19509 /* Gather up the deferred checks. */
19510 stop_deferring_access_checks ();
19512 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19514 /* If the DECLARATOR was erroneous, there's no need to go
19516 if (declarator
== cp_error_declarator
)
19517 return error_mark_node
;
19519 /* Check that the number of template-parameter-lists is OK. */
19520 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19522 return error_mark_node
;
19524 if (declares_class_or_enum
& 2)
19525 cp_parser_check_for_definition_in_return_type (declarator
,
19526 decl_specifiers
->type
,
19527 decl_specifiers
->locations
[ds_type_spec
]);
19529 /* Figure out what scope the entity declared by the DECLARATOR is
19530 located in. `grokdeclarator' sometimes changes the scope, so
19531 we compute it now. */
19532 scope
= get_scope_of_declarator (declarator
);
19534 /* Perform any lookups in the declared type which were thought to be
19535 dependent, but are not in the scope of the declarator. */
19536 decl_specifiers
->type
19537 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19539 /* If we're allowing GNU extensions, look for an
19540 asm-specification. */
19541 if (cp_parser_allow_gnu_extensions_p (parser
))
19543 /* Look for an asm-specification. */
19544 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19545 asm_specification
= cp_parser_asm_specification_opt (parser
);
19548 asm_specification
= NULL_TREE
;
19550 /* Look for attributes. */
19551 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19552 attributes
= cp_parser_attributes_opt (parser
);
19554 /* Peek at the next token. */
19555 token
= cp_lexer_peek_token (parser
->lexer
);
19557 bool bogus_implicit_tmpl
= false;
19559 if (function_declarator_p (declarator
))
19561 /* Handle C++17 deduction guides. */
19562 if (!decl_specifiers
->type
19563 && ctor_dtor_or_conv_p
<= 0
19564 && cxx_dialect
>= cxx17
)
19566 cp_declarator
*id
= get_id_declarator (declarator
);
19567 tree name
= id
->u
.id
.unqualified_name
;
19568 parser
->scope
= id
->u
.id
.qualifying_scope
;
19569 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19571 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19572 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19574 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19575 id
->u
.id
.sfk
= sfk_deduction_guide
;
19576 ctor_dtor_or_conv_p
= 1;
19580 /* Check to see if the token indicates the start of a
19581 function-definition. */
19582 if (cp_parser_token_starts_function_definition_p (token
))
19584 if (!function_definition_allowed_p
)
19586 /* If a function-definition should not appear here, issue an
19588 cp_parser_error (parser
,
19589 "a function-definition is not allowed here");
19590 return error_mark_node
;
19593 location_t func_brace_location
19594 = cp_lexer_peek_token (parser
->lexer
)->location
;
19596 /* Neither attributes nor an asm-specification are allowed
19597 on a function-definition. */
19598 if (asm_specification
)
19599 error_at (asm_spec_start_token
->location
,
19600 "an asm-specification is not allowed "
19601 "on a function-definition");
19603 error_at (attributes_start_token
->location
,
19604 "attributes are not allowed "
19605 "on a function-definition");
19606 /* This is a function-definition. */
19607 *function_definition_p
= true;
19609 /* Parse the function definition. */
19611 decl
= cp_parser_save_member_function_body (parser
,
19614 prefix_attributes
);
19617 (cp_parser_function_definition_from_specifiers_and_declarator
19618 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19620 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19622 /* This is where the prologue starts... */
19623 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19624 = func_brace_location
;
19630 else if (parser
->fully_implicit_function_template_p
)
19632 /* A non-template declaration involving a function parameter list
19633 containing an implicit template parameter will be made into a
19634 template. If the resulting declaration is not going to be an
19635 actual function then finish the template scope here to prevent it.
19636 An error message will be issued once we have a decl to talk about.
19638 FIXME probably we should do type deduction rather than create an
19639 implicit template, but the standard currently doesn't allow it. */
19640 bogus_implicit_tmpl
= true;
19641 finish_fully_implicit_template (parser
, NULL_TREE
);
19646 Only in function declarations for constructors, destructors, type
19647 conversions, and deduction guides can the decl-specifier-seq be omitted.
19649 We explicitly postpone this check past the point where we handle
19650 function-definitions because we tolerate function-definitions
19651 that are missing their return types in some modes. */
19652 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19654 cp_parser_error (parser
,
19655 "expected constructor, destructor, or type conversion");
19656 return error_mark_node
;
19659 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19660 if (token
->type
== CPP_EQ
19661 || token
->type
== CPP_OPEN_PAREN
19662 || token
->type
== CPP_OPEN_BRACE
)
19664 is_initialized
= SD_INITIALIZED
;
19665 initialization_kind
= token
->type
;
19666 if (maybe_range_for_decl
)
19667 *maybe_range_for_decl
= error_mark_node
;
19668 tmp_init_loc
= token
->location
;
19669 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19670 *init_loc
= tmp_init_loc
;
19672 if (token
->type
== CPP_EQ
19673 && function_declarator_p (declarator
))
19675 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19676 if (t2
->keyword
== RID_DEFAULT
)
19677 is_initialized
= SD_DEFAULTED
;
19678 else if (t2
->keyword
== RID_DELETE
)
19679 is_initialized
= SD_DELETED
;
19684 /* If the init-declarator isn't initialized and isn't followed by a
19685 `,' or `;', it's not a valid init-declarator. */
19686 if (token
->type
!= CPP_COMMA
19687 && token
->type
!= CPP_SEMICOLON
)
19689 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19690 range_for_decl_p
= true;
19693 if (!maybe_range_for_decl
)
19694 cp_parser_error (parser
, "expected initializer");
19695 return error_mark_node
;
19698 is_initialized
= SD_UNINITIALIZED
;
19699 initialization_kind
= CPP_EOF
;
19702 /* Because start_decl has side-effects, we should only call it if we
19703 know we're going ahead. By this point, we know that we cannot
19704 possibly be looking at any other construct. */
19705 cp_parser_commit_to_tentative_parse (parser
);
19707 /* Enter the newly declared entry in the symbol table. If we're
19708 processing a declaration in a class-specifier, we wait until
19709 after processing the initializer. */
19712 if (parser
->in_unbraced_linkage_specification_p
)
19713 decl_specifiers
->storage_class
= sc_extern
;
19714 decl
= start_decl (declarator
, decl_specifiers
,
19715 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19716 attributes
, prefix_attributes
, &pushed_scope
);
19717 cp_finalize_omp_declare_simd (parser
, decl
);
19718 cp_finalize_oacc_routine (parser
, decl
, false);
19719 /* Adjust location of decl if declarator->id_loc is more appropriate:
19720 set, and decl wasn't merged with another decl, in which case its
19721 location would be different from input_location, and more accurate. */
19723 && declarator
->id_loc
!= UNKNOWN_LOCATION
19724 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19725 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19728 /* Enter the SCOPE. That way unqualified names appearing in the
19729 initializer will be looked up in SCOPE. */
19730 pushed_scope
= push_scope (scope
);
19732 /* Perform deferred access control checks, now that we know in which
19733 SCOPE the declared entity resides. */
19734 if (!member_p
&& decl
)
19736 tree saved_current_function_decl
= NULL_TREE
;
19738 /* If the entity being declared is a function, pretend that we
19739 are in its scope. If it is a `friend', it may have access to
19740 things that would not otherwise be accessible. */
19741 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19743 saved_current_function_decl
= current_function_decl
;
19744 current_function_decl
= decl
;
19747 /* Perform access checks for template parameters. */
19748 cp_parser_perform_template_parameter_access_checks (checks
);
19750 /* Perform the access control checks for the declarator and the
19751 decl-specifiers. */
19752 perform_deferred_access_checks (tf_warning_or_error
);
19754 /* Restore the saved value. */
19755 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19756 current_function_decl
= saved_current_function_decl
;
19759 /* Parse the initializer. */
19760 initializer
= NULL_TREE
;
19761 is_direct_init
= false;
19762 is_non_constant_init
= true;
19763 if (is_initialized
)
19765 if (function_declarator_p (declarator
))
19767 if (initialization_kind
== CPP_EQ
)
19768 initializer
= cp_parser_pure_specifier (parser
);
19771 /* If the declaration was erroneous, we don't really
19772 know what the user intended, so just silently
19773 consume the initializer. */
19774 if (decl
!= error_mark_node
)
19775 error_at (tmp_init_loc
, "initializer provided for function");
19776 cp_parser_skip_to_closing_parenthesis (parser
,
19777 /*recovering=*/true,
19778 /*or_comma=*/false,
19779 /*consume_paren=*/true);
19784 /* We want to record the extra mangling scope for in-class
19785 initializers of class members and initializers of static data
19786 member templates. The former involves deferring
19787 parsing of the initializer until end of class as with default
19788 arguments. So right here we only handle the latter. */
19789 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19790 start_lambda_scope (decl
);
19791 initializer
= cp_parser_initializer (parser
,
19793 &is_non_constant_init
);
19794 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19795 finish_lambda_scope ();
19796 if (initializer
== error_mark_node
)
19797 cp_parser_skip_to_end_of_statement (parser
);
19801 /* The old parser allows attributes to appear after a parenthesized
19802 initializer. Mark Mitchell proposed removing this functionality
19803 on the GCC mailing lists on 2002-08-13. This parser accepts the
19804 attributes -- but ignores them. Made a permerror in GCC 8. */
19805 if (cp_parser_allow_gnu_extensions_p (parser
)
19806 && initialization_kind
== CPP_OPEN_PAREN
19807 && cp_parser_attributes_opt (parser
)
19808 && permerror (input_location
,
19809 "attributes after parenthesized initializer ignored"))
19812 if (flag_permissive
&& !hint
)
19815 inform (input_location
,
19816 "this flexibility is deprecated and will be removed");
19820 /* And now complain about a non-function implicit template. */
19821 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19822 error_at (DECL_SOURCE_LOCATION (decl
),
19823 "non-function %qD declared as implicit template", decl
);
19825 /* For an in-class declaration, use `grokfield' to create the
19831 pop_scope (pushed_scope
);
19832 pushed_scope
= NULL_TREE
;
19834 decl
= grokfield (declarator
, decl_specifiers
,
19835 initializer
, !is_non_constant_init
,
19836 /*asmspec=*/NULL_TREE
,
19837 attr_chainon (attributes
, prefix_attributes
));
19838 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19839 cp_parser_save_default_args (parser
, decl
);
19840 cp_finalize_omp_declare_simd (parser
, decl
);
19841 cp_finalize_oacc_routine (parser
, decl
, false);
19844 /* Finish processing the declaration. But, skip member
19846 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19848 cp_finish_decl (decl
,
19849 initializer
, !is_non_constant_init
,
19851 /* If the initializer is in parentheses, then this is
19852 a direct-initialization, which means that an
19853 `explicit' constructor is OK. Otherwise, an
19854 `explicit' constructor cannot be used. */
19855 ((is_direct_init
|| !is_initialized
)
19856 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19858 else if ((cxx_dialect
!= cxx98
) && friend_p
19859 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19860 /* Core issue #226 (C++0x only): A default template-argument
19861 shall not be specified in a friend class template
19863 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19864 /*is_partial=*/false, /*is_friend_decl=*/1);
19866 if (!friend_p
&& pushed_scope
)
19867 pop_scope (pushed_scope
);
19869 if (function_declarator_p (declarator
)
19870 && parser
->fully_implicit_function_template_p
)
19873 decl
= finish_fully_implicit_template (parser
, decl
);
19875 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19878 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19879 && type_uses_auto (decl_specifiers
->type
))
19880 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19885 /* Parse a declarator.
19889 ptr-operator declarator
19891 abstract-declarator:
19892 ptr-operator abstract-declarator [opt]
19893 direct-abstract-declarator
19898 attributes [opt] direct-declarator
19899 attributes [opt] ptr-operator declarator
19901 abstract-declarator:
19902 attributes [opt] ptr-operator abstract-declarator [opt]
19903 attributes [opt] direct-abstract-declarator
19905 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19906 detect constructors, destructors, deduction guides, or conversion operators.
19907 It is set to -1 if the declarator is a name, and +1 if it is a
19908 function. Otherwise it is set to zero. Usually you just want to
19909 test for >0, but internally the negative value is used.
19911 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19912 a decl-specifier-seq unless it declares a constructor, destructor,
19913 or conversion. It might seem that we could check this condition in
19914 semantic analysis, rather than parsing, but that makes it difficult
19915 to handle something like `f()'. We want to notice that there are
19916 no decl-specifiers, and therefore realize that this is an
19917 expression, not a declaration.)
19919 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19920 the declarator is a direct-declarator of the form "(...)".
19922 MEMBER_P is true iff this declarator is a member-declarator.
19924 FRIEND_P is true iff this declarator is a friend. */
19926 static cp_declarator
*
19927 cp_parser_declarator (cp_parser
* parser
,
19928 cp_parser_declarator_kind dcl_kind
,
19929 int* ctor_dtor_or_conv_p
,
19930 bool* parenthesized_p
,
19931 bool member_p
, bool friend_p
)
19933 cp_declarator
*declarator
;
19934 enum tree_code code
;
19935 cp_cv_quals cv_quals
;
19937 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19939 /* Assume this is not a constructor, destructor, or type-conversion
19941 if (ctor_dtor_or_conv_p
)
19942 *ctor_dtor_or_conv_p
= 0;
19944 if (cp_parser_allow_gnu_extensions_p (parser
))
19945 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19947 /* Check for the ptr-operator production. */
19948 cp_parser_parse_tentatively (parser
);
19949 /* Parse the ptr-operator. */
19950 code
= cp_parser_ptr_operator (parser
,
19955 /* If that worked, then we have a ptr-operator. */
19956 if (cp_parser_parse_definitely (parser
))
19958 /* If a ptr-operator was found, then this declarator was not
19960 if (parenthesized_p
)
19961 *parenthesized_p
= true;
19962 /* The dependent declarator is optional if we are parsing an
19963 abstract-declarator. */
19964 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19965 cp_parser_parse_tentatively (parser
);
19967 /* Parse the dependent declarator. */
19968 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19969 /*ctor_dtor_or_conv_p=*/NULL
,
19970 /*parenthesized_p=*/NULL
,
19971 /*member_p=*/false,
19974 /* If we are parsing an abstract-declarator, we must handle the
19975 case where the dependent declarator is absent. */
19976 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19977 && !cp_parser_parse_definitely (parser
))
19980 declarator
= cp_parser_make_indirect_declarator
19981 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19983 /* Everything else is a direct-declarator. */
19986 if (parenthesized_p
)
19987 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19989 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19990 ctor_dtor_or_conv_p
,
19991 member_p
, friend_p
);
19994 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19995 declarator
->attributes
= gnu_attributes
;
19999 /* Parse a direct-declarator or direct-abstract-declarator.
20003 direct-declarator ( parameter-declaration-clause )
20004 cv-qualifier-seq [opt]
20005 ref-qualifier [opt]
20006 exception-specification [opt]
20007 direct-declarator [ constant-expression [opt] ]
20010 direct-abstract-declarator:
20011 direct-abstract-declarator [opt]
20012 ( parameter-declaration-clause )
20013 cv-qualifier-seq [opt]
20014 ref-qualifier [opt]
20015 exception-specification [opt]
20016 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20017 ( abstract-declarator )
20019 Returns a representation of the declarator. DCL_KIND is
20020 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20021 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20022 we are parsing a direct-declarator. It is
20023 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20024 of ambiguity we prefer an abstract declarator, as per
20025 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20026 as for cp_parser_declarator. */
20028 static cp_declarator
*
20029 cp_parser_direct_declarator (cp_parser
* parser
,
20030 cp_parser_declarator_kind dcl_kind
,
20031 int* ctor_dtor_or_conv_p
,
20032 bool member_p
, bool friend_p
)
20035 cp_declarator
*declarator
= NULL
;
20036 tree scope
= NULL_TREE
;
20037 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20038 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20040 tree pushed_scope
= NULL_TREE
;
20041 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20045 /* Peek at the next token. */
20046 token
= cp_lexer_peek_token (parser
->lexer
);
20047 if (token
->type
== CPP_OPEN_PAREN
)
20049 /* This is either a parameter-declaration-clause, or a
20050 parenthesized declarator. When we know we are parsing a
20051 named declarator, it must be a parenthesized declarator
20052 if FIRST is true. For instance, `(int)' is a
20053 parameter-declaration-clause, with an omitted
20054 direct-abstract-declarator. But `((*))', is a
20055 parenthesized abstract declarator. Finally, when T is a
20056 template parameter `(T)' is a
20057 parameter-declaration-clause, and not a parenthesized
20060 We first try and parse a parameter-declaration-clause,
20061 and then try a nested declarator (if FIRST is true).
20063 It is not an error for it not to be a
20064 parameter-declaration-clause, even when FIRST is
20070 The first is the declaration of a function while the
20071 second is the definition of a variable, including its
20074 Having seen only the parenthesis, we cannot know which of
20075 these two alternatives should be selected. Even more
20076 complex are examples like:
20081 The former is a function-declaration; the latter is a
20082 variable initialization.
20084 Thus again, we try a parameter-declaration-clause, and if
20085 that fails, we back out and return. */
20087 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20090 bool is_declarator
= false;
20094 /* In a member-declarator, the only valid interpretation
20095 of a parenthesis is the start of a
20096 parameter-declaration-clause. (It is invalid to
20097 initialize a static data member with a parenthesized
20098 initializer; only the "=" form of initialization is
20101 cp_parser_parse_tentatively (parser
);
20103 /* Consume the `('. */
20104 matching_parens parens
;
20105 parens
.consume_open (parser
);
20108 /* If this is going to be an abstract declarator, we're
20109 in a declarator and we can't have default args. */
20110 parser
->default_arg_ok_p
= false;
20111 parser
->in_declarator_p
= true;
20114 begin_scope (sk_function_parms
, NULL_TREE
);
20116 /* Parse the parameter-declaration-clause. */
20117 params
= cp_parser_parameter_declaration_clause (parser
);
20119 /* Consume the `)'. */
20120 parens
.require_close (parser
);
20122 /* If all went well, parse the cv-qualifier-seq,
20123 ref-qualifier and the exception-specification. */
20124 if (member_p
|| cp_parser_parse_definitely (parser
))
20126 cp_cv_quals cv_quals
;
20127 cp_virt_specifiers virt_specifiers
;
20128 cp_ref_qualifier ref_qual
;
20129 tree exception_specification
;
20132 bool memfn
= (member_p
|| (pushed_scope
20133 && CLASS_TYPE_P (pushed_scope
)));
20135 is_declarator
= true;
20137 if (ctor_dtor_or_conv_p
)
20138 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20141 /* Parse the cv-qualifier-seq. */
20142 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20143 /* Parse the ref-qualifier. */
20144 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20145 /* Parse the tx-qualifier. */
20146 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20147 /* And the exception-specification. */
20148 exception_specification
20149 = cp_parser_exception_specification_opt (parser
);
20151 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20153 /* In here, we handle cases where attribute is used after
20154 the function declaration. For example:
20155 void func (int x) __attribute__((vector(..))); */
20156 tree gnu_attrs
= NULL_TREE
;
20157 tree requires_clause
= NULL_TREE
;
20158 late_return
= (cp_parser_late_return_type_opt
20159 (parser
, declarator
, requires_clause
,
20160 memfn
? cv_quals
: -1));
20162 /* Parse the virt-specifier-seq. */
20163 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20165 /* Create the function-declarator. */
20166 declarator
= make_call_declarator (declarator
,
20172 exception_specification
,
20175 declarator
->std_attributes
= attrs
;
20176 declarator
->attributes
= gnu_attrs
;
20177 /* Any subsequent parameter lists are to do with
20178 return type, so are not those of the declared
20180 parser
->default_arg_ok_p
= false;
20183 /* Remove the function parms from scope. */
20184 pop_bindings_and_leave_scope ();
20187 /* Repeat the main loop. */
20191 /* If this is the first, we can try a parenthesized
20195 bool saved_in_type_id_in_expr_p
;
20197 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20198 parser
->in_declarator_p
= saved_in_declarator_p
;
20200 open_paren
= token
;
20201 /* Consume the `('. */
20202 matching_parens parens
;
20203 parens
.consume_open (parser
);
20204 /* Parse the nested declarator. */
20205 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20206 parser
->in_type_id_in_expr_p
= true;
20208 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20209 /*parenthesized_p=*/NULL
,
20210 member_p
, friend_p
);
20211 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20213 /* Expect a `)'. */
20214 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20215 if (!parens
.require_close (parser
))
20216 declarator
= cp_error_declarator
;
20217 if (declarator
== cp_error_declarator
)
20220 goto handle_declarator
;
20222 /* Otherwise, we must be done. */
20226 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20227 && token
->type
== CPP_OPEN_SQUARE
20228 && !cp_next_tokens_can_be_attribute_p (parser
))
20230 /* Parse an array-declarator. */
20231 tree bounds
, attrs
;
20233 if (ctor_dtor_or_conv_p
)
20234 *ctor_dtor_or_conv_p
= 0;
20238 parser
->default_arg_ok_p
= false;
20239 parser
->in_declarator_p
= true;
20240 /* Consume the `['. */
20241 cp_lexer_consume_token (parser
->lexer
);
20242 /* Peek at the next token. */
20243 token
= cp_lexer_peek_token (parser
->lexer
);
20244 /* If the next token is `]', then there is no
20245 constant-expression. */
20246 if (token
->type
!= CPP_CLOSE_SQUARE
)
20248 bool non_constant_p
;
20250 = cp_parser_constant_expression (parser
,
20251 /*allow_non_constant=*/true,
20253 if (!non_constant_p
)
20255 else if (error_operand_p (bounds
))
20256 /* Already gave an error. */;
20257 else if (!parser
->in_function_body
20258 || current_binding_level
->kind
== sk_function_parms
)
20260 /* Normally, the array bound must be an integral constant
20261 expression. However, as an extension, we allow VLAs
20262 in function scopes as long as they aren't part of a
20263 parameter declaration. */
20264 cp_parser_error (parser
,
20265 "array bound is not an integer constant");
20266 bounds
= error_mark_node
;
20268 else if (processing_template_decl
20269 && !type_dependent_expression_p (bounds
))
20271 /* Remember this wasn't a constant-expression. */
20272 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20273 TREE_SIDE_EFFECTS (bounds
) = 1;
20277 bounds
= NULL_TREE
;
20278 /* Look for the closing `]'. */
20279 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20281 declarator
= cp_error_declarator
;
20285 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20286 declarator
= make_array_declarator (declarator
, bounds
);
20287 declarator
->std_attributes
= attrs
;
20289 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20292 tree qualifying_scope
;
20293 tree unqualified_name
;
20295 special_function_kind sfk
;
20297 bool pack_expansion_p
= false;
20298 cp_token
*declarator_id_start_token
;
20300 /* Parse a declarator-id */
20301 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20304 cp_parser_parse_tentatively (parser
);
20306 /* If we see an ellipsis, we should be looking at a
20308 if (token
->type
== CPP_ELLIPSIS
)
20310 /* Consume the `...' */
20311 cp_lexer_consume_token (parser
->lexer
);
20313 pack_expansion_p
= true;
20317 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20319 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20320 qualifying_scope
= parser
->scope
;
20325 if (!unqualified_name
&& pack_expansion_p
)
20327 /* Check whether an error occurred. */
20328 okay
= !cp_parser_error_occurred (parser
);
20330 /* We already consumed the ellipsis to mark a
20331 parameter pack, but we have no way to report it,
20332 so abort the tentative parse. We will be exiting
20333 immediately anyway. */
20334 cp_parser_abort_tentative_parse (parser
);
20337 okay
= cp_parser_parse_definitely (parser
);
20340 unqualified_name
= error_mark_node
;
20341 else if (unqualified_name
20342 && (qualifying_scope
20343 || (!identifier_p (unqualified_name
))))
20345 cp_parser_error (parser
, "expected unqualified-id");
20346 unqualified_name
= error_mark_node
;
20350 if (!unqualified_name
)
20352 if (unqualified_name
== error_mark_node
)
20354 declarator
= cp_error_declarator
;
20355 pack_expansion_p
= false;
20356 declarator
->parameter_pack_p
= false;
20360 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20362 if (qualifying_scope
&& at_namespace_scope_p ()
20363 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20365 /* In the declaration of a member of a template class
20366 outside of the class itself, the SCOPE will sometimes
20367 be a TYPENAME_TYPE. For example, given:
20369 template <typename T>
20370 int S<T>::R::i = 3;
20372 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20373 this context, we must resolve S<T>::R to an ordinary
20374 type, rather than a typename type.
20376 The reason we normally avoid resolving TYPENAME_TYPEs
20377 is that a specialization of `S' might render
20378 `S<T>::R' not a type. However, if `S' is
20379 specialized, then this `i' will not be used, so there
20380 is no harm in resolving the types here. */
20383 /* Resolve the TYPENAME_TYPE. */
20384 type
= resolve_typename_type (qualifying_scope
,
20385 /*only_current_p=*/false);
20386 /* If that failed, the declarator is invalid. */
20387 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20389 if (typedef_variant_p (type
))
20390 error_at (declarator_id_start_token
->location
,
20391 "cannot define member of dependent typedef "
20394 error_at (declarator_id_start_token
->location
,
20395 "%<%T::%E%> is not a type",
20396 TYPE_CONTEXT (qualifying_scope
),
20397 TYPE_IDENTIFIER (qualifying_scope
));
20399 qualifying_scope
= type
;
20404 if (unqualified_name
)
20408 if (qualifying_scope
20409 && CLASS_TYPE_P (qualifying_scope
))
20410 class_type
= qualifying_scope
;
20412 class_type
= current_class_type
;
20414 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20416 tree name_type
= TREE_TYPE (unqualified_name
);
20418 if (!class_type
|| !same_type_p (name_type
, class_type
))
20420 /* We do not attempt to print the declarator
20421 here because we do not have enough
20422 information about its original syntactic
20424 cp_parser_error (parser
, "invalid declarator");
20425 declarator
= cp_error_declarator
;
20428 else if (qualifying_scope
20429 && CLASSTYPE_USE_TEMPLATE (name_type
))
20431 error_at (declarator_id_start_token
->location
,
20432 "invalid use of constructor as a template");
20433 inform (declarator_id_start_token
->location
,
20434 "use %<%T::%D%> instead of %<%T::%D%> to "
20435 "name the constructor in a qualified name",
20437 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20438 class_type
, name_type
);
20439 declarator
= cp_error_declarator
;
20442 unqualified_name
= constructor_name (class_type
);
20447 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20448 sfk
= sfk_destructor
;
20449 else if (identifier_p (unqualified_name
)
20450 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20451 sfk
= sfk_conversion
;
20452 else if (/* There's no way to declare a constructor
20453 for an unnamed type, even if the type
20454 got a name for linkage purposes. */
20455 !TYPE_WAS_UNNAMED (class_type
)
20456 /* Handle correctly (c++/19200):
20470 friend void N::S();
20472 && (!friend_p
|| class_type
== qualifying_scope
)
20473 && constructor_name_p (unqualified_name
,
20475 sfk
= sfk_constructor
;
20476 else if (is_overloaded_fn (unqualified_name
)
20477 && DECL_CONSTRUCTOR_P (get_first_fn
20478 (unqualified_name
)))
20479 sfk
= sfk_constructor
;
20481 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20482 *ctor_dtor_or_conv_p
= -1;
20485 declarator
= make_id_declarator (qualifying_scope
,
20488 declarator
->std_attributes
= attrs
;
20489 declarator
->id_loc
= token
->location
;
20490 declarator
->parameter_pack_p
= pack_expansion_p
;
20492 if (pack_expansion_p
)
20493 maybe_warn_variadic_templates ();
20496 handle_declarator
:;
20497 scope
= get_scope_of_declarator (declarator
);
20500 /* Any names that appear after the declarator-id for a
20501 member are looked up in the containing scope. */
20502 if (at_function_scope_p ())
20504 /* But declarations with qualified-ids can't appear in a
20506 cp_parser_error (parser
, "qualified-id in declaration");
20507 declarator
= cp_error_declarator
;
20510 pushed_scope
= push_scope (scope
);
20512 parser
->in_declarator_p
= true;
20513 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20514 || (declarator
&& declarator
->kind
== cdk_id
))
20515 /* Default args are only allowed on function
20517 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20519 parser
->default_arg_ok_p
= false;
20528 /* For an abstract declarator, we might wind up with nothing at this
20529 point. That's an error; the declarator is not optional. */
20531 cp_parser_error (parser
, "expected declarator");
20532 else if (open_paren
)
20534 /* Record overly parenthesized declarator so we can give a
20535 diagnostic about confusing decl/expr disambiguation. */
20536 if (declarator
->kind
== cdk_array
)
20538 /* If the open and close parens are on different lines, this
20539 is probably a formatting thing, so ignore. */
20540 expanded_location open
= expand_location (open_paren
->location
);
20541 expanded_location close
= expand_location (close_paren
->location
);
20542 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20546 declarator
->parenthesized
= open_paren
->location
;
20549 /* If we entered a scope, we must exit it now. */
20551 pop_scope (pushed_scope
);
20553 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20554 parser
->in_declarator_p
= saved_in_declarator_p
;
20559 /* Parse a ptr-operator.
20562 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20563 * cv-qualifier-seq [opt]
20565 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20566 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20571 & cv-qualifier-seq [opt]
20573 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20574 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20575 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20576 filled in with the TYPE containing the member. *CV_QUALS is
20577 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20578 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20579 Note that the tree codes returned by this function have nothing
20580 to do with the types of trees that will be eventually be created
20581 to represent the pointer or reference type being parsed. They are
20582 just constants with suggestive names. */
20583 static enum tree_code
20584 cp_parser_ptr_operator (cp_parser
* parser
,
20586 cp_cv_quals
*cv_quals
,
20589 enum tree_code code
= ERROR_MARK
;
20591 tree attrs
= NULL_TREE
;
20593 /* Assume that it's not a pointer-to-member. */
20595 /* And that there are no cv-qualifiers. */
20596 *cv_quals
= TYPE_UNQUALIFIED
;
20598 /* Peek at the next token. */
20599 token
= cp_lexer_peek_token (parser
->lexer
);
20601 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20602 if (token
->type
== CPP_MULT
)
20603 code
= INDIRECT_REF
;
20604 else if (token
->type
== CPP_AND
)
20606 else if ((cxx_dialect
!= cxx98
) &&
20607 token
->type
== CPP_AND_AND
) /* C++0x only */
20608 code
= NON_LVALUE_EXPR
;
20610 if (code
!= ERROR_MARK
)
20612 /* Consume the `*', `&' or `&&'. */
20613 cp_lexer_consume_token (parser
->lexer
);
20615 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20616 `&', if we are allowing GNU extensions. (The only qualifier
20617 that can legally appear after `&' is `restrict', but that is
20618 enforced during semantic analysis. */
20619 if (code
== INDIRECT_REF
20620 || cp_parser_allow_gnu_extensions_p (parser
))
20621 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20623 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20624 if (attributes
!= NULL
)
20625 *attributes
= attrs
;
20629 /* Try the pointer-to-member case. */
20630 cp_parser_parse_tentatively (parser
);
20631 /* Look for the optional `::' operator. */
20632 cp_parser_global_scope_opt (parser
,
20633 /*current_scope_valid_p=*/false);
20634 /* Look for the nested-name specifier. */
20635 token
= cp_lexer_peek_token (parser
->lexer
);
20636 cp_parser_nested_name_specifier (parser
,
20637 /*typename_keyword_p=*/false,
20638 /*check_dependency_p=*/true,
20640 /*is_declaration=*/false);
20641 /* If we found it, and the next token is a `*', then we are
20642 indeed looking at a pointer-to-member operator. */
20643 if (!cp_parser_error_occurred (parser
)
20644 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20646 /* Indicate that the `*' operator was used. */
20647 code
= INDIRECT_REF
;
20649 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20650 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20651 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20652 error_at (token
->location
, "cannot form pointer to member of "
20653 "non-class %q#T", parser
->scope
);
20656 /* The type of which the member is a member is given by the
20658 *type
= parser
->scope
;
20659 /* The next name will not be qualified. */
20660 parser
->scope
= NULL_TREE
;
20661 parser
->qualifying_scope
= NULL_TREE
;
20662 parser
->object_scope
= NULL_TREE
;
20663 /* Look for optional c++11 attributes. */
20664 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20665 if (attributes
!= NULL
)
20666 *attributes
= attrs
;
20667 /* Look for the optional cv-qualifier-seq. */
20668 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20671 /* If that didn't work we don't have a ptr-operator. */
20672 if (!cp_parser_parse_definitely (parser
))
20673 cp_parser_error (parser
, "expected ptr-operator");
20679 /* Parse an (optional) cv-qualifier-seq.
20682 cv-qualifier cv-qualifier-seq [opt]
20693 Returns a bitmask representing the cv-qualifiers. */
20696 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20698 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20703 cp_cv_quals cv_qualifier
;
20705 /* Peek at the next token. */
20706 token
= cp_lexer_peek_token (parser
->lexer
);
20707 /* See if it's a cv-qualifier. */
20708 switch (token
->keyword
)
20711 cv_qualifier
= TYPE_QUAL_CONST
;
20715 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20719 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20723 cv_qualifier
= TYPE_UNQUALIFIED
;
20730 if (cv_quals
& cv_qualifier
)
20732 gcc_rich_location
richloc (token
->location
);
20733 richloc
.add_fixit_remove ();
20734 error_at (&richloc
, "duplicate cv-qualifier");
20735 cp_lexer_purge_token (parser
->lexer
);
20739 cp_lexer_consume_token (parser
->lexer
);
20740 cv_quals
|= cv_qualifier
;
20747 /* Parse an (optional) ref-qualifier
20753 Returns cp_ref_qualifier representing ref-qualifier. */
20755 static cp_ref_qualifier
20756 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20758 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20760 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20761 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20766 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20767 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20769 switch (token
->type
)
20772 curr_ref_qual
= REF_QUAL_LVALUE
;
20776 curr_ref_qual
= REF_QUAL_RVALUE
;
20780 curr_ref_qual
= REF_QUAL_NONE
;
20784 if (!curr_ref_qual
)
20788 error_at (token
->location
, "multiple ref-qualifiers");
20789 cp_lexer_purge_token (parser
->lexer
);
20793 ref_qual
= curr_ref_qual
;
20794 cp_lexer_consume_token (parser
->lexer
);
20801 /* Parse an optional tx-qualifier.
20805 transaction_safe_dynamic */
20808 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20810 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20811 if (token
->type
== CPP_NAME
)
20813 tree name
= token
->u
.value
;
20814 const char *p
= IDENTIFIER_POINTER (name
);
20815 const int len
= strlen ("transaction_safe");
20816 if (!strncmp (p
, "transaction_safe", len
))
20820 || !strcmp (p
, "_dynamic"))
20822 cp_lexer_consume_token (parser
->lexer
);
20825 error ("%qE requires %<-fgnu-tm%>", name
);
20836 /* Parse an (optional) virt-specifier-seq.
20838 virt-specifier-seq:
20839 virt-specifier virt-specifier-seq [opt]
20845 Returns a bitmask representing the virt-specifiers. */
20847 static cp_virt_specifiers
20848 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20850 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20855 cp_virt_specifiers virt_specifier
;
20857 /* Peek at the next token. */
20858 token
= cp_lexer_peek_token (parser
->lexer
);
20859 /* See if it's a virt-specifier-qualifier. */
20860 if (token
->type
!= CPP_NAME
)
20862 if (id_equal (token
->u
.value
, "override"))
20864 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20865 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20867 else if (id_equal (token
->u
.value
, "final"))
20869 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20870 virt_specifier
= VIRT_SPEC_FINAL
;
20872 else if (id_equal (token
->u
.value
, "__final"))
20874 virt_specifier
= VIRT_SPEC_FINAL
;
20879 if (virt_specifiers
& virt_specifier
)
20881 gcc_rich_location
richloc (token
->location
);
20882 richloc
.add_fixit_remove ();
20883 error_at (&richloc
, "duplicate virt-specifier");
20884 cp_lexer_purge_token (parser
->lexer
);
20888 cp_lexer_consume_token (parser
->lexer
);
20889 virt_specifiers
|= virt_specifier
;
20892 return virt_specifiers
;
20895 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20896 is in scope even though it isn't real. */
20899 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20903 if (current_class_ptr
)
20905 /* We don't clear this between NSDMIs. Is it already what we want? */
20906 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20907 if (DECL_P (current_class_ptr
)
20908 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20909 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20910 && cp_type_quals (type
) == quals
)
20914 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20915 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20916 current_class_ptr
= NULL_TREE
;
20918 = cp_build_fold_indirect_ref (this_parm
);
20919 current_class_ptr
= this_parm
;
20922 /* Return true iff our current scope is a non-static data member
20926 parsing_nsdmi (void)
20928 /* We recognize NSDMI context by the context-less 'this' pointer set up
20929 by the function above. */
20930 if (current_class_ptr
20931 && TREE_CODE (current_class_ptr
) == PARM_DECL
20932 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20937 /* Parse a late-specified return type, if any. This is not a separate
20938 non-terminal, but part of a function declarator, which looks like
20940 -> trailing-type-specifier-seq abstract-declarator(opt)
20942 Returns the type indicated by the type-id.
20944 In addition to this, parse any queued up #pragma omp declare simd
20945 clauses, and #pragma acc routine clauses.
20947 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20951 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20952 tree
& requires_clause
, cp_cv_quals quals
)
20955 tree type
= NULL_TREE
;
20956 bool declare_simd_p
= (parser
->omp_declare_simd
20958 && declarator
->kind
== cdk_id
);
20960 bool oacc_routine_p
= (parser
->oacc_routine
20962 && declarator
->kind
== cdk_id
);
20964 /* Peek at the next token. */
20965 token
= cp_lexer_peek_token (parser
->lexer
);
20966 /* A late-specified return type is indicated by an initial '->'. */
20967 if (token
->type
!= CPP_DEREF
20968 && token
->keyword
!= RID_REQUIRES
20969 && !(token
->type
== CPP_NAME
20970 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20971 && !(declare_simd_p
|| oacc_routine_p
))
20974 tree save_ccp
= current_class_ptr
;
20975 tree save_ccr
= current_class_ref
;
20978 /* DR 1207: 'this' is in scope in the trailing return type. */
20979 inject_this_parameter (current_class_type
, quals
);
20982 if (token
->type
== CPP_DEREF
)
20984 /* Consume the ->. */
20985 cp_lexer_consume_token (parser
->lexer
);
20987 type
= cp_parser_trailing_type_id (parser
);
20990 /* Function declarations may be followed by a trailing
20991 requires-clause. */
20992 requires_clause
= cp_parser_requires_clause_opt (parser
);
20994 if (declare_simd_p
)
20995 declarator
->attributes
20996 = cp_parser_late_parsing_omp_declare_simd (parser
,
20997 declarator
->attributes
);
20998 if (oacc_routine_p
)
20999 declarator
->attributes
21000 = cp_parser_late_parsing_oacc_routine (parser
,
21001 declarator
->attributes
);
21005 current_class_ptr
= save_ccp
;
21006 current_class_ref
= save_ccr
;
21012 /* Parse a declarator-id.
21016 :: [opt] nested-name-specifier [opt] type-name
21018 In the `id-expression' case, the value returned is as for
21019 cp_parser_id_expression if the id-expression was an unqualified-id.
21020 If the id-expression was a qualified-id, then a SCOPE_REF is
21021 returned. The first operand is the scope (either a NAMESPACE_DECL
21022 or TREE_TYPE), but the second is still just a representation of an
21026 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21029 /* The expression must be an id-expression. Assume that qualified
21030 names are the names of types so that:
21033 int S<T>::R::i = 3;
21035 will work; we must treat `S<T>::R' as the name of a type.
21036 Similarly, assume that qualified names are templates, where
21040 int S<T>::R<T>::i = 3;
21043 id
= cp_parser_id_expression (parser
,
21044 /*template_keyword_p=*/false,
21045 /*check_dependency_p=*/false,
21046 /*template_p=*/NULL
,
21047 /*declarator_p=*/true,
21049 if (id
&& BASELINK_P (id
))
21050 id
= BASELINK_FUNCTIONS (id
);
21054 /* Parse a type-id.
21057 type-specifier-seq abstract-declarator [opt]
21059 Returns the TYPE specified. */
21062 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21063 bool is_trailing_return
)
21065 cp_decl_specifier_seq type_specifier_seq
;
21066 cp_declarator
*abstract_declarator
;
21068 /* Parse the type-specifier-seq. */
21069 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21070 is_trailing_return
,
21071 &type_specifier_seq
);
21072 if (is_template_arg
&& type_specifier_seq
.type
21073 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21074 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21075 /* A bare template name as a template argument is a template template
21076 argument, not a placeholder, so fail parsing it as a type argument. */
21078 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21079 cp_parser_simulate_error (parser
);
21080 return error_mark_node
;
21082 if (type_specifier_seq
.type
== error_mark_node
)
21083 return error_mark_node
;
21085 /* There might or might not be an abstract declarator. */
21086 cp_parser_parse_tentatively (parser
);
21087 /* Look for the declarator. */
21088 abstract_declarator
21089 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21090 /*parenthesized_p=*/NULL
,
21091 /*member_p=*/false,
21092 /*friend_p=*/false);
21093 /* Check to see if there really was a declarator. */
21094 if (!cp_parser_parse_definitely (parser
))
21095 abstract_declarator
= NULL
;
21097 if (type_specifier_seq
.type
21098 /* The concepts TS allows 'auto' as a type-id. */
21099 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21100 /* None of the valid uses of 'auto' in C++14 involve the type-id
21101 nonterminal, but it is valid in a trailing-return-type. */
21102 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21103 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21105 /* A type-id with type 'auto' is only ok if the abstract declarator
21106 is a function declarator with a late-specified return type.
21108 A type-id with 'auto' is also valid in a trailing-return-type
21109 in a compound-requirement. */
21110 if (abstract_declarator
21111 && abstract_declarator
->kind
== cdk_function
21112 && abstract_declarator
->u
.function
.late_return_type
)
21114 else if (parser
->in_result_type_constraint_p
)
21118 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21119 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21121 error_at (loc
, "missing template arguments after %qT",
21123 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21127 error_at (loc
, "invalid use of %qT", auto_node
);
21128 return error_mark_node
;
21132 return groktypename (&type_specifier_seq
, abstract_declarator
,
21137 cp_parser_type_id (cp_parser
*parser
)
21139 return cp_parser_type_id_1 (parser
, false, false);
21143 cp_parser_template_type_arg (cp_parser
*parser
)
21146 const char *saved_message
= parser
->type_definition_forbidden_message
;
21147 parser
->type_definition_forbidden_message
21148 = G_("types may not be defined in template arguments");
21149 r
= cp_parser_type_id_1 (parser
, true, false);
21150 parser
->type_definition_forbidden_message
= saved_message
;
21151 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21153 error ("invalid use of %<auto%> in template argument");
21154 r
= error_mark_node
;
21160 cp_parser_trailing_type_id (cp_parser
*parser
)
21162 return cp_parser_type_id_1 (parser
, false, true);
21165 /* Parse a type-specifier-seq.
21167 type-specifier-seq:
21168 type-specifier type-specifier-seq [opt]
21172 type-specifier-seq:
21173 attributes type-specifier-seq [opt]
21175 If IS_DECLARATION is true, we are at the start of a "condition" or
21176 exception-declaration, so we might be followed by a declarator-id.
21178 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21179 i.e. we've just seen "->".
21181 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21184 cp_parser_type_specifier_seq (cp_parser
* parser
,
21185 bool is_declaration
,
21186 bool is_trailing_return
,
21187 cp_decl_specifier_seq
*type_specifier_seq
)
21189 bool seen_type_specifier
= false;
21190 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21191 cp_token
*start_token
= NULL
;
21193 /* Clear the TYPE_SPECIFIER_SEQ. */
21194 clear_decl_specs (type_specifier_seq
);
21196 /* In the context of a trailing return type, enum E { } is an
21197 elaborated-type-specifier followed by a function-body, not an
21199 if (is_trailing_return
)
21200 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21202 /* Parse the type-specifiers and attributes. */
21205 tree type_specifier
;
21206 bool is_cv_qualifier
;
21208 /* Check for attributes first. */
21209 if (cp_next_tokens_can_be_attribute_p (parser
))
21211 type_specifier_seq
->attributes
21212 = attr_chainon (type_specifier_seq
->attributes
,
21213 cp_parser_attributes_opt (parser
));
21217 /* record the token of the beginning of the type specifier seq,
21218 for error reporting purposes*/
21220 start_token
= cp_lexer_peek_token (parser
->lexer
);
21222 /* Look for the type-specifier. */
21223 type_specifier
= cp_parser_type_specifier (parser
,
21225 type_specifier_seq
,
21226 /*is_declaration=*/false,
21229 if (!type_specifier
)
21231 /* If the first type-specifier could not be found, this is not a
21232 type-specifier-seq at all. */
21233 if (!seen_type_specifier
)
21235 /* Set in_declarator_p to avoid skipping to the semicolon. */
21236 int in_decl
= parser
->in_declarator_p
;
21237 parser
->in_declarator_p
= true;
21239 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21240 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21241 cp_parser_error (parser
, "expected type-specifier");
21243 parser
->in_declarator_p
= in_decl
;
21245 type_specifier_seq
->type
= error_mark_node
;
21248 /* If subsequent type-specifiers could not be found, the
21249 type-specifier-seq is complete. */
21253 seen_type_specifier
= true;
21254 /* The standard says that a condition can be:
21256 type-specifier-seq declarator = assignment-expression
21263 we should treat the "S" as a declarator, not as a
21264 type-specifier. The standard doesn't say that explicitly for
21265 type-specifier-seq, but it does say that for
21266 decl-specifier-seq in an ordinary declaration. Perhaps it
21267 would be clearer just to allow a decl-specifier-seq here, and
21268 then add a semantic restriction that if any decl-specifiers
21269 that are not type-specifiers appear, the program is invalid. */
21270 if (is_declaration
&& !is_cv_qualifier
)
21271 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21275 /* Return whether the function currently being declared has an associated
21276 template parameter list. */
21279 function_being_declared_is_template_p (cp_parser
* parser
)
21281 if (!current_template_parms
|| processing_template_parmlist
)
21284 if (parser
->implicit_template_scope
)
21287 if (at_class_scope_p ()
21288 && TYPE_BEING_DEFINED (current_class_type
))
21289 return parser
->num_template_parameter_lists
!= 0;
21291 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21292 (current_class_type
));
21295 /* Parse a parameter-declaration-clause.
21297 parameter-declaration-clause:
21298 parameter-declaration-list [opt] ... [opt]
21299 parameter-declaration-list , ...
21301 Returns a representation for the parameter declarations. A return
21302 value of NULL indicates a parameter-declaration-clause consisting
21303 only of an ellipsis. */
21306 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21312 temp_override
<bool> cleanup
21313 (parser
->auto_is_implicit_function_template_parm_p
);
21315 if (!processing_specialization
21316 && !processing_template_parmlist
21317 && !processing_explicit_instantiation
21318 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21319 actual function or a random abstract declarator. */
21320 && parser
->default_arg_ok_p
)
21321 if (!current_function_decl
21322 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21323 parser
->auto_is_implicit_function_template_parm_p
= true;
21325 /* Peek at the next token. */
21326 token
= cp_lexer_peek_token (parser
->lexer
);
21327 /* Check for trivial parameter-declaration-clauses. */
21328 if (token
->type
== CPP_ELLIPSIS
)
21330 /* Consume the `...' token. */
21331 cp_lexer_consume_token (parser
->lexer
);
21334 else if (token
->type
== CPP_CLOSE_PAREN
)
21335 /* There are no parameters. */
21337 #ifndef NO_IMPLICIT_EXTERN_C
21338 if (in_system_header_at (input_location
)
21339 && current_class_type
== NULL
21340 && current_lang_name
== lang_name_c
)
21344 return void_list_node
;
21346 /* Check for `(void)', too, which is a special case. */
21347 else if (token
->keyword
== RID_VOID
21348 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21349 == CPP_CLOSE_PAREN
))
21351 /* Consume the `void' token. */
21352 cp_lexer_consume_token (parser
->lexer
);
21353 /* There are no parameters. */
21354 return void_list_node
;
21357 /* Parse the parameter-declaration-list. */
21358 parameters
= cp_parser_parameter_declaration_list (parser
);
21359 /* If a parse error occurred while parsing the
21360 parameter-declaration-list, then the entire
21361 parameter-declaration-clause is erroneous. */
21362 if (parameters
== error_mark_node
)
21365 /* Peek at the next token. */
21366 token
= cp_lexer_peek_token (parser
->lexer
);
21367 /* If it's a `,', the clause should terminate with an ellipsis. */
21368 if (token
->type
== CPP_COMMA
)
21370 /* Consume the `,'. */
21371 cp_lexer_consume_token (parser
->lexer
);
21372 /* Expect an ellipsis. */
21374 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21376 /* It might also be `...' if the optional trailing `,' was
21378 else if (token
->type
== CPP_ELLIPSIS
)
21380 /* Consume the `...' token. */
21381 cp_lexer_consume_token (parser
->lexer
);
21382 /* And remember that we saw it. */
21386 ellipsis_p
= false;
21388 /* Finish the parameter list. */
21390 parameters
= chainon (parameters
, void_list_node
);
21395 /* Parse a parameter-declaration-list.
21397 parameter-declaration-list:
21398 parameter-declaration
21399 parameter-declaration-list , parameter-declaration
21401 Returns a representation of the parameter-declaration-list, as for
21402 cp_parser_parameter_declaration_clause. However, the
21403 `void_list_node' is never appended to the list. */
21406 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21408 tree parameters
= NULL_TREE
;
21409 tree
*tail
= ¶meters
;
21410 bool saved_in_unbraced_linkage_specification_p
;
21413 /* The special considerations that apply to a function within an
21414 unbraced linkage specifications do not apply to the parameters
21415 to the function. */
21416 saved_in_unbraced_linkage_specification_p
21417 = parser
->in_unbraced_linkage_specification_p
;
21418 parser
->in_unbraced_linkage_specification_p
= false;
21420 /* Look for more parameters. */
21423 cp_parameter_declarator
*parameter
;
21424 tree decl
= error_mark_node
;
21425 bool parenthesized_p
= false;
21427 /* Parse the parameter. */
21429 = cp_parser_parameter_declaration (parser
,
21430 /*template_parm_p=*/false,
21433 /* We don't know yet if the enclosing context is deprecated, so wait
21434 and warn in grokparms if appropriate. */
21435 deprecated_state
= DEPRECATED_SUPPRESS
;
21439 decl
= grokdeclarator (parameter
->declarator
,
21440 ¶meter
->decl_specifiers
,
21442 parameter
->default_argument
!= NULL_TREE
,
21443 ¶meter
->decl_specifiers
.attributes
);
21444 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21445 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21448 deprecated_state
= DEPRECATED_NORMAL
;
21450 /* If a parse error occurred parsing the parameter declaration,
21451 then the entire parameter-declaration-list is erroneous. */
21452 if (decl
== error_mark_node
)
21454 parameters
= error_mark_node
;
21458 if (parameter
->decl_specifiers
.attributes
)
21459 cplus_decl_attributes (&decl
,
21460 parameter
->decl_specifiers
.attributes
,
21462 if (DECL_NAME (decl
))
21463 decl
= pushdecl (decl
);
21465 if (decl
!= error_mark_node
)
21467 retrofit_lang_decl (decl
);
21468 DECL_PARM_INDEX (decl
) = ++index
;
21469 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21472 /* Add the new parameter to the list. */
21473 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21474 tail
= &TREE_CHAIN (*tail
);
21476 /* Peek at the next token. */
21477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21478 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21479 /* These are for Objective-C++ */
21480 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21481 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21482 /* The parameter-declaration-list is complete. */
21484 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21488 /* Peek at the next token. */
21489 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21490 /* If it's an ellipsis, then the list is complete. */
21491 if (token
->type
== CPP_ELLIPSIS
)
21493 /* Otherwise, there must be more parameters. Consume the
21495 cp_lexer_consume_token (parser
->lexer
);
21496 /* When parsing something like:
21498 int i(float f, double d)
21500 we can tell after seeing the declaration for "f" that we
21501 are not looking at an initialization of a variable "i",
21502 but rather at the declaration of a function "i".
21504 Due to the fact that the parsing of template arguments
21505 (as specified to a template-id) requires backtracking we
21506 cannot use this technique when inside a template argument
21508 if (!parser
->in_template_argument_list_p
21509 && !parser
->in_type_id_in_expr_p
21510 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21511 /* However, a parameter-declaration of the form
21512 "float(f)" (which is a valid declaration of a
21513 parameter "f") can also be interpreted as an
21514 expression (the conversion of "f" to "float"). */
21515 && !parenthesized_p
)
21516 cp_parser_commit_to_tentative_parse (parser
);
21520 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21521 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21522 cp_parser_skip_to_closing_parenthesis (parser
,
21523 /*recovering=*/true,
21524 /*or_comma=*/false,
21525 /*consume_paren=*/false);
21530 parser
->in_unbraced_linkage_specification_p
21531 = saved_in_unbraced_linkage_specification_p
;
21533 /* Reset implicit_template_scope if we are about to leave the function
21534 parameter list that introduced it. Note that for out-of-line member
21535 definitions, there will be one or more class scopes before we get to
21536 the template parameter scope. */
21538 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21539 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21541 while (maybe_its
->kind
== sk_class
)
21542 maybe_its
= maybe_its
->level_chain
;
21543 if (maybe_its
== its
)
21545 parser
->implicit_template_parms
= 0;
21546 parser
->implicit_template_scope
= 0;
21553 /* Parse a parameter declaration.
21555 parameter-declaration:
21556 decl-specifier-seq ... [opt] declarator
21557 decl-specifier-seq declarator = assignment-expression
21558 decl-specifier-seq ... [opt] abstract-declarator [opt]
21559 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21561 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21562 declares a template parameter. (In that case, a non-nested `>'
21563 token encountered during the parsing of the assignment-expression
21564 is not interpreted as a greater-than operator.)
21566 Returns a representation of the parameter, or NULL if an error
21567 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21568 true iff the declarator is of the form "(p)". */
21570 static cp_parameter_declarator
*
21571 cp_parser_parameter_declaration (cp_parser
*parser
,
21572 bool template_parm_p
,
21573 bool *parenthesized_p
)
21575 int declares_class_or_enum
;
21576 cp_decl_specifier_seq decl_specifiers
;
21577 cp_declarator
*declarator
;
21578 tree default_argument
;
21579 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21580 const char *saved_message
;
21581 bool template_parameter_pack_p
= false;
21583 /* In a template parameter, `>' is not an operator.
21587 When parsing a default template-argument for a non-type
21588 template-parameter, the first non-nested `>' is taken as the end
21589 of the template parameter-list rather than a greater-than
21592 /* Type definitions may not appear in parameter types. */
21593 saved_message
= parser
->type_definition_forbidden_message
;
21594 parser
->type_definition_forbidden_message
21595 = G_("types may not be defined in parameter types");
21597 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21598 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21599 (current_template_parms
)) : 0);
21601 /* Parse the declaration-specifiers. */
21602 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21603 cp_parser_decl_specifier_seq (parser
,
21604 CP_PARSER_FLAGS_NONE
,
21606 &declares_class_or_enum
);
21608 /* Complain about missing 'typename' or other invalid type names. */
21609 if (!decl_specifiers
.any_type_specifiers_p
21610 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21611 decl_specifiers
.type
= error_mark_node
;
21613 /* If an error occurred, there's no reason to attempt to parse the
21614 rest of the declaration. */
21615 if (cp_parser_error_occurred (parser
))
21617 parser
->type_definition_forbidden_message
= saved_message
;
21621 /* Peek at the next token. */
21622 token
= cp_lexer_peek_token (parser
->lexer
);
21624 /* If the next token is a `)', `,', `=', `>', or `...', then there
21625 is no declarator. However, when variadic templates are enabled,
21626 there may be a declarator following `...'. */
21627 if (token
->type
== CPP_CLOSE_PAREN
21628 || token
->type
== CPP_COMMA
21629 || token
->type
== CPP_EQ
21630 || token
->type
== CPP_GREATER
)
21633 if (parenthesized_p
)
21634 *parenthesized_p
= false;
21636 /* Otherwise, there should be a declarator. */
21639 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21640 parser
->default_arg_ok_p
= false;
21642 /* After seeing a decl-specifier-seq, if the next token is not a
21643 "(", there is no possibility that the code is a valid
21644 expression. Therefore, if parsing tentatively, we commit at
21646 if (!parser
->in_template_argument_list_p
21647 /* In an expression context, having seen:
21651 we cannot be sure whether we are looking at a
21652 function-type (taking a "char" as a parameter) or a cast
21653 of some object of type "char" to "int". */
21654 && !parser
->in_type_id_in_expr_p
21655 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21656 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21657 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21658 cp_parser_commit_to_tentative_parse (parser
);
21659 /* Parse the declarator. */
21660 declarator_token_start
= token
;
21661 declarator
= cp_parser_declarator (parser
,
21662 CP_PARSER_DECLARATOR_EITHER
,
21663 /*ctor_dtor_or_conv_p=*/NULL
,
21665 /*member_p=*/false,
21666 /*friend_p=*/false);
21667 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21668 /* After the declarator, allow more attributes. */
21669 decl_specifiers
.attributes
21670 = attr_chainon (decl_specifiers
.attributes
,
21671 cp_parser_attributes_opt (parser
));
21673 /* If the declarator is a template parameter pack, remember that and
21674 clear the flag in the declarator itself so we don't get errors
21675 from grokdeclarator. */
21676 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21678 declarator
->parameter_pack_p
= false;
21679 template_parameter_pack_p
= true;
21683 /* If the next token is an ellipsis, and we have not seen a declarator
21684 name, and if either the type of the declarator contains parameter
21685 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21686 for, eg, abbreviated integral type names), then we actually have a
21687 parameter pack expansion expression. Otherwise, leave the ellipsis
21688 for a C-style variadic function. */
21689 token
= cp_lexer_peek_token (parser
->lexer
);
21691 /* If a function parameter pack was specified and an implicit template
21692 parameter was introduced during cp_parser_parameter_declaration,
21693 change any implicit parameters introduced into packs. */
21694 if (parser
->implicit_template_parms
21695 && (token
->type
== CPP_ELLIPSIS
21696 || (declarator
&& declarator
->parameter_pack_p
)))
21698 int latest_template_parm_idx
= TREE_VEC_LENGTH
21699 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21701 if (latest_template_parm_idx
!= template_parm_idx
)
21702 decl_specifiers
.type
= convert_generic_types_to_packs
21703 (decl_specifiers
.type
,
21704 template_parm_idx
, latest_template_parm_idx
);
21707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21709 tree type
= decl_specifiers
.type
;
21711 if (type
&& DECL_P (type
))
21712 type
= TREE_TYPE (type
);
21715 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21716 && (template_parm_p
|| uses_parameter_packs (type
)))
21717 || (!type
&& template_parm_p
))
21718 && declarator_can_be_parameter_pack (declarator
))
21720 /* Consume the `...'. */
21721 cp_lexer_consume_token (parser
->lexer
);
21722 maybe_warn_variadic_templates ();
21724 /* Build a pack expansion type */
21725 if (template_parm_p
)
21726 template_parameter_pack_p
= true;
21727 else if (declarator
)
21728 declarator
->parameter_pack_p
= true;
21730 decl_specifiers
.type
= make_pack_expansion (type
);
21734 /* The restriction on defining new types applies only to the type
21735 of the parameter, not to the default argument. */
21736 parser
->type_definition_forbidden_message
= saved_message
;
21738 /* If the next token is `=', then process a default argument. */
21739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21741 tree type
= decl_specifiers
.type
;
21742 token
= cp_lexer_peek_token (parser
->lexer
);
21743 /* If we are defining a class, then the tokens that make up the
21744 default argument must be saved and processed later. */
21745 if (!template_parm_p
&& at_class_scope_p ()
21746 && TYPE_BEING_DEFINED (current_class_type
)
21747 && !LAMBDA_TYPE_P (current_class_type
))
21748 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21750 // A constrained-type-specifier may declare a type template-parameter.
21751 else if (declares_constrained_type_template_parameter (type
))
21753 = cp_parser_default_type_template_argument (parser
);
21755 // A constrained-type-specifier may declare a template-template-parameter.
21756 else if (declares_constrained_template_template_parameter (type
))
21758 = cp_parser_default_template_template_argument (parser
);
21760 /* Outside of a class definition, we can just parse the
21761 assignment-expression. */
21764 = cp_parser_default_argument (parser
, template_parm_p
);
21766 if (!parser
->default_arg_ok_p
)
21768 permerror (token
->location
,
21769 "default arguments are only "
21770 "permitted for function parameters");
21772 else if ((declarator
&& declarator
->parameter_pack_p
)
21773 || template_parameter_pack_p
21774 || (decl_specifiers
.type
21775 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21777 /* Find the name of the parameter pack. */
21778 cp_declarator
*id_declarator
= declarator
;
21779 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21780 id_declarator
= id_declarator
->declarator
;
21782 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21783 error_at (declarator_token_start
->location
,
21785 ? G_("template parameter pack %qD "
21786 "cannot have a default argument")
21787 : G_("parameter pack %qD cannot have "
21788 "a default argument"),
21789 id_declarator
->u
.id
.unqualified_name
);
21791 error_at (declarator_token_start
->location
,
21793 ? G_("template parameter pack cannot have "
21794 "a default argument")
21795 : G_("parameter pack cannot have a "
21796 "default argument"));
21798 default_argument
= NULL_TREE
;
21802 default_argument
= NULL_TREE
;
21804 /* Generate a location for the parameter, ranging from the start of the
21805 initial token to the end of the final token (using input_location for
21806 the latter, set up by cp_lexer_set_source_position_from_token when
21809 If we have a identifier, then use it for the caret location, e.g.
21811 extern int callee (int one, int (*two)(int, int), float three);
21812 ~~~~~~^~~~~~~~~~~~~~
21814 otherwise, reuse the start location for the caret location e.g.:
21816 extern int callee (int one, int (*)(int, int), float three);
21820 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21821 ? declarator
->id_loc
21822 : decl_spec_token_start
->location
);
21823 location_t param_loc
= make_location (caret_loc
,
21824 decl_spec_token_start
->location
,
21827 return make_parameter_declarator (&decl_specifiers
,
21831 template_parameter_pack_p
);
21834 /* Parse a default argument and return it.
21836 TEMPLATE_PARM_P is true if this is a default argument for a
21837 non-type template parameter. */
21839 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21841 tree default_argument
= NULL_TREE
;
21842 bool saved_greater_than_is_operator_p
;
21843 bool saved_local_variables_forbidden_p
;
21844 bool non_constant_p
, is_direct_init
;
21846 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21848 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21849 parser
->greater_than_is_operator_p
= !template_parm_p
;
21850 /* Local variable names (and the `this' keyword) may not
21851 appear in a default argument. */
21852 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21853 parser
->local_variables_forbidden_p
= true;
21854 /* Parse the assignment-expression. */
21855 if (template_parm_p
)
21856 push_deferring_access_checks (dk_no_deferred
);
21857 tree saved_class_ptr
= NULL_TREE
;
21858 tree saved_class_ref
= NULL_TREE
;
21859 /* The "this" pointer is not valid in a default argument. */
21862 saved_class_ptr
= current_class_ptr
;
21863 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21864 saved_class_ref
= current_class_ref
;
21865 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21868 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21869 /* Restore the "this" pointer. */
21872 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21873 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21875 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21876 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21877 if (template_parm_p
)
21878 pop_deferring_access_checks ();
21879 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21880 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21882 return default_argument
;
21885 /* Parse a function-body.
21888 compound_statement */
21891 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21893 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21894 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21898 /* Parse a ctor-initializer-opt followed by a function-body. Return
21899 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21900 is true we are parsing a function-try-block. */
21903 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21904 bool in_function_try_block
)
21907 const bool check_body_p
=
21908 DECL_CONSTRUCTOR_P (current_function_decl
)
21909 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21912 /* Begin the function body. */
21913 body
= begin_function_body ();
21914 /* Parse the optional ctor-initializer. */
21915 cp_parser_ctor_initializer_opt (parser
);
21917 /* If we're parsing a constexpr constructor definition, we need
21918 to check that the constructor body is indeed empty. However,
21919 before we get to cp_parser_function_body lot of junk has been
21920 generated, so we can't just check that we have an empty block.
21921 Rather we take a snapshot of the outermost block, and check whether
21922 cp_parser_function_body changed its state. */
21925 list
= cur_stmt_list
;
21926 if (STATEMENT_LIST_TAIL (list
))
21927 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21929 /* Parse the function-body. */
21930 cp_parser_function_body (parser
, in_function_try_block
);
21932 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21933 /* Finish the function body. */
21934 finish_function_body (body
);
21937 /* Parse an initializer.
21940 = initializer-clause
21941 ( expression-list )
21943 Returns an expression representing the initializer. If no
21944 initializer is present, NULL_TREE is returned.
21946 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21947 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21948 set to TRUE if there is no initializer present. If there is an
21949 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21950 is set to true; otherwise it is set to false. */
21953 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21954 bool* non_constant_p
, bool subexpression_p
)
21959 /* Peek at the next token. */
21960 token
= cp_lexer_peek_token (parser
->lexer
);
21962 /* Let our caller know whether or not this initializer was
21964 *is_direct_init
= (token
->type
!= CPP_EQ
);
21965 /* Assume that the initializer is constant. */
21966 *non_constant_p
= false;
21968 if (token
->type
== CPP_EQ
)
21970 /* Consume the `='. */
21971 cp_lexer_consume_token (parser
->lexer
);
21972 /* Parse the initializer-clause. */
21973 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21975 else if (token
->type
== CPP_OPEN_PAREN
)
21977 vec
<tree
, va_gc
> *vec
;
21978 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21980 /*allow_expansion_p=*/true,
21983 return error_mark_node
;
21984 init
= build_tree_list_vec (vec
);
21985 release_tree_vector (vec
);
21987 else if (token
->type
== CPP_OPEN_BRACE
)
21989 cp_lexer_set_source_position (parser
->lexer
);
21990 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21991 init
= cp_parser_braced_list (parser
, non_constant_p
);
21992 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21996 /* Anything else is an error. */
21997 cp_parser_error (parser
, "expected initializer");
21998 init
= error_mark_node
;
22001 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22002 init
= error_mark_node
;
22007 /* Parse an initializer-clause.
22009 initializer-clause:
22010 assignment-expression
22013 Returns an expression representing the initializer.
22015 If the `assignment-expression' production is used the value
22016 returned is simply a representation for the expression.
22018 Otherwise, calls cp_parser_braced_list. */
22021 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22023 cp_expr initializer
;
22025 /* Assume the expression is constant. */
22026 *non_constant_p
= false;
22028 /* If it is not a `{', then we are looking at an
22029 assignment-expression. */
22030 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22033 = cp_parser_constant_expression (parser
,
22034 /*allow_non_constant_p=*/true,
22038 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22040 return initializer
;
22043 /* Parse a brace-enclosed initializer list.
22046 { initializer-list , [opt] }
22047 { designated-initializer-list , [opt] }
22050 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22051 the elements of the initializer-list (or NULL, if the last
22052 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22053 NULL_TREE. There is no way to detect whether or not the optional
22054 trailing `,' was provided. NON_CONSTANT_P is as for
22055 cp_parser_initializer. */
22058 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22061 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22063 /* Consume the `{' token. */
22064 matching_braces braces
;
22065 braces
.require_open (parser
);
22066 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22067 initializer
= make_node (CONSTRUCTOR
);
22068 /* If it's not a `}', then there is a non-trivial initializer. */
22069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22071 /* Parse the initializer list. */
22072 CONSTRUCTOR_ELTS (initializer
)
22073 = cp_parser_initializer_list (parser
, non_constant_p
);
22074 /* A trailing `,' token is allowed. */
22075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22076 cp_lexer_consume_token (parser
->lexer
);
22079 *non_constant_p
= false;
22080 /* Now, there should be a trailing `}'. */
22081 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22082 braces
.require_close (parser
);
22083 TREE_TYPE (initializer
) = init_list_type_node
;
22085 cp_expr
result (initializer
);
22086 /* Build a location of the form:
22089 with caret==start at the open brace, finish at the close brace. */
22090 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22091 result
.set_location (combined_loc
);
22095 /* Consume tokens up to, and including, the next non-nested closing `]'.
22096 Returns true iff we found a closing `]'. */
22099 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22101 unsigned square_depth
= 0;
22105 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22107 switch (token
->type
)
22110 case CPP_PRAGMA_EOL
:
22111 /* If we've run out of tokens, then there is no closing `]'. */
22114 case CPP_OPEN_SQUARE
:
22118 case CPP_CLOSE_SQUARE
:
22119 if (!square_depth
--)
22121 cp_lexer_consume_token (parser
->lexer
);
22130 /* Consume the token. */
22131 cp_lexer_consume_token (parser
->lexer
);
22135 /* Return true if we are looking at an array-designator, false otherwise. */
22138 cp_parser_array_designator_p (cp_parser
*parser
)
22140 /* Consume the `['. */
22141 cp_lexer_consume_token (parser
->lexer
);
22143 cp_lexer_save_tokens (parser
->lexer
);
22145 /* Skip tokens until the next token is a closing square bracket.
22146 If we find the closing `]', and the next token is a `=', then
22147 we are looking at an array designator. */
22148 bool array_designator_p
22149 = (cp_parser_skip_to_closing_square_bracket (parser
)
22150 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22152 /* Roll back the tokens we skipped. */
22153 cp_lexer_rollback_tokens (parser
->lexer
);
22155 return array_designator_p
;
22158 /* Parse an initializer-list.
22161 initializer-clause ... [opt]
22162 initializer-list , initializer-clause ... [opt]
22166 designated-initializer-list:
22167 designated-initializer-clause
22168 designated-initializer-list , designated-initializer-clause
22170 designated-initializer-clause:
22171 designator brace-or-equal-initializer
22179 designation initializer-clause ...[opt]
22180 initializer-list , designation initializer-clause ...[opt]
22185 [ constant-expression ] =
22187 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22188 for the initializer. If the INDEX of the elt is non-NULL, it is the
22189 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22190 as for cp_parser_initializer. */
22192 static vec
<constructor_elt
, va_gc
> *
22193 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22195 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22196 bool first_p
= true;
22197 tree first_designator
= NULL_TREE
;
22199 /* Assume all of the expressions are constant. */
22200 *non_constant_p
= false;
22202 /* Parse the rest of the list. */
22208 bool clause_non_constant_p
;
22209 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22211 /* Handle the C++2A syntax, '. id ='. */
22212 if ((cxx_dialect
>= cxx2a
22213 || cp_parser_allow_gnu_extensions_p (parser
))
22214 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22215 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22216 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22217 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22218 == CPP_OPEN_BRACE
)))
22220 if (cxx_dialect
< cxx2a
)
22221 pedwarn (loc
, OPT_Wpedantic
,
22222 "C++ designated initializers only available with "
22223 "-std=c++2a or -std=gnu++2a");
22224 /* Consume the `.'. */
22225 cp_lexer_consume_token (parser
->lexer
);
22226 /* Consume the identifier. */
22227 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22229 /* Consume the `='. */
22230 cp_lexer_consume_token (parser
->lexer
);
22232 /* Also, if the next token is an identifier and the following one is a
22233 colon, we are looking at the GNU designated-initializer
22235 else if (cp_parser_allow_gnu_extensions_p (parser
)
22236 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22237 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22240 /* Warn the user that they are using an extension. */
22241 pedwarn (loc
, OPT_Wpedantic
,
22242 "ISO C++ does not allow GNU designated initializers");
22243 /* Consume the identifier. */
22244 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22245 /* Consume the `:'. */
22246 cp_lexer_consume_token (parser
->lexer
);
22248 /* Also handle C99 array designators, '[ const ] ='. */
22249 else if (cp_parser_allow_gnu_extensions_p (parser
)
22250 && !c_dialect_objc ()
22251 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22253 /* In C++11, [ could start a lambda-introducer. */
22254 bool non_const
= false;
22256 cp_parser_parse_tentatively (parser
);
22258 if (!cp_parser_array_designator_p (parser
))
22260 cp_parser_simulate_error (parser
);
22261 designator
= NULL_TREE
;
22265 designator
= cp_parser_constant_expression (parser
, true,
22267 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22268 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22271 if (!cp_parser_parse_definitely (parser
))
22272 designator
= NULL_TREE
;
22274 && (!require_potential_rvalue_constant_expression
22276 designator
= NULL_TREE
;
22278 /* Warn the user that they are using an extension. */
22279 pedwarn (loc
, OPT_Wpedantic
,
22280 "ISO C++ does not allow C99 designated initializers");
22283 designator
= NULL_TREE
;
22287 first_designator
= designator
;
22290 else if (cxx_dialect
>= cxx2a
22291 && first_designator
!= error_mark_node
22292 && (!first_designator
!= !designator
))
22294 error_at (loc
, "either all initializer clauses should be designated "
22295 "or none of them should be");
22296 first_designator
= error_mark_node
;
22298 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22299 first_designator
= designator
;
22301 /* Parse the initializer. */
22302 initializer
= cp_parser_initializer_clause (parser
,
22303 &clause_non_constant_p
);
22304 /* If any clause is non-constant, so is the entire initializer. */
22305 if (clause_non_constant_p
)
22306 *non_constant_p
= true;
22308 /* If we have an ellipsis, this is an initializer pack
22310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22312 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22314 /* Consume the `...'. */
22315 cp_lexer_consume_token (parser
->lexer
);
22317 if (designator
&& cxx_dialect
>= cxx2a
)
22319 "%<...%> not allowed in designated initializer list");
22321 /* Turn the initializer into an initializer expansion. */
22322 initializer
= make_pack_expansion (initializer
);
22325 /* Add it to the vector. */
22326 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22328 /* If the next token is not a comma, we have reached the end of
22330 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22333 /* Peek at the next token. */
22334 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22335 /* If the next token is a `}', then we're still done. An
22336 initializer-clause can have a trailing `,' after the
22337 initializer-list and before the closing `}'. */
22338 if (token
->type
== CPP_CLOSE_BRACE
)
22341 /* Consume the `,' token. */
22342 cp_lexer_consume_token (parser
->lexer
);
22345 /* The same identifier shall not appear in multiple designators
22346 of a designated-initializer-list. */
22347 if (first_designator
)
22350 tree designator
, val
;
22351 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22352 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22354 if (IDENTIFIER_MARKED (designator
))
22356 error_at (cp_expr_loc_or_loc (val
, input_location
),
22357 "%<.%s%> designator used multiple times in "
22358 "the same initializer list",
22359 IDENTIFIER_POINTER (designator
));
22360 (*v
)[i
].index
= NULL_TREE
;
22363 IDENTIFIER_MARKED (designator
) = 1;
22365 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22366 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22367 IDENTIFIER_MARKED (designator
) = 0;
22373 /* Classes [gram.class] */
22375 /* Parse a class-name.
22381 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22382 to indicate that names looked up in dependent types should be
22383 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22384 keyword has been used to indicate that the name that appears next
22385 is a template. TAG_TYPE indicates the explicit tag given before
22386 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22387 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22388 is the class being defined in a class-head. If ENUM_OK is TRUE,
22389 enum-names are also accepted.
22391 Returns the TYPE_DECL representing the class. */
22394 cp_parser_class_name (cp_parser
*parser
,
22395 bool typename_keyword_p
,
22396 bool template_keyword_p
,
22397 enum tag_types tag_type
,
22398 bool check_dependency_p
,
22400 bool is_declaration
,
22407 tree identifier
= NULL_TREE
;
22409 /* All class-names start with an identifier. */
22410 token
= cp_lexer_peek_token (parser
->lexer
);
22411 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22413 cp_parser_error (parser
, "expected class-name");
22414 return error_mark_node
;
22417 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22418 to a template-id, so we save it here. */
22419 scope
= parser
->scope
;
22420 if (scope
== error_mark_node
)
22421 return error_mark_node
;
22423 /* Any name names a type if we're following the `typename' keyword
22424 in a qualified name where the enclosing scope is type-dependent. */
22425 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22426 && dependent_type_p (scope
));
22427 /* Handle the common case (an identifier, but not a template-id)
22429 if (token
->type
== CPP_NAME
22430 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22432 cp_token
*identifier_token
;
22435 /* Look for the identifier. */
22436 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22437 ambiguous_p
= identifier_token
->error_reported
;
22438 identifier
= cp_parser_identifier (parser
);
22439 /* If the next token isn't an identifier, we are certainly not
22440 looking at a class-name. */
22441 if (identifier
== error_mark_node
)
22442 decl
= error_mark_node
;
22443 /* If we know this is a type-name, there's no need to look it
22445 else if (typename_p
)
22449 tree ambiguous_decls
;
22450 /* If we already know that this lookup is ambiguous, then
22451 we've already issued an error message; there's no reason
22455 cp_parser_simulate_error (parser
);
22456 return error_mark_node
;
22458 /* If the next token is a `::', then the name must be a type
22461 [basic.lookup.qual]
22463 During the lookup for a name preceding the :: scope
22464 resolution operator, object, function, and enumerator
22465 names are ignored. */
22466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22467 tag_type
= scope_type
;
22468 /* Look up the name. */
22469 decl
= cp_parser_lookup_name (parser
, identifier
,
22471 /*is_template=*/false,
22472 /*is_namespace=*/false,
22473 check_dependency_p
,
22475 identifier_token
->location
);
22476 if (ambiguous_decls
)
22478 if (cp_parser_parsing_tentatively (parser
))
22479 cp_parser_simulate_error (parser
);
22480 return error_mark_node
;
22486 /* Try a template-id. */
22487 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22488 check_dependency_p
,
22491 if (decl
== error_mark_node
)
22492 return error_mark_node
;
22495 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22497 /* If this is a typename, create a TYPENAME_TYPE. */
22498 if (typename_p
&& decl
!= error_mark_node
)
22500 decl
= make_typename_type (scope
, decl
, typename_type
,
22501 /*complain=*/tf_error
);
22502 if (decl
!= error_mark_node
)
22503 decl
= TYPE_NAME (decl
);
22506 decl
= strip_using_decl (decl
);
22508 /* Check to see that it is really the name of a class. */
22509 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22510 && identifier_p (TREE_OPERAND (decl
, 0))
22511 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22512 /* Situations like this:
22514 template <typename T> struct A {
22515 typename T::template X<int>::I i;
22518 are problematic. Is `T::template X<int>' a class-name? The
22519 standard does not seem to be definitive, but there is no other
22520 valid interpretation of the following `::'. Therefore, those
22521 names are considered class-names. */
22523 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22524 if (decl
!= error_mark_node
)
22525 decl
= TYPE_NAME (decl
);
22527 else if (TREE_CODE (decl
) != TYPE_DECL
22528 || TREE_TYPE (decl
) == error_mark_node
22529 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22530 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22531 /* In Objective-C 2.0, a classname followed by '.' starts a
22532 dot-syntax expression, and it's not a type-name. */
22533 || (c_dialect_objc ()
22534 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22535 && objc_is_class_name (decl
)))
22536 decl
= error_mark_node
;
22538 if (decl
== error_mark_node
)
22539 cp_parser_error (parser
, "expected class-name");
22540 else if (identifier
&& !parser
->scope
)
22541 maybe_note_name_used_in_class (identifier
, decl
);
22546 /* Parse a class-specifier.
22549 class-head { member-specification [opt] }
22551 Returns the TREE_TYPE representing the class. */
22554 cp_parser_class_specifier_1 (cp_parser
* parser
)
22557 tree attributes
= NULL_TREE
;
22558 bool nested_name_specifier_p
;
22559 unsigned saved_num_template_parameter_lists
;
22560 bool saved_in_function_body
;
22561 unsigned char in_statement
;
22562 bool in_switch_statement_p
;
22563 bool saved_in_unbraced_linkage_specification_p
;
22564 tree old_scope
= NULL_TREE
;
22565 tree scope
= NULL_TREE
;
22566 cp_token
*closing_brace
;
22568 push_deferring_access_checks (dk_no_deferred
);
22570 /* Parse the class-head. */
22571 type
= cp_parser_class_head (parser
,
22572 &nested_name_specifier_p
);
22573 /* If the class-head was a semantic disaster, skip the entire body
22577 cp_parser_skip_to_end_of_block_or_statement (parser
);
22578 pop_deferring_access_checks ();
22579 return error_mark_node
;
22582 /* Look for the `{'. */
22583 matching_braces braces
;
22584 if (!braces
.require_open (parser
))
22586 pop_deferring_access_checks ();
22587 return error_mark_node
;
22590 cp_ensure_no_omp_declare_simd (parser
);
22591 cp_ensure_no_oacc_routine (parser
);
22593 /* Issue an error message if type-definitions are forbidden here. */
22594 cp_parser_check_type_definition (parser
);
22595 /* Remember that we are defining one more class. */
22596 ++parser
->num_classes_being_defined
;
22597 /* Inside the class, surrounding template-parameter-lists do not
22599 saved_num_template_parameter_lists
22600 = parser
->num_template_parameter_lists
;
22601 parser
->num_template_parameter_lists
= 0;
22602 /* We are not in a function body. */
22603 saved_in_function_body
= parser
->in_function_body
;
22604 parser
->in_function_body
= false;
22605 /* Or in a loop. */
22606 in_statement
= parser
->in_statement
;
22607 parser
->in_statement
= 0;
22608 /* Or in a switch. */
22609 in_switch_statement_p
= parser
->in_switch_statement_p
;
22610 parser
->in_switch_statement_p
= false;
22611 /* We are not immediately inside an extern "lang" block. */
22612 saved_in_unbraced_linkage_specification_p
22613 = parser
->in_unbraced_linkage_specification_p
;
22614 parser
->in_unbraced_linkage_specification_p
= false;
22616 // Associate constraints with the type.
22618 type
= associate_classtype_constraints (type
);
22620 /* Start the class. */
22621 if (nested_name_specifier_p
)
22623 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22624 old_scope
= push_inner_scope (scope
);
22626 type
= begin_class_definition (type
);
22628 if (type
== error_mark_node
)
22629 /* If the type is erroneous, skip the entire body of the class. */
22630 cp_parser_skip_to_closing_brace (parser
);
22632 /* Parse the member-specification. */
22633 cp_parser_member_specification_opt (parser
);
22635 /* Look for the trailing `}'. */
22636 closing_brace
= braces
.require_close (parser
);
22637 /* Look for trailing attributes to apply to this class. */
22638 if (cp_parser_allow_gnu_extensions_p (parser
))
22639 attributes
= cp_parser_gnu_attributes_opt (parser
);
22640 if (type
!= error_mark_node
)
22641 type
= finish_struct (type
, attributes
);
22642 if (nested_name_specifier_p
)
22643 pop_inner_scope (old_scope
, scope
);
22645 /* We've finished a type definition. Check for the common syntax
22646 error of forgetting a semicolon after the definition. We need to
22647 be careful, as we can't just check for not-a-semicolon and be done
22648 with it; the user might have typed:
22650 class X { } c = ...;
22651 class X { } *p = ...;
22653 and so forth. Instead, enumerate all the possible tokens that
22654 might follow this production; if we don't see one of them, then
22655 complain and silently insert the semicolon. */
22657 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22658 bool want_semicolon
= true;
22660 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22661 /* Don't try to parse c++11 attributes here. As per the
22662 grammar, that should be a task for
22663 cp_parser_decl_specifier_seq. */
22664 want_semicolon
= false;
22666 switch (token
->type
)
22669 case CPP_SEMICOLON
:
22672 case CPP_OPEN_PAREN
:
22673 case CPP_CLOSE_PAREN
:
22675 want_semicolon
= false;
22678 /* While it's legal for type qualifiers and storage class
22679 specifiers to follow type definitions in the grammar, only
22680 compiler testsuites contain code like that. Assume that if
22681 we see such code, then what we're really seeing is a case
22685 const <type> var = ...;
22690 static <type> func (...) ...
22692 i.e. the qualifier or specifier applies to the next
22693 declaration. To do so, however, we need to look ahead one
22694 more token to see if *that* token is a type specifier.
22696 This code could be improved to handle:
22699 static const <type> var = ...; */
22701 if (keyword_is_decl_specifier (token
->keyword
))
22703 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22705 /* Handling user-defined types here would be nice, but very
22708 = (lookahead
->type
== CPP_KEYWORD
22709 && keyword_begins_type_specifier (lookahead
->keyword
));
22716 /* If we don't have a type, then something is very wrong and we
22717 shouldn't try to do anything clever. Likewise for not seeing the
22719 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22721 /* Locate the closing brace. */
22722 cp_token_position prev
22723 = cp_lexer_previous_token_position (parser
->lexer
);
22724 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22725 location_t loc
= prev_token
->location
;
22727 /* We want to suggest insertion of a ';' immediately *after* the
22728 closing brace, so, if we can, offset the location by 1 column. */
22729 location_t next_loc
= loc
;
22730 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22731 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22733 rich_location
richloc (line_table
, next_loc
);
22735 /* If we successfully offset the location, suggest the fix-it. */
22736 if (next_loc
!= loc
)
22737 richloc
.add_fixit_insert_before (next_loc
, ";");
22739 if (CLASSTYPE_DECLARED_CLASS (type
))
22740 error_at (&richloc
,
22741 "expected %<;%> after class definition");
22742 else if (TREE_CODE (type
) == RECORD_TYPE
)
22743 error_at (&richloc
,
22744 "expected %<;%> after struct definition");
22745 else if (TREE_CODE (type
) == UNION_TYPE
)
22746 error_at (&richloc
,
22747 "expected %<;%> after union definition");
22749 gcc_unreachable ();
22751 /* Unget one token and smash it to look as though we encountered
22752 a semicolon in the input stream. */
22753 cp_lexer_set_token_position (parser
->lexer
, prev
);
22754 token
= cp_lexer_peek_token (parser
->lexer
);
22755 token
->type
= CPP_SEMICOLON
;
22756 token
->keyword
= RID_MAX
;
22760 /* If this class is not itself within the scope of another class,
22761 then we need to parse the bodies of all of the queued function
22762 definitions. Note that the queued functions defined in a class
22763 are not always processed immediately following the
22764 class-specifier for that class. Consider:
22767 struct B { void f() { sizeof (A); } };
22770 If `f' were processed before the processing of `A' were
22771 completed, there would be no way to compute the size of `A'.
22772 Note that the nesting we are interested in here is lexical --
22773 not the semantic nesting given by TYPE_CONTEXT. In particular,
22776 struct A { struct B; };
22777 struct A::B { void f() { } };
22779 there is no need to delay the parsing of `A::B::f'. */
22780 if (--parser
->num_classes_being_defined
== 0)
22783 tree class_type
= NULL_TREE
;
22784 tree pushed_scope
= NULL_TREE
;
22786 cp_default_arg_entry
*e
;
22787 tree save_ccp
, save_ccr
;
22789 if (any_erroneous_template_args_p (type
))
22791 /* Skip default arguments, NSDMIs, etc, in order to improve
22792 error recovery (c++/71169, c++/71832). */
22793 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22794 vec_safe_truncate (unparsed_nsdmis
, 0);
22795 vec_safe_truncate (unparsed_classes
, 0);
22796 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22799 /* In a first pass, parse default arguments to the functions.
22800 Then, in a second pass, parse the bodies of the functions.
22801 This two-phased approach handles cases like:
22809 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22812 /* If there are default arguments that have not yet been processed,
22813 take care of them now. */
22814 if (class_type
!= e
->class_type
)
22817 pop_scope (pushed_scope
);
22818 class_type
= e
->class_type
;
22819 pushed_scope
= push_scope (class_type
);
22821 /* Make sure that any template parameters are in scope. */
22822 maybe_begin_member_template_processing (decl
);
22823 /* Parse the default argument expressions. */
22824 cp_parser_late_parsing_default_args (parser
, decl
);
22825 /* Remove any template parameters from the symbol table. */
22826 maybe_end_member_template_processing ();
22828 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22829 /* Now parse any NSDMIs. */
22830 save_ccp
= current_class_ptr
;
22831 save_ccr
= current_class_ref
;
22832 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22834 if (class_type
!= DECL_CONTEXT (decl
))
22837 pop_scope (pushed_scope
);
22838 class_type
= DECL_CONTEXT (decl
);
22839 pushed_scope
= push_scope (class_type
);
22841 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22842 cp_parser_late_parsing_nsdmi (parser
, decl
);
22844 vec_safe_truncate (unparsed_nsdmis
, 0);
22845 current_class_ptr
= save_ccp
;
22846 current_class_ref
= save_ccr
;
22848 pop_scope (pushed_scope
);
22850 /* Now do some post-NSDMI bookkeeping. */
22851 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22852 after_nsdmi_defaulted_late_checks (class_type
);
22853 vec_safe_truncate (unparsed_classes
, 0);
22854 after_nsdmi_defaulted_late_checks (type
);
22856 /* Now parse the body of the functions. */
22859 /* OpenMP UDRs need to be parsed before all other functions. */
22860 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22861 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22862 cp_parser_late_parsing_for_member (parser
, decl
);
22863 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22864 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22865 cp_parser_late_parsing_for_member (parser
, decl
);
22868 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22869 cp_parser_late_parsing_for_member (parser
, decl
);
22870 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22873 vec_safe_push (unparsed_classes
, type
);
22875 /* Put back any saved access checks. */
22876 pop_deferring_access_checks ();
22878 /* Restore saved state. */
22879 parser
->in_switch_statement_p
= in_switch_statement_p
;
22880 parser
->in_statement
= in_statement
;
22881 parser
->in_function_body
= saved_in_function_body
;
22882 parser
->num_template_parameter_lists
22883 = saved_num_template_parameter_lists
;
22884 parser
->in_unbraced_linkage_specification_p
22885 = saved_in_unbraced_linkage_specification_p
;
22891 cp_parser_class_specifier (cp_parser
* parser
)
22894 timevar_push (TV_PARSE_STRUCT
);
22895 ret
= cp_parser_class_specifier_1 (parser
);
22896 timevar_pop (TV_PARSE_STRUCT
);
22900 /* Parse a class-head.
22903 class-key identifier [opt] base-clause [opt]
22904 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22905 class-key nested-name-specifier [opt] template-id
22908 class-virt-specifier:
22912 class-key attributes identifier [opt] base-clause [opt]
22913 class-key attributes nested-name-specifier identifier base-clause [opt]
22914 class-key attributes nested-name-specifier [opt] template-id
22917 Upon return BASES is initialized to the list of base classes (or
22918 NULL, if there are none) in the same form returned by
22919 cp_parser_base_clause.
22921 Returns the TYPE of the indicated class. Sets
22922 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22923 involving a nested-name-specifier was used, and FALSE otherwise.
22925 Returns error_mark_node if this is not a class-head.
22927 Returns NULL_TREE if the class-head is syntactically valid, but
22928 semantically invalid in a way that means we should skip the entire
22929 body of the class. */
22932 cp_parser_class_head (cp_parser
* parser
,
22933 bool* nested_name_specifier_p
)
22935 tree nested_name_specifier
;
22936 enum tag_types class_key
;
22937 tree id
= NULL_TREE
;
22938 tree type
= NULL_TREE
;
22941 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22942 bool template_id_p
= false;
22943 bool qualified_p
= false;
22944 bool invalid_nested_name_p
= false;
22945 bool invalid_explicit_specialization_p
= false;
22946 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22947 tree pushed_scope
= NULL_TREE
;
22948 unsigned num_templates
;
22949 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22950 /* Assume no nested-name-specifier will be present. */
22951 *nested_name_specifier_p
= false;
22952 /* Assume no template parameter lists will be used in defining the
22955 parser
->colon_corrects_to_scope_p
= false;
22957 /* Look for the class-key. */
22958 class_key
= cp_parser_class_key (parser
);
22959 if (class_key
== none_type
)
22960 return error_mark_node
;
22962 location_t class_head_start_location
= input_location
;
22964 /* Parse the attributes. */
22965 attributes
= cp_parser_attributes_opt (parser
);
22967 /* If the next token is `::', that is invalid -- but sometimes
22968 people do try to write:
22972 Handle this gracefully by accepting the extra qualifier, and then
22973 issuing an error about it later if this really is a
22974 class-head. If it turns out just to be an elaborated type
22975 specifier, remain silent. */
22976 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22977 qualified_p
= true;
22979 push_deferring_access_checks (dk_no_check
);
22981 /* Determine the name of the class. Begin by looking for an
22982 optional nested-name-specifier. */
22983 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22984 nested_name_specifier
22985 = cp_parser_nested_name_specifier_opt (parser
,
22986 /*typename_keyword_p=*/false,
22987 /*check_dependency_p=*/false,
22989 /*is_declaration=*/false);
22990 /* If there was a nested-name-specifier, then there *must* be an
22993 cp_token
*bad_template_keyword
= NULL
;
22995 if (nested_name_specifier
)
22997 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22998 /* Although the grammar says `identifier', it really means
22999 `class-name' or `template-name'. You are only allowed to
23000 define a class that has already been declared with this
23003 The proposed resolution for Core Issue 180 says that wherever
23004 you see `class T::X' you should treat `X' as a type-name.
23006 It is OK to define an inaccessible class; for example:
23008 class A { class B; };
23011 We do not know if we will see a class-name, or a
23012 template-name. We look for a class-name first, in case the
23013 class-name is a template-id; if we looked for the
23014 template-name first we would stop after the template-name. */
23015 cp_parser_parse_tentatively (parser
);
23016 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23017 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23018 type
= cp_parser_class_name (parser
,
23019 /*typename_keyword_p=*/false,
23020 /*template_keyword_p=*/false,
23022 /*check_dependency_p=*/false,
23023 /*class_head_p=*/true,
23024 /*is_declaration=*/false);
23025 /* If that didn't work, ignore the nested-name-specifier. */
23026 if (!cp_parser_parse_definitely (parser
))
23028 invalid_nested_name_p
= true;
23029 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23030 id
= cp_parser_identifier (parser
);
23031 if (id
== error_mark_node
)
23034 /* If we could not find a corresponding TYPE, treat this
23035 declaration like an unqualified declaration. */
23036 if (type
== error_mark_node
)
23037 nested_name_specifier
= NULL_TREE
;
23038 /* Otherwise, count the number of templates used in TYPE and its
23039 containing scopes. */
23041 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23043 /* Otherwise, the identifier is optional. */
23046 /* We don't know whether what comes next is a template-id,
23047 an identifier, or nothing at all. */
23048 cp_parser_parse_tentatively (parser
);
23049 /* Check for a template-id. */
23050 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23051 id
= cp_parser_template_id (parser
,
23052 /*template_keyword_p=*/false,
23053 /*check_dependency_p=*/true,
23055 /*is_declaration=*/true);
23056 /* If that didn't work, it could still be an identifier. */
23057 if (!cp_parser_parse_definitely (parser
))
23059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23061 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23062 id
= cp_parser_identifier (parser
);
23069 template_id_p
= true;
23074 pop_deferring_access_checks ();
23078 cp_parser_check_for_invalid_template_id (parser
, id
,
23080 type_start_token
->location
);
23082 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23084 /* If it's not a `:' or a `{' then we can't really be looking at a
23085 class-head, since a class-head only appears as part of a
23086 class-specifier. We have to detect this situation before calling
23087 xref_tag, since that has irreversible side-effects. */
23088 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23090 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23091 type
= error_mark_node
;
23095 /* At this point, we're going ahead with the class-specifier, even
23096 if some other problem occurs. */
23097 cp_parser_commit_to_tentative_parse (parser
);
23098 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23100 cp_parser_error (parser
,
23101 "cannot specify %<override%> for a class");
23102 type
= error_mark_node
;
23105 /* Issue the error about the overly-qualified name now. */
23108 cp_parser_error (parser
,
23109 "global qualification of class name is invalid");
23110 type
= error_mark_node
;
23113 else if (invalid_nested_name_p
)
23115 cp_parser_error (parser
,
23116 "qualified name does not name a class");
23117 type
= error_mark_node
;
23120 else if (nested_name_specifier
)
23124 if (bad_template_keyword
)
23125 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23126 keyword template shall not appear at the top level. */
23127 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23128 "keyword %<template%> not allowed in class-head-name");
23130 /* Reject typedef-names in class heads. */
23131 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23133 error_at (type_start_token
->location
,
23134 "invalid class name in declaration of %qD",
23140 /* Figure out in what scope the declaration is being placed. */
23141 scope
= current_scope ();
23142 /* If that scope does not contain the scope in which the
23143 class was originally declared, the program is invalid. */
23144 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23146 if (at_namespace_scope_p ())
23147 error_at (type_start_token
->location
,
23148 "declaration of %qD in namespace %qD which does not "
23150 type
, scope
, nested_name_specifier
);
23152 error_at (type_start_token
->location
,
23153 "declaration of %qD in %qD which does not enclose %qD",
23154 type
, scope
, nested_name_specifier
);
23160 A declarator-id shall not be qualified except for the
23161 definition of a ... nested class outside of its class
23162 ... [or] the definition or explicit instantiation of a
23163 class member of a namespace outside of its namespace. */
23164 if (scope
== nested_name_specifier
)
23166 permerror (nested_name_specifier_token_start
->location
,
23167 "extra qualification not allowed");
23168 nested_name_specifier
= NULL_TREE
;
23172 /* An explicit-specialization must be preceded by "template <>". If
23173 it is not, try to recover gracefully. */
23174 if (at_namespace_scope_p ()
23175 && parser
->num_template_parameter_lists
== 0
23176 && !processing_template_parmlist
23179 /* Build a location of this form:
23180 struct typename <ARGS>
23181 ^~~~~~~~~~~~~~~~~~~~~~
23182 with caret==start at the start token, and
23183 finishing at the end of the type. */
23184 location_t reported_loc
23185 = make_location (class_head_start_location
,
23186 class_head_start_location
,
23187 get_finish (type_start_token
->location
));
23188 rich_location
richloc (line_table
, reported_loc
);
23189 richloc
.add_fixit_insert_before (class_head_start_location
,
23191 error_at (&richloc
,
23192 "an explicit specialization must be preceded by"
23193 " %<template <>%>");
23194 invalid_explicit_specialization_p
= true;
23195 /* Take the same action that would have been taken by
23196 cp_parser_explicit_specialization. */
23197 ++parser
->num_template_parameter_lists
;
23198 begin_specialization ();
23200 /* There must be no "return" statements between this point and the
23201 end of this function; set "type "to the correct return value and
23202 use "goto done;" to return. */
23203 /* Make sure that the right number of template parameters were
23205 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23207 type_start_token
->location
,
23208 /*declarator=*/NULL
))
23210 /* If something went wrong, there is no point in even trying to
23211 process the class-definition. */
23216 /* Look up the type. */
23219 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23220 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23221 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23223 error_at (type_start_token
->location
,
23224 "function template %qD redeclared as a class template", id
);
23225 type
= error_mark_node
;
23229 type
= TREE_TYPE (id
);
23230 type
= maybe_process_partial_specialization (type
);
23232 /* Check the scope while we still know whether or not we had a
23233 nested-name-specifier. */
23234 if (type
!= error_mark_node
)
23235 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23237 if (nested_name_specifier
)
23238 pushed_scope
= push_scope (nested_name_specifier
);
23240 else if (nested_name_specifier
)
23246 template <typename T> struct S { struct T };
23247 template <typename T> struct S<T>::T { };
23249 we will get a TYPENAME_TYPE when processing the definition of
23250 `S::T'. We need to resolve it to the actual type before we
23251 try to define it. */
23252 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23254 class_type
= resolve_typename_type (TREE_TYPE (type
),
23255 /*only_current_p=*/false);
23256 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23257 type
= TYPE_NAME (class_type
);
23260 cp_parser_error (parser
, "could not resolve typename type");
23261 type
= error_mark_node
;
23265 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23266 == error_mark_node
)
23272 class_type
= current_class_type
;
23273 /* Enter the scope indicated by the nested-name-specifier. */
23274 pushed_scope
= push_scope (nested_name_specifier
);
23275 /* Get the canonical version of this type. */
23276 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23277 /* Call push_template_decl if it seems like we should be defining a
23278 template either from the template headers or the type we're
23279 defining, so that we diagnose both extra and missing headers. */
23280 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23281 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23282 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23284 type
= push_template_decl (type
);
23285 if (type
== error_mark_node
)
23292 type
= TREE_TYPE (type
);
23293 *nested_name_specifier_p
= true;
23295 else /* The name is not a nested name. */
23297 /* If the class was unnamed, create a dummy name. */
23299 id
= make_anon_name ();
23300 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23301 ? ts_within_enclosing_non_class
23303 type
= xref_tag (class_key
, id
, tag_scope
,
23304 parser
->num_template_parameter_lists
);
23307 /* Indicate whether this class was declared as a `class' or as a
23309 if (TREE_CODE (type
) == RECORD_TYPE
)
23310 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23311 cp_parser_check_class_key (class_key
, type
);
23313 /* If this type was already complete, and we see another definition,
23314 that's an error. */
23315 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23317 error_at (type_start_token
->location
, "redefinition of %q#T",
23319 inform (location_of (type
), "previous definition of %q#T",
23324 else if (type
== error_mark_node
)
23329 /* Apply attributes now, before any use of the class as a template
23330 argument in its base list. */
23331 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23332 fixup_attribute_variants (type
);
23335 /* We will have entered the scope containing the class; the names of
23336 base classes should be looked up in that context. For example:
23338 struct A { struct B {}; struct C; };
23339 struct A::C : B {};
23343 /* Get the list of base-classes, if there is one. */
23344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23346 /* PR59482: enter the class scope so that base-specifiers are looked
23350 bases
= cp_parser_base_clause (parser
);
23351 /* PR59482: get out of the previously pushed class scope so that the
23352 subsequent pops pop the right thing. */
23359 /* If we're really defining a class, process the base classes.
23360 If they're invalid, fail. */
23361 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23362 xref_basetypes (type
, bases
);
23365 /* Leave the scope given by the nested-name-specifier. We will
23366 enter the class scope itself while processing the members. */
23368 pop_scope (pushed_scope
);
23370 if (invalid_explicit_specialization_p
)
23372 end_specialization ();
23373 --parser
->num_template_parameter_lists
;
23377 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23378 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23379 CLASSTYPE_FINAL (type
) = 1;
23381 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23385 /* Parse a class-key.
23392 Returns the kind of class-key specified, or none_type to indicate
23395 static enum tag_types
23396 cp_parser_class_key (cp_parser
* parser
)
23399 enum tag_types tag_type
;
23401 /* Look for the class-key. */
23402 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23406 /* Check to see if the TOKEN is a class-key. */
23407 tag_type
= cp_parser_token_is_class_key (token
);
23409 cp_parser_error (parser
, "expected class-key");
23413 /* Parse a type-parameter-key.
23415 type-parameter-key:
23421 cp_parser_type_parameter_key (cp_parser
* parser
)
23423 /* Look for the type-parameter-key. */
23424 enum tag_types tag_type
= none_type
;
23425 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23426 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23428 cp_lexer_consume_token (parser
->lexer
);
23429 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23430 /* typename is not allowed in a template template parameter
23431 by the standard until C++17. */
23432 pedwarn (token
->location
, OPT_Wpedantic
,
23433 "ISO C++ forbids typename key in template template parameter;"
23434 " use -std=c++17 or -std=gnu++17");
23437 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23442 /* Parse an (optional) member-specification.
23444 member-specification:
23445 member-declaration member-specification [opt]
23446 access-specifier : member-specification [opt] */
23449 cp_parser_member_specification_opt (cp_parser
* parser
)
23456 /* Peek at the next token. */
23457 token
= cp_lexer_peek_token (parser
->lexer
);
23458 /* If it's a `}', or EOF then we've seen all the members. */
23459 if (token
->type
== CPP_CLOSE_BRACE
23460 || token
->type
== CPP_EOF
23461 || token
->type
== CPP_PRAGMA_EOL
)
23464 /* See if this token is a keyword. */
23465 keyword
= token
->keyword
;
23469 case RID_PROTECTED
:
23471 /* Consume the access-specifier. */
23472 cp_lexer_consume_token (parser
->lexer
);
23473 /* Remember which access-specifier is active. */
23474 current_access_specifier
= token
->u
.value
;
23475 /* Look for the `:'. */
23476 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23480 /* Accept #pragmas at class scope. */
23481 if (token
->type
== CPP_PRAGMA
)
23483 cp_parser_pragma (parser
, pragma_member
, NULL
);
23487 /* Otherwise, the next construction must be a
23488 member-declaration. */
23489 cp_parser_member_declaration (parser
);
23494 /* Parse a member-declaration.
23496 member-declaration:
23497 decl-specifier-seq [opt] member-declarator-list [opt] ;
23498 function-definition ; [opt]
23499 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23501 template-declaration
23504 member-declarator-list:
23506 member-declarator-list , member-declarator
23509 declarator pure-specifier [opt]
23510 declarator constant-initializer [opt]
23511 identifier [opt] : constant-expression
23515 member-declaration:
23516 __extension__ member-declaration
23519 declarator attributes [opt] pure-specifier [opt]
23520 declarator attributes [opt] constant-initializer [opt]
23521 identifier [opt] attributes [opt] : constant-expression
23525 member-declaration:
23526 static_assert-declaration */
23529 cp_parser_member_declaration (cp_parser
* parser
)
23531 cp_decl_specifier_seq decl_specifiers
;
23532 tree prefix_attributes
;
23534 int declares_class_or_enum
;
23536 cp_token
*token
= NULL
;
23537 cp_token
*decl_spec_token_start
= NULL
;
23538 cp_token
*initializer_token_start
= NULL
;
23539 int saved_pedantic
;
23540 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23542 /* Check for the `__extension__' keyword. */
23543 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23546 cp_parser_member_declaration (parser
);
23547 /* Restore the old value of the PEDANTIC flag. */
23548 pedantic
= saved_pedantic
;
23553 /* Check for a template-declaration. */
23554 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23556 /* An explicit specialization here is an error condition, and we
23557 expect the specialization handler to detect and report this. */
23558 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23559 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23560 cp_parser_explicit_specialization (parser
);
23562 cp_parser_template_declaration (parser
, /*member_p=*/true);
23566 /* Check for a template introduction. */
23567 else if (cp_parser_template_declaration_after_export (parser
, true))
23570 /* Check for a using-declaration. */
23571 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23573 if (cxx_dialect
< cxx11
)
23575 /* Parse the using-declaration. */
23576 cp_parser_using_declaration (parser
,
23577 /*access_declaration_p=*/false);
23583 bool alias_decl_expected
;
23584 cp_parser_parse_tentatively (parser
);
23585 decl
= cp_parser_alias_declaration (parser
);
23586 /* Note that if we actually see the '=' token after the
23587 identifier, cp_parser_alias_declaration commits the
23588 tentative parse. In that case, we really expect an
23589 alias-declaration. Otherwise, we expect a using
23591 alias_decl_expected
=
23592 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23593 cp_parser_parse_definitely (parser
);
23595 if (alias_decl_expected
)
23596 finish_member_declaration (decl
);
23598 cp_parser_using_declaration (parser
,
23599 /*access_declaration_p=*/false);
23604 /* Check for @defs. */
23605 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23608 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23609 ivar
= ivar_chains
;
23613 ivar
= TREE_CHAIN (member
);
23614 TREE_CHAIN (member
) = NULL_TREE
;
23615 finish_member_declaration (member
);
23620 /* If the next token is `static_assert' we have a static assertion. */
23621 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23623 cp_parser_static_assert (parser
, /*member_p=*/true);
23627 parser
->colon_corrects_to_scope_p
= false;
23629 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23632 /* Parse the decl-specifier-seq. */
23633 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23634 cp_parser_decl_specifier_seq (parser
,
23635 CP_PARSER_FLAGS_OPTIONAL
,
23637 &declares_class_or_enum
);
23638 /* Check for an invalid type-name. */
23639 if (!decl_specifiers
.any_type_specifiers_p
23640 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23642 /* If there is no declarator, then the decl-specifier-seq should
23644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23646 /* If there was no decl-specifier-seq, and the next token is a
23647 `;', then we have something like:
23653 Each member-declaration shall declare at least one member
23654 name of the class. */
23655 if (!decl_specifiers
.any_specifiers_p
)
23657 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23658 if (!in_system_header_at (token
->location
))
23660 gcc_rich_location
richloc (token
->location
);
23661 richloc
.add_fixit_remove ();
23662 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23669 /* See if this declaration is a friend. */
23670 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23671 /* If there were decl-specifiers, check to see if there was
23672 a class-declaration. */
23673 type
= check_tag_decl (&decl_specifiers
,
23674 /*explicit_type_instantiation_p=*/false);
23675 /* Nested classes have already been added to the class, but
23676 a `friend' needs to be explicitly registered. */
23679 /* If the `friend' keyword was present, the friend must
23680 be introduced with a class-key. */
23681 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23682 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23683 "in C++03 a class-key must be used "
23684 "when declaring a friend");
23687 template <typename T> struct A {
23688 friend struct A<T>::B;
23691 A<T>::B will be represented by a TYPENAME_TYPE, and
23692 therefore not recognized by check_tag_decl. */
23695 type
= decl_specifiers
.type
;
23696 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23697 type
= TREE_TYPE (type
);
23699 if (!type
|| !TYPE_P (type
))
23700 error_at (decl_spec_token_start
->location
,
23701 "friend declaration does not name a class or "
23704 make_friend_class (current_class_type
, type
,
23705 /*complain=*/true);
23707 /* If there is no TYPE, an error message will already have
23709 else if (!type
|| type
== error_mark_node
)
23711 /* An anonymous aggregate has to be handled specially; such
23712 a declaration really declares a data member (with a
23713 particular type), as opposed to a nested class. */
23714 else if (ANON_AGGR_TYPE_P (type
))
23717 if (decl_specifiers
.storage_class
!= sc_none
)
23718 error_at (decl_spec_token_start
->location
,
23719 "a storage class on an anonymous aggregate "
23720 "in class scope is not allowed");
23722 /* Remove constructors and such from TYPE, now that we
23723 know it is an anonymous aggregate. */
23724 fixup_anonymous_aggr (type
);
23725 /* And make the corresponding data member. */
23726 decl
= build_decl (decl_spec_token_start
->location
,
23727 FIELD_DECL
, NULL_TREE
, type
);
23728 /* Add it to the class. */
23729 finish_member_declaration (decl
);
23732 cp_parser_check_access_in_redeclaration
23734 decl_spec_token_start
->location
);
23739 bool assume_semicolon
= false;
23741 /* Clear attributes from the decl_specifiers but keep them
23742 around as prefix attributes that apply them to the entity
23744 prefix_attributes
= decl_specifiers
.attributes
;
23745 decl_specifiers
.attributes
= NULL_TREE
;
23747 /* See if these declarations will be friends. */
23748 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23750 /* Keep going until we hit the `;' at the end of the
23752 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23754 tree attributes
= NULL_TREE
;
23755 tree first_attribute
;
23757 bool named_bitfld
= false;
23759 /* Peek at the next token. */
23760 token
= cp_lexer_peek_token (parser
->lexer
);
23762 /* The following code wants to know early if it is a bit-field
23763 or some other declaration. Attributes can appear before
23764 the `:' token. Skip over them without consuming any tokens
23765 to peek if they are followed by `:'. */
23766 if (cp_next_tokens_can_be_attribute_p (parser
)
23767 || (token
->type
== CPP_NAME
23768 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23769 && (named_bitfld
= true)))
23772 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23773 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23776 /* Check for a bitfield declaration. */
23777 if (token
->type
== CPP_COLON
23778 || (token
->type
== CPP_NAME
23779 && token
== cp_lexer_peek_token (parser
->lexer
)
23780 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23781 && (named_bitfld
= true)))
23785 tree late_attributes
= NULL_TREE
;
23788 identifier
= cp_parser_identifier (parser
);
23790 identifier
= NULL_TREE
;
23792 /* Look for attributes that apply to the bitfield. */
23793 attributes
= cp_parser_attributes_opt (parser
);
23795 /* Consume the `:' token. */
23796 cp_lexer_consume_token (parser
->lexer
);
23798 /* Get the width of the bitfield. */
23799 width
= cp_parser_constant_expression (parser
, false, NULL
,
23800 cxx_dialect
>= cxx11
);
23802 /* In C++2A and as extension for C++11 and above we allow
23803 default member initializers for bit-fields. */
23804 initializer
= NULL_TREE
;
23805 if (cxx_dialect
>= cxx11
23806 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23807 || cp_lexer_next_token_is (parser
->lexer
,
23811 = cp_lexer_peek_token (parser
->lexer
)->location
;
23812 if (cxx_dialect
< cxx2a
23813 && !in_system_header_at (loc
)
23814 && identifier
!= NULL_TREE
)
23816 "default member initializers for bit-fields "
23817 "only available with -std=c++2a or "
23820 initializer
= cp_parser_save_nsdmi (parser
);
23821 if (identifier
== NULL_TREE
)
23823 error_at (loc
, "default member initializer for "
23824 "unnamed bit-field");
23825 initializer
= NULL_TREE
;
23830 /* Look for attributes that apply to the bitfield after
23831 the `:' token and width. This is where GCC used to
23832 parse attributes in the past, pedwarn if there is
23833 a std attribute. */
23834 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23835 pedwarn (input_location
, OPT_Wpedantic
,
23836 "ISO C++ allows bit-field attributes only "
23837 "before the %<:%> token");
23839 late_attributes
= cp_parser_attributes_opt (parser
);
23842 attributes
= attr_chainon (attributes
, late_attributes
);
23844 /* Remember which attributes are prefix attributes and
23846 first_attribute
= attributes
;
23847 /* Combine the attributes. */
23848 attributes
= attr_chainon (prefix_attributes
, attributes
);
23850 /* Create the bitfield declaration. */
23851 decl
= grokbitfield (identifier
23852 ? make_id_declarator (NULL_TREE
,
23857 width
, initializer
,
23862 cp_declarator
*declarator
;
23863 tree asm_specification
;
23864 int ctor_dtor_or_conv_p
;
23866 /* Parse the declarator. */
23868 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23869 &ctor_dtor_or_conv_p
,
23870 /*parenthesized_p=*/NULL
,
23874 /* If something went wrong parsing the declarator, make sure
23875 that we at least consume some tokens. */
23876 if (declarator
== cp_error_declarator
)
23878 /* Skip to the end of the statement. */
23879 cp_parser_skip_to_end_of_statement (parser
);
23880 /* If the next token is not a semicolon, that is
23881 probably because we just skipped over the body of
23882 a function. So, we consume a semicolon if
23883 present, but do not issue an error message if it
23885 if (cp_lexer_next_token_is (parser
->lexer
,
23887 cp_lexer_consume_token (parser
->lexer
);
23891 if (declares_class_or_enum
& 2)
23892 cp_parser_check_for_definition_in_return_type
23893 (declarator
, decl_specifiers
.type
,
23894 decl_specifiers
.locations
[ds_type_spec
]);
23896 /* Look for an asm-specification. */
23897 asm_specification
= cp_parser_asm_specification_opt (parser
);
23898 /* Look for attributes that apply to the declaration. */
23899 attributes
= cp_parser_attributes_opt (parser
);
23900 /* Remember which attributes are prefix attributes and
23902 first_attribute
= attributes
;
23903 /* Combine the attributes. */
23904 attributes
= attr_chainon (prefix_attributes
, attributes
);
23906 /* If it's an `=', then we have a constant-initializer or a
23907 pure-specifier. It is not correct to parse the
23908 initializer before registering the member declaration
23909 since the member declaration should be in scope while
23910 its initializer is processed. However, the rest of the
23911 front end does not yet provide an interface that allows
23912 us to handle this correctly. */
23913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23917 A pure-specifier shall be used only in the declaration of
23918 a virtual function.
23920 A member-declarator can contain a constant-initializer
23921 only if it declares a static member of integral or
23924 Therefore, if the DECLARATOR is for a function, we look
23925 for a pure-specifier; otherwise, we look for a
23926 constant-initializer. When we call `grokfield', it will
23927 perform more stringent semantics checks. */
23928 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23929 if (function_declarator_p (declarator
)
23930 || (decl_specifiers
.type
23931 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23932 && declarator
->kind
== cdk_id
23933 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23934 == FUNCTION_TYPE
)))
23935 initializer
= cp_parser_pure_specifier (parser
);
23936 else if (decl_specifiers
.storage_class
!= sc_static
)
23937 initializer
= cp_parser_save_nsdmi (parser
);
23938 else if (cxx_dialect
>= cxx11
)
23941 /* Don't require a constant rvalue in C++11, since we
23942 might want a reference constant. We'll enforce
23943 constancy later. */
23944 cp_lexer_consume_token (parser
->lexer
);
23945 /* Parse the initializer. */
23946 initializer
= cp_parser_initializer_clause (parser
,
23950 /* Parse the initializer. */
23951 initializer
= cp_parser_constant_initializer (parser
);
23953 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23954 && !function_declarator_p (declarator
))
23957 if (decl_specifiers
.storage_class
!= sc_static
)
23958 initializer
= cp_parser_save_nsdmi (parser
);
23960 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23962 /* Otherwise, there is no initializer. */
23964 initializer
= NULL_TREE
;
23966 /* See if we are probably looking at a function
23967 definition. We are certainly not looking at a
23968 member-declarator. Calling `grokfield' has
23969 side-effects, so we must not do it unless we are sure
23970 that we are looking at a member-declarator. */
23971 if (cp_parser_token_starts_function_definition_p
23972 (cp_lexer_peek_token (parser
->lexer
)))
23974 /* The grammar does not allow a pure-specifier to be
23975 used when a member function is defined. (It is
23976 possible that this fact is an oversight in the
23977 standard, since a pure function may be defined
23978 outside of the class-specifier. */
23979 if (initializer
&& initializer_token_start
)
23980 error_at (initializer_token_start
->location
,
23981 "pure-specifier on function-definition");
23982 decl
= cp_parser_save_member_function_body (parser
,
23986 if (parser
->fully_implicit_function_template_p
)
23987 decl
= finish_fully_implicit_template (parser
, decl
);
23988 /* If the member was not a friend, declare it here. */
23990 finish_member_declaration (decl
);
23991 /* Peek at the next token. */
23992 token
= cp_lexer_peek_token (parser
->lexer
);
23993 /* If the next token is a semicolon, consume it. */
23994 if (token
->type
== CPP_SEMICOLON
)
23996 location_t semicolon_loc
23997 = cp_lexer_consume_token (parser
->lexer
)->location
;
23998 gcc_rich_location
richloc (semicolon_loc
);
23999 richloc
.add_fixit_remove ();
24000 warning_at (&richloc
, OPT_Wextra_semi
,
24001 "extra %<;%> after in-class "
24002 "function definition");
24007 if (declarator
->kind
== cdk_function
)
24008 declarator
->id_loc
= token
->location
;
24009 /* Create the declaration. */
24010 decl
= grokfield (declarator
, &decl_specifiers
,
24011 initializer
, /*init_const_expr_p=*/true,
24012 asm_specification
, attributes
);
24013 if (parser
->fully_implicit_function_template_p
)
24016 finish_fully_implicit_template (parser
, 0);
24018 decl
= finish_fully_implicit_template (parser
, decl
);
24022 cp_finalize_omp_declare_simd (parser
, decl
);
24023 cp_finalize_oacc_routine (parser
, decl
, false);
24025 /* Reset PREFIX_ATTRIBUTES. */
24026 if (attributes
!= error_mark_node
)
24028 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24029 attributes
= TREE_CHAIN (attributes
);
24031 TREE_CHAIN (attributes
) = NULL_TREE
;
24034 /* If there is any qualification still in effect, clear it
24035 now; we will be starting fresh with the next declarator. */
24036 parser
->scope
= NULL_TREE
;
24037 parser
->qualifying_scope
= NULL_TREE
;
24038 parser
->object_scope
= NULL_TREE
;
24039 /* If it's a `,', then there are more declarators. */
24040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24042 cp_lexer_consume_token (parser
->lexer
);
24043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24045 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24046 gcc_rich_location
richloc (token
->location
);
24047 richloc
.add_fixit_remove ();
24048 error_at (&richloc
, "stray %<,%> at end of "
24049 "member declaration");
24052 /* If the next token isn't a `;', then we have a parse error. */
24053 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24056 /* The next token might be a ways away from where the
24057 actual semicolon is missing. Find the previous token
24058 and use that for our error position. */
24059 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24060 gcc_rich_location
richloc (token
->location
);
24061 richloc
.add_fixit_insert_after (";");
24062 error_at (&richloc
, "expected %<;%> at end of "
24063 "member declaration");
24065 /* Assume that the user meant to provide a semicolon. If
24066 we were to cp_parser_skip_to_end_of_statement, we might
24067 skip to a semicolon inside a member function definition
24068 and issue nonsensical error messages. */
24069 assume_semicolon
= true;
24074 /* Add DECL to the list of members. */
24076 /* Explicitly include, eg, NSDMIs, for better error
24077 recovery (c++/58650). */
24078 || !DECL_DECLARES_FUNCTION_P (decl
))
24079 finish_member_declaration (decl
);
24081 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24082 cp_parser_save_default_args (parser
, decl
);
24083 else if (TREE_CODE (decl
) == FIELD_DECL
24084 && DECL_INITIAL (decl
))
24085 /* Add DECL to the queue of NSDMI to be parsed later. */
24086 vec_safe_push (unparsed_nsdmis
, decl
);
24089 if (assume_semicolon
)
24094 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24096 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24099 /* Parse a pure-specifier.
24104 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24105 Otherwise, ERROR_MARK_NODE is returned. */
24108 cp_parser_pure_specifier (cp_parser
* parser
)
24112 /* Look for the `=' token. */
24113 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24114 return error_mark_node
;
24115 /* Look for the `0' token. */
24116 token
= cp_lexer_peek_token (parser
->lexer
);
24118 if (token
->type
== CPP_EOF
24119 || token
->type
== CPP_PRAGMA_EOL
)
24120 return error_mark_node
;
24122 cp_lexer_consume_token (parser
->lexer
);
24124 /* Accept = default or = delete in c++0x mode. */
24125 if (token
->keyword
== RID_DEFAULT
24126 || token
->keyword
== RID_DELETE
)
24128 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24129 return token
->u
.value
;
24132 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24133 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24135 cp_parser_error (parser
,
24136 "invalid pure specifier (only %<= 0%> is allowed)");
24137 cp_parser_skip_to_end_of_statement (parser
);
24138 return error_mark_node
;
24140 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24142 error_at (token
->location
, "templates may not be %<virtual%>");
24143 return error_mark_node
;
24146 return integer_zero_node
;
24149 /* Parse a constant-initializer.
24151 constant-initializer:
24152 = constant-expression
24154 Returns a representation of the constant-expression. */
24157 cp_parser_constant_initializer (cp_parser
* parser
)
24159 /* Look for the `=' token. */
24160 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24161 return error_mark_node
;
24163 /* It is invalid to write:
24165 struct S { static const int i = { 7 }; };
24168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24170 cp_parser_error (parser
,
24171 "a brace-enclosed initializer is not allowed here");
24172 /* Consume the opening brace. */
24173 matching_braces braces
;
24174 braces
.consume_open (parser
);
24175 /* Skip the initializer. */
24176 cp_parser_skip_to_closing_brace (parser
);
24177 /* Look for the trailing `}'. */
24178 braces
.require_close (parser
);
24180 return error_mark_node
;
24183 return cp_parser_constant_expression (parser
);
24186 /* Derived classes [gram.class.derived] */
24188 /* Parse a base-clause.
24191 : base-specifier-list
24193 base-specifier-list:
24194 base-specifier ... [opt]
24195 base-specifier-list , base-specifier ... [opt]
24197 Returns a TREE_LIST representing the base-classes, in the order in
24198 which they were declared. The representation of each node is as
24199 described by cp_parser_base_specifier.
24201 In the case that no bases are specified, this function will return
24202 NULL_TREE, not ERROR_MARK_NODE. */
24205 cp_parser_base_clause (cp_parser
* parser
)
24207 tree bases
= NULL_TREE
;
24209 /* Look for the `:' that begins the list. */
24210 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24212 /* Scan the base-specifier-list. */
24217 bool pack_expansion_p
= false;
24219 /* Look for the base-specifier. */
24220 base
= cp_parser_base_specifier (parser
);
24221 /* Look for the (optional) ellipsis. */
24222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24224 /* Consume the `...'. */
24225 cp_lexer_consume_token (parser
->lexer
);
24227 pack_expansion_p
= true;
24230 /* Add BASE to the front of the list. */
24231 if (base
&& base
!= error_mark_node
)
24233 if (pack_expansion_p
)
24234 /* Make this a pack expansion type. */
24235 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24237 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24239 TREE_CHAIN (base
) = bases
;
24243 /* Peek at the next token. */
24244 token
= cp_lexer_peek_token (parser
->lexer
);
24245 /* If it's not a comma, then the list is complete. */
24246 if (token
->type
!= CPP_COMMA
)
24248 /* Consume the `,'. */
24249 cp_lexer_consume_token (parser
->lexer
);
24252 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24253 base class had a qualified name. However, the next name that
24254 appears is certainly not qualified. */
24255 parser
->scope
= NULL_TREE
;
24256 parser
->qualifying_scope
= NULL_TREE
;
24257 parser
->object_scope
= NULL_TREE
;
24259 return nreverse (bases
);
24262 /* Parse a base-specifier.
24265 :: [opt] nested-name-specifier [opt] class-name
24266 virtual access-specifier [opt] :: [opt] nested-name-specifier
24268 access-specifier virtual [opt] :: [opt] nested-name-specifier
24271 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24272 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24273 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24274 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24277 cp_parser_base_specifier (cp_parser
* parser
)
24281 bool virtual_p
= false;
24282 bool duplicate_virtual_error_issued_p
= false;
24283 bool duplicate_access_error_issued_p
= false;
24284 bool class_scope_p
, template_p
;
24285 tree access
= access_default_node
;
24288 /* Process the optional `virtual' and `access-specifier'. */
24291 /* Peek at the next token. */
24292 token
= cp_lexer_peek_token (parser
->lexer
);
24293 /* Process `virtual'. */
24294 switch (token
->keyword
)
24297 /* If `virtual' appears more than once, issue an error. */
24298 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24300 cp_parser_error (parser
,
24301 "%<virtual%> specified more than once in base-specifier");
24302 duplicate_virtual_error_issued_p
= true;
24307 /* Consume the `virtual' token. */
24308 cp_lexer_consume_token (parser
->lexer
);
24313 case RID_PROTECTED
:
24315 /* If more than one access specifier appears, issue an
24317 if (access
!= access_default_node
24318 && !duplicate_access_error_issued_p
)
24320 cp_parser_error (parser
,
24321 "more than one access specifier in base-specifier");
24322 duplicate_access_error_issued_p
= true;
24325 access
= ridpointers
[(int) token
->keyword
];
24327 /* Consume the access-specifier. */
24328 cp_lexer_consume_token (parser
->lexer
);
24337 /* It is not uncommon to see programs mechanically, erroneously, use
24338 the 'typename' keyword to denote (dependent) qualified types
24339 as base classes. */
24340 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24342 token
= cp_lexer_peek_token (parser
->lexer
);
24343 if (!processing_template_decl
)
24344 error_at (token
->location
,
24345 "keyword %<typename%> not allowed outside of templates");
24347 error_at (token
->location
,
24348 "keyword %<typename%> not allowed in this context "
24349 "(the base class is implicitly a type)");
24350 cp_lexer_consume_token (parser
->lexer
);
24353 /* Look for the optional `::' operator. */
24354 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24355 /* Look for the nested-name-specifier. The simplest way to
24360 The keyword `typename' is not permitted in a base-specifier or
24361 mem-initializer; in these contexts a qualified name that
24362 depends on a template-parameter is implicitly assumed to be a
24365 is to pretend that we have seen the `typename' keyword at this
24367 cp_parser_nested_name_specifier_opt (parser
,
24368 /*typename_keyword_p=*/true,
24369 /*check_dependency_p=*/true,
24371 /*is_declaration=*/true);
24372 /* If the base class is given by a qualified name, assume that names
24373 we see are type names or templates, as appropriate. */
24374 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24375 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24378 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24379 /* DR 950 allows decltype as a base-specifier. */
24380 type
= cp_parser_decltype (parser
);
24383 /* Otherwise, look for the class-name. */
24384 type
= cp_parser_class_name (parser
,
24388 /*check_dependency_p=*/true,
24389 /*class_head_p=*/false,
24390 /*is_declaration=*/true);
24391 type
= TREE_TYPE (type
);
24394 if (type
== error_mark_node
)
24395 return error_mark_node
;
24397 return finish_base_specifier (type
, access
, virtual_p
);
24400 /* Exception handling [gram.exception] */
24402 /* Parse an (optional) noexcept-specification.
24404 noexcept-specification:
24405 noexcept ( constant-expression ) [opt]
24407 If no noexcept-specification is present, returns NULL_TREE.
24408 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24409 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24410 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24411 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24412 in which case a boolean condition is returned instead. */
24415 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24416 bool require_constexpr
,
24417 bool* consumed_expr
,
24421 const char *saved_message
;
24423 /* Peek at the next token. */
24424 token
= cp_lexer_peek_token (parser
->lexer
);
24426 /* Is it a noexcept-specification? */
24427 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24430 cp_lexer_consume_token (parser
->lexer
);
24432 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24434 matching_parens parens
;
24435 parens
.consume_open (parser
);
24437 if (require_constexpr
)
24439 /* Types may not be defined in an exception-specification. */
24440 saved_message
= parser
->type_definition_forbidden_message
;
24441 parser
->type_definition_forbidden_message
24442 = G_("types may not be defined in an exception-specification");
24444 expr
= cp_parser_constant_expression (parser
);
24446 /* Restore the saved message. */
24447 parser
->type_definition_forbidden_message
= saved_message
;
24451 expr
= cp_parser_expression (parser
);
24452 *consumed_expr
= true;
24455 parens
.require_close (parser
);
24459 expr
= boolean_true_node
;
24460 if (!require_constexpr
)
24461 *consumed_expr
= false;
24464 /* We cannot build a noexcept-spec right away because this will check
24465 that expr is a constexpr. */
24467 return build_noexcept_spec (expr
, tf_warning_or_error
);
24475 /* Parse an (optional) exception-specification.
24477 exception-specification:
24478 throw ( type-id-list [opt] )
24480 Returns a TREE_LIST representing the exception-specification. The
24481 TREE_VALUE of each node is a type. */
24484 cp_parser_exception_specification_opt (cp_parser
* parser
)
24488 const char *saved_message
;
24490 /* Peek at the next token. */
24491 token
= cp_lexer_peek_token (parser
->lexer
);
24493 /* Is it a noexcept-specification? */
24494 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24496 if (type_id_list
!= NULL_TREE
)
24497 return type_id_list
;
24499 /* If it's not `throw', then there's no exception-specification. */
24500 if (!cp_parser_is_keyword (token
, RID_THROW
))
24503 location_t loc
= token
->location
;
24505 /* Consume the `throw'. */
24506 cp_lexer_consume_token (parser
->lexer
);
24508 /* Look for the `('. */
24509 matching_parens parens
;
24510 parens
.require_open (parser
);
24512 /* Peek at the next token. */
24513 token
= cp_lexer_peek_token (parser
->lexer
);
24514 /* If it's not a `)', then there is a type-id-list. */
24515 if (token
->type
!= CPP_CLOSE_PAREN
)
24517 /* Types may not be defined in an exception-specification. */
24518 saved_message
= parser
->type_definition_forbidden_message
;
24519 parser
->type_definition_forbidden_message
24520 = G_("types may not be defined in an exception-specification");
24521 /* Parse the type-id-list. */
24522 type_id_list
= cp_parser_type_id_list (parser
);
24523 /* Restore the saved message. */
24524 parser
->type_definition_forbidden_message
= saved_message
;
24526 if (cxx_dialect
>= cxx17
)
24528 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24530 type_id_list
= NULL_TREE
;
24532 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24533 warning_at (loc
, OPT_Wdeprecated
,
24534 "dynamic exception specifications are deprecated in "
24537 /* In C++17, throw() is equivalent to noexcept (true). throw()
24538 is deprecated in C++11 and above as well, but is still widely used,
24539 so don't warn about it yet. */
24540 else if (cxx_dialect
>= cxx17
)
24541 type_id_list
= noexcept_true_spec
;
24543 type_id_list
= empty_except_spec
;
24545 /* Look for the `)'. */
24546 parens
.require_close (parser
);
24548 return type_id_list
;
24551 /* Parse an (optional) type-id-list.
24555 type-id-list , type-id ... [opt]
24557 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24558 in the order that the types were presented. */
24561 cp_parser_type_id_list (cp_parser
* parser
)
24563 tree types
= NULL_TREE
;
24570 token
= cp_lexer_peek_token (parser
->lexer
);
24572 /* Get the next type-id. */
24573 type
= cp_parser_type_id (parser
);
24574 /* Check for invalid 'auto'. */
24575 if (flag_concepts
&& type_uses_auto (type
))
24577 error_at (token
->location
,
24578 "invalid use of %<auto%> in exception-specification");
24579 type
= error_mark_node
;
24581 /* Parse the optional ellipsis. */
24582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24584 /* Consume the `...'. */
24585 cp_lexer_consume_token (parser
->lexer
);
24587 /* Turn the type into a pack expansion expression. */
24588 type
= make_pack_expansion (type
);
24590 /* Add it to the list. */
24591 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24592 /* Peek at the next token. */
24593 token
= cp_lexer_peek_token (parser
->lexer
);
24594 /* If it is not a `,', we are done. */
24595 if (token
->type
!= CPP_COMMA
)
24597 /* Consume the `,'. */
24598 cp_lexer_consume_token (parser
->lexer
);
24601 return nreverse (types
);
24604 /* Parse a try-block.
24607 try compound-statement handler-seq */
24610 cp_parser_try_block (cp_parser
* parser
)
24614 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24615 if (parser
->in_function_body
24616 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24617 error ("%<try%> in %<constexpr%> function");
24619 try_block
= begin_try_block ();
24620 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24621 finish_try_block (try_block
);
24622 cp_parser_handler_seq (parser
);
24623 finish_handler_sequence (try_block
);
24628 /* Parse a function-try-block.
24630 function-try-block:
24631 try ctor-initializer [opt] function-body handler-seq */
24634 cp_parser_function_try_block (cp_parser
* parser
)
24636 tree compound_stmt
;
24639 /* Look for the `try' keyword. */
24640 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24642 /* Let the rest of the front end know where we are. */
24643 try_block
= begin_function_try_block (&compound_stmt
);
24644 /* Parse the function-body. */
24645 cp_parser_ctor_initializer_opt_and_function_body
24646 (parser
, /*in_function_try_block=*/true);
24647 /* We're done with the `try' part. */
24648 finish_function_try_block (try_block
);
24649 /* Parse the handlers. */
24650 cp_parser_handler_seq (parser
);
24651 /* We're done with the handlers. */
24652 finish_function_handler_sequence (try_block
, compound_stmt
);
24655 /* Parse a handler-seq.
24658 handler handler-seq [opt] */
24661 cp_parser_handler_seq (cp_parser
* parser
)
24667 /* Parse the handler. */
24668 cp_parser_handler (parser
);
24669 /* Peek at the next token. */
24670 token
= cp_lexer_peek_token (parser
->lexer
);
24671 /* If it's not `catch' then there are no more handlers. */
24672 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24677 /* Parse a handler.
24680 catch ( exception-declaration ) compound-statement */
24683 cp_parser_handler (cp_parser
* parser
)
24688 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24689 handler
= begin_handler ();
24690 matching_parens parens
;
24691 parens
.require_open (parser
);
24692 declaration
= cp_parser_exception_declaration (parser
);
24693 finish_handler_parms (declaration
, handler
);
24694 parens
.require_close (parser
);
24695 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24696 finish_handler (handler
);
24699 /* Parse an exception-declaration.
24701 exception-declaration:
24702 type-specifier-seq declarator
24703 type-specifier-seq abstract-declarator
24707 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24708 ellipsis variant is used. */
24711 cp_parser_exception_declaration (cp_parser
* parser
)
24713 cp_decl_specifier_seq type_specifiers
;
24714 cp_declarator
*declarator
;
24715 const char *saved_message
;
24717 /* If it's an ellipsis, it's easy to handle. */
24718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24720 /* Consume the `...' token. */
24721 cp_lexer_consume_token (parser
->lexer
);
24725 /* Types may not be defined in exception-declarations. */
24726 saved_message
= parser
->type_definition_forbidden_message
;
24727 parser
->type_definition_forbidden_message
24728 = G_("types may not be defined in exception-declarations");
24730 /* Parse the type-specifier-seq. */
24731 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24732 /*is_trailing_return=*/false,
24734 /* If it's a `)', then there is no declarator. */
24735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24738 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24739 /*ctor_dtor_or_conv_p=*/NULL
,
24740 /*parenthesized_p=*/NULL
,
24741 /*member_p=*/false,
24742 /*friend_p=*/false);
24744 /* Restore the saved message. */
24745 parser
->type_definition_forbidden_message
= saved_message
;
24747 if (!type_specifiers
.any_specifiers_p
)
24748 return error_mark_node
;
24750 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24753 /* Parse a throw-expression.
24756 throw assignment-expression [opt]
24758 Returns a THROW_EXPR representing the throw-expression. */
24761 cp_parser_throw_expression (cp_parser
* parser
)
24766 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24767 token
= cp_lexer_peek_token (parser
->lexer
);
24768 /* Figure out whether or not there is an assignment-expression
24769 following the "throw" keyword. */
24770 if (token
->type
== CPP_COMMA
24771 || token
->type
== CPP_SEMICOLON
24772 || token
->type
== CPP_CLOSE_PAREN
24773 || token
->type
== CPP_CLOSE_SQUARE
24774 || token
->type
== CPP_CLOSE_BRACE
24775 || token
->type
== CPP_COLON
)
24776 expression
= NULL_TREE
;
24778 expression
= cp_parser_assignment_expression (parser
);
24780 return build_throw (expression
);
24783 /* GNU Extensions */
24785 /* Parse an (optional) asm-specification.
24788 asm ( string-literal )
24790 If the asm-specification is present, returns a STRING_CST
24791 corresponding to the string-literal. Otherwise, returns
24795 cp_parser_asm_specification_opt (cp_parser
* parser
)
24798 tree asm_specification
;
24800 /* Peek at the next token. */
24801 token
= cp_lexer_peek_token (parser
->lexer
);
24802 /* If the next token isn't the `asm' keyword, then there's no
24803 asm-specification. */
24804 if (!cp_parser_is_keyword (token
, RID_ASM
))
24807 /* Consume the `asm' token. */
24808 cp_lexer_consume_token (parser
->lexer
);
24809 /* Look for the `('. */
24810 matching_parens parens
;
24811 parens
.require_open (parser
);
24813 /* Look for the string-literal. */
24814 asm_specification
= cp_parser_string_literal (parser
, false, false);
24816 /* Look for the `)'. */
24817 parens
.require_close (parser
);
24819 return asm_specification
;
24822 /* Parse an asm-operand-list.
24826 asm-operand-list , asm-operand
24829 string-literal ( expression )
24830 [ string-literal ] string-literal ( expression )
24832 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24833 each node is the expression. The TREE_PURPOSE is itself a
24834 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24835 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24836 is a STRING_CST for the string literal before the parenthesis. Returns
24837 ERROR_MARK_NODE if any of the operands are invalid. */
24840 cp_parser_asm_operand_list (cp_parser
* parser
)
24842 tree asm_operands
= NULL_TREE
;
24843 bool invalid_operands
= false;
24847 tree string_literal
;
24851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24853 /* Consume the `[' token. */
24854 cp_lexer_consume_token (parser
->lexer
);
24855 /* Read the operand name. */
24856 name
= cp_parser_identifier (parser
);
24857 if (name
!= error_mark_node
)
24858 name
= build_string (IDENTIFIER_LENGTH (name
),
24859 IDENTIFIER_POINTER (name
));
24860 /* Look for the closing `]'. */
24861 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24865 /* Look for the string-literal. */
24866 string_literal
= cp_parser_string_literal (parser
, false, false);
24868 /* Look for the `('. */
24869 matching_parens parens
;
24870 parens
.require_open (parser
);
24871 /* Parse the expression. */
24872 expression
= cp_parser_expression (parser
);
24873 /* Look for the `)'. */
24874 parens
.require_close (parser
);
24876 if (name
== error_mark_node
24877 || string_literal
== error_mark_node
24878 || expression
== error_mark_node
)
24879 invalid_operands
= true;
24881 /* Add this operand to the list. */
24882 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24885 /* If the next token is not a `,', there are no more
24887 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24889 /* Consume the `,'. */
24890 cp_lexer_consume_token (parser
->lexer
);
24893 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24896 /* Parse an asm-clobber-list.
24900 asm-clobber-list , string-literal
24902 Returns a TREE_LIST, indicating the clobbers in the order that they
24903 appeared. The TREE_VALUE of each node is a STRING_CST. */
24906 cp_parser_asm_clobber_list (cp_parser
* parser
)
24908 tree clobbers
= NULL_TREE
;
24912 tree string_literal
;
24914 /* Look for the string literal. */
24915 string_literal
= cp_parser_string_literal (parser
, false, false);
24916 /* Add it to the list. */
24917 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24918 /* If the next token is not a `,', then the list is
24920 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24922 /* Consume the `,' token. */
24923 cp_lexer_consume_token (parser
->lexer
);
24929 /* Parse an asm-label-list.
24933 asm-label-list , identifier
24935 Returns a TREE_LIST, indicating the labels in the order that they
24936 appeared. The TREE_VALUE of each node is a label. */
24939 cp_parser_asm_label_list (cp_parser
* parser
)
24941 tree labels
= NULL_TREE
;
24945 tree identifier
, label
, name
;
24947 /* Look for the identifier. */
24948 identifier
= cp_parser_identifier (parser
);
24949 if (!error_operand_p (identifier
))
24951 label
= lookup_label (identifier
);
24952 if (TREE_CODE (label
) == LABEL_DECL
)
24954 TREE_USED (label
) = 1;
24955 check_goto (label
);
24956 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24957 IDENTIFIER_POINTER (identifier
));
24958 labels
= tree_cons (name
, label
, labels
);
24961 /* If the next token is not a `,', then the list is
24963 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24965 /* Consume the `,' token. */
24966 cp_lexer_consume_token (parser
->lexer
);
24969 return nreverse (labels
);
24972 /* Return TRUE iff the next tokens in the stream are possibly the
24973 beginning of a GNU extension attribute. */
24976 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24978 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24981 /* Return TRUE iff the next tokens in the stream are possibly the
24982 beginning of a standard C++-11 attribute specifier. */
24985 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24987 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24990 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24991 beginning of a standard C++-11 attribute specifier. */
24994 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24996 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24998 return (cxx_dialect
>= cxx11
24999 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25000 || (token
->type
== CPP_OPEN_SQUARE
25001 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25002 && token
->type
== CPP_OPEN_SQUARE
)));
25005 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25006 beginning of a GNU extension attribute. */
25009 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25011 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25013 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25016 /* Return true iff the next tokens can be the beginning of either a
25017 GNU attribute list, or a standard C++11 attribute sequence. */
25020 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25022 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25023 || cp_next_tokens_can_be_std_attribute_p (parser
));
25026 /* Return true iff the next Nth tokens can be the beginning of either
25027 a GNU attribute list, or a standard C++11 attribute sequence. */
25030 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25032 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25033 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25036 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25037 of GNU attributes, or return NULL. */
25040 cp_parser_attributes_opt (cp_parser
*parser
)
25042 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25043 return cp_parser_gnu_attributes_opt (parser
);
25044 return cp_parser_std_attribute_spec_seq (parser
);
25047 /* Parse an (optional) series of attributes.
25050 attributes attribute
25053 __attribute__ (( attribute-list [opt] ))
25055 The return value is as for cp_parser_gnu_attribute_list. */
25058 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25060 tree attributes
= NULL_TREE
;
25062 temp_override
<bool> cleanup
25063 (parser
->auto_is_implicit_function_template_parm_p
, false);
25068 tree attribute_list
;
25071 /* Peek at the next token. */
25072 token
= cp_lexer_peek_token (parser
->lexer
);
25073 /* If it's not `__attribute__', then we're done. */
25074 if (token
->keyword
!= RID_ATTRIBUTE
)
25077 /* Consume the `__attribute__' keyword. */
25078 cp_lexer_consume_token (parser
->lexer
);
25079 /* Look for the two `(' tokens. */
25080 matching_parens outer_parens
;
25081 outer_parens
.require_open (parser
);
25082 matching_parens inner_parens
;
25083 inner_parens
.require_open (parser
);
25085 /* Peek at the next token. */
25086 token
= cp_lexer_peek_token (parser
->lexer
);
25087 if (token
->type
!= CPP_CLOSE_PAREN
)
25088 /* Parse the attribute-list. */
25089 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25091 /* If the next token is a `)', then there is no attribute
25093 attribute_list
= NULL
;
25095 /* Look for the two `)' tokens. */
25096 if (!inner_parens
.require_close (parser
))
25098 if (!outer_parens
.require_close (parser
))
25101 cp_parser_skip_to_end_of_statement (parser
);
25103 /* Add these new attributes to the list. */
25104 attributes
= attr_chainon (attributes
, attribute_list
);
25110 /* Parse a GNU attribute-list.
25114 attribute-list , attribute
25118 identifier ( identifier )
25119 identifier ( identifier , expression-list )
25120 identifier ( expression-list )
25122 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25123 to an attribute. The TREE_PURPOSE of each node is the identifier
25124 indicating which attribute is in use. The TREE_VALUE represents
25125 the arguments, if any. */
25128 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25130 tree attribute_list
= NULL_TREE
;
25131 bool save_translate_strings_p
= parser
->translate_strings_p
;
25133 parser
->translate_strings_p
= false;
25140 /* Look for the identifier. We also allow keywords here; for
25141 example `__attribute__ ((const))' is legal. */
25142 token
= cp_lexer_peek_token (parser
->lexer
);
25143 if (token
->type
== CPP_NAME
25144 || token
->type
== CPP_KEYWORD
)
25146 tree arguments
= NULL_TREE
;
25148 /* Consume the token, but save it since we need it for the
25149 SIMD enabled function parsing. */
25150 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25152 /* Save away the identifier that indicates which attribute
25154 identifier
= (token
->type
== CPP_KEYWORD
)
25155 /* For keywords, use the canonical spelling, not the
25156 parsed identifier. */
25157 ? ridpointers
[(int) token
->keyword
]
25158 : id_token
->u
.value
;
25160 identifier
= canonicalize_attr_name (identifier
);
25161 attribute
= build_tree_list (identifier
, NULL_TREE
);
25163 /* Peek at the next token. */
25164 token
= cp_lexer_peek_token (parser
->lexer
);
25165 /* If it's an `(', then parse the attribute arguments. */
25166 if (token
->type
== CPP_OPEN_PAREN
)
25168 vec
<tree
, va_gc
> *vec
;
25169 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25170 ? id_attr
: normal_attr
);
25171 vec
= cp_parser_parenthesized_expression_list
25172 (parser
, attr_flag
, /*cast_p=*/false,
25173 /*allow_expansion_p=*/false,
25174 /*non_constant_p=*/NULL
);
25176 arguments
= error_mark_node
;
25179 arguments
= build_tree_list_vec (vec
);
25180 release_tree_vector (vec
);
25182 /* Save the arguments away. */
25183 TREE_VALUE (attribute
) = arguments
;
25186 if (arguments
!= error_mark_node
)
25188 /* Add this attribute to the list. */
25189 TREE_CHAIN (attribute
) = attribute_list
;
25190 attribute_list
= attribute
;
25193 token
= cp_lexer_peek_token (parser
->lexer
);
25195 /* Now, look for more attributes. If the next token isn't a
25196 `,', we're done. */
25197 if (token
->type
!= CPP_COMMA
)
25200 /* Consume the comma and keep going. */
25201 cp_lexer_consume_token (parser
->lexer
);
25203 parser
->translate_strings_p
= save_translate_strings_p
;
25205 /* We built up the list in reverse order. */
25206 return nreverse (attribute_list
);
25209 /* Parse a standard C++11 attribute.
25211 The returned representation is a TREE_LIST which TREE_PURPOSE is
25212 the scoped name of the attribute, and the TREE_VALUE is its
25215 Note that the scoped name of the attribute is itself a TREE_LIST
25216 which TREE_PURPOSE is the namespace of the attribute, and
25217 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25218 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25219 and which TREE_PURPOSE is directly the attribute name.
25221 Clients of the attribute code should use get_attribute_namespace
25222 and get_attribute_name to get the actual namespace and name of
25223 attributes, regardless of their being GNU or C++11 attributes.
25226 attribute-token attribute-argument-clause [opt]
25230 attribute-scoped-token
25232 attribute-scoped-token:
25233 attribute-namespace :: identifier
25235 attribute-namespace:
25238 attribute-argument-clause:
25239 ( balanced-token-seq )
25241 balanced-token-seq:
25242 balanced-token [opt]
25243 balanced-token-seq balanced-token
25246 ( balanced-token-seq )
25247 [ balanced-token-seq ]
25248 { balanced-token-seq }. */
25251 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25253 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25256 temp_override
<bool> cleanup
25257 (parser
->auto_is_implicit_function_template_parm_p
, false);
25259 /* First, parse name of the attribute, a.k.a attribute-token. */
25261 token
= cp_lexer_peek_token (parser
->lexer
);
25262 if (token
->type
== CPP_NAME
)
25263 attr_id
= token
->u
.value
;
25264 else if (token
->type
== CPP_KEYWORD
)
25265 attr_id
= ridpointers
[(int) token
->keyword
];
25266 else if (token
->flags
& NAMED_OP
)
25267 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25269 if (attr_id
== NULL_TREE
)
25272 cp_lexer_consume_token (parser
->lexer
);
25274 token
= cp_lexer_peek_token (parser
->lexer
);
25275 if (token
->type
== CPP_SCOPE
)
25277 /* We are seeing a scoped attribute token. */
25279 cp_lexer_consume_token (parser
->lexer
);
25281 error_at (token
->location
, "attribute using prefix used together "
25282 "with scoped attribute token");
25285 token
= cp_lexer_consume_token (parser
->lexer
);
25286 if (token
->type
== CPP_NAME
)
25287 attr_id
= token
->u
.value
;
25288 else if (token
->type
== CPP_KEYWORD
)
25289 attr_id
= ridpointers
[(int) token
->keyword
];
25290 else if (token
->flags
& NAMED_OP
)
25291 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25294 error_at (token
->location
,
25295 "expected an identifier for the attribute name");
25296 return error_mark_node
;
25299 attr_id
= canonicalize_attr_name (attr_id
);
25300 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25302 token
= cp_lexer_peek_token (parser
->lexer
);
25305 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25309 attr_id
= canonicalize_attr_name (attr_id
);
25310 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25312 /* C++11 noreturn attribute is equivalent to GNU's. */
25313 if (is_attribute_p ("noreturn", attr_id
))
25314 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25315 /* C++14 deprecated attribute is equivalent to GNU's. */
25316 else if (is_attribute_p ("deprecated", attr_id
))
25317 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25318 /* C++17 fallthrough attribute is equivalent to GNU's. */
25319 else if (is_attribute_p ("fallthrough", attr_id
))
25320 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25321 /* Transactional Memory TS optimize_for_synchronized attribute is
25322 equivalent to GNU transaction_callable. */
25323 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25324 TREE_PURPOSE (attribute
)
25325 = get_identifier ("transaction_callable");
25326 /* Transactional Memory attributes are GNU attributes. */
25327 else if (tm_attr_to_mask (attr_id
))
25328 TREE_PURPOSE (attribute
) = attr_id
;
25331 /* Now parse the optional argument clause of the attribute. */
25333 if (token
->type
!= CPP_OPEN_PAREN
)
25337 vec
<tree
, va_gc
> *vec
;
25338 int attr_flag
= normal_attr
;
25340 if (attr_ns
== get_identifier ("gnu")
25341 && attribute_takes_identifier_p (attr_id
))
25342 /* A GNU attribute that takes an identifier in parameter. */
25343 attr_flag
= id_attr
;
25345 vec
= cp_parser_parenthesized_expression_list
25346 (parser
, attr_flag
, /*cast_p=*/false,
25347 /*allow_expansion_p=*/true,
25348 /*non_constant_p=*/NULL
);
25350 arguments
= error_mark_node
;
25353 arguments
= build_tree_list_vec (vec
);
25354 release_tree_vector (vec
);
25357 if (arguments
== error_mark_node
)
25358 attribute
= error_mark_node
;
25360 TREE_VALUE (attribute
) = arguments
;
25366 /* Check that the attribute ATTRIBUTE appears at most once in the
25367 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25368 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25369 isn't implemented yet in GCC. */
25372 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25376 tree name
= get_attribute_name (attribute
);
25377 if (is_attribute_p ("noreturn", name
)
25378 && lookup_attribute ("noreturn", attributes
))
25379 error ("attribute %<noreturn%> can appear at most once "
25380 "in an attribute-list");
25381 else if (is_attribute_p ("deprecated", name
)
25382 && lookup_attribute ("deprecated", attributes
))
25383 error ("attribute %<deprecated%> can appear at most once "
25384 "in an attribute-list");
25388 /* Parse a list of standard C++-11 attributes.
25392 attribute-list , attribute[opt]
25394 attribute-list , attribute ...
25398 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25400 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25401 cp_token
*token
= NULL
;
25405 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25406 if (attribute
== error_mark_node
)
25408 if (attribute
!= NULL_TREE
)
25410 cp_parser_check_std_attribute (attributes
, attribute
);
25411 TREE_CHAIN (attribute
) = attributes
;
25412 attributes
= attribute
;
25414 token
= cp_lexer_peek_token (parser
->lexer
);
25415 if (token
->type
== CPP_ELLIPSIS
)
25417 cp_lexer_consume_token (parser
->lexer
);
25418 if (attribute
== NULL_TREE
)
25419 error_at (token
->location
,
25420 "expected attribute before %<...%>");
25423 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25424 if (pack
== error_mark_node
)
25425 return error_mark_node
;
25426 TREE_VALUE (attribute
) = pack
;
25428 token
= cp_lexer_peek_token (parser
->lexer
);
25430 if (token
->type
!= CPP_COMMA
)
25432 cp_lexer_consume_token (parser
->lexer
);
25434 attributes
= nreverse (attributes
);
25438 /* Parse a standard C++-11 attribute specifier.
25440 attribute-specifier:
25441 [ [ attribute-using-prefix [opt] attribute-list ] ]
25442 alignment-specifier
25444 attribute-using-prefix:
25445 using attribute-namespace :
25447 alignment-specifier:
25448 alignas ( type-id ... [opt] )
25449 alignas ( alignment-expression ... [opt] ). */
25452 cp_parser_std_attribute_spec (cp_parser
*parser
)
25454 tree attributes
= NULL_TREE
;
25455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25457 if (token
->type
== CPP_OPEN_SQUARE
25458 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25460 tree attr_ns
= NULL_TREE
;
25462 cp_lexer_consume_token (parser
->lexer
);
25463 cp_lexer_consume_token (parser
->lexer
);
25465 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25467 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25468 if (token
->type
== CPP_NAME
)
25469 attr_ns
= token
->u
.value
;
25470 else if (token
->type
== CPP_KEYWORD
)
25471 attr_ns
= ridpointers
[(int) token
->keyword
];
25472 else if (token
->flags
& NAMED_OP
)
25473 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25476 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25478 if (cxx_dialect
< cxx17
25479 && !in_system_header_at (input_location
))
25480 pedwarn (input_location
, 0,
25481 "attribute using prefix only available "
25482 "with -std=c++17 or -std=gnu++17");
25484 cp_lexer_consume_token (parser
->lexer
);
25485 cp_lexer_consume_token (parser
->lexer
);
25486 cp_lexer_consume_token (parser
->lexer
);
25489 attr_ns
= NULL_TREE
;
25492 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25494 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25495 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25496 cp_parser_skip_to_end_of_statement (parser
);
25498 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25499 when we are sure that we have actually parsed them. */
25500 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25506 /* Look for an alignment-specifier. */
25508 token
= cp_lexer_peek_token (parser
->lexer
);
25510 if (token
->type
!= CPP_KEYWORD
25511 || token
->keyword
!= RID_ALIGNAS
)
25514 cp_lexer_consume_token (parser
->lexer
);
25515 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25517 matching_parens parens
;
25518 if (!parens
.require_open (parser
))
25519 return error_mark_node
;
25521 cp_parser_parse_tentatively (parser
);
25522 alignas_expr
= cp_parser_type_id (parser
);
25524 if (!cp_parser_parse_definitely (parser
))
25526 alignas_expr
= cp_parser_assignment_expression (parser
);
25527 if (alignas_expr
== error_mark_node
)
25528 cp_parser_skip_to_end_of_statement (parser
);
25529 if (alignas_expr
== NULL_TREE
25530 || alignas_expr
== error_mark_node
)
25531 return alignas_expr
;
25534 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25535 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25537 /* Handle alignas (pack...). */
25538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25540 cp_lexer_consume_token (parser
->lexer
);
25541 alignas_expr
= make_pack_expansion (alignas_expr
);
25544 /* Something went wrong, so don't build the attribute. */
25545 if (alignas_expr
== error_mark_node
)
25546 return error_mark_node
;
25548 if (!parens
.require_close (parser
))
25549 return error_mark_node
;
25551 /* Build the C++-11 representation of an 'aligned'
25554 build_tree_list (build_tree_list (get_identifier ("gnu"),
25555 get_identifier ("aligned")),
25562 /* Parse a standard C++-11 attribute-specifier-seq.
25564 attribute-specifier-seq:
25565 attribute-specifier-seq [opt] attribute-specifier
25569 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25571 tree attr_specs
= NULL_TREE
;
25572 tree attr_last
= NULL_TREE
;
25576 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25577 if (attr_spec
== NULL_TREE
)
25579 if (attr_spec
== error_mark_node
)
25580 return error_mark_node
;
25583 TREE_CHAIN (attr_last
) = attr_spec
;
25585 attr_specs
= attr_last
= attr_spec
;
25586 attr_last
= tree_last (attr_last
);
25592 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25593 return index of the first token after balanced-token, or N on failure. */
25596 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25599 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25601 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25604 case CPP_PRAGMA_EOL
:
25605 /* Ran out of tokens. */
25607 case CPP_OPEN_PAREN
:
25610 case CPP_OPEN_BRACE
:
25613 case CPP_OPEN_SQUARE
:
25616 case CPP_CLOSE_PAREN
:
25619 case CPP_CLOSE_BRACE
:
25622 case CPP_CLOSE_SQUARE
:
25628 while (nparens
|| nbraces
|| nsquares
);
25632 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25633 return index of the first token after the GNU attribute tokens, or N on
25637 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25641 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25642 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25643 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25646 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25649 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25656 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25657 next token), return index of the first token after the standard C++11
25658 attribute tokens, or N on failure. */
25661 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25665 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25666 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25668 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25671 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25675 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25676 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25678 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25689 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25690 as the next token), return index of the first token after the attribute
25691 tokens, or N on failure. */
25694 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25696 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25697 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25698 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25701 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25702 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25703 current value of the PEDANTIC flag, regardless of whether or not
25704 the `__extension__' keyword is present. The caller is responsible
25705 for restoring the value of the PEDANTIC flag. */
25708 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25710 /* Save the old value of the PEDANTIC flag. */
25711 *saved_pedantic
= pedantic
;
25713 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25715 /* Consume the `__extension__' token. */
25716 cp_lexer_consume_token (parser
->lexer
);
25717 /* We're not being pedantic while the `__extension__' keyword is
25727 /* Parse a label declaration.
25730 __label__ label-declarator-seq ;
25732 label-declarator-seq:
25733 identifier , label-declarator-seq
25737 cp_parser_label_declaration (cp_parser
* parser
)
25739 /* Look for the `__label__' keyword. */
25740 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25746 /* Look for an identifier. */
25747 identifier
= cp_parser_identifier (parser
);
25748 /* If we failed, stop. */
25749 if (identifier
== error_mark_node
)
25751 /* Declare it as a label. */
25752 finish_label_decl (identifier
);
25753 /* If the next token is a `;', stop. */
25754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25756 /* Look for the `,' separating the label declarations. */
25757 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25760 /* Look for the final `;'. */
25761 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25764 // -------------------------------------------------------------------------- //
25767 // Parse a requires clause.
25769 // requires-clause:
25770 // 'requires' logical-or-expression
25772 // The required logical-or-expression must be a constant expression. Note
25773 // that we don't check that the expression is constepxr here. We defer until
25774 // we analyze constraints and then, we only check atomic constraints.
25776 cp_parser_requires_clause (cp_parser
*parser
)
25778 // Parse the requires clause so that it is not automatically folded.
25779 ++processing_template_decl
;
25780 tree expr
= cp_parser_binary_expression (parser
, false, false,
25781 PREC_NOT_OPERATOR
, NULL
);
25782 if (check_for_bare_parameter_packs (expr
))
25783 expr
= error_mark_node
;
25784 --processing_template_decl
;
25788 // Optionally parse a requires clause:
25790 cp_parser_requires_clause_opt (cp_parser
*parser
)
25792 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25793 if (tok
->keyword
!= RID_REQUIRES
)
25795 if (!flag_concepts
&& tok
->type
== CPP_NAME
25796 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25798 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25799 "%<requires%> only available with -fconcepts");
25800 /* Parse and discard the requires-clause. */
25801 cp_lexer_consume_token (parser
->lexer
);
25802 cp_parser_requires_clause (parser
);
25806 cp_lexer_consume_token (parser
->lexer
);
25807 return cp_parser_requires_clause (parser
);
25811 /*---------------------------------------------------------------------------
25812 Requires expressions
25813 ---------------------------------------------------------------------------*/
25815 /* Parse a requires expression
25817 requirement-expression:
25818 'requires' requirement-parameter-list [opt] requirement-body */
25820 cp_parser_requires_expression (cp_parser
*parser
)
25822 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25823 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25825 /* A requires-expression shall appear only within a concept
25826 definition or a requires-clause.
25828 TODO: Implement this diagnostic correctly. */
25829 if (!processing_template_decl
)
25831 error_at (loc
, "a requires expression cannot appear outside a template");
25832 cp_parser_skip_to_end_of_statement (parser
);
25833 return error_mark_node
;
25838 /* Local parameters are delared as variables within the scope
25839 of the expression. They are not visible past the end of
25840 the expression. Expressions within the requires-expression
25841 are unevaluated. */
25842 struct scope_sentinel
25846 ++cp_unevaluated_operand
;
25847 begin_scope (sk_block
, NULL_TREE
);
25852 pop_bindings_and_leave_scope ();
25853 --cp_unevaluated_operand
;
25857 /* Parse the optional parameter list. */
25858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25860 parms
= cp_parser_requirement_parameter_list (parser
);
25861 if (parms
== error_mark_node
)
25862 return error_mark_node
;
25867 /* Parse the requirement body. */
25868 reqs
= cp_parser_requirement_body (parser
);
25869 if (reqs
== error_mark_node
)
25870 return error_mark_node
;
25873 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25875 grokparms (parms
, &parms
);
25876 return finish_requires_expr (parms
, reqs
);
25879 /* Parse a parameterized requirement.
25881 requirement-parameter-list:
25882 '(' parameter-declaration-clause ')' */
25884 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25886 matching_parens parens
;
25887 if (!parens
.require_open (parser
))
25888 return error_mark_node
;
25890 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25892 if (!parens
.require_close (parser
))
25893 return error_mark_node
;
25898 /* Parse the body of a requirement.
25901 '{' requirement-list '}' */
25903 cp_parser_requirement_body (cp_parser
*parser
)
25905 matching_braces braces
;
25906 if (!braces
.require_open (parser
))
25907 return error_mark_node
;
25909 tree reqs
= cp_parser_requirement_list (parser
);
25911 if (!braces
.require_close (parser
))
25912 return error_mark_node
;
25917 /* Parse a list of requirements.
25921 requirement-list ';' requirement[opt] */
25923 cp_parser_requirement_list (cp_parser
*parser
)
25925 tree result
= NULL_TREE
;
25928 tree req
= cp_parser_requirement (parser
);
25929 if (req
== error_mark_node
)
25930 return error_mark_node
;
25932 result
= tree_cons (NULL_TREE
, req
, result
);
25934 /* If we see a semi-colon, consume it. */
25935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25936 cp_lexer_consume_token (parser
->lexer
);
25938 /* Stop processing at the end of the list. */
25939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25943 /* Reverse the order of requirements so they are analyzed in
25944 declaration order. */
25945 return nreverse (result
);
25948 /* Parse a syntactic requirement or type requirement.
25952 compound-requirement
25954 nested-requirement */
25956 cp_parser_requirement (cp_parser
*parser
)
25958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25959 return cp_parser_compound_requirement (parser
);
25960 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25961 return cp_parser_type_requirement (parser
);
25962 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25963 return cp_parser_nested_requirement (parser
);
25965 return cp_parser_simple_requirement (parser
);
25968 /* Parse a simple requirement.
25970 simple-requirement:
25973 cp_parser_simple_requirement (cp_parser
*parser
)
25975 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25976 if (!expr
|| expr
== error_mark_node
)
25977 return error_mark_node
;
25979 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25980 return error_mark_node
;
25982 return finish_simple_requirement (expr
);
25985 /* Parse a type requirement
25988 nested-name-specifier [opt] required-type-name ';'
25990 required-type-name:
25992 'template' [opt] simple-template-id */
25994 cp_parser_type_requirement (cp_parser
*parser
)
25996 cp_lexer_consume_token (parser
->lexer
);
25998 // Save the scope before parsing name specifiers.
25999 tree saved_scope
= parser
->scope
;
26000 tree saved_object_scope
= parser
->object_scope
;
26001 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26002 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26003 cp_parser_nested_name_specifier_opt (parser
,
26004 /*typename_keyword_p=*/true,
26005 /*check_dependency_p=*/false,
26007 /*is_declaration=*/false);
26010 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26012 cp_lexer_consume_token (parser
->lexer
);
26013 type
= cp_parser_template_id (parser
,
26014 /*template_keyword_p=*/true,
26015 /*check_dependency=*/false,
26016 /*tag_type=*/none_type
,
26017 /*is_declaration=*/false);
26018 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26019 /*complain=*/tf_error
);
26022 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26024 if (TREE_CODE (type
) == TYPE_DECL
)
26025 type
= TREE_TYPE (type
);
26027 parser
->scope
= saved_scope
;
26028 parser
->object_scope
= saved_object_scope
;
26029 parser
->qualifying_scope
= saved_qualifying_scope
;
26031 if (type
== error_mark_node
)
26032 cp_parser_skip_to_end_of_statement (parser
);
26034 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26035 return error_mark_node
;
26036 if (type
== error_mark_node
)
26037 return error_mark_node
;
26039 return finish_type_requirement (type
);
26042 /* Parse a compound requirement
26044 compound-requirement:
26045 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26047 cp_parser_compound_requirement (cp_parser
*parser
)
26049 /* Parse an expression enclosed in '{ }'s. */
26050 matching_braces braces
;
26051 if (!braces
.require_open (parser
))
26052 return error_mark_node
;
26054 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26055 if (!expr
|| expr
== error_mark_node
)
26056 return error_mark_node
;
26058 if (!braces
.require_close (parser
))
26059 return error_mark_node
;
26061 /* Parse the optional noexcept. */
26062 bool noexcept_p
= false;
26063 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26065 cp_lexer_consume_token (parser
->lexer
);
26069 /* Parse the optional trailing return type. */
26070 tree type
= NULL_TREE
;
26071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26073 cp_lexer_consume_token (parser
->lexer
);
26074 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26075 parser
->in_result_type_constraint_p
= true;
26076 type
= cp_parser_trailing_type_id (parser
);
26077 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26078 if (type
== error_mark_node
)
26079 return error_mark_node
;
26082 return finish_compound_requirement (expr
, type
, noexcept_p
);
26085 /* Parse a nested requirement. This is the same as a requires clause.
26087 nested-requirement:
26090 cp_parser_nested_requirement (cp_parser
*parser
)
26092 cp_lexer_consume_token (parser
->lexer
);
26093 tree req
= cp_parser_requires_clause (parser
);
26094 if (req
== error_mark_node
)
26095 return error_mark_node
;
26096 return finish_nested_requirement (req
);
26099 /* Support Functions */
26101 /* Return the appropriate prefer_type argument for lookup_name_real based on
26102 tag_type and template_mem_access. */
26105 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26107 /* DR 141: When looking in the current enclosing context for a template-name
26108 after -> or ., only consider class templates. */
26109 if (template_mem_access
)
26113 case none_type
: return 0; // No preference.
26114 case scope_type
: return 1; // Type or namespace.
26115 default: return 2; // Type only.
26119 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26120 NAME should have one of the representations used for an
26121 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26122 is returned. If PARSER->SCOPE is a dependent type, then a
26123 SCOPE_REF is returned.
26125 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26126 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26127 was formed. Abstractly, such entities should not be passed to this
26128 function, because they do not need to be looked up, but it is
26129 simpler to check for this special case here, rather than at the
26132 In cases not explicitly covered above, this function returns a
26133 DECL, OVERLOAD, or baselink representing the result of the lookup.
26134 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26137 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26138 (e.g., "struct") that was used. In that case bindings that do not
26139 refer to types are ignored.
26141 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26144 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26147 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26150 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26151 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26152 NULL_TREE otherwise. */
26155 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26156 enum tag_types tag_type
,
26159 bool check_dependency
,
26160 tree
*ambiguous_decls
,
26161 location_t name_location
)
26164 tree object_type
= parser
->context
->object_type
;
26166 /* Assume that the lookup will be unambiguous. */
26167 if (ambiguous_decls
)
26168 *ambiguous_decls
= NULL_TREE
;
26170 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26171 no longer valid. Note that if we are parsing tentatively, and
26172 the parse fails, OBJECT_TYPE will be automatically restored. */
26173 parser
->context
->object_type
= NULL_TREE
;
26175 if (name
== error_mark_node
)
26176 return error_mark_node
;
26178 /* A template-id has already been resolved; there is no lookup to
26180 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26182 if (BASELINK_P (name
))
26184 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26185 == TEMPLATE_ID_EXPR
);
26189 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26190 it should already have been checked to make sure that the name
26191 used matches the type being destroyed. */
26192 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26196 /* Figure out to which type this destructor applies. */
26198 type
= parser
->scope
;
26199 else if (object_type
)
26200 type
= object_type
;
26202 type
= current_class_type
;
26203 /* If that's not a class type, there is no destructor. */
26204 if (!type
|| !CLASS_TYPE_P (type
))
26205 return error_mark_node
;
26207 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26208 lazily_declare_fn (sfk_destructor
, type
);
26210 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26213 return error_mark_node
;
26216 /* By this point, the NAME should be an ordinary identifier. If
26217 the id-expression was a qualified name, the qualifying scope is
26218 stored in PARSER->SCOPE at this point. */
26219 gcc_assert (identifier_p (name
));
26221 /* Perform the lookup. */
26226 if (parser
->scope
== error_mark_node
)
26227 return error_mark_node
;
26229 /* If the SCOPE is dependent, the lookup must be deferred until
26230 the template is instantiated -- unless we are explicitly
26231 looking up names in uninstantiated templates. Even then, we
26232 cannot look up the name if the scope is not a class type; it
26233 might, for example, be a template type parameter. */
26234 dependent_p
= (TYPE_P (parser
->scope
)
26235 && dependent_scope_p (parser
->scope
));
26236 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26238 /* Defer lookup. */
26239 decl
= error_mark_node
;
26242 tree pushed_scope
= NULL_TREE
;
26244 /* If PARSER->SCOPE is a dependent type, then it must be a
26245 class type, and we must not be checking dependencies;
26246 otherwise, we would have processed this lookup above. So
26247 that PARSER->SCOPE is not considered a dependent base by
26248 lookup_member, we must enter the scope here. */
26250 pushed_scope
= push_scope (parser
->scope
);
26252 /* If the PARSER->SCOPE is a template specialization, it
26253 may be instantiated during name lookup. In that case,
26254 errors may be issued. Even if we rollback the current
26255 tentative parse, those errors are valid. */
26256 decl
= lookup_qualified_name (parser
->scope
, name
,
26257 prefer_type_arg (tag_type
),
26258 /*complain=*/true);
26260 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26261 lookup result and the nested-name-specifier nominates a class C:
26262 * if the name specified after the nested-name-specifier, when
26263 looked up in C, is the injected-class-name of C (Clause 9), or
26264 * if the name specified after the nested-name-specifier is the
26265 same as the identifier or the simple-template-id's template-
26266 name in the last component of the nested-name-specifier,
26267 the name is instead considered to name the constructor of
26268 class C. [ Note: for example, the constructor is not an
26269 acceptable lookup result in an elaborated-type-specifier so
26270 the constructor would not be used in place of the
26271 injected-class-name. --end note ] Such a constructor name
26272 shall be used only in the declarator-id of a declaration that
26273 names a constructor or in a using-declaration. */
26274 if (tag_type
== none_type
26275 && DECL_SELF_REFERENCE_P (decl
)
26276 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26277 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26278 prefer_type_arg (tag_type
),
26279 /*complain=*/true);
26281 /* If we have a single function from a using decl, pull it out. */
26282 if (TREE_CODE (decl
) == OVERLOAD
26283 && !really_overloaded_fn (decl
))
26284 decl
= OVL_FUNCTION (decl
);
26287 pop_scope (pushed_scope
);
26290 /* If the scope is a dependent type and either we deferred lookup or
26291 we did lookup but didn't find the name, rememeber the name. */
26292 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26293 && dependent_type_p (parser
->scope
))
26299 /* The resolution to Core Issue 180 says that `struct
26300 A::B' should be considered a type-name, even if `A'
26302 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26303 /*complain=*/tf_error
);
26304 if (type
!= error_mark_node
)
26305 decl
= TYPE_NAME (type
);
26307 else if (is_template
26308 && (cp_parser_next_token_ends_template_argument_p (parser
)
26309 || cp_lexer_next_token_is (parser
->lexer
,
26311 decl
= make_unbound_class_template (parser
->scope
,
26313 /*complain=*/tf_error
);
26315 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26316 parser
->scope
, name
,
26319 parser
->qualifying_scope
= parser
->scope
;
26320 parser
->object_scope
= NULL_TREE
;
26322 else if (object_type
)
26324 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26325 OBJECT_TYPE is not a class. */
26326 if (CLASS_TYPE_P (object_type
))
26327 /* If the OBJECT_TYPE is a template specialization, it may
26328 be instantiated during name lookup. In that case, errors
26329 may be issued. Even if we rollback the current tentative
26330 parse, those errors are valid. */
26331 decl
= lookup_member (object_type
,
26334 prefer_type_arg (tag_type
),
26335 tf_warning_or_error
);
26340 /* Look it up in the enclosing context. DR 141: When looking for a
26341 template-name after -> or ., only consider class templates. */
26342 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26344 /*block_p=*/true, is_namespace
, 0);
26345 if (object_type
== unknown_type_node
)
26346 /* The object is type-dependent, so we can't look anything up; we used
26347 this to get the DR 141 behavior. */
26348 object_type
= NULL_TREE
;
26349 parser
->object_scope
= object_type
;
26350 parser
->qualifying_scope
= NULL_TREE
;
26354 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26356 /*block_p=*/true, is_namespace
, 0);
26357 parser
->qualifying_scope
= NULL_TREE
;
26358 parser
->object_scope
= NULL_TREE
;
26361 /* If the lookup failed, let our caller know. */
26362 if (!decl
|| decl
== error_mark_node
)
26363 return error_mark_node
;
26365 /* Pull out the template from an injected-class-name (or multiple). */
26367 decl
= maybe_get_template_decl_from_type_decl (decl
);
26369 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26370 if (TREE_CODE (decl
) == TREE_LIST
)
26372 if (ambiguous_decls
)
26373 *ambiguous_decls
= decl
;
26374 /* The error message we have to print is too complicated for
26375 cp_parser_error, so we incorporate its actions directly. */
26376 if (!cp_parser_simulate_error (parser
))
26378 error_at (name_location
, "reference to %qD is ambiguous",
26380 print_candidates (decl
);
26382 return error_mark_node
;
26385 gcc_assert (DECL_P (decl
)
26386 || TREE_CODE (decl
) == OVERLOAD
26387 || TREE_CODE (decl
) == SCOPE_REF
26388 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26389 || BASELINK_P (decl
));
26391 /* If we have resolved the name of a member declaration, check to
26392 see if the declaration is accessible. When the name resolves to
26393 set of overloaded functions, accessibility is checked when
26394 overload resolution is done.
26396 During an explicit instantiation, access is not checked at all,
26397 as per [temp.explicit]. */
26399 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26401 maybe_record_typedef_use (decl
);
26403 return cp_expr (decl
, name_location
);
26406 /* Like cp_parser_lookup_name, but for use in the typical case where
26407 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26408 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26411 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26413 return cp_parser_lookup_name (parser
, name
,
26415 /*is_template=*/false,
26416 /*is_namespace=*/false,
26417 /*check_dependency=*/true,
26418 /*ambiguous_decls=*/NULL
,
26422 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26423 the current context, return the TYPE_DECL. If TAG_NAME_P is
26424 true, the DECL indicates the class being defined in a class-head,
26425 or declared in an elaborated-type-specifier.
26427 Otherwise, return DECL. */
26430 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26432 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26433 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26436 template <typename T> struct B;
26439 template <typename T> struct A::B {};
26441 Similarly, in an elaborated-type-specifier:
26443 namespace N { struct X{}; }
26446 template <typename T> friend struct N::X;
26449 However, if the DECL refers to a class type, and we are in
26450 the scope of the class, then the name lookup automatically
26451 finds the TYPE_DECL created by build_self_reference rather
26452 than a TEMPLATE_DECL. For example, in:
26454 template <class T> struct S {
26458 there is no need to handle such case. */
26460 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26461 return DECL_TEMPLATE_RESULT (decl
);
26466 /* If too many, or too few, template-parameter lists apply to the
26467 declarator, issue an error message. Returns TRUE if all went well,
26468 and FALSE otherwise. */
26471 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26472 cp_declarator
*declarator
,
26473 location_t declarator_location
)
26475 switch (declarator
->kind
)
26479 unsigned num_templates
= 0;
26480 tree scope
= declarator
->u
.id
.qualifying_scope
;
26481 bool template_id_p
= false;
26484 num_templates
= num_template_headers_for_class (scope
);
26485 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26486 == TEMPLATE_ID_EXPR
)
26488 /* If the DECLARATOR has the form `X<y>' then it uses one
26489 additional level of template parameters. */
26491 template_id_p
= true;
26494 return cp_parser_check_template_parameters
26495 (parser
, num_templates
, template_id_p
, declarator_location
,
26502 case cdk_reference
:
26504 return (cp_parser_check_declarator_template_parameters
26505 (parser
, declarator
->declarator
, declarator_location
));
26512 gcc_unreachable ();
26517 /* NUM_TEMPLATES were used in the current declaration. If that is
26518 invalid, return FALSE and issue an error messages. Otherwise,
26519 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26520 declarator and we can print more accurate diagnostics. */
26523 cp_parser_check_template_parameters (cp_parser
* parser
,
26524 unsigned num_templates
,
26525 bool template_id_p
,
26526 location_t location
,
26527 cp_declarator
*declarator
)
26529 /* If there are the same number of template classes and parameter
26530 lists, that's OK. */
26531 if (parser
->num_template_parameter_lists
== num_templates
)
26533 /* If there are more, but only one more, and the name ends in an identifier,
26534 then we are declaring a primary template. That's OK too. */
26536 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26538 /* If there are more template classes than parameter lists, we have
26541 template <class T> void S<T>::R<T>::f (); */
26542 if (parser
->num_template_parameter_lists
< num_templates
)
26544 if (declarator
&& !current_function_decl
)
26545 error_at (location
, "specializing member %<%T::%E%> "
26546 "requires %<template<>%> syntax",
26547 declarator
->u
.id
.qualifying_scope
,
26548 declarator
->u
.id
.unqualified_name
);
26549 else if (declarator
)
26550 error_at (location
, "invalid declaration of %<%T::%E%>",
26551 declarator
->u
.id
.qualifying_scope
,
26552 declarator
->u
.id
.unqualified_name
);
26554 error_at (location
, "too few template-parameter-lists");
26557 /* Otherwise, there are too many template parameter lists. We have
26560 template <class T> template <class U> void S::f(); */
26561 error_at (location
, "too many template-parameter-lists");
26565 /* Parse an optional `::' token indicating that the following name is
26566 from the global namespace. If so, PARSER->SCOPE is set to the
26567 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26568 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26569 Returns the new value of PARSER->SCOPE, if the `::' token is
26570 present, and NULL_TREE otherwise. */
26573 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26577 /* Peek at the next token. */
26578 token
= cp_lexer_peek_token (parser
->lexer
);
26579 /* If we're looking at a `::' token then we're starting from the
26580 global namespace, not our current location. */
26581 if (token
->type
== CPP_SCOPE
)
26583 /* Consume the `::' token. */
26584 cp_lexer_consume_token (parser
->lexer
);
26585 /* Set the SCOPE so that we know where to start the lookup. */
26586 parser
->scope
= global_namespace
;
26587 parser
->qualifying_scope
= global_namespace
;
26588 parser
->object_scope
= NULL_TREE
;
26590 return parser
->scope
;
26592 else if (!current_scope_valid_p
)
26594 parser
->scope
= NULL_TREE
;
26595 parser
->qualifying_scope
= NULL_TREE
;
26596 parser
->object_scope
= NULL_TREE
;
26602 /* Returns TRUE if the upcoming token sequence is the start of a
26603 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26604 declarator is preceded by the `friend' specifier. */
26607 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26609 bool constructor_p
;
26610 bool outside_class_specifier_p
;
26611 tree nested_name_specifier
;
26612 cp_token
*next_token
;
26614 /* The common case is that this is not a constructor declarator, so
26615 try to avoid doing lots of work if at all possible. It's not
26616 valid declare a constructor at function scope. */
26617 if (parser
->in_function_body
)
26619 /* And only certain tokens can begin a constructor declarator. */
26620 next_token
= cp_lexer_peek_token (parser
->lexer
);
26621 if (next_token
->type
!= CPP_NAME
26622 && next_token
->type
!= CPP_SCOPE
26623 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26624 && next_token
->type
!= CPP_TEMPLATE_ID
)
26627 /* Parse tentatively; we are going to roll back all of the tokens
26629 cp_parser_parse_tentatively (parser
);
26630 /* Assume that we are looking at a constructor declarator. */
26631 constructor_p
= true;
26633 /* Look for the optional `::' operator. */
26634 cp_parser_global_scope_opt (parser
,
26635 /*current_scope_valid_p=*/false);
26636 /* Look for the nested-name-specifier. */
26637 nested_name_specifier
26638 = (cp_parser_nested_name_specifier_opt (parser
,
26639 /*typename_keyword_p=*/false,
26640 /*check_dependency_p=*/false,
26642 /*is_declaration=*/false));
26644 outside_class_specifier_p
= (!at_class_scope_p ()
26645 || !TYPE_BEING_DEFINED (current_class_type
)
26648 /* Outside of a class-specifier, there must be a
26649 nested-name-specifier. Except in C++17 mode, where we
26650 might be declaring a guiding declaration. */
26651 if (!nested_name_specifier
&& outside_class_specifier_p
26652 && cxx_dialect
< cxx17
)
26653 constructor_p
= false;
26654 else if (nested_name_specifier
== error_mark_node
)
26655 constructor_p
= false;
26657 /* If we have a class scope, this is easy; DR 147 says that S::S always
26658 names the constructor, and no other qualified name could. */
26659 if (constructor_p
&& nested_name_specifier
26660 && CLASS_TYPE_P (nested_name_specifier
))
26662 tree id
= cp_parser_unqualified_id (parser
,
26663 /*template_keyword_p=*/false,
26664 /*check_dependency_p=*/false,
26665 /*declarator_p=*/true,
26666 /*optional_p=*/false);
26667 if (is_overloaded_fn (id
))
26668 id
= DECL_NAME (get_first_fn (id
));
26669 if (!constructor_name_p (id
, nested_name_specifier
))
26670 constructor_p
= false;
26672 /* If we still think that this might be a constructor-declarator,
26673 look for a class-name. */
26674 else if (constructor_p
)
26678 template <typename T> struct S {
26682 we must recognize that the nested `S' names a class. */
26683 if (cxx_dialect
>= cxx17
)
26684 cp_parser_parse_tentatively (parser
);
26687 type_decl
= cp_parser_class_name (parser
,
26688 /*typename_keyword_p=*/false,
26689 /*template_keyword_p=*/false,
26691 /*check_dependency_p=*/false,
26692 /*class_head_p=*/false,
26693 /*is_declaration=*/false);
26695 if (cxx_dialect
>= cxx17
26696 && !cp_parser_parse_definitely (parser
))
26698 type_decl
= NULL_TREE
;
26699 tree tmpl
= cp_parser_template_name (parser
,
26700 /*template_keyword*/false,
26701 /*check_dependency_p*/false,
26702 /*is_declaration*/false,
26704 /*is_identifier*/NULL
);
26705 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26706 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26707 /* It's a deduction guide, return true. */;
26709 cp_parser_simulate_error (parser
);
26712 /* If there was no class-name, then this is not a constructor.
26713 Otherwise, if we are in a class-specifier and we aren't
26714 handling a friend declaration, check that its type matches
26715 current_class_type (c++/38313). Note: error_mark_node
26716 is left alone for error recovery purposes. */
26717 constructor_p
= (!cp_parser_error_occurred (parser
)
26718 && (outside_class_specifier_p
26719 || type_decl
== NULL_TREE
26720 || type_decl
== error_mark_node
26721 || same_type_p (current_class_type
,
26722 TREE_TYPE (type_decl
))));
26724 /* If we're still considering a constructor, we have to see a `(',
26725 to begin the parameter-declaration-clause, followed by either a
26726 `)', an `...', or a decl-specifier. We need to check for a
26727 type-specifier to avoid being fooled into thinking that:
26731 is a constructor. (It is actually a function named `f' that
26732 takes one parameter (of type `int') and returns a value of type
26735 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26736 constructor_p
= false;
26739 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26740 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26741 /* A parameter declaration begins with a decl-specifier,
26742 which is either the "attribute" keyword, a storage class
26743 specifier, or (usually) a type-specifier. */
26744 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26747 tree pushed_scope
= NULL_TREE
;
26748 unsigned saved_num_template_parameter_lists
;
26750 /* Names appearing in the type-specifier should be looked up
26751 in the scope of the class. */
26752 if (current_class_type
)
26754 else if (type_decl
)
26756 type
= TREE_TYPE (type_decl
);
26757 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26759 type
= resolve_typename_type (type
,
26760 /*only_current_p=*/false);
26761 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26763 cp_parser_abort_tentative_parse (parser
);
26767 pushed_scope
= push_scope (type
);
26770 /* Inside the constructor parameter list, surrounding
26771 template-parameter-lists do not apply. */
26772 saved_num_template_parameter_lists
26773 = parser
->num_template_parameter_lists
;
26774 parser
->num_template_parameter_lists
= 0;
26776 /* Look for the type-specifier. */
26777 cp_parser_type_specifier (parser
,
26778 CP_PARSER_FLAGS_NONE
,
26779 /*decl_specs=*/NULL
,
26780 /*is_declarator=*/true,
26781 /*declares_class_or_enum=*/NULL
,
26782 /*is_cv_qualifier=*/NULL
);
26784 parser
->num_template_parameter_lists
26785 = saved_num_template_parameter_lists
;
26787 /* Leave the scope of the class. */
26789 pop_scope (pushed_scope
);
26791 constructor_p
= !cp_parser_error_occurred (parser
);
26795 /* We did not really want to consume any tokens. */
26796 cp_parser_abort_tentative_parse (parser
);
26798 return constructor_p
;
26801 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26802 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26803 they must be performed once we are in the scope of the function.
26805 Returns the function defined. */
26808 cp_parser_function_definition_from_specifiers_and_declarator
26809 (cp_parser
* parser
,
26810 cp_decl_specifier_seq
*decl_specifiers
,
26812 const cp_declarator
*declarator
)
26817 /* Begin the function-definition. */
26818 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26820 /* The things we're about to see are not directly qualified by any
26821 template headers we've seen thus far. */
26822 reset_specialization ();
26824 /* If there were names looked up in the decl-specifier-seq that we
26825 did not check, check them now. We must wait until we are in the
26826 scope of the function to perform the checks, since the function
26827 might be a friend. */
26828 perform_deferred_access_checks (tf_warning_or_error
);
26832 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26833 parser
->omp_declare_simd
= NULL
;
26834 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26835 parser
->oacc_routine
= NULL
;
26840 /* Skip the entire function. */
26841 cp_parser_skip_to_end_of_block_or_statement (parser
);
26842 fn
= error_mark_node
;
26844 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26846 /* Seen already, skip it. An error message has already been output. */
26847 cp_parser_skip_to_end_of_block_or_statement (parser
);
26848 fn
= current_function_decl
;
26849 current_function_decl
= NULL_TREE
;
26850 /* If this is a function from a class, pop the nested class. */
26851 if (current_class_name
)
26852 pop_nested_class ();
26857 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26858 tv
= TV_PARSE_INLINE
;
26860 tv
= TV_PARSE_FUNC
;
26862 fn
= cp_parser_function_definition_after_declarator (parser
,
26863 /*inline_p=*/false);
26870 /* Parse the part of a function-definition that follows the
26871 declarator. INLINE_P is TRUE iff this function is an inline
26872 function defined within a class-specifier.
26874 Returns the function defined. */
26877 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26881 bool saved_in_unbraced_linkage_specification_p
;
26882 bool saved_in_function_body
;
26883 unsigned saved_num_template_parameter_lists
;
26885 bool fully_implicit_function_template_p
26886 = parser
->fully_implicit_function_template_p
;
26887 parser
->fully_implicit_function_template_p
= false;
26888 tree implicit_template_parms
26889 = parser
->implicit_template_parms
;
26890 parser
->implicit_template_parms
= 0;
26891 cp_binding_level
* implicit_template_scope
26892 = parser
->implicit_template_scope
;
26893 parser
->implicit_template_scope
= 0;
26895 saved_in_function_body
= parser
->in_function_body
;
26896 parser
->in_function_body
= true;
26897 /* If the next token is `return', then the code may be trying to
26898 make use of the "named return value" extension that G++ used to
26900 token
= cp_lexer_peek_token (parser
->lexer
);
26901 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26903 /* Consume the `return' keyword. */
26904 cp_lexer_consume_token (parser
->lexer
);
26905 /* Look for the identifier that indicates what value is to be
26907 cp_parser_identifier (parser
);
26908 /* Issue an error message. */
26909 error_at (token
->location
,
26910 "named return values are no longer supported");
26911 /* Skip tokens until we reach the start of the function body. */
26914 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26915 if (token
->type
== CPP_OPEN_BRACE
26916 || token
->type
== CPP_EOF
26917 || token
->type
== CPP_PRAGMA_EOL
)
26919 cp_lexer_consume_token (parser
->lexer
);
26922 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26923 anything declared inside `f'. */
26924 saved_in_unbraced_linkage_specification_p
26925 = parser
->in_unbraced_linkage_specification_p
;
26926 parser
->in_unbraced_linkage_specification_p
= false;
26927 /* Inside the function, surrounding template-parameter-lists do not
26929 saved_num_template_parameter_lists
26930 = parser
->num_template_parameter_lists
;
26931 parser
->num_template_parameter_lists
= 0;
26933 /* If the next token is `try', `__transaction_atomic', or
26934 `__transaction_relaxed`, then we are looking at either function-try-block
26935 or function-transaction-block. Note that all of these include the
26937 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26938 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26939 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26940 RID_TRANSACTION_RELAXED
))
26941 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26942 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26943 cp_parser_function_try_block (parser
);
26945 cp_parser_ctor_initializer_opt_and_function_body
26946 (parser
, /*in_function_try_block=*/false);
26948 /* Finish the function. */
26949 fn
= finish_function (inline_p
);
26950 /* Generate code for it, if necessary. */
26951 expand_or_defer_fn (fn
);
26952 /* Restore the saved values. */
26953 parser
->in_unbraced_linkage_specification_p
26954 = saved_in_unbraced_linkage_specification_p
;
26955 parser
->num_template_parameter_lists
26956 = saved_num_template_parameter_lists
;
26957 parser
->in_function_body
= saved_in_function_body
;
26959 parser
->fully_implicit_function_template_p
26960 = fully_implicit_function_template_p
;
26961 parser
->implicit_template_parms
26962 = implicit_template_parms
;
26963 parser
->implicit_template_scope
26964 = implicit_template_scope
;
26966 if (parser
->fully_implicit_function_template_p
)
26967 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26972 /* Parse a template-declaration body (following argument list). */
26975 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26976 tree parameter_list
,
26979 tree decl
= NULL_TREE
;
26980 bool friend_p
= false;
26982 /* We just processed one more parameter list. */
26983 ++parser
->num_template_parameter_lists
;
26985 /* Get the deferred access checks from the parameter list. These
26986 will be checked once we know what is being declared, as for a
26987 member template the checks must be performed in the scope of the
26988 class containing the member. */
26989 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26991 /* Tentatively parse for a new template parameter list, which can either be
26992 the template keyword or a template introduction. */
26993 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26995 else if (cxx_dialect
>= cxx11
26996 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26997 decl
= cp_parser_alias_declaration (parser
);
27000 /* There are no access checks when parsing a template, as we do not
27001 know if a specialization will be a friend. */
27002 push_deferring_access_checks (dk_no_check
);
27003 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27004 decl
= cp_parser_single_declaration (parser
,
27007 /*explicit_specialization_p=*/false,
27009 pop_deferring_access_checks ();
27011 /* If this is a member template declaration, let the front
27013 if (member_p
&& !friend_p
&& decl
)
27015 if (TREE_CODE (decl
) == TYPE_DECL
)
27016 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27018 decl
= finish_member_template_decl (decl
);
27020 else if (friend_p
&& decl
27021 && DECL_DECLARES_TYPE_P (decl
))
27022 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27023 /*complain=*/true);
27025 /* We are done with the current parameter list. */
27026 --parser
->num_template_parameter_lists
;
27028 pop_deferring_access_checks ();
27031 finish_template_decl (parameter_list
);
27033 /* Check the template arguments for a literal operator template. */
27035 && DECL_DECLARES_FUNCTION_P (decl
)
27036 && UDLIT_OPER_P (DECL_NAME (decl
)))
27039 if (parameter_list
== NULL_TREE
)
27043 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27044 if (num_parms
== 1)
27046 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27047 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27048 if (TREE_TYPE (parm
) != char_type_node
27049 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27052 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27054 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27055 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27056 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27057 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27058 if (parm
== error_mark_node
27059 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27060 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27068 if (cxx_dialect
>= cxx14
)
27069 error ("literal operator template %qD has invalid parameter list."
27070 " Expected non-type template argument pack <char...>"
27071 " or <typename CharT, CharT...>",
27074 error ("literal operator template %qD has invalid parameter list."
27075 " Expected non-type template argument pack <char...>",
27080 /* Register member declarations. */
27081 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27082 finish_member_declaration (decl
);
27083 /* If DECL is a function template, we must return to parse it later.
27084 (Even though there is no definition, there might be default
27085 arguments that need handling.) */
27086 if (member_p
&& decl
27087 && DECL_DECLARES_FUNCTION_P (decl
))
27088 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27091 /* Parse a template introduction header for a template-declaration. Returns
27092 false if tentative parse fails. */
27095 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27097 cp_parser_parse_tentatively (parser
);
27099 tree saved_scope
= parser
->scope
;
27100 tree saved_object_scope
= parser
->object_scope
;
27101 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27103 /* Look for the optional `::' operator. */
27104 cp_parser_global_scope_opt (parser
,
27105 /*current_scope_valid_p=*/false);
27106 /* Look for the nested-name-specifier. */
27107 cp_parser_nested_name_specifier_opt (parser
,
27108 /*typename_keyword_p=*/false,
27109 /*check_dependency_p=*/true,
27111 /*is_declaration=*/false);
27113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27114 tree concept_name
= cp_parser_identifier (parser
);
27116 /* Look up the concept for which we will be matching
27117 template parameters. */
27118 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27120 parser
->scope
= saved_scope
;
27121 parser
->object_scope
= saved_object_scope
;
27122 parser
->qualifying_scope
= saved_qualifying_scope
;
27124 if (concept_name
== error_mark_node
)
27125 cp_parser_simulate_error (parser
);
27127 /* Look for opening brace for introduction. */
27128 matching_braces braces
;
27129 braces
.require_open (parser
);
27131 if (!cp_parser_parse_definitely (parser
))
27134 push_deferring_access_checks (dk_deferred
);
27136 /* Build vector of placeholder parameters and grab
27137 matching identifiers. */
27138 tree introduction_list
= cp_parser_introduction_list (parser
);
27140 /* The introduction-list shall not be empty. */
27141 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27144 error ("empty introduction-list");
27148 /* Look for closing brace for introduction. */
27149 if (!braces
.require_close (parser
))
27152 if (tmpl_decl
== error_mark_node
)
27154 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27159 /* Build and associate the constraint. */
27160 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27161 if (parms
&& parms
!= error_mark_node
)
27163 cp_parser_template_declaration_after_parameters (parser
, parms
,
27168 error_at (token
->location
, "no matching concept for template-introduction");
27172 /* Parse a normal template-declaration following the template keyword. */
27175 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27177 tree parameter_list
;
27178 bool need_lang_pop
;
27179 location_t location
= input_location
;
27181 /* Look for the `<' token. */
27182 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27184 if (at_class_scope_p () && current_function_decl
)
27186 /* 14.5.2.2 [temp.mem]
27188 A local class shall not have member templates. */
27189 error_at (location
,
27190 "invalid declaration of member template in local class");
27191 cp_parser_skip_to_end_of_block_or_statement (parser
);
27196 A template ... shall not have C linkage. */
27197 if (current_lang_name
== lang_name_c
)
27199 error_at (location
, "template with C linkage");
27200 maybe_show_extern_c_location ();
27201 /* Give it C++ linkage to avoid confusing other parts of the
27203 push_lang_context (lang_name_cplusplus
);
27204 need_lang_pop
= true;
27207 need_lang_pop
= false;
27209 /* We cannot perform access checks on the template parameter
27210 declarations until we know what is being declared, just as we
27211 cannot check the decl-specifier list. */
27212 push_deferring_access_checks (dk_deferred
);
27214 /* If the next token is `>', then we have an invalid
27215 specialization. Rather than complain about an invalid template
27216 parameter, issue an error message here. */
27217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27219 cp_parser_error (parser
, "invalid explicit specialization");
27220 begin_specialization ();
27221 parameter_list
= NULL_TREE
;
27225 /* Parse the template parameters. */
27226 parameter_list
= cp_parser_template_parameter_list (parser
);
27229 /* Look for the `>'. */
27230 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27232 /* Manage template requirements */
27235 tree reqs
= get_shorthand_constraints (current_template_parms
);
27236 if (tree r
= cp_parser_requires_clause_opt (parser
))
27237 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27238 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27241 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27244 /* For the erroneous case of a template with C linkage, we pushed an
27245 implicit C++ linkage scope; exit that scope now. */
27247 pop_lang_context ();
27250 /* Parse a template-declaration, assuming that the `export' (and
27251 `extern') keywords, if present, has already been scanned. MEMBER_P
27252 is as for cp_parser_template_declaration. */
27255 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27257 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27259 cp_lexer_consume_token (parser
->lexer
);
27260 cp_parser_explicit_template_declaration (parser
, member_p
);
27263 else if (flag_concepts
)
27264 return cp_parser_template_introduction (parser
, member_p
);
27269 /* Perform the deferred access checks from a template-parameter-list.
27270 CHECKS is a TREE_LIST of access checks, as returned by
27271 get_deferred_access_checks. */
27274 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27276 ++processing_template_parmlist
;
27277 perform_access_checks (checks
, tf_warning_or_error
);
27278 --processing_template_parmlist
;
27281 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27282 `function-definition' sequence that follows a template header.
27283 If MEMBER_P is true, this declaration appears in a class scope.
27285 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27286 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27289 cp_parser_single_declaration (cp_parser
* parser
,
27290 vec
<deferred_access_check
, va_gc
> *checks
,
27292 bool explicit_specialization_p
,
27295 int declares_class_or_enum
;
27296 tree decl
= NULL_TREE
;
27297 cp_decl_specifier_seq decl_specifiers
;
27298 bool function_definition_p
= false;
27299 cp_token
*decl_spec_token_start
;
27301 /* This function is only used when processing a template
27303 gcc_assert (innermost_scope_kind () == sk_template_parms
27304 || innermost_scope_kind () == sk_template_spec
);
27306 /* Defer access checks until we know what is being declared. */
27307 push_deferring_access_checks (dk_deferred
);
27309 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27311 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27312 cp_parser_decl_specifier_seq (parser
,
27313 CP_PARSER_FLAGS_OPTIONAL
,
27315 &declares_class_or_enum
);
27317 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27319 /* There are no template typedefs. */
27320 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27322 error_at (decl_spec_token_start
->location
,
27323 "template declaration of %<typedef%>");
27324 decl
= error_mark_node
;
27327 /* Gather up the access checks that occurred the
27328 decl-specifier-seq. */
27329 stop_deferring_access_checks ();
27331 /* Check for the declaration of a template class. */
27332 if (declares_class_or_enum
)
27334 if (cp_parser_declares_only_class_p (parser
)
27335 || (declares_class_or_enum
& 2))
27337 // If this is a declaration, but not a definition, associate
27338 // any constraints with the type declaration. Constraints
27339 // are associated with definitions in cp_parser_class_specifier.
27340 if (declares_class_or_enum
== 1)
27341 associate_classtype_constraints (decl_specifiers
.type
);
27343 decl
= shadow_tag (&decl_specifiers
);
27348 friend template <typename T> struct A<T>::B;
27351 A<T>::B will be represented by a TYPENAME_TYPE, and
27352 therefore not recognized by shadow_tag. */
27353 if (friend_p
&& *friend_p
27355 && decl_specifiers
.type
27356 && TYPE_P (decl_specifiers
.type
))
27357 decl
= decl_specifiers
.type
;
27359 if (decl
&& decl
!= error_mark_node
)
27360 decl
= TYPE_NAME (decl
);
27362 decl
= error_mark_node
;
27364 /* Perform access checks for template parameters. */
27365 cp_parser_perform_template_parameter_access_checks (checks
);
27367 /* Give a helpful diagnostic for
27368 template <class T> struct A { } a;
27369 if we aren't already recovering from an error. */
27370 if (!cp_parser_declares_only_class_p (parser
)
27373 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27374 "a class template declaration must not declare "
27376 cp_parser_skip_to_end_of_block_or_statement (parser
);
27382 /* Complain about missing 'typename' or other invalid type names. */
27383 if (!decl_specifiers
.any_type_specifiers_p
27384 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27386 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27387 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27388 the rest of this declaration. */
27389 decl
= error_mark_node
;
27393 /* If it's not a template class, try for a template function. If
27394 the next token is a `;', then this declaration does not declare
27395 anything. But, if there were errors in the decl-specifiers, then
27396 the error might well have come from an attempted class-specifier.
27397 In that case, there's no need to warn about a missing declarator. */
27399 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27400 || decl_specifiers
.type
!= error_mark_node
))
27402 decl
= cp_parser_init_declarator (parser
,
27405 /*function_definition_allowed_p=*/true,
27407 declares_class_or_enum
,
27408 &function_definition_p
,
27411 /* 7.1.1-1 [dcl.stc]
27413 A storage-class-specifier shall not be specified in an explicit
27414 specialization... */
27416 && explicit_specialization_p
27417 && decl_specifiers
.storage_class
!= sc_none
)
27419 error_at (decl_spec_token_start
->location
,
27420 "explicit template specialization cannot have a storage class");
27421 decl
= error_mark_node
;
27424 if (decl
&& VAR_P (decl
))
27425 check_template_variable (decl
);
27428 /* Look for a trailing `;' after the declaration. */
27429 if (!function_definition_p
27430 && (decl
== error_mark_node
27431 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27432 cp_parser_skip_to_end_of_block_or_statement (parser
);
27435 pop_deferring_access_checks ();
27437 /* Clear any current qualification; whatever comes next is the start
27438 of something new. */
27439 parser
->scope
= NULL_TREE
;
27440 parser
->qualifying_scope
= NULL_TREE
;
27441 parser
->object_scope
= NULL_TREE
;
27446 /* Parse a cast-expression that is not the operand of a unary "&". */
27449 cp_parser_simple_cast_expression (cp_parser
*parser
)
27451 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27452 /*cast_p=*/false, /*decltype*/false, NULL
);
27455 /* Parse a functional cast to TYPE. Returns an expression
27456 representing the cast. */
27459 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27461 vec
<tree
, va_gc
> *vec
;
27462 tree expression_list
;
27466 location_t start_loc
= input_location
;
27469 type
= error_mark_node
;
27471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27473 cp_lexer_set_source_position (parser
->lexer
);
27474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27475 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27476 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27477 if (TREE_CODE (type
) == TYPE_DECL
)
27478 type
= TREE_TYPE (type
);
27480 cast
= finish_compound_literal (type
, expression_list
,
27481 tf_warning_or_error
, fcl_functional
);
27482 /* Create a location of the form:
27485 with caret == start at the start of the type name,
27486 finishing at the closing brace. */
27487 location_t finish_loc
27488 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27489 location_t combined_loc
= make_location (start_loc
, start_loc
,
27491 cast
.set_location (combined_loc
);
27496 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27498 /*allow_expansion_p=*/true,
27499 /*non_constant_p=*/NULL
);
27501 expression_list
= error_mark_node
;
27504 expression_list
= build_tree_list_vec (vec
);
27505 release_tree_vector (vec
);
27508 cast
= build_functional_cast (type
, expression_list
,
27509 tf_warning_or_error
);
27510 /* [expr.const]/1: In an integral constant expression "only type
27511 conversions to integral or enumeration type can be used". */
27512 if (TREE_CODE (type
) == TYPE_DECL
)
27513 type
= TREE_TYPE (type
);
27514 if (cast
!= error_mark_node
27515 && !cast_valid_in_integral_constant_expression_p (type
)
27516 && cp_parser_non_integral_constant_expression (parser
,
27518 return error_mark_node
;
27520 /* Create a location of the form:
27523 with caret == start at the start of the type name,
27524 finishing at the closing paren. */
27525 location_t finish_loc
27526 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27527 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27528 cast
.set_location (combined_loc
);
27532 /* Save the tokens that make up the body of a member function defined
27533 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27534 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27535 specifiers applied to the declaration. Returns the FUNCTION_DECL
27536 for the member function. */
27539 cp_parser_save_member_function_body (cp_parser
* parser
,
27540 cp_decl_specifier_seq
*decl_specifiers
,
27541 cp_declarator
*declarator
,
27547 bool function_try_block
= false;
27549 /* Create the FUNCTION_DECL. */
27550 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27551 cp_finalize_omp_declare_simd (parser
, fn
);
27552 cp_finalize_oacc_routine (parser
, fn
, true);
27553 /* If something went badly wrong, bail out now. */
27554 if (fn
== error_mark_node
)
27556 /* If there's a function-body, skip it. */
27557 if (cp_parser_token_starts_function_definition_p
27558 (cp_lexer_peek_token (parser
->lexer
)))
27559 cp_parser_skip_to_end_of_block_or_statement (parser
);
27560 return error_mark_node
;
27563 /* Remember it, if there default args to post process. */
27564 cp_parser_save_default_args (parser
, fn
);
27566 /* Save away the tokens that make up the body of the
27568 first
= parser
->lexer
->next_token
;
27570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27571 cp_lexer_consume_token (parser
->lexer
);
27572 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27573 RID_TRANSACTION_ATOMIC
))
27575 cp_lexer_consume_token (parser
->lexer
);
27576 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27578 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27579 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27580 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27581 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27582 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27584 cp_lexer_consume_token (parser
->lexer
);
27585 cp_lexer_consume_token (parser
->lexer
);
27586 cp_lexer_consume_token (parser
->lexer
);
27587 cp_lexer_consume_token (parser
->lexer
);
27588 cp_lexer_consume_token (parser
->lexer
);
27591 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27592 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27594 cp_lexer_consume_token (parser
->lexer
);
27595 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27600 /* Handle function try blocks. */
27601 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27603 cp_lexer_consume_token (parser
->lexer
);
27604 function_try_block
= true;
27606 /* We can have braced-init-list mem-initializers before the fn body. */
27607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27609 cp_lexer_consume_token (parser
->lexer
);
27610 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27612 /* cache_group will stop after an un-nested { } pair, too. */
27613 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27616 /* variadic mem-inits have ... after the ')'. */
27617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27618 cp_lexer_consume_token (parser
->lexer
);
27621 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27622 /* Handle function try blocks. */
27623 if (function_try_block
)
27624 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27625 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27626 last
= parser
->lexer
->next_token
;
27628 /* Save away the inline definition; we will process it when the
27629 class is complete. */
27630 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27631 DECL_PENDING_INLINE_P (fn
) = 1;
27633 /* We need to know that this was defined in the class, so that
27634 friend templates are handled correctly. */
27635 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27637 /* Add FN to the queue of functions to be parsed later. */
27638 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27643 /* Save the tokens that make up the in-class initializer for a non-static
27644 data member. Returns a DEFAULT_ARG. */
27647 cp_parser_save_nsdmi (cp_parser
* parser
)
27649 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27652 /* Parse a template-argument-list, as well as the trailing ">" (but
27653 not the opening "<"). See cp_parser_template_argument_list for the
27657 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27661 tree saved_qualifying_scope
;
27662 tree saved_object_scope
;
27663 bool saved_greater_than_is_operator_p
;
27664 int saved_unevaluated_operand
;
27665 int saved_inhibit_evaluation_warnings
;
27669 When parsing a template-id, the first non-nested `>' is taken as
27670 the end of the template-argument-list rather than a greater-than
27672 saved_greater_than_is_operator_p
27673 = parser
->greater_than_is_operator_p
;
27674 parser
->greater_than_is_operator_p
= false;
27675 /* Parsing the argument list may modify SCOPE, so we save it
27677 saved_scope
= parser
->scope
;
27678 saved_qualifying_scope
= parser
->qualifying_scope
;
27679 saved_object_scope
= parser
->object_scope
;
27680 /* We need to evaluate the template arguments, even though this
27681 template-id may be nested within a "sizeof". */
27682 saved_unevaluated_operand
= cp_unevaluated_operand
;
27683 cp_unevaluated_operand
= 0;
27684 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27685 c_inhibit_evaluation_warnings
= 0;
27686 /* Parse the template-argument-list itself. */
27687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27688 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27689 arguments
= NULL_TREE
;
27691 arguments
= cp_parser_template_argument_list (parser
);
27692 /* Look for the `>' that ends the template-argument-list. If we find
27693 a '>>' instead, it's probably just a typo. */
27694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27696 if (cxx_dialect
!= cxx98
)
27698 /* In C++0x, a `>>' in a template argument list or cast
27699 expression is considered to be two separate `>'
27700 tokens. So, change the current token to a `>', but don't
27701 consume it: it will be consumed later when the outer
27702 template argument list (or cast expression) is parsed.
27703 Note that this replacement of `>' for `>>' is necessary
27704 even if we are parsing tentatively: in the tentative
27705 case, after calling
27706 cp_parser_enclosed_template_argument_list we will always
27707 throw away all of the template arguments and the first
27708 closing `>', either because the template argument list
27709 was erroneous or because we are replacing those tokens
27710 with a CPP_TEMPLATE_ID token. The second `>' (which will
27711 not have been thrown away) is needed either to close an
27712 outer template argument list or to complete a new-style
27714 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27715 token
->type
= CPP_GREATER
;
27717 else if (!saved_greater_than_is_operator_p
)
27719 /* If we're in a nested template argument list, the '>>' has
27720 to be a typo for '> >'. We emit the error message, but we
27721 continue parsing and we push a '>' as next token, so that
27722 the argument list will be parsed correctly. Note that the
27723 global source location is still on the token before the
27724 '>>', so we need to say explicitly where we want it. */
27725 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27726 gcc_rich_location
richloc (token
->location
);
27727 richloc
.add_fixit_replace ("> >");
27728 error_at (&richloc
, "%<>>%> should be %<> >%> "
27729 "within a nested template argument list");
27731 token
->type
= CPP_GREATER
;
27735 /* If this is not a nested template argument list, the '>>'
27736 is a typo for '>'. Emit an error message and continue.
27737 Same deal about the token location, but here we can get it
27738 right by consuming the '>>' before issuing the diagnostic. */
27739 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27740 error_at (token
->location
,
27741 "spurious %<>>%>, use %<>%> to terminate "
27742 "a template argument list");
27746 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27747 /* The `>' token might be a greater-than operator again now. */
27748 parser
->greater_than_is_operator_p
27749 = saved_greater_than_is_operator_p
;
27750 /* Restore the SAVED_SCOPE. */
27751 parser
->scope
= saved_scope
;
27752 parser
->qualifying_scope
= saved_qualifying_scope
;
27753 parser
->object_scope
= saved_object_scope
;
27754 cp_unevaluated_operand
= saved_unevaluated_operand
;
27755 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27760 /* MEMBER_FUNCTION is a member function, or a friend. If default
27761 arguments, or the body of the function have not yet been parsed,
27765 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27767 timevar_push (TV_PARSE_INMETH
);
27768 /* If this member is a template, get the underlying
27770 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27771 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27773 /* There should not be any class definitions in progress at this
27774 point; the bodies of members are only parsed outside of all class
27776 gcc_assert (parser
->num_classes_being_defined
== 0);
27777 /* While we're parsing the member functions we might encounter more
27778 classes. We want to handle them right away, but we don't want
27779 them getting mixed up with functions that are currently in the
27781 push_unparsed_function_queues (parser
);
27783 /* Make sure that any template parameters are in scope. */
27784 maybe_begin_member_template_processing (member_function
);
27786 /* If the body of the function has not yet been parsed, parse it
27788 if (DECL_PENDING_INLINE_P (member_function
))
27790 tree function_scope
;
27791 cp_token_cache
*tokens
;
27793 /* The function is no longer pending; we are processing it. */
27794 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27795 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27796 DECL_PENDING_INLINE_P (member_function
) = 0;
27798 /* If this is a local class, enter the scope of the containing
27800 function_scope
= current_function_decl
;
27801 if (function_scope
)
27802 push_function_context ();
27804 /* Push the body of the function onto the lexer stack. */
27805 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27807 /* Let the front end know that we going to be defining this
27809 start_preparsed_function (member_function
, NULL_TREE
,
27810 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27812 /* Don't do access checking if it is a templated function. */
27813 if (processing_template_decl
)
27814 push_deferring_access_checks (dk_no_check
);
27816 /* #pragma omp declare reduction needs special parsing. */
27817 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27819 parser
->lexer
->in_pragma
= true;
27820 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27821 finish_function (/*inline_p=*/true);
27822 cp_check_omp_declare_reduction (member_function
);
27825 /* Now, parse the body of the function. */
27826 cp_parser_function_definition_after_declarator (parser
,
27827 /*inline_p=*/true);
27829 if (processing_template_decl
)
27830 pop_deferring_access_checks ();
27832 /* Leave the scope of the containing function. */
27833 if (function_scope
)
27834 pop_function_context ();
27835 cp_parser_pop_lexer (parser
);
27838 /* Remove any template parameters from the symbol table. */
27839 maybe_end_member_template_processing ();
27841 /* Restore the queue. */
27842 pop_unparsed_function_queues (parser
);
27843 timevar_pop (TV_PARSE_INMETH
);
27846 /* If DECL contains any default args, remember it on the unparsed
27847 functions queue. */
27850 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27854 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27856 probe
= TREE_CHAIN (probe
))
27857 if (TREE_PURPOSE (probe
))
27859 cp_default_arg_entry entry
= {current_class_type
, decl
};
27860 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27865 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27866 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27867 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27868 from the parameter-type-list. */
27871 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27872 tree default_arg
, tree parmtype
)
27874 cp_token_cache
*tokens
;
27878 if (default_arg
== error_mark_node
)
27879 return error_mark_node
;
27881 /* Push the saved tokens for the default argument onto the parser's
27883 tokens
= DEFARG_TOKENS (default_arg
);
27884 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27886 start_lambda_scope (decl
);
27888 /* Parse the default argument. */
27889 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27890 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27891 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27893 finish_lambda_scope ();
27895 if (parsed_arg
== error_mark_node
)
27896 cp_parser_skip_to_end_of_statement (parser
);
27898 if (!processing_template_decl
)
27900 /* In a non-template class, check conversions now. In a template,
27901 we'll wait and instantiate these as needed. */
27902 if (TREE_CODE (decl
) == PARM_DECL
)
27903 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27904 tf_warning_or_error
);
27905 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27906 parsed_arg
= error_mark_node
;
27908 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27911 /* If the token stream has not been completely used up, then
27912 there was extra junk after the end of the default
27914 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27916 if (TREE_CODE (decl
) == PARM_DECL
)
27917 cp_parser_error (parser
, "expected %<,%>");
27919 cp_parser_error (parser
, "expected %<;%>");
27922 /* Revert to the main lexer. */
27923 cp_parser_pop_lexer (parser
);
27928 /* FIELD is a non-static data member with an initializer which we saved for
27929 later; parse it now. */
27932 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27936 maybe_begin_member_template_processing (field
);
27938 push_unparsed_function_queues (parser
);
27939 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27940 DECL_INITIAL (field
),
27942 pop_unparsed_function_queues (parser
);
27944 maybe_end_member_template_processing ();
27946 DECL_INITIAL (field
) = def
;
27949 /* FN is a FUNCTION_DECL which may contains a parameter with an
27950 unparsed DEFAULT_ARG. Parse the default args now. This function
27951 assumes that the current scope is the scope in which the default
27952 argument should be processed. */
27955 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27957 bool saved_local_variables_forbidden_p
;
27958 tree parm
, parmdecl
;
27960 /* While we're parsing the default args, we might (due to the
27961 statement expression extension) encounter more classes. We want
27962 to handle them right away, but we don't want them getting mixed
27963 up with default args that are currently in the queue. */
27964 push_unparsed_function_queues (parser
);
27966 /* Local variable names (and the `this' keyword) may not appear
27967 in a default argument. */
27968 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27969 parser
->local_variables_forbidden_p
= true;
27971 push_defarg_context (fn
);
27973 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27974 parmdecl
= DECL_ARGUMENTS (fn
);
27975 parm
&& parm
!= void_list_node
;
27976 parm
= TREE_CHAIN (parm
),
27977 parmdecl
= DECL_CHAIN (parmdecl
))
27979 tree default_arg
= TREE_PURPOSE (parm
);
27981 vec
<tree
, va_gc
> *insts
;
27988 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27989 /* This can happen for a friend declaration for a function
27990 already declared with default arguments. */
27994 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27996 TREE_VALUE (parm
));
27997 TREE_PURPOSE (parm
) = parsed_arg
;
27999 /* Update any instantiations we've already created. */
28000 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28001 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28002 TREE_PURPOSE (copy
) = parsed_arg
;
28005 pop_defarg_context ();
28007 /* Make sure no default arg is missing. */
28008 check_default_args (fn
);
28010 /* Restore the state of local_variables_forbidden_p. */
28011 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28013 /* Restore the queue. */
28014 pop_unparsed_function_queues (parser
);
28017 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28019 sizeof ... ( identifier )
28021 where the 'sizeof' token has already been consumed. */
28024 cp_parser_sizeof_pack (cp_parser
*parser
)
28026 /* Consume the `...'. */
28027 cp_lexer_consume_token (parser
->lexer
);
28028 maybe_warn_variadic_templates ();
28030 matching_parens parens
;
28031 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28033 parens
.consume_open (parser
);
28035 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28036 "%<sizeof...%> argument must be surrounded by parentheses");
28038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28039 tree name
= cp_parser_identifier (parser
);
28040 if (name
== error_mark_node
)
28041 return error_mark_node
;
28042 /* The name is not qualified. */
28043 parser
->scope
= NULL_TREE
;
28044 parser
->qualifying_scope
= NULL_TREE
;
28045 parser
->object_scope
= NULL_TREE
;
28046 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28047 if (expr
== error_mark_node
)
28048 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28050 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28051 expr
= TREE_TYPE (expr
);
28052 else if (TREE_CODE (expr
) == CONST_DECL
)
28053 expr
= DECL_INITIAL (expr
);
28054 expr
= make_pack_expansion (expr
);
28055 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28058 parens
.require_close (parser
);
28063 /* Parse the operand of `sizeof' (or a similar operator). Returns
28064 either a TYPE or an expression, depending on the form of the
28065 input. The KEYWORD indicates which kind of expression we have
28069 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28071 tree expr
= NULL_TREE
;
28072 const char *saved_message
;
28074 bool saved_integral_constant_expression_p
;
28075 bool saved_non_integral_constant_expression_p
;
28077 /* If it's a `...', then we are computing the length of a parameter
28079 if (keyword
== RID_SIZEOF
28080 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28081 return cp_parser_sizeof_pack (parser
);
28083 /* Types cannot be defined in a `sizeof' expression. Save away the
28085 saved_message
= parser
->type_definition_forbidden_message
;
28086 /* And create the new one. */
28087 tmp
= concat ("types may not be defined in %<",
28088 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28089 "%> expressions", NULL
);
28090 parser
->type_definition_forbidden_message
= tmp
;
28092 /* The restrictions on constant-expressions do not apply inside
28093 sizeof expressions. */
28094 saved_integral_constant_expression_p
28095 = parser
->integral_constant_expression_p
;
28096 saved_non_integral_constant_expression_p
28097 = parser
->non_integral_constant_expression_p
;
28098 parser
->integral_constant_expression_p
= false;
28100 /* Do not actually evaluate the expression. */
28101 ++cp_unevaluated_operand
;
28102 ++c_inhibit_evaluation_warnings
;
28103 /* If it's a `(', then we might be looking at the type-id
28105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28107 tree type
= NULL_TREE
;
28109 /* We can't be sure yet whether we're looking at a type-id or an
28111 cp_parser_parse_tentatively (parser
);
28113 matching_parens parens
;
28114 parens
.consume_open (parser
);
28116 /* Note: as a GNU Extension, compound literals are considered
28117 postfix-expressions as they are in C99, so they are valid
28118 arguments to sizeof. See comment in cp_parser_cast_expression
28120 if (cp_parser_compound_literal_p (parser
))
28121 cp_parser_simulate_error (parser
);
28124 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28125 parser
->in_type_id_in_expr_p
= true;
28126 /* Look for the type-id. */
28127 type
= cp_parser_type_id (parser
);
28128 /* Look for the closing `)'. */
28129 parens
.require_close (parser
);
28130 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28133 /* If all went well, then we're done. */
28134 if (cp_parser_parse_definitely (parser
))
28136 cp_decl_specifier_seq decl_specs
;
28138 /* Build a trivial decl-specifier-seq. */
28139 clear_decl_specs (&decl_specs
);
28140 decl_specs
.type
= type
;
28142 /* Call grokdeclarator to figure out what type this is. */
28143 expr
= grokdeclarator (NULL
,
28147 /*attrlist=*/NULL
);
28151 /* If the type-id production did not work out, then we must be
28152 looking at the unary-expression production. */
28154 expr
= cp_parser_unary_expression (parser
);
28156 /* Go back to evaluating expressions. */
28157 --cp_unevaluated_operand
;
28158 --c_inhibit_evaluation_warnings
;
28160 /* Free the message we created. */
28162 /* And restore the old one. */
28163 parser
->type_definition_forbidden_message
= saved_message
;
28164 parser
->integral_constant_expression_p
28165 = saved_integral_constant_expression_p
;
28166 parser
->non_integral_constant_expression_p
28167 = saved_non_integral_constant_expression_p
;
28172 /* If the current declaration has no declarator, return true. */
28175 cp_parser_declares_only_class_p (cp_parser
*parser
)
28177 /* If the next token is a `;' or a `,' then there is no
28179 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28180 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28183 /* Update the DECL_SPECS to reflect the storage class indicated by
28187 cp_parser_set_storage_class (cp_parser
*parser
,
28188 cp_decl_specifier_seq
*decl_specs
,
28192 cp_storage_class storage_class
;
28194 if (parser
->in_unbraced_linkage_specification_p
)
28196 error_at (token
->location
, "invalid use of %qD in linkage specification",
28197 ridpointers
[keyword
]);
28200 else if (decl_specs
->storage_class
!= sc_none
)
28202 decl_specs
->conflicting_specifiers_p
= true;
28206 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28207 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28208 && decl_specs
->gnu_thread_keyword_p
)
28210 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28211 "%<__thread%> before %qD", ridpointers
[keyword
]);
28217 storage_class
= sc_auto
;
28220 storage_class
= sc_register
;
28223 storage_class
= sc_static
;
28226 storage_class
= sc_extern
;
28229 storage_class
= sc_mutable
;
28232 gcc_unreachable ();
28234 decl_specs
->storage_class
= storage_class
;
28235 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28237 /* A storage class specifier cannot be applied alongside a typedef
28238 specifier. If there is a typedef specifier present then set
28239 conflicting_specifiers_p which will trigger an error later
28240 on in grokdeclarator. */
28241 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28242 decl_specs
->conflicting_specifiers_p
= true;
28245 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28246 is true, the type is a class or enum definition. */
28249 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28252 bool type_definition_p
)
28254 decl_specs
->any_specifiers_p
= true;
28256 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28257 (with, for example, in "typedef int wchar_t;") we remember that
28258 this is what happened. In system headers, we ignore these
28259 declarations so that G++ can work with system headers that are not
28261 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28262 && !type_definition_p
28263 && (type_spec
== boolean_type_node
28264 || type_spec
== char16_type_node
28265 || type_spec
== char32_type_node
28266 || type_spec
== wchar_type_node
)
28267 && (decl_specs
->type
28268 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28269 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28270 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28271 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28273 decl_specs
->redefined_builtin_type
= type_spec
;
28274 set_and_check_decl_spec_loc (decl_specs
,
28275 ds_redefined_builtin_type_spec
,
28277 if (!decl_specs
->type
)
28279 decl_specs
->type
= type_spec
;
28280 decl_specs
->type_definition_p
= false;
28281 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28284 else if (decl_specs
->type
)
28285 decl_specs
->multiple_types_p
= true;
28288 decl_specs
->type
= type_spec
;
28289 decl_specs
->type_definition_p
= type_definition_p
;
28290 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28291 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28295 /* True iff TOKEN is the GNU keyword __thread. */
28298 token_is__thread (cp_token
*token
)
28300 gcc_assert (token
->keyword
== RID_THREAD
);
28301 return id_equal (token
->u
.value
, "__thread");
28304 /* Set the location for a declarator specifier and check if it is
28307 DECL_SPECS is the sequence of declarator specifiers onto which to
28310 DS is the single declarator specifier to set which location is to
28311 be set onto the existing sequence of declarators.
28313 LOCATION is the location for the declarator specifier to
28317 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28318 cp_decl_spec ds
, cp_token
*token
)
28320 gcc_assert (ds
< ds_last
);
28322 if (decl_specs
== NULL
)
28325 source_location location
= token
->location
;
28327 if (decl_specs
->locations
[ds
] == 0)
28329 decl_specs
->locations
[ds
] = location
;
28330 if (ds
== ds_thread
)
28331 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28337 if (decl_specs
->locations
[ds_long_long
] != 0)
28338 error_at (location
,
28339 "%<long long long%> is too long for GCC");
28342 decl_specs
->locations
[ds_long_long
] = location
;
28343 pedwarn_cxx98 (location
,
28345 "ISO C++ 1998 does not support %<long long%>");
28348 else if (ds
== ds_thread
)
28350 bool gnu
= token_is__thread (token
);
28351 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28352 error_at (location
,
28353 "both %<__thread%> and %<thread_local%> specified");
28356 gcc_rich_location
richloc (location
);
28357 richloc
.add_fixit_remove ();
28358 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28363 static const char *const decl_spec_names
[] = {
28380 gcc_rich_location
richloc (location
);
28381 richloc
.add_fixit_remove ();
28382 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28387 /* Return true iff the declarator specifier DS is present in the
28388 sequence of declarator specifiers DECL_SPECS. */
28391 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28394 gcc_assert (ds
< ds_last
);
28396 if (decl_specs
== NULL
)
28399 return decl_specs
->locations
[ds
] != 0;
28402 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28403 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28406 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28408 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28411 /* Issue an error message indicating that TOKEN_DESC was expected.
28412 If KEYWORD is true, it indicated this function is called by
28413 cp_parser_require_keword and the required token can only be
28414 a indicated keyword.
28416 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28417 within any error as the location of an "opening" token matching
28418 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28419 RT_CLOSE_PAREN). */
28422 cp_parser_required_error (cp_parser
*parser
,
28423 required_token token_desc
,
28425 location_t matching_location
)
28427 if (cp_parser_simulate_error (parser
))
28430 const char *gmsgid
= NULL
;
28431 switch (token_desc
)
28434 gmsgid
= G_("expected %<new%>");
28437 gmsgid
= G_("expected %<delete%>");
28440 gmsgid
= G_("expected %<return%>");
28443 gmsgid
= G_("expected %<while%>");
28446 gmsgid
= G_("expected %<extern%>");
28448 case RT_STATIC_ASSERT
:
28449 gmsgid
= G_("expected %<static_assert%>");
28452 gmsgid
= G_("expected %<decltype%>");
28455 gmsgid
= G_("expected %<operator%>");
28458 gmsgid
= G_("expected %<class%>");
28461 gmsgid
= G_("expected %<template%>");
28464 gmsgid
= G_("expected %<namespace%>");
28467 gmsgid
= G_("expected %<using%>");
28470 gmsgid
= G_("expected %<asm%>");
28473 gmsgid
= G_("expected %<try%>");
28476 gmsgid
= G_("expected %<catch%>");
28479 gmsgid
= G_("expected %<throw%>");
28482 gmsgid
= G_("expected %<__label__%>");
28485 gmsgid
= G_("expected %<@try%>");
28487 case RT_AT_SYNCHRONIZED
:
28488 gmsgid
= G_("expected %<@synchronized%>");
28491 gmsgid
= G_("expected %<@throw%>");
28493 case RT_TRANSACTION_ATOMIC
:
28494 gmsgid
= G_("expected %<__transaction_atomic%>");
28496 case RT_TRANSACTION_RELAXED
:
28497 gmsgid
= G_("expected %<__transaction_relaxed%>");
28503 if (!gmsgid
&& !keyword
)
28505 switch (token_desc
)
28508 gmsgid
= G_("expected %<;%>");
28510 case RT_OPEN_PAREN
:
28511 gmsgid
= G_("expected %<(%>");
28513 case RT_CLOSE_BRACE
:
28514 gmsgid
= G_("expected %<}%>");
28516 case RT_OPEN_BRACE
:
28517 gmsgid
= G_("expected %<{%>");
28519 case RT_CLOSE_SQUARE
:
28520 gmsgid
= G_("expected %<]%>");
28522 case RT_OPEN_SQUARE
:
28523 gmsgid
= G_("expected %<[%>");
28526 gmsgid
= G_("expected %<,%>");
28529 gmsgid
= G_("expected %<::%>");
28532 gmsgid
= G_("expected %<<%>");
28535 gmsgid
= G_("expected %<>%>");
28538 gmsgid
= G_("expected %<=%>");
28541 gmsgid
= G_("expected %<...%>");
28544 gmsgid
= G_("expected %<*%>");
28547 gmsgid
= G_("expected %<~%>");
28550 gmsgid
= G_("expected %<:%>");
28552 case RT_COLON_SCOPE
:
28553 gmsgid
= G_("expected %<:%> or %<::%>");
28555 case RT_CLOSE_PAREN
:
28556 gmsgid
= G_("expected %<)%>");
28558 case RT_COMMA_CLOSE_PAREN
:
28559 gmsgid
= G_("expected %<,%> or %<)%>");
28561 case RT_PRAGMA_EOL
:
28562 gmsgid
= G_("expected end of line");
28565 gmsgid
= G_("expected identifier");
28568 gmsgid
= G_("expected selection-statement");
28571 gmsgid
= G_("expected iteration-statement");
28574 gmsgid
= G_("expected jump-statement");
28577 gmsgid
= G_("expected class-key");
28579 case RT_CLASS_TYPENAME_TEMPLATE
:
28580 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28583 gcc_unreachable ();
28588 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28592 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28593 issue an error message indicating that TOKEN_DESC was expected.
28595 Returns the token consumed, if the token had the appropriate type.
28596 Otherwise, returns NULL.
28598 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28599 within any error as the location of an "opening" token matching
28600 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28601 RT_CLOSE_PAREN). */
28604 cp_parser_require (cp_parser
* parser
,
28605 enum cpp_ttype type
,
28606 required_token token_desc
,
28607 location_t matching_location
)
28609 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28610 return cp_lexer_consume_token (parser
->lexer
);
28613 /* Output the MESSAGE -- unless we're parsing tentatively. */
28614 if (!cp_parser_simulate_error (parser
))
28615 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28616 matching_location
);
28621 /* An error message is produced if the next token is not '>'.
28622 All further tokens are skipped until the desired token is
28623 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28626 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28628 /* Current level of '< ... >'. */
28629 unsigned level
= 0;
28630 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28631 unsigned nesting_depth
= 0;
28633 /* Are we ready, yet? If not, issue error message. */
28634 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28637 /* Skip tokens until the desired token is found. */
28640 /* Peek at the next token. */
28641 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28644 if (!nesting_depth
)
28649 if (cxx_dialect
== cxx98
)
28650 /* C++0x views the `>>' operator as two `>' tokens, but
28653 else if (!nesting_depth
&& level
-- == 0)
28655 /* We've hit a `>>' where the first `>' closes the
28656 template argument list, and the second `>' is
28657 spurious. Just consume the `>>' and stop; we've
28658 already produced at least one error. */
28659 cp_lexer_consume_token (parser
->lexer
);
28662 /* Fall through for C++0x, so we handle the second `>' in
28664 gcc_fallthrough ();
28667 if (!nesting_depth
&& level
-- == 0)
28669 /* We've reached the token we want, consume it and stop. */
28670 cp_lexer_consume_token (parser
->lexer
);
28675 case CPP_OPEN_PAREN
:
28676 case CPP_OPEN_SQUARE
:
28680 case CPP_CLOSE_PAREN
:
28681 case CPP_CLOSE_SQUARE
:
28682 if (nesting_depth
-- == 0)
28687 case CPP_PRAGMA_EOL
:
28688 case CPP_SEMICOLON
:
28689 case CPP_OPEN_BRACE
:
28690 case CPP_CLOSE_BRACE
:
28691 /* The '>' was probably forgotten, don't look further. */
28698 /* Consume this token. */
28699 cp_lexer_consume_token (parser
->lexer
);
28703 /* If the next token is the indicated keyword, consume it. Otherwise,
28704 issue an error message indicating that TOKEN_DESC was expected.
28706 Returns the token consumed, if the token had the appropriate type.
28707 Otherwise, returns NULL. */
28710 cp_parser_require_keyword (cp_parser
* parser
,
28712 required_token token_desc
)
28714 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28716 if (token
&& token
->keyword
!= keyword
)
28718 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28726 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28727 function-definition. */
28730 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28732 return (/* An ordinary function-body begins with an `{'. */
28733 token
->type
== CPP_OPEN_BRACE
28734 /* A ctor-initializer begins with a `:'. */
28735 || token
->type
== CPP_COLON
28736 /* A function-try-block begins with `try'. */
28737 || token
->keyword
== RID_TRY
28738 /* A function-transaction-block begins with `__transaction_atomic'
28739 or `__transaction_relaxed'. */
28740 || token
->keyword
== RID_TRANSACTION_ATOMIC
28741 || token
->keyword
== RID_TRANSACTION_RELAXED
28742 /* The named return value extension begins with `return'. */
28743 || token
->keyword
== RID_RETURN
);
28746 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28750 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28754 token
= cp_lexer_peek_token (parser
->lexer
);
28755 return (token
->type
== CPP_OPEN_BRACE
28756 || (token
->type
== CPP_COLON
28757 && !parser
->colon_doesnt_start_class_def_p
));
28760 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28761 C++0x) ending a template-argument. */
28764 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28768 token
= cp_lexer_peek_token (parser
->lexer
);
28769 return (token
->type
== CPP_COMMA
28770 || token
->type
== CPP_GREATER
28771 || token
->type
== CPP_ELLIPSIS
28772 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28775 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28776 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28779 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28784 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28785 if (token
->type
== CPP_LESS
)
28787 /* Check for the sequence `<::' in the original code. It would be lexed as
28788 `[:', where `[' is a digraph, and there is no whitespace before
28790 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28793 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28794 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28800 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28801 or none_type otherwise. */
28803 static enum tag_types
28804 cp_parser_token_is_class_key (cp_token
* token
)
28806 switch (token
->keyword
)
28811 return record_type
;
28820 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28821 or none_type otherwise or if the token is null. */
28823 static enum tag_types
28824 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28829 switch (token
->keyword
)
28834 return typename_type
;
28841 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28844 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28846 if (type
== error_mark_node
)
28848 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28850 if (permerror (input_location
, "%qs tag used in naming %q#T",
28851 class_key
== union_type
? "union"
28852 : class_key
== record_type
? "struct" : "class",
28854 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28855 "%q#T was previously declared here", type
);
28859 /* Issue an error message if DECL is redeclared with different
28860 access than its original declaration [class.access.spec/3].
28861 This applies to nested classes, nested class templates and
28862 enumerations [class.mem/1]. */
28865 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28868 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28869 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28872 if ((TREE_PRIVATE (decl
)
28873 != (current_access_specifier
== access_private_node
))
28874 || (TREE_PROTECTED (decl
)
28875 != (current_access_specifier
== access_protected_node
)))
28876 error_at (location
, "%qD redeclared with different access", decl
);
28879 /* Look for the `template' keyword, as a syntactic disambiguator.
28880 Return TRUE iff it is present, in which case it will be
28884 cp_parser_optional_template_keyword (cp_parser
*parser
)
28886 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28888 /* In C++98 the `template' keyword can only be used within templates;
28889 outside templates the parser can always figure out what is a
28890 template and what is not. In C++11, per the resolution of DR 468,
28891 `template' is allowed in cases where it is not strictly necessary. */
28892 if (!processing_template_decl
28893 && pedantic
&& cxx_dialect
== cxx98
)
28895 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28896 pedwarn (token
->location
, OPT_Wpedantic
,
28897 "in C++98 %<template%> (as a disambiguator) is only "
28898 "allowed within templates");
28899 /* If this part of the token stream is rescanned, the same
28900 error message would be generated. So, we purge the token
28901 from the stream. */
28902 cp_lexer_purge_token (parser
->lexer
);
28907 /* Consume the `template' keyword. */
28908 cp_lexer_consume_token (parser
->lexer
);
28915 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28916 set PARSER->SCOPE, and perform other related actions. */
28919 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28921 struct tree_check
*check_value
;
28923 /* Get the stored value. */
28924 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28925 /* Set the scope from the stored value. */
28926 parser
->scope
= saved_checks_value (check_value
);
28927 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28928 parser
->object_scope
= NULL_TREE
;
28931 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28932 encounter the end of a block before what we were looking for. */
28935 cp_parser_cache_group (cp_parser
*parser
,
28936 enum cpp_ttype end
,
28941 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28943 /* Abort a parenthesized expression if we encounter a semicolon. */
28944 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28945 && token
->type
== CPP_SEMICOLON
)
28947 /* If we've reached the end of the file, stop. */
28948 if (token
->type
== CPP_EOF
28949 || (end
!= CPP_PRAGMA_EOL
28950 && token
->type
== CPP_PRAGMA_EOL
))
28952 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28953 /* We've hit the end of an enclosing block, so there's been some
28954 kind of syntax error. */
28957 /* Consume the token. */
28958 cp_lexer_consume_token (parser
->lexer
);
28959 /* See if it starts a new group. */
28960 if (token
->type
== CPP_OPEN_BRACE
)
28962 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28963 /* In theory this should probably check end == '}', but
28964 cp_parser_save_member_function_body needs it to exit
28965 after either '}' or ')' when called with ')'. */
28969 else if (token
->type
== CPP_OPEN_PAREN
)
28971 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28972 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28975 else if (token
->type
== CPP_PRAGMA
)
28976 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28977 else if (token
->type
== end
)
28982 /* Like above, for caching a default argument or NSDMI. Both of these are
28983 terminated by a non-nested comma, but it can be unclear whether or not a
28984 comma is nested in a template argument list unless we do more parsing.
28985 In order to handle this ambiguity, when we encounter a ',' after a '<'
28986 we try to parse what follows as a parameter-declaration-list (in the
28987 case of a default argument) or a member-declarator (in the case of an
28988 NSDMI). If that succeeds, then we stop caching. */
28991 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28993 unsigned depth
= 0;
28994 int maybe_template_id
= 0;
28995 cp_token
*first_token
;
28997 tree default_argument
;
28999 /* Add tokens until we have processed the entire default
29000 argument. We add the range [first_token, token). */
29001 first_token
= cp_lexer_peek_token (parser
->lexer
);
29002 if (first_token
->type
== CPP_OPEN_BRACE
)
29004 /* For list-initialization, this is straightforward. */
29005 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29006 token
= cp_lexer_peek_token (parser
->lexer
);
29012 /* Peek at the next token. */
29013 token
= cp_lexer_peek_token (parser
->lexer
);
29014 /* What we do depends on what token we have. */
29015 switch (token
->type
)
29017 /* In valid code, a default argument must be
29018 immediately followed by a `,' `)', or `...'. */
29020 if (depth
== 0 && maybe_template_id
)
29022 /* If we've seen a '<', we might be in a
29023 template-argument-list. Until Core issue 325 is
29024 resolved, we don't know how this situation ought
29025 to be handled, so try to DTRT. We check whether
29026 what comes after the comma is a valid parameter
29027 declaration list. If it is, then the comma ends
29028 the default argument; otherwise the default
29029 argument continues. */
29030 bool error
= false;
29033 /* Set ITALP so cp_parser_parameter_declaration_list
29034 doesn't decide to commit to this parse. */
29035 bool saved_italp
= parser
->in_template_argument_list_p
;
29036 parser
->in_template_argument_list_p
= true;
29038 cp_parser_parse_tentatively (parser
);
29042 /* Parse declarators until we reach a non-comma or
29043 somthing that cannot be an initializer.
29044 Just checking whether we're looking at a single
29045 declarator is insufficient. Consider:
29046 int var = tuple<T,U>::x;
29047 The template parameter 'U' looks exactly like a
29051 int ctor_dtor_or_conv_p
;
29052 cp_lexer_consume_token (parser
->lexer
);
29053 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29054 &ctor_dtor_or_conv_p
,
29055 /*parenthesized_p=*/NULL
,
29057 /*friend_p=*/false);
29058 peek
= cp_lexer_peek_token (parser
->lexer
);
29059 if (cp_parser_error_occurred (parser
))
29062 while (peek
->type
== CPP_COMMA
);
29063 /* If we met an '=' or ';' then the original comma
29064 was the end of the NSDMI. Otherwise assume
29065 we're still in the NSDMI. */
29066 error
= (peek
->type
!= CPP_EQ
29067 && peek
->type
!= CPP_SEMICOLON
);
29071 cp_lexer_consume_token (parser
->lexer
);
29072 begin_scope (sk_function_parms
, NULL_TREE
);
29073 if (cp_parser_parameter_declaration_list (parser
)
29074 == error_mark_node
)
29076 pop_bindings_and_leave_scope ();
29078 if (!cp_parser_error_occurred (parser
) && !error
)
29080 cp_parser_abort_tentative_parse (parser
);
29082 parser
->in_template_argument_list_p
= saved_italp
;
29086 case CPP_CLOSE_PAREN
:
29088 /* If we run into a non-nested `;', `}', or `]',
29089 then the code is invalid -- but the default
29090 argument is certainly over. */
29091 case CPP_SEMICOLON
:
29092 case CPP_CLOSE_BRACE
:
29093 case CPP_CLOSE_SQUARE
:
29095 /* Handle correctly int n = sizeof ... ( p ); */
29096 && token
->type
!= CPP_ELLIPSIS
)
29098 /* Update DEPTH, if necessary. */
29099 else if (token
->type
== CPP_CLOSE_PAREN
29100 || token
->type
== CPP_CLOSE_BRACE
29101 || token
->type
== CPP_CLOSE_SQUARE
)
29105 case CPP_OPEN_PAREN
:
29106 case CPP_OPEN_SQUARE
:
29107 case CPP_OPEN_BRACE
:
29113 /* This might be the comparison operator, or it might
29114 start a template argument list. */
29115 ++maybe_template_id
;
29119 if (cxx_dialect
== cxx98
)
29121 /* Fall through for C++0x, which treats the `>>'
29122 operator like two `>' tokens in certain
29124 gcc_fallthrough ();
29129 /* This might be an operator, or it might close a
29130 template argument list. But if a previous '<'
29131 started a template argument list, this will have
29132 closed it, so we can't be in one anymore. */
29133 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29134 if (maybe_template_id
< 0)
29135 maybe_template_id
= 0;
29139 /* If we run out of tokens, issue an error message. */
29141 case CPP_PRAGMA_EOL
:
29142 error_at (token
->location
, "file ends in default argument");
29143 return error_mark_node
;
29147 /* In these cases, we should look for template-ids.
29148 For example, if the default argument is
29149 `X<int, double>()', we need to do name lookup to
29150 figure out whether or not `X' is a template; if
29151 so, the `,' does not end the default argument.
29153 That is not yet done. */
29160 /* If we've reached the end, stop. */
29164 /* Add the token to the token block. */
29165 token
= cp_lexer_consume_token (parser
->lexer
);
29168 /* Create a DEFAULT_ARG to represent the unparsed default
29170 default_argument
= make_node (DEFAULT_ARG
);
29171 DEFARG_TOKENS (default_argument
)
29172 = cp_token_cache_new (first_token
, token
);
29173 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29175 return default_argument
;
29178 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29181 defarg_location (tree default_argument
)
29183 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29184 location_t start
= tokens
->first
->location
;
29185 location_t end
= tokens
->last
->location
;
29186 return make_location (start
, start
, end
);
29189 /* Begin parsing tentatively. We always save tokens while parsing
29190 tentatively so that if the tentative parsing fails we can restore the
29194 cp_parser_parse_tentatively (cp_parser
* parser
)
29196 /* Enter a new parsing context. */
29197 parser
->context
= cp_parser_context_new (parser
->context
);
29198 /* Begin saving tokens. */
29199 cp_lexer_save_tokens (parser
->lexer
);
29200 /* In order to avoid repetitive access control error messages,
29201 access checks are queued up until we are no longer parsing
29203 push_deferring_access_checks (dk_deferred
);
29206 /* Commit to the currently active tentative parse. */
29209 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29211 cp_parser_context
*context
;
29214 /* Mark all of the levels as committed. */
29215 lexer
= parser
->lexer
;
29216 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29218 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29220 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29221 while (!cp_lexer_saving_tokens (lexer
))
29222 lexer
= lexer
->next
;
29223 cp_lexer_commit_tokens (lexer
);
29227 /* Commit to the topmost currently active tentative parse.
29229 Note that this function shouldn't be called when there are
29230 irreversible side-effects while in a tentative state. For
29231 example, we shouldn't create a permanent entry in the symbol
29232 table, or issue an error message that might not apply if the
29233 tentative parse is aborted. */
29236 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29238 cp_parser_context
*context
= parser
->context
;
29239 cp_lexer
*lexer
= parser
->lexer
;
29243 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29245 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29247 while (!cp_lexer_saving_tokens (lexer
))
29248 lexer
= lexer
->next
;
29249 cp_lexer_commit_tokens (lexer
);
29253 /* Abort the currently active tentative parse. All consumed tokens
29254 will be rolled back, and no diagnostics will be issued. */
29257 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29259 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29260 || errorcount
> 0);
29261 cp_parser_simulate_error (parser
);
29262 /* Now, pretend that we want to see if the construct was
29263 successfully parsed. */
29264 cp_parser_parse_definitely (parser
);
29267 /* Stop parsing tentatively. If a parse error has occurred, restore the
29268 token stream. Otherwise, commit to the tokens we have consumed.
29269 Returns true if no error occurred; false otherwise. */
29272 cp_parser_parse_definitely (cp_parser
* parser
)
29274 bool error_occurred
;
29275 cp_parser_context
*context
;
29277 /* Remember whether or not an error occurred, since we are about to
29278 destroy that information. */
29279 error_occurred
= cp_parser_error_occurred (parser
);
29280 /* Remove the topmost context from the stack. */
29281 context
= parser
->context
;
29282 parser
->context
= context
->next
;
29283 /* If no parse errors occurred, commit to the tentative parse. */
29284 if (!error_occurred
)
29286 /* Commit to the tokens read tentatively, unless that was
29288 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29289 cp_lexer_commit_tokens (parser
->lexer
);
29291 pop_to_parent_deferring_access_checks ();
29293 /* Otherwise, if errors occurred, roll back our state so that things
29294 are just as they were before we began the tentative parse. */
29297 cp_lexer_rollback_tokens (parser
->lexer
);
29298 pop_deferring_access_checks ();
29300 /* Add the context to the front of the free list. */
29301 context
->next
= cp_parser_context_free_list
;
29302 cp_parser_context_free_list
= context
;
29304 return !error_occurred
;
29307 /* Returns true if we are parsing tentatively and are not committed to
29308 this tentative parse. */
29311 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29313 return (cp_parser_parsing_tentatively (parser
)
29314 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29317 /* Returns nonzero iff an error has occurred during the most recent
29318 tentative parse. */
29321 cp_parser_error_occurred (cp_parser
* parser
)
29323 return (cp_parser_parsing_tentatively (parser
)
29324 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29327 /* Returns nonzero if GNU extensions are allowed. */
29330 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29332 return parser
->allow_gnu_extensions_p
;
29335 /* Objective-C++ Productions */
29338 /* Parse an Objective-C expression, which feeds into a primary-expression
29342 objc-message-expression
29343 objc-string-literal
29344 objc-encode-expression
29345 objc-protocol-expression
29346 objc-selector-expression
29348 Returns a tree representation of the expression. */
29351 cp_parser_objc_expression (cp_parser
* parser
)
29353 /* Try to figure out what kind of declaration is present. */
29354 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29358 case CPP_OPEN_SQUARE
:
29359 return cp_parser_objc_message_expression (parser
);
29361 case CPP_OBJC_STRING
:
29362 kwd
= cp_lexer_consume_token (parser
->lexer
);
29363 return objc_build_string_object (kwd
->u
.value
);
29366 switch (kwd
->keyword
)
29368 case RID_AT_ENCODE
:
29369 return cp_parser_objc_encode_expression (parser
);
29371 case RID_AT_PROTOCOL
:
29372 return cp_parser_objc_protocol_expression (parser
);
29374 case RID_AT_SELECTOR
:
29375 return cp_parser_objc_selector_expression (parser
);
29382 error_at (kwd
->location
,
29383 "misplaced %<@%D%> Objective-C++ construct",
29385 cp_parser_skip_to_end_of_block_or_statement (parser
);
29388 return error_mark_node
;
29391 /* Parse an Objective-C message expression.
29393 objc-message-expression:
29394 [ objc-message-receiver objc-message-args ]
29396 Returns a representation of an Objective-C message. */
29399 cp_parser_objc_message_expression (cp_parser
* parser
)
29401 tree receiver
, messageargs
;
29403 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29404 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29405 receiver
= cp_parser_objc_message_receiver (parser
);
29406 messageargs
= cp_parser_objc_message_args (parser
);
29407 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29408 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29410 tree result
= objc_build_message_expr (receiver
, messageargs
);
29412 /* Construct a location e.g.
29415 ranging from the '[' to the ']', with the caret at the start. */
29416 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29417 protected_set_expr_location (result
, combined_loc
);
29422 /* Parse an objc-message-receiver.
29424 objc-message-receiver:
29426 simple-type-specifier
29428 Returns a representation of the type or expression. */
29431 cp_parser_objc_message_receiver (cp_parser
* parser
)
29435 /* An Objective-C message receiver may be either (1) a type
29436 or (2) an expression. */
29437 cp_parser_parse_tentatively (parser
);
29438 rcv
= cp_parser_expression (parser
);
29440 /* If that worked out, fine. */
29441 if (cp_parser_parse_definitely (parser
))
29444 cp_parser_parse_tentatively (parser
);
29445 rcv
= cp_parser_simple_type_specifier (parser
,
29446 /*decl_specs=*/NULL
,
29447 CP_PARSER_FLAGS_NONE
);
29449 if (cp_parser_parse_definitely (parser
))
29450 return objc_get_class_reference (rcv
);
29452 cp_parser_error (parser
, "objective-c++ message receiver expected");
29453 return error_mark_node
;
29456 /* Parse the arguments and selectors comprising an Objective-C message.
29461 objc-selector-args , objc-comma-args
29463 objc-selector-args:
29464 objc-selector [opt] : assignment-expression
29465 objc-selector-args objc-selector [opt] : assignment-expression
29468 assignment-expression
29469 objc-comma-args , assignment-expression
29471 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29472 selector arguments and TREE_VALUE containing a list of comma
29476 cp_parser_objc_message_args (cp_parser
* parser
)
29478 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29479 bool maybe_unary_selector_p
= true;
29480 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29482 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29484 tree selector
= NULL_TREE
, arg
;
29486 if (token
->type
!= CPP_COLON
)
29487 selector
= cp_parser_objc_selector (parser
);
29489 /* Detect if we have a unary selector. */
29490 if (maybe_unary_selector_p
29491 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29492 return build_tree_list (selector
, NULL_TREE
);
29494 maybe_unary_selector_p
= false;
29495 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29496 arg
= cp_parser_assignment_expression (parser
);
29499 = chainon (sel_args
,
29500 build_tree_list (selector
, arg
));
29502 token
= cp_lexer_peek_token (parser
->lexer
);
29505 /* Handle non-selector arguments, if any. */
29506 while (token
->type
== CPP_COMMA
)
29510 cp_lexer_consume_token (parser
->lexer
);
29511 arg
= cp_parser_assignment_expression (parser
);
29514 = chainon (addl_args
,
29515 build_tree_list (NULL_TREE
, arg
));
29517 token
= cp_lexer_peek_token (parser
->lexer
);
29520 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29522 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29523 return build_tree_list (error_mark_node
, error_mark_node
);
29526 return build_tree_list (sel_args
, addl_args
);
29529 /* Parse an Objective-C encode expression.
29531 objc-encode-expression:
29532 @encode objc-typename
29534 Returns an encoded representation of the type argument. */
29537 cp_parser_objc_encode_expression (cp_parser
* parser
)
29541 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29543 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29544 matching_parens parens
;
29545 parens
.require_open (parser
);
29546 token
= cp_lexer_peek_token (parser
->lexer
);
29547 type
= complete_type (cp_parser_type_id (parser
));
29548 parens
.require_close (parser
);
29552 error_at (token
->location
,
29553 "%<@encode%> must specify a type as an argument");
29554 return error_mark_node
;
29557 /* This happens if we find @encode(T) (where T is a template
29558 typename or something dependent on a template typename) when
29559 parsing a template. In that case, we can't compile it
29560 immediately, but we rather create an AT_ENCODE_EXPR which will
29561 need to be instantiated when the template is used.
29563 if (dependent_type_p (type
))
29565 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29566 TREE_READONLY (value
) = 1;
29571 /* Build a location of the form:
29574 with caret==start at the @ token, finishing at the close paren. */
29575 location_t combined_loc
29576 = make_location (start_loc
, start_loc
,
29577 cp_lexer_previous_token (parser
->lexer
)->location
);
29579 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29582 /* Parse an Objective-C @defs expression. */
29585 cp_parser_objc_defs_expression (cp_parser
*parser
)
29589 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29590 matching_parens parens
;
29591 parens
.require_open (parser
);
29592 name
= cp_parser_identifier (parser
);
29593 parens
.require_close (parser
);
29595 return objc_get_class_ivars (name
);
29598 /* Parse an Objective-C protocol expression.
29600 objc-protocol-expression:
29601 @protocol ( identifier )
29603 Returns a representation of the protocol expression. */
29606 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29609 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29611 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29612 matching_parens parens
;
29613 parens
.require_open (parser
);
29614 proto
= cp_parser_identifier (parser
);
29615 parens
.require_close (parser
);
29617 /* Build a location of the form:
29620 with caret==start at the @ token, finishing at the close paren. */
29621 location_t combined_loc
29622 = make_location (start_loc
, start_loc
,
29623 cp_lexer_previous_token (parser
->lexer
)->location
);
29624 tree result
= objc_build_protocol_expr (proto
);
29625 protected_set_expr_location (result
, combined_loc
);
29629 /* Parse an Objective-C selector expression.
29631 objc-selector-expression:
29632 @selector ( objc-method-signature )
29634 objc-method-signature:
29640 objc-selector-seq objc-selector :
29642 Returns a representation of the method selector. */
29645 cp_parser_objc_selector_expression (cp_parser
* parser
)
29647 tree sel_seq
= NULL_TREE
;
29648 bool maybe_unary_selector_p
= true;
29650 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29652 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29653 matching_parens parens
;
29654 parens
.require_open (parser
);
29655 token
= cp_lexer_peek_token (parser
->lexer
);
29657 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29658 || token
->type
== CPP_SCOPE
)
29660 tree selector
= NULL_TREE
;
29662 if (token
->type
!= CPP_COLON
29663 || token
->type
== CPP_SCOPE
)
29664 selector
= cp_parser_objc_selector (parser
);
29666 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29667 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29669 /* Detect if we have a unary selector. */
29670 if (maybe_unary_selector_p
)
29672 sel_seq
= selector
;
29673 goto finish_selector
;
29677 cp_parser_error (parser
, "expected %<:%>");
29680 maybe_unary_selector_p
= false;
29681 token
= cp_lexer_consume_token (parser
->lexer
);
29683 if (token
->type
== CPP_SCOPE
)
29686 = chainon (sel_seq
,
29687 build_tree_list (selector
, NULL_TREE
));
29689 = chainon (sel_seq
,
29690 build_tree_list (NULL_TREE
, NULL_TREE
));
29694 = chainon (sel_seq
,
29695 build_tree_list (selector
, NULL_TREE
));
29697 token
= cp_lexer_peek_token (parser
->lexer
);
29701 parens
.require_close (parser
);
29704 /* Build a location of the form:
29707 with caret==start at the @ token, finishing at the close paren. */
29708 location_t combined_loc
29709 = make_location (loc
, loc
,
29710 cp_lexer_previous_token (parser
->lexer
)->location
);
29711 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29712 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29713 protected_set_expr_location (result
, combined_loc
);
29717 /* Parse a list of identifiers.
29719 objc-identifier-list:
29721 objc-identifier-list , identifier
29723 Returns a TREE_LIST of identifier nodes. */
29726 cp_parser_objc_identifier_list (cp_parser
* parser
)
29732 identifier
= cp_parser_identifier (parser
);
29733 if (identifier
== error_mark_node
)
29734 return error_mark_node
;
29736 list
= build_tree_list (NULL_TREE
, identifier
);
29737 sep
= cp_lexer_peek_token (parser
->lexer
);
29739 while (sep
->type
== CPP_COMMA
)
29741 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29742 identifier
= cp_parser_identifier (parser
);
29743 if (identifier
== error_mark_node
)
29746 list
= chainon (list
, build_tree_list (NULL_TREE
,
29748 sep
= cp_lexer_peek_token (parser
->lexer
);
29754 /* Parse an Objective-C alias declaration.
29756 objc-alias-declaration:
29757 @compatibility_alias identifier identifier ;
29759 This function registers the alias mapping with the Objective-C front end.
29760 It returns nothing. */
29763 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29767 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29768 alias
= cp_parser_identifier (parser
);
29769 orig
= cp_parser_identifier (parser
);
29770 objc_declare_alias (alias
, orig
);
29771 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29774 /* Parse an Objective-C class forward-declaration.
29776 objc-class-declaration:
29777 @class objc-identifier-list ;
29779 The function registers the forward declarations with the Objective-C
29780 front end. It returns nothing. */
29783 cp_parser_objc_class_declaration (cp_parser
* parser
)
29785 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29790 id
= cp_parser_identifier (parser
);
29791 if (id
== error_mark_node
)
29794 objc_declare_class (id
);
29796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29797 cp_lexer_consume_token (parser
->lexer
);
29801 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29804 /* Parse a list of Objective-C protocol references.
29806 objc-protocol-refs-opt:
29807 objc-protocol-refs [opt]
29809 objc-protocol-refs:
29810 < objc-identifier-list >
29812 Returns a TREE_LIST of identifiers, if any. */
29815 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29817 tree protorefs
= NULL_TREE
;
29819 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29821 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29822 protorefs
= cp_parser_objc_identifier_list (parser
);
29823 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29829 /* Parse a Objective-C visibility specification. */
29832 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29834 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29836 switch (vis
->keyword
)
29838 case RID_AT_PRIVATE
:
29839 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29841 case RID_AT_PROTECTED
:
29842 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29844 case RID_AT_PUBLIC
:
29845 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29847 case RID_AT_PACKAGE
:
29848 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29854 /* Eat '@private'/'@protected'/'@public'. */
29855 cp_lexer_consume_token (parser
->lexer
);
29858 /* Parse an Objective-C method type. Return 'true' if it is a class
29859 (+) method, and 'false' if it is an instance (-) method. */
29862 cp_parser_objc_method_type (cp_parser
* parser
)
29864 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29870 /* Parse an Objective-C protocol qualifier. */
29873 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29875 tree quals
= NULL_TREE
, node
;
29876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29878 node
= token
->u
.value
;
29880 while (node
&& identifier_p (node
)
29881 && (node
== ridpointers
[(int) RID_IN
]
29882 || node
== ridpointers
[(int) RID_OUT
]
29883 || node
== ridpointers
[(int) RID_INOUT
]
29884 || node
== ridpointers
[(int) RID_BYCOPY
]
29885 || node
== ridpointers
[(int) RID_BYREF
]
29886 || node
== ridpointers
[(int) RID_ONEWAY
]))
29888 quals
= tree_cons (NULL_TREE
, node
, quals
);
29889 cp_lexer_consume_token (parser
->lexer
);
29890 token
= cp_lexer_peek_token (parser
->lexer
);
29891 node
= token
->u
.value
;
29897 /* Parse an Objective-C typename. */
29900 cp_parser_objc_typename (cp_parser
* parser
)
29902 tree type_name
= NULL_TREE
;
29904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29906 tree proto_quals
, cp_type
= NULL_TREE
;
29908 matching_parens parens
;
29909 parens
.consume_open (parser
); /* Eat '('. */
29910 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29912 /* An ObjC type name may consist of just protocol qualifiers, in which
29913 case the type shall default to 'id'. */
29914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29916 cp_type
= cp_parser_type_id (parser
);
29918 /* If the type could not be parsed, an error has already
29919 been produced. For error recovery, behave as if it had
29920 not been specified, which will use the default type
29922 if (cp_type
== error_mark_node
)
29924 cp_type
= NULL_TREE
;
29925 /* We need to skip to the closing parenthesis as
29926 cp_parser_type_id() does not seem to do it for
29928 cp_parser_skip_to_closing_parenthesis (parser
,
29929 /*recovering=*/true,
29930 /*or_comma=*/false,
29931 /*consume_paren=*/false);
29935 parens
.require_close (parser
);
29936 type_name
= build_tree_list (proto_quals
, cp_type
);
29942 /* Check to see if TYPE refers to an Objective-C selector name. */
29945 cp_parser_objc_selector_p (enum cpp_ttype type
)
29947 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29948 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29949 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29950 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29951 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29954 /* Parse an Objective-C selector. */
29957 cp_parser_objc_selector (cp_parser
* parser
)
29959 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29961 if (!cp_parser_objc_selector_p (token
->type
))
29963 error_at (token
->location
, "invalid Objective-C++ selector name");
29964 return error_mark_node
;
29967 /* C++ operator names are allowed to appear in ObjC selectors. */
29968 switch (token
->type
)
29970 case CPP_AND_AND
: return get_identifier ("and");
29971 case CPP_AND_EQ
: return get_identifier ("and_eq");
29972 case CPP_AND
: return get_identifier ("bitand");
29973 case CPP_OR
: return get_identifier ("bitor");
29974 case CPP_COMPL
: return get_identifier ("compl");
29975 case CPP_NOT
: return get_identifier ("not");
29976 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29977 case CPP_OR_OR
: return get_identifier ("or");
29978 case CPP_OR_EQ
: return get_identifier ("or_eq");
29979 case CPP_XOR
: return get_identifier ("xor");
29980 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29981 default: return token
->u
.value
;
29985 /* Parse an Objective-C params list. */
29988 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29990 tree params
= NULL_TREE
;
29991 bool maybe_unary_selector_p
= true;
29992 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29994 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29996 tree selector
= NULL_TREE
, type_name
, identifier
;
29997 tree parm_attr
= NULL_TREE
;
29999 if (token
->keyword
== RID_ATTRIBUTE
)
30002 if (token
->type
!= CPP_COLON
)
30003 selector
= cp_parser_objc_selector (parser
);
30005 /* Detect if we have a unary selector. */
30006 if (maybe_unary_selector_p
30007 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30009 params
= selector
; /* Might be followed by attributes. */
30013 maybe_unary_selector_p
= false;
30014 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30016 /* Something went quite wrong. There should be a colon
30017 here, but there is not. Stop parsing parameters. */
30020 type_name
= cp_parser_objc_typename (parser
);
30021 /* New ObjC allows attributes on parameters too. */
30022 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30023 parm_attr
= cp_parser_attributes_opt (parser
);
30024 identifier
= cp_parser_identifier (parser
);
30028 objc_build_keyword_decl (selector
,
30033 token
= cp_lexer_peek_token (parser
->lexer
);
30036 if (params
== NULL_TREE
)
30038 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30039 return error_mark_node
;
30042 /* We allow tail attributes for the method. */
30043 if (token
->keyword
== RID_ATTRIBUTE
)
30045 *attributes
= cp_parser_attributes_opt (parser
);
30046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30047 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30049 cp_parser_error (parser
,
30050 "method attributes must be specified at the end");
30051 return error_mark_node
;
30054 if (params
== NULL_TREE
)
30056 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30057 return error_mark_node
;
30062 /* Parse the non-keyword Objective-C params. */
30065 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30068 tree params
= make_node (TREE_LIST
);
30069 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30070 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30072 while (token
->type
== CPP_COMMA
)
30074 cp_parameter_declarator
*parmdecl
;
30077 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30078 token
= cp_lexer_peek_token (parser
->lexer
);
30080 if (token
->type
== CPP_ELLIPSIS
)
30082 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30084 token
= cp_lexer_peek_token (parser
->lexer
);
30088 /* TODO: parse attributes for tail parameters. */
30089 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30090 parm
= grokdeclarator (parmdecl
->declarator
,
30091 &parmdecl
->decl_specifiers
,
30092 PARM
, /*initialized=*/0,
30093 /*attrlist=*/NULL
);
30095 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30096 token
= cp_lexer_peek_token (parser
->lexer
);
30099 /* We allow tail attributes for the method. */
30100 if (token
->keyword
== RID_ATTRIBUTE
)
30102 if (*attributes
== NULL_TREE
)
30104 *attributes
= cp_parser_attributes_opt (parser
);
30105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30106 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30110 /* We have an error, but parse the attributes, so that we can
30112 *attributes
= cp_parser_attributes_opt (parser
);
30114 cp_parser_error (parser
,
30115 "method attributes must be specified at the end");
30116 return error_mark_node
;
30122 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30125 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30127 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30129 /* If the next token is `extern' and the following token is a string
30130 literal, then we have a linkage specification. */
30131 if (token
->keyword
== RID_EXTERN
30132 && cp_parser_is_pure_string_literal
30133 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30134 cp_parser_linkage_specification (parser
);
30135 /* Handle #pragma, if any. */
30136 else if (token
->type
== CPP_PRAGMA
)
30137 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30138 /* Allow stray semicolons. */
30139 else if (token
->type
== CPP_SEMICOLON
)
30140 cp_lexer_consume_token (parser
->lexer
);
30141 /* Mark methods as optional or required, when building protocols. */
30142 else if (token
->keyword
== RID_AT_OPTIONAL
)
30144 cp_lexer_consume_token (parser
->lexer
);
30145 objc_set_method_opt (true);
30147 else if (token
->keyword
== RID_AT_REQUIRED
)
30149 cp_lexer_consume_token (parser
->lexer
);
30150 objc_set_method_opt (false);
30152 else if (token
->keyword
== RID_NAMESPACE
)
30153 cp_parser_namespace_definition (parser
);
30154 /* Other stray characters must generate errors. */
30155 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30157 cp_lexer_consume_token (parser
->lexer
);
30158 error ("stray %qs between Objective-C++ methods",
30159 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30161 /* Finally, try to parse a block-declaration, or a function-definition. */
30163 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30166 /* Parse a method signature. */
30169 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30171 tree rettype
, kwdparms
, optparms
;
30172 bool ellipsis
= false;
30173 bool is_class_method
;
30175 is_class_method
= cp_parser_objc_method_type (parser
);
30176 rettype
= cp_parser_objc_typename (parser
);
30177 *attributes
= NULL_TREE
;
30178 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30179 if (kwdparms
== error_mark_node
)
30180 return error_mark_node
;
30181 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30182 if (optparms
== error_mark_node
)
30183 return error_mark_node
;
30185 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30189 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30192 cp_lexer_save_tokens (parser
->lexer
);
30193 tattr
= cp_parser_attributes_opt (parser
);
30194 gcc_assert (tattr
) ;
30196 /* If the attributes are followed by a method introducer, this is not allowed.
30197 Dump the attributes and flag the situation. */
30198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30199 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30202 /* Otherwise, the attributes introduce some interstitial code, possibly so
30203 rewind to allow that check. */
30204 cp_lexer_rollback_tokens (parser
->lexer
);
30208 /* Parse an Objective-C method prototype list. */
30211 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30213 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30215 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30217 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30219 tree attributes
, sig
;
30220 bool is_class_method
;
30221 if (token
->type
== CPP_PLUS
)
30222 is_class_method
= true;
30224 is_class_method
= false;
30225 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30226 if (sig
== error_mark_node
)
30228 cp_parser_skip_to_end_of_block_or_statement (parser
);
30229 token
= cp_lexer_peek_token (parser
->lexer
);
30232 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30233 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30235 else if (token
->keyword
== RID_AT_PROPERTY
)
30236 cp_parser_objc_at_property_declaration (parser
);
30237 else if (token
->keyword
== RID_ATTRIBUTE
30238 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30239 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30241 "prefix attributes are ignored for methods");
30243 /* Allow for interspersed non-ObjC++ code. */
30244 cp_parser_objc_interstitial_code (parser
);
30246 token
= cp_lexer_peek_token (parser
->lexer
);
30249 if (token
->type
!= CPP_EOF
)
30250 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30252 cp_parser_error (parser
, "expected %<@end%>");
30254 objc_finish_interface ();
30257 /* Parse an Objective-C method definition list. */
30260 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30262 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30264 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30268 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30271 tree sig
, attribute
;
30272 bool is_class_method
;
30273 if (token
->type
== CPP_PLUS
)
30274 is_class_method
= true;
30276 is_class_method
= false;
30277 push_deferring_access_checks (dk_deferred
);
30278 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30279 if (sig
== error_mark_node
)
30281 cp_parser_skip_to_end_of_block_or_statement (parser
);
30282 token
= cp_lexer_peek_token (parser
->lexer
);
30285 objc_start_method_definition (is_class_method
, sig
, attribute
,
30288 /* For historical reasons, we accept an optional semicolon. */
30289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30290 cp_lexer_consume_token (parser
->lexer
);
30292 ptk
= cp_lexer_peek_token (parser
->lexer
);
30293 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30294 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30296 perform_deferred_access_checks (tf_warning_or_error
);
30297 stop_deferring_access_checks ();
30298 meth
= cp_parser_function_definition_after_declarator (parser
,
30300 pop_deferring_access_checks ();
30301 objc_finish_method_definition (meth
);
30304 /* The following case will be removed once @synthesize is
30305 completely implemented. */
30306 else if (token
->keyword
== RID_AT_PROPERTY
)
30307 cp_parser_objc_at_property_declaration (parser
);
30308 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30309 cp_parser_objc_at_synthesize_declaration (parser
);
30310 else if (token
->keyword
== RID_AT_DYNAMIC
)
30311 cp_parser_objc_at_dynamic_declaration (parser
);
30312 else if (token
->keyword
== RID_ATTRIBUTE
30313 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30314 warning_at (token
->location
, OPT_Wattributes
,
30315 "prefix attributes are ignored for methods");
30317 /* Allow for interspersed non-ObjC++ code. */
30318 cp_parser_objc_interstitial_code (parser
);
30320 token
= cp_lexer_peek_token (parser
->lexer
);
30323 if (token
->type
!= CPP_EOF
)
30324 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30326 cp_parser_error (parser
, "expected %<@end%>");
30328 objc_finish_implementation ();
30331 /* Parse Objective-C ivars. */
30334 cp_parser_objc_class_ivars (cp_parser
* parser
)
30336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30338 if (token
->type
!= CPP_OPEN_BRACE
)
30339 return; /* No ivars specified. */
30341 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30342 token
= cp_lexer_peek_token (parser
->lexer
);
30344 while (token
->type
!= CPP_CLOSE_BRACE
30345 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30347 cp_decl_specifier_seq declspecs
;
30348 int decl_class_or_enum_p
;
30349 tree prefix_attributes
;
30351 cp_parser_objc_visibility_spec (parser
);
30353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30356 cp_parser_decl_specifier_seq (parser
,
30357 CP_PARSER_FLAGS_OPTIONAL
,
30359 &decl_class_or_enum_p
);
30361 /* auto, register, static, extern, mutable. */
30362 if (declspecs
.storage_class
!= sc_none
)
30364 cp_parser_error (parser
, "invalid type for instance variable");
30365 declspecs
.storage_class
= sc_none
;
30368 /* thread_local. */
30369 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30371 cp_parser_error (parser
, "invalid type for instance variable");
30372 declspecs
.locations
[ds_thread
] = 0;
30376 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30378 cp_parser_error (parser
, "invalid type for instance variable");
30379 declspecs
.locations
[ds_typedef
] = 0;
30382 prefix_attributes
= declspecs
.attributes
;
30383 declspecs
.attributes
= NULL_TREE
;
30385 /* Keep going until we hit the `;' at the end of the
30387 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30389 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30390 cp_declarator
*declarator
= NULL
;
30391 int ctor_dtor_or_conv_p
;
30393 /* Check for a (possibly unnamed) bitfield declaration. */
30394 token
= cp_lexer_peek_token (parser
->lexer
);
30395 if (token
->type
== CPP_COLON
)
30398 if (token
->type
== CPP_NAME
30399 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30402 /* Get the name of the bitfield. */
30403 declarator
= make_id_declarator (NULL_TREE
,
30404 cp_parser_identifier (parser
),
30408 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30409 /* Get the width of the bitfield. */
30411 = cp_parser_constant_expression (parser
);
30415 /* Parse the declarator. */
30417 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30418 &ctor_dtor_or_conv_p
,
30419 /*parenthesized_p=*/NULL
,
30420 /*member_p=*/false,
30421 /*friend_p=*/false);
30424 /* Look for attributes that apply to the ivar. */
30425 attributes
= cp_parser_attributes_opt (parser
);
30426 /* Remember which attributes are prefix attributes and
30428 first_attribute
= attributes
;
30429 /* Combine the attributes. */
30430 attributes
= attr_chainon (prefix_attributes
, attributes
);
30433 /* Create the bitfield declaration. */
30434 decl
= grokbitfield (declarator
, &declspecs
,
30435 width
, NULL_TREE
, attributes
);
30437 decl
= grokfield (declarator
, &declspecs
,
30438 NULL_TREE
, /*init_const_expr_p=*/false,
30439 NULL_TREE
, attributes
);
30441 /* Add the instance variable. */
30442 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30443 objc_add_instance_variable (decl
);
30445 /* Reset PREFIX_ATTRIBUTES. */
30446 if (attributes
!= error_mark_node
)
30448 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30449 attributes
= TREE_CHAIN (attributes
);
30451 TREE_CHAIN (attributes
) = NULL_TREE
;
30454 token
= cp_lexer_peek_token (parser
->lexer
);
30456 if (token
->type
== CPP_COMMA
)
30458 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30464 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30465 token
= cp_lexer_peek_token (parser
->lexer
);
30468 if (token
->keyword
== RID_AT_END
)
30469 cp_parser_error (parser
, "expected %<}%>");
30471 /* Do not consume the RID_AT_END, so it will be read again as terminating
30472 the @interface of @implementation. */
30473 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30474 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30476 /* For historical reasons, we accept an optional semicolon. */
30477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30478 cp_lexer_consume_token (parser
->lexer
);
30481 /* Parse an Objective-C protocol declaration. */
30484 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30486 tree proto
, protorefs
;
30489 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30490 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30492 tok
= cp_lexer_peek_token (parser
->lexer
);
30493 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30494 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30498 /* See if we have a forward declaration or a definition. */
30499 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30501 /* Try a forward declaration first. */
30502 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30508 id
= cp_parser_identifier (parser
);
30509 if (id
== error_mark_node
)
30512 objc_declare_protocol (id
, attributes
);
30514 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30515 cp_lexer_consume_token (parser
->lexer
);
30519 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30522 /* Ok, we got a full-fledged definition (or at least should). */
30525 proto
= cp_parser_identifier (parser
);
30526 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30527 objc_start_protocol (proto
, protorefs
, attributes
);
30528 cp_parser_objc_method_prototype_list (parser
);
30532 /* Parse an Objective-C superclass or category. */
30535 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30538 tree
*categ
, bool *is_class_extension
)
30540 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30542 *super
= *categ
= NULL_TREE
;
30543 *is_class_extension
= false;
30544 if (next
->type
== CPP_COLON
)
30546 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30547 *super
= cp_parser_identifier (parser
);
30549 else if (next
->type
== CPP_OPEN_PAREN
)
30551 matching_parens parens
;
30552 parens
.consume_open (parser
); /* Eat '('. */
30554 /* If there is no category name, and this is an @interface, we
30555 have a class extension. */
30556 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30558 *categ
= NULL_TREE
;
30559 *is_class_extension
= true;
30562 *categ
= cp_parser_identifier (parser
);
30564 parens
.require_close (parser
);
30568 /* Parse an Objective-C class interface. */
30571 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30573 tree name
, super
, categ
, protos
;
30574 bool is_class_extension
;
30576 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30577 name
= cp_parser_identifier (parser
);
30578 if (name
== error_mark_node
)
30580 /* It's hard to recover because even if valid @interface stuff
30581 is to follow, we can't compile it (or validate it) if we
30582 don't even know which class it refers to. Let's assume this
30583 was a stray '@interface' token in the stream and skip it.
30587 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30588 &is_class_extension
);
30589 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30591 /* We have either a class or a category on our hands. */
30592 if (categ
|| is_class_extension
)
30593 objc_start_category_interface (name
, categ
, protos
, attributes
);
30596 objc_start_class_interface (name
, super
, protos
, attributes
);
30597 /* Handle instance variable declarations, if any. */
30598 cp_parser_objc_class_ivars (parser
);
30599 objc_continue_interface ();
30602 cp_parser_objc_method_prototype_list (parser
);
30605 /* Parse an Objective-C class implementation. */
30608 cp_parser_objc_class_implementation (cp_parser
* parser
)
30610 tree name
, super
, categ
;
30611 bool is_class_extension
;
30613 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30614 name
= cp_parser_identifier (parser
);
30615 if (name
== error_mark_node
)
30617 /* It's hard to recover because even if valid @implementation
30618 stuff is to follow, we can't compile it (or validate it) if
30619 we don't even know which class it refers to. Let's assume
30620 this was a stray '@implementation' token in the stream and
30625 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30626 &is_class_extension
);
30628 /* We have either a class or a category on our hands. */
30630 objc_start_category_implementation (name
, categ
);
30633 objc_start_class_implementation (name
, super
);
30634 /* Handle instance variable declarations, if any. */
30635 cp_parser_objc_class_ivars (parser
);
30636 objc_continue_implementation ();
30639 cp_parser_objc_method_definition_list (parser
);
30642 /* Consume the @end token and finish off the implementation. */
30645 cp_parser_objc_end_implementation (cp_parser
* parser
)
30647 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30648 objc_finish_implementation ();
30651 /* Parse an Objective-C declaration. */
30654 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30656 /* Try to figure out what kind of declaration is present. */
30657 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30660 switch (kwd
->keyword
)
30665 error_at (kwd
->location
, "attributes may not be specified before"
30666 " the %<@%D%> Objective-C++ keyword",
30670 case RID_AT_IMPLEMENTATION
:
30671 warning_at (kwd
->location
, OPT_Wattributes
,
30672 "prefix attributes are ignored before %<@%D%>",
30679 switch (kwd
->keyword
)
30682 cp_parser_objc_alias_declaration (parser
);
30685 cp_parser_objc_class_declaration (parser
);
30687 case RID_AT_PROTOCOL
:
30688 cp_parser_objc_protocol_declaration (parser
, attributes
);
30690 case RID_AT_INTERFACE
:
30691 cp_parser_objc_class_interface (parser
, attributes
);
30693 case RID_AT_IMPLEMENTATION
:
30694 cp_parser_objc_class_implementation (parser
);
30697 cp_parser_objc_end_implementation (parser
);
30700 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30702 cp_parser_skip_to_end_of_block_or_statement (parser
);
30706 /* Parse an Objective-C try-catch-finally statement.
30708 objc-try-catch-finally-stmt:
30709 @try compound-statement objc-catch-clause-seq [opt]
30710 objc-finally-clause [opt]
30712 objc-catch-clause-seq:
30713 objc-catch-clause objc-catch-clause-seq [opt]
30716 @catch ( objc-exception-declaration ) compound-statement
30718 objc-finally-clause:
30719 @finally compound-statement
30721 objc-exception-declaration:
30722 parameter-declaration
30725 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30729 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30730 for C. Keep them in sync. */
30733 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30735 location_t location
;
30738 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30739 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30740 objc_maybe_warn_exceptions (location
);
30741 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30742 node, lest it get absorbed into the surrounding block. */
30743 stmt
= push_stmt_list ();
30744 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30745 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30747 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30749 cp_parameter_declarator
*parm
;
30750 tree parameter_declaration
= error_mark_node
;
30751 bool seen_open_paren
= false;
30752 matching_parens parens
;
30754 cp_lexer_consume_token (parser
->lexer
);
30755 if (parens
.require_open (parser
))
30756 seen_open_paren
= true;
30757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30759 /* We have "@catch (...)" (where the '...' are literally
30760 what is in the code). Skip the '...'.
30761 parameter_declaration is set to NULL_TREE, and
30762 objc_being_catch_clauses() knows that that means
30764 cp_lexer_consume_token (parser
->lexer
);
30765 parameter_declaration
= NULL_TREE
;
30769 /* We have "@catch (NSException *exception)" or something
30770 like that. Parse the parameter declaration. */
30771 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30773 parameter_declaration
= error_mark_node
;
30775 parameter_declaration
= grokdeclarator (parm
->declarator
,
30776 &parm
->decl_specifiers
,
30777 PARM
, /*initialized=*/0,
30778 /*attrlist=*/NULL
);
30780 if (seen_open_paren
)
30781 parens
.require_close (parser
);
30784 /* If there was no open parenthesis, we are recovering from
30785 an error, and we are trying to figure out what mistake
30786 the user has made. */
30788 /* If there is an immediate closing parenthesis, the user
30789 probably forgot the opening one (ie, they typed "@catch
30790 NSException *e)". Parse the closing parenthesis and keep
30792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30793 cp_lexer_consume_token (parser
->lexer
);
30795 /* If these is no immediate closing parenthesis, the user
30796 probably doesn't know that parenthesis are required at
30797 all (ie, they typed "@catch NSException *e"). So, just
30798 forget about the closing parenthesis and keep going. */
30800 objc_begin_catch_clause (parameter_declaration
);
30801 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30802 objc_finish_catch_clause ();
30804 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30806 cp_lexer_consume_token (parser
->lexer
);
30807 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30808 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30809 node, lest it get absorbed into the surrounding block. */
30810 stmt
= push_stmt_list ();
30811 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30812 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30815 return objc_finish_try_stmt ();
30818 /* Parse an Objective-C synchronized statement.
30820 objc-synchronized-stmt:
30821 @synchronized ( expression ) compound-statement
30823 Returns NULL_TREE. */
30826 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30828 location_t location
;
30831 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30833 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30834 objc_maybe_warn_exceptions (location
);
30835 matching_parens parens
;
30836 parens
.require_open (parser
);
30837 lock
= cp_parser_expression (parser
);
30838 parens
.require_close (parser
);
30840 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30841 node, lest it get absorbed into the surrounding block. */
30842 stmt
= push_stmt_list ();
30843 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30845 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30848 /* Parse an Objective-C throw statement.
30851 @throw assignment-expression [opt] ;
30853 Returns a constructed '@throw' statement. */
30856 cp_parser_objc_throw_statement (cp_parser
*parser
)
30858 tree expr
= NULL_TREE
;
30859 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30861 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30863 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30864 expr
= cp_parser_expression (parser
);
30866 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30868 return objc_build_throw_stmt (loc
, expr
);
30871 /* Parse an Objective-C statement. */
30874 cp_parser_objc_statement (cp_parser
* parser
)
30876 /* Try to figure out what kind of declaration is present. */
30877 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30879 switch (kwd
->keyword
)
30882 return cp_parser_objc_try_catch_finally_statement (parser
);
30883 case RID_AT_SYNCHRONIZED
:
30884 return cp_parser_objc_synchronized_statement (parser
);
30886 return cp_parser_objc_throw_statement (parser
);
30888 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30890 cp_parser_skip_to_end_of_block_or_statement (parser
);
30893 return error_mark_node
;
30896 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30897 look ahead to see if an objc keyword follows the attributes. This
30898 is to detect the use of prefix attributes on ObjC @interface and
30902 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30904 cp_lexer_save_tokens (parser
->lexer
);
30905 *attrib
= cp_parser_attributes_opt (parser
);
30906 gcc_assert (*attrib
);
30907 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30909 cp_lexer_commit_tokens (parser
->lexer
);
30912 cp_lexer_rollback_tokens (parser
->lexer
);
30916 /* This routine is a minimal replacement for
30917 c_parser_struct_declaration () used when parsing the list of
30918 types/names or ObjC++ properties. For example, when parsing the
30921 @property (readonly) int a, b, c;
30923 this function is responsible for parsing "int a, int b, int c" and
30924 returning the declarations as CHAIN of DECLs.
30926 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30927 similar parsing. */
30929 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30931 tree decls
= NULL_TREE
;
30932 cp_decl_specifier_seq declspecs
;
30933 int decl_class_or_enum_p
;
30934 tree prefix_attributes
;
30936 cp_parser_decl_specifier_seq (parser
,
30937 CP_PARSER_FLAGS_NONE
,
30939 &decl_class_or_enum_p
);
30941 if (declspecs
.type
== error_mark_node
)
30942 return error_mark_node
;
30944 /* auto, register, static, extern, mutable. */
30945 if (declspecs
.storage_class
!= sc_none
)
30947 cp_parser_error (parser
, "invalid type for property");
30948 declspecs
.storage_class
= sc_none
;
30951 /* thread_local. */
30952 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30954 cp_parser_error (parser
, "invalid type for property");
30955 declspecs
.locations
[ds_thread
] = 0;
30959 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30961 cp_parser_error (parser
, "invalid type for property");
30962 declspecs
.locations
[ds_typedef
] = 0;
30965 prefix_attributes
= declspecs
.attributes
;
30966 declspecs
.attributes
= NULL_TREE
;
30968 /* Keep going until we hit the `;' at the end of the declaration. */
30969 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30971 tree attributes
, first_attribute
, decl
;
30972 cp_declarator
*declarator
;
30975 /* Parse the declarator. */
30976 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30977 NULL
, NULL
, false, false);
30979 /* Look for attributes that apply to the ivar. */
30980 attributes
= cp_parser_attributes_opt (parser
);
30981 /* Remember which attributes are prefix attributes and
30983 first_attribute
= attributes
;
30984 /* Combine the attributes. */
30985 attributes
= attr_chainon (prefix_attributes
, attributes
);
30987 decl
= grokfield (declarator
, &declspecs
,
30988 NULL_TREE
, /*init_const_expr_p=*/false,
30989 NULL_TREE
, attributes
);
30991 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30992 return error_mark_node
;
30994 /* Reset PREFIX_ATTRIBUTES. */
30995 if (attributes
!= error_mark_node
)
30997 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30998 attributes
= TREE_CHAIN (attributes
);
31000 TREE_CHAIN (attributes
) = NULL_TREE
;
31003 DECL_CHAIN (decl
) = decls
;
31006 token
= cp_lexer_peek_token (parser
->lexer
);
31007 if (token
->type
== CPP_COMMA
)
31009 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31018 /* Parse an Objective-C @property declaration. The syntax is:
31020 objc-property-declaration:
31021 '@property' objc-property-attributes[opt] struct-declaration ;
31023 objc-property-attributes:
31024 '(' objc-property-attribute-list ')'
31026 objc-property-attribute-list:
31027 objc-property-attribute
31028 objc-property-attribute-list, objc-property-attribute
31030 objc-property-attribute
31031 'getter' = identifier
31032 'setter' = identifier
31041 @property NSString *name;
31042 @property (readonly) id object;
31043 @property (retain, nonatomic, getter=getTheName) id name;
31044 @property int a, b, c;
31046 PS: This function is identical to
31047 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31049 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31051 /* The following variables hold the attributes of the properties as
31052 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31053 seen. When we see an attribute, we set them to 'true' (if they
31054 are boolean properties) or to the identifier (if they have an
31055 argument, ie, for getter and setter). Note that here we only
31056 parse the list of attributes, check the syntax and accumulate the
31057 attributes that we find. objc_add_property_declaration() will
31058 then process the information. */
31059 bool property_assign
= false;
31060 bool property_copy
= false;
31061 tree property_getter_ident
= NULL_TREE
;
31062 bool property_nonatomic
= false;
31063 bool property_readonly
= false;
31064 bool property_readwrite
= false;
31065 bool property_retain
= false;
31066 tree property_setter_ident
= NULL_TREE
;
31068 /* 'properties' is the list of properties that we read. Usually a
31069 single one, but maybe more (eg, in "@property int a, b, c;" there
31074 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31076 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31078 /* Parse the optional attribute list... */
31079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31082 matching_parens parens
;
31083 parens
.consume_open (parser
);
31087 bool syntax_error
= false;
31088 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31091 if (token
->type
!= CPP_NAME
)
31093 cp_parser_error (parser
, "expected identifier");
31096 keyword
= C_RID_CODE (token
->u
.value
);
31097 cp_lexer_consume_token (parser
->lexer
);
31100 case RID_ASSIGN
: property_assign
= true; break;
31101 case RID_COPY
: property_copy
= true; break;
31102 case RID_NONATOMIC
: property_nonatomic
= true; break;
31103 case RID_READONLY
: property_readonly
= true; break;
31104 case RID_READWRITE
: property_readwrite
= true; break;
31105 case RID_RETAIN
: property_retain
= true; break;
31109 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31111 if (keyword
== RID_GETTER
)
31112 cp_parser_error (parser
,
31113 "missing %<=%> (after %<getter%> attribute)");
31115 cp_parser_error (parser
,
31116 "missing %<=%> (after %<setter%> attribute)");
31117 syntax_error
= true;
31120 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31121 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31123 cp_parser_error (parser
, "expected identifier");
31124 syntax_error
= true;
31127 if (keyword
== RID_SETTER
)
31129 if (property_setter_ident
!= NULL_TREE
)
31131 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31132 cp_lexer_consume_token (parser
->lexer
);
31135 property_setter_ident
= cp_parser_objc_selector (parser
);
31136 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31137 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31139 cp_lexer_consume_token (parser
->lexer
);
31143 if (property_getter_ident
!= NULL_TREE
)
31145 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31146 cp_lexer_consume_token (parser
->lexer
);
31149 property_getter_ident
= cp_parser_objc_selector (parser
);
31153 cp_parser_error (parser
, "unknown property attribute");
31154 syntax_error
= true;
31161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31162 cp_lexer_consume_token (parser
->lexer
);
31167 /* FIXME: "@property (setter, assign);" will generate a spurious
31168 "error: expected ‘)’ before ‘,’ token". This is because
31169 cp_parser_require, unlike the C counterpart, will produce an
31170 error even if we are in error recovery. */
31171 if (!parens
.require_close (parser
))
31173 cp_parser_skip_to_closing_parenthesis (parser
,
31174 /*recovering=*/true,
31175 /*or_comma=*/false,
31176 /*consume_paren=*/true);
31180 /* ... and the property declaration(s). */
31181 properties
= cp_parser_objc_struct_declaration (parser
);
31183 if (properties
== error_mark_node
)
31185 cp_parser_skip_to_end_of_statement (parser
);
31186 /* If the next token is now a `;', consume it. */
31187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31188 cp_lexer_consume_token (parser
->lexer
);
31192 if (properties
== NULL_TREE
)
31193 cp_parser_error (parser
, "expected identifier");
31196 /* Comma-separated properties are chained together in
31197 reverse order; add them one by one. */
31198 properties
= nreverse (properties
);
31200 for (; properties
; properties
= TREE_CHAIN (properties
))
31201 objc_add_property_declaration (loc
, copy_node (properties
),
31202 property_readonly
, property_readwrite
,
31203 property_assign
, property_retain
,
31204 property_copy
, property_nonatomic
,
31205 property_getter_ident
, property_setter_ident
);
31208 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31211 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31213 objc-synthesize-declaration:
31214 @synthesize objc-synthesize-identifier-list ;
31216 objc-synthesize-identifier-list:
31217 objc-synthesize-identifier
31218 objc-synthesize-identifier-list, objc-synthesize-identifier
31220 objc-synthesize-identifier
31222 identifier = identifier
31225 @synthesize MyProperty;
31226 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31228 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31229 for C. Keep them in sync.
31232 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31234 tree list
= NULL_TREE
;
31236 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31238 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31241 tree property
, ivar
;
31242 property
= cp_parser_identifier (parser
);
31243 if (property
== error_mark_node
)
31245 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31250 cp_lexer_consume_token (parser
->lexer
);
31251 ivar
= cp_parser_identifier (parser
);
31252 if (ivar
== error_mark_node
)
31254 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31260 list
= chainon (list
, build_tree_list (ivar
, property
));
31261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31262 cp_lexer_consume_token (parser
->lexer
);
31266 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31267 objc_add_synthesize_declaration (loc
, list
);
31270 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31272 objc-dynamic-declaration:
31273 @dynamic identifier-list ;
31276 @dynamic MyProperty;
31277 @dynamic MyProperty, AnotherProperty;
31279 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31280 for C. Keep them in sync.
31283 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31285 tree list
= NULL_TREE
;
31287 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31289 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31293 property
= cp_parser_identifier (parser
);
31294 if (property
== error_mark_node
)
31296 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31299 list
= chainon (list
, build_tree_list (NULL
, property
));
31300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31301 cp_lexer_consume_token (parser
->lexer
);
31305 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31306 objc_add_dynamic_declaration (loc
, list
);
31310 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31312 /* Returns name of the next clause.
31313 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31314 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31315 returned and the token is consumed. */
31317 static pragma_omp_clause
31318 cp_parser_omp_clause_name (cp_parser
*parser
)
31320 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31322 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31323 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31324 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31325 result
= PRAGMA_OMP_CLAUSE_IF
;
31326 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31327 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31328 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31329 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31330 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31331 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31332 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31333 result
= PRAGMA_OMP_CLAUSE_FOR
;
31334 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31336 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31337 const char *p
= IDENTIFIER_POINTER (id
);
31342 if (!strcmp ("aligned", p
))
31343 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31344 else if (!strcmp ("async", p
))
31345 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31348 if (!strcmp ("collapse", p
))
31349 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31350 else if (!strcmp ("copy", p
))
31351 result
= PRAGMA_OACC_CLAUSE_COPY
;
31352 else if (!strcmp ("copyin", p
))
31353 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31354 else if (!strcmp ("copyout", p
))
31355 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31356 else if (!strcmp ("copyprivate", p
))
31357 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31358 else if (!strcmp ("create", p
))
31359 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31362 if (!strcmp ("defaultmap", p
))
31363 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31364 else if (!strcmp ("depend", p
))
31365 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31366 else if (!strcmp ("device", p
))
31367 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31368 else if (!strcmp ("deviceptr", p
))
31369 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31370 else if (!strcmp ("device_resident", p
))
31371 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31372 else if (!strcmp ("dist_schedule", p
))
31373 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31376 if (!strcmp ("final", p
))
31377 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31378 else if (!strcmp ("finalize", p
))
31379 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31380 else if (!strcmp ("firstprivate", p
))
31381 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31382 else if (!strcmp ("from", p
))
31383 result
= PRAGMA_OMP_CLAUSE_FROM
;
31386 if (!strcmp ("gang", p
))
31387 result
= PRAGMA_OACC_CLAUSE_GANG
;
31388 else if (!strcmp ("grainsize", p
))
31389 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31392 if (!strcmp ("hint", p
))
31393 result
= PRAGMA_OMP_CLAUSE_HINT
;
31394 else if (!strcmp ("host", p
))
31395 result
= PRAGMA_OACC_CLAUSE_HOST
;
31398 if (!strcmp ("if_present", p
))
31399 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31400 else if (!strcmp ("inbranch", p
))
31401 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31402 else if (!strcmp ("independent", p
))
31403 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31404 else if (!strcmp ("is_device_ptr", p
))
31405 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31408 if (!strcmp ("lastprivate", p
))
31409 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31410 else if (!strcmp ("linear", p
))
31411 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31412 else if (!strcmp ("link", p
))
31413 result
= PRAGMA_OMP_CLAUSE_LINK
;
31416 if (!strcmp ("map", p
))
31417 result
= PRAGMA_OMP_CLAUSE_MAP
;
31418 else if (!strcmp ("mergeable", p
))
31419 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31422 if (!strcmp ("nogroup", p
))
31423 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31424 else if (!strcmp ("notinbranch", p
))
31425 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31426 else if (!strcmp ("nowait", p
))
31427 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31428 else if (!strcmp ("num_gangs", p
))
31429 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31430 else if (!strcmp ("num_tasks", p
))
31431 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31432 else if (!strcmp ("num_teams", p
))
31433 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31434 else if (!strcmp ("num_threads", p
))
31435 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31436 else if (!strcmp ("num_workers", p
))
31437 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31440 if (!strcmp ("ordered", p
))
31441 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31444 if (!strcmp ("parallel", p
))
31445 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31446 else if (!strcmp ("present", p
))
31447 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31448 else if (!strcmp ("present_or_copy", p
)
31449 || !strcmp ("pcopy", p
))
31450 result
= PRAGMA_OACC_CLAUSE_COPY
;
31451 else if (!strcmp ("present_or_copyin", p
)
31452 || !strcmp ("pcopyin", p
))
31453 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31454 else if (!strcmp ("present_or_copyout", p
)
31455 || !strcmp ("pcopyout", p
))
31456 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31457 else if (!strcmp ("present_or_create", p
)
31458 || !strcmp ("pcreate", p
))
31459 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31460 else if (!strcmp ("priority", p
))
31461 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31462 else if (!strcmp ("proc_bind", p
))
31463 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31466 if (!strcmp ("reduction", p
))
31467 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31470 if (!strcmp ("safelen", p
))
31471 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31472 else if (!strcmp ("schedule", p
))
31473 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31474 else if (!strcmp ("sections", p
))
31475 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31476 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31477 result
= PRAGMA_OACC_CLAUSE_HOST
;
31478 else if (!strcmp ("seq", p
))
31479 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31480 else if (!strcmp ("shared", p
))
31481 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31482 else if (!strcmp ("simd", p
))
31483 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31484 else if (!strcmp ("simdlen", p
))
31485 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31488 if (!strcmp ("taskgroup", p
))
31489 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31490 else if (!strcmp ("thread_limit", p
))
31491 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31492 else if (!strcmp ("threads", p
))
31493 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31494 else if (!strcmp ("tile", p
))
31495 result
= PRAGMA_OACC_CLAUSE_TILE
;
31496 else if (!strcmp ("to", p
))
31497 result
= PRAGMA_OMP_CLAUSE_TO
;
31500 if (!strcmp ("uniform", p
))
31501 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31502 else if (!strcmp ("untied", p
))
31503 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31504 else if (!strcmp ("use_device", p
))
31505 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31506 else if (!strcmp ("use_device_ptr", p
))
31507 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31510 if (!strcmp ("vector", p
))
31511 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31512 else if (!strcmp ("vector_length", p
))
31513 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31516 if (!strcmp ("wait", p
))
31517 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31518 else if (!strcmp ("worker", p
))
31519 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31524 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31525 cp_lexer_consume_token (parser
->lexer
);
31530 /* Validate that a clause of the given type does not already exist. */
31533 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31534 const char *name
, location_t location
)
31538 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31539 if (OMP_CLAUSE_CODE (c
) == code
)
31541 error_at (location
, "too many %qs clauses", name
);
31549 variable-list , identifier
31551 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31552 colon). An opening parenthesis will have been consumed by the caller.
31554 If KIND is nonzero, create the appropriate node and install the decl
31555 in OMP_CLAUSE_DECL and add the node to the head of the list.
31557 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31558 return the list created.
31560 COLON can be NULL if only closing parenthesis should end the list,
31561 or pointer to bool which will receive false if the list is terminated
31562 by closing parenthesis or true if the list is terminated by colon. */
31565 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31566 tree list
, bool *colon
)
31569 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31572 parser
->colon_corrects_to_scope_p
= false;
31579 token
= cp_lexer_peek_token (parser
->lexer
);
31581 && current_class_ptr
31582 && cp_parser_is_keyword (token
, RID_THIS
))
31584 decl
= finish_this_expr ();
31585 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31586 || CONVERT_EXPR_P (decl
))
31587 decl
= TREE_OPERAND (decl
, 0);
31588 cp_lexer_consume_token (parser
->lexer
);
31592 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31593 /*check_dependency_p=*/true,
31594 /*template_p=*/NULL
,
31595 /*declarator_p=*/false,
31596 /*optional_p=*/false);
31597 if (name
== error_mark_node
)
31600 if (identifier_p (name
))
31601 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31604 if (decl
== error_mark_node
)
31605 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31608 if (decl
== error_mark_node
)
31610 else if (kind
!= 0)
31614 case OMP_CLAUSE__CACHE_
:
31615 /* The OpenACC cache directive explicitly only allows "array
31616 elements or subarrays". */
31617 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31619 error_at (token
->location
, "expected %<[%>");
31620 decl
= error_mark_node
;
31624 case OMP_CLAUSE_MAP
:
31625 case OMP_CLAUSE_FROM
:
31626 case OMP_CLAUSE_TO
:
31627 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31630 = cp_lexer_peek_token (parser
->lexer
)->location
;
31631 cp_id_kind idk
= CP_ID_KIND_NONE
;
31632 cp_lexer_consume_token (parser
->lexer
);
31633 decl
= convert_from_reference (decl
);
31635 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31640 case OMP_CLAUSE_DEPEND
:
31641 case OMP_CLAUSE_REDUCTION
:
31642 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31644 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31646 parser
->colon_corrects_to_scope_p
= false;
31647 cp_lexer_consume_token (parser
->lexer
);
31648 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31649 low_bound
= cp_parser_expression (parser
);
31651 parser
->colon_corrects_to_scope_p
31652 = saved_colon_corrects_to_scope_p
;
31653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31654 length
= integer_one_node
;
31657 /* Look for `:'. */
31658 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31660 if (!cp_lexer_next_token_is (parser
->lexer
,
31662 length
= cp_parser_expression (parser
);
31664 /* Look for the closing `]'. */
31665 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31669 decl
= tree_cons (low_bound
, length
, decl
);
31676 tree u
= build_omp_clause (token
->location
, kind
);
31677 OMP_CLAUSE_DECL (u
) = decl
;
31678 OMP_CLAUSE_CHAIN (u
) = list
;
31682 list
= tree_cons (decl
, NULL_TREE
, list
);
31685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31687 cp_lexer_consume_token (parser
->lexer
);
31691 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31693 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31696 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31700 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31704 /* Try to resync to an unnested comma. Copied from
31705 cp_parser_parenthesized_expression_list. */
31708 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31709 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31710 /*recovering=*/true,
31712 /*consume_paren=*/true);
31720 /* Similarly, but expect leading and trailing parenthesis. This is a very
31721 common case for omp clauses. */
31724 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31726 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31727 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31732 copy ( variable-list )
31733 copyin ( variable-list )
31734 copyout ( variable-list )
31735 create ( variable-list )
31736 delete ( variable-list )
31737 present ( variable-list ) */
31740 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31743 enum gomp_map_kind kind
;
31746 case PRAGMA_OACC_CLAUSE_COPY
:
31747 kind
= GOMP_MAP_TOFROM
;
31749 case PRAGMA_OACC_CLAUSE_COPYIN
:
31750 kind
= GOMP_MAP_TO
;
31752 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31753 kind
= GOMP_MAP_FROM
;
31755 case PRAGMA_OACC_CLAUSE_CREATE
:
31756 kind
= GOMP_MAP_ALLOC
;
31758 case PRAGMA_OACC_CLAUSE_DELETE
:
31759 kind
= GOMP_MAP_RELEASE
;
31761 case PRAGMA_OACC_CLAUSE_DEVICE
:
31762 kind
= GOMP_MAP_FORCE_TO
;
31764 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31765 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31767 case PRAGMA_OACC_CLAUSE_HOST
:
31768 kind
= GOMP_MAP_FORCE_FROM
;
31770 case PRAGMA_OACC_CLAUSE_LINK
:
31771 kind
= GOMP_MAP_LINK
;
31773 case PRAGMA_OACC_CLAUSE_PRESENT
:
31774 kind
= GOMP_MAP_FORCE_PRESENT
;
31777 gcc_unreachable ();
31780 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31782 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31783 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31789 deviceptr ( variable-list ) */
31792 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31794 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31797 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31798 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31799 variable-list must only allow for pointer variables. */
31800 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31801 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31803 tree v
= TREE_PURPOSE (t
);
31804 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31805 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31806 OMP_CLAUSE_DECL (u
) = v
;
31807 OMP_CLAUSE_CHAIN (u
) = list
;
31822 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31823 enum omp_clause_code code
,
31824 tree list
, location_t location
)
31826 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31827 tree c
= build_omp_clause (location
, code
);
31828 OMP_CLAUSE_CHAIN (c
) = list
;
31833 num_gangs ( expression )
31834 num_workers ( expression )
31835 vector_length ( expression ) */
31838 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31839 const char *str
, tree list
)
31841 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31843 matching_parens parens
;
31844 if (!parens
.require_open (parser
))
31847 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31849 if (t
== error_mark_node
31850 || !parens
.require_close (parser
))
31852 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31853 /*or_comma=*/false,
31854 /*consume_paren=*/true);
31858 check_no_duplicate_clause (list
, code
, str
, loc
);
31860 tree c
= build_omp_clause (loc
, code
);
31861 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31862 OMP_CLAUSE_CHAIN (c
) = list
;
31868 gang [( gang-arg-list )]
31869 worker [( [num:] int-expr )]
31870 vector [( [length:] int-expr )]
31872 where gang-arg is one of:
31877 and size-expr may be:
31884 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31885 const char *str
, tree list
)
31887 const char *id
= "num";
31888 cp_lexer
*lexer
= parser
->lexer
;
31889 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31890 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31892 if (kind
== OMP_CLAUSE_VECTOR
)
31895 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31897 matching_parens parens
;
31898 parens
.consume_open (parser
);
31902 cp_token
*next
= cp_lexer_peek_token (lexer
);
31905 /* Gang static argument. */
31906 if (kind
== OMP_CLAUSE_GANG
31907 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31909 cp_lexer_consume_token (lexer
);
31911 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31912 goto cleanup_error
;
31915 if (ops
[idx
] != NULL
)
31917 cp_parser_error (parser
, "too many %<static%> arguments");
31918 goto cleanup_error
;
31921 /* Check for the '*' argument. */
31922 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31923 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31924 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31927 cp_lexer_consume_token (lexer
);
31928 ops
[idx
] = integer_minus_one_node
;
31930 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31932 cp_lexer_consume_token (lexer
);
31938 /* Worker num: argument and vector length: arguments. */
31939 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31940 && id_equal (next
->u
.value
, id
)
31941 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31943 cp_lexer_consume_token (lexer
); /* id */
31944 cp_lexer_consume_token (lexer
); /* ':' */
31947 /* Now collect the actual argument. */
31948 if (ops
[idx
] != NULL_TREE
)
31950 cp_parser_error (parser
, "unexpected argument");
31951 goto cleanup_error
;
31954 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31956 if (expr
== error_mark_node
)
31957 goto cleanup_error
;
31959 mark_exp_read (expr
);
31962 if (kind
== OMP_CLAUSE_GANG
31963 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31965 cp_lexer_consume_token (lexer
);
31972 if (!parens
.require_close (parser
))
31973 goto cleanup_error
;
31976 check_no_duplicate_clause (list
, kind
, str
, loc
);
31978 c
= build_omp_clause (loc
, kind
);
31981 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31983 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31984 OMP_CLAUSE_CHAIN (c
) = list
;
31989 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31994 tile ( size-expr-list ) */
31997 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31999 tree c
, expr
= error_mark_node
;
32000 tree tile
= NULL_TREE
;
32002 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32003 so, but the spec authors never considered such a case and have
32004 differing opinions on what it might mean, including 'not
32006 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32007 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32010 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32015 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32019 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32020 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32022 cp_lexer_consume_token (parser
->lexer
);
32023 expr
= integer_zero_node
;
32026 expr
= cp_parser_constant_expression (parser
);
32028 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32030 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32032 /* Consume the trailing ')'. */
32033 cp_lexer_consume_token (parser
->lexer
);
32035 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32036 tile
= nreverse (tile
);
32037 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32038 OMP_CLAUSE_CHAIN (c
) = list
;
32043 Parse wait clause or directive parameters. */
32046 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32048 vec
<tree
, va_gc
> *args
;
32051 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32053 /*allow_expansion_p=*/true,
32054 /*non_constant_p=*/NULL
);
32056 if (args
== NULL
|| args
->length () == 0)
32058 cp_parser_error (parser
, "expected integer expression before ')'");
32060 release_tree_vector (args
);
32064 args_tree
= build_tree_list_vec (args
);
32066 release_tree_vector (args
);
32068 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32070 tree targ
= TREE_VALUE (t
);
32072 if (targ
!= error_mark_node
)
32074 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32075 error ("%<wait%> expression must be integral");
32078 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32080 targ
= mark_rvalue_use (targ
);
32081 OMP_CLAUSE_DECL (c
) = targ
;
32082 OMP_CLAUSE_CHAIN (c
) = list
;
32092 wait ( int-expr-list ) */
32095 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32097 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32099 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32102 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32108 collapse ( constant-expression ) */
32111 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32117 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32118 matching_parens parens
;
32119 if (!parens
.require_open (parser
))
32122 num
= cp_parser_constant_expression (parser
);
32124 if (!parens
.require_close (parser
))
32125 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32126 /*or_comma=*/false,
32127 /*consume_paren=*/true);
32129 if (num
== error_mark_node
)
32131 num
= fold_non_dependent_expr (num
);
32132 if (!tree_fits_shwi_p (num
)
32133 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32134 || (n
= tree_to_shwi (num
)) <= 0
32137 error_at (loc
, "collapse argument needs positive constant integer expression");
32141 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32142 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32143 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32144 OMP_CLAUSE_CHAIN (c
) = list
;
32145 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32151 default ( none | shared )
32154 default ( none | present ) */
32157 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32158 location_t location
, bool is_oacc
)
32160 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32163 matching_parens parens
;
32164 if (!parens
.require_open (parser
))
32166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32168 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32169 const char *p
= IDENTIFIER_POINTER (id
);
32174 if (strcmp ("none", p
) != 0)
32176 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32180 if (strcmp ("present", p
) != 0 || !is_oacc
)
32182 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32186 if (strcmp ("shared", p
) != 0 || is_oacc
)
32188 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32195 cp_lexer_consume_token (parser
->lexer
);
32201 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32203 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32206 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32207 || !parens
.require_close (parser
))
32208 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32209 /*or_comma=*/false,
32210 /*consume_paren=*/true);
32212 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32215 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32216 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32217 OMP_CLAUSE_CHAIN (c
) = list
;
32218 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32224 final ( expression ) */
32227 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32231 matching_parens parens
;
32232 if (!parens
.require_open (parser
))
32235 t
= cp_parser_condition (parser
);
32237 if (t
== error_mark_node
32238 || !parens
.require_close (parser
))
32239 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32240 /*or_comma=*/false,
32241 /*consume_paren=*/true);
32243 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32245 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32246 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32247 OMP_CLAUSE_CHAIN (c
) = list
;
32256 if ( directive-name-modifier : expression )
32258 directive-name-modifier:
32259 parallel | task | taskloop | target data | target | target update
32260 | target enter data | target exit data */
32263 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32267 enum tree_code if_modifier
= ERROR_MARK
;
32269 matching_parens parens
;
32270 if (!parens
.require_open (parser
))
32273 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32275 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32276 const char *p
= IDENTIFIER_POINTER (id
);
32279 if (strcmp ("parallel", p
) == 0)
32280 if_modifier
= OMP_PARALLEL
;
32281 else if (strcmp ("task", p
) == 0)
32282 if_modifier
= OMP_TASK
;
32283 else if (strcmp ("taskloop", p
) == 0)
32284 if_modifier
= OMP_TASKLOOP
;
32285 else if (strcmp ("target", p
) == 0)
32287 if_modifier
= OMP_TARGET
;
32288 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32290 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32291 p
= IDENTIFIER_POINTER (id
);
32292 if (strcmp ("data", p
) == 0)
32293 if_modifier
= OMP_TARGET_DATA
;
32294 else if (strcmp ("update", p
) == 0)
32295 if_modifier
= OMP_TARGET_UPDATE
;
32296 else if (strcmp ("enter", p
) == 0)
32297 if_modifier
= OMP_TARGET_ENTER_DATA
;
32298 else if (strcmp ("exit", p
) == 0)
32299 if_modifier
= OMP_TARGET_EXIT_DATA
;
32300 if (if_modifier
!= OMP_TARGET
)
32305 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32306 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32308 if_modifier
= ERROR_MARK
;
32310 if (if_modifier
== OMP_TARGET_ENTER_DATA
32311 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32313 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32315 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32316 p
= IDENTIFIER_POINTER (id
);
32317 if (strcmp ("data", p
) == 0)
32323 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32324 error_at (loc
, "expected %<data%>");
32325 if_modifier
= ERROR_MARK
;
32330 if (if_modifier
!= ERROR_MARK
)
32332 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32335 cp_lexer_consume_token (parser
->lexer
);
32342 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32343 error_at (loc
, "expected %<:%>");
32345 if_modifier
= ERROR_MARK
;
32350 t
= cp_parser_condition (parser
);
32352 if (t
== error_mark_node
32353 || !parens
.require_close (parser
))
32354 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32355 /*or_comma=*/false,
32356 /*consume_paren=*/true);
32358 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32359 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32361 if (if_modifier
!= ERROR_MARK
32362 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32364 const char *p
= NULL
;
32365 switch (if_modifier
)
32367 case OMP_PARALLEL
: p
= "parallel"; break;
32368 case OMP_TASK
: p
= "task"; break;
32369 case OMP_TASKLOOP
: p
= "taskloop"; break;
32370 case OMP_TARGET_DATA
: p
= "target data"; break;
32371 case OMP_TARGET
: p
= "target"; break;
32372 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32373 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32374 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32375 default: gcc_unreachable ();
32377 error_at (location
, "too many %<if%> clauses with %qs modifier",
32381 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32384 error_at (location
, "too many %<if%> clauses");
32386 error_at (location
, "too many %<if%> clauses without modifier");
32389 else if (if_modifier
== ERROR_MARK
32390 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32392 error_at (location
, "if any %<if%> clause has modifier, then all "
32393 "%<if%> clauses have to use modifier");
32398 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32399 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32400 OMP_CLAUSE_IF_EXPR (c
) = t
;
32401 OMP_CLAUSE_CHAIN (c
) = list
;
32410 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32411 tree list
, location_t location
)
32415 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32418 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32419 OMP_CLAUSE_CHAIN (c
) = list
;
32427 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32428 tree list
, location_t location
)
32432 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32434 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32435 OMP_CLAUSE_CHAIN (c
) = list
;
32440 num_threads ( expression ) */
32443 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32444 location_t location
)
32448 matching_parens parens
;
32449 if (!parens
.require_open (parser
))
32452 t
= cp_parser_expression (parser
);
32454 if (t
== error_mark_node
32455 || !parens
.require_close (parser
))
32456 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32457 /*or_comma=*/false,
32458 /*consume_paren=*/true);
32460 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32461 "num_threads", location
);
32463 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32464 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32465 OMP_CLAUSE_CHAIN (c
) = list
;
32471 num_tasks ( expression ) */
32474 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32475 location_t location
)
32479 matching_parens parens
;
32480 if (!parens
.require_open (parser
))
32483 t
= cp_parser_expression (parser
);
32485 if (t
== error_mark_node
32486 || !parens
.require_close (parser
))
32487 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32488 /*or_comma=*/false,
32489 /*consume_paren=*/true);
32491 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32492 "num_tasks", location
);
32494 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32495 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32496 OMP_CLAUSE_CHAIN (c
) = list
;
32502 grainsize ( expression ) */
32505 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32506 location_t location
)
32510 matching_parens parens
;
32511 if (!parens
.require_open (parser
))
32514 t
= cp_parser_expression (parser
);
32516 if (t
== error_mark_node
32517 || !parens
.require_close (parser
))
32518 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32519 /*or_comma=*/false,
32520 /*consume_paren=*/true);
32522 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32523 "grainsize", location
);
32525 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32526 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32527 OMP_CLAUSE_CHAIN (c
) = list
;
32533 priority ( expression ) */
32536 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32537 location_t location
)
32541 matching_parens parens
;
32542 if (!parens
.require_open (parser
))
32545 t
= cp_parser_expression (parser
);
32547 if (t
== error_mark_node
32548 || !parens
.require_close (parser
))
32549 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32550 /*or_comma=*/false,
32551 /*consume_paren=*/true);
32553 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32554 "priority", location
);
32556 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32557 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32558 OMP_CLAUSE_CHAIN (c
) = list
;
32564 hint ( expression ) */
32567 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32568 location_t location
)
32572 matching_parens parens
;
32573 if (!parens
.require_open (parser
))
32576 t
= cp_parser_expression (parser
);
32578 if (t
== error_mark_node
32579 || !parens
.require_close (parser
))
32580 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32581 /*or_comma=*/false,
32582 /*consume_paren=*/true);
32584 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32586 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32587 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32588 OMP_CLAUSE_CHAIN (c
) = list
;
32594 defaultmap ( tofrom : scalar ) */
32597 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32598 location_t location
)
32603 matching_parens parens
;
32604 if (!parens
.require_open (parser
))
32607 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32609 cp_parser_error (parser
, "expected %<tofrom%>");
32612 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32613 p
= IDENTIFIER_POINTER (id
);
32614 if (strcmp (p
, "tofrom") != 0)
32616 cp_parser_error (parser
, "expected %<tofrom%>");
32619 cp_lexer_consume_token (parser
->lexer
);
32620 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32623 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32625 cp_parser_error (parser
, "expected %<scalar%>");
32628 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32629 p
= IDENTIFIER_POINTER (id
);
32630 if (strcmp (p
, "scalar") != 0)
32632 cp_parser_error (parser
, "expected %<scalar%>");
32635 cp_lexer_consume_token (parser
->lexer
);
32636 if (!parens
.require_close (parser
))
32639 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32642 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32643 OMP_CLAUSE_CHAIN (c
) = list
;
32647 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32648 /*or_comma=*/false,
32649 /*consume_paren=*/true);
32657 ordered ( constant-expression ) */
32660 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32661 tree list
, location_t location
)
32663 tree c
, num
= NULL_TREE
;
32666 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32667 "ordered", location
);
32669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32671 matching_parens parens
;
32672 parens
.consume_open (parser
);
32674 num
= cp_parser_constant_expression (parser
);
32676 if (!parens
.require_close (parser
))
32677 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32678 /*or_comma=*/false,
32679 /*consume_paren=*/true);
32681 if (num
== error_mark_node
)
32683 num
= fold_non_dependent_expr (num
);
32684 if (!tree_fits_shwi_p (num
)
32685 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32686 || (n
= tree_to_shwi (num
)) <= 0
32689 error_at (location
,
32690 "ordered argument needs positive constant integer "
32696 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32697 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32698 OMP_CLAUSE_CHAIN (c
) = list
;
32703 reduction ( reduction-operator : variable-list )
32705 reduction-operator:
32706 One of: + * - & ^ | && ||
32710 reduction-operator:
32711 One of: + * - & ^ | && || min max
32715 reduction-operator:
32716 One of: + * - & ^ | && ||
32720 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32722 enum tree_code code
= ERROR_MARK
;
32723 tree nlist
, c
, id
= NULL_TREE
;
32725 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32728 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32730 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32731 case CPP_MULT
: code
= MULT_EXPR
; break;
32732 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32733 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32734 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32735 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32736 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32737 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32741 if (code
!= ERROR_MARK
)
32742 cp_lexer_consume_token (parser
->lexer
);
32745 bool saved_colon_corrects_to_scope_p
;
32746 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32747 parser
->colon_corrects_to_scope_p
= false;
32748 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32749 /*check_dependency_p=*/true,
32750 /*template_p=*/NULL
,
32751 /*declarator_p=*/false,
32752 /*optional_p=*/false);
32753 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32754 if (identifier_p (id
))
32756 const char *p
= IDENTIFIER_POINTER (id
);
32758 if (strcmp (p
, "min") == 0)
32760 else if (strcmp (p
, "max") == 0)
32762 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32764 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32766 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32768 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32769 code
= BIT_AND_EXPR
;
32770 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32771 code
= BIT_IOR_EXPR
;
32772 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32773 code
= BIT_XOR_EXPR
;
32774 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32775 code
= TRUTH_ANDIF_EXPR
;
32776 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32777 code
= TRUTH_ORIF_EXPR
;
32778 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32779 tree scope
= parser
->scope
;
32781 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32782 parser
->scope
= NULL_TREE
;
32783 parser
->qualifying_scope
= NULL_TREE
;
32784 parser
->object_scope
= NULL_TREE
;
32788 error ("invalid reduction-identifier");
32790 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32791 /*or_comma=*/false,
32792 /*consume_paren=*/true);
32797 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32800 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32802 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32804 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32805 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32812 schedule ( schedule-kind )
32813 schedule ( schedule-kind , expression )
32816 static | dynamic | guided | runtime | auto
32819 schedule ( schedule-modifier : schedule-kind )
32820 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32828 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32831 int modifiers
= 0, nmodifiers
= 0;
32833 matching_parens parens
;
32834 if (!parens
.require_open (parser
))
32837 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32839 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32841 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32842 const char *p
= IDENTIFIER_POINTER (id
);
32843 if (strcmp ("simd", p
) == 0)
32844 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32845 else if (strcmp ("monotonic", p
) == 0)
32846 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32847 else if (strcmp ("nonmonotonic", p
) == 0)
32848 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32851 cp_lexer_consume_token (parser
->lexer
);
32852 if (nmodifiers
++ == 0
32853 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32854 cp_lexer_consume_token (parser
->lexer
);
32857 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32864 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32865 const char *p
= IDENTIFIER_POINTER (id
);
32870 if (strcmp ("dynamic", p
) != 0)
32872 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32876 if (strcmp ("guided", p
) != 0)
32878 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32882 if (strcmp ("runtime", p
) != 0)
32884 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32891 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32892 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32893 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32894 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32897 cp_lexer_consume_token (parser
->lexer
);
32899 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32900 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32901 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32902 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32904 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32912 cp_lexer_consume_token (parser
->lexer
);
32914 token
= cp_lexer_peek_token (parser
->lexer
);
32915 t
= cp_parser_assignment_expression (parser
);
32917 if (t
== error_mark_node
)
32919 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32920 error_at (token
->location
, "schedule %<runtime%> does not take "
32921 "a %<chunk_size%> parameter");
32922 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32923 error_at (token
->location
, "schedule %<auto%> does not take "
32924 "a %<chunk_size%> parameter");
32926 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32928 if (!parens
.require_close (parser
))
32931 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32934 OMP_CLAUSE_SCHEDULE_KIND (c
)
32935 = (enum omp_clause_schedule_kind
)
32936 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32938 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32939 OMP_CLAUSE_CHAIN (c
) = list
;
32943 cp_parser_error (parser
, "invalid schedule kind");
32945 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32946 /*or_comma=*/false,
32947 /*consume_paren=*/true);
32955 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32956 tree list
, location_t location
)
32960 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32962 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32963 OMP_CLAUSE_CHAIN (c
) = list
;
32972 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32973 tree list
, location_t location
)
32975 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32976 tree c
= build_omp_clause (location
, code
);
32977 OMP_CLAUSE_CHAIN (c
) = list
;
32988 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32989 enum omp_clause_code code
,
32990 tree list
, location_t location
)
32992 tree c
= build_omp_clause (location
, code
);
32993 OMP_CLAUSE_CHAIN (c
) = list
;
33001 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33002 tree list
, location_t location
)
33004 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33005 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33006 OMP_CLAUSE_CHAIN (c
) = list
;
33015 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33016 enum omp_clause_code code
,
33017 tree list
, location_t location
)
33019 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33020 tree c
= build_omp_clause (location
, code
);
33021 OMP_CLAUSE_CHAIN (c
) = list
;
33026 num_teams ( expression ) */
33029 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33030 location_t location
)
33034 matching_parens parens
;
33035 if (!parens
.require_open (parser
))
33038 t
= cp_parser_expression (parser
);
33040 if (t
== error_mark_node
33041 || !parens
.require_close (parser
))
33042 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33043 /*or_comma=*/false,
33044 /*consume_paren=*/true);
33046 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33047 "num_teams", location
);
33049 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33050 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33051 OMP_CLAUSE_CHAIN (c
) = list
;
33057 thread_limit ( expression ) */
33060 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33061 location_t location
)
33065 matching_parens parens
;
33066 if (!parens
.require_open (parser
))
33069 t
= cp_parser_expression (parser
);
33071 if (t
== error_mark_node
33072 || !parens
.require_close (parser
))
33073 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33074 /*or_comma=*/false,
33075 /*consume_paren=*/true);
33077 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33078 "thread_limit", location
);
33080 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33081 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33082 OMP_CLAUSE_CHAIN (c
) = list
;
33088 aligned ( variable-list )
33089 aligned ( variable-list : constant-expression ) */
33092 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33094 tree nlist
, c
, alignment
= NULL_TREE
;
33097 matching_parens parens
;
33098 if (!parens
.require_open (parser
))
33101 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33106 alignment
= cp_parser_constant_expression (parser
);
33108 if (!parens
.require_close (parser
))
33109 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33110 /*or_comma=*/false,
33111 /*consume_paren=*/true);
33113 if (alignment
== error_mark_node
)
33114 alignment
= NULL_TREE
;
33117 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33118 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33124 linear ( variable-list )
33125 linear ( variable-list : expression )
33128 linear ( modifier ( variable-list ) )
33129 linear ( modifier ( variable-list ) : expression ) */
33132 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33135 tree nlist
, c
, step
= integer_one_node
;
33137 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33139 matching_parens parens
;
33140 if (!parens
.require_open (parser
))
33143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33145 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33146 const char *p
= IDENTIFIER_POINTER (id
);
33148 if (strcmp ("ref", p
) == 0)
33149 kind
= OMP_CLAUSE_LINEAR_REF
;
33150 else if (strcmp ("val", p
) == 0)
33151 kind
= OMP_CLAUSE_LINEAR_VAL
;
33152 else if (strcmp ("uval", p
) == 0)
33153 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33154 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33155 cp_lexer_consume_token (parser
->lexer
);
33157 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33160 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33161 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33165 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33166 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33168 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33169 else if (!parens
.require_close (parser
))
33170 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33171 /*or_comma=*/false,
33172 /*consume_paren=*/true);
33179 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33180 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33182 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33183 cp_parser_parse_tentatively (parser
);
33184 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33185 /*check_dependency_p=*/true,
33186 /*template_p=*/NULL
,
33187 /*declarator_p=*/false,
33188 /*optional_p=*/false);
33189 if (step
!= error_mark_node
)
33190 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33191 if (step
== error_mark_node
)
33194 cp_parser_abort_tentative_parse (parser
);
33196 else if (!cp_parser_parse_definitely (parser
))
33200 step
= cp_parser_expression (parser
);
33202 if (!parens
.require_close (parser
))
33203 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33204 /*or_comma=*/false,
33205 /*consume_paren=*/true);
33207 if (step
== error_mark_node
)
33211 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33213 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33214 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33221 safelen ( constant-expression ) */
33224 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33225 location_t location
)
33229 matching_parens parens
;
33230 if (!parens
.require_open (parser
))
33233 t
= cp_parser_constant_expression (parser
);
33235 if (t
== error_mark_node
33236 || !parens
.require_close (parser
))
33237 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33238 /*or_comma=*/false,
33239 /*consume_paren=*/true);
33241 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33243 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33244 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33245 OMP_CLAUSE_CHAIN (c
) = list
;
33251 simdlen ( constant-expression ) */
33254 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33255 location_t location
)
33259 matching_parens parens
;
33260 if (!parens
.require_open (parser
))
33263 t
= cp_parser_constant_expression (parser
);
33265 if (t
== error_mark_node
33266 || !parens
.require_close (parser
))
33267 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33268 /*or_comma=*/false,
33269 /*consume_paren=*/true);
33271 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33273 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33274 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33275 OMP_CLAUSE_CHAIN (c
) = list
;
33282 identifier [+/- integer]
33283 vec , identifier [+/- integer]
33287 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33294 cp_parser_error (parser
, "expected identifier");
33298 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33300 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33301 tree t
, identifier
= cp_parser_identifier (parser
);
33302 tree addend
= NULL
;
33304 if (identifier
== error_mark_node
)
33305 t
= error_mark_node
;
33308 t
= cp_parser_lookup_name_simple
33309 (parser
, identifier
,
33310 cp_lexer_peek_token (parser
->lexer
)->location
);
33311 if (t
== error_mark_node
)
33312 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33319 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33321 addend
= integer_zero_node
;
33322 goto add_to_vector
;
33324 cp_lexer_consume_token (parser
->lexer
);
33326 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33328 cp_parser_error (parser
, "expected integer");
33332 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33333 if (TREE_CODE (addend
) != INTEGER_CST
)
33335 cp_parser_error (parser
, "expected integer");
33338 cp_lexer_consume_token (parser
->lexer
);
33341 if (t
!= error_mark_node
)
33343 vec
= tree_cons (addend
, t
, vec
);
33345 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33348 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33351 cp_lexer_consume_token (parser
->lexer
);
33354 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33356 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33357 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33358 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33359 OMP_CLAUSE_CHAIN (u
) = list
;
33366 depend ( depend-kind : variable-list )
33374 depend ( sink : vec ) */
33377 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33380 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33382 matching_parens parens
;
33383 if (!parens
.require_open (parser
))
33386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33388 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33389 const char *p
= IDENTIFIER_POINTER (id
);
33391 if (strcmp ("in", p
) == 0)
33392 kind
= OMP_CLAUSE_DEPEND_IN
;
33393 else if (strcmp ("inout", p
) == 0)
33394 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33395 else if (strcmp ("out", p
) == 0)
33396 kind
= OMP_CLAUSE_DEPEND_OUT
;
33397 else if (strcmp ("source", p
) == 0)
33398 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33399 else if (strcmp ("sink", p
) == 0)
33400 kind
= OMP_CLAUSE_DEPEND_SINK
;
33407 cp_lexer_consume_token (parser
->lexer
);
33409 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33411 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33412 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33413 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33414 OMP_CLAUSE_CHAIN (c
) = list
;
33415 if (!parens
.require_close (parser
))
33416 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33417 /*or_comma=*/false,
33418 /*consume_paren=*/true);
33422 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33425 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33426 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33429 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33432 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33433 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33438 cp_parser_error (parser
, "invalid depend kind");
33440 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33441 /*or_comma=*/false,
33442 /*consume_paren=*/true);
33447 map ( map-kind : variable-list )
33448 map ( variable-list )
33451 alloc | to | from | tofrom
33455 alloc | to | from | tofrom | release | delete
33457 map ( always [,] map-kind: variable-list ) */
33460 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33463 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33464 bool always
= false;
33466 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33471 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33472 const char *p
= IDENTIFIER_POINTER (id
);
33474 if (strcmp ("always", p
) == 0)
33477 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33479 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33480 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33482 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33486 cp_lexer_consume_token (parser
->lexer
);
33488 cp_lexer_consume_token (parser
->lexer
);
33493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33494 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33496 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33497 const char *p
= IDENTIFIER_POINTER (id
);
33499 if (strcmp ("alloc", p
) == 0)
33500 kind
= GOMP_MAP_ALLOC
;
33501 else if (strcmp ("to", p
) == 0)
33502 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33503 else if (strcmp ("from", p
) == 0)
33504 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33505 else if (strcmp ("tofrom", p
) == 0)
33506 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33507 else if (strcmp ("release", p
) == 0)
33508 kind
= GOMP_MAP_RELEASE
;
33511 cp_parser_error (parser
, "invalid map kind");
33512 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33513 /*or_comma=*/false,
33514 /*consume_paren=*/true);
33517 cp_lexer_consume_token (parser
->lexer
);
33518 cp_lexer_consume_token (parser
->lexer
);
33520 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33521 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33523 kind
= GOMP_MAP_DELETE
;
33524 cp_lexer_consume_token (parser
->lexer
);
33525 cp_lexer_consume_token (parser
->lexer
);
33528 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33531 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33532 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33538 device ( expression ) */
33541 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33542 location_t location
)
33546 matching_parens parens
;
33547 if (!parens
.require_open (parser
))
33550 t
= cp_parser_expression (parser
);
33552 if (t
== error_mark_node
33553 || !parens
.require_close (parser
))
33554 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33555 /*or_comma=*/false,
33556 /*consume_paren=*/true);
33558 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33559 "device", location
);
33561 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33562 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33563 OMP_CLAUSE_CHAIN (c
) = list
;
33569 dist_schedule ( static )
33570 dist_schedule ( static , expression ) */
33573 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33574 location_t location
)
33578 matching_parens parens
;
33579 if (!parens
.require_open (parser
))
33582 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33584 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33586 cp_lexer_consume_token (parser
->lexer
);
33588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33590 cp_lexer_consume_token (parser
->lexer
);
33592 t
= cp_parser_assignment_expression (parser
);
33594 if (t
== error_mark_node
)
33596 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33598 if (!parens
.require_close (parser
))
33601 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33604 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33606 OMP_CLAUSE_CHAIN (c
) = list
;
33610 cp_parser_error (parser
, "invalid dist_schedule kind");
33612 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33613 /*or_comma=*/false,
33614 /*consume_paren=*/true);
33619 proc_bind ( proc-bind-kind )
33622 master | close | spread */
33625 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33626 location_t location
)
33629 enum omp_clause_proc_bind_kind kind
;
33631 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33636 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33637 const char *p
= IDENTIFIER_POINTER (id
);
33639 if (strcmp ("master", p
) == 0)
33640 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33641 else if (strcmp ("close", p
) == 0)
33642 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33643 else if (strcmp ("spread", p
) == 0)
33644 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33651 cp_lexer_consume_token (parser
->lexer
);
33652 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33655 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33656 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33658 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33659 OMP_CLAUSE_CHAIN (c
) = list
;
33663 cp_parser_error (parser
, "invalid depend kind");
33665 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33666 /*or_comma=*/false,
33667 /*consume_paren=*/true);
33672 async [( int-expr )] */
33675 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33678 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33680 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33682 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33684 matching_parens parens
;
33685 parens
.consume_open (parser
);
33687 t
= cp_parser_expression (parser
);
33688 if (t
== error_mark_node
33689 || !parens
.require_close (parser
))
33690 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33691 /*or_comma=*/false,
33692 /*consume_paren=*/true);
33695 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33697 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33698 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33699 OMP_CLAUSE_CHAIN (c
) = list
;
33705 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33706 is a bitmask in MASK. Return the list of clauses found. */
33709 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33710 const char *where
, cp_token
*pragma_tok
,
33711 bool finish_p
= true)
33713 tree clauses
= NULL
;
33716 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33719 pragma_omp_clause c_kind
;
33720 omp_clause_code code
;
33721 const char *c_name
;
33722 tree prev
= clauses
;
33724 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33725 cp_lexer_consume_token (parser
->lexer
);
33727 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33728 c_kind
= cp_parser_omp_clause_name (parser
);
33732 case PRAGMA_OACC_CLAUSE_ASYNC
:
33733 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33736 case PRAGMA_OACC_CLAUSE_AUTO
:
33737 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33741 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33742 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33743 c_name
= "collapse";
33745 case PRAGMA_OACC_CLAUSE_COPY
:
33746 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33749 case PRAGMA_OACC_CLAUSE_COPYIN
:
33750 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33753 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33754 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33755 c_name
= "copyout";
33757 case PRAGMA_OACC_CLAUSE_CREATE
:
33758 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33761 case PRAGMA_OACC_CLAUSE_DELETE
:
33762 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33765 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33766 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33767 c_name
= "default";
33769 case PRAGMA_OACC_CLAUSE_DEVICE
:
33770 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33773 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33774 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33775 c_name
= "deviceptr";
33777 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33778 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33779 c_name
= "device_resident";
33781 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33782 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33784 c_name
= "finalize";
33786 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33787 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33789 c_name
= "firstprivate";
33791 case PRAGMA_OACC_CLAUSE_GANG
:
33793 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33796 case PRAGMA_OACC_CLAUSE_HOST
:
33797 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33800 case PRAGMA_OACC_CLAUSE_IF
:
33801 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33804 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33805 clauses
= cp_parser_oacc_simple_clause (parser
,
33806 OMP_CLAUSE_IF_PRESENT
,
33808 c_name
= "if_present";
33810 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33811 clauses
= cp_parser_oacc_simple_clause (parser
,
33812 OMP_CLAUSE_INDEPENDENT
,
33814 c_name
= "independent";
33816 case PRAGMA_OACC_CLAUSE_LINK
:
33817 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33820 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33821 code
= OMP_CLAUSE_NUM_GANGS
;
33822 c_name
= "num_gangs";
33823 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33826 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33827 c_name
= "num_workers";
33828 code
= OMP_CLAUSE_NUM_WORKERS
;
33829 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33832 case PRAGMA_OACC_CLAUSE_PRESENT
:
33833 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33834 c_name
= "present";
33836 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33837 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33839 c_name
= "private";
33841 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33842 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33843 c_name
= "reduction";
33845 case PRAGMA_OACC_CLAUSE_SEQ
:
33846 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33850 case PRAGMA_OACC_CLAUSE_TILE
:
33851 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33854 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33855 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33857 c_name
= "use_device";
33859 case PRAGMA_OACC_CLAUSE_VECTOR
:
33861 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33864 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33865 c_name
= "vector_length";
33866 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33867 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33870 case PRAGMA_OACC_CLAUSE_WAIT
:
33871 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33874 case PRAGMA_OACC_CLAUSE_WORKER
:
33876 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33880 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33886 if (((mask
>> c_kind
) & 1) == 0)
33888 /* Remove the invalid clause(s) from the list to avoid
33889 confusing the rest of the compiler. */
33891 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33896 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33899 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33904 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33905 is a bitmask in MASK. Return the list of clauses found; the result
33906 of clause default goes in *pdefault. */
33909 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33910 const char *where
, cp_token
*pragma_tok
,
33911 bool finish_p
= true)
33913 tree clauses
= NULL
;
33915 cp_token
*token
= NULL
;
33917 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33919 pragma_omp_clause c_kind
;
33920 const char *c_name
;
33921 tree prev
= clauses
;
33923 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33924 cp_lexer_consume_token (parser
->lexer
);
33926 token
= cp_lexer_peek_token (parser
->lexer
);
33927 c_kind
= cp_parser_omp_clause_name (parser
);
33931 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33932 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33934 c_name
= "collapse";
33936 case PRAGMA_OMP_CLAUSE_COPYIN
:
33937 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33940 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33941 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33943 c_name
= "copyprivate";
33945 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33946 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33947 token
->location
, false);
33948 c_name
= "default";
33950 case PRAGMA_OMP_CLAUSE_FINAL
:
33951 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33954 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33955 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33957 c_name
= "firstprivate";
33959 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33960 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33962 c_name
= "grainsize";
33964 case PRAGMA_OMP_CLAUSE_HINT
:
33965 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33969 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33970 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33972 c_name
= "defaultmap";
33974 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33975 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33977 c_name
= "use_device_ptr";
33979 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33980 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33982 c_name
= "is_device_ptr";
33984 case PRAGMA_OMP_CLAUSE_IF
:
33985 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33989 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33990 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33992 c_name
= "lastprivate";
33994 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33995 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33997 c_name
= "mergeable";
33999 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34000 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34003 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34004 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34006 c_name
= "num_tasks";
34008 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34009 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34011 c_name
= "num_threads";
34013 case PRAGMA_OMP_CLAUSE_ORDERED
:
34014 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34016 c_name
= "ordered";
34018 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34019 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34021 c_name
= "priority";
34023 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34024 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34026 c_name
= "private";
34028 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34029 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34030 c_name
= "reduction";
34032 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34033 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34035 c_name
= "schedule";
34037 case PRAGMA_OMP_CLAUSE_SHARED
:
34038 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34042 case PRAGMA_OMP_CLAUSE_UNTIED
:
34043 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34047 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34048 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34049 clauses
, token
->location
);
34050 c_name
= "inbranch";
34052 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34053 clauses
= cp_parser_omp_clause_branch (parser
,
34054 OMP_CLAUSE_NOTINBRANCH
,
34055 clauses
, token
->location
);
34056 c_name
= "notinbranch";
34058 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34059 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34060 clauses
, token
->location
);
34061 c_name
= "parallel";
34065 error_at (token
->location
, "%qs must be the first clause of %qs",
34070 case PRAGMA_OMP_CLAUSE_FOR
:
34071 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34072 clauses
, token
->location
);
34075 goto clause_not_first
;
34077 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34078 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34079 clauses
, token
->location
);
34080 c_name
= "sections";
34082 goto clause_not_first
;
34084 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34085 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34086 clauses
, token
->location
);
34087 c_name
= "taskgroup";
34089 goto clause_not_first
;
34091 case PRAGMA_OMP_CLAUSE_LINK
:
34092 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34095 case PRAGMA_OMP_CLAUSE_TO
:
34096 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34097 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34100 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34103 case PRAGMA_OMP_CLAUSE_FROM
:
34104 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34107 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34108 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34110 c_name
= "uniform";
34112 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34113 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34115 c_name
= "num_teams";
34117 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34118 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34120 c_name
= "thread_limit";
34122 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34123 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34124 c_name
= "aligned";
34126 case PRAGMA_OMP_CLAUSE_LINEAR
:
34128 bool declare_simd
= false;
34129 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34130 declare_simd
= true;
34131 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34135 case PRAGMA_OMP_CLAUSE_DEPEND
:
34136 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34140 case PRAGMA_OMP_CLAUSE_MAP
:
34141 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34144 case PRAGMA_OMP_CLAUSE_DEVICE
:
34145 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34149 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34150 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34152 c_name
= "dist_schedule";
34154 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34155 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34157 c_name
= "proc_bind";
34159 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34160 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34162 c_name
= "safelen";
34164 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34165 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34167 c_name
= "simdlen";
34169 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34170 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34172 c_name
= "nogroup";
34174 case PRAGMA_OMP_CLAUSE_THREADS
:
34176 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34177 clauses
, token
->location
);
34178 c_name
= "threads";
34180 case PRAGMA_OMP_CLAUSE_SIMD
:
34182 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34183 clauses
, token
->location
);
34187 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34193 if (((mask
>> c_kind
) & 1) == 0)
34195 /* Remove the invalid clause(s) from the list to avoid
34196 confusing the rest of the compiler. */
34198 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34202 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34205 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34206 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34208 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34217 In practice, we're also interested in adding the statement to an
34218 outer node. So it is convenient if we work around the fact that
34219 cp_parser_statement calls add_stmt. */
34222 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34224 unsigned save
= parser
->in_statement
;
34226 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34227 This preserves the "not within loop or switch" style error messages
34228 for nonsense cases like
34234 if (parser
->in_statement
)
34235 parser
->in_statement
= IN_OMP_BLOCK
;
34241 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34243 parser
->in_statement
= save
;
34247 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34249 tree stmt
= begin_omp_structured_block ();
34250 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34252 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34254 cp_parser_end_omp_structured_block (parser
, save
);
34255 return finish_omp_structured_block (stmt
);
34259 # pragma omp atomic new-line
34263 x binop= expr | x++ | ++x | x-- | --x
34265 +, *, -, /, &, ^, |, <<, >>
34267 where x is an lvalue expression with scalar type.
34270 # pragma omp atomic new-line
34273 # pragma omp atomic read new-line
34276 # pragma omp atomic write new-line
34279 # pragma omp atomic update new-line
34282 # pragma omp atomic capture new-line
34285 # pragma omp atomic capture new-line
34293 expression-stmt | x = x binop expr
34295 v = expression-stmt
34297 { v = x; update-stmt; } | { update-stmt; v = x; }
34301 expression-stmt | x = x binop expr | x = expr binop x
34305 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34307 where x and v are lvalue expressions with scalar type. */
34310 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34312 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34313 tree rhs1
= NULL_TREE
, orig_lhs
;
34314 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34315 bool structured_block
= false;
34316 bool seq_cst
= false;
34318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34320 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34321 const char *p
= IDENTIFIER_POINTER (id
);
34323 if (!strcmp (p
, "seq_cst"))
34326 cp_lexer_consume_token (parser
->lexer
);
34327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34328 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34329 cp_lexer_consume_token (parser
->lexer
);
34332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34334 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34335 const char *p
= IDENTIFIER_POINTER (id
);
34337 if (!strcmp (p
, "read"))
34338 code
= OMP_ATOMIC_READ
;
34339 else if (!strcmp (p
, "write"))
34341 else if (!strcmp (p
, "update"))
34343 else if (!strcmp (p
, "capture"))
34344 code
= OMP_ATOMIC_CAPTURE_NEW
;
34348 cp_lexer_consume_token (parser
->lexer
);
34352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34353 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34354 cp_lexer_consume_token (parser
->lexer
);
34356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34358 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34359 const char *p
= IDENTIFIER_POINTER (id
);
34361 if (!strcmp (p
, "seq_cst"))
34364 cp_lexer_consume_token (parser
->lexer
);
34368 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34372 case OMP_ATOMIC_READ
:
34373 case NOP_EXPR
: /* atomic write */
34374 v
= cp_parser_unary_expression (parser
);
34375 if (v
== error_mark_node
)
34377 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34379 if (code
== NOP_EXPR
)
34380 lhs
= cp_parser_expression (parser
);
34382 lhs
= cp_parser_unary_expression (parser
);
34383 if (lhs
== error_mark_node
)
34385 if (code
== NOP_EXPR
)
34387 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34395 case OMP_ATOMIC_CAPTURE_NEW
:
34396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34398 cp_lexer_consume_token (parser
->lexer
);
34399 structured_block
= true;
34403 v
= cp_parser_unary_expression (parser
);
34404 if (v
== error_mark_node
)
34406 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34414 lhs
= cp_parser_unary_expression (parser
);
34416 switch (TREE_CODE (lhs
))
34421 case POSTINCREMENT_EXPR
:
34422 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34423 code
= OMP_ATOMIC_CAPTURE_OLD
;
34425 case PREINCREMENT_EXPR
:
34426 lhs
= TREE_OPERAND (lhs
, 0);
34427 opcode
= PLUS_EXPR
;
34428 rhs
= integer_one_node
;
34431 case POSTDECREMENT_EXPR
:
34432 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34433 code
= OMP_ATOMIC_CAPTURE_OLD
;
34435 case PREDECREMENT_EXPR
:
34436 lhs
= TREE_OPERAND (lhs
, 0);
34437 opcode
= MINUS_EXPR
;
34438 rhs
= integer_one_node
;
34441 case COMPOUND_EXPR
:
34442 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34443 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34444 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34445 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34446 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34447 (TREE_OPERAND (lhs
, 1), 0), 0)))
34449 /* Undo effects of boolean_increment for post {in,de}crement. */
34450 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34453 if (TREE_CODE (lhs
) == MODIFY_EXPR
34454 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34456 /* Undo effects of boolean_increment. */
34457 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34459 /* This is pre or post increment. */
34460 rhs
= TREE_OPERAND (lhs
, 1);
34461 lhs
= TREE_OPERAND (lhs
, 0);
34463 if (code
== OMP_ATOMIC_CAPTURE_NEW
34464 && !structured_block
34465 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34466 code
= OMP_ATOMIC_CAPTURE_OLD
;
34472 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34475 opcode
= MULT_EXPR
;
34478 opcode
= TRUNC_DIV_EXPR
;
34481 opcode
= PLUS_EXPR
;
34484 opcode
= MINUS_EXPR
;
34486 case CPP_LSHIFT_EQ
:
34487 opcode
= LSHIFT_EXPR
;
34489 case CPP_RSHIFT_EQ
:
34490 opcode
= RSHIFT_EXPR
;
34493 opcode
= BIT_AND_EXPR
;
34496 opcode
= BIT_IOR_EXPR
;
34499 opcode
= BIT_XOR_EXPR
;
34502 enum cp_parser_prec oprec
;
34504 cp_lexer_consume_token (parser
->lexer
);
34505 cp_parser_parse_tentatively (parser
);
34506 rhs1
= cp_parser_simple_cast_expression (parser
);
34507 if (rhs1
== error_mark_node
)
34509 cp_parser_abort_tentative_parse (parser
);
34510 cp_parser_simple_cast_expression (parser
);
34513 token
= cp_lexer_peek_token (parser
->lexer
);
34514 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34516 cp_parser_abort_tentative_parse (parser
);
34517 cp_parser_parse_tentatively (parser
);
34518 rhs
= cp_parser_binary_expression (parser
, false, true,
34519 PREC_NOT_OPERATOR
, NULL
);
34520 if (rhs
== error_mark_node
)
34522 cp_parser_abort_tentative_parse (parser
);
34523 cp_parser_binary_expression (parser
, false, true,
34524 PREC_NOT_OPERATOR
, NULL
);
34527 switch (TREE_CODE (rhs
))
34530 case TRUNC_DIV_EXPR
:
34539 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34541 if (cp_parser_parse_definitely (parser
))
34543 opcode
= TREE_CODE (rhs
);
34544 rhs1
= TREE_OPERAND (rhs
, 0);
34545 rhs
= TREE_OPERAND (rhs
, 1);
34555 cp_parser_abort_tentative_parse (parser
);
34556 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34558 rhs
= cp_parser_expression (parser
);
34559 if (rhs
== error_mark_node
)
34565 cp_parser_error (parser
,
34566 "invalid form of %<#pragma omp atomic%>");
34569 if (!cp_parser_parse_definitely (parser
))
34571 switch (token
->type
)
34573 case CPP_SEMICOLON
:
34574 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34576 code
= OMP_ATOMIC_CAPTURE_OLD
;
34581 cp_lexer_consume_token (parser
->lexer
);
34584 else if (structured_block
)
34591 cp_parser_error (parser
,
34592 "invalid form of %<#pragma omp atomic%>");
34595 opcode
= MULT_EXPR
;
34598 opcode
= TRUNC_DIV_EXPR
;
34601 opcode
= PLUS_EXPR
;
34604 opcode
= MINUS_EXPR
;
34607 opcode
= LSHIFT_EXPR
;
34610 opcode
= RSHIFT_EXPR
;
34613 opcode
= BIT_AND_EXPR
;
34616 opcode
= BIT_IOR_EXPR
;
34619 opcode
= BIT_XOR_EXPR
;
34622 cp_parser_error (parser
,
34623 "invalid operator for %<#pragma omp atomic%>");
34626 oprec
= TOKEN_PRECEDENCE (token
);
34627 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34628 if (commutative_tree_code (opcode
))
34629 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34630 cp_lexer_consume_token (parser
->lexer
);
34631 rhs
= cp_parser_binary_expression (parser
, false, false,
34633 if (rhs
== error_mark_node
)
34638 cp_parser_error (parser
,
34639 "invalid operator for %<#pragma omp atomic%>");
34642 cp_lexer_consume_token (parser
->lexer
);
34644 rhs
= cp_parser_expression (parser
);
34645 if (rhs
== error_mark_node
)
34650 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34652 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34654 v
= cp_parser_unary_expression (parser
);
34655 if (v
== error_mark_node
)
34657 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34659 lhs1
= cp_parser_unary_expression (parser
);
34660 if (lhs1
== error_mark_node
)
34663 if (structured_block
)
34665 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34666 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34669 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34670 if (!structured_block
)
34671 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34675 cp_parser_skip_to_end_of_block_or_statement (parser
);
34676 if (structured_block
)
34678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34679 cp_lexer_consume_token (parser
->lexer
);
34680 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34682 cp_parser_skip_to_end_of_block_or_statement (parser
);
34683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34684 cp_lexer_consume_token (parser
->lexer
);
34691 # pragma omp barrier new-line */
34694 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34696 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34697 finish_omp_barrier ();
34701 # pragma omp critical [(name)] new-line
34705 # pragma omp critical [(name) [hint(expression)]] new-line
34706 structured-block */
34708 #define OMP_CRITICAL_CLAUSE_MASK \
34709 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34712 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34714 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34718 matching_parens parens
;
34719 parens
.consume_open (parser
);
34721 name
= cp_parser_identifier (parser
);
34723 if (name
== error_mark_node
34724 || !parens
.require_close (parser
))
34725 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34726 /*or_comma=*/false,
34727 /*consume_paren=*/true);
34728 if (name
== error_mark_node
)
34731 clauses
= cp_parser_omp_all_clauses (parser
,
34732 OMP_CRITICAL_CLAUSE_MASK
,
34733 "#pragma omp critical", pragma_tok
);
34736 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34738 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34739 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34743 # pragma omp flush flush-vars[opt] new-line
34746 ( variable-list ) */
34749 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34751 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34752 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34753 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34755 finish_omp_flush ();
34758 /* Helper function, to parse omp for increment expression. */
34761 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34763 tree cond
= cp_parser_binary_expression (parser
, false, true,
34764 PREC_NOT_OPERATOR
, NULL
);
34765 if (cond
== error_mark_node
34766 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34768 cp_parser_skip_to_end_of_statement (parser
);
34769 return error_mark_node
;
34772 switch (TREE_CODE (cond
))
34780 /* Fall through: OpenMP disallows NE_EXPR. */
34781 gcc_fallthrough ();
34783 return error_mark_node
;
34786 /* If decl is an iterator, preserve LHS and RHS of the relational
34787 expr until finish_omp_for. */
34789 && (type_dependent_expression_p (decl
)
34790 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34793 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34795 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34796 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34797 /*overload=*/NULL
, tf_warning_or_error
);
34800 /* Helper function, to parse omp for increment expression. */
34803 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34805 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34811 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34813 op
= (token
->type
== CPP_PLUS_PLUS
34814 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34815 cp_lexer_consume_token (parser
->lexer
);
34816 lhs
= cp_parser_simple_cast_expression (parser
);
34818 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34819 return error_mark_node
;
34820 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34823 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34825 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34826 return error_mark_node
;
34828 token
= cp_lexer_peek_token (parser
->lexer
);
34829 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34831 op
= (token
->type
== CPP_PLUS_PLUS
34832 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34833 cp_lexer_consume_token (parser
->lexer
);
34834 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34837 op
= cp_parser_assignment_operator_opt (parser
);
34838 if (op
== ERROR_MARK
)
34839 return error_mark_node
;
34841 if (op
!= NOP_EXPR
)
34843 rhs
= cp_parser_assignment_expression (parser
);
34844 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34845 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34848 lhs
= cp_parser_binary_expression (parser
, false, false,
34849 PREC_ADDITIVE_EXPRESSION
, NULL
);
34850 token
= cp_lexer_peek_token (parser
->lexer
);
34851 decl_first
= (lhs
== decl
34852 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34855 if (token
->type
!= CPP_PLUS
34856 && token
->type
!= CPP_MINUS
)
34857 return error_mark_node
;
34861 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34862 cp_lexer_consume_token (parser
->lexer
);
34863 rhs
= cp_parser_binary_expression (parser
, false, false,
34864 PREC_ADDITIVE_EXPRESSION
, NULL
);
34865 token
= cp_lexer_peek_token (parser
->lexer
);
34866 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34868 if (lhs
== NULL_TREE
)
34870 if (op
== PLUS_EXPR
)
34873 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34874 tf_warning_or_error
);
34877 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34878 ERROR_MARK
, NULL
, tf_warning_or_error
);
34881 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34886 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34887 || op
== MINUS_EXPR
)
34888 return error_mark_node
;
34889 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34892 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34894 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34897 /* Parse the initialization statement of an OpenMP for loop.
34899 Return true if the resulting construct should have an
34900 OMP_CLAUSE_PRIVATE added to it. */
34903 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34904 tree
&this_pre_body
,
34905 vec
<tree
, va_gc
> *for_block
,
34911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34914 tree add_private_clause
= NULL_TREE
;
34916 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34920 integer-type var = lb
34921 random-access-iterator-type var = lb
34922 pointer-type var = lb
34924 cp_decl_specifier_seq type_specifiers
;
34926 /* First, try to parse as an initialized declaration. See
34927 cp_parser_condition, from whence the bulk of this is copied. */
34929 cp_parser_parse_tentatively (parser
);
34930 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34931 /*is_trailing_return=*/false,
34933 if (cp_parser_parse_definitely (parser
))
34935 /* If parsing a type specifier seq succeeded, then this
34936 MUST be a initialized declaration. */
34937 tree asm_specification
, attributes
;
34938 cp_declarator
*declarator
;
34940 declarator
= cp_parser_declarator (parser
,
34941 CP_PARSER_DECLARATOR_NAMED
,
34942 /*ctor_dtor_or_conv_p=*/NULL
,
34943 /*parenthesized_p=*/NULL
,
34944 /*member_p=*/false,
34945 /*friend_p=*/false);
34946 attributes
= cp_parser_attributes_opt (parser
);
34947 asm_specification
= cp_parser_asm_specification_opt (parser
);
34949 if (declarator
== cp_error_declarator
)
34950 cp_parser_skip_to_end_of_statement (parser
);
34954 tree pushed_scope
, auto_node
;
34956 decl
= start_decl (declarator
, &type_specifiers
,
34957 SD_INITIALIZED
, attributes
,
34958 /*prefix_attributes=*/NULL_TREE
,
34961 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34962 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34964 if (cp_lexer_next_token_is (parser
->lexer
,
34966 error ("parenthesized initialization is not allowed in "
34967 "OpenMP %<for%> loop");
34969 /* Trigger an error. */
34970 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34972 init
= error_mark_node
;
34973 cp_parser_skip_to_end_of_statement (parser
);
34975 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34976 || type_dependent_expression_p (decl
)
34979 bool is_direct_init
, is_non_constant_init
;
34981 init
= cp_parser_initializer (parser
,
34983 &is_non_constant_init
);
34988 = do_auto_deduction (TREE_TYPE (decl
), init
,
34991 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34992 && !type_dependent_expression_p (decl
))
34996 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34998 LOOKUP_ONLYCONVERTING
);
35000 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35002 vec_safe_push (for_block
, this_pre_body
);
35007 init
= pop_stmt_list (this_pre_body
);
35008 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35010 tree_stmt_iterator i
= tsi_start (init
);
35011 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35012 while (!tsi_end_p (i
))
35014 tree t
= tsi_stmt (i
);
35015 if (TREE_CODE (t
) == DECL_EXPR
35016 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35019 vec_safe_push (for_block
, t
);
35024 if (tsi_one_before_end_p (i
))
35026 tree t
= tsi_stmt (i
);
35028 free_stmt_list (init
);
35033 this_pre_body
= NULL_TREE
;
35038 cp_lexer_consume_token (parser
->lexer
);
35039 init
= cp_parser_assignment_expression (parser
);
35042 if (TYPE_REF_P (TREE_TYPE (decl
)))
35043 init
= error_mark_node
;
35045 cp_finish_decl (decl
, NULL_TREE
,
35046 /*init_const_expr_p=*/false,
35048 LOOKUP_ONLYCONVERTING
);
35052 pop_scope (pushed_scope
);
35058 /* If parsing a type specifier sequence failed, then
35059 this MUST be a simple expression. */
35060 cp_parser_parse_tentatively (parser
);
35061 decl
= cp_parser_primary_expression (parser
, false, false,
35063 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35064 if (!cp_parser_error_occurred (parser
)
35066 && (TREE_CODE (decl
) == COMPONENT_REF
35067 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35069 cp_parser_abort_tentative_parse (parser
);
35070 cp_parser_parse_tentatively (parser
);
35071 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35072 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35073 /*check_dependency_p=*/true,
35074 /*template_p=*/NULL
,
35075 /*declarator_p=*/false,
35076 /*optional_p=*/false);
35077 if (name
!= error_mark_node
35078 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35080 decl
= cp_parser_lookup_name_simple (parser
, name
,
35082 if (TREE_CODE (decl
) == FIELD_DECL
)
35083 add_private_clause
= omp_privatize_field (decl
, false);
35085 cp_parser_abort_tentative_parse (parser
);
35086 cp_parser_parse_tentatively (parser
);
35087 decl
= cp_parser_primary_expression (parser
, false, false,
35090 if (!cp_parser_error_occurred (parser
)
35093 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35097 cp_parser_parse_definitely (parser
);
35098 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35099 rhs
= cp_parser_assignment_expression (parser
);
35101 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35104 tf_warning_or_error
));
35105 if (!add_private_clause
)
35106 add_private_clause
= decl
;
35111 cp_parser_abort_tentative_parse (parser
);
35112 init
= cp_parser_expression (parser
);
35115 if (TREE_CODE (init
) == MODIFY_EXPR
35116 || TREE_CODE (init
) == MODOP_EXPR
)
35117 real_decl
= TREE_OPERAND (init
, 0);
35121 return add_private_clause
;
35124 /* Parse the restricted form of the for statement allowed by OpenMP. */
35127 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35128 tree
*cclauses
, bool *if_p
)
35130 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35131 tree real_decl
, initv
, condv
, incrv
, declv
;
35132 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35133 location_t loc_first
;
35134 bool collapse_err
= false;
35135 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35136 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35137 auto_vec
<tree
, 4> orig_inits
;
35138 bool tiling
= false;
35140 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35141 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35142 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35143 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35146 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35148 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35149 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35152 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35155 if (ordered
&& ordered
< collapse
)
35157 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35158 "%<ordered%> clause parameter is less than %<collapse%>");
35159 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35160 = build_int_cst (NULL_TREE
, collapse
);
35161 ordered
= collapse
;
35165 for (tree
*pc
= &clauses
; *pc
; )
35166 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35168 error_at (OMP_CLAUSE_LOCATION (*pc
),
35169 "%<linear%> clause may not be specified together "
35170 "with %<ordered%> clause with a parameter");
35171 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35174 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35177 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35178 count
= ordered
? ordered
: collapse
;
35180 declv
= make_tree_vec (count
);
35181 initv
= make_tree_vec (count
);
35182 condv
= make_tree_vec (count
);
35183 incrv
= make_tree_vec (count
);
35185 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35187 for (i
= 0; i
< count
; i
++)
35189 int bracecount
= 0;
35190 tree add_private_clause
= NULL_TREE
;
35193 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35196 cp_parser_error (parser
, "for statement expected");
35199 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35201 matching_parens parens
;
35202 if (!parens
.require_open (parser
))
35205 init
= orig_init
= decl
= real_decl
= NULL
;
35206 this_pre_body
= push_stmt_list ();
35209 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35210 init
, orig_init
, decl
, real_decl
);
35212 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35215 this_pre_body
= pop_stmt_list (this_pre_body
);
35219 pre_body
= push_stmt_list ();
35221 add_stmt (this_pre_body
);
35222 pre_body
= pop_stmt_list (pre_body
);
35225 pre_body
= this_pre_body
;
35230 if (cclauses
!= NULL
35231 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35232 && real_decl
!= NULL_TREE
)
35235 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35236 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35237 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35239 error_at (loc
, "iteration variable %qD"
35240 " should not be firstprivate", real_decl
);
35241 *c
= OMP_CLAUSE_CHAIN (*c
);
35243 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35244 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35246 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35248 *c
= OMP_CLAUSE_CHAIN (*c
);
35249 if (code
== OMP_SIMD
)
35251 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35252 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35256 OMP_CLAUSE_CHAIN (l
) = clauses
;
35259 add_private_clause
= NULL_TREE
;
35263 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35264 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35265 add_private_clause
= NULL_TREE
;
35266 c
= &OMP_CLAUSE_CHAIN (*c
);
35270 if (add_private_clause
)
35273 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35275 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35276 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35277 && OMP_CLAUSE_DECL (c
) == decl
)
35279 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35280 && OMP_CLAUSE_DECL (c
) == decl
)
35281 error_at (loc
, "iteration variable %qD "
35282 "should not be firstprivate",
35284 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35285 && OMP_CLAUSE_DECL (c
) == decl
)
35286 error_at (loc
, "iteration variable %qD should not be reduction",
35291 if (code
!= OMP_SIMD
)
35292 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35293 else if (collapse
== 1)
35294 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35296 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35297 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35298 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35301 OMP_CLAUSE_CHAIN (c
) = clauses
;
35303 /* For linear, signal that we need to fill up
35304 the so far unknown linear step. */
35305 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35306 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35312 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35313 cond
= cp_parser_omp_for_cond (parser
, decl
);
35314 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35317 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35319 /* If decl is an iterator, preserve the operator on decl
35320 until finish_omp_for. */
35322 && ((processing_template_decl
35323 && (TREE_TYPE (real_decl
) == NULL_TREE
35324 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35325 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35326 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35328 incr
= cp_parser_expression (parser
);
35329 if (!EXPR_HAS_LOCATION (incr
))
35330 protected_set_expr_location (incr
, input_location
);
35333 if (!parens
.require_close (parser
))
35334 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35335 /*or_comma=*/false,
35336 /*consume_paren=*/true);
35338 TREE_VEC_ELT (declv
, i
) = decl
;
35339 TREE_VEC_ELT (initv
, i
) = init
;
35340 TREE_VEC_ELT (condv
, i
) = cond
;
35341 TREE_VEC_ELT (incrv
, i
) = incr
;
35344 orig_inits
.safe_grow_cleared (i
+ 1);
35345 orig_inits
[i
] = orig_init
;
35348 if (i
== count
- 1)
35351 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35352 in between the collapsed for loops to be still considered perfectly
35353 nested. Hopefully the final version clarifies this.
35354 For now handle (multiple) {'s and empty statements. */
35355 cp_parser_parse_tentatively (parser
);
35358 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35360 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35362 cp_lexer_consume_token (parser
->lexer
);
35365 else if (bracecount
35366 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35367 cp_lexer_consume_token (parser
->lexer
);
35370 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35371 error_at (loc
, "not enough for loops to collapse");
35372 collapse_err
= true;
35373 cp_parser_abort_tentative_parse (parser
);
35381 cp_parser_parse_definitely (parser
);
35382 nbraces
+= bracecount
;
35389 /* Note that we saved the original contents of this flag when we entered
35390 the structured block, and so we don't need to re-save it here. */
35391 parser
->in_statement
= IN_OMP_FOR
;
35393 /* Note that the grammar doesn't call for a structured block here,
35394 though the loop as a whole is a structured block. */
35395 body
= push_stmt_list ();
35396 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35397 body
= pop_stmt_list (body
);
35399 if (declv
== NULL_TREE
)
35402 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35403 body
, pre_body
, &orig_inits
, clauses
);
35407 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35409 cp_lexer_consume_token (parser
->lexer
);
35412 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35413 cp_lexer_consume_token (parser
->lexer
);
35418 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35419 "collapsed loops not perfectly nested");
35421 collapse_err
= true;
35422 cp_parser_statement_seq_opt (parser
, NULL
);
35423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35428 while (!for_block
->is_empty ())
35430 tree t
= for_block
->pop ();
35431 if (TREE_CODE (t
) == STATEMENT_LIST
)
35432 add_stmt (pop_stmt_list (t
));
35436 release_tree_vector (for_block
);
35441 /* Helper function for OpenMP parsing, split clauses and call
35442 finish_omp_clauses on each of the set of clauses afterwards. */
35445 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35446 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35449 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35450 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35452 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35456 #pragma omp simd simd-clause[optseq] new-line
35459 #define OMP_SIMD_CLAUSE_MASK \
35460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35470 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35471 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35474 tree clauses
, sb
, ret
;
35476 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35478 strcat (p_name
, " simd");
35479 mask
|= OMP_SIMD_CLAUSE_MASK
;
35481 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35485 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35486 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35487 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35488 OMP_CLAUSE_ORDERED
);
35489 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35491 error_at (OMP_CLAUSE_LOCATION (c
),
35492 "%<ordered%> clause with parameter may not be specified "
35493 "on %qs construct", p_name
);
35494 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35498 sb
= begin_omp_structured_block ();
35499 save
= cp_parser_begin_omp_structured_block (parser
);
35501 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35503 cp_parser_end_omp_structured_block (parser
, save
);
35504 add_stmt (finish_omp_structured_block (sb
));
35510 #pragma omp for for-clause[optseq] new-line
35514 #pragma omp for simd for-simd-clause[optseq] new-line
35517 #define OMP_FOR_CLAUSE_MASK \
35518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35529 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35530 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35533 tree clauses
, sb
, ret
;
35535 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35537 strcat (p_name
, " for");
35538 mask
|= OMP_FOR_CLAUSE_MASK
;
35539 /* parallel for{, simd} disallows nowait clause, but for
35540 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35541 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35542 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35543 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35544 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35545 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35549 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35550 const char *p
= IDENTIFIER_POINTER (id
);
35552 if (strcmp (p
, "simd") == 0)
35554 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35555 if (cclauses
== NULL
)
35556 cclauses
= cclauses_buf
;
35558 cp_lexer_consume_token (parser
->lexer
);
35559 if (!flag_openmp
) /* flag_openmp_simd */
35560 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35562 sb
= begin_omp_structured_block ();
35563 save
= cp_parser_begin_omp_structured_block (parser
);
35564 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35566 cp_parser_end_omp_structured_block (parser
, save
);
35567 tree body
= finish_omp_structured_block (sb
);
35570 ret
= make_node (OMP_FOR
);
35571 TREE_TYPE (ret
) = void_type_node
;
35572 OMP_FOR_BODY (ret
) = body
;
35573 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35574 SET_EXPR_LOCATION (ret
, loc
);
35579 if (!flag_openmp
) /* flag_openmp_simd */
35581 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35585 /* Composite distribute parallel for disallows linear clause. */
35586 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35587 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35589 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35593 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35594 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35597 sb
= begin_omp_structured_block ();
35598 save
= cp_parser_begin_omp_structured_block (parser
);
35600 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35602 cp_parser_end_omp_structured_block (parser
, save
);
35603 add_stmt (finish_omp_structured_block (sb
));
35609 # pragma omp master new-line
35610 structured-block */
35613 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35615 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35616 return c_finish_omp_master (input_location
,
35617 cp_parser_omp_structured_block (parser
, if_p
));
35621 # pragma omp ordered new-line
35625 # pragma omp ordered ordered-clauses new-line
35626 structured-block */
35628 #define OMP_ORDERED_CLAUSE_MASK \
35629 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35632 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35633 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35636 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35637 enum pragma_context context
, bool *if_p
)
35639 location_t loc
= pragma_tok
->location
;
35641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35643 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35644 const char *p
= IDENTIFIER_POINTER (id
);
35646 if (strcmp (p
, "depend") == 0)
35648 if (!flag_openmp
) /* flag_openmp_simd */
35650 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35653 if (context
== pragma_stmt
)
35655 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35656 "%<depend%> clause may only be used in compound "
35658 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35662 = cp_parser_omp_all_clauses (parser
,
35663 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35664 "#pragma omp ordered", pragma_tok
);
35665 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35671 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35672 "#pragma omp ordered", pragma_tok
);
35674 if (!flag_openmp
/* flag_openmp_simd */
35675 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35678 c_finish_omp_ordered (loc
, clauses
,
35679 cp_parser_omp_structured_block (parser
, if_p
));
35686 { section-sequence }
35689 section-directive[opt] structured-block
35690 section-sequence section-directive structured-block */
35693 cp_parser_omp_sections_scope (cp_parser
*parser
)
35695 tree stmt
, substmt
;
35696 bool error_suppress
= false;
35699 matching_braces braces
;
35700 if (!braces
.require_open (parser
))
35703 stmt
= push_stmt_list ();
35705 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35706 != PRAGMA_OMP_SECTION
)
35708 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35709 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35710 add_stmt (substmt
);
35715 tok
= cp_lexer_peek_token (parser
->lexer
);
35716 if (tok
->type
== CPP_CLOSE_BRACE
)
35718 if (tok
->type
== CPP_EOF
)
35721 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35723 cp_lexer_consume_token (parser
->lexer
);
35724 cp_parser_require_pragma_eol (parser
, tok
);
35725 error_suppress
= false;
35727 else if (!error_suppress
)
35729 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35730 error_suppress
= true;
35733 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35734 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35735 add_stmt (substmt
);
35737 braces
.require_close (parser
);
35739 substmt
= pop_stmt_list (stmt
);
35741 stmt
= make_node (OMP_SECTIONS
);
35742 TREE_TYPE (stmt
) = void_type_node
;
35743 OMP_SECTIONS_BODY (stmt
) = substmt
;
35750 # pragma omp sections sections-clause[optseq] newline
35753 #define OMP_SECTIONS_CLAUSE_MASK \
35754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35761 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35762 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35765 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35767 strcat (p_name
, " sections");
35768 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35770 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35772 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35776 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35777 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35780 ret
= cp_parser_omp_sections_scope (parser
);
35782 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35788 # pragma omp parallel parallel-clause[optseq] new-line
35790 # pragma omp parallel for parallel-for-clause[optseq] new-line
35792 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35796 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35797 structured-block */
35799 #define OMP_PARALLEL_CLAUSE_MASK \
35800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35811 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35812 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35815 tree stmt
, clauses
, block
;
35817 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35819 strcat (p_name
, " parallel");
35820 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35821 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35822 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35823 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35824 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35826 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35828 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35829 if (cclauses
== NULL
)
35830 cclauses
= cclauses_buf
;
35832 cp_lexer_consume_token (parser
->lexer
);
35833 if (!flag_openmp
) /* flag_openmp_simd */
35834 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35836 block
= begin_omp_parallel ();
35837 save
= cp_parser_begin_omp_structured_block (parser
);
35838 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35840 cp_parser_end_omp_structured_block (parser
, save
);
35841 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35843 if (ret
== NULL_TREE
)
35845 OMP_PARALLEL_COMBINED (stmt
) = 1;
35848 /* When combined with distribute, parallel has to be followed by for.
35849 #pragma omp target parallel is allowed though. */
35851 && (mask
& (OMP_CLAUSE_MASK_1
35852 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35854 error_at (loc
, "expected %<for%> after %qs", p_name
);
35855 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35858 else if (!flag_openmp
) /* flag_openmp_simd */
35860 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35863 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35865 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35866 const char *p
= IDENTIFIER_POINTER (id
);
35867 if (strcmp (p
, "sections") == 0)
35869 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35870 cclauses
= cclauses_buf
;
35872 cp_lexer_consume_token (parser
->lexer
);
35873 block
= begin_omp_parallel ();
35874 save
= cp_parser_begin_omp_structured_block (parser
);
35875 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35876 cp_parser_end_omp_structured_block (parser
, save
);
35877 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35879 OMP_PARALLEL_COMBINED (stmt
) = 1;
35884 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35888 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35889 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35892 block
= begin_omp_parallel ();
35893 save
= cp_parser_begin_omp_structured_block (parser
);
35894 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35895 cp_parser_end_omp_structured_block (parser
, save
);
35896 stmt
= finish_omp_parallel (clauses
, block
);
35901 # pragma omp single single-clause[optseq] new-line
35902 structured-block */
35904 #define OMP_SINGLE_CLAUSE_MASK \
35905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35911 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35913 tree stmt
= make_node (OMP_SINGLE
);
35914 TREE_TYPE (stmt
) = void_type_node
;
35916 OMP_SINGLE_CLAUSES (stmt
)
35917 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35918 "#pragma omp single", pragma_tok
);
35919 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35921 return add_stmt (stmt
);
35925 # pragma omp task task-clause[optseq] new-line
35926 structured-block */
35928 #define OMP_TASK_CLAUSE_MASK \
35929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35941 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35943 tree clauses
, block
;
35946 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35947 "#pragma omp task", pragma_tok
);
35948 block
= begin_omp_task ();
35949 save
= cp_parser_begin_omp_structured_block (parser
);
35950 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35951 cp_parser_end_omp_structured_block (parser
, save
);
35952 return finish_omp_task (clauses
, block
);
35956 # pragma omp taskwait new-line */
35959 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35961 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35962 finish_omp_taskwait ();
35966 # pragma omp taskyield new-line */
35969 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35971 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35972 finish_omp_taskyield ();
35976 # pragma omp taskgroup new-line
35977 structured-block */
35980 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35982 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35983 return c_finish_omp_taskgroup (input_location
,
35984 cp_parser_omp_structured_block (parser
,
35990 # pragma omp threadprivate (variable-list) */
35993 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35997 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35998 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36000 finish_omp_threadprivate (vars
);
36004 # pragma omp cancel cancel-clause[optseq] new-line */
36006 #define OMP_CANCEL_CLAUSE_MASK \
36007 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36014 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36016 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36017 "#pragma omp cancel", pragma_tok
);
36018 finish_omp_cancel (clauses
);
36022 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36024 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36031 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36032 enum pragma_context context
)
36035 bool point_seen
= false;
36037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36039 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36040 const char *p
= IDENTIFIER_POINTER (id
);
36042 if (strcmp (p
, "point") == 0)
36044 cp_lexer_consume_token (parser
->lexer
);
36050 cp_parser_error (parser
, "expected %<point%>");
36051 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36055 if (context
!= pragma_compound
)
36057 if (context
== pragma_stmt
)
36058 error_at (pragma_tok
->location
,
36059 "%<#pragma %s%> may only be used in compound statements",
36060 "omp cancellation point");
36062 cp_parser_error (parser
, "expected declaration specifiers");
36063 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36067 clauses
= cp_parser_omp_all_clauses (parser
,
36068 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36069 "#pragma omp cancellation point",
36071 finish_omp_cancellation_point (clauses
);
36075 #pragma omp distribute distribute-clause[optseq] new-line
36078 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36086 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36087 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36090 tree clauses
, sb
, ret
;
36092 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36094 strcat (p_name
, " distribute");
36095 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36099 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36100 const char *p
= IDENTIFIER_POINTER (id
);
36102 bool parallel
= false;
36104 if (strcmp (p
, "simd") == 0)
36107 parallel
= strcmp (p
, "parallel") == 0;
36108 if (parallel
|| simd
)
36110 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36111 if (cclauses
== NULL
)
36112 cclauses
= cclauses_buf
;
36113 cp_lexer_consume_token (parser
->lexer
);
36114 if (!flag_openmp
) /* flag_openmp_simd */
36117 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36120 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36123 sb
= begin_omp_structured_block ();
36124 save
= cp_parser_begin_omp_structured_block (parser
);
36126 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36129 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36131 cp_parser_end_omp_structured_block (parser
, save
);
36132 tree body
= finish_omp_structured_block (sb
);
36135 ret
= make_node (OMP_DISTRIBUTE
);
36136 TREE_TYPE (ret
) = void_type_node
;
36137 OMP_FOR_BODY (ret
) = body
;
36138 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36139 SET_EXPR_LOCATION (ret
, loc
);
36144 if (!flag_openmp
) /* flag_openmp_simd */
36146 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36150 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36154 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36155 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36158 sb
= begin_omp_structured_block ();
36159 save
= cp_parser_begin_omp_structured_block (parser
);
36161 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36163 cp_parser_end_omp_structured_block (parser
, save
);
36164 add_stmt (finish_omp_structured_block (sb
));
36170 # pragma omp teams teams-clause[optseq] new-line
36171 structured-block */
36173 #define OMP_TEAMS_CLAUSE_MASK \
36174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36183 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36184 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36187 tree clauses
, sb
, ret
;
36189 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36191 strcat (p_name
, " teams");
36192 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36196 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36197 const char *p
= IDENTIFIER_POINTER (id
);
36198 if (strcmp (p
, "distribute") == 0)
36200 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36201 if (cclauses
== NULL
)
36202 cclauses
= cclauses_buf
;
36204 cp_lexer_consume_token (parser
->lexer
);
36205 if (!flag_openmp
) /* flag_openmp_simd */
36206 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36208 sb
= begin_omp_structured_block ();
36209 save
= cp_parser_begin_omp_structured_block (parser
);
36210 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36212 cp_parser_end_omp_structured_block (parser
, save
);
36213 tree body
= finish_omp_structured_block (sb
);
36216 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36217 ret
= make_node (OMP_TEAMS
);
36218 TREE_TYPE (ret
) = void_type_node
;
36219 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36220 OMP_TEAMS_BODY (ret
) = body
;
36221 OMP_TEAMS_COMBINED (ret
) = 1;
36222 SET_EXPR_LOCATION (ret
, loc
);
36223 return add_stmt (ret
);
36226 if (!flag_openmp
) /* flag_openmp_simd */
36228 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36232 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36236 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36237 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36240 tree stmt
= make_node (OMP_TEAMS
);
36241 TREE_TYPE (stmt
) = void_type_node
;
36242 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36243 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36244 SET_EXPR_LOCATION (stmt
, loc
);
36246 return add_stmt (stmt
);
36250 # pragma omp target data target-data-clause[optseq] new-line
36251 structured-block */
36253 #define OMP_TARGET_DATA_CLAUSE_MASK \
36254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36260 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36263 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36264 "#pragma omp target data", pragma_tok
);
36266 for (tree
*pc
= &clauses
; *pc
;)
36268 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36269 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36272 case GOMP_MAP_ALWAYS_TO
:
36273 case GOMP_MAP_FROM
:
36274 case GOMP_MAP_ALWAYS_FROM
:
36275 case GOMP_MAP_TOFROM
:
36276 case GOMP_MAP_ALWAYS_TOFROM
:
36277 case GOMP_MAP_ALLOC
:
36280 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36281 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36282 case GOMP_MAP_ALWAYS_POINTER
:
36286 error_at (OMP_CLAUSE_LOCATION (*pc
),
36287 "%<#pragma omp target data%> with map-type other "
36288 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36289 "on %<map%> clause");
36290 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36293 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36299 error_at (pragma_tok
->location
,
36300 "%<#pragma omp target data%> must contain at least "
36301 "one %<map%> clause");
36305 tree stmt
= make_node (OMP_TARGET_DATA
);
36306 TREE_TYPE (stmt
) = void_type_node
;
36307 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36309 keep_next_level (true);
36310 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36312 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36313 return add_stmt (stmt
);
36317 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36318 structured-block */
36320 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36328 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36329 enum pragma_context context
)
36331 bool data_seen
= false;
36332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36334 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36335 const char *p
= IDENTIFIER_POINTER (id
);
36337 if (strcmp (p
, "data") == 0)
36339 cp_lexer_consume_token (parser
->lexer
);
36345 cp_parser_error (parser
, "expected %<data%>");
36346 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36350 if (context
== pragma_stmt
)
36352 error_at (pragma_tok
->location
,
36353 "%<#pragma %s%> may only be used in compound statements",
36354 "omp target enter data");
36355 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36360 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36361 "#pragma omp target enter data", pragma_tok
);
36363 for (tree
*pc
= &clauses
; *pc
;)
36365 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36366 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36369 case GOMP_MAP_ALWAYS_TO
:
36370 case GOMP_MAP_ALLOC
:
36373 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36374 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36375 case GOMP_MAP_ALWAYS_POINTER
:
36379 error_at (OMP_CLAUSE_LOCATION (*pc
),
36380 "%<#pragma omp target enter data%> with map-type other "
36381 "than %<to%> or %<alloc%> on %<map%> clause");
36382 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36385 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36391 error_at (pragma_tok
->location
,
36392 "%<#pragma omp target enter data%> must contain at least "
36393 "one %<map%> clause");
36397 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36398 TREE_TYPE (stmt
) = void_type_node
;
36399 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36400 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36401 return add_stmt (stmt
);
36405 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36406 structured-block */
36408 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36416 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36417 enum pragma_context context
)
36419 bool data_seen
= false;
36420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36422 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36423 const char *p
= IDENTIFIER_POINTER (id
);
36425 if (strcmp (p
, "data") == 0)
36427 cp_lexer_consume_token (parser
->lexer
);
36433 cp_parser_error (parser
, "expected %<data%>");
36434 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36438 if (context
== pragma_stmt
)
36440 error_at (pragma_tok
->location
,
36441 "%<#pragma %s%> may only be used in compound statements",
36442 "omp target exit data");
36443 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36448 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36449 "#pragma omp target exit data", pragma_tok
);
36451 for (tree
*pc
= &clauses
; *pc
;)
36453 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36454 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36456 case GOMP_MAP_FROM
:
36457 case GOMP_MAP_ALWAYS_FROM
:
36458 case GOMP_MAP_RELEASE
:
36459 case GOMP_MAP_DELETE
:
36462 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36463 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36464 case GOMP_MAP_ALWAYS_POINTER
:
36468 error_at (OMP_CLAUSE_LOCATION (*pc
),
36469 "%<#pragma omp target exit data%> with map-type other "
36470 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36472 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36475 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36481 error_at (pragma_tok
->location
,
36482 "%<#pragma omp target exit data%> must contain at least "
36483 "one %<map%> clause");
36487 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36488 TREE_TYPE (stmt
) = void_type_node
;
36489 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36490 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36491 return add_stmt (stmt
);
36495 # pragma omp target update target-update-clause[optseq] new-line */
36497 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36506 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36507 enum pragma_context context
)
36509 if (context
== pragma_stmt
)
36511 error_at (pragma_tok
->location
,
36512 "%<#pragma %s%> may only be used in compound statements",
36513 "omp target update");
36514 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36519 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36520 "#pragma omp target update", pragma_tok
);
36521 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36522 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36524 error_at (pragma_tok
->location
,
36525 "%<#pragma omp target update%> must contain at least one "
36526 "%<from%> or %<to%> clauses");
36530 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36531 TREE_TYPE (stmt
) = void_type_node
;
36532 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36533 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36539 # pragma omp target target-clause[optseq] new-line
36540 structured-block */
36542 #define OMP_TARGET_CLAUSE_MASK \
36543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36554 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36555 enum pragma_context context
, bool *if_p
)
36557 tree
*pc
= NULL
, stmt
;
36559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36561 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36562 const char *p
= IDENTIFIER_POINTER (id
);
36563 enum tree_code ccode
= ERROR_MARK
;
36565 if (strcmp (p
, "teams") == 0)
36567 else if (strcmp (p
, "parallel") == 0)
36568 ccode
= OMP_PARALLEL
;
36569 else if (strcmp (p
, "simd") == 0)
36571 if (ccode
!= ERROR_MARK
)
36573 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36574 char p_name
[sizeof ("#pragma omp target teams distribute "
36575 "parallel for simd")];
36577 cp_lexer_consume_token (parser
->lexer
);
36578 strcpy (p_name
, "#pragma omp target");
36579 if (!flag_openmp
) /* flag_openmp_simd */
36585 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36586 OMP_TARGET_CLAUSE_MASK
,
36590 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36591 OMP_TARGET_CLAUSE_MASK
,
36595 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36596 OMP_TARGET_CLAUSE_MASK
,
36600 gcc_unreachable ();
36602 return stmt
!= NULL_TREE
;
36604 keep_next_level (true);
36605 tree sb
= begin_omp_structured_block (), ret
;
36606 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36610 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36611 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36615 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36616 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36620 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36621 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36625 gcc_unreachable ();
36627 cp_parser_end_omp_structured_block (parser
, save
);
36628 tree body
= finish_omp_structured_block (sb
);
36629 if (ret
== NULL_TREE
)
36631 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36633 /* For combined target teams, ensure the num_teams and
36634 thread_limit clause expressions are evaluated on the host,
36635 before entering the target construct. */
36637 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36638 c
; c
= OMP_CLAUSE_CHAIN (c
))
36639 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36640 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36641 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36643 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36644 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36645 if (expr
== error_mark_node
)
36647 tree tmp
= TARGET_EXPR_SLOT (expr
);
36649 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36650 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36651 OMP_CLAUSE_FIRSTPRIVATE
);
36652 OMP_CLAUSE_DECL (tc
) = tmp
;
36653 OMP_CLAUSE_CHAIN (tc
)
36654 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36655 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36658 tree stmt
= make_node (OMP_TARGET
);
36659 TREE_TYPE (stmt
) = void_type_node
;
36660 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36661 OMP_TARGET_BODY (stmt
) = body
;
36662 OMP_TARGET_COMBINED (stmt
) = 1;
36663 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36665 pc
= &OMP_TARGET_CLAUSES (stmt
);
36666 goto check_clauses
;
36668 else if (!flag_openmp
) /* flag_openmp_simd */
36670 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36673 else if (strcmp (p
, "data") == 0)
36675 cp_lexer_consume_token (parser
->lexer
);
36676 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36679 else if (strcmp (p
, "enter") == 0)
36681 cp_lexer_consume_token (parser
->lexer
);
36682 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36685 else if (strcmp (p
, "exit") == 0)
36687 cp_lexer_consume_token (parser
->lexer
);
36688 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36691 else if (strcmp (p
, "update") == 0)
36693 cp_lexer_consume_token (parser
->lexer
);
36694 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36697 if (!flag_openmp
) /* flag_openmp_simd */
36699 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36703 stmt
= make_node (OMP_TARGET
);
36704 TREE_TYPE (stmt
) = void_type_node
;
36706 OMP_TARGET_CLAUSES (stmt
)
36707 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36708 "#pragma omp target", pragma_tok
);
36709 pc
= &OMP_TARGET_CLAUSES (stmt
);
36710 keep_next_level (true);
36711 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36713 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36719 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36720 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36723 case GOMP_MAP_ALWAYS_TO
:
36724 case GOMP_MAP_FROM
:
36725 case GOMP_MAP_ALWAYS_FROM
:
36726 case GOMP_MAP_TOFROM
:
36727 case GOMP_MAP_ALWAYS_TOFROM
:
36728 case GOMP_MAP_ALLOC
:
36729 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36730 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36731 case GOMP_MAP_ALWAYS_POINTER
:
36734 error_at (OMP_CLAUSE_LOCATION (*pc
),
36735 "%<#pragma omp target%> with map-type other "
36736 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36737 "on %<map%> clause");
36738 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36741 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36747 # pragma acc cache (variable-list) new-line
36751 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36753 tree stmt
, clauses
;
36755 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36756 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36758 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36760 stmt
= make_node (OACC_CACHE
);
36761 TREE_TYPE (stmt
) = void_type_node
;
36762 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36763 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36770 # pragma acc data oacc-data-clause[optseq] new-line
36771 structured-block */
36773 #define OACC_DATA_CLAUSE_MASK \
36774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36783 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36785 tree stmt
, clauses
, block
;
36788 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36789 "#pragma acc data", pragma_tok
);
36791 block
= begin_omp_parallel ();
36792 save
= cp_parser_begin_omp_structured_block (parser
);
36793 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36794 cp_parser_end_omp_structured_block (parser
, save
);
36795 stmt
= finish_oacc_data (clauses
, block
);
36800 # pragma acc host_data <clauses> new-line
36801 structured-block */
36803 #define OACC_HOST_DATA_CLAUSE_MASK \
36804 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36807 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36809 tree stmt
, clauses
, block
;
36812 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36813 "#pragma acc host_data", pragma_tok
);
36815 block
= begin_omp_parallel ();
36816 save
= cp_parser_begin_omp_structured_block (parser
);
36817 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36818 cp_parser_end_omp_structured_block (parser
, save
);
36819 stmt
= finish_oacc_host_data (clauses
, block
);
36824 # pragma acc declare oacc-data-clause[optseq] new-line
36827 #define OACC_DECLARE_CLAUSE_MASK \
36828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36838 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36840 tree clauses
, stmt
;
36841 bool error
= false;
36843 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36844 "#pragma acc declare", pragma_tok
, true);
36847 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36849 error_at (pragma_tok
->location
,
36850 "no valid clauses specified in %<#pragma acc declare%>");
36854 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36856 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36857 tree decl
= OMP_CLAUSE_DECL (t
);
36858 if (!DECL_P (decl
))
36860 error_at (loc
, "array section in %<#pragma acc declare%>");
36864 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36865 switch (OMP_CLAUSE_MAP_KIND (t
))
36867 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36868 case GOMP_MAP_ALLOC
:
36870 case GOMP_MAP_FORCE_DEVICEPTR
:
36871 case GOMP_MAP_DEVICE_RESIDENT
:
36874 case GOMP_MAP_LINK
:
36875 if (!global_bindings_p ()
36876 && (TREE_STATIC (decl
)
36877 || !DECL_EXTERNAL (decl
)))
36880 "%qD must be a global variable in "
36881 "%<#pragma acc declare link%>",
36889 if (global_bindings_p ())
36891 error_at (loc
, "invalid OpenACC clause at file scope");
36895 if (DECL_EXTERNAL (decl
))
36898 "invalid use of %<extern%> variable %qD "
36899 "in %<#pragma acc declare%>", decl
);
36903 else if (TREE_PUBLIC (decl
))
36906 "invalid use of %<global%> variable %qD "
36907 "in %<#pragma acc declare%>", decl
);
36914 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36915 || lookup_attribute ("omp declare target link",
36916 DECL_ATTRIBUTES (decl
)))
36918 error_at (loc
, "variable %qD used more than once with "
36919 "%<#pragma acc declare%>", decl
);
36928 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36929 id
= get_identifier ("omp declare target link");
36931 id
= get_identifier ("omp declare target");
36933 DECL_ATTRIBUTES (decl
)
36934 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36935 if (global_bindings_p ())
36937 symtab_node
*node
= symtab_node::get (decl
);
36940 node
->offloadable
= 1;
36941 if (ENABLE_OFFLOADING
)
36943 g
->have_offload
= true;
36944 if (is_a
<varpool_node
*> (node
))
36945 vec_safe_push (offload_vars
, decl
);
36952 if (error
|| global_bindings_p ())
36955 stmt
= make_node (OACC_DECLARE
);
36956 TREE_TYPE (stmt
) = void_type_node
;
36957 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36958 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36966 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36970 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36972 LOC is the location of the #pragma token.
36975 #define OACC_ENTER_DATA_CLAUSE_MASK \
36976 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36982 #define OACC_EXIT_DATA_CLAUSE_MASK \
36983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36991 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36994 location_t loc
= pragma_tok
->location
;
36995 tree stmt
, clauses
;
36996 const char *p
= "";
36998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36999 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37001 if (strcmp (p
, "data") != 0)
37003 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37004 enter
? "enter" : "exit");
37005 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37009 cp_lexer_consume_token (parser
->lexer
);
37012 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37013 "#pragma acc enter data", pragma_tok
);
37015 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37016 "#pragma acc exit data", pragma_tok
);
37018 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37020 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37021 enter
? "enter" : "exit");
37025 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37026 TREE_TYPE (stmt
) = void_type_node
;
37027 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37028 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37034 # pragma acc loop oacc-loop-clause[optseq] new-line
37035 structured-block */
37037 #define OACC_LOOP_CLAUSE_MASK \
37038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37050 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37051 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37053 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37055 strcat (p_name
, " loop");
37056 mask
|= OACC_LOOP_CLAUSE_MASK
;
37058 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37062 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37064 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37066 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37069 tree block
= begin_omp_structured_block ();
37070 int save
= cp_parser_begin_omp_structured_block (parser
);
37071 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37072 cp_parser_end_omp_structured_block (parser
, save
);
37073 add_stmt (finish_omp_structured_block (block
));
37079 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37084 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37088 #define OACC_KERNELS_CLAUSE_MASK \
37089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37103 #define OACC_PARALLEL_CLAUSE_MASK \
37104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37122 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37123 char *p_name
, bool *if_p
)
37125 omp_clause_mask mask
;
37126 enum tree_code code
;
37127 switch (cp_parser_pragma_kind (pragma_tok
))
37129 case PRAGMA_OACC_KERNELS
:
37130 strcat (p_name
, " kernels");
37131 mask
= OACC_KERNELS_CLAUSE_MASK
;
37132 code
= OACC_KERNELS
;
37134 case PRAGMA_OACC_PARALLEL
:
37135 strcat (p_name
, " parallel");
37136 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37137 code
= OACC_PARALLEL
;
37140 gcc_unreachable ();
37143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37146 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37147 if (strcmp (p
, "loop") == 0)
37149 cp_lexer_consume_token (parser
->lexer
);
37150 tree block
= begin_omp_parallel ();
37152 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37154 return finish_omp_construct (code
, block
, clauses
);
37158 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37160 tree block
= begin_omp_parallel ();
37161 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37162 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37163 cp_parser_end_omp_structured_block (parser
, save
);
37164 return finish_omp_construct (code
, block
, clauses
);
37168 # pragma acc update oacc-update-clause[optseq] new-line
37171 #define OACC_UPDATE_CLAUSE_MASK \
37172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37180 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37182 tree stmt
, clauses
;
37184 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37185 "#pragma acc update", pragma_tok
);
37187 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37189 error_at (pragma_tok
->location
,
37190 "%<#pragma acc update%> must contain at least one "
37191 "%<device%> or %<host%> or %<self%> clause");
37195 stmt
= make_node (OACC_UPDATE
);
37196 TREE_TYPE (stmt
) = void_type_node
;
37197 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37198 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37204 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37206 LOC is the location of the #pragma token.
37209 #define OACC_WAIT_CLAUSE_MASK \
37210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37213 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37215 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37216 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37218 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37219 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37221 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37222 "#pragma acc wait", pragma_tok
);
37224 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37225 stmt
= finish_expr_stmt (stmt
);
37231 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37233 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37242 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37243 enum pragma_context context
)
37245 bool first_p
= parser
->omp_declare_simd
== NULL
;
37246 cp_omp_declare_simd_data data
;
37249 data
.error_seen
= false;
37250 data
.fndecl_seen
= false;
37251 data
.tokens
= vNULL
;
37252 data
.clauses
= NULL_TREE
;
37253 /* It is safe to take the address of a local variable; it will only be
37254 used while this scope is live. */
37255 parser
->omp_declare_simd
= &data
;
37258 /* Store away all pragma tokens. */
37259 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37260 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37261 cp_lexer_consume_token (parser
->lexer
);
37262 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37263 parser
->omp_declare_simd
->error_seen
= true;
37264 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37265 struct cp_token_cache
*cp
37266 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37267 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37271 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37272 cp_parser_pragma (parser
, context
, NULL
);
37275 case pragma_external
:
37276 cp_parser_declaration (parser
);
37278 case pragma_member
:
37279 cp_parser_member_declaration (parser
);
37281 case pragma_objc_icode
:
37282 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37285 cp_parser_declaration_statement (parser
);
37288 if (parser
->omp_declare_simd
37289 && !parser
->omp_declare_simd
->error_seen
37290 && !parser
->omp_declare_simd
->fndecl_seen
)
37291 error_at (pragma_tok
->location
,
37292 "%<#pragma omp declare simd%> not immediately followed by "
37293 "function declaration or definition");
37294 data
.tokens
.release ();
37295 parser
->omp_declare_simd
= NULL
;
37299 /* Finalize #pragma omp declare simd clauses after direct declarator has
37300 been parsed, and put that into "omp declare simd" attribute. */
37303 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37305 struct cp_token_cache
*ce
;
37306 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37309 if (!data
->error_seen
&& data
->fndecl_seen
)
37311 error ("%<#pragma omp declare simd%> not immediately followed by "
37312 "a single function declaration or definition");
37313 data
->error_seen
= true;
37315 if (data
->error_seen
)
37318 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37322 cp_parser_push_lexer_for_tokens (parser
, ce
);
37323 parser
->lexer
->in_pragma
= true;
37324 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37325 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37326 cp_lexer_consume_token (parser
->lexer
);
37327 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37328 "#pragma omp declare simd", pragma_tok
);
37329 cp_parser_pop_lexer (parser
);
37331 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37332 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37333 TREE_CHAIN (c
) = attrs
;
37334 if (processing_template_decl
)
37335 ATTR_IS_DEPENDENT (c
) = 1;
37339 data
->fndecl_seen
= true;
37345 # pragma omp declare target new-line
37346 declarations and definitions
37347 # pragma omp end declare target new-line
37350 # pragma omp declare target ( extended-list ) new-line
37352 # pragma omp declare target declare-target-clauses[seq] new-line */
37354 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37359 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37361 tree clauses
= NULL_TREE
;
37362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37364 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37365 "#pragma omp declare target", pragma_tok
);
37366 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37368 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37370 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37371 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37375 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37376 scope_chain
->omp_declare_target_attribute
++;
37379 if (scope_chain
->omp_declare_target_attribute
)
37380 error_at (pragma_tok
->location
,
37381 "%<#pragma omp declare target%> with clauses in between "
37382 "%<#pragma omp declare target%> without clauses and "
37383 "%<#pragma omp end declare target%>");
37384 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37386 tree t
= OMP_CLAUSE_DECL (c
), id
;
37387 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37388 tree at2
= lookup_attribute ("omp declare target link",
37389 DECL_ATTRIBUTES (t
));
37390 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37392 id
= get_identifier ("omp declare target link");
37393 std::swap (at1
, at2
);
37396 id
= get_identifier ("omp declare target");
37399 error_at (OMP_CLAUSE_LOCATION (c
),
37400 "%qD specified both in declare target %<link%> and %<to%>"
37406 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37407 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37410 symtab_node
*node
= symtab_node::get (t
);
37413 node
->offloadable
= 1;
37414 if (ENABLE_OFFLOADING
)
37416 g
->have_offload
= true;
37417 if (is_a
<varpool_node
*> (node
))
37418 vec_safe_push (offload_vars
, t
);
37426 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37428 const char *p
= "";
37429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37431 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37432 p
= IDENTIFIER_POINTER (id
);
37434 if (strcmp (p
, "declare") == 0)
37436 cp_lexer_consume_token (parser
->lexer
);
37438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37440 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37441 p
= IDENTIFIER_POINTER (id
);
37443 if (strcmp (p
, "target") == 0)
37444 cp_lexer_consume_token (parser
->lexer
);
37447 cp_parser_error (parser
, "expected %<target%>");
37448 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37454 cp_parser_error (parser
, "expected %<declare%>");
37455 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37458 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37459 if (!scope_chain
->omp_declare_target_attribute
)
37460 error_at (pragma_tok
->location
,
37461 "%<#pragma omp end declare target%> without corresponding "
37462 "%<#pragma omp declare target%>");
37464 scope_chain
->omp_declare_target_attribute
--;
37467 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37468 expression and optional initializer clause of
37469 #pragma omp declare reduction. We store the expression(s) as
37470 either 3, 6 or 7 special statements inside of the artificial function's
37471 body. The first two statements are DECL_EXPRs for the artificial
37472 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37473 expression that uses those variables.
37474 If there was any INITIALIZER clause, this is followed by further statements,
37475 the fourth and fifth statements are DECL_EXPRs for the artificial
37476 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37477 constructor variant (first token after open paren is not omp_priv),
37478 then the sixth statement is a statement with the function call expression
37479 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37480 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37481 to initialize the OMP_PRIV artificial variable and there is seventh
37482 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37485 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37487 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37488 gcc_assert (TYPE_REF_P (type
));
37489 type
= TREE_TYPE (type
);
37490 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37491 DECL_ARTIFICIAL (omp_out
) = 1;
37492 pushdecl (omp_out
);
37493 add_decl_expr (omp_out
);
37494 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37495 DECL_ARTIFICIAL (omp_in
) = 1;
37497 add_decl_expr (omp_in
);
37499 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37501 keep_next_level (true);
37502 tree block
= begin_omp_structured_block ();
37503 combiner
= cp_parser_expression (parser
);
37504 finish_expr_stmt (combiner
);
37505 block
= finish_omp_structured_block (block
);
37508 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37511 const char *p
= "";
37512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37514 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37515 p
= IDENTIFIER_POINTER (id
);
37518 if (strcmp (p
, "initializer") == 0)
37520 cp_lexer_consume_token (parser
->lexer
);
37521 matching_parens parens
;
37522 if (!parens
.require_open (parser
))
37526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37528 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37529 p
= IDENTIFIER_POINTER (id
);
37532 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37533 DECL_ARTIFICIAL (omp_priv
) = 1;
37534 pushdecl (omp_priv
);
37535 add_decl_expr (omp_priv
);
37536 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37537 DECL_ARTIFICIAL (omp_orig
) = 1;
37538 pushdecl (omp_orig
);
37539 add_decl_expr (omp_orig
);
37541 keep_next_level (true);
37542 block
= begin_omp_structured_block ();
37545 if (strcmp (p
, "omp_priv") == 0)
37547 bool is_direct_init
, is_non_constant_init
;
37549 cp_lexer_consume_token (parser
->lexer
);
37550 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37552 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37553 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37555 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37556 == CPP_CLOSE_PAREN
))
37558 finish_omp_structured_block (block
);
37559 error ("invalid initializer clause");
37562 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37563 &is_non_constant_init
);
37564 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37565 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37569 cp_parser_parse_tentatively (parser
);
37570 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37571 /*check_dependency_p=*/true,
37572 /*template_p=*/NULL
,
37573 /*declarator_p=*/false,
37574 /*optional_p=*/false);
37575 vec
<tree
, va_gc
> *args
;
37576 if (fn_name
== error_mark_node
37577 || cp_parser_error_occurred (parser
)
37578 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37579 || ((args
= cp_parser_parenthesized_expression_list
37580 (parser
, non_attr
, /*cast_p=*/false,
37581 /*allow_expansion_p=*/true,
37582 /*non_constant_p=*/NULL
)),
37583 cp_parser_error_occurred (parser
)))
37585 finish_omp_structured_block (block
);
37586 cp_parser_abort_tentative_parse (parser
);
37587 cp_parser_error (parser
, "expected id-expression (arguments)");
37592 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37593 if (arg
== omp_priv
37594 || (TREE_CODE (arg
) == ADDR_EXPR
37595 && TREE_OPERAND (arg
, 0) == omp_priv
))
37597 cp_parser_abort_tentative_parse (parser
);
37598 if (arg
== NULL_TREE
)
37599 error ("one of the initializer call arguments should be %<omp_priv%>"
37600 " or %<&omp_priv%>");
37601 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37603 finish_expr_stmt (initializer
);
37606 block
= finish_omp_structured_block (block
);
37607 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37611 add_decl_expr (omp_orig
);
37613 if (!parens
.require_close (parser
))
37617 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37618 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37625 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37626 initializer-clause[opt] new-line
37628 initializer-clause:
37629 initializer (omp_priv initializer)
37630 initializer (function-name (argument-list)) */
37633 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37634 enum pragma_context
)
37636 auto_vec
<tree
> types
;
37637 enum tree_code reduc_code
= ERROR_MARK
;
37638 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37640 cp_token
*first_token
;
37641 cp_token_cache
*cp
;
37645 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37646 p
= obstack_alloc (&declarator_obstack
, 0);
37648 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37651 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37654 reduc_code
= PLUS_EXPR
;
37657 reduc_code
= MULT_EXPR
;
37660 reduc_code
= MINUS_EXPR
;
37663 reduc_code
= BIT_AND_EXPR
;
37666 reduc_code
= BIT_XOR_EXPR
;
37669 reduc_code
= BIT_IOR_EXPR
;
37672 reduc_code
= TRUTH_ANDIF_EXPR
;
37675 reduc_code
= TRUTH_ORIF_EXPR
;
37678 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37681 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37682 "%<|%>, %<&&%>, %<||%> or identifier");
37686 if (reduc_code
!= ERROR_MARK
)
37687 cp_lexer_consume_token (parser
->lexer
);
37689 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37690 if (reduc_id
== error_mark_node
)
37693 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37696 /* Types may not be defined in declare reduction type list. */
37697 const char *saved_message
;
37698 saved_message
= parser
->type_definition_forbidden_message
;
37699 parser
->type_definition_forbidden_message
37700 = G_("types may not be defined in declare reduction type list");
37701 bool saved_colon_corrects_to_scope_p
;
37702 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37703 parser
->colon_corrects_to_scope_p
= false;
37704 bool saved_colon_doesnt_start_class_def_p
;
37705 saved_colon_doesnt_start_class_def_p
37706 = parser
->colon_doesnt_start_class_def_p
;
37707 parser
->colon_doesnt_start_class_def_p
= true;
37711 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37712 type
= cp_parser_type_id (parser
);
37713 if (type
== error_mark_node
)
37715 else if (ARITHMETIC_TYPE_P (type
)
37716 && (orig_reduc_id
== NULL_TREE
37717 || (TREE_CODE (type
) != COMPLEX_TYPE
37718 && (id_equal (orig_reduc_id
, "min")
37719 || id_equal (orig_reduc_id
, "max")))))
37720 error_at (loc
, "predeclared arithmetic type %qT in "
37721 "%<#pragma omp declare reduction%>", type
);
37722 else if (TREE_CODE (type
) == FUNCTION_TYPE
37723 || TREE_CODE (type
) == METHOD_TYPE
37724 || TREE_CODE (type
) == ARRAY_TYPE
)
37725 error_at (loc
, "function or array type %qT in "
37726 "%<#pragma omp declare reduction%>", type
);
37727 else if (TYPE_REF_P (type
))
37728 error_at (loc
, "reference type %qT in "
37729 "%<#pragma omp declare reduction%>", type
);
37730 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37731 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37732 "%<#pragma omp declare reduction%>", type
);
37734 types
.safe_push (type
);
37736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37737 cp_lexer_consume_token (parser
->lexer
);
37742 /* Restore the saved message. */
37743 parser
->type_definition_forbidden_message
= saved_message
;
37744 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37745 parser
->colon_doesnt_start_class_def_p
37746 = saved_colon_doesnt_start_class_def_p
;
37748 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37749 || types
.is_empty ())
37752 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37756 first_token
= cp_lexer_peek_token (parser
->lexer
);
37759 FOR_EACH_VEC_ELT (types
, i
, type
)
37762 = build_function_type_list (void_type_node
,
37763 cp_build_reference_type (type
, false),
37765 tree this_reduc_id
= reduc_id
;
37766 if (!dependent_type_p (type
))
37767 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37768 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37769 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37770 DECL_ARTIFICIAL (fndecl
) = 1;
37771 DECL_EXTERNAL (fndecl
) = 1;
37772 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37773 DECL_IGNORED_P (fndecl
) = 1;
37774 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37775 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37776 DECL_ATTRIBUTES (fndecl
)
37777 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37778 DECL_ATTRIBUTES (fndecl
));
37779 if (processing_template_decl
)
37780 fndecl
= push_template_decl (fndecl
);
37781 bool block_scope
= false;
37782 tree block
= NULL_TREE
;
37783 if (current_function_decl
)
37785 block_scope
= true;
37786 DECL_CONTEXT (fndecl
) = global_namespace
;
37787 if (!processing_template_decl
)
37790 else if (current_class_type
)
37794 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37795 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37796 cp_lexer_consume_token (parser
->lexer
);
37797 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37799 cp
= cp_token_cache_new (first_token
,
37800 cp_lexer_peek_nth_token (parser
->lexer
,
37803 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37804 finish_member_declaration (fndecl
);
37805 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37806 DECL_PENDING_INLINE_P (fndecl
) = 1;
37807 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37812 DECL_CONTEXT (fndecl
) = current_namespace
;
37816 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37818 block
= begin_omp_structured_block ();
37821 cp_parser_push_lexer_for_tokens (parser
, cp
);
37822 parser
->lexer
->in_pragma
= true;
37824 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37827 finish_function (/*inline_p=*/false);
37829 DECL_CONTEXT (fndecl
) = current_function_decl
;
37831 cp_parser_pop_lexer (parser
);
37835 cp_parser_pop_lexer (parser
);
37837 finish_function (/*inline_p=*/false);
37840 DECL_CONTEXT (fndecl
) = current_function_decl
;
37841 block
= finish_omp_structured_block (block
);
37842 if (TREE_CODE (block
) == BIND_EXPR
)
37843 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37844 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37845 DECL_SAVED_TREE (fndecl
) = block
;
37846 if (processing_template_decl
)
37847 add_decl_expr (fndecl
);
37849 cp_check_omp_declare_reduction (fndecl
);
37850 if (cp
== NULL
&& types
.length () > 1)
37851 cp
= cp_token_cache_new (first_token
,
37852 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37853 if (errs
!= errorcount
)
37857 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37860 /* Free any declarators allocated. */
37861 obstack_free (&declarator_obstack
, p
);
37865 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37866 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37867 initializer-clause[opt] new-line
37868 #pragma omp declare target new-line */
37871 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37872 enum pragma_context context
)
37874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37876 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37877 const char *p
= IDENTIFIER_POINTER (id
);
37879 if (strcmp (p
, "simd") == 0)
37881 cp_lexer_consume_token (parser
->lexer
);
37882 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37886 cp_ensure_no_omp_declare_simd (parser
);
37887 if (strcmp (p
, "reduction") == 0)
37889 cp_lexer_consume_token (parser
->lexer
);
37890 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37894 if (!flag_openmp
) /* flag_openmp_simd */
37896 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37899 if (strcmp (p
, "target") == 0)
37901 cp_lexer_consume_token (parser
->lexer
);
37902 cp_parser_omp_declare_target (parser
, pragma_tok
);
37906 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37908 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37913 #pragma omp taskloop taskloop-clause[optseq] new-line
37916 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37919 #define OMP_TASKLOOP_CLAUSE_MASK \
37920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37936 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37937 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37940 tree clauses
, sb
, ret
;
37942 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37944 strcat (p_name
, " taskloop");
37945 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37949 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37950 const char *p
= IDENTIFIER_POINTER (id
);
37952 if (strcmp (p
, "simd") == 0)
37954 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37955 if (cclauses
== NULL
)
37956 cclauses
= cclauses_buf
;
37958 cp_lexer_consume_token (parser
->lexer
);
37959 if (!flag_openmp
) /* flag_openmp_simd */
37960 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37962 sb
= begin_omp_structured_block ();
37963 save
= cp_parser_begin_omp_structured_block (parser
);
37964 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37966 cp_parser_end_omp_structured_block (parser
, save
);
37967 tree body
= finish_omp_structured_block (sb
);
37970 ret
= make_node (OMP_TASKLOOP
);
37971 TREE_TYPE (ret
) = void_type_node
;
37972 OMP_FOR_BODY (ret
) = body
;
37973 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37974 SET_EXPR_LOCATION (ret
, loc
);
37979 if (!flag_openmp
) /* flag_openmp_simd */
37981 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37985 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37989 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37990 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37993 sb
= begin_omp_structured_block ();
37994 save
= cp_parser_begin_omp_structured_block (parser
);
37996 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37999 cp_parser_end_omp_structured_block (parser
, save
);
38000 add_stmt (finish_omp_structured_block (sb
));
38007 # pragma acc routine oacc-routine-clause[optseq] new-line
38008 function-definition
38010 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38013 #define OACC_ROUTINE_CLAUSE_MASK \
38014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38020 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38021 component, which must resolve to a declared namespace-scope
38022 function. The clauses are either processed directly (for a named
38023 function), or defered until the immediatley following declaration
38027 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38028 enum pragma_context context
)
38030 gcc_checking_assert (context
== pragma_external
);
38031 /* The checking for "another pragma following this one" in the "no optional
38032 '( name )'" case makes sure that we dont re-enter. */
38033 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38035 cp_oacc_routine_data data
;
38036 data
.error_seen
= false;
38037 data
.fndecl_seen
= false;
38038 data
.tokens
= vNULL
;
38039 data
.clauses
= NULL_TREE
;
38040 data
.loc
= pragma_tok
->location
;
38041 /* It is safe to take the address of a local variable; it will only be
38042 used while this scope is live. */
38043 parser
->oacc_routine
= &data
;
38045 /* Look for optional '( name )'. */
38046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38048 matching_parens parens
;
38049 parens
.consume_open (parser
); /* '(' */
38051 /* We parse the name as an id-expression. If it resolves to
38052 anything other than a non-overloaded function at namespace
38053 scope, it's an error. */
38054 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38055 tree name
= cp_parser_id_expression (parser
,
38056 /*template_keyword_p=*/false,
38057 /*check_dependency_p=*/false,
38058 /*template_p=*/NULL
,
38059 /*declarator_p=*/false,
38060 /*optional_p=*/false);
38061 tree decl
= (identifier_p (name
)
38062 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38064 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38065 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38067 if (decl
== error_mark_node
38068 || !parens
.require_close (parser
))
38070 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38071 parser
->oacc_routine
= NULL
;
38076 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38077 "#pragma acc routine",
38078 cp_lexer_peek_token (parser
->lexer
));
38080 if (decl
&& is_overloaded_fn (decl
)
38081 && (TREE_CODE (decl
) != FUNCTION_DECL
38082 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38084 error_at (name_loc
,
38085 "%<#pragma acc routine%> names a set of overloads");
38086 parser
->oacc_routine
= NULL
;
38090 /* Perhaps we should use the same rule as declarations in different
38092 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38094 error_at (name_loc
,
38095 "%qD does not refer to a namespace scope function", decl
);
38096 parser
->oacc_routine
= NULL
;
38100 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38102 error_at (name_loc
, "%qD does not refer to a function", decl
);
38103 parser
->oacc_routine
= NULL
;
38107 cp_finalize_oacc_routine (parser
, decl
, false);
38108 parser
->oacc_routine
= NULL
;
38110 else /* No optional '( name )'. */
38112 /* Store away all pragma tokens. */
38113 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38114 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38115 cp_lexer_consume_token (parser
->lexer
);
38116 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38117 parser
->oacc_routine
->error_seen
= true;
38118 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38119 struct cp_token_cache
*cp
38120 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38121 parser
->oacc_routine
->tokens
.safe_push (cp
);
38123 /* Emit a helpful diagnostic if there's another pragma following this
38125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38127 cp_ensure_no_oacc_routine (parser
);
38128 data
.tokens
.release ();
38129 /* ..., and then just keep going. */
38133 /* We only have to consider the pragma_external case here. */
38134 cp_parser_declaration (parser
);
38135 if (parser
->oacc_routine
38136 && !parser
->oacc_routine
->fndecl_seen
)
38137 cp_ensure_no_oacc_routine (parser
);
38139 parser
->oacc_routine
= NULL
;
38140 data
.tokens
.release ();
38144 /* Finalize #pragma acc routine clauses after direct declarator has
38148 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38150 struct cp_token_cache
*ce
;
38151 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38153 if (!data
->error_seen
&& data
->fndecl_seen
)
38155 error_at (data
->loc
,
38156 "%<#pragma acc routine%> not immediately followed by "
38157 "a single function declaration or definition");
38158 data
->error_seen
= true;
38160 if (data
->error_seen
)
38163 gcc_checking_assert (data
->tokens
.length () == 1);
38164 ce
= data
->tokens
[0];
38166 cp_parser_push_lexer_for_tokens (parser
, ce
);
38167 parser
->lexer
->in_pragma
= true;
38168 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38170 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38171 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38172 parser
->oacc_routine
->clauses
38173 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38174 "#pragma acc routine", pragma_tok
);
38175 cp_parser_pop_lexer (parser
);
38176 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38182 /* Apply any saved OpenACC routine clauses to a just-parsed
38186 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38188 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38190 /* Keep going if we're in error reporting mode. */
38191 if (parser
->oacc_routine
->error_seen
38192 || fndecl
== error_mark_node
)
38195 if (parser
->oacc_routine
->fndecl_seen
)
38197 error_at (parser
->oacc_routine
->loc
,
38198 "%<#pragma acc routine%> not immediately followed by"
38199 " a single function declaration or definition");
38200 parser
->oacc_routine
= NULL
;
38203 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38205 cp_ensure_no_oacc_routine (parser
);
38209 if (oacc_get_fn_attrib (fndecl
))
38211 error_at (parser
->oacc_routine
->loc
,
38212 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38213 parser
->oacc_routine
= NULL
;
38217 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38219 error_at (parser
->oacc_routine
->loc
,
38221 ? G_("%<#pragma acc routine%> must be applied before use")
38222 : G_("%<#pragma acc routine%> must be applied before "
38224 parser
->oacc_routine
= NULL
;
38228 /* Process the routine's dimension clauses. */
38229 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38230 oacc_replace_fn_attrib (fndecl
, dims
);
38232 /* Add an "omp declare target" attribute. */
38233 DECL_ATTRIBUTES (fndecl
)
38234 = tree_cons (get_identifier ("omp declare target"),
38235 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38237 /* Don't unset parser->oacc_routine here: we may still need it to
38238 diagnose wrong usage. But, remember that we've used this "#pragma acc
38240 parser
->oacc_routine
->fndecl_seen
= true;
38244 /* Main entry point to OpenMP statement pragmas. */
38247 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38250 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38251 omp_clause_mask
mask (0);
38253 switch (cp_parser_pragma_kind (pragma_tok
))
38255 case PRAGMA_OACC_ATOMIC
:
38256 cp_parser_omp_atomic (parser
, pragma_tok
);
38258 case PRAGMA_OACC_CACHE
:
38259 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38261 case PRAGMA_OACC_DATA
:
38262 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38264 case PRAGMA_OACC_ENTER_DATA
:
38265 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38267 case PRAGMA_OACC_EXIT_DATA
:
38268 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38270 case PRAGMA_OACC_HOST_DATA
:
38271 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38273 case PRAGMA_OACC_KERNELS
:
38274 case PRAGMA_OACC_PARALLEL
:
38275 strcpy (p_name
, "#pragma acc");
38276 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38279 case PRAGMA_OACC_LOOP
:
38280 strcpy (p_name
, "#pragma acc");
38281 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38284 case PRAGMA_OACC_UPDATE
:
38285 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38287 case PRAGMA_OACC_WAIT
:
38288 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38290 case PRAGMA_OMP_ATOMIC
:
38291 cp_parser_omp_atomic (parser
, pragma_tok
);
38293 case PRAGMA_OMP_CRITICAL
:
38294 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38296 case PRAGMA_OMP_DISTRIBUTE
:
38297 strcpy (p_name
, "#pragma omp");
38298 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38301 case PRAGMA_OMP_FOR
:
38302 strcpy (p_name
, "#pragma omp");
38303 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38306 case PRAGMA_OMP_MASTER
:
38307 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38309 case PRAGMA_OMP_PARALLEL
:
38310 strcpy (p_name
, "#pragma omp");
38311 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38314 case PRAGMA_OMP_SECTIONS
:
38315 strcpy (p_name
, "#pragma omp");
38316 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38318 case PRAGMA_OMP_SIMD
:
38319 strcpy (p_name
, "#pragma omp");
38320 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38323 case PRAGMA_OMP_SINGLE
:
38324 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38326 case PRAGMA_OMP_TASK
:
38327 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38329 case PRAGMA_OMP_TASKGROUP
:
38330 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38332 case PRAGMA_OMP_TASKLOOP
:
38333 strcpy (p_name
, "#pragma omp");
38334 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38337 case PRAGMA_OMP_TEAMS
:
38338 strcpy (p_name
, "#pragma omp");
38339 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38343 gcc_unreachable ();
38346 protected_set_expr_location (stmt
, pragma_tok
->location
);
38349 /* Transactional Memory parsing routines. */
38351 /* Parse a transaction attribute.
38357 We use this instead of cp_parser_attributes_opt for transactions to avoid
38358 the pedwarn in C++98 mode. */
38361 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38364 tree attr_name
, attr
= NULL
;
38366 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38367 return cp_parser_attributes_opt (parser
);
38369 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38371 cp_lexer_consume_token (parser
->lexer
);
38372 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38375 token
= cp_lexer_peek_token (parser
->lexer
);
38376 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38378 token
= cp_lexer_consume_token (parser
->lexer
);
38380 attr_name
= (token
->type
== CPP_KEYWORD
38381 /* For keywords, use the canonical spelling,
38382 not the parsed identifier. */
38383 ? ridpointers
[(int) token
->keyword
]
38385 attr
= build_tree_list (attr_name
, NULL_TREE
);
38388 cp_parser_error (parser
, "expected identifier");
38390 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38392 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38396 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38398 transaction-statement:
38399 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38401 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38405 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38407 unsigned char old_in
= parser
->in_transaction
;
38408 unsigned char this_in
= 1, new_in
;
38409 enum rid keyword
= token
->keyword
;
38410 tree stmt
, attrs
, noex
;
38412 cp_lexer_consume_token (parser
->lexer
);
38414 if (keyword
== RID_TRANSACTION_RELAXED
38415 || keyword
== RID_SYNCHRONIZED
)
38416 this_in
|= TM_STMT_ATTR_RELAXED
;
38419 attrs
= cp_parser_txn_attribute_opt (parser
);
38421 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38424 /* Parse a noexcept specification. */
38425 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38426 noex
= boolean_true_node
;
38427 else if (keyword
== RID_ATOMIC_CANCEL
)
38429 /* cancel-and-throw is unimplemented. */
38430 sorry ("atomic_cancel");
38434 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38436 /* Keep track if we're in the lexical scope of an outer transaction. */
38437 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38439 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38441 parser
->in_transaction
= new_in
;
38442 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38443 parser
->in_transaction
= old_in
;
38445 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38450 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38452 transaction-expression:
38453 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38454 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38458 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38460 unsigned char old_in
= parser
->in_transaction
;
38461 unsigned char this_in
= 1;
38465 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38467 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38468 || keyword
== RID_TRANSACTION_RELAXED
);
38472 keyword
== RID_TRANSACTION_RELAXED
38473 ? G_("%<__transaction_relaxed%> without transactional memory "
38475 : G_("%<__transaction_atomic%> without transactional memory "
38476 "support enabled"));
38478 token
= cp_parser_require_keyword (parser
, keyword
,
38479 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38480 : RT_TRANSACTION_RELAXED
));
38481 gcc_assert (token
!= NULL
);
38483 if (keyword
== RID_TRANSACTION_RELAXED
)
38484 this_in
|= TM_STMT_ATTR_RELAXED
;
38486 /* Set this early. This might mean that we allow transaction_cancel in
38487 an expression that we find out later actually has to be a constexpr.
38488 However, we expect that cxx_constant_value will be able to deal with
38489 this; also, if the noexcept has no constexpr, then what we parse next
38490 really is a transaction's body. */
38491 parser
->in_transaction
= this_in
;
38493 /* Parse a noexcept specification. */
38494 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38497 if (!noex
|| !noex_expr
38498 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38500 matching_parens parens
;
38501 parens
.require_open (parser
);
38503 expr
= cp_parser_expression (parser
);
38504 expr
= finish_parenthesized_expr (expr
);
38506 parens
.require_close (parser
);
38510 /* The only expression that is available got parsed for the noexcept
38511 already. noexcept is true then. */
38513 noex
= boolean_true_node
;
38516 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38517 parser
->in_transaction
= old_in
;
38519 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38520 return error_mark_node
;
38522 return (flag_tm
? expr
: error_mark_node
);
38525 /* Parse a function-transaction-block.
38527 function-transaction-block:
38528 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38530 __transaction_atomic txn-attribute[opt] function-try-block
38531 __transaction_relaxed ctor-initializer[opt] function-body
38532 __transaction_relaxed function-try-block
38536 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38538 unsigned char old_in
= parser
->in_transaction
;
38539 unsigned char new_in
= 1;
38540 tree compound_stmt
, stmt
, attrs
;
38543 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38544 || keyword
== RID_TRANSACTION_RELAXED
);
38545 token
= cp_parser_require_keyword (parser
, keyword
,
38546 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38547 : RT_TRANSACTION_RELAXED
));
38548 gcc_assert (token
!= NULL
);
38550 if (keyword
== RID_TRANSACTION_RELAXED
)
38551 new_in
|= TM_STMT_ATTR_RELAXED
;
38554 attrs
= cp_parser_txn_attribute_opt (parser
);
38556 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38559 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38561 parser
->in_transaction
= new_in
;
38563 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38564 cp_parser_function_try_block (parser
);
38566 cp_parser_ctor_initializer_opt_and_function_body
38567 (parser
, /*in_function_try_block=*/false);
38569 parser
->in_transaction
= old_in
;
38571 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38574 /* Parse a __transaction_cancel statement.
38577 __transaction_cancel txn-attribute[opt] ;
38578 __transaction_cancel txn-attribute[opt] throw-expression ;
38580 ??? Cancel and throw is not yet implemented. */
38583 cp_parser_transaction_cancel (cp_parser
*parser
)
38586 bool is_outer
= false;
38589 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38590 RT_TRANSACTION_CANCEL
);
38591 gcc_assert (token
!= NULL
);
38593 attrs
= cp_parser_txn_attribute_opt (parser
);
38595 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38597 /* ??? Parse cancel-and-throw here. */
38599 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38603 error_at (token
->location
, "%<__transaction_cancel%> without "
38604 "transactional memory support enabled");
38605 return error_mark_node
;
38607 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38609 error_at (token
->location
, "%<__transaction_cancel%> within a "
38610 "%<__transaction_relaxed%>");
38611 return error_mark_node
;
38615 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38616 && !is_tm_may_cancel_outer (current_function_decl
))
38618 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38619 "within outer %<__transaction_atomic%>");
38620 error_at (token
->location
,
38621 " or a %<transaction_may_cancel_outer%> function");
38622 return error_mark_node
;
38625 else if (parser
->in_transaction
== 0)
38627 error_at (token
->location
, "%<__transaction_cancel%> not within "
38628 "%<__transaction_atomic%>");
38629 return error_mark_node
;
38632 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38640 static GTY (()) cp_parser
*the_parser
;
38643 /* Special handling for the first token or line in the file. The first
38644 thing in the file might be #pragma GCC pch_preprocess, which loads a
38645 PCH file, which is a GC collection point. So we need to handle this
38646 first pragma without benefit of an existing lexer structure.
38648 Always returns one token to the caller in *FIRST_TOKEN. This is
38649 either the true first token of the file, or the first token after
38650 the initial pragma. */
38653 cp_parser_initial_pragma (cp_token
*first_token
)
38657 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38658 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38661 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38662 if (first_token
->type
== CPP_STRING
)
38664 name
= first_token
->u
.value
;
38666 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38667 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38668 error_at (first_token
->location
,
38669 "junk at end of %<#pragma GCC pch_preprocess%>");
38672 error_at (first_token
->location
, "expected string literal");
38674 /* Skip to the end of the pragma. */
38675 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38676 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38678 /* Now actually load the PCH file. */
38680 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38682 /* Read one more token to return to our caller. We have to do this
38683 after reading the PCH file in, since its pointers have to be
38685 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38688 /* Parse a pragma GCC ivdep. */
38691 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38693 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38697 /* Parse a pragma GCC unroll. */
38699 static unsigned short
38700 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38702 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38703 tree expr
= cp_parser_constant_expression (parser
);
38704 unsigned short unroll
;
38705 expr
= maybe_constant_value (expr
);
38706 HOST_WIDE_INT lunroll
= 0;
38707 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38708 || TREE_CODE (expr
) != INTEGER_CST
38709 || (lunroll
= tree_to_shwi (expr
)) < 0
38710 || lunroll
>= USHRT_MAX
)
38712 error_at (location
, "%<#pragma GCC unroll%> requires an"
38713 " assignment-expression that evaluates to a non-negative"
38714 " integral constant less than %u", USHRT_MAX
);
38719 unroll
= (unsigned short)lunroll
;
38723 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38727 /* Normal parsing of a pragma token. Here we can (and must) use the
38731 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38733 cp_token
*pragma_tok
;
38738 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38739 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38740 parser
->lexer
->in_pragma
= true;
38742 id
= cp_parser_pragma_kind (pragma_tok
);
38743 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38744 cp_ensure_no_omp_declare_simd (parser
);
38747 case PRAGMA_GCC_PCH_PREPROCESS
:
38748 error_at (pragma_tok
->location
,
38749 "%<#pragma GCC pch_preprocess%> must be first");
38752 case PRAGMA_OMP_BARRIER
:
38755 case pragma_compound
:
38756 cp_parser_omp_barrier (parser
, pragma_tok
);
38759 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38760 "used in compound statements", "omp barrier");
38767 case PRAGMA_OMP_FLUSH
:
38770 case pragma_compound
:
38771 cp_parser_omp_flush (parser
, pragma_tok
);
38774 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38775 "used in compound statements", "omp flush");
38782 case PRAGMA_OMP_TASKWAIT
:
38785 case pragma_compound
:
38786 cp_parser_omp_taskwait (parser
, pragma_tok
);
38789 error_at (pragma_tok
->location
,
38790 "%<#pragma %s%> may only be used in compound statements",
38798 case PRAGMA_OMP_TASKYIELD
:
38801 case pragma_compound
:
38802 cp_parser_omp_taskyield (parser
, pragma_tok
);
38805 error_at (pragma_tok
->location
,
38806 "%<#pragma %s%> may only be used in compound statements",
38814 case PRAGMA_OMP_CANCEL
:
38817 case pragma_compound
:
38818 cp_parser_omp_cancel (parser
, pragma_tok
);
38821 error_at (pragma_tok
->location
,
38822 "%<#pragma %s%> may only be used in compound statements",
38830 case PRAGMA_OMP_CANCELLATION_POINT
:
38831 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38834 case PRAGMA_OMP_THREADPRIVATE
:
38835 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38838 case PRAGMA_OMP_DECLARE
:
38839 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38841 case PRAGMA_OACC_DECLARE
:
38842 cp_parser_oacc_declare (parser
, pragma_tok
);
38845 case PRAGMA_OACC_ENTER_DATA
:
38846 if (context
== pragma_stmt
)
38848 error_at (pragma_tok
->location
,
38849 "%<#pragma %s%> may only be used in compound statements",
38853 else if (context
!= pragma_compound
)
38855 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38858 case PRAGMA_OACC_EXIT_DATA
:
38859 if (context
== pragma_stmt
)
38861 error_at (pragma_tok
->location
,
38862 "%<#pragma %s%> may only be used in compound statements",
38866 else if (context
!= pragma_compound
)
38868 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38871 case PRAGMA_OACC_ROUTINE
:
38872 if (context
!= pragma_external
)
38874 error_at (pragma_tok
->location
,
38875 "%<#pragma acc routine%> must be at file scope");
38878 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38881 case PRAGMA_OACC_UPDATE
:
38882 if (context
== pragma_stmt
)
38884 error_at (pragma_tok
->location
,
38885 "%<#pragma %s%> may only be used in compound statements",
38889 else if (context
!= pragma_compound
)
38891 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38894 case PRAGMA_OACC_WAIT
:
38895 if (context
== pragma_stmt
)
38897 error_at (pragma_tok
->location
,
38898 "%<#pragma %s%> may only be used in compound statements",
38902 else if (context
!= pragma_compound
)
38904 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38907 case PRAGMA_OACC_ATOMIC
:
38908 case PRAGMA_OACC_CACHE
:
38909 case PRAGMA_OACC_DATA
:
38910 case PRAGMA_OACC_HOST_DATA
:
38911 case PRAGMA_OACC_KERNELS
:
38912 case PRAGMA_OACC_PARALLEL
:
38913 case PRAGMA_OACC_LOOP
:
38914 case PRAGMA_OMP_ATOMIC
:
38915 case PRAGMA_OMP_CRITICAL
:
38916 case PRAGMA_OMP_DISTRIBUTE
:
38917 case PRAGMA_OMP_FOR
:
38918 case PRAGMA_OMP_MASTER
:
38919 case PRAGMA_OMP_PARALLEL
:
38920 case PRAGMA_OMP_SECTIONS
:
38921 case PRAGMA_OMP_SIMD
:
38922 case PRAGMA_OMP_SINGLE
:
38923 case PRAGMA_OMP_TASK
:
38924 case PRAGMA_OMP_TASKGROUP
:
38925 case PRAGMA_OMP_TASKLOOP
:
38926 case PRAGMA_OMP_TEAMS
:
38927 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38929 stmt
= push_omp_privatization_clauses (false);
38930 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38931 pop_omp_privatization_clauses (stmt
);
38934 case PRAGMA_OMP_ORDERED
:
38935 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38937 stmt
= push_omp_privatization_clauses (false);
38938 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38939 pop_omp_privatization_clauses (stmt
);
38942 case PRAGMA_OMP_TARGET
:
38943 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38945 stmt
= push_omp_privatization_clauses (false);
38946 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38947 pop_omp_privatization_clauses (stmt
);
38950 case PRAGMA_OMP_END_DECLARE_TARGET
:
38951 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38954 case PRAGMA_OMP_SECTION
:
38955 error_at (pragma_tok
->location
,
38956 "%<#pragma omp section%> may only be used in "
38957 "%<#pragma omp sections%> construct");
38962 if (context
== pragma_external
)
38964 error_at (pragma_tok
->location
,
38965 "%<#pragma GCC ivdep%> must be inside a function");
38968 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38969 unsigned short unroll
;
38970 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38971 if (tok
->type
== CPP_PRAGMA
38972 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38974 tok
= cp_lexer_consume_token (parser
->lexer
);
38975 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38976 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38980 if (tok
->type
!= CPP_KEYWORD
38981 || (tok
->keyword
!= RID_FOR
38982 && tok
->keyword
!= RID_WHILE
38983 && tok
->keyword
!= RID_DO
))
38985 cp_parser_error (parser
, "for, while or do statement expected");
38988 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38992 case PRAGMA_UNROLL
:
38994 if (context
== pragma_external
)
38996 error_at (pragma_tok
->location
,
38997 "%<#pragma GCC unroll%> must be inside a function");
39000 const unsigned short unroll
39001 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39003 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39004 if (tok
->type
== CPP_PRAGMA
39005 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39007 tok
= cp_lexer_consume_token (parser
->lexer
);
39008 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39009 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39013 if (tok
->type
!= CPP_KEYWORD
39014 || (tok
->keyword
!= RID_FOR
39015 && tok
->keyword
!= RID_WHILE
39016 && tok
->keyword
!= RID_DO
))
39018 cp_parser_error (parser
, "for, while or do statement expected");
39021 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39026 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39027 c_invoke_pragma_handler (id
);
39031 cp_parser_error (parser
, "expected declaration specifiers");
39035 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39039 /* The interface the pragma parsers have to the lexer. */
39042 pragma_lex (tree
*value
, location_t
*loc
)
39044 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39045 enum cpp_ttype ret
= tok
->type
;
39047 *value
= tok
->u
.value
;
39049 *loc
= tok
->location
;
39051 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39053 else if (ret
== CPP_STRING
)
39054 *value
= cp_parser_string_literal (the_parser
, false, false);
39057 if (ret
== CPP_KEYWORD
)
39059 cp_lexer_consume_token (the_parser
->lexer
);
39066 /* External interface. */
39068 /* Parse one entire translation unit. */
39071 c_parse_file (void)
39073 static bool already_called
= false;
39075 if (already_called
)
39076 fatal_error (input_location
,
39077 "inter-module optimizations not implemented for C++");
39078 already_called
= true;
39080 the_parser
= cp_parser_new ();
39081 push_deferring_access_checks (flag_access_control
39082 ? dk_no_deferred
: dk_no_check
);
39083 cp_parser_translation_unit (the_parser
);
39087 /* Create an identifier for a generic parameter type (a synthesized
39088 template parameter implied by `auto' or a concept identifier). */
39090 static GTY(()) int generic_parm_count
;
39092 make_generic_type_name ()
39095 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39096 return get_identifier (buf
);
39099 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39100 (creating a new template parameter list if necessary). Returns the newly
39101 created template type parm. */
39104 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39106 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39108 /* Before committing to modifying any scope, if we're in an
39109 implicit template scope, and we're trying to synthesize a
39110 constrained parameter, try to find a previous parameter with
39111 the same name. This is the same-type rule for abbreviated
39112 function templates.
39114 NOTE: We can generate implicit parameters when tentatively
39115 parsing a nested name specifier, only to reject that parse
39116 later. However, matching the same template-id as part of a
39117 direct-declarator should generate an identical template
39118 parameter, so this rule will merge them. */
39119 if (parser
->implicit_template_scope
&& constr
)
39121 tree t
= parser
->implicit_template_parms
;
39124 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39126 tree d
= TREE_VALUE (t
);
39127 if (TREE_CODE (d
) == PARM_DECL
)
39128 /* Return the TEMPLATE_PARM_INDEX. */
39129 d
= DECL_INITIAL (d
);
39132 t
= TREE_CHAIN (t
);
39136 /* We are either continuing a function template that already contains implicit
39137 template parameters, creating a new fully-implicit function template, or
39138 extending an existing explicit function template with implicit template
39141 cp_binding_level
*const entry_scope
= current_binding_level
;
39143 bool become_template
= false;
39144 cp_binding_level
*parent_scope
= 0;
39146 if (parser
->implicit_template_scope
)
39148 gcc_assert (parser
->implicit_template_parms
);
39150 current_binding_level
= parser
->implicit_template_scope
;
39154 /* Roll back to the existing template parameter scope (in the case of
39155 extending an explicit function template) or introduce a new template
39156 parameter scope ahead of the function parameter scope (or class scope
39157 in the case of out-of-line member definitions). The function scope is
39158 added back after template parameter synthesis below. */
39160 cp_binding_level
*scope
= entry_scope
;
39162 while (scope
->kind
== sk_function_parms
)
39164 parent_scope
= scope
;
39165 scope
= scope
->level_chain
;
39167 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39169 /* If not defining a class, then any class scope is a scope level in
39170 an out-of-line member definition. In this case simply wind back
39171 beyond the first such scope to inject the template parameter list.
39172 Otherwise wind back to the class being defined. The latter can
39173 occur in class member friend declarations such as:
39179 friend void A::foo (auto);
39182 The template parameter list synthesized for the friend declaration
39183 must be injected in the scope of 'B'. This can also occur in
39184 erroneous cases such as:
39190 void B::foo (auto) {}
39193 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39194 but, nevertheless, the template parameter list synthesized for the
39195 declarator should be injected into the scope of 'A' as if the
39196 ill-formed template was specified explicitly. */
39198 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39200 parent_scope
= scope
;
39201 scope
= scope
->level_chain
;
39205 current_binding_level
= scope
;
39207 if (scope
->kind
!= sk_template_parms
39208 || !function_being_declared_is_template_p (parser
))
39210 /* Introduce a new template parameter list for implicit template
39213 become_template
= true;
39215 parser
->implicit_template_scope
39216 = begin_scope (sk_template_parms
, NULL
);
39218 ++processing_template_decl
;
39220 parser
->fully_implicit_function_template_p
= true;
39221 ++parser
->num_template_parameter_lists
;
39225 /* Synthesize implicit template parameters at the end of the explicit
39226 template parameter list. */
39228 gcc_assert (current_template_parms
);
39230 parser
->implicit_template_scope
= scope
;
39232 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39233 parser
->implicit_template_parms
39234 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39238 /* Synthesize a new template parameter and track the current template
39239 parameter chain with implicit_template_parms. */
39241 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39242 tree synth_id
= make_generic_type_name ();
39243 tree synth_tmpl_parm
;
39244 bool non_type
= false;
39246 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39248 = finish_template_type_parm (class_type_node
, synth_id
);
39249 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39251 = finish_constrained_template_template_parm (proto
, synth_id
);
39254 synth_tmpl_parm
= copy_decl (proto
);
39255 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39259 // Attach the constraint to the parm before processing.
39260 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39261 TREE_TYPE (node
) = constr
;
39263 = process_template_parm (parser
->implicit_template_parms
,
39266 /*non_type=*/non_type
,
39267 /*param_pack=*/false);
39269 // Chain the new parameter to the list of implicit parameters.
39270 if (parser
->implicit_template_parms
)
39271 parser
->implicit_template_parms
39272 = TREE_CHAIN (parser
->implicit_template_parms
);
39274 parser
->implicit_template_parms
= new_parm
;
39276 tree new_decl
= get_local_decls ();
39278 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39279 new_decl
= DECL_INITIAL (new_decl
);
39281 /* If creating a fully implicit function template, start the new implicit
39282 template parameter list with this synthesized type, otherwise grow the
39283 current template parameter list. */
39285 if (become_template
)
39287 parent_scope
->level_chain
= current_binding_level
;
39289 tree new_parms
= make_tree_vec (1);
39290 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39291 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39292 new_parms
, current_template_parms
);
39296 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39297 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39298 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39299 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39302 // If the new parameter was constrained, we need to add that to the
39303 // constraints in the template parameter list.
39304 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39306 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39307 reqs
= conjoin_constraints (reqs
, req
);
39308 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39311 current_binding_level
= entry_scope
;
39316 /* Finish the declaration of a fully implicit function template. Such a
39317 template has no explicit template parameter list so has not been through the
39318 normal template head and tail processing. synthesize_implicit_template_parm
39319 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39320 provided if the declaration is a class member such that its template
39321 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39322 form is returned. Otherwise NULL_TREE is returned. */
39325 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39327 gcc_assert (parser
->fully_implicit_function_template_p
);
39329 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39330 && DECL_VIRTUAL_P (member_decl_opt
))
39332 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39333 "implicit templates may not be %<virtual%>");
39334 DECL_VIRTUAL_P (member_decl_opt
) = false;
39337 if (member_decl_opt
)
39338 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39339 end_template_decl ();
39341 parser
->fully_implicit_function_template_p
= false;
39342 parser
->implicit_template_parms
= 0;
39343 parser
->implicit_template_scope
= 0;
39344 --parser
->num_template_parameter_lists
;
39346 return member_decl_opt
;
39349 /* Like finish_fully_implicit_template, but to be used in error
39350 recovery, rearranging scopes so that we restore the state we had
39351 before synthesize_implicit_template_parm inserted the implement
39352 template parms scope. */
39355 abort_fully_implicit_template (cp_parser
*parser
)
39357 cp_binding_level
*return_to_scope
= current_binding_level
;
39359 if (parser
->implicit_template_scope
39360 && return_to_scope
!= parser
->implicit_template_scope
)
39362 cp_binding_level
*child
= return_to_scope
;
39363 for (cp_binding_level
*scope
= child
->level_chain
;
39364 scope
!= parser
->implicit_template_scope
;
39365 scope
= child
->level_chain
)
39367 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39368 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39369 current_binding_level
= parser
->implicit_template_scope
;
39372 return_to_scope
= return_to_scope
->level_chain
;
39374 finish_fully_implicit_template (parser
, NULL
);
39376 gcc_assert (current_binding_level
== return_to_scope
);
39379 /* Helper function for diagnostics that have complained about things
39380 being used with 'extern "C"' linkage.
39382 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39385 maybe_show_extern_c_location (void)
39387 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39388 inform (the_parser
->innermost_linkage_specification_location
,
39389 "%<extern \"C\"%> linkage started here");
39392 #include "gt-cp-parser.h"