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
2238 (cp_parser
*, bool *);
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 *);
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
);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2274 static tree cp_parser_class_head
2275 (cp_parser
*, bool *);
2276 static enum tag_types cp_parser_class_key
2278 static void cp_parser_type_parameter_key
2279 (cp_parser
* parser
);
2280 static void cp_parser_member_specification_opt
2282 static void cp_parser_member_declaration
2284 static tree cp_parser_pure_specifier
2286 static tree cp_parser_constant_initializer
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2293 static tree cp_parser_base_specifier
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2300 static tree cp_parser_conversion_type_id
2302 static cp_declarator
*cp_parser_conversion_declarator_opt
2304 static void cp_parser_ctor_initializer_opt
2306 static void cp_parser_mem_initializer_list
2308 static tree cp_parser_mem_initializer
2310 static tree cp_parser_mem_initializer_id
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2317 static cp_expr cp_parser_operator
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser
*, bool);
2324 static tree cp_parser_template_parameter_list
2326 static tree cp_parser_template_parameter
2327 (cp_parser
*, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser
*, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser
*, bool, bool, enum tag_types
, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2334 static tree cp_parser_template_argument_list
2336 static tree cp_parser_template_argument
2338 static void cp_parser_explicit_instantiation
2340 static void cp_parser_explicit_specialization
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2347 static void cp_parser_function_try_block
2349 static void cp_parser_handler_seq
2351 static void cp_parser_handler
2353 static tree cp_parser_exception_declaration
2355 static tree cp_parser_throw_expression
2357 static tree cp_parser_exception_specification_opt
2359 static tree cp_parser_type_id_list
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2366 static tree cp_parser_asm_operand_list
2368 static tree cp_parser_asm_clobber_list
2370 static tree cp_parser_asm_label_list
2372 static bool cp_next_tokens_can_be_attribute_p
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2376 static bool cp_next_tokens_can_be_std_attribute_p
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser
*, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser
*, size_t);
2384 static tree cp_parser_attributes_opt
2386 static tree cp_parser_gnu_attributes_opt
2388 static tree cp_parser_gnu_attribute_list
2390 static tree cp_parser_std_attribute
2391 (cp_parser
*, tree
);
2392 static tree cp_parser_std_attribute_spec
2394 static tree cp_parser_std_attribute_spec_seq
2396 static bool cp_parser_extension_opt
2397 (cp_parser
*, int *);
2398 static void cp_parser_label_declaration
2401 /* Concept Extensions */
2403 static tree cp_parser_requires_clause
2405 static tree cp_parser_requires_clause_opt
2407 static tree cp_parser_requires_expression
2409 static tree cp_parser_requirement_parameter_list
2411 static tree cp_parser_requirement_body
2413 static tree cp_parser_requirement_list
2415 static tree cp_parser_requirement
2417 static tree cp_parser_simple_requirement
2419 static tree cp_parser_compound_requirement
2421 static tree cp_parser_type_requirement
2423 static tree cp_parser_nested_requirement
2426 /* Transactional Memory Extensions */
2428 static tree cp_parser_transaction
2429 (cp_parser
*, cp_token
*);
2430 static tree cp_parser_transaction_expression
2431 (cp_parser
*, enum rid
);
2432 static void cp_parser_function_transaction
2433 (cp_parser
*, enum rid
);
2434 static tree cp_parser_transaction_cancel
2437 enum pragma_context
{
2444 static bool cp_parser_pragma
2445 (cp_parser
*, enum pragma_context
, bool *);
2447 /* Objective-C++ Productions */
2449 static tree cp_parser_objc_message_receiver
2451 static tree cp_parser_objc_message_args
2453 static tree cp_parser_objc_message_expression
2455 static cp_expr cp_parser_objc_encode_expression
2457 static tree cp_parser_objc_defs_expression
2459 static tree cp_parser_objc_protocol_expression
2461 static tree cp_parser_objc_selector_expression
2463 static cp_expr cp_parser_objc_expression
2465 static bool cp_parser_objc_selector_p
2467 static tree cp_parser_objc_selector
2469 static tree cp_parser_objc_protocol_refs_opt
2471 static void cp_parser_objc_declaration
2472 (cp_parser
*, tree
);
2473 static tree cp_parser_objc_statement
2475 static bool cp_parser_objc_valid_prefix_attributes
2476 (cp_parser
*, tree
*);
2477 static void cp_parser_objc_at_property_declaration
2479 static void cp_parser_objc_at_synthesize_declaration
2481 static void cp_parser_objc_at_dynamic_declaration
2483 static tree cp_parser_objc_struct_declaration
2486 /* Utility Routines */
2488 static cp_expr cp_parser_lookup_name
2489 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2490 static tree cp_parser_lookup_name_simple
2491 (cp_parser
*, tree
, location_t
);
2492 static tree cp_parser_maybe_treat_template_as_class
2494 static bool cp_parser_check_declarator_template_parameters
2495 (cp_parser
*, cp_declarator
*, location_t
);
2496 static bool cp_parser_check_template_parameters
2497 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2498 static cp_expr cp_parser_simple_cast_expression
2500 static tree cp_parser_global_scope_opt
2501 (cp_parser
*, bool);
2502 static bool cp_parser_constructor_declarator_p
2503 (cp_parser
*, bool);
2504 static tree cp_parser_function_definition_from_specifiers_and_declarator
2505 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2506 static tree cp_parser_function_definition_after_declarator
2507 (cp_parser
*, bool);
2508 static bool cp_parser_template_declaration_after_export
2509 (cp_parser
*, bool);
2510 static void cp_parser_perform_template_parameter_access_checks
2511 (vec
<deferred_access_check
, va_gc
> *);
2512 static tree cp_parser_single_declaration
2513 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2514 static cp_expr cp_parser_functional_cast
2515 (cp_parser
*, tree
);
2516 static tree cp_parser_save_member_function_body
2517 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2518 static tree cp_parser_save_nsdmi
2520 static tree cp_parser_enclosed_template_argument_list
2522 static void cp_parser_save_default_args
2523 (cp_parser
*, tree
);
2524 static void cp_parser_late_parsing_for_member
2525 (cp_parser
*, tree
);
2526 static tree cp_parser_late_parse_one_default_arg
2527 (cp_parser
*, tree
, tree
, tree
);
2528 static void cp_parser_late_parsing_nsdmi
2529 (cp_parser
*, tree
);
2530 static void cp_parser_late_parsing_default_args
2531 (cp_parser
*, tree
);
2532 static tree cp_parser_sizeof_operand
2533 (cp_parser
*, enum rid
);
2534 static cp_expr cp_parser_trait_expr
2535 (cp_parser
*, enum rid
);
2536 static bool cp_parser_declares_only_class_p
2538 static void cp_parser_set_storage_class
2539 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2540 static void cp_parser_set_decl_spec_type
2541 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2542 static void set_and_check_decl_spec_loc
2543 (cp_decl_specifier_seq
*decl_specs
,
2544 cp_decl_spec ds
, cp_token
*);
2545 static bool cp_parser_friend_p
2546 (const cp_decl_specifier_seq
*);
2547 static void cp_parser_required_error
2548 (cp_parser
*, required_token
, bool, location_t
);
2549 static cp_token
*cp_parser_require
2550 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2551 static cp_token
*cp_parser_require_keyword
2552 (cp_parser
*, enum rid
, required_token
);
2553 static bool cp_parser_token_starts_function_definition_p
2555 static bool cp_parser_next_token_starts_class_definition_p
2557 static bool cp_parser_next_token_ends_template_argument_p
2559 static bool cp_parser_nth_token_starts_template_argument_list_p
2560 (cp_parser
*, size_t);
2561 static enum tag_types cp_parser_token_is_class_key
2563 static enum tag_types cp_parser_token_is_type_parameter_key
2565 static void cp_parser_check_class_key
2566 (enum tag_types
, tree type
);
2567 static void cp_parser_check_access_in_redeclaration
2568 (tree type
, location_t location
);
2569 static bool cp_parser_optional_template_keyword
2571 static void cp_parser_pre_parsed_nested_name_specifier
2573 static bool cp_parser_cache_group
2574 (cp_parser
*, enum cpp_ttype
, unsigned);
2575 static tree cp_parser_cache_defarg
2576 (cp_parser
*parser
, bool nsdmi
);
2577 static void cp_parser_parse_tentatively
2579 static void cp_parser_commit_to_tentative_parse
2581 static void cp_parser_commit_to_topmost_tentative_parse
2583 static void cp_parser_abort_tentative_parse
2585 static bool cp_parser_parse_definitely
2587 static inline bool cp_parser_parsing_tentatively
2589 static bool cp_parser_uncommitted_to_tentative_parse_p
2591 static void cp_parser_error
2592 (cp_parser
*, const char *);
2593 static void cp_parser_name_lookup_error
2594 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2595 static bool cp_parser_simulate_error
2597 static bool cp_parser_check_type_definition
2599 static void cp_parser_check_for_definition_in_return_type
2600 (cp_declarator
*, tree
, location_t type_location
);
2601 static void cp_parser_check_for_invalid_template_id
2602 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2603 static bool cp_parser_non_integral_constant_expression
2604 (cp_parser
*, non_integral_constant
);
2605 static void cp_parser_diagnose_invalid_type_name
2606 (cp_parser
*, tree
, location_t
);
2607 static bool cp_parser_parse_and_diagnose_invalid_type_name
2609 static int cp_parser_skip_to_closing_parenthesis
2610 (cp_parser
*, bool, bool, bool);
2611 static void cp_parser_skip_to_end_of_statement
2613 static void cp_parser_consume_semicolon_at_end_of_statement
2615 static void cp_parser_skip_to_end_of_block_or_statement
2617 static bool cp_parser_skip_to_closing_brace
2619 static void cp_parser_skip_to_end_of_template_parameter_list
2621 static void cp_parser_skip_to_pragma_eol
2622 (cp_parser
*, cp_token
*);
2623 static bool cp_parser_error_occurred
2625 static bool cp_parser_allow_gnu_extensions_p
2627 static bool cp_parser_is_pure_string_literal
2629 static bool cp_parser_is_string_literal
2631 static bool cp_parser_is_keyword
2632 (cp_token
*, enum rid
);
2633 static tree cp_parser_make_typename_type
2634 (cp_parser
*, tree
, location_t location
);
2635 static cp_declarator
* cp_parser_make_indirect_declarator
2636 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2637 static bool cp_parser_compound_literal_p
2639 static bool cp_parser_array_designator_p
2641 static bool cp_parser_init_statement_p
2643 static bool cp_parser_skip_to_closing_square_bracket
2646 /* Concept-related syntactic transformations */
2648 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2649 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2651 // -------------------------------------------------------------------------- //
2652 // Unevaluated Operand Guard
2654 // Implementation of an RAII helper for unevaluated operand parsing.
2655 cp_unevaluated::cp_unevaluated ()
2657 ++cp_unevaluated_operand
;
2658 ++c_inhibit_evaluation_warnings
;
2661 cp_unevaluated::~cp_unevaluated ()
2663 --c_inhibit_evaluation_warnings
;
2664 --cp_unevaluated_operand
;
2667 // -------------------------------------------------------------------------- //
2668 // Tentative Parsing
2670 /* Returns nonzero if we are parsing tentatively. */
2673 cp_parser_parsing_tentatively (cp_parser
* parser
)
2675 return parser
->context
->next
!= NULL
;
2678 /* Returns nonzero if TOKEN is a string literal. */
2681 cp_parser_is_pure_string_literal (cp_token
* token
)
2683 return (token
->type
== CPP_STRING
||
2684 token
->type
== CPP_STRING16
||
2685 token
->type
== CPP_STRING32
||
2686 token
->type
== CPP_WSTRING
||
2687 token
->type
== CPP_UTF8STRING
);
2690 /* Returns nonzero if TOKEN is a string literal
2691 of a user-defined string literal. */
2694 cp_parser_is_string_literal (cp_token
* token
)
2696 return (cp_parser_is_pure_string_literal (token
) ||
2697 token
->type
== CPP_STRING_USERDEF
||
2698 token
->type
== CPP_STRING16_USERDEF
||
2699 token
->type
== CPP_STRING32_USERDEF
||
2700 token
->type
== CPP_WSTRING_USERDEF
||
2701 token
->type
== CPP_UTF8STRING_USERDEF
);
2704 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2707 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2709 return token
->keyword
== keyword
;
2712 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 static enum pragma_kind
2716 cp_parser_pragma_kind (cp_token
*token
)
2718 if (token
->type
!= CPP_PRAGMA
)
2720 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2721 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2724 /* Helper function for cp_parser_error.
2725 Having peeked a token of kind TOK1_KIND that might signify
2726 a conflict marker, peek successor tokens to determine
2727 if we actually do have a conflict marker.
2728 Specifically, we consider a run of 7 '<', '=' or '>' characters
2729 at the start of a line as a conflict marker.
2730 These come through the lexer as three pairs and a single,
2731 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2732 If it returns true, *OUT_LOC is written to with the location/range
2736 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2737 location_t
*out_loc
)
2739 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2740 if (token2
->type
!= tok1_kind
)
2742 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2743 if (token3
->type
!= tok1_kind
)
2745 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2746 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2749 /* It must be at the start of the line. */
2750 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2751 if (LOCATION_COLUMN (start_loc
) != 1)
2754 /* We have a conflict marker. Construct a location of the form:
2757 with start == caret, finishing at the end of the marker. */
2758 location_t finish_loc
= get_finish (token4
->location
);
2759 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2764 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2768 get_matching_symbol (required_token token_desc
)
2775 case RT_CLOSE_BRACE
:
2777 case RT_CLOSE_PAREN
:
2782 /* Attempt to convert TOKEN_DESC from a required_token to an
2783 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785 static enum cpp_ttype
2786 get_required_cpp_ttype (required_token token_desc
)
2791 return CPP_SEMICOLON
;
2793 return CPP_OPEN_PAREN
;
2794 case RT_CLOSE_BRACE
:
2795 return CPP_CLOSE_BRACE
;
2797 return CPP_OPEN_BRACE
;
2798 case RT_CLOSE_SQUARE
:
2799 return CPP_CLOSE_SQUARE
;
2800 case RT_OPEN_SQUARE
:
2801 return CPP_OPEN_SQUARE
;
2806 case RT_CLOSE_PAREN
:
2807 return CPP_CLOSE_PAREN
;
2810 /* Use CPP_EOF as a "no completions possible" code. */
2816 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818 Issue a diagnostic of the form
2819 FILE:LINE: MESSAGE before TOKEN
2820 where TOKEN is the next token in the input stream. MESSAGE
2821 (specified by the caller) is usually of the form "expected
2824 This bypasses the check for tentative passing, and potentially
2825 adds material needed by cp_parser_required_error.
2827 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2828 suggesting insertion of the missing token.
2830 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2831 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2835 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2836 required_token missing_token_desc
,
2837 location_t matching_location
)
2839 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2840 /* This diagnostic makes more sense if it is tagged to the line
2841 of the token we just peeked at. */
2842 cp_lexer_set_source_position_from_token (token
);
2844 if (token
->type
== CPP_PRAGMA
)
2846 error_at (token
->location
,
2847 "%<#pragma%> is not allowed here");
2848 cp_parser_skip_to_pragma_eol (parser
, token
);
2852 /* If this is actually a conflict marker, report it as such. */
2853 if (token
->type
== CPP_LSHIFT
2854 || token
->type
== CPP_RSHIFT
2855 || token
->type
== CPP_EQ_EQ
)
2858 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2860 error_at (loc
, "version control conflict marker in file");
2865 gcc_rich_location
richloc (input_location
);
2867 bool added_matching_location
= false;
2869 if (missing_token_desc
!= RT_NONE
)
2871 /* Potentially supply a fix-it hint, suggesting to add the
2872 missing token immediately after the *previous* token.
2873 This may move the primary location within richloc. */
2874 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2875 location_t prev_token_loc
2876 = cp_lexer_previous_token (parser
->lexer
)->location
;
2877 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2879 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2880 Attempt to consolidate diagnostics by printing it as a
2881 secondary range within the main diagnostic. */
2882 if (matching_location
!= UNKNOWN_LOCATION
)
2883 added_matching_location
2884 = richloc
.add_location_if_nearby (matching_location
);
2887 /* Actually emit the error. */
2888 c_parse_error (gmsgid
,
2889 /* Because c_parser_error does not understand
2890 CPP_KEYWORD, keywords are treated like
2892 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2893 token
->u
.value
, token
->flags
, &richloc
);
2895 if (missing_token_desc
!= RT_NONE
)
2897 /* If we weren't able to consolidate matching_location, then
2898 print it as a secondary diagnostic. */
2899 if (matching_location
!= UNKNOWN_LOCATION
2900 && !added_matching_location
)
2901 inform (matching_location
, "to match this %qs",
2902 get_matching_symbol (missing_token_desc
));
2906 /* If not parsing tentatively, issue a diagnostic of the form
2907 FILE:LINE: MESSAGE before TOKEN
2908 where TOKEN is the next token in the input stream. MESSAGE
2909 (specified by the caller) is usually of the form "expected
2913 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2915 if (!cp_parser_simulate_error (parser
))
2916 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2919 /* Issue an error about name-lookup failing. NAME is the
2920 IDENTIFIER_NODE DECL is the result of
2921 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2922 the thing that we hoped to find. */
2925 cp_parser_name_lookup_error (cp_parser
* parser
,
2928 name_lookup_error desired
,
2929 location_t location
)
2931 /* If name lookup completely failed, tell the user that NAME was not
2933 if (decl
== error_mark_node
)
2935 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2936 error_at (location
, "%<%E::%E%> has not been declared",
2937 parser
->scope
, name
);
2938 else if (parser
->scope
== global_namespace
)
2939 error_at (location
, "%<::%E%> has not been declared", name
);
2940 else if (parser
->object_scope
2941 && !CLASS_TYPE_P (parser
->object_scope
))
2942 error_at (location
, "request for member %qE in non-class type %qT",
2943 name
, parser
->object_scope
);
2944 else if (parser
->object_scope
)
2945 error_at (location
, "%<%T::%E%> has not been declared",
2946 parser
->object_scope
, name
);
2948 error_at (location
, "%qE has not been declared", name
);
2950 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2955 error_at (location
, "%<%E::%E%> is not a type",
2956 parser
->scope
, name
);
2959 error_at (location
, "%<%E::%E%> is not a class or namespace",
2960 parser
->scope
, name
);
2964 "%<%E::%E%> is not a class, namespace, or enumeration",
2965 parser
->scope
, name
);
2972 else if (parser
->scope
== global_namespace
)
2977 error_at (location
, "%<::%E%> is not a type", name
);
2980 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2984 "%<::%E%> is not a class, namespace, or enumeration",
2996 error_at (location
, "%qE is not a type", name
);
2999 error_at (location
, "%qE is not a class or namespace", name
);
3003 "%qE is not a class, namespace, or enumeration", name
);
3011 /* If we are parsing tentatively, remember that an error has occurred
3012 during this tentative parse. Returns true if the error was
3013 simulated; false if a message should be issued by the caller. */
3016 cp_parser_simulate_error (cp_parser
* parser
)
3018 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3020 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3026 /* This function is called when a type is defined. If type
3027 definitions are forbidden at this point, an error message is
3031 cp_parser_check_type_definition (cp_parser
* parser
)
3033 /* If types are forbidden here, issue a message. */
3034 if (parser
->type_definition_forbidden_message
)
3036 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3037 in the message need to be interpreted. */
3038 error (parser
->type_definition_forbidden_message
);
3044 /* This function is called when the DECLARATOR is processed. The TYPE
3045 was a type defined in the decl-specifiers. If it is invalid to
3046 define a type in the decl-specifiers for DECLARATOR, an error is
3047 issued. TYPE_LOCATION is the location of TYPE and is used
3048 for error reporting. */
3051 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3052 tree type
, location_t type_location
)
3054 /* [dcl.fct] forbids type definitions in return types.
3055 Unfortunately, it's not easy to know whether or not we are
3056 processing a return type until after the fact. */
3058 && (declarator
->kind
== cdk_pointer
3059 || declarator
->kind
== cdk_reference
3060 || declarator
->kind
== cdk_ptrmem
))
3061 declarator
= declarator
->declarator
;
3063 && declarator
->kind
== cdk_function
)
3065 error_at (type_location
,
3066 "new types may not be defined in a return type");
3067 inform (type_location
,
3068 "(perhaps a semicolon is missing after the definition of %qT)",
3073 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3074 "<" in any valid C++ program. If the next token is indeed "<",
3075 issue a message warning the user about what appears to be an
3076 invalid attempt to form a template-id. LOCATION is the location
3077 of the type-specifier (TYPE) */
3080 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3082 enum tag_types tag_type
,
3083 location_t location
)
3085 cp_token_position start
= 0;
3087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3089 if (TREE_CODE (type
) == TYPE_DECL
)
3090 type
= TREE_TYPE (type
);
3091 if (TYPE_P (type
) && !template_placeholder_p (type
))
3092 error_at (location
, "%qT is not a template", type
);
3093 else if (identifier_p (type
))
3095 if (tag_type
!= none_type
)
3096 error_at (location
, "%qE is not a class template", type
);
3098 error_at (location
, "%qE is not a template", type
);
3101 error_at (location
, "invalid template-id");
3102 /* Remember the location of the invalid "<". */
3103 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3104 start
= cp_lexer_token_position (parser
->lexer
, true);
3105 /* Consume the "<". */
3106 cp_lexer_consume_token (parser
->lexer
);
3107 /* Parse the template arguments. */
3108 cp_parser_enclosed_template_argument_list (parser
);
3109 /* Permanently remove the invalid template arguments so that
3110 this error message is not issued again. */
3112 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3116 /* If parsing an integral constant-expression, issue an error message
3117 about the fact that THING appeared and return true. Otherwise,
3118 return false. In either case, set
3119 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3122 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3123 non_integral_constant thing
)
3125 parser
->non_integral_constant_expression_p
= true;
3126 if (parser
->integral_constant_expression_p
)
3128 if (!parser
->allow_non_integral_constant_expression_p
)
3130 const char *msg
= NULL
;
3134 pedwarn (input_location
, OPT_Wpedantic
,
3135 "ISO C++ forbids using a floating-point literal "
3136 "in a constant-expression");
3139 error ("a cast to a type other than an integral or "
3140 "enumeration type cannot appear in a "
3141 "constant-expression");
3144 error ("%<typeid%> operator "
3145 "cannot appear in a constant-expression");
3148 error ("non-constant compound literals "
3149 "cannot appear in a constant-expression");
3152 error ("a function call "
3153 "cannot appear in a constant-expression");
3156 error ("an increment "
3157 "cannot appear in a constant-expression");
3160 error ("an decrement "
3161 "cannot appear in a constant-expression");
3164 error ("an array reference "
3165 "cannot appear in a constant-expression");
3167 case NIC_ADDR_LABEL
:
3168 error ("the address of a label "
3169 "cannot appear in a constant-expression");
3171 case NIC_OVERLOADED
:
3172 error ("calls to overloaded operators "
3173 "cannot appear in a constant-expression");
3175 case NIC_ASSIGNMENT
:
3176 error ("an assignment cannot appear in a constant-expression");
3179 error ("a comma operator "
3180 "cannot appear in a constant-expression");
3182 case NIC_CONSTRUCTOR
:
3183 error ("a call to a constructor "
3184 "cannot appear in a constant-expression");
3186 case NIC_TRANSACTION
:
3187 error ("a transaction expression "
3188 "cannot appear in a constant-expression");
3194 msg
= "__FUNCTION__";
3196 case NIC_PRETTY_FUNC
:
3197 msg
= "__PRETTY_FUNCTION__";
3217 case NIC_PREINCREMENT
:
3220 case NIC_PREDECREMENT
:
3233 error ("%qs cannot appear in a constant-expression", msg
);
3240 /* Emit a diagnostic for an invalid type name. This function commits
3241 to the current active tentative parse, if any. (Otherwise, the
3242 problematic construct might be encountered again later, resulting
3243 in duplicate error messages.) LOCATION is the location of ID. */
3246 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3247 location_t location
)
3249 tree decl
, ambiguous_decls
;
3250 cp_parser_commit_to_tentative_parse (parser
);
3251 /* Try to lookup the identifier. */
3252 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3253 /*is_template=*/false,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3256 &ambiguous_decls
, location
);
3257 if (ambiguous_decls
)
3258 /* If the lookup was ambiguous, an error will already have
3261 /* If the lookup found a template-name, it means that the user forgot
3262 to specify an argument list. Emit a useful error message. */
3263 if (DECL_TYPE_TEMPLATE_P (decl
))
3266 "invalid use of template-name %qE without an argument list",
3268 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3269 inform (location
, "class template argument deduction is only available "
3270 "with -std=c++17 or -std=gnu++17");
3271 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3273 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3274 error_at (location
, "invalid use of destructor %qD as a type", id
);
3275 else if (TREE_CODE (decl
) == TYPE_DECL
)
3276 /* Something like 'unsigned A a;' */
3277 error_at (location
, "invalid combination of multiple type-specifiers");
3278 else if (!parser
->scope
)
3280 /* Issue an error message. */
3282 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3283 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3286 gcc_rich_location
richloc (location
);
3287 richloc
.add_fixit_replace (hint
.suggestion ());
3289 "%qE does not name a type; did you mean %qs?",
3290 id
, hint
.suggestion ());
3293 error_at (location
, "%qE does not name a type", id
);
3294 /* If we're in a template class, it's possible that the user was
3295 referring to a type from a base class. For example:
3297 template <typename T> struct A { typedef T X; };
3298 template <typename T> struct B : public A<T> { X x; };
3300 The user should have said "typename A<T>::X". */
3301 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3302 inform (location
, "C++11 %<constexpr%> only available with "
3303 "-std=c++11 or -std=gnu++11");
3304 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3305 inform (location
, "C++11 %<noexcept%> only available with "
3306 "-std=c++11 or -std=gnu++11");
3307 else if (cxx_dialect
< cxx11
3308 && TREE_CODE (id
) == IDENTIFIER_NODE
3309 && id_equal (id
, "thread_local"))
3310 inform (location
, "C++11 %<thread_local%> only available with "
3311 "-std=c++11 or -std=gnu++11");
3312 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3313 inform (location
, "%<concept%> only available with -fconcepts");
3314 else if (processing_template_decl
&& current_class_type
3315 && TYPE_BINFO (current_class_type
))
3319 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3323 tree base_type
= BINFO_TYPE (b
);
3324 if (CLASS_TYPE_P (base_type
)
3325 && dependent_type_p (base_type
))
3328 /* Go from a particular instantiation of the
3329 template (which will have an empty TYPE_FIELDs),
3330 to the main version. */
3331 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3332 for (field
= TYPE_FIELDS (base_type
);
3334 field
= DECL_CHAIN (field
))
3335 if (TREE_CODE (field
) == TYPE_DECL
3336 && DECL_NAME (field
) == id
)
3339 "(perhaps %<typename %T::%E%> was intended)",
3340 BINFO_TYPE (b
), id
);
3349 /* Here we diagnose qualified-ids where the scope is actually correct,
3350 but the identifier does not resolve to a valid type name. */
3351 else if (parser
->scope
!= error_mark_node
)
3353 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3355 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3356 error_at (location_of (id
),
3357 "%qE in namespace %qE does not name a template type",
3359 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3362 TREE_OPERAND (id
, 0), parser
->scope
);
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a type",
3368 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3369 else if (decl
== error_mark_node
)
3370 suggest_alternative_in_explicit_scope (location
, id
,
3373 else if (CLASS_TYPE_P (parser
->scope
)
3374 && constructor_name_p (id
, parser
->scope
))
3377 error_at (location
, "%<%T::%E%> names the constructor, not"
3378 " the type", parser
->scope
, id
);
3379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3380 error_at (location
, "and %qT has no template constructors",
3383 else if (TYPE_P (parser
->scope
)
3384 && dependent_scope_p (parser
->scope
))
3386 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3388 "need %<typename%> before %<%T::%D::%E%> because "
3389 "%<%T::%D%> is a dependent scope",
3390 TYPE_CONTEXT (parser
->scope
),
3391 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3393 TYPE_CONTEXT (parser
->scope
),
3394 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3396 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3397 "%qT is a dependent scope",
3398 parser
->scope
, id
, parser
->scope
);
3400 else if (TYPE_P (parser
->scope
))
3402 if (!COMPLETE_TYPE_P (parser
->scope
))
3403 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3405 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3406 error_at (location_of (id
),
3407 "%qE in %q#T does not name a template type",
3409 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3412 TREE_OPERAND (id
, 0), parser
->scope
);
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a type",
3418 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3425 /* Check for a common situation where a type-name should be present,
3426 but is not, and issue a sensible error message. Returns true if an
3427 invalid type-name was detected.
3429 The situation handled by this function are variable declarations of the
3430 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3431 Usually, `ID' should name a type, but if we got here it means that it
3432 does not. We try to emit the best possible error message depending on
3433 how exactly the id-expression looks like. */
3436 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3441 /* Avoid duplicate error about ambiguous lookup. */
3442 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3444 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3445 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3449 cp_parser_parse_tentatively (parser
);
3450 id
= cp_parser_id_expression (parser
,
3451 /*template_keyword_p=*/false,
3452 /*check_dependency_p=*/true,
3453 /*template_p=*/NULL
,
3454 /*declarator_p=*/true,
3455 /*optional_p=*/false);
3456 /* If the next token is a (, this is a function with no explicit return
3457 type, i.e. constructor, destructor or conversion op. */
3458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3459 || TREE_CODE (id
) == TYPE_DECL
)
3461 cp_parser_abort_tentative_parse (parser
);
3464 if (!cp_parser_parse_definitely (parser
))
3467 /* Emit a diagnostic for the invalid type. */
3468 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3470 /* If we aren't in the middle of a declarator (i.e. in a
3471 parameter-declaration-clause), skip to the end of the declaration;
3472 there's no point in trying to process it. */
3473 if (!parser
->in_declarator_p
)
3474 cp_parser_skip_to_end_of_block_or_statement (parser
);
3478 /* Consume tokens up to, and including, the next non-nested closing `)'.
3479 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3480 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3481 found an unnested token of that type. */
3484 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3489 unsigned paren_depth
= 0;
3490 unsigned brace_depth
= 0;
3491 unsigned square_depth
= 0;
3493 if (recovering
&& or_ttype
== CPP_EOF
3494 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3499 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3501 /* Have we found what we're looking for before the closing paren? */
3502 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3503 && !brace_depth
&& !paren_depth
&& !square_depth
)
3506 switch (token
->type
)
3509 case CPP_PRAGMA_EOL
:
3510 /* If we've run out of tokens, then there is no closing `)'. */
3513 /* This is good for lambda expression capture-lists. */
3514 case CPP_OPEN_SQUARE
:
3517 case CPP_CLOSE_SQUARE
:
3518 if (!square_depth
--)
3523 /* This matches the processing in skip_to_end_of_statement. */
3528 case CPP_OPEN_BRACE
:
3531 case CPP_CLOSE_BRACE
:
3536 case CPP_OPEN_PAREN
:
3541 case CPP_CLOSE_PAREN
:
3542 if (!brace_depth
&& !paren_depth
--)
3545 cp_lexer_consume_token (parser
->lexer
);
3554 /* Consume the token. */
3555 cp_lexer_consume_token (parser
->lexer
);
3559 /* Consume tokens up to, and including, the next non-nested closing `)'.
3560 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3561 are doing error recovery. Returns -1 if OR_COMMA is true and we
3562 found an unnested token of that type. */
3565 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3570 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3571 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3572 ttype
, consume_paren
);
3575 /* Consume tokens until we reach the end of the current statement.
3576 Normally, that will be just before consuming a `;'. However, if a
3577 non-nested `}' comes first, then we stop before consuming that. */
3580 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3582 unsigned nesting_depth
= 0;
3584 /* Unwind generic function template scope if necessary. */
3585 if (parser
->fully_implicit_function_template_p
)
3586 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3592 switch (token
->type
)
3595 case CPP_PRAGMA_EOL
:
3596 /* If we've run out of tokens, stop. */
3600 /* If the next token is a `;', we have reached the end of the
3606 case CPP_CLOSE_BRACE
:
3607 /* If this is a non-nested '}', stop before consuming it.
3608 That way, when confronted with something like:
3612 we stop before consuming the closing '}', even though we
3613 have not yet reached a `;'. */
3614 if (nesting_depth
== 0)
3617 /* If it is the closing '}' for a block that we have
3618 scanned, stop -- but only after consuming the token.
3624 we will stop after the body of the erroneously declared
3625 function, but before consuming the following `typedef'
3627 if (--nesting_depth
== 0)
3629 cp_lexer_consume_token (parser
->lexer
);
3634 case CPP_OPEN_BRACE
:
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser
->lexer
);
3647 /* This function is called at the end of a statement or declaration.
3648 If the next token is a semicolon, it is consumed; otherwise, error
3649 recovery is attempted. */
3652 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3654 /* Look for the trailing `;'. */
3655 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3657 /* If there is additional (erroneous) input, skip to the end of
3659 cp_parser_skip_to_end_of_statement (parser
);
3660 /* If the next token is now a `;', consume it. */
3661 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3662 cp_lexer_consume_token (parser
->lexer
);
3666 /* Skip tokens until we have consumed an entire block, or until we
3667 have consumed a non-nested `;'. */
3670 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3672 int nesting_depth
= 0;
3674 /* Unwind generic function template scope if necessary. */
3675 if (parser
->fully_implicit_function_template_p
)
3676 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3678 while (nesting_depth
>= 0)
3680 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3682 switch (token
->type
)
3685 case CPP_PRAGMA_EOL
:
3686 /* If we've run out of tokens, stop. */
3690 /* Stop if this is an unnested ';'. */
3695 case CPP_CLOSE_BRACE
:
3696 /* Stop if this is an unnested '}', or closes the outermost
3699 if (nesting_depth
< 0)
3705 case CPP_OPEN_BRACE
:
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser
->lexer
);
3719 /* Skip tokens until a non-nested closing curly brace is the next
3720 token, or there are no more tokens. Return true in the first case,
3724 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3726 unsigned nesting_depth
= 0;
3730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3732 switch (token
->type
)
3735 case CPP_PRAGMA_EOL
:
3736 /* If we've run out of tokens, stop. */
3739 case CPP_CLOSE_BRACE
:
3740 /* If the next token is a non-nested `}', then we have reached
3741 the end of the current block. */
3742 if (nesting_depth
-- == 0)
3746 case CPP_OPEN_BRACE
:
3747 /* If it the next token is a `{', then we are entering a new
3748 block. Consume the entire block. */
3756 /* Consume the token. */
3757 cp_lexer_consume_token (parser
->lexer
);
3761 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3762 parameter is the PRAGMA token, allowing us to purge the entire pragma
3766 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3770 parser
->lexer
->in_pragma
= false;
3773 token
= cp_lexer_consume_token (parser
->lexer
);
3774 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3776 /* Ensure that the pragma is not parsed again. */
3777 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3780 /* Require pragma end of line, resyncing with it as necessary. The
3781 arguments are as for cp_parser_skip_to_pragma_eol. */
3784 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3786 parser
->lexer
->in_pragma
= false;
3787 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3788 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3791 /* This is a simple wrapper around make_typename_type. When the id is
3792 an unresolved identifier node, we can provide a superior diagnostic
3793 using cp_parser_diagnose_invalid_type_name. */
3796 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3797 location_t id_location
)
3800 if (identifier_p (id
))
3802 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3803 /*complain=*/tf_none
);
3804 if (result
== error_mark_node
)
3805 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3808 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3811 /* This is a wrapper around the
3812 make_{pointer,ptrmem,reference}_declarator functions that decides
3813 which one to call based on the CODE and CLASS_TYPE arguments. The
3814 CODE argument should be one of the values returned by
3815 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3816 appertain to the pointer or reference. */
3818 static cp_declarator
*
3819 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3820 cp_cv_quals cv_qualifiers
,
3821 cp_declarator
*target
,
3824 if (code
== ERROR_MARK
)
3825 return cp_error_declarator
;
3827 if (code
== INDIRECT_REF
)
3828 if (class_type
== NULL_TREE
)
3829 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3831 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3832 target
, attributes
);
3833 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3834 return make_reference_declarator (cv_qualifiers
, target
,
3836 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3837 return make_reference_declarator (cv_qualifiers
, target
,
3842 /* Create a new C++ parser. */
3845 cp_parser_new (void)
3851 /* cp_lexer_new_main is called before doing GC allocation because
3852 cp_lexer_new_main might load a PCH file. */
3853 lexer
= cp_lexer_new_main ();
3855 /* Initialize the binops_by_token so that we can get the tree
3856 directly from the token. */
3857 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3858 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3860 parser
= ggc_cleared_alloc
<cp_parser
> ();
3861 parser
->lexer
= lexer
;
3862 parser
->context
= cp_parser_context_new (NULL
);
3864 /* For now, we always accept GNU extensions. */
3865 parser
->allow_gnu_extensions_p
= 1;
3867 /* The `>' token is a greater-than operator, not the end of a
3869 parser
->greater_than_is_operator_p
= true;
3871 parser
->default_arg_ok_p
= true;
3873 /* We are not parsing a constant-expression. */
3874 parser
->integral_constant_expression_p
= false;
3875 parser
->allow_non_integral_constant_expression_p
= false;
3876 parser
->non_integral_constant_expression_p
= false;
3878 /* Local variable names are not forbidden. */
3879 parser
->local_variables_forbidden_p
= false;
3881 /* We are not processing an `extern "C"' declaration. */
3882 parser
->in_unbraced_linkage_specification_p
= false;
3884 /* We are not processing a declarator. */
3885 parser
->in_declarator_p
= false;
3887 /* We are not processing a template-argument-list. */
3888 parser
->in_template_argument_list_p
= false;
3890 /* We are not in an iteration statement. */
3891 parser
->in_statement
= 0;
3893 /* We are not in a switch statement. */
3894 parser
->in_switch_statement_p
= false;
3896 /* We are not parsing a type-id inside an expression. */
3897 parser
->in_type_id_in_expr_p
= false;
3899 /* Declarations aren't implicitly extern "C". */
3900 parser
->implicit_extern_c
= false;
3902 /* String literals should be translated to the execution character set. */
3903 parser
->translate_strings_p
= true;
3905 /* We are not parsing a function body. */
3906 parser
->in_function_body
= false;
3908 /* We can correct until told otherwise. */
3909 parser
->colon_corrects_to_scope_p
= true;
3911 /* The unparsed function queue is empty. */
3912 push_unparsed_function_queues (parser
);
3914 /* There are no classes being defined. */
3915 parser
->num_classes_being_defined
= 0;
3917 /* No template parameters apply. */
3918 parser
->num_template_parameter_lists
= 0;
3920 /* Special parsing data structures. */
3921 parser
->omp_declare_simd
= NULL
;
3922 parser
->oacc_routine
= NULL
;
3924 /* Not declaring an implicit function template. */
3925 parser
->auto_is_implicit_function_template_parm_p
= false;
3926 parser
->fully_implicit_function_template_p
= false;
3927 parser
->implicit_template_parms
= 0;
3928 parser
->implicit_template_scope
= 0;
3930 /* Allow constrained-type-specifiers. */
3931 parser
->prevent_constrained_type_specifiers
= 0;
3933 /* We haven't yet seen an 'extern "C"'. */
3934 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3939 /* Create a cp_lexer structure which will emit the tokens in CACHE
3940 and push it onto the parser's lexer stack. This is used for delayed
3941 parsing of in-class method bodies and default arguments, and should
3942 not be confused with tentative parsing. */
3944 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3946 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3947 lexer
->next
= parser
->lexer
;
3948 parser
->lexer
= lexer
;
3950 /* Move the current source position to that of the first token in the
3952 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3955 /* Pop the top lexer off the parser stack. This is never used for the
3956 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3958 cp_parser_pop_lexer (cp_parser
*parser
)
3960 cp_lexer
*lexer
= parser
->lexer
;
3961 parser
->lexer
= lexer
->next
;
3962 cp_lexer_destroy (lexer
);
3964 /* Put the current source position back where it was before this
3965 lexer was pushed. */
3966 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3969 /* Lexical conventions [gram.lex] */
3971 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3975 cp_parser_identifier (cp_parser
* parser
)
3979 /* Look for the identifier. */
3980 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3981 /* Return the value. */
3983 return cp_expr (token
->u
.value
, token
->location
);
3985 return error_mark_node
;
3988 /* Parse a sequence of adjacent string constants. Returns a
3989 TREE_STRING representing the combined, nul-terminated string
3990 constant. If TRANSLATE is true, translate the string to the
3991 execution character set. If WIDE_OK is true, a wide string is
3994 C++98 [lex.string] says that if a narrow string literal token is
3995 adjacent to a wide string literal token, the behavior is undefined.
3996 However, C99 6.4.5p4 says that this results in a wide string literal.
3997 We follow C99 here, for consistency with the C front end.
3999 This code is largely lifted from lex_string() in c-lex.c.
4001 FUTURE: ObjC++ will need to handle @-strings here. */
4003 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4004 bool lookup_udlit
= true)
4008 struct obstack str_ob
;
4009 cpp_string str
, istr
, *strs
;
4011 enum cpp_ttype type
, curr_type
;
4012 int have_suffix_p
= 0;
4014 tree suffix_id
= NULL_TREE
;
4015 bool curr_tok_is_userdef_p
= false;
4017 tok
= cp_lexer_peek_token (parser
->lexer
);
4018 if (!cp_parser_is_string_literal (tok
))
4020 cp_parser_error (parser
, "expected string-literal");
4021 return error_mark_node
;
4024 location_t loc
= tok
->location
;
4026 if (cpp_userdef_string_p (tok
->type
))
4028 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4029 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4030 curr_tok_is_userdef_p
= true;
4034 string_tree
= tok
->u
.value
;
4035 curr_type
= tok
->type
;
4039 /* Try to avoid the overhead of creating and destroying an obstack
4040 for the common case of just one string. */
4041 if (!cp_parser_is_string_literal
4042 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4044 cp_lexer_consume_token (parser
->lexer
);
4046 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4047 str
.len
= TREE_STRING_LENGTH (string_tree
);
4050 if (curr_tok_is_userdef_p
)
4052 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4054 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4057 curr_type
= tok
->type
;
4063 location_t last_tok_loc
= tok
->location
;
4064 gcc_obstack_init (&str_ob
);
4069 cp_lexer_consume_token (parser
->lexer
);
4071 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4072 str
.len
= TREE_STRING_LENGTH (string_tree
);
4074 if (curr_tok_is_userdef_p
)
4076 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4077 if (have_suffix_p
== 0)
4079 suffix_id
= curr_suffix_id
;
4082 else if (have_suffix_p
== 1
4083 && curr_suffix_id
!= suffix_id
)
4085 error ("inconsistent user-defined literal suffixes"
4086 " %qD and %qD in string literal",
4087 suffix_id
, curr_suffix_id
);
4090 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4093 curr_type
= tok
->type
;
4095 if (type
!= curr_type
)
4097 if (type
== CPP_STRING
)
4099 else if (curr_type
!= CPP_STRING
)
4101 rich_location
rich_loc (line_table
, tok
->location
);
4102 rich_loc
.add_range (last_tok_loc
, false);
4103 error_at (&rich_loc
,
4104 "unsupported non-standard concatenation "
4105 "of string literals");
4109 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4111 last_tok_loc
= tok
->location
;
4113 tok
= cp_lexer_peek_token (parser
->lexer
);
4114 if (cpp_userdef_string_p (tok
->type
))
4116 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4117 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4118 curr_tok_is_userdef_p
= true;
4122 string_tree
= tok
->u
.value
;
4123 curr_type
= tok
->type
;
4124 curr_tok_is_userdef_p
= false;
4127 while (cp_parser_is_string_literal (tok
));
4129 /* A string literal built by concatenation has its caret=start at
4130 the start of the initial string, and its finish at the finish of
4131 the final string literal. */
4132 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4134 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4137 if (type
!= CPP_STRING
&& !wide_ok
)
4139 cp_parser_error (parser
, "a wide string is invalid in this context");
4143 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4144 (parse_in
, strs
, count
, &istr
, type
))
4146 value
= build_string (istr
.len
, (const char *)istr
.text
);
4147 free (CONST_CAST (unsigned char *, istr
.text
));
4153 case CPP_UTF8STRING
:
4154 TREE_TYPE (value
) = char_array_type_node
;
4157 TREE_TYPE (value
) = char16_array_type_node
;
4160 TREE_TYPE (value
) = char32_array_type_node
;
4163 TREE_TYPE (value
) = wchar_array_type_node
;
4167 value
= fix_string_type (value
);
4171 tree literal
= build_userdef_literal (suffix_id
, value
,
4172 OT_NONE
, NULL_TREE
);
4174 value
= cp_parser_userdef_string_literal (literal
);
4180 /* cpp_interpret_string has issued an error. */
4181 value
= error_mark_node
;
4184 obstack_free (&str_ob
, 0);
4186 return cp_expr (value
, loc
);
4189 /* Look up a literal operator with the name and the exact arguments. */
4192 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4195 decl
= lookup_name (name
);
4196 if (!decl
|| !is_overloaded_fn (decl
))
4197 return error_mark_node
;
4199 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4204 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4205 if (parmtypes
!= NULL_TREE
)
4207 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4208 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4210 tree tparm
= TREE_VALUE (parmtypes
);
4211 tree targ
= TREE_TYPE ((*args
)[ix
]);
4212 bool ptr
= TYPE_PTR_P (tparm
);
4213 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4214 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4216 || !same_type_p (TREE_TYPE (tparm
),
4221 && ix
== vec_safe_length (args
)
4222 /* May be this should be sufficient_parms_p instead,
4223 depending on how exactly should user-defined literals
4224 work in presence of default arguments on the literal
4225 operator parameters. */
4226 && parmtypes
== void_list_node
)
4231 return error_mark_node
;
4234 /* Parse a user-defined char constant. Returns a call to a user-defined
4235 literal operator taking the character as an argument. */
4238 cp_parser_userdef_char_literal (cp_parser
*parser
)
4240 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4241 tree literal
= token
->u
.value
;
4242 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4243 tree value
= USERDEF_LITERAL_VALUE (literal
);
4244 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4247 /* Build up a call to the user-defined operator */
4248 /* Lookup the name we got back from the id-expression. */
4249 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4250 vec_safe_push (args
, value
);
4251 decl
= lookup_literal_operator (name
, args
);
4252 if (!decl
|| decl
== error_mark_node
)
4254 error ("unable to find character literal operator %qD with %qT argument",
4255 name
, TREE_TYPE (value
));
4256 release_tree_vector (args
);
4257 return error_mark_node
;
4259 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4260 release_tree_vector (args
);
4264 /* A subroutine of cp_parser_userdef_numeric_literal to
4265 create a char... template parameter pack from a string node. */
4268 make_char_string_pack (tree value
)
4271 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4272 const char *str
= TREE_STRING_POINTER (value
);
4273 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4274 tree argvec
= make_tree_vec (1);
4276 /* Fill in CHARVEC with all of the parameters. */
4277 charvec
= make_tree_vec (len
);
4278 for (i
= 0; i
< len
; ++i
)
4279 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4281 /* Build the argument packs. */
4282 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4284 TREE_VEC_ELT (argvec
, 0) = argpack
;
4289 /* A subroutine of cp_parser_userdef_numeric_literal to
4290 create a char... template parameter pack from a string node. */
4293 make_string_pack (tree value
)
4296 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4297 const unsigned char *str
4298 = (const unsigned char *) TREE_STRING_POINTER (value
);
4299 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4300 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4301 tree argvec
= make_tree_vec (2);
4303 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4304 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4306 /* First template parm is character type. */
4307 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4309 /* Fill in CHARVEC with all of the parameters. */
4310 charvec
= make_tree_vec (len
);
4311 for (int i
= 0; i
< len
; ++i
)
4312 TREE_VEC_ELT (charvec
, i
)
4313 = double_int_to_tree (str_char_type_node
,
4314 double_int::from_buffer (str
+ i
* sz
, sz
));
4316 /* Build the argument packs. */
4317 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4319 TREE_VEC_ELT (argvec
, 1) = argpack
;
4324 /* Parse a user-defined numeric constant. returns a call to a user-defined
4325 literal operator. */
4328 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4330 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4331 tree literal
= token
->u
.value
;
4332 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4333 tree value
= USERDEF_LITERAL_VALUE (literal
);
4334 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4335 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4336 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4338 vec
<tree
, va_gc
> *args
;
4340 /* Look for a literal operator taking the exact type of numeric argument
4341 as the literal value. */
4342 args
= make_tree_vector ();
4343 vec_safe_push (args
, value
);
4344 decl
= lookup_literal_operator (name
, args
);
4345 if (decl
&& decl
!= error_mark_node
)
4347 result
= finish_call_expr (decl
, &args
, false, true,
4348 tf_warning_or_error
);
4350 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4352 warning_at (token
->location
, OPT_Woverflow
,
4353 "integer literal exceeds range of %qT type",
4354 long_long_unsigned_type_node
);
4359 warning_at (token
->location
, OPT_Woverflow
,
4360 "floating literal exceeds range of %qT type",
4361 long_double_type_node
);
4362 else if (overflow
< 0)
4363 warning_at (token
->location
, OPT_Woverflow
,
4364 "floating literal truncated to zero");
4367 release_tree_vector (args
);
4370 release_tree_vector (args
);
4372 /* If the numeric argument didn't work, look for a raw literal
4373 operator taking a const char* argument consisting of the number
4374 in string format. */
4375 args
= make_tree_vector ();
4376 vec_safe_push (args
, num_string
);
4377 decl
= lookup_literal_operator (name
, args
);
4378 if (decl
&& decl
!= error_mark_node
)
4380 result
= finish_call_expr (decl
, &args
, false, true,
4381 tf_warning_or_error
);
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the raw literal didn't work, look for a non-type template
4388 function with parameter pack char.... Call the function with
4389 template parameter characters representing the number. */
4390 args
= make_tree_vector ();
4391 decl
= lookup_literal_operator (name
, args
);
4392 if (decl
&& decl
!= error_mark_node
)
4394 tree tmpl_args
= make_char_string_pack (num_string
);
4395 decl
= lookup_template_function (decl
, tmpl_args
);
4396 result
= finish_call_expr (decl
, &args
, false, true,
4397 tf_warning_or_error
);
4398 release_tree_vector (args
);
4402 release_tree_vector (args
);
4404 /* In C++14 the standard library defines complex number suffixes that
4405 conflict with GNU extensions. Prefer them if <complex> is #included. */
4406 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4407 bool i14
= (cxx_dialect
> cxx11
4408 && (id_equal (suffix_id
, "i")
4409 || id_equal (suffix_id
, "if")
4410 || id_equal (suffix_id
, "il")));
4411 diagnostic_t kind
= DK_ERROR
;
4416 tree cxlit
= lookup_qualified_name (std_node
,
4417 get_identifier ("complex_literals"),
4419 if (cxlit
== error_mark_node
)
4421 /* No <complex>, so pedwarn and use GNU semantics. */
4423 opt
= OPT_Wpedantic
;
4428 = emit_diagnostic (kind
, input_location
, opt
,
4429 "unable to find numeric literal operator %qD", name
);
4432 /* Don't inform either. */;
4435 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4436 "(from <complex>) to enable the C++14 user-defined literal "
4439 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4440 "GNU built-in suffix");
4443 inform (token
->location
, "use -fext-numeric-literals "
4444 "to enable more built-in suffixes");
4446 if (kind
== DK_ERROR
)
4447 value
= error_mark_node
;
4450 /* Use the built-in semantics. */
4452 if (id_equal (suffix_id
, "i"))
4454 if (TREE_CODE (value
) == INTEGER_CST
)
4455 type
= integer_type_node
;
4457 type
= double_type_node
;
4459 else if (id_equal (suffix_id
, "if"))
4460 type
= float_type_node
;
4461 else /* if (id_equal (suffix_id, "il")) */
4462 type
= long_double_type_node
;
4464 value
= build_complex (build_complex_type (type
),
4465 fold_convert (type
, integer_zero_node
),
4466 fold_convert (type
, value
));
4469 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4470 /* Avoid repeated diagnostics. */
4471 token
->u
.value
= value
;
4475 /* Parse a user-defined string constant. Returns a call to a user-defined
4476 literal operator taking a character pointer and the length of the string
4480 cp_parser_userdef_string_literal (tree literal
)
4482 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4483 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4484 tree value
= USERDEF_LITERAL_VALUE (literal
);
4485 int len
= TREE_STRING_LENGTH (value
)
4486 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4488 vec
<tree
, va_gc
> *args
;
4490 /* Build up a call to the user-defined operator. */
4491 /* Lookup the name we got back from the id-expression. */
4492 args
= make_tree_vector ();
4493 vec_safe_push (args
, value
);
4494 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4495 decl
= lookup_literal_operator (name
, args
);
4497 if (decl
&& decl
!= error_mark_node
)
4499 result
= finish_call_expr (decl
, &args
, false, true,
4500 tf_warning_or_error
);
4501 release_tree_vector (args
);
4504 release_tree_vector (args
);
4506 /* Look for a template function with typename parameter CharT
4507 and parameter pack CharT... Call the function with
4508 template parameter characters representing the string. */
4509 args
= make_tree_vector ();
4510 decl
= lookup_literal_operator (name
, args
);
4511 if (decl
&& decl
!= error_mark_node
)
4513 tree tmpl_args
= make_string_pack (value
);
4514 decl
= lookup_template_function (decl
, tmpl_args
);
4515 result
= finish_call_expr (decl
, &args
, false, true,
4516 tf_warning_or_error
);
4517 release_tree_vector (args
);
4520 release_tree_vector (args
);
4522 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4523 name
, TREE_TYPE (value
), size_type_node
);
4524 return error_mark_node
;
4528 /* Basic concepts [gram.basic] */
4530 /* Parse a translation-unit.
4533 declaration-seq [opt]
4535 Returns TRUE if all went well. */
4538 cp_parser_translation_unit (cp_parser
* parser
)
4540 /* The address of the first non-permanent object on the declarator
4542 static void *declarator_obstack_base
;
4546 /* Create the declarator obstack, if necessary. */
4547 if (!cp_error_declarator
)
4549 gcc_obstack_init (&declarator_obstack
);
4550 /* Create the error declarator. */
4551 cp_error_declarator
= make_declarator (cdk_error
);
4552 /* Create the empty parameter list. */
4553 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4555 /* Remember where the base of the declarator obstack lies. */
4556 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4559 cp_parser_declaration_seq_opt (parser
);
4561 /* If there are no tokens left then all went well. */
4562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4564 /* Get rid of the token array; we don't need it any more. */
4565 cp_lexer_destroy (parser
->lexer
);
4566 parser
->lexer
= NULL
;
4568 /* This file might have been a context that's implicitly extern
4569 "C". If so, pop the lang context. (Only relevant for PCH.) */
4570 if (parser
->implicit_extern_c
)
4572 pop_lang_context ();
4573 parser
->implicit_extern_c
= false;
4577 finish_translation_unit ();
4583 cp_parser_error (parser
, "expected declaration");
4587 /* Make sure the declarator obstack was fully cleaned up. */
4588 gcc_assert (obstack_next_free (&declarator_obstack
)
4589 == declarator_obstack_base
);
4591 /* All went well. */
4595 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4596 decltype context. */
4598 static inline tsubst_flags_t
4599 complain_flags (bool decltype_p
)
4601 tsubst_flags_t complain
= tf_warning_or_error
;
4603 complain
|= tf_decltype
;
4607 /* We're about to parse a collection of statements. If we're currently
4608 parsing tentatively, set up a firewall so that any nested
4609 cp_parser_commit_to_tentative_parse won't affect the current context. */
4611 static cp_token_position
4612 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4617 cp_parser_parse_tentatively (parser
);
4618 cp_parser_commit_to_topmost_tentative_parse (parser
);
4619 return cp_lexer_token_position (parser
->lexer
, false);
4622 /* We've finished parsing the collection of statements. Wrap up the
4623 firewall and replace the relevant tokens with the parsed form. */
4626 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4632 /* Finish the firewall level. */
4633 cp_parser_parse_definitely (parser
);
4634 /* And remember the result of the parse for when we try again. */
4635 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4636 token
->type
= CPP_PREPARSED_EXPR
;
4637 token
->u
.value
= expr
;
4638 token
->keyword
= RID_MAX
;
4639 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4642 /* Like the above functions, but let the user modify the tokens. Used by
4643 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4644 later parses, so it makes sense to localize the effects of
4645 cp_parser_commit_to_tentative_parse. */
4647 struct tentative_firewall
4652 tentative_firewall (cp_parser
*p
): parser(p
)
4654 /* If we're currently parsing tentatively, start a committed level as a
4655 firewall and then an inner tentative parse. */
4656 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4658 cp_parser_parse_tentatively (parser
);
4659 cp_parser_commit_to_topmost_tentative_parse (parser
);
4660 cp_parser_parse_tentatively (parser
);
4664 ~tentative_firewall()
4668 /* Finish the inner tentative parse and the firewall, propagating any
4669 uncommitted error state to the outer tentative parse. */
4670 bool err
= cp_parser_error_occurred (parser
);
4671 cp_parser_parse_definitely (parser
);
4672 cp_parser_parse_definitely (parser
);
4674 cp_parser_simulate_error (parser
);
4679 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4680 This class is for tracking such a matching pair of symbols.
4681 In particular, it tracks the location of the first token,
4682 so that if the second token is missing, we can highlight the
4683 location of the first token when notifying the user about the
4686 template <typename traits_t
>
4690 /* token_pair's ctor. */
4691 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4693 /* If the next token is the opening symbol for this pair, consume it and
4695 Otherwise, issue an error and return false.
4696 In either case, record the location of the opening token. */
4698 bool require_open (cp_parser
*parser
)
4700 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4701 return cp_parser_require (parser
, traits_t::open_token_type
,
4702 traits_t::required_token_open
);
4705 /* Consume the next token from PARSER, recording its location as
4706 that of the opening token within the pair. */
4708 cp_token
* consume_open (cp_parser
*parser
)
4710 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4711 gcc_assert (tok
->type
== traits_t::open_token_type
);
4712 m_open_loc
= tok
->location
;
4716 /* If the next token is the closing symbol for this pair, consume it
4718 Otherwise, issue an error, highlighting the location of the
4719 corresponding opening token, and return NULL. */
4721 cp_token
*require_close (cp_parser
*parser
) const
4723 return cp_parser_require (parser
, traits_t::close_token_type
,
4724 traits_t::required_token_close
,
4729 location_t m_open_loc
;
4732 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4734 struct matching_paren_traits
4736 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4737 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4738 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4739 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4742 /* "matching_parens" is a token_pair<T> class for tracking matching
4743 pairs of parentheses. */
4745 typedef token_pair
<matching_paren_traits
> matching_parens
;
4747 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4749 struct matching_brace_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4752 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4754 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4757 /* "matching_braces" is a token_pair<T> class for tracking matching
4760 typedef token_pair
<matching_brace_traits
> matching_braces
;
4763 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4764 enclosing parentheses. */
4767 cp_parser_statement_expr (cp_parser
*parser
)
4769 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4771 /* Consume the '('. */
4772 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4773 matching_parens parens
;
4774 parens
.consume_open (parser
);
4775 /* Start the statement-expression. */
4776 tree expr
= begin_stmt_expr ();
4777 /* Parse the compound-statement. */
4778 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4780 expr
= finish_stmt_expr (expr
, false);
4781 /* Consume the ')'. */
4782 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4783 if (!parens
.require_close (parser
))
4784 cp_parser_skip_to_end_of_statement (parser
);
4786 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4787 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4788 return cp_expr (expr
, combined_loc
);
4791 /* Expressions [gram.expr] */
4793 /* Parse a fold-operator.
4796 - * / % ^ & | = < > << >>
4797 = -= *= /= %= ^= &= |= <<= >>=
4798 == != <= >= && || , .* ->*
4800 This returns the tree code corresponding to the matched operator
4801 as an int. When the current token matches a compound assignment
4802 opertor, the resulting tree code is the negative value of the
4803 non-assignment operator. */
4806 cp_parser_fold_operator (cp_token
*token
)
4808 switch (token
->type
)
4810 case CPP_PLUS
: return PLUS_EXPR
;
4811 case CPP_MINUS
: return MINUS_EXPR
;
4812 case CPP_MULT
: return MULT_EXPR
;
4813 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4814 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4815 case CPP_XOR
: return BIT_XOR_EXPR
;
4816 case CPP_AND
: return BIT_AND_EXPR
;
4817 case CPP_OR
: return BIT_IOR_EXPR
;
4818 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4819 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4821 case CPP_EQ
: return -NOP_EXPR
;
4822 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4823 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4824 case CPP_MULT_EQ
: return -MULT_EXPR
;
4825 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4826 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4827 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4828 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4829 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4830 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4831 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4833 case CPP_EQ_EQ
: return EQ_EXPR
;
4834 case CPP_NOT_EQ
: return NE_EXPR
;
4835 case CPP_LESS
: return LT_EXPR
;
4836 case CPP_GREATER
: return GT_EXPR
;
4837 case CPP_LESS_EQ
: return LE_EXPR
;
4838 case CPP_GREATER_EQ
: return GE_EXPR
;
4840 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4841 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4843 case CPP_COMMA
: return COMPOUND_EXPR
;
4845 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4846 case CPP_DEREF_STAR
: return MEMBER_REF
;
4848 default: return ERROR_MARK
;
4852 /* Returns true if CODE indicates a binary expression, which is not allowed in
4853 the LHS of a fold-expression. More codes will need to be added to use this
4854 function in other contexts. */
4857 is_binary_op (tree_code code
)
4862 case POINTER_PLUS_EXPR
:
4865 case TRUNC_DIV_EXPR
:
4866 case TRUNC_MOD_EXPR
:
4882 case TRUTH_ANDIF_EXPR
:
4883 case TRUTH_ORIF_EXPR
:
4896 /* If the next token is a suitable fold operator, consume it and return as
4897 the function above. */
4900 cp_parser_fold_operator (cp_parser
*parser
)
4902 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4903 int code
= cp_parser_fold_operator (token
);
4904 if (code
!= ERROR_MARK
)
4905 cp_lexer_consume_token (parser
->lexer
);
4909 /* Parse a fold-expression.
4912 ( ... folding-operator cast-expression)
4913 ( cast-expression folding-operator ... )
4914 ( cast-expression folding operator ... folding-operator cast-expression)
4916 Note that the '(' and ')' are matched in primary expression. */
4919 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4926 cp_lexer_consume_token (parser
->lexer
);
4927 int op
= cp_parser_fold_operator (parser
);
4928 if (op
== ERROR_MARK
)
4930 cp_parser_error (parser
, "expected binary operator");
4931 return error_mark_node
;
4934 tree expr
= cp_parser_cast_expression (parser
, false, false,
4936 if (expr
== error_mark_node
)
4937 return error_mark_node
;
4938 return finish_left_unary_fold_expr (expr
, op
);
4941 const cp_token
* token
= cp_lexer_peek_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 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4951 cp_parser_error (parser
, "expected ...");
4952 return error_mark_node
;
4954 cp_lexer_consume_token (parser
->lexer
);
4956 /* The operands of a fold-expression are cast-expressions, so binary or
4957 conditional expressions are not allowed. We check this here to avoid
4958 tentative parsing. */
4959 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4960 /* OK, the expression was parenthesized. */;
4961 else if (is_binary_op (TREE_CODE (expr1
)))
4962 error_at (location_of (expr1
),
4963 "binary expression in operand of fold-expression");
4964 else if (TREE_CODE (expr1
) == COND_EXPR
)
4965 error_at (location_of (expr1
),
4966 "conditional expression in operand of fold-expression");
4969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4970 return finish_right_unary_fold_expr (expr1
, op
);
4972 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4974 cp_parser_error (parser
, "mismatched operator in fold-expression");
4975 return error_mark_node
;
4977 cp_lexer_consume_token (parser
->lexer
);
4979 // Binary left or right fold.
4980 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4981 if (expr2
== error_mark_node
)
4982 return error_mark_node
;
4983 return finish_binary_fold_expr (expr1
, expr2
, op
);
4986 /* Parse a primary-expression.
4993 lambda-expression (C++11)
4998 ( compound-statement )
4999 __builtin_va_arg ( assignment-expression , type-id )
5000 __builtin_offsetof ( type-id , offsetof-expression )
5003 __has_nothrow_assign ( type-id )
5004 __has_nothrow_constructor ( type-id )
5005 __has_nothrow_copy ( type-id )
5006 __has_trivial_assign ( type-id )
5007 __has_trivial_constructor ( type-id )
5008 __has_trivial_copy ( type-id )
5009 __has_trivial_destructor ( type-id )
5010 __has_virtual_destructor ( type-id )
5011 __is_abstract ( type-id )
5012 __is_base_of ( type-id , type-id )
5013 __is_class ( type-id )
5014 __is_empty ( type-id )
5015 __is_enum ( type-id )
5016 __is_final ( type-id )
5017 __is_literal_type ( type-id )
5018 __is_pod ( type-id )
5019 __is_polymorphic ( type-id )
5020 __is_std_layout ( type-id )
5021 __is_trivial ( type-id )
5022 __is_union ( type-id )
5024 Objective-C++ Extension:
5032 ADDRESS_P is true iff this expression was immediately preceded by
5033 "&" and therefore might denote a pointer-to-member. CAST_P is true
5034 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5035 true iff this expression is a template argument.
5037 Returns a representation of the expression. Upon return, *IDK
5038 indicates what kind of id-expression (if any) was present. */
5041 cp_parser_primary_expression (cp_parser
*parser
,
5044 bool template_arg_p
,
5048 cp_token
*token
= NULL
;
5050 /* Assume the primary expression is not an id-expression. */
5051 *idk
= CP_ID_KIND_NONE
;
5053 /* Peek at the next token. */
5054 token
= cp_lexer_peek_token (parser
->lexer
);
5055 switch ((int) token
->type
)
5064 user-defined-literal */
5071 case CPP_PREPARSED_EXPR
:
5072 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5073 return cp_parser_userdef_numeric_literal (parser
);
5074 token
= cp_lexer_consume_token (parser
->lexer
);
5075 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5077 error_at (token
->location
,
5078 "fixed-point types not supported in C++");
5079 return error_mark_node
;
5081 /* Floating-point literals are only allowed in an integral
5082 constant expression if they are cast to an integral or
5083 enumeration type. */
5084 if (TREE_CODE (token
->u
.value
) == REAL_CST
5085 && parser
->integral_constant_expression_p
5088 /* CAST_P will be set even in invalid code like "int(2.7 +
5089 ...)". Therefore, we have to check that the next token
5090 is sure to end the cast. */
5093 cp_token
*next_token
;
5095 next_token
= cp_lexer_peek_token (parser
->lexer
);
5096 if (/* The comma at the end of an
5097 enumerator-definition. */
5098 next_token
->type
!= CPP_COMMA
5099 /* The curly brace at the end of an enum-specifier. */
5100 && next_token
->type
!= CPP_CLOSE_BRACE
5101 /* The end of a statement. */
5102 && next_token
->type
!= CPP_SEMICOLON
5103 /* The end of the cast-expression. */
5104 && next_token
->type
!= CPP_CLOSE_PAREN
5105 /* The end of an array bound. */
5106 && next_token
->type
!= CPP_CLOSE_SQUARE
5107 /* The closing ">" in a template-argument-list. */
5108 && (next_token
->type
!= CPP_GREATER
5109 || parser
->greater_than_is_operator_p
)
5110 /* C++0x only: A ">>" treated like two ">" tokens,
5111 in a template-argument-list. */
5112 && (next_token
->type
!= CPP_RSHIFT
5113 || (cxx_dialect
== cxx98
)
5114 || parser
->greater_than_is_operator_p
))
5118 /* If we are within a cast, then the constraint that the
5119 cast is to an integral or enumeration type will be
5120 checked at that point. If we are not within a cast, then
5121 this code is invalid. */
5123 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5125 return cp_expr (token
->u
.value
, token
->location
);
5127 case CPP_CHAR_USERDEF
:
5128 case CPP_CHAR16_USERDEF
:
5129 case CPP_CHAR32_USERDEF
:
5130 case CPP_WCHAR_USERDEF
:
5131 case CPP_UTF8CHAR_USERDEF
:
5132 return cp_parser_userdef_char_literal (parser
);
5138 case CPP_UTF8STRING
:
5139 case CPP_STRING_USERDEF
:
5140 case CPP_STRING16_USERDEF
:
5141 case CPP_STRING32_USERDEF
:
5142 case CPP_WSTRING_USERDEF
:
5143 case CPP_UTF8STRING_USERDEF
:
5144 /* ??? Should wide strings be allowed when parser->translate_strings_p
5145 is false (i.e. in attributes)? If not, we can kill the third
5146 argument to cp_parser_string_literal. */
5147 return cp_parser_string_literal (parser
,
5148 parser
->translate_strings_p
,
5151 case CPP_OPEN_PAREN
:
5152 /* If we see `( { ' then we are looking at the beginning of
5153 a GNU statement-expression. */
5154 if (cp_parser_allow_gnu_extensions_p (parser
)
5155 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5157 /* Statement-expressions are not allowed by the standard. */
5158 pedwarn (token
->location
, OPT_Wpedantic
,
5159 "ISO C++ forbids braced-groups within expressions");
5161 /* And they're not allowed outside of a function-body; you
5162 cannot, for example, write:
5164 int i = ({ int j = 3; j + 1; });
5166 at class or namespace scope. */
5167 if (!parser
->in_function_body
5168 || parser
->in_template_argument_list_p
)
5170 error_at (token
->location
,
5171 "statement-expressions are not allowed outside "
5172 "functions nor in template-argument lists");
5173 cp_parser_skip_to_end_of_block_or_statement (parser
);
5174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5175 cp_lexer_consume_token (parser
->lexer
);
5176 return error_mark_node
;
5179 return cp_parser_statement_expr (parser
);
5181 /* Otherwise it's a normal parenthesized expression. */
5184 bool saved_greater_than_is_operator_p
;
5186 location_t open_paren_loc
= token
->location
;
5188 /* Consume the `('. */
5189 matching_parens parens
;
5190 parens
.consume_open (parser
);
5191 /* Within a parenthesized expression, a `>' token is always
5192 the greater-than operator. */
5193 saved_greater_than_is_operator_p
5194 = parser
->greater_than_is_operator_p
;
5195 parser
->greater_than_is_operator_p
= true;
5197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5198 /* Left fold expression. */
5201 /* Parse the parenthesized expression. */
5202 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5204 token
= cp_lexer_peek_token (parser
->lexer
);
5205 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5207 expr
= cp_parser_fold_expression (parser
, expr
);
5208 if (expr
!= error_mark_node
5209 && cxx_dialect
< cxx17
5210 && !in_system_header_at (input_location
))
5211 pedwarn (input_location
, 0, "fold-expressions only available "
5212 "with -std=c++17 or -std=gnu++17");
5215 /* Let the front end know that this expression was
5216 enclosed in parentheses. This matters in case, for
5217 example, the expression is of the form `A::B', since
5218 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5220 expr
= finish_parenthesized_expr (expr
);
5222 /* DR 705: Wrapping an unqualified name in parentheses
5223 suppresses arg-dependent lookup. We want to pass back
5224 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5225 (c++/37862), but none of the others. */
5226 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5227 *idk
= CP_ID_KIND_NONE
;
5229 /* The `>' token might be the end of a template-id or
5230 template-parameter-list now. */
5231 parser
->greater_than_is_operator_p
5232 = saved_greater_than_is_operator_p
;
5234 /* Consume the `)'. */
5235 token
= cp_lexer_peek_token (parser
->lexer
);
5236 location_t close_paren_loc
= token
->location
;
5237 expr
.set_range (open_paren_loc
, close_paren_loc
);
5238 if (!parens
.require_close (parser
)
5239 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5240 cp_parser_skip_to_end_of_statement (parser
);
5245 case CPP_OPEN_SQUARE
:
5247 if (c_dialect_objc ())
5249 /* We might have an Objective-C++ message. */
5250 cp_parser_parse_tentatively (parser
);
5251 tree msg
= cp_parser_objc_message_expression (parser
);
5252 /* If that works out, we're done ... */
5253 if (cp_parser_parse_definitely (parser
))
5255 /* ... else, fall though to see if it's a lambda. */
5257 cp_expr lam
= cp_parser_lambda_expression (parser
);
5258 /* Don't warn about a failed tentative parse. */
5259 if (cp_parser_error_occurred (parser
))
5260 return error_mark_node
;
5261 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5265 case CPP_OBJC_STRING
:
5266 if (c_dialect_objc ())
5267 /* We have an Objective-C++ string literal. */
5268 return cp_parser_objc_expression (parser
);
5269 cp_parser_error (parser
, "expected primary-expression");
5270 return error_mark_node
;
5273 switch (token
->keyword
)
5275 /* These two are the boolean literals. */
5277 cp_lexer_consume_token (parser
->lexer
);
5278 return cp_expr (boolean_true_node
, token
->location
);
5280 cp_lexer_consume_token (parser
->lexer
);
5281 return cp_expr (boolean_false_node
, token
->location
);
5283 /* The `__null' literal. */
5285 cp_lexer_consume_token (parser
->lexer
);
5286 return cp_expr (null_node
, token
->location
);
5288 /* The `nullptr' literal. */
5290 cp_lexer_consume_token (parser
->lexer
);
5291 return cp_expr (nullptr_node
, token
->location
);
5293 /* Recognize the `this' keyword. */
5295 cp_lexer_consume_token (parser
->lexer
);
5296 if (parser
->local_variables_forbidden_p
)
5298 error_at (token
->location
,
5299 "%<this%> may not be used in this context");
5300 return error_mark_node
;
5302 /* Pointers cannot appear in constant-expressions. */
5303 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5304 return error_mark_node
;
5305 return cp_expr (finish_this_expr (), token
->location
);
5307 /* The `operator' keyword can be the beginning of an
5312 case RID_FUNCTION_NAME
:
5313 case RID_PRETTY_FUNCTION_NAME
:
5314 case RID_C99_FUNCTION_NAME
:
5316 non_integral_constant name
;
5318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5319 __func__ are the names of variables -- but they are
5320 treated specially. Therefore, they are handled here,
5321 rather than relying on the generic id-expression logic
5322 below. Grammatically, these names are id-expressions.
5324 Consume the token. */
5325 token
= cp_lexer_consume_token (parser
->lexer
);
5327 switch (token
->keyword
)
5329 case RID_FUNCTION_NAME
:
5330 name
= NIC_FUNC_NAME
;
5332 case RID_PRETTY_FUNCTION_NAME
:
5333 name
= NIC_PRETTY_FUNC
;
5335 case RID_C99_FUNCTION_NAME
:
5336 name
= NIC_C99_FUNC
;
5342 if (cp_parser_non_integral_constant_expression (parser
, name
))
5343 return error_mark_node
;
5345 /* Look up the name. */
5346 return finish_fname (token
->u
.value
);
5353 source_location type_location
;
5354 location_t start_loc
5355 = cp_lexer_peek_token (parser
->lexer
)->location
;
5356 /* The `__builtin_va_arg' construct is used to handle
5357 `va_arg'. Consume the `__builtin_va_arg' token. */
5358 cp_lexer_consume_token (parser
->lexer
);
5359 /* Look for the opening `('. */
5360 matching_parens parens
;
5361 parens
.require_open (parser
);
5362 /* Now, parse the assignment-expression. */
5363 expression
= cp_parser_assignment_expression (parser
);
5364 /* Look for the `,'. */
5365 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5366 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5367 /* Parse the type-id. */
5369 type_id_in_expr_sentinel
s (parser
);
5370 type
= cp_parser_type_id (parser
);
5372 /* Look for the closing `)'. */
5373 location_t finish_loc
5374 = cp_lexer_peek_token (parser
->lexer
)->location
;
5375 parens
.require_close (parser
);
5376 /* Using `va_arg' in a constant-expression is not
5378 if (cp_parser_non_integral_constant_expression (parser
,
5380 return error_mark_node
;
5381 /* Construct a location of the form:
5382 __builtin_va_arg (v, int)
5383 ~~~~~~~~~~~~~~~~~~~~~^~~~
5384 with the caret at the type, ranging from the start of the
5385 "__builtin_va_arg" token to the close paren. */
5386 location_t combined_loc
5387 = make_location (type_location
, start_loc
, finish_loc
);
5388 return build_x_va_arg (combined_loc
, expression
, type
);
5392 return cp_parser_builtin_offsetof (parser
);
5394 case RID_HAS_NOTHROW_ASSIGN
:
5395 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5396 case RID_HAS_NOTHROW_COPY
:
5397 case RID_HAS_TRIVIAL_ASSIGN
:
5398 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5399 case RID_HAS_TRIVIAL_COPY
:
5400 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5402 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5403 case RID_IS_ABSTRACT
:
5404 case RID_IS_AGGREGATE
:
5405 case RID_IS_BASE_OF
:
5410 case RID_IS_LITERAL_TYPE
:
5412 case RID_IS_POLYMORPHIC
:
5413 case RID_IS_SAME_AS
:
5414 case RID_IS_STD_LAYOUT
:
5415 case RID_IS_TRIVIAL
:
5416 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5418 case RID_IS_TRIVIALLY_COPYABLE
:
5420 case RID_IS_ASSIGNABLE
:
5421 case RID_IS_CONSTRUCTIBLE
:
5422 return cp_parser_trait_expr (parser
, token
->keyword
);
5426 return cp_parser_requires_expression (parser
);
5428 /* Objective-C++ expressions. */
5430 case RID_AT_PROTOCOL
:
5431 case RID_AT_SELECTOR
:
5432 return cp_parser_objc_expression (parser
);
5435 if (parser
->in_function_body
5436 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5439 error_at (token
->location
,
5440 "a template declaration cannot appear at block scope");
5441 cp_parser_skip_to_end_of_block_or_statement (parser
);
5442 return error_mark_node
;
5446 cp_parser_error (parser
, "expected primary-expression");
5447 return error_mark_node
;
5450 /* An id-expression can start with either an identifier, a
5451 `::' as the beginning of a qualified-id, or the "operator"
5455 case CPP_TEMPLATE_ID
:
5456 case CPP_NESTED_NAME_SPECIFIER
:
5459 cp_expr id_expression
;
5461 const char *error_msg
;
5464 cp_token
*id_expr_token
;
5466 /* Parse the id-expression. */
5468 = cp_parser_id_expression (parser
,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5472 /*declarator_p=*/false,
5473 /*optional_p=*/false);
5474 if (id_expression
== error_mark_node
)
5475 return error_mark_node
;
5476 id_expr_token
= token
;
5477 token
= cp_lexer_peek_token (parser
->lexer
);
5478 done
= (token
->type
!= CPP_OPEN_SQUARE
5479 && token
->type
!= CPP_OPEN_PAREN
5480 && token
->type
!= CPP_DOT
5481 && token
->type
!= CPP_DEREF
5482 && token
->type
!= CPP_PLUS_PLUS
5483 && token
->type
!= CPP_MINUS_MINUS
);
5484 /* If we have a template-id, then no further lookup is
5485 required. If the template-id was for a template-class, we
5486 will sometimes have a TYPE_DECL at this point. */
5487 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5488 || TREE_CODE (id_expression
) == TYPE_DECL
)
5489 decl
= id_expression
;
5490 /* Look up the name. */
5493 tree ambiguous_decls
;
5495 /* If we already know that this lookup is ambiguous, then
5496 we've already issued an error message; there's no reason
5498 if (id_expr_token
->type
== CPP_NAME
5499 && id_expr_token
->error_reported
)
5501 cp_parser_simulate_error (parser
);
5502 return error_mark_node
;
5505 decl
= cp_parser_lookup_name (parser
, id_expression
,
5508 /*is_namespace=*/false,
5509 /*check_dependency=*/true,
5511 id_expr_token
->location
);
5512 /* If the lookup was ambiguous, an error will already have
5514 if (ambiguous_decls
)
5515 return error_mark_node
;
5517 /* In Objective-C++, we may have an Objective-C 2.0
5518 dot-syntax for classes here. */
5519 if (c_dialect_objc ()
5520 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5521 && TREE_CODE (decl
) == TYPE_DECL
5522 && objc_is_class_name (decl
))
5525 cp_lexer_consume_token (parser
->lexer
);
5526 component
= cp_parser_identifier (parser
);
5527 if (component
== error_mark_node
)
5528 return error_mark_node
;
5530 tree result
= objc_build_class_component_ref (id_expression
,
5532 /* Build a location of the form:
5535 with caret at the start of the component name (at
5536 input_location), ranging from the start of the id_expression
5537 to the end of the component name. */
5538 location_t combined_loc
5539 = make_location (input_location
, id_expression
.get_start (),
5540 get_finish (input_location
));
5541 protected_set_expr_location (result
, combined_loc
);
5545 /* In Objective-C++, an instance variable (ivar) may be preferred
5546 to whatever cp_parser_lookup_name() found.
5547 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5548 rest of c-family, we have to do a little extra work to preserve
5549 any location information in cp_expr "decl". Given that
5550 objc_lookup_ivar is implemented in "c-family" and "objc", we
5551 have a trip through the pure "tree" type, rather than cp_expr.
5552 Naively copying it back to "decl" would implicitly give the
5553 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5554 store an EXPR_LOCATION. Hence we only update "decl" (and
5555 hence its location_t) if we get back a different tree node. */
5556 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5558 if (decl_tree
!= decl
.get_value ())
5559 decl
= cp_expr (decl_tree
);
5561 /* If name lookup gives us a SCOPE_REF, then the
5562 qualifying scope was dependent. */
5563 if (TREE_CODE (decl
) == SCOPE_REF
)
5565 /* At this point, we do not know if DECL is a valid
5566 integral constant expression. We assume that it is
5567 in fact such an expression, so that code like:
5569 template <int N> struct A {
5573 is accepted. At template-instantiation time, we
5574 will check that B<N>::i is actually a constant. */
5577 /* Check to see if DECL is a local variable in a context
5578 where that is forbidden. */
5579 if (parser
->local_variables_forbidden_p
5580 && local_variable_p (decl
))
5582 /* It might be that we only found DECL because we are
5583 trying to be generous with pre-ISO scoping rules.
5584 For example, consider:
5588 for (int i = 0; i < 10; ++i) {}
5589 extern void f(int j = i);
5592 Here, name look up will originally find the out
5593 of scope `i'. We need to issue a warning message,
5594 but then use the global `i'. */
5595 decl
= check_for_out_of_scope_variable (decl
);
5596 if (local_variable_p (decl
))
5598 error_at (id_expr_token
->location
,
5599 "local variable %qD may not appear in this context",
5601 return error_mark_node
;
5606 decl
= (finish_id_expression
5607 (id_expression
, decl
, parser
->scope
,
5609 parser
->integral_constant_expression_p
,
5610 parser
->allow_non_integral_constant_expression_p
,
5611 &parser
->non_integral_constant_expression_p
,
5612 template_p
, done
, address_p
,
5615 id_expression
.get_location ()));
5617 cp_parser_error (parser
, error_msg
);
5618 decl
.set_location (id_expr_token
->location
);
5622 /* Anything else is an error. */
5624 cp_parser_error (parser
, "expected primary-expression");
5625 return error_mark_node
;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser
*parser
,
5633 bool template_arg_p
,
5636 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5637 /*decltype*/false, idk
);
5640 /* Parse an id-expression.
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5649 :: operator-function-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5675 cp_parser_id_expression (cp_parser
*parser
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5682 bool global_scope_p
;
5683 bool nested_name_specifier_p
;
5685 /* Assume the `template' keyword was not used. */
5687 *template_p
= template_keyword_p
;
5689 /* Look for the optional `::' operator. */
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser
,
5693 /*current_scope_valid_p=*/false)
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser
,
5699 /*typename_keyword_p=*/false,
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p
)
5711 tree saved_object_scope
;
5712 tree saved_qualifying_scope
;
5713 cp_expr unqualified_id
;
5716 /* See if the next token is the `template' keyword. */
5718 template_p
= &is_template
;
5719 *template_p
= cp_parser_optional_template_keyword (parser
);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope
= parser
->scope
;
5723 saved_object_scope
= parser
->object_scope
;
5724 saved_qualifying_scope
= parser
->qualifying_scope
;
5725 /* Process the final unqualified-id. */
5726 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser
->scope
= saved_scope
;
5732 parser
->object_scope
= saved_object_scope
;
5733 parser
->qualifying_scope
= saved_qualifying_scope
;
5735 return unqualified_id
;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p
)
5744 /* Peek at the next token. */
5745 token
= cp_lexer_peek_token (parser
->lexer
);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token
->type
== CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5753 return cp_parser_identifier (parser
);
5755 cp_parser_parse_tentatively (parser
);
5756 /* Try a template-id. */
5757 id
= cp_parser_template_id (parser
,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser
))
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token
= cp_lexer_peek_token (parser
->lexer
);
5770 switch (token
->type
)
5773 return cp_parser_identifier (parser
);
5776 if (token
->keyword
== RID_OPERATOR
)
5777 return cp_parser_operator_function_id (parser
);
5781 cp_parser_error (parser
, "expected id-expression");
5782 return error_mark_node
;
5786 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5787 /*check_dependency_p=*/true,
5792 /* Parse an unqualified-id.
5796 operator-function-id
5797 conversion-function-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5815 cp_parser_unqualified_id (cp_parser
* parser
,
5816 bool template_keyword_p
,
5817 bool check_dependency_p
,
5823 /* Peek at the next token. */
5824 token
= cp_lexer_peek_token (parser
->lexer
);
5826 switch ((int) token
->type
)
5832 /* We don't know yet whether or not this will be a
5834 cp_parser_parse_tentatively (parser
);
5835 /* Try a template-id. */
5836 id
= cp_parser_template_id (parser
, template_keyword_p
,
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser
))
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser
);
5847 case CPP_TEMPLATE_ID
:
5848 return cp_parser_template_id (parser
, template_keyword_p
,
5856 tree qualifying_scope
;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser
->lexer
);
5863 /* Parse the class-name. The standard, as written, seems to
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope
= parser
->scope
;
5899 object_scope
= parser
->object_scope
;
5900 qualifying_scope
= parser
->qualifying_scope
;
5902 /* Check for invalid scopes. */
5903 if (scope
== error_mark_node
)
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5912 error_at (token
->location
,
5913 "scope %qT before %<~%> is not a class-name",
5915 cp_parser_simulate_error (parser
);
5916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5917 cp_lexer_consume_token (parser
->lexer
);
5918 return error_mark_node
;
5920 gcc_assert (!scope
|| TYPE_P (scope
));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 && token
->type
== CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5929 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5930 || (CLASS_TYPE_P (scope
)
5931 && constructor_name_p (token
->u
.value
, scope
))))
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, scope
);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token
, RID_AUTO
))
5940 if (cxx_dialect
< cxx14
)
5941 pedwarn (input_location
, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser
->lexer
);
5945 return build_nt (BIT_NOT_EXPR
, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5955 type_decl
= NULL_TREE
;
5958 cp_parser_parse_tentatively (parser
);
5959 type_decl
= cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (cp_parser_parse_definitely (parser
))
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done
&& scope
&& qualifying_scope
)
5972 cp_parser_parse_tentatively (parser
);
5973 parser
->scope
= qualifying_scope
;
5974 parser
->object_scope
= NULL_TREE
;
5975 parser
->qualifying_scope
= NULL_TREE
;
5977 = cp_parser_class_name (parser
,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5984 if (cp_parser_parse_definitely (parser
))
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done
&& object_scope
)
5990 cp_parser_parse_tentatively (parser
);
5991 parser
->scope
= object_scope
;
5992 parser
->object_scope
= NULL_TREE
;
5993 parser
->qualifying_scope
= NULL_TREE
;
5995 = cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* Look in the surrounding context. */
6008 parser
->scope
= NULL_TREE
;
6009 parser
->object_scope
= NULL_TREE
;
6010 parser
->qualifying_scope
= NULL_TREE
;
6011 if (processing_template_decl
)
6012 cp_parser_parse_tentatively (parser
);
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser
))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6028 cp_parser_simulate_error (parser
);
6029 return error_mark_node
;
6031 /* Otherwise, accept it and check for a match at instantiation
6033 type_decl
= cp_parser_identifier (parser
);
6034 if (type_decl
!= error_mark_node
)
6035 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl
== error_mark_node
&& scope
)
6044 return build_nt (BIT_NOT_EXPR
, scope
);
6045 else if (type_decl
== error_mark_node
)
6046 return error_mark_node
;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6052 error_at (token
->location
,
6053 "declaration of %<~%T%> as member of %qT",
6055 cp_parser_simulate_error (parser
);
6056 return error_mark_node
;
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6065 && !DECL_SELF_REFERENCE_P (type_decl
)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6067 error_at (token
->location
,
6068 "typedef-name %qD used as destructor declarator",
6071 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6075 if (token
->keyword
== RID_OPERATOR
)
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try a template-id. */
6082 id
= cp_parser_template_id (parser
, template_keyword_p
,
6083 /*check_dependency_p=*/true,
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser
))
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser
);
6092 /* Try an operator-function-id. */
6093 id
= cp_parser_operator_function_id (parser
);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser
))
6096 id
= cp_parser_conversion_function_id (parser
);
6097 else if (UDLIT_OPER_P (id
))
6100 const char *name
= UDLIT_OP_SUFFIX (id
);
6101 if (name
[0] != '_' && !in_system_header_at (input_location
)
6103 warning (OPT_Wliteral_suffix
,
6104 "literal operator suffixes not preceded by %<_%>"
6105 " are reserved for future standardization");
6115 cp_parser_error (parser
, "expected unqualified-id");
6116 return error_mark_node
;
6120 /* Parse an (optional) nested-name-specifier.
6122 nested-name-specifier: [C++98]
6123 class-or-namespace-name :: nested-name-specifier [opt]
6124 class-or-namespace-name :: template nested-name-specifier [opt]
6126 nested-name-specifier: [C++0x]
6129 nested-name-specifier identifier ::
6130 nested-name-specifier template [opt] simple-template-id ::
6132 PARSER->SCOPE should be set appropriately before this function is
6133 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6137 Sets PARSER->SCOPE to the class (TYPE) or namespace
6138 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139 it unchanged if there is no nested-name-specifier. Returns the new
6140 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6142 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143 part of a declaration and/or decl-specifier. */
6146 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6147 bool typename_keyword_p
,
6148 bool check_dependency_p
,
6150 bool is_declaration
,
6151 bool template_keyword_p
/* = false */)
6153 bool success
= false;
6154 cp_token_position start
= 0;
6157 /* Remember where the nested-name-specifier starts. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6160 start
= cp_lexer_token_position (parser
->lexer
, false);
6161 push_deferring_access_checks (dk_deferred
);
6168 tree saved_qualifying_scope
;
6170 /* Spot cases that cannot be the beginning of a
6171 nested-name-specifier. */
6172 token
= cp_lexer_peek_token (parser
->lexer
);
6174 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 the already parsed nested-name-specifier. */
6176 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6178 /* Grab the nested-name-specifier and continue the loop. */
6179 cp_parser_pre_parsed_nested_name_specifier (parser
);
6180 /* If we originally encountered this nested-name-specifier
6181 with IS_DECLARATION set to false, we will not have
6182 resolved TYPENAME_TYPEs, so we must do so here. */
6184 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6186 new_scope
= resolve_typename_type (parser
->scope
,
6187 /*only_current_p=*/false);
6188 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6189 parser
->scope
= new_scope
;
6195 /* Spot cases that cannot be the beginning of a
6196 nested-name-specifier. On the second and subsequent times
6197 through the loop, we look for the `template' keyword. */
6198 if (success
&& token
->keyword
== RID_TEMPLATE
)
6200 /* A template-id can start a nested-name-specifier. */
6201 else if (token
->type
== CPP_TEMPLATE_ID
)
6203 /* DR 743: decltype can be used in a nested-name-specifier. */
6204 else if (token_is_decltype (token
))
6208 /* If the next token is not an identifier, then it is
6209 definitely not a type-name or namespace-name. */
6210 if (token
->type
!= CPP_NAME
)
6212 /* If the following token is neither a `<' (to begin a
6213 template-id), nor a `::', then we are not looking at a
6214 nested-name-specifier. */
6215 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6217 if (token
->type
== CPP_COLON
6218 && parser
->colon_corrects_to_scope_p
6219 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6221 gcc_rich_location
richloc (token
->location
);
6222 richloc
.add_fixit_replace ("::");
6224 "found %<:%> in nested-name-specifier, "
6226 token
->type
= CPP_SCOPE
;
6229 if (token
->type
!= CPP_SCOPE
6230 && !cp_parser_nth_token_starts_template_argument_list_p
6235 /* The nested-name-specifier is optional, so we parse
6237 cp_parser_parse_tentatively (parser
);
6239 /* Look for the optional `template' keyword, if this isn't the
6240 first time through the loop. */
6242 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6244 /* Save the old scope since the name lookup we are about to do
6245 might destroy it. */
6246 old_scope
= parser
->scope
;
6247 saved_qualifying_scope
= parser
->qualifying_scope
;
6248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 look up names in "X<T>::I" in order to determine that "Y" is
6250 a template. So, if we have a typename at this point, we make
6251 an effort to look through it. */
6253 && !typename_keyword_p
6255 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6256 parser
->scope
= resolve_typename_type (parser
->scope
,
6257 /*only_current_p=*/false);
6258 /* Parse the qualifying entity. */
6260 = cp_parser_qualifying_entity (parser
,
6266 /* Look for the `::' token. */
6267 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6269 /* If we found what we wanted, we keep going; otherwise, we're
6271 if (!cp_parser_parse_definitely (parser
))
6273 bool error_p
= false;
6275 /* Restore the OLD_SCOPE since it was valid before the
6276 failed attempt at finding the last
6277 class-or-namespace-name. */
6278 parser
->scope
= old_scope
;
6279 parser
->qualifying_scope
= saved_qualifying_scope
;
6281 /* If the next token is a decltype, and the one after that is a
6282 `::', then the decltype has failed to resolve to a class or
6283 enumeration type. Give this error even when parsing
6284 tentatively since it can't possibly be valid--and we're going
6285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 won't get another chance.*/
6287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6288 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6291 token
= cp_lexer_consume_token (parser
->lexer
);
6292 error_at (token
->location
, "decltype evaluates to %qT, "
6293 "which is not a class or enumeration type",
6294 token
->u
.tree_check_value
->value
);
6295 parser
->scope
= error_mark_node
;
6299 cp_lexer_consume_token (parser
->lexer
);
6302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6303 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6305 /* If we have a non-type template-id followed by ::, it can't
6306 possibly be valid. */
6307 token
= cp_lexer_peek_token (parser
->lexer
);
6308 tree tid
= token
->u
.tree_check_value
->value
;
6309 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6310 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6312 tree tmpl
= NULL_TREE
;
6313 if (is_overloaded_fn (tid
))
6315 tree fns
= get_fns (tid
);
6316 if (OVL_SINGLE_P (fns
))
6317 tmpl
= OVL_FIRST (fns
);
6318 error_at (token
->location
, "function template-id %qD "
6319 "in nested-name-specifier", tid
);
6323 /* Variable template. */
6324 tmpl
= TREE_OPERAND (tid
, 0);
6325 gcc_assert (variable_template_p (tmpl
));
6326 error_at (token
->location
, "variable template-id %qD "
6327 "in nested-name-specifier", tid
);
6330 inform (DECL_SOURCE_LOCATION (tmpl
),
6331 "%qD declared here", tmpl
);
6333 parser
->scope
= error_mark_node
;
6337 cp_lexer_consume_token (parser
->lexer
);
6338 cp_lexer_consume_token (parser
->lexer
);
6342 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6344 /* If the next token is an identifier, and the one after
6345 that is a `::', then any valid interpretation would have
6346 found a class-or-namespace-name. */
6347 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6348 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6350 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6353 token
= cp_lexer_consume_token (parser
->lexer
);
6356 if (!token
->error_reported
)
6359 tree ambiguous_decls
;
6361 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6363 /*is_template=*/false,
6364 /*is_namespace=*/false,
6365 /*check_dependency=*/true,
6368 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6369 error_at (token
->location
,
6370 "%qD used without template parameters",
6372 else if (ambiguous_decls
)
6374 // cp_parser_lookup_name has the same diagnostic,
6375 // thus make sure to emit it at most once.
6376 if (cp_parser_uncommitted_to_tentative_parse_p
6379 error_at (token
->location
,
6380 "reference to %qD is ambiguous",
6382 print_candidates (ambiguous_decls
);
6384 decl
= error_mark_node
;
6388 if (cxx_dialect
!= cxx98
)
6389 cp_parser_name_lookup_error
6390 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6393 cp_parser_name_lookup_error
6394 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6398 parser
->scope
= error_mark_node
;
6400 /* Treat this as a successful nested-name-specifier
6405 If the name found is not a class-name (clause
6406 _class_) or namespace-name (_namespace.def_), the
6407 program is ill-formed. */
6410 cp_lexer_consume_token (parser
->lexer
);
6414 /* We've found one valid nested-name-specifier. */
6416 /* Name lookup always gives us a DECL. */
6417 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6418 new_scope
= TREE_TYPE (new_scope
);
6419 /* Uses of "template" must be followed by actual templates. */
6420 if (template_keyword_p
6421 && !(CLASS_TYPE_P (new_scope
)
6422 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6424 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6425 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6426 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6427 == TEMPLATE_ID_EXPR
)))
6428 permerror (input_location
, TYPE_P (new_scope
)
6429 ? G_("%qT is not a template")
6430 : G_("%qD is not a template"),
6432 /* If it is a class scope, try to complete it; we are about to
6433 be looking up names inside the class. */
6434 if (TYPE_P (new_scope
)
6435 /* Since checking types for dependency can be expensive,
6436 avoid doing it if the type is already complete. */
6437 && !COMPLETE_TYPE_P (new_scope
)
6438 /* Do not try to complete dependent types. */
6439 && !dependent_type_p (new_scope
))
6441 new_scope
= complete_type (new_scope
);
6442 /* If it is a typedef to current class, use the current
6443 class instead, as the typedef won't have any names inside
6445 if (!COMPLETE_TYPE_P (new_scope
)
6446 && currently_open_class (new_scope
))
6447 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6449 /* Make sure we look in the right scope the next time through
6451 parser
->scope
= new_scope
;
6454 /* If parsing tentatively, replace the sequence of tokens that makes
6455 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456 token. That way, should we re-parse the token stream, we will
6457 not have to repeat the effort required to do the parse, nor will
6458 we issue duplicate error messages. */
6459 if (success
&& start
)
6463 token
= cp_lexer_token_at (parser
->lexer
, start
);
6464 /* Reset the contents of the START token. */
6465 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6466 /* Retrieve any deferred checks. Do not pop this access checks yet
6467 so the memory will not be reclaimed during token replacing below. */
6468 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6469 token
->u
.tree_check_value
->value
= parser
->scope
;
6470 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6471 token
->u
.tree_check_value
->qualifying_scope
=
6472 parser
->qualifying_scope
;
6473 token
->keyword
= RID_MAX
;
6475 /* Purge all subsequent tokens. */
6476 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6480 pop_to_parent_deferring_access_checks ();
6482 return success
? parser
->scope
: NULL_TREE
;
6485 /* Parse a nested-name-specifier. See
6486 cp_parser_nested_name_specifier_opt for details. This function
6487 behaves identically, except that it will an issue an error if no
6488 nested-name-specifier is present. */
6491 cp_parser_nested_name_specifier (cp_parser
*parser
,
6492 bool typename_keyword_p
,
6493 bool check_dependency_p
,
6495 bool is_declaration
)
6499 /* Look for the nested-name-specifier. */
6500 scope
= cp_parser_nested_name_specifier_opt (parser
,
6505 /* If it was not present, issue an error message. */
6508 cp_parser_error (parser
, "expected nested-name-specifier");
6509 parser
->scope
= NULL_TREE
;
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516 this is either a class-name or a namespace-name (which corresponds
6517 to the class-or-namespace-name production in the grammar). For
6518 C++0x, it can also be a type-name that refers to an enumeration
6519 type or a simple-template-id.
6521 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524 TYPE_P is TRUE iff the next name should be taken as a class-name,
6525 even the same name is declared to be another entity in the same
6528 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529 specified by the class-or-namespace-name. If neither is found the
6530 ERROR_MARK_NODE is returned. */
6533 cp_parser_qualifying_entity (cp_parser
*parser
,
6534 bool typename_keyword_p
,
6535 bool template_keyword_p
,
6536 bool check_dependency_p
,
6538 bool is_declaration
)
6541 tree saved_qualifying_scope
;
6542 tree saved_object_scope
;
6545 bool successful_parse_p
;
6547 /* DR 743: decltype can appear in a nested-name-specifier. */
6548 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6550 scope
= cp_parser_decltype (parser
);
6551 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6552 && !MAYBE_CLASS_TYPE_P (scope
))
6554 cp_parser_simulate_error (parser
);
6555 return error_mark_node
;
6557 if (TYPE_NAME (scope
))
6558 scope
= TYPE_NAME (scope
);
6562 /* Before we try to parse the class-name, we must save away the
6563 current PARSER->SCOPE since cp_parser_class_name will destroy
6565 saved_scope
= parser
->scope
;
6566 saved_qualifying_scope
= parser
->qualifying_scope
;
6567 saved_object_scope
= parser
->object_scope
;
6568 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6569 there is no need to look for a namespace-name. */
6570 only_class_p
= template_keyword_p
6571 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6573 cp_parser_parse_tentatively (parser
);
6574 scope
= cp_parser_class_name (parser
,
6577 type_p
? class_type
: none_type
,
6579 /*class_head_p=*/false,
6581 /*enum_ok=*/cxx_dialect
> cxx98
);
6582 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6583 /* If that didn't work, try for a namespace-name. */
6584 if (!only_class_p
&& !successful_parse_p
)
6586 /* Restore the saved scope. */
6587 parser
->scope
= saved_scope
;
6588 parser
->qualifying_scope
= saved_qualifying_scope
;
6589 parser
->object_scope
= saved_object_scope
;
6590 /* If we are not looking at an identifier followed by the scope
6591 resolution operator, then this is not part of a
6592 nested-name-specifier. (Note that this function is only used
6593 to parse the components of a nested-name-specifier.) */
6594 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6595 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6596 return error_mark_node
;
6597 scope
= cp_parser_namespace_name (parser
);
6603 /* Return true if we are looking at a compound-literal, false otherwise. */
6606 cp_parser_compound_literal_p (cp_parser
*parser
)
6608 cp_lexer_save_tokens (parser
->lexer
);
6610 /* Skip tokens until the next token is a closing parenthesis.
6611 If we find the closing `)', and the next token is a `{', then
6612 we are looking at a compound-literal. */
6613 bool compound_literal_p
6614 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6615 /*consume_paren=*/true)
6616 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6618 /* Roll back the tokens we skipped. */
6619 cp_lexer_rollback_tokens (parser
->lexer
);
6621 return compound_literal_p
;
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625 of zero, without any folding, but ignoring location wrappers. */
6628 literal_integer_zerop (const_tree expr
)
6630 STRIP_ANY_LOCATION_WRAPPER (expr
);
6631 return integer_zerop (expr
);
6634 /* Parse a postfix-expression.
6638 postfix-expression [ expression ]
6639 postfix-expression ( expression-list [opt] )
6640 simple-type-specifier ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier identifier
6642 ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier template [opt] template-id
6644 ( expression-list [opt] )
6645 postfix-expression . template [opt] id-expression
6646 postfix-expression -> template [opt] id-expression
6647 postfix-expression . pseudo-destructor-name
6648 postfix-expression -> pseudo-destructor-name
6649 postfix-expression ++
6650 postfix-expression --
6651 dynamic_cast < type-id > ( expression )
6652 static_cast < type-id > ( expression )
6653 reinterpret_cast < type-id > ( expression )
6654 const_cast < type-id > ( expression )
6655 typeid ( expression )
6661 ( type-id ) { initializer-list , [opt] }
6663 This extension is a GNU version of the C99 compound-literal
6664 construct. (The C99 grammar uses `type-name' instead of `type-id',
6665 but they are essentially the same concept.)
6667 If ADDRESS_P is true, the postfix expression is the operand of the
6668 `&' operator. CAST_P is true if this expression is the target of a
6671 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672 class member access expressions [expr.ref].
6674 Returns a representation of the expression. */
6677 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6678 bool member_access_only_p
, bool decltype_p
,
6679 cp_id_kind
* pidk_return
)
6684 cp_id_kind idk
= CP_ID_KIND_NONE
;
6685 cp_expr postfix_expression
= NULL_TREE
;
6686 bool is_member_access
= false;
6688 /* Peek at the next token. */
6689 token
= cp_lexer_peek_token (parser
->lexer
);
6690 loc
= token
->location
;
6691 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6693 /* Some of the productions are determined by keywords. */
6694 keyword
= token
->keyword
;
6704 const char *saved_message
;
6705 bool saved_in_type_id_in_expr_p
;
6707 /* All of these can be handled in the same way from the point
6708 of view of parsing. Begin by consuming the token
6709 identifying the cast. */
6710 cp_lexer_consume_token (parser
->lexer
);
6712 /* New types cannot be defined in the cast. */
6713 saved_message
= parser
->type_definition_forbidden_message
;
6714 parser
->type_definition_forbidden_message
6715 = G_("types may not be defined in casts");
6717 /* Look for the opening `<'. */
6718 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6719 /* Parse the type to which we are casting. */
6720 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6721 parser
->in_type_id_in_expr_p
= true;
6722 type
= cp_parser_type_id (parser
);
6723 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6724 /* Look for the closing `>'. */
6725 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6726 /* Restore the old message. */
6727 parser
->type_definition_forbidden_message
= saved_message
;
6729 bool saved_greater_than_is_operator_p
6730 = parser
->greater_than_is_operator_p
;
6731 parser
->greater_than_is_operator_p
= true;
6733 /* And the expression which is being cast. */
6734 matching_parens parens
;
6735 parens
.require_open (parser
);
6736 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6737 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6739 location_t end_loc
= close_paren
?
6740 close_paren
->location
: UNKNOWN_LOCATION
;
6742 parser
->greater_than_is_operator_p
6743 = saved_greater_than_is_operator_p
;
6745 /* Only type conversions to integral or enumeration types
6746 can be used in constant-expressions. */
6747 if (!cast_valid_in_integral_constant_expression_p (type
)
6748 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6750 postfix_expression
= error_mark_node
;
6758 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6762 = build_static_cast (type
, expression
, tf_warning_or_error
);
6766 = build_reinterpret_cast (type
, expression
,
6767 tf_warning_or_error
);
6771 = build_const_cast (type
, expression
, tf_warning_or_error
);
6777 /* Construct a location e.g. :
6778 reinterpret_cast <int *> (expr)
6779 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 ranging from the start of the "*_cast" token to the final closing
6781 paren, with the caret at the start. */
6782 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6783 postfix_expression
.set_location (cp_cast_loc
);
6790 const char *saved_message
;
6791 bool saved_in_type_id_in_expr_p
;
6793 /* Consume the `typeid' token. */
6794 cp_lexer_consume_token (parser
->lexer
);
6795 /* Look for the `(' token. */
6796 matching_parens parens
;
6797 parens
.require_open (parser
);
6798 /* Types cannot be defined in a `typeid' expression. */
6799 saved_message
= parser
->type_definition_forbidden_message
;
6800 parser
->type_definition_forbidden_message
6801 = G_("types may not be defined in a %<typeid%> expression");
6802 /* We can't be sure yet whether we're looking at a type-id or an
6804 cp_parser_parse_tentatively (parser
);
6805 /* Try a type-id first. */
6806 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6807 parser
->in_type_id_in_expr_p
= true;
6808 type
= cp_parser_type_id (parser
);
6809 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6810 /* Look for the `)' token. Otherwise, we can't be sure that
6811 we're not looking at an expression: consider `typeid (int
6812 (3))', for example. */
6813 cp_token
*close_paren
= parens
.require_close (parser
);
6814 /* If all went well, simply lookup the type-id. */
6815 if (cp_parser_parse_definitely (parser
))
6816 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6817 /* Otherwise, fall back to the expression variant. */
6822 /* Look for an expression. */
6823 expression
= cp_parser_expression (parser
, & idk
);
6824 /* Compute its typeid. */
6825 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6826 /* Look for the `)' token. */
6827 close_paren
= parens
.require_close (parser
);
6829 /* Restore the saved message. */
6830 parser
->type_definition_forbidden_message
= saved_message
;
6831 /* `typeid' may not appear in an integral constant expression. */
6832 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6833 postfix_expression
= error_mark_node
;
6835 /* Construct a location e.g. :
6838 ranging from the start of the "typeid" token to the final closing
6839 paren, with the caret at the start. */
6842 location_t typeid_loc
6843 = make_location (start_loc
, start_loc
, close_paren
->location
);
6844 postfix_expression
.set_location (typeid_loc
);
6845 postfix_expression
.maybe_add_location_wrapper ();
6853 /* The syntax permitted here is the same permitted for an
6854 elaborated-type-specifier. */
6855 ++parser
->prevent_constrained_type_specifiers
;
6856 type
= cp_parser_elaborated_type_specifier (parser
,
6857 /*is_friend=*/false,
6858 /*is_declaration=*/false);
6859 --parser
->prevent_constrained_type_specifiers
;
6860 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6865 case RID_BUILTIN_SHUFFLE
:
6866 case RID_BUILTIN_LAUNDER
:
6868 vec
<tree
, va_gc
> *vec
;
6872 cp_lexer_consume_token (parser
->lexer
);
6873 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6874 /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 /*non_constant_p=*/NULL
);
6878 postfix_expression
= error_mark_node
;
6882 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6888 if (vec
->length () == 1)
6890 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6893 error_at (loc
, "wrong number of arguments to "
6894 "%<__builtin_addressof%>");
6895 postfix_expression
= error_mark_node
;
6899 case RID_BUILTIN_LAUNDER
:
6900 if (vec
->length () == 1)
6901 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6902 tf_warning_or_error
);
6905 error_at (loc
, "wrong number of arguments to "
6906 "%<__builtin_launder%>");
6907 postfix_expression
= error_mark_node
;
6911 case RID_BUILTIN_SHUFFLE
:
6912 if (vec
->length () == 2)
6914 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6915 (*vec
)[1], tf_warning_or_error
);
6916 else if (vec
->length () == 3)
6918 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6919 (*vec
)[2], tf_warning_or_error
);
6922 error_at (loc
, "wrong number of arguments to "
6923 "%<__builtin_shuffle%>");
6924 postfix_expression
= error_mark_node
;
6938 /* If the next thing is a simple-type-specifier, we may be
6939 looking at a functional cast. We could also be looking at
6940 an id-expression. So, we try the functional cast, and if
6941 that doesn't work we fall back to the primary-expression. */
6942 cp_parser_parse_tentatively (parser
);
6943 /* Look for the simple-type-specifier. */
6944 ++parser
->prevent_constrained_type_specifiers
;
6945 type
= cp_parser_simple_type_specifier (parser
,
6946 /*decl_specs=*/NULL
,
6947 CP_PARSER_FLAGS_NONE
);
6948 --parser
->prevent_constrained_type_specifiers
;
6949 /* Parse the cast itself. */
6950 if (!cp_parser_error_occurred (parser
))
6952 = cp_parser_functional_cast (parser
, type
);
6953 /* If that worked, we're done. */
6954 if (cp_parser_parse_definitely (parser
))
6957 /* If the functional-cast didn't work out, try a
6958 compound-literal. */
6959 if (cp_parser_allow_gnu_extensions_p (parser
)
6960 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6962 cp_expr initializer
= NULL_TREE
;
6964 cp_parser_parse_tentatively (parser
);
6966 matching_parens parens
;
6967 parens
.consume_open (parser
);
6969 /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 in cp_parser_cast_expression about c++/29234. */
6971 if (!cp_parser_compound_literal_p (parser
))
6972 cp_parser_simulate_error (parser
);
6975 /* Parse the type. */
6976 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6977 parser
->in_type_id_in_expr_p
= true;
6978 type
= cp_parser_type_id (parser
);
6979 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6980 parens
.require_close (parser
);
6983 /* If things aren't going well, there's no need to
6985 if (!cp_parser_error_occurred (parser
))
6987 bool non_constant_p
;
6988 /* Parse the brace-enclosed initializer list. */
6989 initializer
= cp_parser_braced_list (parser
,
6992 /* If that worked, we're definitely looking at a
6993 compound-literal expression. */
6994 if (cp_parser_parse_definitely (parser
))
6996 /* Warn the user that a compound literal is not
6997 allowed in standard C++. */
6998 pedwarn (input_location
, OPT_Wpedantic
,
6999 "ISO C++ forbids compound-literals");
7000 /* For simplicity, we disallow compound literals in
7001 constant-expressions. We could
7002 allow compound literals of integer type, whose
7003 initializer was a constant, in constant
7004 expressions. Permitting that usage, as a further
7005 extension, would not change the meaning of any
7006 currently accepted programs. (Of course, as
7007 compound literals are not part of ISO C++, the
7008 standard has nothing to say.) */
7009 if (cp_parser_non_integral_constant_expression (parser
,
7012 postfix_expression
= error_mark_node
;
7015 /* Form the representation of the compound-literal. */
7017 = finish_compound_literal (type
, initializer
,
7018 tf_warning_or_error
, fcl_c99
);
7019 postfix_expression
.set_location (initializer
.get_location ());
7024 /* It must be a primary-expression. */
7026 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7027 /*template_arg_p=*/false,
7034 /* Note that we don't need to worry about calling build_cplus_new on a
7035 class-valued CALL_EXPR in decltype when it isn't the end of the
7036 postfix-expression; unary_complex_lvalue will take care of that for
7039 /* Keep looping until the postfix-expression is complete. */
7042 if (idk
== CP_ID_KIND_UNQUALIFIED
7043 && identifier_p (postfix_expression
)
7044 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7045 /* It is not a Koenig lookup function call. */
7047 = unqualified_name_lookup_error (postfix_expression
);
7049 /* Peek at the next token. */
7050 token
= cp_lexer_peek_token (parser
->lexer
);
7052 switch (token
->type
)
7054 case CPP_OPEN_SQUARE
:
7055 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7057 cp_parser_error (parser
,
7058 "two consecutive %<[%> shall "
7059 "only introduce an attribute");
7060 return error_mark_node
;
7063 = cp_parser_postfix_open_square_expression (parser
,
7067 postfix_expression
.set_range (start_loc
,
7068 postfix_expression
.get_location ());
7070 idk
= CP_ID_KIND_NONE
;
7071 is_member_access
= false;
7074 case CPP_OPEN_PAREN
:
7075 /* postfix-expression ( expression-list [opt] ) */
7078 bool is_builtin_constant_p
;
7079 bool saved_integral_constant_expression_p
= false;
7080 bool saved_non_integral_constant_expression_p
= false;
7081 tsubst_flags_t complain
= complain_flags (decltype_p
);
7082 vec
<tree
, va_gc
> *args
;
7083 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7085 is_member_access
= false;
7087 is_builtin_constant_p
7088 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7089 if (is_builtin_constant_p
)
7091 /* The whole point of __builtin_constant_p is to allow
7092 non-constant expressions to appear as arguments. */
7093 saved_integral_constant_expression_p
7094 = parser
->integral_constant_expression_p
;
7095 saved_non_integral_constant_expression_p
7096 = parser
->non_integral_constant_expression_p
;
7097 parser
->integral_constant_expression_p
= false;
7099 args
= (cp_parser_parenthesized_expression_list
7101 /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 /*non_constant_p=*/NULL
,
7103 /*close_paren_loc=*/&close_paren_loc
,
7104 /*wrap_locations_p=*/true));
7105 if (is_builtin_constant_p
)
7107 parser
->integral_constant_expression_p
7108 = saved_integral_constant_expression_p
;
7109 parser
->non_integral_constant_expression_p
7110 = saved_non_integral_constant_expression_p
;
7115 postfix_expression
= error_mark_node
;
7119 /* Function calls are not permitted in
7120 constant-expressions. */
7121 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7122 && cp_parser_non_integral_constant_expression (parser
,
7125 postfix_expression
= error_mark_node
;
7126 release_tree_vector (args
);
7131 if (idk
== CP_ID_KIND_UNQUALIFIED
7132 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7134 if (identifier_p (postfix_expression
))
7136 if (!args
->is_empty ())
7139 if (!any_type_dependent_arguments_p (args
))
7141 = perform_koenig_lookup (postfix_expression
, args
,
7146 = unqualified_fn_lookup_error (postfix_expression
);
7148 /* We do not perform argument-dependent lookup if
7149 normal lookup finds a non-function, in accordance
7150 with the expected resolution of DR 218. */
7151 else if (!args
->is_empty ()
7152 && is_overloaded_fn (postfix_expression
))
7154 tree fn
= get_first_fn (postfix_expression
);
7155 fn
= STRIP_TEMPLATE (fn
);
7157 /* Do not do argument dependent lookup if regular
7158 lookup finds a member function or a block-scope
7159 function declaration. [basic.lookup.argdep]/3 */
7160 if (!DECL_FUNCTION_MEMBER_P (fn
)
7161 && !DECL_LOCAL_FUNCTION_P (fn
))
7164 if (!any_type_dependent_arguments_p (args
))
7166 = perform_koenig_lookup (postfix_expression
, args
,
7172 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7173 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7175 && vec_safe_length (args
) == 3)
7177 tree arg0
= (*args
)[0];
7178 tree arg1
= (*args
)[1];
7179 tree arg2
= (*args
)[2];
7180 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7181 | (literal_integer_zerop (arg2
) << 2));
7182 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7185 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7187 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7188 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7190 if (processing_template_decl
7191 && (type_dependent_object_expression_p (instance
)
7192 || (!BASELINK_P (fn
)
7193 && TREE_CODE (fn
) != FIELD_DECL
)
7194 || type_dependent_expression_p (fn
)
7195 || any_type_dependent_arguments_p (args
)))
7197 maybe_generic_this_capture (instance
, fn
);
7199 = build_min_nt_call_vec (postfix_expression
, args
);
7200 release_tree_vector (args
);
7204 if (BASELINK_P (fn
))
7207 = (build_new_method_call
7208 (instance
, fn
, &args
, NULL_TREE
,
7209 (idk
== CP_ID_KIND_QUALIFIED
7210 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7217 = finish_call_expr (postfix_expression
, &args
,
7218 /*disallow_virtual=*/false,
7222 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7223 || TREE_CODE (postfix_expression
) == MEMBER_REF
7224 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7225 postfix_expression
= (build_offset_ref_call_from_tree
7226 (postfix_expression
, &args
,
7228 else if (idk
== CP_ID_KIND_QUALIFIED
)
7229 /* A call to a static class member, or a namespace-scope
7232 = finish_call_expr (postfix_expression
, &args
,
7233 /*disallow_virtual=*/true,
7237 /* All other function calls. */
7239 = finish_call_expr (postfix_expression
, &args
,
7240 /*disallow_virtual=*/false,
7244 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7246 location_t combined_loc
= make_location (token
->location
,
7249 postfix_expression
.set_location (combined_loc
);
7252 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7253 idk
= CP_ID_KIND_NONE
;
7255 release_tree_vector (args
);
7261 /* postfix-expression . template [opt] id-expression
7262 postfix-expression . pseudo-destructor-name
7263 postfix-expression -> template [opt] id-expression
7264 postfix-expression -> pseudo-destructor-name */
7266 /* Consume the `.' or `->' operator. */
7267 cp_lexer_consume_token (parser
->lexer
);
7270 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7274 is_member_access
= true;
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 POSTINCREMENT_EXPR
);
7285 /* Increments may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7287 postfix_expression
= error_mark_node
;
7288 idk
= CP_ID_KIND_NONE
;
7289 is_member_access
= false;
7292 case CPP_MINUS_MINUS
:
7293 /* postfix-expression -- */
7294 /* Consume the `--' token. */
7295 cp_lexer_consume_token (parser
->lexer
);
7296 /* Generate a representation for the complete expression. */
7298 = finish_increment_expr (postfix_expression
,
7299 POSTDECREMENT_EXPR
);
7300 /* Decrements may not appear in constant-expressions. */
7301 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7302 postfix_expression
= error_mark_node
;
7303 idk
= CP_ID_KIND_NONE
;
7304 is_member_access
= false;
7308 if (pidk_return
!= NULL
)
7309 * pidk_return
= idk
;
7310 if (member_access_only_p
)
7311 return is_member_access
7312 ? postfix_expression
7313 : cp_expr (error_mark_node
);
7315 return postfix_expression
;
7319 /* We should never get here. */
7321 return error_mark_node
;
7324 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7325 by cp_parser_builtin_offsetof. We're looking for
7327 postfix-expression [ expression ]
7328 postfix-expression [ braced-init-list ] (C++11)
7330 FOR_OFFSETOF is set if we're being called in that context, which
7331 changes how we deal with integer constant expressions. */
7334 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7335 tree postfix_expression
,
7339 tree index
= NULL_TREE
;
7340 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7341 bool saved_greater_than_is_operator_p
;
7343 /* Consume the `[' token. */
7344 cp_lexer_consume_token (parser
->lexer
);
7346 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7347 parser
->greater_than_is_operator_p
= true;
7349 /* Parse the index expression. */
7350 /* ??? For offsetof, there is a question of what to allow here. If
7351 offsetof is not being used in an integral constant expression context,
7352 then we *could* get the right answer by computing the value at runtime.
7353 If we are in an integral constant expression context, then we might
7354 could accept any constant expression; hard to say without analysis.
7355 Rather than open the barn door too wide right away, allow only integer
7356 constant expressions here. */
7358 index
= cp_parser_constant_expression (parser
);
7361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7363 bool expr_nonconst_p
;
7364 cp_lexer_set_source_position (parser
->lexer
);
7365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7366 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7369 index
= cp_parser_expression (parser
);
7372 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7374 /* Look for the closing `]'. */
7375 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7377 /* Build the ARRAY_REF. */
7378 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7381 /* When not doing offsetof, array references are not permitted in
7382 constant-expressions. */
7384 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7385 postfix_expression
= error_mark_node
;
7387 return postfix_expression
;
7390 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7391 by cp_parser_builtin_offsetof. We're looking for
7393 postfix-expression . template [opt] id-expression
7394 postfix-expression . pseudo-destructor-name
7395 postfix-expression -> template [opt] id-expression
7396 postfix-expression -> pseudo-destructor-name
7398 FOR_OFFSETOF is set if we're being called in that context. That sorta
7399 limits what of the above we'll actually accept, but nevermind.
7400 TOKEN_TYPE is the "." or "->" token, which will already have been
7401 removed from the stream. */
7404 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7405 enum cpp_ttype token_type
,
7406 cp_expr postfix_expression
,
7407 bool for_offsetof
, cp_id_kind
*idk
,
7408 location_t location
)
7412 bool pseudo_destructor_p
;
7413 tree scope
= NULL_TREE
;
7414 location_t start_loc
= postfix_expression
.get_start ();
7416 /* If this is a `->' operator, dereference the pointer. */
7417 if (token_type
== CPP_DEREF
)
7418 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7419 tf_warning_or_error
);
7420 /* Check to see whether or not the expression is type-dependent and
7421 not the current instantiation. */
7422 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7423 /* The identifier following the `->' or `.' is not qualified. */
7424 parser
->scope
= NULL_TREE
;
7425 parser
->qualifying_scope
= NULL_TREE
;
7426 parser
->object_scope
= NULL_TREE
;
7427 *idk
= CP_ID_KIND_NONE
;
7429 /* Enter the scope corresponding to the type of the object
7430 given by the POSTFIX_EXPRESSION. */
7433 scope
= TREE_TYPE (postfix_expression
);
7434 /* According to the standard, no expression should ever have
7435 reference type. Unfortunately, we do not currently match
7436 the standard in this respect in that our internal representation
7437 of an expression may have reference type even when the standard
7438 says it does not. Therefore, we have to manually obtain the
7439 underlying type here. */
7440 scope
= non_reference (scope
);
7441 /* The type of the POSTFIX_EXPRESSION must be complete. */
7442 /* Unlike the object expression in other contexts, *this is not
7443 required to be of complete type for purposes of class member
7444 access (5.2.5) outside the member function body. */
7445 if (postfix_expression
!= current_class_ref
7446 && scope
!= error_mark_node
7447 && !(processing_template_decl
7448 && current_class_type
7449 && (same_type_ignoring_top_level_qualifiers_p
7450 (scope
, current_class_type
))))
7452 scope
= complete_type (scope
);
7453 if (!COMPLETE_TYPE_P (scope
)
7454 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7455 && EXPR_P (postfix_expression
))
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind
= (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (scope
)
7463 cxx_incomplete_type_diagnostic
7464 (location_of (postfix_expression
),
7465 postfix_expression
, scope
, kind
);
7466 if (!MAYBE_CLASS_TYPE_P (scope
))
7467 return error_mark_node
;
7468 if (processing_template_decl
)
7471 scope
= TREE_TYPE (postfix_expression
) = NULL_TREE
;
7478 /* Let the name lookup machinery know that we are processing a
7479 class member access expression. */
7480 parser
->context
->object_type
= scope
;
7481 /* If something went wrong, we want to be able to discern that case,
7482 as opposed to the case where there was no SCOPE due to the type
7483 of expression being dependent. */
7485 scope
= error_mark_node
;
7486 /* If the SCOPE was erroneous, make the various semantic analysis
7487 functions exit quickly -- and without issuing additional error
7489 if (scope
== error_mark_node
)
7490 postfix_expression
= error_mark_node
;
7495 /* Tell cp_parser_lookup_name that there was an object, even though it's
7497 parser
->context
->object_type
= unknown_type_node
;
7499 /* Assume this expression is not a pseudo-destructor access. */
7500 pseudo_destructor_p
= false;
7502 /* If the SCOPE is a scalar type, then, if this is a valid program,
7503 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7504 is type dependent, it can be pseudo-destructor-name or something else.
7505 Try to parse it as pseudo-destructor-name first. */
7506 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7511 cp_parser_parse_tentatively (parser
);
7512 /* Parse the pseudo-destructor-name. */
7514 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7517 && (cp_parser_error_occurred (parser
)
7518 || !SCALAR_TYPE_P (type
)))
7519 cp_parser_abort_tentative_parse (parser
);
7520 else if (cp_parser_parse_definitely (parser
))
7522 pseudo_destructor_p
= true;
7524 = finish_pseudo_destructor_expr (postfix_expression
,
7529 if (!pseudo_destructor_p
)
7531 /* If the SCOPE is not a scalar type, we are looking at an
7532 ordinary class member access expression, rather than a
7533 pseudo-destructor-name. */
7535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7536 /* Parse the id-expression. */
7537 name
= (cp_parser_id_expression
7539 cp_parser_optional_template_keyword (parser
),
7540 /*check_dependency_p=*/true,
7542 /*declarator_p=*/false,
7543 /*optional_p=*/false));
7544 /* In general, build a SCOPE_REF if the member name is qualified.
7545 However, if the name was not dependent and has already been
7546 resolved; there is no need to build the SCOPE_REF. For example;
7548 struct X { void f(); };
7549 template <typename T> void f(T* t) { t->X::f(); }
7551 Even though "t" is dependent, "X::f" is not and has been resolved
7552 to a BASELINK; there is no need to include scope information. */
7554 /* But we do need to remember that there was an explicit scope for
7555 virtual function calls. */
7557 *idk
= CP_ID_KIND_QUALIFIED
;
7559 /* If the name is a template-id that names a type, we will get a
7560 TYPE_DECL here. That is invalid code. */
7561 if (TREE_CODE (name
) == TYPE_DECL
)
7563 error_at (token
->location
, "invalid use of %qD", name
);
7564 postfix_expression
= error_mark_node
;
7568 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7570 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7572 error_at (token
->location
, "%<%D::%D%> is not a class member",
7573 parser
->scope
, name
);
7574 postfix_expression
= error_mark_node
;
7577 name
= build_qualified_name (/*type=*/NULL_TREE
,
7581 parser
->scope
= NULL_TREE
;
7582 parser
->qualifying_scope
= NULL_TREE
;
7583 parser
->object_scope
= NULL_TREE
;
7585 if (parser
->scope
&& name
&& BASELINK_P (name
))
7586 adjust_result_of_qualified_name_lookup
7587 (name
, parser
->scope
, scope
);
7589 = finish_class_member_access_expr (postfix_expression
, name
,
7591 tf_warning_or_error
);
7592 /* Build a location e.g.:
7595 where the caret is at the deref token, ranging from
7596 the start of postfix_expression to the end of the access expr. */
7598 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7599 location_t combined_loc
7600 = make_location (input_location
, start_loc
, end_loc
);
7601 protected_set_expr_location (postfix_expression
, combined_loc
);
7605 /* We no longer need to look up names in the scope of the object on
7606 the left-hand side of the `.' or `->' operator. */
7607 parser
->context
->object_type
= NULL_TREE
;
7609 /* Outside of offsetof, these operators may not appear in
7610 constant-expressions. */
7612 && (cp_parser_non_integral_constant_expression
7613 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7614 postfix_expression
= error_mark_node
;
7616 return postfix_expression
;
7619 /* Parse a parenthesized expression-list.
7622 assignment-expression
7623 expression-list, assignment-expression
7628 identifier, expression-list
7630 CAST_P is true if this expression is the target of a cast.
7632 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7635 WRAP_LOCATIONS_P is true if expressions within this list for which
7636 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7637 their source locations.
7639 Returns a vector of trees. Each element is a representation of an
7640 assignment-expression. NULL is returned if the ( and or ) are
7641 missing. An empty, but allocated, vector is returned on no
7642 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7643 if we are parsing an attribute list for an attribute that wants a
7644 plain identifier argument, normal_attr for an attribute that wants
7645 an expression, or non_attr if we aren't parsing an attribute list. If
7646 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7647 not all of the expressions in the list were constant.
7648 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7649 will be written to with the location of the closing parenthesis. If
7650 an error occurs, it may or may not be written to. */
7652 static vec
<tree
, va_gc
> *
7653 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7654 int is_attribute_list
,
7656 bool allow_expansion_p
,
7657 bool *non_constant_p
,
7658 location_t
*close_paren_loc
,
7659 bool wrap_locations_p
)
7661 vec
<tree
, va_gc
> *expression_list
;
7662 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7663 tree identifier
= NULL_TREE
;
7664 bool saved_greater_than_is_operator_p
;
7666 /* Assume all the expressions will be constant. */
7668 *non_constant_p
= false;
7670 matching_parens parens
;
7671 if (!parens
.require_open (parser
))
7674 expression_list
= make_tree_vector ();
7676 /* Within a parenthesized expression, a `>' token is always
7677 the greater-than operator. */
7678 saved_greater_than_is_operator_p
7679 = parser
->greater_than_is_operator_p
;
7680 parser
->greater_than_is_operator_p
= true;
7682 cp_expr
expr (NULL_TREE
);
7684 /* Consume expressions until there are no more. */
7685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7688 /* At the beginning of attribute lists, check to see if the
7689 next token is an identifier. */
7690 if (is_attribute_list
== id_attr
7691 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7695 /* Consume the identifier. */
7696 token
= cp_lexer_consume_token (parser
->lexer
);
7697 /* Save the identifier. */
7698 identifier
= token
->u
.value
;
7702 bool expr_non_constant_p
;
7704 /* Parse the next assignment-expression. */
7705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7707 /* A braced-init-list. */
7708 cp_lexer_set_source_position (parser
->lexer
);
7709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7710 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7711 if (non_constant_p
&& expr_non_constant_p
)
7712 *non_constant_p
= true;
7714 else if (non_constant_p
)
7716 expr
= (cp_parser_constant_expression
7717 (parser
, /*allow_non_constant_p=*/true,
7718 &expr_non_constant_p
));
7719 if (expr_non_constant_p
)
7720 *non_constant_p
= true;
7723 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7727 expr
= instantiate_non_dependent_expr (expr
);
7729 /* If we have an ellipsis, then this is an expression
7731 if (allow_expansion_p
7732 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7734 /* Consume the `...'. */
7735 cp_lexer_consume_token (parser
->lexer
);
7737 /* Build the argument pack. */
7738 expr
= make_pack_expansion (expr
);
7741 if (wrap_locations_p
)
7742 expr
.maybe_add_location_wrapper ();
7744 /* Add it to the list. We add error_mark_node
7745 expressions to the list, so that we can still tell if
7746 the correct form for a parenthesized expression-list
7747 is found. That gives better errors. */
7748 vec_safe_push (expression_list
, expr
.get_value ());
7750 if (expr
== error_mark_node
)
7754 /* After the first item, attribute lists look the same as
7755 expression lists. */
7756 is_attribute_list
= non_attr
;
7759 /* If the next token isn't a `,', then we are done. */
7760 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7763 /* Otherwise, consume the `,' and keep going. */
7764 cp_lexer_consume_token (parser
->lexer
);
7767 if (close_paren_loc
)
7768 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7770 if (!parens
.require_close (parser
))
7775 /* We try and resync to an unnested comma, as that will give the
7776 user better diagnostics. */
7777 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7778 /*recovering=*/true,
7780 /*consume_paren=*/true);
7785 parser
->greater_than_is_operator_p
7786 = saved_greater_than_is_operator_p
;
7791 parser
->greater_than_is_operator_p
7792 = saved_greater_than_is_operator_p
;
7795 vec_safe_insert (expression_list
, 0, identifier
);
7797 return expression_list
;
7800 /* Parse a pseudo-destructor-name.
7802 pseudo-destructor-name:
7803 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7804 :: [opt] nested-name-specifier template template-id :: ~ type-name
7805 :: [opt] nested-name-specifier [opt] ~ type-name
7807 If either of the first two productions is used, sets *SCOPE to the
7808 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7809 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7810 or ERROR_MARK_NODE if the parse fails. */
7813 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7818 bool nested_name_specifier_p
;
7821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7822 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7823 && !type_dependent_expression_p (object
))
7825 if (cxx_dialect
< cxx14
)
7826 pedwarn (input_location
, 0,
7827 "%<~auto%> only available with "
7828 "-std=c++14 or -std=gnu++14");
7829 cp_lexer_consume_token (parser
->lexer
);
7830 cp_lexer_consume_token (parser
->lexer
);
7832 *type
= TREE_TYPE (object
);
7836 /* Assume that things will not work out. */
7837 *type
= error_mark_node
;
7839 /* Look for the optional `::' operator. */
7840 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7841 /* Look for the optional nested-name-specifier. */
7842 nested_name_specifier_p
7843 = (cp_parser_nested_name_specifier_opt (parser
,
7844 /*typename_keyword_p=*/false,
7845 /*check_dependency_p=*/true,
7847 /*is_declaration=*/false)
7849 /* Now, if we saw a nested-name-specifier, we might be doing the
7850 second production. */
7851 if (nested_name_specifier_p
7852 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7854 /* Consume the `template' keyword. */
7855 cp_lexer_consume_token (parser
->lexer
);
7856 /* Parse the template-id. */
7857 cp_parser_template_id (parser
,
7858 /*template_keyword_p=*/true,
7859 /*check_dependency_p=*/false,
7861 /*is_declaration=*/true);
7862 /* Look for the `::' token. */
7863 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7865 /* If the next token is not a `~', then there might be some
7866 additional qualification. */
7867 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7869 /* At this point, we're looking for "type-name :: ~". The type-name
7870 must not be a class-name, since this is a pseudo-destructor. So,
7871 it must be either an enum-name, or a typedef-name -- both of which
7872 are just identifiers. So, we peek ahead to check that the "::"
7873 and "~" tokens are present; if they are not, then we can avoid
7874 calling type_name. */
7875 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7876 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7877 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7879 cp_parser_error (parser
, "non-scalar type");
7883 /* Look for the type-name. */
7884 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7885 if (*scope
== error_mark_node
)
7888 /* Look for the `::' token. */
7889 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7894 /* Look for the `~'. */
7895 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7897 /* Once we see the ~, this has to be a pseudo-destructor. */
7898 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7899 cp_parser_commit_to_topmost_tentative_parse (parser
);
7901 /* Look for the type-name again. We are not responsible for
7902 checking that it matches the first type-name. */
7903 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7906 /* Parse a unary-expression.
7912 unary-operator cast-expression
7913 sizeof unary-expression
7915 alignof ( type-id ) [C++0x]
7922 __extension__ cast-expression
7923 __alignof__ unary-expression
7924 __alignof__ ( type-id )
7925 alignof unary-expression [C++0x]
7926 __real__ cast-expression
7927 __imag__ cast-expression
7929 sizeof ( type-id ) { initializer-list , [opt] }
7930 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7931 __alignof__ ( type-id ) { initializer-list , [opt] }
7933 ADDRESS_P is true iff the unary-expression is appearing as the
7934 operand of the `&' operator. CAST_P is true if this expression is
7935 the target of a cast.
7937 Returns a representation of the expression. */
7940 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7941 bool address_p
, bool cast_p
, bool decltype_p
)
7944 enum tree_code unary_operator
;
7946 /* Peek at the next token. */
7947 token
= cp_lexer_peek_token (parser
->lexer
);
7948 /* Some keywords give away the kind of expression. */
7949 if (token
->type
== CPP_KEYWORD
)
7951 enum rid keyword
= token
->keyword
;
7960 location_t start_loc
= token
->location
;
7962 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7963 /* Consume the token. */
7964 cp_lexer_consume_token (parser
->lexer
);
7965 /* Parse the operand. */
7966 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7968 if (TYPE_P (operand
))
7969 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7972 /* ISO C++ defines alignof only with types, not with
7973 expressions. So pedwarn if alignof is used with a non-
7974 type expression. However, __alignof__ is ok. */
7975 if (id_equal (token
->u
.value
, "alignof"))
7976 pedwarn (token
->location
, OPT_Wpedantic
,
7977 "ISO C++ does not allow %<alignof%> "
7980 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7982 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7983 SIZEOF_EXPR with the original operand. */
7984 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7986 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7988 if (!processing_template_decl
&& TYPE_P (operand
))
7990 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7991 build1 (NOP_EXPR
, operand
,
7993 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7996 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7997 TREE_SIDE_EFFECTS (ret
) = 0;
7998 TREE_READONLY (ret
) = 1;
8002 /* Construct a location e.g. :
8005 with start == caret at the start of the "alignof"/"sizeof"
8006 token, with the endpoint at the final closing paren. */
8007 location_t finish_loc
8008 = cp_lexer_previous_token (parser
->lexer
)->location
;
8009 location_t compound_loc
8010 = make_location (start_loc
, start_loc
, finish_loc
);
8012 cp_expr
ret_expr (ret
);
8013 ret_expr
.set_location (compound_loc
);
8014 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8019 return cp_parser_new_expression (parser
);
8022 return cp_parser_delete_expression (parser
);
8026 /* The saved value of the PEDANTIC flag. */
8030 /* Save away the PEDANTIC flag. */
8031 cp_parser_extension_opt (parser
, &saved_pedantic
);
8032 /* Parse the cast-expression. */
8033 expr
= cp_parser_simple_cast_expression (parser
);
8034 /* Restore the PEDANTIC flag. */
8035 pedantic
= saved_pedantic
;
8045 /* Consume the `__real__' or `__imag__' token. */
8046 cp_lexer_consume_token (parser
->lexer
);
8047 /* Parse the cast-expression. */
8048 expression
= cp_parser_simple_cast_expression (parser
);
8049 /* Create the complete representation. */
8050 return build_x_unary_op (token
->location
,
8051 (keyword
== RID_REALPART
8052 ? REALPART_EXPR
: IMAGPART_EXPR
),
8054 tf_warning_or_error
);
8058 case RID_TRANSACTION_ATOMIC
:
8059 case RID_TRANSACTION_RELAXED
:
8060 return cp_parser_transaction_expression (parser
, keyword
);
8065 const char *saved_message
;
8066 bool saved_integral_constant_expression_p
;
8067 bool saved_non_integral_constant_expression_p
;
8068 bool saved_greater_than_is_operator_p
;
8070 location_t start_loc
= token
->location
;
8072 cp_lexer_consume_token (parser
->lexer
);
8073 matching_parens parens
;
8074 parens
.require_open (parser
);
8076 saved_message
= parser
->type_definition_forbidden_message
;
8077 parser
->type_definition_forbidden_message
8078 = G_("types may not be defined in %<noexcept%> expressions");
8080 saved_integral_constant_expression_p
8081 = parser
->integral_constant_expression_p
;
8082 saved_non_integral_constant_expression_p
8083 = parser
->non_integral_constant_expression_p
;
8084 parser
->integral_constant_expression_p
= false;
8086 saved_greater_than_is_operator_p
8087 = parser
->greater_than_is_operator_p
;
8088 parser
->greater_than_is_operator_p
= true;
8090 ++cp_unevaluated_operand
;
8091 ++c_inhibit_evaluation_warnings
;
8092 ++cp_noexcept_operand
;
8093 expr
= cp_parser_expression (parser
);
8094 --cp_noexcept_operand
;
8095 --c_inhibit_evaluation_warnings
;
8096 --cp_unevaluated_operand
;
8098 parser
->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p
;
8101 parser
->integral_constant_expression_p
8102 = saved_integral_constant_expression_p
;
8103 parser
->non_integral_constant_expression_p
8104 = saved_non_integral_constant_expression_p
;
8106 parser
->type_definition_forbidden_message
= saved_message
;
8108 location_t finish_loc
8109 = cp_lexer_peek_token (parser
->lexer
)->location
;
8110 parens
.require_close (parser
);
8112 /* Construct a location of the form:
8115 with start == caret, finishing at the close-paren. */
8116 location_t noexcept_loc
8117 = make_location (start_loc
, start_loc
, finish_loc
);
8119 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8128 /* Look for the `:: new' and `:: delete', which also signal the
8129 beginning of a new-expression, or delete-expression,
8130 respectively. If the next token is `::', then it might be one of
8132 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8136 /* See if the token after the `::' is one of the keywords in
8137 which we're interested. */
8138 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8139 /* If it's `new', we have a new-expression. */
8140 if (keyword
== RID_NEW
)
8141 return cp_parser_new_expression (parser
);
8142 /* Similarly, for `delete'. */
8143 else if (keyword
== RID_DELETE
)
8144 return cp_parser_delete_expression (parser
);
8147 /* Look for a unary operator. */
8148 unary_operator
= cp_parser_unary_operator (token
);
8149 /* The `++' and `--' operators can be handled similarly, even though
8150 they are not technically unary-operators in the grammar. */
8151 if (unary_operator
== ERROR_MARK
)
8153 if (token
->type
== CPP_PLUS_PLUS
)
8154 unary_operator
= PREINCREMENT_EXPR
;
8155 else if (token
->type
== CPP_MINUS_MINUS
)
8156 unary_operator
= PREDECREMENT_EXPR
;
8157 /* Handle the GNU address-of-label extension. */
8158 else if (cp_parser_allow_gnu_extensions_p (parser
)
8159 && token
->type
== CPP_AND_AND
)
8163 location_t start_loc
= token
->location
;
8165 /* Consume the '&&' token. */
8166 cp_lexer_consume_token (parser
->lexer
);
8167 /* Look for the identifier. */
8168 location_t finish_loc
8169 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8170 identifier
= cp_parser_identifier (parser
);
8171 /* Construct a location of the form:
8174 with caret==start at the "&&", finish at the end of the label. */
8175 location_t combined_loc
8176 = make_location (start_loc
, start_loc
, finish_loc
);
8177 /* Create an expression representing the address. */
8178 expression
= finish_label_address_expr (identifier
, combined_loc
);
8179 if (cp_parser_non_integral_constant_expression (parser
,
8181 expression
= error_mark_node
;
8185 if (unary_operator
!= ERROR_MARK
)
8187 cp_expr cast_expression
;
8188 cp_expr expression
= error_mark_node
;
8189 non_integral_constant non_constant_p
= NIC_NONE
;
8190 location_t loc
= token
->location
;
8191 tsubst_flags_t complain
= complain_flags (decltype_p
);
8193 /* Consume the operator token. */
8194 token
= cp_lexer_consume_token (parser
->lexer
);
8195 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8197 /* Parse the cast-expression. */
8199 = cp_parser_cast_expression (parser
,
8200 unary_operator
== ADDR_EXPR
,
8206 OP_TOKEN CAST_EXPRESSION
8207 ^~~~~~~~~~~~~~~~~~~~~~~~~
8208 with start==caret at the operator token, and
8209 extending to the end of the cast_expression. */
8210 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8212 /* Now, build an appropriate representation. */
8213 switch (unary_operator
)
8216 non_constant_p
= NIC_STAR
;
8217 expression
= build_x_indirect_ref (loc
, cast_expression
,
8220 /* TODO: build_x_indirect_ref does not always honor the
8221 location, so ensure it is set. */
8222 expression
.set_location (loc
);
8226 non_constant_p
= NIC_ADDR
;
8229 expression
= build_x_unary_op (loc
, unary_operator
,
8232 /* TODO: build_x_unary_op does not always honor the location,
8233 so ensure it is set. */
8234 expression
.set_location (loc
);
8237 case PREINCREMENT_EXPR
:
8238 case PREDECREMENT_EXPR
:
8239 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8240 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8243 /* Immediately fold negation of a constant, unless the constant is 0
8244 (since -0 == 0) or it would overflow. */
8245 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8246 && CONSTANT_CLASS_P (cast_expression
)
8247 && !integer_zerop (cast_expression
)
8248 && !TREE_OVERFLOW (cast_expression
))
8250 tree folded
= fold_build1 (unary_operator
,
8251 TREE_TYPE (cast_expression
),
8253 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8255 expression
= cp_expr (folded
, loc
);
8260 case UNARY_PLUS_EXPR
:
8261 case TRUTH_NOT_EXPR
:
8262 expression
= finish_unary_op_expr (loc
, unary_operator
,
8263 cast_expression
, complain
);
8270 if (non_constant_p
!= NIC_NONE
8271 && cp_parser_non_integral_constant_expression (parser
,
8273 expression
= error_mark_node
;
8278 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8279 /*member_access_only_p=*/false,
8284 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8285 unary-operator, the corresponding tree code is returned. */
8287 static enum tree_code
8288 cp_parser_unary_operator (cp_token
* token
)
8290 switch (token
->type
)
8293 return INDIRECT_REF
;
8299 return UNARY_PLUS_EXPR
;
8305 return TRUTH_NOT_EXPR
;
8308 return BIT_NOT_EXPR
;
8315 /* Parse a new-expression.
8318 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8319 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8321 Returns a representation of the expression. */
8324 cp_parser_new_expression (cp_parser
* parser
)
8326 bool global_scope_p
;
8327 vec
<tree
, va_gc
> *placement
;
8329 vec
<tree
, va_gc
> *initializer
;
8330 tree nelts
= NULL_TREE
;
8333 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8335 /* Look for the optional `::' operator. */
8337 = (cp_parser_global_scope_opt (parser
,
8338 /*current_scope_valid_p=*/false)
8340 /* Look for the `new' operator. */
8341 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8342 /* There's no easy way to tell a new-placement from the
8343 `( type-id )' construct. */
8344 cp_parser_parse_tentatively (parser
);
8345 /* Look for a new-placement. */
8346 placement
= cp_parser_new_placement (parser
);
8347 /* If that didn't work out, there's no new-placement. */
8348 if (!cp_parser_parse_definitely (parser
))
8350 if (placement
!= NULL
)
8351 release_tree_vector (placement
);
8355 /* If the next token is a `(', then we have a parenthesized
8357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8360 const char *saved_message
= parser
->type_definition_forbidden_message
;
8362 /* Consume the `('. */
8363 matching_parens parens
;
8364 parens
.consume_open (parser
);
8366 /* Parse the type-id. */
8367 parser
->type_definition_forbidden_message
8368 = G_("types may not be defined in a new-expression");
8370 type_id_in_expr_sentinel
s (parser
);
8371 type
= cp_parser_type_id (parser
);
8373 parser
->type_definition_forbidden_message
= saved_message
;
8375 /* Look for the closing `)'. */
8376 parens
.require_close (parser
);
8377 token
= cp_lexer_peek_token (parser
->lexer
);
8378 /* There should not be a direct-new-declarator in this production,
8379 but GCC used to allowed this, so we check and emit a sensible error
8380 message for this case. */
8381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8383 error_at (token
->location
,
8384 "array bound forbidden after parenthesized type-id");
8385 inform (token
->location
,
8386 "try removing the parentheses around the type-id");
8387 cp_parser_direct_new_declarator (parser
);
8390 /* Otherwise, there must be a new-type-id. */
8392 type
= cp_parser_new_type_id (parser
, &nelts
);
8394 /* If the next token is a `(' or '{', then we have a new-initializer. */
8395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8396 if (token
->type
== CPP_OPEN_PAREN
8397 || token
->type
== CPP_OPEN_BRACE
)
8398 initializer
= cp_parser_new_initializer (parser
);
8402 /* A new-expression may not appear in an integral constant
8404 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8405 ret
= error_mark_node
;
8406 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8407 of a new-type-id or type-id of a new-expression, the new-expression shall
8408 contain a new-initializer of the form ( assignment-expression )".
8409 Additionally, consistently with the spirit of DR 1467, we want to accept
8410 'new auto { 2 }' too. */
8411 else if ((ret
= type_uses_auto (type
))
8412 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8413 && (vec_safe_length (initializer
) != 1
8414 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8415 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8417 error_at (token
->location
,
8418 "initialization of new-expression for type %<auto%> "
8419 "requires exactly one element");
8420 ret
= error_mark_node
;
8424 /* Construct a location e.g.:
8427 with caret == start at the start of the "new" token, and the end
8428 at the end of the final token we consumed. */
8429 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8430 location_t end_loc
= get_finish (end_tok
->location
);
8431 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8433 /* Create a representation of the new-expression. */
8434 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8435 tf_warning_or_error
);
8436 protected_set_expr_location (ret
, combined_loc
);
8439 if (placement
!= NULL
)
8440 release_tree_vector (placement
);
8441 if (initializer
!= NULL
)
8442 release_tree_vector (initializer
);
8447 /* Parse a new-placement.
8452 Returns the same representation as for an expression-list. */
8454 static vec
<tree
, va_gc
> *
8455 cp_parser_new_placement (cp_parser
* parser
)
8457 vec
<tree
, va_gc
> *expression_list
;
8459 /* Parse the expression-list. */
8460 expression_list
= (cp_parser_parenthesized_expression_list
8461 (parser
, non_attr
, /*cast_p=*/false,
8462 /*allow_expansion_p=*/true,
8463 /*non_constant_p=*/NULL
));
8465 if (expression_list
&& expression_list
->is_empty ())
8466 error ("expected expression-list or type-id");
8468 return expression_list
;
8471 /* Parse a new-type-id.
8474 type-specifier-seq new-declarator [opt]
8476 Returns the TYPE allocated. If the new-type-id indicates an array
8477 type, *NELTS is set to the number of elements in the last array
8478 bound; the TYPE will not include the last array bound. */
8481 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8483 cp_decl_specifier_seq type_specifier_seq
;
8484 cp_declarator
*new_declarator
;
8485 cp_declarator
*declarator
;
8486 cp_declarator
*outer_declarator
;
8487 const char *saved_message
;
8489 /* The type-specifier sequence must not contain type definitions.
8490 (It cannot contain declarations of new types either, but if they
8491 are not definitions we will catch that because they are not
8493 saved_message
= parser
->type_definition_forbidden_message
;
8494 parser
->type_definition_forbidden_message
8495 = G_("types may not be defined in a new-type-id");
8496 /* Parse the type-specifier-seq. */
8497 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8498 /*is_trailing_return=*/false,
8499 &type_specifier_seq
);
8500 /* Restore the old message. */
8501 parser
->type_definition_forbidden_message
= saved_message
;
8503 if (type_specifier_seq
.type
== error_mark_node
)
8504 return error_mark_node
;
8506 /* Parse the new-declarator. */
8507 new_declarator
= cp_parser_new_declarator_opt (parser
);
8509 /* Determine the number of elements in the last array dimension, if
8512 /* Skip down to the last array dimension. */
8513 declarator
= new_declarator
;
8514 outer_declarator
= NULL
;
8515 while (declarator
&& (declarator
->kind
== cdk_pointer
8516 || declarator
->kind
== cdk_ptrmem
))
8518 outer_declarator
= declarator
;
8519 declarator
= declarator
->declarator
;
8522 && declarator
->kind
== cdk_array
8523 && declarator
->declarator
8524 && declarator
->declarator
->kind
== cdk_array
)
8526 outer_declarator
= declarator
;
8527 declarator
= declarator
->declarator
;
8530 if (declarator
&& declarator
->kind
== cdk_array
)
8532 *nelts
= declarator
->u
.array
.bounds
;
8533 if (*nelts
== error_mark_node
)
8534 *nelts
= integer_one_node
;
8536 if (outer_declarator
)
8537 outer_declarator
->declarator
= declarator
->declarator
;
8539 new_declarator
= NULL
;
8542 return groktypename (&type_specifier_seq
, new_declarator
, false);
8545 /* Parse an (optional) new-declarator.
8548 ptr-operator new-declarator [opt]
8549 direct-new-declarator
8551 Returns the declarator. */
8553 static cp_declarator
*
8554 cp_parser_new_declarator_opt (cp_parser
* parser
)
8556 enum tree_code code
;
8557 tree type
, std_attributes
= NULL_TREE
;
8558 cp_cv_quals cv_quals
;
8560 /* We don't know if there's a ptr-operator next, or not. */
8561 cp_parser_parse_tentatively (parser
);
8562 /* Look for a ptr-operator. */
8563 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8564 /* If that worked, look for more new-declarators. */
8565 if (cp_parser_parse_definitely (parser
))
8567 cp_declarator
*declarator
;
8569 /* Parse another optional declarator. */
8570 declarator
= cp_parser_new_declarator_opt (parser
);
8572 declarator
= cp_parser_make_indirect_declarator
8573 (code
, type
, cv_quals
, declarator
, std_attributes
);
8578 /* If the next token is a `[', there is a direct-new-declarator. */
8579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8580 return cp_parser_direct_new_declarator (parser
);
8585 /* Parse a direct-new-declarator.
8587 direct-new-declarator:
8589 direct-new-declarator [constant-expression]
8593 static cp_declarator
*
8594 cp_parser_direct_new_declarator (cp_parser
* parser
)
8596 cp_declarator
*declarator
= NULL
;
8603 /* Look for the opening `['. */
8604 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8606 token
= cp_lexer_peek_token (parser
->lexer
);
8607 expression
= cp_parser_expression (parser
);
8608 /* The standard requires that the expression have integral
8609 type. DR 74 adds enumeration types. We believe that the
8610 real intent is that these expressions be handled like the
8611 expression in a `switch' condition, which also allows
8612 classes with a single conversion to integral or
8613 enumeration type. */
8614 if (!processing_template_decl
)
8617 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8622 error_at (token
->location
,
8623 "expression in new-declarator must have integral "
8624 "or enumeration type");
8625 expression
= error_mark_node
;
8629 /* Look for the closing `]'. */
8630 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8632 /* Add this bound to the declarator. */
8633 declarator
= make_array_declarator (declarator
, expression
);
8635 /* If the next token is not a `[', then there are no more
8637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8644 /* Parse a new-initializer.
8647 ( expression-list [opt] )
8650 Returns a representation of the expression-list. */
8652 static vec
<tree
, va_gc
> *
8653 cp_parser_new_initializer (cp_parser
* parser
)
8655 vec
<tree
, va_gc
> *expression_list
;
8657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8660 bool expr_non_constant_p
;
8661 cp_lexer_set_source_position (parser
->lexer
);
8662 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8663 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8664 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8665 expression_list
= make_tree_vector_single (t
);
8668 expression_list
= (cp_parser_parenthesized_expression_list
8669 (parser
, non_attr
, /*cast_p=*/false,
8670 /*allow_expansion_p=*/true,
8671 /*non_constant_p=*/NULL
));
8673 return expression_list
;
8676 /* Parse a delete-expression.
8679 :: [opt] delete cast-expression
8680 :: [opt] delete [ ] cast-expression
8682 Returns a representation of the expression. */
8685 cp_parser_delete_expression (cp_parser
* parser
)
8687 bool global_scope_p
;
8691 /* Look for the optional `::' operator. */
8693 = (cp_parser_global_scope_opt (parser
,
8694 /*current_scope_valid_p=*/false)
8696 /* Look for the `delete' keyword. */
8697 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8698 /* See if the array syntax is in use. */
8699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8701 /* Consume the `[' token. */
8702 cp_lexer_consume_token (parser
->lexer
);
8703 /* Look for the `]' token. */
8704 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8705 /* Remember that this is the `[]' construct. */
8711 /* Parse the cast-expression. */
8712 expression
= cp_parser_simple_cast_expression (parser
);
8714 /* A delete-expression may not appear in an integral constant
8716 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8717 return error_mark_node
;
8719 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8720 tf_warning_or_error
);
8723 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8724 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8728 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8731 switch (token
->type
)
8737 case CPP_CLOSE_SQUARE
:
8738 case CPP_CLOSE_PAREN
:
8739 case CPP_CLOSE_BRACE
:
8740 case CPP_OPEN_BRACE
:
8744 case CPP_DEREF_STAR
:
8752 case CPP_GREATER_EQ
:
8773 case CPP_OPEN_PAREN
:
8774 /* In ((type ()) () the last () isn't a valid cast-expression,
8775 so the whole must be parsed as postfix-expression. */
8776 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8779 case CPP_OPEN_SQUARE
:
8780 /* '[' may start a primary-expression in obj-c++ and in C++11,
8781 as a lambda-expression, eg, '(void)[]{}'. */
8782 if (cxx_dialect
>= cxx11
)
8784 return c_dialect_objc ();
8787 case CPP_MINUS_MINUS
:
8788 /* '++' and '--' may or may not start a cast-expression:
8790 struct T { void operator++(int); };
8791 void f() { (T())++; }
8804 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8805 in the order: const_cast, static_cast, reinterpret_cast.
8807 Don't suggest dynamic_cast.
8809 Return the first legal cast kind found, or NULL otherwise. */
8812 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8816 /* Reuse the parser logic by attempting to build the various kinds of
8817 cast, with "complain" disabled.
8818 Identify the first such cast that is valid. */
8820 /* Don't attempt to run such logic within template processing. */
8821 if (processing_template_decl
)
8824 /* First try const_cast. */
8825 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8826 if (trial
!= error_mark_node
)
8827 return "const_cast";
8829 /* If that fails, try static_cast. */
8830 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8831 if (trial
!= error_mark_node
)
8832 return "static_cast";
8834 /* Finally, try reinterpret_cast. */
8835 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8836 if (trial
!= error_mark_node
)
8837 return "reinterpret_cast";
8839 /* No such cast possible. */
8843 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8844 suggesting how to convert a C-style cast of the form:
8848 to a C++-style cast.
8850 The primary range of RICHLOC is asssumed to be that of the original
8851 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8852 of the parens in the C-style cast. */
8855 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8856 location_t close_paren_loc
, tree orig_expr
,
8859 /* This function is non-trivial, so bail out now if the warning isn't
8860 going to be emitted. */
8861 if (!warn_old_style_cast
)
8864 /* Try to find a legal C++ cast, trying them in order:
8865 const_cast, static_cast, reinterpret_cast. */
8866 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8867 if (!cast_suggestion
)
8870 /* Replace the open paren with "CAST_SUGGESTION<". */
8872 pp_printf (&pp
, "%s<", cast_suggestion
);
8873 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8875 /* Replace the close paren with "> (". */
8876 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8878 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8879 rich_loc
->add_fixit_insert_after (")");
8883 /* Parse a cast-expression.
8887 ( type-id ) cast-expression
8889 ADDRESS_P is true iff the unary-expression is appearing as the
8890 operand of the `&' operator. CAST_P is true if this expression is
8891 the target of a cast.
8893 Returns a representation of the expression. */
8896 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8897 bool decltype_p
, cp_id_kind
* pidk
)
8899 /* If it's a `(', then we might be looking at a cast. */
8900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8902 tree type
= NULL_TREE
;
8903 cp_expr
expr (NULL_TREE
);
8904 int cast_expression
= 0;
8905 const char *saved_message
;
8907 /* There's no way to know yet whether or not this is a cast.
8908 For example, `(int (3))' is a unary-expression, while `(int)
8909 3' is a cast. So, we resort to parsing tentatively. */
8910 cp_parser_parse_tentatively (parser
);
8911 /* Types may not be defined in a cast. */
8912 saved_message
= parser
->type_definition_forbidden_message
;
8913 parser
->type_definition_forbidden_message
8914 = G_("types may not be defined in casts");
8915 /* Consume the `('. */
8916 matching_parens parens
;
8917 cp_token
*open_paren
= parens
.consume_open (parser
);
8918 location_t open_paren_loc
= open_paren
->location
;
8919 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8921 /* A very tricky bit is that `(struct S) { 3 }' is a
8922 compound-literal (which we permit in C++ as an extension).
8923 But, that construct is not a cast-expression -- it is a
8924 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8925 is legal; if the compound-literal were a cast-expression,
8926 you'd need an extra set of parentheses.) But, if we parse
8927 the type-id, and it happens to be a class-specifier, then we
8928 will commit to the parse at that point, because we cannot
8929 undo the action that is done when creating a new class. So,
8930 then we cannot back up and do a postfix-expression.
8932 Another tricky case is the following (c++/29234):
8934 struct S { void operator () (); };
8941 As a type-id we parse the parenthesized S()() as a function
8942 returning a function, groktypename complains and we cannot
8943 back up in this case either.
8945 Therefore, we scan ahead to the closing `)', and check to see
8946 if the tokens after the `)' can start a cast-expression. Otherwise
8947 we are dealing with an unary-expression, a postfix-expression
8950 Yet another tricky case, in C++11, is the following (c++/54891):
8954 The issue is that usually, besides the case of lambda-expressions,
8955 the parenthesized type-id cannot be followed by '[', and, eg, we
8956 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8957 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8958 we don't commit, we try a cast-expression, then an unary-expression.
8960 Save tokens so that we can put them back. */
8961 cp_lexer_save_tokens (parser
->lexer
);
8963 /* We may be looking at a cast-expression. */
8964 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8965 /*consume_paren=*/true))
8967 = cp_parser_tokens_start_cast_expression (parser
);
8969 /* Roll back the tokens we skipped. */
8970 cp_lexer_rollback_tokens (parser
->lexer
);
8971 /* If we aren't looking at a cast-expression, simulate an error so
8972 that the call to cp_parser_error_occurred below returns true. */
8973 if (!cast_expression
)
8974 cp_parser_simulate_error (parser
);
8977 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8978 parser
->in_type_id_in_expr_p
= true;
8979 /* Look for the type-id. */
8980 type
= cp_parser_type_id (parser
);
8981 /* Look for the closing `)'. */
8982 cp_token
*close_paren
= parens
.require_close (parser
);
8984 close_paren_loc
= close_paren
->location
;
8985 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8988 /* Restore the saved message. */
8989 parser
->type_definition_forbidden_message
= saved_message
;
8991 /* At this point this can only be either a cast or a
8992 parenthesized ctor such as `(T ())' that looks like a cast to
8993 function returning T. */
8994 if (!cp_parser_error_occurred (parser
))
8996 /* Only commit if the cast-expression doesn't start with
8997 '++', '--', or '[' in C++11. */
8998 if (cast_expression
> 0)
8999 cp_parser_commit_to_topmost_tentative_parse (parser
);
9001 expr
= cp_parser_cast_expression (parser
,
9002 /*address_p=*/false,
9004 /*decltype_p=*/false,
9007 if (cp_parser_parse_definitely (parser
))
9009 /* Warn about old-style casts, if so requested. */
9010 if (warn_old_style_cast
9011 && !in_system_header_at (input_location
)
9012 && !VOID_TYPE_P (type
)
9013 && current_lang_name
!= lang_name_c
)
9015 gcc_rich_location
rich_loc (input_location
);
9016 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9018 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9019 "use of old-style cast to %q#T", type
);
9022 /* Only type conversions to integral or enumeration types
9023 can be used in constant-expressions. */
9024 if (!cast_valid_in_integral_constant_expression_p (type
)
9025 && cp_parser_non_integral_constant_expression (parser
,
9027 return error_mark_node
;
9029 /* Perform the cast. */
9033 with start==caret at the open paren, extending to the
9035 location_t cast_loc
= make_location (open_paren_loc
,
9037 expr
.get_finish ());
9038 expr
= build_c_cast (cast_loc
, type
, expr
);
9043 cp_parser_abort_tentative_parse (parser
);
9046 /* If we get here, then it's not a cast, so it must be a
9047 unary-expression. */
9048 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9049 cast_p
, decltype_p
);
9052 /* Parse a binary expression of the general form:
9056 pm-expression .* cast-expression
9057 pm-expression ->* cast-expression
9059 multiplicative-expression:
9061 multiplicative-expression * pm-expression
9062 multiplicative-expression / pm-expression
9063 multiplicative-expression % pm-expression
9065 additive-expression:
9066 multiplicative-expression
9067 additive-expression + multiplicative-expression
9068 additive-expression - multiplicative-expression
9072 shift-expression << additive-expression
9073 shift-expression >> additive-expression
9075 relational-expression:
9077 relational-expression < shift-expression
9078 relational-expression > shift-expression
9079 relational-expression <= shift-expression
9080 relational-expression >= shift-expression
9084 relational-expression:
9085 relational-expression <? shift-expression
9086 relational-expression >? shift-expression
9088 equality-expression:
9089 relational-expression
9090 equality-expression == relational-expression
9091 equality-expression != relational-expression
9095 and-expression & equality-expression
9097 exclusive-or-expression:
9099 exclusive-or-expression ^ and-expression
9101 inclusive-or-expression:
9102 exclusive-or-expression
9103 inclusive-or-expression | exclusive-or-expression
9105 logical-and-expression:
9106 inclusive-or-expression
9107 logical-and-expression && inclusive-or-expression
9109 logical-or-expression:
9110 logical-and-expression
9111 logical-or-expression || logical-and-expression
9113 All these are implemented with a single function like:
9116 simple-cast-expression
9117 binary-expression <token> binary-expression
9119 CAST_P is true if this expression is the target of a cast.
9121 The binops_by_token map is used to get the tree codes for each <token> type.
9122 binary-expressions are associated according to a precedence table. */
9124 #define TOKEN_PRECEDENCE(token) \
9125 (((token->type == CPP_GREATER \
9126 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9127 && !parser->greater_than_is_operator_p) \
9128 ? PREC_NOT_OPERATOR \
9129 : binops_by_token[token->type].prec)
9132 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9133 bool no_toplevel_fold_p
,
9135 enum cp_parser_prec prec
,
9138 cp_parser_expression_stack stack
;
9139 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9140 cp_parser_expression_stack_entry current
;
9143 enum tree_code rhs_type
;
9144 enum cp_parser_prec new_prec
, lookahead_prec
;
9147 /* Parse the first expression. */
9148 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9149 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9150 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9151 cast_p
, decltype_p
, pidk
);
9152 current
.prec
= prec
;
9154 if (cp_parser_error_occurred (parser
))
9155 return error_mark_node
;
9159 /* Get an operator token. */
9160 token
= cp_lexer_peek_token (parser
->lexer
);
9162 if (warn_cxx11_compat
9163 && token
->type
== CPP_RSHIFT
9164 && !parser
->greater_than_is_operator_p
)
9166 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9167 "%<>>%> operator is treated"
9168 " as two right angle brackets in C++11"))
9169 inform (token
->location
,
9170 "suggest parentheses around %<>>%> expression");
9173 new_prec
= TOKEN_PRECEDENCE (token
);
9174 if (new_prec
!= PREC_NOT_OPERATOR
9175 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9176 /* This is a fold-expression; handle it later. */
9177 new_prec
= PREC_NOT_OPERATOR
;
9179 /* Popping an entry off the stack means we completed a subexpression:
9180 - either we found a token which is not an operator (`>' where it is not
9181 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9182 will happen repeatedly;
9183 - or, we found an operator which has lower priority. This is the case
9184 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9186 if (new_prec
<= current
.prec
)
9195 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9196 current
.loc
= token
->location
;
9198 /* We used the operator token. */
9199 cp_lexer_consume_token (parser
->lexer
);
9201 /* For "false && x" or "true || x", x will never be executed;
9202 disable warnings while evaluating it. */
9203 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9204 c_inhibit_evaluation_warnings
+=
9205 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9206 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9207 c_inhibit_evaluation_warnings
+=
9208 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9210 /* Extract another operand. It may be the RHS of this expression
9211 or the LHS of a new, higher priority expression. */
9212 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9213 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9214 rhs
= cp_parser_simple_cast_expression (parser
);
9216 /* Get another operator token. Look up its precedence to avoid
9217 building a useless (immediately popped) stack entry for common
9218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9219 token
= cp_lexer_peek_token (parser
->lexer
);
9220 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9221 if (lookahead_prec
!= PREC_NOT_OPERATOR
9222 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9223 lookahead_prec
= PREC_NOT_OPERATOR
;
9224 if (lookahead_prec
> new_prec
)
9226 /* ... and prepare to parse the RHS of the new, higher priority
9227 expression. Since precedence levels on the stack are
9228 monotonically increasing, we do not have to care about
9233 current
.lhs_type
= rhs_type
;
9234 current
.prec
= new_prec
;
9235 new_prec
= lookahead_prec
;
9239 lookahead_prec
= new_prec
;
9240 /* If the stack is not empty, we have parsed into LHS the right side
9241 (`4' in the example above) of an expression we had suspended.
9242 We can use the information on the stack to recover the LHS (`3')
9243 from the stack together with the tree code (`MULT_EXPR'), and
9244 the precedence of the higher level subexpression
9245 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9246 which will be used to actually build the additive expression. */
9248 rhs_type
= current
.lhs_type
;
9253 /* Undo the disabling of warnings done above. */
9254 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9255 c_inhibit_evaluation_warnings
-=
9256 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9257 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9258 c_inhibit_evaluation_warnings
-=
9259 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9261 if (warn_logical_not_paren
9262 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9263 && current
.lhs_type
== TRUTH_NOT_EXPR
9264 /* Avoid warning for !!x == y. */
9265 && (TREE_CODE (current
.lhs
) != NE_EXPR
9266 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9267 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9268 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9269 /* Avoid warning for !b == y where b is boolean. */
9270 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9271 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9273 /* Avoid warning for !!b == y where b is boolean. */
9274 && (!DECL_P (current
.lhs
)
9275 || TREE_TYPE (current
.lhs
) == NULL_TREE
9276 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9277 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9278 current
.lhs
, maybe_constant_value (rhs
));
9282 location_t combined_loc
= make_location (current
.loc
,
9283 current
.lhs
.get_start (),
9286 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9287 ERROR_MARK for everything that is not a binary expression.
9288 This makes warn_about_parentheses miss some warnings that
9289 involve unary operators. For unary expressions we should
9290 pass the correct tree_code unless the unary expression was
9291 surrounded by parentheses.
9293 if (no_toplevel_fold_p
9294 && lookahead_prec
<= current
.prec
9296 current
.lhs
= build2_loc (combined_loc
,
9298 TREE_CODE_CLASS (current
.tree_type
)
9300 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9304 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9305 current
.lhs
, current
.lhs_type
,
9306 rhs
, rhs_type
, &overload
,
9307 complain_flags (decltype_p
));
9308 /* TODO: build_x_binary_op doesn't always honor the location. */
9309 current
.lhs
.set_location (combined_loc
);
9311 current
.lhs_type
= current
.tree_type
;
9313 /* If the binary operator required the use of an overloaded operator,
9314 then this expression cannot be an integral constant-expression.
9315 An overloaded operator can be used even if both operands are
9316 otherwise permissible in an integral constant-expression if at
9317 least one of the operands is of enumeration type. */
9320 && cp_parser_non_integral_constant_expression (parser
,
9322 return error_mark_node
;
9329 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9330 bool no_toplevel_fold_p
,
9331 enum cp_parser_prec prec
,
9334 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9335 /*decltype*/false, prec
, pidk
);
9338 /* Parse the `? expression : assignment-expression' part of a
9339 conditional-expression. The LOGICAL_OR_EXPR is the
9340 logical-or-expression that started the conditional-expression.
9341 Returns a representation of the entire conditional-expression.
9343 This routine is used by cp_parser_assignment_expression.
9345 ? expression : assignment-expression
9349 ? : assignment-expression */
9352 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9354 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9355 cp_expr assignment_expr
;
9356 struct cp_token
*token
;
9357 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9359 /* Consume the `?' token. */
9360 cp_lexer_consume_token (parser
->lexer
);
9361 token
= cp_lexer_peek_token (parser
->lexer
);
9362 if (cp_parser_allow_gnu_extensions_p (parser
)
9363 && token
->type
== CPP_COLON
)
9365 pedwarn (token
->location
, OPT_Wpedantic
,
9366 "ISO C++ does not allow ?: with omitted middle operand");
9367 /* Implicit true clause. */
9369 c_inhibit_evaluation_warnings
+=
9370 folded_logical_or_expr
== truthvalue_true_node
;
9371 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9375 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9376 parser
->colon_corrects_to_scope_p
= false;
9377 /* Parse the expression. */
9378 c_inhibit_evaluation_warnings
+=
9379 folded_logical_or_expr
== truthvalue_false_node
;
9380 expr
= cp_parser_expression (parser
);
9381 c_inhibit_evaluation_warnings
+=
9382 ((folded_logical_or_expr
== truthvalue_true_node
)
9383 - (folded_logical_or_expr
== truthvalue_false_node
));
9384 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9387 /* The next token should be a `:'. */
9388 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9389 /* Parse the assignment-expression. */
9390 assignment_expr
= cp_parser_assignment_expression (parser
);
9391 c_inhibit_evaluation_warnings
-=
9392 folded_logical_or_expr
== truthvalue_true_node
;
9395 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9396 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9397 with the caret at the "?", ranging from the start of
9398 the logical_or_expr to the end of the assignment_expr. */
9399 loc
= make_location (loc
,
9400 logical_or_expr
.get_start (),
9401 assignment_expr
.get_finish ());
9403 /* Build the conditional-expression. */
9404 return build_x_conditional_expr (loc
, logical_or_expr
,
9407 tf_warning_or_error
);
9410 /* Parse an assignment-expression.
9412 assignment-expression:
9413 conditional-expression
9414 logical-or-expression assignment-operator assignment_expression
9417 CAST_P is true if this expression is the target of a cast.
9418 DECLTYPE_P is true if this expression is the operand of decltype.
9420 Returns a representation for the expression. */
9423 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9424 bool cast_p
, bool decltype_p
)
9428 /* If the next token is the `throw' keyword, then we're looking at
9429 a throw-expression. */
9430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9431 expr
= cp_parser_throw_expression (parser
);
9432 /* Otherwise, it must be that we are looking at a
9433 logical-or-expression. */
9436 /* Parse the binary expressions (logical-or-expression). */
9437 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9439 PREC_NOT_OPERATOR
, pidk
);
9440 /* If the next token is a `?' then we're actually looking at a
9441 conditional-expression. */
9442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9443 return cp_parser_question_colon_clause (parser
, expr
);
9446 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9448 /* If it's an assignment-operator, we're using the second
9450 enum tree_code assignment_operator
9451 = cp_parser_assignment_operator_opt (parser
);
9452 if (assignment_operator
!= ERROR_MARK
)
9454 bool non_constant_p
;
9456 /* Parse the right-hand side of the assignment. */
9457 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9460 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9463 /* An assignment may not appear in a
9464 constant-expression. */
9465 if (cp_parser_non_integral_constant_expression (parser
,
9467 return error_mark_node
;
9468 /* Build the assignment expression. Its default
9472 is the location of the '=' token as the
9473 caret, ranging from the start of the lhs to the
9475 loc
= make_location (loc
,
9478 expr
= build_x_modify_expr (loc
, expr
,
9479 assignment_operator
,
9481 complain_flags (decltype_p
));
9482 /* TODO: build_x_modify_expr doesn't honor the location,
9483 so we must set it here. */
9484 expr
.set_location (loc
);
9492 /* Parse an (optional) assignment-operator.
9494 assignment-operator: one of
9495 = *= /= %= += -= >>= <<= &= ^= |=
9499 assignment-operator: one of
9502 If the next token is an assignment operator, the corresponding tree
9503 code is returned, and the token is consumed. For example, for
9504 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9505 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9506 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9507 operator, ERROR_MARK is returned. */
9509 static enum tree_code
9510 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9515 /* Peek at the next token. */
9516 token
= cp_lexer_peek_token (parser
->lexer
);
9518 switch (token
->type
)
9529 op
= TRUNC_DIV_EXPR
;
9533 op
= TRUNC_MOD_EXPR
;
9565 /* Nothing else is an assignment operator. */
9569 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9570 if (op
!= ERROR_MARK
9571 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9574 /* If it was an assignment operator, consume it. */
9575 if (op
!= ERROR_MARK
)
9576 cp_lexer_consume_token (parser
->lexer
);
9581 /* Parse an expression.
9584 assignment-expression
9585 expression , assignment-expression
9587 CAST_P is true if this expression is the target of a cast.
9588 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9589 except possibly parenthesized or on the RHS of a comma (N3276).
9591 Returns a representation of the expression. */
9594 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9595 bool cast_p
, bool decltype_p
)
9597 cp_expr expression
= NULL_TREE
;
9598 location_t loc
= UNKNOWN_LOCATION
;
9602 cp_expr assignment_expression
;
9604 /* Parse the next assignment-expression. */
9605 assignment_expression
9606 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9608 /* We don't create a temporary for a call that is the immediate operand
9609 of decltype or on the RHS of a comma. But when we see a comma, we
9610 need to create a temporary for a call on the LHS. */
9611 if (decltype_p
&& !processing_template_decl
9612 && TREE_CODE (assignment_expression
) == CALL_EXPR
9613 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9614 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9615 assignment_expression
9616 = build_cplus_new (TREE_TYPE (assignment_expression
),
9617 assignment_expression
, tf_warning_or_error
);
9619 /* If this is the first assignment-expression, we can just
9622 expression
= assignment_expression
;
9625 /* Create a location with caret at the comma, ranging
9626 from the start of the LHS to the end of the RHS. */
9627 loc
= make_location (loc
,
9628 expression
.get_start (),
9629 assignment_expression
.get_finish ());
9630 expression
= build_x_compound_expr (loc
, expression
,
9631 assignment_expression
,
9632 complain_flags (decltype_p
));
9633 expression
.set_location (loc
);
9635 /* If the next token is not a comma, or we're in a fold-expression, then
9636 we are done with the expression. */
9637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9638 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9640 /* Consume the `,'. */
9641 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9642 cp_lexer_consume_token (parser
->lexer
);
9643 /* A comma operator cannot appear in a constant-expression. */
9644 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9645 expression
= error_mark_node
;
9651 /* Parse a constant-expression.
9653 constant-expression:
9654 conditional-expression
9656 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9657 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9658 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9659 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9660 only parse a conditional-expression, otherwise parse an
9661 assignment-expression. See below for rationale. */
9664 cp_parser_constant_expression (cp_parser
* parser
,
9665 bool allow_non_constant_p
,
9666 bool *non_constant_p
,
9669 bool saved_integral_constant_expression_p
;
9670 bool saved_allow_non_integral_constant_expression_p
;
9671 bool saved_non_integral_constant_expression_p
;
9674 /* It might seem that we could simply parse the
9675 conditional-expression, and then check to see if it were
9676 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9677 one that the compiler can figure out is constant, possibly after
9678 doing some simplifications or optimizations. The standard has a
9679 precise definition of constant-expression, and we must honor
9680 that, even though it is somewhat more restrictive.
9686 is not a legal declaration, because `(2, 3)' is not a
9687 constant-expression. The `,' operator is forbidden in a
9688 constant-expression. However, GCC's constant-folding machinery
9689 will fold this operation to an INTEGER_CST for `3'. */
9691 /* Save the old settings. */
9692 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9693 saved_allow_non_integral_constant_expression_p
9694 = parser
->allow_non_integral_constant_expression_p
;
9695 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9696 /* We are now parsing a constant-expression. */
9697 parser
->integral_constant_expression_p
= true;
9698 parser
->allow_non_integral_constant_expression_p
9699 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9700 parser
->non_integral_constant_expression_p
= false;
9701 /* Although the grammar says "conditional-expression", when not STRICT_P,
9702 we parse an "assignment-expression", which also permits
9703 "throw-expression" and the use of assignment operators. In the case
9704 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9705 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9706 actually essential that we look for an assignment-expression.
9707 For example, cp_parser_initializer_clauses uses this function to
9708 determine whether a particular assignment-expression is in fact
9712 /* Parse the binary expressions (logical-or-expression). */
9713 expression
= cp_parser_binary_expression (parser
, false, false, false,
9714 PREC_NOT_OPERATOR
, NULL
);
9715 /* If the next token is a `?' then we're actually looking at
9716 a conditional-expression; otherwise we're done. */
9717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9718 expression
= cp_parser_question_colon_clause (parser
, expression
);
9721 expression
= cp_parser_assignment_expression (parser
);
9722 /* Restore the old settings. */
9723 parser
->integral_constant_expression_p
9724 = saved_integral_constant_expression_p
;
9725 parser
->allow_non_integral_constant_expression_p
9726 = saved_allow_non_integral_constant_expression_p
;
9727 if (cxx_dialect
>= cxx11
)
9729 /* Require an rvalue constant expression here; that's what our
9730 callers expect. Reference constant expressions are handled
9731 separately in e.g. cp_parser_template_argument. */
9732 tree decay
= expression
;
9733 if (TREE_TYPE (expression
)
9734 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9735 decay
= build_address (expression
);
9736 bool is_const
= potential_rvalue_constant_expression (decay
);
9737 parser
->non_integral_constant_expression_p
= !is_const
;
9738 if (!is_const
&& !allow_non_constant_p
)
9739 require_potential_rvalue_constant_expression (decay
);
9741 if (allow_non_constant_p
)
9742 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9743 parser
->non_integral_constant_expression_p
9744 = saved_non_integral_constant_expression_p
;
9749 /* Parse __builtin_offsetof.
9751 offsetof-expression:
9752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9754 offsetof-member-designator:
9756 | offsetof-member-designator "." id-expression
9757 | offsetof-member-designator "[" expression "]"
9758 | offsetof-member-designator "->" id-expression */
9761 cp_parser_builtin_offsetof (cp_parser
*parser
)
9763 int save_ice_p
, save_non_ice_p
;
9768 location_t finish_loc
;
9770 /* We're about to accept non-integral-constant things, but will
9771 definitely yield an integral constant expression. Save and
9772 restore these values around our local parsing. */
9773 save_ice_p
= parser
->integral_constant_expression_p
;
9774 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9776 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9778 /* Consume the "__builtin_offsetof" token. */
9779 cp_lexer_consume_token (parser
->lexer
);
9780 /* Consume the opening `('. */
9781 matching_parens parens
;
9782 parens
.require_open (parser
);
9783 /* Parse the type-id. */
9784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9785 type
= cp_parser_type_id (parser
);
9786 /* Look for the `,'. */
9787 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9788 token
= cp_lexer_peek_token (parser
->lexer
);
9790 /* Build the (type *)null that begins the traditional offsetof macro. */
9792 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9793 tf_warning_or_error
);
9795 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9796 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9797 true, &dummy
, token
->location
);
9800 token
= cp_lexer_peek_token (parser
->lexer
);
9801 switch (token
->type
)
9803 case CPP_OPEN_SQUARE
:
9804 /* offsetof-member-designator "[" expression "]" */
9805 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9810 /* offsetof-member-designator "->" identifier */
9811 expr
= grok_array_decl (token
->location
, expr
,
9812 integer_zero_node
, false);
9816 /* offsetof-member-designator "." identifier */
9817 cp_lexer_consume_token (parser
->lexer
);
9818 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9823 case CPP_CLOSE_PAREN
:
9824 /* Consume the ")" token. */
9825 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9826 cp_lexer_consume_token (parser
->lexer
);
9830 /* Error. We know the following require will fail, but
9831 that gives the proper error message. */
9832 parens
.require_close (parser
);
9833 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9834 expr
= error_mark_node
;
9840 /* Make a location of the form:
9841 __builtin_offsetof (struct s, f)
9842 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9843 with caret at the type-id, ranging from the start of the
9844 "_builtin_offsetof" token to the close paren. */
9845 loc
= make_location (loc
, start_loc
, finish_loc
);
9846 /* The result will be an INTEGER_CST, so we need to explicitly
9847 preserve the location. */
9848 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9851 parser
->integral_constant_expression_p
= save_ice_p
;
9852 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9854 expr
= expr
.maybe_add_location_wrapper ();
9858 /* Parse a trait expression.
9860 Returns a representation of the expression, the underlying type
9861 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9864 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9867 tree type1
, type2
= NULL_TREE
;
9868 bool binary
= false;
9869 bool variadic
= false;
9873 case RID_HAS_NOTHROW_ASSIGN
:
9874 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9876 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9877 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9879 case RID_HAS_NOTHROW_COPY
:
9880 kind
= CPTK_HAS_NOTHROW_COPY
;
9882 case RID_HAS_TRIVIAL_ASSIGN
:
9883 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9885 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9886 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9888 case RID_HAS_TRIVIAL_COPY
:
9889 kind
= CPTK_HAS_TRIVIAL_COPY
;
9891 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9892 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9894 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9895 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9897 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9898 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9900 case RID_IS_ABSTRACT
:
9901 kind
= CPTK_IS_ABSTRACT
;
9903 case RID_IS_AGGREGATE
:
9904 kind
= CPTK_IS_AGGREGATE
;
9906 case RID_IS_BASE_OF
:
9907 kind
= CPTK_IS_BASE_OF
;
9911 kind
= CPTK_IS_CLASS
;
9914 kind
= CPTK_IS_EMPTY
;
9917 kind
= CPTK_IS_ENUM
;
9920 kind
= CPTK_IS_FINAL
;
9922 case RID_IS_LITERAL_TYPE
:
9923 kind
= CPTK_IS_LITERAL_TYPE
;
9928 case RID_IS_POLYMORPHIC
:
9929 kind
= CPTK_IS_POLYMORPHIC
;
9931 case RID_IS_SAME_AS
:
9932 kind
= CPTK_IS_SAME_AS
;
9935 case RID_IS_STD_LAYOUT
:
9936 kind
= CPTK_IS_STD_LAYOUT
;
9938 case RID_IS_TRIVIAL
:
9939 kind
= CPTK_IS_TRIVIAL
;
9941 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9942 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9945 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9946 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9949 case RID_IS_TRIVIALLY_COPYABLE
:
9950 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9953 kind
= CPTK_IS_UNION
;
9955 case RID_UNDERLYING_TYPE
:
9956 kind
= CPTK_UNDERLYING_TYPE
;
9961 case RID_DIRECT_BASES
:
9962 kind
= CPTK_DIRECT_BASES
;
9964 case RID_IS_ASSIGNABLE
:
9965 kind
= CPTK_IS_ASSIGNABLE
;
9968 case RID_IS_CONSTRUCTIBLE
:
9969 kind
= CPTK_IS_CONSTRUCTIBLE
;
9976 /* Get location of initial token. */
9977 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9979 /* Consume the token. */
9980 cp_lexer_consume_token (parser
->lexer
);
9982 matching_parens parens
;
9983 parens
.require_open (parser
);
9986 type_id_in_expr_sentinel
s (parser
);
9987 type1
= cp_parser_type_id (parser
);
9990 if (type1
== error_mark_node
)
9991 return error_mark_node
;
9995 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9998 type_id_in_expr_sentinel
s (parser
);
9999 type2
= cp_parser_type_id (parser
);
10002 if (type2
== error_mark_node
)
10003 return error_mark_node
;
10007 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10009 cp_lexer_consume_token (parser
->lexer
);
10010 tree elt
= cp_parser_type_id (parser
);
10011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10013 cp_lexer_consume_token (parser
->lexer
);
10014 elt
= make_pack_expansion (elt
);
10016 if (elt
== error_mark_node
)
10017 return error_mark_node
;
10018 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10022 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10023 parens
.require_close (parser
);
10025 /* Construct a location of the form:
10026 __is_trivially_copyable(_Tp)
10027 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10028 with start == caret, finishing at the close-paren. */
10029 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10031 /* Complete the trait expression, which may mean either processing
10032 the trait expr now or saving it for template instantiation. */
10035 case CPTK_UNDERLYING_TYPE
:
10036 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10038 return cp_expr (finish_bases (type1
, false), trait_loc
);
10039 case CPTK_DIRECT_BASES
:
10040 return cp_expr (finish_bases (type1
, true), trait_loc
);
10042 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10046 /* Parse a lambda expression.
10049 lambda-introducer lambda-declarator [opt] compound-statement
10051 Returns a representation of the expression. */
10054 cp_parser_lambda_expression (cp_parser
* parser
)
10056 tree lambda_expr
= build_lambda_expr ();
10059 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10060 cp_token_position start
= 0;
10062 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10064 if (cp_unevaluated_operand
)
10066 if (!token
->error_reported
)
10068 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10069 "lambda-expression in unevaluated context");
10070 token
->error_reported
= true;
10074 else if (parser
->in_template_argument_list_p
)
10076 if (!token
->error_reported
)
10078 error_at (token
->location
, "lambda-expression in template-argument");
10079 token
->error_reported
= true;
10084 /* We may be in the middle of deferred access check. Disable
10086 push_deferring_access_checks (dk_no_deferred
);
10088 cp_parser_lambda_introducer (parser
, lambda_expr
);
10090 type
= begin_lambda_type (lambda_expr
);
10091 if (type
== error_mark_node
)
10092 return error_mark_node
;
10094 record_lambda_scope (lambda_expr
);
10096 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10097 determine_visibility (TYPE_NAME (type
));
10099 /* Now that we've started the type, add the capture fields for any
10100 explicit captures. */
10101 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10104 /* Inside the class, surrounding template-parameter-lists do not apply. */
10105 unsigned int saved_num_template_parameter_lists
10106 = parser
->num_template_parameter_lists
;
10107 unsigned char in_statement
= parser
->in_statement
;
10108 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10109 bool fully_implicit_function_template_p
10110 = parser
->fully_implicit_function_template_p
;
10111 tree implicit_template_parms
= parser
->implicit_template_parms
;
10112 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10113 bool auto_is_implicit_function_template_parm_p
10114 = parser
->auto_is_implicit_function_template_parm_p
;
10116 parser
->num_template_parameter_lists
= 0;
10117 parser
->in_statement
= 0;
10118 parser
->in_switch_statement_p
= false;
10119 parser
->fully_implicit_function_template_p
= false;
10120 parser
->implicit_template_parms
= 0;
10121 parser
->implicit_template_scope
= 0;
10122 parser
->auto_is_implicit_function_template_parm_p
= false;
10124 /* By virtue of defining a local class, a lambda expression has access to
10125 the private variables of enclosing classes. */
10127 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10129 if (ok
&& cp_parser_error_occurred (parser
))
10134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10135 && cp_parser_start_tentative_firewall (parser
))
10137 cp_parser_lambda_body (parser
, lambda_expr
);
10139 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10141 if (cp_parser_skip_to_closing_brace (parser
))
10142 cp_lexer_consume_token (parser
->lexer
);
10145 /* The capture list was built up in reverse order; fix that now. */
10146 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10147 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10150 maybe_add_lambda_conv_op (type
);
10152 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10154 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10155 parser
->in_statement
= in_statement
;
10156 parser
->in_switch_statement_p
= in_switch_statement_p
;
10157 parser
->fully_implicit_function_template_p
10158 = fully_implicit_function_template_p
;
10159 parser
->implicit_template_parms
= implicit_template_parms
;
10160 parser
->implicit_template_scope
= implicit_template_scope
;
10161 parser
->auto_is_implicit_function_template_parm_p
10162 = auto_is_implicit_function_template_parm_p
;
10165 /* This field is only used during parsing of the lambda. */
10166 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10168 /* This lambda shouldn't have any proxies left at this point. */
10169 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10170 /* And now that we're done, push proxies for an enclosing lambda. */
10171 insert_pending_capture_proxies ();
10174 lambda_expr
= build_lambda_object (lambda_expr
);
10176 lambda_expr
= error_mark_node
;
10178 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10180 pop_deferring_access_checks ();
10182 return lambda_expr
;
10185 /* Parse the beginning of a lambda expression.
10188 [ lambda-capture [opt] ]
10190 LAMBDA_EXPR is the current representation of the lambda expression. */
10193 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10195 /* Need commas after the first capture. */
10198 /* Eat the leading `['. */
10199 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10201 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10203 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10205 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10206 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10208 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10210 cp_lexer_consume_token (parser
->lexer
);
10214 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10216 cp_token
* capture_token
;
10218 tree capture_init_expr
;
10219 cp_id_kind idk
= CP_ID_KIND_NONE
;
10220 bool explicit_init_p
= false;
10222 enum capture_kind_type
10227 enum capture_kind_type capture_kind
= BY_COPY
;
10229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10231 error ("expected end of capture-list");
10238 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10240 /* Possibly capture `this'. */
10241 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10243 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10244 if (cxx_dialect
< cxx2a
10245 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10246 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10247 "with by-copy capture default");
10248 cp_lexer_consume_token (parser
->lexer
);
10249 add_capture (lambda_expr
,
10250 /*id=*/this_identifier
,
10251 /*initializer=*/finish_this_expr (),
10252 /*by_reference_p=*/true,
10257 /* Possibly capture `*this'. */
10258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10259 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10261 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10262 if (cxx_dialect
< cxx17
)
10263 pedwarn (loc
, 0, "%<*this%> capture only available with "
10264 "-std=c++17 or -std=gnu++17");
10265 cp_lexer_consume_token (parser
->lexer
);
10266 cp_lexer_consume_token (parser
->lexer
);
10267 add_capture (lambda_expr
,
10268 /*id=*/this_identifier
,
10269 /*initializer=*/finish_this_expr (),
10270 /*by_reference_p=*/false,
10275 /* Remember whether we want to capture as a reference or not. */
10276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10278 capture_kind
= BY_REFERENCE
;
10279 cp_lexer_consume_token (parser
->lexer
);
10282 /* Get the identifier. */
10283 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10284 capture_id
= cp_parser_identifier (parser
);
10286 if (capture_id
== error_mark_node
)
10287 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10288 delimiters, but I modified this to stop on unnested ']' as well. It
10289 was already changed to stop on unnested '}', so the
10290 "closing_parenthesis" name is no more misleading with my change. */
10292 cp_parser_skip_to_closing_parenthesis (parser
,
10293 /*recovering=*/true,
10295 /*consume_paren=*/true);
10299 /* Find the initializer for this capture. */
10300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10301 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10302 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10304 bool direct
, non_constant
;
10305 /* An explicit initializer exists. */
10306 if (cxx_dialect
< cxx14
)
10307 pedwarn (input_location
, 0,
10308 "lambda capture initializers "
10309 "only available with -std=c++14 or -std=gnu++14");
10310 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10312 explicit_init_p
= true;
10313 if (capture_init_expr
== NULL_TREE
)
10315 error ("empty initializer for lambda init-capture");
10316 capture_init_expr
= error_mark_node
;
10321 const char* error_msg
;
10323 /* Turn the identifier into an id-expression. */
10325 = cp_parser_lookup_name_simple (parser
, capture_id
,
10326 capture_token
->location
);
10328 if (capture_init_expr
== error_mark_node
)
10330 unqualified_name_lookup_error (capture_id
);
10333 else if (DECL_P (capture_init_expr
)
10334 && (!VAR_P (capture_init_expr
)
10335 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
10337 error_at (capture_token
->location
,
10338 "capture of non-variable %qD ",
10339 capture_init_expr
);
10340 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10341 "%q#D declared here", capture_init_expr
);
10344 if (VAR_P (capture_init_expr
)
10345 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10347 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10348 "%qD with non-automatic storage duration",
10349 capture_init_expr
))
10350 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10351 "%q#D declared here", capture_init_expr
);
10356 = finish_id_expression
10361 /*integral_constant_expression_p=*/false,
10362 /*allow_non_integral_constant_expression_p=*/false,
10363 /*non_integral_constant_expression_p=*/NULL
,
10364 /*template_p=*/false,
10366 /*address_p=*/false,
10367 /*template_arg_p=*/false,
10369 capture_token
->location
);
10371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10373 cp_lexer_consume_token (parser
->lexer
);
10374 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10377 check_for_bare_parameter_packs (capture_init_expr
);
10380 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10381 && !explicit_init_p
)
10383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10384 && capture_kind
== BY_COPY
)
10385 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10386 "of %qD redundant with by-copy capture default",
10388 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10389 && capture_kind
== BY_REFERENCE
)
10390 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10391 "capture of %qD redundant with by-reference capture "
10392 "default", capture_id
);
10395 add_capture (lambda_expr
,
10398 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10402 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10405 /* Parse the (optional) middle of a lambda expression.
10408 < template-parameter-list [opt] >
10409 ( parameter-declaration-clause [opt] )
10410 attribute-specifier [opt]
10411 decl-specifier-seq [opt]
10412 exception-specification [opt]
10413 lambda-return-type-clause [opt]
10415 LAMBDA_EXPR is the current representation of the lambda expression. */
10418 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10420 /* 5.1.1.4 of the standard says:
10421 If a lambda-expression does not include a lambda-declarator, it is as if
10422 the lambda-declarator were ().
10423 This means an empty parameter list, no attributes, and no exception
10425 tree param_list
= void_list_node
;
10426 tree attributes
= NULL_TREE
;
10427 tree exception_spec
= NULL_TREE
;
10428 tree template_param_list
= NULL_TREE
;
10429 tree tx_qual
= NULL_TREE
;
10430 tree return_type
= NULL_TREE
;
10431 cp_decl_specifier_seq lambda_specs
;
10432 clear_decl_specs (&lambda_specs
);
10434 /* The template-parameter-list is optional, but must begin with
10435 an opening angle if present. */
10436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10438 if (cxx_dialect
< cxx14
)
10439 pedwarn (parser
->lexer
->next_token
->location
, 0,
10440 "lambda templates are only available with "
10441 "-std=c++14 or -std=gnu++14");
10442 else if (cxx_dialect
< cxx2a
)
10443 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10444 "lambda templates are only available with "
10445 "-std=c++2a or -std=gnu++2a");
10447 cp_lexer_consume_token (parser
->lexer
);
10449 template_param_list
= cp_parser_template_parameter_list (parser
);
10451 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10453 /* We just processed one more parameter list. */
10454 ++parser
->num_template_parameter_lists
;
10457 /* The parameter-declaration-clause is optional (unless
10458 template-parameter-list was given), but must begin with an
10459 opening parenthesis if present. */
10460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10462 matching_parens parens
;
10463 parens
.consume_open (parser
);
10465 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10467 /* Parse parameters. */
10468 param_list
= cp_parser_parameter_declaration_clause (parser
);
10470 /* Default arguments shall not be specified in the
10471 parameter-declaration-clause of a lambda-declarator. */
10472 if (cxx_dialect
< cxx14
)
10473 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10474 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10475 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10476 "default argument specified for lambda parameter");
10478 parens
.require_close (parser
);
10480 attributes
= cp_parser_attributes_opt (parser
);
10482 /* In the decl-specifier-seq of the lambda-declarator, each
10483 decl-specifier shall either be mutable or constexpr. */
10484 int declares_class_or_enum
;
10485 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10486 cp_parser_decl_specifier_seq (parser
,
10487 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10488 &lambda_specs
, &declares_class_or_enum
);
10489 if (lambda_specs
.storage_class
== sc_mutable
)
10491 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10492 if (lambda_specs
.conflicting_specifiers_p
)
10493 error_at (lambda_specs
.locations
[ds_storage_class
],
10494 "duplicate %<mutable%>");
10497 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10499 /* Parse optional exception specification. */
10500 exception_spec
= cp_parser_exception_specification_opt (parser
);
10502 /* Parse optional trailing return type. */
10503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10505 cp_lexer_consume_token (parser
->lexer
);
10506 return_type
= cp_parser_trailing_type_id (parser
);
10509 /* The function parameters must be in scope all the way until after the
10510 trailing-return-type in case of decltype. */
10511 pop_bindings_and_leave_scope ();
10513 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10514 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10516 /* Create the function call operator.
10518 Messing with declarators like this is no uglier than building up the
10519 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10522 cp_decl_specifier_seq return_type_specs
;
10523 cp_declarator
* declarator
;
10528 clear_decl_specs (&return_type_specs
);
10530 return_type_specs
.type
= return_type
;
10532 /* Maybe we will deduce the return type later. */
10533 return_type_specs
.type
= make_auto ();
10535 if (lambda_specs
.locations
[ds_constexpr
])
10537 if (cxx_dialect
>= cxx17
)
10538 return_type_specs
.locations
[ds_constexpr
]
10539 = lambda_specs
.locations
[ds_constexpr
];
10541 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10542 "lambda only available with -std=c++17 or -std=gnu++17");
10545 p
= obstack_alloc (&declarator_obstack
, 0);
10547 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10549 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10550 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10551 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10552 VIRT_SPEC_UNSPECIFIED
,
10556 /*late_return_type=*/NULL_TREE
,
10557 /*requires_clause*/NULL_TREE
);
10558 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10560 fco
= grokmethod (&return_type_specs
,
10563 if (fco
!= error_mark_node
)
10565 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10566 DECL_ARTIFICIAL (fco
) = 1;
10567 /* Give the object parameter a different name. */
10568 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10570 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10572 if (template_param_list
)
10574 fco
= finish_member_template_decl (fco
);
10575 finish_template_decl (template_param_list
);
10576 --parser
->num_template_parameter_lists
;
10578 else if (parser
->fully_implicit_function_template_p
)
10579 fco
= finish_fully_implicit_template (parser
, fco
);
10581 finish_member_declaration (fco
);
10583 obstack_free (&declarator_obstack
, p
);
10585 return (fco
!= error_mark_node
);
10589 /* Parse the body of a lambda expression, which is simply
10593 but which requires special handling.
10594 LAMBDA_EXPR is the current representation of the lambda expression. */
10597 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10599 bool nested
= (current_function_decl
!= NULL_TREE
);
10600 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10601 bool in_function_body
= parser
->in_function_body
;
10604 push_function_context ();
10606 /* Still increment function_depth so that we don't GC in the
10607 middle of an expression. */
10610 vec
<tree
> omp_privatization_save
;
10611 save_omp_privatization_clauses (omp_privatization_save
);
10612 /* Clear this in case we're in the middle of a default argument. */
10613 parser
->local_variables_forbidden_p
= false;
10614 parser
->in_function_body
= true;
10617 local_specialization_stack
s (lss_copy
);
10618 tree fco
= lambda_function (lambda_expr
);
10619 tree body
= start_lambda_function (fco
, lambda_expr
);
10620 matching_braces braces
;
10622 if (braces
.require_open (parser
))
10624 tree compound_stmt
= begin_compound_stmt (0);
10626 /* Originally C++11 required us to peek for 'return expr'; and
10627 process it specially here to deduce the return type. N3638
10628 removed the need for that. */
10630 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10631 cp_parser_label_declaration (parser
);
10632 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10633 braces
.require_close (parser
);
10635 finish_compound_stmt (compound_stmt
);
10638 finish_lambda_function (body
);
10641 restore_omp_privatization_clauses (omp_privatization_save
);
10642 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10643 parser
->in_function_body
= in_function_body
;
10645 pop_function_context();
10650 /* Statements [gram.stmt.stmt] */
10652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10655 add_debug_begin_stmt (location_t loc
)
10657 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10660 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10661 SET_EXPR_LOCATION (stmt
, loc
);
10665 /* Parse a statement.
10669 expression-statement
10671 selection-statement
10672 iteration-statement
10674 declaration-statement
10681 attribute-specifier-seq (opt) expression-statement
10682 attribute-specifier-seq (opt) compound-statement
10683 attribute-specifier-seq (opt) selection-statement
10684 attribute-specifier-seq (opt) iteration-statement
10685 attribute-specifier-seq (opt) jump-statement
10686 declaration-statement
10687 attribute-specifier-seq (opt) try-block
10690 expression-statement
10698 IN_COMPOUND is true when the statement is nested inside a
10699 cp_parser_compound_statement; this matters for certain pragmas.
10701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10702 is a (possibly labeled) if statement which is not enclosed in braces
10703 and has an else clause. This is used to implement -Wparentheses.
10705 CHAIN is a vector of if-else-if conditions. */
10708 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10709 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10710 location_t
*loc_after_labels
)
10712 tree statement
, std_attrs
= NULL_TREE
;
10714 location_t statement_location
, attrs_location
;
10719 /* There is no statement yet. */
10720 statement
= NULL_TREE
;
10722 saved_token_sentinel
saved_tokens (parser
->lexer
);
10723 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10724 if (c_dialect_objc ())
10725 /* In obj-c++, seeing '[[' might be the either the beginning of
10726 c++11 attributes, or a nested objc-message-expression. So
10727 let's parse the c++11 attributes tentatively. */
10728 cp_parser_parse_tentatively (parser
);
10729 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10730 if (c_dialect_objc ())
10732 if (!cp_parser_parse_definitely (parser
))
10733 std_attrs
= NULL_TREE
;
10736 /* Peek at the next token. */
10737 token
= cp_lexer_peek_token (parser
->lexer
);
10738 /* Remember the location of the first token in the statement. */
10739 statement_location
= token
->location
;
10740 add_debug_begin_stmt (statement_location
);
10741 /* If this is a keyword, then that will often determine what kind of
10742 statement we have. */
10743 if (token
->type
== CPP_KEYWORD
)
10745 enum rid keyword
= token
->keyword
;
10751 /* Looks like a labeled-statement with a case label.
10752 Parse the label, and then use tail recursion to parse
10754 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10755 in_compound
= false;
10760 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10766 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10773 statement
= cp_parser_jump_statement (parser
);
10776 /* Objective-C++ exception-handling constructs. */
10779 case RID_AT_FINALLY
:
10780 case RID_AT_SYNCHRONIZED
:
10782 statement
= cp_parser_objc_statement (parser
);
10786 statement
= cp_parser_try_block (parser
);
10789 case RID_NAMESPACE
:
10790 /* This must be a namespace alias definition. */
10791 cp_parser_declaration_statement (parser
);
10794 case RID_TRANSACTION_ATOMIC
:
10795 case RID_TRANSACTION_RELAXED
:
10796 case RID_SYNCHRONIZED
:
10797 case RID_ATOMIC_NOEXCEPT
:
10798 case RID_ATOMIC_CANCEL
:
10799 statement
= cp_parser_transaction (parser
, token
);
10801 case RID_TRANSACTION_CANCEL
:
10802 statement
= cp_parser_transaction_cancel (parser
);
10806 /* It might be a keyword like `int' that can start a
10807 declaration-statement. */
10811 else if (token
->type
== CPP_NAME
)
10813 /* If the next token is a `:', then we are looking at a
10814 labeled-statement. */
10815 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10816 if (token
->type
== CPP_COLON
)
10818 /* Looks like a labeled-statement with an ordinary label.
10819 Parse the label, and then use tail recursion to parse
10822 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10823 in_compound
= false;
10827 /* Anything that starts with a `{' must be a compound-statement. */
10828 else if (token
->type
== CPP_OPEN_BRACE
)
10829 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10830 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10831 a statement all its own. */
10832 else if (token
->type
== CPP_PRAGMA
)
10834 /* Only certain OpenMP pragmas are attached to statements, and thus
10835 are considered statements themselves. All others are not. In
10836 the context of a compound, accept the pragma as a "statement" and
10837 return so that we can check for a close brace. Otherwise we
10838 require a real statement and must go back and read one. */
10840 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10841 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10845 else if (token
->type
== CPP_EOF
)
10847 cp_parser_error (parser
, "expected statement");
10851 /* Everything else must be a declaration-statement or an
10852 expression-statement. Try for the declaration-statement
10853 first, unless we are looking at a `;', in which case we know that
10854 we have an expression-statement. */
10857 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10859 if (std_attrs
!= NULL_TREE
)
10861 /* Attributes should be parsed as part of the the
10862 declaration, so let's un-parse them. */
10863 saved_tokens
.rollback();
10864 std_attrs
= NULL_TREE
;
10867 cp_parser_parse_tentatively (parser
);
10868 /* Try to parse the declaration-statement. */
10869 cp_parser_declaration_statement (parser
);
10870 /* If that worked, we're done. */
10871 if (cp_parser_parse_definitely (parser
))
10874 /* All preceding labels have been parsed at this point. */
10875 if (loc_after_labels
!= NULL
)
10876 *loc_after_labels
= statement_location
;
10878 /* Look for an expression-statement instead. */
10879 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10881 /* Handle [[fallthrough]];. */
10882 if (attribute_fallthrough_p (std_attrs
))
10884 /* The next token after the fallthrough attribute is ';'. */
10885 if (statement
== NULL_TREE
)
10887 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10888 statement
= build_call_expr_internal_loc (statement_location
,
10890 void_type_node
, 0);
10891 finish_expr_stmt (statement
);
10894 warning_at (statement_location
, OPT_Wattributes
,
10895 "%<fallthrough%> attribute not followed by %<;%>");
10896 std_attrs
= NULL_TREE
;
10900 /* Set the line number for the statement. */
10901 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10902 SET_EXPR_LOCATION (statement
, statement_location
);
10904 /* Allow "[[fallthrough]];", but warn otherwise. */
10905 if (std_attrs
!= NULL_TREE
)
10906 warning_at (attrs_location
,
10908 "attributes at the beginning of statement are ignored");
10911 /* Parse the label for a labeled-statement, i.e.
10914 case constant-expression :
10918 case constant-expression ... constant-expression : statement
10920 When a label is parsed without errors, the label is added to the
10921 parse tree by the finish_* functions, so this function doesn't
10922 have to return the label. */
10925 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10928 tree label
= NULL_TREE
;
10929 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10931 /* The next token should be an identifier. */
10932 token
= cp_lexer_peek_token (parser
->lexer
);
10933 if (token
->type
!= CPP_NAME
10934 && token
->type
!= CPP_KEYWORD
)
10936 cp_parser_error (parser
, "expected labeled-statement");
10940 /* Remember whether this case or a user-defined label is allowed to fall
10942 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10944 parser
->colon_corrects_to_scope_p
= false;
10945 switch (token
->keyword
)
10949 tree expr
, expr_hi
;
10950 cp_token
*ellipsis
;
10952 /* Consume the `case' token. */
10953 cp_lexer_consume_token (parser
->lexer
);
10954 /* Parse the constant-expression. */
10955 expr
= cp_parser_constant_expression (parser
);
10956 if (check_for_bare_parameter_packs (expr
))
10957 expr
= error_mark_node
;
10959 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10960 if (ellipsis
->type
== CPP_ELLIPSIS
)
10962 /* Consume the `...' token. */
10963 cp_lexer_consume_token (parser
->lexer
);
10964 expr_hi
= cp_parser_constant_expression (parser
);
10965 if (check_for_bare_parameter_packs (expr_hi
))
10966 expr_hi
= error_mark_node
;
10968 /* We don't need to emit warnings here, as the common code
10969 will do this for us. */
10972 expr_hi
= NULL_TREE
;
10974 if (parser
->in_switch_statement_p
)
10976 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
10977 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10978 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10981 error_at (token
->location
,
10982 "case label %qE not within a switch statement",
10988 /* Consume the `default' token. */
10989 cp_lexer_consume_token (parser
->lexer
);
10991 if (parser
->in_switch_statement_p
)
10993 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10994 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
10995 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
10998 error_at (token
->location
, "case label not within a switch statement");
11002 /* Anything else must be an ordinary label. */
11003 label
= finish_label_stmt (cp_parser_identifier (parser
));
11004 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11005 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11009 /* Require the `:' token. */
11010 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11012 /* An ordinary label may optionally be followed by attributes.
11013 However, this is only permitted if the attributes are then
11014 followed by a semicolon. This is because, for backward
11015 compatibility, when parsing
11016 lab: __attribute__ ((unused)) int i;
11017 we want the attribute to attach to "i", not "lab". */
11018 if (label
!= NULL_TREE
11019 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11022 cp_parser_parse_tentatively (parser
);
11023 attrs
= cp_parser_gnu_attributes_opt (parser
);
11024 if (attrs
== NULL_TREE
11025 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11026 cp_parser_abort_tentative_parse (parser
);
11027 else if (!cp_parser_parse_definitely (parser
))
11030 attributes
= chainon (attributes
, attrs
);
11033 if (attributes
!= NULL_TREE
)
11034 cplus_decl_attributes (&label
, attributes
, 0);
11036 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11039 /* Parse an expression-statement.
11041 expression-statement:
11044 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11045 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11046 indicates whether this expression-statement is part of an
11047 expression statement. */
11050 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11052 tree statement
= NULL_TREE
;
11053 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11054 location_t loc
= token
->location
;
11056 /* There might be attribute fallthrough. */
11057 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11059 /* If the next token is a ';', then there is no expression
11061 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11063 statement
= cp_parser_expression (parser
);
11064 if (statement
== error_mark_node
11065 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11067 cp_parser_skip_to_end_of_block_or_statement (parser
);
11068 return error_mark_node
;
11072 /* Handle [[fallthrough]];. */
11073 if (attribute_fallthrough_p (attr
))
11075 /* The next token after the fallthrough attribute is ';'. */
11076 if (statement
== NULL_TREE
)
11077 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11078 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11079 void_type_node
, 0);
11081 warning_at (loc
, OPT_Wattributes
,
11082 "%<fallthrough%> attribute not followed by %<;%>");
11086 /* Allow "[[fallthrough]];", but warn otherwise. */
11087 if (attr
!= NULL_TREE
)
11088 warning_at (loc
, OPT_Wattributes
,
11089 "attributes at the beginning of statement are ignored");
11091 /* Give a helpful message for "A<T>::type t;" and the like. */
11092 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11093 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11095 if (TREE_CODE (statement
) == SCOPE_REF
)
11096 error_at (token
->location
, "need %<typename%> before %qE because "
11097 "%qT is a dependent scope",
11098 statement
, TREE_OPERAND (statement
, 0));
11099 else if (is_overloaded_fn (statement
)
11100 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11103 tree fn
= get_first_fn (statement
);
11104 error_at (token
->location
,
11105 "%<%T::%D%> names the constructor, not the type",
11106 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11110 /* Consume the final `;'. */
11111 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11113 if (in_statement_expr
11114 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11115 /* This is the final expression statement of a statement
11117 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11118 else if (statement
)
11119 statement
= finish_expr_stmt (statement
);
11124 /* Parse a compound-statement.
11126 compound-statement:
11127 { statement-seq [opt] }
11131 compound-statement:
11132 { label-declaration-seq [opt] statement-seq [opt] }
11134 label-declaration-seq:
11136 label-declaration-seq label-declaration
11138 Returns a tree representing the statement. */
11141 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11142 int bcs_flags
, bool function_body
)
11144 tree compound_stmt
;
11145 matching_braces braces
;
11147 /* Consume the `{'. */
11148 if (!braces
.require_open (parser
))
11149 return error_mark_node
;
11150 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11151 && !function_body
&& cxx_dialect
< cxx14
)
11152 pedwarn (input_location
, OPT_Wpedantic
,
11153 "compound-statement in %<constexpr%> function");
11154 /* Begin the compound-statement. */
11155 compound_stmt
= begin_compound_stmt (bcs_flags
);
11156 /* If the next keyword is `__label__' we have a label declaration. */
11157 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11158 cp_parser_label_declaration (parser
);
11159 /* Parse an (optional) statement-seq. */
11160 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11161 /* Finish the compound-statement. */
11162 finish_compound_stmt (compound_stmt
);
11163 /* Consume the `}'. */
11164 braces
.require_close (parser
);
11166 return compound_stmt
;
11169 /* Parse an (optional) statement-seq.
11173 statement-seq [opt] statement */
11176 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11178 /* Scan statements until there aren't any more. */
11181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11183 /* If we are looking at a `}', then we have run out of
11184 statements; the same is true if we have reached the end
11185 of file, or have stumbled upon a stray '@end'. */
11186 if (token
->type
== CPP_CLOSE_BRACE
11187 || token
->type
== CPP_EOF
11188 || token
->type
== CPP_PRAGMA_EOL
11189 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11192 /* If we are in a compound statement and find 'else' then
11193 something went wrong. */
11194 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11196 if (parser
->in_statement
& IN_IF_STMT
)
11200 token
= cp_lexer_consume_token (parser
->lexer
);
11201 error_at (token
->location
, "%<else%> without a previous %<if%>");
11205 /* Parse the statement. */
11206 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11210 /* Return true if we're looking at (init; cond), false otherwise. */
11213 cp_parser_init_statement_p (cp_parser
*parser
)
11215 /* Save tokens so that we can put them back. */
11216 cp_lexer_save_tokens (parser
->lexer
);
11218 /* Look for ';' that is not nested in () or {}. */
11219 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11220 /*recovering=*/false,
11222 /*consume_paren=*/false);
11224 /* Roll back the tokens we skipped. */
11225 cp_lexer_rollback_tokens (parser
->lexer
);
11230 /* Parse a selection-statement.
11232 selection-statement:
11233 if ( init-statement [opt] condition ) statement
11234 if ( init-statement [opt] condition ) statement else statement
11235 switch ( init-statement [opt] condition ) statement
11237 Returns the new IF_STMT or SWITCH_STMT.
11239 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11240 is a (possibly labeled) if statement which is not enclosed in
11241 braces and has an else clause. This is used to implement
11244 CHAIN is a vector of if-else-if conditions. This is used to implement
11245 -Wduplicated-cond. */
11248 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11253 token_indent_info guard_tinfo
;
11258 /* Peek at the next token. */
11259 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11260 guard_tinfo
= get_token_indent_info (token
);
11262 /* See what kind of keyword it is. */
11263 keyword
= token
->keyword
;
11273 if (keyword
== RID_IF
11274 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11278 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11279 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11280 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11281 "with -std=c++17 or -std=gnu++17");
11284 /* Look for the `('. */
11285 matching_parens parens
;
11286 if (!parens
.require_open (parser
))
11288 cp_parser_skip_to_end_of_statement (parser
);
11289 return error_mark_node
;
11292 /* Begin the selection-statement. */
11293 if (keyword
== RID_IF
)
11295 statement
= begin_if_stmt ();
11296 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11299 statement
= begin_switch_stmt ();
11301 /* Parse the optional init-statement. */
11302 if (cp_parser_init_statement_p (parser
))
11305 if (cxx_dialect
< cxx17
)
11306 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11307 "init-statement in selection statements only available "
11308 "with -std=c++17 or -std=gnu++17");
11309 cp_parser_init_statement (parser
, &decl
);
11312 /* Parse the condition. */
11313 condition
= cp_parser_condition (parser
);
11314 /* Look for the `)'. */
11315 if (!parens
.require_close (parser
))
11316 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11317 /*consume_paren=*/true);
11319 if (keyword
== RID_IF
)
11322 unsigned char in_statement
;
11324 /* Add the condition. */
11325 condition
= finish_if_stmt_cond (condition
, statement
);
11327 if (warn_duplicated_cond
)
11328 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11331 /* Parse the then-clause. */
11332 in_statement
= parser
->in_statement
;
11333 parser
->in_statement
|= IN_IF_STMT
;
11335 /* Outside a template, the non-selected branch of a constexpr
11336 if is a 'discarded statement', i.e. unevaluated. */
11337 bool was_discarded
= in_discarded_stmt
;
11338 bool discard_then
= (cx
&& !processing_template_decl
11339 && integer_zerop (condition
));
11342 in_discarded_stmt
= true;
11343 ++c_inhibit_evaluation_warnings
;
11346 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11349 parser
->in_statement
= in_statement
;
11351 finish_then_clause (statement
);
11355 THEN_CLAUSE (statement
) = NULL_TREE
;
11356 in_discarded_stmt
= was_discarded
;
11357 --c_inhibit_evaluation_warnings
;
11360 /* If the next token is `else', parse the else-clause. */
11361 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11364 bool discard_else
= (cx
&& !processing_template_decl
11365 && integer_nonzerop (condition
));
11368 in_discarded_stmt
= true;
11369 ++c_inhibit_evaluation_warnings
;
11373 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11374 /* Consume the `else' keyword. */
11375 cp_lexer_consume_token (parser
->lexer
);
11376 if (warn_duplicated_cond
)
11378 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11382 /* We've got "if (COND) else if (COND2)". Start
11383 the condition chain and add COND as the first
11385 chain
= new vec
<tree
> ();
11386 if (!CONSTANT_CLASS_P (condition
)
11387 && !TREE_SIDE_EFFECTS (condition
))
11389 /* Wrap it in a NOP_EXPR so that we can set the
11390 location of the condition. */
11391 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11393 SET_EXPR_LOCATION (e
, token
->location
);
11394 chain
->safe_push (e
);
11397 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11400 /* This is if-else without subsequent if. Zap the
11401 condition chain; we would have already warned at
11407 begin_else_clause (statement
);
11408 /* Parse the else-clause. */
11409 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11410 guard_tinfo
, chain
);
11412 finish_else_clause (statement
);
11414 /* If we are currently parsing a then-clause, then
11415 IF_P will not be NULL. We set it to true to
11416 indicate that this if statement has an else clause.
11417 This may trigger the Wparentheses warning below
11418 when we get back up to the parent if statement. */
11424 ELSE_CLAUSE (statement
) = NULL_TREE
;
11425 in_discarded_stmt
= was_discarded
;
11426 --c_inhibit_evaluation_warnings
;
11431 /* This if statement does not have an else clause. If
11432 NESTED_IF is true, then the then-clause has an if
11433 statement which does have an else clause. We warn
11434 about the potential ambiguity. */
11436 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11437 "suggest explicit braces to avoid ambiguous"
11439 if (warn_duplicated_cond
)
11441 /* We don't need the condition chain anymore. */
11447 /* Now we're all done with the if-statement. */
11448 finish_if_stmt (statement
);
11452 bool in_switch_statement_p
;
11453 unsigned char in_statement
;
11455 /* Add the condition. */
11456 finish_switch_cond (condition
, statement
);
11458 /* Parse the body of the switch-statement. */
11459 in_switch_statement_p
= parser
->in_switch_statement_p
;
11460 in_statement
= parser
->in_statement
;
11461 parser
->in_switch_statement_p
= true;
11462 parser
->in_statement
|= IN_SWITCH_STMT
;
11463 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11465 parser
->in_switch_statement_p
= in_switch_statement_p
;
11466 parser
->in_statement
= in_statement
;
11468 /* Now we're all done with the switch-statement. */
11469 finish_switch_stmt (statement
);
11477 cp_parser_error (parser
, "expected selection-statement");
11478 return error_mark_node
;
11482 /* Parse a condition.
11486 type-specifier-seq declarator = initializer-clause
11487 type-specifier-seq declarator braced-init-list
11492 type-specifier-seq declarator asm-specification [opt]
11493 attributes [opt] = assignment-expression
11495 Returns the expression that should be tested. */
11498 cp_parser_condition (cp_parser
* parser
)
11500 cp_decl_specifier_seq type_specifiers
;
11501 const char *saved_message
;
11502 int declares_class_or_enum
;
11504 /* Try the declaration first. */
11505 cp_parser_parse_tentatively (parser
);
11506 /* New types are not allowed in the type-specifier-seq for a
11508 saved_message
= parser
->type_definition_forbidden_message
;
11509 parser
->type_definition_forbidden_message
11510 = G_("types may not be defined in conditions");
11511 /* Parse the type-specifier-seq. */
11512 cp_parser_decl_specifier_seq (parser
,
11513 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11515 &declares_class_or_enum
);
11516 /* Restore the saved message. */
11517 parser
->type_definition_forbidden_message
= saved_message
;
11518 /* If all is well, we might be looking at a declaration. */
11519 if (!cp_parser_error_occurred (parser
))
11522 tree asm_specification
;
11524 cp_declarator
*declarator
;
11525 tree initializer
= NULL_TREE
;
11527 /* Parse the declarator. */
11528 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11529 /*ctor_dtor_or_conv_p=*/NULL
,
11530 /*parenthesized_p=*/NULL
,
11531 /*member_p=*/false,
11532 /*friend_p=*/false);
11533 /* Parse the attributes. */
11534 attributes
= cp_parser_attributes_opt (parser
);
11535 /* Parse the asm-specification. */
11536 asm_specification
= cp_parser_asm_specification_opt (parser
);
11537 /* If the next token is not an `=' or '{', then we might still be
11538 looking at an expression. For example:
11542 looks like a decl-specifier-seq and a declarator -- but then
11543 there is no `=', so this is an expression. */
11544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11545 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11546 cp_parser_simulate_error (parser
);
11548 /* If we did see an `=' or '{', then we are looking at a declaration
11550 if (cp_parser_parse_definitely (parser
))
11553 bool non_constant_p
;
11554 int flags
= LOOKUP_ONLYCONVERTING
;
11556 /* Create the declaration. */
11557 decl
= start_decl (declarator
, &type_specifiers
,
11558 /*initialized_p=*/true,
11559 attributes
, /*prefix_attributes=*/NULL_TREE
,
11562 /* Parse the initializer. */
11563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11565 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11566 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11571 /* Consume the `='. */
11572 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11573 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11575 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11576 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11578 /* Process the initializer. */
11579 cp_finish_decl (decl
,
11580 initializer
, !non_constant_p
,
11585 pop_scope (pushed_scope
);
11587 return convert_from_reference (decl
);
11590 /* If we didn't even get past the declarator successfully, we are
11591 definitely not looking at a declaration. */
11593 cp_parser_abort_tentative_parse (parser
);
11595 /* Otherwise, we are looking at an expression. */
11596 return cp_parser_expression (parser
);
11599 /* Parses a for-statement or range-for-statement until the closing ')',
11603 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11605 tree init
, scope
, decl
;
11608 /* Begin the for-statement. */
11609 scope
= begin_for_scope (&init
);
11611 /* Parse the initialization. */
11612 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11615 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11617 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11621 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11622 unsigned short unroll
)
11624 /* Normal for loop */
11625 tree condition
= NULL_TREE
;
11626 tree expression
= NULL_TREE
;
11629 stmt
= begin_for_stmt (scope
, init
);
11630 /* The init-statement has already been parsed in
11631 cp_parser_init_statement, so no work is needed here. */
11632 finish_init_stmt (stmt
);
11634 /* If there's a condition, process it. */
11635 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11636 condition
= cp_parser_condition (parser
);
11639 cp_parser_error (parser
, "missing loop condition in loop with "
11640 "%<GCC ivdep%> pragma");
11641 condition
= error_mark_node
;
11645 cp_parser_error (parser
, "missing loop condition in loop with "
11646 "%<GCC unroll%> pragma");
11647 condition
= error_mark_node
;
11649 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11650 /* Look for the `;'. */
11651 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11653 /* If there's an expression, process it. */
11654 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11655 expression
= cp_parser_expression (parser
);
11656 finish_for_expr (expression
, stmt
);
11661 /* Tries to parse a range-based for-statement:
11664 decl-specifier-seq declarator : expression
11666 The decl-specifier-seq declarator and the `:' are already parsed by
11667 cp_parser_init_statement. If processing_template_decl it returns a
11668 newly created RANGE_FOR_STMT; if not, it is converted to a
11669 regular FOR_STMT. */
11672 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11673 bool ivdep
, unsigned short unroll
)
11675 tree stmt
, range_expr
;
11676 auto_vec
<cxx_binding
*, 16> bindings
;
11677 auto_vec
<tree
, 16> names
;
11678 tree decomp_first_name
= NULL_TREE
;
11679 unsigned int decomp_cnt
= 0;
11681 /* Get the range declaration momentarily out of the way so that
11682 the range expression doesn't clash with it. */
11683 if (range_decl
!= error_mark_node
)
11685 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11687 tree v
= DECL_VALUE_EXPR (range_decl
);
11688 /* For decomposition declaration get all of the corresponding
11689 declarations out of the way. */
11690 if (TREE_CODE (v
) == ARRAY_REF
11691 && VAR_P (TREE_OPERAND (v
, 0))
11692 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11694 tree d
= range_decl
;
11695 range_decl
= TREE_OPERAND (v
, 0);
11696 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11697 decomp_first_name
= d
;
11698 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11700 tree name
= DECL_NAME (d
);
11701 names
.safe_push (name
);
11702 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11703 IDENTIFIER_BINDING (name
)
11704 = IDENTIFIER_BINDING (name
)->previous
;
11708 if (names
.is_empty ())
11710 tree name
= DECL_NAME (range_decl
);
11711 names
.safe_push (name
);
11712 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11713 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11719 bool expr_non_constant_p
;
11720 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11723 range_expr
= cp_parser_expression (parser
);
11725 /* Put the range declaration(s) back into scope. */
11726 for (unsigned int i
= 0; i
< names
.length (); i
++)
11728 cxx_binding
*binding
= bindings
[i
];
11729 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11730 IDENTIFIER_BINDING (names
[i
]) = binding
;
11733 /* If in template, STMT is converted to a normal for-statement
11734 at instantiation. If not, it is done just ahead. */
11735 if (processing_template_decl
)
11737 if (check_for_bare_parameter_packs (range_expr
))
11738 range_expr
= error_mark_node
;
11739 stmt
= begin_range_for_stmt (scope
, init
);
11741 RANGE_FOR_IVDEP (stmt
) = 1;
11743 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11744 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11745 if (!type_dependent_expression_p (range_expr
)
11746 /* do_auto_deduction doesn't mess with template init-lists. */
11747 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11748 do_range_for_auto_deduction (range_decl
, range_expr
);
11752 stmt
= begin_for_stmt (scope
, init
);
11753 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11754 decomp_first_name
, decomp_cnt
, ivdep
,
11760 /* Subroutine of cp_convert_range_for: given the initializer expression,
11761 builds up the range temporary. */
11764 build_range_temp (tree range_expr
)
11766 tree range_type
, range_temp
;
11768 /* Find out the type deduced by the declaration
11769 `auto &&__range = range_expr'. */
11770 range_type
= cp_build_reference_type (make_auto (), true);
11771 range_type
= do_auto_deduction (range_type
, range_expr
,
11772 type_uses_auto (range_type
));
11774 /* Create the __range variable. */
11775 range_temp
= build_decl (input_location
, VAR_DECL
,
11776 get_identifier ("__for_range"), range_type
);
11777 TREE_USED (range_temp
) = 1;
11778 DECL_ARTIFICIAL (range_temp
) = 1;
11783 /* Used by cp_parser_range_for in template context: we aren't going to
11784 do a full conversion yet, but we still need to resolve auto in the
11785 type of the for-range-declaration if present. This is basically
11786 a shortcut version of cp_convert_range_for. */
11789 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11791 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11794 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11795 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11796 iter_type
= (cp_parser_perform_range_for_lookup
11797 (range_temp
, &begin_dummy
, &end_dummy
));
11800 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11802 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11804 tf_warning_or_error
);
11805 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11806 iter_decl
, auto_node
);
11811 /* Converts a range-based for-statement into a normal
11812 for-statement, as per the definition.
11814 for (RANGE_DECL : RANGE_EXPR)
11817 should be equivalent to:
11820 auto &&__range = RANGE_EXPR;
11821 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11825 RANGE_DECL = *__begin;
11830 If RANGE_EXPR is an array:
11831 BEGIN_EXPR = __range
11832 END_EXPR = __range + ARRAY_SIZE(__range)
11833 Else if RANGE_EXPR has a member 'begin' or 'end':
11834 BEGIN_EXPR = __range.begin()
11835 END_EXPR = __range.end()
11837 BEGIN_EXPR = begin(__range)
11838 END_EXPR = end(__range);
11840 If __range has a member 'begin' but not 'end', or vice versa, we must
11841 still use the second alternative (it will surely fail, however).
11842 When calling begin()/end() in the third alternative we must use
11843 argument dependent lookup, but always considering 'std' as an associated
11847 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11848 tree decomp_first_name
, unsigned int decomp_cnt
,
11849 bool ivdep
, unsigned short unroll
)
11852 tree iter_type
, begin_expr
, end_expr
;
11853 tree condition
, expression
;
11855 range_expr
= mark_lvalue_use (range_expr
);
11857 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11858 /* If an error happened previously do nothing or else a lot of
11859 unhelpful errors would be issued. */
11860 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11865 if (VAR_P (range_expr
)
11866 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11867 /* Can't bind a reference to an array of runtime bound. */
11868 range_temp
= range_expr
;
11871 range_temp
= build_range_temp (range_expr
);
11872 pushdecl (range_temp
);
11873 cp_finish_decl (range_temp
, range_expr
,
11874 /*is_constant_init*/false, NULL_TREE
,
11875 LOOKUP_ONLYCONVERTING
);
11876 range_temp
= convert_from_reference (range_temp
);
11878 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11879 &begin_expr
, &end_expr
);
11882 /* The new for initialization statement. */
11883 begin
= build_decl (input_location
, VAR_DECL
,
11884 get_identifier ("__for_begin"), iter_type
);
11885 TREE_USED (begin
) = 1;
11886 DECL_ARTIFICIAL (begin
) = 1;
11888 cp_finish_decl (begin
, begin_expr
,
11889 /*is_constant_init*/false, NULL_TREE
,
11890 LOOKUP_ONLYCONVERTING
);
11892 if (cxx_dialect
>= cxx17
)
11893 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11894 end
= build_decl (input_location
, VAR_DECL
,
11895 get_identifier ("__for_end"), iter_type
);
11896 TREE_USED (end
) = 1;
11897 DECL_ARTIFICIAL (end
) = 1;
11899 cp_finish_decl (end
, end_expr
,
11900 /*is_constant_init*/false, NULL_TREE
,
11901 LOOKUP_ONLYCONVERTING
);
11903 finish_init_stmt (statement
);
11905 /* The new for condition. */
11906 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11909 NULL
, tf_warning_or_error
);
11910 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11912 /* The new increment expression. */
11913 expression
= finish_unary_op_expr (input_location
,
11914 PREINCREMENT_EXPR
, begin
,
11915 tf_warning_or_error
);
11916 finish_for_expr (expression
, statement
);
11918 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11919 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11921 /* The declaration is initialized with *__begin inside the loop body. */
11922 cp_finish_decl (range_decl
,
11923 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11924 tf_warning_or_error
),
11925 /*is_constant_init*/false, NULL_TREE
,
11926 LOOKUP_ONLYCONVERTING
);
11927 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11928 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11933 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11934 We need to solve both at the same time because the method used
11935 depends on the existence of members begin or end.
11936 Returns the type deduced for the iterator expression. */
11939 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11941 if (error_operand_p (range
))
11943 *begin
= *end
= error_mark_node
;
11944 return error_mark_node
;
11947 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11949 error ("range-based %<for%> expression of type %qT "
11950 "has incomplete type", TREE_TYPE (range
));
11951 *begin
= *end
= error_mark_node
;
11952 return error_mark_node
;
11954 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11956 /* If RANGE is an array, we will use pointer arithmetic. */
11957 *begin
= decay_conversion (range
, tf_warning_or_error
);
11958 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11960 array_type_nelts_top (TREE_TYPE (range
)),
11962 return TREE_TYPE (*begin
);
11966 /* If it is not an array, we must do a bit of magic. */
11967 tree id_begin
, id_end
;
11968 tree member_begin
, member_end
;
11970 *begin
= *end
= error_mark_node
;
11972 id_begin
= get_identifier ("begin");
11973 id_end
= get_identifier ("end");
11974 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11975 /*protect=*/2, /*want_type=*/false,
11976 tf_warning_or_error
);
11977 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11978 /*protect=*/2, /*want_type=*/false,
11979 tf_warning_or_error
);
11981 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11983 /* Use the member functions. */
11984 if (member_begin
!= NULL_TREE
)
11985 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11987 error ("range-based %<for%> expression of type %qT has an "
11988 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11990 if (member_end
!= NULL_TREE
)
11991 *end
= cp_parser_range_for_member_function (range
, id_end
);
11993 error ("range-based %<for%> expression of type %qT has a "
11994 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11998 /* Use global functions with ADL. */
11999 vec
<tree
, va_gc
> *vec
;
12000 vec
= make_tree_vector ();
12002 vec_safe_push (vec
, range
);
12004 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12005 tf_warning_or_error
);
12006 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12007 tf_warning_or_error
);
12008 member_end
= perform_koenig_lookup (id_end
, vec
,
12009 tf_warning_or_error
);
12010 *end
= finish_call_expr (member_end
, &vec
, false, true,
12011 tf_warning_or_error
);
12013 release_tree_vector (vec
);
12016 /* Last common checks. */
12017 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12019 /* If one of the expressions is an error do no more checks. */
12020 *begin
= *end
= error_mark_node
;
12021 return error_mark_node
;
12023 else if (type_dependent_expression_p (*begin
)
12024 || type_dependent_expression_p (*end
))
12025 /* Can happen, when, eg, in a template context, Koenig lookup
12026 can't resolve begin/end (c++/58503). */
12030 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12031 /* The unqualified type of the __begin and __end temporaries should
12032 be the same, as required by the multiple auto declaration. */
12033 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12035 if (cxx_dialect
>= cxx17
12036 && (build_x_binary_op (input_location
, NE_EXPR
,
12037 *begin
, ERROR_MARK
,
12040 != error_mark_node
))
12041 /* P0184R0 allows __begin and __end to have different types,
12042 but make sure they are comparable so we can give a better
12045 error ("inconsistent begin/end types in range-based %<for%> "
12046 "statement: %qT and %qT",
12047 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12054 /* Helper function for cp_parser_perform_range_for_lookup.
12055 Builds a tree for RANGE.IDENTIFIER(). */
12058 cp_parser_range_for_member_function (tree range
, tree identifier
)
12061 vec
<tree
, va_gc
> *vec
;
12063 member
= finish_class_member_access_expr (range
, identifier
,
12064 false, tf_warning_or_error
);
12065 if (member
== error_mark_node
)
12066 return error_mark_node
;
12068 vec
= make_tree_vector ();
12069 res
= finish_call_expr (member
, &vec
,
12070 /*disallow_virtual=*/false,
12071 /*koenig_p=*/false,
12072 tf_warning_or_error
);
12073 release_tree_vector (vec
);
12077 /* Parse an iteration-statement.
12079 iteration-statement:
12080 while ( condition ) statement
12081 do statement while ( expression ) ;
12082 for ( init-statement condition [opt] ; expression [opt] )
12085 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12088 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12089 unsigned short unroll
)
12094 unsigned char in_statement
;
12095 token_indent_info guard_tinfo
;
12097 /* Peek at the next token. */
12098 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12100 return error_mark_node
;
12102 guard_tinfo
= get_token_indent_info (token
);
12104 /* Remember whether or not we are already within an iteration
12106 in_statement
= parser
->in_statement
;
12108 /* See what kind of keyword it is. */
12109 keyword
= token
->keyword
;
12116 /* Begin the while-statement. */
12117 statement
= begin_while_stmt ();
12118 /* Look for the `('. */
12119 matching_parens parens
;
12120 parens
.require_open (parser
);
12121 /* Parse the condition. */
12122 condition
= cp_parser_condition (parser
);
12123 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12124 /* Look for the `)'. */
12125 parens
.require_close (parser
);
12126 /* Parse the dependent statement. */
12127 parser
->in_statement
= IN_ITERATION_STMT
;
12128 bool prev
= note_iteration_stmt_body_start ();
12129 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12130 note_iteration_stmt_body_end (prev
);
12131 parser
->in_statement
= in_statement
;
12132 /* We're done with the while-statement. */
12133 finish_while_stmt (statement
);
12141 /* Begin the do-statement. */
12142 statement
= begin_do_stmt ();
12143 /* Parse the body of the do-statement. */
12144 parser
->in_statement
= IN_ITERATION_STMT
;
12145 bool prev
= note_iteration_stmt_body_start ();
12146 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12147 note_iteration_stmt_body_end (prev
);
12148 parser
->in_statement
= in_statement
;
12149 finish_do_body (statement
);
12150 /* Look for the `while' keyword. */
12151 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12152 /* Look for the `('. */
12153 matching_parens parens
;
12154 parens
.require_open (parser
);
12155 /* Parse the expression. */
12156 expression
= cp_parser_expression (parser
);
12157 /* We're done with the do-statement. */
12158 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12159 /* Look for the `)'. */
12160 parens
.require_close (parser
);
12161 /* Look for the `;'. */
12162 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12168 /* Look for the `('. */
12169 matching_parens parens
;
12170 parens
.require_open (parser
);
12172 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12174 /* Look for the `)'. */
12175 parens
.require_close (parser
);
12177 /* Parse the body of the for-statement. */
12178 parser
->in_statement
= IN_ITERATION_STMT
;
12179 bool prev
= note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12181 note_iteration_stmt_body_end (prev
);
12182 parser
->in_statement
= in_statement
;
12184 /* We're done with the for-statement. */
12185 finish_for_stmt (statement
);
12190 cp_parser_error (parser
, "expected iteration-statement");
12191 statement
= error_mark_node
;
12198 /* Parse a init-statement or the declarator of a range-based-for.
12199 Returns true if a range-based-for declaration is seen.
12202 expression-statement
12203 simple-declaration */
12206 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12208 /* If the next token is a `;', then we have an empty
12209 expression-statement. Grammatically, this is also a
12210 simple-declaration, but an invalid one, because it does not
12211 declare anything. Therefore, if we did not handle this case
12212 specially, we would issue an error message about an invalid
12214 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12216 bool is_range_for
= false;
12217 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12219 /* A colon is used in range-based for. */
12220 parser
->colon_corrects_to_scope_p
= false;
12222 /* We're going to speculatively look for a declaration, falling back
12223 to an expression, if necessary. */
12224 cp_parser_parse_tentatively (parser
);
12225 /* Parse the declaration. */
12226 cp_parser_simple_declaration (parser
,
12227 /*function_definition_allowed_p=*/false,
12229 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12232 /* It is a range-for, consume the ':' */
12233 cp_lexer_consume_token (parser
->lexer
);
12234 is_range_for
= true;
12235 if (cxx_dialect
< cxx11
)
12237 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12238 "range-based %<for%> loops only available with "
12239 "-std=c++11 or -std=gnu++11");
12240 *decl
= error_mark_node
;
12244 /* The ';' is not consumed yet because we told
12245 cp_parser_simple_declaration not to. */
12246 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12248 if (cp_parser_parse_definitely (parser
))
12249 return is_range_for
;
12250 /* If the tentative parse failed, then we shall need to look for an
12251 expression-statement. */
12253 /* If we are here, it is an expression-statement. */
12254 cp_parser_expression_statement (parser
, NULL_TREE
);
12258 /* Parse a jump-statement.
12263 return expression [opt] ;
12264 return braced-init-list ;
12270 goto * expression ;
12272 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12275 cp_parser_jump_statement (cp_parser
* parser
)
12277 tree statement
= error_mark_node
;
12280 unsigned char in_statement
;
12282 /* Peek at the next token. */
12283 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12285 return error_mark_node
;
12287 /* See what kind of keyword it is. */
12288 keyword
= token
->keyword
;
12292 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12293 switch (in_statement
)
12296 error_at (token
->location
, "break statement not within loop or switch");
12299 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12300 || in_statement
== IN_ITERATION_STMT
);
12301 statement
= finish_break_stmt ();
12302 if (in_statement
== IN_ITERATION_STMT
)
12303 break_maybe_infinite_loop ();
12306 error_at (token
->location
, "invalid exit from OpenMP structured block");
12309 error_at (token
->location
, "break statement used with OpenMP for loop");
12312 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12316 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12319 error_at (token
->location
, "continue statement not within a loop");
12321 /* Fall through. */
12322 case IN_ITERATION_STMT
:
12324 statement
= finish_continue_stmt ();
12327 error_at (token
->location
, "invalid exit from OpenMP structured block");
12330 gcc_unreachable ();
12332 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12338 bool expr_non_constant_p
;
12340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12342 cp_lexer_set_source_position (parser
->lexer
);
12343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12344 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12346 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12347 expr
= cp_parser_expression (parser
);
12349 /* If the next token is a `;', then there is no
12352 /* Build the return-statement. */
12353 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12354 /* Don't deduce from a discarded return statement. */;
12356 statement
= finish_return_stmt (expr
);
12357 /* Look for the final `;'. */
12358 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12363 if (parser
->in_function_body
12364 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12366 error ("%<goto%> in %<constexpr%> function");
12367 cp_function_chain
->invalid_constexpr
= true;
12370 /* Create the goto-statement. */
12371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12373 /* Issue a warning about this use of a GNU extension. */
12374 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12375 /* Consume the '*' token. */
12376 cp_lexer_consume_token (parser
->lexer
);
12377 /* Parse the dependent expression. */
12378 finish_goto_stmt (cp_parser_expression (parser
));
12381 finish_goto_stmt (cp_parser_identifier (parser
));
12382 /* Look for the final `;'. */
12383 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12387 cp_parser_error (parser
, "expected jump-statement");
12394 /* Parse a declaration-statement.
12396 declaration-statement:
12397 block-declaration */
12400 cp_parser_declaration_statement (cp_parser
* parser
)
12404 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12405 p
= obstack_alloc (&declarator_obstack
, 0);
12407 /* Parse the block-declaration. */
12408 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12410 /* Free any declarators allocated. */
12411 obstack_free (&declarator_obstack
, p
);
12414 /* Some dependent statements (like `if (cond) statement'), are
12415 implicitly in their own scope. In other words, if the statement is
12416 a single statement (as opposed to a compound-statement), it is
12417 none-the-less treated as if it were enclosed in braces. Any
12418 declarations appearing in the dependent statement are out of scope
12419 after control passes that point. This function parses a statement,
12420 but ensures that is in its own scope, even if it is not a
12421 compound-statement.
12423 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12424 is a (possibly labeled) if statement which is not enclosed in
12425 braces and has an else clause. This is used to implement
12428 CHAIN is a vector of if-else-if conditions. This is used to implement
12431 Returns the new statement. */
12434 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12435 const token_indent_info
&guard_tinfo
,
12439 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12440 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12441 token_indent_info body_tinfo
12442 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12447 /* Mark if () ; with a special NOP_EXPR. */
12448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12450 cp_lexer_consume_token (parser
->lexer
);
12451 statement
= add_stmt (build_empty_stmt (body_loc
));
12453 if (guard_tinfo
.keyword
== RID_IF
12454 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12455 warning_at (body_loc
, OPT_Wempty_body
,
12456 "suggest braces around empty body in an %<if%> statement");
12457 else if (guard_tinfo
.keyword
== RID_ELSE
)
12458 warning_at (body_loc
, OPT_Wempty_body
,
12459 "suggest braces around empty body in an %<else%> statement");
12461 /* if a compound is opened, we simply parse the statement directly. */
12462 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12463 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12464 /* If the token is not a `{', then we must take special action. */
12467 /* Create a compound-statement. */
12468 statement
= begin_compound_stmt (0);
12469 /* Parse the dependent-statement. */
12470 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12471 &body_loc_after_labels
);
12472 /* Finish the dummy compound-statement. */
12473 finish_compound_stmt (statement
);
12476 token_indent_info next_tinfo
12477 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12478 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12480 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12481 && next_tinfo
.type
!= CPP_SEMICOLON
)
12482 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12483 guard_tinfo
.location
, guard_tinfo
.keyword
);
12485 /* Return the statement. */
12489 /* For some dependent statements (like `while (cond) statement'), we
12490 have already created a scope. Therefore, even if the dependent
12491 statement is a compound-statement, we do not want to create another
12495 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12496 const token_indent_info
&guard_tinfo
)
12498 /* If the token is a `{', then we must take special action. */
12499 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12501 token_indent_info body_tinfo
12502 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12503 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12505 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12506 &loc_after_labels
);
12507 token_indent_info next_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12509 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12511 if (loc_after_labels
!= UNKNOWN_LOCATION
12512 && next_tinfo
.type
!= CPP_SEMICOLON
)
12513 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12514 guard_tinfo
.location
,
12515 guard_tinfo
.keyword
);
12519 /* Avoid calling cp_parser_compound_statement, so that we
12520 don't create a new scope. Do everything else by hand. */
12521 matching_braces braces
;
12522 braces
.require_open (parser
);
12523 /* If the next keyword is `__label__' we have a label declaration. */
12524 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12525 cp_parser_label_declaration (parser
);
12526 /* Parse an (optional) statement-seq. */
12527 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12528 braces
.require_close (parser
);
12532 /* Declarations [gram.dcl.dcl] */
12534 /* Parse an optional declaration-sequence.
12538 declaration-seq declaration */
12541 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12547 token
= cp_lexer_peek_token (parser
->lexer
);
12549 if (token
->type
== CPP_CLOSE_BRACE
12550 || token
->type
== CPP_EOF
12551 || token
->type
== CPP_PRAGMA_EOL
)
12554 if (token
->type
== CPP_SEMICOLON
)
12556 /* A declaration consisting of a single semicolon is
12557 invalid. Allow it unless we're being pedantic. */
12558 cp_lexer_consume_token (parser
->lexer
);
12559 if (!in_system_header_at (input_location
))
12560 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12564 /* If we're entering or exiting a region that's implicitly
12565 extern "C", modify the lang context appropriately. */
12566 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12568 push_lang_context (lang_name_c
);
12569 parser
->implicit_extern_c
= true;
12571 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12573 pop_lang_context ();
12574 parser
->implicit_extern_c
= false;
12577 if (token
->type
== CPP_PRAGMA
)
12579 /* A top-level declaration can consist solely of a #pragma.
12580 A nested declaration cannot, so this is done here and not
12581 in cp_parser_declaration. (A #pragma at block scope is
12582 handled in cp_parser_statement.) */
12583 cp_parser_pragma (parser
, pragma_external
, NULL
);
12587 /* Parse the declaration itself. */
12588 cp_parser_declaration (parser
);
12592 /* Parse a declaration.
12596 function-definition
12597 template-declaration
12598 explicit-instantiation
12599 explicit-specialization
12600 linkage-specification
12601 namespace-definition
12609 __extension__ declaration */
12612 cp_parser_declaration (cp_parser
* parser
)
12616 int saved_pedantic
;
12618 tree attributes
= NULL_TREE
;
12620 /* Check for the `__extension__' keyword. */
12621 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12623 /* Parse the qualified declaration. */
12624 cp_parser_declaration (parser
);
12625 /* Restore the PEDANTIC flag. */
12626 pedantic
= saved_pedantic
;
12631 /* Try to figure out what kind of declaration is present. */
12632 token1
= *cp_lexer_peek_token (parser
->lexer
);
12634 if (token1
.type
!= CPP_EOF
)
12635 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12638 token2
.type
= CPP_EOF
;
12639 token2
.keyword
= RID_MAX
;
12642 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12643 p
= obstack_alloc (&declarator_obstack
, 0);
12645 /* If the next token is `extern' and the following token is a string
12646 literal, then we have a linkage specification. */
12647 if (token1
.keyword
== RID_EXTERN
12648 && cp_parser_is_pure_string_literal (&token2
))
12649 cp_parser_linkage_specification (parser
);
12650 /* If the next token is `template', then we have either a template
12651 declaration, an explicit instantiation, or an explicit
12653 else if (token1
.keyword
== RID_TEMPLATE
)
12655 /* `template <>' indicates a template specialization. */
12656 if (token2
.type
== CPP_LESS
12657 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12658 cp_parser_explicit_specialization (parser
);
12659 /* `template <' indicates a template declaration. */
12660 else if (token2
.type
== CPP_LESS
)
12661 cp_parser_template_declaration (parser
, /*member_p=*/false);
12662 /* Anything else must be an explicit instantiation. */
12664 cp_parser_explicit_instantiation (parser
);
12666 /* If the next token is `export', then we have a template
12668 else if (token1
.keyword
== RID_EXPORT
)
12669 cp_parser_template_declaration (parser
, /*member_p=*/false);
12670 /* If the next token is `extern', 'static' or 'inline' and the one
12671 after that is `template', we have a GNU extended explicit
12672 instantiation directive. */
12673 else if (cp_parser_allow_gnu_extensions_p (parser
)
12674 && (token1
.keyword
== RID_EXTERN
12675 || token1
.keyword
== RID_STATIC
12676 || token1
.keyword
== RID_INLINE
)
12677 && token2
.keyword
== RID_TEMPLATE
)
12678 cp_parser_explicit_instantiation (parser
);
12679 /* If the next token is `namespace', check for a named or unnamed
12680 namespace definition. */
12681 else if (token1
.keyword
== RID_NAMESPACE
12682 && (/* A named namespace definition. */
12683 (token2
.type
== CPP_NAME
12684 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12686 || (token2
.type
== CPP_OPEN_SQUARE
12687 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12688 == CPP_OPEN_SQUARE
)
12689 /* An unnamed namespace definition. */
12690 || token2
.type
== CPP_OPEN_BRACE
12691 || token2
.keyword
== RID_ATTRIBUTE
))
12692 cp_parser_namespace_definition (parser
);
12693 /* An inline (associated) namespace definition. */
12694 else if (token1
.keyword
== RID_INLINE
12695 && token2
.keyword
== RID_NAMESPACE
)
12696 cp_parser_namespace_definition (parser
);
12697 /* Objective-C++ declaration/definition. */
12698 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12699 cp_parser_objc_declaration (parser
, NULL_TREE
);
12700 else if (c_dialect_objc ()
12701 && token1
.keyword
== RID_ATTRIBUTE
12702 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12703 cp_parser_objc_declaration (parser
, attributes
);
12704 /* At this point we may have a template declared by a concept
12706 else if (flag_concepts
12707 && cp_parser_template_declaration_after_export (parser
,
12708 /*member_p=*/false))
12711 /* Try to parse a block-declaration, or a function-definition. */
12712 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12714 /* Free any declarators allocated. */
12715 obstack_free (&declarator_obstack
, p
);
12718 /* Parse a block-declaration.
12723 namespace-alias-definition
12730 __extension__ block-declaration
12735 static_assert-declaration
12737 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12738 part of a declaration-statement. */
12741 cp_parser_block_declaration (cp_parser
*parser
,
12745 int saved_pedantic
;
12747 /* Check for the `__extension__' keyword. */
12748 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12750 /* Parse the qualified declaration. */
12751 cp_parser_block_declaration (parser
, statement_p
);
12752 /* Restore the PEDANTIC flag. */
12753 pedantic
= saved_pedantic
;
12758 /* Peek at the next token to figure out which kind of declaration is
12760 token1
= cp_lexer_peek_token (parser
->lexer
);
12762 /* If the next keyword is `asm', we have an asm-definition. */
12763 if (token1
->keyword
== RID_ASM
)
12766 cp_parser_commit_to_tentative_parse (parser
);
12767 cp_parser_asm_definition (parser
);
12769 /* If the next keyword is `namespace', we have a
12770 namespace-alias-definition. */
12771 else if (token1
->keyword
== RID_NAMESPACE
)
12772 cp_parser_namespace_alias_definition (parser
);
12773 /* If the next keyword is `using', we have a
12774 using-declaration, a using-directive, or an alias-declaration. */
12775 else if (token1
->keyword
== RID_USING
)
12780 cp_parser_commit_to_tentative_parse (parser
);
12781 /* If the token after `using' is `namespace', then we have a
12782 using-directive. */
12783 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12784 if (token2
->keyword
== RID_NAMESPACE
)
12785 cp_parser_using_directive (parser
);
12786 /* If the second token after 'using' is '=', then we have an
12787 alias-declaration. */
12788 else if (cxx_dialect
>= cxx11
12789 && token2
->type
== CPP_NAME
12790 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12791 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12792 cp_parser_alias_declaration (parser
);
12793 /* Otherwise, it's a using-declaration. */
12795 cp_parser_using_declaration (parser
,
12796 /*access_declaration_p=*/false);
12798 /* If the next keyword is `__label__' we have a misplaced label
12800 else if (token1
->keyword
== RID_LABEL
)
12802 cp_lexer_consume_token (parser
->lexer
);
12803 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12804 cp_parser_skip_to_end_of_statement (parser
);
12805 /* If the next token is now a `;', consume it. */
12806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12807 cp_lexer_consume_token (parser
->lexer
);
12809 /* If the next token is `static_assert' we have a static assertion. */
12810 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12811 cp_parser_static_assert (parser
, /*member_p=*/false);
12812 /* Anything else must be a simple-declaration. */
12814 cp_parser_simple_declaration (parser
, !statement_p
,
12815 /*maybe_range_for_decl*/NULL
);
12818 /* Parse a simple-declaration.
12820 simple-declaration:
12821 decl-specifier-seq [opt] init-declarator-list [opt] ;
12822 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12823 brace-or-equal-initializer ;
12825 init-declarator-list:
12827 init-declarator-list , init-declarator
12829 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12830 function-definition as a simple-declaration.
12832 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12833 parsed declaration if it is an uninitialized single declarator not followed
12834 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12835 if present, will not be consumed. */
12838 cp_parser_simple_declaration (cp_parser
* parser
,
12839 bool function_definition_allowed_p
,
12840 tree
*maybe_range_for_decl
)
12842 cp_decl_specifier_seq decl_specifiers
;
12843 int declares_class_or_enum
;
12844 bool saw_declarator
;
12845 location_t comma_loc
= UNKNOWN_LOCATION
;
12846 location_t init_loc
= UNKNOWN_LOCATION
;
12848 if (maybe_range_for_decl
)
12849 *maybe_range_for_decl
= NULL_TREE
;
12851 /* Defer access checks until we know what is being declared; the
12852 checks for names appearing in the decl-specifier-seq should be
12853 done as if we were in the scope of the thing being declared. */
12854 push_deferring_access_checks (dk_deferred
);
12856 /* Parse the decl-specifier-seq. We have to keep track of whether
12857 or not the decl-specifier-seq declares a named class or
12858 enumeration type, since that is the only case in which the
12859 init-declarator-list is allowed to be empty.
12863 In a simple-declaration, the optional init-declarator-list can be
12864 omitted only when declaring a class or enumeration, that is when
12865 the decl-specifier-seq contains either a class-specifier, an
12866 elaborated-type-specifier, or an enum-specifier. */
12867 cp_parser_decl_specifier_seq (parser
,
12868 CP_PARSER_FLAGS_OPTIONAL
,
12870 &declares_class_or_enum
);
12871 /* We no longer need to defer access checks. */
12872 stop_deferring_access_checks ();
12874 /* In a block scope, a valid declaration must always have a
12875 decl-specifier-seq. By not trying to parse declarators, we can
12876 resolve the declaration/expression ambiguity more quickly. */
12877 if (!function_definition_allowed_p
12878 && !decl_specifiers
.any_specifiers_p
)
12880 cp_parser_error (parser
, "expected declaration");
12884 /* If the next two tokens are both identifiers, the code is
12885 erroneous. The usual cause of this situation is code like:
12889 where "T" should name a type -- but does not. */
12890 if (!decl_specifiers
.any_type_specifiers_p
12891 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12893 /* If parsing tentatively, we should commit; we really are
12894 looking at a declaration. */
12895 cp_parser_commit_to_tentative_parse (parser
);
12900 /* If we have seen at least one decl-specifier, and the next token
12901 is not a parenthesis, then we must be looking at a declaration.
12902 (After "int (" we might be looking at a functional cast.) */
12903 if (decl_specifiers
.any_specifiers_p
12904 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12905 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12906 && !cp_parser_error_occurred (parser
))
12907 cp_parser_commit_to_tentative_parse (parser
);
12909 /* Look for C++17 decomposition declaration. */
12910 for (size_t n
= 1; ; n
++)
12911 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12912 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12914 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12915 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12916 && decl_specifiers
.any_specifiers_p
)
12919 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12920 maybe_range_for_decl
,
12923 /* The next token should be either a `,' or a `;'. */
12924 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12925 /* If it's a `;', we are done. */
12926 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12928 /* Anything else is an error. */
12931 /* If we have already issued an error message we don't need
12932 to issue another one. */
12933 if ((decl
!= error_mark_node
12934 && DECL_INITIAL (decl
) != error_mark_node
)
12935 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12936 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12937 /* Skip tokens until we reach the end of the statement. */
12938 cp_parser_skip_to_end_of_statement (parser
);
12939 /* If the next token is now a `;', consume it. */
12940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12941 cp_lexer_consume_token (parser
->lexer
);
12949 bool auto_specifier_p
;
12950 /* NULL_TREE if both variable and function declaration are allowed,
12951 error_mark_node if function declaration are not allowed and
12952 a FUNCTION_DECL that should be diagnosed if it is followed by
12953 variable declarations. */
12954 tree auto_function_declaration
;
12956 last_type
= NULL_TREE
;
12958 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
12959 auto_function_declaration
= NULL_TREE
;
12961 /* Keep going until we hit the `;' at the end of the simple
12963 saw_declarator
= false;
12964 while (cp_lexer_next_token_is_not (parser
->lexer
,
12968 bool function_definition_p
;
12970 tree auto_result
= NULL_TREE
;
12972 if (saw_declarator
)
12974 /* If we are processing next declarator, comma is expected */
12975 token
= cp_lexer_peek_token (parser
->lexer
);
12976 gcc_assert (token
->type
== CPP_COMMA
);
12977 cp_lexer_consume_token (parser
->lexer
);
12978 if (maybe_range_for_decl
)
12980 *maybe_range_for_decl
= error_mark_node
;
12981 if (comma_loc
== UNKNOWN_LOCATION
)
12982 comma_loc
= token
->location
;
12986 saw_declarator
= true;
12988 /* Parse the init-declarator. */
12989 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12991 function_definition_allowed_p
,
12992 /*member_p=*/false,
12993 declares_class_or_enum
,
12994 &function_definition_p
,
12995 maybe_range_for_decl
,
12998 /* If an error occurred while parsing tentatively, exit quickly.
12999 (That usually happens when in the body of a function; each
13000 statement is treated as a declaration-statement until proven
13002 if (cp_parser_error_occurred (parser
))
13005 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13007 /* If the init-declarator-list contains more than one
13008 init-declarator, they shall all form declarations of
13010 if (auto_function_declaration
== NULL_TREE
)
13011 auto_function_declaration
13012 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13013 else if (TREE_CODE (decl
) == FUNCTION_DECL
13014 || auto_function_declaration
!= error_mark_node
)
13016 error_at (decl_specifiers
.locations
[ds_type_spec
],
13017 "non-variable %qD in declaration with more than one "
13018 "declarator with placeholder type",
13019 TREE_CODE (decl
) == FUNCTION_DECL
13020 ? decl
: auto_function_declaration
);
13021 auto_function_declaration
= error_mark_node
;
13026 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13029 && last_type
!= error_mark_node
13030 && !same_type_p (auto_result
, last_type
))
13032 /* If the list of declarators contains more than one declarator,
13033 the type of each declared variable is determined as described
13034 above. If the type deduced for the template parameter U is not
13035 the same in each deduction, the program is ill-formed. */
13036 error_at (decl_specifiers
.locations
[ds_type_spec
],
13037 "inconsistent deduction for %qT: %qT and then %qT",
13038 decl_specifiers
.type
, last_type
, auto_result
);
13039 last_type
= error_mark_node
;
13042 last_type
= auto_result
;
13045 /* Handle function definitions specially. */
13046 if (function_definition_p
)
13048 /* If the next token is a `,', then we are probably
13049 processing something like:
13053 which is erroneous. */
13054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13056 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13057 error_at (token
->location
,
13059 " declarations and function-definitions is forbidden");
13061 /* Otherwise, we're done with the list of declarators. */
13064 pop_deferring_access_checks ();
13068 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13069 *maybe_range_for_decl
= decl
;
13070 /* The next token should be either a `,' or a `;'. */
13071 token
= cp_lexer_peek_token (parser
->lexer
);
13072 /* If it's a `,', there are more declarators to come. */
13073 if (token
->type
== CPP_COMMA
)
13074 /* will be consumed next time around */;
13075 /* If it's a `;', we are done. */
13076 else if (token
->type
== CPP_SEMICOLON
)
13078 else if (maybe_range_for_decl
)
13080 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13081 permerror (decl_specifiers
.locations
[ds_type_spec
],
13082 "types may not be defined in a for-range-declaration");
13085 /* Anything else is an error. */
13088 /* If we have already issued an error message we don't need
13089 to issue another one. */
13090 if ((decl
!= error_mark_node
13091 && DECL_INITIAL (decl
) != error_mark_node
)
13092 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13093 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13094 /* Skip tokens until we reach the end of the statement. */
13095 cp_parser_skip_to_end_of_statement (parser
);
13096 /* If the next token is now a `;', consume it. */
13097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13098 cp_lexer_consume_token (parser
->lexer
);
13101 /* After the first time around, a function-definition is not
13102 allowed -- even if it was OK at first. For example:
13107 function_definition_allowed_p
= false;
13110 /* Issue an error message if no declarators are present, and the
13111 decl-specifier-seq does not itself declare a class or
13112 enumeration: [dcl.dcl]/3. */
13113 if (!saw_declarator
)
13115 if (cp_parser_declares_only_class_p (parser
))
13117 if (!declares_class_or_enum
13118 && decl_specifiers
.type
13119 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13120 /* Ensure an error is issued anyway when finish_decltype_type,
13121 called via cp_parser_decl_specifier_seq, returns a class or
13122 an enumeration (c++/51786). */
13123 decl_specifiers
.type
= NULL_TREE
;
13124 shadow_tag (&decl_specifiers
);
13126 /* Perform any deferred access checks. */
13127 perform_deferred_access_checks (tf_warning_or_error
);
13130 /* Consume the `;'. */
13132 if (!maybe_range_for_decl
)
13133 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13134 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13136 if (init_loc
!= UNKNOWN_LOCATION
)
13137 error_at (init_loc
, "initializer in range-based %<for%> loop");
13138 if (comma_loc
!= UNKNOWN_LOCATION
)
13139 error_at (comma_loc
,
13140 "multiple declarations in range-based %<for%> loop");
13144 pop_deferring_access_checks ();
13147 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13148 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13152 cp_parser_decomposition_declaration (cp_parser
*parser
,
13153 cp_decl_specifier_seq
*decl_specifiers
,
13154 tree
*maybe_range_for_decl
,
13155 location_t
*init_loc
)
13157 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13158 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13159 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13161 /* Parse the identifier-list. */
13162 auto_vec
<cp_expr
, 10> v
;
13163 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13166 cp_expr e
= cp_parser_identifier (parser
);
13167 if (e
.get_value () == error_mark_node
)
13170 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13172 cp_lexer_consume_token (parser
->lexer
);
13175 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13176 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13178 end_loc
= UNKNOWN_LOCATION
;
13179 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13182 cp_lexer_consume_token (parser
->lexer
);
13185 cp_parser_skip_to_end_of_statement (parser
);
13186 return error_mark_node
;
13190 if (cxx_dialect
< cxx17
)
13191 pedwarn (loc
, 0, "structured bindings only available with "
13192 "-std=c++17 or -std=gnu++17");
13195 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13196 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13197 declarator
->id_loc
= loc
;
13198 if (ref_qual
!= REF_QUAL_NONE
)
13199 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13200 ref_qual
== REF_QUAL_RVALUE
,
13202 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13203 NULL_TREE
, decl_specifiers
->attributes
,
13205 tree orig_decl
= decl
;
13209 cp_decl_specifier_seq decl_specs
;
13210 clear_decl_specs (&decl_specs
);
13211 decl_specs
.type
= make_auto ();
13213 FOR_EACH_VEC_ELT (v
, i
, e
)
13216 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13218 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13219 declarator
->id_loc
= e
.get_location ();
13220 tree elt_pushed_scope
;
13221 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13222 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13223 if (decl2
== error_mark_node
)
13224 decl
= error_mark_node
;
13225 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13227 /* Ensure we've diagnosed redeclaration if we aren't creating
13229 gcc_assert (errorcount
);
13230 decl
= error_mark_node
;
13234 if (elt_pushed_scope
)
13235 pop_scope (elt_pushed_scope
);
13240 error_at (loc
, "empty structured binding declaration");
13241 decl
= error_mark_node
;
13244 if (maybe_range_for_decl
== NULL
13245 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13247 bool non_constant_p
= false, is_direct_init
= false;
13248 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13249 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13251 if (initializer
== NULL_TREE
13252 || (TREE_CODE (initializer
) == TREE_LIST
13253 && TREE_CHAIN (initializer
))
13255 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13256 && CONSTRUCTOR_NELTS (initializer
) != 1))
13258 error_at (loc
, "invalid initializer for structured binding "
13260 initializer
= error_mark_node
;
13263 if (decl
!= error_mark_node
)
13265 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13266 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13267 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13268 cp_finish_decomp (decl
, prev
, v
.length ());
13271 else if (decl
!= error_mark_node
)
13273 *maybe_range_for_decl
= prev
;
13274 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13275 the underlying DECL. */
13276 cp_finish_decomp (decl
, prev
, v
.length ());
13280 pop_scope (pushed_scope
);
13282 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13284 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13285 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13291 /* Parse a decl-specifier-seq.
13293 decl-specifier-seq:
13294 decl-specifier-seq [opt] decl-specifier
13295 decl-specifier attribute-specifier-seq [opt] (C++11)
13298 storage-class-specifier
13309 Concepts Extension:
13314 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13316 The parser flags FLAGS is used to control type-specifier parsing.
13318 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13321 1: one of the decl-specifiers is an elaborated-type-specifier
13322 (i.e., a type declaration)
13323 2: one of the decl-specifiers is an enum-specifier or a
13324 class-specifier (i.e., a type definition)
13329 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13330 cp_parser_flags flags
,
13331 cp_decl_specifier_seq
*decl_specs
,
13332 int* declares_class_or_enum
)
13334 bool constructor_possible_p
= !parser
->in_declarator_p
;
13335 bool found_decl_spec
= false;
13336 cp_token
*start_token
= NULL
;
13339 /* Clear DECL_SPECS. */
13340 clear_decl_specs (decl_specs
);
13342 /* Assume no class or enumeration type is declared. */
13343 *declares_class_or_enum
= 0;
13345 /* Keep reading specifiers until there are no more to read. */
13348 bool constructor_p
;
13352 /* Peek at the next token. */
13353 token
= cp_lexer_peek_token (parser
->lexer
);
13355 /* Save the first token of the decl spec list for error
13358 start_token
= token
;
13359 /* Handle attributes. */
13360 if (cp_next_tokens_can_be_attribute_p (parser
))
13362 /* Parse the attributes. */
13363 tree attrs
= cp_parser_attributes_opt (parser
);
13365 /* In a sequence of declaration specifiers, c++11 attributes
13366 appertain to the type that precede them. In that case
13369 The attribute-specifier-seq affects the type only for
13370 the declaration it appears in, not other declarations
13371 involving the same type.
13373 But for now let's force the user to position the
13374 attribute either at the beginning of the declaration or
13375 after the declarator-id, which would clearly mean that it
13376 applies to the declarator. */
13377 if (cxx11_attribute_p (attrs
))
13379 if (!found_decl_spec
)
13380 /* The c++11 attribute is at the beginning of the
13381 declaration. It appertains to the entity being
13385 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13387 /* This is an attribute following a
13388 class-specifier. */
13389 if (decl_specs
->type_definition_p
)
13390 warn_misplaced_attr_for_class_type (token
->location
,
13396 decl_specs
->std_attributes
13397 = chainon (decl_specs
->std_attributes
,
13399 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13400 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13406 decl_specs
->attributes
13407 = chainon (decl_specs
->attributes
,
13409 if (decl_specs
->locations
[ds_attribute
] == 0)
13410 decl_specs
->locations
[ds_attribute
] = token
->location
;
13413 /* Assume we will find a decl-specifier keyword. */
13414 found_decl_spec
= true;
13415 /* If the next token is an appropriate keyword, we can simply
13416 add it to the list. */
13417 switch (token
->keyword
)
13423 if (!at_class_scope_p ())
13425 gcc_rich_location
richloc (token
->location
);
13426 richloc
.add_fixit_remove ();
13427 error_at (&richloc
, "%<friend%> used outside of class");
13428 cp_lexer_purge_token (parser
->lexer
);
13433 /* Consume the token. */
13434 cp_lexer_consume_token (parser
->lexer
);
13438 case RID_CONSTEXPR
:
13440 cp_lexer_consume_token (parser
->lexer
);
13445 cp_lexer_consume_token (parser
->lexer
);
13448 /* function-specifier:
13455 cp_parser_function_specifier_opt (parser
, decl_specs
);
13462 /* Consume the token. */
13463 cp_lexer_consume_token (parser
->lexer
);
13464 /* A constructor declarator cannot appear in a typedef. */
13465 constructor_possible_p
= false;
13466 /* The "typedef" keyword can only occur in a declaration; we
13467 may as well commit at this point. */
13468 cp_parser_commit_to_tentative_parse (parser
);
13470 if (decl_specs
->storage_class
!= sc_none
)
13471 decl_specs
->conflicting_specifiers_p
= true;
13474 /* storage-class-specifier:
13484 if (cxx_dialect
== cxx98
)
13486 /* Consume the token. */
13487 cp_lexer_consume_token (parser
->lexer
);
13489 /* Complain about `auto' as a storage specifier, if
13490 we're complaining about C++0x compatibility. */
13491 gcc_rich_location
richloc (token
->location
);
13492 richloc
.add_fixit_remove ();
13493 warning_at (&richloc
, OPT_Wc__11_compat
,
13494 "%<auto%> changes meaning in C++11; "
13495 "please remove it");
13497 /* Set the storage class anyway. */
13498 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13502 /* C++0x auto type-specifier. */
13503 found_decl_spec
= false;
13510 /* Consume the token. */
13511 cp_lexer_consume_token (parser
->lexer
);
13512 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13516 /* Consume the token. */
13518 cp_lexer_consume_token (parser
->lexer
);
13522 /* We did not yet find a decl-specifier yet. */
13523 found_decl_spec
= false;
13527 if (found_decl_spec
13528 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13529 && token
->keyword
!= RID_CONSTEXPR
)
13530 error ("decl-specifier invalid in condition");
13532 if (found_decl_spec
13533 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13534 && token
->keyword
!= RID_MUTABLE
13535 && token
->keyword
!= RID_CONSTEXPR
)
13536 error_at (token
->location
, "%qD invalid in lambda",
13537 ridpointers
[token
->keyword
]);
13540 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13542 /* Constructors are a special case. The `S' in `S()' is not a
13543 decl-specifier; it is the beginning of the declarator. */
13545 = (!found_decl_spec
13546 && constructor_possible_p
13547 && (cp_parser_constructor_declarator_p
13548 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13550 /* If we don't have a DECL_SPEC yet, then we must be looking at
13551 a type-specifier. */
13552 if (!found_decl_spec
&& !constructor_p
)
13554 int decl_spec_declares_class_or_enum
;
13555 bool is_cv_qualifier
;
13559 = cp_parser_type_specifier (parser
, flags
,
13561 /*is_declaration=*/true,
13562 &decl_spec_declares_class_or_enum
,
13564 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13566 /* If this type-specifier referenced a user-defined type
13567 (a typedef, class-name, etc.), then we can't allow any
13568 more such type-specifiers henceforth.
13572 The longest sequence of decl-specifiers that could
13573 possibly be a type name is taken as the
13574 decl-specifier-seq of a declaration. The sequence shall
13575 be self-consistent as described below.
13579 As a general rule, at most one type-specifier is allowed
13580 in the complete decl-specifier-seq of a declaration. The
13581 only exceptions are the following:
13583 -- const or volatile can be combined with any other
13586 -- signed or unsigned can be combined with char, long,
13594 void g (const int Pc);
13596 Here, Pc is *not* part of the decl-specifier seq; it's
13597 the declarator. Therefore, once we see a type-specifier
13598 (other than a cv-qualifier), we forbid any additional
13599 user-defined types. We *do* still allow things like `int
13600 int' to be considered a decl-specifier-seq, and issue the
13601 error message later. */
13602 if (type_spec
&& !is_cv_qualifier
)
13603 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13604 /* A constructor declarator cannot follow a type-specifier. */
13607 constructor_possible_p
= false;
13608 found_decl_spec
= true;
13609 if (!is_cv_qualifier
)
13610 decl_specs
->any_type_specifiers_p
= true;
13614 /* If we still do not have a DECL_SPEC, then there are no more
13615 decl-specifiers. */
13616 if (!found_decl_spec
)
13619 decl_specs
->any_specifiers_p
= true;
13620 /* After we see one decl-specifier, further decl-specifiers are
13621 always optional. */
13622 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13625 /* Don't allow a friend specifier with a class definition. */
13626 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13627 && (*declares_class_or_enum
& 2))
13628 error_at (decl_specs
->locations
[ds_friend
],
13629 "class definition may not be declared a friend");
13632 /* Parse an (optional) storage-class-specifier.
13634 storage-class-specifier:
13643 storage-class-specifier:
13646 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13649 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13651 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13654 if (cxx_dialect
!= cxx98
)
13656 /* Fall through for C++98. */
13657 gcc_fallthrough ();
13664 /* Consume the token. */
13665 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13672 /* Parse an (optional) function-specifier.
13674 function-specifier:
13679 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13680 Updates DECL_SPECS, if it is non-NULL. */
13683 cp_parser_function_specifier_opt (cp_parser
* parser
,
13684 cp_decl_specifier_seq
*decl_specs
)
13686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13687 switch (token
->keyword
)
13690 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13694 /* 14.5.2.3 [temp.mem]
13696 A member function template shall not be virtual. */
13697 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13698 && current_class_type
)
13699 error_at (token
->location
, "templates may not be %<virtual%>");
13701 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13705 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13712 /* Consume the token. */
13713 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13716 /* Parse a linkage-specification.
13718 linkage-specification:
13719 extern string-literal { declaration-seq [opt] }
13720 extern string-literal declaration */
13723 cp_parser_linkage_specification (cp_parser
* parser
)
13727 /* Look for the `extern' keyword. */
13728 cp_token
*extern_token
13729 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13731 /* Look for the string-literal. */
13732 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13733 linkage
= cp_parser_string_literal (parser
, false, false);
13735 /* Transform the literal into an identifier. If the literal is a
13736 wide-character string, or contains embedded NULs, then we can't
13737 handle it as the user wants. */
13738 if (strlen (TREE_STRING_POINTER (linkage
))
13739 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13741 cp_parser_error (parser
, "invalid linkage-specification");
13742 /* Assume C++ linkage. */
13743 linkage
= lang_name_cplusplus
;
13746 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13748 /* We're now using the new linkage. */
13749 push_lang_context (linkage
);
13751 /* Preserve the location of the the innermost linkage specification,
13752 tracking the locations of nested specifications via a local. */
13753 location_t saved_location
13754 = parser
->innermost_linkage_specification_location
;
13755 /* Construct a location ranging from the start of the "extern" to
13756 the end of the string-literal, with the caret at the start, e.g.:
13760 parser
->innermost_linkage_specification_location
13761 = make_location (extern_token
->location
,
13762 extern_token
->location
,
13763 get_finish (string_token
->location
));
13765 /* If the next token is a `{', then we're using the first
13767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13769 cp_ensure_no_omp_declare_simd (parser
);
13770 cp_ensure_no_oacc_routine (parser
);
13772 /* Consume the `{' token. */
13773 matching_braces braces
;
13774 braces
.consume_open (parser
)->location
;
13775 /* Parse the declarations. */
13776 cp_parser_declaration_seq_opt (parser
);
13777 /* Look for the closing `}'. */
13778 braces
.require_close (parser
);
13780 /* Otherwise, there's just one declaration. */
13783 bool saved_in_unbraced_linkage_specification_p
;
13785 saved_in_unbraced_linkage_specification_p
13786 = parser
->in_unbraced_linkage_specification_p
;
13787 parser
->in_unbraced_linkage_specification_p
= true;
13788 cp_parser_declaration (parser
);
13789 parser
->in_unbraced_linkage_specification_p
13790 = saved_in_unbraced_linkage_specification_p
;
13793 /* We're done with the linkage-specification. */
13794 pop_lang_context ();
13796 /* Restore location of parent linkage specification, if any. */
13797 parser
->innermost_linkage_specification_location
= saved_location
;
13800 /* Parse a static_assert-declaration.
13802 static_assert-declaration:
13803 static_assert ( constant-expression , string-literal ) ;
13804 static_assert ( constant-expression ) ; (C++17)
13806 If MEMBER_P, this static_assert is a class member. */
13809 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13812 location_t token_loc
;
13816 /* Peek at the `static_assert' token so we can keep track of exactly
13817 where the static assertion started. */
13818 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13820 /* Look for the `static_assert' keyword. */
13821 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13825 /* We know we are in a static assertion; commit to any tentative
13827 if (cp_parser_parsing_tentatively (parser
))
13828 cp_parser_commit_to_tentative_parse (parser
);
13830 /* Parse the `(' starting the static assertion condition. */
13831 matching_parens parens
;
13832 parens
.require_open (parser
);
13834 /* Parse the constant-expression. Allow a non-constant expression
13835 here in order to give better diagnostics in finish_static_assert. */
13837 cp_parser_constant_expression (parser
,
13838 /*allow_non_constant_p=*/true,
13839 /*non_constant_p=*/&dummy
);
13841 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13843 if (cxx_dialect
< cxx17
)
13844 pedwarn (input_location
, OPT_Wpedantic
,
13845 "static_assert without a message "
13846 "only available with -std=c++17 or -std=gnu++17");
13848 cp_lexer_consume_token (parser
->lexer
);
13849 message
= build_string (1, "");
13850 TREE_TYPE (message
) = char_array_type_node
;
13851 fix_string_type (message
);
13855 /* Parse the separating `,'. */
13856 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13858 /* Parse the string-literal message. */
13859 message
= cp_parser_string_literal (parser
,
13860 /*translate=*/false,
13863 /* A `)' completes the static assertion. */
13864 if (!parens
.require_close (parser
))
13865 cp_parser_skip_to_closing_parenthesis (parser
,
13866 /*recovering=*/true,
13867 /*or_comma=*/false,
13868 /*consume_paren=*/true);
13871 /* A semicolon terminates the declaration. */
13872 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13874 /* Get the location for the static assertion. Use that of the
13875 condition if available, otherwise, use that of the "static_assert"
13877 location_t assert_loc
= condition
.get_location ();
13878 if (assert_loc
== UNKNOWN_LOCATION
)
13879 assert_loc
= token_loc
;
13881 /* Complete the static assertion, which may mean either processing
13882 the static assert now or saving it for template instantiation. */
13883 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13886 /* Parse the expression in decltype ( expression ). */
13889 cp_parser_decltype_expr (cp_parser
*parser
,
13890 bool &id_expression_or_member_access_p
)
13892 cp_token
*id_expr_start_token
;
13895 /* Since we're going to preserve any side-effects from this parse, set up a
13896 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13897 in the expression. */
13898 tentative_firewall
firewall (parser
);
13900 /* First, try parsing an id-expression. */
13901 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13902 cp_parser_parse_tentatively (parser
);
13903 expr
= cp_parser_id_expression (parser
,
13904 /*template_keyword_p=*/false,
13905 /*check_dependency_p=*/true,
13906 /*template_p=*/NULL
,
13907 /*declarator_p=*/false,
13908 /*optional_p=*/false);
13910 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13912 bool non_integral_constant_expression_p
= false;
13913 tree id_expression
= expr
;
13915 const char *error_msg
;
13917 if (identifier_p (expr
))
13918 /* Lookup the name we got back from the id-expression. */
13919 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13920 id_expr_start_token
->location
);
13923 && expr
!= error_mark_node
13924 && TREE_CODE (expr
) != TYPE_DECL
13925 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13926 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13927 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13929 /* Complete lookup of the id-expression. */
13930 expr
= (finish_id_expression
13931 (id_expression
, expr
, parser
->scope
, &idk
,
13932 /*integral_constant_expression_p=*/false,
13933 /*allow_non_integral_constant_expression_p=*/true,
13934 &non_integral_constant_expression_p
,
13935 /*template_p=*/false,
13937 /*address_p=*/false,
13938 /*template_arg_p=*/false,
13940 id_expr_start_token
->location
));
13942 if (expr
== error_mark_node
)
13943 /* We found an id-expression, but it was something that we
13944 should not have found. This is an error, not something
13945 we can recover from, so note that we found an
13946 id-expression and we'll recover as gracefully as
13948 id_expression_or_member_access_p
= true;
13952 && expr
!= error_mark_node
13953 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13954 /* We have an id-expression. */
13955 id_expression_or_member_access_p
= true;
13958 if (!id_expression_or_member_access_p
)
13960 /* Abort the id-expression parse. */
13961 cp_parser_abort_tentative_parse (parser
);
13963 /* Parsing tentatively, again. */
13964 cp_parser_parse_tentatively (parser
);
13966 /* Parse a class member access. */
13967 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13968 /*cast_p=*/false, /*decltype*/true,
13969 /*member_access_only_p=*/true, NULL
);
13972 && expr
!= error_mark_node
13973 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13974 /* We have an id-expression. */
13975 id_expression_or_member_access_p
= true;
13978 if (id_expression_or_member_access_p
)
13979 /* We have parsed the complete id-expression or member access. */
13980 cp_parser_parse_definitely (parser
);
13983 /* Abort our attempt to parse an id-expression or member access
13985 cp_parser_abort_tentative_parse (parser
);
13987 /* Parse a full expression. */
13988 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13989 /*decltype_p=*/true);
13995 /* Parse a `decltype' type. Returns the type.
13997 simple-type-specifier:
13998 decltype ( expression )
14000 decltype ( auto ) */
14003 cp_parser_decltype (cp_parser
*parser
)
14006 bool id_expression_or_member_access_p
= false;
14007 const char *saved_message
;
14008 bool saved_integral_constant_expression_p
;
14009 bool saved_non_integral_constant_expression_p
;
14010 bool saved_greater_than_is_operator_p
;
14011 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14013 if (start_token
->type
== CPP_DECLTYPE
)
14015 /* Already parsed. */
14016 cp_lexer_consume_token (parser
->lexer
);
14017 return saved_checks_value (start_token
->u
.tree_check_value
);
14020 /* Look for the `decltype' token. */
14021 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14022 return error_mark_node
;
14024 /* Parse the opening `('. */
14025 matching_parens parens
;
14026 if (!parens
.require_open (parser
))
14027 return error_mark_node
;
14029 /* decltype (auto) */
14030 if (cxx_dialect
>= cxx14
14031 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14033 cp_lexer_consume_token (parser
->lexer
);
14034 if (!parens
.require_close (parser
))
14035 return error_mark_node
;
14036 expr
= make_decltype_auto ();
14037 AUTO_IS_DECLTYPE (expr
) = true;
14041 /* Types cannot be defined in a `decltype' expression. Save away the
14043 saved_message
= parser
->type_definition_forbidden_message
;
14045 /* And create the new one. */
14046 parser
->type_definition_forbidden_message
14047 = G_("types may not be defined in %<decltype%> expressions");
14049 /* The restrictions on constant-expressions do not apply inside
14050 decltype expressions. */
14051 saved_integral_constant_expression_p
14052 = parser
->integral_constant_expression_p
;
14053 saved_non_integral_constant_expression_p
14054 = parser
->non_integral_constant_expression_p
;
14055 parser
->integral_constant_expression_p
= false;
14057 /* Within a parenthesized expression, a `>' token is always
14058 the greater-than operator. */
14059 saved_greater_than_is_operator_p
14060 = parser
->greater_than_is_operator_p
;
14061 parser
->greater_than_is_operator_p
= true;
14063 /* Do not actually evaluate the expression. */
14064 ++cp_unevaluated_operand
;
14066 /* Do not warn about problems with the expression. */
14067 ++c_inhibit_evaluation_warnings
;
14069 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14071 /* Go back to evaluating expressions. */
14072 --cp_unevaluated_operand
;
14073 --c_inhibit_evaluation_warnings
;
14075 /* The `>' token might be the end of a template-id or
14076 template-parameter-list now. */
14077 parser
->greater_than_is_operator_p
14078 = saved_greater_than_is_operator_p
;
14080 /* Restore the old message and the integral constant expression
14082 parser
->type_definition_forbidden_message
= saved_message
;
14083 parser
->integral_constant_expression_p
14084 = saved_integral_constant_expression_p
;
14085 parser
->non_integral_constant_expression_p
14086 = saved_non_integral_constant_expression_p
;
14088 /* Parse to the closing `)'. */
14089 if (!parens
.require_close (parser
))
14091 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14092 /*consume_paren=*/true);
14093 return error_mark_node
;
14096 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14097 tf_warning_or_error
);
14100 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14102 start_token
->type
= CPP_DECLTYPE
;
14103 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14104 start_token
->u
.tree_check_value
->value
= expr
;
14105 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14106 start_token
->keyword
= RID_MAX
;
14107 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14112 /* Special member functions [gram.special] */
14114 /* Parse a conversion-function-id.
14116 conversion-function-id:
14117 operator conversion-type-id
14119 Returns an IDENTIFIER_NODE representing the operator. */
14122 cp_parser_conversion_function_id (cp_parser
* parser
)
14126 tree saved_qualifying_scope
;
14127 tree saved_object_scope
;
14128 tree pushed_scope
= NULL_TREE
;
14130 /* Look for the `operator' token. */
14131 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14132 return error_mark_node
;
14133 /* When we parse the conversion-type-id, the current scope will be
14134 reset. However, we need that information in able to look up the
14135 conversion function later, so we save it here. */
14136 saved_scope
= parser
->scope
;
14137 saved_qualifying_scope
= parser
->qualifying_scope
;
14138 saved_object_scope
= parser
->object_scope
;
14139 /* We must enter the scope of the class so that the names of
14140 entities declared within the class are available in the
14141 conversion-type-id. For example, consider:
14148 S::operator I() { ... }
14150 In order to see that `I' is a type-name in the definition, we
14151 must be in the scope of `S'. */
14153 pushed_scope
= push_scope (saved_scope
);
14154 /* Parse the conversion-type-id. */
14155 type
= cp_parser_conversion_type_id (parser
);
14156 /* Leave the scope of the class, if any. */
14158 pop_scope (pushed_scope
);
14159 /* Restore the saved scope. */
14160 parser
->scope
= saved_scope
;
14161 parser
->qualifying_scope
= saved_qualifying_scope
;
14162 parser
->object_scope
= saved_object_scope
;
14163 /* If the TYPE is invalid, indicate failure. */
14164 if (type
== error_mark_node
)
14165 return error_mark_node
;
14166 return make_conv_op_name (type
);
14169 /* Parse a conversion-type-id:
14171 conversion-type-id:
14172 type-specifier-seq conversion-declarator [opt]
14174 Returns the TYPE specified. */
14177 cp_parser_conversion_type_id (cp_parser
* parser
)
14180 cp_decl_specifier_seq type_specifiers
;
14181 cp_declarator
*declarator
;
14182 tree type_specified
;
14183 const char *saved_message
;
14185 /* Parse the attributes. */
14186 attributes
= cp_parser_attributes_opt (parser
);
14188 saved_message
= parser
->type_definition_forbidden_message
;
14189 parser
->type_definition_forbidden_message
14190 = G_("types may not be defined in a conversion-type-id");
14192 /* Parse the type-specifiers. */
14193 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14194 /*is_trailing_return=*/false,
14197 parser
->type_definition_forbidden_message
= saved_message
;
14199 /* If that didn't work, stop. */
14200 if (type_specifiers
.type
== error_mark_node
)
14201 return error_mark_node
;
14202 /* Parse the conversion-declarator. */
14203 declarator
= cp_parser_conversion_declarator_opt (parser
);
14205 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14206 /*initialized=*/0, &attributes
);
14208 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14210 /* Don't give this error when parsing tentatively. This happens to
14211 work because we always parse this definitively once. */
14212 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14213 && type_uses_auto (type_specified
))
14215 if (cxx_dialect
< cxx14
)
14217 error ("invalid use of %<auto%> in conversion operator");
14218 return error_mark_node
;
14220 else if (template_parm_scope_p ())
14221 warning (0, "use of %<auto%> in member template "
14222 "conversion operator can never be deduced");
14225 return type_specified
;
14228 /* Parse an (optional) conversion-declarator.
14230 conversion-declarator:
14231 ptr-operator conversion-declarator [opt]
14235 static cp_declarator
*
14236 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14238 enum tree_code code
;
14239 tree class_type
, std_attributes
= NULL_TREE
;
14240 cp_cv_quals cv_quals
;
14242 /* We don't know if there's a ptr-operator next, or not. */
14243 cp_parser_parse_tentatively (parser
);
14244 /* Try the ptr-operator. */
14245 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14247 /* If it worked, look for more conversion-declarators. */
14248 if (cp_parser_parse_definitely (parser
))
14250 cp_declarator
*declarator
;
14252 /* Parse another optional declarator. */
14253 declarator
= cp_parser_conversion_declarator_opt (parser
);
14255 declarator
= cp_parser_make_indirect_declarator
14256 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14264 /* Parse an (optional) ctor-initializer.
14267 : mem-initializer-list */
14270 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14272 /* If the next token is not a `:', then there is no
14273 ctor-initializer. */
14274 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14276 /* Do default initialization of any bases and members. */
14277 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14278 finish_mem_initializers (NULL_TREE
);
14282 /* Consume the `:' token. */
14283 cp_lexer_consume_token (parser
->lexer
);
14284 /* And the mem-initializer-list. */
14285 cp_parser_mem_initializer_list (parser
);
14288 /* Parse a mem-initializer-list.
14290 mem-initializer-list:
14291 mem-initializer ... [opt]
14292 mem-initializer ... [opt] , mem-initializer-list */
14295 cp_parser_mem_initializer_list (cp_parser
* parser
)
14297 tree mem_initializer_list
= NULL_TREE
;
14298 tree target_ctor
= error_mark_node
;
14299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14301 /* Let the semantic analysis code know that we are starting the
14302 mem-initializer-list. */
14303 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14304 error_at (token
->location
,
14305 "only constructors take member initializers");
14307 /* Loop through the list. */
14310 tree mem_initializer
;
14312 token
= cp_lexer_peek_token (parser
->lexer
);
14313 /* Parse the mem-initializer. */
14314 mem_initializer
= cp_parser_mem_initializer (parser
);
14315 /* If the next token is a `...', we're expanding member initializers. */
14316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14318 /* Consume the `...'. */
14319 cp_lexer_consume_token (parser
->lexer
);
14321 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14322 can be expanded but members cannot. */
14323 if (mem_initializer
!= error_mark_node
14324 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14326 error_at (token
->location
,
14327 "cannot expand initializer for member %qD",
14328 TREE_PURPOSE (mem_initializer
));
14329 mem_initializer
= error_mark_node
;
14332 /* Construct the pack expansion type. */
14333 if (mem_initializer
!= error_mark_node
)
14334 mem_initializer
= make_pack_expansion (mem_initializer
);
14336 if (target_ctor
!= error_mark_node
14337 && mem_initializer
!= error_mark_node
)
14339 error ("mem-initializer for %qD follows constructor delegation",
14340 TREE_PURPOSE (mem_initializer
));
14341 mem_initializer
= error_mark_node
;
14343 /* Look for a target constructor. */
14344 if (mem_initializer
!= error_mark_node
14345 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14346 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14348 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14349 if (mem_initializer_list
)
14351 error ("constructor delegation follows mem-initializer for %qD",
14352 TREE_PURPOSE (mem_initializer_list
));
14353 mem_initializer
= error_mark_node
;
14355 target_ctor
= mem_initializer
;
14357 /* Add it to the list, unless it was erroneous. */
14358 if (mem_initializer
!= error_mark_node
)
14360 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14361 mem_initializer_list
= mem_initializer
;
14363 /* If the next token is not a `,', we're done. */
14364 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14366 /* Consume the `,' token. */
14367 cp_lexer_consume_token (parser
->lexer
);
14370 /* Perform semantic analysis. */
14371 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14372 finish_mem_initializers (mem_initializer_list
);
14375 /* Parse a mem-initializer.
14378 mem-initializer-id ( expression-list [opt] )
14379 mem-initializer-id braced-init-list
14384 ( expression-list [opt] )
14386 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14387 class) or FIELD_DECL (for a non-static data member) to initialize;
14388 the TREE_VALUE is the expression-list. An empty initialization
14389 list is represented by void_list_node. */
14392 cp_parser_mem_initializer (cp_parser
* parser
)
14394 tree mem_initializer_id
;
14395 tree expression_list
;
14397 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14399 /* Find out what is being initialized. */
14400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14402 permerror (token
->location
,
14403 "anachronistic old-style base class initializer");
14404 mem_initializer_id
= NULL_TREE
;
14408 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14409 if (mem_initializer_id
== error_mark_node
)
14410 return mem_initializer_id
;
14412 member
= expand_member_init (mem_initializer_id
);
14413 if (member
&& !DECL_P (member
))
14414 in_base_initializer
= 1;
14416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14418 bool expr_non_constant_p
;
14419 cp_lexer_set_source_position (parser
->lexer
);
14420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14421 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14422 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14423 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14427 vec
<tree
, va_gc
> *vec
;
14428 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14430 /*allow_expansion_p=*/true,
14431 /*non_constant_p=*/NULL
);
14433 return error_mark_node
;
14434 expression_list
= build_tree_list_vec (vec
);
14435 release_tree_vector (vec
);
14438 if (expression_list
== error_mark_node
)
14439 return error_mark_node
;
14440 if (!expression_list
)
14441 expression_list
= void_type_node
;
14443 in_base_initializer
= 0;
14445 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14448 /* Parse a mem-initializer-id.
14450 mem-initializer-id:
14451 :: [opt] nested-name-specifier [opt] class-name
14452 decltype-specifier (C++11)
14455 Returns a TYPE indicating the class to be initialized for the first
14456 production (and the second in C++11). Returns an IDENTIFIER_NODE
14457 indicating the data member to be initialized for the last production. */
14460 cp_parser_mem_initializer_id (cp_parser
* parser
)
14462 bool global_scope_p
;
14463 bool nested_name_specifier_p
;
14464 bool template_p
= false;
14467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14469 /* `typename' is not allowed in this context ([temp.res]). */
14470 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14472 error_at (token
->location
,
14473 "keyword %<typename%> not allowed in this context (a qualified "
14474 "member initializer is implicitly a type)");
14475 cp_lexer_consume_token (parser
->lexer
);
14477 /* Look for the optional `::' operator. */
14479 = (cp_parser_global_scope_opt (parser
,
14480 /*current_scope_valid_p=*/false)
14482 /* Look for the optional nested-name-specifier. The simplest way to
14487 The keyword `typename' is not permitted in a base-specifier or
14488 mem-initializer; in these contexts a qualified name that
14489 depends on a template-parameter is implicitly assumed to be a
14492 is to assume that we have seen the `typename' keyword at this
14494 nested_name_specifier_p
14495 = (cp_parser_nested_name_specifier_opt (parser
,
14496 /*typename_keyword_p=*/true,
14497 /*check_dependency_p=*/true,
14499 /*is_declaration=*/true)
14501 if (nested_name_specifier_p
)
14502 template_p
= cp_parser_optional_template_keyword (parser
);
14503 /* If there is a `::' operator or a nested-name-specifier, then we
14504 are definitely looking for a class-name. */
14505 if (global_scope_p
|| nested_name_specifier_p
)
14506 return cp_parser_class_name (parser
,
14507 /*typename_keyword_p=*/true,
14508 /*template_keyword_p=*/template_p
,
14510 /*check_dependency_p=*/true,
14511 /*class_head_p=*/false,
14512 /*is_declaration=*/true);
14513 /* Otherwise, we could also be looking for an ordinary identifier. */
14514 cp_parser_parse_tentatively (parser
);
14515 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14516 /* Try a decltype-specifier. */
14517 id
= cp_parser_decltype (parser
);
14519 /* Otherwise, try a class-name. */
14520 id
= cp_parser_class_name (parser
,
14521 /*typename_keyword_p=*/true,
14522 /*template_keyword_p=*/false,
14524 /*check_dependency_p=*/true,
14525 /*class_head_p=*/false,
14526 /*is_declaration=*/true);
14527 /* If we found one, we're done. */
14528 if (cp_parser_parse_definitely (parser
))
14530 /* Otherwise, look for an ordinary identifier. */
14531 return cp_parser_identifier (parser
);
14534 /* Overloading [gram.over] */
14536 /* Parse an operator-function-id.
14538 operator-function-id:
14541 Returns an IDENTIFIER_NODE for the operator which is a
14542 human-readable spelling of the identifier, e.g., `operator +'. */
14545 cp_parser_operator_function_id (cp_parser
* parser
)
14547 /* Look for the `operator' keyword. */
14548 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14549 return error_mark_node
;
14550 /* And then the name of the operator itself. */
14551 return cp_parser_operator (parser
);
14554 /* Return an identifier node for a user-defined literal operator.
14555 The suffix identifier is chained to the operator name identifier. */
14558 cp_literal_operator_id (const char* name
)
14561 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14562 + strlen (name
) + 10);
14563 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14564 identifier
= get_identifier (buffer
);
14569 /* Parse an operator.
14572 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14573 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14574 || ++ -- , ->* -> () []
14581 Returns an IDENTIFIER_NODE for the operator which is a
14582 human-readable spelling of the identifier, e.g., `operator +'. */
14585 cp_parser_operator (cp_parser
* parser
)
14587 tree id
= NULL_TREE
;
14591 /* Peek at the next token. */
14592 token
= cp_lexer_peek_token (parser
->lexer
);
14594 location_t start_loc
= token
->location
;
14596 /* Figure out which operator we have. */
14597 enum tree_code op
= ERROR_MARK
;
14598 bool assop
= false;
14599 bool consumed
= false;
14600 switch (token
->type
)
14604 /* The keyword should be either `new' or `delete'. */
14605 if (token
->keyword
== RID_NEW
)
14607 else if (token
->keyword
== RID_DELETE
)
14612 /* Consume the `new' or `delete' token. */
14613 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14615 /* Peek at the next token. */
14616 token
= cp_lexer_peek_token (parser
->lexer
);
14617 /* If it's a `[' token then this is the array variant of the
14619 if (token
->type
== CPP_OPEN_SQUARE
)
14621 /* Consume the `[' token. */
14622 cp_lexer_consume_token (parser
->lexer
);
14623 /* Look for the `]' token. */
14624 if (cp_token
*close_token
14625 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14626 end_loc
= close_token
->location
;
14627 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14629 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14647 op
= TRUNC_DIV_EXPR
;
14651 op
= TRUNC_MOD_EXPR
;
14671 op
= TRUTH_NOT_EXPR
;
14704 op
= TRUNC_DIV_EXPR
;
14709 op
= TRUNC_MOD_EXPR
;
14735 case CPP_LSHIFT_EQ
:
14740 case CPP_RSHIFT_EQ
:
14757 case CPP_GREATER_EQ
:
14762 op
= TRUTH_ANDIF_EXPR
;
14766 op
= TRUTH_ORIF_EXPR
;
14769 case CPP_PLUS_PLUS
:
14770 op
= POSTINCREMENT_EXPR
;
14773 case CPP_MINUS_MINUS
:
14774 op
= PREDECREMENT_EXPR
;
14778 op
= COMPOUND_EXPR
;
14781 case CPP_DEREF_STAR
:
14786 op
= COMPONENT_REF
;
14789 case CPP_OPEN_PAREN
:
14791 /* Consume the `('. */
14792 matching_parens parens
;
14793 parens
.consume_open (parser
);
14794 /* Look for the matching `)'. */
14795 parens
.require_close (parser
);
14801 case CPP_OPEN_SQUARE
:
14802 /* Consume the `['. */
14803 cp_lexer_consume_token (parser
->lexer
);
14804 /* Look for the matching `]'. */
14805 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14810 case CPP_UTF8STRING
:
14811 case CPP_UTF8STRING_USERDEF
:
14818 case CPP_STRING_USERDEF
:
14819 case CPP_WSTRING_USERDEF
:
14820 case CPP_STRING16_USERDEF
:
14821 case CPP_STRING32_USERDEF
:
14823 tree str
, string_tree
;
14826 if (cxx_dialect
== cxx98
)
14827 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14829 /* Consume the string. */
14830 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14831 /*wide_ok=*/true, /*lookup_udlit=*/false);
14832 if (str
== error_mark_node
)
14833 return error_mark_node
;
14834 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14836 string_tree
= USERDEF_LITERAL_VALUE (str
);
14837 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14842 /* Look for the suffix identifier. */
14843 token
= cp_lexer_peek_token (parser
->lexer
);
14844 if (token
->type
== CPP_NAME
)
14845 id
= cp_parser_identifier (parser
);
14846 else if (token
->type
== CPP_KEYWORD
)
14848 error ("unexpected keyword;"
14849 " remove space between quotes and suffix identifier");
14850 return error_mark_node
;
14854 error ("expected suffix identifier");
14855 return error_mark_node
;
14858 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14859 (TREE_TYPE (TREE_TYPE (string_tree
))));
14860 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14863 error ("expected empty string after %<operator%> keyword");
14864 return error_mark_node
;
14866 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14869 error ("invalid encoding prefix in literal operator");
14870 return error_mark_node
;
14872 if (id
!= error_mark_node
)
14874 const char *name
= IDENTIFIER_POINTER (id
);
14875 id
= cp_literal_operator_id (name
);
14881 /* Anything else is an error. */
14885 /* If we have selected an identifier, we need to consume the
14887 if (op
!= ERROR_MARK
)
14889 id
= ovl_op_identifier (assop
, op
);
14891 cp_lexer_consume_token (parser
->lexer
);
14893 /* Otherwise, no valid operator name was present. */
14896 cp_parser_error (parser
, "expected operator");
14897 id
= error_mark_node
;
14900 return cp_expr (id
, start_loc
);
14903 /* Parse a template-declaration.
14905 template-declaration:
14906 export [opt] template < template-parameter-list > declaration
14908 If MEMBER_P is TRUE, this template-declaration occurs within a
14911 The grammar rule given by the standard isn't correct. What
14912 is really meant is:
14914 template-declaration:
14915 export [opt] template-parameter-list-seq
14916 decl-specifier-seq [opt] init-declarator [opt] ;
14917 export [opt] template-parameter-list-seq
14918 function-definition
14920 template-parameter-list-seq:
14921 template-parameter-list-seq [opt]
14922 template < template-parameter-list >
14924 Concept Extensions:
14926 template-parameter-list-seq:
14927 template < template-parameter-list > requires-clause [opt]
14930 requires logical-or-expression */
14933 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14935 /* Check for `export'. */
14936 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14938 /* Consume the `export' token. */
14939 cp_lexer_consume_token (parser
->lexer
);
14940 /* Warn that we do not support `export'. */
14941 warning (0, "keyword %<export%> not implemented, and will be ignored");
14944 cp_parser_template_declaration_after_export (parser
, member_p
);
14947 /* Parse a template-parameter-list.
14949 template-parameter-list:
14951 template-parameter-list , template-parameter
14953 Returns a TREE_LIST. Each node represents a template parameter.
14954 The nodes are connected via their TREE_CHAINs. */
14957 cp_parser_template_parameter_list (cp_parser
* parser
)
14959 tree parameter_list
= NULL_TREE
;
14961 begin_template_parm_list ();
14963 /* The loop below parses the template parms. We first need to know
14964 the total number of template parms to be able to compute proper
14965 canonical types of each dependent type. So after the loop, when
14966 we know the total number of template parms,
14967 end_template_parm_list computes the proper canonical types and
14968 fixes up the dependent types accordingly. */
14973 bool is_parameter_pack
;
14974 location_t parm_loc
;
14976 /* Parse the template-parameter. */
14977 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14978 parameter
= cp_parser_template_parameter (parser
,
14980 &is_parameter_pack
);
14981 /* Add it to the list. */
14982 if (parameter
!= error_mark_node
)
14983 parameter_list
= process_template_parm (parameter_list
,
14987 is_parameter_pack
);
14990 tree err_parm
= build_tree_list (parameter
, parameter
);
14991 parameter_list
= chainon (parameter_list
, err_parm
);
14994 /* If the next token is not a `,', we're done. */
14995 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14997 /* Otherwise, consume the `,' token. */
14998 cp_lexer_consume_token (parser
->lexer
);
15001 return end_template_parm_list (parameter_list
);
15004 /* Parse a introduction-list.
15007 introduced-parameter
15008 introduction-list , introduced-parameter
15010 introduced-parameter:
15011 ...[opt] identifier
15013 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15014 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15015 WILDCARD_DECL will also have DECL_NAME set and token location in
15016 DECL_SOURCE_LOCATION. */
15019 cp_parser_introduction_list (cp_parser
*parser
)
15021 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15025 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15027 cp_lexer_consume_token (parser
->lexer
);
15029 /* Build placeholder. */
15030 tree parm
= build_nt (WILDCARD_DECL
);
15031 DECL_SOURCE_LOCATION (parm
)
15032 = cp_lexer_peek_token (parser
->lexer
)->location
;
15033 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15034 WILDCARD_PACK_P (parm
) = is_pack
;
15035 vec_safe_push (introduction_vec
, parm
);
15037 /* If the next token is not a `,', we're done. */
15038 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15040 /* Otherwise, consume the `,' token. */
15041 cp_lexer_consume_token (parser
->lexer
);
15044 /* Convert the vec into a TREE_VEC. */
15045 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15048 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15049 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15051 release_tree_vector (introduction_vec
);
15052 return introduction_list
;
15055 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15056 is an abstract declarator. */
15058 static inline cp_declarator
*
15059 get_id_declarator (cp_declarator
*declarator
)
15061 cp_declarator
*d
= declarator
;
15062 while (d
&& d
->kind
!= cdk_id
)
15067 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15068 is an abstract declarator. */
15071 get_unqualified_id (cp_declarator
*declarator
)
15073 declarator
= get_id_declarator (declarator
);
15075 return declarator
->u
.id
.unqualified_name
;
15080 /* Returns true if DECL represents a constrained-parameter. */
15083 is_constrained_parameter (tree decl
)
15086 && TREE_CODE (decl
) == TYPE_DECL
15087 && CONSTRAINED_PARM_CONCEPT (decl
)
15088 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15091 /* Returns true if PARM declares a constrained-parameter. */
15094 is_constrained_parameter (cp_parameter_declarator
*parm
)
15096 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15099 /* Check that the type parameter is only a declarator-id, and that its
15100 type is not cv-qualified. */
15103 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15104 cp_parameter_declarator
*parm
)
15106 if (!parm
->declarator
)
15109 if (parm
->declarator
->kind
!= cdk_id
)
15111 cp_parser_error (parser
, "invalid constrained type parameter");
15115 /* Don't allow cv-qualified type parameters. */
15116 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15117 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15119 cp_parser_error (parser
, "cv-qualified type parameter");
15126 /* Finish parsing/processing a template type parameter and checking
15127 various restrictions. */
15130 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15132 cp_parameter_declarator
* parmdecl
)
15134 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15135 return finish_template_type_parm (class_type_node
, id
);
15137 return error_mark_node
;
15141 finish_constrained_template_template_parm (tree proto
, tree id
)
15143 /* FIXME: This should probably be copied, and we may need to adjust
15144 the template parameter depths. */
15145 tree saved_parms
= current_template_parms
;
15146 begin_template_parm_list ();
15147 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15148 end_template_parm_list ();
15150 tree parm
= finish_template_template_parm (class_type_node
, id
);
15151 current_template_parms
= saved_parms
;
15156 /* Finish parsing/processing a template template parameter by borrowing
15157 the template parameter list from the prototype parameter. */
15160 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15163 cp_parameter_declarator
*parmdecl
)
15165 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15166 return error_mark_node
;
15167 return finish_constrained_template_template_parm (proto
, id
);
15170 /* Create a new non-type template parameter from the given PARM
15174 constrained_non_type_template_parm (bool *is_non_type
,
15175 cp_parameter_declarator
*parm
)
15177 *is_non_type
= true;
15178 cp_declarator
*decl
= parm
->declarator
;
15179 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15180 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15181 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15184 /* Build a constrained template parameter based on the PARMDECL
15185 declarator. The type of PARMDECL is the constrained type, which
15186 refers to the prototype template parameter that ultimately
15187 specifies the type of the declared parameter. */
15190 finish_constrained_parameter (cp_parser
*parser
,
15191 cp_parameter_declarator
*parmdecl
,
15193 bool *is_parameter_pack
)
15195 tree decl
= parmdecl
->decl_specifiers
.type
;
15196 tree id
= get_unqualified_id (parmdecl
->declarator
);
15197 tree def
= parmdecl
->default_argument
;
15198 tree proto
= DECL_INITIAL (decl
);
15200 /* A template parameter constrained by a variadic concept shall also
15201 be declared as a template parameter pack. */
15202 bool is_variadic
= template_parameter_pack_p (proto
);
15203 if (is_variadic
&& !*is_parameter_pack
)
15204 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15206 /* Build the parameter. Return an error if the declarator was invalid. */
15208 if (TREE_CODE (proto
) == TYPE_DECL
)
15209 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15210 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15211 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15214 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15215 if (parm
== error_mark_node
)
15216 return error_mark_node
;
15218 /* Finish the parameter decl and create a node attaching the
15219 default argument and constraint. */
15220 parm
= build_tree_list (def
, parm
);
15221 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15226 /* Returns true if the parsed type actually represents the declaration
15227 of a type template-parameter. */
15230 declares_constrained_type_template_parameter (tree type
)
15232 return (is_constrained_parameter (type
)
15233 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15237 /* Returns true if the parsed type actually represents the declaration of
15238 a template template-parameter. */
15241 declares_constrained_template_template_parameter (tree type
)
15243 return (is_constrained_parameter (type
)
15244 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15247 /* Parse a default argument for a type template-parameter.
15248 Note that diagnostics are handled in cp_parser_template_parameter. */
15251 cp_parser_default_type_template_argument (cp_parser
*parser
)
15253 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15255 /* Consume the `=' token. */
15256 cp_lexer_consume_token (parser
->lexer
);
15258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15260 /* Parse the default-argument. */
15261 push_deferring_access_checks (dk_no_deferred
);
15262 tree default_argument
= cp_parser_type_id (parser
);
15263 pop_deferring_access_checks ();
15265 if (flag_concepts
&& type_uses_auto (default_argument
))
15267 error_at (token
->location
,
15268 "invalid use of %<auto%> in default template argument");
15269 return error_mark_node
;
15272 return default_argument
;
15275 /* Parse a default argument for a template template-parameter. */
15278 cp_parser_default_template_template_argument (cp_parser
*parser
)
15280 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15284 /* Consume the `='. */
15285 cp_lexer_consume_token (parser
->lexer
);
15286 /* Parse the id-expression. */
15287 push_deferring_access_checks (dk_no_deferred
);
15288 /* save token before parsing the id-expression, for error
15290 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15291 tree default_argument
15292 = cp_parser_id_expression (parser
,
15293 /*template_keyword_p=*/false,
15294 /*check_dependency_p=*/true,
15295 /*template_p=*/&is_template
,
15296 /*declarator_p=*/false,
15297 /*optional_p=*/false);
15298 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15299 /* If the id-expression was a template-id that refers to
15300 a template-class, we already have the declaration here,
15301 so no further lookup is needed. */
15304 /* Look up the name. */
15306 = cp_parser_lookup_name (parser
, default_argument
,
15308 /*is_template=*/is_template
,
15309 /*is_namespace=*/false,
15310 /*check_dependency=*/true,
15311 /*ambiguous_decls=*/NULL
,
15313 /* See if the default argument is valid. */
15314 default_argument
= check_template_template_default_arg (default_argument
);
15315 pop_deferring_access_checks ();
15316 return default_argument
;
15319 /* Parse a template-parameter.
15321 template-parameter:
15323 parameter-declaration
15325 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15326 the parameter. The TREE_PURPOSE is the default value, if any.
15327 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15328 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15329 set to true iff this parameter is a parameter pack. */
15332 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15333 bool *is_parameter_pack
)
15336 cp_parameter_declarator
*parameter_declarator
;
15339 /* Assume it is a type parameter or a template parameter. */
15340 *is_non_type
= false;
15341 /* Assume it not a parameter pack. */
15342 *is_parameter_pack
= false;
15343 /* Peek at the next token. */
15344 token
= cp_lexer_peek_token (parser
->lexer
);
15345 /* If it is `template', we have a type-parameter. */
15346 if (token
->keyword
== RID_TEMPLATE
)
15347 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15348 /* If it is `class' or `typename' we do not know yet whether it is a
15349 type parameter or a non-type parameter. Consider:
15351 template <typename T, typename T::X X> ...
15355 template <class C, class D*> ...
15357 Here, the first parameter is a type parameter, and the second is
15358 a non-type parameter. We can tell by looking at the token after
15359 the identifier -- if it is a `,', `=', or `>' then we have a type
15361 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15363 /* Peek at the token after `class' or `typename'. */
15364 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15365 /* If it's an ellipsis, we have a template type parameter
15367 if (token
->type
== CPP_ELLIPSIS
)
15368 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15369 /* If it's an identifier, skip it. */
15370 if (token
->type
== CPP_NAME
)
15371 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15372 /* Now, see if the token looks like the end of a template
15374 if (token
->type
== CPP_COMMA
15375 || token
->type
== CPP_EQ
15376 || token
->type
== CPP_GREATER
)
15377 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15380 /* Otherwise, it is a non-type parameter or a constrained parameter.
15384 When parsing a default template-argument for a non-type
15385 template-parameter, the first non-nested `>' is taken as the end
15386 of the template parameter-list rather than a greater-than
15388 parameter_declarator
15389 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15390 /*parenthesized_p=*/NULL
);
15392 if (!parameter_declarator
)
15393 return error_mark_node
;
15395 /* If the parameter declaration is marked as a parameter pack, set
15396 *IS_PARAMETER_PACK to notify the caller. */
15397 if (parameter_declarator
->template_parameter_pack_p
)
15398 *is_parameter_pack
= true;
15400 if (parameter_declarator
->default_argument
)
15402 /* Can happen in some cases of erroneous input (c++/34892). */
15403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15404 /* Consume the `...' for better error recovery. */
15405 cp_lexer_consume_token (parser
->lexer
);
15408 // The parameter may have been constrained.
15409 if (is_constrained_parameter (parameter_declarator
))
15410 return finish_constrained_parameter (parser
,
15411 parameter_declarator
,
15413 is_parameter_pack
);
15415 // Now we're sure that the parameter is a non-type parameter.
15416 *is_non_type
= true;
15418 parm
= grokdeclarator (parameter_declarator
->declarator
,
15419 ¶meter_declarator
->decl_specifiers
,
15420 TPARM
, /*initialized=*/0,
15421 /*attrlist=*/NULL
);
15422 if (parm
== error_mark_node
)
15423 return error_mark_node
;
15425 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15428 /* Parse a type-parameter.
15431 class identifier [opt]
15432 class identifier [opt] = type-id
15433 typename identifier [opt]
15434 typename identifier [opt] = type-id
15435 template < template-parameter-list > class identifier [opt]
15436 template < template-parameter-list > class identifier [opt]
15439 GNU Extension (variadic templates):
15442 class ... identifier [opt]
15443 typename ... identifier [opt]
15445 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15446 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15447 the declaration of the parameter.
15449 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15452 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15457 /* Look for a keyword to tell us what kind of parameter this is. */
15458 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15460 return error_mark_node
;
15462 switch (token
->keyword
)
15468 tree default_argument
;
15470 /* If the next token is an ellipsis, we have a template
15472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15474 /* Consume the `...' token. */
15475 cp_lexer_consume_token (parser
->lexer
);
15476 maybe_warn_variadic_templates ();
15478 *is_parameter_pack
= true;
15481 /* If the next token is an identifier, then it names the
15483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15484 identifier
= cp_parser_identifier (parser
);
15486 identifier
= NULL_TREE
;
15488 /* Create the parameter. */
15489 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15491 /* If the next token is an `=', we have a default argument. */
15492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15495 = cp_parser_default_type_template_argument (parser
);
15497 /* Template parameter packs cannot have default
15499 if (*is_parameter_pack
)
15502 error_at (token
->location
,
15503 "template parameter pack %qD cannot have a "
15504 "default argument", identifier
);
15506 error_at (token
->location
,
15507 "template parameter packs cannot have "
15508 "default arguments");
15509 default_argument
= NULL_TREE
;
15511 else if (check_for_bare_parameter_packs (default_argument
))
15512 default_argument
= error_mark_node
;
15515 default_argument
= NULL_TREE
;
15517 /* Create the combined representation of the parameter and the
15518 default argument. */
15519 parameter
= build_tree_list (default_argument
, parameter
);
15526 tree default_argument
;
15528 /* Look for the `<'. */
15529 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15530 /* Parse the template-parameter-list. */
15531 cp_parser_template_parameter_list (parser
);
15532 /* Look for the `>'. */
15533 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15535 // If template requirements are present, parse them.
15538 tree reqs
= get_shorthand_constraints (current_template_parms
);
15539 if (tree r
= cp_parser_requires_clause_opt (parser
))
15540 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15541 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15544 /* Look for the `class' or 'typename' keywords. */
15545 cp_parser_type_parameter_key (parser
);
15546 /* If the next token is an ellipsis, we have a template
15548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15550 /* Consume the `...' token. */
15551 cp_lexer_consume_token (parser
->lexer
);
15552 maybe_warn_variadic_templates ();
15554 *is_parameter_pack
= true;
15556 /* If the next token is an `=', then there is a
15557 default-argument. If the next token is a `>', we are at
15558 the end of the parameter-list. If the next token is a `,',
15559 then we are at the end of this parameter. */
15560 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15561 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15562 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15564 identifier
= cp_parser_identifier (parser
);
15565 /* Treat invalid names as if the parameter were nameless. */
15566 if (identifier
== error_mark_node
)
15567 identifier
= NULL_TREE
;
15570 identifier
= NULL_TREE
;
15572 /* Create the template parameter. */
15573 parameter
= finish_template_template_parm (class_type_node
,
15576 /* If the next token is an `=', then there is a
15577 default-argument. */
15578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15581 = cp_parser_default_template_template_argument (parser
);
15583 /* Template parameter packs cannot have default
15585 if (*is_parameter_pack
)
15588 error_at (token
->location
,
15589 "template parameter pack %qD cannot "
15590 "have a default argument",
15593 error_at (token
->location
, "template parameter packs cannot "
15594 "have default arguments");
15595 default_argument
= NULL_TREE
;
15599 default_argument
= NULL_TREE
;
15601 /* Create the combined representation of the parameter and the
15602 default argument. */
15603 parameter
= build_tree_list (default_argument
, parameter
);
15608 gcc_unreachable ();
15615 /* Parse a template-id.
15618 template-name < template-argument-list [opt] >
15620 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15621 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15622 returned. Otherwise, if the template-name names a function, or set
15623 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15624 names a class, returns a TYPE_DECL for the specialization.
15626 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15627 uninstantiated templates. */
15630 cp_parser_template_id (cp_parser
*parser
,
15631 bool template_keyword_p
,
15632 bool check_dependency_p
,
15633 enum tag_types tag_type
,
15634 bool is_declaration
)
15639 cp_token_position start_of_id
= 0;
15640 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15641 bool is_identifier
;
15643 /* If the next token corresponds to a template-id, there is no need
15645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15646 if (token
->type
== CPP_TEMPLATE_ID
)
15648 cp_lexer_consume_token (parser
->lexer
);
15649 return saved_checks_value (token
->u
.tree_check_value
);
15652 /* Avoid performing name lookup if there is no possibility of
15653 finding a template-id. */
15654 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15655 || (token
->type
== CPP_NAME
15656 && !cp_parser_nth_token_starts_template_argument_list_p
15659 cp_parser_error (parser
, "expected template-id");
15660 return error_mark_node
;
15663 /* Remember where the template-id starts. */
15664 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15665 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15667 push_deferring_access_checks (dk_deferred
);
15669 /* Parse the template-name. */
15670 is_identifier
= false;
15671 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15672 check_dependency_p
,
15676 if (templ
== error_mark_node
|| is_identifier
)
15678 pop_deferring_access_checks ();
15682 /* Since we're going to preserve any side-effects from this parse, set up a
15683 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15684 in the template arguments. */
15685 tentative_firewall
firewall (parser
);
15687 /* If we find the sequence `[:' after a template-name, it's probably
15688 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15689 parse correctly the argument list. */
15690 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15691 == CPP_OPEN_SQUARE
)
15692 && next_token
->flags
& DIGRAPH
15693 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15695 && !(next_token_2
->flags
& PREV_WHITE
))
15697 cp_parser_parse_tentatively (parser
);
15698 /* Change `:' into `::'. */
15699 next_token_2
->type
= CPP_SCOPE
;
15700 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15702 cp_lexer_consume_token (parser
->lexer
);
15704 /* Parse the arguments. */
15705 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15706 if (!cp_parser_parse_definitely (parser
))
15708 /* If we couldn't parse an argument list, then we revert our changes
15709 and return simply an error. Maybe this is not a template-id
15711 next_token_2
->type
= CPP_COLON
;
15712 cp_parser_error (parser
, "expected %<<%>");
15713 pop_deferring_access_checks ();
15714 return error_mark_node
;
15716 /* Otherwise, emit an error about the invalid digraph, but continue
15717 parsing because we got our argument list. */
15718 if (permerror (next_token
->location
,
15719 "%<<::%> cannot begin a template-argument list"))
15721 static bool hint
= false;
15722 inform (next_token
->location
,
15723 "%<<:%> is an alternate spelling for %<[%>."
15724 " Insert whitespace between %<<%> and %<::%>");
15725 if (!hint
&& !flag_permissive
)
15727 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15728 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15729 "accept your code)");
15736 /* Look for the `<' that starts the template-argument-list. */
15737 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15739 pop_deferring_access_checks ();
15740 return error_mark_node
;
15742 /* Parse the arguments. */
15743 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15746 /* Set the location to be of the form:
15747 template-name < template-argument-list [opt] >
15748 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15749 with caret == start at the start of the template-name,
15750 ranging until the closing '>'. */
15751 location_t finish_loc
15752 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15753 location_t combined_loc
15754 = make_location (token
->location
, token
->location
, finish_loc
);
15756 /* Build a representation of the specialization. */
15757 if (identifier_p (templ
))
15758 template_id
= build_min_nt_loc (combined_loc
,
15761 else if (DECL_TYPE_TEMPLATE_P (templ
)
15762 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15764 bool entering_scope
;
15765 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15766 template (rather than some instantiation thereof) only if
15767 is not nested within some other construct. For example, in
15768 "template <typename T> void f(T) { A<T>::", A<T> is just an
15769 instantiation of A. */
15770 entering_scope
= (template_parm_scope_p ()
15771 && cp_lexer_next_token_is (parser
->lexer
,
15774 = finish_template_type (templ
, arguments
, entering_scope
);
15776 /* A template-like identifier may be a partial concept id. */
15777 else if (flag_concepts
15778 && (template_id
= (cp_parser_maybe_partial_concept_id
15779 (parser
, templ
, arguments
))))
15780 return template_id
;
15781 else if (variable_template_p (templ
))
15783 template_id
= lookup_template_variable (templ
, arguments
);
15784 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15785 SET_EXPR_LOCATION (template_id
, combined_loc
);
15789 /* If it's not a class-template or a template-template, it should be
15790 a function-template. */
15791 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15792 || TREE_CODE (templ
) == OVERLOAD
15793 || BASELINK_P (templ
)));
15795 template_id
= lookup_template_function (templ
, arguments
);
15796 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15797 SET_EXPR_LOCATION (template_id
, combined_loc
);
15800 /* If parsing tentatively, replace the sequence of tokens that makes
15801 up the template-id with a CPP_TEMPLATE_ID token. That way,
15802 should we re-parse the token stream, we will not have to repeat
15803 the effort required to do the parse, nor will we issue duplicate
15804 error messages about problems during instantiation of the
15807 /* Don't do this if we had a parse error in a declarator; re-parsing
15808 might succeed if a name changes meaning (60361). */
15809 && !(cp_parser_error_occurred (parser
)
15810 && cp_parser_parsing_tentatively (parser
)
15811 && parser
->in_declarator_p
))
15813 /* Reset the contents of the START_OF_ID token. */
15814 token
->type
= CPP_TEMPLATE_ID
;
15815 token
->location
= combined_loc
;
15817 /* We must mark the lookup as kept, so we don't throw it away on
15818 the first parse. */
15819 if (is_overloaded_fn (template_id
))
15820 lookup_keep (get_fns (template_id
), true);
15822 /* Retrieve any deferred checks. Do not pop this access checks yet
15823 so the memory will not be reclaimed during token replacing below. */
15824 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15825 token
->u
.tree_check_value
->value
= template_id
;
15826 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15827 token
->keyword
= RID_MAX
;
15829 /* Purge all subsequent tokens. */
15830 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15832 /* ??? Can we actually assume that, if template_id ==
15833 error_mark_node, we will have issued a diagnostic to the
15834 user, as opposed to simply marking the tentative parse as
15836 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15837 error_at (token
->location
, "parse error in template argument list");
15840 pop_to_parent_deferring_access_checks ();
15841 return template_id
;
15844 /* Parse a template-name.
15849 The standard should actually say:
15853 operator-function-id
15855 A defect report has been filed about this issue.
15857 A conversion-function-id cannot be a template name because they cannot
15858 be part of a template-id. In fact, looking at this code:
15860 a.operator K<int>()
15862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15863 It is impossible to call a templated conversion-function-id with an
15864 explicit argument list, since the only allowed template parameter is
15865 the type to which it is converting.
15867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15868 `template' keyword, in a construction like:
15872 In that case `f' is taken to be a template-name, even though there
15873 is no way of knowing for sure.
15875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15876 name refers to a set of overloaded functions, at least one of which
15877 is a template, or an IDENTIFIER_NODE with the name of the template,
15878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15879 names are looked up inside uninstantiated templates. */
15882 cp_parser_template_name (cp_parser
* parser
,
15883 bool template_keyword_p
,
15884 bool check_dependency_p
,
15885 bool is_declaration
,
15886 enum tag_types tag_type
,
15887 bool *is_identifier
)
15891 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15893 /* If the next token is `operator', then we have either an
15894 operator-function-id or a conversion-function-id. */
15895 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15897 /* We don't know whether we're looking at an
15898 operator-function-id or a conversion-function-id. */
15899 cp_parser_parse_tentatively (parser
);
15900 /* Try an operator-function-id. */
15901 identifier
= cp_parser_operator_function_id (parser
);
15902 /* If that didn't work, try a conversion-function-id. */
15903 if (!cp_parser_parse_definitely (parser
))
15905 cp_parser_error (parser
, "expected template-name");
15906 return error_mark_node
;
15909 /* Look for the identifier. */
15911 identifier
= cp_parser_identifier (parser
);
15913 /* If we didn't find an identifier, we don't have a template-id. */
15914 if (identifier
== error_mark_node
)
15915 return error_mark_node
;
15917 /* If the name immediately followed the `template' keyword, then it
15918 is a template-name. However, if the next token is not `<', then
15919 we do not treat it as a template-name, since it is not being used
15920 as part of a template-id. This enables us to handle constructs
15923 template <typename T> struct S { S(); };
15924 template <typename T> S<T>::S();
15926 correctly. We would treat `S' as a template -- if it were `S<T>'
15927 -- but we do not if there is no `<'. */
15929 if (processing_template_decl
15930 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15932 /* In a declaration, in a dependent context, we pretend that the
15933 "template" keyword was present in order to improve error
15934 recovery. For example, given:
15936 template <typename T> void f(T::X<int>);
15938 we want to treat "X<int>" as a template-id. */
15940 && !template_keyword_p
15941 && parser
->scope
&& TYPE_P (parser
->scope
)
15942 && check_dependency_p
15943 && dependent_scope_p (parser
->scope
)
15944 /* Do not do this for dtors (or ctors), since they never
15945 need the template keyword before their name. */
15946 && !constructor_name_p (identifier
, parser
->scope
))
15948 cp_token_position start
= 0;
15950 /* Explain what went wrong. */
15951 error_at (token
->location
, "non-template %qD used as template",
15953 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15954 parser
->scope
, identifier
);
15955 /* If parsing tentatively, find the location of the "<" token. */
15956 if (cp_parser_simulate_error (parser
))
15957 start
= cp_lexer_token_position (parser
->lexer
, true);
15958 /* Parse the template arguments so that we can issue error
15959 messages about them. */
15960 cp_lexer_consume_token (parser
->lexer
);
15961 cp_parser_enclosed_template_argument_list (parser
);
15962 /* Skip tokens until we find a good place from which to
15963 continue parsing. */
15964 cp_parser_skip_to_closing_parenthesis (parser
,
15965 /*recovering=*/true,
15967 /*consume_paren=*/false);
15968 /* If parsing tentatively, permanently remove the
15969 template argument list. That will prevent duplicate
15970 error messages from being issued about the missing
15971 "template" keyword. */
15973 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15975 *is_identifier
= true;
15976 parser
->context
->object_type
= NULL_TREE
;
15980 /* If the "template" keyword is present, then there is generally
15981 no point in doing name-lookup, so we just return IDENTIFIER.
15982 But, if the qualifying scope is non-dependent then we can
15983 (and must) do name-lookup normally. */
15984 if (template_keyword_p
)
15986 tree scope
= (parser
->scope
? parser
->scope
15987 : parser
->context
->object_type
);
15988 if (scope
&& TYPE_P (scope
)
15989 && (!CLASS_TYPE_P (scope
)
15990 || (check_dependency_p
&& dependent_type_p (scope
))))
15992 /* We're optimizing away the call to cp_parser_lookup_name, but
15993 we still need to do this. */
15994 parser
->context
->object_type
= NULL_TREE
;
16000 /* Look up the name. */
16001 decl
= cp_parser_lookup_name (parser
, identifier
,
16003 /*is_template=*/true,
16004 /*is_namespace=*/false,
16005 check_dependency_p
,
16006 /*ambiguous_decls=*/NULL
,
16009 decl
= strip_using_decl (decl
);
16011 /* If DECL is a template, then the name was a template-name. */
16012 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16014 if (TREE_DEPRECATED (decl
)
16015 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16016 warn_deprecated_use (decl
, NULL_TREE
);
16020 /* The standard does not explicitly indicate whether a name that
16021 names a set of overloaded declarations, some of which are
16022 templates, is a template-name. However, such a name should
16023 be a template-name; otherwise, there is no way to form a
16024 template-id for the overloaded templates. */
16025 bool found
= false;
16027 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16028 !found
&& iter
; ++iter
)
16029 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16034 /* The name does not name a template. */
16035 cp_parser_error (parser
, "expected template-name");
16036 return error_mark_node
;
16040 /* If DECL is dependent, and refers to a function, then just return
16041 its name; we will look it up again during template instantiation. */
16042 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16044 tree scope
= ovl_scope (decl
);
16045 if (TYPE_P (scope
) && dependent_type_p (scope
))
16052 /* Parse a template-argument-list.
16054 template-argument-list:
16055 template-argument ... [opt]
16056 template-argument-list , template-argument ... [opt]
16058 Returns a TREE_VEC containing the arguments. */
16061 cp_parser_template_argument_list (cp_parser
* parser
)
16063 tree fixed_args
[10];
16064 unsigned n_args
= 0;
16065 unsigned alloced
= 10;
16066 tree
*arg_ary
= fixed_args
;
16068 bool saved_in_template_argument_list_p
;
16070 bool saved_non_ice_p
;
16072 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16073 parser
->in_template_argument_list_p
= true;
16074 /* Even if the template-id appears in an integral
16075 constant-expression, the contents of the argument list do
16077 saved_ice_p
= parser
->integral_constant_expression_p
;
16078 parser
->integral_constant_expression_p
= false;
16079 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16080 parser
->non_integral_constant_expression_p
= false;
16082 /* Parse the arguments. */
16088 /* Consume the comma. */
16089 cp_lexer_consume_token (parser
->lexer
);
16091 /* Parse the template-argument. */
16092 argument
= cp_parser_template_argument (parser
);
16094 /* If the next token is an ellipsis, we're expanding a template
16096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16098 if (argument
== error_mark_node
)
16100 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16101 error_at (token
->location
,
16102 "expected parameter pack before %<...%>");
16104 /* Consume the `...' token. */
16105 cp_lexer_consume_token (parser
->lexer
);
16107 /* Make the argument into a TYPE_PACK_EXPANSION or
16108 EXPR_PACK_EXPANSION. */
16109 argument
= make_pack_expansion (argument
);
16112 if (n_args
== alloced
)
16116 if (arg_ary
== fixed_args
)
16118 arg_ary
= XNEWVEC (tree
, alloced
);
16119 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16122 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16124 arg_ary
[n_args
++] = argument
;
16126 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16128 vec
= make_tree_vec (n_args
);
16131 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16133 if (arg_ary
!= fixed_args
)
16135 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16136 parser
->integral_constant_expression_p
= saved_ice_p
;
16137 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16139 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16143 /* Parse a template-argument.
16146 assignment-expression
16150 The representation is that of an assignment-expression, type-id, or
16151 id-expression -- except that the qualified id-expression is
16152 evaluated, so that the value returned is either a DECL or an
16155 Although the standard says "assignment-expression", it forbids
16156 throw-expressions or assignments in the template argument.
16157 Therefore, we use "conditional-expression" instead. */
16160 cp_parser_template_argument (cp_parser
* parser
)
16165 bool maybe_type_id
= false;
16166 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16167 location_t loc
= 0;
16170 /* There's really no way to know what we're looking at, so we just
16171 try each alternative in order.
16175 In a template-argument, an ambiguity between a type-id and an
16176 expression is resolved to a type-id, regardless of the form of
16177 the corresponding template-parameter.
16179 Therefore, we try a type-id first. */
16180 cp_parser_parse_tentatively (parser
);
16181 argument
= cp_parser_template_type_arg (parser
);
16182 /* If there was no error parsing the type-id but the next token is a
16183 '>>', our behavior depends on which dialect of C++ we're
16184 parsing. In C++98, we probably found a typo for '> >'. But there
16185 are type-id which are also valid expressions. For instance:
16187 struct X { int operator >> (int); };
16188 template <int V> struct Foo {};
16191 Here 'X()' is a valid type-id of a function type, but the user just
16192 wanted to write the expression "X() >> 5". Thus, we remember that we
16193 found a valid type-id, but we still try to parse the argument as an
16194 expression to see what happens.
16196 In C++0x, the '>>' will be considered two separate '>'
16198 if (!cp_parser_error_occurred (parser
)
16199 && cxx_dialect
== cxx98
16200 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16202 maybe_type_id
= true;
16203 cp_parser_abort_tentative_parse (parser
);
16207 /* If the next token isn't a `,' or a `>', then this argument wasn't
16208 really finished. This means that the argument is not a valid
16210 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16211 cp_parser_error (parser
, "expected template-argument");
16212 /* If that worked, we're done. */
16213 if (cp_parser_parse_definitely (parser
))
16216 /* We're still not sure what the argument will be. */
16217 cp_parser_parse_tentatively (parser
);
16218 /* Try a template. */
16219 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16220 argument
= cp_parser_id_expression (parser
,
16221 /*template_keyword_p=*/false,
16222 /*check_dependency_p=*/true,
16224 /*declarator_p=*/false,
16225 /*optional_p=*/false);
16226 /* If the next token isn't a `,' or a `>', then this argument wasn't
16227 really finished. */
16228 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16229 cp_parser_error (parser
, "expected template-argument");
16230 if (!cp_parser_error_occurred (parser
))
16232 /* Figure out what is being referred to. If the id-expression
16233 was for a class template specialization, then we will have a
16234 TYPE_DECL at this point. There is no need to do name lookup
16235 at this point in that case. */
16236 if (TREE_CODE (argument
) != TYPE_DECL
)
16237 argument
= cp_parser_lookup_name (parser
, argument
,
16239 /*is_template=*/template_p
,
16240 /*is_namespace=*/false,
16241 /*check_dependency=*/true,
16242 /*ambiguous_decls=*/NULL
,
16243 argument_start_token
->location
);
16244 /* Handle a constrained-type-specifier for a non-type template
16246 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16248 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16249 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16250 cp_parser_error (parser
, "expected template-name");
16252 if (cp_parser_parse_definitely (parser
))
16254 if (TREE_DEPRECATED (argument
))
16255 warn_deprecated_use (argument
, NULL_TREE
);
16258 /* It must be a non-type argument. In C++17 any constant-expression is
16260 if (cxx_dialect
> cxx14
)
16263 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16265 -- an integral constant-expression of integral or enumeration
16268 -- the name of a non-type template-parameter; or
16270 -- the name of an object or function with external linkage...
16272 -- the address of an object or function with external linkage...
16274 -- a pointer to member... */
16275 /* Look for a non-type template parameter. */
16276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16278 cp_parser_parse_tentatively (parser
);
16279 argument
= cp_parser_primary_expression (parser
,
16280 /*address_p=*/false,
16282 /*template_arg_p=*/true,
16284 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16285 || !cp_parser_next_token_ends_template_argument_p (parser
))
16286 cp_parser_simulate_error (parser
);
16287 if (cp_parser_parse_definitely (parser
))
16291 /* If the next token is "&", the argument must be the address of an
16292 object or function with external linkage. */
16293 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16296 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16297 cp_lexer_consume_token (parser
->lexer
);
16299 /* See if we might have an id-expression. */
16300 token
= cp_lexer_peek_token (parser
->lexer
);
16301 if (token
->type
== CPP_NAME
16302 || token
->keyword
== RID_OPERATOR
16303 || token
->type
== CPP_SCOPE
16304 || token
->type
== CPP_TEMPLATE_ID
16305 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16307 cp_parser_parse_tentatively (parser
);
16308 argument
= cp_parser_primary_expression (parser
,
16311 /*template_arg_p=*/true,
16313 if (cp_parser_error_occurred (parser
)
16314 || !cp_parser_next_token_ends_template_argument_p (parser
))
16315 cp_parser_abort_tentative_parse (parser
);
16320 if (INDIRECT_REF_P (argument
))
16322 /* Strip the dereference temporarily. */
16323 gcc_assert (REFERENCE_REF_P (argument
));
16324 argument
= TREE_OPERAND (argument
, 0);
16327 /* If we're in a template, we represent a qualified-id referring
16328 to a static data member as a SCOPE_REF even if the scope isn't
16329 dependent so that we can check access control later. */
16331 if (TREE_CODE (probe
) == SCOPE_REF
)
16332 probe
= TREE_OPERAND (probe
, 1);
16335 /* A variable without external linkage might still be a
16336 valid constant-expression, so no error is issued here
16337 if the external-linkage check fails. */
16338 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16339 cp_parser_simulate_error (parser
);
16341 else if (is_overloaded_fn (argument
))
16342 /* All overloaded functions are allowed; if the external
16343 linkage test does not pass, an error will be issued
16347 && (TREE_CODE (argument
) == OFFSET_REF
16348 || TREE_CODE (argument
) == SCOPE_REF
))
16349 /* A pointer-to-member. */
16351 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16354 cp_parser_simulate_error (parser
);
16356 if (cp_parser_parse_definitely (parser
))
16359 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16360 tf_warning_or_error
);
16362 argument
= convert_from_reference (argument
);
16367 /* If the argument started with "&", there are no other valid
16368 alternatives at this point. */
16371 cp_parser_error (parser
, "invalid non-type template argument");
16372 return error_mark_node
;
16376 /* If the argument wasn't successfully parsed as a type-id followed
16377 by '>>', the argument can only be a constant expression now.
16378 Otherwise, we try parsing the constant-expression tentatively,
16379 because the argument could really be a type-id. */
16381 cp_parser_parse_tentatively (parser
);
16383 if (cxx_dialect
<= cxx14
)
16384 argument
= cp_parser_constant_expression (parser
);
16387 /* With C++17 generalized non-type template arguments we need to handle
16388 lvalue constant expressions, too. */
16389 argument
= cp_parser_assignment_expression (parser
);
16390 require_potential_constant_expression (argument
);
16393 if (!maybe_type_id
)
16395 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16396 cp_parser_error (parser
, "expected template-argument");
16397 if (cp_parser_parse_definitely (parser
))
16399 /* We did our best to parse the argument as a non type-id, but that
16400 was the only alternative that matched (albeit with a '>' after
16401 it). We can assume it's just a typo from the user, and a
16402 diagnostic will then be issued. */
16403 return cp_parser_template_type_arg (parser
);
16406 /* Parse an explicit-instantiation.
16408 explicit-instantiation:
16409 template declaration
16411 Although the standard says `declaration', what it really means is:
16413 explicit-instantiation:
16414 template decl-specifier-seq [opt] declarator [opt] ;
16416 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16417 supposed to be allowed. A defect report has been filed about this
16422 explicit-instantiation:
16423 storage-class-specifier template
16424 decl-specifier-seq [opt] declarator [opt] ;
16425 function-specifier template
16426 decl-specifier-seq [opt] declarator [opt] ; */
16429 cp_parser_explicit_instantiation (cp_parser
* parser
)
16431 int declares_class_or_enum
;
16432 cp_decl_specifier_seq decl_specifiers
;
16433 tree extension_specifier
= NULL_TREE
;
16435 timevar_push (TV_TEMPLATE_INST
);
16437 /* Look for an (optional) storage-class-specifier or
16438 function-specifier. */
16439 if (cp_parser_allow_gnu_extensions_p (parser
))
16441 extension_specifier
16442 = cp_parser_storage_class_specifier_opt (parser
);
16443 if (!extension_specifier
)
16444 extension_specifier
16445 = cp_parser_function_specifier_opt (parser
,
16446 /*decl_specs=*/NULL
);
16449 /* Look for the `template' keyword. */
16450 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16451 /* Let the front end know that we are processing an explicit
16453 begin_explicit_instantiation ();
16454 /* [temp.explicit] says that we are supposed to ignore access
16455 control while processing explicit instantiation directives. */
16456 push_deferring_access_checks (dk_no_check
);
16457 /* Parse a decl-specifier-seq. */
16458 cp_parser_decl_specifier_seq (parser
,
16459 CP_PARSER_FLAGS_OPTIONAL
,
16461 &declares_class_or_enum
);
16462 /* If there was exactly one decl-specifier, and it declared a class,
16463 and there's no declarator, then we have an explicit type
16465 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16469 type
= check_tag_decl (&decl_specifiers
,
16470 /*explicit_type_instantiation_p=*/true);
16471 /* Turn access control back on for names used during
16472 template instantiation. */
16473 pop_deferring_access_checks ();
16475 do_type_instantiation (type
, extension_specifier
,
16476 /*complain=*/tf_error
);
16480 cp_declarator
*declarator
;
16483 /* Parse the declarator. */
16485 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16486 /*ctor_dtor_or_conv_p=*/NULL
,
16487 /*parenthesized_p=*/NULL
,
16488 /*member_p=*/false,
16489 /*friend_p=*/false);
16490 if (declares_class_or_enum
& 2)
16491 cp_parser_check_for_definition_in_return_type (declarator
,
16492 decl_specifiers
.type
,
16493 decl_specifiers
.locations
[ds_type_spec
]);
16494 if (declarator
!= cp_error_declarator
)
16496 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16497 permerror (decl_specifiers
.locations
[ds_inline
],
16498 "explicit instantiation shall not use"
16499 " %<inline%> specifier");
16500 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16501 permerror (decl_specifiers
.locations
[ds_constexpr
],
16502 "explicit instantiation shall not use"
16503 " %<constexpr%> specifier");
16505 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16506 NORMAL
, 0, &decl_specifiers
.attributes
);
16507 /* Turn access control back on for names used during
16508 template instantiation. */
16509 pop_deferring_access_checks ();
16510 /* Do the explicit instantiation. */
16511 do_decl_instantiation (decl
, extension_specifier
);
16515 pop_deferring_access_checks ();
16516 /* Skip the body of the explicit instantiation. */
16517 cp_parser_skip_to_end_of_statement (parser
);
16520 /* We're done with the instantiation. */
16521 end_explicit_instantiation ();
16523 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16525 timevar_pop (TV_TEMPLATE_INST
);
16528 /* Parse an explicit-specialization.
16530 explicit-specialization:
16531 template < > declaration
16533 Although the standard says `declaration', what it really means is:
16535 explicit-specialization:
16536 template <> decl-specifier [opt] init-declarator [opt] ;
16537 template <> function-definition
16538 template <> explicit-specialization
16539 template <> template-declaration */
16542 cp_parser_explicit_specialization (cp_parser
* parser
)
16544 bool need_lang_pop
;
16545 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16547 /* Look for the `template' keyword. */
16548 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16549 /* Look for the `<'. */
16550 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16551 /* Look for the `>'. */
16552 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16553 /* We have processed another parameter list. */
16554 ++parser
->num_template_parameter_lists
;
16557 A template ... explicit specialization ... shall not have C
16559 if (current_lang_name
== lang_name_c
)
16561 error_at (token
->location
, "template specialization with C linkage");
16562 maybe_show_extern_c_location ();
16563 /* Give it C++ linkage to avoid confusing other parts of the
16565 push_lang_context (lang_name_cplusplus
);
16566 need_lang_pop
= true;
16569 need_lang_pop
= false;
16570 /* Let the front end know that we are beginning a specialization. */
16571 if (!begin_specialization ())
16573 end_specialization ();
16577 /* If the next keyword is `template', we need to figure out whether
16578 or not we're looking a template-declaration. */
16579 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16581 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16582 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16583 cp_parser_template_declaration_after_export (parser
,
16584 /*member_p=*/false);
16586 cp_parser_explicit_specialization (parser
);
16589 /* Parse the dependent declaration. */
16590 cp_parser_single_declaration (parser
,
16592 /*member_p=*/false,
16593 /*explicit_specialization_p=*/true,
16594 /*friend_p=*/NULL
);
16595 /* We're done with the specialization. */
16596 end_specialization ();
16597 /* For the erroneous case of a template with C linkage, we pushed an
16598 implicit C++ linkage scope; exit that scope now. */
16600 pop_lang_context ();
16601 /* We're done with this parameter list. */
16602 --parser
->num_template_parameter_lists
;
16605 /* Parse a type-specifier.
16608 simple-type-specifier
16611 elaborated-type-specifier
16619 Returns a representation of the type-specifier. For a
16620 class-specifier, enum-specifier, or elaborated-type-specifier, a
16621 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16623 The parser flags FLAGS is used to control type-specifier parsing.
16625 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16626 in a decl-specifier-seq.
16628 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16629 class-specifier, enum-specifier, or elaborated-type-specifier, then
16630 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16631 if a type is declared; 2 if it is defined. Otherwise, it is set to
16634 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16635 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16636 is set to FALSE. */
16639 cp_parser_type_specifier (cp_parser
* parser
,
16640 cp_parser_flags flags
,
16641 cp_decl_specifier_seq
*decl_specs
,
16642 bool is_declaration
,
16643 int* declares_class_or_enum
,
16644 bool* is_cv_qualifier
)
16646 tree type_spec
= NULL_TREE
;
16649 cp_decl_spec ds
= ds_last
;
16651 /* Assume this type-specifier does not declare a new type. */
16652 if (declares_class_or_enum
)
16653 *declares_class_or_enum
= 0;
16654 /* And that it does not specify a cv-qualifier. */
16655 if (is_cv_qualifier
)
16656 *is_cv_qualifier
= false;
16657 /* Peek at the next token. */
16658 token
= cp_lexer_peek_token (parser
->lexer
);
16660 /* If we're looking at a keyword, we can use that to guide the
16661 production we choose. */
16662 keyword
= token
->keyword
;
16666 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16667 goto elaborated_type_specifier
;
16669 /* Look for the enum-specifier. */
16670 type_spec
= cp_parser_enum_specifier (parser
);
16671 /* If that worked, we're done. */
16674 if (declares_class_or_enum
)
16675 *declares_class_or_enum
= 2;
16677 cp_parser_set_decl_spec_type (decl_specs
,
16680 /*type_definition_p=*/true);
16684 goto elaborated_type_specifier
;
16686 /* Any of these indicate either a class-specifier, or an
16687 elaborated-type-specifier. */
16691 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16692 goto elaborated_type_specifier
;
16694 /* Parse tentatively so that we can back up if we don't find a
16695 class-specifier. */
16696 cp_parser_parse_tentatively (parser
);
16697 /* Look for the class-specifier. */
16698 type_spec
= cp_parser_class_specifier (parser
);
16699 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16700 /* If that worked, we're done. */
16701 if (cp_parser_parse_definitely (parser
))
16703 if (declares_class_or_enum
)
16704 *declares_class_or_enum
= 2;
16706 cp_parser_set_decl_spec_type (decl_specs
,
16709 /*type_definition_p=*/true);
16713 /* Fall through. */
16714 elaborated_type_specifier
:
16715 /* We're declaring (not defining) a class or enum. */
16716 if (declares_class_or_enum
)
16717 *declares_class_or_enum
= 1;
16719 /* Fall through. */
16721 /* Look for an elaborated-type-specifier. */
16723 = (cp_parser_elaborated_type_specifier
16725 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16728 cp_parser_set_decl_spec_type (decl_specs
,
16731 /*type_definition_p=*/false);
16736 if (is_cv_qualifier
)
16737 *is_cv_qualifier
= true;
16742 if (is_cv_qualifier
)
16743 *is_cv_qualifier
= true;
16748 if (is_cv_qualifier
)
16749 *is_cv_qualifier
= true;
16753 /* The `__complex__' keyword is a GNU extension. */
16761 /* Handle simple keywords. */
16766 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16767 decl_specs
->any_specifiers_p
= true;
16769 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16772 /* If we do not already have a type-specifier, assume we are looking
16773 at a simple-type-specifier. */
16774 type_spec
= cp_parser_simple_type_specifier (parser
,
16778 /* If we didn't find a type-specifier, and a type-specifier was not
16779 optional in this context, issue an error message. */
16780 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16782 cp_parser_error (parser
, "expected type specifier");
16783 return error_mark_node
;
16789 /* Parse a simple-type-specifier.
16791 simple-type-specifier:
16792 :: [opt] nested-name-specifier [opt] type-name
16793 :: [opt] nested-name-specifier template template-id
16808 simple-type-specifier:
16810 decltype ( expression )
16813 __underlying_type ( type-id )
16817 nested-name-specifier(opt) template-name
16821 simple-type-specifier:
16823 __typeof__ unary-expression
16824 __typeof__ ( type-id )
16825 __typeof__ ( type-id ) { initializer-list , [opt] }
16827 Concepts Extension:
16829 simple-type-specifier:
16830 constrained-type-specifier
16832 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16833 appropriately updated. */
16836 cp_parser_simple_type_specifier (cp_parser
* parser
,
16837 cp_decl_specifier_seq
*decl_specs
,
16838 cp_parser_flags flags
)
16840 tree type
= NULL_TREE
;
16844 /* Peek at the next token. */
16845 token
= cp_lexer_peek_token (parser
->lexer
);
16847 /* If we're looking at a keyword, things are easy. */
16848 switch (token
->keyword
)
16852 decl_specs
->explicit_char_p
= true;
16853 type
= char_type_node
;
16856 type
= char16_type_node
;
16859 type
= char32_type_node
;
16862 type
= wchar_type_node
;
16865 type
= boolean_type_node
;
16868 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16869 type
= short_integer_type_node
;
16873 decl_specs
->explicit_int_p
= true;
16874 type
= integer_type_node
;
16880 idx
= token
->keyword
- RID_INT_N_0
;
16881 if (! int_n_enabled_p
[idx
])
16885 decl_specs
->explicit_intN_p
= true;
16886 decl_specs
->int_n_idx
= idx
;
16888 type
= int_n_trees
[idx
].signed_type
;
16892 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16893 type
= long_integer_type_node
;
16896 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16897 type
= integer_type_node
;
16900 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16901 type
= unsigned_type_node
;
16904 type
= float_type_node
;
16907 type
= double_type_node
;
16910 type
= void_type_node
;
16914 maybe_warn_cpp0x (CPP0X_AUTO
);
16915 if (parser
->auto_is_implicit_function_template_parm_p
)
16917 /* The 'auto' might be the placeholder return type for a function decl
16918 with trailing return type. */
16919 bool have_trailing_return_fn_decl
= false;
16921 cp_parser_parse_tentatively (parser
);
16922 cp_lexer_consume_token (parser
->lexer
);
16923 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16924 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16925 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16926 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16930 cp_lexer_consume_token (parser
->lexer
);
16931 cp_parser_skip_to_closing_parenthesis (parser
,
16932 /*recovering*/false,
16934 /*consume_paren*/true);
16938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16940 have_trailing_return_fn_decl
= true;
16944 cp_lexer_consume_token (parser
->lexer
);
16946 cp_parser_abort_tentative_parse (parser
);
16948 if (have_trailing_return_fn_decl
)
16950 type
= make_auto ();
16954 if (cxx_dialect
>= cxx14
)
16956 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16957 type
= TREE_TYPE (type
);
16960 type
= error_mark_node
;
16962 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16964 if (cxx_dialect
< cxx14
)
16965 error_at (token
->location
,
16966 "use of %<auto%> in lambda parameter declaration "
16967 "only available with "
16968 "-std=c++14 or -std=gnu++14");
16970 else if (cxx_dialect
< cxx14
)
16971 error_at (token
->location
,
16972 "use of %<auto%> in parameter declaration "
16973 "only available with "
16974 "-std=c++14 or -std=gnu++14");
16975 else if (!flag_concepts
)
16976 pedwarn (token
->location
, OPT_Wpedantic
,
16977 "ISO C++ forbids use of %<auto%> in parameter "
16981 type
= make_auto ();
16985 /* Since DR 743, decltype can either be a simple-type-specifier by
16986 itself or begin a nested-name-specifier. Parsing it will replace
16987 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16988 handling below decide what to do. */
16989 cp_parser_decltype (parser
);
16990 cp_lexer_set_token_position (parser
->lexer
, token
);
16994 /* Consume the `typeof' token. */
16995 cp_lexer_consume_token (parser
->lexer
);
16996 /* Parse the operand to `typeof'. */
16997 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16998 /* If it is not already a TYPE, take its type. */
16999 if (!TYPE_P (type
))
17000 type
= finish_typeof (type
);
17003 cp_parser_set_decl_spec_type (decl_specs
, type
,
17005 /*type_definition_p=*/false);
17009 case RID_UNDERLYING_TYPE
:
17010 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17012 cp_parser_set_decl_spec_type (decl_specs
, type
,
17014 /*type_definition_p=*/false);
17019 case RID_DIRECT_BASES
:
17020 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17022 cp_parser_set_decl_spec_type (decl_specs
, type
,
17024 /*type_definition_p=*/false);
17030 /* If token is an already-parsed decltype not followed by ::,
17031 it's a simple-type-specifier. */
17032 if (token
->type
== CPP_DECLTYPE
17033 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17035 type
= saved_checks_value (token
->u
.tree_check_value
);
17038 cp_parser_set_decl_spec_type (decl_specs
, type
,
17040 /*type_definition_p=*/false);
17041 /* Remember that we are handling a decltype in order to
17042 implement the resolution of DR 1510 when the argument
17043 isn't instantiation dependent. */
17044 decl_specs
->decltype_p
= true;
17046 cp_lexer_consume_token (parser
->lexer
);
17050 /* If the type-specifier was for a built-in type, we're done. */
17053 /* Record the type. */
17055 && (token
->keyword
!= RID_SIGNED
17056 && token
->keyword
!= RID_UNSIGNED
17057 && token
->keyword
!= RID_SHORT
17058 && token
->keyword
!= RID_LONG
))
17059 cp_parser_set_decl_spec_type (decl_specs
,
17062 /*type_definition_p=*/false);
17064 decl_specs
->any_specifiers_p
= true;
17066 /* Consume the token. */
17067 cp_lexer_consume_token (parser
->lexer
);
17069 if (type
== error_mark_node
)
17070 return error_mark_node
;
17072 /* There is no valid C++ program where a non-template type is
17073 followed by a "<". That usually indicates that the user thought
17074 that the type was a template. */
17075 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17078 return TYPE_NAME (type
);
17081 /* The type-specifier must be a user-defined type. */
17082 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17087 /* Don't gobble tokens or issue error messages if this is an
17088 optional type-specifier. */
17089 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17090 cp_parser_parse_tentatively (parser
);
17092 token
= cp_lexer_peek_token (parser
->lexer
);
17094 /* Look for the optional `::' operator. */
17096 = (cp_parser_global_scope_opt (parser
,
17097 /*current_scope_valid_p=*/false)
17099 /* Look for the nested-name specifier. */
17101 = (cp_parser_nested_name_specifier_opt (parser
,
17102 /*typename_keyword_p=*/false,
17103 /*check_dependency_p=*/true,
17105 /*is_declaration=*/false)
17107 /* If we have seen a nested-name-specifier, and the next token
17108 is `template', then we are using the template-id production. */
17110 && cp_parser_optional_template_keyword (parser
))
17112 /* Look for the template-id. */
17113 type
= cp_parser_template_id (parser
,
17114 /*template_keyword_p=*/true,
17115 /*check_dependency_p=*/true,
17117 /*is_declaration=*/false);
17118 /* If the template-id did not name a type, we are out of
17120 if (TREE_CODE (type
) != TYPE_DECL
)
17122 cp_parser_error (parser
, "expected template-id for type");
17126 /* Otherwise, look for a type-name. */
17128 type
= cp_parser_type_name (parser
);
17129 /* Keep track of all name-lookups performed in class scopes. */
17133 && TREE_CODE (type
) == TYPE_DECL
17134 && identifier_p (DECL_NAME (type
)))
17135 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17136 /* If it didn't work out, we don't have a TYPE. */
17137 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17138 && !cp_parser_parse_definitely (parser
))
17140 if (!type
&& cxx_dialect
>= cxx17
)
17142 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17143 cp_parser_parse_tentatively (parser
);
17145 cp_parser_global_scope_opt (parser
,
17146 /*current_scope_valid_p=*/false);
17147 cp_parser_nested_name_specifier_opt (parser
,
17148 /*typename_keyword_p=*/false,
17149 /*check_dependency_p=*/true,
17151 /*is_declaration=*/false);
17152 tree name
= cp_parser_identifier (parser
);
17153 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17154 && parser
->scope
!= error_mark_node
)
17156 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17158 /*is_template=*/false,
17159 /*is_namespace=*/false,
17160 /*check_dependency=*/true,
17161 /*ambiguous_decls=*/NULL
,
17163 if (tmpl
&& tmpl
!= error_mark_node
17164 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17165 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17166 type
= make_template_placeholder (tmpl
);
17169 type
= error_mark_node
;
17170 if (!cp_parser_simulate_error (parser
))
17171 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17172 NLE_TYPE
, token
->location
);
17176 type
= error_mark_node
;
17178 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17179 && !cp_parser_parse_definitely (parser
))
17182 if (type
&& decl_specs
)
17183 cp_parser_set_decl_spec_type (decl_specs
, type
,
17185 /*type_definition_p=*/false);
17188 /* If we didn't get a type-name, issue an error message. */
17189 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17191 cp_parser_error (parser
, "expected type-name");
17192 return error_mark_node
;
17195 if (type
&& type
!= error_mark_node
)
17197 /* See if TYPE is an Objective-C type, and if so, parse and
17198 accept any protocol references following it. Do this before
17199 the cp_parser_check_for_invalid_template_id() call, because
17200 Objective-C types can be followed by '<...>' which would
17201 enclose protocol names rather than template arguments, and so
17202 everything is fine. */
17203 if (c_dialect_objc () && !parser
->scope
17204 && (objc_is_id (type
) || objc_is_class_name (type
)))
17206 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17207 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17209 /* Clobber the "unqualified" type previously entered into
17210 DECL_SPECS with the new, improved protocol-qualified version. */
17212 decl_specs
->type
= qual_type
;
17217 /* There is no valid C++ program where a non-template type is
17218 followed by a "<". That usually indicates that the user
17219 thought that the type was a template. */
17220 cp_parser_check_for_invalid_template_id (parser
, type
,
17228 /* Parse a type-name.
17234 simple-template-id [in c++0x]
17251 Returns a TYPE_DECL for the type. */
17254 cp_parser_type_name (cp_parser
* parser
)
17256 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17261 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17265 /* We can't know yet whether it is a class-name or not. */
17266 cp_parser_parse_tentatively (parser
);
17267 /* Try a class-name. */
17268 type_decl
= cp_parser_class_name (parser
,
17269 typename_keyword_p
,
17270 /*template_keyword_p=*/false,
17272 /*check_dependency_p=*/true,
17273 /*class_head_p=*/false,
17274 /*is_declaration=*/false);
17275 /* If it's not a class-name, keep looking. */
17276 if (!cp_parser_parse_definitely (parser
))
17278 if (cxx_dialect
< cxx11
)
17279 /* It must be a typedef-name or an enum-name. */
17280 return cp_parser_nonclass_name (parser
);
17282 cp_parser_parse_tentatively (parser
);
17283 /* It is either a simple-template-id representing an
17284 instantiation of an alias template... */
17285 type_decl
= cp_parser_template_id (parser
,
17286 /*template_keyword_p=*/false,
17287 /*check_dependency_p=*/true,
17289 /*is_declaration=*/false);
17290 /* Note that this must be an instantiation of an alias template
17291 because [temp.names]/6 says:
17293 A template-id that names an alias template specialization
17296 Whereas [temp.names]/7 says:
17298 A simple-template-id that names a class template
17299 specialization is a class-name.
17301 With concepts, this could also be a partial-concept-id that
17302 declares a non-type template parameter. */
17303 if (type_decl
!= NULL_TREE
17304 && TREE_CODE (type_decl
) == TYPE_DECL
17305 && TYPE_DECL_ALIAS_P (type_decl
))
17306 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17307 else if (is_constrained_parameter (type_decl
))
17308 /* Don't do anything. */ ;
17310 cp_parser_simulate_error (parser
);
17312 if (!cp_parser_parse_definitely (parser
))
17313 /* ... Or a typedef-name or an enum-name. */
17314 return cp_parser_nonclass_name (parser
);
17320 /* Check if DECL and ARGS can form a constrained-type-specifier.
17321 If ARGS is non-null, we try to form a concept check of the
17322 form DECL<?, ARGS> where ? is a wildcard that matches any
17323 kind of template argument. If ARGS is NULL, then we try to
17324 form a concept check of the form DECL<?>. */
17327 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17328 tree decl
, tree args
)
17330 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17332 /* If we a constrained-type-specifier cannot be deduced. */
17333 if (parser
->prevent_constrained_type_specifiers
)
17336 /* A constrained type specifier can only be found in an
17337 overload set or as a reference to a template declaration.
17339 FIXME: This might be masking a bug. It's possible that
17340 that the deduction below is causing template specializations
17341 to be formed with the wildcard as an argument. */
17342 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17345 /* Try to build a call expression that evaluates the
17346 concept. This can fail if the overload set refers
17347 only to non-templates. */
17348 tree placeholder
= build_nt (WILDCARD_DECL
);
17349 tree check
= build_concept_check (decl
, placeholder
, args
);
17350 if (check
== error_mark_node
)
17353 /* Deduce the checked constraint and the prototype parameter.
17355 FIXME: In certain cases, failure to deduce should be a
17356 diagnosable error. */
17359 if (!deduce_constrained_parameter (check
, conc
, proto
))
17362 /* In template parameter scope, this results in a constrained
17363 parameter. Return a descriptor of that parm. */
17364 if (processing_template_parmlist
)
17365 return build_constrained_parameter (conc
, proto
, args
);
17367 /* In a parameter-declaration-clause, constrained-type
17368 specifiers result in invented template parameters. */
17369 if (parser
->auto_is_implicit_function_template_parm_p
)
17371 tree x
= build_constrained_parameter (conc
, proto
, args
);
17372 return synthesize_implicit_template_parm (parser
, x
);
17376 /* Otherwise, we're in a context where the constrained
17377 type name is deduced and the constraint applies
17378 after deduction. */
17379 return make_constrained_auto (conc
, args
);
17385 /* If DECL refers to a concept, return a TYPE_DECL representing
17386 the result of using the constrained type specifier in the
17387 current context. DECL refers to a concept if
17389 - it is an overload set containing a function concept taking a single
17392 - it is a variable concept taking a single type argument. */
17395 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17398 && (TREE_CODE (decl
) == OVERLOAD
17399 || BASELINK_P (decl
)
17400 || variable_concept_p (decl
)))
17401 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17406 /* Check if DECL and ARGS form a partial-concept-id. If so,
17407 assign ID to the resulting constrained placeholder.
17409 Returns true if the partial-concept-id designates a placeholder
17410 and false otherwise. Note that *id is set to NULL_TREE in
17414 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17416 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17419 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17431 Returns a TYPE_DECL for the type. */
17434 cp_parser_nonclass_name (cp_parser
* parser
)
17439 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17440 identifier
= cp_parser_identifier (parser
);
17441 if (identifier
== error_mark_node
)
17442 return error_mark_node
;
17444 /* Look up the type-name. */
17445 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17447 type_decl
= strip_using_decl (type_decl
);
17449 /* If we found an overload set, then it may refer to a concept-name. */
17450 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17453 if (TREE_CODE (type_decl
) != TYPE_DECL
17454 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17456 /* See if this is an Objective-C type. */
17457 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17458 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17460 type_decl
= TYPE_NAME (type
);
17463 /* Issue an error if we did not find a type-name. */
17464 if (TREE_CODE (type_decl
) != TYPE_DECL
17465 /* In Objective-C, we have the complication that class names are
17466 normally type names and start declarations (eg, the
17467 "NSObject" in "NSObject *object;"), but can be used in an
17468 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17469 is an expression. So, a classname followed by a dot is not a
17470 valid type-name. */
17471 || (objc_is_class_name (TREE_TYPE (type_decl
))
17472 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17474 if (!cp_parser_simulate_error (parser
))
17475 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17476 NLE_TYPE
, token
->location
);
17477 return error_mark_node
;
17479 /* Remember that the name was used in the definition of the
17480 current class so that we can check later to see if the
17481 meaning would have been different after the class was
17482 entirely defined. */
17483 else if (type_decl
!= error_mark_node
17485 maybe_note_name_used_in_class (identifier
, type_decl
);
17490 /* Parse an elaborated-type-specifier. Note that the grammar given
17491 here incorporates the resolution to DR68.
17493 elaborated-type-specifier:
17494 class-key :: [opt] nested-name-specifier [opt] identifier
17495 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17496 enum-key :: [opt] nested-name-specifier [opt] identifier
17497 typename :: [opt] nested-name-specifier identifier
17498 typename :: [opt] nested-name-specifier template [opt]
17503 elaborated-type-specifier:
17504 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17505 class-key attributes :: [opt] nested-name-specifier [opt]
17506 template [opt] template-id
17507 enum attributes :: [opt] nested-name-specifier [opt] identifier
17509 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17510 declared `friend'. If IS_DECLARATION is TRUE, then this
17511 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17512 something is being declared.
17514 Returns the TYPE specified. */
17517 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17519 bool is_declaration
)
17521 enum tag_types tag_type
;
17523 tree type
= NULL_TREE
;
17524 tree attributes
= NULL_TREE
;
17526 cp_token
*token
= NULL
;
17528 /* See if we're looking at the `enum' keyword. */
17529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17531 /* Consume the `enum' token. */
17532 cp_lexer_consume_token (parser
->lexer
);
17533 /* Remember that it's an enumeration type. */
17534 tag_type
= enum_type
;
17535 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17536 enums) is used here. */
17537 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17538 if (cp_parser_is_keyword (token
, RID_CLASS
)
17539 || cp_parser_is_keyword (token
, RID_STRUCT
))
17541 gcc_rich_location
richloc (token
->location
);
17542 richloc
.add_range (input_location
, false);
17543 richloc
.add_fixit_remove ();
17544 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17545 "a scoped enum must not use the %qD keyword",
17547 /* Consume the `struct' or `class' and parse it anyway. */
17548 cp_lexer_consume_token (parser
->lexer
);
17550 /* Parse the attributes. */
17551 attributes
= cp_parser_attributes_opt (parser
);
17553 /* Or, it might be `typename'. */
17554 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17557 /* Consume the `typename' token. */
17558 cp_lexer_consume_token (parser
->lexer
);
17559 /* Remember that it's a `typename' type. */
17560 tag_type
= typename_type
;
17562 /* Otherwise it must be a class-key. */
17565 tag_type
= cp_parser_class_key (parser
);
17566 if (tag_type
== none_type
)
17567 return error_mark_node
;
17568 /* Parse the attributes. */
17569 attributes
= cp_parser_attributes_opt (parser
);
17572 /* Look for the `::' operator. */
17573 globalscope
= cp_parser_global_scope_opt (parser
,
17574 /*current_scope_valid_p=*/false);
17575 /* Look for the nested-name-specifier. */
17576 tree nested_name_specifier
;
17577 if (tag_type
== typename_type
&& !globalscope
)
17579 nested_name_specifier
17580 = cp_parser_nested_name_specifier (parser
,
17581 /*typename_keyword_p=*/true,
17582 /*check_dependency_p=*/true,
17585 if (!nested_name_specifier
)
17586 return error_mark_node
;
17589 /* Even though `typename' is not present, the proposed resolution
17590 to Core Issue 180 says that in `class A<T>::B', `B' should be
17591 considered a type-name, even if `A<T>' is dependent. */
17592 nested_name_specifier
17593 = cp_parser_nested_name_specifier_opt (parser
,
17594 /*typename_keyword_p=*/true,
17595 /*check_dependency_p=*/true,
17598 /* For everything but enumeration types, consider a template-id.
17599 For an enumeration type, consider only a plain identifier. */
17600 if (tag_type
!= enum_type
)
17602 bool template_p
= false;
17605 /* Allow the `template' keyword. */
17606 template_p
= cp_parser_optional_template_keyword (parser
);
17607 /* If we didn't see `template', we don't know if there's a
17608 template-id or not. */
17610 cp_parser_parse_tentatively (parser
);
17611 /* Parse the template-id. */
17612 token
= cp_lexer_peek_token (parser
->lexer
);
17613 decl
= cp_parser_template_id (parser
, template_p
,
17614 /*check_dependency_p=*/true,
17617 /* If we didn't find a template-id, look for an ordinary
17619 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17621 /* We can get here when cp_parser_template_id, called by
17622 cp_parser_class_name with tag_type == none_type, succeeds
17623 and caches a BASELINK. Then, when called again here,
17624 instead of failing and returning an error_mark_node
17625 returns it (see template/typename17.C in C++11).
17626 ??? Could we diagnose this earlier? */
17627 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17629 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17630 type
= error_mark_node
;
17632 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17633 in effect, then we must assume that, upon instantiation, the
17634 template will correspond to a class. */
17635 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17636 && tag_type
== typename_type
)
17637 type
= make_typename_type (parser
->scope
, decl
,
17639 /*complain=*/tf_error
);
17640 /* If the `typename' keyword is in effect and DECL is not a type
17641 decl, then type is non existent. */
17642 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17644 else if (TREE_CODE (decl
) == TYPE_DECL
)
17646 type
= check_elaborated_type_specifier (tag_type
, decl
,
17647 /*allow_template_p=*/true);
17649 /* If the next token is a semicolon, this must be a specialization,
17650 instantiation, or friend declaration. Check the scope while we
17651 still know whether or not we had a nested-name-specifier. */
17652 if (type
!= error_mark_node
17653 && !nested_name_specifier
&& !is_friend
17654 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17655 check_unqualified_spec_or_inst (type
, token
->location
);
17657 else if (decl
== error_mark_node
)
17658 type
= error_mark_node
;
17663 token
= cp_lexer_peek_token (parser
->lexer
);
17664 identifier
= cp_parser_identifier (parser
);
17666 if (identifier
== error_mark_node
)
17668 parser
->scope
= NULL_TREE
;
17669 return error_mark_node
;
17672 /* For a `typename', we needn't call xref_tag. */
17673 if (tag_type
== typename_type
17674 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17675 return cp_parser_make_typename_type (parser
, identifier
,
17678 /* Template parameter lists apply only if we are not within a
17679 function parameter list. */
17680 bool template_parm_lists_apply
17681 = parser
->num_template_parameter_lists
;
17682 if (template_parm_lists_apply
)
17683 for (cp_binding_level
*s
= current_binding_level
;
17684 s
&& s
->kind
!= sk_template_parms
;
17685 s
= s
->level_chain
)
17686 if (s
->kind
== sk_function_parms
)
17687 template_parm_lists_apply
= false;
17689 /* Look up a qualified name in the usual way. */
17693 tree ambiguous_decls
;
17695 decl
= cp_parser_lookup_name (parser
, identifier
,
17697 /*is_template=*/false,
17698 /*is_namespace=*/false,
17699 /*check_dependency=*/true,
17703 /* If the lookup was ambiguous, an error will already have been
17705 if (ambiguous_decls
)
17706 return error_mark_node
;
17708 /* If we are parsing friend declaration, DECL may be a
17709 TEMPLATE_DECL tree node here. However, we need to check
17710 whether this TEMPLATE_DECL results in valid code. Consider
17711 the following example:
17714 template <class T> class C {};
17717 template <class T> friend class N::C; // #1, valid code
17719 template <class T> class Y {
17720 friend class N::C; // #2, invalid code
17723 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17724 name lookup of `N::C'. We see that friend declaration must
17725 be template for the code to be valid. Note that
17726 processing_template_decl does not work here since it is
17727 always 1 for the above two cases. */
17729 decl
= (cp_parser_maybe_treat_template_as_class
17730 (decl
, /*tag_name_p=*/is_friend
17731 && template_parm_lists_apply
));
17733 if (TREE_CODE (decl
) != TYPE_DECL
)
17735 cp_parser_diagnose_invalid_type_name (parser
,
17738 return error_mark_node
;
17741 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17743 bool allow_template
= (template_parm_lists_apply
17744 || DECL_SELF_REFERENCE_P (decl
));
17745 type
= check_elaborated_type_specifier (tag_type
, decl
,
17748 if (type
== error_mark_node
)
17749 return error_mark_node
;
17752 /* Forward declarations of nested types, such as
17757 are invalid unless all components preceding the final '::'
17758 are complete. If all enclosing types are complete, these
17759 declarations become merely pointless.
17761 Invalid forward declarations of nested types are errors
17762 caught elsewhere in parsing. Those that are pointless arrive
17765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17766 && !is_friend
&& !processing_explicit_instantiation
)
17767 warning (0, "declaration %qD does not declare anything", decl
);
17769 type
= TREE_TYPE (decl
);
17773 /* An elaborated-type-specifier sometimes introduces a new type and
17774 sometimes names an existing type. Normally, the rule is that it
17775 introduces a new type only if there is not an existing type of
17776 the same name already in scope. For example, given:
17779 void f() { struct S s; }
17781 the `struct S' in the body of `f' is the same `struct S' as in
17782 the global scope; the existing definition is used. However, if
17783 there were no global declaration, this would introduce a new
17784 local class named `S'.
17786 An exception to this rule applies to the following code:
17788 namespace N { struct S; }
17790 Here, the elaborated-type-specifier names a new type
17791 unconditionally; even if there is already an `S' in the
17792 containing scope this declaration names a new type.
17793 This exception only applies if the elaborated-type-specifier
17794 forms the complete declaration:
17798 A declaration consisting solely of `class-key identifier ;' is
17799 either a redeclaration of the name in the current scope or a
17800 forward declaration of the identifier as a class name. It
17801 introduces the name into the current scope.
17803 We are in this situation precisely when the next token is a `;'.
17805 An exception to the exception is that a `friend' declaration does
17806 *not* name a new type; i.e., given:
17808 struct S { friend struct T; };
17810 `T' is not a new type in the scope of `S'.
17812 Also, `new struct S' or `sizeof (struct S)' never results in the
17813 definition of a new type; a new type can only be declared in a
17814 declaration context. */
17820 /* Friends have special name lookup rules. */
17821 ts
= ts_within_enclosing_non_class
;
17822 else if (is_declaration
17823 && cp_lexer_next_token_is (parser
->lexer
,
17825 /* This is a `class-key identifier ;' */
17831 (template_parm_lists_apply
17832 && (cp_parser_next_token_starts_class_definition_p (parser
)
17833 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17834 /* An unqualified name was used to reference this type, so
17835 there were no qualifying templates. */
17836 if (template_parm_lists_apply
17837 && !cp_parser_check_template_parameters (parser
,
17838 /*num_templates=*/0,
17840 /*declarator=*/NULL
))
17841 return error_mark_node
;
17842 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17846 if (type
== error_mark_node
)
17847 return error_mark_node
;
17849 /* Allow attributes on forward declarations of classes. */
17852 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17853 warning (OPT_Wattributes
,
17854 "attributes ignored on uninstantiated type");
17855 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17856 && ! processing_explicit_instantiation
)
17857 warning (OPT_Wattributes
,
17858 "attributes ignored on template instantiation");
17859 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17860 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17862 warning (OPT_Wattributes
,
17863 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17866 if (tag_type
!= enum_type
)
17868 /* Indicate whether this class was declared as a `class' or as a
17870 if (CLASS_TYPE_P (type
))
17871 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17872 cp_parser_check_class_key (tag_type
, type
);
17875 /* A "<" cannot follow an elaborated type specifier. If that
17876 happens, the user was probably trying to form a template-id. */
17877 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17883 /* Parse an enum-specifier.
17886 enum-head { enumerator-list [opt] }
17887 enum-head { enumerator-list , } [C++0x]
17890 enum-key identifier [opt] enum-base [opt]
17891 enum-key nested-name-specifier identifier enum-base [opt]
17896 enum struct [C++0x]
17899 : type-specifier-seq
17901 opaque-enum-specifier:
17902 enum-key identifier enum-base [opt] ;
17905 enum-key attributes[opt] identifier [opt] enum-base [opt]
17906 { enumerator-list [opt] }attributes[opt]
17907 enum-key attributes[opt] identifier [opt] enum-base [opt]
17908 { enumerator-list, }attributes[opt] [C++0x]
17910 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17911 if the token stream isn't an enum-specifier after all. */
17914 cp_parser_enum_specifier (cp_parser
* parser
)
17917 tree type
= NULL_TREE
;
17919 tree nested_name_specifier
= NULL_TREE
;
17921 bool scoped_enum_p
= false;
17922 bool has_underlying_type
= false;
17923 bool nested_being_defined
= false;
17924 bool new_value_list
= false;
17925 bool is_new_type
= false;
17926 bool is_unnamed
= false;
17927 tree underlying_type
= NULL_TREE
;
17928 cp_token
*type_start_token
= NULL
;
17929 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
17931 parser
->colon_corrects_to_scope_p
= false;
17933 /* Parse tentatively so that we can back up if we don't find a
17935 cp_parser_parse_tentatively (parser
);
17937 /* Caller guarantees that the current token is 'enum', an identifier
17938 possibly follows, and the token after that is an opening brace.
17939 If we don't have an identifier, fabricate an anonymous name for
17940 the enumeration being defined. */
17941 cp_lexer_consume_token (parser
->lexer
);
17943 /* Parse the "class" or "struct", which indicates a scoped
17944 enumeration type in C++0x. */
17945 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17946 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17948 if (cxx_dialect
< cxx11
)
17949 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17951 /* Consume the `struct' or `class' token. */
17952 cp_lexer_consume_token (parser
->lexer
);
17954 scoped_enum_p
= true;
17957 attributes
= cp_parser_attributes_opt (parser
);
17959 /* Clear the qualification. */
17960 parser
->scope
= NULL_TREE
;
17961 parser
->qualifying_scope
= NULL_TREE
;
17962 parser
->object_scope
= NULL_TREE
;
17964 /* Figure out in what scope the declaration is being placed. */
17965 prev_scope
= current_scope ();
17967 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17969 push_deferring_access_checks (dk_no_check
);
17970 nested_name_specifier
17971 = cp_parser_nested_name_specifier_opt (parser
,
17972 /*typename_keyword_p=*/true,
17973 /*check_dependency_p=*/false,
17975 /*is_declaration=*/false);
17977 if (nested_name_specifier
)
17981 identifier
= cp_parser_identifier (parser
);
17982 name
= cp_parser_lookup_name (parser
, identifier
,
17984 /*is_template=*/false,
17985 /*is_namespace=*/false,
17986 /*check_dependency=*/true,
17987 /*ambiguous_decls=*/NULL
,
17989 if (name
&& name
!= error_mark_node
)
17991 type
= TREE_TYPE (name
);
17992 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17994 /* Are template enums allowed in ISO? */
17995 if (template_parm_scope_p ())
17996 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17997 "%qD is an enumeration template", name
);
17998 /* ignore a typename reference, for it will be solved by name
18003 else if (nested_name_specifier
== error_mark_node
)
18004 /* We already issued an error. */;
18007 error_at (type_start_token
->location
,
18008 "%qD does not name an enumeration in %qT",
18009 identifier
, nested_name_specifier
);
18010 nested_name_specifier
= error_mark_node
;
18015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18016 identifier
= cp_parser_identifier (parser
);
18019 identifier
= make_anon_name ();
18022 error_at (type_start_token
->location
,
18023 "unnamed scoped enum is not allowed");
18026 pop_deferring_access_checks ();
18028 /* Check for the `:' that denotes a specified underlying type in C++0x.
18029 Note that a ':' could also indicate a bitfield width, however. */
18030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18032 cp_decl_specifier_seq type_specifiers
;
18034 /* Consume the `:'. */
18035 cp_lexer_consume_token (parser
->lexer
);
18037 /* Parse the type-specifier-seq. */
18038 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18039 /*is_trailing_return=*/false,
18042 /* At this point this is surely not elaborated type specifier. */
18043 if (!cp_parser_parse_definitely (parser
))
18046 if (cxx_dialect
< cxx11
)
18047 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18049 has_underlying_type
= true;
18051 /* If that didn't work, stop. */
18052 if (type_specifiers
.type
!= error_mark_node
)
18054 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18055 /*initialized=*/0, NULL
);
18056 if (underlying_type
== error_mark_node
18057 || check_for_bare_parameter_packs (underlying_type
))
18058 underlying_type
= NULL_TREE
;
18062 /* Look for the `{' but don't consume it yet. */
18063 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18065 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18067 cp_parser_error (parser
, "expected %<{%>");
18068 if (has_underlying_type
)
18074 /* An opaque-enum-specifier must have a ';' here. */
18075 if ((scoped_enum_p
|| underlying_type
)
18076 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18078 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18079 if (has_underlying_type
)
18087 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18090 if (nested_name_specifier
)
18092 if (CLASS_TYPE_P (nested_name_specifier
))
18094 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18095 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18096 push_scope (nested_name_specifier
);
18098 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18100 push_nested_namespace (nested_name_specifier
);
18104 /* Issue an error message if type-definitions are forbidden here. */
18105 if (!cp_parser_check_type_definition (parser
))
18106 type
= error_mark_node
;
18108 /* Create the new type. We do this before consuming the opening
18109 brace so the enum will be recorded as being on the line of its
18110 tag (or the 'enum' keyword, if there is no tag). */
18111 type
= start_enum (identifier
, type
, underlying_type
,
18112 attributes
, scoped_enum_p
, &is_new_type
);
18114 /* If the next token is not '{' it is an opaque-enum-specifier or an
18115 elaborated-type-specifier. */
18116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18118 timevar_push (TV_PARSE_ENUM
);
18119 if (nested_name_specifier
18120 && nested_name_specifier
!= error_mark_node
)
18122 /* The following catches invalid code such as:
18123 enum class S<int>::E { A, B, C }; */
18124 if (!processing_specialization
18125 && CLASS_TYPE_P (nested_name_specifier
)
18126 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18127 error_at (type_start_token
->location
, "cannot add an enumerator "
18128 "list to a template instantiation");
18130 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18132 error_at (type_start_token
->location
,
18133 "%<%T::%E%> has not been declared",
18134 TYPE_CONTEXT (nested_name_specifier
),
18135 nested_name_specifier
);
18136 type
= error_mark_node
;
18138 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18139 && !CLASS_TYPE_P (nested_name_specifier
))
18141 error_at (type_start_token
->location
, "nested name specifier "
18142 "%qT for enum declaration does not name a class "
18143 "or namespace", nested_name_specifier
);
18144 type
= error_mark_node
;
18146 /* If that scope does not contain the scope in which the
18147 class was originally declared, the program is invalid. */
18148 else if (prev_scope
&& !is_ancestor (prev_scope
,
18149 nested_name_specifier
))
18151 if (at_namespace_scope_p ())
18152 error_at (type_start_token
->location
,
18153 "declaration of %qD in namespace %qD which does not "
18155 type
, prev_scope
, nested_name_specifier
);
18157 error_at (type_start_token
->location
,
18158 "declaration of %qD in %qD which does not "
18160 type
, prev_scope
, nested_name_specifier
);
18161 type
= error_mark_node
;
18163 /* If that scope is the scope where the declaration is being placed
18164 the program is invalid. */
18165 else if (CLASS_TYPE_P (nested_name_specifier
)
18166 && CLASS_TYPE_P (prev_scope
)
18167 && same_type_p (nested_name_specifier
, prev_scope
))
18169 permerror (type_start_token
->location
,
18170 "extra qualification not allowed");
18171 nested_name_specifier
= NULL_TREE
;
18176 begin_scope (sk_scoped_enum
, type
);
18178 /* Consume the opening brace. */
18179 matching_braces braces
;
18180 braces
.consume_open (parser
);
18182 if (type
== error_mark_node
)
18183 ; /* Nothing to add */
18184 else if (OPAQUE_ENUM_P (type
)
18185 || (cxx_dialect
> cxx98
&& processing_specialization
))
18187 new_value_list
= true;
18188 SET_OPAQUE_ENUM_P (type
, false);
18189 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18193 error_at (type_start_token
->location
,
18194 "multiple definition of %q#T", type
);
18195 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18196 "previous definition here");
18197 type
= error_mark_node
;
18200 if (type
== error_mark_node
)
18201 cp_parser_skip_to_end_of_block_or_statement (parser
);
18202 /* If the next token is not '}', then there are some enumerators. */
18203 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18205 if (is_unnamed
&& !scoped_enum_p
)
18206 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18207 "ISO C++ forbids empty unnamed enum");
18210 cp_parser_enumerator_list (parser
, type
);
18212 /* Consume the final '}'. */
18213 braces
.require_close (parser
);
18217 timevar_pop (TV_PARSE_ENUM
);
18221 /* If a ';' follows, then it is an opaque-enum-specifier
18222 and additional restrictions apply. */
18223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18226 error_at (type_start_token
->location
,
18227 "opaque-enum-specifier without name");
18228 else if (nested_name_specifier
)
18229 error_at (type_start_token
->location
,
18230 "opaque-enum-specifier must use a simple identifier");
18234 /* Look for trailing attributes to apply to this enumeration, and
18235 apply them if appropriate. */
18236 if (cp_parser_allow_gnu_extensions_p (parser
))
18238 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18239 cplus_decl_attributes (&type
,
18241 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18244 /* Finish up the enumeration. */
18245 if (type
!= error_mark_node
)
18247 if (new_value_list
)
18248 finish_enum_value_list (type
);
18250 finish_enum (type
);
18253 if (nested_name_specifier
)
18255 if (CLASS_TYPE_P (nested_name_specifier
))
18257 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18258 pop_scope (nested_name_specifier
);
18260 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18262 pop_nested_namespace (nested_name_specifier
);
18266 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18270 /* Parse an enumerator-list. The enumerators all have the indicated
18274 enumerator-definition
18275 enumerator-list , enumerator-definition */
18278 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18282 /* Parse an enumerator-definition. */
18283 cp_parser_enumerator_definition (parser
, type
);
18285 /* If the next token is not a ',', we've reached the end of
18287 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18289 /* Otherwise, consume the `,' and keep going. */
18290 cp_lexer_consume_token (parser
->lexer
);
18291 /* If the next token is a `}', there is a trailing comma. */
18292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18294 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18295 pedwarn (input_location
, OPT_Wpedantic
,
18296 "comma at end of enumerator list");
18302 /* Parse an enumerator-definition. The enumerator has the indicated
18305 enumerator-definition:
18307 enumerator = constant-expression
18314 enumerator-definition:
18315 enumerator attributes [opt]
18316 enumerator attributes [opt] = constant-expression */
18319 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18325 /* Save the input location because we are interested in the location
18326 of the identifier and not the location of the explicit value. */
18327 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18329 /* Look for the identifier. */
18330 identifier
= cp_parser_identifier (parser
);
18331 if (identifier
== error_mark_node
)
18334 /* Parse any specified attributes. */
18335 tree attrs
= cp_parser_attributes_opt (parser
);
18337 /* If the next token is an '=', then there is an explicit value. */
18338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18340 /* Consume the `=' token. */
18341 cp_lexer_consume_token (parser
->lexer
);
18342 /* Parse the value. */
18343 value
= cp_parser_constant_expression (parser
);
18348 /* If we are processing a template, make sure the initializer of the
18349 enumerator doesn't contain any bare template parameter pack. */
18350 if (check_for_bare_parameter_packs (value
))
18351 value
= error_mark_node
;
18353 /* Create the enumerator. */
18354 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18357 /* Parse a namespace-name.
18360 original-namespace-name
18363 Returns the NAMESPACE_DECL for the namespace. */
18366 cp_parser_namespace_name (cp_parser
* parser
)
18369 tree namespace_decl
;
18371 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18373 /* Get the name of the namespace. */
18374 identifier
= cp_parser_identifier (parser
);
18375 if (identifier
== error_mark_node
)
18376 return error_mark_node
;
18378 /* Look up the identifier in the currently active scope. Look only
18379 for namespaces, due to:
18381 [basic.lookup.udir]
18383 When looking up a namespace-name in a using-directive or alias
18384 definition, only namespace names are considered.
18388 [basic.lookup.qual]
18390 During the lookup of a name preceding the :: scope resolution
18391 operator, object, function, and enumerator names are ignored.
18393 (Note that cp_parser_qualifying_entity only calls this
18394 function if the token after the name is the scope resolution
18396 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18398 /*is_template=*/false,
18399 /*is_namespace=*/true,
18400 /*check_dependency=*/true,
18401 /*ambiguous_decls=*/NULL
,
18403 /* If it's not a namespace, issue an error. */
18404 if (namespace_decl
== error_mark_node
18405 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18407 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18409 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18410 if (namespace_decl
== error_mark_node
18411 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18412 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18415 cp_parser_error (parser
, "expected namespace-name");
18416 namespace_decl
= error_mark_node
;
18419 return namespace_decl
;
18422 /* Parse a namespace-definition.
18424 namespace-definition:
18425 named-namespace-definition
18426 unnamed-namespace-definition
18428 named-namespace-definition:
18429 original-namespace-definition
18430 extension-namespace-definition
18432 original-namespace-definition:
18433 namespace identifier { namespace-body }
18435 extension-namespace-definition:
18436 namespace original-namespace-name { namespace-body }
18438 unnamed-namespace-definition:
18439 namespace { namespace-body } */
18442 cp_parser_namespace_definition (cp_parser
* parser
)
18445 int nested_definition_count
= 0;
18447 cp_ensure_no_omp_declare_simd (parser
);
18448 cp_ensure_no_oacc_routine (parser
);
18450 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18454 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18455 cp_lexer_consume_token (parser
->lexer
);
18458 /* Look for the `namespace' keyword. */
18460 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18462 /* Parse any specified attributes before the identifier. */
18463 tree attribs
= cp_parser_attributes_opt (parser
);
18467 identifier
= NULL_TREE
;
18469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18471 identifier
= cp_parser_identifier (parser
);
18473 /* Parse any attributes specified after the identifier. */
18474 attribs
= chainon (attribs
, cp_parser_attributes_opt (parser
));
18477 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18480 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18481 pedwarn (input_location
, OPT_Wpedantic
,
18482 "nested namespace definitions only available with "
18483 "-std=c++17 or -std=gnu++17");
18485 /* Nested namespace names can create new namespaces (unlike
18486 other qualified-ids). */
18487 if (int count
= identifier
? push_namespace (identifier
) : 0)
18488 nested_definition_count
+= count
;
18490 cp_parser_error (parser
, "nested namespace name required");
18491 cp_lexer_consume_token (parser
->lexer
);
18494 if (nested_definition_count
&& !identifier
)
18495 cp_parser_error (parser
, "namespace name required");
18497 if (nested_definition_count
&& attribs
)
18498 error_at (token
->location
,
18499 "a nested namespace definition cannot have attributes");
18500 if (nested_definition_count
&& is_inline
)
18501 error_at (token
->location
,
18502 "a nested namespace definition cannot be inline");
18504 /* Start the namespace. */
18505 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18507 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18509 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18511 /* Look for the `{' to validate starting the namespace. */
18512 matching_braces braces
;
18513 if (braces
.require_open (parser
))
18515 /* Parse the body of the namespace. */
18516 cp_parser_namespace_body (parser
);
18518 /* Look for the final `}'. */
18519 braces
.require_close (parser
);
18522 if (has_visibility
)
18523 pop_visibility (1);
18525 /* Pop the nested namespace definitions. */
18526 while (nested_definition_count
--)
18530 /* Parse a namespace-body.
18533 declaration-seq [opt] */
18536 cp_parser_namespace_body (cp_parser
* parser
)
18538 cp_parser_declaration_seq_opt (parser
);
18541 /* Parse a namespace-alias-definition.
18543 namespace-alias-definition:
18544 namespace identifier = qualified-namespace-specifier ; */
18547 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18550 tree namespace_specifier
;
18552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18554 /* Look for the `namespace' keyword. */
18555 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18556 /* Look for the identifier. */
18557 identifier
= cp_parser_identifier (parser
);
18558 if (identifier
== error_mark_node
)
18560 /* Look for the `=' token. */
18561 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18562 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18564 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18565 /* Skip the definition. */
18566 cp_lexer_consume_token (parser
->lexer
);
18567 if (cp_parser_skip_to_closing_brace (parser
))
18568 cp_lexer_consume_token (parser
->lexer
);
18571 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18572 /* Look for the qualified-namespace-specifier. */
18573 namespace_specifier
18574 = cp_parser_qualified_namespace_specifier (parser
);
18575 /* Look for the `;' token. */
18576 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18578 /* Register the alias in the symbol table. */
18579 do_namespace_alias (identifier
, namespace_specifier
);
18582 /* Parse a qualified-namespace-specifier.
18584 qualified-namespace-specifier:
18585 :: [opt] nested-name-specifier [opt] namespace-name
18587 Returns a NAMESPACE_DECL corresponding to the specified
18591 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18593 /* Look for the optional `::'. */
18594 cp_parser_global_scope_opt (parser
,
18595 /*current_scope_valid_p=*/false);
18597 /* Look for the optional nested-name-specifier. */
18598 cp_parser_nested_name_specifier_opt (parser
,
18599 /*typename_keyword_p=*/false,
18600 /*check_dependency_p=*/true,
18602 /*is_declaration=*/true);
18604 return cp_parser_namespace_name (parser
);
18607 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18608 access declaration.
18611 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18612 using :: unqualified-id ;
18614 access-declaration:
18620 cp_parser_using_declaration (cp_parser
* parser
,
18621 bool access_declaration_p
)
18624 bool typename_p
= false;
18625 bool global_scope_p
;
18629 int oldcount
= errorcount
;
18630 cp_token
*diag_token
= NULL
;
18632 if (access_declaration_p
)
18634 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18635 cp_parser_parse_tentatively (parser
);
18639 /* Look for the `using' keyword. */
18640 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18643 /* Peek at the next token. */
18644 token
= cp_lexer_peek_token (parser
->lexer
);
18645 /* See if it's `typename'. */
18646 if (token
->keyword
== RID_TYPENAME
)
18648 /* Remember that we've seen it. */
18650 /* Consume the `typename' token. */
18651 cp_lexer_consume_token (parser
->lexer
);
18655 /* Look for the optional global scope qualification. */
18657 = (cp_parser_global_scope_opt (parser
,
18658 /*current_scope_valid_p=*/false)
18661 /* If we saw `typename', or didn't see `::', then there must be a
18662 nested-name-specifier present. */
18663 if (typename_p
|| !global_scope_p
)
18665 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18666 /*check_dependency_p=*/true,
18668 /*is_declaration=*/true);
18669 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18671 cp_parser_skip_to_end_of_block_or_statement (parser
);
18675 /* Otherwise, we could be in either of the two productions. In that
18676 case, treat the nested-name-specifier as optional. */
18678 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18679 /*typename_keyword_p=*/false,
18680 /*check_dependency_p=*/true,
18682 /*is_declaration=*/true);
18684 qscope
= global_namespace
;
18685 else if (UNSCOPED_ENUM_P (qscope
))
18686 qscope
= CP_TYPE_CONTEXT (qscope
);
18688 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18689 /* Something has already gone wrong; there's no need to parse
18690 further. Since an error has occurred, the return value of
18691 cp_parser_parse_definitely will be false, as required. */
18692 return cp_parser_parse_definitely (parser
);
18694 token
= cp_lexer_peek_token (parser
->lexer
);
18695 /* Parse the unqualified-id. */
18696 identifier
= cp_parser_unqualified_id (parser
,
18697 /*template_keyword_p=*/false,
18698 /*check_dependency_p=*/true,
18699 /*declarator_p=*/true,
18700 /*optional_p=*/false);
18702 if (access_declaration_p
)
18704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18705 cp_parser_simulate_error (parser
);
18706 if (!cp_parser_parse_definitely (parser
))
18709 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18711 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18712 if (cxx_dialect
< cxx17
18713 && !in_system_header_at (ell
->location
))
18714 pedwarn (ell
->location
, 0,
18715 "pack expansion in using-declaration only available "
18716 "with -std=c++17 or -std=gnu++17");
18717 qscope
= make_pack_expansion (qscope
);
18720 /* The function we call to handle a using-declaration is different
18721 depending on what scope we are in. */
18722 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18724 else if (!identifier_p (identifier
)
18725 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18726 /* [namespace.udecl]
18728 A using declaration shall not name a template-id. */
18729 error_at (token
->location
,
18730 "a template-id may not appear in a using-declaration");
18733 if (at_class_scope_p ())
18735 /* Create the USING_DECL. */
18736 decl
= do_class_using_decl (qscope
, identifier
);
18738 if (decl
&& typename_p
)
18739 USING_DECL_TYPENAME_P (decl
) = 1;
18741 if (check_for_bare_parameter_packs (decl
))
18743 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18747 /* Add it to the list of members in this class. */
18748 finish_member_declaration (decl
);
18752 decl
= cp_parser_lookup_name_simple (parser
,
18755 if (decl
== error_mark_node
)
18756 cp_parser_name_lookup_error (parser
, identifier
,
18759 else if (check_for_bare_parameter_packs (decl
))
18761 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18764 else if (!at_namespace_scope_p ())
18765 finish_local_using_decl (decl
, qscope
, identifier
);
18767 finish_namespace_using_decl (decl
, qscope
, identifier
);
18771 if (!access_declaration_p
18772 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18774 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18775 if (cxx_dialect
< cxx17
)
18776 pedwarn (comma
->location
, 0,
18777 "comma-separated list in using-declaration only available "
18778 "with -std=c++17 or -std=gnu++17");
18782 /* Look for the final `;'. */
18783 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18785 if (access_declaration_p
&& errorcount
== oldcount
)
18786 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18787 "access declarations are deprecated "
18788 "in favour of using-declarations; "
18789 "suggestion: add the %<using%> keyword");
18794 /* Parse an alias-declaration.
18797 using identifier attribute-specifier-seq [opt] = type-id */
18800 cp_parser_alias_declaration (cp_parser
* parser
)
18802 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18803 location_t id_location
;
18804 cp_declarator
*declarator
;
18805 cp_decl_specifier_seq decl_specs
;
18807 const char *saved_message
= NULL
;
18809 /* Look for the `using' keyword. */
18810 cp_token
*using_token
18811 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18812 if (using_token
== NULL
)
18813 return error_mark_node
;
18815 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18816 id
= cp_parser_identifier (parser
);
18817 if (id
== error_mark_node
)
18818 return error_mark_node
;
18820 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18821 attributes
= cp_parser_attributes_opt (parser
);
18822 if (attributes
== error_mark_node
)
18823 return error_mark_node
;
18825 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18827 if (cp_parser_error_occurred (parser
))
18828 return error_mark_node
;
18830 cp_parser_commit_to_tentative_parse (parser
);
18832 /* Now we are going to parse the type-id of the declaration. */
18837 "A type-specifier-seq shall not define a class or enumeration
18838 unless it appears in the type-id of an alias-declaration (7.1.3) that
18839 is not the declaration of a template-declaration."
18841 In other words, if we currently are in an alias template, the
18842 type-id should not define a type.
18844 So let's set parser->type_definition_forbidden_message in that
18845 case; cp_parser_check_type_definition (called by
18846 cp_parser_class_specifier) will then emit an error if a type is
18847 defined in the type-id. */
18848 if (parser
->num_template_parameter_lists
)
18850 saved_message
= parser
->type_definition_forbidden_message
;
18851 parser
->type_definition_forbidden_message
=
18852 G_("types may not be defined in alias template declarations");
18855 type
= cp_parser_type_id (parser
);
18857 /* Restore the error message if need be. */
18858 if (parser
->num_template_parameter_lists
)
18859 parser
->type_definition_forbidden_message
= saved_message
;
18861 if (type
== error_mark_node
18862 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18864 cp_parser_skip_to_end_of_block_or_statement (parser
);
18865 return error_mark_node
;
18868 /* A typedef-name can also be introduced by an alias-declaration. The
18869 identifier following the using keyword becomes a typedef-name. It has
18870 the same semantics as if it were introduced by the typedef
18871 specifier. In particular, it does not define a new type and it shall
18872 not appear in the type-id. */
18874 clear_decl_specs (&decl_specs
);
18875 decl_specs
.type
= type
;
18876 if (attributes
!= NULL_TREE
)
18878 decl_specs
.attributes
= attributes
;
18879 set_and_check_decl_spec_loc (&decl_specs
,
18883 set_and_check_decl_spec_loc (&decl_specs
,
18886 set_and_check_decl_spec_loc (&decl_specs
,
18890 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18891 declarator
->id_loc
= id_location
;
18893 member_p
= at_class_scope_p ();
18895 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18896 NULL_TREE
, attributes
);
18898 decl
= start_decl (declarator
, &decl_specs
, 0,
18899 attributes
, NULL_TREE
, &pushed_scope
);
18900 if (decl
== error_mark_node
)
18903 // Attach constraints to the alias declaration.
18904 if (flag_concepts
&& current_template_parms
)
18906 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18907 tree constr
= build_constraints (reqs
, NULL_TREE
);
18908 set_constraints (decl
, constr
);
18911 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18914 pop_scope (pushed_scope
);
18916 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18917 added into the symbol table; otherwise, return the TYPE_DECL. */
18918 if (DECL_LANG_SPECIFIC (decl
)
18919 && DECL_TEMPLATE_INFO (decl
)
18920 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
18922 decl
= DECL_TI_TEMPLATE (decl
);
18924 check_member_template (decl
);
18930 /* Parse a using-directive.
18933 using namespace :: [opt] nested-name-specifier [opt]
18934 namespace-name ; */
18937 cp_parser_using_directive (cp_parser
* parser
)
18939 tree namespace_decl
;
18942 /* Look for the `using' keyword. */
18943 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18944 /* And the `namespace' keyword. */
18945 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18946 /* Look for the optional `::' operator. */
18947 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18948 /* And the optional nested-name-specifier. */
18949 cp_parser_nested_name_specifier_opt (parser
,
18950 /*typename_keyword_p=*/false,
18951 /*check_dependency_p=*/true,
18953 /*is_declaration=*/true);
18954 /* Get the namespace being used. */
18955 namespace_decl
= cp_parser_namespace_name (parser
);
18956 /* And any specified attributes. */
18957 attribs
= cp_parser_attributes_opt (parser
);
18959 /* Update the symbol table. */
18960 if (namespace_bindings_p ())
18961 finish_namespace_using_directive (namespace_decl
, attribs
);
18963 finish_local_using_directive (namespace_decl
, attribs
);
18965 /* Look for the final `;'. */
18966 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18969 /* Parse an asm-definition.
18972 asm ( string-literal ) ;
18977 asm volatile [opt] ( string-literal ) ;
18978 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18979 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18980 : asm-operand-list [opt] ) ;
18981 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18982 : asm-operand-list [opt]
18983 : asm-clobber-list [opt] ) ;
18984 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18985 : asm-clobber-list [opt]
18986 : asm-goto-list ) ; */
18989 cp_parser_asm_definition (cp_parser
* parser
)
18992 tree outputs
= NULL_TREE
;
18993 tree inputs
= NULL_TREE
;
18994 tree clobbers
= NULL_TREE
;
18995 tree labels
= NULL_TREE
;
18997 bool volatile_p
= false;
18998 bool extended_p
= false;
18999 bool invalid_inputs_p
= false;
19000 bool invalid_outputs_p
= false;
19001 bool goto_p
= false;
19002 required_token missing
= RT_NONE
;
19004 /* Look for the `asm' keyword. */
19005 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19007 if (parser
->in_function_body
19008 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19010 error ("%<asm%> in %<constexpr%> function");
19011 cp_function_chain
->invalid_constexpr
= true;
19014 /* See if the next token is `volatile'. */
19015 if (cp_parser_allow_gnu_extensions_p (parser
)
19016 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19018 /* Remember that we saw the `volatile' keyword. */
19020 /* Consume the token. */
19021 cp_lexer_consume_token (parser
->lexer
);
19023 if (cp_parser_allow_gnu_extensions_p (parser
)
19024 && parser
->in_function_body
19025 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19027 /* Remember that we saw the `goto' keyword. */
19029 /* Consume the token. */
19030 cp_lexer_consume_token (parser
->lexer
);
19032 /* Look for the opening `('. */
19033 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19035 /* Look for the string. */
19036 string
= cp_parser_string_literal (parser
, false, false);
19037 if (string
== error_mark_node
)
19039 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19040 /*consume_paren=*/true);
19044 /* If we're allowing GNU extensions, check for the extended assembly
19045 syntax. Unfortunately, the `:' tokens need not be separated by
19046 a space in C, and so, for compatibility, we tolerate that here
19047 too. Doing that means that we have to treat the `::' operator as
19049 if (cp_parser_allow_gnu_extensions_p (parser
)
19050 && parser
->in_function_body
19051 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19052 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19054 bool inputs_p
= false;
19055 bool clobbers_p
= false;
19056 bool labels_p
= false;
19058 /* The extended syntax was used. */
19061 /* Look for outputs. */
19062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19064 /* Consume the `:'. */
19065 cp_lexer_consume_token (parser
->lexer
);
19066 /* Parse the output-operands. */
19067 if (cp_lexer_next_token_is_not (parser
->lexer
,
19069 && cp_lexer_next_token_is_not (parser
->lexer
,
19071 && cp_lexer_next_token_is_not (parser
->lexer
,
19075 outputs
= cp_parser_asm_operand_list (parser
);
19076 if (outputs
== error_mark_node
)
19077 invalid_outputs_p
= true;
19080 /* If the next token is `::', there are no outputs, and the
19081 next token is the beginning of the inputs. */
19082 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19083 /* The inputs are coming next. */
19086 /* Look for inputs. */
19088 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19090 /* Consume the `:' or `::'. */
19091 cp_lexer_consume_token (parser
->lexer
);
19092 /* Parse the output-operands. */
19093 if (cp_lexer_next_token_is_not (parser
->lexer
,
19095 && cp_lexer_next_token_is_not (parser
->lexer
,
19097 && cp_lexer_next_token_is_not (parser
->lexer
,
19100 inputs
= cp_parser_asm_operand_list (parser
);
19101 if (inputs
== error_mark_node
)
19102 invalid_inputs_p
= true;
19105 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19106 /* The clobbers are coming next. */
19109 /* Look for clobbers. */
19111 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19114 /* Consume the `:' or `::'. */
19115 cp_lexer_consume_token (parser
->lexer
);
19116 /* Parse the clobbers. */
19117 if (cp_lexer_next_token_is_not (parser
->lexer
,
19119 && cp_lexer_next_token_is_not (parser
->lexer
,
19121 clobbers
= cp_parser_asm_clobber_list (parser
);
19124 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19125 /* The labels are coming next. */
19128 /* Look for labels. */
19130 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19133 /* Consume the `:' or `::'. */
19134 cp_lexer_consume_token (parser
->lexer
);
19135 /* Parse the labels. */
19136 labels
= cp_parser_asm_label_list (parser
);
19139 if (goto_p
&& !labels_p
)
19140 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19143 missing
= RT_COLON_SCOPE
;
19145 /* Look for the closing `)'. */
19146 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19147 missing
? missing
: RT_CLOSE_PAREN
))
19148 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19149 /*consume_paren=*/true);
19150 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19152 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19154 /* Create the ASM_EXPR. */
19155 if (parser
->in_function_body
)
19157 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19158 inputs
, clobbers
, labels
);
19159 /* If the extended syntax was not used, mark the ASM_EXPR. */
19162 tree temp
= asm_stmt
;
19163 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19164 temp
= TREE_OPERAND (temp
, 0);
19166 ASM_INPUT_P (temp
) = 1;
19170 symtab
->finalize_toplevel_asm (string
);
19174 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19175 type that comes from the decl-specifier-seq. */
19178 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19180 for (cp_declarator
*d
= declarator
; d
;)
19190 if (TYPE_PTRMEMFUNC_P (type
))
19191 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19192 type
= TREE_TYPE (type
);
19200 /* Declarators [gram.dcl.decl] */
19202 /* Parse an init-declarator.
19205 declarator initializer [opt]
19210 declarator asm-specification [opt] attributes [opt] initializer [opt]
19212 function-definition:
19213 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19215 decl-specifier-seq [opt] declarator function-try-block
19219 function-definition:
19220 __extension__ function-definition
19224 function-definition:
19225 decl-specifier-seq [opt] declarator function-transaction-block
19227 The DECL_SPECIFIERS apply to this declarator. Returns a
19228 representation of the entity declared. If MEMBER_P is TRUE, then
19229 this declarator appears in a class scope. The new DECL created by
19230 this declarator is returned.
19232 The CHECKS are access checks that should be performed once we know
19233 what entity is being declared (and, therefore, what classes have
19236 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19237 for a function-definition here as well. If the declarator is a
19238 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19239 be TRUE upon return. By that point, the function-definition will
19240 have been completely parsed.
19242 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19245 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19246 parsed declaration if it is an uninitialized single declarator not followed
19247 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19248 if present, will not be consumed. If returned, this declarator will be
19249 created with SD_INITIALIZED but will not call cp_finish_decl.
19251 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19252 and there is an initializer, the pointed location_t is set to the
19253 location of the '=' or `(', or '{' in C++11 token introducing the
19257 cp_parser_init_declarator (cp_parser
* parser
,
19258 cp_decl_specifier_seq
*decl_specifiers
,
19259 vec
<deferred_access_check
, va_gc
> *checks
,
19260 bool function_definition_allowed_p
,
19262 int declares_class_or_enum
,
19263 bool* function_definition_p
,
19264 tree
* maybe_range_for_decl
,
19265 location_t
* init_loc
,
19268 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19269 *attributes_start_token
= NULL
;
19270 cp_declarator
*declarator
;
19271 tree prefix_attributes
;
19272 tree attributes
= NULL
;
19273 tree asm_specification
;
19275 tree decl
= NULL_TREE
;
19277 int is_initialized
;
19278 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19279 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19281 enum cpp_ttype initialization_kind
;
19282 bool is_direct_init
= false;
19283 bool is_non_constant_init
;
19284 int ctor_dtor_or_conv_p
;
19285 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19286 tree pushed_scope
= NULL_TREE
;
19287 bool range_for_decl_p
= false;
19288 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19289 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19291 /* Gather the attributes that were provided with the
19292 decl-specifiers. */
19293 prefix_attributes
= decl_specifiers
->attributes
;
19295 /* Assume that this is not the declarator for a function
19297 if (function_definition_p
)
19298 *function_definition_p
= false;
19300 /* Default arguments are only permitted for function parameters. */
19301 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19302 parser
->default_arg_ok_p
= false;
19304 /* Defer access checks while parsing the declarator; we cannot know
19305 what names are accessible until we know what is being
19307 resume_deferring_access_checks ();
19309 token
= cp_lexer_peek_token (parser
->lexer
);
19311 /* Parse the declarator. */
19313 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19314 &ctor_dtor_or_conv_p
,
19315 /*parenthesized_p=*/NULL
,
19316 member_p
, friend_p
);
19317 /* Gather up the deferred checks. */
19318 stop_deferring_access_checks ();
19320 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19322 /* If the DECLARATOR was erroneous, there's no need to go
19324 if (declarator
== cp_error_declarator
)
19325 return error_mark_node
;
19327 /* Check that the number of template-parameter-lists is OK. */
19328 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19330 return error_mark_node
;
19332 if (declares_class_or_enum
& 2)
19333 cp_parser_check_for_definition_in_return_type (declarator
,
19334 decl_specifiers
->type
,
19335 decl_specifiers
->locations
[ds_type_spec
]);
19337 /* Figure out what scope the entity declared by the DECLARATOR is
19338 located in. `grokdeclarator' sometimes changes the scope, so
19339 we compute it now. */
19340 scope
= get_scope_of_declarator (declarator
);
19342 /* Perform any lookups in the declared type which were thought to be
19343 dependent, but are not in the scope of the declarator. */
19344 decl_specifiers
->type
19345 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19347 /* If we're allowing GNU extensions, look for an
19348 asm-specification. */
19349 if (cp_parser_allow_gnu_extensions_p (parser
))
19351 /* Look for an asm-specification. */
19352 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19353 asm_specification
= cp_parser_asm_specification_opt (parser
);
19356 asm_specification
= NULL_TREE
;
19358 /* Look for attributes. */
19359 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19360 attributes
= cp_parser_attributes_opt (parser
);
19362 /* Peek at the next token. */
19363 token
= cp_lexer_peek_token (parser
->lexer
);
19365 bool bogus_implicit_tmpl
= false;
19367 if (function_declarator_p (declarator
))
19369 /* Handle C++17 deduction guides. */
19370 if (!decl_specifiers
->type
19371 && ctor_dtor_or_conv_p
<= 0
19372 && cxx_dialect
>= cxx17
)
19374 cp_declarator
*id
= get_id_declarator (declarator
);
19375 tree name
= id
->u
.id
.unqualified_name
;
19376 parser
->scope
= id
->u
.id
.qualifying_scope
;
19377 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19379 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19382 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19383 id
->u
.id
.sfk
= sfk_deduction_guide
;
19384 ctor_dtor_or_conv_p
= 1;
19388 /* Check to see if the token indicates the start of a
19389 function-definition. */
19390 if (cp_parser_token_starts_function_definition_p (token
))
19392 if (!function_definition_allowed_p
)
19394 /* If a function-definition should not appear here, issue an
19396 cp_parser_error (parser
,
19397 "a function-definition is not allowed here");
19398 return error_mark_node
;
19401 location_t func_brace_location
19402 = cp_lexer_peek_token (parser
->lexer
)->location
;
19404 /* Neither attributes nor an asm-specification are allowed
19405 on a function-definition. */
19406 if (asm_specification
)
19407 error_at (asm_spec_start_token
->location
,
19408 "an asm-specification is not allowed "
19409 "on a function-definition");
19411 error_at (attributes_start_token
->location
,
19412 "attributes are not allowed "
19413 "on a function-definition");
19414 /* This is a function-definition. */
19415 *function_definition_p
= true;
19417 /* Parse the function definition. */
19419 decl
= cp_parser_save_member_function_body (parser
,
19422 prefix_attributes
);
19425 (cp_parser_function_definition_from_specifiers_and_declarator
19426 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19428 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19430 /* This is where the prologue starts... */
19431 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19432 = func_brace_location
;
19438 else if (parser
->fully_implicit_function_template_p
)
19440 /* A non-template declaration involving a function parameter list
19441 containing an implicit template parameter will be made into a
19442 template. If the resulting declaration is not going to be an
19443 actual function then finish the template scope here to prevent it.
19444 An error message will be issued once we have a decl to talk about.
19446 FIXME probably we should do type deduction rather than create an
19447 implicit template, but the standard currently doesn't allow it. */
19448 bogus_implicit_tmpl
= true;
19449 finish_fully_implicit_template (parser
, NULL_TREE
);
19454 Only in function declarations for constructors, destructors, type
19455 conversions, and deduction guides can the decl-specifier-seq be omitted.
19457 We explicitly postpone this check past the point where we handle
19458 function-definitions because we tolerate function-definitions
19459 that are missing their return types in some modes. */
19460 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19462 cp_parser_error (parser
,
19463 "expected constructor, destructor, or type conversion");
19464 return error_mark_node
;
19467 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19468 if (token
->type
== CPP_EQ
19469 || token
->type
== CPP_OPEN_PAREN
19470 || token
->type
== CPP_OPEN_BRACE
)
19472 is_initialized
= SD_INITIALIZED
;
19473 initialization_kind
= token
->type
;
19474 if (maybe_range_for_decl
)
19475 *maybe_range_for_decl
= error_mark_node
;
19476 tmp_init_loc
= token
->location
;
19477 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19478 *init_loc
= tmp_init_loc
;
19480 if (token
->type
== CPP_EQ
19481 && function_declarator_p (declarator
))
19483 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19484 if (t2
->keyword
== RID_DEFAULT
)
19485 is_initialized
= SD_DEFAULTED
;
19486 else if (t2
->keyword
== RID_DELETE
)
19487 is_initialized
= SD_DELETED
;
19492 /* If the init-declarator isn't initialized and isn't followed by a
19493 `,' or `;', it's not a valid init-declarator. */
19494 if (token
->type
!= CPP_COMMA
19495 && token
->type
!= CPP_SEMICOLON
)
19497 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19498 range_for_decl_p
= true;
19501 if (!maybe_range_for_decl
)
19502 cp_parser_error (parser
, "expected initializer");
19503 return error_mark_node
;
19506 is_initialized
= SD_UNINITIALIZED
;
19507 initialization_kind
= CPP_EOF
;
19510 /* Because start_decl has side-effects, we should only call it if we
19511 know we're going ahead. By this point, we know that we cannot
19512 possibly be looking at any other construct. */
19513 cp_parser_commit_to_tentative_parse (parser
);
19515 /* Enter the newly declared entry in the symbol table. If we're
19516 processing a declaration in a class-specifier, we wait until
19517 after processing the initializer. */
19520 if (parser
->in_unbraced_linkage_specification_p
)
19521 decl_specifiers
->storage_class
= sc_extern
;
19522 decl
= start_decl (declarator
, decl_specifiers
,
19523 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19524 attributes
, prefix_attributes
, &pushed_scope
);
19525 cp_finalize_omp_declare_simd (parser
, decl
);
19526 cp_finalize_oacc_routine (parser
, decl
, false);
19527 /* Adjust location of decl if declarator->id_loc is more appropriate:
19528 set, and decl wasn't merged with another decl, in which case its
19529 location would be different from input_location, and more accurate. */
19531 && declarator
->id_loc
!= UNKNOWN_LOCATION
19532 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19533 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19536 /* Enter the SCOPE. That way unqualified names appearing in the
19537 initializer will be looked up in SCOPE. */
19538 pushed_scope
= push_scope (scope
);
19540 /* Perform deferred access control checks, now that we know in which
19541 SCOPE the declared entity resides. */
19542 if (!member_p
&& decl
)
19544 tree saved_current_function_decl
= NULL_TREE
;
19546 /* If the entity being declared is a function, pretend that we
19547 are in its scope. If it is a `friend', it may have access to
19548 things that would not otherwise be accessible. */
19549 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19551 saved_current_function_decl
= current_function_decl
;
19552 current_function_decl
= decl
;
19555 /* Perform access checks for template parameters. */
19556 cp_parser_perform_template_parameter_access_checks (checks
);
19558 /* Perform the access control checks for the declarator and the
19559 decl-specifiers. */
19560 perform_deferred_access_checks (tf_warning_or_error
);
19562 /* Restore the saved value. */
19563 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19564 current_function_decl
= saved_current_function_decl
;
19567 /* Parse the initializer. */
19568 initializer
= NULL_TREE
;
19569 is_direct_init
= false;
19570 is_non_constant_init
= true;
19571 if (is_initialized
)
19573 if (function_declarator_p (declarator
))
19575 if (initialization_kind
== CPP_EQ
)
19576 initializer
= cp_parser_pure_specifier (parser
);
19579 /* If the declaration was erroneous, we don't really
19580 know what the user intended, so just silently
19581 consume the initializer. */
19582 if (decl
!= error_mark_node
)
19583 error_at (tmp_init_loc
, "initializer provided for function");
19584 cp_parser_skip_to_closing_parenthesis (parser
,
19585 /*recovering=*/true,
19586 /*or_comma=*/false,
19587 /*consume_paren=*/true);
19592 /* We want to record the extra mangling scope for in-class
19593 initializers of class members and initializers of static data
19594 member templates. The former involves deferring
19595 parsing of the initializer until end of class as with default
19596 arguments. So right here we only handle the latter. */
19597 if (!member_p
&& processing_template_decl
)
19598 start_lambda_scope (decl
);
19599 initializer
= cp_parser_initializer (parser
,
19601 &is_non_constant_init
);
19602 if (!member_p
&& processing_template_decl
)
19603 finish_lambda_scope ();
19604 if (initializer
== error_mark_node
)
19605 cp_parser_skip_to_end_of_statement (parser
);
19609 /* The old parser allows attributes to appear after a parenthesized
19610 initializer. Mark Mitchell proposed removing this functionality
19611 on the GCC mailing lists on 2002-08-13. This parser accepts the
19612 attributes -- but ignores them. */
19613 if (cp_parser_allow_gnu_extensions_p (parser
)
19614 && initialization_kind
== CPP_OPEN_PAREN
)
19615 if (cp_parser_attributes_opt (parser
))
19616 warning (OPT_Wattributes
,
19617 "attributes after parenthesized initializer ignored");
19619 /* And now complain about a non-function implicit template. */
19620 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19621 error_at (DECL_SOURCE_LOCATION (decl
),
19622 "non-function %qD declared as implicit template", decl
);
19624 /* For an in-class declaration, use `grokfield' to create the
19630 pop_scope (pushed_scope
);
19631 pushed_scope
= NULL_TREE
;
19633 decl
= grokfield (declarator
, decl_specifiers
,
19634 initializer
, !is_non_constant_init
,
19635 /*asmspec=*/NULL_TREE
,
19636 chainon (attributes
, prefix_attributes
));
19637 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19638 cp_parser_save_default_args (parser
, decl
);
19639 cp_finalize_omp_declare_simd (parser
, decl
);
19640 cp_finalize_oacc_routine (parser
, decl
, false);
19643 /* Finish processing the declaration. But, skip member
19645 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19647 cp_finish_decl (decl
,
19648 initializer
, !is_non_constant_init
,
19650 /* If the initializer is in parentheses, then this is
19651 a direct-initialization, which means that an
19652 `explicit' constructor is OK. Otherwise, an
19653 `explicit' constructor cannot be used. */
19654 ((is_direct_init
|| !is_initialized
)
19655 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19657 else if ((cxx_dialect
!= cxx98
) && friend_p
19658 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19659 /* Core issue #226 (C++0x only): A default template-argument
19660 shall not be specified in a friend class template
19662 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19663 /*is_partial=*/false, /*is_friend_decl=*/1);
19665 if (!friend_p
&& pushed_scope
)
19666 pop_scope (pushed_scope
);
19668 if (function_declarator_p (declarator
)
19669 && parser
->fully_implicit_function_template_p
)
19672 decl
= finish_fully_implicit_template (parser
, decl
);
19674 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19677 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19678 && type_uses_auto (decl_specifiers
->type
))
19679 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19684 /* Parse a declarator.
19688 ptr-operator declarator
19690 abstract-declarator:
19691 ptr-operator abstract-declarator [opt]
19692 direct-abstract-declarator
19697 attributes [opt] direct-declarator
19698 attributes [opt] ptr-operator declarator
19700 abstract-declarator:
19701 attributes [opt] ptr-operator abstract-declarator [opt]
19702 attributes [opt] direct-abstract-declarator
19704 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19705 detect constructors, destructors, deduction guides, or conversion operators.
19706 It is set to -1 if the declarator is a name, and +1 if it is a
19707 function. Otherwise it is set to zero. Usually you just want to
19708 test for >0, but internally the negative value is used.
19710 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19711 a decl-specifier-seq unless it declares a constructor, destructor,
19712 or conversion. It might seem that we could check this condition in
19713 semantic analysis, rather than parsing, but that makes it difficult
19714 to handle something like `f()'. We want to notice that there are
19715 no decl-specifiers, and therefore realize that this is an
19716 expression, not a declaration.)
19718 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19719 the declarator is a direct-declarator of the form "(...)".
19721 MEMBER_P is true iff this declarator is a member-declarator.
19723 FRIEND_P is true iff this declarator is a friend. */
19725 static cp_declarator
*
19726 cp_parser_declarator (cp_parser
* parser
,
19727 cp_parser_declarator_kind dcl_kind
,
19728 int* ctor_dtor_or_conv_p
,
19729 bool* parenthesized_p
,
19730 bool member_p
, bool friend_p
)
19732 cp_declarator
*declarator
;
19733 enum tree_code code
;
19734 cp_cv_quals cv_quals
;
19736 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19738 /* Assume this is not a constructor, destructor, or type-conversion
19740 if (ctor_dtor_or_conv_p
)
19741 *ctor_dtor_or_conv_p
= 0;
19743 if (cp_parser_allow_gnu_extensions_p (parser
))
19744 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19746 /* Check for the ptr-operator production. */
19747 cp_parser_parse_tentatively (parser
);
19748 /* Parse the ptr-operator. */
19749 code
= cp_parser_ptr_operator (parser
,
19754 /* If that worked, then we have a ptr-operator. */
19755 if (cp_parser_parse_definitely (parser
))
19757 /* If a ptr-operator was found, then this declarator was not
19759 if (parenthesized_p
)
19760 *parenthesized_p
= true;
19761 /* The dependent declarator is optional if we are parsing an
19762 abstract-declarator. */
19763 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19764 cp_parser_parse_tentatively (parser
);
19766 /* Parse the dependent declarator. */
19767 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19768 /*ctor_dtor_or_conv_p=*/NULL
,
19769 /*parenthesized_p=*/NULL
,
19770 /*member_p=*/false,
19773 /* If we are parsing an abstract-declarator, we must handle the
19774 case where the dependent declarator is absent. */
19775 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19776 && !cp_parser_parse_definitely (parser
))
19779 declarator
= cp_parser_make_indirect_declarator
19780 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19782 /* Everything else is a direct-declarator. */
19785 if (parenthesized_p
)
19786 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19788 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19789 ctor_dtor_or_conv_p
,
19790 member_p
, friend_p
);
19793 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19794 declarator
->attributes
= gnu_attributes
;
19798 /* Parse a direct-declarator or direct-abstract-declarator.
19802 direct-declarator ( parameter-declaration-clause )
19803 cv-qualifier-seq [opt]
19804 ref-qualifier [opt]
19805 exception-specification [opt]
19806 direct-declarator [ constant-expression [opt] ]
19809 direct-abstract-declarator:
19810 direct-abstract-declarator [opt]
19811 ( parameter-declaration-clause )
19812 cv-qualifier-seq [opt]
19813 ref-qualifier [opt]
19814 exception-specification [opt]
19815 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19816 ( abstract-declarator )
19818 Returns a representation of the declarator. DCL_KIND is
19819 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19820 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19821 we are parsing a direct-declarator. It is
19822 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19823 of ambiguity we prefer an abstract declarator, as per
19824 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19825 as for cp_parser_declarator. */
19827 static cp_declarator
*
19828 cp_parser_direct_declarator (cp_parser
* parser
,
19829 cp_parser_declarator_kind dcl_kind
,
19830 int* ctor_dtor_or_conv_p
,
19831 bool member_p
, bool friend_p
)
19834 cp_declarator
*declarator
= NULL
;
19835 tree scope
= NULL_TREE
;
19836 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19837 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19839 tree pushed_scope
= NULL_TREE
;
19840 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19844 /* Peek at the next token. */
19845 token
= cp_lexer_peek_token (parser
->lexer
);
19846 if (token
->type
== CPP_OPEN_PAREN
)
19848 /* This is either a parameter-declaration-clause, or a
19849 parenthesized declarator. When we know we are parsing a
19850 named declarator, it must be a parenthesized declarator
19851 if FIRST is true. For instance, `(int)' is a
19852 parameter-declaration-clause, with an omitted
19853 direct-abstract-declarator. But `((*))', is a
19854 parenthesized abstract declarator. Finally, when T is a
19855 template parameter `(T)' is a
19856 parameter-declaration-clause, and not a parenthesized
19859 We first try and parse a parameter-declaration-clause,
19860 and then try a nested declarator (if FIRST is true).
19862 It is not an error for it not to be a
19863 parameter-declaration-clause, even when FIRST is
19869 The first is the declaration of a function while the
19870 second is the definition of a variable, including its
19873 Having seen only the parenthesis, we cannot know which of
19874 these two alternatives should be selected. Even more
19875 complex are examples like:
19880 The former is a function-declaration; the latter is a
19881 variable initialization.
19883 Thus again, we try a parameter-declaration-clause, and if
19884 that fails, we back out and return. */
19886 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19889 bool is_declarator
= false;
19893 /* In a member-declarator, the only valid interpretation
19894 of a parenthesis is the start of a
19895 parameter-declaration-clause. (It is invalid to
19896 initialize a static data member with a parenthesized
19897 initializer; only the "=" form of initialization is
19900 cp_parser_parse_tentatively (parser
);
19902 /* Consume the `('. */
19903 matching_parens parens
;
19904 parens
.consume_open (parser
);
19907 /* If this is going to be an abstract declarator, we're
19908 in a declarator and we can't have default args. */
19909 parser
->default_arg_ok_p
= false;
19910 parser
->in_declarator_p
= true;
19913 begin_scope (sk_function_parms
, NULL_TREE
);
19915 /* Parse the parameter-declaration-clause. */
19916 params
= cp_parser_parameter_declaration_clause (parser
);
19918 /* Consume the `)'. */
19919 parens
.require_close (parser
);
19921 /* If all went well, parse the cv-qualifier-seq,
19922 ref-qualifier and the exception-specification. */
19923 if (member_p
|| cp_parser_parse_definitely (parser
))
19925 cp_cv_quals cv_quals
;
19926 cp_virt_specifiers virt_specifiers
;
19927 cp_ref_qualifier ref_qual
;
19928 tree exception_specification
;
19931 bool memfn
= (member_p
|| (pushed_scope
19932 && CLASS_TYPE_P (pushed_scope
)));
19934 is_declarator
= true;
19936 if (ctor_dtor_or_conv_p
)
19937 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
19940 /* Parse the cv-qualifier-seq. */
19941 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19942 /* Parse the ref-qualifier. */
19943 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
19944 /* Parse the tx-qualifier. */
19945 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
19946 /* And the exception-specification. */
19947 exception_specification
19948 = cp_parser_exception_specification_opt (parser
);
19950 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19952 /* In here, we handle cases where attribute is used after
19953 the function declaration. For example:
19954 void func (int x) __attribute__((vector(..))); */
19955 tree gnu_attrs
= NULL_TREE
;
19956 tree requires_clause
= NULL_TREE
;
19957 late_return
= (cp_parser_late_return_type_opt
19958 (parser
, declarator
, requires_clause
,
19959 memfn
? cv_quals
: -1));
19961 /* Parse the virt-specifier-seq. */
19962 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19964 /* Create the function-declarator. */
19965 declarator
= make_call_declarator (declarator
,
19971 exception_specification
,
19974 declarator
->std_attributes
= attrs
;
19975 declarator
->attributes
= gnu_attrs
;
19976 /* Any subsequent parameter lists are to do with
19977 return type, so are not those of the declared
19979 parser
->default_arg_ok_p
= false;
19982 /* Remove the function parms from scope. */
19983 pop_bindings_and_leave_scope ();
19986 /* Repeat the main loop. */
19990 /* If this is the first, we can try a parenthesized
19994 bool saved_in_type_id_in_expr_p
;
19996 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19997 parser
->in_declarator_p
= saved_in_declarator_p
;
19999 open_paren
= token
;
20000 /* Consume the `('. */
20001 matching_parens parens
;
20002 parens
.consume_open (parser
);
20003 /* Parse the nested declarator. */
20004 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20005 parser
->in_type_id_in_expr_p
= true;
20007 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20008 /*parenthesized_p=*/NULL
,
20009 member_p
, friend_p
);
20010 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20012 /* Expect a `)'. */
20013 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20014 if (!parens
.require_close (parser
))
20015 declarator
= cp_error_declarator
;
20016 if (declarator
== cp_error_declarator
)
20019 goto handle_declarator
;
20021 /* Otherwise, we must be done. */
20025 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20026 && token
->type
== CPP_OPEN_SQUARE
20027 && !cp_next_tokens_can_be_attribute_p (parser
))
20029 /* Parse an array-declarator. */
20030 tree bounds
, attrs
;
20032 if (ctor_dtor_or_conv_p
)
20033 *ctor_dtor_or_conv_p
= 0;
20037 parser
->default_arg_ok_p
= false;
20038 parser
->in_declarator_p
= true;
20039 /* Consume the `['. */
20040 cp_lexer_consume_token (parser
->lexer
);
20041 /* Peek at the next token. */
20042 token
= cp_lexer_peek_token (parser
->lexer
);
20043 /* If the next token is `]', then there is no
20044 constant-expression. */
20045 if (token
->type
!= CPP_CLOSE_SQUARE
)
20047 bool non_constant_p
;
20049 = cp_parser_constant_expression (parser
,
20050 /*allow_non_constant=*/true,
20052 if (!non_constant_p
)
20054 else if (error_operand_p (bounds
))
20055 /* Already gave an error. */;
20056 else if (!parser
->in_function_body
20057 || current_binding_level
->kind
== sk_function_parms
)
20059 /* Normally, the array bound must be an integral constant
20060 expression. However, as an extension, we allow VLAs
20061 in function scopes as long as they aren't part of a
20062 parameter declaration. */
20063 cp_parser_error (parser
,
20064 "array bound is not an integer constant");
20065 bounds
= error_mark_node
;
20067 else if (processing_template_decl
20068 && !type_dependent_expression_p (bounds
))
20070 /* Remember this wasn't a constant-expression. */
20071 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20072 TREE_SIDE_EFFECTS (bounds
) = 1;
20076 bounds
= NULL_TREE
;
20077 /* Look for the closing `]'. */
20078 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20080 declarator
= cp_error_declarator
;
20084 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20085 declarator
= make_array_declarator (declarator
, bounds
);
20086 declarator
->std_attributes
= attrs
;
20088 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20091 tree qualifying_scope
;
20092 tree unqualified_name
;
20094 special_function_kind sfk
;
20096 bool pack_expansion_p
= false;
20097 cp_token
*declarator_id_start_token
;
20099 /* Parse a declarator-id */
20100 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20103 cp_parser_parse_tentatively (parser
);
20105 /* If we see an ellipsis, we should be looking at a
20107 if (token
->type
== CPP_ELLIPSIS
)
20109 /* Consume the `...' */
20110 cp_lexer_consume_token (parser
->lexer
);
20112 pack_expansion_p
= true;
20116 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20118 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20119 qualifying_scope
= parser
->scope
;
20124 if (!unqualified_name
&& pack_expansion_p
)
20126 /* Check whether an error occurred. */
20127 okay
= !cp_parser_error_occurred (parser
);
20129 /* We already consumed the ellipsis to mark a
20130 parameter pack, but we have no way to report it,
20131 so abort the tentative parse. We will be exiting
20132 immediately anyway. */
20133 cp_parser_abort_tentative_parse (parser
);
20136 okay
= cp_parser_parse_definitely (parser
);
20139 unqualified_name
= error_mark_node
;
20140 else if (unqualified_name
20141 && (qualifying_scope
20142 || (!identifier_p (unqualified_name
))))
20144 cp_parser_error (parser
, "expected unqualified-id");
20145 unqualified_name
= error_mark_node
;
20149 if (!unqualified_name
)
20151 if (unqualified_name
== error_mark_node
)
20153 declarator
= cp_error_declarator
;
20154 pack_expansion_p
= false;
20155 declarator
->parameter_pack_p
= false;
20159 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20161 if (qualifying_scope
&& at_namespace_scope_p ()
20162 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20164 /* In the declaration of a member of a template class
20165 outside of the class itself, the SCOPE will sometimes
20166 be a TYPENAME_TYPE. For example, given:
20168 template <typename T>
20169 int S<T>::R::i = 3;
20171 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20172 this context, we must resolve S<T>::R to an ordinary
20173 type, rather than a typename type.
20175 The reason we normally avoid resolving TYPENAME_TYPEs
20176 is that a specialization of `S' might render
20177 `S<T>::R' not a type. However, if `S' is
20178 specialized, then this `i' will not be used, so there
20179 is no harm in resolving the types here. */
20182 /* Resolve the TYPENAME_TYPE. */
20183 type
= resolve_typename_type (qualifying_scope
,
20184 /*only_current_p=*/false);
20185 /* If that failed, the declarator is invalid. */
20186 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20188 if (typedef_variant_p (type
))
20189 error_at (declarator_id_start_token
->location
,
20190 "cannot define member of dependent typedef "
20193 error_at (declarator_id_start_token
->location
,
20194 "%<%T::%E%> is not a type",
20195 TYPE_CONTEXT (qualifying_scope
),
20196 TYPE_IDENTIFIER (qualifying_scope
));
20198 qualifying_scope
= type
;
20203 if (unqualified_name
)
20207 if (qualifying_scope
20208 && CLASS_TYPE_P (qualifying_scope
))
20209 class_type
= qualifying_scope
;
20211 class_type
= current_class_type
;
20213 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20215 tree name_type
= TREE_TYPE (unqualified_name
);
20217 if (!class_type
|| !same_type_p (name_type
, class_type
))
20219 /* We do not attempt to print the declarator
20220 here because we do not have enough
20221 information about its original syntactic
20223 cp_parser_error (parser
, "invalid declarator");
20224 declarator
= cp_error_declarator
;
20227 else if (qualifying_scope
20228 && CLASSTYPE_USE_TEMPLATE (name_type
))
20230 error_at (declarator_id_start_token
->location
,
20231 "invalid use of constructor as a template");
20232 inform (declarator_id_start_token
->location
,
20233 "use %<%T::%D%> instead of %<%T::%D%> to "
20234 "name the constructor in a qualified name",
20236 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20237 class_type
, name_type
);
20238 declarator
= cp_error_declarator
;
20241 unqualified_name
= constructor_name (class_type
);
20246 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20247 sfk
= sfk_destructor
;
20248 else if (identifier_p (unqualified_name
)
20249 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20250 sfk
= sfk_conversion
;
20251 else if (/* There's no way to declare a constructor
20252 for an unnamed type, even if the type
20253 got a name for linkage purposes. */
20254 !TYPE_WAS_UNNAMED (class_type
)
20255 /* Handle correctly (c++/19200):
20269 friend void N::S();
20271 && (!friend_p
|| class_type
== qualifying_scope
)
20272 && constructor_name_p (unqualified_name
,
20274 sfk
= sfk_constructor
;
20275 else if (is_overloaded_fn (unqualified_name
)
20276 && DECL_CONSTRUCTOR_P (get_first_fn
20277 (unqualified_name
)))
20278 sfk
= sfk_constructor
;
20280 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20281 *ctor_dtor_or_conv_p
= -1;
20284 declarator
= make_id_declarator (qualifying_scope
,
20287 declarator
->std_attributes
= attrs
;
20288 declarator
->id_loc
= token
->location
;
20289 declarator
->parameter_pack_p
= pack_expansion_p
;
20291 if (pack_expansion_p
)
20292 maybe_warn_variadic_templates ();
20295 handle_declarator
:;
20296 scope
= get_scope_of_declarator (declarator
);
20299 /* Any names that appear after the declarator-id for a
20300 member are looked up in the containing scope. */
20301 if (at_function_scope_p ())
20303 /* But declarations with qualified-ids can't appear in a
20305 cp_parser_error (parser
, "qualified-id in declaration");
20306 declarator
= cp_error_declarator
;
20309 pushed_scope
= push_scope (scope
);
20311 parser
->in_declarator_p
= true;
20312 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20313 || (declarator
&& declarator
->kind
== cdk_id
))
20314 /* Default args are only allowed on function
20316 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20318 parser
->default_arg_ok_p
= false;
20327 /* For an abstract declarator, we might wind up with nothing at this
20328 point. That's an error; the declarator is not optional. */
20330 cp_parser_error (parser
, "expected declarator");
20331 else if (open_paren
)
20333 /* Record overly parenthesized declarator so we can give a
20334 diagnostic about confusing decl/expr disambiguation. */
20335 if (declarator
->kind
== cdk_array
)
20337 /* If the open and close parens are on different lines, this
20338 is probably a formatting thing, so ignore. */
20339 expanded_location open
= expand_location (open_paren
->location
);
20340 expanded_location close
= expand_location (close_paren
->location
);
20341 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20345 declarator
->parenthesized
= open_paren
->location
;
20348 /* If we entered a scope, we must exit it now. */
20350 pop_scope (pushed_scope
);
20352 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20353 parser
->in_declarator_p
= saved_in_declarator_p
;
20358 /* Parse a ptr-operator.
20361 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20362 * cv-qualifier-seq [opt]
20364 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20365 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20370 & cv-qualifier-seq [opt]
20372 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20373 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20374 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20375 filled in with the TYPE containing the member. *CV_QUALS is
20376 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20377 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20378 Note that the tree codes returned by this function have nothing
20379 to do with the types of trees that will be eventually be created
20380 to represent the pointer or reference type being parsed. They are
20381 just constants with suggestive names. */
20382 static enum tree_code
20383 cp_parser_ptr_operator (cp_parser
* parser
,
20385 cp_cv_quals
*cv_quals
,
20388 enum tree_code code
= ERROR_MARK
;
20390 tree attrs
= NULL_TREE
;
20392 /* Assume that it's not a pointer-to-member. */
20394 /* And that there are no cv-qualifiers. */
20395 *cv_quals
= TYPE_UNQUALIFIED
;
20397 /* Peek at the next token. */
20398 token
= cp_lexer_peek_token (parser
->lexer
);
20400 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20401 if (token
->type
== CPP_MULT
)
20402 code
= INDIRECT_REF
;
20403 else if (token
->type
== CPP_AND
)
20405 else if ((cxx_dialect
!= cxx98
) &&
20406 token
->type
== CPP_AND_AND
) /* C++0x only */
20407 code
= NON_LVALUE_EXPR
;
20409 if (code
!= ERROR_MARK
)
20411 /* Consume the `*', `&' or `&&'. */
20412 cp_lexer_consume_token (parser
->lexer
);
20414 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20415 `&', if we are allowing GNU extensions. (The only qualifier
20416 that can legally appear after `&' is `restrict', but that is
20417 enforced during semantic analysis. */
20418 if (code
== INDIRECT_REF
20419 || cp_parser_allow_gnu_extensions_p (parser
))
20420 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20422 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20423 if (attributes
!= NULL
)
20424 *attributes
= attrs
;
20428 /* Try the pointer-to-member case. */
20429 cp_parser_parse_tentatively (parser
);
20430 /* Look for the optional `::' operator. */
20431 cp_parser_global_scope_opt (parser
,
20432 /*current_scope_valid_p=*/false);
20433 /* Look for the nested-name specifier. */
20434 token
= cp_lexer_peek_token (parser
->lexer
);
20435 cp_parser_nested_name_specifier (parser
,
20436 /*typename_keyword_p=*/false,
20437 /*check_dependency_p=*/true,
20439 /*is_declaration=*/false);
20440 /* If we found it, and the next token is a `*', then we are
20441 indeed looking at a pointer-to-member operator. */
20442 if (!cp_parser_error_occurred (parser
)
20443 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20445 /* Indicate that the `*' operator was used. */
20446 code
= INDIRECT_REF
;
20448 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20449 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20450 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20451 error_at (token
->location
, "cannot form pointer to member of "
20452 "non-class %q#T", parser
->scope
);
20455 /* The type of which the member is a member is given by the
20457 *type
= parser
->scope
;
20458 /* The next name will not be qualified. */
20459 parser
->scope
= NULL_TREE
;
20460 parser
->qualifying_scope
= NULL_TREE
;
20461 parser
->object_scope
= NULL_TREE
;
20462 /* Look for optional c++11 attributes. */
20463 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20464 if (attributes
!= NULL
)
20465 *attributes
= attrs
;
20466 /* Look for the optional cv-qualifier-seq. */
20467 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20470 /* If that didn't work we don't have a ptr-operator. */
20471 if (!cp_parser_parse_definitely (parser
))
20472 cp_parser_error (parser
, "expected ptr-operator");
20478 /* Parse an (optional) cv-qualifier-seq.
20481 cv-qualifier cv-qualifier-seq [opt]
20492 Returns a bitmask representing the cv-qualifiers. */
20495 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20497 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20502 cp_cv_quals cv_qualifier
;
20504 /* Peek at the next token. */
20505 token
= cp_lexer_peek_token (parser
->lexer
);
20506 /* See if it's a cv-qualifier. */
20507 switch (token
->keyword
)
20510 cv_qualifier
= TYPE_QUAL_CONST
;
20514 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20518 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20522 cv_qualifier
= TYPE_UNQUALIFIED
;
20529 if (cv_quals
& cv_qualifier
)
20531 gcc_rich_location
richloc (token
->location
);
20532 richloc
.add_fixit_remove ();
20533 error_at (&richloc
, "duplicate cv-qualifier");
20534 cp_lexer_purge_token (parser
->lexer
);
20538 cp_lexer_consume_token (parser
->lexer
);
20539 cv_quals
|= cv_qualifier
;
20546 /* Parse an (optional) ref-qualifier
20552 Returns cp_ref_qualifier representing ref-qualifier. */
20554 static cp_ref_qualifier
20555 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20557 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20559 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20560 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20565 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20566 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20568 switch (token
->type
)
20571 curr_ref_qual
= REF_QUAL_LVALUE
;
20575 curr_ref_qual
= REF_QUAL_RVALUE
;
20579 curr_ref_qual
= REF_QUAL_NONE
;
20583 if (!curr_ref_qual
)
20587 error_at (token
->location
, "multiple ref-qualifiers");
20588 cp_lexer_purge_token (parser
->lexer
);
20592 ref_qual
= curr_ref_qual
;
20593 cp_lexer_consume_token (parser
->lexer
);
20600 /* Parse an optional tx-qualifier.
20604 transaction_safe_dynamic */
20607 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20610 if (token
->type
== CPP_NAME
)
20612 tree name
= token
->u
.value
;
20613 const char *p
= IDENTIFIER_POINTER (name
);
20614 const int len
= strlen ("transaction_safe");
20615 if (!strncmp (p
, "transaction_safe", len
))
20619 || !strcmp (p
, "_dynamic"))
20621 cp_lexer_consume_token (parser
->lexer
);
20624 error ("%qE requires %<-fgnu-tm%>", name
);
20635 /* Parse an (optional) virt-specifier-seq.
20637 virt-specifier-seq:
20638 virt-specifier virt-specifier-seq [opt]
20644 Returns a bitmask representing the virt-specifiers. */
20646 static cp_virt_specifiers
20647 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20649 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20654 cp_virt_specifiers virt_specifier
;
20656 /* Peek at the next token. */
20657 token
= cp_lexer_peek_token (parser
->lexer
);
20658 /* See if it's a virt-specifier-qualifier. */
20659 if (token
->type
!= CPP_NAME
)
20661 if (id_equal (token
->u
.value
, "override"))
20663 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20664 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20666 else if (id_equal (token
->u
.value
, "final"))
20668 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20669 virt_specifier
= VIRT_SPEC_FINAL
;
20671 else if (id_equal (token
->u
.value
, "__final"))
20673 virt_specifier
= VIRT_SPEC_FINAL
;
20678 if (virt_specifiers
& virt_specifier
)
20680 gcc_rich_location
richloc (token
->location
);
20681 richloc
.add_fixit_remove ();
20682 error_at (&richloc
, "duplicate virt-specifier");
20683 cp_lexer_purge_token (parser
->lexer
);
20687 cp_lexer_consume_token (parser
->lexer
);
20688 virt_specifiers
|= virt_specifier
;
20691 return virt_specifiers
;
20694 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20695 is in scope even though it isn't real. */
20698 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20702 if (current_class_ptr
)
20704 /* We don't clear this between NSDMIs. Is it already what we want? */
20705 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20706 if (DECL_P (current_class_ptr
)
20707 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20708 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20709 && cp_type_quals (type
) == quals
)
20713 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20715 current_class_ptr
= NULL_TREE
;
20717 = cp_build_fold_indirect_ref (this_parm
);
20718 current_class_ptr
= this_parm
;
20721 /* Return true iff our current scope is a non-static data member
20725 parsing_nsdmi (void)
20727 /* We recognize NSDMI context by the context-less 'this' pointer set up
20728 by the function above. */
20729 if (current_class_ptr
20730 && TREE_CODE (current_class_ptr
) == PARM_DECL
20731 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20736 /* Parse a late-specified return type, if any. This is not a separate
20737 non-terminal, but part of a function declarator, which looks like
20739 -> trailing-type-specifier-seq abstract-declarator(opt)
20741 Returns the type indicated by the type-id.
20743 In addition to this, parse any queued up #pragma omp declare simd
20744 clauses, and #pragma acc routine clauses.
20746 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20750 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20751 tree
& requires_clause
, cp_cv_quals quals
)
20754 tree type
= NULL_TREE
;
20755 bool declare_simd_p
= (parser
->omp_declare_simd
20757 && declarator
->kind
== cdk_id
);
20759 bool oacc_routine_p
= (parser
->oacc_routine
20761 && declarator
->kind
== cdk_id
);
20763 /* Peek at the next token. */
20764 token
= cp_lexer_peek_token (parser
->lexer
);
20765 /* A late-specified return type is indicated by an initial '->'. */
20766 if (token
->type
!= CPP_DEREF
20767 && token
->keyword
!= RID_REQUIRES
20768 && !(token
->type
== CPP_NAME
20769 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20770 && !(declare_simd_p
|| oacc_routine_p
))
20773 tree save_ccp
= current_class_ptr
;
20774 tree save_ccr
= current_class_ref
;
20777 /* DR 1207: 'this' is in scope in the trailing return type. */
20778 inject_this_parameter (current_class_type
, quals
);
20781 if (token
->type
== CPP_DEREF
)
20783 /* Consume the ->. */
20784 cp_lexer_consume_token (parser
->lexer
);
20786 type
= cp_parser_trailing_type_id (parser
);
20789 /* Function declarations may be followed by a trailing
20790 requires-clause. */
20791 requires_clause
= cp_parser_requires_clause_opt (parser
);
20793 if (declare_simd_p
)
20794 declarator
->attributes
20795 = cp_parser_late_parsing_omp_declare_simd (parser
,
20796 declarator
->attributes
);
20797 if (oacc_routine_p
)
20798 declarator
->attributes
20799 = cp_parser_late_parsing_oacc_routine (parser
,
20800 declarator
->attributes
);
20804 current_class_ptr
= save_ccp
;
20805 current_class_ref
= save_ccr
;
20811 /* Parse a declarator-id.
20815 :: [opt] nested-name-specifier [opt] type-name
20817 In the `id-expression' case, the value returned is as for
20818 cp_parser_id_expression if the id-expression was an unqualified-id.
20819 If the id-expression was a qualified-id, then a SCOPE_REF is
20820 returned. The first operand is the scope (either a NAMESPACE_DECL
20821 or TREE_TYPE), but the second is still just a representation of an
20825 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20828 /* The expression must be an id-expression. Assume that qualified
20829 names are the names of types so that:
20832 int S<T>::R::i = 3;
20834 will work; we must treat `S<T>::R' as the name of a type.
20835 Similarly, assume that qualified names are templates, where
20839 int S<T>::R<T>::i = 3;
20842 id
= cp_parser_id_expression (parser
,
20843 /*template_keyword_p=*/false,
20844 /*check_dependency_p=*/false,
20845 /*template_p=*/NULL
,
20846 /*declarator_p=*/true,
20848 if (id
&& BASELINK_P (id
))
20849 id
= BASELINK_FUNCTIONS (id
);
20853 /* Parse a type-id.
20856 type-specifier-seq abstract-declarator [opt]
20858 Returns the TYPE specified. */
20861 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20862 bool is_trailing_return
)
20864 cp_decl_specifier_seq type_specifier_seq
;
20865 cp_declarator
*abstract_declarator
;
20867 /* Parse the type-specifier-seq. */
20868 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20869 is_trailing_return
,
20870 &type_specifier_seq
);
20871 if (is_template_arg
&& type_specifier_seq
.type
20872 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20873 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20874 /* A bare template name as a template argument is a template template
20875 argument, not a placeholder, so fail parsing it as a type argument. */
20877 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20878 cp_parser_simulate_error (parser
);
20879 return error_mark_node
;
20881 if (type_specifier_seq
.type
== error_mark_node
)
20882 return error_mark_node
;
20884 /* There might or might not be an abstract declarator. */
20885 cp_parser_parse_tentatively (parser
);
20886 /* Look for the declarator. */
20887 abstract_declarator
20888 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20889 /*parenthesized_p=*/NULL
,
20890 /*member_p=*/false,
20891 /*friend_p=*/false);
20892 /* Check to see if there really was a declarator. */
20893 if (!cp_parser_parse_definitely (parser
))
20894 abstract_declarator
= NULL
;
20896 if (type_specifier_seq
.type
20897 /* The concepts TS allows 'auto' as a type-id. */
20898 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20899 /* None of the valid uses of 'auto' in C++14 involve the type-id
20900 nonterminal, but it is valid in a trailing-return-type. */
20901 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20902 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20904 /* A type-id with type 'auto' is only ok if the abstract declarator
20905 is a function declarator with a late-specified return type.
20907 A type-id with 'auto' is also valid in a trailing-return-type
20908 in a compound-requirement. */
20909 if (abstract_declarator
20910 && abstract_declarator
->kind
== cdk_function
20911 && abstract_declarator
->u
.function
.late_return_type
)
20913 else if (parser
->in_result_type_constraint_p
)
20917 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
20918 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
20920 error_at (loc
, "missing template arguments after %qT",
20922 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
20926 error_at (loc
, "invalid use of %qT", auto_node
);
20927 return error_mark_node
;
20931 return groktypename (&type_specifier_seq
, abstract_declarator
,
20936 cp_parser_type_id (cp_parser
*parser
)
20938 return cp_parser_type_id_1 (parser
, false, false);
20942 cp_parser_template_type_arg (cp_parser
*parser
)
20945 const char *saved_message
= parser
->type_definition_forbidden_message
;
20946 parser
->type_definition_forbidden_message
20947 = G_("types may not be defined in template arguments");
20948 r
= cp_parser_type_id_1 (parser
, true, false);
20949 parser
->type_definition_forbidden_message
= saved_message
;
20950 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
20952 error ("invalid use of %<auto%> in template argument");
20953 r
= error_mark_node
;
20959 cp_parser_trailing_type_id (cp_parser
*parser
)
20961 return cp_parser_type_id_1 (parser
, false, true);
20964 /* Parse a type-specifier-seq.
20966 type-specifier-seq:
20967 type-specifier type-specifier-seq [opt]
20971 type-specifier-seq:
20972 attributes type-specifier-seq [opt]
20974 If IS_DECLARATION is true, we are at the start of a "condition" or
20975 exception-declaration, so we might be followed by a declarator-id.
20977 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20978 i.e. we've just seen "->".
20980 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20983 cp_parser_type_specifier_seq (cp_parser
* parser
,
20984 bool is_declaration
,
20985 bool is_trailing_return
,
20986 cp_decl_specifier_seq
*type_specifier_seq
)
20988 bool seen_type_specifier
= false;
20989 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20990 cp_token
*start_token
= NULL
;
20992 /* Clear the TYPE_SPECIFIER_SEQ. */
20993 clear_decl_specs (type_specifier_seq
);
20995 /* In the context of a trailing return type, enum E { } is an
20996 elaborated-type-specifier followed by a function-body, not an
20998 if (is_trailing_return
)
20999 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21001 /* Parse the type-specifiers and attributes. */
21004 tree type_specifier
;
21005 bool is_cv_qualifier
;
21007 /* Check for attributes first. */
21008 if (cp_next_tokens_can_be_attribute_p (parser
))
21010 type_specifier_seq
->attributes
=
21011 chainon (type_specifier_seq
->attributes
,
21012 cp_parser_attributes_opt (parser
));
21016 /* record the token of the beginning of the type specifier seq,
21017 for error reporting purposes*/
21019 start_token
= cp_lexer_peek_token (parser
->lexer
);
21021 /* Look for the type-specifier. */
21022 type_specifier
= cp_parser_type_specifier (parser
,
21024 type_specifier_seq
,
21025 /*is_declaration=*/false,
21028 if (!type_specifier
)
21030 /* If the first type-specifier could not be found, this is not a
21031 type-specifier-seq at all. */
21032 if (!seen_type_specifier
)
21034 /* Set in_declarator_p to avoid skipping to the semicolon. */
21035 int in_decl
= parser
->in_declarator_p
;
21036 parser
->in_declarator_p
= true;
21038 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21039 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21040 cp_parser_error (parser
, "expected type-specifier");
21042 parser
->in_declarator_p
= in_decl
;
21044 type_specifier_seq
->type
= error_mark_node
;
21047 /* If subsequent type-specifiers could not be found, the
21048 type-specifier-seq is complete. */
21052 seen_type_specifier
= true;
21053 /* The standard says that a condition can be:
21055 type-specifier-seq declarator = assignment-expression
21062 we should treat the "S" as a declarator, not as a
21063 type-specifier. The standard doesn't say that explicitly for
21064 type-specifier-seq, but it does say that for
21065 decl-specifier-seq in an ordinary declaration. Perhaps it
21066 would be clearer just to allow a decl-specifier-seq here, and
21067 then add a semantic restriction that if any decl-specifiers
21068 that are not type-specifiers appear, the program is invalid. */
21069 if (is_declaration
&& !is_cv_qualifier
)
21070 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21074 /* Return whether the function currently being declared has an associated
21075 template parameter list. */
21078 function_being_declared_is_template_p (cp_parser
* parser
)
21080 if (!current_template_parms
|| processing_template_parmlist
)
21083 if (parser
->implicit_template_scope
)
21086 if (at_class_scope_p ()
21087 && TYPE_BEING_DEFINED (current_class_type
))
21088 return parser
->num_template_parameter_lists
!= 0;
21090 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21091 (current_class_type
));
21094 /* Parse a parameter-declaration-clause.
21096 parameter-declaration-clause:
21097 parameter-declaration-list [opt] ... [opt]
21098 parameter-declaration-list , ...
21100 Returns a representation for the parameter declarations. A return
21101 value of NULL indicates a parameter-declaration-clause consisting
21102 only of an ellipsis. */
21105 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21114 int auto_is_implicit_function_template_parm_p
;
21116 parser
->auto_is_implicit_function_template_parm_p
21117 = auto_is_implicit_function_template_parm_p
;
21119 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
21123 if (!processing_specialization
21124 && !processing_template_parmlist
21125 && !processing_explicit_instantiation
)
21126 if (!current_function_decl
21127 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21128 parser
->auto_is_implicit_function_template_parm_p
= true;
21130 /* Peek at the next token. */
21131 token
= cp_lexer_peek_token (parser
->lexer
);
21132 /* Check for trivial parameter-declaration-clauses. */
21133 if (token
->type
== CPP_ELLIPSIS
)
21135 /* Consume the `...' token. */
21136 cp_lexer_consume_token (parser
->lexer
);
21139 else if (token
->type
== CPP_CLOSE_PAREN
)
21140 /* There are no parameters. */
21142 #ifndef NO_IMPLICIT_EXTERN_C
21143 if (in_system_header_at (input_location
)
21144 && current_class_type
== NULL
21145 && current_lang_name
== lang_name_c
)
21149 return void_list_node
;
21151 /* Check for `(void)', too, which is a special case. */
21152 else if (token
->keyword
== RID_VOID
21153 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21154 == CPP_CLOSE_PAREN
))
21156 /* Consume the `void' token. */
21157 cp_lexer_consume_token (parser
->lexer
);
21158 /* There are no parameters. */
21159 return void_list_node
;
21162 /* Parse the parameter-declaration-list. */
21163 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21164 /* If a parse error occurred while parsing the
21165 parameter-declaration-list, then the entire
21166 parameter-declaration-clause is erroneous. */
21170 /* Peek at the next token. */
21171 token
= cp_lexer_peek_token (parser
->lexer
);
21172 /* If it's a `,', the clause should terminate with an ellipsis. */
21173 if (token
->type
== CPP_COMMA
)
21175 /* Consume the `,'. */
21176 cp_lexer_consume_token (parser
->lexer
);
21177 /* Expect an ellipsis. */
21179 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21181 /* It might also be `...' if the optional trailing `,' was
21183 else if (token
->type
== CPP_ELLIPSIS
)
21185 /* Consume the `...' token. */
21186 cp_lexer_consume_token (parser
->lexer
);
21187 /* And remember that we saw it. */
21191 ellipsis_p
= false;
21193 /* Finish the parameter list. */
21195 parameters
= chainon (parameters
, void_list_node
);
21200 /* Parse a parameter-declaration-list.
21202 parameter-declaration-list:
21203 parameter-declaration
21204 parameter-declaration-list , parameter-declaration
21206 Returns a representation of the parameter-declaration-list, as for
21207 cp_parser_parameter_declaration_clause. However, the
21208 `void_list_node' is never appended to the list. Upon return,
21209 *IS_ERROR will be true iff an error occurred. */
21212 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21214 tree parameters
= NULL_TREE
;
21215 tree
*tail
= ¶meters
;
21216 bool saved_in_unbraced_linkage_specification_p
;
21219 /* Assume all will go well. */
21221 /* The special considerations that apply to a function within an
21222 unbraced linkage specifications do not apply to the parameters
21223 to the function. */
21224 saved_in_unbraced_linkage_specification_p
21225 = parser
->in_unbraced_linkage_specification_p
;
21226 parser
->in_unbraced_linkage_specification_p
= false;
21228 /* Look for more parameters. */
21231 cp_parameter_declarator
*parameter
;
21232 tree decl
= error_mark_node
;
21233 bool parenthesized_p
= false;
21234 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21235 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21236 (current_template_parms
)) : 0);
21238 /* Parse the parameter. */
21240 = cp_parser_parameter_declaration (parser
,
21241 /*template_parm_p=*/false,
21244 /* We don't know yet if the enclosing context is deprecated, so wait
21245 and warn in grokparms if appropriate. */
21246 deprecated_state
= DEPRECATED_SUPPRESS
;
21250 /* If a function parameter pack was specified and an implicit template
21251 parameter was introduced during cp_parser_parameter_declaration,
21252 change any implicit parameters introduced into packs. */
21253 if (parser
->implicit_template_parms
21254 && parameter
->declarator
21255 && parameter
->declarator
->parameter_pack_p
)
21257 int latest_template_parm_idx
= TREE_VEC_LENGTH
21258 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21260 if (latest_template_parm_idx
!= template_parm_idx
)
21261 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21262 (parameter
->decl_specifiers
.type
,
21263 template_parm_idx
, latest_template_parm_idx
);
21266 decl
= grokdeclarator (parameter
->declarator
,
21267 ¶meter
->decl_specifiers
,
21269 parameter
->default_argument
!= NULL_TREE
,
21270 ¶meter
->decl_specifiers
.attributes
);
21271 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21272 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21275 deprecated_state
= DEPRECATED_NORMAL
;
21277 /* If a parse error occurred parsing the parameter declaration,
21278 then the entire parameter-declaration-list is erroneous. */
21279 if (decl
== error_mark_node
)
21282 parameters
= error_mark_node
;
21286 if (parameter
->decl_specifiers
.attributes
)
21287 cplus_decl_attributes (&decl
,
21288 parameter
->decl_specifiers
.attributes
,
21290 if (DECL_NAME (decl
))
21291 decl
= pushdecl (decl
);
21293 if (decl
!= error_mark_node
)
21295 retrofit_lang_decl (decl
);
21296 DECL_PARM_INDEX (decl
) = ++index
;
21297 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21300 /* Add the new parameter to the list. */
21301 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21302 tail
= &TREE_CHAIN (*tail
);
21304 /* Peek at the next token. */
21305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21306 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21307 /* These are for Objective-C++ */
21308 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21309 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21310 /* The parameter-declaration-list is complete. */
21312 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21316 /* Peek at the next token. */
21317 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21318 /* If it's an ellipsis, then the list is complete. */
21319 if (token
->type
== CPP_ELLIPSIS
)
21321 /* Otherwise, there must be more parameters. Consume the
21323 cp_lexer_consume_token (parser
->lexer
);
21324 /* When parsing something like:
21326 int i(float f, double d)
21328 we can tell after seeing the declaration for "f" that we
21329 are not looking at an initialization of a variable "i",
21330 but rather at the declaration of a function "i".
21332 Due to the fact that the parsing of template arguments
21333 (as specified to a template-id) requires backtracking we
21334 cannot use this technique when inside a template argument
21336 if (!parser
->in_template_argument_list_p
21337 && !parser
->in_type_id_in_expr_p
21338 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21339 /* However, a parameter-declaration of the form
21340 "float(f)" (which is a valid declaration of a
21341 parameter "f") can also be interpreted as an
21342 expression (the conversion of "f" to "float"). */
21343 && !parenthesized_p
)
21344 cp_parser_commit_to_tentative_parse (parser
);
21348 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21349 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21350 cp_parser_skip_to_closing_parenthesis (parser
,
21351 /*recovering=*/true,
21352 /*or_comma=*/false,
21353 /*consume_paren=*/false);
21358 parser
->in_unbraced_linkage_specification_p
21359 = saved_in_unbraced_linkage_specification_p
;
21361 /* Reset implicit_template_scope if we are about to leave the function
21362 parameter list that introduced it. Note that for out-of-line member
21363 definitions, there will be one or more class scopes before we get to
21364 the template parameter scope. */
21366 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21367 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21369 while (maybe_its
->kind
== sk_class
)
21370 maybe_its
= maybe_its
->level_chain
;
21371 if (maybe_its
== its
)
21373 parser
->implicit_template_parms
= 0;
21374 parser
->implicit_template_scope
= 0;
21381 /* Parse a parameter declaration.
21383 parameter-declaration:
21384 decl-specifier-seq ... [opt] declarator
21385 decl-specifier-seq declarator = assignment-expression
21386 decl-specifier-seq ... [opt] abstract-declarator [opt]
21387 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21389 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21390 declares a template parameter. (In that case, a non-nested `>'
21391 token encountered during the parsing of the assignment-expression
21392 is not interpreted as a greater-than operator.)
21394 Returns a representation of the parameter, or NULL if an error
21395 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21396 true iff the declarator is of the form "(p)". */
21398 static cp_parameter_declarator
*
21399 cp_parser_parameter_declaration (cp_parser
*parser
,
21400 bool template_parm_p
,
21401 bool *parenthesized_p
)
21403 int declares_class_or_enum
;
21404 cp_decl_specifier_seq decl_specifiers
;
21405 cp_declarator
*declarator
;
21406 tree default_argument
;
21407 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21408 const char *saved_message
;
21409 bool template_parameter_pack_p
= false;
21411 /* In a template parameter, `>' is not an operator.
21415 When parsing a default template-argument for a non-type
21416 template-parameter, the first non-nested `>' is taken as the end
21417 of the template parameter-list rather than a greater-than
21420 /* Type definitions may not appear in parameter types. */
21421 saved_message
= parser
->type_definition_forbidden_message
;
21422 parser
->type_definition_forbidden_message
21423 = G_("types may not be defined in parameter types");
21425 /* Parse the declaration-specifiers. */
21426 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21427 cp_parser_decl_specifier_seq (parser
,
21428 CP_PARSER_FLAGS_NONE
,
21430 &declares_class_or_enum
);
21432 /* Complain about missing 'typename' or other invalid type names. */
21433 if (!decl_specifiers
.any_type_specifiers_p
21434 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21435 decl_specifiers
.type
= error_mark_node
;
21437 /* If an error occurred, there's no reason to attempt to parse the
21438 rest of the declaration. */
21439 if (cp_parser_error_occurred (parser
))
21441 parser
->type_definition_forbidden_message
= saved_message
;
21445 /* Peek at the next token. */
21446 token
= cp_lexer_peek_token (parser
->lexer
);
21448 /* If the next token is a `)', `,', `=', `>', or `...', then there
21449 is no declarator. However, when variadic templates are enabled,
21450 there may be a declarator following `...'. */
21451 if (token
->type
== CPP_CLOSE_PAREN
21452 || token
->type
== CPP_COMMA
21453 || token
->type
== CPP_EQ
21454 || token
->type
== CPP_GREATER
)
21457 if (parenthesized_p
)
21458 *parenthesized_p
= false;
21460 /* Otherwise, there should be a declarator. */
21463 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21464 parser
->default_arg_ok_p
= false;
21466 /* After seeing a decl-specifier-seq, if the next token is not a
21467 "(", there is no possibility that the code is a valid
21468 expression. Therefore, if parsing tentatively, we commit at
21470 if (!parser
->in_template_argument_list_p
21471 /* In an expression context, having seen:
21475 we cannot be sure whether we are looking at a
21476 function-type (taking a "char" as a parameter) or a cast
21477 of some object of type "char" to "int". */
21478 && !parser
->in_type_id_in_expr_p
21479 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21480 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21481 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21482 cp_parser_commit_to_tentative_parse (parser
);
21483 /* Parse the declarator. */
21484 declarator_token_start
= token
;
21485 declarator
= cp_parser_declarator (parser
,
21486 CP_PARSER_DECLARATOR_EITHER
,
21487 /*ctor_dtor_or_conv_p=*/NULL
,
21489 /*member_p=*/false,
21490 /*friend_p=*/false);
21491 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21492 /* After the declarator, allow more attributes. */
21493 decl_specifiers
.attributes
21494 = chainon (decl_specifiers
.attributes
,
21495 cp_parser_attributes_opt (parser
));
21497 /* If the declarator is a template parameter pack, remember that and
21498 clear the flag in the declarator itself so we don't get errors
21499 from grokdeclarator. */
21500 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21502 declarator
->parameter_pack_p
= false;
21503 template_parameter_pack_p
= true;
21507 /* If the next token is an ellipsis, and we have not seen a declarator
21508 name, and if either the type of the declarator contains parameter
21509 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21510 for, eg, abbreviated integral type names), then we actually have a
21511 parameter pack expansion expression. Otherwise, leave the ellipsis
21512 for a C-style variadic function. */
21513 token
= cp_lexer_peek_token (parser
->lexer
);
21514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21516 tree type
= decl_specifiers
.type
;
21518 if (type
&& DECL_P (type
))
21519 type
= TREE_TYPE (type
);
21522 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21523 && (template_parm_p
|| uses_parameter_packs (type
)))
21524 || (!type
&& template_parm_p
))
21525 && declarator_can_be_parameter_pack (declarator
))
21527 /* Consume the `...'. */
21528 cp_lexer_consume_token (parser
->lexer
);
21529 maybe_warn_variadic_templates ();
21531 /* Build a pack expansion type */
21532 if (template_parm_p
)
21533 template_parameter_pack_p
= true;
21534 else if (declarator
)
21535 declarator
->parameter_pack_p
= true;
21537 decl_specifiers
.type
= make_pack_expansion (type
);
21541 /* The restriction on defining new types applies only to the type
21542 of the parameter, not to the default argument. */
21543 parser
->type_definition_forbidden_message
= saved_message
;
21545 /* If the next token is `=', then process a default argument. */
21546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21548 tree type
= decl_specifiers
.type
;
21549 token
= cp_lexer_peek_token (parser
->lexer
);
21550 /* If we are defining a class, then the tokens that make up the
21551 default argument must be saved and processed later. */
21552 if (!template_parm_p
&& at_class_scope_p ()
21553 && TYPE_BEING_DEFINED (current_class_type
)
21554 && !LAMBDA_TYPE_P (current_class_type
))
21555 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21557 // A constrained-type-specifier may declare a type template-parameter.
21558 else if (declares_constrained_type_template_parameter (type
))
21560 = cp_parser_default_type_template_argument (parser
);
21562 // A constrained-type-specifier may declare a template-template-parameter.
21563 else if (declares_constrained_template_template_parameter (type
))
21565 = cp_parser_default_template_template_argument (parser
);
21567 /* Outside of a class definition, we can just parse the
21568 assignment-expression. */
21571 = cp_parser_default_argument (parser
, template_parm_p
);
21573 if (!parser
->default_arg_ok_p
)
21575 permerror (token
->location
,
21576 "default arguments are only "
21577 "permitted for function parameters");
21579 else if ((declarator
&& declarator
->parameter_pack_p
)
21580 || template_parameter_pack_p
21581 || (decl_specifiers
.type
21582 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21584 /* Find the name of the parameter pack. */
21585 cp_declarator
*id_declarator
= declarator
;
21586 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21587 id_declarator
= id_declarator
->declarator
;
21589 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21590 error_at (declarator_token_start
->location
,
21592 ? G_("template parameter pack %qD "
21593 "cannot have a default argument")
21594 : G_("parameter pack %qD cannot have "
21595 "a default argument"),
21596 id_declarator
->u
.id
.unqualified_name
);
21598 error_at (declarator_token_start
->location
,
21600 ? G_("template parameter pack cannot have "
21601 "a default argument")
21602 : G_("parameter pack cannot have a "
21603 "default argument"));
21605 default_argument
= NULL_TREE
;
21609 default_argument
= NULL_TREE
;
21611 /* Generate a location for the parameter, ranging from the start of the
21612 initial token to the end of the final token (using input_location for
21613 the latter, set up by cp_lexer_set_source_position_from_token when
21616 If we have a identifier, then use it for the caret location, e.g.
21618 extern int callee (int one, int (*two)(int, int), float three);
21619 ~~~~~~^~~~~~~~~~~~~~
21621 otherwise, reuse the start location for the caret location e.g.:
21623 extern int callee (int one, int (*)(int, int), float three);
21627 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21628 ? declarator
->id_loc
21629 : decl_spec_token_start
->location
);
21630 location_t param_loc
= make_location (caret_loc
,
21631 decl_spec_token_start
->location
,
21634 return make_parameter_declarator (&decl_specifiers
,
21638 template_parameter_pack_p
);
21641 /* Parse a default argument and return it.
21643 TEMPLATE_PARM_P is true if this is a default argument for a
21644 non-type template parameter. */
21646 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21648 tree default_argument
= NULL_TREE
;
21649 bool saved_greater_than_is_operator_p
;
21650 bool saved_local_variables_forbidden_p
;
21651 bool non_constant_p
, is_direct_init
;
21653 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21655 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21656 parser
->greater_than_is_operator_p
= !template_parm_p
;
21657 /* Local variable names (and the `this' keyword) may not
21658 appear in a default argument. */
21659 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21660 parser
->local_variables_forbidden_p
= true;
21661 /* Parse the assignment-expression. */
21662 if (template_parm_p
)
21663 push_deferring_access_checks (dk_no_deferred
);
21664 tree saved_class_ptr
= NULL_TREE
;
21665 tree saved_class_ref
= NULL_TREE
;
21666 /* The "this" pointer is not valid in a default argument. */
21669 saved_class_ptr
= current_class_ptr
;
21670 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21671 saved_class_ref
= current_class_ref
;
21672 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21675 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21676 /* Restore the "this" pointer. */
21679 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21680 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21682 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21683 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21684 if (template_parm_p
)
21685 pop_deferring_access_checks ();
21686 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21687 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21689 return default_argument
;
21692 /* Parse a function-body.
21695 compound_statement */
21698 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21700 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21701 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21705 /* Parse a ctor-initializer-opt followed by a function-body. Return
21706 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21707 is true we are parsing a function-try-block. */
21710 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21711 bool in_function_try_block
)
21714 const bool check_body_p
=
21715 DECL_CONSTRUCTOR_P (current_function_decl
)
21716 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21719 /* Begin the function body. */
21720 body
= begin_function_body ();
21721 /* Parse the optional ctor-initializer. */
21722 cp_parser_ctor_initializer_opt (parser
);
21724 /* If we're parsing a constexpr constructor definition, we need
21725 to check that the constructor body is indeed empty. However,
21726 before we get to cp_parser_function_body lot of junk has been
21727 generated, so we can't just check that we have an empty block.
21728 Rather we take a snapshot of the outermost block, and check whether
21729 cp_parser_function_body changed its state. */
21732 list
= cur_stmt_list
;
21733 if (STATEMENT_LIST_TAIL (list
))
21734 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21736 /* Parse the function-body. */
21737 cp_parser_function_body (parser
, in_function_try_block
);
21739 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21740 /* Finish the function body. */
21741 finish_function_body (body
);
21744 /* Parse an initializer.
21747 = initializer-clause
21748 ( expression-list )
21750 Returns an expression representing the initializer. If no
21751 initializer is present, NULL_TREE is returned.
21753 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21754 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21755 set to TRUE if there is no initializer present. If there is an
21756 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21757 is set to true; otherwise it is set to false. */
21760 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21761 bool* non_constant_p
)
21766 /* Peek at the next token. */
21767 token
= cp_lexer_peek_token (parser
->lexer
);
21769 /* Let our caller know whether or not this initializer was
21771 *is_direct_init
= (token
->type
!= CPP_EQ
);
21772 /* Assume that the initializer is constant. */
21773 *non_constant_p
= false;
21775 if (token
->type
== CPP_EQ
)
21777 /* Consume the `='. */
21778 cp_lexer_consume_token (parser
->lexer
);
21779 /* Parse the initializer-clause. */
21780 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21782 else if (token
->type
== CPP_OPEN_PAREN
)
21784 vec
<tree
, va_gc
> *vec
;
21785 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21787 /*allow_expansion_p=*/true,
21790 return error_mark_node
;
21791 init
= build_tree_list_vec (vec
);
21792 release_tree_vector (vec
);
21794 else if (token
->type
== CPP_OPEN_BRACE
)
21796 cp_lexer_set_source_position (parser
->lexer
);
21797 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21798 init
= cp_parser_braced_list (parser
, non_constant_p
);
21799 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21803 /* Anything else is an error. */
21804 cp_parser_error (parser
, "expected initializer");
21805 init
= error_mark_node
;
21808 if (check_for_bare_parameter_packs (init
))
21809 init
= error_mark_node
;
21814 /* Parse an initializer-clause.
21816 initializer-clause:
21817 assignment-expression
21820 Returns an expression representing the initializer.
21822 If the `assignment-expression' production is used the value
21823 returned is simply a representation for the expression.
21825 Otherwise, calls cp_parser_braced_list. */
21828 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21830 cp_expr initializer
;
21832 /* Assume the expression is constant. */
21833 *non_constant_p
= false;
21835 /* If it is not a `{', then we are looking at an
21836 assignment-expression. */
21837 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21840 = cp_parser_constant_expression (parser
,
21841 /*allow_non_constant_p=*/true,
21845 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21847 return initializer
;
21850 /* Parse a brace-enclosed initializer list.
21853 { initializer-list , [opt] }
21854 { designated-initializer-list , [opt] }
21857 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21858 the elements of the initializer-list (or NULL, if the last
21859 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21860 NULL_TREE. There is no way to detect whether or not the optional
21861 trailing `,' was provided. NON_CONSTANT_P is as for
21862 cp_parser_initializer. */
21865 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21868 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21870 /* Consume the `{' token. */
21871 matching_braces braces
;
21872 braces
.consume_open (parser
);
21873 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21874 initializer
= make_node (CONSTRUCTOR
);
21875 /* If it's not a `}', then there is a non-trivial initializer. */
21876 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21878 /* Parse the initializer list. */
21879 CONSTRUCTOR_ELTS (initializer
)
21880 = cp_parser_initializer_list (parser
, non_constant_p
);
21881 /* A trailing `,' token is allowed. */
21882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21883 cp_lexer_consume_token (parser
->lexer
);
21886 *non_constant_p
= false;
21887 /* Now, there should be a trailing `}'. */
21888 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21889 braces
.require_close (parser
);
21890 TREE_TYPE (initializer
) = init_list_type_node
;
21892 cp_expr
result (initializer
);
21893 /* Build a location of the form:
21896 with caret==start at the open brace, finish at the close brace. */
21897 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21898 result
.set_location (combined_loc
);
21902 /* Consume tokens up to, and including, the next non-nested closing `]'.
21903 Returns true iff we found a closing `]'. */
21906 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21908 unsigned square_depth
= 0;
21912 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
21914 switch (token
->type
)
21917 case CPP_PRAGMA_EOL
:
21918 /* If we've run out of tokens, then there is no closing `]'. */
21921 case CPP_OPEN_SQUARE
:
21925 case CPP_CLOSE_SQUARE
:
21926 if (!square_depth
--)
21928 cp_lexer_consume_token (parser
->lexer
);
21937 /* Consume the token. */
21938 cp_lexer_consume_token (parser
->lexer
);
21942 /* Return true if we are looking at an array-designator, false otherwise. */
21945 cp_parser_array_designator_p (cp_parser
*parser
)
21947 /* Consume the `['. */
21948 cp_lexer_consume_token (parser
->lexer
);
21950 cp_lexer_save_tokens (parser
->lexer
);
21952 /* Skip tokens until the next token is a closing square bracket.
21953 If we find the closing `]', and the next token is a `=', then
21954 we are looking at an array designator. */
21955 bool array_designator_p
21956 = (cp_parser_skip_to_closing_square_bracket (parser
)
21957 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
21959 /* Roll back the tokens we skipped. */
21960 cp_lexer_rollback_tokens (parser
->lexer
);
21962 return array_designator_p
;
21965 /* Parse an initializer-list.
21968 initializer-clause ... [opt]
21969 initializer-list , initializer-clause ... [opt]
21973 designated-initializer-list:
21974 designated-initializer-clause
21975 designated-initializer-list , designated-initializer-clause
21977 designated-initializer-clause:
21978 designator brace-or-equal-initializer
21986 designation initializer-clause ...[opt]
21987 initializer-list , designation initializer-clause ...[opt]
21992 [ constant-expression ] =
21994 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21995 for the initializer. If the INDEX of the elt is non-NULL, it is the
21996 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21997 as for cp_parser_initializer. */
21999 static vec
<constructor_elt
, va_gc
> *
22000 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22002 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22003 bool first_p
= true;
22004 tree first_designator
= NULL_TREE
;
22006 /* Assume all of the expressions are constant. */
22007 *non_constant_p
= false;
22009 /* Parse the rest of the list. */
22015 bool clause_non_constant_p
;
22016 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22018 /* Handle the C++2A syntax, '. id ='. */
22019 if ((cxx_dialect
>= cxx2a
22020 || cp_parser_allow_gnu_extensions_p (parser
))
22021 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22022 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22023 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22024 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22025 == CPP_OPEN_BRACE
)))
22027 if (cxx_dialect
< cxx2a
)
22028 pedwarn (loc
, OPT_Wpedantic
,
22029 "C++ designated initializers only available with "
22030 "-std=c++2a or -std=gnu++2a");
22031 /* Consume the `.'. */
22032 cp_lexer_consume_token (parser
->lexer
);
22033 /* Consume the identifier. */
22034 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22036 /* Consume the `='. */
22037 cp_lexer_consume_token (parser
->lexer
);
22039 /* Also, if the next token is an identifier and the following one is a
22040 colon, we are looking at the GNU designated-initializer
22042 else if (cp_parser_allow_gnu_extensions_p (parser
)
22043 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22044 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22047 /* Warn the user that they are using an extension. */
22048 pedwarn (loc
, OPT_Wpedantic
,
22049 "ISO C++ does not allow GNU designated initializers");
22050 /* Consume the identifier. */
22051 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22052 /* Consume the `:'. */
22053 cp_lexer_consume_token (parser
->lexer
);
22055 /* Also handle C99 array designators, '[ const ] ='. */
22056 else if (cp_parser_allow_gnu_extensions_p (parser
)
22057 && !c_dialect_objc ()
22058 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22060 /* In C++11, [ could start a lambda-introducer. */
22061 bool non_const
= false;
22063 cp_parser_parse_tentatively (parser
);
22065 if (!cp_parser_array_designator_p (parser
))
22067 cp_parser_simulate_error (parser
);
22068 designator
= NULL_TREE
;
22072 designator
= cp_parser_constant_expression (parser
, true,
22074 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22075 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22078 if (!cp_parser_parse_definitely (parser
))
22079 designator
= NULL_TREE
;
22081 && (!require_potential_rvalue_constant_expression
22083 designator
= NULL_TREE
;
22085 /* Warn the user that they are using an extension. */
22086 pedwarn (loc
, OPT_Wpedantic
,
22087 "ISO C++ does not allow C99 designated initializers");
22090 designator
= NULL_TREE
;
22094 first_designator
= designator
;
22097 else if (cxx_dialect
>= cxx2a
22098 && first_designator
!= error_mark_node
22099 && (!first_designator
!= !designator
))
22101 error_at (loc
, "either all initializer clauses should be designated "
22102 "or none of them should be");
22103 first_designator
= error_mark_node
;
22105 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22106 first_designator
= designator
;
22108 /* Parse the initializer. */
22109 initializer
= cp_parser_initializer_clause (parser
,
22110 &clause_non_constant_p
);
22111 /* If any clause is non-constant, so is the entire initializer. */
22112 if (clause_non_constant_p
)
22113 *non_constant_p
= true;
22115 /* If we have an ellipsis, this is an initializer pack
22117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22119 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22121 /* Consume the `...'. */
22122 cp_lexer_consume_token (parser
->lexer
);
22124 if (designator
&& cxx_dialect
>= cxx2a
)
22126 "%<...%> not allowed in designated initializer list");
22128 /* Turn the initializer into an initializer expansion. */
22129 initializer
= make_pack_expansion (initializer
);
22132 /* Add it to the vector. */
22133 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22135 /* If the next token is not a comma, we have reached the end of
22137 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22140 /* Peek at the next token. */
22141 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22142 /* If the next token is a `}', then we're still done. An
22143 initializer-clause can have a trailing `,' after the
22144 initializer-list and before the closing `}'. */
22145 if (token
->type
== CPP_CLOSE_BRACE
)
22148 /* Consume the `,' token. */
22149 cp_lexer_consume_token (parser
->lexer
);
22152 /* The same identifier shall not appear in multiple designators
22153 of a designated-initializer-list. */
22154 if (first_designator
)
22157 tree designator
, val
;
22158 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22159 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22161 if (IDENTIFIER_MARKED (designator
))
22163 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22164 "%<.%s%> designator used multiple times in "
22165 "the same initializer list",
22166 IDENTIFIER_POINTER (designator
));
22167 (*v
)[i
].index
= NULL_TREE
;
22170 IDENTIFIER_MARKED (designator
) = 1;
22172 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22173 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22174 IDENTIFIER_MARKED (designator
) = 0;
22180 /* Classes [gram.class] */
22182 /* Parse a class-name.
22188 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22189 to indicate that names looked up in dependent types should be
22190 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22191 keyword has been used to indicate that the name that appears next
22192 is a template. TAG_TYPE indicates the explicit tag given before
22193 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22194 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22195 is the class being defined in a class-head. If ENUM_OK is TRUE,
22196 enum-names are also accepted.
22198 Returns the TYPE_DECL representing the class. */
22201 cp_parser_class_name (cp_parser
*parser
,
22202 bool typename_keyword_p
,
22203 bool template_keyword_p
,
22204 enum tag_types tag_type
,
22205 bool check_dependency_p
,
22207 bool is_declaration
,
22214 tree identifier
= NULL_TREE
;
22216 /* All class-names start with an identifier. */
22217 token
= cp_lexer_peek_token (parser
->lexer
);
22218 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22220 cp_parser_error (parser
, "expected class-name");
22221 return error_mark_node
;
22224 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22225 to a template-id, so we save it here. */
22226 scope
= parser
->scope
;
22227 if (scope
== error_mark_node
)
22228 return error_mark_node
;
22230 /* Any name names a type if we're following the `typename' keyword
22231 in a qualified name where the enclosing scope is type-dependent. */
22232 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22233 && dependent_type_p (scope
));
22234 /* Handle the common case (an identifier, but not a template-id)
22236 if (token
->type
== CPP_NAME
22237 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22239 cp_token
*identifier_token
;
22242 /* Look for the identifier. */
22243 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22244 ambiguous_p
= identifier_token
->error_reported
;
22245 identifier
= cp_parser_identifier (parser
);
22246 /* If the next token isn't an identifier, we are certainly not
22247 looking at a class-name. */
22248 if (identifier
== error_mark_node
)
22249 decl
= error_mark_node
;
22250 /* If we know this is a type-name, there's no need to look it
22252 else if (typename_p
)
22256 tree ambiguous_decls
;
22257 /* If we already know that this lookup is ambiguous, then
22258 we've already issued an error message; there's no reason
22262 cp_parser_simulate_error (parser
);
22263 return error_mark_node
;
22265 /* If the next token is a `::', then the name must be a type
22268 [basic.lookup.qual]
22270 During the lookup for a name preceding the :: scope
22271 resolution operator, object, function, and enumerator
22272 names are ignored. */
22273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22274 tag_type
= scope_type
;
22275 /* Look up the name. */
22276 decl
= cp_parser_lookup_name (parser
, identifier
,
22278 /*is_template=*/false,
22279 /*is_namespace=*/false,
22280 check_dependency_p
,
22282 identifier_token
->location
);
22283 if (ambiguous_decls
)
22285 if (cp_parser_parsing_tentatively (parser
))
22286 cp_parser_simulate_error (parser
);
22287 return error_mark_node
;
22293 /* Try a template-id. */
22294 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22295 check_dependency_p
,
22298 if (decl
== error_mark_node
)
22299 return error_mark_node
;
22302 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22304 /* If this is a typename, create a TYPENAME_TYPE. */
22305 if (typename_p
&& decl
!= error_mark_node
)
22307 decl
= make_typename_type (scope
, decl
, typename_type
,
22308 /*complain=*/tf_error
);
22309 if (decl
!= error_mark_node
)
22310 decl
= TYPE_NAME (decl
);
22313 decl
= strip_using_decl (decl
);
22315 /* Check to see that it is really the name of a class. */
22316 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22317 && identifier_p (TREE_OPERAND (decl
, 0))
22318 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22319 /* Situations like this:
22321 template <typename T> struct A {
22322 typename T::template X<int>::I i;
22325 are problematic. Is `T::template X<int>' a class-name? The
22326 standard does not seem to be definitive, but there is no other
22327 valid interpretation of the following `::'. Therefore, those
22328 names are considered class-names. */
22330 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22331 if (decl
!= error_mark_node
)
22332 decl
= TYPE_NAME (decl
);
22334 else if (TREE_CODE (decl
) != TYPE_DECL
22335 || TREE_TYPE (decl
) == error_mark_node
22336 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22337 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22338 /* In Objective-C 2.0, a classname followed by '.' starts a
22339 dot-syntax expression, and it's not a type-name. */
22340 || (c_dialect_objc ()
22341 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22342 && objc_is_class_name (decl
)))
22343 decl
= error_mark_node
;
22345 if (decl
== error_mark_node
)
22346 cp_parser_error (parser
, "expected class-name");
22347 else if (identifier
&& !parser
->scope
)
22348 maybe_note_name_used_in_class (identifier
, decl
);
22353 /* Parse a class-specifier.
22356 class-head { member-specification [opt] }
22358 Returns the TREE_TYPE representing the class. */
22361 cp_parser_class_specifier_1 (cp_parser
* parser
)
22364 tree attributes
= NULL_TREE
;
22365 bool nested_name_specifier_p
;
22366 unsigned saved_num_template_parameter_lists
;
22367 bool saved_in_function_body
;
22368 unsigned char in_statement
;
22369 bool in_switch_statement_p
;
22370 bool saved_in_unbraced_linkage_specification_p
;
22371 tree old_scope
= NULL_TREE
;
22372 tree scope
= NULL_TREE
;
22373 cp_token
*closing_brace
;
22375 push_deferring_access_checks (dk_no_deferred
);
22377 /* Parse the class-head. */
22378 type
= cp_parser_class_head (parser
,
22379 &nested_name_specifier_p
);
22380 /* If the class-head was a semantic disaster, skip the entire body
22384 cp_parser_skip_to_end_of_block_or_statement (parser
);
22385 pop_deferring_access_checks ();
22386 return error_mark_node
;
22389 /* Look for the `{'. */
22390 matching_braces braces
;
22391 if (!braces
.require_open (parser
))
22393 pop_deferring_access_checks ();
22394 return error_mark_node
;
22397 cp_ensure_no_omp_declare_simd (parser
);
22398 cp_ensure_no_oacc_routine (parser
);
22400 /* Issue an error message if type-definitions are forbidden here. */
22401 cp_parser_check_type_definition (parser
);
22402 /* Remember that we are defining one more class. */
22403 ++parser
->num_classes_being_defined
;
22404 /* Inside the class, surrounding template-parameter-lists do not
22406 saved_num_template_parameter_lists
22407 = parser
->num_template_parameter_lists
;
22408 parser
->num_template_parameter_lists
= 0;
22409 /* We are not in a function body. */
22410 saved_in_function_body
= parser
->in_function_body
;
22411 parser
->in_function_body
= false;
22412 /* Or in a loop. */
22413 in_statement
= parser
->in_statement
;
22414 parser
->in_statement
= 0;
22415 /* Or in a switch. */
22416 in_switch_statement_p
= parser
->in_switch_statement_p
;
22417 parser
->in_switch_statement_p
= false;
22418 /* We are not immediately inside an extern "lang" block. */
22419 saved_in_unbraced_linkage_specification_p
22420 = parser
->in_unbraced_linkage_specification_p
;
22421 parser
->in_unbraced_linkage_specification_p
= false;
22423 // Associate constraints with the type.
22425 type
= associate_classtype_constraints (type
);
22427 /* Start the class. */
22428 if (nested_name_specifier_p
)
22430 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22431 old_scope
= push_inner_scope (scope
);
22433 type
= begin_class_definition (type
);
22435 if (type
== error_mark_node
)
22436 /* If the type is erroneous, skip the entire body of the class. */
22437 cp_parser_skip_to_closing_brace (parser
);
22439 /* Parse the member-specification. */
22440 cp_parser_member_specification_opt (parser
);
22442 /* Look for the trailing `}'. */
22443 closing_brace
= braces
.require_close (parser
);
22444 /* Look for trailing attributes to apply to this class. */
22445 if (cp_parser_allow_gnu_extensions_p (parser
))
22446 attributes
= cp_parser_gnu_attributes_opt (parser
);
22447 if (type
!= error_mark_node
)
22448 type
= finish_struct (type
, attributes
);
22449 if (nested_name_specifier_p
)
22450 pop_inner_scope (old_scope
, scope
);
22452 /* We've finished a type definition. Check for the common syntax
22453 error of forgetting a semicolon after the definition. We need to
22454 be careful, as we can't just check for not-a-semicolon and be done
22455 with it; the user might have typed:
22457 class X { } c = ...;
22458 class X { } *p = ...;
22460 and so forth. Instead, enumerate all the possible tokens that
22461 might follow this production; if we don't see one of them, then
22462 complain and silently insert the semicolon. */
22464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22465 bool want_semicolon
= true;
22467 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22468 /* Don't try to parse c++11 attributes here. As per the
22469 grammar, that should be a task for
22470 cp_parser_decl_specifier_seq. */
22471 want_semicolon
= false;
22473 switch (token
->type
)
22476 case CPP_SEMICOLON
:
22479 case CPP_OPEN_PAREN
:
22480 case CPP_CLOSE_PAREN
:
22482 want_semicolon
= false;
22485 /* While it's legal for type qualifiers and storage class
22486 specifiers to follow type definitions in the grammar, only
22487 compiler testsuites contain code like that. Assume that if
22488 we see such code, then what we're really seeing is a case
22492 const <type> var = ...;
22497 static <type> func (...) ...
22499 i.e. the qualifier or specifier applies to the next
22500 declaration. To do so, however, we need to look ahead one
22501 more token to see if *that* token is a type specifier.
22503 This code could be improved to handle:
22506 static const <type> var = ...; */
22508 if (keyword_is_decl_specifier (token
->keyword
))
22510 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22512 /* Handling user-defined types here would be nice, but very
22515 = (lookahead
->type
== CPP_KEYWORD
22516 && keyword_begins_type_specifier (lookahead
->keyword
));
22523 /* If we don't have a type, then something is very wrong and we
22524 shouldn't try to do anything clever. Likewise for not seeing the
22526 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22528 /* Locate the closing brace. */
22529 cp_token_position prev
22530 = cp_lexer_previous_token_position (parser
->lexer
);
22531 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22532 location_t loc
= prev_token
->location
;
22534 /* We want to suggest insertion of a ';' immediately *after* the
22535 closing brace, so, if we can, offset the location by 1 column. */
22536 location_t next_loc
= loc
;
22537 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22538 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22540 rich_location
richloc (line_table
, next_loc
);
22542 /* If we successfully offset the location, suggest the fix-it. */
22543 if (next_loc
!= loc
)
22544 richloc
.add_fixit_insert_before (next_loc
, ";");
22546 if (CLASSTYPE_DECLARED_CLASS (type
))
22547 error_at (&richloc
,
22548 "expected %<;%> after class definition");
22549 else if (TREE_CODE (type
) == RECORD_TYPE
)
22550 error_at (&richloc
,
22551 "expected %<;%> after struct definition");
22552 else if (TREE_CODE (type
) == UNION_TYPE
)
22553 error_at (&richloc
,
22554 "expected %<;%> after union definition");
22556 gcc_unreachable ();
22558 /* Unget one token and smash it to look as though we encountered
22559 a semicolon in the input stream. */
22560 cp_lexer_set_token_position (parser
->lexer
, prev
);
22561 token
= cp_lexer_peek_token (parser
->lexer
);
22562 token
->type
= CPP_SEMICOLON
;
22563 token
->keyword
= RID_MAX
;
22567 /* If this class is not itself within the scope of another class,
22568 then we need to parse the bodies of all of the queued function
22569 definitions. Note that the queued functions defined in a class
22570 are not always processed immediately following the
22571 class-specifier for that class. Consider:
22574 struct B { void f() { sizeof (A); } };
22577 If `f' were processed before the processing of `A' were
22578 completed, there would be no way to compute the size of `A'.
22579 Note that the nesting we are interested in here is lexical --
22580 not the semantic nesting given by TYPE_CONTEXT. In particular,
22583 struct A { struct B; };
22584 struct A::B { void f() { } };
22586 there is no need to delay the parsing of `A::B::f'. */
22587 if (--parser
->num_classes_being_defined
== 0)
22590 tree class_type
= NULL_TREE
;
22591 tree pushed_scope
= NULL_TREE
;
22593 cp_default_arg_entry
*e
;
22594 tree save_ccp
, save_ccr
;
22596 /* In a first pass, parse default arguments to the functions.
22597 Then, in a second pass, parse the bodies of the functions.
22598 This two-phased approach handles cases like:
22606 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22609 /* If there are default arguments that have not yet been processed,
22610 take care of them now. */
22611 if (class_type
!= e
->class_type
)
22614 pop_scope (pushed_scope
);
22615 class_type
= e
->class_type
;
22616 pushed_scope
= push_scope (class_type
);
22618 /* Make sure that any template parameters are in scope. */
22619 maybe_begin_member_template_processing (decl
);
22620 /* Parse the default argument expressions. */
22621 cp_parser_late_parsing_default_args (parser
, decl
);
22622 /* Remove any template parameters from the symbol table. */
22623 maybe_end_member_template_processing ();
22625 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22626 /* Now parse any NSDMIs. */
22627 save_ccp
= current_class_ptr
;
22628 save_ccr
= current_class_ref
;
22629 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22631 if (class_type
!= DECL_CONTEXT (decl
))
22634 pop_scope (pushed_scope
);
22635 class_type
= DECL_CONTEXT (decl
);
22636 pushed_scope
= push_scope (class_type
);
22638 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22639 cp_parser_late_parsing_nsdmi (parser
, decl
);
22641 vec_safe_truncate (unparsed_nsdmis
, 0);
22642 current_class_ptr
= save_ccp
;
22643 current_class_ref
= save_ccr
;
22645 pop_scope (pushed_scope
);
22647 /* Now do some post-NSDMI bookkeeping. */
22648 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22649 after_nsdmi_defaulted_late_checks (class_type
);
22650 vec_safe_truncate (unparsed_classes
, 0);
22651 after_nsdmi_defaulted_late_checks (type
);
22653 /* Now parse the body of the functions. */
22656 /* OpenMP UDRs need to be parsed before all other functions. */
22657 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22658 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22659 cp_parser_late_parsing_for_member (parser
, decl
);
22660 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22661 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22662 cp_parser_late_parsing_for_member (parser
, decl
);
22665 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22666 cp_parser_late_parsing_for_member (parser
, decl
);
22667 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22670 vec_safe_push (unparsed_classes
, type
);
22672 /* Put back any saved access checks. */
22673 pop_deferring_access_checks ();
22675 /* Restore saved state. */
22676 parser
->in_switch_statement_p
= in_switch_statement_p
;
22677 parser
->in_statement
= in_statement
;
22678 parser
->in_function_body
= saved_in_function_body
;
22679 parser
->num_template_parameter_lists
22680 = saved_num_template_parameter_lists
;
22681 parser
->in_unbraced_linkage_specification_p
22682 = saved_in_unbraced_linkage_specification_p
;
22688 cp_parser_class_specifier (cp_parser
* parser
)
22691 timevar_push (TV_PARSE_STRUCT
);
22692 ret
= cp_parser_class_specifier_1 (parser
);
22693 timevar_pop (TV_PARSE_STRUCT
);
22697 /* Parse a class-head.
22700 class-key identifier [opt] base-clause [opt]
22701 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22702 class-key nested-name-specifier [opt] template-id
22705 class-virt-specifier:
22709 class-key attributes identifier [opt] base-clause [opt]
22710 class-key attributes nested-name-specifier identifier base-clause [opt]
22711 class-key attributes nested-name-specifier [opt] template-id
22714 Upon return BASES is initialized to the list of base classes (or
22715 NULL, if there are none) in the same form returned by
22716 cp_parser_base_clause.
22718 Returns the TYPE of the indicated class. Sets
22719 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22720 involving a nested-name-specifier was used, and FALSE otherwise.
22722 Returns error_mark_node if this is not a class-head.
22724 Returns NULL_TREE if the class-head is syntactically valid, but
22725 semantically invalid in a way that means we should skip the entire
22726 body of the class. */
22729 cp_parser_class_head (cp_parser
* parser
,
22730 bool* nested_name_specifier_p
)
22732 tree nested_name_specifier
;
22733 enum tag_types class_key
;
22734 tree id
= NULL_TREE
;
22735 tree type
= NULL_TREE
;
22738 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22739 bool template_id_p
= false;
22740 bool qualified_p
= false;
22741 bool invalid_nested_name_p
= false;
22742 bool invalid_explicit_specialization_p
= false;
22743 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22744 tree pushed_scope
= NULL_TREE
;
22745 unsigned num_templates
;
22746 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22747 /* Assume no nested-name-specifier will be present. */
22748 *nested_name_specifier_p
= false;
22749 /* Assume no template parameter lists will be used in defining the
22752 parser
->colon_corrects_to_scope_p
= false;
22754 /* Look for the class-key. */
22755 class_key
= cp_parser_class_key (parser
);
22756 if (class_key
== none_type
)
22757 return error_mark_node
;
22759 location_t class_head_start_location
= input_location
;
22761 /* Parse the attributes. */
22762 attributes
= cp_parser_attributes_opt (parser
);
22764 /* If the next token is `::', that is invalid -- but sometimes
22765 people do try to write:
22769 Handle this gracefully by accepting the extra qualifier, and then
22770 issuing an error about it later if this really is a
22771 class-head. If it turns out just to be an elaborated type
22772 specifier, remain silent. */
22773 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22774 qualified_p
= true;
22776 push_deferring_access_checks (dk_no_check
);
22778 /* Determine the name of the class. Begin by looking for an
22779 optional nested-name-specifier. */
22780 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22781 nested_name_specifier
22782 = cp_parser_nested_name_specifier_opt (parser
,
22783 /*typename_keyword_p=*/false,
22784 /*check_dependency_p=*/false,
22786 /*is_declaration=*/false);
22787 /* If there was a nested-name-specifier, then there *must* be an
22790 cp_token
*bad_template_keyword
= NULL
;
22792 if (nested_name_specifier
)
22794 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22795 /* Although the grammar says `identifier', it really means
22796 `class-name' or `template-name'. You are only allowed to
22797 define a class that has already been declared with this
22800 The proposed resolution for Core Issue 180 says that wherever
22801 you see `class T::X' you should treat `X' as a type-name.
22803 It is OK to define an inaccessible class; for example:
22805 class A { class B; };
22808 We do not know if we will see a class-name, or a
22809 template-name. We look for a class-name first, in case the
22810 class-name is a template-id; if we looked for the
22811 template-name first we would stop after the template-name. */
22812 cp_parser_parse_tentatively (parser
);
22813 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22814 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22815 type
= cp_parser_class_name (parser
,
22816 /*typename_keyword_p=*/false,
22817 /*template_keyword_p=*/false,
22819 /*check_dependency_p=*/false,
22820 /*class_head_p=*/true,
22821 /*is_declaration=*/false);
22822 /* If that didn't work, ignore the nested-name-specifier. */
22823 if (!cp_parser_parse_definitely (parser
))
22825 invalid_nested_name_p
= true;
22826 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22827 id
= cp_parser_identifier (parser
);
22828 if (id
== error_mark_node
)
22831 /* If we could not find a corresponding TYPE, treat this
22832 declaration like an unqualified declaration. */
22833 if (type
== error_mark_node
)
22834 nested_name_specifier
= NULL_TREE
;
22835 /* Otherwise, count the number of templates used in TYPE and its
22836 containing scopes. */
22841 for (scope
= TREE_TYPE (type
);
22842 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22843 scope
= get_containing_scope (scope
))
22845 && CLASS_TYPE_P (scope
)
22846 && CLASSTYPE_TEMPLATE_INFO (scope
)
22847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22848 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22849 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22853 /* Otherwise, the identifier is optional. */
22856 /* We don't know whether what comes next is a template-id,
22857 an identifier, or nothing at all. */
22858 cp_parser_parse_tentatively (parser
);
22859 /* Check for a template-id. */
22860 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22861 id
= cp_parser_template_id (parser
,
22862 /*template_keyword_p=*/false,
22863 /*check_dependency_p=*/true,
22865 /*is_declaration=*/true);
22866 /* If that didn't work, it could still be an identifier. */
22867 if (!cp_parser_parse_definitely (parser
))
22869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22871 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22872 id
= cp_parser_identifier (parser
);
22879 template_id_p
= true;
22884 pop_deferring_access_checks ();
22888 cp_parser_check_for_invalid_template_id (parser
, id
,
22890 type_start_token
->location
);
22892 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22894 /* If it's not a `:' or a `{' then we can't really be looking at a
22895 class-head, since a class-head only appears as part of a
22896 class-specifier. We have to detect this situation before calling
22897 xref_tag, since that has irreversible side-effects. */
22898 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22900 cp_parser_error (parser
, "expected %<{%> or %<:%>");
22901 type
= error_mark_node
;
22905 /* At this point, we're going ahead with the class-specifier, even
22906 if some other problem occurs. */
22907 cp_parser_commit_to_tentative_parse (parser
);
22908 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
22910 cp_parser_error (parser
,
22911 "cannot specify %<override%> for a class");
22912 type
= error_mark_node
;
22915 /* Issue the error about the overly-qualified name now. */
22918 cp_parser_error (parser
,
22919 "global qualification of class name is invalid");
22920 type
= error_mark_node
;
22923 else if (invalid_nested_name_p
)
22925 cp_parser_error (parser
,
22926 "qualified name does not name a class");
22927 type
= error_mark_node
;
22930 else if (nested_name_specifier
)
22934 if (bad_template_keyword
)
22935 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22936 keyword template shall not appear at the top level. */
22937 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
22938 "keyword %<template%> not allowed in class-head-name");
22940 /* Reject typedef-names in class heads. */
22941 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
22943 error_at (type_start_token
->location
,
22944 "invalid class name in declaration of %qD",
22950 /* Figure out in what scope the declaration is being placed. */
22951 scope
= current_scope ();
22952 /* If that scope does not contain the scope in which the
22953 class was originally declared, the program is invalid. */
22954 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
22956 if (at_namespace_scope_p ())
22957 error_at (type_start_token
->location
,
22958 "declaration of %qD in namespace %qD which does not "
22960 type
, scope
, nested_name_specifier
);
22962 error_at (type_start_token
->location
,
22963 "declaration of %qD in %qD which does not enclose %qD",
22964 type
, scope
, nested_name_specifier
);
22970 A declarator-id shall not be qualified except for the
22971 definition of a ... nested class outside of its class
22972 ... [or] the definition or explicit instantiation of a
22973 class member of a namespace outside of its namespace. */
22974 if (scope
== nested_name_specifier
)
22976 permerror (nested_name_specifier_token_start
->location
,
22977 "extra qualification not allowed");
22978 nested_name_specifier
= NULL_TREE
;
22982 /* An explicit-specialization must be preceded by "template <>". If
22983 it is not, try to recover gracefully. */
22984 if (at_namespace_scope_p ()
22985 && parser
->num_template_parameter_lists
== 0
22986 && !processing_template_parmlist
22989 /* Build a location of this form:
22990 struct typename <ARGS>
22991 ^~~~~~~~~~~~~~~~~~~~~~
22992 with caret==start at the start token, and
22993 finishing at the end of the type. */
22994 location_t reported_loc
22995 = make_location (class_head_start_location
,
22996 class_head_start_location
,
22997 get_finish (type_start_token
->location
));
22998 rich_location
richloc (line_table
, reported_loc
);
22999 richloc
.add_fixit_insert_before (class_head_start_location
,
23001 error_at (&richloc
,
23002 "an explicit specialization must be preceded by"
23003 " %<template <>%>");
23004 invalid_explicit_specialization_p
= true;
23005 /* Take the same action that would have been taken by
23006 cp_parser_explicit_specialization. */
23007 ++parser
->num_template_parameter_lists
;
23008 begin_specialization ();
23010 /* There must be no "return" statements between this point and the
23011 end of this function; set "type "to the correct return value and
23012 use "goto done;" to return. */
23013 /* Make sure that the right number of template parameters were
23015 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23016 type_start_token
->location
,
23017 /*declarator=*/NULL
))
23019 /* If something went wrong, there is no point in even trying to
23020 process the class-definition. */
23025 /* Look up the type. */
23028 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23029 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23030 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23032 error_at (type_start_token
->location
,
23033 "function template %qD redeclared as a class template", id
);
23034 type
= error_mark_node
;
23038 type
= TREE_TYPE (id
);
23039 type
= maybe_process_partial_specialization (type
);
23041 /* Check the scope while we still know whether or not we had a
23042 nested-name-specifier. */
23043 if (type
!= error_mark_node
)
23044 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23046 if (nested_name_specifier
)
23047 pushed_scope
= push_scope (nested_name_specifier
);
23049 else if (nested_name_specifier
)
23055 template <typename T> struct S { struct T };
23056 template <typename T> struct S<T>::T { };
23058 we will get a TYPENAME_TYPE when processing the definition of
23059 `S::T'. We need to resolve it to the actual type before we
23060 try to define it. */
23061 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23063 class_type
= resolve_typename_type (TREE_TYPE (type
),
23064 /*only_current_p=*/false);
23065 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23066 type
= TYPE_NAME (class_type
);
23069 cp_parser_error (parser
, "could not resolve typename type");
23070 type
= error_mark_node
;
23074 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23075 == error_mark_node
)
23081 class_type
= current_class_type
;
23082 /* Enter the scope indicated by the nested-name-specifier. */
23083 pushed_scope
= push_scope (nested_name_specifier
);
23084 /* Get the canonical version of this type. */
23085 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23086 /* Call push_template_decl if it seems like we should be defining a
23087 template either from the template headers or the type we're
23088 defining, so that we diagnose both extra and missing headers. */
23089 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23090 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23091 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23093 type
= push_template_decl (type
);
23094 if (type
== error_mark_node
)
23101 type
= TREE_TYPE (type
);
23102 *nested_name_specifier_p
= true;
23104 else /* The name is not a nested name. */
23106 /* If the class was unnamed, create a dummy name. */
23108 id
= make_anon_name ();
23109 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23110 ? ts_within_enclosing_non_class
23112 type
= xref_tag (class_key
, id
, tag_scope
,
23113 parser
->num_template_parameter_lists
);
23116 /* Indicate whether this class was declared as a `class' or as a
23118 if (TREE_CODE (type
) == RECORD_TYPE
)
23119 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23120 cp_parser_check_class_key (class_key
, type
);
23122 /* If this type was already complete, and we see another definition,
23123 that's an error. */
23124 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23126 error_at (type_start_token
->location
, "redefinition of %q#T",
23128 inform (location_of (type
), "previous definition of %q#T",
23133 else if (type
== error_mark_node
)
23138 /* Apply attributes now, before any use of the class as a template
23139 argument in its base list. */
23140 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23141 fixup_attribute_variants (type
);
23144 /* We will have entered the scope containing the class; the names of
23145 base classes should be looked up in that context. For example:
23147 struct A { struct B {}; struct C; };
23148 struct A::C : B {};
23152 /* Get the list of base-classes, if there is one. */
23153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23155 /* PR59482: enter the class scope so that base-specifiers are looked
23159 bases
= cp_parser_base_clause (parser
);
23160 /* PR59482: get out of the previously pushed class scope so that the
23161 subsequent pops pop the right thing. */
23168 /* If we're really defining a class, process the base classes.
23169 If they're invalid, fail. */
23170 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23171 xref_basetypes (type
, bases
);
23174 /* Leave the scope given by the nested-name-specifier. We will
23175 enter the class scope itself while processing the members. */
23177 pop_scope (pushed_scope
);
23179 if (invalid_explicit_specialization_p
)
23181 end_specialization ();
23182 --parser
->num_template_parameter_lists
;
23186 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23187 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23188 CLASSTYPE_FINAL (type
) = 1;
23190 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23194 /* Parse a class-key.
23201 Returns the kind of class-key specified, or none_type to indicate
23204 static enum tag_types
23205 cp_parser_class_key (cp_parser
* parser
)
23208 enum tag_types tag_type
;
23210 /* Look for the class-key. */
23211 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23215 /* Check to see if the TOKEN is a class-key. */
23216 tag_type
= cp_parser_token_is_class_key (token
);
23218 cp_parser_error (parser
, "expected class-key");
23222 /* Parse a type-parameter-key.
23224 type-parameter-key:
23230 cp_parser_type_parameter_key (cp_parser
* parser
)
23232 /* Look for the type-parameter-key. */
23233 enum tag_types tag_type
= none_type
;
23234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23235 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23237 cp_lexer_consume_token (parser
->lexer
);
23238 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23239 /* typename is not allowed in a template template parameter
23240 by the standard until C++17. */
23241 pedwarn (token
->location
, OPT_Wpedantic
,
23242 "ISO C++ forbids typename key in template template parameter;"
23243 " use -std=c++17 or -std=gnu++17");
23246 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23251 /* Parse an (optional) member-specification.
23253 member-specification:
23254 member-declaration member-specification [opt]
23255 access-specifier : member-specification [opt] */
23258 cp_parser_member_specification_opt (cp_parser
* parser
)
23265 /* Peek at the next token. */
23266 token
= cp_lexer_peek_token (parser
->lexer
);
23267 /* If it's a `}', or EOF then we've seen all the members. */
23268 if (token
->type
== CPP_CLOSE_BRACE
23269 || token
->type
== CPP_EOF
23270 || token
->type
== CPP_PRAGMA_EOL
)
23273 /* See if this token is a keyword. */
23274 keyword
= token
->keyword
;
23278 case RID_PROTECTED
:
23280 /* Consume the access-specifier. */
23281 cp_lexer_consume_token (parser
->lexer
);
23282 /* Remember which access-specifier is active. */
23283 current_access_specifier
= token
->u
.value
;
23284 /* Look for the `:'. */
23285 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23289 /* Accept #pragmas at class scope. */
23290 if (token
->type
== CPP_PRAGMA
)
23292 cp_parser_pragma (parser
, pragma_member
, NULL
);
23296 /* Otherwise, the next construction must be a
23297 member-declaration. */
23298 cp_parser_member_declaration (parser
);
23303 /* Parse a member-declaration.
23305 member-declaration:
23306 decl-specifier-seq [opt] member-declarator-list [opt] ;
23307 function-definition ; [opt]
23308 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23310 template-declaration
23313 member-declarator-list:
23315 member-declarator-list , member-declarator
23318 declarator pure-specifier [opt]
23319 declarator constant-initializer [opt]
23320 identifier [opt] : constant-expression
23324 member-declaration:
23325 __extension__ member-declaration
23328 declarator attributes [opt] pure-specifier [opt]
23329 declarator attributes [opt] constant-initializer [opt]
23330 identifier [opt] attributes [opt] : constant-expression
23334 member-declaration:
23335 static_assert-declaration */
23338 cp_parser_member_declaration (cp_parser
* parser
)
23340 cp_decl_specifier_seq decl_specifiers
;
23341 tree prefix_attributes
;
23343 int declares_class_or_enum
;
23345 cp_token
*token
= NULL
;
23346 cp_token
*decl_spec_token_start
= NULL
;
23347 cp_token
*initializer_token_start
= NULL
;
23348 int saved_pedantic
;
23349 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23351 /* Check for the `__extension__' keyword. */
23352 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23355 cp_parser_member_declaration (parser
);
23356 /* Restore the old value of the PEDANTIC flag. */
23357 pedantic
= saved_pedantic
;
23362 /* Check for a template-declaration. */
23363 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23365 /* An explicit specialization here is an error condition, and we
23366 expect the specialization handler to detect and report this. */
23367 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23368 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23369 cp_parser_explicit_specialization (parser
);
23371 cp_parser_template_declaration (parser
, /*member_p=*/true);
23375 /* Check for a template introduction. */
23376 else if (cp_parser_template_declaration_after_export (parser
, true))
23379 /* Check for a using-declaration. */
23380 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23382 if (cxx_dialect
< cxx11
)
23384 /* Parse the using-declaration. */
23385 cp_parser_using_declaration (parser
,
23386 /*access_declaration_p=*/false);
23392 bool alias_decl_expected
;
23393 cp_parser_parse_tentatively (parser
);
23394 decl
= cp_parser_alias_declaration (parser
);
23395 /* Note that if we actually see the '=' token after the
23396 identifier, cp_parser_alias_declaration commits the
23397 tentative parse. In that case, we really expect an
23398 alias-declaration. Otherwise, we expect a using
23400 alias_decl_expected
=
23401 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23402 cp_parser_parse_definitely (parser
);
23404 if (alias_decl_expected
)
23405 finish_member_declaration (decl
);
23407 cp_parser_using_declaration (parser
,
23408 /*access_declaration_p=*/false);
23413 /* Check for @defs. */
23414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23417 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23418 ivar
= ivar_chains
;
23422 ivar
= TREE_CHAIN (member
);
23423 TREE_CHAIN (member
) = NULL_TREE
;
23424 finish_member_declaration (member
);
23429 /* If the next token is `static_assert' we have a static assertion. */
23430 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23432 cp_parser_static_assert (parser
, /*member_p=*/true);
23436 parser
->colon_corrects_to_scope_p
= false;
23438 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23441 /* Parse the decl-specifier-seq. */
23442 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23443 cp_parser_decl_specifier_seq (parser
,
23444 CP_PARSER_FLAGS_OPTIONAL
,
23446 &declares_class_or_enum
);
23447 /* Check for an invalid type-name. */
23448 if (!decl_specifiers
.any_type_specifiers_p
23449 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23451 /* If there is no declarator, then the decl-specifier-seq should
23453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23455 /* If there was no decl-specifier-seq, and the next token is a
23456 `;', then we have something like:
23462 Each member-declaration shall declare at least one member
23463 name of the class. */
23464 if (!decl_specifiers
.any_specifiers_p
)
23466 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23467 if (!in_system_header_at (token
->location
))
23469 gcc_rich_location
richloc (token
->location
);
23470 richloc
.add_fixit_remove ();
23471 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23478 /* See if this declaration is a friend. */
23479 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23480 /* If there were decl-specifiers, check to see if there was
23481 a class-declaration. */
23482 type
= check_tag_decl (&decl_specifiers
,
23483 /*explicit_type_instantiation_p=*/false);
23484 /* Nested classes have already been added to the class, but
23485 a `friend' needs to be explicitly registered. */
23488 /* If the `friend' keyword was present, the friend must
23489 be introduced with a class-key. */
23490 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23491 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23492 "in C++03 a class-key must be used "
23493 "when declaring a friend");
23496 template <typename T> struct A {
23497 friend struct A<T>::B;
23500 A<T>::B will be represented by a TYPENAME_TYPE, and
23501 therefore not recognized by check_tag_decl. */
23504 type
= decl_specifiers
.type
;
23505 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23506 type
= TREE_TYPE (type
);
23508 if (!type
|| !TYPE_P (type
))
23509 error_at (decl_spec_token_start
->location
,
23510 "friend declaration does not name a class or "
23513 make_friend_class (current_class_type
, type
,
23514 /*complain=*/true);
23516 /* If there is no TYPE, an error message will already have
23518 else if (!type
|| type
== error_mark_node
)
23520 /* An anonymous aggregate has to be handled specially; such
23521 a declaration really declares a data member (with a
23522 particular type), as opposed to a nested class. */
23523 else if (ANON_AGGR_TYPE_P (type
))
23526 if (decl_specifiers
.storage_class
!= sc_none
)
23527 error_at (decl_spec_token_start
->location
,
23528 "a storage class on an anonymous aggregate "
23529 "in class scope is not allowed");
23531 /* Remove constructors and such from TYPE, now that we
23532 know it is an anonymous aggregate. */
23533 fixup_anonymous_aggr (type
);
23534 /* And make the corresponding data member. */
23535 decl
= build_decl (decl_spec_token_start
->location
,
23536 FIELD_DECL
, NULL_TREE
, type
);
23537 /* Add it to the class. */
23538 finish_member_declaration (decl
);
23541 cp_parser_check_access_in_redeclaration
23543 decl_spec_token_start
->location
);
23548 bool assume_semicolon
= false;
23550 /* Clear attributes from the decl_specifiers but keep them
23551 around as prefix attributes that apply them to the entity
23553 prefix_attributes
= decl_specifiers
.attributes
;
23554 decl_specifiers
.attributes
= NULL_TREE
;
23556 /* See if these declarations will be friends. */
23557 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23559 /* Keep going until we hit the `;' at the end of the
23561 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23563 tree attributes
= NULL_TREE
;
23564 tree first_attribute
;
23566 bool is_bitfld
= false;
23567 bool named_bitfld
= false;
23569 /* Peek at the next token. */
23570 token
= cp_lexer_peek_token (parser
->lexer
);
23572 /* The following code wants to know early if it is a bit-field
23573 or some other declaration. Attributes can appear before
23574 the `:' token, but are hopefully rare enough that the
23575 simplicity of the tentative lookup pays off. */
23576 if (cp_next_tokens_can_be_attribute_p (parser
)
23577 || (token
->type
== CPP_NAME
23578 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23579 && (named_bitfld
= true)))
23581 cp_parser_parse_tentatively (parser
);
23583 cp_lexer_consume_token (parser
->lexer
);
23584 cp_parser_attributes_opt (parser
);
23585 token
= cp_lexer_peek_token (parser
->lexer
);
23586 is_bitfld
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
23587 cp_parser_abort_tentative_parse (parser
);
23590 /* Check for a bitfield declaration. */
23592 || token
->type
== CPP_COLON
23593 || (token
->type
== CPP_NAME
23594 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23595 && (named_bitfld
= true)))
23599 tree late_attributes
= NULL_TREE
;
23602 identifier
= cp_parser_identifier (parser
);
23604 identifier
= NULL_TREE
;
23606 /* Look for attributes that apply to the bitfield. */
23607 attributes
= cp_parser_attributes_opt (parser
);
23609 /* Consume the `:' token. */
23610 cp_lexer_consume_token (parser
->lexer
);
23612 /* Get the width of the bitfield. */
23613 width
= cp_parser_constant_expression (parser
, false, NULL
,
23614 cxx_dialect
>= cxx11
);
23616 /* In C++2A and as extension for C++11 and above we allow
23617 default member initializers for bit-fields. */
23618 initializer
= NULL_TREE
;
23619 if (cxx_dialect
>= cxx11
23620 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23621 || cp_lexer_next_token_is (parser
->lexer
,
23625 = cp_lexer_peek_token (parser
->lexer
)->location
;
23626 if (cxx_dialect
< cxx2a
23627 && !in_system_header_at (loc
)
23628 && identifier
!= NULL_TREE
)
23630 "default member initializers for bit-fields "
23631 "only available with -std=c++2a or "
23634 initializer
= cp_parser_save_nsdmi (parser
);
23635 if (identifier
== NULL_TREE
)
23637 error_at (loc
, "default member initializer for "
23638 "unnamed bit-field");
23639 initializer
= NULL_TREE
;
23644 /* Look for attributes that apply to the bitfield after
23645 the `:' token and width. This is where GCC used to
23646 parse attributes in the past, pedwarn if there is
23647 a std attribute. */
23648 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23649 pedwarn (input_location
, OPT_Wpedantic
,
23650 "ISO C++ allows bit-field attributes only "
23651 "before the %<:%> token");
23653 late_attributes
= cp_parser_attributes_opt (parser
);
23656 attributes
= chainon (attributes
, late_attributes
);
23658 /* Remember which attributes are prefix attributes and
23660 first_attribute
= attributes
;
23661 /* Combine the attributes. */
23662 attributes
= chainon (prefix_attributes
, attributes
);
23664 /* Create the bitfield declaration. */
23665 decl
= grokbitfield (identifier
23666 ? make_id_declarator (NULL_TREE
,
23671 width
, initializer
,
23676 cp_declarator
*declarator
;
23677 tree asm_specification
;
23678 int ctor_dtor_or_conv_p
;
23680 /* Parse the declarator. */
23682 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23683 &ctor_dtor_or_conv_p
,
23684 /*parenthesized_p=*/NULL
,
23688 /* If something went wrong parsing the declarator, make sure
23689 that we at least consume some tokens. */
23690 if (declarator
== cp_error_declarator
)
23692 /* Skip to the end of the statement. */
23693 cp_parser_skip_to_end_of_statement (parser
);
23694 /* If the next token is not a semicolon, that is
23695 probably because we just skipped over the body of
23696 a function. So, we consume a semicolon if
23697 present, but do not issue an error message if it
23699 if (cp_lexer_next_token_is (parser
->lexer
,
23701 cp_lexer_consume_token (parser
->lexer
);
23705 if (declares_class_or_enum
& 2)
23706 cp_parser_check_for_definition_in_return_type
23707 (declarator
, decl_specifiers
.type
,
23708 decl_specifiers
.locations
[ds_type_spec
]);
23710 /* Look for an asm-specification. */
23711 asm_specification
= cp_parser_asm_specification_opt (parser
);
23712 /* Look for attributes that apply to the declaration. */
23713 attributes
= cp_parser_attributes_opt (parser
);
23714 /* Remember which attributes are prefix attributes and
23716 first_attribute
= attributes
;
23717 /* Combine the attributes. */
23718 attributes
= chainon (prefix_attributes
, attributes
);
23720 /* If it's an `=', then we have a constant-initializer or a
23721 pure-specifier. It is not correct to parse the
23722 initializer before registering the member declaration
23723 since the member declaration should be in scope while
23724 its initializer is processed. However, the rest of the
23725 front end does not yet provide an interface that allows
23726 us to handle this correctly. */
23727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23731 A pure-specifier shall be used only in the declaration of
23732 a virtual function.
23734 A member-declarator can contain a constant-initializer
23735 only if it declares a static member of integral or
23738 Therefore, if the DECLARATOR is for a function, we look
23739 for a pure-specifier; otherwise, we look for a
23740 constant-initializer. When we call `grokfield', it will
23741 perform more stringent semantics checks. */
23742 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23743 if (function_declarator_p (declarator
)
23744 || (decl_specifiers
.type
23745 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23746 && declarator
->kind
== cdk_id
23747 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23748 == FUNCTION_TYPE
)))
23749 initializer
= cp_parser_pure_specifier (parser
);
23750 else if (decl_specifiers
.storage_class
!= sc_static
)
23751 initializer
= cp_parser_save_nsdmi (parser
);
23752 else if (cxx_dialect
>= cxx11
)
23755 /* Don't require a constant rvalue in C++11, since we
23756 might want a reference constant. We'll enforce
23757 constancy later. */
23758 cp_lexer_consume_token (parser
->lexer
);
23759 /* Parse the initializer. */
23760 initializer
= cp_parser_initializer_clause (parser
,
23764 /* Parse the initializer. */
23765 initializer
= cp_parser_constant_initializer (parser
);
23767 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23768 && !function_declarator_p (declarator
))
23771 if (decl_specifiers
.storage_class
!= sc_static
)
23772 initializer
= cp_parser_save_nsdmi (parser
);
23774 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23776 /* Otherwise, there is no initializer. */
23778 initializer
= NULL_TREE
;
23780 /* See if we are probably looking at a function
23781 definition. We are certainly not looking at a
23782 member-declarator. Calling `grokfield' has
23783 side-effects, so we must not do it unless we are sure
23784 that we are looking at a member-declarator. */
23785 if (cp_parser_token_starts_function_definition_p
23786 (cp_lexer_peek_token (parser
->lexer
)))
23788 /* The grammar does not allow a pure-specifier to be
23789 used when a member function is defined. (It is
23790 possible that this fact is an oversight in the
23791 standard, since a pure function may be defined
23792 outside of the class-specifier. */
23793 if (initializer
&& initializer_token_start
)
23794 error_at (initializer_token_start
->location
,
23795 "pure-specifier on function-definition");
23796 decl
= cp_parser_save_member_function_body (parser
,
23800 if (parser
->fully_implicit_function_template_p
)
23801 decl
= finish_fully_implicit_template (parser
, decl
);
23802 /* If the member was not a friend, declare it here. */
23804 finish_member_declaration (decl
);
23805 /* Peek at the next token. */
23806 token
= cp_lexer_peek_token (parser
->lexer
);
23807 /* If the next token is a semicolon, consume it. */
23808 if (token
->type
== CPP_SEMICOLON
)
23810 location_t semicolon_loc
23811 = cp_lexer_consume_token (parser
->lexer
)->location
;
23812 gcc_rich_location
richloc (semicolon_loc
);
23813 richloc
.add_fixit_remove ();
23814 warning_at (&richloc
, OPT_Wextra_semi
,
23815 "extra %<;%> after in-class "
23816 "function definition");
23821 if (declarator
->kind
== cdk_function
)
23822 declarator
->id_loc
= token
->location
;
23823 /* Create the declaration. */
23824 decl
= grokfield (declarator
, &decl_specifiers
,
23825 initializer
, /*init_const_expr_p=*/true,
23826 asm_specification
, attributes
);
23827 if (parser
->fully_implicit_function_template_p
)
23830 finish_fully_implicit_template (parser
, 0);
23832 decl
= finish_fully_implicit_template (parser
, decl
);
23836 cp_finalize_omp_declare_simd (parser
, decl
);
23837 cp_finalize_oacc_routine (parser
, decl
, false);
23839 /* Reset PREFIX_ATTRIBUTES. */
23840 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23841 attributes
= TREE_CHAIN (attributes
);
23843 TREE_CHAIN (attributes
) = NULL_TREE
;
23845 /* If there is any qualification still in effect, clear it
23846 now; we will be starting fresh with the next declarator. */
23847 parser
->scope
= NULL_TREE
;
23848 parser
->qualifying_scope
= NULL_TREE
;
23849 parser
->object_scope
= NULL_TREE
;
23850 /* If it's a `,', then there are more declarators. */
23851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23853 cp_lexer_consume_token (parser
->lexer
);
23854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23856 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23857 gcc_rich_location
richloc (token
->location
);
23858 richloc
.add_fixit_remove ();
23859 error_at (&richloc
, "stray %<,%> at end of "
23860 "member declaration");
23863 /* If the next token isn't a `;', then we have a parse error. */
23864 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23867 /* The next token might be a ways away from where the
23868 actual semicolon is missing. Find the previous token
23869 and use that for our error position. */
23870 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23871 gcc_rich_location
richloc (token
->location
);
23872 richloc
.add_fixit_insert_after (";");
23873 error_at (&richloc
, "expected %<;%> at end of "
23874 "member declaration");
23876 /* Assume that the user meant to provide a semicolon. If
23877 we were to cp_parser_skip_to_end_of_statement, we might
23878 skip to a semicolon inside a member function definition
23879 and issue nonsensical error messages. */
23880 assume_semicolon
= true;
23885 /* Add DECL to the list of members. */
23887 /* Explicitly include, eg, NSDMIs, for better error
23888 recovery (c++/58650). */
23889 || !DECL_DECLARES_FUNCTION_P (decl
))
23890 finish_member_declaration (decl
);
23892 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23893 cp_parser_save_default_args (parser
, decl
);
23894 else if (TREE_CODE (decl
) == FIELD_DECL
23895 && DECL_INITIAL (decl
))
23896 /* Add DECL to the queue of NSDMI to be parsed later. */
23897 vec_safe_push (unparsed_nsdmis
, decl
);
23900 if (assume_semicolon
)
23905 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23907 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23910 /* Parse a pure-specifier.
23915 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23916 Otherwise, ERROR_MARK_NODE is returned. */
23919 cp_parser_pure_specifier (cp_parser
* parser
)
23923 /* Look for the `=' token. */
23924 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23925 return error_mark_node
;
23926 /* Look for the `0' token. */
23927 token
= cp_lexer_peek_token (parser
->lexer
);
23929 if (token
->type
== CPP_EOF
23930 || token
->type
== CPP_PRAGMA_EOL
)
23931 return error_mark_node
;
23933 cp_lexer_consume_token (parser
->lexer
);
23935 /* Accept = default or = delete in c++0x mode. */
23936 if (token
->keyword
== RID_DEFAULT
23937 || token
->keyword
== RID_DELETE
)
23939 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
23940 return token
->u
.value
;
23943 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23944 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
23946 cp_parser_error (parser
,
23947 "invalid pure specifier (only %<= 0%> is allowed)");
23948 cp_parser_skip_to_end_of_statement (parser
);
23949 return error_mark_node
;
23951 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23953 error_at (token
->location
, "templates may not be %<virtual%>");
23954 return error_mark_node
;
23957 return integer_zero_node
;
23960 /* Parse a constant-initializer.
23962 constant-initializer:
23963 = constant-expression
23965 Returns a representation of the constant-expression. */
23968 cp_parser_constant_initializer (cp_parser
* parser
)
23970 /* Look for the `=' token. */
23971 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
23972 return error_mark_node
;
23974 /* It is invalid to write:
23976 struct S { static const int i = { 7 }; };
23979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23981 cp_parser_error (parser
,
23982 "a brace-enclosed initializer is not allowed here");
23983 /* Consume the opening brace. */
23984 matching_braces braces
;
23985 braces
.consume_open (parser
);
23986 /* Skip the initializer. */
23987 cp_parser_skip_to_closing_brace (parser
);
23988 /* Look for the trailing `}'. */
23989 braces
.require_close (parser
);
23991 return error_mark_node
;
23994 return cp_parser_constant_expression (parser
);
23997 /* Derived classes [gram.class.derived] */
23999 /* Parse a base-clause.
24002 : base-specifier-list
24004 base-specifier-list:
24005 base-specifier ... [opt]
24006 base-specifier-list , base-specifier ... [opt]
24008 Returns a TREE_LIST representing the base-classes, in the order in
24009 which they were declared. The representation of each node is as
24010 described by cp_parser_base_specifier.
24012 In the case that no bases are specified, this function will return
24013 NULL_TREE, not ERROR_MARK_NODE. */
24016 cp_parser_base_clause (cp_parser
* parser
)
24018 tree bases
= NULL_TREE
;
24020 /* Look for the `:' that begins the list. */
24021 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24023 /* Scan the base-specifier-list. */
24028 bool pack_expansion_p
= false;
24030 /* Look for the base-specifier. */
24031 base
= cp_parser_base_specifier (parser
);
24032 /* Look for the (optional) ellipsis. */
24033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24035 /* Consume the `...'. */
24036 cp_lexer_consume_token (parser
->lexer
);
24038 pack_expansion_p
= true;
24041 /* Add BASE to the front of the list. */
24042 if (base
&& base
!= error_mark_node
)
24044 if (pack_expansion_p
)
24045 /* Make this a pack expansion type. */
24046 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24048 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24050 TREE_CHAIN (base
) = bases
;
24054 /* Peek at the next token. */
24055 token
= cp_lexer_peek_token (parser
->lexer
);
24056 /* If it's not a comma, then the list is complete. */
24057 if (token
->type
!= CPP_COMMA
)
24059 /* Consume the `,'. */
24060 cp_lexer_consume_token (parser
->lexer
);
24063 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24064 base class had a qualified name. However, the next name that
24065 appears is certainly not qualified. */
24066 parser
->scope
= NULL_TREE
;
24067 parser
->qualifying_scope
= NULL_TREE
;
24068 parser
->object_scope
= NULL_TREE
;
24070 return nreverse (bases
);
24073 /* Parse a base-specifier.
24076 :: [opt] nested-name-specifier [opt] class-name
24077 virtual access-specifier [opt] :: [opt] nested-name-specifier
24079 access-specifier virtual [opt] :: [opt] nested-name-specifier
24082 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24083 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24084 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24085 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24088 cp_parser_base_specifier (cp_parser
* parser
)
24092 bool virtual_p
= false;
24093 bool duplicate_virtual_error_issued_p
= false;
24094 bool duplicate_access_error_issued_p
= false;
24095 bool class_scope_p
, template_p
;
24096 tree access
= access_default_node
;
24099 /* Process the optional `virtual' and `access-specifier'. */
24102 /* Peek at the next token. */
24103 token
= cp_lexer_peek_token (parser
->lexer
);
24104 /* Process `virtual'. */
24105 switch (token
->keyword
)
24108 /* If `virtual' appears more than once, issue an error. */
24109 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24111 cp_parser_error (parser
,
24112 "%<virtual%> specified more than once in base-specifier");
24113 duplicate_virtual_error_issued_p
= true;
24118 /* Consume the `virtual' token. */
24119 cp_lexer_consume_token (parser
->lexer
);
24124 case RID_PROTECTED
:
24126 /* If more than one access specifier appears, issue an
24128 if (access
!= access_default_node
24129 && !duplicate_access_error_issued_p
)
24131 cp_parser_error (parser
,
24132 "more than one access specifier in base-specifier");
24133 duplicate_access_error_issued_p
= true;
24136 access
= ridpointers
[(int) token
->keyword
];
24138 /* Consume the access-specifier. */
24139 cp_lexer_consume_token (parser
->lexer
);
24148 /* It is not uncommon to see programs mechanically, erroneously, use
24149 the 'typename' keyword to denote (dependent) qualified types
24150 as base classes. */
24151 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24153 token
= cp_lexer_peek_token (parser
->lexer
);
24154 if (!processing_template_decl
)
24155 error_at (token
->location
,
24156 "keyword %<typename%> not allowed outside of templates");
24158 error_at (token
->location
,
24159 "keyword %<typename%> not allowed in this context "
24160 "(the base class is implicitly a type)");
24161 cp_lexer_consume_token (parser
->lexer
);
24164 /* Look for the optional `::' operator. */
24165 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24166 /* Look for the nested-name-specifier. The simplest way to
24171 The keyword `typename' is not permitted in a base-specifier or
24172 mem-initializer; in these contexts a qualified name that
24173 depends on a template-parameter is implicitly assumed to be a
24176 is to pretend that we have seen the `typename' keyword at this
24178 cp_parser_nested_name_specifier_opt (parser
,
24179 /*typename_keyword_p=*/true,
24180 /*check_dependency_p=*/true,
24182 /*is_declaration=*/true);
24183 /* If the base class is given by a qualified name, assume that names
24184 we see are type names or templates, as appropriate. */
24185 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24186 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24189 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24190 /* DR 950 allows decltype as a base-specifier. */
24191 type
= cp_parser_decltype (parser
);
24194 /* Otherwise, look for the class-name. */
24195 type
= cp_parser_class_name (parser
,
24199 /*check_dependency_p=*/true,
24200 /*class_head_p=*/false,
24201 /*is_declaration=*/true);
24202 type
= TREE_TYPE (type
);
24205 if (type
== error_mark_node
)
24206 return error_mark_node
;
24208 return finish_base_specifier (type
, access
, virtual_p
);
24211 /* Exception handling [gram.exception] */
24213 /* Parse an (optional) noexcept-specification.
24215 noexcept-specification:
24216 noexcept ( constant-expression ) [opt]
24218 If no noexcept-specification is present, returns NULL_TREE.
24219 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24220 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24221 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24222 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24223 in which case a boolean condition is returned instead. */
24226 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24227 bool require_constexpr
,
24228 bool* consumed_expr
,
24232 const char *saved_message
;
24234 /* Peek at the next token. */
24235 token
= cp_lexer_peek_token (parser
->lexer
);
24237 /* Is it a noexcept-specification? */
24238 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24241 cp_lexer_consume_token (parser
->lexer
);
24243 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24245 matching_parens parens
;
24246 parens
.consume_open (parser
);
24248 if (require_constexpr
)
24250 /* Types may not be defined in an exception-specification. */
24251 saved_message
= parser
->type_definition_forbidden_message
;
24252 parser
->type_definition_forbidden_message
24253 = G_("types may not be defined in an exception-specification");
24255 expr
= cp_parser_constant_expression (parser
);
24257 /* Restore the saved message. */
24258 parser
->type_definition_forbidden_message
= saved_message
;
24262 expr
= cp_parser_expression (parser
);
24263 *consumed_expr
= true;
24266 parens
.require_close (parser
);
24270 expr
= boolean_true_node
;
24271 if (!require_constexpr
)
24272 *consumed_expr
= false;
24275 /* We cannot build a noexcept-spec right away because this will check
24276 that expr is a constexpr. */
24278 return build_noexcept_spec (expr
, tf_warning_or_error
);
24286 /* Parse an (optional) exception-specification.
24288 exception-specification:
24289 throw ( type-id-list [opt] )
24291 Returns a TREE_LIST representing the exception-specification. The
24292 TREE_VALUE of each node is a type. */
24295 cp_parser_exception_specification_opt (cp_parser
* parser
)
24299 const char *saved_message
;
24301 /* Peek at the next token. */
24302 token
= cp_lexer_peek_token (parser
->lexer
);
24304 /* Is it a noexcept-specification? */
24305 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24307 if (type_id_list
!= NULL_TREE
)
24308 return type_id_list
;
24310 /* If it's not `throw', then there's no exception-specification. */
24311 if (!cp_parser_is_keyword (token
, RID_THROW
))
24314 location_t loc
= token
->location
;
24316 /* Consume the `throw'. */
24317 cp_lexer_consume_token (parser
->lexer
);
24319 /* Look for the `('. */
24320 matching_parens parens
;
24321 parens
.require_open (parser
);
24323 /* Peek at the next token. */
24324 token
= cp_lexer_peek_token (parser
->lexer
);
24325 /* If it's not a `)', then there is a type-id-list. */
24326 if (token
->type
!= CPP_CLOSE_PAREN
)
24328 /* Types may not be defined in an exception-specification. */
24329 saved_message
= parser
->type_definition_forbidden_message
;
24330 parser
->type_definition_forbidden_message
24331 = G_("types may not be defined in an exception-specification");
24332 /* Parse the type-id-list. */
24333 type_id_list
= cp_parser_type_id_list (parser
);
24334 /* Restore the saved message. */
24335 parser
->type_definition_forbidden_message
= saved_message
;
24337 if (cxx_dialect
>= cxx17
)
24339 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24341 type_id_list
= NULL_TREE
;
24343 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24344 warning_at (loc
, OPT_Wdeprecated
,
24345 "dynamic exception specifications are deprecated in "
24348 /* In C++17, throw() is equivalent to noexcept (true). throw()
24349 is deprecated in C++11 and above as well, but is still widely used,
24350 so don't warn about it yet. */
24351 else if (cxx_dialect
>= cxx17
)
24352 type_id_list
= noexcept_true_spec
;
24354 type_id_list
= empty_except_spec
;
24356 /* Look for the `)'. */
24357 parens
.require_close (parser
);
24359 return type_id_list
;
24362 /* Parse an (optional) type-id-list.
24366 type-id-list , type-id ... [opt]
24368 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24369 in the order that the types were presented. */
24372 cp_parser_type_id_list (cp_parser
* parser
)
24374 tree types
= NULL_TREE
;
24381 token
= cp_lexer_peek_token (parser
->lexer
);
24383 /* Get the next type-id. */
24384 type
= cp_parser_type_id (parser
);
24385 /* Check for invalid 'auto'. */
24386 if (flag_concepts
&& type_uses_auto (type
))
24388 error_at (token
->location
,
24389 "invalid use of %<auto%> in exception-specification");
24390 type
= error_mark_node
;
24392 /* Parse the optional ellipsis. */
24393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24395 /* Consume the `...'. */
24396 cp_lexer_consume_token (parser
->lexer
);
24398 /* Turn the type into a pack expansion expression. */
24399 type
= make_pack_expansion (type
);
24401 /* Add it to the list. */
24402 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24403 /* Peek at the next token. */
24404 token
= cp_lexer_peek_token (parser
->lexer
);
24405 /* If it is not a `,', we are done. */
24406 if (token
->type
!= CPP_COMMA
)
24408 /* Consume the `,'. */
24409 cp_lexer_consume_token (parser
->lexer
);
24412 return nreverse (types
);
24415 /* Parse a try-block.
24418 try compound-statement handler-seq */
24421 cp_parser_try_block (cp_parser
* parser
)
24425 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24426 if (parser
->in_function_body
24427 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24428 error ("%<try%> in %<constexpr%> function");
24430 try_block
= begin_try_block ();
24431 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24432 finish_try_block (try_block
);
24433 cp_parser_handler_seq (parser
);
24434 finish_handler_sequence (try_block
);
24439 /* Parse a function-try-block.
24441 function-try-block:
24442 try ctor-initializer [opt] function-body handler-seq */
24445 cp_parser_function_try_block (cp_parser
* parser
)
24447 tree compound_stmt
;
24450 /* Look for the `try' keyword. */
24451 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24453 /* Let the rest of the front end know where we are. */
24454 try_block
= begin_function_try_block (&compound_stmt
);
24455 /* Parse the function-body. */
24456 cp_parser_ctor_initializer_opt_and_function_body
24457 (parser
, /*in_function_try_block=*/true);
24458 /* We're done with the `try' part. */
24459 finish_function_try_block (try_block
);
24460 /* Parse the handlers. */
24461 cp_parser_handler_seq (parser
);
24462 /* We're done with the handlers. */
24463 finish_function_handler_sequence (try_block
, compound_stmt
);
24466 /* Parse a handler-seq.
24469 handler handler-seq [opt] */
24472 cp_parser_handler_seq (cp_parser
* parser
)
24478 /* Parse the handler. */
24479 cp_parser_handler (parser
);
24480 /* Peek at the next token. */
24481 token
= cp_lexer_peek_token (parser
->lexer
);
24482 /* If it's not `catch' then there are no more handlers. */
24483 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24488 /* Parse a handler.
24491 catch ( exception-declaration ) compound-statement */
24494 cp_parser_handler (cp_parser
* parser
)
24499 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24500 handler
= begin_handler ();
24501 matching_parens parens
;
24502 parens
.require_open (parser
);
24503 declaration
= cp_parser_exception_declaration (parser
);
24504 finish_handler_parms (declaration
, handler
);
24505 parens
.require_close (parser
);
24506 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24507 finish_handler (handler
);
24510 /* Parse an exception-declaration.
24512 exception-declaration:
24513 type-specifier-seq declarator
24514 type-specifier-seq abstract-declarator
24518 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24519 ellipsis variant is used. */
24522 cp_parser_exception_declaration (cp_parser
* parser
)
24524 cp_decl_specifier_seq type_specifiers
;
24525 cp_declarator
*declarator
;
24526 const char *saved_message
;
24528 /* If it's an ellipsis, it's easy to handle. */
24529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24531 /* Consume the `...' token. */
24532 cp_lexer_consume_token (parser
->lexer
);
24536 /* Types may not be defined in exception-declarations. */
24537 saved_message
= parser
->type_definition_forbidden_message
;
24538 parser
->type_definition_forbidden_message
24539 = G_("types may not be defined in exception-declarations");
24541 /* Parse the type-specifier-seq. */
24542 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24543 /*is_trailing_return=*/false,
24545 /* If it's a `)', then there is no declarator. */
24546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24549 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24550 /*ctor_dtor_or_conv_p=*/NULL
,
24551 /*parenthesized_p=*/NULL
,
24552 /*member_p=*/false,
24553 /*friend_p=*/false);
24555 /* Restore the saved message. */
24556 parser
->type_definition_forbidden_message
= saved_message
;
24558 if (!type_specifiers
.any_specifiers_p
)
24559 return error_mark_node
;
24561 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24564 /* Parse a throw-expression.
24567 throw assignment-expression [opt]
24569 Returns a THROW_EXPR representing the throw-expression. */
24572 cp_parser_throw_expression (cp_parser
* parser
)
24577 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24578 token
= cp_lexer_peek_token (parser
->lexer
);
24579 /* Figure out whether or not there is an assignment-expression
24580 following the "throw" keyword. */
24581 if (token
->type
== CPP_COMMA
24582 || token
->type
== CPP_SEMICOLON
24583 || token
->type
== CPP_CLOSE_PAREN
24584 || token
->type
== CPP_CLOSE_SQUARE
24585 || token
->type
== CPP_CLOSE_BRACE
24586 || token
->type
== CPP_COLON
)
24587 expression
= NULL_TREE
;
24589 expression
= cp_parser_assignment_expression (parser
);
24591 return build_throw (expression
);
24594 /* GNU Extensions */
24596 /* Parse an (optional) asm-specification.
24599 asm ( string-literal )
24601 If the asm-specification is present, returns a STRING_CST
24602 corresponding to the string-literal. Otherwise, returns
24606 cp_parser_asm_specification_opt (cp_parser
* parser
)
24609 tree asm_specification
;
24611 /* Peek at the next token. */
24612 token
= cp_lexer_peek_token (parser
->lexer
);
24613 /* If the next token isn't the `asm' keyword, then there's no
24614 asm-specification. */
24615 if (!cp_parser_is_keyword (token
, RID_ASM
))
24618 /* Consume the `asm' token. */
24619 cp_lexer_consume_token (parser
->lexer
);
24620 /* Look for the `('. */
24621 matching_parens parens
;
24622 parens
.require_open (parser
);
24624 /* Look for the string-literal. */
24625 asm_specification
= cp_parser_string_literal (parser
, false, false);
24627 /* Look for the `)'. */
24628 parens
.require_close (parser
);
24630 return asm_specification
;
24633 /* Parse an asm-operand-list.
24637 asm-operand-list , asm-operand
24640 string-literal ( expression )
24641 [ string-literal ] string-literal ( expression )
24643 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24644 each node is the expression. The TREE_PURPOSE is itself a
24645 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24646 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24647 is a STRING_CST for the string literal before the parenthesis. Returns
24648 ERROR_MARK_NODE if any of the operands are invalid. */
24651 cp_parser_asm_operand_list (cp_parser
* parser
)
24653 tree asm_operands
= NULL_TREE
;
24654 bool invalid_operands
= false;
24658 tree string_literal
;
24662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24664 /* Consume the `[' token. */
24665 cp_lexer_consume_token (parser
->lexer
);
24666 /* Read the operand name. */
24667 name
= cp_parser_identifier (parser
);
24668 if (name
!= error_mark_node
)
24669 name
= build_string (IDENTIFIER_LENGTH (name
),
24670 IDENTIFIER_POINTER (name
));
24671 /* Look for the closing `]'. */
24672 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24676 /* Look for the string-literal. */
24677 string_literal
= cp_parser_string_literal (parser
, false, false);
24679 /* Look for the `('. */
24680 matching_parens parens
;
24681 parens
.require_open (parser
);
24682 /* Parse the expression. */
24683 expression
= cp_parser_expression (parser
);
24684 /* Look for the `)'. */
24685 parens
.require_close (parser
);
24687 if (name
== error_mark_node
24688 || string_literal
== error_mark_node
24689 || expression
== error_mark_node
)
24690 invalid_operands
= true;
24692 /* Add this operand to the list. */
24693 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24696 /* If the next token is not a `,', there are no more
24698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24700 /* Consume the `,'. */
24701 cp_lexer_consume_token (parser
->lexer
);
24704 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24707 /* Parse an asm-clobber-list.
24711 asm-clobber-list , string-literal
24713 Returns a TREE_LIST, indicating the clobbers in the order that they
24714 appeared. The TREE_VALUE of each node is a STRING_CST. */
24717 cp_parser_asm_clobber_list (cp_parser
* parser
)
24719 tree clobbers
= NULL_TREE
;
24723 tree string_literal
;
24725 /* Look for the string literal. */
24726 string_literal
= cp_parser_string_literal (parser
, false, false);
24727 /* Add it to the list. */
24728 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24729 /* If the next token is not a `,', then the list is
24731 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24733 /* Consume the `,' token. */
24734 cp_lexer_consume_token (parser
->lexer
);
24740 /* Parse an asm-label-list.
24744 asm-label-list , identifier
24746 Returns a TREE_LIST, indicating the labels in the order that they
24747 appeared. The TREE_VALUE of each node is a label. */
24750 cp_parser_asm_label_list (cp_parser
* parser
)
24752 tree labels
= NULL_TREE
;
24756 tree identifier
, label
, name
;
24758 /* Look for the identifier. */
24759 identifier
= cp_parser_identifier (parser
);
24760 if (!error_operand_p (identifier
))
24762 label
= lookup_label (identifier
);
24763 if (TREE_CODE (label
) == LABEL_DECL
)
24765 TREE_USED (label
) = 1;
24766 check_goto (label
);
24767 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24768 IDENTIFIER_POINTER (identifier
));
24769 labels
= tree_cons (name
, label
, labels
);
24772 /* If the next token is not a `,', then the list is
24774 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24776 /* Consume the `,' token. */
24777 cp_lexer_consume_token (parser
->lexer
);
24780 return nreverse (labels
);
24783 /* Return TRUE iff the next tokens in the stream are possibly the
24784 beginning of a GNU extension attribute. */
24787 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24789 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24792 /* Return TRUE iff the next tokens in the stream are possibly the
24793 beginning of a standard C++-11 attribute specifier. */
24796 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24798 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24801 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24802 beginning of a standard C++-11 attribute specifier. */
24805 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24807 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24809 return (cxx_dialect
>= cxx11
24810 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24811 || (token
->type
== CPP_OPEN_SQUARE
24812 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24813 && token
->type
== CPP_OPEN_SQUARE
)));
24816 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24817 beginning of a GNU extension attribute. */
24820 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24822 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24824 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24827 /* Return true iff the next tokens can be the beginning of either a
24828 GNU attribute list, or a standard C++11 attribute sequence. */
24831 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24833 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24834 || cp_next_tokens_can_be_std_attribute_p (parser
));
24837 /* Return true iff the next Nth tokens can be the beginning of either
24838 a GNU attribute list, or a standard C++11 attribute sequence. */
24841 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24843 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24844 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24847 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24848 of GNU attributes, or return NULL. */
24851 cp_parser_attributes_opt (cp_parser
*parser
)
24853 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24854 return cp_parser_gnu_attributes_opt (parser
);
24855 return cp_parser_std_attribute_spec_seq (parser
);
24858 /* Parse an (optional) series of attributes.
24861 attributes attribute
24864 __attribute__ (( attribute-list [opt] ))
24866 The return value is as for cp_parser_gnu_attribute_list. */
24869 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24871 tree attributes
= NULL_TREE
;
24876 tree attribute_list
;
24879 /* Peek at the next token. */
24880 token
= cp_lexer_peek_token (parser
->lexer
);
24881 /* If it's not `__attribute__', then we're done. */
24882 if (token
->keyword
!= RID_ATTRIBUTE
)
24885 /* Consume the `__attribute__' keyword. */
24886 cp_lexer_consume_token (parser
->lexer
);
24887 /* Look for the two `(' tokens. */
24888 matching_parens outer_parens
;
24889 outer_parens
.require_open (parser
);
24890 matching_parens inner_parens
;
24891 inner_parens
.require_open (parser
);
24893 /* Peek at the next token. */
24894 token
= cp_lexer_peek_token (parser
->lexer
);
24895 if (token
->type
!= CPP_CLOSE_PAREN
)
24896 /* Parse the attribute-list. */
24897 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24899 /* If the next token is a `)', then there is no attribute
24901 attribute_list
= NULL
;
24903 /* Look for the two `)' tokens. */
24904 if (!inner_parens
.require_close (parser
))
24906 if (!outer_parens
.require_close (parser
))
24909 cp_parser_skip_to_end_of_statement (parser
);
24911 /* Add these new attributes to the list. */
24912 attributes
= chainon (attributes
, attribute_list
);
24918 /* Parse a GNU attribute-list.
24922 attribute-list , attribute
24926 identifier ( identifier )
24927 identifier ( identifier , expression-list )
24928 identifier ( expression-list )
24930 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24931 to an attribute. The TREE_PURPOSE of each node is the identifier
24932 indicating which attribute is in use. The TREE_VALUE represents
24933 the arguments, if any. */
24936 cp_parser_gnu_attribute_list (cp_parser
* parser
)
24938 tree attribute_list
= NULL_TREE
;
24939 bool save_translate_strings_p
= parser
->translate_strings_p
;
24941 parser
->translate_strings_p
= false;
24948 /* Look for the identifier. We also allow keywords here; for
24949 example `__attribute__ ((const))' is legal. */
24950 token
= cp_lexer_peek_token (parser
->lexer
);
24951 if (token
->type
== CPP_NAME
24952 || token
->type
== CPP_KEYWORD
)
24954 tree arguments
= NULL_TREE
;
24956 /* Consume the token, but save it since we need it for the
24957 SIMD enabled function parsing. */
24958 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
24960 /* Save away the identifier that indicates which attribute
24962 identifier
= (token
->type
== CPP_KEYWORD
)
24963 /* For keywords, use the canonical spelling, not the
24964 parsed identifier. */
24965 ? ridpointers
[(int) token
->keyword
]
24966 : id_token
->u
.value
;
24968 identifier
= canonicalize_attr_name (identifier
);
24969 attribute
= build_tree_list (identifier
, NULL_TREE
);
24971 /* Peek at the next token. */
24972 token
= cp_lexer_peek_token (parser
->lexer
);
24973 /* If it's an `(', then parse the attribute arguments. */
24974 if (token
->type
== CPP_OPEN_PAREN
)
24976 vec
<tree
, va_gc
> *vec
;
24977 int attr_flag
= (attribute_takes_identifier_p (identifier
)
24978 ? id_attr
: normal_attr
);
24979 vec
= cp_parser_parenthesized_expression_list
24980 (parser
, attr_flag
, /*cast_p=*/false,
24981 /*allow_expansion_p=*/false,
24982 /*non_constant_p=*/NULL
);
24984 arguments
= error_mark_node
;
24987 arguments
= build_tree_list_vec (vec
);
24988 release_tree_vector (vec
);
24990 /* Save the arguments away. */
24991 TREE_VALUE (attribute
) = arguments
;
24994 if (arguments
!= error_mark_node
)
24996 /* Add this attribute to the list. */
24997 TREE_CHAIN (attribute
) = attribute_list
;
24998 attribute_list
= attribute
;
25001 token
= cp_lexer_peek_token (parser
->lexer
);
25003 /* Now, look for more attributes. If the next token isn't a
25004 `,', we're done. */
25005 if (token
->type
!= CPP_COMMA
)
25008 /* Consume the comma and keep going. */
25009 cp_lexer_consume_token (parser
->lexer
);
25011 parser
->translate_strings_p
= save_translate_strings_p
;
25013 /* We built up the list in reverse order. */
25014 return nreverse (attribute_list
);
25017 /* Parse a standard C++11 attribute.
25019 The returned representation is a TREE_LIST which TREE_PURPOSE is
25020 the scoped name of the attribute, and the TREE_VALUE is its
25023 Note that the scoped name of the attribute is itself a TREE_LIST
25024 which TREE_PURPOSE is the namespace of the attribute, and
25025 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25026 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25027 and which TREE_PURPOSE is directly the attribute name.
25029 Clients of the attribute code should use get_attribute_namespace
25030 and get_attribute_name to get the actual namespace and name of
25031 attributes, regardless of their being GNU or C++11 attributes.
25034 attribute-token attribute-argument-clause [opt]
25038 attribute-scoped-token
25040 attribute-scoped-token:
25041 attribute-namespace :: identifier
25043 attribute-namespace:
25046 attribute-argument-clause:
25047 ( balanced-token-seq )
25049 balanced-token-seq:
25050 balanced-token [opt]
25051 balanced-token-seq balanced-token
25054 ( balanced-token-seq )
25055 [ balanced-token-seq ]
25056 { balanced-token-seq }. */
25059 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25061 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25064 /* First, parse name of the attribute, a.k.a attribute-token. */
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25067 if (token
->type
== CPP_NAME
)
25068 attr_id
= token
->u
.value
;
25069 else if (token
->type
== CPP_KEYWORD
)
25070 attr_id
= ridpointers
[(int) token
->keyword
];
25071 else if (token
->flags
& NAMED_OP
)
25072 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25074 if (attr_id
== NULL_TREE
)
25077 cp_lexer_consume_token (parser
->lexer
);
25079 token
= cp_lexer_peek_token (parser
->lexer
);
25080 if (token
->type
== CPP_SCOPE
)
25082 /* We are seeing a scoped attribute token. */
25084 cp_lexer_consume_token (parser
->lexer
);
25086 error_at (token
->location
, "attribute using prefix used together "
25087 "with scoped attribute token");
25090 token
= cp_lexer_consume_token (parser
->lexer
);
25091 if (token
->type
== CPP_NAME
)
25092 attr_id
= token
->u
.value
;
25093 else if (token
->type
== CPP_KEYWORD
)
25094 attr_id
= ridpointers
[(int) token
->keyword
];
25095 else if (token
->flags
& NAMED_OP
)
25096 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25099 error_at (token
->location
,
25100 "expected an identifier for the attribute name");
25101 return error_mark_node
;
25104 attr_id
= canonicalize_attr_name (attr_id
);
25105 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25107 token
= cp_lexer_peek_token (parser
->lexer
);
25110 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25114 attr_id
= canonicalize_attr_name (attr_id
);
25115 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25117 /* C++11 noreturn attribute is equivalent to GNU's. */
25118 if (is_attribute_p ("noreturn", attr_id
))
25119 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25120 /* C++14 deprecated attribute is equivalent to GNU's. */
25121 else if (is_attribute_p ("deprecated", attr_id
))
25122 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25123 /* C++17 fallthrough attribute is equivalent to GNU's. */
25124 else if (is_attribute_p ("fallthrough", attr_id
))
25125 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25126 /* Transactional Memory TS optimize_for_synchronized attribute is
25127 equivalent to GNU transaction_callable. */
25128 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25129 TREE_PURPOSE (attribute
)
25130 = get_identifier ("transaction_callable");
25131 /* Transactional Memory attributes are GNU attributes. */
25132 else if (tm_attr_to_mask (attr_id
))
25133 TREE_PURPOSE (attribute
) = attr_id
;
25136 /* Now parse the optional argument clause of the attribute. */
25138 if (token
->type
!= CPP_OPEN_PAREN
)
25142 vec
<tree
, va_gc
> *vec
;
25143 int attr_flag
= normal_attr
;
25145 if (attr_ns
== get_identifier ("gnu")
25146 && attribute_takes_identifier_p (attr_id
))
25147 /* A GNU attribute that takes an identifier in parameter. */
25148 attr_flag
= id_attr
;
25150 vec
= cp_parser_parenthesized_expression_list
25151 (parser
, attr_flag
, /*cast_p=*/false,
25152 /*allow_expansion_p=*/true,
25153 /*non_constant_p=*/NULL
);
25155 arguments
= error_mark_node
;
25158 arguments
= build_tree_list_vec (vec
);
25159 release_tree_vector (vec
);
25162 if (arguments
== error_mark_node
)
25163 attribute
= error_mark_node
;
25165 TREE_VALUE (attribute
) = arguments
;
25171 /* Check that the attribute ATTRIBUTE appears at most once in the
25172 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25173 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25174 isn't implemented yet in GCC. */
25177 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25181 tree name
= get_attribute_name (attribute
);
25182 if (is_attribute_p ("noreturn", name
)
25183 && lookup_attribute ("noreturn", attributes
))
25184 error ("attribute %<noreturn%> can appear at most once "
25185 "in an attribute-list");
25186 else if (is_attribute_p ("deprecated", name
)
25187 && lookup_attribute ("deprecated", attributes
))
25188 error ("attribute %<deprecated%> can appear at most once "
25189 "in an attribute-list");
25193 /* Parse a list of standard C++-11 attributes.
25197 attribute-list , attribute[opt]
25199 attribute-list , attribute ...
25203 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25205 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25206 cp_token
*token
= NULL
;
25210 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25211 if (attribute
== error_mark_node
)
25213 if (attribute
!= NULL_TREE
)
25215 cp_parser_check_std_attribute (attributes
, attribute
);
25216 TREE_CHAIN (attribute
) = attributes
;
25217 attributes
= attribute
;
25219 token
= cp_lexer_peek_token (parser
->lexer
);
25220 if (token
->type
== CPP_ELLIPSIS
)
25222 cp_lexer_consume_token (parser
->lexer
);
25223 if (attribute
== NULL_TREE
)
25224 error_at (token
->location
,
25225 "expected attribute before %<...%>");
25228 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25229 if (pack
== error_mark_node
)
25230 return error_mark_node
;
25231 TREE_VALUE (attribute
) = pack
;
25233 token
= cp_lexer_peek_token (parser
->lexer
);
25235 if (token
->type
!= CPP_COMMA
)
25237 cp_lexer_consume_token (parser
->lexer
);
25239 attributes
= nreverse (attributes
);
25243 /* Parse a standard C++-11 attribute specifier.
25245 attribute-specifier:
25246 [ [ attribute-using-prefix [opt] attribute-list ] ]
25247 alignment-specifier
25249 attribute-using-prefix:
25250 using attribute-namespace :
25252 alignment-specifier:
25253 alignas ( type-id ... [opt] )
25254 alignas ( alignment-expression ... [opt] ). */
25257 cp_parser_std_attribute_spec (cp_parser
*parser
)
25259 tree attributes
= NULL_TREE
;
25260 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25262 if (token
->type
== CPP_OPEN_SQUARE
25263 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25265 tree attr_ns
= NULL_TREE
;
25267 cp_lexer_consume_token (parser
->lexer
);
25268 cp_lexer_consume_token (parser
->lexer
);
25270 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25272 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25273 if (token
->type
== CPP_NAME
)
25274 attr_ns
= token
->u
.value
;
25275 else if (token
->type
== CPP_KEYWORD
)
25276 attr_ns
= ridpointers
[(int) token
->keyword
];
25277 else if (token
->flags
& NAMED_OP
)
25278 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25281 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25283 if (cxx_dialect
< cxx17
25284 && !in_system_header_at (input_location
))
25285 pedwarn (input_location
, 0,
25286 "attribute using prefix only available "
25287 "with -std=c++17 or -std=gnu++17");
25289 cp_lexer_consume_token (parser
->lexer
);
25290 cp_lexer_consume_token (parser
->lexer
);
25291 cp_lexer_consume_token (parser
->lexer
);
25294 attr_ns
= NULL_TREE
;
25297 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25299 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25300 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25301 cp_parser_skip_to_end_of_statement (parser
);
25303 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25304 when we are sure that we have actually parsed them. */
25305 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25311 /* Look for an alignment-specifier. */
25313 token
= cp_lexer_peek_token (parser
->lexer
);
25315 if (token
->type
!= CPP_KEYWORD
25316 || token
->keyword
!= RID_ALIGNAS
)
25319 cp_lexer_consume_token (parser
->lexer
);
25320 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25322 matching_parens parens
;
25323 if (!parens
.require_open (parser
))
25324 return error_mark_node
;
25326 cp_parser_parse_tentatively (parser
);
25327 alignas_expr
= cp_parser_type_id (parser
);
25329 if (!cp_parser_parse_definitely (parser
))
25331 alignas_expr
= cp_parser_assignment_expression (parser
);
25332 if (alignas_expr
== error_mark_node
)
25333 cp_parser_skip_to_end_of_statement (parser
);
25334 if (alignas_expr
== NULL_TREE
25335 || alignas_expr
== error_mark_node
)
25336 return alignas_expr
;
25339 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25340 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25342 /* Handle alignas (pack...). */
25343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25345 cp_lexer_consume_token (parser
->lexer
);
25346 alignas_expr
= make_pack_expansion (alignas_expr
);
25349 /* Something went wrong, so don't build the attribute. */
25350 if (alignas_expr
== error_mark_node
)
25351 return error_mark_node
;
25353 if (!parens
.require_close (parser
))
25354 return error_mark_node
;
25356 /* Build the C++-11 representation of an 'aligned'
25359 build_tree_list (build_tree_list (get_identifier ("gnu"),
25360 get_identifier ("aligned")),
25367 /* Parse a standard C++-11 attribute-specifier-seq.
25369 attribute-specifier-seq:
25370 attribute-specifier-seq [opt] attribute-specifier
25374 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25376 tree attr_specs
= NULL_TREE
;
25377 tree attr_last
= NULL_TREE
;
25381 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25382 if (attr_spec
== NULL_TREE
)
25384 if (attr_spec
== error_mark_node
)
25385 return error_mark_node
;
25388 TREE_CHAIN (attr_last
) = attr_spec
;
25390 attr_specs
= attr_last
= attr_spec
;
25391 attr_last
= tree_last (attr_last
);
25397 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25398 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25399 current value of the PEDANTIC flag, regardless of whether or not
25400 the `__extension__' keyword is present. The caller is responsible
25401 for restoring the value of the PEDANTIC flag. */
25404 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25406 /* Save the old value of the PEDANTIC flag. */
25407 *saved_pedantic
= pedantic
;
25409 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25411 /* Consume the `__extension__' token. */
25412 cp_lexer_consume_token (parser
->lexer
);
25413 /* We're not being pedantic while the `__extension__' keyword is
25423 /* Parse a label declaration.
25426 __label__ label-declarator-seq ;
25428 label-declarator-seq:
25429 identifier , label-declarator-seq
25433 cp_parser_label_declaration (cp_parser
* parser
)
25435 /* Look for the `__label__' keyword. */
25436 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25442 /* Look for an identifier. */
25443 identifier
= cp_parser_identifier (parser
);
25444 /* If we failed, stop. */
25445 if (identifier
== error_mark_node
)
25447 /* Declare it as a label. */
25448 finish_label_decl (identifier
);
25449 /* If the next token is a `;', stop. */
25450 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25452 /* Look for the `,' separating the label declarations. */
25453 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25456 /* Look for the final `;'. */
25457 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25460 // -------------------------------------------------------------------------- //
25463 // Parse a requires clause.
25465 // requires-clause:
25466 // 'requires' logical-or-expression
25468 // The required logical-or-expression must be a constant expression. Note
25469 // that we don't check that the expression is constepxr here. We defer until
25470 // we analyze constraints and then, we only check atomic constraints.
25472 cp_parser_requires_clause (cp_parser
*parser
)
25474 // Parse the requires clause so that it is not automatically folded.
25475 ++processing_template_decl
;
25476 tree expr
= cp_parser_binary_expression (parser
, false, false,
25477 PREC_NOT_OPERATOR
, NULL
);
25478 if (check_for_bare_parameter_packs (expr
))
25479 expr
= error_mark_node
;
25480 --processing_template_decl
;
25484 // Optionally parse a requires clause:
25486 cp_parser_requires_clause_opt (cp_parser
*parser
)
25488 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25489 if (tok
->keyword
!= RID_REQUIRES
)
25491 if (!flag_concepts
&& tok
->type
== CPP_NAME
25492 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25494 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25495 "%<requires%> only available with -fconcepts");
25496 /* Parse and discard the requires-clause. */
25497 cp_lexer_consume_token (parser
->lexer
);
25498 cp_parser_requires_clause (parser
);
25502 cp_lexer_consume_token (parser
->lexer
);
25503 return cp_parser_requires_clause (parser
);
25507 /*---------------------------------------------------------------------------
25508 Requires expressions
25509 ---------------------------------------------------------------------------*/
25511 /* Parse a requires expression
25513 requirement-expression:
25514 'requires' requirement-parameter-list [opt] requirement-body */
25516 cp_parser_requires_expression (cp_parser
*parser
)
25518 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25519 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25521 /* A requires-expression shall appear only within a concept
25522 definition or a requires-clause.
25524 TODO: Implement this diagnostic correctly. */
25525 if (!processing_template_decl
)
25527 error_at (loc
, "a requires expression cannot appear outside a template");
25528 cp_parser_skip_to_end_of_statement (parser
);
25529 return error_mark_node
;
25534 /* Local parameters are delared as variables within the scope
25535 of the expression. They are not visible past the end of
25536 the expression. Expressions within the requires-expression
25537 are unevaluated. */
25538 struct scope_sentinel
25542 ++cp_unevaluated_operand
;
25543 begin_scope (sk_block
, NULL_TREE
);
25548 pop_bindings_and_leave_scope ();
25549 --cp_unevaluated_operand
;
25553 /* Parse the optional parameter list. */
25554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25556 parms
= cp_parser_requirement_parameter_list (parser
);
25557 if (parms
== error_mark_node
)
25558 return error_mark_node
;
25563 /* Parse the requirement body. */
25564 reqs
= cp_parser_requirement_body (parser
);
25565 if (reqs
== error_mark_node
)
25566 return error_mark_node
;
25569 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25571 grokparms (parms
, &parms
);
25572 return finish_requires_expr (parms
, reqs
);
25575 /* Parse a parameterized requirement.
25577 requirement-parameter-list:
25578 '(' parameter-declaration-clause ')' */
25580 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25582 matching_parens parens
;
25583 if (!parens
.require_open (parser
))
25584 return error_mark_node
;
25586 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25588 if (!parens
.require_close (parser
))
25589 return error_mark_node
;
25594 /* Parse the body of a requirement.
25597 '{' requirement-list '}' */
25599 cp_parser_requirement_body (cp_parser
*parser
)
25601 matching_braces braces
;
25602 if (!braces
.require_open (parser
))
25603 return error_mark_node
;
25605 tree reqs
= cp_parser_requirement_list (parser
);
25607 if (!braces
.require_close (parser
))
25608 return error_mark_node
;
25613 /* Parse a list of requirements.
25617 requirement-list ';' requirement[opt] */
25619 cp_parser_requirement_list (cp_parser
*parser
)
25621 tree result
= NULL_TREE
;
25624 tree req
= cp_parser_requirement (parser
);
25625 if (req
== error_mark_node
)
25626 return error_mark_node
;
25628 result
= tree_cons (NULL_TREE
, req
, result
);
25630 /* If we see a semi-colon, consume it. */
25631 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25632 cp_lexer_consume_token (parser
->lexer
);
25634 /* Stop processing at the end of the list. */
25635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25639 /* Reverse the order of requirements so they are analyzed in
25640 declaration order. */
25641 return nreverse (result
);
25644 /* Parse a syntactic requirement or type requirement.
25648 compound-requirement
25650 nested-requirement */
25652 cp_parser_requirement (cp_parser
*parser
)
25654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25655 return cp_parser_compound_requirement (parser
);
25656 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25657 return cp_parser_type_requirement (parser
);
25658 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25659 return cp_parser_nested_requirement (parser
);
25661 return cp_parser_simple_requirement (parser
);
25664 /* Parse a simple requirement.
25666 simple-requirement:
25669 cp_parser_simple_requirement (cp_parser
*parser
)
25671 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25672 if (!expr
|| expr
== error_mark_node
)
25673 return error_mark_node
;
25675 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25676 return error_mark_node
;
25678 return finish_simple_requirement (expr
);
25681 /* Parse a type requirement
25684 nested-name-specifier [opt] required-type-name ';'
25686 required-type-name:
25688 'template' [opt] simple-template-id */
25690 cp_parser_type_requirement (cp_parser
*parser
)
25692 cp_lexer_consume_token (parser
->lexer
);
25694 // Save the scope before parsing name specifiers.
25695 tree saved_scope
= parser
->scope
;
25696 tree saved_object_scope
= parser
->object_scope
;
25697 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25698 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25699 cp_parser_nested_name_specifier_opt (parser
,
25700 /*typename_keyword_p=*/true,
25701 /*check_dependency_p=*/false,
25703 /*is_declaration=*/false);
25706 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25708 cp_lexer_consume_token (parser
->lexer
);
25709 type
= cp_parser_template_id (parser
,
25710 /*template_keyword_p=*/true,
25711 /*check_dependency=*/false,
25712 /*tag_type=*/none_type
,
25713 /*is_declaration=*/false);
25714 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25715 /*complain=*/tf_error
);
25718 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25720 if (TREE_CODE (type
) == TYPE_DECL
)
25721 type
= TREE_TYPE (type
);
25723 parser
->scope
= saved_scope
;
25724 parser
->object_scope
= saved_object_scope
;
25725 parser
->qualifying_scope
= saved_qualifying_scope
;
25727 if (type
== error_mark_node
)
25728 cp_parser_skip_to_end_of_statement (parser
);
25730 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25731 return error_mark_node
;
25732 if (type
== error_mark_node
)
25733 return error_mark_node
;
25735 return finish_type_requirement (type
);
25738 /* Parse a compound requirement
25740 compound-requirement:
25741 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25743 cp_parser_compound_requirement (cp_parser
*parser
)
25745 /* Parse an expression enclosed in '{ }'s. */
25746 matching_braces braces
;
25747 if (!braces
.require_open (parser
))
25748 return error_mark_node
;
25750 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25751 if (!expr
|| expr
== error_mark_node
)
25752 return error_mark_node
;
25754 if (!braces
.require_close (parser
))
25755 return error_mark_node
;
25757 /* Parse the optional noexcept. */
25758 bool noexcept_p
= false;
25759 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25761 cp_lexer_consume_token (parser
->lexer
);
25765 /* Parse the optional trailing return type. */
25766 tree type
= NULL_TREE
;
25767 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25769 cp_lexer_consume_token (parser
->lexer
);
25770 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25771 parser
->in_result_type_constraint_p
= true;
25772 type
= cp_parser_trailing_type_id (parser
);
25773 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25774 if (type
== error_mark_node
)
25775 return error_mark_node
;
25778 return finish_compound_requirement (expr
, type
, noexcept_p
);
25781 /* Parse a nested requirement. This is the same as a requires clause.
25783 nested-requirement:
25786 cp_parser_nested_requirement (cp_parser
*parser
)
25788 cp_lexer_consume_token (parser
->lexer
);
25789 tree req
= cp_parser_requires_clause (parser
);
25790 if (req
== error_mark_node
)
25791 return error_mark_node
;
25792 return finish_nested_requirement (req
);
25795 /* Support Functions */
25797 /* Return the appropriate prefer_type argument for lookup_name_real based on
25798 tag_type and template_mem_access. */
25801 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
25803 /* DR 141: When looking in the current enclosing context for a template-name
25804 after -> or ., only consider class templates. */
25805 if (template_mem_access
)
25809 case none_type
: return 0; // No preference.
25810 case scope_type
: return 1; // Type or namespace.
25811 default: return 2; // Type only.
25815 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25816 NAME should have one of the representations used for an
25817 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25818 is returned. If PARSER->SCOPE is a dependent type, then a
25819 SCOPE_REF is returned.
25821 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25822 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25823 was formed. Abstractly, such entities should not be passed to this
25824 function, because they do not need to be looked up, but it is
25825 simpler to check for this special case here, rather than at the
25828 In cases not explicitly covered above, this function returns a
25829 DECL, OVERLOAD, or baselink representing the result of the lookup.
25830 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25833 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25834 (e.g., "struct") that was used. In that case bindings that do not
25835 refer to types are ignored.
25837 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25840 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25843 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25846 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25847 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25848 NULL_TREE otherwise. */
25851 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
25852 enum tag_types tag_type
,
25855 bool check_dependency
,
25856 tree
*ambiguous_decls
,
25857 location_t name_location
)
25860 tree object_type
= parser
->context
->object_type
;
25862 /* Assume that the lookup will be unambiguous. */
25863 if (ambiguous_decls
)
25864 *ambiguous_decls
= NULL_TREE
;
25866 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25867 no longer valid. Note that if we are parsing tentatively, and
25868 the parse fails, OBJECT_TYPE will be automatically restored. */
25869 parser
->context
->object_type
= NULL_TREE
;
25871 if (name
== error_mark_node
)
25872 return error_mark_node
;
25874 /* A template-id has already been resolved; there is no lookup to
25876 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
25878 if (BASELINK_P (name
))
25880 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
25881 == TEMPLATE_ID_EXPR
);
25885 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25886 it should already have been checked to make sure that the name
25887 used matches the type being destroyed. */
25888 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
25892 /* Figure out to which type this destructor applies. */
25894 type
= parser
->scope
;
25895 else if (object_type
)
25896 type
= object_type
;
25898 type
= current_class_type
;
25899 /* If that's not a class type, there is no destructor. */
25900 if (!type
|| !CLASS_TYPE_P (type
))
25901 return error_mark_node
;
25903 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
25904 lazily_declare_fn (sfk_destructor
, type
);
25906 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
25909 return error_mark_node
;
25912 /* By this point, the NAME should be an ordinary identifier. If
25913 the id-expression was a qualified name, the qualifying scope is
25914 stored in PARSER->SCOPE at this point. */
25915 gcc_assert (identifier_p (name
));
25917 /* Perform the lookup. */
25922 if (parser
->scope
== error_mark_node
)
25923 return error_mark_node
;
25925 /* If the SCOPE is dependent, the lookup must be deferred until
25926 the template is instantiated -- unless we are explicitly
25927 looking up names in uninstantiated templates. Even then, we
25928 cannot look up the name if the scope is not a class type; it
25929 might, for example, be a template type parameter. */
25930 dependent_p
= (TYPE_P (parser
->scope
)
25931 && dependent_scope_p (parser
->scope
));
25932 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
25934 /* Defer lookup. */
25935 decl
= error_mark_node
;
25938 tree pushed_scope
= NULL_TREE
;
25940 /* If PARSER->SCOPE is a dependent type, then it must be a
25941 class type, and we must not be checking dependencies;
25942 otherwise, we would have processed this lookup above. So
25943 that PARSER->SCOPE is not considered a dependent base by
25944 lookup_member, we must enter the scope here. */
25946 pushed_scope
= push_scope (parser
->scope
);
25948 /* If the PARSER->SCOPE is a template specialization, it
25949 may be instantiated during name lookup. In that case,
25950 errors may be issued. Even if we rollback the current
25951 tentative parse, those errors are valid. */
25952 decl
= lookup_qualified_name (parser
->scope
, name
,
25953 prefer_type_arg (tag_type
),
25954 /*complain=*/true);
25956 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25957 lookup result and the nested-name-specifier nominates a class C:
25958 * if the name specified after the nested-name-specifier, when
25959 looked up in C, is the injected-class-name of C (Clause 9), or
25960 * if the name specified after the nested-name-specifier is the
25961 same as the identifier or the simple-template-id's template-
25962 name in the last component of the nested-name-specifier,
25963 the name is instead considered to name the constructor of
25964 class C. [ Note: for example, the constructor is not an
25965 acceptable lookup result in an elaborated-type-specifier so
25966 the constructor would not be used in place of the
25967 injected-class-name. --end note ] Such a constructor name
25968 shall be used only in the declarator-id of a declaration that
25969 names a constructor or in a using-declaration. */
25970 if (tag_type
== none_type
25971 && DECL_SELF_REFERENCE_P (decl
)
25972 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
25973 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
25974 prefer_type_arg (tag_type
),
25975 /*complain=*/true);
25977 /* If we have a single function from a using decl, pull it out. */
25978 if (TREE_CODE (decl
) == OVERLOAD
25979 && !really_overloaded_fn (decl
))
25980 decl
= OVL_FUNCTION (decl
);
25983 pop_scope (pushed_scope
);
25986 /* If the scope is a dependent type and either we deferred lookup or
25987 we did lookup but didn't find the name, rememeber the name. */
25988 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
25989 && dependent_type_p (parser
->scope
))
25995 /* The resolution to Core Issue 180 says that `struct
25996 A::B' should be considered a type-name, even if `A'
25998 type
= make_typename_type (parser
->scope
, name
, tag_type
,
25999 /*complain=*/tf_error
);
26000 if (type
!= error_mark_node
)
26001 decl
= TYPE_NAME (type
);
26003 else if (is_template
26004 && (cp_parser_next_token_ends_template_argument_p (parser
)
26005 || cp_lexer_next_token_is (parser
->lexer
,
26007 decl
= make_unbound_class_template (parser
->scope
,
26009 /*complain=*/tf_error
);
26011 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26012 parser
->scope
, name
,
26015 parser
->qualifying_scope
= parser
->scope
;
26016 parser
->object_scope
= NULL_TREE
;
26018 else if (object_type
)
26020 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26021 OBJECT_TYPE is not a class. */
26022 if (CLASS_TYPE_P (object_type
))
26023 /* If the OBJECT_TYPE is a template specialization, it may
26024 be instantiated during name lookup. In that case, errors
26025 may be issued. Even if we rollback the current tentative
26026 parse, those errors are valid. */
26027 decl
= lookup_member (object_type
,
26030 prefer_type_arg (tag_type
),
26031 tf_warning_or_error
);
26036 /* Look it up in the enclosing context. DR 141: When looking for a
26037 template-name after -> or ., only consider class templates. */
26038 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26040 /*block_p=*/true, is_namespace
, 0);
26041 if (object_type
== unknown_type_node
)
26042 /* The object is type-dependent, so we can't look anything up; we used
26043 this to get the DR 141 behavior. */
26044 object_type
= NULL_TREE
;
26045 parser
->object_scope
= object_type
;
26046 parser
->qualifying_scope
= NULL_TREE
;
26050 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26052 /*block_p=*/true, is_namespace
, 0);
26053 parser
->qualifying_scope
= NULL_TREE
;
26054 parser
->object_scope
= NULL_TREE
;
26057 /* If the lookup failed, let our caller know. */
26058 if (!decl
|| decl
== error_mark_node
)
26059 return error_mark_node
;
26061 /* Pull out the template from an injected-class-name (or multiple). */
26063 decl
= maybe_get_template_decl_from_type_decl (decl
);
26065 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26066 if (TREE_CODE (decl
) == TREE_LIST
)
26068 if (ambiguous_decls
)
26069 *ambiguous_decls
= decl
;
26070 /* The error message we have to print is too complicated for
26071 cp_parser_error, so we incorporate its actions directly. */
26072 if (!cp_parser_simulate_error (parser
))
26074 error_at (name_location
, "reference to %qD is ambiguous",
26076 print_candidates (decl
);
26078 return error_mark_node
;
26081 gcc_assert (DECL_P (decl
)
26082 || TREE_CODE (decl
) == OVERLOAD
26083 || TREE_CODE (decl
) == SCOPE_REF
26084 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26085 || BASELINK_P (decl
));
26087 /* If we have resolved the name of a member declaration, check to
26088 see if the declaration is accessible. When the name resolves to
26089 set of overloaded functions, accessibility is checked when
26090 overload resolution is done.
26092 During an explicit instantiation, access is not checked at all,
26093 as per [temp.explicit]. */
26095 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26097 maybe_record_typedef_use (decl
);
26099 return cp_expr (decl
, name_location
);
26102 /* Like cp_parser_lookup_name, but for use in the typical case where
26103 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26104 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26107 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26109 return cp_parser_lookup_name (parser
, name
,
26111 /*is_template=*/false,
26112 /*is_namespace=*/false,
26113 /*check_dependency=*/true,
26114 /*ambiguous_decls=*/NULL
,
26118 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26119 the current context, return the TYPE_DECL. If TAG_NAME_P is
26120 true, the DECL indicates the class being defined in a class-head,
26121 or declared in an elaborated-type-specifier.
26123 Otherwise, return DECL. */
26126 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26128 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26129 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26132 template <typename T> struct B;
26135 template <typename T> struct A::B {};
26137 Similarly, in an elaborated-type-specifier:
26139 namespace N { struct X{}; }
26142 template <typename T> friend struct N::X;
26145 However, if the DECL refers to a class type, and we are in
26146 the scope of the class, then the name lookup automatically
26147 finds the TYPE_DECL created by build_self_reference rather
26148 than a TEMPLATE_DECL. For example, in:
26150 template <class T> struct S {
26154 there is no need to handle such case. */
26156 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26157 return DECL_TEMPLATE_RESULT (decl
);
26162 /* If too many, or too few, template-parameter lists apply to the
26163 declarator, issue an error message. Returns TRUE if all went well,
26164 and FALSE otherwise. */
26167 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26168 cp_declarator
*declarator
,
26169 location_t declarator_location
)
26171 switch (declarator
->kind
)
26175 unsigned num_templates
= 0;
26176 tree scope
= declarator
->u
.id
.qualifying_scope
;
26179 num_templates
= num_template_headers_for_class (scope
);
26180 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26181 == TEMPLATE_ID_EXPR
)
26182 /* If the DECLARATOR has the form `X<y>' then it uses one
26183 additional level of template parameters. */
26186 return cp_parser_check_template_parameters
26187 (parser
, num_templates
, declarator_location
, declarator
);
26193 case cdk_reference
:
26195 return (cp_parser_check_declarator_template_parameters
26196 (parser
, declarator
->declarator
, declarator_location
));
26203 gcc_unreachable ();
26208 /* NUM_TEMPLATES were used in the current declaration. If that is
26209 invalid, return FALSE and issue an error messages. Otherwise,
26210 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26211 declarator and we can print more accurate diagnostics. */
26214 cp_parser_check_template_parameters (cp_parser
* parser
,
26215 unsigned num_templates
,
26216 location_t location
,
26217 cp_declarator
*declarator
)
26219 /* If there are the same number of template classes and parameter
26220 lists, that's OK. */
26221 if (parser
->num_template_parameter_lists
== num_templates
)
26223 /* If there are more, but only one more, then we are referring to a
26224 member template. That's OK too. */
26225 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26227 /* If there are more template classes than parameter lists, we have
26230 template <class T> void S<T>::R<T>::f (); */
26231 if (parser
->num_template_parameter_lists
< num_templates
)
26233 if (declarator
&& !current_function_decl
)
26234 error_at (location
, "specializing member %<%T::%E%> "
26235 "requires %<template<>%> syntax",
26236 declarator
->u
.id
.qualifying_scope
,
26237 declarator
->u
.id
.unqualified_name
);
26238 else if (declarator
)
26239 error_at (location
, "invalid declaration of %<%T::%E%>",
26240 declarator
->u
.id
.qualifying_scope
,
26241 declarator
->u
.id
.unqualified_name
);
26243 error_at (location
, "too few template-parameter-lists");
26246 /* Otherwise, there are too many template parameter lists. We have
26249 template <class T> template <class U> void S::f(); */
26250 error_at (location
, "too many template-parameter-lists");
26254 /* Parse an optional `::' token indicating that the following name is
26255 from the global namespace. If so, PARSER->SCOPE is set to the
26256 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26257 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26258 Returns the new value of PARSER->SCOPE, if the `::' token is
26259 present, and NULL_TREE otherwise. */
26262 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26266 /* Peek at the next token. */
26267 token
= cp_lexer_peek_token (parser
->lexer
);
26268 /* If we're looking at a `::' token then we're starting from the
26269 global namespace, not our current location. */
26270 if (token
->type
== CPP_SCOPE
)
26272 /* Consume the `::' token. */
26273 cp_lexer_consume_token (parser
->lexer
);
26274 /* Set the SCOPE so that we know where to start the lookup. */
26275 parser
->scope
= global_namespace
;
26276 parser
->qualifying_scope
= global_namespace
;
26277 parser
->object_scope
= NULL_TREE
;
26279 return parser
->scope
;
26281 else if (!current_scope_valid_p
)
26283 parser
->scope
= NULL_TREE
;
26284 parser
->qualifying_scope
= NULL_TREE
;
26285 parser
->object_scope
= NULL_TREE
;
26291 /* Returns TRUE if the upcoming token sequence is the start of a
26292 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26293 declarator is preceded by the `friend' specifier. */
26296 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26298 bool constructor_p
;
26299 bool outside_class_specifier_p
;
26300 tree nested_name_specifier
;
26301 cp_token
*next_token
;
26303 /* The common case is that this is not a constructor declarator, so
26304 try to avoid doing lots of work if at all possible. It's not
26305 valid declare a constructor at function scope. */
26306 if (parser
->in_function_body
)
26308 /* And only certain tokens can begin a constructor declarator. */
26309 next_token
= cp_lexer_peek_token (parser
->lexer
);
26310 if (next_token
->type
!= CPP_NAME
26311 && next_token
->type
!= CPP_SCOPE
26312 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26313 && next_token
->type
!= CPP_TEMPLATE_ID
)
26316 /* Parse tentatively; we are going to roll back all of the tokens
26318 cp_parser_parse_tentatively (parser
);
26319 /* Assume that we are looking at a constructor declarator. */
26320 constructor_p
= true;
26322 /* Look for the optional `::' operator. */
26323 cp_parser_global_scope_opt (parser
,
26324 /*current_scope_valid_p=*/false);
26325 /* Look for the nested-name-specifier. */
26326 nested_name_specifier
26327 = (cp_parser_nested_name_specifier_opt (parser
,
26328 /*typename_keyword_p=*/false,
26329 /*check_dependency_p=*/false,
26331 /*is_declaration=*/false));
26333 outside_class_specifier_p
= (!at_class_scope_p ()
26334 || !TYPE_BEING_DEFINED (current_class_type
)
26337 /* Outside of a class-specifier, there must be a
26338 nested-name-specifier. Except in C++17 mode, where we
26339 might be declaring a guiding declaration. */
26340 if (!nested_name_specifier
&& outside_class_specifier_p
26341 && cxx_dialect
< cxx17
)
26342 constructor_p
= false;
26343 else if (nested_name_specifier
== error_mark_node
)
26344 constructor_p
= false;
26346 /* If we have a class scope, this is easy; DR 147 says that S::S always
26347 names the constructor, and no other qualified name could. */
26348 if (constructor_p
&& nested_name_specifier
26349 && CLASS_TYPE_P (nested_name_specifier
))
26351 tree id
= cp_parser_unqualified_id (parser
,
26352 /*template_keyword_p=*/false,
26353 /*check_dependency_p=*/false,
26354 /*declarator_p=*/true,
26355 /*optional_p=*/false);
26356 if (is_overloaded_fn (id
))
26357 id
= DECL_NAME (get_first_fn (id
));
26358 if (!constructor_name_p (id
, nested_name_specifier
))
26359 constructor_p
= false;
26361 /* If we still think that this might be a constructor-declarator,
26362 look for a class-name. */
26363 else if (constructor_p
)
26367 template <typename T> struct S {
26371 we must recognize that the nested `S' names a class. */
26372 if (cxx_dialect
>= cxx17
)
26373 cp_parser_parse_tentatively (parser
);
26376 type_decl
= cp_parser_class_name (parser
,
26377 /*typename_keyword_p=*/false,
26378 /*template_keyword_p=*/false,
26380 /*check_dependency_p=*/false,
26381 /*class_head_p=*/false,
26382 /*is_declaration=*/false);
26384 if (cxx_dialect
>= cxx17
26385 && !cp_parser_parse_definitely (parser
))
26387 type_decl
= NULL_TREE
;
26388 tree tmpl
= cp_parser_template_name (parser
,
26389 /*template_keyword*/false,
26390 /*check_dependency_p*/false,
26391 /*is_declaration*/false,
26393 /*is_identifier*/NULL
);
26394 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26395 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26396 /* It's a deduction guide, return true. */;
26398 cp_parser_simulate_error (parser
);
26401 /* If there was no class-name, then this is not a constructor.
26402 Otherwise, if we are in a class-specifier and we aren't
26403 handling a friend declaration, check that its type matches
26404 current_class_type (c++/38313). Note: error_mark_node
26405 is left alone for error recovery purposes. */
26406 constructor_p
= (!cp_parser_error_occurred (parser
)
26407 && (outside_class_specifier_p
26408 || type_decl
== NULL_TREE
26409 || type_decl
== error_mark_node
26410 || same_type_p (current_class_type
,
26411 TREE_TYPE (type_decl
))));
26413 /* If we're still considering a constructor, we have to see a `(',
26414 to begin the parameter-declaration-clause, followed by either a
26415 `)', an `...', or a decl-specifier. We need to check for a
26416 type-specifier to avoid being fooled into thinking that:
26420 is a constructor. (It is actually a function named `f' that
26421 takes one parameter (of type `int') and returns a value of type
26424 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26425 constructor_p
= false;
26428 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26429 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26430 /* A parameter declaration begins with a decl-specifier,
26431 which is either the "attribute" keyword, a storage class
26432 specifier, or (usually) a type-specifier. */
26433 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26436 tree pushed_scope
= NULL_TREE
;
26437 unsigned saved_num_template_parameter_lists
;
26439 /* Names appearing in the type-specifier should be looked up
26440 in the scope of the class. */
26441 if (current_class_type
)
26443 else if (type_decl
)
26445 type
= TREE_TYPE (type_decl
);
26446 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26448 type
= resolve_typename_type (type
,
26449 /*only_current_p=*/false);
26450 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26452 cp_parser_abort_tentative_parse (parser
);
26456 pushed_scope
= push_scope (type
);
26459 /* Inside the constructor parameter list, surrounding
26460 template-parameter-lists do not apply. */
26461 saved_num_template_parameter_lists
26462 = parser
->num_template_parameter_lists
;
26463 parser
->num_template_parameter_lists
= 0;
26465 /* Look for the type-specifier. */
26466 cp_parser_type_specifier (parser
,
26467 CP_PARSER_FLAGS_NONE
,
26468 /*decl_specs=*/NULL
,
26469 /*is_declarator=*/true,
26470 /*declares_class_or_enum=*/NULL
,
26471 /*is_cv_qualifier=*/NULL
);
26473 parser
->num_template_parameter_lists
26474 = saved_num_template_parameter_lists
;
26476 /* Leave the scope of the class. */
26478 pop_scope (pushed_scope
);
26480 constructor_p
= !cp_parser_error_occurred (parser
);
26484 /* We did not really want to consume any tokens. */
26485 cp_parser_abort_tentative_parse (parser
);
26487 return constructor_p
;
26490 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26491 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26492 they must be performed once we are in the scope of the function.
26494 Returns the function defined. */
26497 cp_parser_function_definition_from_specifiers_and_declarator
26498 (cp_parser
* parser
,
26499 cp_decl_specifier_seq
*decl_specifiers
,
26501 const cp_declarator
*declarator
)
26506 /* Begin the function-definition. */
26507 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26509 /* The things we're about to see are not directly qualified by any
26510 template headers we've seen thus far. */
26511 reset_specialization ();
26513 /* If there were names looked up in the decl-specifier-seq that we
26514 did not check, check them now. We must wait until we are in the
26515 scope of the function to perform the checks, since the function
26516 might be a friend. */
26517 perform_deferred_access_checks (tf_warning_or_error
);
26521 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26522 parser
->omp_declare_simd
= NULL
;
26523 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26524 parser
->oacc_routine
= NULL
;
26529 /* Skip the entire function. */
26530 cp_parser_skip_to_end_of_block_or_statement (parser
);
26531 fn
= error_mark_node
;
26533 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26535 /* Seen already, skip it. An error message has already been output. */
26536 cp_parser_skip_to_end_of_block_or_statement (parser
);
26537 fn
= current_function_decl
;
26538 current_function_decl
= NULL_TREE
;
26539 /* If this is a function from a class, pop the nested class. */
26540 if (current_class_name
)
26541 pop_nested_class ();
26546 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26547 tv
= TV_PARSE_INLINE
;
26549 tv
= TV_PARSE_FUNC
;
26551 fn
= cp_parser_function_definition_after_declarator (parser
,
26552 /*inline_p=*/false);
26559 /* Parse the part of a function-definition that follows the
26560 declarator. INLINE_P is TRUE iff this function is an inline
26561 function defined within a class-specifier.
26563 Returns the function defined. */
26566 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26570 bool saved_in_unbraced_linkage_specification_p
;
26571 bool saved_in_function_body
;
26572 unsigned saved_num_template_parameter_lists
;
26574 bool fully_implicit_function_template_p
26575 = parser
->fully_implicit_function_template_p
;
26576 parser
->fully_implicit_function_template_p
= false;
26577 tree implicit_template_parms
26578 = parser
->implicit_template_parms
;
26579 parser
->implicit_template_parms
= 0;
26580 cp_binding_level
* implicit_template_scope
26581 = parser
->implicit_template_scope
;
26582 parser
->implicit_template_scope
= 0;
26584 saved_in_function_body
= parser
->in_function_body
;
26585 parser
->in_function_body
= true;
26586 /* If the next token is `return', then the code may be trying to
26587 make use of the "named return value" extension that G++ used to
26589 token
= cp_lexer_peek_token (parser
->lexer
);
26590 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26592 /* Consume the `return' keyword. */
26593 cp_lexer_consume_token (parser
->lexer
);
26594 /* Look for the identifier that indicates what value is to be
26596 cp_parser_identifier (parser
);
26597 /* Issue an error message. */
26598 error_at (token
->location
,
26599 "named return values are no longer supported");
26600 /* Skip tokens until we reach the start of the function body. */
26603 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26604 if (token
->type
== CPP_OPEN_BRACE
26605 || token
->type
== CPP_EOF
26606 || token
->type
== CPP_PRAGMA_EOL
)
26608 cp_lexer_consume_token (parser
->lexer
);
26611 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26612 anything declared inside `f'. */
26613 saved_in_unbraced_linkage_specification_p
26614 = parser
->in_unbraced_linkage_specification_p
;
26615 parser
->in_unbraced_linkage_specification_p
= false;
26616 /* Inside the function, surrounding template-parameter-lists do not
26618 saved_num_template_parameter_lists
26619 = parser
->num_template_parameter_lists
;
26620 parser
->num_template_parameter_lists
= 0;
26622 /* If the next token is `try', `__transaction_atomic', or
26623 `__transaction_relaxed`, then we are looking at either function-try-block
26624 or function-transaction-block. Note that all of these include the
26626 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26627 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26628 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26629 RID_TRANSACTION_RELAXED
))
26630 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26631 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26632 cp_parser_function_try_block (parser
);
26634 cp_parser_ctor_initializer_opt_and_function_body
26635 (parser
, /*in_function_try_block=*/false);
26637 /* Finish the function. */
26638 fn
= finish_function (inline_p
);
26639 /* Generate code for it, if necessary. */
26640 expand_or_defer_fn (fn
);
26641 /* Restore the saved values. */
26642 parser
->in_unbraced_linkage_specification_p
26643 = saved_in_unbraced_linkage_specification_p
;
26644 parser
->num_template_parameter_lists
26645 = saved_num_template_parameter_lists
;
26646 parser
->in_function_body
= saved_in_function_body
;
26648 parser
->fully_implicit_function_template_p
26649 = fully_implicit_function_template_p
;
26650 parser
->implicit_template_parms
26651 = implicit_template_parms
;
26652 parser
->implicit_template_scope
26653 = implicit_template_scope
;
26655 if (parser
->fully_implicit_function_template_p
)
26656 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26661 /* Parse a template-declaration body (following argument list). */
26664 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26665 tree parameter_list
,
26668 tree decl
= NULL_TREE
;
26669 bool friend_p
= false;
26671 /* We just processed one more parameter list. */
26672 ++parser
->num_template_parameter_lists
;
26674 /* Get the deferred access checks from the parameter list. These
26675 will be checked once we know what is being declared, as for a
26676 member template the checks must be performed in the scope of the
26677 class containing the member. */
26678 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26680 /* Tentatively parse for a new template parameter list, which can either be
26681 the template keyword or a template introduction. */
26682 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26684 else if (cxx_dialect
>= cxx11
26685 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26686 decl
= cp_parser_alias_declaration (parser
);
26689 /* There are no access checks when parsing a template, as we do not
26690 know if a specialization will be a friend. */
26691 push_deferring_access_checks (dk_no_check
);
26692 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26693 decl
= cp_parser_single_declaration (parser
,
26696 /*explicit_specialization_p=*/false,
26698 pop_deferring_access_checks ();
26700 /* If this is a member template declaration, let the front
26702 if (member_p
&& !friend_p
&& decl
)
26704 if (TREE_CODE (decl
) == TYPE_DECL
)
26705 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26707 decl
= finish_member_template_decl (decl
);
26709 else if (friend_p
&& decl
26710 && DECL_DECLARES_TYPE_P (decl
))
26711 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26712 /*complain=*/true);
26714 /* We are done with the current parameter list. */
26715 --parser
->num_template_parameter_lists
;
26717 pop_deferring_access_checks ();
26720 finish_template_decl (parameter_list
);
26722 /* Check the template arguments for a literal operator template. */
26724 && DECL_DECLARES_FUNCTION_P (decl
)
26725 && UDLIT_OPER_P (DECL_NAME (decl
)))
26728 if (parameter_list
== NULL_TREE
)
26732 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26733 if (num_parms
== 1)
26735 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26736 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26737 if (TREE_TYPE (parm
) != char_type_node
26738 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26741 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26743 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26744 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26745 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26746 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26747 if (parm
== error_mark_node
26748 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26749 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26757 if (cxx_dialect
>= cxx14
)
26758 error ("literal operator template %qD has invalid parameter list."
26759 " Expected non-type template argument pack <char...>"
26760 " or <typename CharT, CharT...>",
26763 error ("literal operator template %qD has invalid parameter list."
26764 " Expected non-type template argument pack <char...>",
26769 /* Register member declarations. */
26770 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26771 finish_member_declaration (decl
);
26772 /* If DECL is a function template, we must return to parse it later.
26773 (Even though there is no definition, there might be default
26774 arguments that need handling.) */
26775 if (member_p
&& decl
26776 && DECL_DECLARES_FUNCTION_P (decl
))
26777 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26780 /* Parse a template introduction header for a template-declaration. Returns
26781 false if tentative parse fails. */
26784 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26786 cp_parser_parse_tentatively (parser
);
26788 tree saved_scope
= parser
->scope
;
26789 tree saved_object_scope
= parser
->object_scope
;
26790 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26792 /* Look for the optional `::' operator. */
26793 cp_parser_global_scope_opt (parser
,
26794 /*current_scope_valid_p=*/false);
26795 /* Look for the nested-name-specifier. */
26796 cp_parser_nested_name_specifier_opt (parser
,
26797 /*typename_keyword_p=*/false,
26798 /*check_dependency_p=*/true,
26800 /*is_declaration=*/false);
26802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26803 tree concept_name
= cp_parser_identifier (parser
);
26805 /* Look up the concept for which we will be matching
26806 template parameters. */
26807 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
26809 parser
->scope
= saved_scope
;
26810 parser
->object_scope
= saved_object_scope
;
26811 parser
->qualifying_scope
= saved_qualifying_scope
;
26813 if (concept_name
== error_mark_node
)
26814 cp_parser_simulate_error (parser
);
26816 /* Look for opening brace for introduction. */
26817 matching_braces braces
;
26818 braces
.require_open (parser
);
26820 if (!cp_parser_parse_definitely (parser
))
26823 push_deferring_access_checks (dk_deferred
);
26825 /* Build vector of placeholder parameters and grab
26826 matching identifiers. */
26827 tree introduction_list
= cp_parser_introduction_list (parser
);
26829 /* The introduction-list shall not be empty. */
26830 int nargs
= TREE_VEC_LENGTH (introduction_list
);
26833 error ("empty introduction-list");
26837 /* Look for closing brace for introduction. */
26838 if (!braces
.require_close (parser
))
26841 if (tmpl_decl
== error_mark_node
)
26843 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
26848 /* Build and associate the constraint. */
26849 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
26850 if (parms
&& parms
!= error_mark_node
)
26852 cp_parser_template_declaration_after_parameters (parser
, parms
,
26857 error_at (token
->location
, "no matching concept for template-introduction");
26861 /* Parse a normal template-declaration following the template keyword. */
26864 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
26866 tree parameter_list
;
26867 bool need_lang_pop
;
26868 location_t location
= input_location
;
26870 /* Look for the `<' token. */
26871 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
26873 if (at_class_scope_p () && current_function_decl
)
26875 /* 14.5.2.2 [temp.mem]
26877 A local class shall not have member templates. */
26878 error_at (location
,
26879 "invalid declaration of member template in local class");
26880 cp_parser_skip_to_end_of_block_or_statement (parser
);
26885 A template ... shall not have C linkage. */
26886 if (current_lang_name
== lang_name_c
)
26888 error_at (location
, "template with C linkage");
26889 maybe_show_extern_c_location ();
26890 /* Give it C++ linkage to avoid confusing other parts of the
26892 push_lang_context (lang_name_cplusplus
);
26893 need_lang_pop
= true;
26896 need_lang_pop
= false;
26898 /* We cannot perform access checks on the template parameter
26899 declarations until we know what is being declared, just as we
26900 cannot check the decl-specifier list. */
26901 push_deferring_access_checks (dk_deferred
);
26903 /* If the next token is `>', then we have an invalid
26904 specialization. Rather than complain about an invalid template
26905 parameter, issue an error message here. */
26906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
26908 cp_parser_error (parser
, "invalid explicit specialization");
26909 begin_specialization ();
26910 parameter_list
= NULL_TREE
;
26914 /* Parse the template parameters. */
26915 parameter_list
= cp_parser_template_parameter_list (parser
);
26918 /* Look for the `>'. */
26919 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26921 /* Manage template requirements */
26924 tree reqs
= get_shorthand_constraints (current_template_parms
);
26925 if (tree r
= cp_parser_requires_clause_opt (parser
))
26926 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
26927 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
26930 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
26933 /* For the erroneous case of a template with C linkage, we pushed an
26934 implicit C++ linkage scope; exit that scope now. */
26936 pop_lang_context ();
26939 /* Parse a template-declaration, assuming that the `export' (and
26940 `extern') keywords, if present, has already been scanned. MEMBER_P
26941 is as for cp_parser_template_declaration. */
26944 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
26946 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26948 cp_lexer_consume_token (parser
->lexer
);
26949 cp_parser_explicit_template_declaration (parser
, member_p
);
26952 else if (flag_concepts
)
26953 return cp_parser_template_introduction (parser
, member_p
);
26958 /* Perform the deferred access checks from a template-parameter-list.
26959 CHECKS is a TREE_LIST of access checks, as returned by
26960 get_deferred_access_checks. */
26963 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
26965 ++processing_template_parmlist
;
26966 perform_access_checks (checks
, tf_warning_or_error
);
26967 --processing_template_parmlist
;
26970 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26971 `function-definition' sequence that follows a template header.
26972 If MEMBER_P is true, this declaration appears in a class scope.
26974 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26975 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26978 cp_parser_single_declaration (cp_parser
* parser
,
26979 vec
<deferred_access_check
, va_gc
> *checks
,
26981 bool explicit_specialization_p
,
26984 int declares_class_or_enum
;
26985 tree decl
= NULL_TREE
;
26986 cp_decl_specifier_seq decl_specifiers
;
26987 bool function_definition_p
= false;
26988 cp_token
*decl_spec_token_start
;
26990 /* This function is only used when processing a template
26992 gcc_assert (innermost_scope_kind () == sk_template_parms
26993 || innermost_scope_kind () == sk_template_spec
);
26995 /* Defer access checks until we know what is being declared. */
26996 push_deferring_access_checks (dk_deferred
);
26998 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27000 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27001 cp_parser_decl_specifier_seq (parser
,
27002 CP_PARSER_FLAGS_OPTIONAL
,
27004 &declares_class_or_enum
);
27006 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27008 /* There are no template typedefs. */
27009 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27011 error_at (decl_spec_token_start
->location
,
27012 "template declaration of %<typedef%>");
27013 decl
= error_mark_node
;
27016 /* Gather up the access checks that occurred the
27017 decl-specifier-seq. */
27018 stop_deferring_access_checks ();
27020 /* Check for the declaration of a template class. */
27021 if (declares_class_or_enum
)
27023 if (cp_parser_declares_only_class_p (parser
)
27024 || (declares_class_or_enum
& 2))
27026 // If this is a declaration, but not a definition, associate
27027 // any constraints with the type declaration. Constraints
27028 // are associated with definitions in cp_parser_class_specifier.
27029 if (declares_class_or_enum
== 1)
27030 associate_classtype_constraints (decl_specifiers
.type
);
27032 decl
= shadow_tag (&decl_specifiers
);
27037 friend template <typename T> struct A<T>::B;
27040 A<T>::B will be represented by a TYPENAME_TYPE, and
27041 therefore not recognized by shadow_tag. */
27042 if (friend_p
&& *friend_p
27044 && decl_specifiers
.type
27045 && TYPE_P (decl_specifiers
.type
))
27046 decl
= decl_specifiers
.type
;
27048 if (decl
&& decl
!= error_mark_node
)
27049 decl
= TYPE_NAME (decl
);
27051 decl
= error_mark_node
;
27053 /* Perform access checks for template parameters. */
27054 cp_parser_perform_template_parameter_access_checks (checks
);
27056 /* Give a helpful diagnostic for
27057 template <class T> struct A { } a;
27058 if we aren't already recovering from an error. */
27059 if (!cp_parser_declares_only_class_p (parser
)
27062 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27063 "a class template declaration must not declare "
27065 cp_parser_skip_to_end_of_block_or_statement (parser
);
27071 /* Complain about missing 'typename' or other invalid type names. */
27072 if (!decl_specifiers
.any_type_specifiers_p
27073 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27075 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27076 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27077 the rest of this declaration. */
27078 decl
= error_mark_node
;
27082 /* If it's not a template class, try for a template function. If
27083 the next token is a `;', then this declaration does not declare
27084 anything. But, if there were errors in the decl-specifiers, then
27085 the error might well have come from an attempted class-specifier.
27086 In that case, there's no need to warn about a missing declarator. */
27088 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27089 || decl_specifiers
.type
!= error_mark_node
))
27091 decl
= cp_parser_init_declarator (parser
,
27094 /*function_definition_allowed_p=*/true,
27096 declares_class_or_enum
,
27097 &function_definition_p
,
27100 /* 7.1.1-1 [dcl.stc]
27102 A storage-class-specifier shall not be specified in an explicit
27103 specialization... */
27105 && explicit_specialization_p
27106 && decl_specifiers
.storage_class
!= sc_none
)
27108 error_at (decl_spec_token_start
->location
,
27109 "explicit template specialization cannot have a storage class");
27110 decl
= error_mark_node
;
27113 if (decl
&& VAR_P (decl
))
27114 check_template_variable (decl
);
27117 /* Look for a trailing `;' after the declaration. */
27118 if (!function_definition_p
27119 && (decl
== error_mark_node
27120 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27121 cp_parser_skip_to_end_of_block_or_statement (parser
);
27124 pop_deferring_access_checks ();
27126 /* Clear any current qualification; whatever comes next is the start
27127 of something new. */
27128 parser
->scope
= NULL_TREE
;
27129 parser
->qualifying_scope
= NULL_TREE
;
27130 parser
->object_scope
= NULL_TREE
;
27135 /* Parse a cast-expression that is not the operand of a unary "&". */
27138 cp_parser_simple_cast_expression (cp_parser
*parser
)
27140 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27141 /*cast_p=*/false, /*decltype*/false, NULL
);
27144 /* Parse a functional cast to TYPE. Returns an expression
27145 representing the cast. */
27148 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27150 vec
<tree
, va_gc
> *vec
;
27151 tree expression_list
;
27155 location_t start_loc
= input_location
;
27158 type
= error_mark_node
;
27160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27162 cp_lexer_set_source_position (parser
->lexer
);
27163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27164 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27165 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27166 if (TREE_CODE (type
) == TYPE_DECL
)
27167 type
= TREE_TYPE (type
);
27169 cast
= finish_compound_literal (type
, expression_list
,
27170 tf_warning_or_error
, fcl_functional
);
27171 /* Create a location of the form:
27174 with caret == start at the start of the type name,
27175 finishing at the closing brace. */
27176 location_t finish_loc
27177 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27178 location_t combined_loc
= make_location (start_loc
, start_loc
,
27180 cast
.set_location (combined_loc
);
27185 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27187 /*allow_expansion_p=*/true,
27188 /*non_constant_p=*/NULL
);
27190 expression_list
= error_mark_node
;
27193 expression_list
= build_tree_list_vec (vec
);
27194 release_tree_vector (vec
);
27197 cast
= build_functional_cast (type
, expression_list
,
27198 tf_warning_or_error
);
27199 /* [expr.const]/1: In an integral constant expression "only type
27200 conversions to integral or enumeration type can be used". */
27201 if (TREE_CODE (type
) == TYPE_DECL
)
27202 type
= TREE_TYPE (type
);
27203 if (cast
!= error_mark_node
27204 && !cast_valid_in_integral_constant_expression_p (type
)
27205 && cp_parser_non_integral_constant_expression (parser
,
27207 return error_mark_node
;
27209 /* Create a location of the form:
27212 with caret == start at the start of the type name,
27213 finishing at the closing paren. */
27214 location_t finish_loc
27215 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27216 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27217 cast
.set_location (combined_loc
);
27221 /* Save the tokens that make up the body of a member function defined
27222 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27223 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27224 specifiers applied to the declaration. Returns the FUNCTION_DECL
27225 for the member function. */
27228 cp_parser_save_member_function_body (cp_parser
* parser
,
27229 cp_decl_specifier_seq
*decl_specifiers
,
27230 cp_declarator
*declarator
,
27236 bool function_try_block
= false;
27238 /* Create the FUNCTION_DECL. */
27239 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27240 cp_finalize_omp_declare_simd (parser
, fn
);
27241 cp_finalize_oacc_routine (parser
, fn
, true);
27242 /* If something went badly wrong, bail out now. */
27243 if (fn
== error_mark_node
)
27245 /* If there's a function-body, skip it. */
27246 if (cp_parser_token_starts_function_definition_p
27247 (cp_lexer_peek_token (parser
->lexer
)))
27248 cp_parser_skip_to_end_of_block_or_statement (parser
);
27249 return error_mark_node
;
27252 /* Remember it, if there default args to post process. */
27253 cp_parser_save_default_args (parser
, fn
);
27255 /* Save away the tokens that make up the body of the
27257 first
= parser
->lexer
->next_token
;
27259 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27260 cp_lexer_consume_token (parser
->lexer
);
27261 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27262 RID_TRANSACTION_ATOMIC
))
27264 cp_lexer_consume_token (parser
->lexer
);
27265 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27267 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27268 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27269 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27270 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27271 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27273 cp_lexer_consume_token (parser
->lexer
);
27274 cp_lexer_consume_token (parser
->lexer
);
27275 cp_lexer_consume_token (parser
->lexer
);
27276 cp_lexer_consume_token (parser
->lexer
);
27277 cp_lexer_consume_token (parser
->lexer
);
27280 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27281 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27283 cp_lexer_consume_token (parser
->lexer
);
27284 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27289 /* Handle function try blocks. */
27290 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27292 cp_lexer_consume_token (parser
->lexer
);
27293 function_try_block
= true;
27295 /* We can have braced-init-list mem-initializers before the fn body. */
27296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27298 cp_lexer_consume_token (parser
->lexer
);
27299 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27301 /* cache_group will stop after an un-nested { } pair, too. */
27302 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27305 /* variadic mem-inits have ... after the ')'. */
27306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27307 cp_lexer_consume_token (parser
->lexer
);
27310 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27311 /* Handle function try blocks. */
27312 if (function_try_block
)
27313 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27314 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27315 last
= parser
->lexer
->next_token
;
27317 /* Save away the inline definition; we will process it when the
27318 class is complete. */
27319 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27320 DECL_PENDING_INLINE_P (fn
) = 1;
27322 /* We need to know that this was defined in the class, so that
27323 friend templates are handled correctly. */
27324 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27326 /* Add FN to the queue of functions to be parsed later. */
27327 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27332 /* Save the tokens that make up the in-class initializer for a non-static
27333 data member. Returns a DEFAULT_ARG. */
27336 cp_parser_save_nsdmi (cp_parser
* parser
)
27338 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27341 /* Parse a template-argument-list, as well as the trailing ">" (but
27342 not the opening "<"). See cp_parser_template_argument_list for the
27346 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27350 tree saved_qualifying_scope
;
27351 tree saved_object_scope
;
27352 bool saved_greater_than_is_operator_p
;
27353 int saved_unevaluated_operand
;
27354 int saved_inhibit_evaluation_warnings
;
27358 When parsing a template-id, the first non-nested `>' is taken as
27359 the end of the template-argument-list rather than a greater-than
27361 saved_greater_than_is_operator_p
27362 = parser
->greater_than_is_operator_p
;
27363 parser
->greater_than_is_operator_p
= false;
27364 /* Parsing the argument list may modify SCOPE, so we save it
27366 saved_scope
= parser
->scope
;
27367 saved_qualifying_scope
= parser
->qualifying_scope
;
27368 saved_object_scope
= parser
->object_scope
;
27369 /* We need to evaluate the template arguments, even though this
27370 template-id may be nested within a "sizeof". */
27371 saved_unevaluated_operand
= cp_unevaluated_operand
;
27372 cp_unevaluated_operand
= 0;
27373 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27374 c_inhibit_evaluation_warnings
= 0;
27375 /* Parse the template-argument-list itself. */
27376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27377 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27378 arguments
= NULL_TREE
;
27380 arguments
= cp_parser_template_argument_list (parser
);
27381 /* Look for the `>' that ends the template-argument-list. If we find
27382 a '>>' instead, it's probably just a typo. */
27383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27385 if (cxx_dialect
!= cxx98
)
27387 /* In C++0x, a `>>' in a template argument list or cast
27388 expression is considered to be two separate `>'
27389 tokens. So, change the current token to a `>', but don't
27390 consume it: it will be consumed later when the outer
27391 template argument list (or cast expression) is parsed.
27392 Note that this replacement of `>' for `>>' is necessary
27393 even if we are parsing tentatively: in the tentative
27394 case, after calling
27395 cp_parser_enclosed_template_argument_list we will always
27396 throw away all of the template arguments and the first
27397 closing `>', either because the template argument list
27398 was erroneous or because we are replacing those tokens
27399 with a CPP_TEMPLATE_ID token. The second `>' (which will
27400 not have been thrown away) is needed either to close an
27401 outer template argument list or to complete a new-style
27403 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27404 token
->type
= CPP_GREATER
;
27406 else if (!saved_greater_than_is_operator_p
)
27408 /* If we're in a nested template argument list, the '>>' has
27409 to be a typo for '> >'. We emit the error message, but we
27410 continue parsing and we push a '>' as next token, so that
27411 the argument list will be parsed correctly. Note that the
27412 global source location is still on the token before the
27413 '>>', so we need to say explicitly where we want it. */
27414 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27415 gcc_rich_location
richloc (token
->location
);
27416 richloc
.add_fixit_replace ("> >");
27417 error_at (&richloc
, "%<>>%> should be %<> >%> "
27418 "within a nested template argument list");
27420 token
->type
= CPP_GREATER
;
27424 /* If this is not a nested template argument list, the '>>'
27425 is a typo for '>'. Emit an error message and continue.
27426 Same deal about the token location, but here we can get it
27427 right by consuming the '>>' before issuing the diagnostic. */
27428 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27429 error_at (token
->location
,
27430 "spurious %<>>%>, use %<>%> to terminate "
27431 "a template argument list");
27435 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27436 /* The `>' token might be a greater-than operator again now. */
27437 parser
->greater_than_is_operator_p
27438 = saved_greater_than_is_operator_p
;
27439 /* Restore the SAVED_SCOPE. */
27440 parser
->scope
= saved_scope
;
27441 parser
->qualifying_scope
= saved_qualifying_scope
;
27442 parser
->object_scope
= saved_object_scope
;
27443 cp_unevaluated_operand
= saved_unevaluated_operand
;
27444 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27449 /* MEMBER_FUNCTION is a member function, or a friend. If default
27450 arguments, or the body of the function have not yet been parsed,
27454 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27456 timevar_push (TV_PARSE_INMETH
);
27457 /* If this member is a template, get the underlying
27459 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27460 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27462 /* There should not be any class definitions in progress at this
27463 point; the bodies of members are only parsed outside of all class
27465 gcc_assert (parser
->num_classes_being_defined
== 0);
27466 /* While we're parsing the member functions we might encounter more
27467 classes. We want to handle them right away, but we don't want
27468 them getting mixed up with functions that are currently in the
27470 push_unparsed_function_queues (parser
);
27472 /* Make sure that any template parameters are in scope. */
27473 maybe_begin_member_template_processing (member_function
);
27475 /* If the body of the function has not yet been parsed, parse it
27477 if (DECL_PENDING_INLINE_P (member_function
))
27479 tree function_scope
;
27480 cp_token_cache
*tokens
;
27482 /* The function is no longer pending; we are processing it. */
27483 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27484 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27485 DECL_PENDING_INLINE_P (member_function
) = 0;
27487 /* If this is a local class, enter the scope of the containing
27489 function_scope
= current_function_decl
;
27490 if (function_scope
)
27491 push_function_context ();
27493 /* Push the body of the function onto the lexer stack. */
27494 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27496 /* Let the front end know that we going to be defining this
27498 start_preparsed_function (member_function
, NULL_TREE
,
27499 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27501 /* Don't do access checking if it is a templated function. */
27502 if (processing_template_decl
)
27503 push_deferring_access_checks (dk_no_check
);
27505 /* #pragma omp declare reduction needs special parsing. */
27506 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27508 parser
->lexer
->in_pragma
= true;
27509 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27510 finish_function (/*inline_p=*/true);
27511 cp_check_omp_declare_reduction (member_function
);
27514 /* Now, parse the body of the function. */
27515 cp_parser_function_definition_after_declarator (parser
,
27516 /*inline_p=*/true);
27518 if (processing_template_decl
)
27519 pop_deferring_access_checks ();
27521 /* Leave the scope of the containing function. */
27522 if (function_scope
)
27523 pop_function_context ();
27524 cp_parser_pop_lexer (parser
);
27527 /* Remove any template parameters from the symbol table. */
27528 maybe_end_member_template_processing ();
27530 /* Restore the queue. */
27531 pop_unparsed_function_queues (parser
);
27532 timevar_pop (TV_PARSE_INMETH
);
27535 /* If DECL contains any default args, remember it on the unparsed
27536 functions queue. */
27539 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27543 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27545 probe
= TREE_CHAIN (probe
))
27546 if (TREE_PURPOSE (probe
))
27548 cp_default_arg_entry entry
= {current_class_type
, decl
};
27549 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27554 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27555 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27556 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27557 from the parameter-type-list. */
27560 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27561 tree default_arg
, tree parmtype
)
27563 cp_token_cache
*tokens
;
27567 if (default_arg
== error_mark_node
)
27568 return error_mark_node
;
27570 /* Push the saved tokens for the default argument onto the parser's
27572 tokens
= DEFARG_TOKENS (default_arg
);
27573 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27575 start_lambda_scope (decl
);
27577 /* Parse the default argument. */
27578 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27579 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27580 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27582 finish_lambda_scope ();
27584 if (parsed_arg
== error_mark_node
)
27585 cp_parser_skip_to_end_of_statement (parser
);
27587 if (!processing_template_decl
)
27589 /* In a non-template class, check conversions now. In a template,
27590 we'll wait and instantiate these as needed. */
27591 if (TREE_CODE (decl
) == PARM_DECL
)
27592 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27593 tf_warning_or_error
);
27594 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27595 parsed_arg
= error_mark_node
;
27597 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27600 /* If the token stream has not been completely used up, then
27601 there was extra junk after the end of the default
27603 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27605 if (TREE_CODE (decl
) == PARM_DECL
)
27606 cp_parser_error (parser
, "expected %<,%>");
27608 cp_parser_error (parser
, "expected %<;%>");
27611 /* Revert to the main lexer. */
27612 cp_parser_pop_lexer (parser
);
27617 /* FIELD is a non-static data member with an initializer which we saved for
27618 later; parse it now. */
27621 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27625 maybe_begin_member_template_processing (field
);
27627 push_unparsed_function_queues (parser
);
27628 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27629 DECL_INITIAL (field
),
27631 pop_unparsed_function_queues (parser
);
27633 maybe_end_member_template_processing ();
27635 DECL_INITIAL (field
) = def
;
27638 /* FN is a FUNCTION_DECL which may contains a parameter with an
27639 unparsed DEFAULT_ARG. Parse the default args now. This function
27640 assumes that the current scope is the scope in which the default
27641 argument should be processed. */
27644 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27646 bool saved_local_variables_forbidden_p
;
27647 tree parm
, parmdecl
;
27649 /* While we're parsing the default args, we might (due to the
27650 statement expression extension) encounter more classes. We want
27651 to handle them right away, but we don't want them getting mixed
27652 up with default args that are currently in the queue. */
27653 push_unparsed_function_queues (parser
);
27655 /* Local variable names (and the `this' keyword) may not appear
27656 in a default argument. */
27657 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27658 parser
->local_variables_forbidden_p
= true;
27660 push_defarg_context (fn
);
27662 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27663 parmdecl
= DECL_ARGUMENTS (fn
);
27664 parm
&& parm
!= void_list_node
;
27665 parm
= TREE_CHAIN (parm
),
27666 parmdecl
= DECL_CHAIN (parmdecl
))
27668 tree default_arg
= TREE_PURPOSE (parm
);
27670 vec
<tree
, va_gc
> *insts
;
27677 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27678 /* This can happen for a friend declaration for a function
27679 already declared with default arguments. */
27683 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27685 TREE_VALUE (parm
));
27686 TREE_PURPOSE (parm
) = parsed_arg
;
27688 /* Update any instantiations we've already created. */
27689 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27690 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27691 TREE_PURPOSE (copy
) = parsed_arg
;
27694 pop_defarg_context ();
27696 /* Make sure no default arg is missing. */
27697 check_default_args (fn
);
27699 /* Restore the state of local_variables_forbidden_p. */
27700 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27702 /* Restore the queue. */
27703 pop_unparsed_function_queues (parser
);
27706 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27708 sizeof ... ( identifier )
27710 where the 'sizeof' token has already been consumed. */
27713 cp_parser_sizeof_pack (cp_parser
*parser
)
27715 /* Consume the `...'. */
27716 cp_lexer_consume_token (parser
->lexer
);
27717 maybe_warn_variadic_templates ();
27719 matching_parens parens
;
27720 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27722 parens
.consume_open (parser
);
27724 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27725 "%<sizeof...%> argument must be surrounded by parentheses");
27727 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27728 tree name
= cp_parser_identifier (parser
);
27729 if (name
== error_mark_node
)
27730 return error_mark_node
;
27731 /* The name is not qualified. */
27732 parser
->scope
= NULL_TREE
;
27733 parser
->qualifying_scope
= NULL_TREE
;
27734 parser
->object_scope
= NULL_TREE
;
27735 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27736 if (expr
== error_mark_node
)
27737 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27739 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27740 expr
= TREE_TYPE (expr
);
27741 else if (TREE_CODE (expr
) == CONST_DECL
)
27742 expr
= DECL_INITIAL (expr
);
27743 expr
= make_pack_expansion (expr
);
27744 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27747 parens
.require_close (parser
);
27752 /* Parse the operand of `sizeof' (or a similar operator). Returns
27753 either a TYPE or an expression, depending on the form of the
27754 input. The KEYWORD indicates which kind of expression we have
27758 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27760 tree expr
= NULL_TREE
;
27761 const char *saved_message
;
27763 bool saved_integral_constant_expression_p
;
27764 bool saved_non_integral_constant_expression_p
;
27766 /* If it's a `...', then we are computing the length of a parameter
27768 if (keyword
== RID_SIZEOF
27769 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27770 return cp_parser_sizeof_pack (parser
);
27772 /* Types cannot be defined in a `sizeof' expression. Save away the
27774 saved_message
= parser
->type_definition_forbidden_message
;
27775 /* And create the new one. */
27776 tmp
= concat ("types may not be defined in %<",
27777 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27778 "%> expressions", NULL
);
27779 parser
->type_definition_forbidden_message
= tmp
;
27781 /* The restrictions on constant-expressions do not apply inside
27782 sizeof expressions. */
27783 saved_integral_constant_expression_p
27784 = parser
->integral_constant_expression_p
;
27785 saved_non_integral_constant_expression_p
27786 = parser
->non_integral_constant_expression_p
;
27787 parser
->integral_constant_expression_p
= false;
27789 /* Do not actually evaluate the expression. */
27790 ++cp_unevaluated_operand
;
27791 ++c_inhibit_evaluation_warnings
;
27792 /* If it's a `(', then we might be looking at the type-id
27794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27796 tree type
= NULL_TREE
;
27798 /* We can't be sure yet whether we're looking at a type-id or an
27800 cp_parser_parse_tentatively (parser
);
27802 matching_parens parens
;
27803 parens
.consume_open (parser
);
27805 /* Note: as a GNU Extension, compound literals are considered
27806 postfix-expressions as they are in C99, so they are valid
27807 arguments to sizeof. See comment in cp_parser_cast_expression
27809 if (cp_parser_compound_literal_p (parser
))
27810 cp_parser_simulate_error (parser
);
27813 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
27814 parser
->in_type_id_in_expr_p
= true;
27815 /* Look for the type-id. */
27816 type
= cp_parser_type_id (parser
);
27817 /* Look for the closing `)'. */
27818 parens
.require_close (parser
);
27819 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
27822 /* If all went well, then we're done. */
27823 if (cp_parser_parse_definitely (parser
))
27825 cp_decl_specifier_seq decl_specs
;
27827 /* Build a trivial decl-specifier-seq. */
27828 clear_decl_specs (&decl_specs
);
27829 decl_specs
.type
= type
;
27831 /* Call grokdeclarator to figure out what type this is. */
27832 expr
= grokdeclarator (NULL
,
27836 /*attrlist=*/NULL
);
27840 /* If the type-id production did not work out, then we must be
27841 looking at the unary-expression production. */
27843 expr
= cp_parser_unary_expression (parser
);
27845 /* Go back to evaluating expressions. */
27846 --cp_unevaluated_operand
;
27847 --c_inhibit_evaluation_warnings
;
27849 /* Free the message we created. */
27851 /* And restore the old one. */
27852 parser
->type_definition_forbidden_message
= saved_message
;
27853 parser
->integral_constant_expression_p
27854 = saved_integral_constant_expression_p
;
27855 parser
->non_integral_constant_expression_p
27856 = saved_non_integral_constant_expression_p
;
27861 /* If the current declaration has no declarator, return true. */
27864 cp_parser_declares_only_class_p (cp_parser
*parser
)
27866 /* If the next token is a `;' or a `,' then there is no
27868 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27869 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
27872 /* Update the DECL_SPECS to reflect the storage class indicated by
27876 cp_parser_set_storage_class (cp_parser
*parser
,
27877 cp_decl_specifier_seq
*decl_specs
,
27881 cp_storage_class storage_class
;
27883 if (parser
->in_unbraced_linkage_specification_p
)
27885 error_at (token
->location
, "invalid use of %qD in linkage specification",
27886 ridpointers
[keyword
]);
27889 else if (decl_specs
->storage_class
!= sc_none
)
27891 decl_specs
->conflicting_specifiers_p
= true;
27895 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
27896 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
27897 && decl_specs
->gnu_thread_keyword_p
)
27899 pedwarn (decl_specs
->locations
[ds_thread
], 0,
27900 "%<__thread%> before %qD", ridpointers
[keyword
]);
27906 storage_class
= sc_auto
;
27909 storage_class
= sc_register
;
27912 storage_class
= sc_static
;
27915 storage_class
= sc_extern
;
27918 storage_class
= sc_mutable
;
27921 gcc_unreachable ();
27923 decl_specs
->storage_class
= storage_class
;
27924 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
27926 /* A storage class specifier cannot be applied alongside a typedef
27927 specifier. If there is a typedef specifier present then set
27928 conflicting_specifiers_p which will trigger an error later
27929 on in grokdeclarator. */
27930 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
27931 decl_specs
->conflicting_specifiers_p
= true;
27934 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27935 is true, the type is a class or enum definition. */
27938 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
27941 bool type_definition_p
)
27943 decl_specs
->any_specifiers_p
= true;
27945 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27946 (with, for example, in "typedef int wchar_t;") we remember that
27947 this is what happened. In system headers, we ignore these
27948 declarations so that G++ can work with system headers that are not
27950 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
27951 && !type_definition_p
27952 && (type_spec
== boolean_type_node
27953 || type_spec
== char16_type_node
27954 || type_spec
== char32_type_node
27955 || type_spec
== wchar_type_node
)
27956 && (decl_specs
->type
27957 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
27958 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
27959 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
27960 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
27962 decl_specs
->redefined_builtin_type
= type_spec
;
27963 set_and_check_decl_spec_loc (decl_specs
,
27964 ds_redefined_builtin_type_spec
,
27966 if (!decl_specs
->type
)
27968 decl_specs
->type
= type_spec
;
27969 decl_specs
->type_definition_p
= false;
27970 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
27973 else if (decl_specs
->type
)
27974 decl_specs
->multiple_types_p
= true;
27977 decl_specs
->type
= type_spec
;
27978 decl_specs
->type_definition_p
= type_definition_p
;
27979 decl_specs
->redefined_builtin_type
= NULL_TREE
;
27980 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
27984 /* True iff TOKEN is the GNU keyword __thread. */
27987 token_is__thread (cp_token
*token
)
27989 gcc_assert (token
->keyword
== RID_THREAD
);
27990 return id_equal (token
->u
.value
, "__thread");
27993 /* Set the location for a declarator specifier and check if it is
27996 DECL_SPECS is the sequence of declarator specifiers onto which to
27999 DS is the single declarator specifier to set which location is to
28000 be set onto the existing sequence of declarators.
28002 LOCATION is the location for the declarator specifier to
28006 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28007 cp_decl_spec ds
, cp_token
*token
)
28009 gcc_assert (ds
< ds_last
);
28011 if (decl_specs
== NULL
)
28014 source_location location
= token
->location
;
28016 if (decl_specs
->locations
[ds
] == 0)
28018 decl_specs
->locations
[ds
] = location
;
28019 if (ds
== ds_thread
)
28020 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28026 if (decl_specs
->locations
[ds_long_long
] != 0)
28027 error_at (location
,
28028 "%<long long long%> is too long for GCC");
28031 decl_specs
->locations
[ds_long_long
] = location
;
28032 pedwarn_cxx98 (location
,
28034 "ISO C++ 1998 does not support %<long long%>");
28037 else if (ds
== ds_thread
)
28039 bool gnu
= token_is__thread (token
);
28040 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28041 error_at (location
,
28042 "both %<__thread%> and %<thread_local%> specified");
28045 gcc_rich_location
richloc (location
);
28046 richloc
.add_fixit_remove ();
28047 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28052 static const char *const decl_spec_names
[] = {
28069 gcc_rich_location
richloc (location
);
28070 richloc
.add_fixit_remove ();
28071 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28076 /* Return true iff the declarator specifier DS is present in the
28077 sequence of declarator specifiers DECL_SPECS. */
28080 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28083 gcc_assert (ds
< ds_last
);
28085 if (decl_specs
== NULL
)
28088 return decl_specs
->locations
[ds
] != 0;
28091 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28092 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28095 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28097 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28100 /* Issue an error message indicating that TOKEN_DESC was expected.
28101 If KEYWORD is true, it indicated this function is called by
28102 cp_parser_require_keword and the required token can only be
28103 a indicated keyword.
28105 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28106 within any error as the location of an "opening" token matching
28107 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28108 RT_CLOSE_PAREN). */
28111 cp_parser_required_error (cp_parser
*parser
,
28112 required_token token_desc
,
28114 location_t matching_location
)
28116 if (cp_parser_simulate_error (parser
))
28119 const char *gmsgid
= NULL
;
28120 switch (token_desc
)
28123 gmsgid
= G_("expected %<new%>");
28126 gmsgid
= G_("expected %<delete%>");
28129 gmsgid
= G_("expected %<return%>");
28132 gmsgid
= G_("expected %<while%>");
28135 gmsgid
= G_("expected %<extern%>");
28137 case RT_STATIC_ASSERT
:
28138 gmsgid
= G_("expected %<static_assert%>");
28141 gmsgid
= G_("expected %<decltype%>");
28144 gmsgid
= G_("expected %<operator%>");
28147 gmsgid
= G_("expected %<class%>");
28150 gmsgid
= G_("expected %<template%>");
28153 gmsgid
= G_("expected %<namespace%>");
28156 gmsgid
= G_("expected %<using%>");
28159 gmsgid
= G_("expected %<asm%>");
28162 gmsgid
= G_("expected %<try%>");
28165 gmsgid
= G_("expected %<catch%>");
28168 gmsgid
= G_("expected %<throw%>");
28171 gmsgid
= G_("expected %<__label__%>");
28174 gmsgid
= G_("expected %<@try%>");
28176 case RT_AT_SYNCHRONIZED
:
28177 gmsgid
= G_("expected %<@synchronized%>");
28180 gmsgid
= G_("expected %<@throw%>");
28182 case RT_TRANSACTION_ATOMIC
:
28183 gmsgid
= G_("expected %<__transaction_atomic%>");
28185 case RT_TRANSACTION_RELAXED
:
28186 gmsgid
= G_("expected %<__transaction_relaxed%>");
28192 if (!gmsgid
&& !keyword
)
28194 switch (token_desc
)
28197 gmsgid
= G_("expected %<;%>");
28199 case RT_OPEN_PAREN
:
28200 gmsgid
= G_("expected %<(%>");
28202 case RT_CLOSE_BRACE
:
28203 gmsgid
= G_("expected %<}%>");
28205 case RT_OPEN_BRACE
:
28206 gmsgid
= G_("expected %<{%>");
28208 case RT_CLOSE_SQUARE
:
28209 gmsgid
= G_("expected %<]%>");
28211 case RT_OPEN_SQUARE
:
28212 gmsgid
= G_("expected %<[%>");
28215 gmsgid
= G_("expected %<,%>");
28218 gmsgid
= G_("expected %<::%>");
28221 gmsgid
= G_("expected %<<%>");
28224 gmsgid
= G_("expected %<>%>");
28227 gmsgid
= G_("expected %<=%>");
28230 gmsgid
= G_("expected %<...%>");
28233 gmsgid
= G_("expected %<*%>");
28236 gmsgid
= G_("expected %<~%>");
28239 gmsgid
= G_("expected %<:%>");
28241 case RT_COLON_SCOPE
:
28242 gmsgid
= G_("expected %<:%> or %<::%>");
28244 case RT_CLOSE_PAREN
:
28245 gmsgid
= G_("expected %<)%>");
28247 case RT_COMMA_CLOSE_PAREN
:
28248 gmsgid
= G_("expected %<,%> or %<)%>");
28250 case RT_PRAGMA_EOL
:
28251 gmsgid
= G_("expected end of line");
28254 gmsgid
= G_("expected identifier");
28257 gmsgid
= G_("expected selection-statement");
28260 gmsgid
= G_("expected iteration-statement");
28263 gmsgid
= G_("expected jump-statement");
28266 gmsgid
= G_("expected class-key");
28268 case RT_CLASS_TYPENAME_TEMPLATE
:
28269 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28272 gcc_unreachable ();
28277 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28281 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28282 issue an error message indicating that TOKEN_DESC was expected.
28284 Returns the token consumed, if the token had the appropriate type.
28285 Otherwise, returns NULL.
28287 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28288 within any error as the location of an "opening" token matching
28289 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28290 RT_CLOSE_PAREN). */
28293 cp_parser_require (cp_parser
* parser
,
28294 enum cpp_ttype type
,
28295 required_token token_desc
,
28296 location_t matching_location
)
28298 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28299 return cp_lexer_consume_token (parser
->lexer
);
28302 /* Output the MESSAGE -- unless we're parsing tentatively. */
28303 if (!cp_parser_simulate_error (parser
))
28304 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28305 matching_location
);
28310 /* An error message is produced if the next token is not '>'.
28311 All further tokens are skipped until the desired token is
28312 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28315 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28317 /* Current level of '< ... >'. */
28318 unsigned level
= 0;
28319 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28320 unsigned nesting_depth
= 0;
28322 /* Are we ready, yet? If not, issue error message. */
28323 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28326 /* Skip tokens until the desired token is found. */
28329 /* Peek at the next token. */
28330 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28333 if (!nesting_depth
)
28338 if (cxx_dialect
== cxx98
)
28339 /* C++0x views the `>>' operator as two `>' tokens, but
28342 else if (!nesting_depth
&& level
-- == 0)
28344 /* We've hit a `>>' where the first `>' closes the
28345 template argument list, and the second `>' is
28346 spurious. Just consume the `>>' and stop; we've
28347 already produced at least one error. */
28348 cp_lexer_consume_token (parser
->lexer
);
28351 /* Fall through for C++0x, so we handle the second `>' in
28353 gcc_fallthrough ();
28356 if (!nesting_depth
&& level
-- == 0)
28358 /* We've reached the token we want, consume it and stop. */
28359 cp_lexer_consume_token (parser
->lexer
);
28364 case CPP_OPEN_PAREN
:
28365 case CPP_OPEN_SQUARE
:
28369 case CPP_CLOSE_PAREN
:
28370 case CPP_CLOSE_SQUARE
:
28371 if (nesting_depth
-- == 0)
28376 case CPP_PRAGMA_EOL
:
28377 case CPP_SEMICOLON
:
28378 case CPP_OPEN_BRACE
:
28379 case CPP_CLOSE_BRACE
:
28380 /* The '>' was probably forgotten, don't look further. */
28387 /* Consume this token. */
28388 cp_lexer_consume_token (parser
->lexer
);
28392 /* If the next token is the indicated keyword, consume it. Otherwise,
28393 issue an error message indicating that TOKEN_DESC was expected.
28395 Returns the token consumed, if the token had the appropriate type.
28396 Otherwise, returns NULL. */
28399 cp_parser_require_keyword (cp_parser
* parser
,
28401 required_token token_desc
)
28403 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28405 if (token
&& token
->keyword
!= keyword
)
28407 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28415 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28416 function-definition. */
28419 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28421 return (/* An ordinary function-body begins with an `{'. */
28422 token
->type
== CPP_OPEN_BRACE
28423 /* A ctor-initializer begins with a `:'. */
28424 || token
->type
== CPP_COLON
28425 /* A function-try-block begins with `try'. */
28426 || token
->keyword
== RID_TRY
28427 /* A function-transaction-block begins with `__transaction_atomic'
28428 or `__transaction_relaxed'. */
28429 || token
->keyword
== RID_TRANSACTION_ATOMIC
28430 || token
->keyword
== RID_TRANSACTION_RELAXED
28431 /* The named return value extension begins with `return'. */
28432 || token
->keyword
== RID_RETURN
);
28435 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28439 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28443 token
= cp_lexer_peek_token (parser
->lexer
);
28444 return (token
->type
== CPP_OPEN_BRACE
28445 || (token
->type
== CPP_COLON
28446 && !parser
->colon_doesnt_start_class_def_p
));
28449 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28450 C++0x) ending a template-argument. */
28453 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28457 token
= cp_lexer_peek_token (parser
->lexer
);
28458 return (token
->type
== CPP_COMMA
28459 || token
->type
== CPP_GREATER
28460 || token
->type
== CPP_ELLIPSIS
28461 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28464 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28465 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28468 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28473 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28474 if (token
->type
== CPP_LESS
)
28476 /* Check for the sequence `<::' in the original code. It would be lexed as
28477 `[:', where `[' is a digraph, and there is no whitespace before
28479 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28482 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28483 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28489 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28490 or none_type otherwise. */
28492 static enum tag_types
28493 cp_parser_token_is_class_key (cp_token
* token
)
28495 switch (token
->keyword
)
28500 return record_type
;
28509 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28510 or none_type otherwise or if the token is null. */
28512 static enum tag_types
28513 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28518 switch (token
->keyword
)
28523 return typename_type
;
28530 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28533 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28535 if (type
== error_mark_node
)
28537 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28539 if (permerror (input_location
, "%qs tag used in naming %q#T",
28540 class_key
== union_type
? "union"
28541 : class_key
== record_type
? "struct" : "class",
28543 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28544 "%q#T was previously declared here", type
);
28548 /* Issue an error message if DECL is redeclared with different
28549 access than its original declaration [class.access.spec/3].
28550 This applies to nested classes, nested class templates and
28551 enumerations [class.mem/1]. */
28554 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28557 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28558 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28561 if ((TREE_PRIVATE (decl
)
28562 != (current_access_specifier
== access_private_node
))
28563 || (TREE_PROTECTED (decl
)
28564 != (current_access_specifier
== access_protected_node
)))
28565 error_at (location
, "%qD redeclared with different access", decl
);
28568 /* Look for the `template' keyword, as a syntactic disambiguator.
28569 Return TRUE iff it is present, in which case it will be
28573 cp_parser_optional_template_keyword (cp_parser
*parser
)
28575 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28577 /* In C++98 the `template' keyword can only be used within templates;
28578 outside templates the parser can always figure out what is a
28579 template and what is not. In C++11, per the resolution of DR 468,
28580 `template' is allowed in cases where it is not strictly necessary. */
28581 if (!processing_template_decl
28582 && pedantic
&& cxx_dialect
== cxx98
)
28584 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28585 pedwarn (token
->location
, OPT_Wpedantic
,
28586 "in C++98 %<template%> (as a disambiguator) is only "
28587 "allowed within templates");
28588 /* If this part of the token stream is rescanned, the same
28589 error message would be generated. So, we purge the token
28590 from the stream. */
28591 cp_lexer_purge_token (parser
->lexer
);
28596 /* Consume the `template' keyword. */
28597 cp_lexer_consume_token (parser
->lexer
);
28604 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28605 set PARSER->SCOPE, and perform other related actions. */
28608 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28610 struct tree_check
*check_value
;
28612 /* Get the stored value. */
28613 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28614 /* Set the scope from the stored value. */
28615 parser
->scope
= saved_checks_value (check_value
);
28616 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28617 parser
->object_scope
= NULL_TREE
;
28620 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28621 encounter the end of a block before what we were looking for. */
28624 cp_parser_cache_group (cp_parser
*parser
,
28625 enum cpp_ttype end
,
28630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28632 /* Abort a parenthesized expression if we encounter a semicolon. */
28633 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28634 && token
->type
== CPP_SEMICOLON
)
28636 /* If we've reached the end of the file, stop. */
28637 if (token
->type
== CPP_EOF
28638 || (end
!= CPP_PRAGMA_EOL
28639 && token
->type
== CPP_PRAGMA_EOL
))
28641 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28642 /* We've hit the end of an enclosing block, so there's been some
28643 kind of syntax error. */
28646 /* Consume the token. */
28647 cp_lexer_consume_token (parser
->lexer
);
28648 /* See if it starts a new group. */
28649 if (token
->type
== CPP_OPEN_BRACE
)
28651 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28652 /* In theory this should probably check end == '}', but
28653 cp_parser_save_member_function_body needs it to exit
28654 after either '}' or ')' when called with ')'. */
28658 else if (token
->type
== CPP_OPEN_PAREN
)
28660 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28661 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28664 else if (token
->type
== CPP_PRAGMA
)
28665 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28666 else if (token
->type
== end
)
28671 /* Like above, for caching a default argument or NSDMI. Both of these are
28672 terminated by a non-nested comma, but it can be unclear whether or not a
28673 comma is nested in a template argument list unless we do more parsing.
28674 In order to handle this ambiguity, when we encounter a ',' after a '<'
28675 we try to parse what follows as a parameter-declaration-list (in the
28676 case of a default argument) or a member-declarator (in the case of an
28677 NSDMI). If that succeeds, then we stop caching. */
28680 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28682 unsigned depth
= 0;
28683 int maybe_template_id
= 0;
28684 cp_token
*first_token
;
28686 tree default_argument
;
28688 /* Add tokens until we have processed the entire default
28689 argument. We add the range [first_token, token). */
28690 first_token
= cp_lexer_peek_token (parser
->lexer
);
28691 if (first_token
->type
== CPP_OPEN_BRACE
)
28693 /* For list-initialization, this is straightforward. */
28694 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28695 token
= cp_lexer_peek_token (parser
->lexer
);
28701 /* Peek at the next token. */
28702 token
= cp_lexer_peek_token (parser
->lexer
);
28703 /* What we do depends on what token we have. */
28704 switch (token
->type
)
28706 /* In valid code, a default argument must be
28707 immediately followed by a `,' `)', or `...'. */
28709 if (depth
== 0 && maybe_template_id
)
28711 /* If we've seen a '<', we might be in a
28712 template-argument-list. Until Core issue 325 is
28713 resolved, we don't know how this situation ought
28714 to be handled, so try to DTRT. We check whether
28715 what comes after the comma is a valid parameter
28716 declaration list. If it is, then the comma ends
28717 the default argument; otherwise the default
28718 argument continues. */
28719 bool error
= false;
28722 /* Set ITALP so cp_parser_parameter_declaration_list
28723 doesn't decide to commit to this parse. */
28724 bool saved_italp
= parser
->in_template_argument_list_p
;
28725 parser
->in_template_argument_list_p
= true;
28727 cp_parser_parse_tentatively (parser
);
28731 /* Parse declarators until we reach a non-comma or
28732 somthing that cannot be an initializer.
28733 Just checking whether we're looking at a single
28734 declarator is insufficient. Consider:
28735 int var = tuple<T,U>::x;
28736 The template parameter 'U' looks exactly like a
28740 int ctor_dtor_or_conv_p
;
28741 cp_lexer_consume_token (parser
->lexer
);
28742 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28743 &ctor_dtor_or_conv_p
,
28744 /*parenthesized_p=*/NULL
,
28746 /*friend_p=*/false);
28747 peek
= cp_lexer_peek_token (parser
->lexer
);
28748 if (cp_parser_error_occurred (parser
))
28751 while (peek
->type
== CPP_COMMA
);
28752 /* If we met an '=' or ';' then the original comma
28753 was the end of the NSDMI. Otherwise assume
28754 we're still in the NSDMI. */
28755 error
= (peek
->type
!= CPP_EQ
28756 && peek
->type
!= CPP_SEMICOLON
);
28760 cp_lexer_consume_token (parser
->lexer
);
28761 begin_scope (sk_function_parms
, NULL_TREE
);
28762 cp_parser_parameter_declaration_list (parser
, &error
);
28763 pop_bindings_and_leave_scope ();
28765 if (!cp_parser_error_occurred (parser
) && !error
)
28767 cp_parser_abort_tentative_parse (parser
);
28769 parser
->in_template_argument_list_p
= saved_italp
;
28773 case CPP_CLOSE_PAREN
:
28775 /* If we run into a non-nested `;', `}', or `]',
28776 then the code is invalid -- but the default
28777 argument is certainly over. */
28778 case CPP_SEMICOLON
:
28779 case CPP_CLOSE_BRACE
:
28780 case CPP_CLOSE_SQUARE
:
28782 /* Handle correctly int n = sizeof ... ( p ); */
28783 && token
->type
!= CPP_ELLIPSIS
)
28785 /* Update DEPTH, if necessary. */
28786 else if (token
->type
== CPP_CLOSE_PAREN
28787 || token
->type
== CPP_CLOSE_BRACE
28788 || token
->type
== CPP_CLOSE_SQUARE
)
28792 case CPP_OPEN_PAREN
:
28793 case CPP_OPEN_SQUARE
:
28794 case CPP_OPEN_BRACE
:
28800 /* This might be the comparison operator, or it might
28801 start a template argument list. */
28802 ++maybe_template_id
;
28806 if (cxx_dialect
== cxx98
)
28808 /* Fall through for C++0x, which treats the `>>'
28809 operator like two `>' tokens in certain
28811 gcc_fallthrough ();
28816 /* This might be an operator, or it might close a
28817 template argument list. But if a previous '<'
28818 started a template argument list, this will have
28819 closed it, so we can't be in one anymore. */
28820 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
28821 if (maybe_template_id
< 0)
28822 maybe_template_id
= 0;
28826 /* If we run out of tokens, issue an error message. */
28828 case CPP_PRAGMA_EOL
:
28829 error_at (token
->location
, "file ends in default argument");
28830 return error_mark_node
;
28834 /* In these cases, we should look for template-ids.
28835 For example, if the default argument is
28836 `X<int, double>()', we need to do name lookup to
28837 figure out whether or not `X' is a template; if
28838 so, the `,' does not end the default argument.
28840 That is not yet done. */
28847 /* If we've reached the end, stop. */
28851 /* Add the token to the token block. */
28852 token
= cp_lexer_consume_token (parser
->lexer
);
28855 /* Create a DEFAULT_ARG to represent the unparsed default
28857 default_argument
= make_node (DEFAULT_ARG
);
28858 DEFARG_TOKENS (default_argument
)
28859 = cp_token_cache_new (first_token
, token
);
28860 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
28862 return default_argument
;
28865 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28868 defarg_location (tree default_argument
)
28870 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
28871 location_t start
= tokens
->first
->location
;
28872 location_t end
= tokens
->last
->location
;
28873 return make_location (start
, start
, end
);
28876 /* Begin parsing tentatively. We always save tokens while parsing
28877 tentatively so that if the tentative parsing fails we can restore the
28881 cp_parser_parse_tentatively (cp_parser
* parser
)
28883 /* Enter a new parsing context. */
28884 parser
->context
= cp_parser_context_new (parser
->context
);
28885 /* Begin saving tokens. */
28886 cp_lexer_save_tokens (parser
->lexer
);
28887 /* In order to avoid repetitive access control error messages,
28888 access checks are queued up until we are no longer parsing
28890 push_deferring_access_checks (dk_deferred
);
28893 /* Commit to the currently active tentative parse. */
28896 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
28898 cp_parser_context
*context
;
28901 /* Mark all of the levels as committed. */
28902 lexer
= parser
->lexer
;
28903 for (context
= parser
->context
; context
->next
; context
= context
->next
)
28905 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28907 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28908 while (!cp_lexer_saving_tokens (lexer
))
28909 lexer
= lexer
->next
;
28910 cp_lexer_commit_tokens (lexer
);
28914 /* Commit to the topmost currently active tentative parse.
28916 Note that this function shouldn't be called when there are
28917 irreversible side-effects while in a tentative state. For
28918 example, we shouldn't create a permanent entry in the symbol
28919 table, or issue an error message that might not apply if the
28920 tentative parse is aborted. */
28923 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
28925 cp_parser_context
*context
= parser
->context
;
28926 cp_lexer
*lexer
= parser
->lexer
;
28930 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
28932 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
28934 while (!cp_lexer_saving_tokens (lexer
))
28935 lexer
= lexer
->next
;
28936 cp_lexer_commit_tokens (lexer
);
28940 /* Abort the currently active tentative parse. All consumed tokens
28941 will be rolled back, and no diagnostics will be issued. */
28944 cp_parser_abort_tentative_parse (cp_parser
* parser
)
28946 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
28947 || errorcount
> 0);
28948 cp_parser_simulate_error (parser
);
28949 /* Now, pretend that we want to see if the construct was
28950 successfully parsed. */
28951 cp_parser_parse_definitely (parser
);
28954 /* Stop parsing tentatively. If a parse error has occurred, restore the
28955 token stream. Otherwise, commit to the tokens we have consumed.
28956 Returns true if no error occurred; false otherwise. */
28959 cp_parser_parse_definitely (cp_parser
* parser
)
28961 bool error_occurred
;
28962 cp_parser_context
*context
;
28964 /* Remember whether or not an error occurred, since we are about to
28965 destroy that information. */
28966 error_occurred
= cp_parser_error_occurred (parser
);
28967 /* Remove the topmost context from the stack. */
28968 context
= parser
->context
;
28969 parser
->context
= context
->next
;
28970 /* If no parse errors occurred, commit to the tentative parse. */
28971 if (!error_occurred
)
28973 /* Commit to the tokens read tentatively, unless that was
28975 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
28976 cp_lexer_commit_tokens (parser
->lexer
);
28978 pop_to_parent_deferring_access_checks ();
28980 /* Otherwise, if errors occurred, roll back our state so that things
28981 are just as they were before we began the tentative parse. */
28984 cp_lexer_rollback_tokens (parser
->lexer
);
28985 pop_deferring_access_checks ();
28987 /* Add the context to the front of the free list. */
28988 context
->next
= cp_parser_context_free_list
;
28989 cp_parser_context_free_list
= context
;
28991 return !error_occurred
;
28994 /* Returns true if we are parsing tentatively and are not committed to
28995 this tentative parse. */
28998 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29000 return (cp_parser_parsing_tentatively (parser
)
29001 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29004 /* Returns nonzero iff an error has occurred during the most recent
29005 tentative parse. */
29008 cp_parser_error_occurred (cp_parser
* parser
)
29010 return (cp_parser_parsing_tentatively (parser
)
29011 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29014 /* Returns nonzero if GNU extensions are allowed. */
29017 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29019 return parser
->allow_gnu_extensions_p
;
29022 /* Objective-C++ Productions */
29025 /* Parse an Objective-C expression, which feeds into a primary-expression
29029 objc-message-expression
29030 objc-string-literal
29031 objc-encode-expression
29032 objc-protocol-expression
29033 objc-selector-expression
29035 Returns a tree representation of the expression. */
29038 cp_parser_objc_expression (cp_parser
* parser
)
29040 /* Try to figure out what kind of declaration is present. */
29041 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29045 case CPP_OPEN_SQUARE
:
29046 return cp_parser_objc_message_expression (parser
);
29048 case CPP_OBJC_STRING
:
29049 kwd
= cp_lexer_consume_token (parser
->lexer
);
29050 return objc_build_string_object (kwd
->u
.value
);
29053 switch (kwd
->keyword
)
29055 case RID_AT_ENCODE
:
29056 return cp_parser_objc_encode_expression (parser
);
29058 case RID_AT_PROTOCOL
:
29059 return cp_parser_objc_protocol_expression (parser
);
29061 case RID_AT_SELECTOR
:
29062 return cp_parser_objc_selector_expression (parser
);
29069 error_at (kwd
->location
,
29070 "misplaced %<@%D%> Objective-C++ construct",
29072 cp_parser_skip_to_end_of_block_or_statement (parser
);
29075 return error_mark_node
;
29078 /* Parse an Objective-C message expression.
29080 objc-message-expression:
29081 [ objc-message-receiver objc-message-args ]
29083 Returns a representation of an Objective-C message. */
29086 cp_parser_objc_message_expression (cp_parser
* parser
)
29088 tree receiver
, messageargs
;
29090 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29091 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29092 receiver
= cp_parser_objc_message_receiver (parser
);
29093 messageargs
= cp_parser_objc_message_args (parser
);
29094 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29095 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29097 tree result
= objc_build_message_expr (receiver
, messageargs
);
29099 /* Construct a location e.g.
29102 ranging from the '[' to the ']', with the caret at the start. */
29103 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29104 protected_set_expr_location (result
, combined_loc
);
29109 /* Parse an objc-message-receiver.
29111 objc-message-receiver:
29113 simple-type-specifier
29115 Returns a representation of the type or expression. */
29118 cp_parser_objc_message_receiver (cp_parser
* parser
)
29122 /* An Objective-C message receiver may be either (1) a type
29123 or (2) an expression. */
29124 cp_parser_parse_tentatively (parser
);
29125 rcv
= cp_parser_expression (parser
);
29127 /* If that worked out, fine. */
29128 if (cp_parser_parse_definitely (parser
))
29131 cp_parser_parse_tentatively (parser
);
29132 rcv
= cp_parser_simple_type_specifier (parser
,
29133 /*decl_specs=*/NULL
,
29134 CP_PARSER_FLAGS_NONE
);
29136 if (cp_parser_parse_definitely (parser
))
29137 return objc_get_class_reference (rcv
);
29139 cp_parser_error (parser
, "objective-c++ message receiver expected");
29140 return error_mark_node
;
29143 /* Parse the arguments and selectors comprising an Objective-C message.
29148 objc-selector-args , objc-comma-args
29150 objc-selector-args:
29151 objc-selector [opt] : assignment-expression
29152 objc-selector-args objc-selector [opt] : assignment-expression
29155 assignment-expression
29156 objc-comma-args , assignment-expression
29158 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29159 selector arguments and TREE_VALUE containing a list of comma
29163 cp_parser_objc_message_args (cp_parser
* parser
)
29165 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29166 bool maybe_unary_selector_p
= true;
29167 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29169 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29171 tree selector
= NULL_TREE
, arg
;
29173 if (token
->type
!= CPP_COLON
)
29174 selector
= cp_parser_objc_selector (parser
);
29176 /* Detect if we have a unary selector. */
29177 if (maybe_unary_selector_p
29178 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29179 return build_tree_list (selector
, NULL_TREE
);
29181 maybe_unary_selector_p
= false;
29182 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29183 arg
= cp_parser_assignment_expression (parser
);
29186 = chainon (sel_args
,
29187 build_tree_list (selector
, arg
));
29189 token
= cp_lexer_peek_token (parser
->lexer
);
29192 /* Handle non-selector arguments, if any. */
29193 while (token
->type
== CPP_COMMA
)
29197 cp_lexer_consume_token (parser
->lexer
);
29198 arg
= cp_parser_assignment_expression (parser
);
29201 = chainon (addl_args
,
29202 build_tree_list (NULL_TREE
, arg
));
29204 token
= cp_lexer_peek_token (parser
->lexer
);
29207 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29209 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29210 return build_tree_list (error_mark_node
, error_mark_node
);
29213 return build_tree_list (sel_args
, addl_args
);
29216 /* Parse an Objective-C encode expression.
29218 objc-encode-expression:
29219 @encode objc-typename
29221 Returns an encoded representation of the type argument. */
29224 cp_parser_objc_encode_expression (cp_parser
* parser
)
29228 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29230 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29231 matching_parens parens
;
29232 parens
.require_open (parser
);
29233 token
= cp_lexer_peek_token (parser
->lexer
);
29234 type
= complete_type (cp_parser_type_id (parser
));
29235 parens
.require_close (parser
);
29239 error_at (token
->location
,
29240 "%<@encode%> must specify a type as an argument");
29241 return error_mark_node
;
29244 /* This happens if we find @encode(T) (where T is a template
29245 typename or something dependent on a template typename) when
29246 parsing a template. In that case, we can't compile it
29247 immediately, but we rather create an AT_ENCODE_EXPR which will
29248 need to be instantiated when the template is used.
29250 if (dependent_type_p (type
))
29252 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29253 TREE_READONLY (value
) = 1;
29258 /* Build a location of the form:
29261 with caret==start at the @ token, finishing at the close paren. */
29262 location_t combined_loc
29263 = make_location (start_loc
, start_loc
,
29264 cp_lexer_previous_token (parser
->lexer
)->location
);
29266 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29269 /* Parse an Objective-C @defs expression. */
29272 cp_parser_objc_defs_expression (cp_parser
*parser
)
29276 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29277 matching_parens parens
;
29278 parens
.require_open (parser
);
29279 name
= cp_parser_identifier (parser
);
29280 parens
.require_close (parser
);
29282 return objc_get_class_ivars (name
);
29285 /* Parse an Objective-C protocol expression.
29287 objc-protocol-expression:
29288 @protocol ( identifier )
29290 Returns a representation of the protocol expression. */
29293 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29296 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29298 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29299 matching_parens parens
;
29300 parens
.require_open (parser
);
29301 proto
= cp_parser_identifier (parser
);
29302 parens
.require_close (parser
);
29304 /* Build a location of the form:
29307 with caret==start at the @ token, finishing at the close paren. */
29308 location_t combined_loc
29309 = make_location (start_loc
, start_loc
,
29310 cp_lexer_previous_token (parser
->lexer
)->location
);
29311 tree result
= objc_build_protocol_expr (proto
);
29312 protected_set_expr_location (result
, combined_loc
);
29316 /* Parse an Objective-C selector expression.
29318 objc-selector-expression:
29319 @selector ( objc-method-signature )
29321 objc-method-signature:
29327 objc-selector-seq objc-selector :
29329 Returns a representation of the method selector. */
29332 cp_parser_objc_selector_expression (cp_parser
* parser
)
29334 tree sel_seq
= NULL_TREE
;
29335 bool maybe_unary_selector_p
= true;
29337 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29339 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29340 matching_parens parens
;
29341 parens
.require_open (parser
);
29342 token
= cp_lexer_peek_token (parser
->lexer
);
29344 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29345 || token
->type
== CPP_SCOPE
)
29347 tree selector
= NULL_TREE
;
29349 if (token
->type
!= CPP_COLON
29350 || token
->type
== CPP_SCOPE
)
29351 selector
= cp_parser_objc_selector (parser
);
29353 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29354 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29356 /* Detect if we have a unary selector. */
29357 if (maybe_unary_selector_p
)
29359 sel_seq
= selector
;
29360 goto finish_selector
;
29364 cp_parser_error (parser
, "expected %<:%>");
29367 maybe_unary_selector_p
= false;
29368 token
= cp_lexer_consume_token (parser
->lexer
);
29370 if (token
->type
== CPP_SCOPE
)
29373 = chainon (sel_seq
,
29374 build_tree_list (selector
, NULL_TREE
));
29376 = chainon (sel_seq
,
29377 build_tree_list (NULL_TREE
, NULL_TREE
));
29381 = chainon (sel_seq
,
29382 build_tree_list (selector
, NULL_TREE
));
29384 token
= cp_lexer_peek_token (parser
->lexer
);
29388 parens
.require_close (parser
);
29391 /* Build a location of the form:
29394 with caret==start at the @ token, finishing at the close paren. */
29395 location_t combined_loc
29396 = make_location (loc
, loc
,
29397 cp_lexer_previous_token (parser
->lexer
)->location
);
29398 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29399 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29400 protected_set_expr_location (result
, combined_loc
);
29404 /* Parse a list of identifiers.
29406 objc-identifier-list:
29408 objc-identifier-list , identifier
29410 Returns a TREE_LIST of identifier nodes. */
29413 cp_parser_objc_identifier_list (cp_parser
* parser
)
29419 identifier
= cp_parser_identifier (parser
);
29420 if (identifier
== error_mark_node
)
29421 return error_mark_node
;
29423 list
= build_tree_list (NULL_TREE
, identifier
);
29424 sep
= cp_lexer_peek_token (parser
->lexer
);
29426 while (sep
->type
== CPP_COMMA
)
29428 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29429 identifier
= cp_parser_identifier (parser
);
29430 if (identifier
== error_mark_node
)
29433 list
= chainon (list
, build_tree_list (NULL_TREE
,
29435 sep
= cp_lexer_peek_token (parser
->lexer
);
29441 /* Parse an Objective-C alias declaration.
29443 objc-alias-declaration:
29444 @compatibility_alias identifier identifier ;
29446 This function registers the alias mapping with the Objective-C front end.
29447 It returns nothing. */
29450 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29454 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29455 alias
= cp_parser_identifier (parser
);
29456 orig
= cp_parser_identifier (parser
);
29457 objc_declare_alias (alias
, orig
);
29458 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29461 /* Parse an Objective-C class forward-declaration.
29463 objc-class-declaration:
29464 @class objc-identifier-list ;
29466 The function registers the forward declarations with the Objective-C
29467 front end. It returns nothing. */
29470 cp_parser_objc_class_declaration (cp_parser
* parser
)
29472 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29477 id
= cp_parser_identifier (parser
);
29478 if (id
== error_mark_node
)
29481 objc_declare_class (id
);
29483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29484 cp_lexer_consume_token (parser
->lexer
);
29488 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29491 /* Parse a list of Objective-C protocol references.
29493 objc-protocol-refs-opt:
29494 objc-protocol-refs [opt]
29496 objc-protocol-refs:
29497 < objc-identifier-list >
29499 Returns a TREE_LIST of identifiers, if any. */
29502 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29504 tree protorefs
= NULL_TREE
;
29506 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29508 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29509 protorefs
= cp_parser_objc_identifier_list (parser
);
29510 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29516 /* Parse a Objective-C visibility specification. */
29519 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29521 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29523 switch (vis
->keyword
)
29525 case RID_AT_PRIVATE
:
29526 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29528 case RID_AT_PROTECTED
:
29529 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29531 case RID_AT_PUBLIC
:
29532 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29534 case RID_AT_PACKAGE
:
29535 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29541 /* Eat '@private'/'@protected'/'@public'. */
29542 cp_lexer_consume_token (parser
->lexer
);
29545 /* Parse an Objective-C method type. Return 'true' if it is a class
29546 (+) method, and 'false' if it is an instance (-) method. */
29549 cp_parser_objc_method_type (cp_parser
* parser
)
29551 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29557 /* Parse an Objective-C protocol qualifier. */
29560 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29562 tree quals
= NULL_TREE
, node
;
29563 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29565 node
= token
->u
.value
;
29567 while (node
&& identifier_p (node
)
29568 && (node
== ridpointers
[(int) RID_IN
]
29569 || node
== ridpointers
[(int) RID_OUT
]
29570 || node
== ridpointers
[(int) RID_INOUT
]
29571 || node
== ridpointers
[(int) RID_BYCOPY
]
29572 || node
== ridpointers
[(int) RID_BYREF
]
29573 || node
== ridpointers
[(int) RID_ONEWAY
]))
29575 quals
= tree_cons (NULL_TREE
, node
, quals
);
29576 cp_lexer_consume_token (parser
->lexer
);
29577 token
= cp_lexer_peek_token (parser
->lexer
);
29578 node
= token
->u
.value
;
29584 /* Parse an Objective-C typename. */
29587 cp_parser_objc_typename (cp_parser
* parser
)
29589 tree type_name
= NULL_TREE
;
29591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29593 tree proto_quals
, cp_type
= NULL_TREE
;
29595 matching_parens parens
;
29596 parens
.consume_open (parser
); /* Eat '('. */
29597 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29599 /* An ObjC type name may consist of just protocol qualifiers, in which
29600 case the type shall default to 'id'. */
29601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29603 cp_type
= cp_parser_type_id (parser
);
29605 /* If the type could not be parsed, an error has already
29606 been produced. For error recovery, behave as if it had
29607 not been specified, which will use the default type
29609 if (cp_type
== error_mark_node
)
29611 cp_type
= NULL_TREE
;
29612 /* We need to skip to the closing parenthesis as
29613 cp_parser_type_id() does not seem to do it for
29615 cp_parser_skip_to_closing_parenthesis (parser
,
29616 /*recovering=*/true,
29617 /*or_comma=*/false,
29618 /*consume_paren=*/false);
29622 parens
.require_close (parser
);
29623 type_name
= build_tree_list (proto_quals
, cp_type
);
29629 /* Check to see if TYPE refers to an Objective-C selector name. */
29632 cp_parser_objc_selector_p (enum cpp_ttype type
)
29634 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29635 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29636 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29637 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29638 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29641 /* Parse an Objective-C selector. */
29644 cp_parser_objc_selector (cp_parser
* parser
)
29646 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29648 if (!cp_parser_objc_selector_p (token
->type
))
29650 error_at (token
->location
, "invalid Objective-C++ selector name");
29651 return error_mark_node
;
29654 /* C++ operator names are allowed to appear in ObjC selectors. */
29655 switch (token
->type
)
29657 case CPP_AND_AND
: return get_identifier ("and");
29658 case CPP_AND_EQ
: return get_identifier ("and_eq");
29659 case CPP_AND
: return get_identifier ("bitand");
29660 case CPP_OR
: return get_identifier ("bitor");
29661 case CPP_COMPL
: return get_identifier ("compl");
29662 case CPP_NOT
: return get_identifier ("not");
29663 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29664 case CPP_OR_OR
: return get_identifier ("or");
29665 case CPP_OR_EQ
: return get_identifier ("or_eq");
29666 case CPP_XOR
: return get_identifier ("xor");
29667 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29668 default: return token
->u
.value
;
29672 /* Parse an Objective-C params list. */
29675 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29677 tree params
= NULL_TREE
;
29678 bool maybe_unary_selector_p
= true;
29679 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29681 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29683 tree selector
= NULL_TREE
, type_name
, identifier
;
29684 tree parm_attr
= NULL_TREE
;
29686 if (token
->keyword
== RID_ATTRIBUTE
)
29689 if (token
->type
!= CPP_COLON
)
29690 selector
= cp_parser_objc_selector (parser
);
29692 /* Detect if we have a unary selector. */
29693 if (maybe_unary_selector_p
29694 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29696 params
= selector
; /* Might be followed by attributes. */
29700 maybe_unary_selector_p
= false;
29701 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29703 /* Something went quite wrong. There should be a colon
29704 here, but there is not. Stop parsing parameters. */
29707 type_name
= cp_parser_objc_typename (parser
);
29708 /* New ObjC allows attributes on parameters too. */
29709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29710 parm_attr
= cp_parser_attributes_opt (parser
);
29711 identifier
= cp_parser_identifier (parser
);
29715 objc_build_keyword_decl (selector
,
29720 token
= cp_lexer_peek_token (parser
->lexer
);
29723 if (params
== NULL_TREE
)
29725 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29726 return error_mark_node
;
29729 /* We allow tail attributes for the method. */
29730 if (token
->keyword
== RID_ATTRIBUTE
)
29732 *attributes
= cp_parser_attributes_opt (parser
);
29733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29734 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29736 cp_parser_error (parser
,
29737 "method attributes must be specified at the end");
29738 return error_mark_node
;
29741 if (params
== NULL_TREE
)
29743 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29744 return error_mark_node
;
29749 /* Parse the non-keyword Objective-C params. */
29752 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29755 tree params
= make_node (TREE_LIST
);
29756 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29757 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29759 while (token
->type
== CPP_COMMA
)
29761 cp_parameter_declarator
*parmdecl
;
29764 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29765 token
= cp_lexer_peek_token (parser
->lexer
);
29767 if (token
->type
== CPP_ELLIPSIS
)
29769 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29771 token
= cp_lexer_peek_token (parser
->lexer
);
29775 /* TODO: parse attributes for tail parameters. */
29776 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29777 parm
= grokdeclarator (parmdecl
->declarator
,
29778 &parmdecl
->decl_specifiers
,
29779 PARM
, /*initialized=*/0,
29780 /*attrlist=*/NULL
);
29782 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29783 token
= cp_lexer_peek_token (parser
->lexer
);
29786 /* We allow tail attributes for the method. */
29787 if (token
->keyword
== RID_ATTRIBUTE
)
29789 if (*attributes
== NULL_TREE
)
29791 *attributes
= cp_parser_attributes_opt (parser
);
29792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29793 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29797 /* We have an error, but parse the attributes, so that we can
29799 *attributes
= cp_parser_attributes_opt (parser
);
29801 cp_parser_error (parser
,
29802 "method attributes must be specified at the end");
29803 return error_mark_node
;
29809 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29812 cp_parser_objc_interstitial_code (cp_parser
* parser
)
29814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29816 /* If the next token is `extern' and the following token is a string
29817 literal, then we have a linkage specification. */
29818 if (token
->keyword
== RID_EXTERN
29819 && cp_parser_is_pure_string_literal
29820 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
29821 cp_parser_linkage_specification (parser
);
29822 /* Handle #pragma, if any. */
29823 else if (token
->type
== CPP_PRAGMA
)
29824 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
29825 /* Allow stray semicolons. */
29826 else if (token
->type
== CPP_SEMICOLON
)
29827 cp_lexer_consume_token (parser
->lexer
);
29828 /* Mark methods as optional or required, when building protocols. */
29829 else if (token
->keyword
== RID_AT_OPTIONAL
)
29831 cp_lexer_consume_token (parser
->lexer
);
29832 objc_set_method_opt (true);
29834 else if (token
->keyword
== RID_AT_REQUIRED
)
29836 cp_lexer_consume_token (parser
->lexer
);
29837 objc_set_method_opt (false);
29839 else if (token
->keyword
== RID_NAMESPACE
)
29840 cp_parser_namespace_definition (parser
);
29841 /* Other stray characters must generate errors. */
29842 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
29844 cp_lexer_consume_token (parser
->lexer
);
29845 error ("stray %qs between Objective-C++ methods",
29846 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
29848 /* Finally, try to parse a block-declaration, or a function-definition. */
29850 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29853 /* Parse a method signature. */
29856 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
29858 tree rettype
, kwdparms
, optparms
;
29859 bool ellipsis
= false;
29860 bool is_class_method
;
29862 is_class_method
= cp_parser_objc_method_type (parser
);
29863 rettype
= cp_parser_objc_typename (parser
);
29864 *attributes
= NULL_TREE
;
29865 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
29866 if (kwdparms
== error_mark_node
)
29867 return error_mark_node
;
29868 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
29869 if (optparms
== error_mark_node
)
29870 return error_mark_node
;
29872 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
29876 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
29879 cp_lexer_save_tokens (parser
->lexer
);
29880 tattr
= cp_parser_attributes_opt (parser
);
29881 gcc_assert (tattr
) ;
29883 /* If the attributes are followed by a method introducer, this is not allowed.
29884 Dump the attributes and flag the situation. */
29885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
29886 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
29889 /* Otherwise, the attributes introduce some interstitial code, possibly so
29890 rewind to allow that check. */
29891 cp_lexer_rollback_tokens (parser
->lexer
);
29895 /* Parse an Objective-C method prototype list. */
29898 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
29900 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29902 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29904 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29906 tree attributes
, sig
;
29907 bool is_class_method
;
29908 if (token
->type
== CPP_PLUS
)
29909 is_class_method
= true;
29911 is_class_method
= false;
29912 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
29913 if (sig
== error_mark_node
)
29915 cp_parser_skip_to_end_of_block_or_statement (parser
);
29916 token
= cp_lexer_peek_token (parser
->lexer
);
29919 objc_add_method_declaration (is_class_method
, sig
, attributes
);
29920 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29922 else if (token
->keyword
== RID_AT_PROPERTY
)
29923 cp_parser_objc_at_property_declaration (parser
);
29924 else if (token
->keyword
== RID_ATTRIBUTE
29925 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
29926 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29928 "prefix attributes are ignored for methods");
29930 /* Allow for interspersed non-ObjC++ code. */
29931 cp_parser_objc_interstitial_code (parser
);
29933 token
= cp_lexer_peek_token (parser
->lexer
);
29936 if (token
->type
!= CPP_EOF
)
29937 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29939 cp_parser_error (parser
, "expected %<@end%>");
29941 objc_finish_interface ();
29944 /* Parse an Objective-C method definition list. */
29947 cp_parser_objc_method_definition_list (cp_parser
* parser
)
29949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29951 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
29955 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
29958 tree sig
, attribute
;
29959 bool is_class_method
;
29960 if (token
->type
== CPP_PLUS
)
29961 is_class_method
= true;
29963 is_class_method
= false;
29964 push_deferring_access_checks (dk_deferred
);
29965 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
29966 if (sig
== error_mark_node
)
29968 cp_parser_skip_to_end_of_block_or_statement (parser
);
29969 token
= cp_lexer_peek_token (parser
->lexer
);
29972 objc_start_method_definition (is_class_method
, sig
, attribute
,
29975 /* For historical reasons, we accept an optional semicolon. */
29976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29977 cp_lexer_consume_token (parser
->lexer
);
29979 ptk
= cp_lexer_peek_token (parser
->lexer
);
29980 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
29981 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
29983 perform_deferred_access_checks (tf_warning_or_error
);
29984 stop_deferring_access_checks ();
29985 meth
= cp_parser_function_definition_after_declarator (parser
,
29987 pop_deferring_access_checks ();
29988 objc_finish_method_definition (meth
);
29991 /* The following case will be removed once @synthesize is
29992 completely implemented. */
29993 else if (token
->keyword
== RID_AT_PROPERTY
)
29994 cp_parser_objc_at_property_declaration (parser
);
29995 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
29996 cp_parser_objc_at_synthesize_declaration (parser
);
29997 else if (token
->keyword
== RID_AT_DYNAMIC
)
29998 cp_parser_objc_at_dynamic_declaration (parser
);
29999 else if (token
->keyword
== RID_ATTRIBUTE
30000 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30001 warning_at (token
->location
, OPT_Wattributes
,
30002 "prefix attributes are ignored for methods");
30004 /* Allow for interspersed non-ObjC++ code. */
30005 cp_parser_objc_interstitial_code (parser
);
30007 token
= cp_lexer_peek_token (parser
->lexer
);
30010 if (token
->type
!= CPP_EOF
)
30011 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30013 cp_parser_error (parser
, "expected %<@end%>");
30015 objc_finish_implementation ();
30018 /* Parse Objective-C ivars. */
30021 cp_parser_objc_class_ivars (cp_parser
* parser
)
30023 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30025 if (token
->type
!= CPP_OPEN_BRACE
)
30026 return; /* No ivars specified. */
30028 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30029 token
= cp_lexer_peek_token (parser
->lexer
);
30031 while (token
->type
!= CPP_CLOSE_BRACE
30032 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30034 cp_decl_specifier_seq declspecs
;
30035 int decl_class_or_enum_p
;
30036 tree prefix_attributes
;
30038 cp_parser_objc_visibility_spec (parser
);
30040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30043 cp_parser_decl_specifier_seq (parser
,
30044 CP_PARSER_FLAGS_OPTIONAL
,
30046 &decl_class_or_enum_p
);
30048 /* auto, register, static, extern, mutable. */
30049 if (declspecs
.storage_class
!= sc_none
)
30051 cp_parser_error (parser
, "invalid type for instance variable");
30052 declspecs
.storage_class
= sc_none
;
30055 /* thread_local. */
30056 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30058 cp_parser_error (parser
, "invalid type for instance variable");
30059 declspecs
.locations
[ds_thread
] = 0;
30063 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30065 cp_parser_error (parser
, "invalid type for instance variable");
30066 declspecs
.locations
[ds_typedef
] = 0;
30069 prefix_attributes
= declspecs
.attributes
;
30070 declspecs
.attributes
= NULL_TREE
;
30072 /* Keep going until we hit the `;' at the end of the
30074 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30076 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30077 cp_declarator
*declarator
= NULL
;
30078 int ctor_dtor_or_conv_p
;
30080 /* Check for a (possibly unnamed) bitfield declaration. */
30081 token
= cp_lexer_peek_token (parser
->lexer
);
30082 if (token
->type
== CPP_COLON
)
30085 if (token
->type
== CPP_NAME
30086 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30089 /* Get the name of the bitfield. */
30090 declarator
= make_id_declarator (NULL_TREE
,
30091 cp_parser_identifier (parser
),
30095 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30096 /* Get the width of the bitfield. */
30098 = cp_parser_constant_expression (parser
);
30102 /* Parse the declarator. */
30104 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30105 &ctor_dtor_or_conv_p
,
30106 /*parenthesized_p=*/NULL
,
30107 /*member_p=*/false,
30108 /*friend_p=*/false);
30111 /* Look for attributes that apply to the ivar. */
30112 attributes
= cp_parser_attributes_opt (parser
);
30113 /* Remember which attributes are prefix attributes and
30115 first_attribute
= attributes
;
30116 /* Combine the attributes. */
30117 attributes
= chainon (prefix_attributes
, attributes
);
30120 /* Create the bitfield declaration. */
30121 decl
= grokbitfield (declarator
, &declspecs
,
30122 width
, NULL_TREE
, attributes
);
30124 decl
= grokfield (declarator
, &declspecs
,
30125 NULL_TREE
, /*init_const_expr_p=*/false,
30126 NULL_TREE
, attributes
);
30128 /* Add the instance variable. */
30129 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30130 objc_add_instance_variable (decl
);
30132 /* Reset PREFIX_ATTRIBUTES. */
30133 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30134 attributes
= TREE_CHAIN (attributes
);
30136 TREE_CHAIN (attributes
) = NULL_TREE
;
30138 token
= cp_lexer_peek_token (parser
->lexer
);
30140 if (token
->type
== CPP_COMMA
)
30142 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30148 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30149 token
= cp_lexer_peek_token (parser
->lexer
);
30152 if (token
->keyword
== RID_AT_END
)
30153 cp_parser_error (parser
, "expected %<}%>");
30155 /* Do not consume the RID_AT_END, so it will be read again as terminating
30156 the @interface of @implementation. */
30157 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30158 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30160 /* For historical reasons, we accept an optional semicolon. */
30161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30162 cp_lexer_consume_token (parser
->lexer
);
30165 /* Parse an Objective-C protocol declaration. */
30168 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30170 tree proto
, protorefs
;
30173 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30174 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30176 tok
= cp_lexer_peek_token (parser
->lexer
);
30177 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30178 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30182 /* See if we have a forward declaration or a definition. */
30183 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30185 /* Try a forward declaration first. */
30186 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30192 id
= cp_parser_identifier (parser
);
30193 if (id
== error_mark_node
)
30196 objc_declare_protocol (id
, attributes
);
30198 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30199 cp_lexer_consume_token (parser
->lexer
);
30203 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30206 /* Ok, we got a full-fledged definition (or at least should). */
30209 proto
= cp_parser_identifier (parser
);
30210 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30211 objc_start_protocol (proto
, protorefs
, attributes
);
30212 cp_parser_objc_method_prototype_list (parser
);
30216 /* Parse an Objective-C superclass or category. */
30219 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30222 tree
*categ
, bool *is_class_extension
)
30224 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30226 *super
= *categ
= NULL_TREE
;
30227 *is_class_extension
= false;
30228 if (next
->type
== CPP_COLON
)
30230 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30231 *super
= cp_parser_identifier (parser
);
30233 else if (next
->type
== CPP_OPEN_PAREN
)
30235 matching_parens parens
;
30236 parens
.consume_open (parser
); /* Eat '('. */
30238 /* If there is no category name, and this is an @interface, we
30239 have a class extension. */
30240 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30242 *categ
= NULL_TREE
;
30243 *is_class_extension
= true;
30246 *categ
= cp_parser_identifier (parser
);
30248 parens
.require_close (parser
);
30252 /* Parse an Objective-C class interface. */
30255 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30257 tree name
, super
, categ
, protos
;
30258 bool is_class_extension
;
30260 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30261 name
= cp_parser_identifier (parser
);
30262 if (name
== error_mark_node
)
30264 /* It's hard to recover because even if valid @interface stuff
30265 is to follow, we can't compile it (or validate it) if we
30266 don't even know which class it refers to. Let's assume this
30267 was a stray '@interface' token in the stream and skip it.
30271 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30272 &is_class_extension
);
30273 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30275 /* We have either a class or a category on our hands. */
30276 if (categ
|| is_class_extension
)
30277 objc_start_category_interface (name
, categ
, protos
, attributes
);
30280 objc_start_class_interface (name
, super
, protos
, attributes
);
30281 /* Handle instance variable declarations, if any. */
30282 cp_parser_objc_class_ivars (parser
);
30283 objc_continue_interface ();
30286 cp_parser_objc_method_prototype_list (parser
);
30289 /* Parse an Objective-C class implementation. */
30292 cp_parser_objc_class_implementation (cp_parser
* parser
)
30294 tree name
, super
, categ
;
30295 bool is_class_extension
;
30297 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30298 name
= cp_parser_identifier (parser
);
30299 if (name
== error_mark_node
)
30301 /* It's hard to recover because even if valid @implementation
30302 stuff is to follow, we can't compile it (or validate it) if
30303 we don't even know which class it refers to. Let's assume
30304 this was a stray '@implementation' token in the stream and
30309 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30310 &is_class_extension
);
30312 /* We have either a class or a category on our hands. */
30314 objc_start_category_implementation (name
, categ
);
30317 objc_start_class_implementation (name
, super
);
30318 /* Handle instance variable declarations, if any. */
30319 cp_parser_objc_class_ivars (parser
);
30320 objc_continue_implementation ();
30323 cp_parser_objc_method_definition_list (parser
);
30326 /* Consume the @end token and finish off the implementation. */
30329 cp_parser_objc_end_implementation (cp_parser
* parser
)
30331 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30332 objc_finish_implementation ();
30335 /* Parse an Objective-C declaration. */
30338 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30340 /* Try to figure out what kind of declaration is present. */
30341 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30344 switch (kwd
->keyword
)
30349 error_at (kwd
->location
, "attributes may not be specified before"
30350 " the %<@%D%> Objective-C++ keyword",
30354 case RID_AT_IMPLEMENTATION
:
30355 warning_at (kwd
->location
, OPT_Wattributes
,
30356 "prefix attributes are ignored before %<@%D%>",
30363 switch (kwd
->keyword
)
30366 cp_parser_objc_alias_declaration (parser
);
30369 cp_parser_objc_class_declaration (parser
);
30371 case RID_AT_PROTOCOL
:
30372 cp_parser_objc_protocol_declaration (parser
, attributes
);
30374 case RID_AT_INTERFACE
:
30375 cp_parser_objc_class_interface (parser
, attributes
);
30377 case RID_AT_IMPLEMENTATION
:
30378 cp_parser_objc_class_implementation (parser
);
30381 cp_parser_objc_end_implementation (parser
);
30384 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30386 cp_parser_skip_to_end_of_block_or_statement (parser
);
30390 /* Parse an Objective-C try-catch-finally statement.
30392 objc-try-catch-finally-stmt:
30393 @try compound-statement objc-catch-clause-seq [opt]
30394 objc-finally-clause [opt]
30396 objc-catch-clause-seq:
30397 objc-catch-clause objc-catch-clause-seq [opt]
30400 @catch ( objc-exception-declaration ) compound-statement
30402 objc-finally-clause:
30403 @finally compound-statement
30405 objc-exception-declaration:
30406 parameter-declaration
30409 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30413 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30414 for C. Keep them in sync. */
30417 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30419 location_t location
;
30422 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30423 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30424 objc_maybe_warn_exceptions (location
);
30425 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30426 node, lest it get absorbed into the surrounding block. */
30427 stmt
= push_stmt_list ();
30428 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30429 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30431 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30433 cp_parameter_declarator
*parm
;
30434 tree parameter_declaration
= error_mark_node
;
30435 bool seen_open_paren
= false;
30436 matching_parens parens
;
30438 cp_lexer_consume_token (parser
->lexer
);
30439 if (parens
.require_open (parser
))
30440 seen_open_paren
= true;
30441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30443 /* We have "@catch (...)" (where the '...' are literally
30444 what is in the code). Skip the '...'.
30445 parameter_declaration is set to NULL_TREE, and
30446 objc_being_catch_clauses() knows that that means
30448 cp_lexer_consume_token (parser
->lexer
);
30449 parameter_declaration
= NULL_TREE
;
30453 /* We have "@catch (NSException *exception)" or something
30454 like that. Parse the parameter declaration. */
30455 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30457 parameter_declaration
= error_mark_node
;
30459 parameter_declaration
= grokdeclarator (parm
->declarator
,
30460 &parm
->decl_specifiers
,
30461 PARM
, /*initialized=*/0,
30462 /*attrlist=*/NULL
);
30464 if (seen_open_paren
)
30465 parens
.require_close (parser
);
30468 /* If there was no open parenthesis, we are recovering from
30469 an error, and we are trying to figure out what mistake
30470 the user has made. */
30472 /* If there is an immediate closing parenthesis, the user
30473 probably forgot the opening one (ie, they typed "@catch
30474 NSException *e)". Parse the closing parenthesis and keep
30476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30477 cp_lexer_consume_token (parser
->lexer
);
30479 /* If these is no immediate closing parenthesis, the user
30480 probably doesn't know that parenthesis are required at
30481 all (ie, they typed "@catch NSException *e"). So, just
30482 forget about the closing parenthesis and keep going. */
30484 objc_begin_catch_clause (parameter_declaration
);
30485 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30486 objc_finish_catch_clause ();
30488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30490 cp_lexer_consume_token (parser
->lexer
);
30491 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30492 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30493 node, lest it get absorbed into the surrounding block. */
30494 stmt
= push_stmt_list ();
30495 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30496 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30499 return objc_finish_try_stmt ();
30502 /* Parse an Objective-C synchronized statement.
30504 objc-synchronized-stmt:
30505 @synchronized ( expression ) compound-statement
30507 Returns NULL_TREE. */
30510 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30512 location_t location
;
30515 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30517 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30518 objc_maybe_warn_exceptions (location
);
30519 matching_parens parens
;
30520 parens
.require_open (parser
);
30521 lock
= cp_parser_expression (parser
);
30522 parens
.require_close (parser
);
30524 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30525 node, lest it get absorbed into the surrounding block. */
30526 stmt
= push_stmt_list ();
30527 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30529 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30532 /* Parse an Objective-C throw statement.
30535 @throw assignment-expression [opt] ;
30537 Returns a constructed '@throw' statement. */
30540 cp_parser_objc_throw_statement (cp_parser
*parser
)
30542 tree expr
= NULL_TREE
;
30543 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30545 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30547 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30548 expr
= cp_parser_expression (parser
);
30550 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30552 return objc_build_throw_stmt (loc
, expr
);
30555 /* Parse an Objective-C statement. */
30558 cp_parser_objc_statement (cp_parser
* parser
)
30560 /* Try to figure out what kind of declaration is present. */
30561 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30563 switch (kwd
->keyword
)
30566 return cp_parser_objc_try_catch_finally_statement (parser
);
30567 case RID_AT_SYNCHRONIZED
:
30568 return cp_parser_objc_synchronized_statement (parser
);
30570 return cp_parser_objc_throw_statement (parser
);
30572 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30574 cp_parser_skip_to_end_of_block_or_statement (parser
);
30577 return error_mark_node
;
30580 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30581 look ahead to see if an objc keyword follows the attributes. This
30582 is to detect the use of prefix attributes on ObjC @interface and
30586 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30588 cp_lexer_save_tokens (parser
->lexer
);
30589 *attrib
= cp_parser_attributes_opt (parser
);
30590 gcc_assert (*attrib
);
30591 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30593 cp_lexer_commit_tokens (parser
->lexer
);
30596 cp_lexer_rollback_tokens (parser
->lexer
);
30600 /* This routine is a minimal replacement for
30601 c_parser_struct_declaration () used when parsing the list of
30602 types/names or ObjC++ properties. For example, when parsing the
30605 @property (readonly) int a, b, c;
30607 this function is responsible for parsing "int a, int b, int c" and
30608 returning the declarations as CHAIN of DECLs.
30610 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30611 similar parsing. */
30613 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30615 tree decls
= NULL_TREE
;
30616 cp_decl_specifier_seq declspecs
;
30617 int decl_class_or_enum_p
;
30618 tree prefix_attributes
;
30620 cp_parser_decl_specifier_seq (parser
,
30621 CP_PARSER_FLAGS_NONE
,
30623 &decl_class_or_enum_p
);
30625 if (declspecs
.type
== error_mark_node
)
30626 return error_mark_node
;
30628 /* auto, register, static, extern, mutable. */
30629 if (declspecs
.storage_class
!= sc_none
)
30631 cp_parser_error (parser
, "invalid type for property");
30632 declspecs
.storage_class
= sc_none
;
30635 /* thread_local. */
30636 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30638 cp_parser_error (parser
, "invalid type for property");
30639 declspecs
.locations
[ds_thread
] = 0;
30643 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30645 cp_parser_error (parser
, "invalid type for property");
30646 declspecs
.locations
[ds_typedef
] = 0;
30649 prefix_attributes
= declspecs
.attributes
;
30650 declspecs
.attributes
= NULL_TREE
;
30652 /* Keep going until we hit the `;' at the end of the declaration. */
30653 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30655 tree attributes
, first_attribute
, decl
;
30656 cp_declarator
*declarator
;
30659 /* Parse the declarator. */
30660 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30661 NULL
, NULL
, false, false);
30663 /* Look for attributes that apply to the ivar. */
30664 attributes
= cp_parser_attributes_opt (parser
);
30665 /* Remember which attributes are prefix attributes and
30667 first_attribute
= attributes
;
30668 /* Combine the attributes. */
30669 attributes
= chainon (prefix_attributes
, attributes
);
30671 decl
= grokfield (declarator
, &declspecs
,
30672 NULL_TREE
, /*init_const_expr_p=*/false,
30673 NULL_TREE
, attributes
);
30675 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30676 return error_mark_node
;
30678 /* Reset PREFIX_ATTRIBUTES. */
30679 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30680 attributes
= TREE_CHAIN (attributes
);
30682 TREE_CHAIN (attributes
) = NULL_TREE
;
30684 DECL_CHAIN (decl
) = decls
;
30687 token
= cp_lexer_peek_token (parser
->lexer
);
30688 if (token
->type
== CPP_COMMA
)
30690 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30699 /* Parse an Objective-C @property declaration. The syntax is:
30701 objc-property-declaration:
30702 '@property' objc-property-attributes[opt] struct-declaration ;
30704 objc-property-attributes:
30705 '(' objc-property-attribute-list ')'
30707 objc-property-attribute-list:
30708 objc-property-attribute
30709 objc-property-attribute-list, objc-property-attribute
30711 objc-property-attribute
30712 'getter' = identifier
30713 'setter' = identifier
30722 @property NSString *name;
30723 @property (readonly) id object;
30724 @property (retain, nonatomic, getter=getTheName) id name;
30725 @property int a, b, c;
30727 PS: This function is identical to
30728 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30730 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30732 /* The following variables hold the attributes of the properties as
30733 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30734 seen. When we see an attribute, we set them to 'true' (if they
30735 are boolean properties) or to the identifier (if they have an
30736 argument, ie, for getter and setter). Note that here we only
30737 parse the list of attributes, check the syntax and accumulate the
30738 attributes that we find. objc_add_property_declaration() will
30739 then process the information. */
30740 bool property_assign
= false;
30741 bool property_copy
= false;
30742 tree property_getter_ident
= NULL_TREE
;
30743 bool property_nonatomic
= false;
30744 bool property_readonly
= false;
30745 bool property_readwrite
= false;
30746 bool property_retain
= false;
30747 tree property_setter_ident
= NULL_TREE
;
30749 /* 'properties' is the list of properties that we read. Usually a
30750 single one, but maybe more (eg, in "@property int a, b, c;" there
30755 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30757 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30759 /* Parse the optional attribute list... */
30760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30763 matching_parens parens
;
30764 parens
.consume_open (parser
);
30768 bool syntax_error
= false;
30769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30772 if (token
->type
!= CPP_NAME
)
30774 cp_parser_error (parser
, "expected identifier");
30777 keyword
= C_RID_CODE (token
->u
.value
);
30778 cp_lexer_consume_token (parser
->lexer
);
30781 case RID_ASSIGN
: property_assign
= true; break;
30782 case RID_COPY
: property_copy
= true; break;
30783 case RID_NONATOMIC
: property_nonatomic
= true; break;
30784 case RID_READONLY
: property_readonly
= true; break;
30785 case RID_READWRITE
: property_readwrite
= true; break;
30786 case RID_RETAIN
: property_retain
= true; break;
30790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30792 if (keyword
== RID_GETTER
)
30793 cp_parser_error (parser
,
30794 "missing %<=%> (after %<getter%> attribute)");
30796 cp_parser_error (parser
,
30797 "missing %<=%> (after %<setter%> attribute)");
30798 syntax_error
= true;
30801 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
30802 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
30804 cp_parser_error (parser
, "expected identifier");
30805 syntax_error
= true;
30808 if (keyword
== RID_SETTER
)
30810 if (property_setter_ident
!= NULL_TREE
)
30812 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
30813 cp_lexer_consume_token (parser
->lexer
);
30816 property_setter_ident
= cp_parser_objc_selector (parser
);
30817 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30818 cp_parser_error (parser
, "setter name must terminate with %<:%>");
30820 cp_lexer_consume_token (parser
->lexer
);
30824 if (property_getter_ident
!= NULL_TREE
)
30826 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
30827 cp_lexer_consume_token (parser
->lexer
);
30830 property_getter_ident
= cp_parser_objc_selector (parser
);
30834 cp_parser_error (parser
, "unknown property attribute");
30835 syntax_error
= true;
30842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30843 cp_lexer_consume_token (parser
->lexer
);
30848 /* FIXME: "@property (setter, assign);" will generate a spurious
30849 "error: expected ‘)’ before ‘,’ token". This is because
30850 cp_parser_require, unlike the C counterpart, will produce an
30851 error even if we are in error recovery. */
30852 if (!parens
.require_close (parser
))
30854 cp_parser_skip_to_closing_parenthesis (parser
,
30855 /*recovering=*/true,
30856 /*or_comma=*/false,
30857 /*consume_paren=*/true);
30861 /* ... and the property declaration(s). */
30862 properties
= cp_parser_objc_struct_declaration (parser
);
30864 if (properties
== error_mark_node
)
30866 cp_parser_skip_to_end_of_statement (parser
);
30867 /* If the next token is now a `;', consume it. */
30868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30869 cp_lexer_consume_token (parser
->lexer
);
30873 if (properties
== NULL_TREE
)
30874 cp_parser_error (parser
, "expected identifier");
30877 /* Comma-separated properties are chained together in
30878 reverse order; add them one by one. */
30879 properties
= nreverse (properties
);
30881 for (; properties
; properties
= TREE_CHAIN (properties
))
30882 objc_add_property_declaration (loc
, copy_node (properties
),
30883 property_readonly
, property_readwrite
,
30884 property_assign
, property_retain
,
30885 property_copy
, property_nonatomic
,
30886 property_getter_ident
, property_setter_ident
);
30889 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30892 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30894 objc-synthesize-declaration:
30895 @synthesize objc-synthesize-identifier-list ;
30897 objc-synthesize-identifier-list:
30898 objc-synthesize-identifier
30899 objc-synthesize-identifier-list, objc-synthesize-identifier
30901 objc-synthesize-identifier
30903 identifier = identifier
30906 @synthesize MyProperty;
30907 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30909 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30910 for C. Keep them in sync.
30913 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
30915 tree list
= NULL_TREE
;
30917 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30919 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
30922 tree property
, ivar
;
30923 property
= cp_parser_identifier (parser
);
30924 if (property
== error_mark_node
)
30926 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
30931 cp_lexer_consume_token (parser
->lexer
);
30932 ivar
= cp_parser_identifier (parser
);
30933 if (ivar
== error_mark_node
)
30935 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30941 list
= chainon (list
, build_tree_list (ivar
, property
));
30942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30943 cp_lexer_consume_token (parser
->lexer
);
30947 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30948 objc_add_synthesize_declaration (loc
, list
);
30951 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30953 objc-dynamic-declaration:
30954 @dynamic identifier-list ;
30957 @dynamic MyProperty;
30958 @dynamic MyProperty, AnotherProperty;
30960 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30961 for C. Keep them in sync.
30964 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
30966 tree list
= NULL_TREE
;
30968 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30970 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
30974 property
= cp_parser_identifier (parser
);
30975 if (property
== error_mark_node
)
30977 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30980 list
= chainon (list
, build_tree_list (NULL
, property
));
30981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30982 cp_lexer_consume_token (parser
->lexer
);
30986 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30987 objc_add_dynamic_declaration (loc
, list
);
30991 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30993 /* Returns name of the next clause.
30994 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30995 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30996 returned and the token is consumed. */
30998 static pragma_omp_clause
30999 cp_parser_omp_clause_name (cp_parser
*parser
)
31001 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31003 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31004 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31005 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31006 result
= PRAGMA_OMP_CLAUSE_IF
;
31007 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31008 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31009 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31010 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31011 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31012 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31013 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31014 result
= PRAGMA_OMP_CLAUSE_FOR
;
31015 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31017 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31018 const char *p
= IDENTIFIER_POINTER (id
);
31023 if (!strcmp ("aligned", p
))
31024 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31025 else if (!strcmp ("async", p
))
31026 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31029 if (!strcmp ("collapse", p
))
31030 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31031 else if (!strcmp ("copy", p
))
31032 result
= PRAGMA_OACC_CLAUSE_COPY
;
31033 else if (!strcmp ("copyin", p
))
31034 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31035 else if (!strcmp ("copyout", p
))
31036 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31037 else if (!strcmp ("copyprivate", p
))
31038 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31039 else if (!strcmp ("create", p
))
31040 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31043 if (!strcmp ("defaultmap", p
))
31044 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31045 else if (!strcmp ("depend", p
))
31046 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31047 else if (!strcmp ("device", p
))
31048 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31049 else if (!strcmp ("deviceptr", p
))
31050 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31051 else if (!strcmp ("device_resident", p
))
31052 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31053 else if (!strcmp ("dist_schedule", p
))
31054 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31057 if (!strcmp ("final", p
))
31058 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31059 else if (!strcmp ("firstprivate", p
))
31060 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31061 else if (!strcmp ("from", p
))
31062 result
= PRAGMA_OMP_CLAUSE_FROM
;
31065 if (!strcmp ("gang", p
))
31066 result
= PRAGMA_OACC_CLAUSE_GANG
;
31067 else if (!strcmp ("grainsize", p
))
31068 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31071 if (!strcmp ("hint", p
))
31072 result
= PRAGMA_OMP_CLAUSE_HINT
;
31073 else if (!strcmp ("host", p
))
31074 result
= PRAGMA_OACC_CLAUSE_HOST
;
31077 if (!strcmp ("inbranch", p
))
31078 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31079 else if (!strcmp ("independent", p
))
31080 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31081 else if (!strcmp ("is_device_ptr", p
))
31082 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31085 if (!strcmp ("lastprivate", p
))
31086 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31087 else if (!strcmp ("linear", p
))
31088 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31089 else if (!strcmp ("link", p
))
31090 result
= PRAGMA_OMP_CLAUSE_LINK
;
31093 if (!strcmp ("map", p
))
31094 result
= PRAGMA_OMP_CLAUSE_MAP
;
31095 else if (!strcmp ("mergeable", p
))
31096 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31099 if (!strcmp ("nogroup", p
))
31100 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31101 else if (!strcmp ("notinbranch", p
))
31102 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31103 else if (!strcmp ("nowait", p
))
31104 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31105 else if (!strcmp ("num_gangs", p
))
31106 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31107 else if (!strcmp ("num_tasks", p
))
31108 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31109 else if (!strcmp ("num_teams", p
))
31110 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31111 else if (!strcmp ("num_threads", p
))
31112 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31113 else if (!strcmp ("num_workers", p
))
31114 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31117 if (!strcmp ("ordered", p
))
31118 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31121 if (!strcmp ("parallel", p
))
31122 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31123 else if (!strcmp ("present", p
))
31124 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31125 else if (!strcmp ("present_or_copy", p
)
31126 || !strcmp ("pcopy", p
))
31127 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31128 else if (!strcmp ("present_or_copyin", p
)
31129 || !strcmp ("pcopyin", p
))
31130 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31131 else if (!strcmp ("present_or_copyout", p
)
31132 || !strcmp ("pcopyout", p
))
31133 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31134 else if (!strcmp ("present_or_create", p
)
31135 || !strcmp ("pcreate", p
))
31136 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31137 else if (!strcmp ("priority", p
))
31138 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31139 else if (!strcmp ("proc_bind", p
))
31140 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31143 if (!strcmp ("reduction", p
))
31144 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31147 if (!strcmp ("safelen", p
))
31148 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31149 else if (!strcmp ("schedule", p
))
31150 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31151 else if (!strcmp ("sections", p
))
31152 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31153 else if (!strcmp ("self", p
))
31154 result
= PRAGMA_OACC_CLAUSE_SELF
;
31155 else if (!strcmp ("seq", p
))
31156 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31157 else if (!strcmp ("shared", p
))
31158 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31159 else if (!strcmp ("simd", p
))
31160 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31161 else if (!strcmp ("simdlen", p
))
31162 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31165 if (!strcmp ("taskgroup", p
))
31166 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31167 else if (!strcmp ("thread_limit", p
))
31168 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31169 else if (!strcmp ("threads", p
))
31170 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31171 else if (!strcmp ("tile", p
))
31172 result
= PRAGMA_OACC_CLAUSE_TILE
;
31173 else if (!strcmp ("to", p
))
31174 result
= PRAGMA_OMP_CLAUSE_TO
;
31177 if (!strcmp ("uniform", p
))
31178 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31179 else if (!strcmp ("untied", p
))
31180 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31181 else if (!strcmp ("use_device", p
))
31182 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31183 else if (!strcmp ("use_device_ptr", p
))
31184 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31187 if (!strcmp ("vector", p
))
31188 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31189 else if (!strcmp ("vector_length", p
))
31190 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31193 if (!strcmp ("wait", p
))
31194 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31195 else if (!strcmp ("worker", p
))
31196 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31201 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31202 cp_lexer_consume_token (parser
->lexer
);
31207 /* Validate that a clause of the given type does not already exist. */
31210 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31211 const char *name
, location_t location
)
31215 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31216 if (OMP_CLAUSE_CODE (c
) == code
)
31218 error_at (location
, "too many %qs clauses", name
);
31226 variable-list , identifier
31228 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31229 colon). An opening parenthesis will have been consumed by the caller.
31231 If KIND is nonzero, create the appropriate node and install the decl
31232 in OMP_CLAUSE_DECL and add the node to the head of the list.
31234 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31235 return the list created.
31237 COLON can be NULL if only closing parenthesis should end the list,
31238 or pointer to bool which will receive false if the list is terminated
31239 by closing parenthesis or true if the list is terminated by colon. */
31242 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31243 tree list
, bool *colon
)
31246 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31249 parser
->colon_corrects_to_scope_p
= false;
31256 token
= cp_lexer_peek_token (parser
->lexer
);
31258 && current_class_ptr
31259 && cp_parser_is_keyword (token
, RID_THIS
))
31261 decl
= finish_this_expr ();
31262 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31263 || CONVERT_EXPR_P (decl
))
31264 decl
= TREE_OPERAND (decl
, 0);
31265 cp_lexer_consume_token (parser
->lexer
);
31269 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31270 /*check_dependency_p=*/true,
31271 /*template_p=*/NULL
,
31272 /*declarator_p=*/false,
31273 /*optional_p=*/false);
31274 if (name
== error_mark_node
)
31277 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31278 if (decl
== error_mark_node
)
31279 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31282 if (decl
== error_mark_node
)
31284 else if (kind
!= 0)
31288 case OMP_CLAUSE__CACHE_
:
31289 /* The OpenACC cache directive explicitly only allows "array
31290 elements or subarrays". */
31291 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31293 error_at (token
->location
, "expected %<[%>");
31294 decl
= error_mark_node
;
31298 case OMP_CLAUSE_MAP
:
31299 case OMP_CLAUSE_FROM
:
31300 case OMP_CLAUSE_TO
:
31301 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31304 = cp_lexer_peek_token (parser
->lexer
)->location
;
31305 cp_id_kind idk
= CP_ID_KIND_NONE
;
31306 cp_lexer_consume_token (parser
->lexer
);
31307 decl
= convert_from_reference (decl
);
31309 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31314 case OMP_CLAUSE_DEPEND
:
31315 case OMP_CLAUSE_REDUCTION
:
31316 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31318 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31320 parser
->colon_corrects_to_scope_p
= false;
31321 cp_lexer_consume_token (parser
->lexer
);
31322 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31323 low_bound
= cp_parser_expression (parser
);
31325 parser
->colon_corrects_to_scope_p
31326 = saved_colon_corrects_to_scope_p
;
31327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31328 length
= integer_one_node
;
31331 /* Look for `:'. */
31332 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31334 if (!cp_lexer_next_token_is (parser
->lexer
,
31336 length
= cp_parser_expression (parser
);
31338 /* Look for the closing `]'. */
31339 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31343 decl
= tree_cons (low_bound
, length
, decl
);
31350 tree u
= build_omp_clause (token
->location
, kind
);
31351 OMP_CLAUSE_DECL (u
) = decl
;
31352 OMP_CLAUSE_CHAIN (u
) = list
;
31356 list
= tree_cons (decl
, NULL_TREE
, list
);
31359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31361 cp_lexer_consume_token (parser
->lexer
);
31365 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31367 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31370 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31374 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31378 /* Try to resync to an unnested comma. Copied from
31379 cp_parser_parenthesized_expression_list. */
31382 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31383 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31384 /*recovering=*/true,
31386 /*consume_paren=*/true);
31394 /* Similarly, but expect leading and trailing parenthesis. This is a very
31395 common case for omp clauses. */
31398 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31400 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31401 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31406 copy ( variable-list )
31407 copyin ( variable-list )
31408 copyout ( variable-list )
31409 create ( variable-list )
31410 delete ( variable-list )
31411 present ( variable-list )
31412 present_or_copy ( variable-list )
31413 pcopy ( variable-list )
31414 present_or_copyin ( variable-list )
31415 pcopyin ( variable-list )
31416 present_or_copyout ( variable-list )
31417 pcopyout ( variable-list )
31418 present_or_create ( variable-list )
31419 pcreate ( variable-list ) */
31422 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31425 enum gomp_map_kind kind
;
31428 case PRAGMA_OACC_CLAUSE_COPY
:
31429 kind
= GOMP_MAP_FORCE_TOFROM
;
31431 case PRAGMA_OACC_CLAUSE_COPYIN
:
31432 kind
= GOMP_MAP_FORCE_TO
;
31434 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31435 kind
= GOMP_MAP_FORCE_FROM
;
31437 case PRAGMA_OACC_CLAUSE_CREATE
:
31438 kind
= GOMP_MAP_FORCE_ALLOC
;
31440 case PRAGMA_OACC_CLAUSE_DELETE
:
31441 kind
= GOMP_MAP_DELETE
;
31443 case PRAGMA_OACC_CLAUSE_DEVICE
:
31444 kind
= GOMP_MAP_FORCE_TO
;
31446 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31447 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31449 case PRAGMA_OACC_CLAUSE_HOST
:
31450 case PRAGMA_OACC_CLAUSE_SELF
:
31451 kind
= GOMP_MAP_FORCE_FROM
;
31453 case PRAGMA_OACC_CLAUSE_LINK
:
31454 kind
= GOMP_MAP_LINK
;
31456 case PRAGMA_OACC_CLAUSE_PRESENT
:
31457 kind
= GOMP_MAP_FORCE_PRESENT
;
31459 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31460 kind
= GOMP_MAP_TOFROM
;
31462 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31463 kind
= GOMP_MAP_TO
;
31465 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31466 kind
= GOMP_MAP_FROM
;
31468 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31469 kind
= GOMP_MAP_ALLOC
;
31472 gcc_unreachable ();
31475 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31477 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31478 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31484 deviceptr ( variable-list ) */
31487 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31489 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31492 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31493 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31494 variable-list must only allow for pointer variables. */
31495 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31496 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31498 tree v
= TREE_PURPOSE (t
);
31499 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31500 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31501 OMP_CLAUSE_DECL (u
) = v
;
31502 OMP_CLAUSE_CHAIN (u
) = list
;
31516 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31517 enum omp_clause_code code
,
31518 tree list
, location_t location
)
31520 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31521 tree c
= build_omp_clause (location
, code
);
31522 OMP_CLAUSE_CHAIN (c
) = list
;
31527 num_gangs ( expression )
31528 num_workers ( expression )
31529 vector_length ( expression ) */
31532 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31533 const char *str
, tree list
)
31535 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31537 matching_parens parens
;
31538 if (!parens
.require_open (parser
))
31541 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31543 if (t
== error_mark_node
31544 || !parens
.require_close (parser
))
31546 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31547 /*or_comma=*/false,
31548 /*consume_paren=*/true);
31552 check_no_duplicate_clause (list
, code
, str
, loc
);
31554 tree c
= build_omp_clause (loc
, code
);
31555 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31556 OMP_CLAUSE_CHAIN (c
) = list
;
31562 gang [( gang-arg-list )]
31563 worker [( [num:] int-expr )]
31564 vector [( [length:] int-expr )]
31566 where gang-arg is one of:
31571 and size-expr may be:
31578 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31579 const char *str
, tree list
)
31581 const char *id
= "num";
31582 cp_lexer
*lexer
= parser
->lexer
;
31583 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31584 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31586 if (kind
== OMP_CLAUSE_VECTOR
)
31589 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31591 matching_parens parens
;
31592 parens
.consume_open (parser
);
31596 cp_token
*next
= cp_lexer_peek_token (lexer
);
31599 /* Gang static argument. */
31600 if (kind
== OMP_CLAUSE_GANG
31601 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31603 cp_lexer_consume_token (lexer
);
31605 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31606 goto cleanup_error
;
31609 if (ops
[idx
] != NULL
)
31611 cp_parser_error (parser
, "too many %<static%> arguments");
31612 goto cleanup_error
;
31615 /* Check for the '*' argument. */
31616 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31617 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31618 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31621 cp_lexer_consume_token (lexer
);
31622 ops
[idx
] = integer_minus_one_node
;
31624 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31626 cp_lexer_consume_token (lexer
);
31632 /* Worker num: argument and vector length: arguments. */
31633 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31634 && id_equal (next
->u
.value
, id
)
31635 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31637 cp_lexer_consume_token (lexer
); /* id */
31638 cp_lexer_consume_token (lexer
); /* ':' */
31641 /* Now collect the actual argument. */
31642 if (ops
[idx
] != NULL_TREE
)
31644 cp_parser_error (parser
, "unexpected argument");
31645 goto cleanup_error
;
31648 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31650 if (expr
== error_mark_node
)
31651 goto cleanup_error
;
31653 mark_exp_read (expr
);
31656 if (kind
== OMP_CLAUSE_GANG
31657 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31659 cp_lexer_consume_token (lexer
);
31666 if (!parens
.require_close (parser
))
31667 goto cleanup_error
;
31670 check_no_duplicate_clause (list
, kind
, str
, loc
);
31672 c
= build_omp_clause (loc
, kind
);
31675 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31677 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31678 OMP_CLAUSE_CHAIN (c
) = list
;
31683 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31688 tile ( size-expr-list ) */
31691 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31693 tree c
, expr
= error_mark_node
;
31694 tree tile
= NULL_TREE
;
31696 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31697 so, but the spec authors never considered such a case and have
31698 differing opinions on what it might mean, including 'not
31700 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31701 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31704 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31709 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31713 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31714 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31716 cp_lexer_consume_token (parser
->lexer
);
31717 expr
= integer_zero_node
;
31720 expr
= cp_parser_constant_expression (parser
);
31722 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31724 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31726 /* Consume the trailing ')'. */
31727 cp_lexer_consume_token (parser
->lexer
);
31729 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31730 tile
= nreverse (tile
);
31731 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31732 OMP_CLAUSE_CHAIN (c
) = list
;
31737 Parse wait clause or directive parameters. */
31740 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31742 vec
<tree
, va_gc
> *args
;
31745 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31747 /*allow_expansion_p=*/true,
31748 /*non_constant_p=*/NULL
);
31750 if (args
== NULL
|| args
->length () == 0)
31752 cp_parser_error (parser
, "expected integer expression before ')'");
31754 release_tree_vector (args
);
31758 args_tree
= build_tree_list_vec (args
);
31760 release_tree_vector (args
);
31762 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31764 tree targ
= TREE_VALUE (t
);
31766 if (targ
!= error_mark_node
)
31768 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31769 error ("%<wait%> expression must be integral");
31772 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31774 targ
= mark_rvalue_use (targ
);
31775 OMP_CLAUSE_DECL (c
) = targ
;
31776 OMP_CLAUSE_CHAIN (c
) = list
;
31786 wait ( int-expr-list ) */
31789 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
31791 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31793 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
31796 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
31802 collapse ( constant-expression ) */
31805 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
31811 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31812 matching_parens parens
;
31813 if (!parens
.require_open (parser
))
31816 num
= cp_parser_constant_expression (parser
);
31818 if (!parens
.require_close (parser
))
31819 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31820 /*or_comma=*/false,
31821 /*consume_paren=*/true);
31823 if (num
== error_mark_node
)
31825 num
= fold_non_dependent_expr (num
);
31826 if (!tree_fits_shwi_p (num
)
31827 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31828 || (n
= tree_to_shwi (num
)) <= 0
31831 error_at (loc
, "collapse argument needs positive constant integer expression");
31835 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
31836 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
31837 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
31838 OMP_CLAUSE_CHAIN (c
) = list
;
31839 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
31845 default ( none | shared )
31848 default ( none | present ) */
31851 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
31852 location_t location
, bool is_oacc
)
31854 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
31857 matching_parens parens
;
31858 if (!parens
.require_open (parser
))
31860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31862 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31863 const char *p
= IDENTIFIER_POINTER (id
);
31868 if (strcmp ("none", p
) != 0)
31870 kind
= OMP_CLAUSE_DEFAULT_NONE
;
31874 if (strcmp ("present", p
) != 0 || !is_oacc
)
31876 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
31880 if (strcmp ("shared", p
) != 0 || is_oacc
)
31882 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
31889 cp_lexer_consume_token (parser
->lexer
);
31895 cp_parser_error (parser
, "expected %<none%> or %<present%>");
31897 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
31900 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
31901 || !parens
.require_close (parser
))
31902 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31903 /*or_comma=*/false,
31904 /*consume_paren=*/true);
31906 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
31909 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
31910 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
31911 OMP_CLAUSE_CHAIN (c
) = list
;
31912 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
31918 final ( expression ) */
31921 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
31925 matching_parens parens
;
31926 if (!parens
.require_open (parser
))
31929 t
= cp_parser_condition (parser
);
31931 if (t
== error_mark_node
31932 || !parens
.require_close (parser
))
31933 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31934 /*or_comma=*/false,
31935 /*consume_paren=*/true);
31937 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
31939 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
31940 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
31941 OMP_CLAUSE_CHAIN (c
) = list
;
31950 if ( directive-name-modifier : expression )
31952 directive-name-modifier:
31953 parallel | task | taskloop | target data | target | target update
31954 | target enter data | target exit data */
31957 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
31961 enum tree_code if_modifier
= ERROR_MARK
;
31963 matching_parens parens
;
31964 if (!parens
.require_open (parser
))
31967 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31969 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31970 const char *p
= IDENTIFIER_POINTER (id
);
31973 if (strcmp ("parallel", p
) == 0)
31974 if_modifier
= OMP_PARALLEL
;
31975 else if (strcmp ("task", p
) == 0)
31976 if_modifier
= OMP_TASK
;
31977 else if (strcmp ("taskloop", p
) == 0)
31978 if_modifier
= OMP_TASKLOOP
;
31979 else if (strcmp ("target", p
) == 0)
31981 if_modifier
= OMP_TARGET
;
31982 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
31984 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
31985 p
= IDENTIFIER_POINTER (id
);
31986 if (strcmp ("data", p
) == 0)
31987 if_modifier
= OMP_TARGET_DATA
;
31988 else if (strcmp ("update", p
) == 0)
31989 if_modifier
= OMP_TARGET_UPDATE
;
31990 else if (strcmp ("enter", p
) == 0)
31991 if_modifier
= OMP_TARGET_ENTER_DATA
;
31992 else if (strcmp ("exit", p
) == 0)
31993 if_modifier
= OMP_TARGET_EXIT_DATA
;
31994 if (if_modifier
!= OMP_TARGET
)
31999 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32000 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32002 if_modifier
= ERROR_MARK
;
32004 if (if_modifier
== OMP_TARGET_ENTER_DATA
32005 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32007 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32009 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32010 p
= IDENTIFIER_POINTER (id
);
32011 if (strcmp ("data", p
) == 0)
32017 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32018 error_at (loc
, "expected %<data%>");
32019 if_modifier
= ERROR_MARK
;
32024 if (if_modifier
!= ERROR_MARK
)
32026 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32029 cp_lexer_consume_token (parser
->lexer
);
32036 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32037 error_at (loc
, "expected %<:%>");
32039 if_modifier
= ERROR_MARK
;
32044 t
= cp_parser_condition (parser
);
32046 if (t
== error_mark_node
32047 || !parens
.require_close (parser
))
32048 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32049 /*or_comma=*/false,
32050 /*consume_paren=*/true);
32052 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32053 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32055 if (if_modifier
!= ERROR_MARK
32056 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32058 const char *p
= NULL
;
32059 switch (if_modifier
)
32061 case OMP_PARALLEL
: p
= "parallel"; break;
32062 case OMP_TASK
: p
= "task"; break;
32063 case OMP_TASKLOOP
: p
= "taskloop"; break;
32064 case OMP_TARGET_DATA
: p
= "target data"; break;
32065 case OMP_TARGET
: p
= "target"; break;
32066 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32067 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32068 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32069 default: gcc_unreachable ();
32071 error_at (location
, "too many %<if%> clauses with %qs modifier",
32075 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32078 error_at (location
, "too many %<if%> clauses");
32080 error_at (location
, "too many %<if%> clauses without modifier");
32083 else if (if_modifier
== ERROR_MARK
32084 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32086 error_at (location
, "if any %<if%> clause has modifier, then all "
32087 "%<if%> clauses have to use modifier");
32092 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32093 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32094 OMP_CLAUSE_IF_EXPR (c
) = t
;
32095 OMP_CLAUSE_CHAIN (c
) = list
;
32104 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32105 tree list
, location_t location
)
32109 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32112 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32113 OMP_CLAUSE_CHAIN (c
) = list
;
32121 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32122 tree list
, location_t location
)
32126 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32128 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32129 OMP_CLAUSE_CHAIN (c
) = list
;
32134 num_threads ( expression ) */
32137 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32138 location_t location
)
32142 matching_parens parens
;
32143 if (!parens
.require_open (parser
))
32146 t
= cp_parser_expression (parser
);
32148 if (t
== error_mark_node
32149 || !parens
.require_close (parser
))
32150 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32151 /*or_comma=*/false,
32152 /*consume_paren=*/true);
32154 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32155 "num_threads", location
);
32157 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32158 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32159 OMP_CLAUSE_CHAIN (c
) = list
;
32165 num_tasks ( expression ) */
32168 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32169 location_t location
)
32173 matching_parens parens
;
32174 if (!parens
.require_open (parser
))
32177 t
= cp_parser_expression (parser
);
32179 if (t
== error_mark_node
32180 || !parens
.require_close (parser
))
32181 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32182 /*or_comma=*/false,
32183 /*consume_paren=*/true);
32185 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32186 "num_tasks", location
);
32188 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32189 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32190 OMP_CLAUSE_CHAIN (c
) = list
;
32196 grainsize ( expression ) */
32199 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32200 location_t location
)
32204 matching_parens parens
;
32205 if (!parens
.require_open (parser
))
32208 t
= cp_parser_expression (parser
);
32210 if (t
== error_mark_node
32211 || !parens
.require_close (parser
))
32212 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32213 /*or_comma=*/false,
32214 /*consume_paren=*/true);
32216 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32217 "grainsize", location
);
32219 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32220 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32221 OMP_CLAUSE_CHAIN (c
) = list
;
32227 priority ( expression ) */
32230 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32231 location_t location
)
32235 matching_parens parens
;
32236 if (!parens
.require_open (parser
))
32239 t
= cp_parser_expression (parser
);
32241 if (t
== error_mark_node
32242 || !parens
.require_close (parser
))
32243 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32244 /*or_comma=*/false,
32245 /*consume_paren=*/true);
32247 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32248 "priority", location
);
32250 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32251 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32252 OMP_CLAUSE_CHAIN (c
) = list
;
32258 hint ( expression ) */
32261 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32262 location_t location
)
32266 matching_parens parens
;
32267 if (!parens
.require_open (parser
))
32270 t
= cp_parser_expression (parser
);
32272 if (t
== error_mark_node
32273 || !parens
.require_close (parser
))
32274 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32275 /*or_comma=*/false,
32276 /*consume_paren=*/true);
32278 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32280 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32281 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32282 OMP_CLAUSE_CHAIN (c
) = list
;
32288 defaultmap ( tofrom : scalar ) */
32291 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32292 location_t location
)
32297 matching_parens parens
;
32298 if (!parens
.require_open (parser
))
32301 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32303 cp_parser_error (parser
, "expected %<tofrom%>");
32306 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32307 p
= IDENTIFIER_POINTER (id
);
32308 if (strcmp (p
, "tofrom") != 0)
32310 cp_parser_error (parser
, "expected %<tofrom%>");
32313 cp_lexer_consume_token (parser
->lexer
);
32314 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32317 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32319 cp_parser_error (parser
, "expected %<scalar%>");
32322 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32323 p
= IDENTIFIER_POINTER (id
);
32324 if (strcmp (p
, "scalar") != 0)
32326 cp_parser_error (parser
, "expected %<scalar%>");
32329 cp_lexer_consume_token (parser
->lexer
);
32330 if (!parens
.require_close (parser
))
32333 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32336 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32337 OMP_CLAUSE_CHAIN (c
) = list
;
32341 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32342 /*or_comma=*/false,
32343 /*consume_paren=*/true);
32351 ordered ( constant-expression ) */
32354 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32355 tree list
, location_t location
)
32357 tree c
, num
= NULL_TREE
;
32360 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32361 "ordered", location
);
32363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32365 matching_parens parens
;
32366 parens
.consume_open (parser
);
32368 num
= cp_parser_constant_expression (parser
);
32370 if (!parens
.require_close (parser
))
32371 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 if (num
== error_mark_node
)
32377 num
= fold_non_dependent_expr (num
);
32378 if (!tree_fits_shwi_p (num
)
32379 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32380 || (n
= tree_to_shwi (num
)) <= 0
32383 error_at (location
,
32384 "ordered argument needs positive constant integer "
32390 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32391 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32392 OMP_CLAUSE_CHAIN (c
) = list
;
32397 reduction ( reduction-operator : variable-list )
32399 reduction-operator:
32400 One of: + * - & ^ | && ||
32404 reduction-operator:
32405 One of: + * - & ^ | && || min max
32409 reduction-operator:
32410 One of: + * - & ^ | && ||
32414 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32416 enum tree_code code
= ERROR_MARK
;
32417 tree nlist
, c
, id
= NULL_TREE
;
32419 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32422 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32424 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32425 case CPP_MULT
: code
= MULT_EXPR
; break;
32426 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32427 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32428 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32429 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32430 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32431 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32435 if (code
!= ERROR_MARK
)
32436 cp_lexer_consume_token (parser
->lexer
);
32439 bool saved_colon_corrects_to_scope_p
;
32440 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32441 parser
->colon_corrects_to_scope_p
= false;
32442 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32443 /*check_dependency_p=*/true,
32444 /*template_p=*/NULL
,
32445 /*declarator_p=*/false,
32446 /*optional_p=*/false);
32447 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32448 if (identifier_p (id
))
32450 const char *p
= IDENTIFIER_POINTER (id
);
32452 if (strcmp (p
, "min") == 0)
32454 else if (strcmp (p
, "max") == 0)
32456 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32458 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32460 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32462 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32463 code
= BIT_AND_EXPR
;
32464 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32465 code
= BIT_IOR_EXPR
;
32466 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32467 code
= BIT_XOR_EXPR
;
32468 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32469 code
= TRUTH_ANDIF_EXPR
;
32470 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32471 code
= TRUTH_ORIF_EXPR
;
32472 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32473 tree scope
= parser
->scope
;
32475 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32476 parser
->scope
= NULL_TREE
;
32477 parser
->qualifying_scope
= NULL_TREE
;
32478 parser
->object_scope
= NULL_TREE
;
32482 error ("invalid reduction-identifier");
32484 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32485 /*or_comma=*/false,
32486 /*consume_paren=*/true);
32491 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32494 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32496 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32498 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32499 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32506 schedule ( schedule-kind )
32507 schedule ( schedule-kind , expression )
32510 static | dynamic | guided | runtime | auto
32513 schedule ( schedule-modifier : schedule-kind )
32514 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32522 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32525 int modifiers
= 0, nmodifiers
= 0;
32527 matching_parens parens
;
32528 if (!parens
.require_open (parser
))
32531 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32533 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32535 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32536 const char *p
= IDENTIFIER_POINTER (id
);
32537 if (strcmp ("simd", p
) == 0)
32538 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32539 else if (strcmp ("monotonic", p
) == 0)
32540 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32541 else if (strcmp ("nonmonotonic", p
) == 0)
32542 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32545 cp_lexer_consume_token (parser
->lexer
);
32546 if (nmodifiers
++ == 0
32547 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32548 cp_lexer_consume_token (parser
->lexer
);
32551 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32558 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32559 const char *p
= IDENTIFIER_POINTER (id
);
32564 if (strcmp ("dynamic", p
) != 0)
32566 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32570 if (strcmp ("guided", p
) != 0)
32572 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32576 if (strcmp ("runtime", p
) != 0)
32578 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32585 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32586 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32587 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32588 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32591 cp_lexer_consume_token (parser
->lexer
);
32593 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32594 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32595 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32596 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32598 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32606 cp_lexer_consume_token (parser
->lexer
);
32608 token
= cp_lexer_peek_token (parser
->lexer
);
32609 t
= cp_parser_assignment_expression (parser
);
32611 if (t
== error_mark_node
)
32613 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32614 error_at (token
->location
, "schedule %<runtime%> does not take "
32615 "a %<chunk_size%> parameter");
32616 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32617 error_at (token
->location
, "schedule %<auto%> does not take "
32618 "a %<chunk_size%> parameter");
32620 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32622 if (!parens
.require_close (parser
))
32625 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32628 OMP_CLAUSE_SCHEDULE_KIND (c
)
32629 = (enum omp_clause_schedule_kind
)
32630 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32632 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32633 OMP_CLAUSE_CHAIN (c
) = list
;
32637 cp_parser_error (parser
, "invalid schedule kind");
32639 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32640 /*or_comma=*/false,
32641 /*consume_paren=*/true);
32649 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32650 tree list
, location_t location
)
32654 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32656 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32657 OMP_CLAUSE_CHAIN (c
) = list
;
32666 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32667 tree list
, location_t location
)
32669 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32670 tree c
= build_omp_clause (location
, code
);
32671 OMP_CLAUSE_CHAIN (c
) = list
;
32682 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32683 enum omp_clause_code code
,
32684 tree list
, location_t location
)
32686 tree c
= build_omp_clause (location
, code
);
32687 OMP_CLAUSE_CHAIN (c
) = list
;
32695 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32696 tree list
, location_t location
)
32698 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32699 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32700 OMP_CLAUSE_CHAIN (c
) = list
;
32709 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32710 enum omp_clause_code code
,
32711 tree list
, location_t location
)
32713 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32714 tree c
= build_omp_clause (location
, code
);
32715 OMP_CLAUSE_CHAIN (c
) = list
;
32720 num_teams ( expression ) */
32723 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32724 location_t location
)
32728 matching_parens parens
;
32729 if (!parens
.require_open (parser
))
32732 t
= cp_parser_expression (parser
);
32734 if (t
== error_mark_node
32735 || !parens
.require_close (parser
))
32736 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32737 /*or_comma=*/false,
32738 /*consume_paren=*/true);
32740 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32741 "num_teams", location
);
32743 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32744 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32745 OMP_CLAUSE_CHAIN (c
) = list
;
32751 thread_limit ( expression ) */
32754 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32755 location_t location
)
32759 matching_parens parens
;
32760 if (!parens
.require_open (parser
))
32763 t
= cp_parser_expression (parser
);
32765 if (t
== error_mark_node
32766 || !parens
.require_close (parser
))
32767 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32768 /*or_comma=*/false,
32769 /*consume_paren=*/true);
32771 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32772 "thread_limit", location
);
32774 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32775 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32776 OMP_CLAUSE_CHAIN (c
) = list
;
32782 aligned ( variable-list )
32783 aligned ( variable-list : constant-expression ) */
32786 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
32788 tree nlist
, c
, alignment
= NULL_TREE
;
32791 matching_parens parens
;
32792 if (!parens
.require_open (parser
))
32795 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
32800 alignment
= cp_parser_constant_expression (parser
);
32802 if (!parens
.require_close (parser
))
32803 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32804 /*or_comma=*/false,
32805 /*consume_paren=*/true);
32807 if (alignment
== error_mark_node
)
32808 alignment
= NULL_TREE
;
32811 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32812 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
32818 linear ( variable-list )
32819 linear ( variable-list : expression )
32822 linear ( modifier ( variable-list ) )
32823 linear ( modifier ( variable-list ) : expression ) */
32826 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
32829 tree nlist
, c
, step
= integer_one_node
;
32831 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32833 matching_parens parens
;
32834 if (!parens
.require_open (parser
))
32837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32839 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32840 const char *p
= IDENTIFIER_POINTER (id
);
32842 if (strcmp ("ref", p
) == 0)
32843 kind
= OMP_CLAUSE_LINEAR_REF
;
32844 else if (strcmp ("val", p
) == 0)
32845 kind
= OMP_CLAUSE_LINEAR_VAL
;
32846 else if (strcmp ("uval", p
) == 0)
32847 kind
= OMP_CLAUSE_LINEAR_UVAL
;
32848 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
32849 cp_lexer_consume_token (parser
->lexer
);
32851 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
32854 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
32855 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
32859 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
32860 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
32862 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32863 else if (!parens
.require_close (parser
))
32864 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32865 /*or_comma=*/false,
32866 /*consume_paren=*/true);
32873 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
32874 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
32876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32877 cp_parser_parse_tentatively (parser
);
32878 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
32879 /*check_dependency_p=*/true,
32880 /*template_p=*/NULL
,
32881 /*declarator_p=*/false,
32882 /*optional_p=*/false);
32883 if (step
!= error_mark_node
)
32884 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
32885 if (step
== error_mark_node
)
32888 cp_parser_abort_tentative_parse (parser
);
32890 else if (!cp_parser_parse_definitely (parser
))
32894 step
= cp_parser_expression (parser
);
32896 if (!parens
.require_close (parser
))
32897 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32898 /*or_comma=*/false,
32899 /*consume_paren=*/true);
32901 if (step
== error_mark_node
)
32905 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32907 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
32908 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
32915 safelen ( constant-expression ) */
32918 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
32919 location_t location
)
32923 matching_parens parens
;
32924 if (!parens
.require_open (parser
))
32927 t
= cp_parser_constant_expression (parser
);
32929 if (t
== error_mark_node
32930 || !parens
.require_close (parser
))
32931 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32932 /*or_comma=*/false,
32933 /*consume_paren=*/true);
32935 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
32937 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
32938 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
32939 OMP_CLAUSE_CHAIN (c
) = list
;
32945 simdlen ( constant-expression ) */
32948 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
32949 location_t location
)
32953 matching_parens parens
;
32954 if (!parens
.require_open (parser
))
32957 t
= cp_parser_constant_expression (parser
);
32959 if (t
== error_mark_node
32960 || !parens
.require_close (parser
))
32961 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32962 /*or_comma=*/false,
32963 /*consume_paren=*/true);
32965 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
32967 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
32968 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
32969 OMP_CLAUSE_CHAIN (c
) = list
;
32976 identifier [+/- integer]
32977 vec , identifier [+/- integer]
32981 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
32986 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32988 cp_parser_error (parser
, "expected identifier");
32992 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32994 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32995 tree t
, identifier
= cp_parser_identifier (parser
);
32996 tree addend
= NULL
;
32998 if (identifier
== error_mark_node
)
32999 t
= error_mark_node
;
33002 t
= cp_parser_lookup_name_simple
33003 (parser
, identifier
,
33004 cp_lexer_peek_token (parser
->lexer
)->location
);
33005 if (t
== error_mark_node
)
33006 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33013 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33015 addend
= integer_zero_node
;
33016 goto add_to_vector
;
33018 cp_lexer_consume_token (parser
->lexer
);
33020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33022 cp_parser_error (parser
, "expected integer");
33026 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33027 if (TREE_CODE (addend
) != INTEGER_CST
)
33029 cp_parser_error (parser
, "expected integer");
33032 cp_lexer_consume_token (parser
->lexer
);
33035 if (t
!= error_mark_node
)
33037 vec
= tree_cons (addend
, t
, vec
);
33039 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33042 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33045 cp_lexer_consume_token (parser
->lexer
);
33048 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33050 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33051 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33052 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33053 OMP_CLAUSE_CHAIN (u
) = list
;
33060 depend ( depend-kind : variable-list )
33068 depend ( sink : vec ) */
33071 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33074 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33076 matching_parens parens
;
33077 if (!parens
.require_open (parser
))
33080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33082 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33083 const char *p
= IDENTIFIER_POINTER (id
);
33085 if (strcmp ("in", p
) == 0)
33086 kind
= OMP_CLAUSE_DEPEND_IN
;
33087 else if (strcmp ("inout", p
) == 0)
33088 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33089 else if (strcmp ("out", p
) == 0)
33090 kind
= OMP_CLAUSE_DEPEND_OUT
;
33091 else if (strcmp ("source", p
) == 0)
33092 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33093 else if (strcmp ("sink", p
) == 0)
33094 kind
= OMP_CLAUSE_DEPEND_SINK
;
33101 cp_lexer_consume_token (parser
->lexer
);
33103 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33105 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33106 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33107 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33108 OMP_CLAUSE_CHAIN (c
) = list
;
33109 if (!parens
.require_close (parser
))
33110 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33111 /*or_comma=*/false,
33112 /*consume_paren=*/true);
33116 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33119 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33120 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33123 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33126 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33127 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33132 cp_parser_error (parser
, "invalid depend kind");
33134 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33135 /*or_comma=*/false,
33136 /*consume_paren=*/true);
33141 map ( map-kind : variable-list )
33142 map ( variable-list )
33145 alloc | to | from | tofrom
33149 alloc | to | from | tofrom | release | delete
33151 map ( always [,] map-kind: variable-list ) */
33154 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33157 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33158 bool always
= false;
33160 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33165 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33166 const char *p
= IDENTIFIER_POINTER (id
);
33168 if (strcmp ("always", p
) == 0)
33171 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33173 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33174 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33176 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33180 cp_lexer_consume_token (parser
->lexer
);
33182 cp_lexer_consume_token (parser
->lexer
);
33187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33188 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33190 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33191 const char *p
= IDENTIFIER_POINTER (id
);
33193 if (strcmp ("alloc", p
) == 0)
33194 kind
= GOMP_MAP_ALLOC
;
33195 else if (strcmp ("to", p
) == 0)
33196 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33197 else if (strcmp ("from", p
) == 0)
33198 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33199 else if (strcmp ("tofrom", p
) == 0)
33200 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33201 else if (strcmp ("release", p
) == 0)
33202 kind
= GOMP_MAP_RELEASE
;
33205 cp_parser_error (parser
, "invalid map kind");
33206 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33207 /*or_comma=*/false,
33208 /*consume_paren=*/true);
33211 cp_lexer_consume_token (parser
->lexer
);
33212 cp_lexer_consume_token (parser
->lexer
);
33214 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33215 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33217 kind
= GOMP_MAP_DELETE
;
33218 cp_lexer_consume_token (parser
->lexer
);
33219 cp_lexer_consume_token (parser
->lexer
);
33222 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33225 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33226 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33232 device ( expression ) */
33235 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33236 location_t location
)
33240 matching_parens parens
;
33241 if (!parens
.require_open (parser
))
33244 t
= cp_parser_expression (parser
);
33246 if (t
== error_mark_node
33247 || !parens
.require_close (parser
))
33248 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33249 /*or_comma=*/false,
33250 /*consume_paren=*/true);
33252 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33253 "device", location
);
33255 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33256 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33257 OMP_CLAUSE_CHAIN (c
) = list
;
33263 dist_schedule ( static )
33264 dist_schedule ( static , expression ) */
33267 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33268 location_t location
)
33272 matching_parens parens
;
33273 if (!parens
.require_open (parser
))
33276 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33278 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33280 cp_lexer_consume_token (parser
->lexer
);
33282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33284 cp_lexer_consume_token (parser
->lexer
);
33286 t
= cp_parser_assignment_expression (parser
);
33288 if (t
== error_mark_node
)
33290 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33292 if (!parens
.require_close (parser
))
33295 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33298 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33300 OMP_CLAUSE_CHAIN (c
) = list
;
33304 cp_parser_error (parser
, "invalid dist_schedule kind");
33306 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33307 /*or_comma=*/false,
33308 /*consume_paren=*/true);
33313 proc_bind ( proc-bind-kind )
33316 master | close | spread */
33319 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33320 location_t location
)
33323 enum omp_clause_proc_bind_kind kind
;
33325 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33330 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33331 const char *p
= IDENTIFIER_POINTER (id
);
33333 if (strcmp ("master", p
) == 0)
33334 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33335 else if (strcmp ("close", p
) == 0)
33336 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33337 else if (strcmp ("spread", p
) == 0)
33338 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33345 cp_lexer_consume_token (parser
->lexer
);
33346 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33349 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33350 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33352 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33353 OMP_CLAUSE_CHAIN (c
) = list
;
33357 cp_parser_error (parser
, "invalid depend kind");
33359 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33360 /*or_comma=*/false,
33361 /*consume_paren=*/true);
33366 async [( int-expr )] */
33369 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33372 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33374 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33376 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33378 matching_parens parens
;
33379 parens
.consume_open (parser
);
33381 t
= cp_parser_expression (parser
);
33382 if (t
== error_mark_node
33383 || !parens
.require_close (parser
))
33384 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33385 /*or_comma=*/false,
33386 /*consume_paren=*/true);
33389 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33391 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33392 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33393 OMP_CLAUSE_CHAIN (c
) = list
;
33399 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33400 is a bitmask in MASK. Return the list of clauses found. */
33403 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33404 const char *where
, cp_token
*pragma_tok
,
33405 bool finish_p
= true)
33407 tree clauses
= NULL
;
33410 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33413 pragma_omp_clause c_kind
;
33414 omp_clause_code code
;
33415 const char *c_name
;
33416 tree prev
= clauses
;
33418 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33419 cp_lexer_consume_token (parser
->lexer
);
33421 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33422 c_kind
= cp_parser_omp_clause_name (parser
);
33426 case PRAGMA_OACC_CLAUSE_ASYNC
:
33427 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33430 case PRAGMA_OACC_CLAUSE_AUTO
:
33431 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33435 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33436 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33437 c_name
= "collapse";
33439 case PRAGMA_OACC_CLAUSE_COPY
:
33440 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33443 case PRAGMA_OACC_CLAUSE_COPYIN
:
33444 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33447 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33448 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33449 c_name
= "copyout";
33451 case PRAGMA_OACC_CLAUSE_CREATE
:
33452 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33455 case PRAGMA_OACC_CLAUSE_DELETE
:
33456 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33459 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33460 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33461 c_name
= "default";
33463 case PRAGMA_OACC_CLAUSE_DEVICE
:
33464 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33467 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33468 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33469 c_name
= "deviceptr";
33471 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33472 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33473 c_name
= "device_resident";
33475 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33476 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33478 c_name
= "firstprivate";
33480 case PRAGMA_OACC_CLAUSE_GANG
:
33482 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33485 case PRAGMA_OACC_CLAUSE_HOST
:
33486 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33489 case PRAGMA_OACC_CLAUSE_IF
:
33490 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33493 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33494 clauses
= cp_parser_oacc_simple_clause (parser
,
33495 OMP_CLAUSE_INDEPENDENT
,
33497 c_name
= "independent";
33499 case PRAGMA_OACC_CLAUSE_LINK
:
33500 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33503 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33504 code
= OMP_CLAUSE_NUM_GANGS
;
33505 c_name
= "num_gangs";
33506 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33509 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33510 c_name
= "num_workers";
33511 code
= OMP_CLAUSE_NUM_WORKERS
;
33512 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33515 case PRAGMA_OACC_CLAUSE_PRESENT
:
33516 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33517 c_name
= "present";
33519 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33520 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33521 c_name
= "present_or_copy";
33523 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33524 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33525 c_name
= "present_or_copyin";
33527 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33528 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33529 c_name
= "present_or_copyout";
33531 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33532 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33533 c_name
= "present_or_create";
33535 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33536 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33538 c_name
= "private";
33540 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33541 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33542 c_name
= "reduction";
33544 case PRAGMA_OACC_CLAUSE_SELF
:
33545 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33548 case PRAGMA_OACC_CLAUSE_SEQ
:
33549 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33553 case PRAGMA_OACC_CLAUSE_TILE
:
33554 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33557 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33558 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33560 c_name
= "use_device";
33562 case PRAGMA_OACC_CLAUSE_VECTOR
:
33564 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33567 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33568 c_name
= "vector_length";
33569 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33570 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33573 case PRAGMA_OACC_CLAUSE_WAIT
:
33574 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33577 case PRAGMA_OACC_CLAUSE_WORKER
:
33579 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33583 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33589 if (((mask
>> c_kind
) & 1) == 0)
33591 /* Remove the invalid clause(s) from the list to avoid
33592 confusing the rest of the compiler. */
33594 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33599 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33602 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33607 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33608 is a bitmask in MASK. Return the list of clauses found; the result
33609 of clause default goes in *pdefault. */
33612 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33613 const char *where
, cp_token
*pragma_tok
,
33614 bool finish_p
= true)
33616 tree clauses
= NULL
;
33618 cp_token
*token
= NULL
;
33620 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33622 pragma_omp_clause c_kind
;
33623 const char *c_name
;
33624 tree prev
= clauses
;
33626 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33627 cp_lexer_consume_token (parser
->lexer
);
33629 token
= cp_lexer_peek_token (parser
->lexer
);
33630 c_kind
= cp_parser_omp_clause_name (parser
);
33634 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33635 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33637 c_name
= "collapse";
33639 case PRAGMA_OMP_CLAUSE_COPYIN
:
33640 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33643 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33644 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33646 c_name
= "copyprivate";
33648 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33649 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33650 token
->location
, false);
33651 c_name
= "default";
33653 case PRAGMA_OMP_CLAUSE_FINAL
:
33654 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33657 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33658 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33660 c_name
= "firstprivate";
33662 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33663 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33665 c_name
= "grainsize";
33667 case PRAGMA_OMP_CLAUSE_HINT
:
33668 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33672 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33673 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33675 c_name
= "defaultmap";
33677 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33678 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33680 c_name
= "use_device_ptr";
33682 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33683 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33685 c_name
= "is_device_ptr";
33687 case PRAGMA_OMP_CLAUSE_IF
:
33688 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33692 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33693 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33695 c_name
= "lastprivate";
33697 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33698 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33700 c_name
= "mergeable";
33702 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33703 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33706 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33707 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33709 c_name
= "num_tasks";
33711 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33712 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33714 c_name
= "num_threads";
33716 case PRAGMA_OMP_CLAUSE_ORDERED
:
33717 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33719 c_name
= "ordered";
33721 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33722 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33724 c_name
= "priority";
33726 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33727 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33729 c_name
= "private";
33731 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33732 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33733 c_name
= "reduction";
33735 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33736 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33738 c_name
= "schedule";
33740 case PRAGMA_OMP_CLAUSE_SHARED
:
33741 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33745 case PRAGMA_OMP_CLAUSE_UNTIED
:
33746 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33750 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33751 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33752 clauses
, token
->location
);
33753 c_name
= "inbranch";
33755 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33756 clauses
= cp_parser_omp_clause_branch (parser
,
33757 OMP_CLAUSE_NOTINBRANCH
,
33758 clauses
, token
->location
);
33759 c_name
= "notinbranch";
33761 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33762 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33763 clauses
, token
->location
);
33764 c_name
= "parallel";
33768 error_at (token
->location
, "%qs must be the first clause of %qs",
33773 case PRAGMA_OMP_CLAUSE_FOR
:
33774 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33775 clauses
, token
->location
);
33778 goto clause_not_first
;
33780 case PRAGMA_OMP_CLAUSE_SECTIONS
:
33781 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
33782 clauses
, token
->location
);
33783 c_name
= "sections";
33785 goto clause_not_first
;
33787 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
33788 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
33789 clauses
, token
->location
);
33790 c_name
= "taskgroup";
33792 goto clause_not_first
;
33794 case PRAGMA_OMP_CLAUSE_LINK
:
33795 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
33798 case PRAGMA_OMP_CLAUSE_TO
:
33799 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
33800 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
33803 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
33806 case PRAGMA_OMP_CLAUSE_FROM
:
33807 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
33810 case PRAGMA_OMP_CLAUSE_UNIFORM
:
33811 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
33813 c_name
= "uniform";
33815 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
33816 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
33818 c_name
= "num_teams";
33820 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
33821 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
33823 c_name
= "thread_limit";
33825 case PRAGMA_OMP_CLAUSE_ALIGNED
:
33826 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
33827 c_name
= "aligned";
33829 case PRAGMA_OMP_CLAUSE_LINEAR
:
33831 bool declare_simd
= false;
33832 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
33833 declare_simd
= true;
33834 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
33838 case PRAGMA_OMP_CLAUSE_DEPEND
:
33839 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
33843 case PRAGMA_OMP_CLAUSE_MAP
:
33844 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
33847 case PRAGMA_OMP_CLAUSE_DEVICE
:
33848 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
33852 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
33853 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
33855 c_name
= "dist_schedule";
33857 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
33858 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
33860 c_name
= "proc_bind";
33862 case PRAGMA_OMP_CLAUSE_SAFELEN
:
33863 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
33865 c_name
= "safelen";
33867 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
33868 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
33870 c_name
= "simdlen";
33872 case PRAGMA_OMP_CLAUSE_NOGROUP
:
33873 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
33875 c_name
= "nogroup";
33877 case PRAGMA_OMP_CLAUSE_THREADS
:
33879 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
33880 clauses
, token
->location
);
33881 c_name
= "threads";
33883 case PRAGMA_OMP_CLAUSE_SIMD
:
33885 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
33886 clauses
, token
->location
);
33890 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
33896 if (((mask
>> c_kind
) & 1) == 0)
33898 /* Remove the invalid clause(s) from the list to avoid
33899 confusing the rest of the compiler. */
33901 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
33905 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33908 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
33909 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
33911 return finish_omp_clauses (clauses
, C_ORT_OMP
);
33920 In practice, we're also interested in adding the statement to an
33921 outer node. So it is convenient if we work around the fact that
33922 cp_parser_statement calls add_stmt. */
33925 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
33927 unsigned save
= parser
->in_statement
;
33929 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33930 This preserves the "not within loop or switch" style error messages
33931 for nonsense cases like
33937 if (parser
->in_statement
)
33938 parser
->in_statement
= IN_OMP_BLOCK
;
33944 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
33946 parser
->in_statement
= save
;
33950 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
33952 tree stmt
= begin_omp_structured_block ();
33953 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
33955 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33957 cp_parser_end_omp_structured_block (parser
, save
);
33958 return finish_omp_structured_block (stmt
);
33962 # pragma omp atomic new-line
33966 x binop= expr | x++ | ++x | x-- | --x
33968 +, *, -, /, &, ^, |, <<, >>
33970 where x is an lvalue expression with scalar type.
33973 # pragma omp atomic new-line
33976 # pragma omp atomic read new-line
33979 # pragma omp atomic write new-line
33982 # pragma omp atomic update new-line
33985 # pragma omp atomic capture new-line
33988 # pragma omp atomic capture new-line
33996 expression-stmt | x = x binop expr
33998 v = expression-stmt
34000 { v = x; update-stmt; } | { update-stmt; v = x; }
34004 expression-stmt | x = x binop expr | x = expr binop x
34008 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34010 where x and v are lvalue expressions with scalar type. */
34013 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34015 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34016 tree rhs1
= NULL_TREE
, orig_lhs
;
34017 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34018 bool structured_block
= false;
34019 bool seq_cst
= false;
34021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34024 const char *p
= IDENTIFIER_POINTER (id
);
34026 if (!strcmp (p
, "seq_cst"))
34029 cp_lexer_consume_token (parser
->lexer
);
34030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34031 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34032 cp_lexer_consume_token (parser
->lexer
);
34035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34037 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34038 const char *p
= IDENTIFIER_POINTER (id
);
34040 if (!strcmp (p
, "read"))
34041 code
= OMP_ATOMIC_READ
;
34042 else if (!strcmp (p
, "write"))
34044 else if (!strcmp (p
, "update"))
34046 else if (!strcmp (p
, "capture"))
34047 code
= OMP_ATOMIC_CAPTURE_NEW
;
34051 cp_lexer_consume_token (parser
->lexer
);
34055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34056 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34057 cp_lexer_consume_token (parser
->lexer
);
34059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34061 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34062 const char *p
= IDENTIFIER_POINTER (id
);
34064 if (!strcmp (p
, "seq_cst"))
34067 cp_lexer_consume_token (parser
->lexer
);
34071 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34075 case OMP_ATOMIC_READ
:
34076 case NOP_EXPR
: /* atomic write */
34077 v
= cp_parser_unary_expression (parser
);
34078 if (v
== error_mark_node
)
34080 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34082 if (code
== NOP_EXPR
)
34083 lhs
= cp_parser_expression (parser
);
34085 lhs
= cp_parser_unary_expression (parser
);
34086 if (lhs
== error_mark_node
)
34088 if (code
== NOP_EXPR
)
34090 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34098 case OMP_ATOMIC_CAPTURE_NEW
:
34099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34101 cp_lexer_consume_token (parser
->lexer
);
34102 structured_block
= true;
34106 v
= cp_parser_unary_expression (parser
);
34107 if (v
== error_mark_node
)
34109 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34117 lhs
= cp_parser_unary_expression (parser
);
34119 switch (TREE_CODE (lhs
))
34124 case POSTINCREMENT_EXPR
:
34125 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34126 code
= OMP_ATOMIC_CAPTURE_OLD
;
34128 case PREINCREMENT_EXPR
:
34129 lhs
= TREE_OPERAND (lhs
, 0);
34130 opcode
= PLUS_EXPR
;
34131 rhs
= integer_one_node
;
34134 case POSTDECREMENT_EXPR
:
34135 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34136 code
= OMP_ATOMIC_CAPTURE_OLD
;
34138 case PREDECREMENT_EXPR
:
34139 lhs
= TREE_OPERAND (lhs
, 0);
34140 opcode
= MINUS_EXPR
;
34141 rhs
= integer_one_node
;
34144 case COMPOUND_EXPR
:
34145 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34146 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34147 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34148 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34149 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34150 (TREE_OPERAND (lhs
, 1), 0), 0)))
34152 /* Undo effects of boolean_increment for post {in,de}crement. */
34153 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34156 if (TREE_CODE (lhs
) == MODIFY_EXPR
34157 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34159 /* Undo effects of boolean_increment. */
34160 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34162 /* This is pre or post increment. */
34163 rhs
= TREE_OPERAND (lhs
, 1);
34164 lhs
= TREE_OPERAND (lhs
, 0);
34166 if (code
== OMP_ATOMIC_CAPTURE_NEW
34167 && !structured_block
34168 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34169 code
= OMP_ATOMIC_CAPTURE_OLD
;
34175 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34178 opcode
= MULT_EXPR
;
34181 opcode
= TRUNC_DIV_EXPR
;
34184 opcode
= PLUS_EXPR
;
34187 opcode
= MINUS_EXPR
;
34189 case CPP_LSHIFT_EQ
:
34190 opcode
= LSHIFT_EXPR
;
34192 case CPP_RSHIFT_EQ
:
34193 opcode
= RSHIFT_EXPR
;
34196 opcode
= BIT_AND_EXPR
;
34199 opcode
= BIT_IOR_EXPR
;
34202 opcode
= BIT_XOR_EXPR
;
34205 enum cp_parser_prec oprec
;
34207 cp_lexer_consume_token (parser
->lexer
);
34208 cp_parser_parse_tentatively (parser
);
34209 rhs1
= cp_parser_simple_cast_expression (parser
);
34210 if (rhs1
== error_mark_node
)
34212 cp_parser_abort_tentative_parse (parser
);
34213 cp_parser_simple_cast_expression (parser
);
34216 token
= cp_lexer_peek_token (parser
->lexer
);
34217 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34219 cp_parser_abort_tentative_parse (parser
);
34220 cp_parser_parse_tentatively (parser
);
34221 rhs
= cp_parser_binary_expression (parser
, false, true,
34222 PREC_NOT_OPERATOR
, NULL
);
34223 if (rhs
== error_mark_node
)
34225 cp_parser_abort_tentative_parse (parser
);
34226 cp_parser_binary_expression (parser
, false, true,
34227 PREC_NOT_OPERATOR
, NULL
);
34230 switch (TREE_CODE (rhs
))
34233 case TRUNC_DIV_EXPR
:
34242 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34244 if (cp_parser_parse_definitely (parser
))
34246 opcode
= TREE_CODE (rhs
);
34247 rhs1
= TREE_OPERAND (rhs
, 0);
34248 rhs
= TREE_OPERAND (rhs
, 1);
34258 cp_parser_abort_tentative_parse (parser
);
34259 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34261 rhs
= cp_parser_expression (parser
);
34262 if (rhs
== error_mark_node
)
34268 cp_parser_error (parser
,
34269 "invalid form of %<#pragma omp atomic%>");
34272 if (!cp_parser_parse_definitely (parser
))
34274 switch (token
->type
)
34276 case CPP_SEMICOLON
:
34277 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34279 code
= OMP_ATOMIC_CAPTURE_OLD
;
34284 cp_lexer_consume_token (parser
->lexer
);
34287 else if (structured_block
)
34294 cp_parser_error (parser
,
34295 "invalid form of %<#pragma omp atomic%>");
34298 opcode
= MULT_EXPR
;
34301 opcode
= TRUNC_DIV_EXPR
;
34304 opcode
= PLUS_EXPR
;
34307 opcode
= MINUS_EXPR
;
34310 opcode
= LSHIFT_EXPR
;
34313 opcode
= RSHIFT_EXPR
;
34316 opcode
= BIT_AND_EXPR
;
34319 opcode
= BIT_IOR_EXPR
;
34322 opcode
= BIT_XOR_EXPR
;
34325 cp_parser_error (parser
,
34326 "invalid operator for %<#pragma omp atomic%>");
34329 oprec
= TOKEN_PRECEDENCE (token
);
34330 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34331 if (commutative_tree_code (opcode
))
34332 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34333 cp_lexer_consume_token (parser
->lexer
);
34334 rhs
= cp_parser_binary_expression (parser
, false, false,
34336 if (rhs
== error_mark_node
)
34341 cp_parser_error (parser
,
34342 "invalid operator for %<#pragma omp atomic%>");
34345 cp_lexer_consume_token (parser
->lexer
);
34347 rhs
= cp_parser_expression (parser
);
34348 if (rhs
== error_mark_node
)
34353 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34355 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34357 v
= cp_parser_unary_expression (parser
);
34358 if (v
== error_mark_node
)
34360 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34362 lhs1
= cp_parser_unary_expression (parser
);
34363 if (lhs1
== error_mark_node
)
34366 if (structured_block
)
34368 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34369 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34372 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34373 if (!structured_block
)
34374 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34378 cp_parser_skip_to_end_of_block_or_statement (parser
);
34379 if (structured_block
)
34381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34382 cp_lexer_consume_token (parser
->lexer
);
34383 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34385 cp_parser_skip_to_end_of_block_or_statement (parser
);
34386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34387 cp_lexer_consume_token (parser
->lexer
);
34394 # pragma omp barrier new-line */
34397 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34399 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34400 finish_omp_barrier ();
34404 # pragma omp critical [(name)] new-line
34408 # pragma omp critical [(name) [hint(expression)]] new-line
34409 structured-block */
34411 #define OMP_CRITICAL_CLAUSE_MASK \
34412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34415 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34417 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34421 matching_parens parens
;
34422 parens
.consume_open (parser
);
34424 name
= cp_parser_identifier (parser
);
34426 if (name
== error_mark_node
34427 || !parens
.require_close (parser
))
34428 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34429 /*or_comma=*/false,
34430 /*consume_paren=*/true);
34431 if (name
== error_mark_node
)
34434 clauses
= cp_parser_omp_all_clauses (parser
,
34435 OMP_CRITICAL_CLAUSE_MASK
,
34436 "#pragma omp critical", pragma_tok
);
34439 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34441 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34442 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34446 # pragma omp flush flush-vars[opt] new-line
34449 ( variable-list ) */
34452 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34455 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34456 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34458 finish_omp_flush ();
34461 /* Helper function, to parse omp for increment expression. */
34464 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34466 tree cond
= cp_parser_binary_expression (parser
, false, true,
34467 PREC_NOT_OPERATOR
, NULL
);
34468 if (cond
== error_mark_node
34469 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34471 cp_parser_skip_to_end_of_statement (parser
);
34472 return error_mark_node
;
34475 switch (TREE_CODE (cond
))
34483 /* Fall through: OpenMP disallows NE_EXPR. */
34484 gcc_fallthrough ();
34486 return error_mark_node
;
34489 /* If decl is an iterator, preserve LHS and RHS of the relational
34490 expr until finish_omp_for. */
34492 && (type_dependent_expression_p (decl
)
34493 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34496 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34498 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34499 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34500 /*overload=*/NULL
, tf_warning_or_error
);
34503 /* Helper function, to parse omp for increment expression. */
34506 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34508 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34514 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34516 op
= (token
->type
== CPP_PLUS_PLUS
34517 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34518 cp_lexer_consume_token (parser
->lexer
);
34519 lhs
= cp_parser_simple_cast_expression (parser
);
34521 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34522 return error_mark_node
;
34523 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34526 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34528 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34529 return error_mark_node
;
34531 token
= cp_lexer_peek_token (parser
->lexer
);
34532 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34534 op
= (token
->type
== CPP_PLUS_PLUS
34535 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34536 cp_lexer_consume_token (parser
->lexer
);
34537 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34540 op
= cp_parser_assignment_operator_opt (parser
);
34541 if (op
== ERROR_MARK
)
34542 return error_mark_node
;
34544 if (op
!= NOP_EXPR
)
34546 rhs
= cp_parser_assignment_expression (parser
);
34547 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34548 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34551 lhs
= cp_parser_binary_expression (parser
, false, false,
34552 PREC_ADDITIVE_EXPRESSION
, NULL
);
34553 token
= cp_lexer_peek_token (parser
->lexer
);
34554 decl_first
= (lhs
== decl
34555 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34558 if (token
->type
!= CPP_PLUS
34559 && token
->type
!= CPP_MINUS
)
34560 return error_mark_node
;
34564 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34565 cp_lexer_consume_token (parser
->lexer
);
34566 rhs
= cp_parser_binary_expression (parser
, false, false,
34567 PREC_ADDITIVE_EXPRESSION
, NULL
);
34568 token
= cp_lexer_peek_token (parser
->lexer
);
34569 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34571 if (lhs
== NULL_TREE
)
34573 if (op
== PLUS_EXPR
)
34576 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34577 tf_warning_or_error
);
34580 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34581 ERROR_MARK
, NULL
, tf_warning_or_error
);
34584 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34589 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34590 || op
== MINUS_EXPR
)
34591 return error_mark_node
;
34592 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34595 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34597 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34600 /* Parse the initialization statement of an OpenMP for loop.
34602 Return true if the resulting construct should have an
34603 OMP_CLAUSE_PRIVATE added to it. */
34606 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34607 tree
&this_pre_body
,
34608 vec
<tree
, va_gc
> *for_block
,
34614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34617 tree add_private_clause
= NULL_TREE
;
34619 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34623 integer-type var = lb
34624 random-access-iterator-type var = lb
34625 pointer-type var = lb
34627 cp_decl_specifier_seq type_specifiers
;
34629 /* First, try to parse as an initialized declaration. See
34630 cp_parser_condition, from whence the bulk of this is copied. */
34632 cp_parser_parse_tentatively (parser
);
34633 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34634 /*is_trailing_return=*/false,
34636 if (cp_parser_parse_definitely (parser
))
34638 /* If parsing a type specifier seq succeeded, then this
34639 MUST be a initialized declaration. */
34640 tree asm_specification
, attributes
;
34641 cp_declarator
*declarator
;
34643 declarator
= cp_parser_declarator (parser
,
34644 CP_PARSER_DECLARATOR_NAMED
,
34645 /*ctor_dtor_or_conv_p=*/NULL
,
34646 /*parenthesized_p=*/NULL
,
34647 /*member_p=*/false,
34648 /*friend_p=*/false);
34649 attributes
= cp_parser_attributes_opt (parser
);
34650 asm_specification
= cp_parser_asm_specification_opt (parser
);
34652 if (declarator
== cp_error_declarator
)
34653 cp_parser_skip_to_end_of_statement (parser
);
34657 tree pushed_scope
, auto_node
;
34659 decl
= start_decl (declarator
, &type_specifiers
,
34660 SD_INITIALIZED
, attributes
,
34661 /*prefix_attributes=*/NULL_TREE
,
34664 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34665 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34667 if (cp_lexer_next_token_is (parser
->lexer
,
34669 error ("parenthesized initialization is not allowed in "
34670 "OpenMP %<for%> loop");
34672 /* Trigger an error. */
34673 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34675 init
= error_mark_node
;
34676 cp_parser_skip_to_end_of_statement (parser
);
34678 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34679 || type_dependent_expression_p (decl
)
34682 bool is_direct_init
, is_non_constant_init
;
34684 init
= cp_parser_initializer (parser
,
34686 &is_non_constant_init
);
34691 = do_auto_deduction (TREE_TYPE (decl
), init
,
34694 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34695 && !type_dependent_expression_p (decl
))
34699 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34701 LOOKUP_ONLYCONVERTING
);
34703 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34705 vec_safe_push (for_block
, this_pre_body
);
34710 init
= pop_stmt_list (this_pre_body
);
34711 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34713 tree_stmt_iterator i
= tsi_start (init
);
34714 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34715 while (!tsi_end_p (i
))
34717 tree t
= tsi_stmt (i
);
34718 if (TREE_CODE (t
) == DECL_EXPR
34719 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34722 vec_safe_push (for_block
, t
);
34727 if (tsi_one_before_end_p (i
))
34729 tree t
= tsi_stmt (i
);
34731 free_stmt_list (init
);
34736 this_pre_body
= NULL_TREE
;
34741 cp_lexer_consume_token (parser
->lexer
);
34742 init
= cp_parser_assignment_expression (parser
);
34745 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34746 init
= error_mark_node
;
34748 cp_finish_decl (decl
, NULL_TREE
,
34749 /*init_const_expr_p=*/false,
34751 LOOKUP_ONLYCONVERTING
);
34755 pop_scope (pushed_scope
);
34761 /* If parsing a type specifier sequence failed, then
34762 this MUST be a simple expression. */
34763 cp_parser_parse_tentatively (parser
);
34764 decl
= cp_parser_primary_expression (parser
, false, false,
34766 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34767 if (!cp_parser_error_occurred (parser
)
34769 && (TREE_CODE (decl
) == COMPONENT_REF
34770 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34772 cp_parser_abort_tentative_parse (parser
);
34773 cp_parser_parse_tentatively (parser
);
34774 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34775 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34776 /*check_dependency_p=*/true,
34777 /*template_p=*/NULL
,
34778 /*declarator_p=*/false,
34779 /*optional_p=*/false);
34780 if (name
!= error_mark_node
34781 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
34783 decl
= cp_parser_lookup_name_simple (parser
, name
,
34785 if (TREE_CODE (decl
) == FIELD_DECL
)
34786 add_private_clause
= omp_privatize_field (decl
, false);
34788 cp_parser_abort_tentative_parse (parser
);
34789 cp_parser_parse_tentatively (parser
);
34790 decl
= cp_parser_primary_expression (parser
, false, false,
34793 if (!cp_parser_error_occurred (parser
)
34796 && CLASS_TYPE_P (TREE_TYPE (decl
)))
34800 cp_parser_parse_definitely (parser
);
34801 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34802 rhs
= cp_parser_assignment_expression (parser
);
34804 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
34807 tf_warning_or_error
));
34808 if (!add_private_clause
)
34809 add_private_clause
= decl
;
34814 cp_parser_abort_tentative_parse (parser
);
34815 init
= cp_parser_expression (parser
);
34818 if (TREE_CODE (init
) == MODIFY_EXPR
34819 || TREE_CODE (init
) == MODOP_EXPR
)
34820 real_decl
= TREE_OPERAND (init
, 0);
34824 return add_private_clause
;
34827 /* Parse the restricted form of the for statement allowed by OpenMP. */
34830 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
34831 tree
*cclauses
, bool *if_p
)
34833 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
34834 tree real_decl
, initv
, condv
, incrv
, declv
;
34835 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
34836 location_t loc_first
;
34837 bool collapse_err
= false;
34838 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
34839 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
34840 auto_vec
<tree
, 4> orig_inits
;
34841 bool tiling
= false;
34843 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
34844 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
34845 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
34846 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
34849 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
34851 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
34852 && OMP_CLAUSE_ORDERED_EXPR (cl
))
34855 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
34858 if (ordered
&& ordered
< collapse
)
34860 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
34861 "%<ordered%> clause parameter is less than %<collapse%>");
34862 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
34863 = build_int_cst (NULL_TREE
, collapse
);
34864 ordered
= collapse
;
34868 for (tree
*pc
= &clauses
; *pc
; )
34869 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
34871 error_at (OMP_CLAUSE_LOCATION (*pc
),
34872 "%<linear%> clause may not be specified together "
34873 "with %<ordered%> clause with a parameter");
34874 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34877 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34880 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
34881 count
= ordered
? ordered
: collapse
;
34883 declv
= make_tree_vec (count
);
34884 initv
= make_tree_vec (count
);
34885 condv
= make_tree_vec (count
);
34886 incrv
= make_tree_vec (count
);
34888 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
34890 for (i
= 0; i
< count
; i
++)
34892 int bracecount
= 0;
34893 tree add_private_clause
= NULL_TREE
;
34896 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34899 cp_parser_error (parser
, "for statement expected");
34902 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
34904 matching_parens parens
;
34905 if (!parens
.require_open (parser
))
34908 init
= orig_init
= decl
= real_decl
= NULL
;
34909 this_pre_body
= push_stmt_list ();
34912 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
34913 init
, orig_init
, decl
, real_decl
);
34915 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
34918 this_pre_body
= pop_stmt_list (this_pre_body
);
34922 pre_body
= push_stmt_list ();
34924 add_stmt (this_pre_body
);
34925 pre_body
= pop_stmt_list (pre_body
);
34928 pre_body
= this_pre_body
;
34933 if (cclauses
!= NULL
34934 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
34935 && real_decl
!= NULL_TREE
)
34938 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
34939 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
34940 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34942 error_at (loc
, "iteration variable %qD"
34943 " should not be firstprivate", real_decl
);
34944 *c
= OMP_CLAUSE_CHAIN (*c
);
34946 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
34947 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34949 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34951 *c
= OMP_CLAUSE_CHAIN (*c
);
34952 if (code
== OMP_SIMD
)
34954 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
34955 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
34959 OMP_CLAUSE_CHAIN (l
) = clauses
;
34962 add_private_clause
= NULL_TREE
;
34966 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
34967 && OMP_CLAUSE_DECL (*c
) == real_decl
)
34968 add_private_clause
= NULL_TREE
;
34969 c
= &OMP_CLAUSE_CHAIN (*c
);
34973 if (add_private_clause
)
34976 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34978 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
34979 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
34980 && OMP_CLAUSE_DECL (c
) == decl
)
34982 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
34983 && OMP_CLAUSE_DECL (c
) == decl
)
34984 error_at (loc
, "iteration variable %qD "
34985 "should not be firstprivate",
34987 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
34988 && OMP_CLAUSE_DECL (c
) == decl
)
34989 error_at (loc
, "iteration variable %qD should not be reduction",
34994 if (code
!= OMP_SIMD
)
34995 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
34996 else if (collapse
== 1)
34997 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
34999 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35000 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35001 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35004 OMP_CLAUSE_CHAIN (c
) = clauses
;
35006 /* For linear, signal that we need to fill up
35007 the so far unknown linear step. */
35008 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35009 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35015 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35016 cond
= cp_parser_omp_for_cond (parser
, decl
);
35017 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35022 /* If decl is an iterator, preserve the operator on decl
35023 until finish_omp_for. */
35025 && ((processing_template_decl
35026 && (TREE_TYPE (real_decl
) == NULL_TREE
35027 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35028 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35029 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35031 incr
= cp_parser_expression (parser
);
35032 if (!EXPR_HAS_LOCATION (incr
))
35033 protected_set_expr_location (incr
, input_location
);
35036 if (!parens
.require_close (parser
))
35037 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35038 /*or_comma=*/false,
35039 /*consume_paren=*/true);
35041 TREE_VEC_ELT (declv
, i
) = decl
;
35042 TREE_VEC_ELT (initv
, i
) = init
;
35043 TREE_VEC_ELT (condv
, i
) = cond
;
35044 TREE_VEC_ELT (incrv
, i
) = incr
;
35047 orig_inits
.safe_grow_cleared (i
+ 1);
35048 orig_inits
[i
] = orig_init
;
35051 if (i
== count
- 1)
35054 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35055 in between the collapsed for loops to be still considered perfectly
35056 nested. Hopefully the final version clarifies this.
35057 For now handle (multiple) {'s and empty statements. */
35058 cp_parser_parse_tentatively (parser
);
35061 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35063 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35065 cp_lexer_consume_token (parser
->lexer
);
35068 else if (bracecount
35069 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35070 cp_lexer_consume_token (parser
->lexer
);
35073 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35074 error_at (loc
, "not enough for loops to collapse");
35075 collapse_err
= true;
35076 cp_parser_abort_tentative_parse (parser
);
35084 cp_parser_parse_definitely (parser
);
35085 nbraces
+= bracecount
;
35092 /* Note that we saved the original contents of this flag when we entered
35093 the structured block, and so we don't need to re-save it here. */
35094 parser
->in_statement
= IN_OMP_FOR
;
35096 /* Note that the grammar doesn't call for a structured block here,
35097 though the loop as a whole is a structured block. */
35098 body
= push_stmt_list ();
35099 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35100 body
= pop_stmt_list (body
);
35102 if (declv
== NULL_TREE
)
35105 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35106 body
, pre_body
, &orig_inits
, clauses
);
35110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35112 cp_lexer_consume_token (parser
->lexer
);
35115 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35116 cp_lexer_consume_token (parser
->lexer
);
35121 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35122 "collapsed loops not perfectly nested");
35124 collapse_err
= true;
35125 cp_parser_statement_seq_opt (parser
, NULL
);
35126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35131 while (!for_block
->is_empty ())
35133 tree t
= for_block
->pop ();
35134 if (TREE_CODE (t
) == STATEMENT_LIST
)
35135 add_stmt (pop_stmt_list (t
));
35139 release_tree_vector (for_block
);
35144 /* Helper function for OpenMP parsing, split clauses and call
35145 finish_omp_clauses on each of the set of clauses afterwards. */
35148 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35149 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35152 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35153 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35155 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35159 #pragma omp simd simd-clause[optseq] new-line
35162 #define OMP_SIMD_CLAUSE_MASK \
35163 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35173 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35174 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35177 tree clauses
, sb
, ret
;
35179 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35181 strcat (p_name
, " simd");
35182 mask
|= OMP_SIMD_CLAUSE_MASK
;
35184 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35188 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35189 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35190 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35191 OMP_CLAUSE_ORDERED
);
35192 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35194 error_at (OMP_CLAUSE_LOCATION (c
),
35195 "%<ordered%> clause with parameter may not be specified "
35196 "on %qs construct", p_name
);
35197 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35201 sb
= begin_omp_structured_block ();
35202 save
= cp_parser_begin_omp_structured_block (parser
);
35204 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35206 cp_parser_end_omp_structured_block (parser
, save
);
35207 add_stmt (finish_omp_structured_block (sb
));
35213 #pragma omp for for-clause[optseq] new-line
35217 #pragma omp for simd for-simd-clause[optseq] new-line
35220 #define OMP_FOR_CLAUSE_MASK \
35221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35232 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35233 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35236 tree clauses
, sb
, ret
;
35238 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35240 strcat (p_name
, " for");
35241 mask
|= OMP_FOR_CLAUSE_MASK
;
35242 /* parallel for{, simd} disallows nowait clause, but for
35243 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35244 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35245 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35246 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35247 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35248 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35252 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35253 const char *p
= IDENTIFIER_POINTER (id
);
35255 if (strcmp (p
, "simd") == 0)
35257 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35258 if (cclauses
== NULL
)
35259 cclauses
= cclauses_buf
;
35261 cp_lexer_consume_token (parser
->lexer
);
35262 if (!flag_openmp
) /* flag_openmp_simd */
35263 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35265 sb
= begin_omp_structured_block ();
35266 save
= cp_parser_begin_omp_structured_block (parser
);
35267 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35269 cp_parser_end_omp_structured_block (parser
, save
);
35270 tree body
= finish_omp_structured_block (sb
);
35273 ret
= make_node (OMP_FOR
);
35274 TREE_TYPE (ret
) = void_type_node
;
35275 OMP_FOR_BODY (ret
) = body
;
35276 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35277 SET_EXPR_LOCATION (ret
, loc
);
35282 if (!flag_openmp
) /* flag_openmp_simd */
35284 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35288 /* Composite distribute parallel for disallows linear clause. */
35289 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35290 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35292 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35296 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35297 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35300 sb
= begin_omp_structured_block ();
35301 save
= cp_parser_begin_omp_structured_block (parser
);
35303 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35305 cp_parser_end_omp_structured_block (parser
, save
);
35306 add_stmt (finish_omp_structured_block (sb
));
35312 # pragma omp master new-line
35313 structured-block */
35316 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35318 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35319 return c_finish_omp_master (input_location
,
35320 cp_parser_omp_structured_block (parser
, if_p
));
35324 # pragma omp ordered new-line
35328 # pragma omp ordered ordered-clauses new-line
35329 structured-block */
35331 #define OMP_ORDERED_CLAUSE_MASK \
35332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35335 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35336 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35339 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35340 enum pragma_context context
, bool *if_p
)
35342 location_t loc
= pragma_tok
->location
;
35344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35346 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35347 const char *p
= IDENTIFIER_POINTER (id
);
35349 if (strcmp (p
, "depend") == 0)
35351 if (!flag_openmp
) /* flag_openmp_simd */
35353 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35356 if (context
== pragma_stmt
)
35358 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35359 "%<depend%> clause may only be used in compound "
35361 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35365 = cp_parser_omp_all_clauses (parser
,
35366 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35367 "#pragma omp ordered", pragma_tok
);
35368 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35374 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35375 "#pragma omp ordered", pragma_tok
);
35377 if (!flag_openmp
/* flag_openmp_simd */
35378 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35381 c_finish_omp_ordered (loc
, clauses
,
35382 cp_parser_omp_structured_block (parser
, if_p
));
35389 { section-sequence }
35392 section-directive[opt] structured-block
35393 section-sequence section-directive structured-block */
35396 cp_parser_omp_sections_scope (cp_parser
*parser
)
35398 tree stmt
, substmt
;
35399 bool error_suppress
= false;
35402 matching_braces braces
;
35403 if (!braces
.require_open (parser
))
35406 stmt
= push_stmt_list ();
35408 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35409 != PRAGMA_OMP_SECTION
)
35411 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35412 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35413 add_stmt (substmt
);
35418 tok
= cp_lexer_peek_token (parser
->lexer
);
35419 if (tok
->type
== CPP_CLOSE_BRACE
)
35421 if (tok
->type
== CPP_EOF
)
35424 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35426 cp_lexer_consume_token (parser
->lexer
);
35427 cp_parser_require_pragma_eol (parser
, tok
);
35428 error_suppress
= false;
35430 else if (!error_suppress
)
35432 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35433 error_suppress
= true;
35436 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35437 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35438 add_stmt (substmt
);
35440 braces
.require_close (parser
);
35442 substmt
= pop_stmt_list (stmt
);
35444 stmt
= make_node (OMP_SECTIONS
);
35445 TREE_TYPE (stmt
) = void_type_node
;
35446 OMP_SECTIONS_BODY (stmt
) = substmt
;
35453 # pragma omp sections sections-clause[optseq] newline
35456 #define OMP_SECTIONS_CLAUSE_MASK \
35457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35464 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35465 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35468 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35470 strcat (p_name
, " sections");
35471 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35473 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35475 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35479 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35480 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35483 ret
= cp_parser_omp_sections_scope (parser
);
35485 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35491 # pragma omp parallel parallel-clause[optseq] new-line
35493 # pragma omp parallel for parallel-for-clause[optseq] new-line
35495 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35499 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35500 structured-block */
35502 #define OMP_PARALLEL_CLAUSE_MASK \
35503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35514 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35515 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35518 tree stmt
, clauses
, block
;
35520 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35522 strcat (p_name
, " parallel");
35523 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35524 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35525 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35526 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35527 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35531 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35532 if (cclauses
== NULL
)
35533 cclauses
= cclauses_buf
;
35535 cp_lexer_consume_token (parser
->lexer
);
35536 if (!flag_openmp
) /* flag_openmp_simd */
35537 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35539 block
= begin_omp_parallel ();
35540 save
= cp_parser_begin_omp_structured_block (parser
);
35541 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35543 cp_parser_end_omp_structured_block (parser
, save
);
35544 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35546 if (ret
== NULL_TREE
)
35548 OMP_PARALLEL_COMBINED (stmt
) = 1;
35551 /* When combined with distribute, parallel has to be followed by for.
35552 #pragma omp target parallel is allowed though. */
35554 && (mask
& (OMP_CLAUSE_MASK_1
35555 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35557 error_at (loc
, "expected %<for%> after %qs", p_name
);
35558 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35561 else if (!flag_openmp
) /* flag_openmp_simd */
35563 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35566 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35568 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35569 const char *p
= IDENTIFIER_POINTER (id
);
35570 if (strcmp (p
, "sections") == 0)
35572 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35573 cclauses
= cclauses_buf
;
35575 cp_lexer_consume_token (parser
->lexer
);
35576 block
= begin_omp_parallel ();
35577 save
= cp_parser_begin_omp_structured_block (parser
);
35578 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35579 cp_parser_end_omp_structured_block (parser
, save
);
35580 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35582 OMP_PARALLEL_COMBINED (stmt
) = 1;
35587 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35591 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35592 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35595 block
= begin_omp_parallel ();
35596 save
= cp_parser_begin_omp_structured_block (parser
);
35597 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35598 cp_parser_end_omp_structured_block (parser
, save
);
35599 stmt
= finish_omp_parallel (clauses
, block
);
35604 # pragma omp single single-clause[optseq] new-line
35605 structured-block */
35607 #define OMP_SINGLE_CLAUSE_MASK \
35608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35614 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35616 tree stmt
= make_node (OMP_SINGLE
);
35617 TREE_TYPE (stmt
) = void_type_node
;
35619 OMP_SINGLE_CLAUSES (stmt
)
35620 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35621 "#pragma omp single", pragma_tok
);
35622 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35624 return add_stmt (stmt
);
35628 # pragma omp task task-clause[optseq] new-line
35629 structured-block */
35631 #define OMP_TASK_CLAUSE_MASK \
35632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35644 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35646 tree clauses
, block
;
35649 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35650 "#pragma omp task", pragma_tok
);
35651 block
= begin_omp_task ();
35652 save
= cp_parser_begin_omp_structured_block (parser
);
35653 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35654 cp_parser_end_omp_structured_block (parser
, save
);
35655 return finish_omp_task (clauses
, block
);
35659 # pragma omp taskwait new-line */
35662 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35664 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35665 finish_omp_taskwait ();
35669 # pragma omp taskyield new-line */
35672 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35674 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35675 finish_omp_taskyield ();
35679 # pragma omp taskgroup new-line
35680 structured-block */
35683 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35685 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35686 return c_finish_omp_taskgroup (input_location
,
35687 cp_parser_omp_structured_block (parser
,
35693 # pragma omp threadprivate (variable-list) */
35696 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35700 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35701 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35703 finish_omp_threadprivate (vars
);
35707 # pragma omp cancel cancel-clause[optseq] new-line */
35709 #define OMP_CANCEL_CLAUSE_MASK \
35710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35717 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35719 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35720 "#pragma omp cancel", pragma_tok
);
35721 finish_omp_cancel (clauses
);
35725 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35727 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35734 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35735 enum pragma_context context
)
35738 bool point_seen
= false;
35740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35742 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35743 const char *p
= IDENTIFIER_POINTER (id
);
35745 if (strcmp (p
, "point") == 0)
35747 cp_lexer_consume_token (parser
->lexer
);
35753 cp_parser_error (parser
, "expected %<point%>");
35754 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35758 if (context
!= pragma_compound
)
35760 if (context
== pragma_stmt
)
35761 error_at (pragma_tok
->location
,
35762 "%<#pragma %s%> may only be used in compound statements",
35763 "omp cancellation point");
35765 cp_parser_error (parser
, "expected declaration specifiers");
35766 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35770 clauses
= cp_parser_omp_all_clauses (parser
,
35771 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35772 "#pragma omp cancellation point",
35774 finish_omp_cancellation_point (clauses
);
35778 #pragma omp distribute distribute-clause[optseq] new-line
35781 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35789 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
35790 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35793 tree clauses
, sb
, ret
;
35795 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35797 strcat (p_name
, " distribute");
35798 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
35800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35802 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35803 const char *p
= IDENTIFIER_POINTER (id
);
35805 bool parallel
= false;
35807 if (strcmp (p
, "simd") == 0)
35810 parallel
= strcmp (p
, "parallel") == 0;
35811 if (parallel
|| simd
)
35813 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35814 if (cclauses
== NULL
)
35815 cclauses
= cclauses_buf
;
35816 cp_lexer_consume_token (parser
->lexer
);
35817 if (!flag_openmp
) /* flag_openmp_simd */
35820 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35823 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35826 sb
= begin_omp_structured_block ();
35827 save
= cp_parser_begin_omp_structured_block (parser
);
35829 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35832 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
35834 cp_parser_end_omp_structured_block (parser
, save
);
35835 tree body
= finish_omp_structured_block (sb
);
35838 ret
= make_node (OMP_DISTRIBUTE
);
35839 TREE_TYPE (ret
) = void_type_node
;
35840 OMP_FOR_BODY (ret
) = body
;
35841 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35842 SET_EXPR_LOCATION (ret
, loc
);
35847 if (!flag_openmp
) /* flag_openmp_simd */
35849 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35853 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35857 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
35858 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
35861 sb
= begin_omp_structured_block ();
35862 save
= cp_parser_begin_omp_structured_block (parser
);
35864 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
35866 cp_parser_end_omp_structured_block (parser
, save
);
35867 add_stmt (finish_omp_structured_block (sb
));
35873 # pragma omp teams teams-clause[optseq] new-line
35874 structured-block */
35876 #define OMP_TEAMS_CLAUSE_MASK \
35877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35886 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
35887 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35890 tree clauses
, sb
, ret
;
35892 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35894 strcat (p_name
, " teams");
35895 mask
|= OMP_TEAMS_CLAUSE_MASK
;
35897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35899 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35900 const char *p
= IDENTIFIER_POINTER (id
);
35901 if (strcmp (p
, "distribute") == 0)
35903 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35904 if (cclauses
== NULL
)
35905 cclauses
= cclauses_buf
;
35907 cp_lexer_consume_token (parser
->lexer
);
35908 if (!flag_openmp
) /* flag_openmp_simd */
35909 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35911 sb
= begin_omp_structured_block ();
35912 save
= cp_parser_begin_omp_structured_block (parser
);
35913 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
35915 cp_parser_end_omp_structured_block (parser
, save
);
35916 tree body
= finish_omp_structured_block (sb
);
35919 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35920 ret
= make_node (OMP_TEAMS
);
35921 TREE_TYPE (ret
) = void_type_node
;
35922 OMP_TEAMS_CLAUSES (ret
) = clauses
;
35923 OMP_TEAMS_BODY (ret
) = body
;
35924 OMP_TEAMS_COMBINED (ret
) = 1;
35925 SET_EXPR_LOCATION (ret
, loc
);
35926 return add_stmt (ret
);
35929 if (!flag_openmp
) /* flag_openmp_simd */
35931 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35935 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35939 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
35940 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
35943 tree stmt
= make_node (OMP_TEAMS
);
35944 TREE_TYPE (stmt
) = void_type_node
;
35945 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
35946 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35947 SET_EXPR_LOCATION (stmt
, loc
);
35949 return add_stmt (stmt
);
35953 # pragma omp target data target-data-clause[optseq] new-line
35954 structured-block */
35956 #define OMP_TARGET_DATA_CLAUSE_MASK \
35957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35963 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35966 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
35967 "#pragma omp target data", pragma_tok
);
35969 for (tree
*pc
= &clauses
; *pc
;)
35971 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35972 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35975 case GOMP_MAP_ALWAYS_TO
:
35976 case GOMP_MAP_FROM
:
35977 case GOMP_MAP_ALWAYS_FROM
:
35978 case GOMP_MAP_TOFROM
:
35979 case GOMP_MAP_ALWAYS_TOFROM
:
35980 case GOMP_MAP_ALLOC
:
35983 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35984 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35985 case GOMP_MAP_ALWAYS_POINTER
:
35989 error_at (OMP_CLAUSE_LOCATION (*pc
),
35990 "%<#pragma omp target data%> with map-type other "
35991 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35992 "on %<map%> clause");
35993 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35996 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36002 error_at (pragma_tok
->location
,
36003 "%<#pragma omp target data%> must contain at least "
36004 "one %<map%> clause");
36008 tree stmt
= make_node (OMP_TARGET_DATA
);
36009 TREE_TYPE (stmt
) = void_type_node
;
36010 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36012 keep_next_level (true);
36013 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36015 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36016 return add_stmt (stmt
);
36020 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36021 structured-block */
36023 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36031 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36032 enum pragma_context context
)
36034 bool data_seen
= false;
36035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36037 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36038 const char *p
= IDENTIFIER_POINTER (id
);
36040 if (strcmp (p
, "data") == 0)
36042 cp_lexer_consume_token (parser
->lexer
);
36048 cp_parser_error (parser
, "expected %<data%>");
36049 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36053 if (context
== pragma_stmt
)
36055 error_at (pragma_tok
->location
,
36056 "%<#pragma %s%> may only be used in compound statements",
36057 "omp target enter data");
36058 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36063 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36064 "#pragma omp target enter data", pragma_tok
);
36066 for (tree
*pc
= &clauses
; *pc
;)
36068 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36069 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36072 case GOMP_MAP_ALWAYS_TO
:
36073 case GOMP_MAP_ALLOC
:
36076 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36078 case GOMP_MAP_ALWAYS_POINTER
:
36082 error_at (OMP_CLAUSE_LOCATION (*pc
),
36083 "%<#pragma omp target enter data%> with map-type other "
36084 "than %<to%> or %<alloc%> on %<map%> clause");
36085 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36088 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36094 error_at (pragma_tok
->location
,
36095 "%<#pragma omp target enter data%> must contain at least "
36096 "one %<map%> clause");
36100 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36101 TREE_TYPE (stmt
) = void_type_node
;
36102 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36103 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36104 return add_stmt (stmt
);
36108 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36109 structured-block */
36111 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36119 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36120 enum pragma_context context
)
36122 bool data_seen
= false;
36123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36125 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36126 const char *p
= IDENTIFIER_POINTER (id
);
36128 if (strcmp (p
, "data") == 0)
36130 cp_lexer_consume_token (parser
->lexer
);
36136 cp_parser_error (parser
, "expected %<data%>");
36137 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36141 if (context
== pragma_stmt
)
36143 error_at (pragma_tok
->location
,
36144 "%<#pragma %s%> may only be used in compound statements",
36145 "omp target exit data");
36146 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36151 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36152 "#pragma omp target exit data", pragma_tok
);
36154 for (tree
*pc
= &clauses
; *pc
;)
36156 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36157 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36159 case GOMP_MAP_FROM
:
36160 case GOMP_MAP_ALWAYS_FROM
:
36161 case GOMP_MAP_RELEASE
:
36162 case GOMP_MAP_DELETE
:
36165 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36166 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36167 case GOMP_MAP_ALWAYS_POINTER
:
36171 error_at (OMP_CLAUSE_LOCATION (*pc
),
36172 "%<#pragma omp target exit data%> with map-type other "
36173 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36175 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36178 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36184 error_at (pragma_tok
->location
,
36185 "%<#pragma omp target exit data%> must contain at least "
36186 "one %<map%> clause");
36190 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36191 TREE_TYPE (stmt
) = void_type_node
;
36192 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36193 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36194 return add_stmt (stmt
);
36198 # pragma omp target update target-update-clause[optseq] new-line */
36200 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36209 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36210 enum pragma_context context
)
36212 if (context
== pragma_stmt
)
36214 error_at (pragma_tok
->location
,
36215 "%<#pragma %s%> may only be used in compound statements",
36216 "omp target update");
36217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36222 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36223 "#pragma omp target update", pragma_tok
);
36224 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36225 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36227 error_at (pragma_tok
->location
,
36228 "%<#pragma omp target update%> must contain at least one "
36229 "%<from%> or %<to%> clauses");
36233 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36234 TREE_TYPE (stmt
) = void_type_node
;
36235 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36236 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36242 # pragma omp target target-clause[optseq] new-line
36243 structured-block */
36245 #define OMP_TARGET_CLAUSE_MASK \
36246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36257 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36258 enum pragma_context context
, bool *if_p
)
36260 tree
*pc
= NULL
, stmt
;
36262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36264 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36265 const char *p
= IDENTIFIER_POINTER (id
);
36266 enum tree_code ccode
= ERROR_MARK
;
36268 if (strcmp (p
, "teams") == 0)
36270 else if (strcmp (p
, "parallel") == 0)
36271 ccode
= OMP_PARALLEL
;
36272 else if (strcmp (p
, "simd") == 0)
36274 if (ccode
!= ERROR_MARK
)
36276 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36277 char p_name
[sizeof ("#pragma omp target teams distribute "
36278 "parallel for simd")];
36280 cp_lexer_consume_token (parser
->lexer
);
36281 strcpy (p_name
, "#pragma omp target");
36282 if (!flag_openmp
) /* flag_openmp_simd */
36288 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36289 OMP_TARGET_CLAUSE_MASK
,
36293 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36294 OMP_TARGET_CLAUSE_MASK
,
36298 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36299 OMP_TARGET_CLAUSE_MASK
,
36303 gcc_unreachable ();
36305 return stmt
!= NULL_TREE
;
36307 keep_next_level (true);
36308 tree sb
= begin_omp_structured_block (), ret
;
36309 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36313 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36314 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36318 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36319 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36323 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36324 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36328 gcc_unreachable ();
36330 cp_parser_end_omp_structured_block (parser
, save
);
36331 tree body
= finish_omp_structured_block (sb
);
36332 if (ret
== NULL_TREE
)
36334 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36336 /* For combined target teams, ensure the num_teams and
36337 thread_limit clause expressions are evaluated on the host,
36338 before entering the target construct. */
36340 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36341 c
; c
= OMP_CLAUSE_CHAIN (c
))
36342 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36343 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36344 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36346 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36347 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36348 if (expr
== error_mark_node
)
36350 tree tmp
= TARGET_EXPR_SLOT (expr
);
36352 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36353 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36354 OMP_CLAUSE_FIRSTPRIVATE
);
36355 OMP_CLAUSE_DECL (tc
) = tmp
;
36356 OMP_CLAUSE_CHAIN (tc
)
36357 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36358 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36361 tree stmt
= make_node (OMP_TARGET
);
36362 TREE_TYPE (stmt
) = void_type_node
;
36363 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36364 OMP_TARGET_BODY (stmt
) = body
;
36365 OMP_TARGET_COMBINED (stmt
) = 1;
36366 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36368 pc
= &OMP_TARGET_CLAUSES (stmt
);
36369 goto check_clauses
;
36371 else if (!flag_openmp
) /* flag_openmp_simd */
36373 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36376 else if (strcmp (p
, "data") == 0)
36378 cp_lexer_consume_token (parser
->lexer
);
36379 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36382 else if (strcmp (p
, "enter") == 0)
36384 cp_lexer_consume_token (parser
->lexer
);
36385 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36388 else if (strcmp (p
, "exit") == 0)
36390 cp_lexer_consume_token (parser
->lexer
);
36391 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36394 else if (strcmp (p
, "update") == 0)
36396 cp_lexer_consume_token (parser
->lexer
);
36397 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36400 if (!flag_openmp
) /* flag_openmp_simd */
36402 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36406 stmt
= make_node (OMP_TARGET
);
36407 TREE_TYPE (stmt
) = void_type_node
;
36409 OMP_TARGET_CLAUSES (stmt
)
36410 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36411 "#pragma omp target", pragma_tok
);
36412 pc
= &OMP_TARGET_CLAUSES (stmt
);
36413 keep_next_level (true);
36414 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36416 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36422 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36423 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36426 case GOMP_MAP_ALWAYS_TO
:
36427 case GOMP_MAP_FROM
:
36428 case GOMP_MAP_ALWAYS_FROM
:
36429 case GOMP_MAP_TOFROM
:
36430 case GOMP_MAP_ALWAYS_TOFROM
:
36431 case GOMP_MAP_ALLOC
:
36432 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36433 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36434 case GOMP_MAP_ALWAYS_POINTER
:
36437 error_at (OMP_CLAUSE_LOCATION (*pc
),
36438 "%<#pragma omp target%> with map-type other "
36439 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36440 "on %<map%> clause");
36441 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36444 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36450 # pragma acc cache (variable-list) new-line
36454 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36456 tree stmt
, clauses
;
36458 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36459 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36461 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36463 stmt
= make_node (OACC_CACHE
);
36464 TREE_TYPE (stmt
) = void_type_node
;
36465 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36466 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36473 # pragma acc data oacc-data-clause[optseq] new-line
36474 structured-block */
36476 #define OACC_DATA_CLAUSE_MASK \
36477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36490 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36492 tree stmt
, clauses
, block
;
36495 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36496 "#pragma acc data", pragma_tok
);
36498 block
= begin_omp_parallel ();
36499 save
= cp_parser_begin_omp_structured_block (parser
);
36500 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36501 cp_parser_end_omp_structured_block (parser
, save
);
36502 stmt
= finish_oacc_data (clauses
, block
);
36507 # pragma acc host_data <clauses> new-line
36508 structured-block */
36510 #define OACC_HOST_DATA_CLAUSE_MASK \
36511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36514 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36516 tree stmt
, clauses
, block
;
36519 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36520 "#pragma acc host_data", pragma_tok
);
36522 block
= begin_omp_parallel ();
36523 save
= cp_parser_begin_omp_structured_block (parser
);
36524 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36525 cp_parser_end_omp_structured_block (parser
, save
);
36526 stmt
= finish_oacc_host_data (clauses
, block
);
36531 # pragma acc declare oacc-data-clause[optseq] new-line
36534 #define OACC_DECLARE_CLAUSE_MASK \
36535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36549 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36551 tree clauses
, stmt
;
36552 bool error
= false;
36554 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36555 "#pragma acc declare", pragma_tok
, true);
36558 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36560 error_at (pragma_tok
->location
,
36561 "no valid clauses specified in %<#pragma acc declare%>");
36565 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36567 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36568 tree decl
= OMP_CLAUSE_DECL (t
);
36569 if (!DECL_P (decl
))
36571 error_at (loc
, "array section in %<#pragma acc declare%>");
36575 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36576 switch (OMP_CLAUSE_MAP_KIND (t
))
36578 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36579 case GOMP_MAP_FORCE_ALLOC
:
36580 case GOMP_MAP_FORCE_TO
:
36581 case GOMP_MAP_FORCE_DEVICEPTR
:
36582 case GOMP_MAP_DEVICE_RESIDENT
:
36585 case GOMP_MAP_LINK
:
36586 if (!global_bindings_p ()
36587 && (TREE_STATIC (decl
)
36588 || !DECL_EXTERNAL (decl
)))
36591 "%qD must be a global variable in "
36592 "%<#pragma acc declare link%>",
36600 if (global_bindings_p ())
36602 error_at (loc
, "invalid OpenACC clause at file scope");
36606 if (DECL_EXTERNAL (decl
))
36609 "invalid use of %<extern%> variable %qD "
36610 "in %<#pragma acc declare%>", decl
);
36614 else if (TREE_PUBLIC (decl
))
36617 "invalid use of %<global%> variable %qD "
36618 "in %<#pragma acc declare%>", decl
);
36625 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36626 || lookup_attribute ("omp declare target link",
36627 DECL_ATTRIBUTES (decl
)))
36629 error_at (loc
, "variable %qD used more than once with "
36630 "%<#pragma acc declare%>", decl
);
36639 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36640 id
= get_identifier ("omp declare target link");
36642 id
= get_identifier ("omp declare target");
36644 DECL_ATTRIBUTES (decl
)
36645 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36646 if (global_bindings_p ())
36648 symtab_node
*node
= symtab_node::get (decl
);
36651 node
->offloadable
= 1;
36652 if (ENABLE_OFFLOADING
)
36654 g
->have_offload
= true;
36655 if (is_a
<varpool_node
*> (node
))
36656 vec_safe_push (offload_vars
, decl
);
36663 if (error
|| global_bindings_p ())
36666 stmt
= make_node (OACC_DECLARE
);
36667 TREE_TYPE (stmt
) = void_type_node
;
36668 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36669 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36677 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36681 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36683 LOC is the location of the #pragma token.
36686 #define OACC_ENTER_DATA_CLAUSE_MASK \
36687 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36695 #define OACC_EXIT_DATA_CLAUSE_MASK \
36696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36703 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36706 location_t loc
= pragma_tok
->location
;
36707 tree stmt
, clauses
;
36708 const char *p
= "";
36710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36711 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36713 if (strcmp (p
, "data") != 0)
36715 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36716 enter
? "enter" : "exit");
36717 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36721 cp_lexer_consume_token (parser
->lexer
);
36724 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36725 "#pragma acc enter data", pragma_tok
);
36727 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36728 "#pragma acc exit data", pragma_tok
);
36730 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36732 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36733 enter
? "enter" : "exit");
36737 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36738 TREE_TYPE (stmt
) = void_type_node
;
36739 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36740 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36746 # pragma acc loop oacc-loop-clause[optseq] new-line
36747 structured-block */
36749 #define OACC_LOOP_CLAUSE_MASK \
36750 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36762 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36763 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36765 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36767 strcat (p_name
, " loop");
36768 mask
|= OACC_LOOP_CLAUSE_MASK
;
36770 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36774 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36776 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36778 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36781 tree block
= begin_omp_structured_block ();
36782 int save
= cp_parser_begin_omp_structured_block (parser
);
36783 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
36784 cp_parser_end_omp_structured_block (parser
, save
);
36785 add_stmt (finish_omp_structured_block (block
));
36791 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36796 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36800 #define OACC_KERNELS_CLAUSE_MASK \
36801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36819 #define OACC_PARALLEL_CLAUSE_MASK \
36820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36842 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
36843 char *p_name
, bool *if_p
)
36845 omp_clause_mask mask
;
36846 enum tree_code code
;
36847 switch (cp_parser_pragma_kind (pragma_tok
))
36849 case PRAGMA_OACC_KERNELS
:
36850 strcat (p_name
, " kernels");
36851 mask
= OACC_KERNELS_CLAUSE_MASK
;
36852 code
= OACC_KERNELS
;
36854 case PRAGMA_OACC_PARALLEL
:
36855 strcat (p_name
, " parallel");
36856 mask
= OACC_PARALLEL_CLAUSE_MASK
;
36857 code
= OACC_PARALLEL
;
36860 gcc_unreachable ();
36863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36866 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36867 if (strcmp (p
, "loop") == 0)
36869 cp_lexer_consume_token (parser
->lexer
);
36870 tree block
= begin_omp_parallel ();
36872 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
36874 return finish_omp_construct (code
, block
, clauses
);
36878 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
36880 tree block
= begin_omp_parallel ();
36881 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36882 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36883 cp_parser_end_omp_structured_block (parser
, save
);
36884 return finish_omp_construct (code
, block
, clauses
);
36888 # pragma acc update oacc-update-clause[optseq] new-line
36891 #define OACC_UPDATE_CLAUSE_MASK \
36892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36900 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
36902 tree stmt
, clauses
;
36904 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
36905 "#pragma acc update", pragma_tok
);
36907 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36909 error_at (pragma_tok
->location
,
36910 "%<#pragma acc update%> must contain at least one "
36911 "%<device%> or %<host%> or %<self%> clause");
36915 stmt
= make_node (OACC_UPDATE
);
36916 TREE_TYPE (stmt
) = void_type_node
;
36917 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
36918 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36924 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36926 LOC is the location of the #pragma token.
36929 #define OACC_WAIT_CLAUSE_MASK \
36930 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36933 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
36935 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
36936 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36938 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36939 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
36941 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
36942 "#pragma acc wait", pragma_tok
);
36944 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
36945 stmt
= finish_expr_stmt (stmt
);
36951 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36953 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36962 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
36963 enum pragma_context context
)
36965 bool first_p
= parser
->omp_declare_simd
== NULL
;
36966 cp_omp_declare_simd_data data
;
36969 data
.error_seen
= false;
36970 data
.fndecl_seen
= false;
36971 data
.tokens
= vNULL
;
36972 data
.clauses
= NULL_TREE
;
36973 /* It is safe to take the address of a local variable; it will only be
36974 used while this scope is live. */
36975 parser
->omp_declare_simd
= &data
;
36978 /* Store away all pragma tokens. */
36979 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36980 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36981 cp_lexer_consume_token (parser
->lexer
);
36982 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36983 parser
->omp_declare_simd
->error_seen
= true;
36984 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36985 struct cp_token_cache
*cp
36986 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36987 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
36991 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36992 cp_parser_pragma (parser
, context
, NULL
);
36995 case pragma_external
:
36996 cp_parser_declaration (parser
);
36998 case pragma_member
:
36999 cp_parser_member_declaration (parser
);
37001 case pragma_objc_icode
:
37002 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37005 cp_parser_declaration_statement (parser
);
37008 if (parser
->omp_declare_simd
37009 && !parser
->omp_declare_simd
->error_seen
37010 && !parser
->omp_declare_simd
->fndecl_seen
)
37011 error_at (pragma_tok
->location
,
37012 "%<#pragma omp declare simd%> not immediately followed by "
37013 "function declaration or definition");
37014 data
.tokens
.release ();
37015 parser
->omp_declare_simd
= NULL
;
37019 /* Finalize #pragma omp declare simd clauses after direct declarator has
37020 been parsed, and put that into "omp declare simd" attribute. */
37023 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37025 struct cp_token_cache
*ce
;
37026 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37029 if (!data
->error_seen
&& data
->fndecl_seen
)
37031 error ("%<#pragma omp declare simd%> not immediately followed by "
37032 "a single function declaration or definition");
37033 data
->error_seen
= true;
37035 if (data
->error_seen
)
37038 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37042 cp_parser_push_lexer_for_tokens (parser
, ce
);
37043 parser
->lexer
->in_pragma
= true;
37044 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37045 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37046 cp_lexer_consume_token (parser
->lexer
);
37047 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37048 "#pragma omp declare simd", pragma_tok
);
37049 cp_parser_pop_lexer (parser
);
37051 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37052 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37053 TREE_CHAIN (c
) = attrs
;
37054 if (processing_template_decl
)
37055 ATTR_IS_DEPENDENT (c
) = 1;
37059 data
->fndecl_seen
= true;
37065 # pragma omp declare target new-line
37066 declarations and definitions
37067 # pragma omp end declare target new-line
37070 # pragma omp declare target ( extended-list ) new-line
37072 # pragma omp declare target declare-target-clauses[seq] new-line */
37074 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37079 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37081 tree clauses
= NULL_TREE
;
37082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37084 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37085 "#pragma omp declare target", pragma_tok
);
37086 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37088 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37090 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37091 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37095 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37096 scope_chain
->omp_declare_target_attribute
++;
37099 if (scope_chain
->omp_declare_target_attribute
)
37100 error_at (pragma_tok
->location
,
37101 "%<#pragma omp declare target%> with clauses in between "
37102 "%<#pragma omp declare target%> without clauses and "
37103 "%<#pragma omp end declare target%>");
37104 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37106 tree t
= OMP_CLAUSE_DECL (c
), id
;
37107 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37108 tree at2
= lookup_attribute ("omp declare target link",
37109 DECL_ATTRIBUTES (t
));
37110 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37112 id
= get_identifier ("omp declare target link");
37113 std::swap (at1
, at2
);
37116 id
= get_identifier ("omp declare target");
37119 error_at (OMP_CLAUSE_LOCATION (c
),
37120 "%qD specified both in declare target %<link%> and %<to%>"
37126 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37127 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37130 symtab_node
*node
= symtab_node::get (t
);
37133 node
->offloadable
= 1;
37134 if (ENABLE_OFFLOADING
)
37136 g
->have_offload
= true;
37137 if (is_a
<varpool_node
*> (node
))
37138 vec_safe_push (offload_vars
, t
);
37146 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37148 const char *p
= "";
37149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37151 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37152 p
= IDENTIFIER_POINTER (id
);
37154 if (strcmp (p
, "declare") == 0)
37156 cp_lexer_consume_token (parser
->lexer
);
37158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37160 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37161 p
= IDENTIFIER_POINTER (id
);
37163 if (strcmp (p
, "target") == 0)
37164 cp_lexer_consume_token (parser
->lexer
);
37167 cp_parser_error (parser
, "expected %<target%>");
37168 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37174 cp_parser_error (parser
, "expected %<declare%>");
37175 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37178 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37179 if (!scope_chain
->omp_declare_target_attribute
)
37180 error_at (pragma_tok
->location
,
37181 "%<#pragma omp end declare target%> without corresponding "
37182 "%<#pragma omp declare target%>");
37184 scope_chain
->omp_declare_target_attribute
--;
37187 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37188 expression and optional initializer clause of
37189 #pragma omp declare reduction. We store the expression(s) as
37190 either 3, 6 or 7 special statements inside of the artificial function's
37191 body. The first two statements are DECL_EXPRs for the artificial
37192 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37193 expression that uses those variables.
37194 If there was any INITIALIZER clause, this is followed by further statements,
37195 the fourth and fifth statements are DECL_EXPRs for the artificial
37196 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37197 constructor variant (first token after open paren is not omp_priv),
37198 then the sixth statement is a statement with the function call expression
37199 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37200 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37201 to initialize the OMP_PRIV artificial variable and there is seventh
37202 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37205 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37207 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37208 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37209 type
= TREE_TYPE (type
);
37210 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37211 DECL_ARTIFICIAL (omp_out
) = 1;
37212 pushdecl (omp_out
);
37213 add_decl_expr (omp_out
);
37214 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37215 DECL_ARTIFICIAL (omp_in
) = 1;
37217 add_decl_expr (omp_in
);
37219 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37221 keep_next_level (true);
37222 tree block
= begin_omp_structured_block ();
37223 combiner
= cp_parser_expression (parser
);
37224 finish_expr_stmt (combiner
);
37225 block
= finish_omp_structured_block (block
);
37228 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37231 const char *p
= "";
37232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37234 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37235 p
= IDENTIFIER_POINTER (id
);
37238 if (strcmp (p
, "initializer") == 0)
37240 cp_lexer_consume_token (parser
->lexer
);
37241 matching_parens parens
;
37242 if (!parens
.require_open (parser
))
37246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37248 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37249 p
= IDENTIFIER_POINTER (id
);
37252 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37253 DECL_ARTIFICIAL (omp_priv
) = 1;
37254 pushdecl (omp_priv
);
37255 add_decl_expr (omp_priv
);
37256 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37257 DECL_ARTIFICIAL (omp_orig
) = 1;
37258 pushdecl (omp_orig
);
37259 add_decl_expr (omp_orig
);
37261 keep_next_level (true);
37262 block
= begin_omp_structured_block ();
37265 if (strcmp (p
, "omp_priv") == 0)
37267 bool is_direct_init
, is_non_constant_init
;
37269 cp_lexer_consume_token (parser
->lexer
);
37270 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37272 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37273 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37275 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37276 == CPP_CLOSE_PAREN
))
37278 finish_omp_structured_block (block
);
37279 error ("invalid initializer clause");
37282 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37283 &is_non_constant_init
);
37284 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37285 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37289 cp_parser_parse_tentatively (parser
);
37290 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37291 /*check_dependency_p=*/true,
37292 /*template_p=*/NULL
,
37293 /*declarator_p=*/false,
37294 /*optional_p=*/false);
37295 vec
<tree
, va_gc
> *args
;
37296 if (fn_name
== error_mark_node
37297 || cp_parser_error_occurred (parser
)
37298 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37299 || ((args
= cp_parser_parenthesized_expression_list
37300 (parser
, non_attr
, /*cast_p=*/false,
37301 /*allow_expansion_p=*/true,
37302 /*non_constant_p=*/NULL
)),
37303 cp_parser_error_occurred (parser
)))
37305 finish_omp_structured_block (block
);
37306 cp_parser_abort_tentative_parse (parser
);
37307 cp_parser_error (parser
, "expected id-expression (arguments)");
37312 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37313 if (arg
== omp_priv
37314 || (TREE_CODE (arg
) == ADDR_EXPR
37315 && TREE_OPERAND (arg
, 0) == omp_priv
))
37317 cp_parser_abort_tentative_parse (parser
);
37318 if (arg
== NULL_TREE
)
37319 error ("one of the initializer call arguments should be %<omp_priv%>"
37320 " or %<&omp_priv%>");
37321 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37323 finish_expr_stmt (initializer
);
37326 block
= finish_omp_structured_block (block
);
37327 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37331 add_decl_expr (omp_orig
);
37333 if (!parens
.require_close (parser
))
37337 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37338 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37345 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37346 initializer-clause[opt] new-line
37348 initializer-clause:
37349 initializer (omp_priv initializer)
37350 initializer (function-name (argument-list)) */
37353 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37354 enum pragma_context
)
37356 auto_vec
<tree
> types
;
37357 enum tree_code reduc_code
= ERROR_MARK
;
37358 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37360 cp_token
*first_token
;
37361 cp_token_cache
*cp
;
37365 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37366 p
= obstack_alloc (&declarator_obstack
, 0);
37368 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37371 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37374 reduc_code
= PLUS_EXPR
;
37377 reduc_code
= MULT_EXPR
;
37380 reduc_code
= MINUS_EXPR
;
37383 reduc_code
= BIT_AND_EXPR
;
37386 reduc_code
= BIT_XOR_EXPR
;
37389 reduc_code
= BIT_IOR_EXPR
;
37392 reduc_code
= TRUTH_ANDIF_EXPR
;
37395 reduc_code
= TRUTH_ORIF_EXPR
;
37398 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37401 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37402 "%<|%>, %<&&%>, %<||%> or identifier");
37406 if (reduc_code
!= ERROR_MARK
)
37407 cp_lexer_consume_token (parser
->lexer
);
37409 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37410 if (reduc_id
== error_mark_node
)
37413 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37416 /* Types may not be defined in declare reduction type list. */
37417 const char *saved_message
;
37418 saved_message
= parser
->type_definition_forbidden_message
;
37419 parser
->type_definition_forbidden_message
37420 = G_("types may not be defined in declare reduction type list");
37421 bool saved_colon_corrects_to_scope_p
;
37422 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37423 parser
->colon_corrects_to_scope_p
= false;
37424 bool saved_colon_doesnt_start_class_def_p
;
37425 saved_colon_doesnt_start_class_def_p
37426 = parser
->colon_doesnt_start_class_def_p
;
37427 parser
->colon_doesnt_start_class_def_p
= true;
37431 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37432 type
= cp_parser_type_id (parser
);
37433 if (type
== error_mark_node
)
37435 else if (ARITHMETIC_TYPE_P (type
)
37436 && (orig_reduc_id
== NULL_TREE
37437 || (TREE_CODE (type
) != COMPLEX_TYPE
37438 && (id_equal (orig_reduc_id
, "min")
37439 || id_equal (orig_reduc_id
, "max")))))
37440 error_at (loc
, "predeclared arithmetic type %qT in "
37441 "%<#pragma omp declare reduction%>", type
);
37442 else if (TREE_CODE (type
) == FUNCTION_TYPE
37443 || TREE_CODE (type
) == METHOD_TYPE
37444 || TREE_CODE (type
) == ARRAY_TYPE
)
37445 error_at (loc
, "function or array type %qT in "
37446 "%<#pragma omp declare reduction%>", type
);
37447 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37448 error_at (loc
, "reference type %qT in "
37449 "%<#pragma omp declare reduction%>", type
);
37450 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37451 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37452 "%<#pragma omp declare reduction%>", type
);
37454 types
.safe_push (type
);
37456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37457 cp_lexer_consume_token (parser
->lexer
);
37462 /* Restore the saved message. */
37463 parser
->type_definition_forbidden_message
= saved_message
;
37464 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37465 parser
->colon_doesnt_start_class_def_p
37466 = saved_colon_doesnt_start_class_def_p
;
37468 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37469 || types
.is_empty ())
37472 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37476 first_token
= cp_lexer_peek_token (parser
->lexer
);
37479 FOR_EACH_VEC_ELT (types
, i
, type
)
37482 = build_function_type_list (void_type_node
,
37483 cp_build_reference_type (type
, false),
37485 tree this_reduc_id
= reduc_id
;
37486 if (!dependent_type_p (type
))
37487 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37488 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37489 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37490 DECL_ARTIFICIAL (fndecl
) = 1;
37491 DECL_EXTERNAL (fndecl
) = 1;
37492 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37493 DECL_IGNORED_P (fndecl
) = 1;
37494 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37495 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37496 DECL_ATTRIBUTES (fndecl
)
37497 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37498 DECL_ATTRIBUTES (fndecl
));
37499 if (processing_template_decl
)
37500 fndecl
= push_template_decl (fndecl
);
37501 bool block_scope
= false;
37502 tree block
= NULL_TREE
;
37503 if (current_function_decl
)
37505 block_scope
= true;
37506 DECL_CONTEXT (fndecl
) = global_namespace
;
37507 if (!processing_template_decl
)
37510 else if (current_class_type
)
37514 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37515 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37516 cp_lexer_consume_token (parser
->lexer
);
37517 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37519 cp
= cp_token_cache_new (first_token
,
37520 cp_lexer_peek_nth_token (parser
->lexer
,
37523 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37524 finish_member_declaration (fndecl
);
37525 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37526 DECL_PENDING_INLINE_P (fndecl
) = 1;
37527 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37532 DECL_CONTEXT (fndecl
) = current_namespace
;
37536 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37538 block
= begin_omp_structured_block ();
37541 cp_parser_push_lexer_for_tokens (parser
, cp
);
37542 parser
->lexer
->in_pragma
= true;
37544 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37547 finish_function (/*inline_p=*/false);
37549 DECL_CONTEXT (fndecl
) = current_function_decl
;
37551 cp_parser_pop_lexer (parser
);
37555 cp_parser_pop_lexer (parser
);
37557 finish_function (/*inline_p=*/false);
37560 DECL_CONTEXT (fndecl
) = current_function_decl
;
37561 block
= finish_omp_structured_block (block
);
37562 if (TREE_CODE (block
) == BIND_EXPR
)
37563 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37564 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37565 DECL_SAVED_TREE (fndecl
) = block
;
37566 if (processing_template_decl
)
37567 add_decl_expr (fndecl
);
37569 cp_check_omp_declare_reduction (fndecl
);
37570 if (cp
== NULL
&& types
.length () > 1)
37571 cp
= cp_token_cache_new (first_token
,
37572 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37573 if (errs
!= errorcount
)
37577 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37580 /* Free any declarators allocated. */
37581 obstack_free (&declarator_obstack
, p
);
37585 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37586 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37587 initializer-clause[opt] new-line
37588 #pragma omp declare target new-line */
37591 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37592 enum pragma_context context
)
37594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37596 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37597 const char *p
= IDENTIFIER_POINTER (id
);
37599 if (strcmp (p
, "simd") == 0)
37601 cp_lexer_consume_token (parser
->lexer
);
37602 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37606 cp_ensure_no_omp_declare_simd (parser
);
37607 if (strcmp (p
, "reduction") == 0)
37609 cp_lexer_consume_token (parser
->lexer
);
37610 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37614 if (!flag_openmp
) /* flag_openmp_simd */
37616 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37619 if (strcmp (p
, "target") == 0)
37621 cp_lexer_consume_token (parser
->lexer
);
37622 cp_parser_omp_declare_target (parser
, pragma_tok
);
37626 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37628 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37633 #pragma omp taskloop taskloop-clause[optseq] new-line
37636 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37639 #define OMP_TASKLOOP_CLAUSE_MASK \
37640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37656 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37657 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37660 tree clauses
, sb
, ret
;
37662 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37664 strcat (p_name
, " taskloop");
37665 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37669 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37670 const char *p
= IDENTIFIER_POINTER (id
);
37672 if (strcmp (p
, "simd") == 0)
37674 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37675 if (cclauses
== NULL
)
37676 cclauses
= cclauses_buf
;
37678 cp_lexer_consume_token (parser
->lexer
);
37679 if (!flag_openmp
) /* flag_openmp_simd */
37680 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37682 sb
= begin_omp_structured_block ();
37683 save
= cp_parser_begin_omp_structured_block (parser
);
37684 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37686 cp_parser_end_omp_structured_block (parser
, save
);
37687 tree body
= finish_omp_structured_block (sb
);
37690 ret
= make_node (OMP_TASKLOOP
);
37691 TREE_TYPE (ret
) = void_type_node
;
37692 OMP_FOR_BODY (ret
) = body
;
37693 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37694 SET_EXPR_LOCATION (ret
, loc
);
37699 if (!flag_openmp
) /* flag_openmp_simd */
37701 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37705 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37709 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37710 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37713 sb
= begin_omp_structured_block ();
37714 save
= cp_parser_begin_omp_structured_block (parser
);
37716 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37719 cp_parser_end_omp_structured_block (parser
, save
);
37720 add_stmt (finish_omp_structured_block (sb
));
37727 # pragma acc routine oacc-routine-clause[optseq] new-line
37728 function-definition
37730 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37733 #define OACC_ROUTINE_CLAUSE_MASK \
37734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37740 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37741 component, which must resolve to a declared namespace-scope
37742 function. The clauses are either processed directly (for a named
37743 function), or defered until the immediatley following declaration
37747 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37748 enum pragma_context context
)
37750 gcc_checking_assert (context
== pragma_external
);
37751 /* The checking for "another pragma following this one" in the "no optional
37752 '( name )'" case makes sure that we dont re-enter. */
37753 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37755 cp_oacc_routine_data data
;
37756 data
.error_seen
= false;
37757 data
.fndecl_seen
= false;
37758 data
.tokens
= vNULL
;
37759 data
.clauses
= NULL_TREE
;
37760 data
.loc
= pragma_tok
->location
;
37761 /* It is safe to take the address of a local variable; it will only be
37762 used while this scope is live. */
37763 parser
->oacc_routine
= &data
;
37765 /* Look for optional '( name )'. */
37766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37768 matching_parens parens
;
37769 parens
.consume_open (parser
); /* '(' */
37771 /* We parse the name as an id-expression. If it resolves to
37772 anything other than a non-overloaded function at namespace
37773 scope, it's an error. */
37774 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37775 tree name
= cp_parser_id_expression (parser
,
37776 /*template_keyword_p=*/false,
37777 /*check_dependency_p=*/false,
37778 /*template_p=*/NULL
,
37779 /*declarator_p=*/false,
37780 /*optional_p=*/false);
37781 tree decl
= cp_parser_lookup_name_simple (parser
, name
, name_loc
);
37782 if (name
!= error_mark_node
&& decl
== error_mark_node
)
37783 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
37785 if (decl
== error_mark_node
37786 || !parens
.require_close (parser
))
37788 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37789 parser
->oacc_routine
= NULL
;
37794 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37795 "#pragma acc routine",
37796 cp_lexer_peek_token (parser
->lexer
));
37798 if (decl
&& is_overloaded_fn (decl
)
37799 && (TREE_CODE (decl
) != FUNCTION_DECL
37800 || DECL_FUNCTION_TEMPLATE_P (decl
)))
37802 error_at (name_loc
,
37803 "%<#pragma acc routine%> names a set of overloads");
37804 parser
->oacc_routine
= NULL
;
37808 /* Perhaps we should use the same rule as declarations in different
37810 if (!DECL_NAMESPACE_SCOPE_P (decl
))
37812 error_at (name_loc
,
37813 "%qD does not refer to a namespace scope function", decl
);
37814 parser
->oacc_routine
= NULL
;
37818 if (TREE_CODE (decl
) != FUNCTION_DECL
)
37820 error_at (name_loc
, "%qD does not refer to a function", decl
);
37821 parser
->oacc_routine
= NULL
;
37825 cp_finalize_oacc_routine (parser
, decl
, false);
37826 parser
->oacc_routine
= NULL
;
37828 else /* No optional '( name )'. */
37830 /* Store away all pragma tokens. */
37831 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37832 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37833 cp_lexer_consume_token (parser
->lexer
);
37834 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37835 parser
->oacc_routine
->error_seen
= true;
37836 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37837 struct cp_token_cache
*cp
37838 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37839 parser
->oacc_routine
->tokens
.safe_push (cp
);
37841 /* Emit a helpful diagnostic if there's another pragma following this
37843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37845 cp_ensure_no_oacc_routine (parser
);
37846 data
.tokens
.release ();
37847 /* ..., and then just keep going. */
37851 /* We only have to consider the pragma_external case here. */
37852 cp_parser_declaration (parser
);
37853 if (parser
->oacc_routine
37854 && !parser
->oacc_routine
->fndecl_seen
)
37855 cp_ensure_no_oacc_routine (parser
);
37857 parser
->oacc_routine
= NULL
;
37858 data
.tokens
.release ();
37862 /* Finalize #pragma acc routine clauses after direct declarator has
37866 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
37868 struct cp_token_cache
*ce
;
37869 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
37871 if (!data
->error_seen
&& data
->fndecl_seen
)
37873 error_at (data
->loc
,
37874 "%<#pragma acc routine%> not immediately followed by "
37875 "a single function declaration or definition");
37876 data
->error_seen
= true;
37878 if (data
->error_seen
)
37881 gcc_checking_assert (data
->tokens
.length () == 1);
37882 ce
= data
->tokens
[0];
37884 cp_parser_push_lexer_for_tokens (parser
, ce
);
37885 parser
->lexer
->in_pragma
= true;
37886 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37888 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37889 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
37890 parser
->oacc_routine
->clauses
37891 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
37892 "#pragma acc routine", pragma_tok
);
37893 cp_parser_pop_lexer (parser
);
37894 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37900 /* Apply any saved OpenACC routine clauses to a just-parsed
37904 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
37906 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
37908 /* Keep going if we're in error reporting mode. */
37909 if (parser
->oacc_routine
->error_seen
37910 || fndecl
== error_mark_node
)
37913 if (parser
->oacc_routine
->fndecl_seen
)
37915 error_at (parser
->oacc_routine
->loc
,
37916 "%<#pragma acc routine%> not immediately followed by"
37917 " a single function declaration or definition");
37918 parser
->oacc_routine
= NULL
;
37921 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
37923 cp_ensure_no_oacc_routine (parser
);
37927 if (oacc_get_fn_attrib (fndecl
))
37929 error_at (parser
->oacc_routine
->loc
,
37930 "%<#pragma acc routine%> already applied to %qD", fndecl
);
37931 parser
->oacc_routine
= NULL
;
37935 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
37937 error_at (parser
->oacc_routine
->loc
,
37939 ? G_("%<#pragma acc routine%> must be applied before use")
37940 : G_("%<#pragma acc routine%> must be applied before "
37942 parser
->oacc_routine
= NULL
;
37946 /* Process the routine's dimension clauses. */
37947 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
37948 oacc_replace_fn_attrib (fndecl
, dims
);
37950 /* Add an "omp declare target" attribute. */
37951 DECL_ATTRIBUTES (fndecl
)
37952 = tree_cons (get_identifier ("omp declare target"),
37953 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
37955 /* Don't unset parser->oacc_routine here: we may still need it to
37956 diagnose wrong usage. But, remember that we've used this "#pragma acc
37958 parser
->oacc_routine
->fndecl_seen
= true;
37962 /* Main entry point to OpenMP statement pragmas. */
37965 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37968 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
37969 omp_clause_mask
mask (0);
37971 switch (cp_parser_pragma_kind (pragma_tok
))
37973 case PRAGMA_OACC_ATOMIC
:
37974 cp_parser_omp_atomic (parser
, pragma_tok
);
37976 case PRAGMA_OACC_CACHE
:
37977 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
37979 case PRAGMA_OACC_DATA
:
37980 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
37982 case PRAGMA_OACC_ENTER_DATA
:
37983 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
37985 case PRAGMA_OACC_EXIT_DATA
:
37986 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
37988 case PRAGMA_OACC_HOST_DATA
:
37989 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
37991 case PRAGMA_OACC_KERNELS
:
37992 case PRAGMA_OACC_PARALLEL
:
37993 strcpy (p_name
, "#pragma acc");
37994 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
37997 case PRAGMA_OACC_LOOP
:
37998 strcpy (p_name
, "#pragma acc");
37999 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38002 case PRAGMA_OACC_UPDATE
:
38003 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38005 case PRAGMA_OACC_WAIT
:
38006 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38008 case PRAGMA_OMP_ATOMIC
:
38009 cp_parser_omp_atomic (parser
, pragma_tok
);
38011 case PRAGMA_OMP_CRITICAL
:
38012 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38014 case PRAGMA_OMP_DISTRIBUTE
:
38015 strcpy (p_name
, "#pragma omp");
38016 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38019 case PRAGMA_OMP_FOR
:
38020 strcpy (p_name
, "#pragma omp");
38021 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38024 case PRAGMA_OMP_MASTER
:
38025 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38027 case PRAGMA_OMP_PARALLEL
:
38028 strcpy (p_name
, "#pragma omp");
38029 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38032 case PRAGMA_OMP_SECTIONS
:
38033 strcpy (p_name
, "#pragma omp");
38034 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38036 case PRAGMA_OMP_SIMD
:
38037 strcpy (p_name
, "#pragma omp");
38038 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38041 case PRAGMA_OMP_SINGLE
:
38042 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38044 case PRAGMA_OMP_TASK
:
38045 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38047 case PRAGMA_OMP_TASKGROUP
:
38048 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38050 case PRAGMA_OMP_TASKLOOP
:
38051 strcpy (p_name
, "#pragma omp");
38052 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38055 case PRAGMA_OMP_TEAMS
:
38056 strcpy (p_name
, "#pragma omp");
38057 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38061 gcc_unreachable ();
38064 protected_set_expr_location (stmt
, pragma_tok
->location
);
38067 /* Transactional Memory parsing routines. */
38069 /* Parse a transaction attribute.
38075 We use this instead of cp_parser_attributes_opt for transactions to avoid
38076 the pedwarn in C++98 mode. */
38079 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38082 tree attr_name
, attr
= NULL
;
38084 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38085 return cp_parser_attributes_opt (parser
);
38087 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38089 cp_lexer_consume_token (parser
->lexer
);
38090 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38093 token
= cp_lexer_peek_token (parser
->lexer
);
38094 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38096 token
= cp_lexer_consume_token (parser
->lexer
);
38098 attr_name
= (token
->type
== CPP_KEYWORD
38099 /* For keywords, use the canonical spelling,
38100 not the parsed identifier. */
38101 ? ridpointers
[(int) token
->keyword
]
38103 attr
= build_tree_list (attr_name
, NULL_TREE
);
38106 cp_parser_error (parser
, "expected identifier");
38108 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38110 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38114 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38116 transaction-statement:
38117 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38119 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38123 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38125 unsigned char old_in
= parser
->in_transaction
;
38126 unsigned char this_in
= 1, new_in
;
38127 enum rid keyword
= token
->keyword
;
38128 tree stmt
, attrs
, noex
;
38130 cp_lexer_consume_token (parser
->lexer
);
38132 if (keyword
== RID_TRANSACTION_RELAXED
38133 || keyword
== RID_SYNCHRONIZED
)
38134 this_in
|= TM_STMT_ATTR_RELAXED
;
38137 attrs
= cp_parser_txn_attribute_opt (parser
);
38139 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38142 /* Parse a noexcept specification. */
38143 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38144 noex
= boolean_true_node
;
38145 else if (keyword
== RID_ATOMIC_CANCEL
)
38147 /* cancel-and-throw is unimplemented. */
38148 sorry ("atomic_cancel");
38152 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38154 /* Keep track if we're in the lexical scope of an outer transaction. */
38155 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38157 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38159 parser
->in_transaction
= new_in
;
38160 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38161 parser
->in_transaction
= old_in
;
38163 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38168 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38170 transaction-expression:
38171 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38172 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38176 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38178 unsigned char old_in
= parser
->in_transaction
;
38179 unsigned char this_in
= 1;
38183 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38185 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38186 || keyword
== RID_TRANSACTION_RELAXED
);
38190 keyword
== RID_TRANSACTION_RELAXED
38191 ? G_("%<__transaction_relaxed%> without transactional memory "
38193 : G_("%<__transaction_atomic%> without transactional memory "
38194 "support enabled"));
38196 token
= cp_parser_require_keyword (parser
, keyword
,
38197 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38198 : RT_TRANSACTION_RELAXED
));
38199 gcc_assert (token
!= NULL
);
38201 if (keyword
== RID_TRANSACTION_RELAXED
)
38202 this_in
|= TM_STMT_ATTR_RELAXED
;
38204 /* Set this early. This might mean that we allow transaction_cancel in
38205 an expression that we find out later actually has to be a constexpr.
38206 However, we expect that cxx_constant_value will be able to deal with
38207 this; also, if the noexcept has no constexpr, then what we parse next
38208 really is a transaction's body. */
38209 parser
->in_transaction
= this_in
;
38211 /* Parse a noexcept specification. */
38212 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38215 if (!noex
|| !noex_expr
38216 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38218 matching_parens parens
;
38219 parens
.require_open (parser
);
38221 expr
= cp_parser_expression (parser
);
38222 expr
= finish_parenthesized_expr (expr
);
38224 parens
.require_close (parser
);
38228 /* The only expression that is available got parsed for the noexcept
38229 already. noexcept is true then. */
38231 noex
= boolean_true_node
;
38234 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38235 parser
->in_transaction
= old_in
;
38237 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38238 return error_mark_node
;
38240 return (flag_tm
? expr
: error_mark_node
);
38243 /* Parse a function-transaction-block.
38245 function-transaction-block:
38246 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38248 __transaction_atomic txn-attribute[opt] function-try-block
38249 __transaction_relaxed ctor-initializer[opt] function-body
38250 __transaction_relaxed function-try-block
38254 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38256 unsigned char old_in
= parser
->in_transaction
;
38257 unsigned char new_in
= 1;
38258 tree compound_stmt
, stmt
, attrs
;
38261 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38262 || keyword
== RID_TRANSACTION_RELAXED
);
38263 token
= cp_parser_require_keyword (parser
, keyword
,
38264 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38265 : RT_TRANSACTION_RELAXED
));
38266 gcc_assert (token
!= NULL
);
38268 if (keyword
== RID_TRANSACTION_RELAXED
)
38269 new_in
|= TM_STMT_ATTR_RELAXED
;
38272 attrs
= cp_parser_txn_attribute_opt (parser
);
38274 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38277 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38279 parser
->in_transaction
= new_in
;
38281 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38282 cp_parser_function_try_block (parser
);
38284 cp_parser_ctor_initializer_opt_and_function_body
38285 (parser
, /*in_function_try_block=*/false);
38287 parser
->in_transaction
= old_in
;
38289 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38292 /* Parse a __transaction_cancel statement.
38295 __transaction_cancel txn-attribute[opt] ;
38296 __transaction_cancel txn-attribute[opt] throw-expression ;
38298 ??? Cancel and throw is not yet implemented. */
38301 cp_parser_transaction_cancel (cp_parser
*parser
)
38304 bool is_outer
= false;
38307 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38308 RT_TRANSACTION_CANCEL
);
38309 gcc_assert (token
!= NULL
);
38311 attrs
= cp_parser_txn_attribute_opt (parser
);
38313 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38315 /* ??? Parse cancel-and-throw here. */
38317 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38321 error_at (token
->location
, "%<__transaction_cancel%> without "
38322 "transactional memory support enabled");
38323 return error_mark_node
;
38325 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38327 error_at (token
->location
, "%<__transaction_cancel%> within a "
38328 "%<__transaction_relaxed%>");
38329 return error_mark_node
;
38333 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38334 && !is_tm_may_cancel_outer (current_function_decl
))
38336 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38337 "within outer %<__transaction_atomic%>");
38338 error_at (token
->location
,
38339 " or a %<transaction_may_cancel_outer%> function");
38340 return error_mark_node
;
38343 else if (parser
->in_transaction
== 0)
38345 error_at (token
->location
, "%<__transaction_cancel%> not within "
38346 "%<__transaction_atomic%>");
38347 return error_mark_node
;
38350 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38358 static GTY (()) cp_parser
*the_parser
;
38361 /* Special handling for the first token or line in the file. The first
38362 thing in the file might be #pragma GCC pch_preprocess, which loads a
38363 PCH file, which is a GC collection point. So we need to handle this
38364 first pragma without benefit of an existing lexer structure.
38366 Always returns one token to the caller in *FIRST_TOKEN. This is
38367 either the true first token of the file, or the first token after
38368 the initial pragma. */
38371 cp_parser_initial_pragma (cp_token
*first_token
)
38375 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38376 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38379 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38380 if (first_token
->type
== CPP_STRING
)
38382 name
= first_token
->u
.value
;
38384 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38385 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38386 error_at (first_token
->location
,
38387 "junk at end of %<#pragma GCC pch_preprocess%>");
38390 error_at (first_token
->location
, "expected string literal");
38392 /* Skip to the end of the pragma. */
38393 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38394 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38396 /* Now actually load the PCH file. */
38398 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38400 /* Read one more token to return to our caller. We have to do this
38401 after reading the PCH file in, since its pointers have to be
38403 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38406 /* Parse a pragma GCC ivdep. */
38409 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38411 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38415 /* Parse a pragma GCC unroll. */
38417 static unsigned short
38418 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38420 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38421 tree expr
= cp_parser_constant_expression (parser
);
38422 unsigned short unroll
;
38423 expr
= maybe_constant_value (expr
);
38424 HOST_WIDE_INT lunroll
= 0;
38425 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38426 || TREE_CODE (expr
) != INTEGER_CST
38427 || (lunroll
= tree_to_shwi (expr
)) < 0
38428 || lunroll
>= USHRT_MAX
)
38430 error_at (location
, "%<#pragma GCC unroll%> requires an"
38431 " assignment-expression that evaluates to a non-negative"
38432 " integral constant less than %u", USHRT_MAX
);
38437 unroll
= (unsigned short)lunroll
;
38441 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38445 /* Normal parsing of a pragma token. Here we can (and must) use the
38449 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38451 cp_token
*pragma_tok
;
38456 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38457 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38458 parser
->lexer
->in_pragma
= true;
38460 id
= cp_parser_pragma_kind (pragma_tok
);
38461 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38462 cp_ensure_no_omp_declare_simd (parser
);
38465 case PRAGMA_GCC_PCH_PREPROCESS
:
38466 error_at (pragma_tok
->location
,
38467 "%<#pragma GCC pch_preprocess%> must be first");
38470 case PRAGMA_OMP_BARRIER
:
38473 case pragma_compound
:
38474 cp_parser_omp_barrier (parser
, pragma_tok
);
38477 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38478 "used in compound statements", "omp barrier");
38485 case PRAGMA_OMP_FLUSH
:
38488 case pragma_compound
:
38489 cp_parser_omp_flush (parser
, pragma_tok
);
38492 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38493 "used in compound statements", "omp flush");
38500 case PRAGMA_OMP_TASKWAIT
:
38503 case pragma_compound
:
38504 cp_parser_omp_taskwait (parser
, pragma_tok
);
38507 error_at (pragma_tok
->location
,
38508 "%<#pragma %s%> may only be used in compound statements",
38516 case PRAGMA_OMP_TASKYIELD
:
38519 case pragma_compound
:
38520 cp_parser_omp_taskyield (parser
, pragma_tok
);
38523 error_at (pragma_tok
->location
,
38524 "%<#pragma %s%> may only be used in compound statements",
38532 case PRAGMA_OMP_CANCEL
:
38535 case pragma_compound
:
38536 cp_parser_omp_cancel (parser
, pragma_tok
);
38539 error_at (pragma_tok
->location
,
38540 "%<#pragma %s%> may only be used in compound statements",
38548 case PRAGMA_OMP_CANCELLATION_POINT
:
38549 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38552 case PRAGMA_OMP_THREADPRIVATE
:
38553 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38556 case PRAGMA_OMP_DECLARE
:
38557 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38559 case PRAGMA_OACC_DECLARE
:
38560 cp_parser_oacc_declare (parser
, pragma_tok
);
38563 case PRAGMA_OACC_ENTER_DATA
:
38564 if (context
== pragma_stmt
)
38566 error_at (pragma_tok
->location
,
38567 "%<#pragma %s%> may only be used in compound statements",
38571 else if (context
!= pragma_compound
)
38573 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38576 case PRAGMA_OACC_EXIT_DATA
:
38577 if (context
== pragma_stmt
)
38579 error_at (pragma_tok
->location
,
38580 "%<#pragma %s%> may only be used in compound statements",
38584 else if (context
!= pragma_compound
)
38586 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38589 case PRAGMA_OACC_ROUTINE
:
38590 if (context
!= pragma_external
)
38592 error_at (pragma_tok
->location
,
38593 "%<#pragma acc routine%> must be at file scope");
38596 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38599 case PRAGMA_OACC_UPDATE
:
38600 if (context
== pragma_stmt
)
38602 error_at (pragma_tok
->location
,
38603 "%<#pragma %s%> may only be used in compound statements",
38607 else if (context
!= pragma_compound
)
38609 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38612 case PRAGMA_OACC_WAIT
:
38613 if (context
== pragma_stmt
)
38615 error_at (pragma_tok
->location
,
38616 "%<#pragma %s%> may only be used in compound statements",
38620 else if (context
!= pragma_compound
)
38622 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38625 case PRAGMA_OACC_ATOMIC
:
38626 case PRAGMA_OACC_CACHE
:
38627 case PRAGMA_OACC_DATA
:
38628 case PRAGMA_OACC_HOST_DATA
:
38629 case PRAGMA_OACC_KERNELS
:
38630 case PRAGMA_OACC_PARALLEL
:
38631 case PRAGMA_OACC_LOOP
:
38632 case PRAGMA_OMP_ATOMIC
:
38633 case PRAGMA_OMP_CRITICAL
:
38634 case PRAGMA_OMP_DISTRIBUTE
:
38635 case PRAGMA_OMP_FOR
:
38636 case PRAGMA_OMP_MASTER
:
38637 case PRAGMA_OMP_PARALLEL
:
38638 case PRAGMA_OMP_SECTIONS
:
38639 case PRAGMA_OMP_SIMD
:
38640 case PRAGMA_OMP_SINGLE
:
38641 case PRAGMA_OMP_TASK
:
38642 case PRAGMA_OMP_TASKGROUP
:
38643 case PRAGMA_OMP_TASKLOOP
:
38644 case PRAGMA_OMP_TEAMS
:
38645 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38647 stmt
= push_omp_privatization_clauses (false);
38648 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38649 pop_omp_privatization_clauses (stmt
);
38652 case PRAGMA_OMP_ORDERED
:
38653 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38655 stmt
= push_omp_privatization_clauses (false);
38656 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38657 pop_omp_privatization_clauses (stmt
);
38660 case PRAGMA_OMP_TARGET
:
38661 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38663 stmt
= push_omp_privatization_clauses (false);
38664 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38665 pop_omp_privatization_clauses (stmt
);
38668 case PRAGMA_OMP_END_DECLARE_TARGET
:
38669 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38672 case PRAGMA_OMP_SECTION
:
38673 error_at (pragma_tok
->location
,
38674 "%<#pragma omp section%> may only be used in "
38675 "%<#pragma omp sections%> construct");
38680 if (context
== pragma_external
)
38682 error_at (pragma_tok
->location
,
38683 "%<#pragma GCC ivdep%> must be inside a function");
38686 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38687 unsigned short unroll
;
38688 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38689 if (tok
->type
== CPP_PRAGMA
38690 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38692 tok
= cp_lexer_consume_token (parser
->lexer
);
38693 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38694 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38698 if (tok
->type
!= CPP_KEYWORD
38699 || (tok
->keyword
!= RID_FOR
38700 && tok
->keyword
!= RID_WHILE
38701 && tok
->keyword
!= RID_DO
))
38703 cp_parser_error (parser
, "for, while or do statement expected");
38706 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38710 case PRAGMA_UNROLL
:
38712 if (context
== pragma_external
)
38714 error_at (pragma_tok
->location
,
38715 "%<#pragma GCC unroll%> must be inside a function");
38718 const unsigned short unroll
38719 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38721 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38722 if (tok
->type
== CPP_PRAGMA
38723 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38725 tok
= cp_lexer_consume_token (parser
->lexer
);
38726 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38727 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38731 if (tok
->type
!= CPP_KEYWORD
38732 || (tok
->keyword
!= RID_FOR
38733 && tok
->keyword
!= RID_WHILE
38734 && tok
->keyword
!= RID_DO
))
38736 cp_parser_error (parser
, "for, while or do statement expected");
38739 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38744 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38745 c_invoke_pragma_handler (id
);
38749 cp_parser_error (parser
, "expected declaration specifiers");
38753 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38757 /* The interface the pragma parsers have to the lexer. */
38760 pragma_lex (tree
*value
, location_t
*loc
)
38762 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38763 enum cpp_ttype ret
= tok
->type
;
38765 *value
= tok
->u
.value
;
38767 *loc
= tok
->location
;
38769 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38771 else if (ret
== CPP_STRING
)
38772 *value
= cp_parser_string_literal (the_parser
, false, false);
38775 if (ret
== CPP_KEYWORD
)
38777 cp_lexer_consume_token (the_parser
->lexer
);
38784 /* External interface. */
38786 /* Parse one entire translation unit. */
38789 c_parse_file (void)
38791 static bool already_called
= false;
38793 if (already_called
)
38794 fatal_error (input_location
,
38795 "inter-module optimizations not implemented for C++");
38796 already_called
= true;
38798 the_parser
= cp_parser_new ();
38799 push_deferring_access_checks (flag_access_control
38800 ? dk_no_deferred
: dk_no_check
);
38801 cp_parser_translation_unit (the_parser
);
38805 /* Create an identifier for a generic parameter type (a synthesized
38806 template parameter implied by `auto' or a concept identifier). */
38808 static GTY(()) int generic_parm_count
;
38810 make_generic_type_name ()
38813 sprintf (buf
, "auto:%d", ++generic_parm_count
);
38814 return get_identifier (buf
);
38817 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38818 (creating a new template parameter list if necessary). Returns the newly
38819 created template type parm. */
38822 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
38824 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
38826 /* Before committing to modifying any scope, if we're in an
38827 implicit template scope, and we're trying to synthesize a
38828 constrained parameter, try to find a previous parameter with
38829 the same name. This is the same-type rule for abbreviated
38830 function templates.
38832 NOTE: We can generate implicit parameters when tentatively
38833 parsing a nested name specifier, only to reject that parse
38834 later. However, matching the same template-id as part of a
38835 direct-declarator should generate an identical template
38836 parameter, so this rule will merge them. */
38837 if (parser
->implicit_template_scope
&& constr
)
38839 tree t
= parser
->implicit_template_parms
;
38842 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
38844 tree d
= TREE_VALUE (t
);
38845 if (TREE_CODE (d
) == PARM_DECL
)
38846 /* Return the TEMPLATE_PARM_INDEX. */
38847 d
= DECL_INITIAL (d
);
38850 t
= TREE_CHAIN (t
);
38854 /* We are either continuing a function template that already contains implicit
38855 template parameters, creating a new fully-implicit function template, or
38856 extending an existing explicit function template with implicit template
38859 cp_binding_level
*const entry_scope
= current_binding_level
;
38861 bool become_template
= false;
38862 cp_binding_level
*parent_scope
= 0;
38864 if (parser
->implicit_template_scope
)
38866 gcc_assert (parser
->implicit_template_parms
);
38868 current_binding_level
= parser
->implicit_template_scope
;
38872 /* Roll back to the existing template parameter scope (in the case of
38873 extending an explicit function template) or introduce a new template
38874 parameter scope ahead of the function parameter scope (or class scope
38875 in the case of out-of-line member definitions). The function scope is
38876 added back after template parameter synthesis below. */
38878 cp_binding_level
*scope
= entry_scope
;
38880 while (scope
->kind
== sk_function_parms
)
38882 parent_scope
= scope
;
38883 scope
= scope
->level_chain
;
38885 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
38887 /* If not defining a class, then any class scope is a scope level in
38888 an out-of-line member definition. In this case simply wind back
38889 beyond the first such scope to inject the template parameter list.
38890 Otherwise wind back to the class being defined. The latter can
38891 occur in class member friend declarations such as:
38897 friend void A::foo (auto);
38900 The template parameter list synthesized for the friend declaration
38901 must be injected in the scope of 'B'. This can also occur in
38902 erroneous cases such as:
38908 void B::foo (auto) {}
38911 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38912 but, nevertheless, the template parameter list synthesized for the
38913 declarator should be injected into the scope of 'A' as if the
38914 ill-formed template was specified explicitly. */
38916 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
38918 parent_scope
= scope
;
38919 scope
= scope
->level_chain
;
38923 current_binding_level
= scope
;
38925 if (scope
->kind
!= sk_template_parms
38926 || !function_being_declared_is_template_p (parser
))
38928 /* Introduce a new template parameter list for implicit template
38931 become_template
= true;
38933 parser
->implicit_template_scope
38934 = begin_scope (sk_template_parms
, NULL
);
38936 ++processing_template_decl
;
38938 parser
->fully_implicit_function_template_p
= true;
38939 ++parser
->num_template_parameter_lists
;
38943 /* Synthesize implicit template parameters at the end of the explicit
38944 template parameter list. */
38946 gcc_assert (current_template_parms
);
38948 parser
->implicit_template_scope
= scope
;
38950 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38951 parser
->implicit_template_parms
38952 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
38956 /* Synthesize a new template parameter and track the current template
38957 parameter chain with implicit_template_parms. */
38959 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
38960 tree synth_id
= make_generic_type_name ();
38961 tree synth_tmpl_parm
;
38962 bool non_type
= false;
38964 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
38966 = finish_template_type_parm (class_type_node
, synth_id
);
38967 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
38969 = finish_constrained_template_template_parm (proto
, synth_id
);
38972 synth_tmpl_parm
= copy_decl (proto
);
38973 DECL_NAME (synth_tmpl_parm
) = synth_id
;
38977 // Attach the constraint to the parm before processing.
38978 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
38979 TREE_TYPE (node
) = constr
;
38981 = process_template_parm (parser
->implicit_template_parms
,
38984 /*non_type=*/non_type
,
38985 /*param_pack=*/false);
38987 // Chain the new parameter to the list of implicit parameters.
38988 if (parser
->implicit_template_parms
)
38989 parser
->implicit_template_parms
38990 = TREE_CHAIN (parser
->implicit_template_parms
);
38992 parser
->implicit_template_parms
= new_parm
;
38994 tree new_decl
= get_local_decls ();
38996 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38997 new_decl
= DECL_INITIAL (new_decl
);
38999 /* If creating a fully implicit function template, start the new implicit
39000 template parameter list with this synthesized type, otherwise grow the
39001 current template parameter list. */
39003 if (become_template
)
39005 parent_scope
->level_chain
= current_binding_level
;
39007 tree new_parms
= make_tree_vec (1);
39008 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39009 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39010 new_parms
, current_template_parms
);
39014 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39015 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39016 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39017 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39020 // If the new parameter was constrained, we need to add that to the
39021 // constraints in the template parameter list.
39022 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39024 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39025 reqs
= conjoin_constraints (reqs
, req
);
39026 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39029 current_binding_level
= entry_scope
;
39034 /* Finish the declaration of a fully implicit function template. Such a
39035 template has no explicit template parameter list so has not been through the
39036 normal template head and tail processing. synthesize_implicit_template_parm
39037 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39038 provided if the declaration is a class member such that its template
39039 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39040 form is returned. Otherwise NULL_TREE is returned. */
39043 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39045 gcc_assert (parser
->fully_implicit_function_template_p
);
39047 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39048 && DECL_VIRTUAL_P (member_decl_opt
))
39050 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39051 "implicit templates may not be %<virtual%>");
39052 DECL_VIRTUAL_P (member_decl_opt
) = false;
39055 if (member_decl_opt
)
39056 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39057 end_template_decl ();
39059 parser
->fully_implicit_function_template_p
= false;
39060 --parser
->num_template_parameter_lists
;
39062 return member_decl_opt
;
39065 /* Helper function for diagnostics that have complained about things
39066 being used with 'extern "C"' linkage.
39068 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39071 maybe_show_extern_c_location (void)
39073 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39074 inform (the_parser
->innermost_linkage_specification_location
,
39075 "%<extern \"C\"%> linkage started here");
39078 #include "gt-cp-parser.h"