2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "stringpool.h"
29 #include "print-tree.h"
31 #include "trans-mem.h"
34 #include "c-family/c-objc.h"
36 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token
=
51 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant
{
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 /* a transaction expression */
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error
{
119 /* is not a class or namespace */
121 /* is not a class, namespace, or enumeration */
125 /* The various kinds of required token */
126 enum required_token
{
128 RT_SEMICOLON
, /* ';' */
129 RT_OPEN_PAREN
, /* '(' */
130 RT_CLOSE_BRACE
, /* '}' */
131 RT_OPEN_BRACE
, /* '{' */
132 RT_CLOSE_SQUARE
, /* ']' */
133 RT_OPEN_SQUARE
, /* '[' */
137 RT_GREATER
, /* '>' */
139 RT_ELLIPSIS
, /* '...' */
143 RT_COLON_SCOPE
, /* ':' or '::' */
144 RT_CLOSE_PAREN
, /* ')' */
145 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
146 RT_PRAGMA_EOL
, /* end of line */
147 RT_NAME
, /* identifier */
149 /* The type is CPP_KEYWORD */
151 RT_DELETE
, /* delete */
152 RT_RETURN
, /* return */
153 RT_WHILE
, /* while */
154 RT_EXTERN
, /* extern */
155 RT_STATIC_ASSERT
, /* static_assert */
156 RT_DECLTYPE
, /* decltype */
157 RT_OPERATOR
, /* operator */
158 RT_CLASS
, /* class */
159 RT_TEMPLATE
, /* template */
160 RT_NAMESPACE
, /* namespace */
161 RT_USING
, /* using */
164 RT_CATCH
, /* catch */
165 RT_THROW
, /* throw */
166 RT_LABEL
, /* __label__ */
167 RT_AT_TRY
, /* @try */
168 RT_AT_SYNCHRONIZED
, /* @synchronized */
169 RT_AT_THROW
, /* @throw */
171 RT_SELECT
, /* selection-statement */
172 RT_INTERATION
, /* iteration-statement */
173 RT_JUMP
, /* jump-statement */
174 RT_CLASS_KEY
, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
183 static cp_lexer
*cp_lexer_new_main
185 static cp_lexer
*cp_lexer_new_from_tokens
186 (cp_token_cache
*tokens
);
187 static void cp_lexer_destroy
189 static int cp_lexer_saving_tokens
191 static cp_token
*cp_lexer_token_at
192 (cp_lexer
*, cp_token_position
);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer
*, cp_token
*);
195 static inline cp_token
*cp_lexer_peek_token
197 static cp_token
*cp_lexer_peek_nth_token
198 (cp_lexer
*, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer
*, enum cpp_ttype
);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer
*, enum cpp_ttype
);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer
*, enum rid
);
205 static cp_token
*cp_lexer_consume_token
207 static void cp_lexer_purge_token
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer
*, cp_token_position
);
211 static void cp_lexer_save_tokens
213 static void cp_lexer_commit_tokens
215 static void cp_lexer_rollback_tokens
217 static void cp_lexer_print_token
218 (FILE *, cp_token
*);
219 static inline bool cp_lexer_debugging_p
221 static void cp_lexer_start_debugging
222 (cp_lexer
*) ATTRIBUTE_UNUSED
;
223 static void cp_lexer_stop_debugging
224 (cp_lexer
*) ATTRIBUTE_UNUSED
;
226 static cp_token_cache
*cp_token_cache_new
227 (cp_token
*, cp_token
*);
229 static void cp_parser_initial_pragma
232 static tree cp_literal_operator_id
235 static void cp_parser_cilk_simd
236 (cp_parser
*, cp_token
*);
237 static tree cp_parser_cilk_for
239 static bool cp_parser_omp_declare_reduction_exprs
241 static tree cp_parser_cilk_simd_vectorlength
242 (cp_parser
*, tree
, bool);
243 static void cp_finalize_oacc_routine
244 (cp_parser
*, tree
, bool, bool);
246 /* Manifest constants. */
247 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
248 #define CP_SAVED_TOKEN_STACK 5
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream
;
255 /* Nonzero if we are parsing an unevaluated operand: an operand to
256 sizeof, typeof, or alignof. */
257 int cp_unevaluated_operand
;
259 /* Dump up to NUM tokens in BUFFER to FILE starting with token
260 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
261 first token in BUFFER. If NUM is 0, dump all the tokens. If
262 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
263 highlighted by surrounding it in [[ ]]. */
266 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
267 cp_token
*start_token
, unsigned num
,
268 cp_token
*curr_token
)
270 unsigned i
, nprinted
;
274 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
280 num
= buffer
->length ();
282 if (start_token
== NULL
)
283 start_token
= buffer
->address ();
285 if (start_token
> buffer
->address ())
287 cp_lexer_print_token (file
, &(*buffer
)[0]);
288 fprintf (file
, " ... ");
293 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
295 if (token
== start_token
)
302 if (token
== curr_token
)
303 fprintf (file
, "[[");
305 cp_lexer_print_token (file
, token
);
307 if (token
== curr_token
)
308 fprintf (file
, "]]");
314 case CPP_CLOSE_BRACE
:
324 if (i
== num
&& i
< buffer
->length ())
326 fprintf (file
, " ... ");
327 cp_lexer_print_token (file
, &buffer
->last ());
330 fprintf (file
, "\n");
334 /* Dump all tokens in BUFFER to stderr. */
337 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
339 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
343 debug (vec
<cp_token
, va_gc
> &ref
)
345 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
349 debug (vec
<cp_token
, va_gc
> *ptr
)
354 fprintf (stderr
, "<nil>\n");
358 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
359 description for T. */
362 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
366 fprintf (file
, "%s: ", desc
);
367 print_node_brief (file
, "", t
, 0);
372 /* Dump parser context C to FILE. */
375 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
377 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
378 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
379 print_node_brief (file
, "", c
->object_type
, 0);
380 fprintf (file
, "}\n");
384 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
390 cp_parser_context
*c
;
392 fprintf (file
, "Parsing context stack:\n");
393 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
395 fprintf (file
, "\t#%u: ", i
);
396 cp_debug_print_context (file
, c
);
401 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
407 fprintf (file
, "%s: true\n", desc
);
411 /* Print an unparsed function entry UF to FILE. */
414 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
417 cp_default_arg_entry
*default_arg_fn
;
420 fprintf (file
, "\tFunctions with default args:\n");
422 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
425 fprintf (file
, "\t\tClass type: ");
426 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
427 fprintf (file
, "\t\tDeclaration: ");
428 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
429 fprintf (file
, "\n");
432 fprintf (file
, "\n\tFunctions with definitions that require "
433 "post-processing\n\t\t");
434 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
436 print_node_brief (file
, "", fn
, 0);
439 fprintf (file
, "\n");
441 fprintf (file
, "\n\tNon-static data members with initializers that require "
442 "post-processing\n\t\t");
443 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
445 print_node_brief (file
, "", fn
, 0);
448 fprintf (file
, "\n");
452 /* Print the stack of unparsed member functions S to FILE. */
455 cp_debug_print_unparsed_queues (FILE *file
,
456 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
459 cp_unparsed_functions_entry
*uf
;
461 fprintf (file
, "Unparsed functions\n");
462 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
464 fprintf (file
, "#%u:\n", i
);
465 cp_debug_print_unparsed_function (file
, uf
);
470 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
471 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
476 cp_token
*next_token
, *first_token
, *start_token
;
481 next_token
= parser
->lexer
->next_token
;
482 first_token
= parser
->lexer
->buffer
->address ();
483 start_token
= (next_token
> first_token
+ window_size
/ 2)
484 ? next_token
- window_size
/ 2
486 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
491 /* Dump debugging information for the given PARSER. If FILE is NULL,
492 the output is printed on stderr. */
495 cp_debug_parser (FILE *file
, cp_parser
*parser
)
497 const size_t window_size
= 20;
499 expanded_location eloc
;
504 fprintf (file
, "Parser state\n\n");
505 fprintf (file
, "Number of tokens: %u\n",
506 vec_safe_length (parser
->lexer
->buffer
));
507 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
508 cp_debug_print_tree_if_set (file
, "Object scope",
509 parser
->object_scope
);
510 cp_debug_print_tree_if_set (file
, "Qualifying scope",
511 parser
->qualifying_scope
);
512 cp_debug_print_context_stack (file
, parser
->context
);
513 cp_debug_print_flag (file
, "Allow GNU extensions",
514 parser
->allow_gnu_extensions_p
);
515 cp_debug_print_flag (file
, "'>' token is greater-than",
516 parser
->greater_than_is_operator_p
);
517 cp_debug_print_flag (file
, "Default args allowed in current "
518 "parameter list", parser
->default_arg_ok_p
);
519 cp_debug_print_flag (file
, "Parsing integral constant-expression",
520 parser
->integral_constant_expression_p
);
521 cp_debug_print_flag (file
, "Allow non-constant expression in current "
522 "constant-expression",
523 parser
->allow_non_integral_constant_expression_p
);
524 cp_debug_print_flag (file
, "Seen non-constant expression",
525 parser
->non_integral_constant_expression_p
);
526 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
528 parser
->local_variables_forbidden_p
);
529 cp_debug_print_flag (file
, "In unbraced linkage specification",
530 parser
->in_unbraced_linkage_specification_p
);
531 cp_debug_print_flag (file
, "Parsing a declarator",
532 parser
->in_declarator_p
);
533 cp_debug_print_flag (file
, "In template argument list",
534 parser
->in_template_argument_list_p
);
535 cp_debug_print_flag (file
, "Parsing an iteration statement",
536 parser
->in_statement
& IN_ITERATION_STMT
);
537 cp_debug_print_flag (file
, "Parsing a switch statement",
538 parser
->in_statement
& IN_SWITCH_STMT
);
539 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
540 parser
->in_statement
& IN_OMP_BLOCK
);
541 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
542 parser
->in_statement
& IN_CILK_SIMD_FOR
);
543 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
544 parser
->in_statement
& IN_OMP_FOR
);
545 cp_debug_print_flag (file
, "Parsing an if statement",
546 parser
->in_statement
& IN_IF_STMT
);
547 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
548 "context", parser
->in_type_id_in_expr_p
);
549 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
550 parser
->implicit_extern_c
);
551 cp_debug_print_flag (file
, "String expressions should be translated "
552 "to execution character set",
553 parser
->translate_strings_p
);
554 cp_debug_print_flag (file
, "Parsing function body outside of a "
555 "local class", parser
->in_function_body
);
556 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
557 parser
->colon_corrects_to_scope_p
);
558 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
559 parser
->colon_doesnt_start_class_def_p
);
560 if (parser
->type_definition_forbidden_message
)
561 fprintf (file
, "Error message for forbidden type definitions: %s\n",
562 parser
->type_definition_forbidden_message
);
563 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
564 fprintf (file
, "Number of class definitions in progress: %u\n",
565 parser
->num_classes_being_defined
);
566 fprintf (file
, "Number of template parameter lists for the current "
567 "declaration: %u\n", parser
->num_template_parameter_lists
);
568 cp_debug_parser_tokens (file
, parser
, window_size
);
569 token
= parser
->lexer
->next_token
;
570 fprintf (file
, "Next token to parse:\n");
571 fprintf (file
, "\tToken: ");
572 cp_lexer_print_token (file
, token
);
573 eloc
= expand_location (token
->location
);
574 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
575 fprintf (file
, "\tLine: %d\n", eloc
.line
);
576 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
580 debug (cp_parser
&ref
)
582 cp_debug_parser (stderr
, &ref
);
586 debug (cp_parser
*ptr
)
591 fprintf (stderr
, "<nil>\n");
594 /* Allocate memory for a new lexer object and return it. */
597 cp_lexer_alloc (void)
601 c_common_no_more_pch ();
603 /* Allocate the memory. */
604 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
606 /* Initially we are not debugging. */
607 lexer
->debugging_p
= false;
609 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
611 /* Create the buffer. */
612 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
618 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 cp_lexer_new_main (void)
627 /* It's possible that parsing the first pragma will load a PCH file,
628 which is a GC collection point. So we have to do that before
629 allocating any memory. */
630 cp_parser_initial_pragma (&token
);
632 lexer
= cp_lexer_alloc ();
634 /* Put the first token in the buffer. */
635 lexer
->buffer
->quick_push (token
);
637 /* Get the remaining tokens from the preprocessor. */
638 while (token
.type
!= CPP_EOF
)
640 cp_lexer_get_preprocessor_token (lexer
, &token
);
641 vec_safe_push (lexer
->buffer
, token
);
644 lexer
->last_token
= lexer
->buffer
->address ()
645 + lexer
->buffer
->length ()
647 lexer
->next_token
= lexer
->buffer
->length ()
648 ? lexer
->buffer
->address ()
651 /* Subsequent preprocessor diagnostics should use compiler
652 diagnostic functions to get the compiler source location. */
655 gcc_assert (!lexer
->next_token
->purged_p
);
659 /* Create a new lexer whose token stream is primed with the tokens in
660 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
665 cp_token
*first
= cache
->first
;
666 cp_token
*last
= cache
->last
;
667 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
669 /* We do not own the buffer. */
670 lexer
->buffer
= NULL
;
671 lexer
->next_token
= first
== last
? &eof_token
: first
;
672 lexer
->last_token
= last
;
674 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
676 /* Initially we are not debugging. */
677 lexer
->debugging_p
= false;
679 gcc_assert (!lexer
->next_token
->purged_p
);
683 /* Frees all resources associated with LEXER. */
686 cp_lexer_destroy (cp_lexer
*lexer
)
688 vec_free (lexer
->buffer
);
689 lexer
->saved_tokens
.release ();
693 /* Returns nonzero if debugging information should be output. */
696 cp_lexer_debugging_p (cp_lexer
*lexer
)
698 return lexer
->debugging_p
;
702 static inline cp_token_position
703 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
705 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
707 return lexer
->next_token
- previous_p
;
710 static inline cp_token
*
711 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
717 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
719 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
722 static inline cp_token_position
723 cp_lexer_previous_token_position (cp_lexer
*lexer
)
725 if (lexer
->next_token
== &eof_token
)
726 return lexer
->last_token
- 1;
728 return cp_lexer_token_position (lexer
, true);
731 static inline cp_token
*
732 cp_lexer_previous_token (cp_lexer
*lexer
)
734 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
736 return cp_lexer_token_at (lexer
, tp
);
739 /* nonzero if we are presently saving tokens. */
742 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
744 return lexer
->saved_tokens
.length () != 0;
747 /* Store the next token from the preprocessor in *TOKEN. Return true
748 if we reach EOF. If LEXER is NULL, assume we are handling an
749 initial #pragma pch_preprocess, and thus want the lexer to return
750 processed strings. */
753 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
755 static int is_extern_c
= 0;
757 /* Get a new token from the preprocessor. */
759 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
760 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
761 token
->keyword
= RID_MAX
;
762 token
->pragma_kind
= PRAGMA_NONE
;
763 token
->purged_p
= false;
764 token
->error_reported
= false;
766 /* On some systems, some header files are surrounded by an
767 implicit extern "C" block. Set a flag in the token if it
768 comes from such a header. */
769 is_extern_c
+= pending_lang_change
;
770 pending_lang_change
= 0;
771 token
->implicit_extern_c
= is_extern_c
> 0;
773 /* Check to see if this token is a keyword. */
774 if (token
->type
== CPP_NAME
)
776 if (C_IS_RESERVED_WORD (token
->u
.value
))
778 /* Mark this token as a keyword. */
779 token
->type
= CPP_KEYWORD
;
780 /* Record which keyword. */
781 token
->keyword
= C_RID_CODE (token
->u
.value
);
785 if (warn_cxx11_compat
786 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
787 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
789 /* Warn about the C++0x keyword (but still treat it as
791 warning (OPT_Wc__11_compat
,
792 "identifier %qE is a keyword in C++11",
795 /* Clear out the C_RID_CODE so we don't warn about this
796 particular identifier-turned-keyword again. */
797 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
800 token
->keyword
= RID_MAX
;
803 else if (token
->type
== CPP_AT_NAME
)
805 /* This only happens in Objective-C++; it must be a keyword. */
806 token
->type
= CPP_KEYWORD
;
807 switch (C_RID_CODE (token
->u
.value
))
809 /* Replace 'class' with '@class', 'private' with '@private',
810 etc. This prevents confusion with the C++ keyword
811 'class', and makes the tokens consistent with other
812 Objective-C 'AT' keywords. For example '@class' is
813 reported as RID_AT_CLASS which is consistent with
814 '@synchronized', which is reported as
817 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
818 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
819 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
820 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
821 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
822 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
823 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
824 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
825 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
828 else if (token
->type
== CPP_PRAGMA
)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token
->pragma_kind
= ((enum pragma_kind
)
832 TREE_INT_CST_LOW (token
->u
.value
));
833 token
->u
.value
= NULL_TREE
;
837 /* Update the globals input_location and the input file stack from TOKEN. */
839 cp_lexer_set_source_position_from_token (cp_token
*token
)
841 if (token
->type
!= CPP_EOF
)
843 input_location
= token
->location
;
847 /* Update the globals input_location and the input file stack from LEXER. */
849 cp_lexer_set_source_position (cp_lexer
*lexer
)
851 cp_token
*token
= cp_lexer_peek_token (lexer
);
852 cp_lexer_set_source_position_from_token (token
);
855 /* Return a pointer to the next token in the token stream, but do not
858 static inline cp_token
*
859 cp_lexer_peek_token (cp_lexer
*lexer
)
861 if (cp_lexer_debugging_p (lexer
))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
864 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
865 putc ('\n', cp_lexer_debug_stream
);
867 return lexer
->next_token
;
870 /* Return true if the next token has the indicated TYPE. */
873 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
875 return cp_lexer_peek_token (lexer
)->type
== type
;
878 /* Return true if the next token does not have the indicated TYPE. */
881 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
883 return !cp_lexer_next_token_is (lexer
, type
);
886 /* Return true if the next token is the indicated KEYWORD. */
889 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
891 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
895 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
897 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
901 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
903 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
906 /* Return true if the next token is not the indicated KEYWORD. */
909 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
911 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
914 /* Return true if the next token is a keyword for a decl-specifier. */
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
921 token
= cp_lexer_peek_token (lexer
);
922 switch (token
->keyword
)
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
927 /* Storage classes. */
933 /* Elaborated type specifiers. */
939 /* Simple type specifiers. */
953 /* GNU extensions. */
956 /* C++0x extensions. */
958 case RID_UNDERLYING_TYPE
:
962 if (token
->keyword
>= RID_FIRST_INT_N
963 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
964 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
970 /* Returns TRUE iff the token T begins a decltype type. */
973 token_is_decltype (cp_token
*t
)
975 return (t
->keyword
== RID_DECLTYPE
976 || t
->type
== CPP_DECLTYPE
);
979 /* Returns TRUE iff the next token begins a decltype type. */
982 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
984 cp_token
*t
= cp_lexer_peek_token (lexer
);
985 return token_is_decltype (t
);
988 /* Return a pointer to the Nth token in the token stream. If N is 1,
989 then this is precisely equivalent to cp_lexer_peek_token (except
990 that it is not inline). One would like to disallow that case, but
991 there is one case (cp_parser_nth_token_starts_template_id) where
992 the caller passes a variable for N and it might be 1. */
995 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
999 /* N is 1-based, not zero-based. */
1002 if (cp_lexer_debugging_p (lexer
))
1003 fprintf (cp_lexer_debug_stream
,
1004 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1007 token
= lexer
->next_token
;
1008 gcc_assert (!n
|| token
!= &eof_token
);
1012 if (token
== lexer
->last_token
)
1018 if (!token
->purged_p
)
1022 if (cp_lexer_debugging_p (lexer
))
1024 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1025 putc ('\n', cp_lexer_debug_stream
);
1031 /* Return the next token, and advance the lexer's next_token pointer
1032 to point to the next non-purged token. */
1035 cp_lexer_consume_token (cp_lexer
* lexer
)
1037 cp_token
*token
= lexer
->next_token
;
1039 gcc_assert (token
!= &eof_token
);
1040 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1044 lexer
->next_token
++;
1045 if (lexer
->next_token
== lexer
->last_token
)
1047 lexer
->next_token
= &eof_token
;
1052 while (lexer
->next_token
->purged_p
);
1054 cp_lexer_set_source_position_from_token (token
);
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer
))
1059 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1060 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1061 putc ('\n', cp_lexer_debug_stream
);
1067 /* Permanently remove the next token from the token stream, and
1068 advance the next_token pointer to refer to the next non-purged
1072 cp_lexer_purge_token (cp_lexer
*lexer
)
1074 cp_token
*tok
= lexer
->next_token
;
1076 gcc_assert (tok
!= &eof_token
);
1077 tok
->purged_p
= true;
1078 tok
->location
= UNKNOWN_LOCATION
;
1079 tok
->u
.value
= NULL_TREE
;
1080 tok
->keyword
= RID_MAX
;
1085 if (tok
== lexer
->last_token
)
1091 while (tok
->purged_p
);
1092 lexer
->next_token
= tok
;
1095 /* Permanently remove all tokens after TOK, up to, but not
1096 including, the token that will be returned next by
1097 cp_lexer_peek_token. */
1100 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1102 cp_token
*peek
= lexer
->next_token
;
1104 if (peek
== &eof_token
)
1105 peek
= lexer
->last_token
;
1107 gcc_assert (tok
< peek
);
1109 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1111 tok
->purged_p
= true;
1112 tok
->location
= UNKNOWN_LOCATION
;
1113 tok
->u
.value
= NULL_TREE
;
1114 tok
->keyword
= RID_MAX
;
1118 /* Begin saving tokens. All tokens consumed after this point will be
1122 cp_lexer_save_tokens (cp_lexer
* lexer
)
1124 /* Provide debugging output. */
1125 if (cp_lexer_debugging_p (lexer
))
1126 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1128 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1131 /* Commit to the portion of the token stream most recently saved. */
1134 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1136 /* Provide debugging output. */
1137 if (cp_lexer_debugging_p (lexer
))
1138 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1140 lexer
->saved_tokens
.pop ();
1143 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1144 to the token stream. Stop saving tokens. */
1147 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer
))
1151 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1153 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1156 /* RAII wrapper around the above functions, with sanity checking. Creating
1157 a variable saves tokens, which are committed when the variable is
1158 destroyed unless they are explicitly rolled back by calling the rollback
1161 struct saved_token_sentinel
1166 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1168 len
= lexer
->saved_tokens
.length ();
1169 cp_lexer_save_tokens (lexer
);
1173 cp_lexer_rollback_tokens (lexer
);
1176 ~saved_token_sentinel()
1179 cp_lexer_commit_tokens (lexer
);
1180 gcc_assert (lexer
->saved_tokens
.length () == len
);
1184 /* Print a representation of the TOKEN on the STREAM. */
1187 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1189 /* We don't use cpp_type2name here because the parser defines
1190 a few tokens of its own. */
1191 static const char *const token_names
[] = {
1192 /* cpplib-defined token types */
1193 #define OP(e, s) #e,
1194 #define TK(e, s) #e,
1198 /* C++ parser token types - see "Manifest constants", above. */
1201 "NESTED_NAME_SPECIFIER",
1204 /* For some tokens, print the associated data. */
1205 switch (token
->type
)
1208 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1209 For example, `struct' is mapped to an INTEGER_CST. */
1210 if (!identifier_p (token
->u
.value
))
1212 /* else fall through */
1214 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1221 case CPP_UTF8STRING
:
1222 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1226 print_generic_expr (stream
, token
->u
.value
, 0);
1230 /* If we have a name for the token, print it out. Otherwise, we
1231 simply give the numeric code. */
1232 if (token
->type
< ARRAY_SIZE(token_names
))
1233 fputs (token_names
[token
->type
], stream
);
1235 fprintf (stream
, "[%d]", token
->type
);
1241 debug (cp_token
&ref
)
1243 cp_lexer_print_token (stderr
, &ref
);
1244 fprintf (stderr
, "\n");
1248 debug (cp_token
*ptr
)
1253 fprintf (stderr
, "<nil>\n");
1257 /* Start emitting debugging information. */
1260 cp_lexer_start_debugging (cp_lexer
* lexer
)
1262 lexer
->debugging_p
= true;
1263 cp_lexer_debug_stream
= stderr
;
1266 /* Stop emitting debugging information. */
1269 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1271 lexer
->debugging_p
= false;
1272 cp_lexer_debug_stream
= NULL
;
1275 /* Create a new cp_token_cache, representing a range of tokens. */
1277 static cp_token_cache
*
1278 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1280 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1281 cache
->first
= first
;
1286 /* Diagnose if #pragma omp declare simd isn't followed immediately
1287 by function declaration or definition. */
1290 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1292 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1294 error ("%<#pragma omp declare simd%> not immediately followed by "
1295 "function declaration or definition");
1296 parser
->omp_declare_simd
= NULL
;
1300 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1301 and put that into "omp declare simd" attribute. */
1304 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1306 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1308 if (fndecl
== error_mark_node
)
1310 parser
->omp_declare_simd
= NULL
;
1313 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1315 cp_ensure_no_omp_declare_simd (parser
);
1321 /* Diagnose if #pragma omp routine isn't followed immediately
1322 by function declaration or definition. */
1325 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1327 cp_finalize_oacc_routine (parser
, NULL_TREE
, false, true);
1330 /* Decl-specifiers. */
1332 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1335 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1337 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1342 /* Nothing other than the parser should be creating declarators;
1343 declarators are a semi-syntactic representation of C++ entities.
1344 Other parts of the front end that need to create entities (like
1345 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1347 static cp_declarator
*make_call_declarator
1348 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1349 static cp_declarator
*make_array_declarator
1350 (cp_declarator
*, tree
);
1351 static cp_declarator
*make_pointer_declarator
1352 (cp_cv_quals
, cp_declarator
*, tree
);
1353 static cp_declarator
*make_reference_declarator
1354 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1355 static cp_declarator
*make_ptrmem_declarator
1356 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1358 /* An erroneous declarator. */
1359 static cp_declarator
*cp_error_declarator
;
1361 /* The obstack on which declarators and related data structures are
1363 static struct obstack declarator_obstack
;
1365 /* Alloc BYTES from the declarator memory pool. */
1367 static inline void *
1368 alloc_declarator (size_t bytes
)
1370 return obstack_alloc (&declarator_obstack
, bytes
);
1373 /* Allocate a declarator of the indicated KIND. Clear fields that are
1374 common to all declarators. */
1376 static cp_declarator
*
1377 make_declarator (cp_declarator_kind kind
)
1379 cp_declarator
*declarator
;
1381 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1382 declarator
->kind
= kind
;
1383 declarator
->attributes
= NULL_TREE
;
1384 declarator
->std_attributes
= NULL_TREE
;
1385 declarator
->declarator
= NULL
;
1386 declarator
->parameter_pack_p
= false;
1387 declarator
->id_loc
= UNKNOWN_LOCATION
;
1392 /* Make a declarator for a generalized identifier. If
1393 QUALIFYING_SCOPE is non-NULL, the identifier is
1394 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1395 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1398 static cp_declarator
*
1399 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1400 special_function_kind sfk
)
1402 cp_declarator
*declarator
;
1404 /* It is valid to write:
1406 class C { void f(); };
1410 The standard is not clear about whether `typedef const C D' is
1411 legal; as of 2002-09-15 the committee is considering that
1412 question. EDG 3.0 allows that syntax. Therefore, we do as
1414 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1415 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1417 gcc_assert (identifier_p (unqualified_name
)
1418 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1419 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1421 declarator
= make_declarator (cdk_id
);
1422 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1423 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1424 declarator
->u
.id
.sfk
= sfk
;
1429 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1430 of modifiers such as const or volatile to apply to the pointer
1431 type, represented as identifiers. ATTRIBUTES represent the attributes that
1432 appertain to the pointer or reference. */
1435 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1438 cp_declarator
*declarator
;
1440 declarator
= make_declarator (cdk_pointer
);
1441 declarator
->declarator
= target
;
1442 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1443 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1446 declarator
->id_loc
= target
->id_loc
;
1447 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1448 target
->parameter_pack_p
= false;
1451 declarator
->parameter_pack_p
= false;
1453 declarator
->std_attributes
= attributes
;
1458 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1459 represent the attributes that appertain to the pointer or
1463 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1464 bool rvalue_ref
, tree attributes
)
1466 cp_declarator
*declarator
;
1468 declarator
= make_declarator (cdk_reference
);
1469 declarator
->declarator
= target
;
1470 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1471 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1474 declarator
->id_loc
= target
->id_loc
;
1475 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1476 target
->parameter_pack_p
= false;
1479 declarator
->parameter_pack_p
= false;
1481 declarator
->std_attributes
= attributes
;
1486 /* Like make_pointer_declarator -- but for a pointer to a non-static
1487 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1491 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1492 cp_declarator
*pointee
,
1495 cp_declarator
*declarator
;
1497 declarator
= make_declarator (cdk_ptrmem
);
1498 declarator
->declarator
= pointee
;
1499 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1500 declarator
->u
.pointer
.class_type
= class_type
;
1504 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1505 pointee
->parameter_pack_p
= false;
1508 declarator
->parameter_pack_p
= false;
1510 declarator
->std_attributes
= attributes
;
1515 /* Make a declarator for the function given by TARGET, with the
1516 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1517 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1518 indicates what exceptions can be thrown. */
1521 make_call_declarator (cp_declarator
*target
,
1523 cp_cv_quals cv_qualifiers
,
1524 cp_virt_specifiers virt_specifiers
,
1525 cp_ref_qualifier ref_qualifier
,
1527 tree exception_specification
,
1528 tree late_return_type
,
1529 tree requires_clause
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_function
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.function
.parameters
= parms
;
1536 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1537 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1538 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1539 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1540 declarator
->u
.function
.exception_specification
= exception_specification
;
1541 declarator
->u
.function
.late_return_type
= late_return_type
;
1542 declarator
->u
.function
.requires_clause
= requires_clause
;
1545 declarator
->id_loc
= target
->id_loc
;
1546 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1547 target
->parameter_pack_p
= false;
1550 declarator
->parameter_pack_p
= false;
1555 /* Make a declarator for an array of BOUNDS elements, each of which is
1556 defined by ELEMENT. */
1559 make_array_declarator (cp_declarator
*element
, tree bounds
)
1561 cp_declarator
*declarator
;
1563 declarator
= make_declarator (cdk_array
);
1564 declarator
->declarator
= element
;
1565 declarator
->u
.array
.bounds
= bounds
;
1568 declarator
->id_loc
= element
->id_loc
;
1569 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1570 element
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1578 /* Determine whether the declarator we've seen so far can be a
1579 parameter pack, when followed by an ellipsis. */
1581 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1583 if (declarator
&& declarator
->parameter_pack_p
)
1584 /* We already saw an ellipsis. */
1587 /* Search for a declarator name, or any other declarator that goes
1588 after the point where the ellipsis could appear in a parameter
1589 pack. If we find any of these, then this declarator can not be
1590 made into a parameter pack. */
1592 while (declarator
&& !found
)
1594 switch ((int)declarator
->kind
)
1605 declarator
= declarator
->declarator
;
1613 cp_parameter_declarator
*no_parameters
;
1615 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1616 DECLARATOR and DEFAULT_ARGUMENT. */
1618 cp_parameter_declarator
*
1619 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1620 cp_declarator
*declarator
,
1621 tree default_argument
,
1622 bool template_parameter_pack_p
= false)
1624 cp_parameter_declarator
*parameter
;
1626 parameter
= ((cp_parameter_declarator
*)
1627 alloc_declarator (sizeof (cp_parameter_declarator
)));
1628 parameter
->next
= NULL
;
1629 if (decl_specifiers
)
1630 parameter
->decl_specifiers
= *decl_specifiers
;
1632 clear_decl_specs (¶meter
->decl_specifiers
);
1633 parameter
->declarator
= declarator
;
1634 parameter
->default_argument
= default_argument
;
1635 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1640 /* Returns true iff DECLARATOR is a declaration for a function. */
1643 function_declarator_p (const cp_declarator
*declarator
)
1647 if (declarator
->kind
== cdk_function
1648 && declarator
->declarator
->kind
== cdk_id
)
1650 if (declarator
->kind
== cdk_id
1651 || declarator
->kind
== cdk_error
)
1653 declarator
= declarator
->declarator
;
1663 A cp_parser parses the token stream as specified by the C++
1664 grammar. Its job is purely parsing, not semantic analysis. For
1665 example, the parser breaks the token stream into declarators,
1666 expressions, statements, and other similar syntactic constructs.
1667 It does not check that the types of the expressions on either side
1668 of an assignment-statement are compatible, or that a function is
1669 not declared with a parameter of type `void'.
1671 The parser invokes routines elsewhere in the compiler to perform
1672 semantic analysis and to build up the abstract syntax tree for the
1675 The parser (and the template instantiation code, which is, in a
1676 way, a close relative of parsing) are the only parts of the
1677 compiler that should be calling push_scope and pop_scope, or
1678 related functions. The parser (and template instantiation code)
1679 keeps track of what scope is presently active; everything else
1680 should simply honor that. (The code that generates static
1681 initializers may also need to set the scope, in order to check
1682 access control correctly when emitting the initializers.)
1687 The parser is of the standard recursive-descent variety. Upcoming
1688 tokens in the token stream are examined in order to determine which
1689 production to use when parsing a non-terminal. Some C++ constructs
1690 require arbitrary look ahead to disambiguate. For example, it is
1691 impossible, in the general case, to tell whether a statement is an
1692 expression or declaration without scanning the entire statement.
1693 Therefore, the parser is capable of "parsing tentatively." When the
1694 parser is not sure what construct comes next, it enters this mode.
1695 Then, while we attempt to parse the construct, the parser queues up
1696 error messages, rather than issuing them immediately, and saves the
1697 tokens it consumes. If the construct is parsed successfully, the
1698 parser "commits", i.e., it issues any queued error messages and
1699 the tokens that were being preserved are permanently discarded.
1700 If, however, the construct is not parsed successfully, the parser
1701 rolls back its state completely so that it can resume parsing using
1702 a different alternative.
1707 The performance of the parser could probably be improved substantially.
1708 We could often eliminate the need to parse tentatively by looking ahead
1709 a little bit. In some places, this approach might not entirely eliminate
1710 the need to parse tentatively, but it might still speed up the average
1713 /* Flags that are passed to some parsing functions. These values can
1714 be bitwise-ored together. */
1719 CP_PARSER_FLAGS_NONE
= 0x0,
1720 /* The construct is optional. If it is not present, then no error
1721 should be issued. */
1722 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1723 /* When parsing a type-specifier, treat user-defined type-names
1724 as non-type identifiers. */
1725 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1726 /* When parsing a type-specifier, do not try to parse a class-specifier
1727 or enum-specifier. */
1728 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1729 /* When parsing a decl-specifier-seq, only allow type-specifier or
1731 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1734 /* This type is used for parameters and variables which hold
1735 combinations of the above flags. */
1736 typedef int cp_parser_flags
;
1738 /* The different kinds of declarators we want to parse. */
1740 enum cp_parser_declarator_kind
1742 /* We want an abstract declarator. */
1743 CP_PARSER_DECLARATOR_ABSTRACT
,
1744 /* We want a named declarator. */
1745 CP_PARSER_DECLARATOR_NAMED
,
1746 /* We don't mind, but the name must be an unqualified-id. */
1747 CP_PARSER_DECLARATOR_EITHER
1750 /* The precedence values used to parse binary expressions. The minimum value
1751 of PREC must be 1, because zero is reserved to quickly discriminate
1752 binary operators from other tokens. */
1757 PREC_LOGICAL_OR_EXPRESSION
,
1758 PREC_LOGICAL_AND_EXPRESSION
,
1759 PREC_INCLUSIVE_OR_EXPRESSION
,
1760 PREC_EXCLUSIVE_OR_EXPRESSION
,
1761 PREC_AND_EXPRESSION
,
1762 PREC_EQUALITY_EXPRESSION
,
1763 PREC_RELATIONAL_EXPRESSION
,
1764 PREC_SHIFT_EXPRESSION
,
1765 PREC_ADDITIVE_EXPRESSION
,
1766 PREC_MULTIPLICATIVE_EXPRESSION
,
1768 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1771 /* A mapping from a token type to a corresponding tree node type, with a
1772 precedence value. */
1774 struct cp_parser_binary_operations_map_node
1776 /* The token type. */
1777 enum cpp_ttype token_type
;
1778 /* The corresponding tree code. */
1779 enum tree_code tree_type
;
1780 /* The precedence of this operator. */
1781 enum cp_parser_prec prec
;
1784 struct cp_parser_expression_stack_entry
1786 /* Left hand side of the binary operation we are currently
1789 /* Original tree code for left hand side, if it was a binary
1790 expression itself (used for -Wparentheses). */
1791 enum tree_code lhs_type
;
1792 /* Tree code for the binary operation we are parsing. */
1793 enum tree_code tree_type
;
1794 /* Precedence of the binary operation we are parsing. */
1795 enum cp_parser_prec prec
;
1796 /* Location of the binary operation we are parsing. */
1800 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1801 entries because precedence levels on the stack are monotonically
1803 typedef struct cp_parser_expression_stack_entry
1804 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1808 /* Constructors and destructors. */
1810 static cp_parser_context
*cp_parser_context_new
1811 (cp_parser_context
*);
1813 /* Class variables. */
1815 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1817 /* The operator-precedence table used by cp_parser_binary_expression.
1818 Transformed into an associative array (binops_by_token) by
1821 static const cp_parser_binary_operations_map_node binops
[] = {
1822 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1823 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1825 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1826 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1827 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1829 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1830 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1832 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1833 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1835 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1836 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1837 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1838 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1840 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1841 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1843 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1845 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1847 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1849 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1851 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1854 /* The same as binops, but initialized by cp_parser_new so that
1855 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1857 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1859 /* Constructors and destructors. */
1861 /* Construct a new context. The context below this one on the stack
1862 is given by NEXT. */
1864 static cp_parser_context
*
1865 cp_parser_context_new (cp_parser_context
* next
)
1867 cp_parser_context
*context
;
1869 /* Allocate the storage. */
1870 if (cp_parser_context_free_list
!= NULL
)
1872 /* Pull the first entry from the free list. */
1873 context
= cp_parser_context_free_list
;
1874 cp_parser_context_free_list
= context
->next
;
1875 memset (context
, 0, sizeof (*context
));
1878 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1880 /* No errors have occurred yet in this context. */
1881 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1882 /* If this is not the bottommost context, copy information that we
1883 need from the previous context. */
1886 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1887 expression, then we are parsing one in this context, too. */
1888 context
->object_type
= next
->object_type
;
1889 /* Thread the stack. */
1890 context
->next
= next
;
1896 /* Managing the unparsed function queues. */
1898 #define unparsed_funs_with_default_args \
1899 parser->unparsed_queues->last ().funs_with_default_args
1900 #define unparsed_funs_with_definitions \
1901 parser->unparsed_queues->last ().funs_with_definitions
1902 #define unparsed_nsdmis \
1903 parser->unparsed_queues->last ().nsdmis
1904 #define unparsed_classes \
1905 parser->unparsed_queues->last ().classes
1908 push_unparsed_function_queues (cp_parser
*parser
)
1910 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1911 vec_safe_push (parser
->unparsed_queues
, e
);
1915 pop_unparsed_function_queues (cp_parser
*parser
)
1917 release_tree_vector (unparsed_funs_with_definitions
);
1918 parser
->unparsed_queues
->pop ();
1923 /* Constructors and destructors. */
1925 static cp_parser
*cp_parser_new
1928 /* Routines to parse various constructs.
1930 Those that return `tree' will return the error_mark_node (rather
1931 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1932 Sometimes, they will return an ordinary node if error-recovery was
1933 attempted, even though a parse error occurred. So, to check
1934 whether or not a parse error occurred, you should always use
1935 cp_parser_error_occurred. If the construct is optional (indicated
1936 either by an `_opt' in the name of the function that does the
1937 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1938 the construct is not present. */
1940 /* Lexical conventions [gram.lex] */
1942 static tree cp_parser_identifier
1944 static tree cp_parser_string_literal
1945 (cp_parser
*, bool, bool, bool);
1946 static tree cp_parser_userdef_char_literal
1948 static tree cp_parser_userdef_string_literal
1950 static tree cp_parser_userdef_numeric_literal
1953 /* Basic concepts [gram.basic] */
1955 static bool cp_parser_translation_unit
1958 /* Expressions [gram.expr] */
1960 static tree cp_parser_primary_expression
1961 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1962 static tree cp_parser_id_expression
1963 (cp_parser
*, bool, bool, bool *, bool, bool);
1964 static tree cp_parser_unqualified_id
1965 (cp_parser
*, bool, bool, bool, bool);
1966 static tree cp_parser_nested_name_specifier_opt
1967 (cp_parser
*, bool, bool, bool, bool);
1968 static tree cp_parser_nested_name_specifier
1969 (cp_parser
*, bool, bool, bool, bool);
1970 static tree cp_parser_qualifying_entity
1971 (cp_parser
*, bool, bool, bool, bool, bool);
1972 static tree cp_parser_postfix_expression
1973 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1974 static tree cp_parser_postfix_open_square_expression
1975 (cp_parser
*, tree
, bool, bool);
1976 static tree cp_parser_postfix_dot_deref_expression
1977 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1978 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1979 (cp_parser
*, int, bool, bool, bool *, bool = false);
1980 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1981 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1982 static void cp_parser_pseudo_destructor_name
1983 (cp_parser
*, tree
, tree
*, tree
*);
1984 static tree cp_parser_unary_expression
1985 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1986 static enum tree_code cp_parser_unary_operator
1988 static tree cp_parser_new_expression
1990 static vec
<tree
, va_gc
> *cp_parser_new_placement
1992 static tree cp_parser_new_type_id
1993 (cp_parser
*, tree
*);
1994 static cp_declarator
*cp_parser_new_declarator_opt
1996 static cp_declarator
*cp_parser_direct_new_declarator
1998 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2000 static tree cp_parser_delete_expression
2002 static tree cp_parser_cast_expression
2003 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2004 static tree cp_parser_binary_expression
2005 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2006 static tree cp_parser_question_colon_clause
2007 (cp_parser
*, tree
);
2008 static tree cp_parser_assignment_expression
2009 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2010 static enum tree_code cp_parser_assignment_operator_opt
2012 static tree cp_parser_expression
2013 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2014 static tree cp_parser_constant_expression
2015 (cp_parser
*, bool = false, bool * = NULL
);
2016 static tree cp_parser_builtin_offsetof
2018 static tree cp_parser_lambda_expression
2020 static void cp_parser_lambda_introducer
2021 (cp_parser
*, tree
);
2022 static bool cp_parser_lambda_declarator_opt
2023 (cp_parser
*, tree
);
2024 static void cp_parser_lambda_body
2025 (cp_parser
*, tree
);
2027 /* Statements [gram.stmt.stmt] */
2029 static void cp_parser_statement
2030 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
);
2031 static void cp_parser_label_for_labeled_statement
2032 (cp_parser
*, tree
);
2033 static tree cp_parser_expression_statement
2034 (cp_parser
*, tree
);
2035 static tree cp_parser_compound_statement
2036 (cp_parser
*, tree
, int, bool);
2037 static void cp_parser_statement_seq_opt
2038 (cp_parser
*, tree
);
2039 static tree cp_parser_selection_statement
2040 (cp_parser
*, bool *, vec
<tree
> *);
2041 static tree cp_parser_condition
2043 static tree cp_parser_iteration_statement
2044 (cp_parser
*, bool);
2045 static bool cp_parser_for_init_statement
2046 (cp_parser
*, tree
*decl
);
2047 static tree cp_parser_for
2048 (cp_parser
*, bool);
2049 static tree cp_parser_c_for
2050 (cp_parser
*, tree
, tree
, bool);
2051 static tree cp_parser_range_for
2052 (cp_parser
*, tree
, tree
, tree
, bool);
2053 static void do_range_for_auto_deduction
2055 static tree cp_parser_perform_range_for_lookup
2056 (tree
, tree
*, tree
*);
2057 static tree cp_parser_range_for_member_function
2059 static tree cp_parser_jump_statement
2061 static void cp_parser_declaration_statement
2064 static tree cp_parser_implicitly_scoped_statement
2065 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2066 static void cp_parser_already_scoped_statement
2067 (cp_parser
*, const token_indent_info
&);
2069 /* Declarations [gram.dcl.dcl] */
2071 static void cp_parser_declaration_seq_opt
2073 static void cp_parser_declaration
2075 static void cp_parser_block_declaration
2076 (cp_parser
*, bool);
2077 static void cp_parser_simple_declaration
2078 (cp_parser
*, bool, tree
*);
2079 static void cp_parser_decl_specifier_seq
2080 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2081 static tree cp_parser_storage_class_specifier_opt
2083 static tree cp_parser_function_specifier_opt
2084 (cp_parser
*, cp_decl_specifier_seq
*);
2085 static tree cp_parser_type_specifier
2086 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2088 static tree cp_parser_simple_type_specifier
2089 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2090 static tree cp_parser_type_name
2091 (cp_parser
*, bool);
2092 static tree cp_parser_type_name
2094 static tree cp_parser_nonclass_name
2095 (cp_parser
* parser
);
2096 static tree cp_parser_elaborated_type_specifier
2097 (cp_parser
*, bool, bool);
2098 static tree cp_parser_enum_specifier
2100 static void cp_parser_enumerator_list
2101 (cp_parser
*, tree
);
2102 static void cp_parser_enumerator_definition
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_namespace_name
2106 static void cp_parser_namespace_definition
2108 static void cp_parser_namespace_body
2110 static tree cp_parser_qualified_namespace_specifier
2112 static void cp_parser_namespace_alias_definition
2114 static bool cp_parser_using_declaration
2115 (cp_parser
*, bool);
2116 static void cp_parser_using_directive
2118 static tree cp_parser_alias_declaration
2120 static void cp_parser_asm_definition
2122 static void cp_parser_linkage_specification
2124 static void cp_parser_static_assert
2125 (cp_parser
*, bool);
2126 static tree cp_parser_decltype
2129 /* Declarators [gram.dcl.decl] */
2131 static tree cp_parser_init_declarator
2132 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2133 bool, bool, int, bool *, tree
*, bool, location_t
*);
2134 static cp_declarator
*cp_parser_declarator
2135 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2136 static cp_declarator
*cp_parser_direct_declarator
2137 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2138 static enum tree_code cp_parser_ptr_operator
2139 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2140 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146 static tree cp_parser_tx_qualifier_opt
2148 static tree cp_parser_late_return_type_opt
2149 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2150 static tree cp_parser_declarator_id
2151 (cp_parser
*, bool);
2152 static tree cp_parser_type_id
2154 static tree cp_parser_template_type_arg
2156 static tree
cp_parser_trailing_type_id (cp_parser
*);
2157 static tree cp_parser_type_id_1
2158 (cp_parser
*, bool, bool);
2159 static void cp_parser_type_specifier_seq
2160 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2161 static tree cp_parser_parameter_declaration_clause
2163 static tree cp_parser_parameter_declaration_list
2164 (cp_parser
*, bool *);
2165 static cp_parameter_declarator
*cp_parser_parameter_declaration
2166 (cp_parser
*, bool, bool *);
2167 static tree cp_parser_default_argument
2168 (cp_parser
*, bool);
2169 static void cp_parser_function_body
2170 (cp_parser
*, bool);
2171 static tree cp_parser_initializer
2172 (cp_parser
*, bool *, bool *);
2173 static tree cp_parser_initializer_clause
2174 (cp_parser
*, bool *);
2175 static tree cp_parser_braced_list
2176 (cp_parser
*, bool*);
2177 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2178 (cp_parser
*, bool *);
2180 static bool cp_parser_ctor_initializer_opt_and_function_body
2181 (cp_parser
*, bool);
2183 static tree cp_parser_late_parsing_omp_declare_simd
2184 (cp_parser
*, tree
);
2186 static tree cp_parser_late_parsing_cilk_simd_fn_info
2187 (cp_parser
*, tree
);
2189 static tree synthesize_implicit_template_parm
2190 (cp_parser
*, tree
);
2191 static tree finish_fully_implicit_template
2192 (cp_parser
*, tree
);
2194 /* Classes [gram.class] */
2196 static tree cp_parser_class_name
2197 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2198 static tree cp_parser_class_specifier
2200 static tree cp_parser_class_head
2201 (cp_parser
*, bool *);
2202 static enum tag_types cp_parser_class_key
2204 static void cp_parser_type_parameter_key
2205 (cp_parser
* parser
);
2206 static void cp_parser_member_specification_opt
2208 static void cp_parser_member_declaration
2210 static tree cp_parser_pure_specifier
2212 static tree cp_parser_constant_initializer
2215 /* Derived classes [gram.class.derived] */
2217 static tree cp_parser_base_clause
2219 static tree cp_parser_base_specifier
2222 /* Special member functions [gram.special] */
2224 static tree cp_parser_conversion_function_id
2226 static tree cp_parser_conversion_type_id
2228 static cp_declarator
*cp_parser_conversion_declarator_opt
2230 static bool cp_parser_ctor_initializer_opt
2232 static void cp_parser_mem_initializer_list
2234 static tree cp_parser_mem_initializer
2236 static tree cp_parser_mem_initializer_id
2239 /* Overloading [gram.over] */
2241 static tree cp_parser_operator_function_id
2243 static tree cp_parser_operator
2246 /* Templates [gram.temp] */
2248 static void cp_parser_template_declaration
2249 (cp_parser
*, bool);
2250 static tree cp_parser_template_parameter_list
2252 static tree cp_parser_template_parameter
2253 (cp_parser
*, bool *, bool *);
2254 static tree cp_parser_type_parameter
2255 (cp_parser
*, bool *);
2256 static tree cp_parser_template_id
2257 (cp_parser
*, bool, bool, enum tag_types
, bool);
2258 static tree cp_parser_template_name
2259 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2260 static tree cp_parser_template_argument_list
2262 static tree cp_parser_template_argument
2264 static void cp_parser_explicit_instantiation
2266 static void cp_parser_explicit_specialization
2269 /* Exception handling [gram.exception] */
2271 static tree cp_parser_try_block
2273 static bool cp_parser_function_try_block
2275 static void cp_parser_handler_seq
2277 static void cp_parser_handler
2279 static tree cp_parser_exception_declaration
2281 static tree cp_parser_throw_expression
2283 static tree cp_parser_exception_specification_opt
2285 static tree cp_parser_type_id_list
2288 /* GNU Extensions */
2290 static tree cp_parser_asm_specification_opt
2292 static tree cp_parser_asm_operand_list
2294 static tree cp_parser_asm_clobber_list
2296 static tree cp_parser_asm_label_list
2298 static bool cp_next_tokens_can_be_attribute_p
2300 static bool cp_next_tokens_can_be_gnu_attribute_p
2302 static bool cp_next_tokens_can_be_std_attribute_p
2304 static bool cp_nth_tokens_can_be_std_attribute_p
2305 (cp_parser
*, size_t);
2306 static bool cp_nth_tokens_can_be_gnu_attribute_p
2307 (cp_parser
*, size_t);
2308 static bool cp_nth_tokens_can_be_attribute_p
2309 (cp_parser
*, size_t);
2310 static tree cp_parser_attributes_opt
2312 static tree cp_parser_gnu_attributes_opt
2314 static tree cp_parser_gnu_attribute_list
2316 static tree cp_parser_std_attribute
2318 static tree cp_parser_std_attribute_spec
2320 static tree cp_parser_std_attribute_spec_seq
2322 static bool cp_parser_extension_opt
2323 (cp_parser
*, int *);
2324 static void cp_parser_label_declaration
2327 /* Concept Extensions */
2329 static tree cp_parser_requires_clause
2331 static tree cp_parser_requires_clause_opt
2333 static tree cp_parser_requires_expression
2335 static tree cp_parser_requirement_parameter_list
2337 static tree cp_parser_requirement_body
2339 static tree cp_parser_requirement_list
2341 static tree cp_parser_requirement
2343 static tree cp_parser_simple_requirement
2345 static tree cp_parser_compound_requirement
2347 static tree cp_parser_type_requirement
2349 static tree cp_parser_nested_requirement
2352 /* Transactional Memory Extensions */
2354 static tree cp_parser_transaction
2355 (cp_parser
*, cp_token
*);
2356 static tree cp_parser_transaction_expression
2357 (cp_parser
*, enum rid
);
2358 static bool cp_parser_function_transaction
2359 (cp_parser
*, enum rid
);
2360 static tree cp_parser_transaction_cancel
2363 enum pragma_context
{
2370 static bool cp_parser_pragma
2371 (cp_parser
*, enum pragma_context
);
2373 /* Objective-C++ Productions */
2375 static tree cp_parser_objc_message_receiver
2377 static tree cp_parser_objc_message_args
2379 static tree cp_parser_objc_message_expression
2381 static tree cp_parser_objc_encode_expression
2383 static tree cp_parser_objc_defs_expression
2385 static tree cp_parser_objc_protocol_expression
2387 static tree cp_parser_objc_selector_expression
2389 static tree cp_parser_objc_expression
2391 static bool cp_parser_objc_selector_p
2393 static tree cp_parser_objc_selector
2395 static tree cp_parser_objc_protocol_refs_opt
2397 static void cp_parser_objc_declaration
2398 (cp_parser
*, tree
);
2399 static tree cp_parser_objc_statement
2401 static bool cp_parser_objc_valid_prefix_attributes
2402 (cp_parser
*, tree
*);
2403 static void cp_parser_objc_at_property_declaration
2405 static void cp_parser_objc_at_synthesize_declaration
2407 static void cp_parser_objc_at_dynamic_declaration
2409 static tree cp_parser_objc_struct_declaration
2412 /* Utility Routines */
2414 static tree cp_parser_lookup_name
2415 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2416 static tree cp_parser_lookup_name_simple
2417 (cp_parser
*, tree
, location_t
);
2418 static tree cp_parser_maybe_treat_template_as_class
2420 static bool cp_parser_check_declarator_template_parameters
2421 (cp_parser
*, cp_declarator
*, location_t
);
2422 static bool cp_parser_check_template_parameters
2423 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2424 static tree cp_parser_simple_cast_expression
2426 static tree cp_parser_global_scope_opt
2427 (cp_parser
*, bool);
2428 static bool cp_parser_constructor_declarator_p
2429 (cp_parser
*, bool);
2430 static tree cp_parser_function_definition_from_specifiers_and_declarator
2431 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2432 static tree cp_parser_function_definition_after_declarator
2433 (cp_parser
*, bool);
2434 static bool cp_parser_template_declaration_after_export
2435 (cp_parser
*, bool);
2436 static void cp_parser_perform_template_parameter_access_checks
2437 (vec
<deferred_access_check
, va_gc
> *);
2438 static tree cp_parser_single_declaration
2439 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2440 static tree cp_parser_functional_cast
2441 (cp_parser
*, tree
);
2442 static tree cp_parser_save_member_function_body
2443 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
, bool);
2444 static tree cp_parser_save_nsdmi
2446 static tree cp_parser_enclosed_template_argument_list
2448 static void cp_parser_save_default_args
2449 (cp_parser
*, tree
);
2450 static void cp_parser_late_parsing_for_member
2451 (cp_parser
*, tree
);
2452 static tree cp_parser_late_parse_one_default_arg
2453 (cp_parser
*, tree
, tree
, tree
);
2454 static void cp_parser_late_parsing_nsdmi
2455 (cp_parser
*, tree
);
2456 static void cp_parser_late_parsing_default_args
2457 (cp_parser
*, tree
);
2458 static tree cp_parser_sizeof_operand
2459 (cp_parser
*, enum rid
);
2460 static tree cp_parser_trait_expr
2461 (cp_parser
*, enum rid
);
2462 static bool cp_parser_declares_only_class_p
2464 static void cp_parser_set_storage_class
2465 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2466 static void cp_parser_set_decl_spec_type
2467 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2468 static void set_and_check_decl_spec_loc
2469 (cp_decl_specifier_seq
*decl_specs
,
2470 cp_decl_spec ds
, cp_token
*);
2471 static bool cp_parser_friend_p
2472 (const cp_decl_specifier_seq
*);
2473 static void cp_parser_required_error
2474 (cp_parser
*, required_token
, bool);
2475 static cp_token
*cp_parser_require
2476 (cp_parser
*, enum cpp_ttype
, required_token
);
2477 static cp_token
*cp_parser_require_keyword
2478 (cp_parser
*, enum rid
, required_token
);
2479 static bool cp_parser_token_starts_function_definition_p
2481 static bool cp_parser_next_token_starts_class_definition_p
2483 static bool cp_parser_next_token_ends_template_argument_p
2485 static bool cp_parser_nth_token_starts_template_argument_list_p
2486 (cp_parser
*, size_t);
2487 static enum tag_types cp_parser_token_is_class_key
2489 static enum tag_types cp_parser_token_is_type_parameter_key
2491 static void cp_parser_check_class_key
2492 (enum tag_types
, tree type
);
2493 static void cp_parser_check_access_in_redeclaration
2494 (tree type
, location_t location
);
2495 static bool cp_parser_optional_template_keyword
2497 static void cp_parser_pre_parsed_nested_name_specifier
2499 static bool cp_parser_cache_group
2500 (cp_parser
*, enum cpp_ttype
, unsigned);
2501 static tree cp_parser_cache_defarg
2502 (cp_parser
*parser
, bool nsdmi
);
2503 static void cp_parser_parse_tentatively
2505 static void cp_parser_commit_to_tentative_parse
2507 static void cp_parser_commit_to_topmost_tentative_parse
2509 static void cp_parser_abort_tentative_parse
2511 static bool cp_parser_parse_definitely
2513 static inline bool cp_parser_parsing_tentatively
2515 static bool cp_parser_uncommitted_to_tentative_parse_p
2517 static void cp_parser_error
2518 (cp_parser
*, const char *);
2519 static void cp_parser_name_lookup_error
2520 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2521 static bool cp_parser_simulate_error
2523 static bool cp_parser_check_type_definition
2525 static void cp_parser_check_for_definition_in_return_type
2526 (cp_declarator
*, tree
, location_t type_location
);
2527 static void cp_parser_check_for_invalid_template_id
2528 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2529 static bool cp_parser_non_integral_constant_expression
2530 (cp_parser
*, non_integral_constant
);
2531 static void cp_parser_diagnose_invalid_type_name
2532 (cp_parser
*, tree
, location_t
);
2533 static bool cp_parser_parse_and_diagnose_invalid_type_name
2535 static int cp_parser_skip_to_closing_parenthesis
2536 (cp_parser
*, bool, bool, bool);
2537 static void cp_parser_skip_to_end_of_statement
2539 static void cp_parser_consume_semicolon_at_end_of_statement
2541 static void cp_parser_skip_to_end_of_block_or_statement
2543 static bool cp_parser_skip_to_closing_brace
2545 static void cp_parser_skip_to_end_of_template_parameter_list
2547 static void cp_parser_skip_to_pragma_eol
2548 (cp_parser
*, cp_token
*);
2549 static bool cp_parser_error_occurred
2551 static bool cp_parser_allow_gnu_extensions_p
2553 static bool cp_parser_is_pure_string_literal
2555 static bool cp_parser_is_string_literal
2557 static bool cp_parser_is_keyword
2558 (cp_token
*, enum rid
);
2559 static tree cp_parser_make_typename_type
2560 (cp_parser
*, tree
, location_t location
);
2561 static cp_declarator
* cp_parser_make_indirect_declarator
2562 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2563 static bool cp_parser_compound_literal_p
2565 static bool cp_parser_array_designator_p
2567 static bool cp_parser_skip_to_closing_square_bracket
2570 /* Concept-related syntactic transformations */
2572 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2573 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2575 // -------------------------------------------------------------------------- //
2576 // Unevaluated Operand Guard
2578 // Implementation of an RAII helper for unevaluated operand parsing.
2579 cp_unevaluated::cp_unevaluated ()
2581 ++cp_unevaluated_operand
;
2582 ++c_inhibit_evaluation_warnings
;
2585 cp_unevaluated::~cp_unevaluated ()
2587 --c_inhibit_evaluation_warnings
;
2588 --cp_unevaluated_operand
;
2591 // -------------------------------------------------------------------------- //
2592 // Tentative Parsing
2594 /* Returns nonzero if we are parsing tentatively. */
2597 cp_parser_parsing_tentatively (cp_parser
* parser
)
2599 return parser
->context
->next
!= NULL
;
2602 /* Returns nonzero if TOKEN is a string literal. */
2605 cp_parser_is_pure_string_literal (cp_token
* token
)
2607 return (token
->type
== CPP_STRING
||
2608 token
->type
== CPP_STRING16
||
2609 token
->type
== CPP_STRING32
||
2610 token
->type
== CPP_WSTRING
||
2611 token
->type
== CPP_UTF8STRING
);
2614 /* Returns nonzero if TOKEN is a string literal
2615 of a user-defined string literal. */
2618 cp_parser_is_string_literal (cp_token
* token
)
2620 return (cp_parser_is_pure_string_literal (token
) ||
2621 token
->type
== CPP_STRING_USERDEF
||
2622 token
->type
== CPP_STRING16_USERDEF
||
2623 token
->type
== CPP_STRING32_USERDEF
||
2624 token
->type
== CPP_WSTRING_USERDEF
||
2625 token
->type
== CPP_UTF8STRING_USERDEF
);
2628 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2631 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2633 return token
->keyword
== keyword
;
2636 /* If not parsing tentatively, issue a diagnostic of the form
2637 FILE:LINE: MESSAGE before TOKEN
2638 where TOKEN is the next token in the input stream. MESSAGE
2639 (specified by the caller) is usually of the form "expected
2643 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2645 if (!cp_parser_simulate_error (parser
))
2647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2648 /* This diagnostic makes more sense if it is tagged to the line
2649 of the token we just peeked at. */
2650 cp_lexer_set_source_position_from_token (token
);
2652 if (token
->type
== CPP_PRAGMA
)
2654 error_at (token
->location
,
2655 "%<#pragma%> is not allowed here");
2656 cp_parser_skip_to_pragma_eol (parser
, token
);
2660 c_parse_error (gmsgid
,
2661 /* Because c_parser_error does not understand
2662 CPP_KEYWORD, keywords are treated like
2664 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2665 token
->u
.value
, token
->flags
);
2669 /* Issue an error about name-lookup failing. NAME is the
2670 IDENTIFIER_NODE DECL is the result of
2671 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2672 the thing that we hoped to find. */
2675 cp_parser_name_lookup_error (cp_parser
* parser
,
2678 name_lookup_error desired
,
2679 location_t location
)
2681 /* If name lookup completely failed, tell the user that NAME was not
2683 if (decl
== error_mark_node
)
2685 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2686 error_at (location
, "%<%E::%E%> has not been declared",
2687 parser
->scope
, name
);
2688 else if (parser
->scope
== global_namespace
)
2689 error_at (location
, "%<::%E%> has not been declared", name
);
2690 else if (parser
->object_scope
2691 && !CLASS_TYPE_P (parser
->object_scope
))
2692 error_at (location
, "request for member %qE in non-class type %qT",
2693 name
, parser
->object_scope
);
2694 else if (parser
->object_scope
)
2695 error_at (location
, "%<%T::%E%> has not been declared",
2696 parser
->object_scope
, name
);
2698 error_at (location
, "%qE has not been declared", name
);
2700 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2705 error_at (location
, "%<%E::%E%> is not a type",
2706 parser
->scope
, name
);
2709 error_at (location
, "%<%E::%E%> is not a class or namespace",
2710 parser
->scope
, name
);
2714 "%<%E::%E%> is not a class, namespace, or enumeration",
2715 parser
->scope
, name
);
2722 else if (parser
->scope
== global_namespace
)
2727 error_at (location
, "%<::%E%> is not a type", name
);
2730 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2734 "%<::%E%> is not a class, namespace, or enumeration",
2746 error_at (location
, "%qE is not a type", name
);
2749 error_at (location
, "%qE is not a class or namespace", name
);
2753 "%qE is not a class, namespace, or enumeration", name
);
2761 /* If we are parsing tentatively, remember that an error has occurred
2762 during this tentative parse. Returns true if the error was
2763 simulated; false if a message should be issued by the caller. */
2766 cp_parser_simulate_error (cp_parser
* parser
)
2768 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2770 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2776 /* This function is called when a type is defined. If type
2777 definitions are forbidden at this point, an error message is
2781 cp_parser_check_type_definition (cp_parser
* parser
)
2783 /* If types are forbidden here, issue a message. */
2784 if (parser
->type_definition_forbidden_message
)
2786 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2787 in the message need to be interpreted. */
2788 error (parser
->type_definition_forbidden_message
);
2794 /* This function is called when the DECLARATOR is processed. The TYPE
2795 was a type defined in the decl-specifiers. If it is invalid to
2796 define a type in the decl-specifiers for DECLARATOR, an error is
2797 issued. TYPE_LOCATION is the location of TYPE and is used
2798 for error reporting. */
2801 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2802 tree type
, location_t type_location
)
2804 /* [dcl.fct] forbids type definitions in return types.
2805 Unfortunately, it's not easy to know whether or not we are
2806 processing a return type until after the fact. */
2808 && (declarator
->kind
== cdk_pointer
2809 || declarator
->kind
== cdk_reference
2810 || declarator
->kind
== cdk_ptrmem
))
2811 declarator
= declarator
->declarator
;
2813 && declarator
->kind
== cdk_function
)
2815 error_at (type_location
,
2816 "new types may not be defined in a return type");
2817 inform (type_location
,
2818 "(perhaps a semicolon is missing after the definition of %qT)",
2823 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2824 "<" in any valid C++ program. If the next token is indeed "<",
2825 issue a message warning the user about what appears to be an
2826 invalid attempt to form a template-id. LOCATION is the location
2827 of the type-specifier (TYPE) */
2830 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2832 enum tag_types tag_type
,
2833 location_t location
)
2835 cp_token_position start
= 0;
2837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2840 error_at (location
, "%qT is not a template", type
);
2841 else if (identifier_p (type
))
2843 if (tag_type
!= none_type
)
2844 error_at (location
, "%qE is not a class template", type
);
2846 error_at (location
, "%qE is not a template", type
);
2849 error_at (location
, "invalid template-id");
2850 /* Remember the location of the invalid "<". */
2851 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2852 start
= cp_lexer_token_position (parser
->lexer
, true);
2853 /* Consume the "<". */
2854 cp_lexer_consume_token (parser
->lexer
);
2855 /* Parse the template arguments. */
2856 cp_parser_enclosed_template_argument_list (parser
);
2857 /* Permanently remove the invalid template arguments so that
2858 this error message is not issued again. */
2860 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2864 /* If parsing an integral constant-expression, issue an error message
2865 about the fact that THING appeared and return true. Otherwise,
2866 return false. In either case, set
2867 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2870 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2871 non_integral_constant thing
)
2873 parser
->non_integral_constant_expression_p
= true;
2874 if (parser
->integral_constant_expression_p
)
2876 if (!parser
->allow_non_integral_constant_expression_p
)
2878 const char *msg
= NULL
;
2882 error ("floating-point literal "
2883 "cannot appear in a constant-expression");
2886 error ("a cast to a type other than an integral or "
2887 "enumeration type cannot appear in a "
2888 "constant-expression");
2891 error ("%<typeid%> operator "
2892 "cannot appear in a constant-expression");
2895 error ("non-constant compound literals "
2896 "cannot appear in a constant-expression");
2899 error ("a function call "
2900 "cannot appear in a constant-expression");
2903 error ("an increment "
2904 "cannot appear in a constant-expression");
2907 error ("an decrement "
2908 "cannot appear in a constant-expression");
2911 error ("an array reference "
2912 "cannot appear in a constant-expression");
2914 case NIC_ADDR_LABEL
:
2915 error ("the address of a label "
2916 "cannot appear in a constant-expression");
2918 case NIC_OVERLOADED
:
2919 error ("calls to overloaded operators "
2920 "cannot appear in a constant-expression");
2922 case NIC_ASSIGNMENT
:
2923 error ("an assignment cannot appear in a constant-expression");
2926 error ("a comma operator "
2927 "cannot appear in a constant-expression");
2929 case NIC_CONSTRUCTOR
:
2930 error ("a call to a constructor "
2931 "cannot appear in a constant-expression");
2933 case NIC_TRANSACTION
:
2934 error ("a transaction expression "
2935 "cannot appear in a constant-expression");
2941 msg
= "__FUNCTION__";
2943 case NIC_PRETTY_FUNC
:
2944 msg
= "__PRETTY_FUNCTION__";
2964 case NIC_PREINCREMENT
:
2967 case NIC_PREDECREMENT
:
2980 error ("%qs cannot appear in a constant-expression", msg
);
2987 /* Emit a diagnostic for an invalid type name. This function commits
2988 to the current active tentative parse, if any. (Otherwise, the
2989 problematic construct might be encountered again later, resulting
2990 in duplicate error messages.) LOCATION is the location of ID. */
2993 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2994 location_t location
)
2996 tree decl
, ambiguous_decls
;
2997 cp_parser_commit_to_tentative_parse (parser
);
2998 /* Try to lookup the identifier. */
2999 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3000 /*is_template=*/false,
3001 /*is_namespace=*/false,
3002 /*check_dependency=*/true,
3003 &ambiguous_decls
, location
);
3004 if (ambiguous_decls
)
3005 /* If the lookup was ambiguous, an error will already have
3008 /* If the lookup found a template-name, it means that the user forgot
3009 to specify an argument list. Emit a useful error message. */
3010 if (DECL_TYPE_TEMPLATE_P (decl
))
3013 "invalid use of template-name %qE without an argument list",
3015 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3017 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3018 error_at (location
, "invalid use of destructor %qD as a type", id
);
3019 else if (TREE_CODE (decl
) == TYPE_DECL
)
3020 /* Something like 'unsigned A a;' */
3021 error_at (location
, "invalid combination of multiple type-specifiers");
3022 else if (!parser
->scope
)
3024 /* Issue an error message. */
3025 error_at (location
, "%qE does not name a type", id
);
3026 /* If we're in a template class, it's possible that the user was
3027 referring to a type from a base class. For example:
3029 template <typename T> struct A { typedef T X; };
3030 template <typename T> struct B : public A<T> { X x; };
3032 The user should have said "typename A<T>::X". */
3033 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3034 inform (location
, "C++11 %<constexpr%> only available with "
3035 "-std=c++11 or -std=gnu++11");
3036 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3037 inform (location
, "C++11 %<noexcept%> only available with "
3038 "-std=c++11 or -std=gnu++11");
3039 else if (cxx_dialect
< cxx11
3040 && TREE_CODE (id
) == IDENTIFIER_NODE
3041 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
3042 inform (location
, "C++11 %<thread_local%> only available with "
3043 "-std=c++11 or -std=gnu++11");
3044 else if (processing_template_decl
&& current_class_type
3045 && TYPE_BINFO (current_class_type
))
3049 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3053 tree base_type
= BINFO_TYPE (b
);
3054 if (CLASS_TYPE_P (base_type
)
3055 && dependent_type_p (base_type
))
3058 /* Go from a particular instantiation of the
3059 template (which will have an empty TYPE_FIELDs),
3060 to the main version. */
3061 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3062 for (field
= TYPE_FIELDS (base_type
);
3064 field
= DECL_CHAIN (field
))
3065 if (TREE_CODE (field
) == TYPE_DECL
3066 && DECL_NAME (field
) == id
)
3069 "(perhaps %<typename %T::%E%> was intended)",
3070 BINFO_TYPE (b
), id
);
3079 /* Here we diagnose qualified-ids where the scope is actually correct,
3080 but the identifier does not resolve to a valid type name. */
3081 else if (parser
->scope
!= error_mark_node
)
3083 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3086 error_at (location_of (id
),
3087 "%qE in namespace %qE does not name a template type",
3090 error_at (location_of (id
),
3091 "%qE in namespace %qE does not name a type",
3094 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3096 else if (CLASS_TYPE_P (parser
->scope
)
3097 && constructor_name_p (id
, parser
->scope
))
3100 error_at (location
, "%<%T::%E%> names the constructor, not"
3101 " the type", parser
->scope
, id
);
3102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3103 error_at (location
, "and %qT has no template constructors",
3106 else if (TYPE_P (parser
->scope
)
3107 && dependent_scope_p (parser
->scope
))
3108 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3109 "%qT is a dependent scope",
3110 parser
->scope
, id
, parser
->scope
);
3111 else if (TYPE_P (parser
->scope
))
3113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3114 error_at (location_of (id
),
3115 "%qE in %q#T does not name a template type",
3118 error_at (location_of (id
),
3119 "%qE in %q#T does not name a type",
3122 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3129 /* Check for a common situation where a type-name should be present,
3130 but is not, and issue a sensible error message. Returns true if an
3131 invalid type-name was detected.
3133 The situation handled by this function are variable declarations of the
3134 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3135 Usually, `ID' should name a type, but if we got here it means that it
3136 does not. We try to emit the best possible error message depending on
3137 how exactly the id-expression looks like. */
3140 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3145 /* Avoid duplicate error about ambiguous lookup. */
3146 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3148 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3149 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3153 cp_parser_parse_tentatively (parser
);
3154 id
= cp_parser_id_expression (parser
,
3155 /*template_keyword_p=*/false,
3156 /*check_dependency_p=*/true,
3157 /*template_p=*/NULL
,
3158 /*declarator_p=*/true,
3159 /*optional_p=*/false);
3160 /* If the next token is a (, this is a function with no explicit return
3161 type, i.e. constructor, destructor or conversion op. */
3162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3163 || TREE_CODE (id
) == TYPE_DECL
)
3165 cp_parser_abort_tentative_parse (parser
);
3168 if (!cp_parser_parse_definitely (parser
))
3171 /* Emit a diagnostic for the invalid type. */
3172 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3174 /* If we aren't in the middle of a declarator (i.e. in a
3175 parameter-declaration-clause), skip to the end of the declaration;
3176 there's no point in trying to process it. */
3177 if (!parser
->in_declarator_p
)
3178 cp_parser_skip_to_end_of_block_or_statement (parser
);
3182 /* Consume tokens up to, and including, the next non-nested closing `)'.
3183 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3184 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3185 found an unnested token of that type. */
3188 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3193 unsigned paren_depth
= 0;
3194 unsigned brace_depth
= 0;
3195 unsigned square_depth
= 0;
3197 if (recovering
&& or_ttype
== CPP_EOF
3198 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3203 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3205 /* Have we found what we're looking for before the closing paren? */
3206 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3207 && !brace_depth
&& !paren_depth
&& !square_depth
)
3210 switch (token
->type
)
3213 case CPP_PRAGMA_EOL
:
3214 /* If we've run out of tokens, then there is no closing `)'. */
3217 /* This is good for lambda expression capture-lists. */
3218 case CPP_OPEN_SQUARE
:
3221 case CPP_CLOSE_SQUARE
:
3222 if (!square_depth
--)
3227 /* This matches the processing in skip_to_end_of_statement. */
3232 case CPP_OPEN_BRACE
:
3235 case CPP_CLOSE_BRACE
:
3240 case CPP_OPEN_PAREN
:
3245 case CPP_CLOSE_PAREN
:
3246 if (!brace_depth
&& !paren_depth
--)
3249 cp_lexer_consume_token (parser
->lexer
);
3258 /* Consume the token. */
3259 cp_lexer_consume_token (parser
->lexer
);
3263 /* Consume tokens up to, and including, the next non-nested closing `)'.
3264 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3265 are doing error recovery. Returns -1 if OR_COMMA is true and we
3266 found an unnested token of that type. */
3269 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3274 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3275 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3276 ttype
, consume_paren
);
3279 /* Consume tokens until we reach the end of the current statement.
3280 Normally, that will be just before consuming a `;'. However, if a
3281 non-nested `}' comes first, then we stop before consuming that. */
3284 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3286 unsigned nesting_depth
= 0;
3288 /* Unwind generic function template scope if necessary. */
3289 if (parser
->fully_implicit_function_template_p
)
3290 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3296 switch (token
->type
)
3299 case CPP_PRAGMA_EOL
:
3300 /* If we've run out of tokens, stop. */
3304 /* If the next token is a `;', we have reached the end of the
3310 case CPP_CLOSE_BRACE
:
3311 /* If this is a non-nested '}', stop before consuming it.
3312 That way, when confronted with something like:
3316 we stop before consuming the closing '}', even though we
3317 have not yet reached a `;'. */
3318 if (nesting_depth
== 0)
3321 /* If it is the closing '}' for a block that we have
3322 scanned, stop -- but only after consuming the token.
3328 we will stop after the body of the erroneously declared
3329 function, but before consuming the following `typedef'
3331 if (--nesting_depth
== 0)
3333 cp_lexer_consume_token (parser
->lexer
);
3337 case CPP_OPEN_BRACE
:
3345 /* Consume the token. */
3346 cp_lexer_consume_token (parser
->lexer
);
3350 /* This function is called at the end of a statement or declaration.
3351 If the next token is a semicolon, it is consumed; otherwise, error
3352 recovery is attempted. */
3355 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3357 /* Look for the trailing `;'. */
3358 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3360 /* If there is additional (erroneous) input, skip to the end of
3362 cp_parser_skip_to_end_of_statement (parser
);
3363 /* If the next token is now a `;', consume it. */
3364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3365 cp_lexer_consume_token (parser
->lexer
);
3369 /* Skip tokens until we have consumed an entire block, or until we
3370 have consumed a non-nested `;'. */
3373 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3375 int nesting_depth
= 0;
3377 /* Unwind generic function template scope if necessary. */
3378 if (parser
->fully_implicit_function_template_p
)
3379 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3381 while (nesting_depth
>= 0)
3383 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3385 switch (token
->type
)
3388 case CPP_PRAGMA_EOL
:
3389 /* If we've run out of tokens, stop. */
3393 /* Stop if this is an unnested ';'. */
3398 case CPP_CLOSE_BRACE
:
3399 /* Stop if this is an unnested '}', or closes the outermost
3402 if (nesting_depth
< 0)
3408 case CPP_OPEN_BRACE
:
3417 /* Consume the token. */
3418 cp_lexer_consume_token (parser
->lexer
);
3422 /* Skip tokens until a non-nested closing curly brace is the next
3423 token, or there are no more tokens. Return true in the first case,
3427 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3429 unsigned nesting_depth
= 0;
3433 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3435 switch (token
->type
)
3438 case CPP_PRAGMA_EOL
:
3439 /* If we've run out of tokens, stop. */
3442 case CPP_CLOSE_BRACE
:
3443 /* If the next token is a non-nested `}', then we have reached
3444 the end of the current block. */
3445 if (nesting_depth
-- == 0)
3449 case CPP_OPEN_BRACE
:
3450 /* If it the next token is a `{', then we are entering a new
3451 block. Consume the entire block. */
3459 /* Consume the token. */
3460 cp_lexer_consume_token (parser
->lexer
);
3464 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3465 parameter is the PRAGMA token, allowing us to purge the entire pragma
3469 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3473 parser
->lexer
->in_pragma
= false;
3476 token
= cp_lexer_consume_token (parser
->lexer
);
3477 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3479 /* Ensure that the pragma is not parsed again. */
3480 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3483 /* Require pragma end of line, resyncing with it as necessary. The
3484 arguments are as for cp_parser_skip_to_pragma_eol. */
3487 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3489 parser
->lexer
->in_pragma
= false;
3490 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3491 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3494 /* This is a simple wrapper around make_typename_type. When the id is
3495 an unresolved identifier node, we can provide a superior diagnostic
3496 using cp_parser_diagnose_invalid_type_name. */
3499 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3500 location_t id_location
)
3503 if (identifier_p (id
))
3505 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3506 /*complain=*/tf_none
);
3507 if (result
== error_mark_node
)
3508 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3511 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3514 /* This is a wrapper around the
3515 make_{pointer,ptrmem,reference}_declarator functions that decides
3516 which one to call based on the CODE and CLASS_TYPE arguments. The
3517 CODE argument should be one of the values returned by
3518 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3519 appertain to the pointer or reference. */
3521 static cp_declarator
*
3522 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3523 cp_cv_quals cv_qualifiers
,
3524 cp_declarator
*target
,
3527 if (code
== ERROR_MARK
)
3528 return cp_error_declarator
;
3530 if (code
== INDIRECT_REF
)
3531 if (class_type
== NULL_TREE
)
3532 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3534 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3535 target
, attributes
);
3536 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3537 return make_reference_declarator (cv_qualifiers
, target
,
3539 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3540 return make_reference_declarator (cv_qualifiers
, target
,
3545 /* Create a new C++ parser. */
3548 cp_parser_new (void)
3554 /* cp_lexer_new_main is called before doing GC allocation because
3555 cp_lexer_new_main might load a PCH file. */
3556 lexer
= cp_lexer_new_main ();
3558 /* Initialize the binops_by_token so that we can get the tree
3559 directly from the token. */
3560 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3561 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3563 parser
= ggc_cleared_alloc
<cp_parser
> ();
3564 parser
->lexer
= lexer
;
3565 parser
->context
= cp_parser_context_new (NULL
);
3567 /* For now, we always accept GNU extensions. */
3568 parser
->allow_gnu_extensions_p
= 1;
3570 /* The `>' token is a greater-than operator, not the end of a
3572 parser
->greater_than_is_operator_p
= true;
3574 parser
->default_arg_ok_p
= true;
3576 /* We are not parsing a constant-expression. */
3577 parser
->integral_constant_expression_p
= false;
3578 parser
->allow_non_integral_constant_expression_p
= false;
3579 parser
->non_integral_constant_expression_p
= false;
3581 /* Local variable names are not forbidden. */
3582 parser
->local_variables_forbidden_p
= false;
3584 /* We are not processing an `extern "C"' declaration. */
3585 parser
->in_unbraced_linkage_specification_p
= false;
3587 /* We are not processing a declarator. */
3588 parser
->in_declarator_p
= false;
3590 /* We are not processing a template-argument-list. */
3591 parser
->in_template_argument_list_p
= false;
3593 /* We are not in an iteration statement. */
3594 parser
->in_statement
= 0;
3596 /* We are not in a switch statement. */
3597 parser
->in_switch_statement_p
= false;
3599 /* We are not parsing a type-id inside an expression. */
3600 parser
->in_type_id_in_expr_p
= false;
3602 /* Declarations aren't implicitly extern "C". */
3603 parser
->implicit_extern_c
= false;
3605 /* String literals should be translated to the execution character set. */
3606 parser
->translate_strings_p
= true;
3608 /* We are not parsing a function body. */
3609 parser
->in_function_body
= false;
3611 /* We can correct until told otherwise. */
3612 parser
->colon_corrects_to_scope_p
= true;
3614 /* The unparsed function queue is empty. */
3615 push_unparsed_function_queues (parser
);
3617 /* There are no classes being defined. */
3618 parser
->num_classes_being_defined
= 0;
3620 /* No template parameters apply. */
3621 parser
->num_template_parameter_lists
= 0;
3623 /* Not declaring an implicit function template. */
3624 parser
->auto_is_implicit_function_template_parm_p
= false;
3625 parser
->fully_implicit_function_template_p
= false;
3626 parser
->implicit_template_parms
= 0;
3627 parser
->implicit_template_scope
= 0;
3629 /* Active OpenACC routine clauses. */
3630 parser
->oacc_routine
= NULL
;
3632 /* Allow constrained-type-specifiers. */
3633 parser
->prevent_constrained_type_specifiers
= 0;
3638 /* Create a cp_lexer structure which will emit the tokens in CACHE
3639 and push it onto the parser's lexer stack. This is used for delayed
3640 parsing of in-class method bodies and default arguments, and should
3641 not be confused with tentative parsing. */
3643 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3645 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3646 lexer
->next
= parser
->lexer
;
3647 parser
->lexer
= lexer
;
3649 /* Move the current source position to that of the first token in the
3651 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3654 /* Pop the top lexer off the parser stack. This is never used for the
3655 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3657 cp_parser_pop_lexer (cp_parser
*parser
)
3659 cp_lexer
*lexer
= parser
->lexer
;
3660 parser
->lexer
= lexer
->next
;
3661 cp_lexer_destroy (lexer
);
3663 /* Put the current source position back where it was before this
3664 lexer was pushed. */
3665 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3668 /* Lexical conventions [gram.lex] */
3670 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3674 cp_parser_identifier (cp_parser
* parser
)
3678 /* Look for the identifier. */
3679 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3680 /* Return the value. */
3681 return token
? token
->u
.value
: error_mark_node
;
3684 /* Parse a sequence of adjacent string constants. Returns a
3685 TREE_STRING representing the combined, nul-terminated string
3686 constant. If TRANSLATE is true, translate the string to the
3687 execution character set. If WIDE_OK is true, a wide string is
3690 C++98 [lex.string] says that if a narrow string literal token is
3691 adjacent to a wide string literal token, the behavior is undefined.
3692 However, C99 6.4.5p4 says that this results in a wide string literal.
3693 We follow C99 here, for consistency with the C front end.
3695 This code is largely lifted from lex_string() in c-lex.c.
3697 FUTURE: ObjC++ will need to handle @-strings here. */
3699 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3700 bool lookup_udlit
= true)
3704 struct obstack str_ob
;
3705 cpp_string str
, istr
, *strs
;
3707 enum cpp_ttype type
, curr_type
;
3708 int have_suffix_p
= 0;
3710 tree suffix_id
= NULL_TREE
;
3711 bool curr_tok_is_userdef_p
= false;
3713 tok
= cp_lexer_peek_token (parser
->lexer
);
3714 if (!cp_parser_is_string_literal (tok
))
3716 cp_parser_error (parser
, "expected string-literal");
3717 return error_mark_node
;
3720 if (cpp_userdef_string_p (tok
->type
))
3722 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3723 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3724 curr_tok_is_userdef_p
= true;
3728 string_tree
= tok
->u
.value
;
3729 curr_type
= tok
->type
;
3733 /* Try to avoid the overhead of creating and destroying an obstack
3734 for the common case of just one string. */
3735 if (!cp_parser_is_string_literal
3736 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3738 cp_lexer_consume_token (parser
->lexer
);
3740 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3741 str
.len
= TREE_STRING_LENGTH (string_tree
);
3744 if (curr_tok_is_userdef_p
)
3746 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3748 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3751 curr_type
= tok
->type
;
3757 gcc_obstack_init (&str_ob
);
3762 cp_lexer_consume_token (parser
->lexer
);
3764 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3765 str
.len
= TREE_STRING_LENGTH (string_tree
);
3767 if (curr_tok_is_userdef_p
)
3769 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3770 if (have_suffix_p
== 0)
3772 suffix_id
= curr_suffix_id
;
3775 else if (have_suffix_p
== 1
3776 && curr_suffix_id
!= suffix_id
)
3778 error ("inconsistent user-defined literal suffixes"
3779 " %qD and %qD in string literal",
3780 suffix_id
, curr_suffix_id
);
3783 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3786 curr_type
= tok
->type
;
3788 if (type
!= curr_type
)
3790 if (type
== CPP_STRING
)
3792 else if (curr_type
!= CPP_STRING
)
3793 error_at (tok
->location
,
3794 "unsupported non-standard concatenation "
3795 "of string literals");
3798 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3800 tok
= cp_lexer_peek_token (parser
->lexer
);
3801 if (cpp_userdef_string_p (tok
->type
))
3803 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3804 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3805 curr_tok_is_userdef_p
= true;
3809 string_tree
= tok
->u
.value
;
3810 curr_type
= tok
->type
;
3811 curr_tok_is_userdef_p
= false;
3814 while (cp_parser_is_string_literal (tok
));
3816 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3819 if (type
!= CPP_STRING
&& !wide_ok
)
3821 cp_parser_error (parser
, "a wide string is invalid in this context");
3825 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3826 (parse_in
, strs
, count
, &istr
, type
))
3828 value
= build_string (istr
.len
, (const char *)istr
.text
);
3829 free (CONST_CAST (unsigned char *, istr
.text
));
3835 case CPP_UTF8STRING
:
3836 TREE_TYPE (value
) = char_array_type_node
;
3839 TREE_TYPE (value
) = char16_array_type_node
;
3842 TREE_TYPE (value
) = char32_array_type_node
;
3845 TREE_TYPE (value
) = wchar_array_type_node
;
3849 value
= fix_string_type (value
);
3853 tree literal
= build_userdef_literal (suffix_id
, value
,
3854 OT_NONE
, NULL_TREE
);
3856 value
= cp_parser_userdef_string_literal (literal
);
3862 /* cpp_interpret_string has issued an error. */
3863 value
= error_mark_node
;
3866 obstack_free (&str_ob
, 0);
3871 /* Look up a literal operator with the name and the exact arguments. */
3874 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3877 decl
= lookup_name (name
);
3878 if (!decl
|| !is_overloaded_fn (decl
))
3879 return error_mark_node
;
3881 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3885 tree fn
= OVL_CURRENT (fns
);
3886 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3887 if (parmtypes
!= NULL_TREE
)
3889 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3890 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3892 tree tparm
= TREE_VALUE (parmtypes
);
3893 tree targ
= TREE_TYPE ((*args
)[ix
]);
3894 bool ptr
= TYPE_PTR_P (tparm
);
3895 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3896 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3898 || !same_type_p (TREE_TYPE (tparm
),
3903 && ix
== vec_safe_length (args
)
3904 /* May be this should be sufficient_parms_p instead,
3905 depending on how exactly should user-defined literals
3906 work in presence of default arguments on the literal
3907 operator parameters. */
3908 && parmtypes
== void_list_node
)
3913 return error_mark_node
;
3916 /* Parse a user-defined char constant. Returns a call to a user-defined
3917 literal operator taking the character as an argument. */
3920 cp_parser_userdef_char_literal (cp_parser
*parser
)
3922 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3923 tree literal
= token
->u
.value
;
3924 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3925 tree value
= USERDEF_LITERAL_VALUE (literal
);
3926 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3929 /* Build up a call to the user-defined operator */
3930 /* Lookup the name we got back from the id-expression. */
3931 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3932 vec_safe_push (args
, value
);
3933 decl
= lookup_literal_operator (name
, args
);
3934 if (!decl
|| decl
== error_mark_node
)
3936 error ("unable to find character literal operator %qD with %qT argument",
3937 name
, TREE_TYPE (value
));
3938 release_tree_vector (args
);
3939 return error_mark_node
;
3941 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3942 release_tree_vector (args
);
3946 /* A subroutine of cp_parser_userdef_numeric_literal to
3947 create a char... template parameter pack from a string node. */
3950 make_char_string_pack (tree value
)
3953 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3954 const char *str
= TREE_STRING_POINTER (value
);
3955 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3956 tree argvec
= make_tree_vec (1);
3958 /* Fill in CHARVEC with all of the parameters. */
3959 charvec
= make_tree_vec (len
);
3960 for (i
= 0; i
< len
; ++i
)
3961 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3963 /* Build the argument packs. */
3964 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3965 TREE_TYPE (argpack
) = char_type_node
;
3967 TREE_VEC_ELT (argvec
, 0) = argpack
;
3972 /* A subroutine of cp_parser_userdef_numeric_literal to
3973 create a char... template parameter pack from a string node. */
3976 make_string_pack (tree value
)
3979 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3980 const unsigned char *str
3981 = (const unsigned char *) TREE_STRING_POINTER (value
);
3982 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3983 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3984 tree argvec
= make_tree_vec (2);
3986 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3987 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3989 /* First template parm is character type. */
3990 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3992 /* Fill in CHARVEC with all of the parameters. */
3993 charvec
= make_tree_vec (len
);
3994 for (int i
= 0; i
< len
; ++i
)
3995 TREE_VEC_ELT (charvec
, i
)
3996 = double_int_to_tree (str_char_type_node
,
3997 double_int::from_buffer (str
+ i
* sz
, sz
));
3999 /* Build the argument packs. */
4000 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4001 TREE_TYPE (argpack
) = str_char_type_node
;
4003 TREE_VEC_ELT (argvec
, 1) = argpack
;
4008 /* Parse a user-defined numeric constant. returns a call to a user-defined
4009 literal operator. */
4012 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4014 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4015 tree literal
= token
->u
.value
;
4016 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4017 tree value
= USERDEF_LITERAL_VALUE (literal
);
4018 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4019 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4020 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4022 vec
<tree
, va_gc
> *args
;
4024 /* Look for a literal operator taking the exact type of numeric argument
4025 as the literal value. */
4026 args
= make_tree_vector ();
4027 vec_safe_push (args
, value
);
4028 decl
= lookup_literal_operator (name
, args
);
4029 if (decl
&& decl
!= error_mark_node
)
4031 result
= finish_call_expr (decl
, &args
, false, true,
4032 tf_warning_or_error
);
4034 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4036 warning_at (token
->location
, OPT_Woverflow
,
4037 "integer literal exceeds range of %qT type",
4038 long_long_unsigned_type_node
);
4043 warning_at (token
->location
, OPT_Woverflow
,
4044 "floating literal exceeds range of %qT type",
4045 long_double_type_node
);
4046 else if (overflow
< 0)
4047 warning_at (token
->location
, OPT_Woverflow
,
4048 "floating literal truncated to zero");
4051 release_tree_vector (args
);
4054 release_tree_vector (args
);
4056 /* If the numeric argument didn't work, look for a raw literal
4057 operator taking a const char* argument consisting of the number
4058 in string format. */
4059 args
= make_tree_vector ();
4060 vec_safe_push (args
, num_string
);
4061 decl
= lookup_literal_operator (name
, args
);
4062 if (decl
&& decl
!= error_mark_node
)
4064 result
= finish_call_expr (decl
, &args
, false, true,
4065 tf_warning_or_error
);
4066 release_tree_vector (args
);
4069 release_tree_vector (args
);
4071 /* If the raw literal didn't work, look for a non-type template
4072 function with parameter pack char.... Call the function with
4073 template parameter characters representing the number. */
4074 args
= make_tree_vector ();
4075 decl
= lookup_literal_operator (name
, args
);
4076 if (decl
&& decl
!= error_mark_node
)
4078 tree tmpl_args
= make_char_string_pack (num_string
);
4079 decl
= lookup_template_function (decl
, tmpl_args
);
4080 result
= finish_call_expr (decl
, &args
, false, true,
4081 tf_warning_or_error
);
4082 release_tree_vector (args
);
4086 release_tree_vector (args
);
4088 error ("unable to find numeric literal operator %qD", name
);
4089 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4090 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4091 "to enable more built-in suffixes");
4092 return error_mark_node
;
4095 /* Parse a user-defined string constant. Returns a call to a user-defined
4096 literal operator taking a character pointer and the length of the string
4100 cp_parser_userdef_string_literal (tree literal
)
4102 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4103 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4104 tree value
= USERDEF_LITERAL_VALUE (literal
);
4105 int len
= TREE_STRING_LENGTH (value
)
4106 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4108 vec
<tree
, va_gc
> *args
;
4110 /* Build up a call to the user-defined operator. */
4111 /* Lookup the name we got back from the id-expression. */
4112 args
= make_tree_vector ();
4113 vec_safe_push (args
, value
);
4114 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4115 decl
= lookup_literal_operator (name
, args
);
4117 if (decl
&& decl
!= error_mark_node
)
4119 result
= finish_call_expr (decl
, &args
, false, true,
4120 tf_warning_or_error
);
4121 release_tree_vector (args
);
4124 release_tree_vector (args
);
4126 /* Look for a template function with typename parameter CharT
4127 and parameter pack CharT... Call the function with
4128 template parameter characters representing the string. */
4129 args
= make_tree_vector ();
4130 decl
= lookup_literal_operator (name
, args
);
4131 if (decl
&& decl
!= error_mark_node
)
4133 tree tmpl_args
= make_string_pack (value
);
4134 decl
= lookup_template_function (decl
, tmpl_args
);
4135 result
= finish_call_expr (decl
, &args
, false, true,
4136 tf_warning_or_error
);
4137 release_tree_vector (args
);
4140 release_tree_vector (args
);
4142 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4143 name
, TREE_TYPE (value
), size_type_node
);
4144 return error_mark_node
;
4148 /* Basic concepts [gram.basic] */
4150 /* Parse a translation-unit.
4153 declaration-seq [opt]
4155 Returns TRUE if all went well. */
4158 cp_parser_translation_unit (cp_parser
* parser
)
4160 /* The address of the first non-permanent object on the declarator
4162 static void *declarator_obstack_base
;
4166 /* Create the declarator obstack, if necessary. */
4167 if (!cp_error_declarator
)
4169 gcc_obstack_init (&declarator_obstack
);
4170 /* Create the error declarator. */
4171 cp_error_declarator
= make_declarator (cdk_error
);
4172 /* Create the empty parameter list. */
4173 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4174 /* Remember where the base of the declarator obstack lies. */
4175 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4178 cp_parser_declaration_seq_opt (parser
);
4180 /* If there are no tokens left then all went well. */
4181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4183 /* Get rid of the token array; we don't need it any more. */
4184 cp_lexer_destroy (parser
->lexer
);
4185 parser
->lexer
= NULL
;
4187 /* This file might have been a context that's implicitly extern
4188 "C". If so, pop the lang context. (Only relevant for PCH.) */
4189 if (parser
->implicit_extern_c
)
4191 pop_lang_context ();
4192 parser
->implicit_extern_c
= false;
4196 finish_translation_unit ();
4202 cp_parser_error (parser
, "expected declaration");
4206 /* Make sure the declarator obstack was fully cleaned up. */
4207 gcc_assert (obstack_next_free (&declarator_obstack
)
4208 == declarator_obstack_base
);
4210 /* All went well. */
4214 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4215 decltype context. */
4217 static inline tsubst_flags_t
4218 complain_flags (bool decltype_p
)
4220 tsubst_flags_t complain
= tf_warning_or_error
;
4222 complain
|= tf_decltype
;
4226 /* We're about to parse a collection of statements. If we're currently
4227 parsing tentatively, set up a firewall so that any nested
4228 cp_parser_commit_to_tentative_parse won't affect the current context. */
4230 static cp_token_position
4231 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4233 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4236 cp_parser_parse_tentatively (parser
);
4237 cp_parser_commit_to_topmost_tentative_parse (parser
);
4238 return cp_lexer_token_position (parser
->lexer
, false);
4241 /* We've finished parsing the collection of statements. Wrap up the
4242 firewall and replace the relevant tokens with the parsed form. */
4245 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4251 /* Finish the firewall level. */
4252 cp_parser_parse_definitely (parser
);
4253 /* And remember the result of the parse for when we try again. */
4254 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4255 token
->type
= CPP_PREPARSED_EXPR
;
4256 token
->u
.value
= expr
;
4257 token
->keyword
= RID_MAX
;
4258 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4261 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4262 enclosing parentheses. */
4265 cp_parser_statement_expr (cp_parser
*parser
)
4267 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4269 /* Consume the '('. */
4270 cp_lexer_consume_token (parser
->lexer
);
4271 /* Start the statement-expression. */
4272 tree expr
= begin_stmt_expr ();
4273 /* Parse the compound-statement. */
4274 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4276 expr
= finish_stmt_expr (expr
, false);
4277 /* Consume the ')'. */
4278 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4279 cp_parser_skip_to_end_of_statement (parser
);
4281 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4285 /* Expressions [gram.expr] */
4287 /* Parse a fold-operator.
4290 - * / % ^ & | = < > << >>
4291 = -= *= /= %= ^= &= |= <<= >>=
4292 == != <= >= && || , .* ->*
4294 This returns the tree code corresponding to the matched operator
4295 as an int. When the current token matches a compound assignment
4296 opertor, the resulting tree code is the negative value of the
4297 non-assignment operator. */
4300 cp_parser_fold_operator (cp_token
*token
)
4302 switch (token
->type
)
4304 case CPP_PLUS
: return PLUS_EXPR
;
4305 case CPP_MINUS
: return MINUS_EXPR
;
4306 case CPP_MULT
: return MULT_EXPR
;
4307 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4308 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4309 case CPP_XOR
: return BIT_XOR_EXPR
;
4310 case CPP_AND
: return BIT_AND_EXPR
;
4311 case CPP_OR
: return BIT_IOR_EXPR
;
4312 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4313 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4315 case CPP_EQ
: return -NOP_EXPR
;
4316 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4317 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4318 case CPP_MULT_EQ
: return -MULT_EXPR
;
4319 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4320 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4321 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4322 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4323 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4324 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4325 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4327 case CPP_EQ_EQ
: return EQ_EXPR
;
4328 case CPP_NOT_EQ
: return NE_EXPR
;
4329 case CPP_LESS
: return LT_EXPR
;
4330 case CPP_GREATER
: return GT_EXPR
;
4331 case CPP_LESS_EQ
: return LE_EXPR
;
4332 case CPP_GREATER_EQ
: return GE_EXPR
;
4334 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4335 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4337 case CPP_COMMA
: return COMPOUND_EXPR
;
4339 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4340 case CPP_DEREF_STAR
: return MEMBER_REF
;
4342 default: return ERROR_MARK
;
4346 /* Returns true if CODE indicates a binary expression, which is not allowed in
4347 the LHS of a fold-expression. More codes will need to be added to use this
4348 function in other contexts. */
4351 is_binary_op (tree_code code
)
4356 case POINTER_PLUS_EXPR
:
4359 case TRUNC_DIV_EXPR
:
4360 case TRUNC_MOD_EXPR
:
4376 case TRUTH_ANDIF_EXPR
:
4377 case TRUTH_ORIF_EXPR
:
4390 /* If the next token is a suitable fold operator, consume it and return as
4391 the function above. */
4394 cp_parser_fold_operator (cp_parser
*parser
)
4396 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4397 int code
= cp_parser_fold_operator (token
);
4398 if (code
!= ERROR_MARK
)
4399 cp_lexer_consume_token (parser
->lexer
);
4403 /* Parse a fold-expression.
4406 ( ... folding-operator cast-expression)
4407 ( cast-expression folding-operator ... )
4408 ( cast-expression folding operator ... folding-operator cast-expression)
4410 Note that the '(' and ')' are matched in primary expression. */
4413 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4420 cp_lexer_consume_token (parser
->lexer
);
4421 int op
= cp_parser_fold_operator (parser
);
4422 if (op
== ERROR_MARK
)
4424 cp_parser_error (parser
, "expected binary operator");
4425 return error_mark_node
;
4428 tree expr
= cp_parser_cast_expression (parser
, false, false,
4430 if (expr
== error_mark_node
)
4431 return error_mark_node
;
4432 return finish_left_unary_fold_expr (expr
, op
);
4435 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4436 int op
= cp_parser_fold_operator (parser
);
4437 if (op
== ERROR_MARK
)
4439 cp_parser_error (parser
, "expected binary operator");
4440 return error_mark_node
;
4443 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4445 cp_parser_error (parser
, "expected ...");
4446 return error_mark_node
;
4448 cp_lexer_consume_token (parser
->lexer
);
4450 /* The operands of a fold-expression are cast-expressions, so binary or
4451 conditional expressions are not allowed. We check this here to avoid
4452 tentative parsing. */
4453 if (is_binary_op (TREE_CODE (expr1
)))
4454 error_at (location_of (expr1
),
4455 "binary expression in operand of fold-expression");
4456 else if (TREE_CODE (expr1
) == COND_EXPR
)
4457 error_at (location_of (expr1
),
4458 "conditional expression in operand of fold-expression");
4461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4462 return finish_right_unary_fold_expr (expr1
, op
);
4464 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4466 cp_parser_error (parser
, "mismatched operator in fold-expression");
4467 return error_mark_node
;
4469 cp_lexer_consume_token (parser
->lexer
);
4471 // Binary left or right fold.
4472 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4473 if (expr2
== error_mark_node
)
4474 return error_mark_node
;
4475 return finish_binary_fold_expr (expr1
, expr2
, op
);
4478 /* Parse a primary-expression.
4485 lambda-expression (C++11)
4490 ( compound-statement )
4491 __builtin_va_arg ( assignment-expression , type-id )
4492 __builtin_offsetof ( type-id , offsetof-expression )
4495 __has_nothrow_assign ( type-id )
4496 __has_nothrow_constructor ( type-id )
4497 __has_nothrow_copy ( type-id )
4498 __has_trivial_assign ( type-id )
4499 __has_trivial_constructor ( type-id )
4500 __has_trivial_copy ( type-id )
4501 __has_trivial_destructor ( type-id )
4502 __has_virtual_destructor ( type-id )
4503 __is_abstract ( type-id )
4504 __is_base_of ( type-id , type-id )
4505 __is_class ( type-id )
4506 __is_empty ( type-id )
4507 __is_enum ( type-id )
4508 __is_final ( type-id )
4509 __is_literal_type ( type-id )
4510 __is_pod ( type-id )
4511 __is_polymorphic ( type-id )
4512 __is_std_layout ( type-id )
4513 __is_trivial ( type-id )
4514 __is_union ( type-id )
4516 Objective-C++ Extension:
4524 ADDRESS_P is true iff this expression was immediately preceded by
4525 "&" and therefore might denote a pointer-to-member. CAST_P is true
4526 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4527 true iff this expression is a template argument.
4529 Returns a representation of the expression. Upon return, *IDK
4530 indicates what kind of id-expression (if any) was present. */
4533 cp_parser_primary_expression (cp_parser
*parser
,
4536 bool template_arg_p
,
4540 cp_token
*token
= NULL
;
4542 /* Assume the primary expression is not an id-expression. */
4543 *idk
= CP_ID_KIND_NONE
;
4545 /* Peek at the next token. */
4546 token
= cp_lexer_peek_token (parser
->lexer
);
4547 switch ((int) token
->type
)
4556 user-defined-literal */
4563 case CPP_PREPARSED_EXPR
:
4564 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4565 return cp_parser_userdef_numeric_literal (parser
);
4566 token
= cp_lexer_consume_token (parser
->lexer
);
4567 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4569 error_at (token
->location
,
4570 "fixed-point types not supported in C++");
4571 return error_mark_node
;
4573 /* Floating-point literals are only allowed in an integral
4574 constant expression if they are cast to an integral or
4575 enumeration type. */
4576 if (TREE_CODE (token
->u
.value
) == REAL_CST
4577 && parser
->integral_constant_expression_p
4580 /* CAST_P will be set even in invalid code like "int(2.7 +
4581 ...)". Therefore, we have to check that the next token
4582 is sure to end the cast. */
4585 cp_token
*next_token
;
4587 next_token
= cp_lexer_peek_token (parser
->lexer
);
4588 if (/* The comma at the end of an
4589 enumerator-definition. */
4590 next_token
->type
!= CPP_COMMA
4591 /* The curly brace at the end of an enum-specifier. */
4592 && next_token
->type
!= CPP_CLOSE_BRACE
4593 /* The end of a statement. */
4594 && next_token
->type
!= CPP_SEMICOLON
4595 /* The end of the cast-expression. */
4596 && next_token
->type
!= CPP_CLOSE_PAREN
4597 /* The end of an array bound. */
4598 && next_token
->type
!= CPP_CLOSE_SQUARE
4599 /* The closing ">" in a template-argument-list. */
4600 && (next_token
->type
!= CPP_GREATER
4601 || parser
->greater_than_is_operator_p
)
4602 /* C++0x only: A ">>" treated like two ">" tokens,
4603 in a template-argument-list. */
4604 && (next_token
->type
!= CPP_RSHIFT
4605 || (cxx_dialect
== cxx98
)
4606 || parser
->greater_than_is_operator_p
))
4610 /* If we are within a cast, then the constraint that the
4611 cast is to an integral or enumeration type will be
4612 checked at that point. If we are not within a cast, then
4613 this code is invalid. */
4615 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4617 return token
->u
.value
;
4619 case CPP_CHAR_USERDEF
:
4620 case CPP_CHAR16_USERDEF
:
4621 case CPP_CHAR32_USERDEF
:
4622 case CPP_WCHAR_USERDEF
:
4623 case CPP_UTF8CHAR_USERDEF
:
4624 return cp_parser_userdef_char_literal (parser
);
4630 case CPP_UTF8STRING
:
4631 case CPP_STRING_USERDEF
:
4632 case CPP_STRING16_USERDEF
:
4633 case CPP_STRING32_USERDEF
:
4634 case CPP_WSTRING_USERDEF
:
4635 case CPP_UTF8STRING_USERDEF
:
4636 /* ??? Should wide strings be allowed when parser->translate_strings_p
4637 is false (i.e. in attributes)? If not, we can kill the third
4638 argument to cp_parser_string_literal. */
4639 return cp_parser_string_literal (parser
,
4640 parser
->translate_strings_p
,
4643 case CPP_OPEN_PAREN
:
4644 /* If we see `( { ' then we are looking at the beginning of
4645 a GNU statement-expression. */
4646 if (cp_parser_allow_gnu_extensions_p (parser
)
4647 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4649 /* Statement-expressions are not allowed by the standard. */
4650 pedwarn (token
->location
, OPT_Wpedantic
,
4651 "ISO C++ forbids braced-groups within expressions");
4653 /* And they're not allowed outside of a function-body; you
4654 cannot, for example, write:
4656 int i = ({ int j = 3; j + 1; });
4658 at class or namespace scope. */
4659 if (!parser
->in_function_body
4660 || parser
->in_template_argument_list_p
)
4662 error_at (token
->location
,
4663 "statement-expressions are not allowed outside "
4664 "functions nor in template-argument lists");
4665 cp_parser_skip_to_end_of_block_or_statement (parser
);
4666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4667 cp_lexer_consume_token (parser
->lexer
);
4668 return error_mark_node
;
4671 return cp_parser_statement_expr (parser
);
4673 /* Otherwise it's a normal parenthesized expression. */
4676 bool saved_greater_than_is_operator_p
;
4678 /* Consume the `('. */
4679 cp_lexer_consume_token (parser
->lexer
);
4680 /* Within a parenthesized expression, a `>' token is always
4681 the greater-than operator. */
4682 saved_greater_than_is_operator_p
4683 = parser
->greater_than_is_operator_p
;
4684 parser
->greater_than_is_operator_p
= true;
4686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4687 /* Left fold expression. */
4690 /* Parse the parenthesized expression. */
4691 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4693 token
= cp_lexer_peek_token (parser
->lexer
);
4694 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
4696 expr
= cp_parser_fold_expression (parser
, expr
);
4697 if (expr
!= error_mark_node
4698 && cxx_dialect
< cxx1z
4699 && !in_system_header_at (input_location
))
4700 pedwarn (input_location
, 0, "fold-expressions only available "
4701 "with -std=c++1z or -std=gnu++1z");
4704 /* Let the front end know that this expression was
4705 enclosed in parentheses. This matters in case, for
4706 example, the expression is of the form `A::B', since
4707 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4709 expr
= finish_parenthesized_expr (expr
);
4711 /* DR 705: Wrapping an unqualified name in parentheses
4712 suppresses arg-dependent lookup. We want to pass back
4713 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4714 (c++/37862), but none of the others. */
4715 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4716 *idk
= CP_ID_KIND_NONE
;
4718 /* The `>' token might be the end of a template-id or
4719 template-parameter-list now. */
4720 parser
->greater_than_is_operator_p
4721 = saved_greater_than_is_operator_p
;
4722 /* Consume the `)'. */
4723 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
)
4724 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4725 cp_parser_skip_to_end_of_statement (parser
);
4730 case CPP_OPEN_SQUARE
:
4732 if (c_dialect_objc ())
4734 /* We might have an Objective-C++ message. */
4735 cp_parser_parse_tentatively (parser
);
4736 tree msg
= cp_parser_objc_message_expression (parser
);
4737 /* If that works out, we're done ... */
4738 if (cp_parser_parse_definitely (parser
))
4740 /* ... else, fall though to see if it's a lambda. */
4742 tree lam
= cp_parser_lambda_expression (parser
);
4743 /* Don't warn about a failed tentative parse. */
4744 if (cp_parser_error_occurred (parser
))
4745 return error_mark_node
;
4746 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4750 case CPP_OBJC_STRING
:
4751 if (c_dialect_objc ())
4752 /* We have an Objective-C++ string literal. */
4753 return cp_parser_objc_expression (parser
);
4754 cp_parser_error (parser
, "expected primary-expression");
4755 return error_mark_node
;
4758 switch (token
->keyword
)
4760 /* These two are the boolean literals. */
4762 cp_lexer_consume_token (parser
->lexer
);
4763 return boolean_true_node
;
4765 cp_lexer_consume_token (parser
->lexer
);
4766 return boolean_false_node
;
4768 /* The `__null' literal. */
4770 cp_lexer_consume_token (parser
->lexer
);
4773 /* The `nullptr' literal. */
4775 cp_lexer_consume_token (parser
->lexer
);
4776 return nullptr_node
;
4778 /* Recognize the `this' keyword. */
4780 cp_lexer_consume_token (parser
->lexer
);
4781 if (parser
->local_variables_forbidden_p
)
4783 error_at (token
->location
,
4784 "%<this%> may not be used in this context");
4785 return error_mark_node
;
4787 /* Pointers cannot appear in constant-expressions. */
4788 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4789 return error_mark_node
;
4790 return finish_this_expr ();
4792 /* The `operator' keyword can be the beginning of an
4797 case RID_FUNCTION_NAME
:
4798 case RID_PRETTY_FUNCTION_NAME
:
4799 case RID_C99_FUNCTION_NAME
:
4801 non_integral_constant name
;
4803 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4804 __func__ are the names of variables -- but they are
4805 treated specially. Therefore, they are handled here,
4806 rather than relying on the generic id-expression logic
4807 below. Grammatically, these names are id-expressions.
4809 Consume the token. */
4810 token
= cp_lexer_consume_token (parser
->lexer
);
4812 switch (token
->keyword
)
4814 case RID_FUNCTION_NAME
:
4815 name
= NIC_FUNC_NAME
;
4817 case RID_PRETTY_FUNCTION_NAME
:
4818 name
= NIC_PRETTY_FUNC
;
4820 case RID_C99_FUNCTION_NAME
:
4821 name
= NIC_C99_FUNC
;
4827 if (cp_parser_non_integral_constant_expression (parser
, name
))
4828 return error_mark_node
;
4830 /* Look up the name. */
4831 return finish_fname (token
->u
.value
);
4838 source_location type_location
;
4840 /* The `__builtin_va_arg' construct is used to handle
4841 `va_arg'. Consume the `__builtin_va_arg' token. */
4842 cp_lexer_consume_token (parser
->lexer
);
4843 /* Look for the opening `('. */
4844 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4845 /* Now, parse the assignment-expression. */
4846 expression
= cp_parser_assignment_expression (parser
);
4847 /* Look for the `,'. */
4848 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4849 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4850 /* Parse the type-id. */
4851 type
= cp_parser_type_id (parser
);
4852 /* Look for the closing `)'. */
4853 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4854 /* Using `va_arg' in a constant-expression is not
4856 if (cp_parser_non_integral_constant_expression (parser
,
4858 return error_mark_node
;
4859 return build_x_va_arg (type_location
, expression
, type
);
4863 return cp_parser_builtin_offsetof (parser
);
4865 case RID_HAS_NOTHROW_ASSIGN
:
4866 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4867 case RID_HAS_NOTHROW_COPY
:
4868 case RID_HAS_TRIVIAL_ASSIGN
:
4869 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4870 case RID_HAS_TRIVIAL_COPY
:
4871 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4872 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4873 case RID_IS_ABSTRACT
:
4874 case RID_IS_BASE_OF
:
4879 case RID_IS_LITERAL_TYPE
:
4881 case RID_IS_POLYMORPHIC
:
4882 case RID_IS_SAME_AS
:
4883 case RID_IS_STD_LAYOUT
:
4884 case RID_IS_TRIVIAL
:
4885 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4886 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4887 case RID_IS_TRIVIALLY_COPYABLE
:
4889 return cp_parser_trait_expr (parser
, token
->keyword
);
4893 return cp_parser_requires_expression (parser
);
4895 /* Objective-C++ expressions. */
4897 case RID_AT_PROTOCOL
:
4898 case RID_AT_SELECTOR
:
4899 return cp_parser_objc_expression (parser
);
4902 if (parser
->in_function_body
4903 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4906 error_at (token
->location
,
4907 "a template declaration cannot appear at block scope");
4908 cp_parser_skip_to_end_of_block_or_statement (parser
);
4909 return error_mark_node
;
4912 cp_parser_error (parser
, "expected primary-expression");
4913 return error_mark_node
;
4916 /* An id-expression can start with either an identifier, a
4917 `::' as the beginning of a qualified-id, or the "operator"
4921 case CPP_TEMPLATE_ID
:
4922 case CPP_NESTED_NAME_SPECIFIER
:
4926 const char *error_msg
;
4929 cp_token
*id_expr_token
;
4932 /* Parse the id-expression. */
4934 = cp_parser_id_expression (parser
,
4935 /*template_keyword_p=*/false,
4936 /*check_dependency_p=*/true,
4938 /*declarator_p=*/false,
4939 /*optional_p=*/false);
4940 if (id_expression
== error_mark_node
)
4941 return error_mark_node
;
4942 id_expr_token
= token
;
4943 token
= cp_lexer_peek_token (parser
->lexer
);
4944 done
= (token
->type
!= CPP_OPEN_SQUARE
4945 && token
->type
!= CPP_OPEN_PAREN
4946 && token
->type
!= CPP_DOT
4947 && token
->type
!= CPP_DEREF
4948 && token
->type
!= CPP_PLUS_PLUS
4949 && token
->type
!= CPP_MINUS_MINUS
);
4950 /* If we have a template-id, then no further lookup is
4951 required. If the template-id was for a template-class, we
4952 will sometimes have a TYPE_DECL at this point. */
4953 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4954 || TREE_CODE (id_expression
) == TYPE_DECL
)
4955 decl
= id_expression
;
4956 /* Look up the name. */
4959 tree ambiguous_decls
;
4961 /* If we already know that this lookup is ambiguous, then
4962 we've already issued an error message; there's no reason
4964 if (id_expr_token
->type
== CPP_NAME
4965 && id_expr_token
->error_reported
)
4967 cp_parser_simulate_error (parser
);
4968 return error_mark_node
;
4971 decl
= cp_parser_lookup_name (parser
, id_expression
,
4974 /*is_namespace=*/false,
4975 /*check_dependency=*/true,
4977 id_expr_token
->location
);
4978 /* If the lookup was ambiguous, an error will already have
4980 if (ambiguous_decls
)
4981 return error_mark_node
;
4983 /* In Objective-C++, we may have an Objective-C 2.0
4984 dot-syntax for classes here. */
4985 if (c_dialect_objc ()
4986 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4987 && TREE_CODE (decl
) == TYPE_DECL
4988 && objc_is_class_name (decl
))
4991 cp_lexer_consume_token (parser
->lexer
);
4992 component
= cp_parser_identifier (parser
);
4993 if (component
== error_mark_node
)
4994 return error_mark_node
;
4996 return objc_build_class_component_ref (id_expression
, component
);
4999 /* In Objective-C++, an instance variable (ivar) may be preferred
5000 to whatever cp_parser_lookup_name() found. */
5001 decl
= objc_lookup_ivar (decl
, id_expression
);
5003 /* If name lookup gives us a SCOPE_REF, then the
5004 qualifying scope was dependent. */
5005 if (TREE_CODE (decl
) == SCOPE_REF
)
5007 /* At this point, we do not know if DECL is a valid
5008 integral constant expression. We assume that it is
5009 in fact such an expression, so that code like:
5011 template <int N> struct A {
5015 is accepted. At template-instantiation time, we
5016 will check that B<N>::i is actually a constant. */
5019 /* Check to see if DECL is a local variable in a context
5020 where that is forbidden. */
5021 if (parser
->local_variables_forbidden_p
5022 && local_variable_p (decl
))
5024 /* It might be that we only found DECL because we are
5025 trying to be generous with pre-ISO scoping rules.
5026 For example, consider:
5030 for (int i = 0; i < 10; ++i) {}
5031 extern void f(int j = i);
5034 Here, name look up will originally find the out
5035 of scope `i'. We need to issue a warning message,
5036 but then use the global `i'. */
5037 decl
= check_for_out_of_scope_variable (decl
);
5038 if (local_variable_p (decl
))
5040 error_at (id_expr_token
->location
,
5041 "local variable %qD may not appear in this context",
5043 return error_mark_node
;
5048 decl
= (finish_id_expression
5049 (id_expression
, decl
, parser
->scope
,
5051 parser
->integral_constant_expression_p
,
5052 parser
->allow_non_integral_constant_expression_p
,
5053 &parser
->non_integral_constant_expression_p
,
5054 template_p
, done
, address_p
,
5057 id_expr_token
->location
));
5059 cp_parser_error (parser
, error_msg
);
5063 /* Anything else is an error. */
5065 cp_parser_error (parser
, "expected primary-expression");
5066 return error_mark_node
;
5071 cp_parser_primary_expression (cp_parser
*parser
,
5074 bool template_arg_p
,
5077 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5078 /*decltype*/false, idk
);
5081 /* Parse an id-expression.
5088 :: [opt] nested-name-specifier template [opt] unqualified-id
5090 :: operator-function-id
5093 Return a representation of the unqualified portion of the
5094 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5095 a `::' or nested-name-specifier.
5097 Often, if the id-expression was a qualified-id, the caller will
5098 want to make a SCOPE_REF to represent the qualified-id. This
5099 function does not do this in order to avoid wastefully creating
5100 SCOPE_REFs when they are not required.
5102 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5105 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5106 uninstantiated templates.
5108 If *TEMPLATE_P is non-NULL, it is set to true iff the
5109 `template' keyword is used to explicitly indicate that the entity
5110 named is a template.
5112 If DECLARATOR_P is true, the id-expression is appearing as part of
5113 a declarator, rather than as part of an expression. */
5116 cp_parser_id_expression (cp_parser
*parser
,
5117 bool template_keyword_p
,
5118 bool check_dependency_p
,
5123 bool global_scope_p
;
5124 bool nested_name_specifier_p
;
5126 /* Assume the `template' keyword was not used. */
5128 *template_p
= template_keyword_p
;
5130 /* Look for the optional `::' operator. */
5132 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5134 /* Look for the optional nested-name-specifier. */
5135 nested_name_specifier_p
5136 = (cp_parser_nested_name_specifier_opt (parser
,
5137 /*typename_keyword_p=*/false,
5142 /* If there is a nested-name-specifier, then we are looking at
5143 the first qualified-id production. */
5144 if (nested_name_specifier_p
)
5147 tree saved_object_scope
;
5148 tree saved_qualifying_scope
;
5149 tree unqualified_id
;
5152 /* See if the next token is the `template' keyword. */
5154 template_p
= &is_template
;
5155 *template_p
= cp_parser_optional_template_keyword (parser
);
5156 /* Name lookup we do during the processing of the
5157 unqualified-id might obliterate SCOPE. */
5158 saved_scope
= parser
->scope
;
5159 saved_object_scope
= parser
->object_scope
;
5160 saved_qualifying_scope
= parser
->qualifying_scope
;
5161 /* Process the final unqualified-id. */
5162 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5165 /*optional_p=*/false);
5166 /* Restore the SAVED_SCOPE for our caller. */
5167 parser
->scope
= saved_scope
;
5168 parser
->object_scope
= saved_object_scope
;
5169 parser
->qualifying_scope
= saved_qualifying_scope
;
5171 return unqualified_id
;
5173 /* Otherwise, if we are in global scope, then we are looking at one
5174 of the other qualified-id productions. */
5175 else if (global_scope_p
)
5180 /* Peek at the next token. */
5181 token
= cp_lexer_peek_token (parser
->lexer
);
5183 /* If it's an identifier, and the next token is not a "<", then
5184 we can avoid the template-id case. This is an optimization
5185 for this common case. */
5186 if (token
->type
== CPP_NAME
5187 && !cp_parser_nth_token_starts_template_argument_list_p
5189 return cp_parser_identifier (parser
);
5191 cp_parser_parse_tentatively (parser
);
5192 /* Try a template-id. */
5193 id
= cp_parser_template_id (parser
,
5194 /*template_keyword_p=*/false,
5195 /*check_dependency_p=*/true,
5198 /* If that worked, we're done. */
5199 if (cp_parser_parse_definitely (parser
))
5202 /* Peek at the next token. (Changes in the token buffer may
5203 have invalidated the pointer obtained above.) */
5204 token
= cp_lexer_peek_token (parser
->lexer
);
5206 switch (token
->type
)
5209 return cp_parser_identifier (parser
);
5212 if (token
->keyword
== RID_OPERATOR
)
5213 return cp_parser_operator_function_id (parser
);
5217 cp_parser_error (parser
, "expected id-expression");
5218 return error_mark_node
;
5222 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5223 /*check_dependency_p=*/true,
5228 /* Parse an unqualified-id.
5232 operator-function-id
5233 conversion-function-id
5237 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5238 keyword, in a construct like `A::template ...'.
5240 Returns a representation of unqualified-id. For the `identifier'
5241 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5242 production a BIT_NOT_EXPR is returned; the operand of the
5243 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5244 other productions, see the documentation accompanying the
5245 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5246 names are looked up in uninstantiated templates. If DECLARATOR_P
5247 is true, the unqualified-id is appearing as part of a declarator,
5248 rather than as part of an expression. */
5251 cp_parser_unqualified_id (cp_parser
* parser
,
5252 bool template_keyword_p
,
5253 bool check_dependency_p
,
5259 /* Peek at the next token. */
5260 token
= cp_lexer_peek_token (parser
->lexer
);
5262 switch ((int) token
->type
)
5268 /* We don't know yet whether or not this will be a
5270 cp_parser_parse_tentatively (parser
);
5271 /* Try a template-id. */
5272 id
= cp_parser_template_id (parser
, template_keyword_p
,
5276 /* If it worked, we're done. */
5277 if (cp_parser_parse_definitely (parser
))
5279 /* Otherwise, it's an ordinary identifier. */
5280 return cp_parser_identifier (parser
);
5283 case CPP_TEMPLATE_ID
:
5284 return cp_parser_template_id (parser
, template_keyword_p
,
5292 tree qualifying_scope
;
5297 /* Consume the `~' token. */
5298 cp_lexer_consume_token (parser
->lexer
);
5299 /* Parse the class-name. The standard, as written, seems to
5302 template <typename T> struct S { ~S (); };
5303 template <typename T> S<T>::~S() {}
5305 is invalid, since `~' must be followed by a class-name, but
5306 `S<T>' is dependent, and so not known to be a class.
5307 That's not right; we need to look in uninstantiated
5308 templates. A further complication arises from:
5310 template <typename T> void f(T t) {
5314 Here, it is not possible to look up `T' in the scope of `T'
5315 itself. We must look in both the current scope, and the
5316 scope of the containing complete expression.
5318 Yet another issue is:
5327 The standard does not seem to say that the `S' in `~S'
5328 should refer to the type `S' and not the data member
5331 /* DR 244 says that we look up the name after the "~" in the
5332 same scope as we looked up the qualifying name. That idea
5333 isn't fully worked out; it's more complicated than that. */
5334 scope
= parser
->scope
;
5335 object_scope
= parser
->object_scope
;
5336 qualifying_scope
= parser
->qualifying_scope
;
5338 /* Check for invalid scopes. */
5339 if (scope
== error_mark_node
)
5341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5342 cp_lexer_consume_token (parser
->lexer
);
5343 return error_mark_node
;
5345 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5348 error_at (token
->location
,
5349 "scope %qT before %<~%> is not a class-name",
5351 cp_parser_simulate_error (parser
);
5352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5353 cp_lexer_consume_token (parser
->lexer
);
5354 return error_mark_node
;
5356 gcc_assert (!scope
|| TYPE_P (scope
));
5358 /* If the name is of the form "X::~X" it's OK even if X is a
5360 token
= cp_lexer_peek_token (parser
->lexer
);
5362 && token
->type
== CPP_NAME
5363 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5365 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5366 || (CLASS_TYPE_P (scope
)
5367 && constructor_name_p (token
->u
.value
, scope
))))
5369 cp_lexer_consume_token (parser
->lexer
);
5370 return build_nt (BIT_NOT_EXPR
, scope
);
5373 /* ~auto means the destructor of whatever the object is. */
5374 if (cp_parser_is_keyword (token
, RID_AUTO
))
5376 if (cxx_dialect
< cxx14
)
5377 pedwarn (input_location
, 0,
5378 "%<~auto%> only available with "
5379 "-std=c++14 or -std=gnu++14");
5380 cp_lexer_consume_token (parser
->lexer
);
5381 return build_nt (BIT_NOT_EXPR
, make_auto ());
5384 /* If there was an explicit qualification (S::~T), first look
5385 in the scope given by the qualification (i.e., S).
5387 Note: in the calls to cp_parser_class_name below we pass
5388 typename_type so that lookup finds the injected-class-name
5389 rather than the constructor. */
5391 type_decl
= NULL_TREE
;
5394 cp_parser_parse_tentatively (parser
);
5395 type_decl
= cp_parser_class_name (parser
,
5396 /*typename_keyword_p=*/false,
5397 /*template_keyword_p=*/false,
5399 /*check_dependency=*/false,
5400 /*class_head_p=*/false,
5402 if (cp_parser_parse_definitely (parser
))
5405 /* In "N::S::~S", look in "N" as well. */
5406 if (!done
&& scope
&& qualifying_scope
)
5408 cp_parser_parse_tentatively (parser
);
5409 parser
->scope
= qualifying_scope
;
5410 parser
->object_scope
= NULL_TREE
;
5411 parser
->qualifying_scope
= NULL_TREE
;
5413 = cp_parser_class_name (parser
,
5414 /*typename_keyword_p=*/false,
5415 /*template_keyword_p=*/false,
5417 /*check_dependency=*/false,
5418 /*class_head_p=*/false,
5420 if (cp_parser_parse_definitely (parser
))
5423 /* In "p->S::~T", look in the scope given by "*p" as well. */
5424 else if (!done
&& object_scope
)
5426 cp_parser_parse_tentatively (parser
);
5427 parser
->scope
= object_scope
;
5428 parser
->object_scope
= NULL_TREE
;
5429 parser
->qualifying_scope
= NULL_TREE
;
5431 = cp_parser_class_name (parser
,
5432 /*typename_keyword_p=*/false,
5433 /*template_keyword_p=*/false,
5435 /*check_dependency=*/false,
5436 /*class_head_p=*/false,
5438 if (cp_parser_parse_definitely (parser
))
5441 /* Look in the surrounding context. */
5444 parser
->scope
= NULL_TREE
;
5445 parser
->object_scope
= NULL_TREE
;
5446 parser
->qualifying_scope
= NULL_TREE
;
5447 if (processing_template_decl
)
5448 cp_parser_parse_tentatively (parser
);
5450 = cp_parser_class_name (parser
,
5451 /*typename_keyword_p=*/false,
5452 /*template_keyword_p=*/false,
5454 /*check_dependency=*/false,
5455 /*class_head_p=*/false,
5457 if (processing_template_decl
5458 && ! cp_parser_parse_definitely (parser
))
5460 /* We couldn't find a type with this name. If we're parsing
5461 tentatively, fail and try something else. */
5462 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5464 cp_parser_simulate_error (parser
);
5465 return error_mark_node
;
5467 /* Otherwise, accept it and check for a match at instantiation
5469 type_decl
= cp_parser_identifier (parser
);
5470 if (type_decl
!= error_mark_node
)
5471 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5475 /* If an error occurred, assume that the name of the
5476 destructor is the same as the name of the qualifying
5477 class. That allows us to keep parsing after running
5478 into ill-formed destructor names. */
5479 if (type_decl
== error_mark_node
&& scope
)
5480 return build_nt (BIT_NOT_EXPR
, scope
);
5481 else if (type_decl
== error_mark_node
)
5482 return error_mark_node
;
5484 /* Check that destructor name and scope match. */
5485 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5487 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5488 error_at (token
->location
,
5489 "declaration of %<~%T%> as member of %qT",
5491 cp_parser_simulate_error (parser
);
5492 return error_mark_node
;
5497 A typedef-name that names a class shall not be used as the
5498 identifier in the declarator for a destructor declaration. */
5500 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5501 && !DECL_SELF_REFERENCE_P (type_decl
)
5502 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5503 error_at (token
->location
,
5504 "typedef-name %qD used as destructor declarator",
5507 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5511 if (token
->keyword
== RID_OPERATOR
)
5515 /* This could be a template-id, so we try that first. */
5516 cp_parser_parse_tentatively (parser
);
5517 /* Try a template-id. */
5518 id
= cp_parser_template_id (parser
, template_keyword_p
,
5519 /*check_dependency_p=*/true,
5522 /* If that worked, we're done. */
5523 if (cp_parser_parse_definitely (parser
))
5525 /* We still don't know whether we're looking at an
5526 operator-function-id or a conversion-function-id. */
5527 cp_parser_parse_tentatively (parser
);
5528 /* Try an operator-function-id. */
5529 id
= cp_parser_operator_function_id (parser
);
5530 /* If that didn't work, try a conversion-function-id. */
5531 if (!cp_parser_parse_definitely (parser
))
5532 id
= cp_parser_conversion_function_id (parser
);
5533 else if (UDLIT_OPER_P (id
))
5536 const char *name
= UDLIT_OP_SUFFIX (id
);
5537 if (name
[0] != '_' && !in_system_header_at (input_location
)
5539 warning (0, "literal operator suffixes not preceded by %<_%>"
5540 " are reserved for future standardization");
5550 cp_parser_error (parser
, "expected unqualified-id");
5551 return error_mark_node
;
5555 /* Parse an (optional) nested-name-specifier.
5557 nested-name-specifier: [C++98]
5558 class-or-namespace-name :: nested-name-specifier [opt]
5559 class-or-namespace-name :: template nested-name-specifier [opt]
5561 nested-name-specifier: [C++0x]
5564 nested-name-specifier identifier ::
5565 nested-name-specifier template [opt] simple-template-id ::
5567 PARSER->SCOPE should be set appropriately before this function is
5568 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5569 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5572 Sets PARSER->SCOPE to the class (TYPE) or namespace
5573 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5574 it unchanged if there is no nested-name-specifier. Returns the new
5575 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5577 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5578 part of a declaration and/or decl-specifier. */
5581 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5582 bool typename_keyword_p
,
5583 bool check_dependency_p
,
5585 bool is_declaration
)
5587 bool success
= false;
5588 cp_token_position start
= 0;
5591 /* Remember where the nested-name-specifier starts. */
5592 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5594 start
= cp_lexer_token_position (parser
->lexer
, false);
5595 push_deferring_access_checks (dk_deferred
);
5602 tree saved_qualifying_scope
;
5603 bool template_keyword_p
;
5605 /* Spot cases that cannot be the beginning of a
5606 nested-name-specifier. */
5607 token
= cp_lexer_peek_token (parser
->lexer
);
5609 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5610 the already parsed nested-name-specifier. */
5611 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5613 /* Grab the nested-name-specifier and continue the loop. */
5614 cp_parser_pre_parsed_nested_name_specifier (parser
);
5615 /* If we originally encountered this nested-name-specifier
5616 with IS_DECLARATION set to false, we will not have
5617 resolved TYPENAME_TYPEs, so we must do so here. */
5619 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5621 new_scope
= resolve_typename_type (parser
->scope
,
5622 /*only_current_p=*/false);
5623 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5624 parser
->scope
= new_scope
;
5630 /* Spot cases that cannot be the beginning of a
5631 nested-name-specifier. On the second and subsequent times
5632 through the loop, we look for the `template' keyword. */
5633 if (success
&& token
->keyword
== RID_TEMPLATE
)
5635 /* A template-id can start a nested-name-specifier. */
5636 else if (token
->type
== CPP_TEMPLATE_ID
)
5638 /* DR 743: decltype can be used in a nested-name-specifier. */
5639 else if (token_is_decltype (token
))
5643 /* If the next token is not an identifier, then it is
5644 definitely not a type-name or namespace-name. */
5645 if (token
->type
!= CPP_NAME
)
5647 /* If the following token is neither a `<' (to begin a
5648 template-id), nor a `::', then we are not looking at a
5649 nested-name-specifier. */
5650 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5652 if (token
->type
== CPP_COLON
5653 && parser
->colon_corrects_to_scope_p
5654 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5656 error_at (token
->location
,
5657 "found %<:%> in nested-name-specifier, expected %<::%>");
5658 token
->type
= CPP_SCOPE
;
5661 if (token
->type
!= CPP_SCOPE
5662 && !cp_parser_nth_token_starts_template_argument_list_p
5667 /* The nested-name-specifier is optional, so we parse
5669 cp_parser_parse_tentatively (parser
);
5671 /* Look for the optional `template' keyword, if this isn't the
5672 first time through the loop. */
5674 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5676 template_keyword_p
= false;
5678 /* Save the old scope since the name lookup we are about to do
5679 might destroy it. */
5680 old_scope
= parser
->scope
;
5681 saved_qualifying_scope
= parser
->qualifying_scope
;
5682 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5683 look up names in "X<T>::I" in order to determine that "Y" is
5684 a template. So, if we have a typename at this point, we make
5685 an effort to look through it. */
5687 && !typename_keyword_p
5689 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5690 parser
->scope
= resolve_typename_type (parser
->scope
,
5691 /*only_current_p=*/false);
5692 /* Parse the qualifying entity. */
5694 = cp_parser_qualifying_entity (parser
,
5700 /* Look for the `::' token. */
5701 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5703 /* If we found what we wanted, we keep going; otherwise, we're
5705 if (!cp_parser_parse_definitely (parser
))
5707 bool error_p
= false;
5709 /* Restore the OLD_SCOPE since it was valid before the
5710 failed attempt at finding the last
5711 class-or-namespace-name. */
5712 parser
->scope
= old_scope
;
5713 parser
->qualifying_scope
= saved_qualifying_scope
;
5715 /* If the next token is a decltype, and the one after that is a
5716 `::', then the decltype has failed to resolve to a class or
5717 enumeration type. Give this error even when parsing
5718 tentatively since it can't possibly be valid--and we're going
5719 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5720 won't get another chance.*/
5721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5722 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5725 token
= cp_lexer_consume_token (parser
->lexer
);
5726 error_at (token
->location
, "decltype evaluates to %qT, "
5727 "which is not a class or enumeration type",
5729 parser
->scope
= error_mark_node
;
5733 cp_lexer_consume_token (parser
->lexer
);
5736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5737 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5739 /* If we have a non-type template-id followed by ::, it can't
5740 possibly be valid. */
5741 token
= cp_lexer_peek_token (parser
->lexer
);
5742 tree tid
= token
->u
.tree_check_value
->value
;
5743 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5744 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5746 tree tmpl
= NULL_TREE
;
5747 if (is_overloaded_fn (tid
))
5749 tree fns
= get_fns (tid
);
5750 if (!OVL_CHAIN (fns
))
5751 tmpl
= OVL_CURRENT (fns
);
5752 error_at (token
->location
, "function template-id %qD "
5753 "in nested-name-specifier", tid
);
5757 /* Variable template. */
5758 tmpl
= TREE_OPERAND (tid
, 0);
5759 gcc_assert (variable_template_p (tmpl
));
5760 error_at (token
->location
, "variable template-id %qD "
5761 "in nested-name-specifier", tid
);
5764 inform (DECL_SOURCE_LOCATION (tmpl
),
5765 "%qD declared here", tmpl
);
5767 parser
->scope
= error_mark_node
;
5771 cp_lexer_consume_token (parser
->lexer
);
5772 cp_lexer_consume_token (parser
->lexer
);
5776 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5778 /* If the next token is an identifier, and the one after
5779 that is a `::', then any valid interpretation would have
5780 found a class-or-namespace-name. */
5781 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5782 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5784 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5787 token
= cp_lexer_consume_token (parser
->lexer
);
5790 if (!token
->error_reported
)
5793 tree ambiguous_decls
;
5795 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5797 /*is_template=*/false,
5798 /*is_namespace=*/false,
5799 /*check_dependency=*/true,
5802 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5803 error_at (token
->location
,
5804 "%qD used without template parameters",
5806 else if (ambiguous_decls
)
5808 // cp_parser_lookup_name has the same diagnostic,
5809 // thus make sure to emit it at most once.
5810 if (cp_parser_uncommitted_to_tentative_parse_p
5813 error_at (token
->location
,
5814 "reference to %qD is ambiguous",
5816 print_candidates (ambiguous_decls
);
5818 decl
= error_mark_node
;
5822 if (cxx_dialect
!= cxx98
)
5823 cp_parser_name_lookup_error
5824 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5827 cp_parser_name_lookup_error
5828 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5832 parser
->scope
= error_mark_node
;
5834 /* Treat this as a successful nested-name-specifier
5839 If the name found is not a class-name (clause
5840 _class_) or namespace-name (_namespace.def_), the
5841 program is ill-formed. */
5844 cp_lexer_consume_token (parser
->lexer
);
5848 /* We've found one valid nested-name-specifier. */
5850 /* Name lookup always gives us a DECL. */
5851 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5852 new_scope
= TREE_TYPE (new_scope
);
5853 /* Uses of "template" must be followed by actual templates. */
5854 if (template_keyword_p
5855 && !(CLASS_TYPE_P (new_scope
)
5856 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5857 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5858 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5859 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5860 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5861 == TEMPLATE_ID_EXPR
)))
5862 permerror (input_location
, TYPE_P (new_scope
)
5863 ? G_("%qT is not a template")
5864 : G_("%qD is not a template"),
5866 /* If it is a class scope, try to complete it; we are about to
5867 be looking up names inside the class. */
5868 if (TYPE_P (new_scope
)
5869 /* Since checking types for dependency can be expensive,
5870 avoid doing it if the type is already complete. */
5871 && !COMPLETE_TYPE_P (new_scope
)
5872 /* Do not try to complete dependent types. */
5873 && !dependent_type_p (new_scope
))
5875 new_scope
= complete_type (new_scope
);
5876 /* If it is a typedef to current class, use the current
5877 class instead, as the typedef won't have any names inside
5879 if (!COMPLETE_TYPE_P (new_scope
)
5880 && currently_open_class (new_scope
))
5881 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5883 /* Make sure we look in the right scope the next time through
5885 parser
->scope
= new_scope
;
5888 /* If parsing tentatively, replace the sequence of tokens that makes
5889 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5890 token. That way, should we re-parse the token stream, we will
5891 not have to repeat the effort required to do the parse, nor will
5892 we issue duplicate error messages. */
5893 if (success
&& start
)
5897 token
= cp_lexer_token_at (parser
->lexer
, start
);
5898 /* Reset the contents of the START token. */
5899 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5900 /* Retrieve any deferred checks. Do not pop this access checks yet
5901 so the memory will not be reclaimed during token replacing below. */
5902 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5903 token
->u
.tree_check_value
->value
= parser
->scope
;
5904 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5905 token
->u
.tree_check_value
->qualifying_scope
=
5906 parser
->qualifying_scope
;
5907 token
->keyword
= RID_MAX
;
5909 /* Purge all subsequent tokens. */
5910 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5914 pop_to_parent_deferring_access_checks ();
5916 return success
? parser
->scope
: NULL_TREE
;
5919 /* Parse a nested-name-specifier. See
5920 cp_parser_nested_name_specifier_opt for details. This function
5921 behaves identically, except that it will an issue an error if no
5922 nested-name-specifier is present. */
5925 cp_parser_nested_name_specifier (cp_parser
*parser
,
5926 bool typename_keyword_p
,
5927 bool check_dependency_p
,
5929 bool is_declaration
)
5933 /* Look for the nested-name-specifier. */
5934 scope
= cp_parser_nested_name_specifier_opt (parser
,
5939 /* If it was not present, issue an error message. */
5942 cp_parser_error (parser
, "expected nested-name-specifier");
5943 parser
->scope
= NULL_TREE
;
5949 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5950 this is either a class-name or a namespace-name (which corresponds
5951 to the class-or-namespace-name production in the grammar). For
5952 C++0x, it can also be a type-name that refers to an enumeration
5953 type or a simple-template-id.
5955 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5956 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5957 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5958 TYPE_P is TRUE iff the next name should be taken as a class-name,
5959 even the same name is declared to be another entity in the same
5962 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5963 specified by the class-or-namespace-name. If neither is found the
5964 ERROR_MARK_NODE is returned. */
5967 cp_parser_qualifying_entity (cp_parser
*parser
,
5968 bool typename_keyword_p
,
5969 bool template_keyword_p
,
5970 bool check_dependency_p
,
5972 bool is_declaration
)
5975 tree saved_qualifying_scope
;
5976 tree saved_object_scope
;
5979 bool successful_parse_p
;
5981 /* DR 743: decltype can appear in a nested-name-specifier. */
5982 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5984 scope
= cp_parser_decltype (parser
);
5985 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5986 && !MAYBE_CLASS_TYPE_P (scope
))
5988 cp_parser_simulate_error (parser
);
5989 return error_mark_node
;
5991 if (TYPE_NAME (scope
))
5992 scope
= TYPE_NAME (scope
);
5996 /* Before we try to parse the class-name, we must save away the
5997 current PARSER->SCOPE since cp_parser_class_name will destroy
5999 saved_scope
= parser
->scope
;
6000 saved_qualifying_scope
= parser
->qualifying_scope
;
6001 saved_object_scope
= parser
->object_scope
;
6002 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6003 there is no need to look for a namespace-name. */
6004 only_class_p
= template_keyword_p
6005 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6007 cp_parser_parse_tentatively (parser
);
6008 scope
= cp_parser_class_name (parser
,
6011 type_p
? class_type
: none_type
,
6013 /*class_head_p=*/false,
6015 /*enum_ok=*/cxx_dialect
> cxx98
);
6016 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6017 /* If that didn't work, try for a namespace-name. */
6018 if (!only_class_p
&& !successful_parse_p
)
6020 /* Restore the saved scope. */
6021 parser
->scope
= saved_scope
;
6022 parser
->qualifying_scope
= saved_qualifying_scope
;
6023 parser
->object_scope
= saved_object_scope
;
6024 /* If we are not looking at an identifier followed by the scope
6025 resolution operator, then this is not part of a
6026 nested-name-specifier. (Note that this function is only used
6027 to parse the components of a nested-name-specifier.) */
6028 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6029 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6030 return error_mark_node
;
6031 scope
= cp_parser_namespace_name (parser
);
6037 /* Return true if we are looking at a compound-literal, false otherwise. */
6040 cp_parser_compound_literal_p (cp_parser
*parser
)
6042 /* Consume the `('. */
6043 cp_lexer_consume_token (parser
->lexer
);
6045 cp_lexer_save_tokens (parser
->lexer
);
6047 /* Skip tokens until the next token is a closing parenthesis.
6048 If we find the closing `)', and the next token is a `{', then
6049 we are looking at a compound-literal. */
6050 bool compound_literal_p
6051 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6052 /*consume_paren=*/true)
6053 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6055 /* Roll back the tokens we skipped. */
6056 cp_lexer_rollback_tokens (parser
->lexer
);
6058 return compound_literal_p
;
6061 /* Parse a postfix-expression.
6065 postfix-expression [ expression ]
6066 postfix-expression ( expression-list [opt] )
6067 simple-type-specifier ( expression-list [opt] )
6068 typename :: [opt] nested-name-specifier identifier
6069 ( expression-list [opt] )
6070 typename :: [opt] nested-name-specifier template [opt] template-id
6071 ( expression-list [opt] )
6072 postfix-expression . template [opt] id-expression
6073 postfix-expression -> template [opt] id-expression
6074 postfix-expression . pseudo-destructor-name
6075 postfix-expression -> pseudo-destructor-name
6076 postfix-expression ++
6077 postfix-expression --
6078 dynamic_cast < type-id > ( expression )
6079 static_cast < type-id > ( expression )
6080 reinterpret_cast < type-id > ( expression )
6081 const_cast < type-id > ( expression )
6082 typeid ( expression )
6088 ( type-id ) { initializer-list , [opt] }
6090 This extension is a GNU version of the C99 compound-literal
6091 construct. (The C99 grammar uses `type-name' instead of `type-id',
6092 but they are essentially the same concept.)
6094 If ADDRESS_P is true, the postfix expression is the operand of the
6095 `&' operator. CAST_P is true if this expression is the target of a
6098 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6099 class member access expressions [expr.ref].
6101 Returns a representation of the expression. */
6104 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6105 bool member_access_only_p
, bool decltype_p
,
6106 cp_id_kind
* pidk_return
)
6111 cp_id_kind idk
= CP_ID_KIND_NONE
;
6112 tree postfix_expression
= NULL_TREE
;
6113 bool is_member_access
= false;
6114 int saved_in_statement
= -1;
6116 /* Peek at the next token. */
6117 token
= cp_lexer_peek_token (parser
->lexer
);
6118 loc
= token
->location
;
6119 /* Some of the productions are determined by keywords. */
6120 keyword
= token
->keyword
;
6130 const char *saved_message
;
6131 bool saved_in_type_id_in_expr_p
;
6133 /* All of these can be handled in the same way from the point
6134 of view of parsing. Begin by consuming the token
6135 identifying the cast. */
6136 cp_lexer_consume_token (parser
->lexer
);
6138 /* New types cannot be defined in the cast. */
6139 saved_message
= parser
->type_definition_forbidden_message
;
6140 parser
->type_definition_forbidden_message
6141 = G_("types may not be defined in casts");
6143 /* Look for the opening `<'. */
6144 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6145 /* Parse the type to which we are casting. */
6146 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6147 parser
->in_type_id_in_expr_p
= true;
6148 type
= cp_parser_type_id (parser
);
6149 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6150 /* Look for the closing `>'. */
6151 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6152 /* Restore the old message. */
6153 parser
->type_definition_forbidden_message
= saved_message
;
6155 bool saved_greater_than_is_operator_p
6156 = parser
->greater_than_is_operator_p
;
6157 parser
->greater_than_is_operator_p
= true;
6159 /* And the expression which is being cast. */
6160 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6161 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6162 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6164 parser
->greater_than_is_operator_p
6165 = saved_greater_than_is_operator_p
;
6167 /* Only type conversions to integral or enumeration types
6168 can be used in constant-expressions. */
6169 if (!cast_valid_in_integral_constant_expression_p (type
)
6170 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6171 return error_mark_node
;
6177 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6181 = build_static_cast (type
, expression
, tf_warning_or_error
);
6185 = build_reinterpret_cast (type
, expression
,
6186 tf_warning_or_error
);
6190 = build_const_cast (type
, expression
, tf_warning_or_error
);
6201 const char *saved_message
;
6202 bool saved_in_type_id_in_expr_p
;
6204 /* Consume the `typeid' token. */
6205 cp_lexer_consume_token (parser
->lexer
);
6206 /* Look for the `(' token. */
6207 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6208 /* Types cannot be defined in a `typeid' expression. */
6209 saved_message
= parser
->type_definition_forbidden_message
;
6210 parser
->type_definition_forbidden_message
6211 = G_("types may not be defined in a %<typeid%> expression");
6212 /* We can't be sure yet whether we're looking at a type-id or an
6214 cp_parser_parse_tentatively (parser
);
6215 /* Try a type-id first. */
6216 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6217 parser
->in_type_id_in_expr_p
= true;
6218 type
= cp_parser_type_id (parser
);
6219 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6220 /* Look for the `)' token. Otherwise, we can't be sure that
6221 we're not looking at an expression: consider `typeid (int
6222 (3))', for example. */
6223 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6224 /* If all went well, simply lookup the type-id. */
6225 if (cp_parser_parse_definitely (parser
))
6226 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6227 /* Otherwise, fall back to the expression variant. */
6232 /* Look for an expression. */
6233 expression
= cp_parser_expression (parser
, & idk
);
6234 /* Compute its typeid. */
6235 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6236 /* Look for the `)' token. */
6237 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6239 /* Restore the saved message. */
6240 parser
->type_definition_forbidden_message
= saved_message
;
6241 /* `typeid' may not appear in an integral constant expression. */
6242 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6243 return error_mark_node
;
6250 /* The syntax permitted here is the same permitted for an
6251 elaborated-type-specifier. */
6252 ++parser
->prevent_constrained_type_specifiers
;
6253 type
= cp_parser_elaborated_type_specifier (parser
,
6254 /*is_friend=*/false,
6255 /*is_declaration=*/false);
6256 --parser
->prevent_constrained_type_specifiers
;
6257 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6261 case RID_CILK_SPAWN
:
6263 cp_lexer_consume_token (parser
->lexer
);
6264 token
= cp_lexer_peek_token (parser
->lexer
);
6265 if (token
->type
== CPP_SEMICOLON
)
6267 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6269 postfix_expression
= error_mark_node
;
6272 else if (!current_function_decl
)
6274 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6275 "inside a function");
6276 postfix_expression
= error_mark_node
;
6281 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6282 saved_in_statement
= parser
->in_statement
;
6283 parser
->in_statement
|= IN_CILK_SPAWN
;
6285 cfun
->calls_cilk_spawn
= 1;
6286 postfix_expression
=
6287 cp_parser_postfix_expression (parser
, false, false,
6288 false, false, &idk
);
6291 error_at (token
->location
, "-fcilkplus must be enabled to use"
6292 " %<_Cilk_spawn%>");
6293 cfun
->calls_cilk_spawn
= 0;
6295 else if (saved_in_statement
& IN_CILK_SPAWN
)
6297 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6298 "are not permitted");
6299 postfix_expression
= error_mark_node
;
6300 cfun
->calls_cilk_spawn
= 0;
6304 postfix_expression
= build_cilk_spawn (token
->location
,
6305 postfix_expression
);
6306 if (postfix_expression
!= error_mark_node
)
6307 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6308 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6313 case RID_BUILTIN_SHUFFLE
:
6315 vec
<tree
, va_gc
> *vec
;
6319 cp_lexer_consume_token (parser
->lexer
);
6320 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6321 /*cast_p=*/false, /*allow_expansion_p=*/true,
6322 /*non_constant_p=*/NULL
);
6324 return error_mark_node
;
6326 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6329 if (vec
->length () == 2)
6330 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6331 tf_warning_or_error
);
6332 else if (vec
->length () == 3)
6333 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6334 tf_warning_or_error
);
6337 error_at (loc
, "wrong number of arguments to "
6338 "%<__builtin_shuffle%>");
6339 return error_mark_node
;
6348 /* If the next thing is a simple-type-specifier, we may be
6349 looking at a functional cast. We could also be looking at
6350 an id-expression. So, we try the functional cast, and if
6351 that doesn't work we fall back to the primary-expression. */
6352 cp_parser_parse_tentatively (parser
);
6353 /* Look for the simple-type-specifier. */
6354 ++parser
->prevent_constrained_type_specifiers
;
6355 type
= cp_parser_simple_type_specifier (parser
,
6356 /*decl_specs=*/NULL
,
6357 CP_PARSER_FLAGS_NONE
);
6358 --parser
->prevent_constrained_type_specifiers
;
6359 /* Parse the cast itself. */
6360 if (!cp_parser_error_occurred (parser
))
6362 = cp_parser_functional_cast (parser
, type
);
6363 /* If that worked, we're done. */
6364 if (cp_parser_parse_definitely (parser
))
6367 /* If the functional-cast didn't work out, try a
6368 compound-literal. */
6369 if (cp_parser_allow_gnu_extensions_p (parser
)
6370 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6372 tree initializer
= NULL_TREE
;
6374 cp_parser_parse_tentatively (parser
);
6376 /* Avoid calling cp_parser_type_id pointlessly, see comment
6377 in cp_parser_cast_expression about c++/29234. */
6378 if (!cp_parser_compound_literal_p (parser
))
6379 cp_parser_simulate_error (parser
);
6382 /* Parse the type. */
6383 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6384 parser
->in_type_id_in_expr_p
= true;
6385 type
= cp_parser_type_id (parser
);
6386 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6387 /* Look for the `)'. */
6388 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6391 /* If things aren't going well, there's no need to
6393 if (!cp_parser_error_occurred (parser
))
6395 bool non_constant_p
;
6396 /* Parse the brace-enclosed initializer list. */
6397 initializer
= cp_parser_braced_list (parser
,
6400 /* If that worked, we're definitely looking at a
6401 compound-literal expression. */
6402 if (cp_parser_parse_definitely (parser
))
6404 /* Warn the user that a compound literal is not
6405 allowed in standard C++. */
6406 pedwarn (input_location
, OPT_Wpedantic
,
6407 "ISO C++ forbids compound-literals");
6408 /* For simplicity, we disallow compound literals in
6409 constant-expressions. We could
6410 allow compound literals of integer type, whose
6411 initializer was a constant, in constant
6412 expressions. Permitting that usage, as a further
6413 extension, would not change the meaning of any
6414 currently accepted programs. (Of course, as
6415 compound literals are not part of ISO C++, the
6416 standard has nothing to say.) */
6417 if (cp_parser_non_integral_constant_expression (parser
,
6420 postfix_expression
= error_mark_node
;
6423 /* Form the representation of the compound-literal. */
6425 = finish_compound_literal (type
, initializer
,
6426 tf_warning_or_error
);
6431 /* It must be a primary-expression. */
6433 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6434 /*template_arg_p=*/false,
6441 /* Note that we don't need to worry about calling build_cplus_new on a
6442 class-valued CALL_EXPR in decltype when it isn't the end of the
6443 postfix-expression; unary_complex_lvalue will take care of that for
6446 /* Keep looping until the postfix-expression is complete. */
6449 if (idk
== CP_ID_KIND_UNQUALIFIED
6450 && identifier_p (postfix_expression
)
6451 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6452 /* It is not a Koenig lookup function call. */
6454 = unqualified_name_lookup_error (postfix_expression
);
6456 /* Peek at the next token. */
6457 token
= cp_lexer_peek_token (parser
->lexer
);
6459 switch (token
->type
)
6461 case CPP_OPEN_SQUARE
:
6462 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6464 cp_parser_error (parser
,
6465 "two consecutive %<[%> shall "
6466 "only introduce an attribute");
6467 return error_mark_node
;
6470 = cp_parser_postfix_open_square_expression (parser
,
6474 idk
= CP_ID_KIND_NONE
;
6475 is_member_access
= false;
6478 case CPP_OPEN_PAREN
:
6479 /* postfix-expression ( expression-list [opt] ) */
6482 bool is_builtin_constant_p
;
6483 bool saved_integral_constant_expression_p
= false;
6484 bool saved_non_integral_constant_expression_p
= false;
6485 tsubst_flags_t complain
= complain_flags (decltype_p
);
6486 vec
<tree
, va_gc
> *args
;
6488 is_member_access
= false;
6490 is_builtin_constant_p
6491 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6492 if (is_builtin_constant_p
)
6494 /* The whole point of __builtin_constant_p is to allow
6495 non-constant expressions to appear as arguments. */
6496 saved_integral_constant_expression_p
6497 = parser
->integral_constant_expression_p
;
6498 saved_non_integral_constant_expression_p
6499 = parser
->non_integral_constant_expression_p
;
6500 parser
->integral_constant_expression_p
= false;
6502 args
= (cp_parser_parenthesized_expression_list
6504 /*cast_p=*/false, /*allow_expansion_p=*/true,
6505 /*non_constant_p=*/NULL
,
6506 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6507 if (is_builtin_constant_p
)
6509 parser
->integral_constant_expression_p
6510 = saved_integral_constant_expression_p
;
6511 parser
->non_integral_constant_expression_p
6512 = saved_non_integral_constant_expression_p
;
6517 postfix_expression
= error_mark_node
;
6521 /* Function calls are not permitted in
6522 constant-expressions. */
6523 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6524 && cp_parser_non_integral_constant_expression (parser
,
6527 postfix_expression
= error_mark_node
;
6528 release_tree_vector (args
);
6533 if (idk
== CP_ID_KIND_UNQUALIFIED
6534 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6536 if (identifier_p (postfix_expression
))
6538 if (!args
->is_empty ())
6541 if (!any_type_dependent_arguments_p (args
))
6543 = perform_koenig_lookup (postfix_expression
, args
,
6548 = unqualified_fn_lookup_error (postfix_expression
);
6550 /* We do not perform argument-dependent lookup if
6551 normal lookup finds a non-function, in accordance
6552 with the expected resolution of DR 218. */
6553 else if (!args
->is_empty ()
6554 && is_overloaded_fn (postfix_expression
))
6556 tree fn
= get_first_fn (postfix_expression
);
6557 fn
= STRIP_TEMPLATE (fn
);
6559 /* Do not do argument dependent lookup if regular
6560 lookup finds a member function or a block-scope
6561 function declaration. [basic.lookup.argdep]/3 */
6562 if (!DECL_FUNCTION_MEMBER_P (fn
)
6563 && !DECL_LOCAL_FUNCTION_P (fn
))
6566 if (!any_type_dependent_arguments_p (args
))
6568 = perform_koenig_lookup (postfix_expression
, args
,
6574 if (warn_memset_transposed_args
)
6576 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6577 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6578 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6579 && vec_safe_length (args
) == 3
6580 && integer_zerop ((*args
)[2])
6581 && LITERAL_ZERO_P ((*args
)[2])
6582 && !(integer_zerop ((*args
)[1])
6583 && LITERAL_ZERO_P ((*args
)[1])))
6584 warning (OPT_Wmemset_transposed_args
,
6585 "%<memset%> used with constant zero length "
6586 "parameter; this could be due to transposed "
6589 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6590 to avoid leaking those into folder and middle-end. */
6593 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6594 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6595 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6598 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6600 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6601 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6603 if (processing_template_decl
6604 && (type_dependent_expression_p (instance
)
6605 || (!BASELINK_P (fn
)
6606 && TREE_CODE (fn
) != FIELD_DECL
)
6607 || type_dependent_expression_p (fn
)
6608 || any_type_dependent_arguments_p (args
)))
6611 = build_nt_call_vec (postfix_expression
, args
);
6612 release_tree_vector (args
);
6616 if (BASELINK_P (fn
))
6619 = (build_new_method_call
6620 (instance
, fn
, &args
, NULL_TREE
,
6621 (idk
== CP_ID_KIND_QUALIFIED
6622 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6629 = finish_call_expr (postfix_expression
, &args
,
6630 /*disallow_virtual=*/false,
6634 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6635 || TREE_CODE (postfix_expression
) == MEMBER_REF
6636 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6637 postfix_expression
= (build_offset_ref_call_from_tree
6638 (postfix_expression
, &args
,
6640 else if (idk
== CP_ID_KIND_QUALIFIED
)
6641 /* A call to a static class member, or a namespace-scope
6644 = finish_call_expr (postfix_expression
, &args
,
6645 /*disallow_virtual=*/true,
6649 /* All other function calls. */
6651 = finish_call_expr (postfix_expression
, &args
,
6652 /*disallow_virtual=*/false,
6656 protected_set_expr_location (postfix_expression
, token
->location
);
6658 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6659 idk
= CP_ID_KIND_NONE
;
6661 release_tree_vector (args
);
6667 /* postfix-expression . template [opt] id-expression
6668 postfix-expression . pseudo-destructor-name
6669 postfix-expression -> template [opt] id-expression
6670 postfix-expression -> pseudo-destructor-name */
6672 /* Consume the `.' or `->' operator. */
6673 cp_lexer_consume_token (parser
->lexer
);
6676 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6680 is_member_access
= true;
6684 /* postfix-expression ++ */
6685 /* Consume the `++' token. */
6686 cp_lexer_consume_token (parser
->lexer
);
6687 /* Generate a representation for the complete expression. */
6689 = finish_increment_expr (postfix_expression
,
6690 POSTINCREMENT_EXPR
);
6691 /* Increments may not appear in constant-expressions. */
6692 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6693 postfix_expression
= error_mark_node
;
6694 idk
= CP_ID_KIND_NONE
;
6695 is_member_access
= false;
6698 case CPP_MINUS_MINUS
:
6699 /* postfix-expression -- */
6700 /* Consume the `--' token. */
6701 cp_lexer_consume_token (parser
->lexer
);
6702 /* Generate a representation for the complete expression. */
6704 = finish_increment_expr (postfix_expression
,
6705 POSTDECREMENT_EXPR
);
6706 /* Decrements may not appear in constant-expressions. */
6707 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6708 postfix_expression
= error_mark_node
;
6709 idk
= CP_ID_KIND_NONE
;
6710 is_member_access
= false;
6714 if (pidk_return
!= NULL
)
6715 * pidk_return
= idk
;
6716 if (member_access_only_p
)
6717 return is_member_access
? postfix_expression
: error_mark_node
;
6719 return postfix_expression
;
6723 /* We should never get here. */
6725 return error_mark_node
;
6728 /* This function parses Cilk Plus array notations. If a normal array expr. is
6729 parsed then the array index is passed back to the caller through *INIT_INDEX
6730 and the function returns a NULL_TREE. If array notation expr. is parsed,
6731 then *INIT_INDEX is ignored by the caller and the function returns
6732 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6736 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6739 cp_token
*token
= NULL
;
6740 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6741 if (!array_value
|| array_value
== error_mark_node
)
6743 cp_parser_skip_to_end_of_statement (parser
);
6744 return error_mark_node
;
6747 array_type
= TREE_TYPE (array_value
);
6749 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6750 parser
->colon_corrects_to_scope_p
= false;
6751 token
= cp_lexer_peek_token (parser
->lexer
);
6755 cp_parser_error (parser
, "expected %<:%> or numeral");
6756 return error_mark_node
;
6758 else if (token
->type
== CPP_COLON
)
6760 /* Consume the ':'. */
6761 cp_lexer_consume_token (parser
->lexer
);
6763 /* If we are here, then we have a case like this A[:]. */
6764 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6766 cp_parser_error (parser
, "expected %<]%>");
6767 cp_parser_skip_to_end_of_statement (parser
);
6768 return error_mark_node
;
6770 *init_index
= NULL_TREE
;
6772 length_index
= NULL_TREE
;
6776 /* If we are here, then there are three valid possibilities:
6778 2. ARRAY [ EXP : EXP ]
6779 3. ARRAY [ EXP : EXP : EXP ] */
6781 *init_index
= cp_parser_expression (parser
);
6782 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6784 /* This indicates that we have a normal array expression. */
6785 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6789 /* Consume the ':'. */
6790 cp_lexer_consume_token (parser
->lexer
);
6791 length_index
= cp_parser_expression (parser
);
6792 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6794 cp_lexer_consume_token (parser
->lexer
);
6795 stride
= cp_parser_expression (parser
);
6798 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6800 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6801 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6803 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6804 cp_lexer_consume_token (parser
->lexer
);
6805 return error_mark_node
;
6807 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6809 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6810 length_index
, stride
, array_type
);
6814 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6815 by cp_parser_builtin_offsetof. We're looking for
6817 postfix-expression [ expression ]
6818 postfix-expression [ braced-init-list ] (C++11)
6820 FOR_OFFSETOF is set if we're being called in that context, which
6821 changes how we deal with integer constant expressions. */
6824 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6825 tree postfix_expression
,
6829 tree index
= NULL_TREE
;
6830 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6831 bool saved_greater_than_is_operator_p
;
6833 /* Consume the `[' token. */
6834 cp_lexer_consume_token (parser
->lexer
);
6836 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6837 parser
->greater_than_is_operator_p
= true;
6839 /* Parse the index expression. */
6840 /* ??? For offsetof, there is a question of what to allow here. If
6841 offsetof is not being used in an integral constant expression context,
6842 then we *could* get the right answer by computing the value at runtime.
6843 If we are in an integral constant expression context, then we might
6844 could accept any constant expression; hard to say without analysis.
6845 Rather than open the barn door too wide right away, allow only integer
6846 constant expressions here. */
6848 index
= cp_parser_constant_expression (parser
);
6851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6853 bool expr_nonconst_p
;
6854 cp_lexer_set_source_position (parser
->lexer
);
6855 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6856 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6858 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6860 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6861 "braced list index is not allowed with array "
6863 cp_parser_skip_to_end_of_statement (parser
);
6864 return error_mark_node
;
6867 else if (flag_cilkplus
)
6869 /* Here are have these two options:
6870 ARRAY[EXP : EXP] - Array notation expr with default
6872 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6874 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6875 postfix_expression
);
6880 index
= cp_parser_expression (parser
);
6883 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6885 /* Look for the closing `]'. */
6886 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6888 /* Build the ARRAY_REF. */
6889 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6892 /* When not doing offsetof, array references are not permitted in
6893 constant-expressions. */
6895 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6896 postfix_expression
= error_mark_node
;
6898 return postfix_expression
;
6901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6902 by cp_parser_builtin_offsetof. We're looking for
6904 postfix-expression . template [opt] id-expression
6905 postfix-expression . pseudo-destructor-name
6906 postfix-expression -> template [opt] id-expression
6907 postfix-expression -> pseudo-destructor-name
6909 FOR_OFFSETOF is set if we're being called in that context. That sorta
6910 limits what of the above we'll actually accept, but nevermind.
6911 TOKEN_TYPE is the "." or "->" token, which will already have been
6912 removed from the stream. */
6915 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6916 enum cpp_ttype token_type
,
6917 tree postfix_expression
,
6918 bool for_offsetof
, cp_id_kind
*idk
,
6919 location_t location
)
6923 bool pseudo_destructor_p
;
6924 tree scope
= NULL_TREE
;
6926 /* If this is a `->' operator, dereference the pointer. */
6927 if (token_type
== CPP_DEREF
)
6928 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6929 tf_warning_or_error
);
6930 /* Check to see whether or not the expression is type-dependent. */
6931 dependent_p
= type_dependent_expression_p (postfix_expression
);
6932 /* The identifier following the `->' or `.' is not qualified. */
6933 parser
->scope
= NULL_TREE
;
6934 parser
->qualifying_scope
= NULL_TREE
;
6935 parser
->object_scope
= NULL_TREE
;
6936 *idk
= CP_ID_KIND_NONE
;
6938 /* Enter the scope corresponding to the type of the object
6939 given by the POSTFIX_EXPRESSION. */
6940 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6942 scope
= TREE_TYPE (postfix_expression
);
6943 /* According to the standard, no expression should ever have
6944 reference type. Unfortunately, we do not currently match
6945 the standard in this respect in that our internal representation
6946 of an expression may have reference type even when the standard
6947 says it does not. Therefore, we have to manually obtain the
6948 underlying type here. */
6949 scope
= non_reference (scope
);
6950 /* The type of the POSTFIX_EXPRESSION must be complete. */
6951 if (scope
== unknown_type_node
)
6953 error_at (location
, "%qE does not have class type",
6954 postfix_expression
);
6957 /* Unlike the object expression in other contexts, *this is not
6958 required to be of complete type for purposes of class member
6959 access (5.2.5) outside the member function body. */
6960 else if (postfix_expression
!= current_class_ref
6961 && !(processing_template_decl
&& scope
== current_class_type
))
6962 scope
= complete_type_or_else (scope
, NULL_TREE
);
6963 /* Let the name lookup machinery know that we are processing a
6964 class member access expression. */
6965 parser
->context
->object_type
= scope
;
6966 /* If something went wrong, we want to be able to discern that case,
6967 as opposed to the case where there was no SCOPE due to the type
6968 of expression being dependent. */
6970 scope
= error_mark_node
;
6971 /* If the SCOPE was erroneous, make the various semantic analysis
6972 functions exit quickly -- and without issuing additional error
6974 if (scope
== error_mark_node
)
6975 postfix_expression
= error_mark_node
;
6978 /* Assume this expression is not a pseudo-destructor access. */
6979 pseudo_destructor_p
= false;
6981 /* If the SCOPE is a scalar type, then, if this is a valid program,
6982 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6983 is type dependent, it can be pseudo-destructor-name or something else.
6984 Try to parse it as pseudo-destructor-name first. */
6985 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6990 cp_parser_parse_tentatively (parser
);
6991 /* Parse the pseudo-destructor-name. */
6993 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6996 && (cp_parser_error_occurred (parser
)
6997 || !SCALAR_TYPE_P (type
)))
6998 cp_parser_abort_tentative_parse (parser
);
6999 else if (cp_parser_parse_definitely (parser
))
7001 pseudo_destructor_p
= true;
7003 = finish_pseudo_destructor_expr (postfix_expression
,
7008 if (!pseudo_destructor_p
)
7010 /* If the SCOPE is not a scalar type, we are looking at an
7011 ordinary class member access expression, rather than a
7012 pseudo-destructor-name. */
7014 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7015 /* Parse the id-expression. */
7016 name
= (cp_parser_id_expression
7018 cp_parser_optional_template_keyword (parser
),
7019 /*check_dependency_p=*/true,
7021 /*declarator_p=*/false,
7022 /*optional_p=*/false));
7023 /* In general, build a SCOPE_REF if the member name is qualified.
7024 However, if the name was not dependent and has already been
7025 resolved; there is no need to build the SCOPE_REF. For example;
7027 struct X { void f(); };
7028 template <typename T> void f(T* t) { t->X::f(); }
7030 Even though "t" is dependent, "X::f" is not and has been resolved
7031 to a BASELINK; there is no need to include scope information. */
7033 /* But we do need to remember that there was an explicit scope for
7034 virtual function calls. */
7036 *idk
= CP_ID_KIND_QUALIFIED
;
7038 /* If the name is a template-id that names a type, we will get a
7039 TYPE_DECL here. That is invalid code. */
7040 if (TREE_CODE (name
) == TYPE_DECL
)
7042 error_at (token
->location
, "invalid use of %qD", name
);
7043 postfix_expression
= error_mark_node
;
7047 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7049 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7051 error_at (token
->location
, "%<%D::%D%> is not a class member",
7052 parser
->scope
, name
);
7053 postfix_expression
= error_mark_node
;
7056 name
= build_qualified_name (/*type=*/NULL_TREE
,
7060 parser
->scope
= NULL_TREE
;
7061 parser
->qualifying_scope
= NULL_TREE
;
7062 parser
->object_scope
= NULL_TREE
;
7064 if (parser
->scope
&& name
&& BASELINK_P (name
))
7065 adjust_result_of_qualified_name_lookup
7066 (name
, parser
->scope
, scope
);
7068 = finish_class_member_access_expr (postfix_expression
, name
,
7070 tf_warning_or_error
);
7074 /* We no longer need to look up names in the scope of the object on
7075 the left-hand side of the `.' or `->' operator. */
7076 parser
->context
->object_type
= NULL_TREE
;
7078 /* Outside of offsetof, these operators may not appear in
7079 constant-expressions. */
7081 && (cp_parser_non_integral_constant_expression
7082 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7083 postfix_expression
= error_mark_node
;
7085 return postfix_expression
;
7088 /* Cache of LITERAL_ZERO_P constants. */
7090 static GTY(()) tree literal_zeros
[itk_none
];
7092 /* Parse a parenthesized expression-list.
7095 assignment-expression
7096 expression-list, assignment-expression
7101 identifier, expression-list
7103 CAST_P is true if this expression is the target of a cast.
7105 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7108 Returns a vector of trees. Each element is a representation of an
7109 assignment-expression. NULL is returned if the ( and or ) are
7110 missing. An empty, but allocated, vector is returned on no
7111 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7112 if we are parsing an attribute list for an attribute that wants a
7113 plain identifier argument, normal_attr for an attribute that wants
7114 an expression, or non_attr if we aren't parsing an attribute list. If
7115 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7116 not all of the expressions in the list were constant.
7117 WANT_LITERAL_ZERO_P is true if the caller is interested in
7118 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
7119 immediately, this can be removed. */
7121 static vec
<tree
, va_gc
> *
7122 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7123 int is_attribute_list
,
7125 bool allow_expansion_p
,
7126 bool *non_constant_p
,
7127 bool want_literal_zero_p
)
7129 vec
<tree
, va_gc
> *expression_list
;
7130 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7131 tree identifier
= NULL_TREE
;
7132 bool saved_greater_than_is_operator_p
;
7134 /* Assume all the expressions will be constant. */
7136 *non_constant_p
= false;
7138 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7141 expression_list
= make_tree_vector ();
7143 /* Within a parenthesized expression, a `>' token is always
7144 the greater-than operator. */
7145 saved_greater_than_is_operator_p
7146 = parser
->greater_than_is_operator_p
;
7147 parser
->greater_than_is_operator_p
= true;
7149 /* Consume expressions until there are no more. */
7150 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7155 /* At the beginning of attribute lists, check to see if the
7156 next token is an identifier. */
7157 if (is_attribute_list
== id_attr
7158 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7162 /* Consume the identifier. */
7163 token
= cp_lexer_consume_token (parser
->lexer
);
7164 /* Save the identifier. */
7165 identifier
= token
->u
.value
;
7169 bool expr_non_constant_p
;
7171 /* Parse the next assignment-expression. */
7172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7174 /* A braced-init-list. */
7175 cp_lexer_set_source_position (parser
->lexer
);
7176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7177 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7178 if (non_constant_p
&& expr_non_constant_p
)
7179 *non_constant_p
= true;
7181 else if (non_constant_p
)
7183 expr
= (cp_parser_constant_expression
7184 (parser
, /*allow_non_constant_p=*/true,
7185 &expr_non_constant_p
));
7186 if (expr_non_constant_p
)
7187 *non_constant_p
= true;
7192 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
7201 /* If a parameter is literal zero alone, remember it
7202 for -Wmemset-transposed-args warning. */
7203 if (integer_zerop (tok
->u
.value
)
7204 && !TREE_OVERFLOW (tok
->u
.value
)
7205 && want_literal_zero_p
7206 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7208 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7209 == CPP_CLOSE_PAREN
))
7212 for (i
= 0; i
< itk_none
; ++i
)
7213 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
7215 if (i
< itk_none
&& literal_zeros
[i
])
7216 expr
= literal_zeros
[i
];
7219 expr
= copy_node (tok
->u
.value
);
7220 LITERAL_ZERO_P (expr
) = 1;
7222 literal_zeros
[i
] = expr
;
7224 /* Consume the 0 token (or '\0', 0LL etc.). */
7225 cp_lexer_consume_token (parser
->lexer
);
7231 if (expr
== NULL_TREE
)
7232 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7237 expr
= instantiate_non_dependent_expr (expr
);
7239 /* If we have an ellipsis, then this is an expression
7241 if (allow_expansion_p
7242 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7244 /* Consume the `...'. */
7245 cp_lexer_consume_token (parser
->lexer
);
7247 /* Build the argument pack. */
7248 expr
= make_pack_expansion (expr
);
7251 /* Add it to the list. We add error_mark_node
7252 expressions to the list, so that we can still tell if
7253 the correct form for a parenthesized expression-list
7254 is found. That gives better errors. */
7255 vec_safe_push (expression_list
, expr
);
7257 if (expr
== error_mark_node
)
7261 /* After the first item, attribute lists look the same as
7262 expression lists. */
7263 is_attribute_list
= non_attr
;
7266 /* If the next token isn't a `,', then we are done. */
7267 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7270 /* Otherwise, consume the `,' and keep going. */
7271 cp_lexer_consume_token (parser
->lexer
);
7274 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7279 /* We try and resync to an unnested comma, as that will give the
7280 user better diagnostics. */
7281 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7282 /*recovering=*/true,
7284 /*consume_paren=*/true);
7289 parser
->greater_than_is_operator_p
7290 = saved_greater_than_is_operator_p
;
7295 parser
->greater_than_is_operator_p
7296 = saved_greater_than_is_operator_p
;
7299 vec_safe_insert (expression_list
, 0, identifier
);
7301 return expression_list
;
7304 /* Parse a pseudo-destructor-name.
7306 pseudo-destructor-name:
7307 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7308 :: [opt] nested-name-specifier template template-id :: ~ type-name
7309 :: [opt] nested-name-specifier [opt] ~ type-name
7311 If either of the first two productions is used, sets *SCOPE to the
7312 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7313 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7314 or ERROR_MARK_NODE if the parse fails. */
7317 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7322 bool nested_name_specifier_p
;
7325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7326 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7327 && !type_dependent_expression_p (object
))
7329 if (cxx_dialect
< cxx14
)
7330 pedwarn (input_location
, 0,
7331 "%<~auto%> only available with "
7332 "-std=c++14 or -std=gnu++14");
7333 cp_lexer_consume_token (parser
->lexer
);
7334 cp_lexer_consume_token (parser
->lexer
);
7336 *type
= TREE_TYPE (object
);
7340 /* Assume that things will not work out. */
7341 *type
= error_mark_node
;
7343 /* Look for the optional `::' operator. */
7344 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7345 /* Look for the optional nested-name-specifier. */
7346 nested_name_specifier_p
7347 = (cp_parser_nested_name_specifier_opt (parser
,
7348 /*typename_keyword_p=*/false,
7349 /*check_dependency_p=*/true,
7351 /*is_declaration=*/false)
7353 /* Now, if we saw a nested-name-specifier, we might be doing the
7354 second production. */
7355 if (nested_name_specifier_p
7356 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7358 /* Consume the `template' keyword. */
7359 cp_lexer_consume_token (parser
->lexer
);
7360 /* Parse the template-id. */
7361 cp_parser_template_id (parser
,
7362 /*template_keyword_p=*/true,
7363 /*check_dependency_p=*/false,
7365 /*is_declaration=*/true);
7366 /* Look for the `::' token. */
7367 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7369 /* If the next token is not a `~', then there might be some
7370 additional qualification. */
7371 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7373 /* At this point, we're looking for "type-name :: ~". The type-name
7374 must not be a class-name, since this is a pseudo-destructor. So,
7375 it must be either an enum-name, or a typedef-name -- both of which
7376 are just identifiers. So, we peek ahead to check that the "::"
7377 and "~" tokens are present; if they are not, then we can avoid
7378 calling type_name. */
7379 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7380 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7381 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7383 cp_parser_error (parser
, "non-scalar type");
7387 /* Look for the type-name. */
7388 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7389 if (*scope
== error_mark_node
)
7392 /* Look for the `::' token. */
7393 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7398 /* Look for the `~'. */
7399 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7401 /* Once we see the ~, this has to be a pseudo-destructor. */
7402 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7403 cp_parser_commit_to_topmost_tentative_parse (parser
);
7405 /* Look for the type-name again. We are not responsible for
7406 checking that it matches the first type-name. */
7407 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7410 /* Parse a unary-expression.
7416 unary-operator cast-expression
7417 sizeof unary-expression
7419 alignof ( type-id ) [C++0x]
7426 __extension__ cast-expression
7427 __alignof__ unary-expression
7428 __alignof__ ( type-id )
7429 alignof unary-expression [C++0x]
7430 __real__ cast-expression
7431 __imag__ cast-expression
7433 sizeof ( type-id ) { initializer-list , [opt] }
7434 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7435 __alignof__ ( type-id ) { initializer-list , [opt] }
7437 ADDRESS_P is true iff the unary-expression is appearing as the
7438 operand of the `&' operator. CAST_P is true if this expression is
7439 the target of a cast.
7441 Returns a representation of the expression. */
7444 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7445 bool address_p
, bool cast_p
, bool decltype_p
)
7448 enum tree_code unary_operator
;
7450 /* Peek at the next token. */
7451 token
= cp_lexer_peek_token (parser
->lexer
);
7452 /* Some keywords give away the kind of expression. */
7453 if (token
->type
== CPP_KEYWORD
)
7455 enum rid keyword
= token
->keyword
;
7464 location_t first_loc
;
7466 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7467 /* Consume the token. */
7468 cp_lexer_consume_token (parser
->lexer
);
7469 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7470 /* Parse the operand. */
7471 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7473 if (TYPE_P (operand
))
7474 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7477 /* ISO C++ defines alignof only with types, not with
7478 expressions. So pedwarn if alignof is used with a non-
7479 type expression. However, __alignof__ is ok. */
7480 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7481 pedwarn (token
->location
, OPT_Wpedantic
,
7482 "ISO C++ does not allow %<alignof%> "
7485 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7487 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7488 SIZEOF_EXPR with the original operand. */
7489 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7491 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7493 if (!processing_template_decl
&& TYPE_P (operand
))
7495 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7496 build1 (NOP_EXPR
, operand
,
7498 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7501 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7502 TREE_SIDE_EFFECTS (ret
) = 0;
7503 TREE_READONLY (ret
) = 1;
7505 SET_EXPR_LOCATION (ret
, first_loc
);
7511 return cp_parser_new_expression (parser
);
7514 return cp_parser_delete_expression (parser
);
7518 /* The saved value of the PEDANTIC flag. */
7522 /* Save away the PEDANTIC flag. */
7523 cp_parser_extension_opt (parser
, &saved_pedantic
);
7524 /* Parse the cast-expression. */
7525 expr
= cp_parser_simple_cast_expression (parser
);
7526 /* Restore the PEDANTIC flag. */
7527 pedantic
= saved_pedantic
;
7537 /* Consume the `__real__' or `__imag__' token. */
7538 cp_lexer_consume_token (parser
->lexer
);
7539 /* Parse the cast-expression. */
7540 expression
= cp_parser_simple_cast_expression (parser
);
7541 /* Create the complete representation. */
7542 return build_x_unary_op (token
->location
,
7543 (keyword
== RID_REALPART
7544 ? REALPART_EXPR
: IMAGPART_EXPR
),
7546 tf_warning_or_error
);
7550 case RID_TRANSACTION_ATOMIC
:
7551 case RID_TRANSACTION_RELAXED
:
7552 return cp_parser_transaction_expression (parser
, keyword
);
7557 const char *saved_message
;
7558 bool saved_integral_constant_expression_p
;
7559 bool saved_non_integral_constant_expression_p
;
7560 bool saved_greater_than_is_operator_p
;
7562 cp_lexer_consume_token (parser
->lexer
);
7563 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7565 saved_message
= parser
->type_definition_forbidden_message
;
7566 parser
->type_definition_forbidden_message
7567 = G_("types may not be defined in %<noexcept%> expressions");
7569 saved_integral_constant_expression_p
7570 = parser
->integral_constant_expression_p
;
7571 saved_non_integral_constant_expression_p
7572 = parser
->non_integral_constant_expression_p
;
7573 parser
->integral_constant_expression_p
= false;
7575 saved_greater_than_is_operator_p
7576 = parser
->greater_than_is_operator_p
;
7577 parser
->greater_than_is_operator_p
= true;
7579 ++cp_unevaluated_operand
;
7580 ++c_inhibit_evaluation_warnings
;
7581 ++cp_noexcept_operand
;
7582 expr
= cp_parser_expression (parser
);
7583 --cp_noexcept_operand
;
7584 --c_inhibit_evaluation_warnings
;
7585 --cp_unevaluated_operand
;
7587 parser
->greater_than_is_operator_p
7588 = saved_greater_than_is_operator_p
;
7590 parser
->integral_constant_expression_p
7591 = saved_integral_constant_expression_p
;
7592 parser
->non_integral_constant_expression_p
7593 = saved_non_integral_constant_expression_p
;
7595 parser
->type_definition_forbidden_message
= saved_message
;
7597 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7598 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7606 /* Look for the `:: new' and `:: delete', which also signal the
7607 beginning of a new-expression, or delete-expression,
7608 respectively. If the next token is `::', then it might be one of
7610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7614 /* See if the token after the `::' is one of the keywords in
7615 which we're interested. */
7616 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7617 /* If it's `new', we have a new-expression. */
7618 if (keyword
== RID_NEW
)
7619 return cp_parser_new_expression (parser
);
7620 /* Similarly, for `delete'. */
7621 else if (keyword
== RID_DELETE
)
7622 return cp_parser_delete_expression (parser
);
7625 /* Look for a unary operator. */
7626 unary_operator
= cp_parser_unary_operator (token
);
7627 /* The `++' and `--' operators can be handled similarly, even though
7628 they are not technically unary-operators in the grammar. */
7629 if (unary_operator
== ERROR_MARK
)
7631 if (token
->type
== CPP_PLUS_PLUS
)
7632 unary_operator
= PREINCREMENT_EXPR
;
7633 else if (token
->type
== CPP_MINUS_MINUS
)
7634 unary_operator
= PREDECREMENT_EXPR
;
7635 /* Handle the GNU address-of-label extension. */
7636 else if (cp_parser_allow_gnu_extensions_p (parser
)
7637 && token
->type
== CPP_AND_AND
)
7641 location_t loc
= token
->location
;
7643 /* Consume the '&&' token. */
7644 cp_lexer_consume_token (parser
->lexer
);
7645 /* Look for the identifier. */
7646 identifier
= cp_parser_identifier (parser
);
7647 /* Create an expression representing the address. */
7648 expression
= finish_label_address_expr (identifier
, loc
);
7649 if (cp_parser_non_integral_constant_expression (parser
,
7651 expression
= error_mark_node
;
7655 if (unary_operator
!= ERROR_MARK
)
7657 tree cast_expression
;
7658 tree expression
= error_mark_node
;
7659 non_integral_constant non_constant_p
= NIC_NONE
;
7660 location_t loc
= token
->location
;
7661 tsubst_flags_t complain
= complain_flags (decltype_p
);
7663 /* Consume the operator token. */
7664 token
= cp_lexer_consume_token (parser
->lexer
);
7665 /* Parse the cast-expression. */
7667 = cp_parser_cast_expression (parser
,
7668 unary_operator
== ADDR_EXPR
,
7672 /* Now, build an appropriate representation. */
7673 switch (unary_operator
)
7676 non_constant_p
= NIC_STAR
;
7677 expression
= build_x_indirect_ref (loc
, cast_expression
,
7683 non_constant_p
= NIC_ADDR
;
7686 expression
= build_x_unary_op (loc
, unary_operator
,
7691 case PREINCREMENT_EXPR
:
7692 case PREDECREMENT_EXPR
:
7693 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7694 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7696 case UNARY_PLUS_EXPR
:
7698 case TRUTH_NOT_EXPR
:
7699 expression
= finish_unary_op_expr (loc
, unary_operator
,
7700 cast_expression
, complain
);
7707 if (non_constant_p
!= NIC_NONE
7708 && cp_parser_non_integral_constant_expression (parser
,
7710 expression
= error_mark_node
;
7715 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7716 /*member_access_only_p=*/false,
7721 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7722 unary-operator, the corresponding tree code is returned. */
7724 static enum tree_code
7725 cp_parser_unary_operator (cp_token
* token
)
7727 switch (token
->type
)
7730 return INDIRECT_REF
;
7736 return UNARY_PLUS_EXPR
;
7742 return TRUTH_NOT_EXPR
;
7745 return BIT_NOT_EXPR
;
7752 /* Parse a new-expression.
7755 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7756 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7758 Returns a representation of the expression. */
7761 cp_parser_new_expression (cp_parser
* parser
)
7763 bool global_scope_p
;
7764 vec
<tree
, va_gc
> *placement
;
7766 vec
<tree
, va_gc
> *initializer
;
7767 tree nelts
= NULL_TREE
;
7770 /* Look for the optional `::' operator. */
7772 = (cp_parser_global_scope_opt (parser
,
7773 /*current_scope_valid_p=*/false)
7775 /* Look for the `new' operator. */
7776 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7777 /* There's no easy way to tell a new-placement from the
7778 `( type-id )' construct. */
7779 cp_parser_parse_tentatively (parser
);
7780 /* Look for a new-placement. */
7781 placement
= cp_parser_new_placement (parser
);
7782 /* If that didn't work out, there's no new-placement. */
7783 if (!cp_parser_parse_definitely (parser
))
7785 if (placement
!= NULL
)
7786 release_tree_vector (placement
);
7790 /* If the next token is a `(', then we have a parenthesized
7792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7795 const char *saved_message
= parser
->type_definition_forbidden_message
;
7797 /* Consume the `('. */
7798 cp_lexer_consume_token (parser
->lexer
);
7800 /* Parse the type-id. */
7801 parser
->type_definition_forbidden_message
7802 = G_("types may not be defined in a new-expression");
7803 type
= cp_parser_type_id (parser
);
7804 parser
->type_definition_forbidden_message
= saved_message
;
7806 /* Look for the closing `)'. */
7807 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7808 token
= cp_lexer_peek_token (parser
->lexer
);
7809 /* There should not be a direct-new-declarator in this production,
7810 but GCC used to allowed this, so we check and emit a sensible error
7811 message for this case. */
7812 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7814 error_at (token
->location
,
7815 "array bound forbidden after parenthesized type-id");
7816 inform (token
->location
,
7817 "try removing the parentheses around the type-id");
7818 cp_parser_direct_new_declarator (parser
);
7821 /* Otherwise, there must be a new-type-id. */
7823 type
= cp_parser_new_type_id (parser
, &nelts
);
7825 /* If the next token is a `(' or '{', then we have a new-initializer. */
7826 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7827 if (token
->type
== CPP_OPEN_PAREN
7828 || token
->type
== CPP_OPEN_BRACE
)
7829 initializer
= cp_parser_new_initializer (parser
);
7833 /* A new-expression may not appear in an integral constant
7835 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7836 ret
= error_mark_node
;
7837 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7838 of a new-type-id or type-id of a new-expression, the new-expression shall
7839 contain a new-initializer of the form ( assignment-expression )".
7840 Additionally, consistently with the spirit of DR 1467, we want to accept
7841 'new auto { 2 }' too. */
7842 else if (type_uses_auto (type
)
7843 && (vec_safe_length (initializer
) != 1
7844 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
7845 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
7847 error_at (token
->location
,
7848 "initialization of new-expression for type %<auto%> "
7849 "requires exactly one element");
7850 ret
= error_mark_node
;
7854 /* Create a representation of the new-expression. */
7855 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7856 tf_warning_or_error
);
7859 if (placement
!= NULL
)
7860 release_tree_vector (placement
);
7861 if (initializer
!= NULL
)
7862 release_tree_vector (initializer
);
7867 /* Parse a new-placement.
7872 Returns the same representation as for an expression-list. */
7874 static vec
<tree
, va_gc
> *
7875 cp_parser_new_placement (cp_parser
* parser
)
7877 vec
<tree
, va_gc
> *expression_list
;
7879 /* Parse the expression-list. */
7880 expression_list
= (cp_parser_parenthesized_expression_list
7881 (parser
, non_attr
, /*cast_p=*/false,
7882 /*allow_expansion_p=*/true,
7883 /*non_constant_p=*/NULL
));
7885 if (expression_list
&& expression_list
->is_empty ())
7886 error ("expected expression-list or type-id");
7888 return expression_list
;
7891 /* Parse a new-type-id.
7894 type-specifier-seq new-declarator [opt]
7896 Returns the TYPE allocated. If the new-type-id indicates an array
7897 type, *NELTS is set to the number of elements in the last array
7898 bound; the TYPE will not include the last array bound. */
7901 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7903 cp_decl_specifier_seq type_specifier_seq
;
7904 cp_declarator
*new_declarator
;
7905 cp_declarator
*declarator
;
7906 cp_declarator
*outer_declarator
;
7907 const char *saved_message
;
7909 /* The type-specifier sequence must not contain type definitions.
7910 (It cannot contain declarations of new types either, but if they
7911 are not definitions we will catch that because they are not
7913 saved_message
= parser
->type_definition_forbidden_message
;
7914 parser
->type_definition_forbidden_message
7915 = G_("types may not be defined in a new-type-id");
7916 /* Parse the type-specifier-seq. */
7917 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7918 /*is_trailing_return=*/false,
7919 &type_specifier_seq
);
7920 /* Restore the old message. */
7921 parser
->type_definition_forbidden_message
= saved_message
;
7923 if (type_specifier_seq
.type
== error_mark_node
)
7924 return error_mark_node
;
7926 /* Parse the new-declarator. */
7927 new_declarator
= cp_parser_new_declarator_opt (parser
);
7929 /* Determine the number of elements in the last array dimension, if
7932 /* Skip down to the last array dimension. */
7933 declarator
= new_declarator
;
7934 outer_declarator
= NULL
;
7935 while (declarator
&& (declarator
->kind
== cdk_pointer
7936 || declarator
->kind
== cdk_ptrmem
))
7938 outer_declarator
= declarator
;
7939 declarator
= declarator
->declarator
;
7942 && declarator
->kind
== cdk_array
7943 && declarator
->declarator
7944 && declarator
->declarator
->kind
== cdk_array
)
7946 outer_declarator
= declarator
;
7947 declarator
= declarator
->declarator
;
7950 if (declarator
&& declarator
->kind
== cdk_array
)
7952 *nelts
= declarator
->u
.array
.bounds
;
7953 if (*nelts
== error_mark_node
)
7954 *nelts
= integer_one_node
;
7956 if (outer_declarator
)
7957 outer_declarator
->declarator
= declarator
->declarator
;
7959 new_declarator
= NULL
;
7962 return groktypename (&type_specifier_seq
, new_declarator
, false);
7965 /* Parse an (optional) new-declarator.
7968 ptr-operator new-declarator [opt]
7969 direct-new-declarator
7971 Returns the declarator. */
7973 static cp_declarator
*
7974 cp_parser_new_declarator_opt (cp_parser
* parser
)
7976 enum tree_code code
;
7977 tree type
, std_attributes
= NULL_TREE
;
7978 cp_cv_quals cv_quals
;
7980 /* We don't know if there's a ptr-operator next, or not. */
7981 cp_parser_parse_tentatively (parser
);
7982 /* Look for a ptr-operator. */
7983 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7984 /* If that worked, look for more new-declarators. */
7985 if (cp_parser_parse_definitely (parser
))
7987 cp_declarator
*declarator
;
7989 /* Parse another optional declarator. */
7990 declarator
= cp_parser_new_declarator_opt (parser
);
7992 declarator
= cp_parser_make_indirect_declarator
7993 (code
, type
, cv_quals
, declarator
, std_attributes
);
7998 /* If the next token is a `[', there is a direct-new-declarator. */
7999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8000 return cp_parser_direct_new_declarator (parser
);
8005 /* Parse a direct-new-declarator.
8007 direct-new-declarator:
8009 direct-new-declarator [constant-expression]
8013 static cp_declarator
*
8014 cp_parser_direct_new_declarator (cp_parser
* parser
)
8016 cp_declarator
*declarator
= NULL
;
8023 /* Look for the opening `['. */
8024 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8026 token
= cp_lexer_peek_token (parser
->lexer
);
8027 expression
= cp_parser_expression (parser
);
8028 /* The standard requires that the expression have integral
8029 type. DR 74 adds enumeration types. We believe that the
8030 real intent is that these expressions be handled like the
8031 expression in a `switch' condition, which also allows
8032 classes with a single conversion to integral or
8033 enumeration type. */
8034 if (!processing_template_decl
)
8037 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8042 error_at (token
->location
,
8043 "expression in new-declarator must have integral "
8044 "or enumeration type");
8045 expression
= error_mark_node
;
8049 /* Look for the closing `]'. */
8050 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8052 /* Add this bound to the declarator. */
8053 declarator
= make_array_declarator (declarator
, expression
);
8055 /* If the next token is not a `[', then there are no more
8057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8064 /* Parse a new-initializer.
8067 ( expression-list [opt] )
8070 Returns a representation of the expression-list. */
8072 static vec
<tree
, va_gc
> *
8073 cp_parser_new_initializer (cp_parser
* parser
)
8075 vec
<tree
, va_gc
> *expression_list
;
8077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8080 bool expr_non_constant_p
;
8081 cp_lexer_set_source_position (parser
->lexer
);
8082 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8083 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8084 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8085 expression_list
= make_tree_vector_single (t
);
8088 expression_list
= (cp_parser_parenthesized_expression_list
8089 (parser
, non_attr
, /*cast_p=*/false,
8090 /*allow_expansion_p=*/true,
8091 /*non_constant_p=*/NULL
));
8093 return expression_list
;
8096 /* Parse a delete-expression.
8099 :: [opt] delete cast-expression
8100 :: [opt] delete [ ] cast-expression
8102 Returns a representation of the expression. */
8105 cp_parser_delete_expression (cp_parser
* parser
)
8107 bool global_scope_p
;
8111 /* Look for the optional `::' operator. */
8113 = (cp_parser_global_scope_opt (parser
,
8114 /*current_scope_valid_p=*/false)
8116 /* Look for the `delete' keyword. */
8117 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8118 /* See if the array syntax is in use. */
8119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8121 /* Consume the `[' token. */
8122 cp_lexer_consume_token (parser
->lexer
);
8123 /* Look for the `]' token. */
8124 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8125 /* Remember that this is the `[]' construct. */
8131 /* Parse the cast-expression. */
8132 expression
= cp_parser_simple_cast_expression (parser
);
8134 /* A delete-expression may not appear in an integral constant
8136 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8137 return error_mark_node
;
8139 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8140 tf_warning_or_error
);
8143 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8144 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8148 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8150 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8151 switch (token
->type
)
8157 case CPP_CLOSE_SQUARE
:
8158 case CPP_CLOSE_PAREN
:
8159 case CPP_CLOSE_BRACE
:
8160 case CPP_OPEN_BRACE
:
8164 case CPP_DEREF_STAR
:
8172 case CPP_GREATER_EQ
:
8193 case CPP_OPEN_PAREN
:
8194 /* In ((type ()) () the last () isn't a valid cast-expression,
8195 so the whole must be parsed as postfix-expression. */
8196 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8199 case CPP_OPEN_SQUARE
:
8200 /* '[' may start a primary-expression in obj-c++ and in C++11,
8201 as a lambda-expression, eg, '(void)[]{}'. */
8202 if (cxx_dialect
>= cxx11
)
8204 return c_dialect_objc ();
8207 case CPP_MINUS_MINUS
:
8208 /* '++' and '--' may or may not start a cast-expression:
8210 struct T { void operator++(int); };
8211 void f() { (T())++; }
8224 /* Parse a cast-expression.
8228 ( type-id ) cast-expression
8230 ADDRESS_P is true iff the unary-expression is appearing as the
8231 operand of the `&' operator. CAST_P is true if this expression is
8232 the target of a cast.
8234 Returns a representation of the expression. */
8237 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8238 bool decltype_p
, cp_id_kind
* pidk
)
8240 /* If it's a `(', then we might be looking at a cast. */
8241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8243 tree type
= NULL_TREE
;
8244 tree expr
= NULL_TREE
;
8245 int cast_expression
= 0;
8246 const char *saved_message
;
8248 /* There's no way to know yet whether or not this is a cast.
8249 For example, `(int (3))' is a unary-expression, while `(int)
8250 3' is a cast. So, we resort to parsing tentatively. */
8251 cp_parser_parse_tentatively (parser
);
8252 /* Types may not be defined in a cast. */
8253 saved_message
= parser
->type_definition_forbidden_message
;
8254 parser
->type_definition_forbidden_message
8255 = G_("types may not be defined in casts");
8256 /* Consume the `('. */
8257 cp_lexer_consume_token (parser
->lexer
);
8258 /* A very tricky bit is that `(struct S) { 3 }' is a
8259 compound-literal (which we permit in C++ as an extension).
8260 But, that construct is not a cast-expression -- it is a
8261 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8262 is legal; if the compound-literal were a cast-expression,
8263 you'd need an extra set of parentheses.) But, if we parse
8264 the type-id, and it happens to be a class-specifier, then we
8265 will commit to the parse at that point, because we cannot
8266 undo the action that is done when creating a new class. So,
8267 then we cannot back up and do a postfix-expression.
8269 Another tricky case is the following (c++/29234):
8271 struct S { void operator () (); };
8278 As a type-id we parse the parenthesized S()() as a function
8279 returning a function, groktypename complains and we cannot
8280 back up in this case either.
8282 Therefore, we scan ahead to the closing `)', and check to see
8283 if the tokens after the `)' can start a cast-expression. Otherwise
8284 we are dealing with an unary-expression, a postfix-expression
8287 Yet another tricky case, in C++11, is the following (c++/54891):
8291 The issue is that usually, besides the case of lambda-expressions,
8292 the parenthesized type-id cannot be followed by '[', and, eg, we
8293 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8294 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8295 we don't commit, we try a cast-expression, then an unary-expression.
8297 Save tokens so that we can put them back. */
8298 cp_lexer_save_tokens (parser
->lexer
);
8300 /* We may be looking at a cast-expression. */
8301 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8302 /*consume_paren=*/true))
8304 = cp_parser_tokens_start_cast_expression (parser
);
8306 /* Roll back the tokens we skipped. */
8307 cp_lexer_rollback_tokens (parser
->lexer
);
8308 /* If we aren't looking at a cast-expression, simulate an error so
8309 that the call to cp_parser_error_occurred below returns true. */
8310 if (!cast_expression
)
8311 cp_parser_simulate_error (parser
);
8314 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8315 parser
->in_type_id_in_expr_p
= true;
8316 /* Look for the type-id. */
8317 type
= cp_parser_type_id (parser
);
8318 /* Look for the closing `)'. */
8319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8320 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8323 /* Restore the saved message. */
8324 parser
->type_definition_forbidden_message
= saved_message
;
8326 /* At this point this can only be either a cast or a
8327 parenthesized ctor such as `(T ())' that looks like a cast to
8328 function returning T. */
8329 if (!cp_parser_error_occurred (parser
))
8331 /* Only commit if the cast-expression doesn't start with
8332 '++', '--', or '[' in C++11. */
8333 if (cast_expression
> 0)
8334 cp_parser_commit_to_topmost_tentative_parse (parser
);
8336 expr
= cp_parser_cast_expression (parser
,
8337 /*address_p=*/false,
8339 /*decltype_p=*/false,
8342 if (cp_parser_parse_definitely (parser
))
8344 /* Warn about old-style casts, if so requested. */
8345 if (warn_old_style_cast
8346 && !in_system_header_at (input_location
)
8347 && !VOID_TYPE_P (type
)
8348 && current_lang_name
!= lang_name_c
)
8349 warning (OPT_Wold_style_cast
, "use of old-style cast");
8351 /* Only type conversions to integral or enumeration types
8352 can be used in constant-expressions. */
8353 if (!cast_valid_in_integral_constant_expression_p (type
)
8354 && cp_parser_non_integral_constant_expression (parser
,
8356 return error_mark_node
;
8358 /* Perform the cast. */
8359 expr
= build_c_cast (input_location
, type
, expr
);
8364 cp_parser_abort_tentative_parse (parser
);
8367 /* If we get here, then it's not a cast, so it must be a
8368 unary-expression. */
8369 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8370 cast_p
, decltype_p
);
8373 /* Parse a binary expression of the general form:
8377 pm-expression .* cast-expression
8378 pm-expression ->* cast-expression
8380 multiplicative-expression:
8382 multiplicative-expression * pm-expression
8383 multiplicative-expression / pm-expression
8384 multiplicative-expression % pm-expression
8386 additive-expression:
8387 multiplicative-expression
8388 additive-expression + multiplicative-expression
8389 additive-expression - multiplicative-expression
8393 shift-expression << additive-expression
8394 shift-expression >> additive-expression
8396 relational-expression:
8398 relational-expression < shift-expression
8399 relational-expression > shift-expression
8400 relational-expression <= shift-expression
8401 relational-expression >= shift-expression
8405 relational-expression:
8406 relational-expression <? shift-expression
8407 relational-expression >? shift-expression
8409 equality-expression:
8410 relational-expression
8411 equality-expression == relational-expression
8412 equality-expression != relational-expression
8416 and-expression & equality-expression
8418 exclusive-or-expression:
8420 exclusive-or-expression ^ and-expression
8422 inclusive-or-expression:
8423 exclusive-or-expression
8424 inclusive-or-expression | exclusive-or-expression
8426 logical-and-expression:
8427 inclusive-or-expression
8428 logical-and-expression && inclusive-or-expression
8430 logical-or-expression:
8431 logical-and-expression
8432 logical-or-expression || logical-and-expression
8434 All these are implemented with a single function like:
8437 simple-cast-expression
8438 binary-expression <token> binary-expression
8440 CAST_P is true if this expression is the target of a cast.
8442 The binops_by_token map is used to get the tree codes for each <token> type.
8443 binary-expressions are associated according to a precedence table. */
8445 #define TOKEN_PRECEDENCE(token) \
8446 (((token->type == CPP_GREATER \
8447 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8448 && !parser->greater_than_is_operator_p) \
8449 ? PREC_NOT_OPERATOR \
8450 : binops_by_token[token->type].prec)
8453 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8454 bool no_toplevel_fold_p
,
8456 enum cp_parser_prec prec
,
8459 cp_parser_expression_stack stack
;
8460 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8461 cp_parser_expression_stack_entry current
;
8464 enum tree_code rhs_type
;
8465 enum cp_parser_prec new_prec
, lookahead_prec
;
8468 /* Parse the first expression. */
8469 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8470 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8471 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8472 cast_p
, decltype_p
, pidk
);
8473 current
.prec
= prec
;
8475 if (cp_parser_error_occurred (parser
))
8476 return error_mark_node
;
8480 /* Get an operator token. */
8481 token
= cp_lexer_peek_token (parser
->lexer
);
8483 if (warn_cxx11_compat
8484 && token
->type
== CPP_RSHIFT
8485 && !parser
->greater_than_is_operator_p
)
8487 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8488 "%<>>%> operator is treated"
8489 " as two right angle brackets in C++11"))
8490 inform (token
->location
,
8491 "suggest parentheses around %<>>%> expression");
8494 new_prec
= TOKEN_PRECEDENCE (token
);
8495 if (new_prec
!= PREC_NOT_OPERATOR
8496 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8497 /* This is a fold-expression; handle it later. */
8498 new_prec
= PREC_NOT_OPERATOR
;
8500 /* Popping an entry off the stack means we completed a subexpression:
8501 - either we found a token which is not an operator (`>' where it is not
8502 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8503 will happen repeatedly;
8504 - or, we found an operator which has lower priority. This is the case
8505 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8507 if (new_prec
<= current
.prec
)
8516 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8517 current
.loc
= token
->location
;
8519 /* We used the operator token. */
8520 cp_lexer_consume_token (parser
->lexer
);
8522 /* For "false && x" or "true || x", x will never be executed;
8523 disable warnings while evaluating it. */
8524 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8525 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8526 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8527 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8529 /* Extract another operand. It may be the RHS of this expression
8530 or the LHS of a new, higher priority expression. */
8531 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8532 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8533 rhs
= cp_parser_simple_cast_expression (parser
);
8535 /* Get another operator token. Look up its precedence to avoid
8536 building a useless (immediately popped) stack entry for common
8537 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8538 token
= cp_lexer_peek_token (parser
->lexer
);
8539 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8540 if (lookahead_prec
!= PREC_NOT_OPERATOR
8541 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8542 lookahead_prec
= PREC_NOT_OPERATOR
;
8543 if (lookahead_prec
> new_prec
)
8545 /* ... and prepare to parse the RHS of the new, higher priority
8546 expression. Since precedence levels on the stack are
8547 monotonically increasing, we do not have to care about
8552 current
.lhs_type
= rhs_type
;
8553 current
.prec
= new_prec
;
8554 new_prec
= lookahead_prec
;
8558 lookahead_prec
= new_prec
;
8559 /* If the stack is not empty, we have parsed into LHS the right side
8560 (`4' in the example above) of an expression we had suspended.
8561 We can use the information on the stack to recover the LHS (`3')
8562 from the stack together with the tree code (`MULT_EXPR'), and
8563 the precedence of the higher level subexpression
8564 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8565 which will be used to actually build the additive expression. */
8567 rhs_type
= current
.lhs_type
;
8572 /* Undo the disabling of warnings done above. */
8573 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8574 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8575 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8576 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8578 if (warn_logical_not_paren
8579 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8580 && current
.lhs_type
== TRUTH_NOT_EXPR
8581 /* Avoid warning for !!x == y. */
8582 && (TREE_CODE (current
.lhs
) != NE_EXPR
8583 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8584 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8585 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8586 /* Avoid warning for !b == y where b is boolean. */
8587 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
8588 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
8590 /* Avoid warning for !!b == y where b is boolean. */
8591 && (!DECL_P (current
.lhs
)
8592 || TREE_TYPE (current
.lhs
) == NULL_TREE
8593 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
8594 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
8595 maybe_constant_value (rhs
));
8598 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8599 ERROR_MARK for everything that is not a binary expression.
8600 This makes warn_about_parentheses miss some warnings that
8601 involve unary operators. For unary expressions we should
8602 pass the correct tree_code unless the unary expression was
8603 surrounded by parentheses.
8605 if (no_toplevel_fold_p
8606 && lookahead_prec
<= current
.prec
8608 current
.lhs
= build2 (current
.tree_type
,
8609 TREE_CODE_CLASS (current
.tree_type
)
8611 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8614 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8615 current
.lhs
, current
.lhs_type
,
8616 rhs
, rhs_type
, &overload
,
8617 complain_flags (decltype_p
));
8618 current
.lhs_type
= current
.tree_type
;
8619 protected_set_expr_location (current
.lhs
, current
.loc
);
8621 /* If the binary operator required the use of an overloaded operator,
8622 then this expression cannot be an integral constant-expression.
8623 An overloaded operator can be used even if both operands are
8624 otherwise permissible in an integral constant-expression if at
8625 least one of the operands is of enumeration type. */
8628 && cp_parser_non_integral_constant_expression (parser
,
8630 return error_mark_node
;
8637 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8638 bool no_toplevel_fold_p
,
8639 enum cp_parser_prec prec
,
8642 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8643 /*decltype*/false, prec
, pidk
);
8646 /* Parse the `? expression : assignment-expression' part of a
8647 conditional-expression. The LOGICAL_OR_EXPR is the
8648 logical-or-expression that started the conditional-expression.
8649 Returns a representation of the entire conditional-expression.
8651 This routine is used by cp_parser_assignment_expression.
8653 ? expression : assignment-expression
8657 ? : assignment-expression */
8660 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8663 tree assignment_expr
;
8664 struct cp_token
*token
;
8665 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8667 /* Consume the `?' token. */
8668 cp_lexer_consume_token (parser
->lexer
);
8669 token
= cp_lexer_peek_token (parser
->lexer
);
8670 if (cp_parser_allow_gnu_extensions_p (parser
)
8671 && token
->type
== CPP_COLON
)
8673 pedwarn (token
->location
, OPT_Wpedantic
,
8674 "ISO C++ does not allow ?: with omitted middle operand");
8675 /* Implicit true clause. */
8677 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8678 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8682 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8683 parser
->colon_corrects_to_scope_p
= false;
8684 /* Parse the expression. */
8685 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8686 expr
= cp_parser_expression (parser
);
8687 c_inhibit_evaluation_warnings
+=
8688 ((logical_or_expr
== truthvalue_true_node
)
8689 - (logical_or_expr
== truthvalue_false_node
));
8690 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8693 /* The next token should be a `:'. */
8694 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8695 /* Parse the assignment-expression. */
8696 assignment_expr
= cp_parser_assignment_expression (parser
);
8697 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8699 /* Build the conditional-expression. */
8700 return build_x_conditional_expr (loc
, logical_or_expr
,
8703 tf_warning_or_error
);
8706 /* Parse an assignment-expression.
8708 assignment-expression:
8709 conditional-expression
8710 logical-or-expression assignment-operator assignment_expression
8713 CAST_P is true if this expression is the target of a cast.
8714 DECLTYPE_P is true if this expression is the operand of decltype.
8716 Returns a representation for the expression. */
8719 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8720 bool cast_p
, bool decltype_p
)
8724 /* If the next token is the `throw' keyword, then we're looking at
8725 a throw-expression. */
8726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8727 expr
= cp_parser_throw_expression (parser
);
8728 /* Otherwise, it must be that we are looking at a
8729 logical-or-expression. */
8732 /* Parse the binary expressions (logical-or-expression). */
8733 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8735 PREC_NOT_OPERATOR
, pidk
);
8736 /* If the next token is a `?' then we're actually looking at a
8737 conditional-expression. */
8738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8739 return cp_parser_question_colon_clause (parser
, expr
);
8742 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8744 /* If it's an assignment-operator, we're using the second
8746 enum tree_code assignment_operator
8747 = cp_parser_assignment_operator_opt (parser
);
8748 if (assignment_operator
!= ERROR_MARK
)
8750 bool non_constant_p
;
8751 location_t saved_input_location
;
8753 /* Parse the right-hand side of the assignment. */
8754 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8756 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8759 /* An assignment may not appear in a
8760 constant-expression. */
8761 if (cp_parser_non_integral_constant_expression (parser
,
8763 return error_mark_node
;
8764 /* Build the assignment expression. Its default
8765 location is the location of the '=' token. */
8766 saved_input_location
= input_location
;
8767 input_location
= loc
;
8768 expr
= build_x_modify_expr (loc
, expr
,
8769 assignment_operator
,
8771 complain_flags (decltype_p
));
8772 input_location
= saved_input_location
;
8780 /* Parse an (optional) assignment-operator.
8782 assignment-operator: one of
8783 = *= /= %= += -= >>= <<= &= ^= |=
8787 assignment-operator: one of
8790 If the next token is an assignment operator, the corresponding tree
8791 code is returned, and the token is consumed. For example, for
8792 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8793 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8794 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8795 operator, ERROR_MARK is returned. */
8797 static enum tree_code
8798 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8803 /* Peek at the next token. */
8804 token
= cp_lexer_peek_token (parser
->lexer
);
8806 switch (token
->type
)
8817 op
= TRUNC_DIV_EXPR
;
8821 op
= TRUNC_MOD_EXPR
;
8853 /* Nothing else is an assignment operator. */
8857 /* An operator followed by ... is a fold-expression, handled elsewhere. */
8858 if (op
!= ERROR_MARK
8859 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8862 /* If it was an assignment operator, consume it. */
8863 if (op
!= ERROR_MARK
)
8864 cp_lexer_consume_token (parser
->lexer
);
8869 /* Parse an expression.
8872 assignment-expression
8873 expression , assignment-expression
8875 CAST_P is true if this expression is the target of a cast.
8876 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8877 except possibly parenthesized or on the RHS of a comma (N3276).
8879 Returns a representation of the expression. */
8882 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8883 bool cast_p
, bool decltype_p
)
8885 tree expression
= NULL_TREE
;
8886 location_t loc
= UNKNOWN_LOCATION
;
8890 tree assignment_expression
;
8892 /* Parse the next assignment-expression. */
8893 assignment_expression
8894 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8896 /* We don't create a temporary for a call that is the immediate operand
8897 of decltype or on the RHS of a comma. But when we see a comma, we
8898 need to create a temporary for a call on the LHS. */
8899 if (decltype_p
&& !processing_template_decl
8900 && TREE_CODE (assignment_expression
) == CALL_EXPR
8901 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8902 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8903 assignment_expression
8904 = build_cplus_new (TREE_TYPE (assignment_expression
),
8905 assignment_expression
, tf_warning_or_error
);
8907 /* If this is the first assignment-expression, we can just
8910 expression
= assignment_expression
;
8912 expression
= build_x_compound_expr (loc
, expression
,
8913 assignment_expression
,
8914 complain_flags (decltype_p
));
8915 /* If the next token is not a comma, or we're in a fold-expression, then
8916 we are done with the expression. */
8917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8918 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8920 /* Consume the `,'. */
8921 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8922 cp_lexer_consume_token (parser
->lexer
);
8923 /* A comma operator cannot appear in a constant-expression. */
8924 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8925 expression
= error_mark_node
;
8931 /* Parse a constant-expression.
8933 constant-expression:
8934 conditional-expression
8936 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8937 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8938 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8939 is false, NON_CONSTANT_P should be NULL. */
8942 cp_parser_constant_expression (cp_parser
* parser
,
8943 bool allow_non_constant_p
,
8944 bool *non_constant_p
)
8946 bool saved_integral_constant_expression_p
;
8947 bool saved_allow_non_integral_constant_expression_p
;
8948 bool saved_non_integral_constant_expression_p
;
8951 /* It might seem that we could simply parse the
8952 conditional-expression, and then check to see if it were
8953 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8954 one that the compiler can figure out is constant, possibly after
8955 doing some simplifications or optimizations. The standard has a
8956 precise definition of constant-expression, and we must honor
8957 that, even though it is somewhat more restrictive.
8963 is not a legal declaration, because `(2, 3)' is not a
8964 constant-expression. The `,' operator is forbidden in a
8965 constant-expression. However, GCC's constant-folding machinery
8966 will fold this operation to an INTEGER_CST for `3'. */
8968 /* Save the old settings. */
8969 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8970 saved_allow_non_integral_constant_expression_p
8971 = parser
->allow_non_integral_constant_expression_p
;
8972 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8973 /* We are now parsing a constant-expression. */
8974 parser
->integral_constant_expression_p
= true;
8975 parser
->allow_non_integral_constant_expression_p
8976 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8977 parser
->non_integral_constant_expression_p
= false;
8978 /* Although the grammar says "conditional-expression", we parse an
8979 "assignment-expression", which also permits "throw-expression"
8980 and the use of assignment operators. In the case that
8981 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8982 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8983 actually essential that we look for an assignment-expression.
8984 For example, cp_parser_initializer_clauses uses this function to
8985 determine whether a particular assignment-expression is in fact
8987 expression
= cp_parser_assignment_expression (parser
);
8988 /* Restore the old settings. */
8989 parser
->integral_constant_expression_p
8990 = saved_integral_constant_expression_p
;
8991 parser
->allow_non_integral_constant_expression_p
8992 = saved_allow_non_integral_constant_expression_p
;
8993 if (cxx_dialect
>= cxx11
)
8995 /* Require an rvalue constant expression here; that's what our
8996 callers expect. Reference constant expressions are handled
8997 separately in e.g. cp_parser_template_argument. */
8998 bool is_const
= potential_rvalue_constant_expression (expression
);
8999 parser
->non_integral_constant_expression_p
= !is_const
;
9000 if (!is_const
&& !allow_non_constant_p
)
9001 require_potential_rvalue_constant_expression (expression
);
9003 if (allow_non_constant_p
)
9004 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9005 parser
->non_integral_constant_expression_p
9006 = saved_non_integral_constant_expression_p
;
9011 /* Parse __builtin_offsetof.
9013 offsetof-expression:
9014 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9016 offsetof-member-designator:
9018 | offsetof-member-designator "." id-expression
9019 | offsetof-member-designator "[" expression "]"
9020 | offsetof-member-designator "->" id-expression */
9023 cp_parser_builtin_offsetof (cp_parser
*parser
)
9025 int save_ice_p
, save_non_ice_p
;
9030 /* We're about to accept non-integral-constant things, but will
9031 definitely yield an integral constant expression. Save and
9032 restore these values around our local parsing. */
9033 save_ice_p
= parser
->integral_constant_expression_p
;
9034 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9036 /* Consume the "__builtin_offsetof" token. */
9037 cp_lexer_consume_token (parser
->lexer
);
9038 /* Consume the opening `('. */
9039 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9040 /* Parse the type-id. */
9041 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9042 type
= cp_parser_type_id (parser
);
9043 /* Look for the `,'. */
9044 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9045 token
= cp_lexer_peek_token (parser
->lexer
);
9047 /* Build the (type *)null that begins the traditional offsetof macro. */
9048 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
9049 tf_warning_or_error
);
9051 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9052 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
9053 true, &dummy
, token
->location
);
9056 token
= cp_lexer_peek_token (parser
->lexer
);
9057 switch (token
->type
)
9059 case CPP_OPEN_SQUARE
:
9060 /* offsetof-member-designator "[" expression "]" */
9061 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9066 /* offsetof-member-designator "->" identifier */
9067 expr
= grok_array_decl (token
->location
, expr
,
9068 integer_zero_node
, false);
9072 /* offsetof-member-designator "." identifier */
9073 cp_lexer_consume_token (parser
->lexer
);
9074 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9079 case CPP_CLOSE_PAREN
:
9080 /* Consume the ")" token. */
9081 cp_lexer_consume_token (parser
->lexer
);
9085 /* Error. We know the following require will fail, but
9086 that gives the proper error message. */
9087 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9088 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9089 expr
= error_mark_node
;
9095 expr
= finish_offsetof (expr
, loc
);
9098 parser
->integral_constant_expression_p
= save_ice_p
;
9099 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9104 /* Parse a trait expression.
9106 Returns a representation of the expression, the underlying type
9107 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9110 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9113 tree type1
, type2
= NULL_TREE
;
9114 bool binary
= false;
9115 bool variadic
= false;
9119 case RID_HAS_NOTHROW_ASSIGN
:
9120 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9122 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9123 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9125 case RID_HAS_NOTHROW_COPY
:
9126 kind
= CPTK_HAS_NOTHROW_COPY
;
9128 case RID_HAS_TRIVIAL_ASSIGN
:
9129 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9131 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9132 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9134 case RID_HAS_TRIVIAL_COPY
:
9135 kind
= CPTK_HAS_TRIVIAL_COPY
;
9137 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9138 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9140 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9141 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9143 case RID_IS_ABSTRACT
:
9144 kind
= CPTK_IS_ABSTRACT
;
9146 case RID_IS_BASE_OF
:
9147 kind
= CPTK_IS_BASE_OF
;
9151 kind
= CPTK_IS_CLASS
;
9154 kind
= CPTK_IS_EMPTY
;
9157 kind
= CPTK_IS_ENUM
;
9160 kind
= CPTK_IS_FINAL
;
9162 case RID_IS_LITERAL_TYPE
:
9163 kind
= CPTK_IS_LITERAL_TYPE
;
9168 case RID_IS_POLYMORPHIC
:
9169 kind
= CPTK_IS_POLYMORPHIC
;
9171 case RID_IS_SAME_AS
:
9172 kind
= CPTK_IS_SAME_AS
;
9175 case RID_IS_STD_LAYOUT
:
9176 kind
= CPTK_IS_STD_LAYOUT
;
9178 case RID_IS_TRIVIAL
:
9179 kind
= CPTK_IS_TRIVIAL
;
9181 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9182 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9185 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9186 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9189 case RID_IS_TRIVIALLY_COPYABLE
:
9190 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9193 kind
= CPTK_IS_UNION
;
9195 case RID_UNDERLYING_TYPE
:
9196 kind
= CPTK_UNDERLYING_TYPE
;
9201 case RID_DIRECT_BASES
:
9202 kind
= CPTK_DIRECT_BASES
;
9208 /* Consume the token. */
9209 cp_lexer_consume_token (parser
->lexer
);
9211 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9213 type1
= cp_parser_type_id (parser
);
9215 if (type1
== error_mark_node
)
9216 return error_mark_node
;
9220 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9222 type2
= cp_parser_type_id (parser
);
9224 if (type2
== error_mark_node
)
9225 return error_mark_node
;
9229 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9231 cp_lexer_consume_token (parser
->lexer
);
9232 tree elt
= cp_parser_type_id (parser
);
9233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9235 cp_lexer_consume_token (parser
->lexer
);
9236 elt
= make_pack_expansion (elt
);
9238 if (elt
== error_mark_node
)
9239 return error_mark_node
;
9240 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9244 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9246 /* Complete the trait expression, which may mean either processing
9247 the trait expr now or saving it for template instantiation. */
9250 case CPTK_UNDERLYING_TYPE
:
9251 return finish_underlying_type (type1
);
9253 return finish_bases (type1
, false);
9254 case CPTK_DIRECT_BASES
:
9255 return finish_bases (type1
, true);
9257 return finish_trait_expr (kind
, type1
, type2
);
9261 /* Lambdas that appear in variable initializer or default argument scope
9262 get that in their mangling, so we need to record it. We might as well
9263 use the count for function and namespace scopes as well. */
9264 static GTY(()) tree lambda_scope
;
9265 static GTY(()) int lambda_count
;
9266 struct GTY(()) tree_int
9271 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9274 start_lambda_scope (tree decl
)
9278 /* Once we're inside a function, we ignore other scopes and just push
9279 the function again so that popping works properly. */
9280 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9281 decl
= current_function_decl
;
9282 ti
.t
= lambda_scope
;
9283 ti
.i
= lambda_count
;
9284 vec_safe_push (lambda_scope_stack
, ti
);
9285 if (lambda_scope
!= decl
)
9287 /* Don't reset the count if we're still in the same function. */
9288 lambda_scope
= decl
;
9294 record_lambda_scope (tree lambda
)
9296 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9297 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9301 finish_lambda_scope (void)
9303 tree_int
*p
= &lambda_scope_stack
->last ();
9304 if (lambda_scope
!= p
->t
)
9306 lambda_scope
= p
->t
;
9307 lambda_count
= p
->i
;
9309 lambda_scope_stack
->pop ();
9312 /* Parse a lambda expression.
9315 lambda-introducer lambda-declarator [opt] compound-statement
9317 Returns a representation of the expression. */
9320 cp_parser_lambda_expression (cp_parser
* parser
)
9322 tree lambda_expr
= build_lambda_expr ();
9325 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9326 cp_token_position start
= 0;
9328 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9330 if (cp_unevaluated_operand
)
9332 if (!token
->error_reported
)
9334 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9335 "lambda-expression in unevaluated context");
9336 token
->error_reported
= true;
9340 else if (parser
->in_template_argument_list_p
)
9342 if (!token
->error_reported
)
9344 error_at (token
->location
, "lambda-expression in template-argument");
9345 token
->error_reported
= true;
9350 /* We may be in the middle of deferred access check. Disable
9352 push_deferring_access_checks (dk_no_deferred
);
9354 cp_parser_lambda_introducer (parser
, lambda_expr
);
9356 type
= begin_lambda_type (lambda_expr
);
9357 if (type
== error_mark_node
)
9358 return error_mark_node
;
9360 record_lambda_scope (lambda_expr
);
9362 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9363 determine_visibility (TYPE_NAME (type
));
9365 /* Now that we've started the type, add the capture fields for any
9366 explicit captures. */
9367 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9370 /* Inside the class, surrounding template-parameter-lists do not apply. */
9371 unsigned int saved_num_template_parameter_lists
9372 = parser
->num_template_parameter_lists
;
9373 unsigned char in_statement
= parser
->in_statement
;
9374 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9375 bool fully_implicit_function_template_p
9376 = parser
->fully_implicit_function_template_p
;
9377 tree implicit_template_parms
= parser
->implicit_template_parms
;
9378 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9379 bool auto_is_implicit_function_template_parm_p
9380 = parser
->auto_is_implicit_function_template_parm_p
;
9382 parser
->num_template_parameter_lists
= 0;
9383 parser
->in_statement
= 0;
9384 parser
->in_switch_statement_p
= false;
9385 parser
->fully_implicit_function_template_p
= false;
9386 parser
->implicit_template_parms
= 0;
9387 parser
->implicit_template_scope
= 0;
9388 parser
->auto_is_implicit_function_template_parm_p
= false;
9390 /* By virtue of defining a local class, a lambda expression has access to
9391 the private variables of enclosing classes. */
9393 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9397 if (!cp_parser_error_occurred (parser
)
9398 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9399 && cp_parser_start_tentative_firewall (parser
))
9401 cp_parser_lambda_body (parser
, lambda_expr
);
9403 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9405 if (cp_parser_skip_to_closing_brace (parser
))
9406 cp_lexer_consume_token (parser
->lexer
);
9409 /* The capture list was built up in reverse order; fix that now. */
9410 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9411 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9414 maybe_add_lambda_conv_op (type
);
9416 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9418 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9419 parser
->in_statement
= in_statement
;
9420 parser
->in_switch_statement_p
= in_switch_statement_p
;
9421 parser
->fully_implicit_function_template_p
9422 = fully_implicit_function_template_p
;
9423 parser
->implicit_template_parms
= implicit_template_parms
;
9424 parser
->implicit_template_scope
= implicit_template_scope
;
9425 parser
->auto_is_implicit_function_template_parm_p
9426 = auto_is_implicit_function_template_parm_p
;
9429 pop_deferring_access_checks ();
9431 /* This field is only used during parsing of the lambda. */
9432 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9434 /* This lambda shouldn't have any proxies left at this point. */
9435 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9436 /* And now that we're done, push proxies for an enclosing lambda. */
9437 insert_pending_capture_proxies ();
9440 lambda_expr
= build_lambda_object (lambda_expr
);
9442 lambda_expr
= error_mark_node
;
9444 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9449 /* Parse the beginning of a lambda expression.
9452 [ lambda-capture [opt] ]
9454 LAMBDA_EXPR is the current representation of the lambda expression. */
9457 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9459 /* Need commas after the first capture. */
9462 /* Eat the leading `['. */
9463 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9465 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9467 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9468 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9469 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9470 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9472 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9474 cp_lexer_consume_token (parser
->lexer
);
9478 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9480 cp_token
* capture_token
;
9482 tree capture_init_expr
;
9483 cp_id_kind idk
= CP_ID_KIND_NONE
;
9484 bool explicit_init_p
= false;
9486 enum capture_kind_type
9491 enum capture_kind_type capture_kind
= BY_COPY
;
9493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9495 error ("expected end of capture-list");
9502 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9504 /* Possibly capture `this'. */
9505 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9507 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9508 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9509 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9510 "with by-copy capture default");
9511 cp_lexer_consume_token (parser
->lexer
);
9512 add_capture (lambda_expr
,
9513 /*id=*/this_identifier
,
9514 /*initializer=*/finish_this_expr(),
9515 /*by_reference_p=*/false,
9520 /* Remember whether we want to capture as a reference or not. */
9521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9523 capture_kind
= BY_REFERENCE
;
9524 cp_lexer_consume_token (parser
->lexer
);
9527 /* Get the identifier. */
9528 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9529 capture_id
= cp_parser_identifier (parser
);
9531 if (capture_id
== error_mark_node
)
9532 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9533 delimiters, but I modified this to stop on unnested ']' as well. It
9534 was already changed to stop on unnested '}', so the
9535 "closing_parenthesis" name is no more misleading with my change. */
9537 cp_parser_skip_to_closing_parenthesis (parser
,
9538 /*recovering=*/true,
9540 /*consume_paren=*/true);
9544 /* Find the initializer for this capture. */
9545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9546 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9547 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9549 bool direct
, non_constant
;
9550 /* An explicit initializer exists. */
9551 if (cxx_dialect
< cxx14
)
9552 pedwarn (input_location
, 0,
9553 "lambda capture initializers "
9554 "only available with -std=c++14 or -std=gnu++14");
9555 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9557 explicit_init_p
= true;
9558 if (capture_init_expr
== NULL_TREE
)
9560 error ("empty initializer for lambda init-capture");
9561 capture_init_expr
= error_mark_node
;
9566 const char* error_msg
;
9568 /* Turn the identifier into an id-expression. */
9570 = cp_parser_lookup_name_simple (parser
, capture_id
,
9571 capture_token
->location
);
9573 if (capture_init_expr
== error_mark_node
)
9575 unqualified_name_lookup_error (capture_id
);
9578 else if (DECL_P (capture_init_expr
)
9579 && (!VAR_P (capture_init_expr
)
9580 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9582 error_at (capture_token
->location
,
9583 "capture of non-variable %qD ",
9585 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9586 "%q#D declared here", capture_init_expr
);
9589 if (VAR_P (capture_init_expr
)
9590 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9592 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9593 "%qD with non-automatic storage duration",
9595 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9596 "%q#D declared here", capture_init_expr
);
9601 = finish_id_expression
9606 /*integral_constant_expression_p=*/false,
9607 /*allow_non_integral_constant_expression_p=*/false,
9608 /*non_integral_constant_expression_p=*/NULL
,
9609 /*template_p=*/false,
9611 /*address_p=*/false,
9612 /*template_arg_p=*/false,
9614 capture_token
->location
);
9616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9618 cp_lexer_consume_token (parser
->lexer
);
9619 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9622 check_for_bare_parameter_packs (capture_init_expr
);
9625 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9626 && !explicit_init_p
)
9628 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9629 && capture_kind
== BY_COPY
)
9630 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9631 "of %qD redundant with by-copy capture default",
9633 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9634 && capture_kind
== BY_REFERENCE
)
9635 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9636 "capture of %qD redundant with by-reference capture "
9637 "default", capture_id
);
9640 add_capture (lambda_expr
,
9643 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9647 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9650 /* Parse the (optional) middle of a lambda expression.
9653 < template-parameter-list [opt] >
9654 ( parameter-declaration-clause [opt] )
9655 attribute-specifier [opt]
9657 exception-specification [opt]
9658 lambda-return-type-clause [opt]
9660 LAMBDA_EXPR is the current representation of the lambda expression. */
9663 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9665 /* 5.1.1.4 of the standard says:
9666 If a lambda-expression does not include a lambda-declarator, it is as if
9667 the lambda-declarator were ().
9668 This means an empty parameter list, no attributes, and no exception
9670 tree param_list
= void_list_node
;
9671 tree attributes
= NULL_TREE
;
9672 tree exception_spec
= NULL_TREE
;
9673 tree template_param_list
= NULL_TREE
;
9674 tree tx_qual
= NULL_TREE
;
9676 /* The template-parameter-list is optional, but must begin with
9677 an opening angle if present. */
9678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9680 if (cxx_dialect
< cxx14
)
9681 pedwarn (parser
->lexer
->next_token
->location
, 0,
9682 "lambda templates are only available with "
9683 "-std=c++14 or -std=gnu++14");
9685 cp_lexer_consume_token (parser
->lexer
);
9687 template_param_list
= cp_parser_template_parameter_list (parser
);
9689 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9691 /* We just processed one more parameter list. */
9692 ++parser
->num_template_parameter_lists
;
9695 /* The parameter-declaration-clause is optional (unless
9696 template-parameter-list was given), but must begin with an
9697 opening parenthesis if present. */
9698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9700 cp_lexer_consume_token (parser
->lexer
);
9702 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9704 /* Parse parameters. */
9705 param_list
= cp_parser_parameter_declaration_clause (parser
);
9707 /* Default arguments shall not be specified in the
9708 parameter-declaration-clause of a lambda-declarator. */
9709 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9710 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9711 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9712 "default argument specified for lambda parameter");
9714 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9716 attributes
= cp_parser_attributes_opt (parser
);
9718 /* Parse optional `mutable' keyword. */
9719 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9721 cp_lexer_consume_token (parser
->lexer
);
9722 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9725 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
9727 /* Parse optional exception specification. */
9728 exception_spec
= cp_parser_exception_specification_opt (parser
);
9730 /* Parse optional trailing return type. */
9731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9733 cp_lexer_consume_token (parser
->lexer
);
9734 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9735 = cp_parser_trailing_type_id (parser
);
9738 /* The function parameters must be in scope all the way until after the
9739 trailing-return-type in case of decltype. */
9740 pop_bindings_and_leave_scope ();
9742 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9743 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9745 /* Create the function call operator.
9747 Messing with declarators like this is no uglier than building up the
9748 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9751 cp_decl_specifier_seq return_type_specs
;
9752 cp_declarator
* declarator
;
9757 clear_decl_specs (&return_type_specs
);
9758 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9759 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9761 /* Maybe we will deduce the return type later. */
9762 return_type_specs
.type
= make_auto ();
9764 p
= obstack_alloc (&declarator_obstack
, 0);
9766 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9769 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9770 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9771 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9772 VIRT_SPEC_UNSPECIFIED
,
9776 /*late_return_type=*/NULL_TREE
,
9777 /*requires_clause*/NULL_TREE
);
9778 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9780 fco
= grokmethod (&return_type_specs
,
9783 if (fco
!= error_mark_node
)
9785 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9786 DECL_ARTIFICIAL (fco
) = 1;
9787 /* Give the object parameter a different name. */
9788 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9789 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9790 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9792 if (template_param_list
)
9794 fco
= finish_member_template_decl (fco
);
9795 finish_template_decl (template_param_list
);
9796 --parser
->num_template_parameter_lists
;
9798 else if (parser
->fully_implicit_function_template_p
)
9799 fco
= finish_fully_implicit_template (parser
, fco
);
9801 finish_member_declaration (fco
);
9803 obstack_free (&declarator_obstack
, p
);
9805 return (fco
!= error_mark_node
);
9809 /* Parse the body of a lambda expression, which is simply
9813 but which requires special handling.
9814 LAMBDA_EXPR is the current representation of the lambda expression. */
9817 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9819 bool nested
= (current_function_decl
!= NULL_TREE
);
9820 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9822 push_function_context ();
9824 /* Still increment function_depth so that we don't GC in the
9825 middle of an expression. */
9827 vec
<tree
> omp_privatization_save
;
9828 save_omp_privatization_clauses (omp_privatization_save
);
9829 /* Clear this in case we're in the middle of a default argument. */
9830 parser
->local_variables_forbidden_p
= false;
9832 /* Finish the function call operator
9834 + late_parsing_for_member
9835 + function_definition_after_declarator
9836 + ctor_initializer_opt_and_function_body */
9838 tree fco
= lambda_function (lambda_expr
);
9844 /* Let the front end know that we are going to be defining this
9846 start_preparsed_function (fco
,
9848 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9850 start_lambda_scope (fco
);
9851 body
= begin_function_body ();
9853 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9856 /* Push the proxies for any explicit captures. */
9857 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9858 cap
= TREE_CHAIN (cap
))
9859 build_capture_proxy (TREE_PURPOSE (cap
));
9861 compound_stmt
= begin_compound_stmt (0);
9863 /* 5.1.1.4 of the standard says:
9864 If a lambda-expression does not include a trailing-return-type, it
9865 is as if the trailing-return-type denotes the following type:
9866 * if the compound-statement is of the form
9867 { return attribute-specifier [opt] expression ; }
9868 the type of the returned expression after lvalue-to-rvalue
9869 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9870 (_conv.array_ 4.2), and function-to-pointer conversion
9872 * otherwise, void. */
9874 /* In a lambda that has neither a lambda-return-type-clause
9875 nor a deducible form, errors should be reported for return statements
9876 in the body. Since we used void as the placeholder return type, parsing
9877 the body as usual will give such desired behavior. */
9878 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9879 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9880 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9882 tree expr
= NULL_TREE
;
9883 cp_id_kind idk
= CP_ID_KIND_NONE
;
9885 /* Parse tentatively in case there's more after the initial return
9887 cp_parser_parse_tentatively (parser
);
9889 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9891 expr
= cp_parser_expression (parser
, &idk
);
9893 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9894 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9896 if (cp_parser_parse_definitely (parser
))
9898 if (!processing_template_decl
)
9900 tree type
= lambda_return_type (expr
);
9901 apply_deduced_return_type (fco
, type
);
9902 if (type
== error_mark_node
)
9903 expr
= error_mark_node
;
9906 /* Will get error here if type not deduced yet. */
9907 finish_return_stmt (expr
);
9915 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9916 cp_parser_label_declaration (parser
);
9917 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9918 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9921 finish_compound_stmt (compound_stmt
);
9924 finish_function_body (body
);
9925 finish_lambda_scope ();
9927 /* Finish the function and generate code for it if necessary. */
9928 tree fn
= finish_function (/*inline*/2);
9930 /* Only expand if the call op is not a template. */
9931 if (!DECL_TEMPLATE_INFO (fco
))
9932 expand_or_defer_fn (fn
);
9935 restore_omp_privatization_clauses (omp_privatization_save
);
9936 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9938 pop_function_context();
9943 /* Statements [gram.stmt.stmt] */
9945 /* Parse a statement.
9949 expression-statement
9954 declaration-statement
9961 attribute-specifier-seq (opt) expression-statement
9962 attribute-specifier-seq (opt) compound-statement
9963 attribute-specifier-seq (opt) selection-statement
9964 attribute-specifier-seq (opt) iteration-statement
9965 attribute-specifier-seq (opt) jump-statement
9966 declaration-statement
9967 attribute-specifier-seq (opt) try-block
9974 IN_COMPOUND is true when the statement is nested inside a
9975 cp_parser_compound_statement; this matters for certain pragmas.
9977 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9978 is a (possibly labeled) if statement which is not enclosed in braces
9979 and has an else clause. This is used to implement -Wparentheses.
9981 CHAIN is a vector of if-else-if conditions. */
9984 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9985 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
9987 tree statement
, std_attrs
= NULL_TREE
;
9989 location_t statement_location
, attrs_location
;
9994 /* There is no statement yet. */
9995 statement
= NULL_TREE
;
9997 saved_token_sentinel
saved_tokens (parser
->lexer
);
9998 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9999 if (c_dialect_objc ())
10000 /* In obj-c++, seeing '[[' might be the either the beginning of
10001 c++11 attributes, or a nested objc-message-expression. So
10002 let's parse the c++11 attributes tentatively. */
10003 cp_parser_parse_tentatively (parser
);
10004 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10005 if (c_dialect_objc ())
10007 if (!cp_parser_parse_definitely (parser
))
10008 std_attrs
= NULL_TREE
;
10011 /* Peek at the next token. */
10012 token
= cp_lexer_peek_token (parser
->lexer
);
10013 /* Remember the location of the first token in the statement. */
10014 statement_location
= token
->location
;
10015 /* If this is a keyword, then that will often determine what kind of
10016 statement we have. */
10017 if (token
->type
== CPP_KEYWORD
)
10019 enum rid keyword
= token
->keyword
;
10025 /* Looks like a labeled-statement with a case label.
10026 Parse the label, and then use tail recursion to parse
10028 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10033 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10039 statement
= cp_parser_iteration_statement (parser
, false);
10043 if (!flag_cilkplus
)
10045 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10046 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10047 cp_lexer_consume_token (parser
->lexer
);
10048 statement
= error_mark_node
;
10051 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
10058 statement
= cp_parser_jump_statement (parser
);
10061 case RID_CILK_SYNC
:
10062 cp_lexer_consume_token (parser
->lexer
);
10065 tree sync_expr
= build_cilk_sync ();
10066 SET_EXPR_LOCATION (sync_expr
,
10068 statement
= finish_expr_stmt (sync_expr
);
10072 error_at (token
->location
, "-fcilkplus must be enabled to use"
10073 " %<_Cilk_sync%>");
10074 statement
= error_mark_node
;
10076 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10079 /* Objective-C++ exception-handling constructs. */
10082 case RID_AT_FINALLY
:
10083 case RID_AT_SYNCHRONIZED
:
10085 statement
= cp_parser_objc_statement (parser
);
10089 statement
= cp_parser_try_block (parser
);
10092 case RID_NAMESPACE
:
10093 /* This must be a namespace alias definition. */
10094 cp_parser_declaration_statement (parser
);
10097 case RID_TRANSACTION_ATOMIC
:
10098 case RID_TRANSACTION_RELAXED
:
10099 case RID_SYNCHRONIZED
:
10100 case RID_ATOMIC_NOEXCEPT
:
10101 case RID_ATOMIC_CANCEL
:
10102 statement
= cp_parser_transaction (parser
, token
);
10104 case RID_TRANSACTION_CANCEL
:
10105 statement
= cp_parser_transaction_cancel (parser
);
10109 /* It might be a keyword like `int' that can start a
10110 declaration-statement. */
10114 else if (token
->type
== CPP_NAME
)
10116 /* If the next token is a `:', then we are looking at a
10117 labeled-statement. */
10118 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10119 if (token
->type
== CPP_COLON
)
10121 /* Looks like a labeled-statement with an ordinary label.
10122 Parse the label, and then use tail recursion to parse
10125 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10129 /* Anything that starts with a `{' must be a compound-statement. */
10130 else if (token
->type
== CPP_OPEN_BRACE
)
10131 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10132 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10133 a statement all its own. */
10134 else if (token
->type
== CPP_PRAGMA
)
10136 /* Only certain OpenMP pragmas are attached to statements, and thus
10137 are considered statements themselves. All others are not. In
10138 the context of a compound, accept the pragma as a "statement" and
10139 return so that we can check for a close brace. Otherwise we
10140 require a real statement and must go back and read one. */
10142 cp_parser_pragma (parser
, pragma_compound
);
10143 else if (!cp_parser_pragma (parser
, pragma_stmt
))
10147 else if (token
->type
== CPP_EOF
)
10149 cp_parser_error (parser
, "expected statement");
10153 /* Everything else must be a declaration-statement or an
10154 expression-statement. Try for the declaration-statement
10155 first, unless we are looking at a `;', in which case we know that
10156 we have an expression-statement. */
10159 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10161 if (std_attrs
!= NULL_TREE
)
10163 /* Attributes should be parsed as part of the the
10164 declaration, so let's un-parse them. */
10165 saved_tokens
.rollback();
10166 std_attrs
= NULL_TREE
;
10169 cp_parser_parse_tentatively (parser
);
10170 /* Try to parse the declaration-statement. */
10171 cp_parser_declaration_statement (parser
);
10172 /* If that worked, we're done. */
10173 if (cp_parser_parse_definitely (parser
))
10176 /* Look for an expression-statement instead. */
10177 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10180 /* Set the line number for the statement. */
10181 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10182 SET_EXPR_LOCATION (statement
, statement_location
);
10184 /* Note that for now, we don't do anything with c++11 statements
10185 parsed at this level. */
10186 if (std_attrs
!= NULL_TREE
)
10187 warning_at (attrs_location
,
10189 "attributes at the beginning of statement are ignored");
10192 /* Parse the label for a labeled-statement, i.e.
10195 case constant-expression :
10199 case constant-expression ... constant-expression : statement
10201 When a label is parsed without errors, the label is added to the
10202 parse tree by the finish_* functions, so this function doesn't
10203 have to return the label. */
10206 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10209 tree label
= NULL_TREE
;
10210 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10212 /* The next token should be an identifier. */
10213 token
= cp_lexer_peek_token (parser
->lexer
);
10214 if (token
->type
!= CPP_NAME
10215 && token
->type
!= CPP_KEYWORD
)
10217 cp_parser_error (parser
, "expected labeled-statement");
10221 parser
->colon_corrects_to_scope_p
= false;
10222 switch (token
->keyword
)
10226 tree expr
, expr_hi
;
10227 cp_token
*ellipsis
;
10229 /* Consume the `case' token. */
10230 cp_lexer_consume_token (parser
->lexer
);
10231 /* Parse the constant-expression. */
10232 expr
= cp_parser_constant_expression (parser
);
10233 if (check_for_bare_parameter_packs (expr
))
10234 expr
= error_mark_node
;
10236 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10237 if (ellipsis
->type
== CPP_ELLIPSIS
)
10239 /* Consume the `...' token. */
10240 cp_lexer_consume_token (parser
->lexer
);
10241 expr_hi
= cp_parser_constant_expression (parser
);
10242 if (check_for_bare_parameter_packs (expr_hi
))
10243 expr_hi
= error_mark_node
;
10245 /* We don't need to emit warnings here, as the common code
10246 will do this for us. */
10249 expr_hi
= NULL_TREE
;
10251 if (parser
->in_switch_statement_p
)
10252 finish_case_label (token
->location
, expr
, expr_hi
);
10254 error_at (token
->location
,
10255 "case label %qE not within a switch statement",
10261 /* Consume the `default' token. */
10262 cp_lexer_consume_token (parser
->lexer
);
10264 if (parser
->in_switch_statement_p
)
10265 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10267 error_at (token
->location
, "case label not within a switch statement");
10271 /* Anything else must be an ordinary label. */
10272 label
= finish_label_stmt (cp_parser_identifier (parser
));
10276 /* Require the `:' token. */
10277 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10279 /* An ordinary label may optionally be followed by attributes.
10280 However, this is only permitted if the attributes are then
10281 followed by a semicolon. This is because, for backward
10282 compatibility, when parsing
10283 lab: __attribute__ ((unused)) int i;
10284 we want the attribute to attach to "i", not "lab". */
10285 if (label
!= NULL_TREE
10286 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10289 cp_parser_parse_tentatively (parser
);
10290 attrs
= cp_parser_gnu_attributes_opt (parser
);
10291 if (attrs
== NULL_TREE
10292 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10293 cp_parser_abort_tentative_parse (parser
);
10294 else if (!cp_parser_parse_definitely (parser
))
10297 attributes
= chainon (attributes
, attrs
);
10300 if (attributes
!= NULL_TREE
)
10301 cplus_decl_attributes (&label
, attributes
, 0);
10303 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10306 /* Parse an expression-statement.
10308 expression-statement:
10311 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10312 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10313 indicates whether this expression-statement is part of an
10314 expression statement. */
10317 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10319 tree statement
= NULL_TREE
;
10320 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10322 /* If the next token is a ';', then there is no expression
10324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10326 statement
= cp_parser_expression (parser
);
10327 if (statement
== error_mark_node
10328 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10330 cp_parser_skip_to_end_of_block_or_statement (parser
);
10331 return error_mark_node
;
10335 /* Give a helpful message for "A<T>::type t;" and the like. */
10336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10337 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10339 if (TREE_CODE (statement
) == SCOPE_REF
)
10340 error_at (token
->location
, "need %<typename%> before %qE because "
10341 "%qT is a dependent scope",
10342 statement
, TREE_OPERAND (statement
, 0));
10343 else if (is_overloaded_fn (statement
)
10344 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10347 tree fn
= get_first_fn (statement
);
10348 error_at (token
->location
,
10349 "%<%T::%D%> names the constructor, not the type",
10350 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10354 /* Consume the final `;'. */
10355 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10357 if (in_statement_expr
10358 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10359 /* This is the final expression statement of a statement
10361 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10362 else if (statement
)
10363 statement
= finish_expr_stmt (statement
);
10368 /* Parse a compound-statement.
10370 compound-statement:
10371 { statement-seq [opt] }
10375 compound-statement:
10376 { label-declaration-seq [opt] statement-seq [opt] }
10378 label-declaration-seq:
10380 label-declaration-seq label-declaration
10382 Returns a tree representing the statement. */
10385 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10386 int bcs_flags
, bool function_body
)
10388 tree compound_stmt
;
10390 /* Consume the `{'. */
10391 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10392 return error_mark_node
;
10393 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10394 && !function_body
&& cxx_dialect
< cxx14
)
10395 pedwarn (input_location
, OPT_Wpedantic
,
10396 "compound-statement in constexpr function");
10397 /* Begin the compound-statement. */
10398 compound_stmt
= begin_compound_stmt (bcs_flags
);
10399 /* If the next keyword is `__label__' we have a label declaration. */
10400 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10401 cp_parser_label_declaration (parser
);
10402 /* Parse an (optional) statement-seq. */
10403 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10404 /* Finish the compound-statement. */
10405 finish_compound_stmt (compound_stmt
);
10406 /* Consume the `}'. */
10407 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10409 return compound_stmt
;
10412 /* Parse an (optional) statement-seq.
10416 statement-seq [opt] statement */
10419 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10421 /* Scan statements until there aren't any more. */
10424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10426 /* If we are looking at a `}', then we have run out of
10427 statements; the same is true if we have reached the end
10428 of file, or have stumbled upon a stray '@end'. */
10429 if (token
->type
== CPP_CLOSE_BRACE
10430 || token
->type
== CPP_EOF
10431 || token
->type
== CPP_PRAGMA_EOL
10432 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10435 /* If we are in a compound statement and find 'else' then
10436 something went wrong. */
10437 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10439 if (parser
->in_statement
& IN_IF_STMT
)
10443 token
= cp_lexer_consume_token (parser
->lexer
);
10444 error_at (token
->location
, "%<else%> without a previous %<if%>");
10448 /* Parse the statement. */
10449 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10453 /* Parse a selection-statement.
10455 selection-statement:
10456 if ( condition ) statement
10457 if ( condition ) statement else statement
10458 switch ( condition ) statement
10460 Returns the new IF_STMT or SWITCH_STMT.
10462 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10463 is a (possibly labeled) if statement which is not enclosed in
10464 braces and has an else clause. This is used to implement
10467 CHAIN is a vector of if-else-if conditions. This is used to implement
10468 -Wduplicated-cond. */
10471 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
10476 token_indent_info guard_tinfo
;
10481 /* Peek at the next token. */
10482 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10483 guard_tinfo
= get_token_indent_info (token
);
10485 /* See what kind of keyword it is. */
10486 keyword
= token
->keyword
;
10495 /* Look for the `('. */
10496 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10498 cp_parser_skip_to_end_of_statement (parser
);
10499 return error_mark_node
;
10502 /* Begin the selection-statement. */
10503 if (keyword
== RID_IF
)
10504 statement
= begin_if_stmt ();
10506 statement
= begin_switch_stmt ();
10508 /* Parse the condition. */
10509 condition
= cp_parser_condition (parser
);
10510 /* Look for the `)'. */
10511 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10512 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10513 /*consume_paren=*/true);
10515 if (keyword
== RID_IF
)
10518 unsigned char in_statement
;
10520 /* Add the condition. */
10521 finish_if_stmt_cond (condition
, statement
);
10523 if (warn_duplicated_cond
)
10524 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
10527 /* Parse the then-clause. */
10528 in_statement
= parser
->in_statement
;
10529 parser
->in_statement
|= IN_IF_STMT
;
10530 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
10532 parser
->in_statement
= in_statement
;
10534 finish_then_clause (statement
);
10536 /* If the next token is `else', parse the else-clause. */
10537 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10541 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
10542 /* Consume the `else' keyword. */
10543 cp_lexer_consume_token (parser
->lexer
);
10544 if (warn_duplicated_cond
)
10546 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10550 /* We've got "if (COND) else if (COND2)". Start
10551 the condition chain and add COND as the first
10553 chain
= new vec
<tree
> ();
10554 if (!CONSTANT_CLASS_P (condition
)
10555 && !TREE_SIDE_EFFECTS (condition
))
10557 /* Wrap it in a NOP_EXPR so that we can set the
10558 location of the condition. */
10559 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
10561 SET_EXPR_LOCATION (e
, token
->location
);
10562 chain
->safe_push (e
);
10565 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
10568 /* This is if-else without subsequent if. Zap the
10569 condition chain; we would have already warned at
10575 begin_else_clause (statement
);
10576 /* Parse the else-clause. */
10577 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10578 guard_tinfo
, chain
);
10580 finish_else_clause (statement
);
10582 /* If we are currently parsing a then-clause, then
10583 IF_P will not be NULL. We set it to true to
10584 indicate that this if statement has an else clause.
10585 This may trigger the Wparentheses warning below
10586 when we get back up to the parent if statement. */
10592 /* This if statement does not have an else clause. If
10593 NESTED_IF is true, then the then-clause is an if
10594 statement which does have an else clause. We warn
10595 about the potential ambiguity. */
10597 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10598 "suggest explicit braces to avoid ambiguous"
10600 if (warn_duplicated_cond
)
10602 /* We don't need the condition chain anymore. */
10608 /* Now we're all done with the if-statement. */
10609 finish_if_stmt (statement
);
10613 bool in_switch_statement_p
;
10614 unsigned char in_statement
;
10616 /* Add the condition. */
10617 finish_switch_cond (condition
, statement
);
10619 /* Parse the body of the switch-statement. */
10620 in_switch_statement_p
= parser
->in_switch_statement_p
;
10621 in_statement
= parser
->in_statement
;
10622 parser
->in_switch_statement_p
= true;
10623 parser
->in_statement
|= IN_SWITCH_STMT
;
10624 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10626 parser
->in_switch_statement_p
= in_switch_statement_p
;
10627 parser
->in_statement
= in_statement
;
10629 /* Now we're all done with the switch-statement. */
10630 finish_switch_stmt (statement
);
10638 cp_parser_error (parser
, "expected selection-statement");
10639 return error_mark_node
;
10643 /* Parse a condition.
10647 type-specifier-seq declarator = initializer-clause
10648 type-specifier-seq declarator braced-init-list
10653 type-specifier-seq declarator asm-specification [opt]
10654 attributes [opt] = assignment-expression
10656 Returns the expression that should be tested. */
10659 cp_parser_condition (cp_parser
* parser
)
10661 cp_decl_specifier_seq type_specifiers
;
10662 const char *saved_message
;
10663 int declares_class_or_enum
;
10665 /* Try the declaration first. */
10666 cp_parser_parse_tentatively (parser
);
10667 /* New types are not allowed in the type-specifier-seq for a
10669 saved_message
= parser
->type_definition_forbidden_message
;
10670 parser
->type_definition_forbidden_message
10671 = G_("types may not be defined in conditions");
10672 /* Parse the type-specifier-seq. */
10673 cp_parser_decl_specifier_seq (parser
,
10674 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10676 &declares_class_or_enum
);
10677 /* Restore the saved message. */
10678 parser
->type_definition_forbidden_message
= saved_message
;
10679 /* If all is well, we might be looking at a declaration. */
10680 if (!cp_parser_error_occurred (parser
))
10683 tree asm_specification
;
10685 cp_declarator
*declarator
;
10686 tree initializer
= NULL_TREE
;
10688 /* Parse the declarator. */
10689 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10690 /*ctor_dtor_or_conv_p=*/NULL
,
10691 /*parenthesized_p=*/NULL
,
10692 /*member_p=*/false,
10693 /*friend_p=*/false);
10694 /* Parse the attributes. */
10695 attributes
= cp_parser_attributes_opt (parser
);
10696 /* Parse the asm-specification. */
10697 asm_specification
= cp_parser_asm_specification_opt (parser
);
10698 /* If the next token is not an `=' or '{', then we might still be
10699 looking at an expression. For example:
10703 looks like a decl-specifier-seq and a declarator -- but then
10704 there is no `=', so this is an expression. */
10705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10706 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10707 cp_parser_simulate_error (parser
);
10709 /* If we did see an `=' or '{', then we are looking at a declaration
10711 if (cp_parser_parse_definitely (parser
))
10714 bool non_constant_p
;
10715 bool flags
= LOOKUP_ONLYCONVERTING
;
10717 /* Create the declaration. */
10718 decl
= start_decl (declarator
, &type_specifiers
,
10719 /*initialized_p=*/true,
10720 attributes
, /*prefix_attributes=*/NULL_TREE
,
10723 /* Parse the initializer. */
10724 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10726 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10727 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10732 /* Consume the `='. */
10733 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10734 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10736 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10737 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10739 /* Process the initializer. */
10740 cp_finish_decl (decl
,
10741 initializer
, !non_constant_p
,
10746 pop_scope (pushed_scope
);
10748 return convert_from_reference (decl
);
10751 /* If we didn't even get past the declarator successfully, we are
10752 definitely not looking at a declaration. */
10754 cp_parser_abort_tentative_parse (parser
);
10756 /* Otherwise, we are looking at an expression. */
10757 return cp_parser_expression (parser
);
10760 /* Parses a for-statement or range-for-statement until the closing ')',
10764 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10766 tree init
, scope
, decl
;
10769 /* Begin the for-statement. */
10770 scope
= begin_for_scope (&init
);
10772 /* Parse the initialization. */
10773 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10776 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10778 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10782 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10784 /* Normal for loop */
10785 tree condition
= NULL_TREE
;
10786 tree expression
= NULL_TREE
;
10789 stmt
= begin_for_stmt (scope
, init
);
10790 /* The for-init-statement has already been parsed in
10791 cp_parser_for_init_statement, so no work is needed here. */
10792 finish_for_init_stmt (stmt
);
10794 /* If there's a condition, process it. */
10795 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10796 condition
= cp_parser_condition (parser
);
10799 cp_parser_error (parser
, "missing loop condition in loop with "
10800 "%<GCC ivdep%> pragma");
10801 condition
= error_mark_node
;
10803 finish_for_cond (condition
, stmt
, ivdep
);
10804 /* Look for the `;'. */
10805 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10807 /* If there's an expression, process it. */
10808 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10809 expression
= cp_parser_expression (parser
);
10810 finish_for_expr (expression
, stmt
);
10815 /* Tries to parse a range-based for-statement:
10818 decl-specifier-seq declarator : expression
10820 The decl-specifier-seq declarator and the `:' are already parsed by
10821 cp_parser_for_init_statement. If processing_template_decl it returns a
10822 newly created RANGE_FOR_STMT; if not, it is converted to a
10823 regular FOR_STMT. */
10826 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10829 tree stmt
, range_expr
;
10831 /* Get the range declaration momentarily out of the way so that
10832 the range expression doesn't clash with it. */
10833 if (range_decl
!= error_mark_node
)
10834 pop_binding (DECL_NAME (range_decl
), range_decl
);
10836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10838 bool expr_non_constant_p
;
10839 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10842 range_expr
= cp_parser_expression (parser
);
10844 /* Put the range declaration back into scope. */
10845 if (range_decl
!= error_mark_node
)
10846 push_binding (DECL_NAME (range_decl
), range_decl
, current_binding_level
);
10848 /* If in template, STMT is converted to a normal for-statement
10849 at instantiation. If not, it is done just ahead. */
10850 if (processing_template_decl
)
10852 if (check_for_bare_parameter_packs (range_expr
))
10853 range_expr
= error_mark_node
;
10854 stmt
= begin_range_for_stmt (scope
, init
);
10856 RANGE_FOR_IVDEP (stmt
) = 1;
10857 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10858 if (!type_dependent_expression_p (range_expr
)
10859 /* do_auto_deduction doesn't mess with template init-lists. */
10860 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10861 do_range_for_auto_deduction (range_decl
, range_expr
);
10865 stmt
= begin_for_stmt (scope
, init
);
10866 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10871 /* Subroutine of cp_convert_range_for: given the initializer expression,
10872 builds up the range temporary. */
10875 build_range_temp (tree range_expr
)
10877 tree range_type
, range_temp
;
10879 /* Find out the type deduced by the declaration
10880 `auto &&__range = range_expr'. */
10881 range_type
= cp_build_reference_type (make_auto (), true);
10882 range_type
= do_auto_deduction (range_type
, range_expr
,
10883 type_uses_auto (range_type
));
10885 /* Create the __range variable. */
10886 range_temp
= build_decl (input_location
, VAR_DECL
,
10887 get_identifier ("__for_range"), range_type
);
10888 TREE_USED (range_temp
) = 1;
10889 DECL_ARTIFICIAL (range_temp
) = 1;
10894 /* Used by cp_parser_range_for in template context: we aren't going to
10895 do a full conversion yet, but we still need to resolve auto in the
10896 type of the for-range-declaration if present. This is basically
10897 a shortcut version of cp_convert_range_for. */
10900 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10902 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10905 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10906 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10907 iter_type
= (cp_parser_perform_range_for_lookup
10908 (range_temp
, &begin_dummy
, &end_dummy
));
10911 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10913 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10914 tf_warning_or_error
);
10915 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10916 iter_decl
, auto_node
);
10921 /* Converts a range-based for-statement into a normal
10922 for-statement, as per the definition.
10924 for (RANGE_DECL : RANGE_EXPR)
10927 should be equivalent to:
10930 auto &&__range = RANGE_EXPR;
10931 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10935 RANGE_DECL = *__begin;
10940 If RANGE_EXPR is an array:
10941 BEGIN_EXPR = __range
10942 END_EXPR = __range + ARRAY_SIZE(__range)
10943 Else if RANGE_EXPR has a member 'begin' or 'end':
10944 BEGIN_EXPR = __range.begin()
10945 END_EXPR = __range.end()
10947 BEGIN_EXPR = begin(__range)
10948 END_EXPR = end(__range);
10950 If __range has a member 'begin' but not 'end', or vice versa, we must
10951 still use the second alternative (it will surely fail, however).
10952 When calling begin()/end() in the third alternative we must use
10953 argument dependent lookup, but always considering 'std' as an associated
10957 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10961 tree iter_type
, begin_expr
, end_expr
;
10962 tree condition
, expression
;
10964 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10965 /* If an error happened previously do nothing or else a lot of
10966 unhelpful errors would be issued. */
10967 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10972 if (VAR_P (range_expr
)
10973 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10974 /* Can't bind a reference to an array of runtime bound. */
10975 range_temp
= range_expr
;
10978 range_temp
= build_range_temp (range_expr
);
10979 pushdecl (range_temp
);
10980 cp_finish_decl (range_temp
, range_expr
,
10981 /*is_constant_init*/false, NULL_TREE
,
10982 LOOKUP_ONLYCONVERTING
);
10983 range_temp
= convert_from_reference (range_temp
);
10985 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10986 &begin_expr
, &end_expr
);
10989 /* The new for initialization statement. */
10990 begin
= build_decl (input_location
, VAR_DECL
,
10991 get_identifier ("__for_begin"), iter_type
);
10992 TREE_USED (begin
) = 1;
10993 DECL_ARTIFICIAL (begin
) = 1;
10995 cp_finish_decl (begin
, begin_expr
,
10996 /*is_constant_init*/false, NULL_TREE
,
10997 LOOKUP_ONLYCONVERTING
);
10999 end
= build_decl (input_location
, VAR_DECL
,
11000 get_identifier ("__for_end"), iter_type
);
11001 TREE_USED (end
) = 1;
11002 DECL_ARTIFICIAL (end
) = 1;
11004 cp_finish_decl (end
, end_expr
,
11005 /*is_constant_init*/false, NULL_TREE
,
11006 LOOKUP_ONLYCONVERTING
);
11008 finish_for_init_stmt (statement
);
11010 /* The new for condition. */
11011 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11014 NULL
, tf_warning_or_error
);
11015 finish_for_cond (condition
, statement
, ivdep
);
11017 /* The new increment expression. */
11018 expression
= finish_unary_op_expr (input_location
,
11019 PREINCREMENT_EXPR
, begin
,
11020 tf_warning_or_error
);
11021 finish_for_expr (expression
, statement
);
11023 /* The declaration is initialized with *__begin inside the loop body. */
11024 cp_finish_decl (range_decl
,
11025 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11026 tf_warning_or_error
),
11027 /*is_constant_init*/false, NULL_TREE
,
11028 LOOKUP_ONLYCONVERTING
);
11033 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11034 We need to solve both at the same time because the method used
11035 depends on the existence of members begin or end.
11036 Returns the type deduced for the iterator expression. */
11039 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11041 if (error_operand_p (range
))
11043 *begin
= *end
= error_mark_node
;
11044 return error_mark_node
;
11047 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11049 error ("range-based %<for%> expression of type %qT "
11050 "has incomplete type", TREE_TYPE (range
));
11051 *begin
= *end
= error_mark_node
;
11052 return error_mark_node
;
11054 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11056 /* If RANGE is an array, we will use pointer arithmetic. */
11058 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11060 array_type_nelts_top (TREE_TYPE (range
)),
11062 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
11066 /* If it is not an array, we must do a bit of magic. */
11067 tree id_begin
, id_end
;
11068 tree member_begin
, member_end
;
11070 *begin
= *end
= error_mark_node
;
11072 id_begin
= get_identifier ("begin");
11073 id_end
= get_identifier ("end");
11074 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11075 /*protect=*/2, /*want_type=*/false,
11076 tf_warning_or_error
);
11077 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11078 /*protect=*/2, /*want_type=*/false,
11079 tf_warning_or_error
);
11081 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11083 /* Use the member functions. */
11084 if (member_begin
!= NULL_TREE
)
11085 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11087 error ("range-based %<for%> expression of type %qT has an "
11088 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11090 if (member_end
!= NULL_TREE
)
11091 *end
= cp_parser_range_for_member_function (range
, id_end
);
11093 error ("range-based %<for%> expression of type %qT has a "
11094 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11098 /* Use global functions with ADL. */
11099 vec
<tree
, va_gc
> *vec
;
11100 vec
= make_tree_vector ();
11102 vec_safe_push (vec
, range
);
11104 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11105 tf_warning_or_error
);
11106 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11107 tf_warning_or_error
);
11108 member_end
= perform_koenig_lookup (id_end
, vec
,
11109 tf_warning_or_error
);
11110 *end
= finish_call_expr (member_end
, &vec
, false, true,
11111 tf_warning_or_error
);
11113 release_tree_vector (vec
);
11116 /* Last common checks. */
11117 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11119 /* If one of the expressions is an error do no more checks. */
11120 *begin
= *end
= error_mark_node
;
11121 return error_mark_node
;
11123 else if (type_dependent_expression_p (*begin
)
11124 || type_dependent_expression_p (*end
))
11125 /* Can happen, when, eg, in a template context, Koenig lookup
11126 can't resolve begin/end (c++/58503). */
11130 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11131 /* The unqualified type of the __begin and __end temporaries should
11132 be the same, as required by the multiple auto declaration. */
11133 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11134 error ("inconsistent begin/end types in range-based %<for%> "
11135 "statement: %qT and %qT",
11136 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11142 /* Helper function for cp_parser_perform_range_for_lookup.
11143 Builds a tree for RANGE.IDENTIFIER(). */
11146 cp_parser_range_for_member_function (tree range
, tree identifier
)
11149 vec
<tree
, va_gc
> *vec
;
11151 member
= finish_class_member_access_expr (range
, identifier
,
11152 false, tf_warning_or_error
);
11153 if (member
== error_mark_node
)
11154 return error_mark_node
;
11156 vec
= make_tree_vector ();
11157 res
= finish_call_expr (member
, &vec
,
11158 /*disallow_virtual=*/false,
11159 /*koenig_p=*/false,
11160 tf_warning_or_error
);
11161 release_tree_vector (vec
);
11165 /* Parse an iteration-statement.
11167 iteration-statement:
11168 while ( condition ) statement
11169 do statement while ( expression ) ;
11170 for ( for-init-statement condition [opt] ; expression [opt] )
11173 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11176 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
11181 unsigned char in_statement
;
11182 token_indent_info guard_tinfo
;
11184 /* Peek at the next token. */
11185 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11187 return error_mark_node
;
11189 guard_tinfo
= get_token_indent_info (token
);
11191 /* Remember whether or not we are already within an iteration
11193 in_statement
= parser
->in_statement
;
11195 /* See what kind of keyword it is. */
11196 keyword
= token
->keyword
;
11203 /* Begin the while-statement. */
11204 statement
= begin_while_stmt ();
11205 /* Look for the `('. */
11206 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11207 /* Parse the condition. */
11208 condition
= cp_parser_condition (parser
);
11209 finish_while_stmt_cond (condition
, statement
, ivdep
);
11210 /* Look for the `)'. */
11211 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11212 /* Parse the dependent statement. */
11213 parser
->in_statement
= IN_ITERATION_STMT
;
11214 cp_parser_already_scoped_statement (parser
, guard_tinfo
);
11215 parser
->in_statement
= in_statement
;
11216 /* We're done with the while-statement. */
11217 finish_while_stmt (statement
);
11225 /* Begin the do-statement. */
11226 statement
= begin_do_stmt ();
11227 /* Parse the body of the do-statement. */
11228 parser
->in_statement
= IN_ITERATION_STMT
;
11229 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11230 parser
->in_statement
= in_statement
;
11231 finish_do_body (statement
);
11232 /* Look for the `while' keyword. */
11233 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11234 /* Look for the `('. */
11235 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11236 /* Parse the expression. */
11237 expression
= cp_parser_expression (parser
);
11238 /* We're done with the do-statement. */
11239 finish_do_stmt (expression
, statement
, ivdep
);
11240 /* Look for the `)'. */
11241 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11242 /* Look for the `;'. */
11243 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11249 /* Look for the `('. */
11250 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11252 statement
= cp_parser_for (parser
, ivdep
);
11254 /* Look for the `)'. */
11255 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11257 /* Parse the body of the for-statement. */
11258 parser
->in_statement
= IN_ITERATION_STMT
;
11259 cp_parser_already_scoped_statement (parser
, guard_tinfo
);
11260 parser
->in_statement
= in_statement
;
11262 /* We're done with the for-statement. */
11263 finish_for_stmt (statement
);
11268 cp_parser_error (parser
, "expected iteration-statement");
11269 statement
= error_mark_node
;
11276 /* Parse a for-init-statement or the declarator of a range-based-for.
11277 Returns true if a range-based-for declaration is seen.
11279 for-init-statement:
11280 expression-statement
11281 simple-declaration */
11284 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
11286 /* If the next token is a `;', then we have an empty
11287 expression-statement. Grammatically, this is also a
11288 simple-declaration, but an invalid one, because it does not
11289 declare anything. Therefore, if we did not handle this case
11290 specially, we would issue an error message about an invalid
11292 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11294 bool is_range_for
= false;
11295 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11297 /* A colon is used in range-based for. */
11298 parser
->colon_corrects_to_scope_p
= false;
11300 /* We're going to speculatively look for a declaration, falling back
11301 to an expression, if necessary. */
11302 cp_parser_parse_tentatively (parser
);
11303 /* Parse the declaration. */
11304 cp_parser_simple_declaration (parser
,
11305 /*function_definition_allowed_p=*/false,
11307 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11310 /* It is a range-for, consume the ':' */
11311 cp_lexer_consume_token (parser
->lexer
);
11312 is_range_for
= true;
11313 if (cxx_dialect
< cxx11
)
11315 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11316 "range-based %<for%> loops only available with "
11317 "-std=c++11 or -std=gnu++11");
11318 *decl
= error_mark_node
;
11322 /* The ';' is not consumed yet because we told
11323 cp_parser_simple_declaration not to. */
11324 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11326 if (cp_parser_parse_definitely (parser
))
11327 return is_range_for
;
11328 /* If the tentative parse failed, then we shall need to look for an
11329 expression-statement. */
11331 /* If we are here, it is an expression-statement. */
11332 cp_parser_expression_statement (parser
, NULL_TREE
);
11336 /* Parse a jump-statement.
11341 return expression [opt] ;
11342 return braced-init-list ;
11348 goto * expression ;
11350 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11353 cp_parser_jump_statement (cp_parser
* parser
)
11355 tree statement
= error_mark_node
;
11358 unsigned char in_statement
;
11360 /* Peek at the next token. */
11361 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
11363 return error_mark_node
;
11365 /* See what kind of keyword it is. */
11366 keyword
= token
->keyword
;
11370 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11371 switch (in_statement
)
11374 error_at (token
->location
, "break statement not within loop or switch");
11377 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11378 || in_statement
== IN_ITERATION_STMT
);
11379 statement
= finish_break_stmt ();
11380 if (in_statement
== IN_ITERATION_STMT
)
11381 break_maybe_infinite_loop ();
11384 error_at (token
->location
, "invalid exit from OpenMP structured block");
11387 error_at (token
->location
, "break statement used with OpenMP for loop");
11389 case IN_CILK_SIMD_FOR
:
11390 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11393 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11397 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11400 error_at (token
->location
, "continue statement not within a loop");
11402 case IN_CILK_SIMD_FOR
:
11403 error_at (token
->location
,
11404 "continue statement within %<#pragma simd%> loop body");
11405 /* Fall through. */
11406 case IN_ITERATION_STMT
:
11408 statement
= finish_continue_stmt ();
11411 error_at (token
->location
, "invalid exit from OpenMP structured block");
11414 gcc_unreachable ();
11416 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11422 bool expr_non_constant_p
;
11424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11426 cp_lexer_set_source_position (parser
->lexer
);
11427 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11428 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11430 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11431 expr
= cp_parser_expression (parser
);
11433 /* If the next token is a `;', then there is no
11436 /* Build the return-statement. */
11437 statement
= finish_return_stmt (expr
);
11438 /* Look for the final `;'. */
11439 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11444 if (parser
->in_function_body
11445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11447 error ("%<goto%> in %<constexpr%> function");
11448 cp_function_chain
->invalid_constexpr
= true;
11451 /* Create the goto-statement. */
11452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11454 /* Issue a warning about this use of a GNU extension. */
11455 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11456 /* Consume the '*' token. */
11457 cp_lexer_consume_token (parser
->lexer
);
11458 /* Parse the dependent expression. */
11459 finish_goto_stmt (cp_parser_expression (parser
));
11462 finish_goto_stmt (cp_parser_identifier (parser
));
11463 /* Look for the final `;'. */
11464 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11468 cp_parser_error (parser
, "expected jump-statement");
11475 /* Parse a declaration-statement.
11477 declaration-statement:
11478 block-declaration */
11481 cp_parser_declaration_statement (cp_parser
* parser
)
11485 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11486 p
= obstack_alloc (&declarator_obstack
, 0);
11488 /* Parse the block-declaration. */
11489 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11491 /* Free any declarators allocated. */
11492 obstack_free (&declarator_obstack
, p
);
11495 /* Some dependent statements (like `if (cond) statement'), are
11496 implicitly in their own scope. In other words, if the statement is
11497 a single statement (as opposed to a compound-statement), it is
11498 none-the-less treated as if it were enclosed in braces. Any
11499 declarations appearing in the dependent statement are out of scope
11500 after control passes that point. This function parses a statement,
11501 but ensures that is in its own scope, even if it is not a
11502 compound-statement.
11504 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11505 is a (possibly labeled) if statement which is not enclosed in
11506 braces and has an else clause. This is used to implement
11509 CHAIN is a vector of if-else-if conditions. This is used to implement
11512 Returns the new statement. */
11515 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
11516 const token_indent_info
&guard_tinfo
,
11520 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11521 token_indent_info body_tinfo
11522 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11527 /* Mark if () ; with a special NOP_EXPR. */
11528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11530 cp_lexer_consume_token (parser
->lexer
);
11531 statement
= add_stmt (build_empty_stmt (body_loc
));
11533 if (guard_tinfo
.keyword
== RID_IF
11534 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
11535 warning_at (body_loc
, OPT_Wempty_body
,
11536 "suggest braces around empty body in an %<if%> statement");
11537 else if (guard_tinfo
.keyword
== RID_ELSE
)
11538 warning_at (body_loc
, OPT_Wempty_body
,
11539 "suggest braces around empty body in an %<else%> statement");
11541 /* if a compound is opened, we simply parse the statement directly. */
11542 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11543 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11544 /* If the token is not a `{', then we must take special action. */
11547 /* Create a compound-statement. */
11548 statement
= begin_compound_stmt (0);
11549 /* Parse the dependent-statement. */
11550 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
11551 /* Finish the dummy compound-statement. */
11552 finish_compound_stmt (statement
);
11555 token_indent_info next_tinfo
11556 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11557 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11559 /* Return the statement. */
11563 /* For some dependent statements (like `while (cond) statement'), we
11564 have already created a scope. Therefore, even if the dependent
11565 statement is a compound-statement, we do not want to create another
11569 cp_parser_already_scoped_statement (cp_parser
* parser
,
11570 const token_indent_info
&guard_tinfo
)
11572 /* If the token is a `{', then we must take special action. */
11573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11575 token_indent_info body_tinfo
11576 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11578 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11579 token_indent_info next_tinfo
11580 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11581 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11585 /* Avoid calling cp_parser_compound_statement, so that we
11586 don't create a new scope. Do everything else by hand. */
11587 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11588 /* If the next keyword is `__label__' we have a label declaration. */
11589 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11590 cp_parser_label_declaration (parser
);
11591 /* Parse an (optional) statement-seq. */
11592 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11593 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11597 /* Declarations [gram.dcl.dcl] */
11599 /* Parse an optional declaration-sequence.
11603 declaration-seq declaration */
11606 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11612 token
= cp_lexer_peek_token (parser
->lexer
);
11614 if (token
->type
== CPP_CLOSE_BRACE
11615 || token
->type
== CPP_EOF
11616 || token
->type
== CPP_PRAGMA_EOL
)
11619 if (token
->type
== CPP_SEMICOLON
)
11621 /* A declaration consisting of a single semicolon is
11622 invalid. Allow it unless we're being pedantic. */
11623 cp_lexer_consume_token (parser
->lexer
);
11624 if (!in_system_header_at (input_location
))
11625 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11629 /* If we're entering or exiting a region that's implicitly
11630 extern "C", modify the lang context appropriately. */
11631 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11633 push_lang_context (lang_name_c
);
11634 parser
->implicit_extern_c
= true;
11636 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11638 pop_lang_context ();
11639 parser
->implicit_extern_c
= false;
11642 if (token
->type
== CPP_PRAGMA
)
11644 /* A top-level declaration can consist solely of a #pragma.
11645 A nested declaration cannot, so this is done here and not
11646 in cp_parser_declaration. (A #pragma at block scope is
11647 handled in cp_parser_statement.) */
11648 cp_parser_pragma (parser
, pragma_external
);
11652 /* Parse the declaration itself. */
11653 cp_parser_declaration (parser
);
11657 /* Parse a declaration.
11661 function-definition
11662 template-declaration
11663 explicit-instantiation
11664 explicit-specialization
11665 linkage-specification
11666 namespace-definition
11671 __extension__ declaration */
11674 cp_parser_declaration (cp_parser
* parser
)
11678 int saved_pedantic
;
11680 tree attributes
= NULL_TREE
;
11682 /* Check for the `__extension__' keyword. */
11683 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11685 /* Parse the qualified declaration. */
11686 cp_parser_declaration (parser
);
11687 /* Restore the PEDANTIC flag. */
11688 pedantic
= saved_pedantic
;
11693 /* Try to figure out what kind of declaration is present. */
11694 token1
= *cp_lexer_peek_token (parser
->lexer
);
11696 if (token1
.type
!= CPP_EOF
)
11697 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11700 token2
.type
= CPP_EOF
;
11701 token2
.keyword
= RID_MAX
;
11704 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11705 p
= obstack_alloc (&declarator_obstack
, 0);
11707 /* If the next token is `extern' and the following token is a string
11708 literal, then we have a linkage specification. */
11709 if (token1
.keyword
== RID_EXTERN
11710 && cp_parser_is_pure_string_literal (&token2
))
11711 cp_parser_linkage_specification (parser
);
11712 /* If the next token is `template', then we have either a template
11713 declaration, an explicit instantiation, or an explicit
11715 else if (token1
.keyword
== RID_TEMPLATE
)
11717 /* `template <>' indicates a template specialization. */
11718 if (token2
.type
== CPP_LESS
11719 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11720 cp_parser_explicit_specialization (parser
);
11721 /* `template <' indicates a template declaration. */
11722 else if (token2
.type
== CPP_LESS
)
11723 cp_parser_template_declaration (parser
, /*member_p=*/false);
11724 /* Anything else must be an explicit instantiation. */
11726 cp_parser_explicit_instantiation (parser
);
11728 /* If the next token is `export', then we have a template
11730 else if (token1
.keyword
== RID_EXPORT
)
11731 cp_parser_template_declaration (parser
, /*member_p=*/false);
11732 /* If the next token is `extern', 'static' or 'inline' and the one
11733 after that is `template', we have a GNU extended explicit
11734 instantiation directive. */
11735 else if (cp_parser_allow_gnu_extensions_p (parser
)
11736 && (token1
.keyword
== RID_EXTERN
11737 || token1
.keyword
== RID_STATIC
11738 || token1
.keyword
== RID_INLINE
)
11739 && token2
.keyword
== RID_TEMPLATE
)
11740 cp_parser_explicit_instantiation (parser
);
11741 /* If the next token is `namespace', check for a named or unnamed
11742 namespace definition. */
11743 else if (token1
.keyword
== RID_NAMESPACE
11744 && (/* A named namespace definition. */
11745 (token2
.type
== CPP_NAME
11746 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11748 || (token2
.type
== CPP_OPEN_SQUARE
11749 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11750 == CPP_OPEN_SQUARE
)
11751 /* An unnamed namespace definition. */
11752 || token2
.type
== CPP_OPEN_BRACE
11753 || token2
.keyword
== RID_ATTRIBUTE
))
11754 cp_parser_namespace_definition (parser
);
11755 /* An inline (associated) namespace definition. */
11756 else if (token1
.keyword
== RID_INLINE
11757 && token2
.keyword
== RID_NAMESPACE
)
11758 cp_parser_namespace_definition (parser
);
11759 /* Objective-C++ declaration/definition. */
11760 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11761 cp_parser_objc_declaration (parser
, NULL_TREE
);
11762 else if (c_dialect_objc ()
11763 && token1
.keyword
== RID_ATTRIBUTE
11764 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11765 cp_parser_objc_declaration (parser
, attributes
);
11766 /* At this point we may have a template declared by a concept
11768 else if (flag_concepts
11769 && cp_parser_template_declaration_after_export (parser
,
11770 /*member_p=*/false))
11773 /* Try to parse a block-declaration, or a function-definition. */
11774 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11776 /* Free any declarators allocated. */
11777 obstack_free (&declarator_obstack
, p
);
11780 /* Parse a block-declaration.
11785 namespace-alias-definition
11792 __extension__ block-declaration
11797 static_assert-declaration
11799 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11800 part of a declaration-statement. */
11803 cp_parser_block_declaration (cp_parser
*parser
,
11807 int saved_pedantic
;
11809 /* Check for the `__extension__' keyword. */
11810 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11812 /* Parse the qualified declaration. */
11813 cp_parser_block_declaration (parser
, statement_p
);
11814 /* Restore the PEDANTIC flag. */
11815 pedantic
= saved_pedantic
;
11820 /* Peek at the next token to figure out which kind of declaration is
11822 token1
= cp_lexer_peek_token (parser
->lexer
);
11824 /* If the next keyword is `asm', we have an asm-definition. */
11825 if (token1
->keyword
== RID_ASM
)
11828 cp_parser_commit_to_tentative_parse (parser
);
11829 cp_parser_asm_definition (parser
);
11831 /* If the next keyword is `namespace', we have a
11832 namespace-alias-definition. */
11833 else if (token1
->keyword
== RID_NAMESPACE
)
11834 cp_parser_namespace_alias_definition (parser
);
11835 /* If the next keyword is `using', we have a
11836 using-declaration, a using-directive, or an alias-declaration. */
11837 else if (token1
->keyword
== RID_USING
)
11842 cp_parser_commit_to_tentative_parse (parser
);
11843 /* If the token after `using' is `namespace', then we have a
11844 using-directive. */
11845 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11846 if (token2
->keyword
== RID_NAMESPACE
)
11847 cp_parser_using_directive (parser
);
11848 /* If the second token after 'using' is '=', then we have an
11849 alias-declaration. */
11850 else if (cxx_dialect
>= cxx11
11851 && token2
->type
== CPP_NAME
11852 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11853 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11854 cp_parser_alias_declaration (parser
);
11855 /* Otherwise, it's a using-declaration. */
11857 cp_parser_using_declaration (parser
,
11858 /*access_declaration_p=*/false);
11860 /* If the next keyword is `__label__' we have a misplaced label
11862 else if (token1
->keyword
== RID_LABEL
)
11864 cp_lexer_consume_token (parser
->lexer
);
11865 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11866 cp_parser_skip_to_end_of_statement (parser
);
11867 /* If the next token is now a `;', consume it. */
11868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11869 cp_lexer_consume_token (parser
->lexer
);
11871 /* If the next token is `static_assert' we have a static assertion. */
11872 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11873 cp_parser_static_assert (parser
, /*member_p=*/false);
11874 /* Anything else must be a simple-declaration. */
11876 cp_parser_simple_declaration (parser
, !statement_p
,
11877 /*maybe_range_for_decl*/NULL
);
11880 /* Parse a simple-declaration.
11882 simple-declaration:
11883 decl-specifier-seq [opt] init-declarator-list [opt] ;
11885 init-declarator-list:
11887 init-declarator-list , init-declarator
11889 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11890 function-definition as a simple-declaration.
11892 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11893 parsed declaration if it is an uninitialized single declarator not followed
11894 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11895 if present, will not be consumed. */
11898 cp_parser_simple_declaration (cp_parser
* parser
,
11899 bool function_definition_allowed_p
,
11900 tree
*maybe_range_for_decl
)
11902 cp_decl_specifier_seq decl_specifiers
;
11903 int declares_class_or_enum
;
11904 bool saw_declarator
;
11905 location_t comma_loc
= UNKNOWN_LOCATION
;
11906 location_t init_loc
= UNKNOWN_LOCATION
;
11909 if (maybe_range_for_decl
)
11910 *maybe_range_for_decl
= NULL_TREE
;
11912 /* Defer access checks until we know what is being declared; the
11913 checks for names appearing in the decl-specifier-seq should be
11914 done as if we were in the scope of the thing being declared. */
11915 push_deferring_access_checks (dk_deferred
);
11917 /* Parse the decl-specifier-seq. We have to keep track of whether
11918 or not the decl-specifier-seq declares a named class or
11919 enumeration type, since that is the only case in which the
11920 init-declarator-list is allowed to be empty.
11924 In a simple-declaration, the optional init-declarator-list can be
11925 omitted only when declaring a class or enumeration, that is when
11926 the decl-specifier-seq contains either a class-specifier, an
11927 elaborated-type-specifier, or an enum-specifier. */
11928 cp_parser_decl_specifier_seq (parser
,
11929 CP_PARSER_FLAGS_OPTIONAL
,
11931 &declares_class_or_enum
);
11932 /* We no longer need to defer access checks. */
11933 stop_deferring_access_checks ();
11935 /* In a block scope, a valid declaration must always have a
11936 decl-specifier-seq. By not trying to parse declarators, we can
11937 resolve the declaration/expression ambiguity more quickly. */
11938 if (!function_definition_allowed_p
11939 && !decl_specifiers
.any_specifiers_p
)
11941 cp_parser_error (parser
, "expected declaration");
11945 /* If the next two tokens are both identifiers, the code is
11946 erroneous. The usual cause of this situation is code like:
11950 where "T" should name a type -- but does not. */
11951 if (!decl_specifiers
.any_type_specifiers_p
11952 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11954 /* If parsing tentatively, we should commit; we really are
11955 looking at a declaration. */
11956 cp_parser_commit_to_tentative_parse (parser
);
11961 /* If we have seen at least one decl-specifier, and the next token
11962 is not a parenthesis, then we must be looking at a declaration.
11963 (After "int (" we might be looking at a functional cast.) */
11964 if (decl_specifiers
.any_specifiers_p
11965 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11966 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11967 && !cp_parser_error_occurred (parser
))
11968 cp_parser_commit_to_tentative_parse (parser
);
11970 /* Keep going until we hit the `;' at the end of the simple
11972 saw_declarator
= false;
11973 while (cp_lexer_next_token_is_not (parser
->lexer
,
11977 bool function_definition_p
;
11980 if (saw_declarator
)
11982 /* If we are processing next declarator, comma is expected */
11983 token
= cp_lexer_peek_token (parser
->lexer
);
11984 gcc_assert (token
->type
== CPP_COMMA
);
11985 cp_lexer_consume_token (parser
->lexer
);
11986 if (maybe_range_for_decl
)
11988 *maybe_range_for_decl
= error_mark_node
;
11989 if (comma_loc
== UNKNOWN_LOCATION
)
11990 comma_loc
= token
->location
;
11994 saw_declarator
= true;
11996 /* Parse the init-declarator. */
11997 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11999 function_definition_allowed_p
,
12000 /*member_p=*/false,
12001 declares_class_or_enum
,
12002 &function_definition_p
,
12003 maybe_range_for_decl
,
12008 /* If an error occurred while parsing tentatively, exit quickly.
12009 (That usually happens when in the body of a function; each
12010 statement is treated as a declaration-statement until proven
12012 if (cp_parser_error_occurred (parser
))
12014 /* Handle function definitions specially. */
12015 if (function_definition_p
)
12017 /* If the next token is a `,', then we are probably
12018 processing something like:
12022 which is erroneous. */
12023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12025 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12026 error_at (token
->location
,
12028 " declarations and function-definitions is forbidden");
12030 /* Otherwise, we're done with the list of declarators. */
12033 pop_deferring_access_checks ();
12037 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12038 *maybe_range_for_decl
= decl
;
12039 /* The next token should be either a `,' or a `;'. */
12040 token
= cp_lexer_peek_token (parser
->lexer
);
12041 /* If it's a `,', there are more declarators to come. */
12042 if (token
->type
== CPP_COMMA
)
12043 /* will be consumed next time around */;
12044 /* If it's a `;', we are done. */
12045 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12047 /* Anything else is an error. */
12050 /* If we have already issued an error message we don't need
12051 to issue another one. */
12052 if ((decl
!= error_mark_node
12053 && DECL_INITIAL (decl
) != error_mark_node
)
12054 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12055 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12056 /* Skip tokens until we reach the end of the statement. */
12057 cp_parser_skip_to_end_of_statement (parser
);
12058 /* If the next token is now a `;', consume it. */
12059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12060 cp_lexer_consume_token (parser
->lexer
);
12063 /* After the first time around, a function-definition is not
12064 allowed -- even if it was OK at first. For example:
12069 function_definition_allowed_p
= false;
12072 /* Issue an error message if no declarators are present, and the
12073 decl-specifier-seq does not itself declare a class or
12074 enumeration: [dcl.dcl]/3. */
12075 if (!saw_declarator
)
12077 if (cp_parser_declares_only_class_p (parser
))
12079 if (!declares_class_or_enum
12080 && decl_specifiers
.type
12081 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12082 /* Ensure an error is issued anyway when finish_decltype_type,
12083 called via cp_parser_decl_specifier_seq, returns a class or
12084 an enumeration (c++/51786). */
12085 decl_specifiers
.type
= NULL_TREE
;
12086 shadow_tag (&decl_specifiers
);
12088 /* Perform any deferred access checks. */
12089 perform_deferred_access_checks (tf_warning_or_error
);
12092 /* Consume the `;'. */
12093 if (!maybe_range_for_decl
)
12094 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12095 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12097 if (init_loc
!= UNKNOWN_LOCATION
)
12098 error_at (init_loc
, "initializer in range-based %<for%> loop");
12099 if (comma_loc
!= UNKNOWN_LOCATION
)
12100 error_at (comma_loc
,
12101 "multiple declarations in range-based %<for%> loop");
12105 pop_deferring_access_checks ();
12107 /* Reset any acc routine clauses. */
12108 parser
->oacc_routine
= NULL
;
12111 /* Parse a decl-specifier-seq.
12113 decl-specifier-seq:
12114 decl-specifier-seq [opt] decl-specifier
12115 decl-specifier attribute-specifier-seq [opt] (C++11)
12118 storage-class-specifier
12129 Concepts Extension:
12134 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12136 The parser flags FLAGS is used to control type-specifier parsing.
12138 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12141 1: one of the decl-specifiers is an elaborated-type-specifier
12142 (i.e., a type declaration)
12143 2: one of the decl-specifiers is an enum-specifier or a
12144 class-specifier (i.e., a type definition)
12149 cp_parser_decl_specifier_seq (cp_parser
* parser
,
12150 cp_parser_flags flags
,
12151 cp_decl_specifier_seq
*decl_specs
,
12152 int* declares_class_or_enum
)
12154 bool constructor_possible_p
= !parser
->in_declarator_p
;
12155 bool found_decl_spec
= false;
12156 cp_token
*start_token
= NULL
;
12159 /* Clear DECL_SPECS. */
12160 clear_decl_specs (decl_specs
);
12162 /* Assume no class or enumeration type is declared. */
12163 *declares_class_or_enum
= 0;
12165 /* Keep reading specifiers until there are no more to read. */
12168 bool constructor_p
;
12172 /* Peek at the next token. */
12173 token
= cp_lexer_peek_token (parser
->lexer
);
12175 /* Save the first token of the decl spec list for error
12178 start_token
= token
;
12179 /* Handle attributes. */
12180 if (cp_next_tokens_can_be_attribute_p (parser
))
12182 /* Parse the attributes. */
12183 tree attrs
= cp_parser_attributes_opt (parser
);
12185 /* In a sequence of declaration specifiers, c++11 attributes
12186 appertain to the type that precede them. In that case
12189 The attribute-specifier-seq affects the type only for
12190 the declaration it appears in, not other declarations
12191 involving the same type.
12193 But for now let's force the user to position the
12194 attribute either at the beginning of the declaration or
12195 after the declarator-id, which would clearly mean that it
12196 applies to the declarator. */
12197 if (cxx11_attribute_p (attrs
))
12199 if (!found_decl_spec
)
12200 /* The c++11 attribute is at the beginning of the
12201 declaration. It appertains to the entity being
12205 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
12207 /* This is an attribute following a
12208 class-specifier. */
12209 if (decl_specs
->type_definition_p
)
12210 warn_misplaced_attr_for_class_type (token
->location
,
12216 decl_specs
->std_attributes
12217 = chainon (decl_specs
->std_attributes
,
12219 if (decl_specs
->locations
[ds_std_attribute
] == 0)
12220 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
12226 decl_specs
->attributes
12227 = chainon (decl_specs
->attributes
,
12229 if (decl_specs
->locations
[ds_attribute
] == 0)
12230 decl_specs
->locations
[ds_attribute
] = token
->location
;
12233 /* Assume we will find a decl-specifier keyword. */
12234 found_decl_spec
= true;
12235 /* If the next token is an appropriate keyword, we can simply
12236 add it to the list. */
12237 switch (token
->keyword
)
12243 if (!at_class_scope_p ())
12245 error_at (token
->location
, "%<friend%> used outside of class");
12246 cp_lexer_purge_token (parser
->lexer
);
12251 /* Consume the token. */
12252 cp_lexer_consume_token (parser
->lexer
);
12256 case RID_CONSTEXPR
:
12258 cp_lexer_consume_token (parser
->lexer
);
12263 cp_lexer_consume_token (parser
->lexer
);
12266 /* function-specifier:
12273 cp_parser_function_specifier_opt (parser
, decl_specs
);
12280 /* Consume the token. */
12281 cp_lexer_consume_token (parser
->lexer
);
12282 /* A constructor declarator cannot appear in a typedef. */
12283 constructor_possible_p
= false;
12284 /* The "typedef" keyword can only occur in a declaration; we
12285 may as well commit at this point. */
12286 cp_parser_commit_to_tentative_parse (parser
);
12288 if (decl_specs
->storage_class
!= sc_none
)
12289 decl_specs
->conflicting_specifiers_p
= true;
12292 /* storage-class-specifier:
12302 if (cxx_dialect
== cxx98
)
12304 /* Consume the token. */
12305 cp_lexer_consume_token (parser
->lexer
);
12307 /* Complain about `auto' as a storage specifier, if
12308 we're complaining about C++0x compatibility. */
12309 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
12310 " changes meaning in C++11; please remove it");
12312 /* Set the storage class anyway. */
12313 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
12317 /* C++0x auto type-specifier. */
12318 found_decl_spec
= false;
12325 /* Consume the token. */
12326 cp_lexer_consume_token (parser
->lexer
);
12327 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
12331 /* Consume the token. */
12333 cp_lexer_consume_token (parser
->lexer
);
12337 /* We did not yet find a decl-specifier yet. */
12338 found_decl_spec
= false;
12342 if (found_decl_spec
12343 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
12344 && token
->keyword
!= RID_CONSTEXPR
)
12345 error ("decl-specifier invalid in condition");
12348 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
12350 /* Constructors are a special case. The `S' in `S()' is not a
12351 decl-specifier; it is the beginning of the declarator. */
12353 = (!found_decl_spec
12354 && constructor_possible_p
12355 && (cp_parser_constructor_declarator_p
12356 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
12358 /* If we don't have a DECL_SPEC yet, then we must be looking at
12359 a type-specifier. */
12360 if (!found_decl_spec
&& !constructor_p
)
12362 int decl_spec_declares_class_or_enum
;
12363 bool is_cv_qualifier
;
12367 = cp_parser_type_specifier (parser
, flags
,
12369 /*is_declaration=*/true,
12370 &decl_spec_declares_class_or_enum
,
12372 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
12374 /* If this type-specifier referenced a user-defined type
12375 (a typedef, class-name, etc.), then we can't allow any
12376 more such type-specifiers henceforth.
12380 The longest sequence of decl-specifiers that could
12381 possibly be a type name is taken as the
12382 decl-specifier-seq of a declaration. The sequence shall
12383 be self-consistent as described below.
12387 As a general rule, at most one type-specifier is allowed
12388 in the complete decl-specifier-seq of a declaration. The
12389 only exceptions are the following:
12391 -- const or volatile can be combined with any other
12394 -- signed or unsigned can be combined with char, long,
12402 void g (const int Pc);
12404 Here, Pc is *not* part of the decl-specifier seq; it's
12405 the declarator. Therefore, once we see a type-specifier
12406 (other than a cv-qualifier), we forbid any additional
12407 user-defined types. We *do* still allow things like `int
12408 int' to be considered a decl-specifier-seq, and issue the
12409 error message later. */
12410 if (type_spec
&& !is_cv_qualifier
)
12411 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12412 /* A constructor declarator cannot follow a type-specifier. */
12415 constructor_possible_p
= false;
12416 found_decl_spec
= true;
12417 if (!is_cv_qualifier
)
12418 decl_specs
->any_type_specifiers_p
= true;
12422 /* If we still do not have a DECL_SPEC, then there are no more
12423 decl-specifiers. */
12424 if (!found_decl_spec
)
12427 decl_specs
->any_specifiers_p
= true;
12428 /* After we see one decl-specifier, further decl-specifiers are
12429 always optional. */
12430 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12433 /* Don't allow a friend specifier with a class definition. */
12434 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12435 && (*declares_class_or_enum
& 2))
12436 error_at (decl_specs
->locations
[ds_friend
],
12437 "class definition may not be declared a friend");
12440 /* Parse an (optional) storage-class-specifier.
12442 storage-class-specifier:
12451 storage-class-specifier:
12454 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12457 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12459 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12462 if (cxx_dialect
!= cxx98
)
12464 /* Fall through for C++98. */
12471 /* Consume the token. */
12472 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12479 /* Parse an (optional) function-specifier.
12481 function-specifier:
12486 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12487 Updates DECL_SPECS, if it is non-NULL. */
12490 cp_parser_function_specifier_opt (cp_parser
* parser
,
12491 cp_decl_specifier_seq
*decl_specs
)
12493 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12494 switch (token
->keyword
)
12497 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12501 /* 14.5.2.3 [temp.mem]
12503 A member function template shall not be virtual. */
12504 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12505 error_at (token
->location
, "templates may not be %<virtual%>");
12507 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12511 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12518 /* Consume the token. */
12519 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12522 /* Parse a linkage-specification.
12524 linkage-specification:
12525 extern string-literal { declaration-seq [opt] }
12526 extern string-literal declaration */
12529 cp_parser_linkage_specification (cp_parser
* parser
)
12533 /* Look for the `extern' keyword. */
12534 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12536 /* Look for the string-literal. */
12537 linkage
= cp_parser_string_literal (parser
, false, false);
12539 /* Transform the literal into an identifier. If the literal is a
12540 wide-character string, or contains embedded NULs, then we can't
12541 handle it as the user wants. */
12542 if (strlen (TREE_STRING_POINTER (linkage
))
12543 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12545 cp_parser_error (parser
, "invalid linkage-specification");
12546 /* Assume C++ linkage. */
12547 linkage
= lang_name_cplusplus
;
12550 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12552 /* We're now using the new linkage. */
12553 push_lang_context (linkage
);
12555 /* If the next token is a `{', then we're using the first
12557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12559 cp_ensure_no_omp_declare_simd (parser
);
12560 cp_ensure_no_oacc_routine (parser
);
12562 /* Consume the `{' token. */
12563 cp_lexer_consume_token (parser
->lexer
);
12564 /* Parse the declarations. */
12565 cp_parser_declaration_seq_opt (parser
);
12566 /* Look for the closing `}'. */
12567 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12569 /* Otherwise, there's just one declaration. */
12572 bool saved_in_unbraced_linkage_specification_p
;
12574 saved_in_unbraced_linkage_specification_p
12575 = parser
->in_unbraced_linkage_specification_p
;
12576 parser
->in_unbraced_linkage_specification_p
= true;
12577 cp_parser_declaration (parser
);
12578 parser
->in_unbraced_linkage_specification_p
12579 = saved_in_unbraced_linkage_specification_p
;
12582 /* We're done with the linkage-specification. */
12583 pop_lang_context ();
12586 /* Parse a static_assert-declaration.
12588 static_assert-declaration:
12589 static_assert ( constant-expression , string-literal ) ;
12590 static_assert ( constant-expression ) ; (C++1Z)
12592 If MEMBER_P, this static_assert is a class member. */
12595 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12600 location_t saved_loc
;
12603 /* Peek at the `static_assert' token so we can keep track of exactly
12604 where the static assertion started. */
12605 token
= cp_lexer_peek_token (parser
->lexer
);
12606 saved_loc
= token
->location
;
12608 /* Look for the `static_assert' keyword. */
12609 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12613 /* We know we are in a static assertion; commit to any tentative
12615 if (cp_parser_parsing_tentatively (parser
))
12616 cp_parser_commit_to_tentative_parse (parser
);
12618 /* Parse the `(' starting the static assertion condition. */
12619 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12621 /* Parse the constant-expression. Allow a non-constant expression
12622 here in order to give better diagnostics in finish_static_assert. */
12624 cp_parser_constant_expression (parser
,
12625 /*allow_non_constant_p=*/true,
12626 /*non_constant_p=*/&dummy
);
12628 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12630 if (cxx_dialect
< cxx1z
)
12631 pedwarn (input_location
, OPT_Wpedantic
,
12632 "static_assert without a message "
12633 "only available with -std=c++1z or -std=gnu++1z");
12635 cp_lexer_consume_token (parser
->lexer
);
12636 message
= build_string (1, "");
12637 TREE_TYPE (message
) = char_array_type_node
;
12638 fix_string_type (message
);
12642 /* Parse the separating `,'. */
12643 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12645 /* Parse the string-literal message. */
12646 message
= cp_parser_string_literal (parser
,
12647 /*translate=*/false,
12650 /* A `)' completes the static assertion. */
12651 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12652 cp_parser_skip_to_closing_parenthesis (parser
,
12653 /*recovering=*/true,
12654 /*or_comma=*/false,
12655 /*consume_paren=*/true);
12658 /* A semicolon terminates the declaration. */
12659 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12661 /* Complete the static assertion, which may mean either processing
12662 the static assert now or saving it for template instantiation. */
12663 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12666 /* Parse the expression in decltype ( expression ). */
12669 cp_parser_decltype_expr (cp_parser
*parser
,
12670 bool &id_expression_or_member_access_p
)
12672 cp_token
*id_expr_start_token
;
12675 /* First, try parsing an id-expression. */
12676 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12677 cp_parser_parse_tentatively (parser
);
12678 expr
= cp_parser_id_expression (parser
,
12679 /*template_keyword_p=*/false,
12680 /*check_dependency_p=*/true,
12681 /*template_p=*/NULL
,
12682 /*declarator_p=*/false,
12683 /*optional_p=*/false);
12685 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12687 bool non_integral_constant_expression_p
= false;
12688 tree id_expression
= expr
;
12690 const char *error_msg
;
12692 if (identifier_p (expr
))
12693 /* Lookup the name we got back from the id-expression. */
12694 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12695 id_expr_start_token
->location
);
12698 && expr
!= error_mark_node
12699 && TREE_CODE (expr
) != TYPE_DECL
12700 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12701 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12702 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12704 /* Complete lookup of the id-expression. */
12705 expr
= (finish_id_expression
12706 (id_expression
, expr
, parser
->scope
, &idk
,
12707 /*integral_constant_expression_p=*/false,
12708 /*allow_non_integral_constant_expression_p=*/true,
12709 &non_integral_constant_expression_p
,
12710 /*template_p=*/false,
12712 /*address_p=*/false,
12713 /*template_arg_p=*/false,
12715 id_expr_start_token
->location
));
12717 if (expr
== error_mark_node
)
12718 /* We found an id-expression, but it was something that we
12719 should not have found. This is an error, not something
12720 we can recover from, so note that we found an
12721 id-expression and we'll recover as gracefully as
12723 id_expression_or_member_access_p
= true;
12727 && expr
!= error_mark_node
12728 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12729 /* We have an id-expression. */
12730 id_expression_or_member_access_p
= true;
12733 if (!id_expression_or_member_access_p
)
12735 /* Abort the id-expression parse. */
12736 cp_parser_abort_tentative_parse (parser
);
12738 /* Parsing tentatively, again. */
12739 cp_parser_parse_tentatively (parser
);
12741 /* Parse a class member access. */
12742 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12743 /*cast_p=*/false, /*decltype*/true,
12744 /*member_access_only_p=*/true, NULL
);
12747 && expr
!= error_mark_node
12748 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12749 /* We have an id-expression. */
12750 id_expression_or_member_access_p
= true;
12753 if (id_expression_or_member_access_p
)
12754 /* We have parsed the complete id-expression or member access. */
12755 cp_parser_parse_definitely (parser
);
12758 /* Abort our attempt to parse an id-expression or member access
12760 cp_parser_abort_tentative_parse (parser
);
12762 /* Parse a full expression. */
12763 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12764 /*decltype_p=*/true);
12770 /* Parse a `decltype' type. Returns the type.
12772 simple-type-specifier:
12773 decltype ( expression )
12775 decltype ( auto ) */
12778 cp_parser_decltype (cp_parser
*parser
)
12781 bool id_expression_or_member_access_p
= false;
12782 const char *saved_message
;
12783 bool saved_integral_constant_expression_p
;
12784 bool saved_non_integral_constant_expression_p
;
12785 bool saved_greater_than_is_operator_p
;
12786 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12788 if (start_token
->type
== CPP_DECLTYPE
)
12790 /* Already parsed. */
12791 cp_lexer_consume_token (parser
->lexer
);
12792 return start_token
->u
.value
;
12795 /* Look for the `decltype' token. */
12796 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12797 return error_mark_node
;
12799 /* Parse the opening `('. */
12800 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12801 return error_mark_node
;
12803 /* decltype (auto) */
12804 if (cxx_dialect
>= cxx14
12805 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12807 cp_lexer_consume_token (parser
->lexer
);
12808 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12809 return error_mark_node
;
12810 expr
= make_decltype_auto ();
12811 AUTO_IS_DECLTYPE (expr
) = true;
12815 /* Types cannot be defined in a `decltype' expression. Save away the
12817 saved_message
= parser
->type_definition_forbidden_message
;
12819 /* And create the new one. */
12820 parser
->type_definition_forbidden_message
12821 = G_("types may not be defined in %<decltype%> expressions");
12823 /* The restrictions on constant-expressions do not apply inside
12824 decltype expressions. */
12825 saved_integral_constant_expression_p
12826 = parser
->integral_constant_expression_p
;
12827 saved_non_integral_constant_expression_p
12828 = parser
->non_integral_constant_expression_p
;
12829 parser
->integral_constant_expression_p
= false;
12831 /* Within a parenthesized expression, a `>' token is always
12832 the greater-than operator. */
12833 saved_greater_than_is_operator_p
12834 = parser
->greater_than_is_operator_p
;
12835 parser
->greater_than_is_operator_p
= true;
12837 /* Do not actually evaluate the expression. */
12838 ++cp_unevaluated_operand
;
12840 /* Do not warn about problems with the expression. */
12841 ++c_inhibit_evaluation_warnings
;
12843 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12845 /* Go back to evaluating expressions. */
12846 --cp_unevaluated_operand
;
12847 --c_inhibit_evaluation_warnings
;
12849 /* The `>' token might be the end of a template-id or
12850 template-parameter-list now. */
12851 parser
->greater_than_is_operator_p
12852 = saved_greater_than_is_operator_p
;
12854 /* Restore the old message and the integral constant expression
12856 parser
->type_definition_forbidden_message
= saved_message
;
12857 parser
->integral_constant_expression_p
12858 = saved_integral_constant_expression_p
;
12859 parser
->non_integral_constant_expression_p
12860 = saved_non_integral_constant_expression_p
;
12862 /* Parse to the closing `)'. */
12863 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12865 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12866 /*consume_paren=*/true);
12867 return error_mark_node
;
12870 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12871 tf_warning_or_error
);
12874 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12876 start_token
->type
= CPP_DECLTYPE
;
12877 start_token
->u
.value
= expr
;
12878 start_token
->keyword
= RID_MAX
;
12879 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12884 /* Special member functions [gram.special] */
12886 /* Parse a conversion-function-id.
12888 conversion-function-id:
12889 operator conversion-type-id
12891 Returns an IDENTIFIER_NODE representing the operator. */
12894 cp_parser_conversion_function_id (cp_parser
* parser
)
12898 tree saved_qualifying_scope
;
12899 tree saved_object_scope
;
12900 tree pushed_scope
= NULL_TREE
;
12902 /* Look for the `operator' token. */
12903 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12904 return error_mark_node
;
12905 /* When we parse the conversion-type-id, the current scope will be
12906 reset. However, we need that information in able to look up the
12907 conversion function later, so we save it here. */
12908 saved_scope
= parser
->scope
;
12909 saved_qualifying_scope
= parser
->qualifying_scope
;
12910 saved_object_scope
= parser
->object_scope
;
12911 /* We must enter the scope of the class so that the names of
12912 entities declared within the class are available in the
12913 conversion-type-id. For example, consider:
12920 S::operator I() { ... }
12922 In order to see that `I' is a type-name in the definition, we
12923 must be in the scope of `S'. */
12925 pushed_scope
= push_scope (saved_scope
);
12926 /* Parse the conversion-type-id. */
12927 type
= cp_parser_conversion_type_id (parser
);
12928 /* Leave the scope of the class, if any. */
12930 pop_scope (pushed_scope
);
12931 /* Restore the saved scope. */
12932 parser
->scope
= saved_scope
;
12933 parser
->qualifying_scope
= saved_qualifying_scope
;
12934 parser
->object_scope
= saved_object_scope
;
12935 /* If the TYPE is invalid, indicate failure. */
12936 if (type
== error_mark_node
)
12937 return error_mark_node
;
12938 return mangle_conv_op_name_for_type (type
);
12941 /* Parse a conversion-type-id:
12943 conversion-type-id:
12944 type-specifier-seq conversion-declarator [opt]
12946 Returns the TYPE specified. */
12949 cp_parser_conversion_type_id (cp_parser
* parser
)
12952 cp_decl_specifier_seq type_specifiers
;
12953 cp_declarator
*declarator
;
12954 tree type_specified
;
12955 const char *saved_message
;
12957 /* Parse the attributes. */
12958 attributes
= cp_parser_attributes_opt (parser
);
12960 saved_message
= parser
->type_definition_forbidden_message
;
12961 parser
->type_definition_forbidden_message
12962 = G_("types may not be defined in a conversion-type-id");
12964 /* Parse the type-specifiers. */
12965 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12966 /*is_trailing_return=*/false,
12969 parser
->type_definition_forbidden_message
= saved_message
;
12971 /* If that didn't work, stop. */
12972 if (type_specifiers
.type
== error_mark_node
)
12973 return error_mark_node
;
12974 /* Parse the conversion-declarator. */
12975 declarator
= cp_parser_conversion_declarator_opt (parser
);
12977 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12978 /*initialized=*/0, &attributes
);
12980 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12982 /* Don't give this error when parsing tentatively. This happens to
12983 work because we always parse this definitively once. */
12984 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12985 && type_uses_auto (type_specified
))
12987 if (cxx_dialect
< cxx14
)
12989 error ("invalid use of %<auto%> in conversion operator");
12990 return error_mark_node
;
12992 else if (template_parm_scope_p ())
12993 warning (0, "use of %<auto%> in member template "
12994 "conversion operator can never be deduced");
12997 return type_specified
;
13000 /* Parse an (optional) conversion-declarator.
13002 conversion-declarator:
13003 ptr-operator conversion-declarator [opt]
13007 static cp_declarator
*
13008 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
13010 enum tree_code code
;
13011 tree class_type
, std_attributes
= NULL_TREE
;
13012 cp_cv_quals cv_quals
;
13014 /* We don't know if there's a ptr-operator next, or not. */
13015 cp_parser_parse_tentatively (parser
);
13016 /* Try the ptr-operator. */
13017 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
13019 /* If it worked, look for more conversion-declarators. */
13020 if (cp_parser_parse_definitely (parser
))
13022 cp_declarator
*declarator
;
13024 /* Parse another optional declarator. */
13025 declarator
= cp_parser_conversion_declarator_opt (parser
);
13027 declarator
= cp_parser_make_indirect_declarator
13028 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
13036 /* Parse an (optional) ctor-initializer.
13039 : mem-initializer-list
13041 Returns TRUE iff the ctor-initializer was actually present. */
13044 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
13046 /* If the next token is not a `:', then there is no
13047 ctor-initializer. */
13048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13050 /* Do default initialization of any bases and members. */
13051 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13052 finish_mem_initializers (NULL_TREE
);
13057 /* Consume the `:' token. */
13058 cp_lexer_consume_token (parser
->lexer
);
13059 /* And the mem-initializer-list. */
13060 cp_parser_mem_initializer_list (parser
);
13065 /* Parse a mem-initializer-list.
13067 mem-initializer-list:
13068 mem-initializer ... [opt]
13069 mem-initializer ... [opt] , mem-initializer-list */
13072 cp_parser_mem_initializer_list (cp_parser
* parser
)
13074 tree mem_initializer_list
= NULL_TREE
;
13075 tree target_ctor
= error_mark_node
;
13076 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13078 /* Let the semantic analysis code know that we are starting the
13079 mem-initializer-list. */
13080 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
13081 error_at (token
->location
,
13082 "only constructors take member initializers");
13084 /* Loop through the list. */
13087 tree mem_initializer
;
13089 token
= cp_lexer_peek_token (parser
->lexer
);
13090 /* Parse the mem-initializer. */
13091 mem_initializer
= cp_parser_mem_initializer (parser
);
13092 /* If the next token is a `...', we're expanding member initializers. */
13093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13095 /* Consume the `...'. */
13096 cp_lexer_consume_token (parser
->lexer
);
13098 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13099 can be expanded but members cannot. */
13100 if (mem_initializer
!= error_mark_node
13101 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
13103 error_at (token
->location
,
13104 "cannot expand initializer for member %<%D%>",
13105 TREE_PURPOSE (mem_initializer
));
13106 mem_initializer
= error_mark_node
;
13109 /* Construct the pack expansion type. */
13110 if (mem_initializer
!= error_mark_node
)
13111 mem_initializer
= make_pack_expansion (mem_initializer
);
13113 if (target_ctor
!= error_mark_node
13114 && mem_initializer
!= error_mark_node
)
13116 error ("mem-initializer for %qD follows constructor delegation",
13117 TREE_PURPOSE (mem_initializer
));
13118 mem_initializer
= error_mark_node
;
13120 /* Look for a target constructor. */
13121 if (mem_initializer
!= error_mark_node
13122 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
13123 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
13125 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
13126 if (mem_initializer_list
)
13128 error ("constructor delegation follows mem-initializer for %qD",
13129 TREE_PURPOSE (mem_initializer_list
));
13130 mem_initializer
= error_mark_node
;
13132 target_ctor
= mem_initializer
;
13134 /* Add it to the list, unless it was erroneous. */
13135 if (mem_initializer
!= error_mark_node
)
13137 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
13138 mem_initializer_list
= mem_initializer
;
13140 /* If the next token is not a `,', we're done. */
13141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13143 /* Consume the `,' token. */
13144 cp_lexer_consume_token (parser
->lexer
);
13147 /* Perform semantic analysis. */
13148 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13149 finish_mem_initializers (mem_initializer_list
);
13152 /* Parse a mem-initializer.
13155 mem-initializer-id ( expression-list [opt] )
13156 mem-initializer-id braced-init-list
13161 ( expression-list [opt] )
13163 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13164 class) or FIELD_DECL (for a non-static data member) to initialize;
13165 the TREE_VALUE is the expression-list. An empty initialization
13166 list is represented by void_list_node. */
13169 cp_parser_mem_initializer (cp_parser
* parser
)
13171 tree mem_initializer_id
;
13172 tree expression_list
;
13174 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13176 /* Find out what is being initialized. */
13177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13179 permerror (token
->location
,
13180 "anachronistic old-style base class initializer");
13181 mem_initializer_id
= NULL_TREE
;
13185 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
13186 if (mem_initializer_id
== error_mark_node
)
13187 return mem_initializer_id
;
13189 member
= expand_member_init (mem_initializer_id
);
13190 if (member
&& !DECL_P (member
))
13191 in_base_initializer
= 1;
13193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13195 bool expr_non_constant_p
;
13196 cp_lexer_set_source_position (parser
->lexer
);
13197 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13198 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13199 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
13200 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
13204 vec
<tree
, va_gc
> *vec
;
13205 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
13207 /*allow_expansion_p=*/true,
13208 /*non_constant_p=*/NULL
);
13210 return error_mark_node
;
13211 expression_list
= build_tree_list_vec (vec
);
13212 release_tree_vector (vec
);
13215 if (expression_list
== error_mark_node
)
13216 return error_mark_node
;
13217 if (!expression_list
)
13218 expression_list
= void_type_node
;
13220 in_base_initializer
= 0;
13222 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
13225 /* Parse a mem-initializer-id.
13227 mem-initializer-id:
13228 :: [opt] nested-name-specifier [opt] class-name
13229 decltype-specifier (C++11)
13232 Returns a TYPE indicating the class to be initialized for the first
13233 production (and the second in C++11). Returns an IDENTIFIER_NODE
13234 indicating the data member to be initialized for the last production. */
13237 cp_parser_mem_initializer_id (cp_parser
* parser
)
13239 bool global_scope_p
;
13240 bool nested_name_specifier_p
;
13241 bool template_p
= false;
13244 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13246 /* `typename' is not allowed in this context ([temp.res]). */
13247 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13249 error_at (token
->location
,
13250 "keyword %<typename%> not allowed in this context (a qualified "
13251 "member initializer is implicitly a type)");
13252 cp_lexer_consume_token (parser
->lexer
);
13254 /* Look for the optional `::' operator. */
13256 = (cp_parser_global_scope_opt (parser
,
13257 /*current_scope_valid_p=*/false)
13259 /* Look for the optional nested-name-specifier. The simplest way to
13264 The keyword `typename' is not permitted in a base-specifier or
13265 mem-initializer; in these contexts a qualified name that
13266 depends on a template-parameter is implicitly assumed to be a
13269 is to assume that we have seen the `typename' keyword at this
13271 nested_name_specifier_p
13272 = (cp_parser_nested_name_specifier_opt (parser
,
13273 /*typename_keyword_p=*/true,
13274 /*check_dependency_p=*/true,
13276 /*is_declaration=*/true)
13278 if (nested_name_specifier_p
)
13279 template_p
= cp_parser_optional_template_keyword (parser
);
13280 /* If there is a `::' operator or a nested-name-specifier, then we
13281 are definitely looking for a class-name. */
13282 if (global_scope_p
|| nested_name_specifier_p
)
13283 return cp_parser_class_name (parser
,
13284 /*typename_keyword_p=*/true,
13285 /*template_keyword_p=*/template_p
,
13287 /*check_dependency_p=*/true,
13288 /*class_head_p=*/false,
13289 /*is_declaration=*/true);
13290 /* Otherwise, we could also be looking for an ordinary identifier. */
13291 cp_parser_parse_tentatively (parser
);
13292 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
13293 /* Try a decltype-specifier. */
13294 id
= cp_parser_decltype (parser
);
13296 /* Otherwise, try a class-name. */
13297 id
= cp_parser_class_name (parser
,
13298 /*typename_keyword_p=*/true,
13299 /*template_keyword_p=*/false,
13301 /*check_dependency_p=*/true,
13302 /*class_head_p=*/false,
13303 /*is_declaration=*/true);
13304 /* If we found one, we're done. */
13305 if (cp_parser_parse_definitely (parser
))
13307 /* Otherwise, look for an ordinary identifier. */
13308 return cp_parser_identifier (parser
);
13311 /* Overloading [gram.over] */
13313 /* Parse an operator-function-id.
13315 operator-function-id:
13318 Returns an IDENTIFIER_NODE for the operator which is a
13319 human-readable spelling of the identifier, e.g., `operator +'. */
13322 cp_parser_operator_function_id (cp_parser
* parser
)
13324 /* Look for the `operator' keyword. */
13325 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13326 return error_mark_node
;
13327 /* And then the name of the operator itself. */
13328 return cp_parser_operator (parser
);
13331 /* Return an identifier node for a user-defined literal operator.
13332 The suffix identifier is chained to the operator name identifier. */
13335 cp_literal_operator_id (const char* name
)
13338 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
13339 + strlen (name
) + 10);
13340 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
13341 identifier
= get_identifier (buffer
);
13346 /* Parse an operator.
13349 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13350 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13351 || ++ -- , ->* -> () []
13358 Returns an IDENTIFIER_NODE for the operator which is a
13359 human-readable spelling of the identifier, e.g., `operator +'. */
13362 cp_parser_operator (cp_parser
* parser
)
13364 tree id
= NULL_TREE
;
13368 /* Peek at the next token. */
13369 token
= cp_lexer_peek_token (parser
->lexer
);
13370 /* Figure out which operator we have. */
13371 switch (token
->type
)
13377 /* The keyword should be either `new' or `delete'. */
13378 if (token
->keyword
== RID_NEW
)
13380 else if (token
->keyword
== RID_DELETE
)
13385 /* Consume the `new' or `delete' token. */
13386 cp_lexer_consume_token (parser
->lexer
);
13388 /* Peek at the next token. */
13389 token
= cp_lexer_peek_token (parser
->lexer
);
13390 /* If it's a `[' token then this is the array variant of the
13392 if (token
->type
== CPP_OPEN_SQUARE
)
13394 /* Consume the `[' token. */
13395 cp_lexer_consume_token (parser
->lexer
);
13396 /* Look for the `]' token. */
13397 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13398 id
= ansi_opname (op
== NEW_EXPR
13399 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
13401 /* Otherwise, we have the non-array variant. */
13403 id
= ansi_opname (op
);
13409 id
= ansi_opname (PLUS_EXPR
);
13413 id
= ansi_opname (MINUS_EXPR
);
13417 id
= ansi_opname (MULT_EXPR
);
13421 id
= ansi_opname (TRUNC_DIV_EXPR
);
13425 id
= ansi_opname (TRUNC_MOD_EXPR
);
13429 id
= ansi_opname (BIT_XOR_EXPR
);
13433 id
= ansi_opname (BIT_AND_EXPR
);
13437 id
= ansi_opname (BIT_IOR_EXPR
);
13441 id
= ansi_opname (BIT_NOT_EXPR
);
13445 id
= ansi_opname (TRUTH_NOT_EXPR
);
13449 id
= ansi_assopname (NOP_EXPR
);
13453 id
= ansi_opname (LT_EXPR
);
13457 id
= ansi_opname (GT_EXPR
);
13461 id
= ansi_assopname (PLUS_EXPR
);
13465 id
= ansi_assopname (MINUS_EXPR
);
13469 id
= ansi_assopname (MULT_EXPR
);
13473 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13477 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13481 id
= ansi_assopname (BIT_XOR_EXPR
);
13485 id
= ansi_assopname (BIT_AND_EXPR
);
13489 id
= ansi_assopname (BIT_IOR_EXPR
);
13493 id
= ansi_opname (LSHIFT_EXPR
);
13497 id
= ansi_opname (RSHIFT_EXPR
);
13500 case CPP_LSHIFT_EQ
:
13501 id
= ansi_assopname (LSHIFT_EXPR
);
13504 case CPP_RSHIFT_EQ
:
13505 id
= ansi_assopname (RSHIFT_EXPR
);
13509 id
= ansi_opname (EQ_EXPR
);
13513 id
= ansi_opname (NE_EXPR
);
13517 id
= ansi_opname (LE_EXPR
);
13520 case CPP_GREATER_EQ
:
13521 id
= ansi_opname (GE_EXPR
);
13525 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13529 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13532 case CPP_PLUS_PLUS
:
13533 id
= ansi_opname (POSTINCREMENT_EXPR
);
13536 case CPP_MINUS_MINUS
:
13537 id
= ansi_opname (PREDECREMENT_EXPR
);
13541 id
= ansi_opname (COMPOUND_EXPR
);
13544 case CPP_DEREF_STAR
:
13545 id
= ansi_opname (MEMBER_REF
);
13549 id
= ansi_opname (COMPONENT_REF
);
13552 case CPP_OPEN_PAREN
:
13553 /* Consume the `('. */
13554 cp_lexer_consume_token (parser
->lexer
);
13555 /* Look for the matching `)'. */
13556 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13557 return ansi_opname (CALL_EXPR
);
13559 case CPP_OPEN_SQUARE
:
13560 /* Consume the `['. */
13561 cp_lexer_consume_token (parser
->lexer
);
13562 /* Look for the matching `]'. */
13563 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13564 return ansi_opname (ARRAY_REF
);
13566 case CPP_UTF8STRING
:
13567 case CPP_UTF8STRING_USERDEF
:
13573 case CPP_STRING_USERDEF
:
13574 case CPP_WSTRING_USERDEF
:
13575 case CPP_STRING16_USERDEF
:
13576 case CPP_STRING32_USERDEF
:
13578 tree str
, string_tree
;
13581 if (cxx_dialect
== cxx98
)
13582 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13584 /* Consume the string. */
13585 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13586 /*wide_ok=*/true, /*lookup_udlit=*/false);
13587 if (str
== error_mark_node
)
13588 return error_mark_node
;
13589 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13591 string_tree
= USERDEF_LITERAL_VALUE (str
);
13592 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13597 /* Look for the suffix identifier. */
13598 token
= cp_lexer_peek_token (parser
->lexer
);
13599 if (token
->type
== CPP_NAME
)
13600 id
= cp_parser_identifier (parser
);
13601 else if (token
->type
== CPP_KEYWORD
)
13603 error ("unexpected keyword;"
13604 " remove space between quotes and suffix identifier");
13605 return error_mark_node
;
13609 error ("expected suffix identifier");
13610 return error_mark_node
;
13613 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13614 (TREE_TYPE (TREE_TYPE (string_tree
))));
13615 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13618 error ("expected empty string after %<operator%> keyword");
13619 return error_mark_node
;
13621 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13624 error ("invalid encoding prefix in literal operator");
13625 return error_mark_node
;
13627 if (id
!= error_mark_node
)
13629 const char *name
= IDENTIFIER_POINTER (id
);
13630 id
= cp_literal_operator_id (name
);
13636 /* Anything else is an error. */
13640 /* If we have selected an identifier, we need to consume the
13643 cp_lexer_consume_token (parser
->lexer
);
13644 /* Otherwise, no valid operator name was present. */
13647 cp_parser_error (parser
, "expected operator");
13648 id
= error_mark_node
;
13654 /* Parse a template-declaration.
13656 template-declaration:
13657 export [opt] template < template-parameter-list > declaration
13659 If MEMBER_P is TRUE, this template-declaration occurs within a
13662 The grammar rule given by the standard isn't correct. What
13663 is really meant is:
13665 template-declaration:
13666 export [opt] template-parameter-list-seq
13667 decl-specifier-seq [opt] init-declarator [opt] ;
13668 export [opt] template-parameter-list-seq
13669 function-definition
13671 template-parameter-list-seq:
13672 template-parameter-list-seq [opt]
13673 template < template-parameter-list >
13675 Concept Extensions:
13677 template-parameter-list-seq:
13678 template < template-parameter-list > requires-clause [opt]
13681 requires logical-or-expression */
13684 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13686 /* Check for `export'. */
13687 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13689 /* Consume the `export' token. */
13690 cp_lexer_consume_token (parser
->lexer
);
13691 /* Warn that we do not support `export'. */
13692 warning (0, "keyword %<export%> not implemented, and will be ignored");
13695 cp_parser_template_declaration_after_export (parser
, member_p
);
13698 /* Parse a template-parameter-list.
13700 template-parameter-list:
13702 template-parameter-list , template-parameter
13704 Returns a TREE_LIST. Each node represents a template parameter.
13705 The nodes are connected via their TREE_CHAINs. */
13708 cp_parser_template_parameter_list (cp_parser
* parser
)
13710 tree parameter_list
= NULL_TREE
;
13712 begin_template_parm_list ();
13714 /* The loop below parses the template parms. We first need to know
13715 the total number of template parms to be able to compute proper
13716 canonical types of each dependent type. So after the loop, when
13717 we know the total number of template parms,
13718 end_template_parm_list computes the proper canonical types and
13719 fixes up the dependent types accordingly. */
13724 bool is_parameter_pack
;
13725 location_t parm_loc
;
13727 /* Parse the template-parameter. */
13728 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13729 parameter
= cp_parser_template_parameter (parser
,
13731 &is_parameter_pack
);
13732 /* Add it to the list. */
13733 if (parameter
!= error_mark_node
)
13734 parameter_list
= process_template_parm (parameter_list
,
13738 is_parameter_pack
);
13741 tree err_parm
= build_tree_list (parameter
, parameter
);
13742 parameter_list
= chainon (parameter_list
, err_parm
);
13745 /* If the next token is not a `,', we're done. */
13746 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13748 /* Otherwise, consume the `,' token. */
13749 cp_lexer_consume_token (parser
->lexer
);
13752 return end_template_parm_list (parameter_list
);
13755 /* Parse a introduction-list.
13758 introduced-parameter
13759 introduction-list , introduced-parameter
13761 introduced-parameter:
13762 ...[opt] identifier
13764 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13765 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13766 WILDCARD_DECL will also have DECL_NAME set and token location in
13767 DECL_SOURCE_LOCATION. */
13770 cp_parser_introduction_list (cp_parser
*parser
)
13772 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
13776 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
13778 cp_lexer_consume_token (parser
->lexer
);
13780 /* Build placeholder. */
13781 tree parm
= build_nt (WILDCARD_DECL
);
13782 DECL_SOURCE_LOCATION (parm
)
13783 = cp_lexer_peek_token (parser
->lexer
)->location
;
13784 DECL_NAME (parm
) = cp_parser_identifier (parser
);
13785 WILDCARD_PACK_P (parm
) = is_pack
;
13786 vec_safe_push (introduction_vec
, parm
);
13788 /* If the next token is not a `,', we're done. */
13789 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13791 /* Otherwise, consume the `,' token. */
13792 cp_lexer_consume_token (parser
->lexer
);
13795 /* Convert the vec into a TREE_VEC. */
13796 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
13799 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
13800 TREE_VEC_ELT (introduction_list
, n
) = parm
;
13802 release_tree_vector (introduction_vec
);
13803 return introduction_list
;
13806 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13807 is an abstract declarator. */
13809 static inline cp_declarator
*
13810 get_id_declarator (cp_declarator
*declarator
)
13812 cp_declarator
*d
= declarator
;
13813 while (d
&& d
->kind
!= cdk_id
)
13818 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13819 is an abstract declarator. */
13822 get_unqualified_id (cp_declarator
*declarator
)
13824 declarator
= get_id_declarator (declarator
);
13826 return declarator
->u
.id
.unqualified_name
;
13831 /* Returns true if DECL represents a constrained-parameter. */
13834 is_constrained_parameter (tree decl
)
13837 && TREE_CODE (decl
) == TYPE_DECL
13838 && CONSTRAINED_PARM_CONCEPT (decl
)
13839 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
13842 /* Returns true if PARM declares a constrained-parameter. */
13845 is_constrained_parameter (cp_parameter_declarator
*parm
)
13847 return is_constrained_parameter (parm
->decl_specifiers
.type
);
13850 /* Check that the type parameter is only a declarator-id, and that its
13851 type is not cv-qualified. */
13854 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
13855 cp_parameter_declarator
*parm
)
13857 if (!parm
->declarator
)
13860 if (parm
->declarator
->kind
!= cdk_id
)
13862 cp_parser_error (parser
, "invalid constrained type parameter");
13866 /* Don't allow cv-qualified type parameters. */
13867 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
13868 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
13870 cp_parser_error (parser
, "cv-qualified type parameter");
13877 /* Finish parsing/processing a template type parameter and checking
13878 various restrictions. */
13881 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
13883 cp_parameter_declarator
* parmdecl
)
13885 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
13886 return finish_template_type_parm (class_type_node
, id
);
13888 return error_mark_node
;
13892 finish_constrained_template_template_parm (tree proto
, tree id
)
13894 /* FIXME: This should probably be copied, and we may need to adjust
13895 the template parameter depths. */
13896 tree saved_parms
= current_template_parms
;
13897 begin_template_parm_list ();
13898 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
13899 end_template_parm_list ();
13901 tree parm
= finish_template_template_parm (class_type_node
, id
);
13902 current_template_parms
= saved_parms
;
13907 /* Finish parsing/processing a template template parameter by borrowing
13908 the template parameter list from the prototype parameter. */
13911 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
13914 cp_parameter_declarator
*parmdecl
)
13916 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
13917 return error_mark_node
;
13918 return finish_constrained_template_template_parm (proto
, id
);
13921 /* Create a new non-type template parameter from the given PARM
13925 constrained_non_type_template_parm (bool *is_non_type
,
13926 cp_parameter_declarator
*parm
)
13928 *is_non_type
= true;
13929 cp_declarator
*decl
= parm
->declarator
;
13930 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
13931 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
13932 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
13935 /* Build a constrained template parameter based on the PARMDECL
13936 declarator. The type of PARMDECL is the constrained type, which
13937 refers to the prototype template parameter that ultimately
13938 specifies the type of the declared parameter. */
13941 finish_constrained_parameter (cp_parser
*parser
,
13942 cp_parameter_declarator
*parmdecl
,
13944 bool *is_parameter_pack
)
13946 tree decl
= parmdecl
->decl_specifiers
.type
;
13947 tree id
= get_unqualified_id (parmdecl
->declarator
);
13948 tree def
= parmdecl
->default_argument
;
13949 tree proto
= DECL_INITIAL (decl
);
13951 /* A template parameter constrained by a variadic concept shall also
13952 be declared as a template parameter pack. */
13953 bool is_variadic
= template_parameter_pack_p (proto
);
13954 if (is_variadic
&& !*is_parameter_pack
)
13955 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
13957 /* Build the parameter. Return an error if the declarator was invalid. */
13959 if (TREE_CODE (proto
) == TYPE_DECL
)
13960 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
13961 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
13962 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
13965 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
13966 if (parm
== error_mark_node
)
13967 return error_mark_node
;
13969 /* Finish the parameter decl and create a node attaching the
13970 default argument and constraint. */
13971 parm
= build_tree_list (def
, parm
);
13972 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
13977 /* Returns true if the parsed type actually represents the declaration
13978 of a type template-parameter. */
13981 declares_constrained_type_template_parameter (tree type
)
13983 return (is_constrained_parameter (type
)
13984 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
13988 /* Returns true if the parsed type actually represents the declaration of
13989 a template template-parameter. */
13992 declares_constrained_template_template_parameter (tree type
)
13994 return (is_constrained_parameter (type
)
13995 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
13998 /* Parse a default argument for a type template-parameter.
13999 Note that diagnostics are handled in cp_parser_template_parameter. */
14002 cp_parser_default_type_template_argument (cp_parser
*parser
)
14004 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14006 /* Consume the `=' token. */
14007 cp_lexer_consume_token (parser
->lexer
);
14009 /* Parse the default-argument. */
14010 push_deferring_access_checks (dk_no_deferred
);
14011 tree default_argument
= cp_parser_type_id (parser
);
14012 pop_deferring_access_checks ();
14014 return default_argument
;
14017 /* Parse a default argument for a template template-parameter. */
14020 cp_parser_default_template_template_argument (cp_parser
*parser
)
14022 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14026 /* Consume the `='. */
14027 cp_lexer_consume_token (parser
->lexer
);
14028 /* Parse the id-expression. */
14029 push_deferring_access_checks (dk_no_deferred
);
14030 /* save token before parsing the id-expression, for error
14032 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
14033 tree default_argument
14034 = cp_parser_id_expression (parser
,
14035 /*template_keyword_p=*/false,
14036 /*check_dependency_p=*/true,
14037 /*template_p=*/&is_template
,
14038 /*declarator_p=*/false,
14039 /*optional_p=*/false);
14040 if (TREE_CODE (default_argument
) == TYPE_DECL
)
14041 /* If the id-expression was a template-id that refers to
14042 a template-class, we already have the declaration here,
14043 so no further lookup is needed. */
14046 /* Look up the name. */
14048 = cp_parser_lookup_name (parser
, default_argument
,
14050 /*is_template=*/is_template
,
14051 /*is_namespace=*/false,
14052 /*check_dependency=*/true,
14053 /*ambiguous_decls=*/NULL
,
14055 /* See if the default argument is valid. */
14056 default_argument
= check_template_template_default_arg (default_argument
);
14057 pop_deferring_access_checks ();
14058 return default_argument
;
14061 /* Parse a template-parameter.
14063 template-parameter:
14065 parameter-declaration
14067 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14068 the parameter. The TREE_PURPOSE is the default value, if any.
14069 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14070 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14071 set to true iff this parameter is a parameter pack. */
14074 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
14075 bool *is_parameter_pack
)
14078 cp_parameter_declarator
*parameter_declarator
;
14081 /* Assume it is a type parameter or a template parameter. */
14082 *is_non_type
= false;
14083 /* Assume it not a parameter pack. */
14084 *is_parameter_pack
= false;
14085 /* Peek at the next token. */
14086 token
= cp_lexer_peek_token (parser
->lexer
);
14087 /* If it is `class' or `template', we have a type-parameter. */
14088 if (token
->keyword
== RID_TEMPLATE
)
14089 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14090 /* If it is `class' or `typename' we do not know yet whether it is a
14091 type parameter or a non-type parameter. Consider:
14093 template <typename T, typename T::X X> ...
14097 template <class C, class D*> ...
14099 Here, the first parameter is a type parameter, and the second is
14100 a non-type parameter. We can tell by looking at the token after
14101 the identifier -- if it is a `,', `=', or `>' then we have a type
14103 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
14105 /* Peek at the token after `class' or `typename'. */
14106 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14107 /* If it's an ellipsis, we have a template type parameter
14109 if (token
->type
== CPP_ELLIPSIS
)
14110 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14111 /* If it's an identifier, skip it. */
14112 if (token
->type
== CPP_NAME
)
14113 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
14114 /* Now, see if the token looks like the end of a template
14116 if (token
->type
== CPP_COMMA
14117 || token
->type
== CPP_EQ
14118 || token
->type
== CPP_GREATER
)
14119 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14122 /* Otherwise, it is a non-type parameter or a constrained parameter.
14126 When parsing a default template-argument for a non-type
14127 template-parameter, the first non-nested `>' is taken as the end
14128 of the template parameter-list rather than a greater-than
14130 parameter_declarator
14131 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
14132 /*parenthesized_p=*/NULL
);
14134 if (!parameter_declarator
)
14135 return error_mark_node
;
14137 /* If the parameter declaration is marked as a parameter pack, set
14138 *IS_PARAMETER_PACK to notify the caller. */
14139 if (parameter_declarator
->template_parameter_pack_p
)
14140 *is_parameter_pack
= true;
14142 if (parameter_declarator
->default_argument
)
14144 /* Can happen in some cases of erroneous input (c++/34892). */
14145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14146 /* Consume the `...' for better error recovery. */
14147 cp_lexer_consume_token (parser
->lexer
);
14150 // The parameter may have been constrained.
14151 if (is_constrained_parameter (parameter_declarator
))
14152 return finish_constrained_parameter (parser
,
14153 parameter_declarator
,
14155 is_parameter_pack
);
14157 // Now we're sure that the parameter is a non-type parameter.
14158 *is_non_type
= true;
14160 parm
= grokdeclarator (parameter_declarator
->declarator
,
14161 ¶meter_declarator
->decl_specifiers
,
14162 TPARM
, /*initialized=*/0,
14163 /*attrlist=*/NULL
);
14164 if (parm
== error_mark_node
)
14165 return error_mark_node
;
14167 return build_tree_list (parameter_declarator
->default_argument
, parm
);
14170 /* Parse a type-parameter.
14173 class identifier [opt]
14174 class identifier [opt] = type-id
14175 typename identifier [opt]
14176 typename identifier [opt] = type-id
14177 template < template-parameter-list > class identifier [opt]
14178 template < template-parameter-list > class identifier [opt]
14181 GNU Extension (variadic templates):
14184 class ... identifier [opt]
14185 typename ... identifier [opt]
14187 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14188 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14189 the declaration of the parameter.
14191 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14194 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
14199 /* Look for a keyword to tell us what kind of parameter this is. */
14200 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
14202 return error_mark_node
;
14204 switch (token
->keyword
)
14210 tree default_argument
;
14212 /* If the next token is an ellipsis, we have a template
14214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14216 /* Consume the `...' token. */
14217 cp_lexer_consume_token (parser
->lexer
);
14218 maybe_warn_variadic_templates ();
14220 *is_parameter_pack
= true;
14223 /* If the next token is an identifier, then it names the
14225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14226 identifier
= cp_parser_identifier (parser
);
14228 identifier
= NULL_TREE
;
14230 /* Create the parameter. */
14231 parameter
= finish_template_type_parm (class_type_node
, identifier
);
14233 /* If the next token is an `=', we have a default argument. */
14234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14237 = cp_parser_default_type_template_argument (parser
);
14239 /* Template parameter packs cannot have default
14241 if (*is_parameter_pack
)
14244 error_at (token
->location
,
14245 "template parameter pack %qD cannot have a "
14246 "default argument", identifier
);
14248 error_at (token
->location
,
14249 "template parameter packs cannot have "
14250 "default arguments");
14251 default_argument
= NULL_TREE
;
14253 else if (check_for_bare_parameter_packs (default_argument
))
14254 default_argument
= error_mark_node
;
14257 default_argument
= NULL_TREE
;
14259 /* Create the combined representation of the parameter and the
14260 default argument. */
14261 parameter
= build_tree_list (default_argument
, parameter
);
14268 tree default_argument
;
14270 /* Look for the `<'. */
14271 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14272 /* Parse the template-parameter-list. */
14273 cp_parser_template_parameter_list (parser
);
14274 /* Look for the `>'. */
14275 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14277 // If template requirements are present, parse them.
14280 tree reqs
= get_shorthand_constraints (current_template_parms
);
14281 if (tree r
= cp_parser_requires_clause_opt (parser
))
14282 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
14283 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
14286 /* Look for the `class' or 'typename' keywords. */
14287 cp_parser_type_parameter_key (parser
);
14288 /* If the next token is an ellipsis, we have a template
14290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14292 /* Consume the `...' token. */
14293 cp_lexer_consume_token (parser
->lexer
);
14294 maybe_warn_variadic_templates ();
14296 *is_parameter_pack
= true;
14298 /* If the next token is an `=', then there is a
14299 default-argument. If the next token is a `>', we are at
14300 the end of the parameter-list. If the next token is a `,',
14301 then we are at the end of this parameter. */
14302 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
14303 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
14304 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14306 identifier
= cp_parser_identifier (parser
);
14307 /* Treat invalid names as if the parameter were nameless. */
14308 if (identifier
== error_mark_node
)
14309 identifier
= NULL_TREE
;
14312 identifier
= NULL_TREE
;
14314 /* Create the template parameter. */
14315 parameter
= finish_template_template_parm (class_type_node
,
14318 /* If the next token is an `=', then there is a
14319 default-argument. */
14320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14323 = cp_parser_default_template_template_argument (parser
);
14325 /* Template parameter packs cannot have default
14327 if (*is_parameter_pack
)
14330 error_at (token
->location
,
14331 "template parameter pack %qD cannot "
14332 "have a default argument",
14335 error_at (token
->location
, "template parameter packs cannot "
14336 "have default arguments");
14337 default_argument
= NULL_TREE
;
14341 default_argument
= NULL_TREE
;
14343 /* Create the combined representation of the parameter and the
14344 default argument. */
14345 parameter
= build_tree_list (default_argument
, parameter
);
14350 gcc_unreachable ();
14357 /* Parse a template-id.
14360 template-name < template-argument-list [opt] >
14362 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14363 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14364 returned. Otherwise, if the template-name names a function, or set
14365 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14366 names a class, returns a TYPE_DECL for the specialization.
14368 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14369 uninstantiated templates. */
14372 cp_parser_template_id (cp_parser
*parser
,
14373 bool template_keyword_p
,
14374 bool check_dependency_p
,
14375 enum tag_types tag_type
,
14376 bool is_declaration
)
14382 cp_token_position start_of_id
= 0;
14383 deferred_access_check
*chk
;
14384 vec
<deferred_access_check
, va_gc
> *access_check
;
14385 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
14386 bool is_identifier
;
14388 /* If the next token corresponds to a template-id, there is no need
14390 next_token
= cp_lexer_peek_token (parser
->lexer
);
14391 if (next_token
->type
== CPP_TEMPLATE_ID
)
14393 struct tree_check
*check_value
;
14395 /* Get the stored value. */
14396 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
14397 /* Perform any access checks that were deferred. */
14398 access_check
= check_value
->checks
;
14401 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
14402 perform_or_defer_access_check (chk
->binfo
,
14405 tf_warning_or_error
);
14407 /* Return the stored value. */
14408 return check_value
->value
;
14411 /* Avoid performing name lookup if there is no possibility of
14412 finding a template-id. */
14413 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
14414 || (next_token
->type
== CPP_NAME
14415 && !cp_parser_nth_token_starts_template_argument_list_p
14418 cp_parser_error (parser
, "expected template-id");
14419 return error_mark_node
;
14422 /* Remember where the template-id starts. */
14423 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
14424 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
14426 push_deferring_access_checks (dk_deferred
);
14428 /* Parse the template-name. */
14429 is_identifier
= false;
14430 templ
= cp_parser_template_name (parser
, template_keyword_p
,
14431 check_dependency_p
,
14435 if (templ
== error_mark_node
|| is_identifier
)
14437 pop_deferring_access_checks ();
14441 /* If we find the sequence `[:' after a template-name, it's probably
14442 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14443 parse correctly the argument list. */
14444 next_token
= cp_lexer_peek_token (parser
->lexer
);
14445 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14446 if (next_token
->type
== CPP_OPEN_SQUARE
14447 && next_token
->flags
& DIGRAPH
14448 && next_token_2
->type
== CPP_COLON
14449 && !(next_token_2
->flags
& PREV_WHITE
))
14451 cp_parser_parse_tentatively (parser
);
14452 /* Change `:' into `::'. */
14453 next_token_2
->type
= CPP_SCOPE
;
14454 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14456 cp_lexer_consume_token (parser
->lexer
);
14458 /* Parse the arguments. */
14459 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14460 if (!cp_parser_parse_definitely (parser
))
14462 /* If we couldn't parse an argument list, then we revert our changes
14463 and return simply an error. Maybe this is not a template-id
14465 next_token_2
->type
= CPP_COLON
;
14466 cp_parser_error (parser
, "expected %<<%>");
14467 pop_deferring_access_checks ();
14468 return error_mark_node
;
14470 /* Otherwise, emit an error about the invalid digraph, but continue
14471 parsing because we got our argument list. */
14472 if (permerror (next_token
->location
,
14473 "%<<::%> cannot begin a template-argument list"))
14475 static bool hint
= false;
14476 inform (next_token
->location
,
14477 "%<<:%> is an alternate spelling for %<[%>."
14478 " Insert whitespace between %<<%> and %<::%>");
14479 if (!hint
&& !flag_permissive
)
14481 inform (next_token
->location
, "(if you use %<-fpermissive%> "
14482 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14483 "accept your code)");
14490 /* Look for the `<' that starts the template-argument-list. */
14491 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
14493 pop_deferring_access_checks ();
14494 return error_mark_node
;
14496 /* Parse the arguments. */
14497 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14500 /* Build a representation of the specialization. */
14501 if (identifier_p (templ
))
14502 template_id
= build_min_nt_loc (next_token
->location
,
14505 else if (DECL_TYPE_TEMPLATE_P (templ
)
14506 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
14508 bool entering_scope
;
14509 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14510 template (rather than some instantiation thereof) only if
14511 is not nested within some other construct. For example, in
14512 "template <typename T> void f(T) { A<T>::", A<T> is just an
14513 instantiation of A. */
14514 entering_scope
= (template_parm_scope_p ()
14515 && cp_lexer_next_token_is (parser
->lexer
,
14518 = finish_template_type (templ
, arguments
, entering_scope
);
14520 /* A template-like identifier may be a partial concept id. */
14521 else if (flag_concepts
14522 && (template_id
= (cp_parser_maybe_partial_concept_id
14523 (parser
, templ
, arguments
))))
14524 return template_id
;
14525 else if (variable_template_p (templ
))
14527 template_id
= lookup_template_variable (templ
, arguments
);
14528 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14529 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14533 /* If it's not a class-template or a template-template, it should be
14534 a function-template. */
14535 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
14536 || TREE_CODE (templ
) == OVERLOAD
14537 || BASELINK_P (templ
)));
14539 template_id
= lookup_template_function (templ
, arguments
);
14540 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14541 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14544 /* If parsing tentatively, replace the sequence of tokens that makes
14545 up the template-id with a CPP_TEMPLATE_ID token. That way,
14546 should we re-parse the token stream, we will not have to repeat
14547 the effort required to do the parse, nor will we issue duplicate
14548 error messages about problems during instantiation of the
14551 /* Don't do this if we had a parse error in a declarator; re-parsing
14552 might succeed if a name changes meaning (60361). */
14553 && !(cp_parser_error_occurred (parser
)
14554 && cp_parser_parsing_tentatively (parser
)
14555 && parser
->in_declarator_p
))
14557 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
14559 /* Reset the contents of the START_OF_ID token. */
14560 token
->type
= CPP_TEMPLATE_ID
;
14561 /* Retrieve any deferred checks. Do not pop this access checks yet
14562 so the memory will not be reclaimed during token replacing below. */
14563 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14564 token
->u
.tree_check_value
->value
= template_id
;
14565 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14566 token
->keyword
= RID_MAX
;
14568 /* Purge all subsequent tokens. */
14569 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
14571 /* ??? Can we actually assume that, if template_id ==
14572 error_mark_node, we will have issued a diagnostic to the
14573 user, as opposed to simply marking the tentative parse as
14575 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
14576 error_at (token
->location
, "parse error in template argument list");
14579 pop_to_parent_deferring_access_checks ();
14580 return template_id
;
14583 /* Parse a template-name.
14588 The standard should actually say:
14592 operator-function-id
14594 A defect report has been filed about this issue.
14596 A conversion-function-id cannot be a template name because they cannot
14597 be part of a template-id. In fact, looking at this code:
14599 a.operator K<int>()
14601 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14602 It is impossible to call a templated conversion-function-id with an
14603 explicit argument list, since the only allowed template parameter is
14604 the type to which it is converting.
14606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14607 `template' keyword, in a construction like:
14611 In that case `f' is taken to be a template-name, even though there
14612 is no way of knowing for sure.
14614 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14615 name refers to a set of overloaded functions, at least one of which
14616 is a template, or an IDENTIFIER_NODE with the name of the template,
14617 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14618 names are looked up inside uninstantiated templates. */
14621 cp_parser_template_name (cp_parser
* parser
,
14622 bool template_keyword_p
,
14623 bool check_dependency_p
,
14624 bool is_declaration
,
14625 enum tag_types tag_type
,
14626 bool *is_identifier
)
14631 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14633 /* If the next token is `operator', then we have either an
14634 operator-function-id or a conversion-function-id. */
14635 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
14637 /* We don't know whether we're looking at an
14638 operator-function-id or a conversion-function-id. */
14639 cp_parser_parse_tentatively (parser
);
14640 /* Try an operator-function-id. */
14641 identifier
= cp_parser_operator_function_id (parser
);
14642 /* If that didn't work, try a conversion-function-id. */
14643 if (!cp_parser_parse_definitely (parser
))
14645 cp_parser_error (parser
, "expected template-name");
14646 return error_mark_node
;
14649 /* Look for the identifier. */
14651 identifier
= cp_parser_identifier (parser
);
14653 /* If we didn't find an identifier, we don't have a template-id. */
14654 if (identifier
== error_mark_node
)
14655 return error_mark_node
;
14657 /* If the name immediately followed the `template' keyword, then it
14658 is a template-name. However, if the next token is not `<', then
14659 we do not treat it as a template-name, since it is not being used
14660 as part of a template-id. This enables us to handle constructs
14663 template <typename T> struct S { S(); };
14664 template <typename T> S<T>::S();
14666 correctly. We would treat `S' as a template -- if it were `S<T>'
14667 -- but we do not if there is no `<'. */
14669 if (processing_template_decl
14670 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
14672 /* In a declaration, in a dependent context, we pretend that the
14673 "template" keyword was present in order to improve error
14674 recovery. For example, given:
14676 template <typename T> void f(T::X<int>);
14678 we want to treat "X<int>" as a template-id. */
14680 && !template_keyword_p
14681 && parser
->scope
&& TYPE_P (parser
->scope
)
14682 && check_dependency_p
14683 && dependent_scope_p (parser
->scope
)
14684 /* Do not do this for dtors (or ctors), since they never
14685 need the template keyword before their name. */
14686 && !constructor_name_p (identifier
, parser
->scope
))
14688 cp_token_position start
= 0;
14690 /* Explain what went wrong. */
14691 error_at (token
->location
, "non-template %qD used as template",
14693 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
14694 parser
->scope
, identifier
);
14695 /* If parsing tentatively, find the location of the "<" token. */
14696 if (cp_parser_simulate_error (parser
))
14697 start
= cp_lexer_token_position (parser
->lexer
, true);
14698 /* Parse the template arguments so that we can issue error
14699 messages about them. */
14700 cp_lexer_consume_token (parser
->lexer
);
14701 cp_parser_enclosed_template_argument_list (parser
);
14702 /* Skip tokens until we find a good place from which to
14703 continue parsing. */
14704 cp_parser_skip_to_closing_parenthesis (parser
,
14705 /*recovering=*/true,
14707 /*consume_paren=*/false);
14708 /* If parsing tentatively, permanently remove the
14709 template argument list. That will prevent duplicate
14710 error messages from being issued about the missing
14711 "template" keyword. */
14713 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
14715 *is_identifier
= true;
14719 /* If the "template" keyword is present, then there is generally
14720 no point in doing name-lookup, so we just return IDENTIFIER.
14721 But, if the qualifying scope is non-dependent then we can
14722 (and must) do name-lookup normally. */
14723 if (template_keyword_p
14725 || (TYPE_P (parser
->scope
)
14726 && dependent_type_p (parser
->scope
))))
14730 /* Look up the name. */
14731 decl
= cp_parser_lookup_name (parser
, identifier
,
14733 /*is_template=*/true,
14734 /*is_namespace=*/false,
14735 check_dependency_p
,
14736 /*ambiguous_decls=*/NULL
,
14739 decl
= strip_using_decl (decl
);
14741 /* If DECL is a template, then the name was a template-name. */
14742 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
14744 if (TREE_DEPRECATED (decl
)
14745 && deprecated_state
!= DEPRECATED_SUPPRESS
)
14746 warn_deprecated_use (decl
, NULL_TREE
);
14750 tree fn
= NULL_TREE
;
14752 /* The standard does not explicitly indicate whether a name that
14753 names a set of overloaded declarations, some of which are
14754 templates, is a template-name. However, such a name should
14755 be a template-name; otherwise, there is no way to form a
14756 template-id for the overloaded templates. */
14757 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14758 if (TREE_CODE (fns
) == OVERLOAD
)
14759 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14760 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14765 /* The name does not name a template. */
14766 cp_parser_error (parser
, "expected template-name");
14767 return error_mark_node
;
14771 /* If DECL is dependent, and refers to a function, then just return
14772 its name; we will look it up again during template instantiation. */
14773 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14775 tree scope
= ovl_scope (decl
);
14776 if (TYPE_P (scope
) && dependent_type_p (scope
))
14783 /* Parse a template-argument-list.
14785 template-argument-list:
14786 template-argument ... [opt]
14787 template-argument-list , template-argument ... [opt]
14789 Returns a TREE_VEC containing the arguments. */
14792 cp_parser_template_argument_list (cp_parser
* parser
)
14794 tree fixed_args
[10];
14795 unsigned n_args
= 0;
14796 unsigned alloced
= 10;
14797 tree
*arg_ary
= fixed_args
;
14799 bool saved_in_template_argument_list_p
;
14801 bool saved_non_ice_p
;
14803 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14804 parser
->in_template_argument_list_p
= true;
14805 /* Even if the template-id appears in an integral
14806 constant-expression, the contents of the argument list do
14808 saved_ice_p
= parser
->integral_constant_expression_p
;
14809 parser
->integral_constant_expression_p
= false;
14810 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14811 parser
->non_integral_constant_expression_p
= false;
14813 /* Parse the arguments. */
14819 /* Consume the comma. */
14820 cp_lexer_consume_token (parser
->lexer
);
14822 /* Parse the template-argument. */
14823 argument
= cp_parser_template_argument (parser
);
14825 /* If the next token is an ellipsis, we're expanding a template
14827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14829 if (argument
== error_mark_node
)
14831 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14832 error_at (token
->location
,
14833 "expected parameter pack before %<...%>");
14835 /* Consume the `...' token. */
14836 cp_lexer_consume_token (parser
->lexer
);
14838 /* Make the argument into a TYPE_PACK_EXPANSION or
14839 EXPR_PACK_EXPANSION. */
14840 argument
= make_pack_expansion (argument
);
14843 if (n_args
== alloced
)
14847 if (arg_ary
== fixed_args
)
14849 arg_ary
= XNEWVEC (tree
, alloced
);
14850 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14853 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14855 arg_ary
[n_args
++] = argument
;
14857 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14859 vec
= make_tree_vec (n_args
);
14862 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14864 if (arg_ary
!= fixed_args
)
14866 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14867 parser
->integral_constant_expression_p
= saved_ice_p
;
14868 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14870 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14874 /* Parse a template-argument.
14877 assignment-expression
14881 The representation is that of an assignment-expression, type-id, or
14882 id-expression -- except that the qualified id-expression is
14883 evaluated, so that the value returned is either a DECL or an
14886 Although the standard says "assignment-expression", it forbids
14887 throw-expressions or assignments in the template argument.
14888 Therefore, we use "conditional-expression" instead. */
14891 cp_parser_template_argument (cp_parser
* parser
)
14896 bool maybe_type_id
= false;
14897 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14898 location_t loc
= 0;
14901 /* There's really no way to know what we're looking at, so we just
14902 try each alternative in order.
14906 In a template-argument, an ambiguity between a type-id and an
14907 expression is resolved to a type-id, regardless of the form of
14908 the corresponding template-parameter.
14910 Therefore, we try a type-id first. */
14911 cp_parser_parse_tentatively (parser
);
14912 argument
= cp_parser_template_type_arg (parser
);
14913 /* If there was no error parsing the type-id but the next token is a
14914 '>>', our behavior depends on which dialect of C++ we're
14915 parsing. In C++98, we probably found a typo for '> >'. But there
14916 are type-id which are also valid expressions. For instance:
14918 struct X { int operator >> (int); };
14919 template <int V> struct Foo {};
14922 Here 'X()' is a valid type-id of a function type, but the user just
14923 wanted to write the expression "X() >> 5". Thus, we remember that we
14924 found a valid type-id, but we still try to parse the argument as an
14925 expression to see what happens.
14927 In C++0x, the '>>' will be considered two separate '>'
14929 if (!cp_parser_error_occurred (parser
)
14930 && cxx_dialect
== cxx98
14931 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14933 maybe_type_id
= true;
14934 cp_parser_abort_tentative_parse (parser
);
14938 /* If the next token isn't a `,' or a `>', then this argument wasn't
14939 really finished. This means that the argument is not a valid
14941 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14942 cp_parser_error (parser
, "expected template-argument");
14943 /* If that worked, we're done. */
14944 if (cp_parser_parse_definitely (parser
))
14947 /* We're still not sure what the argument will be. */
14948 cp_parser_parse_tentatively (parser
);
14949 /* Try a template. */
14950 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14951 argument
= cp_parser_id_expression (parser
,
14952 /*template_keyword_p=*/false,
14953 /*check_dependency_p=*/true,
14955 /*declarator_p=*/false,
14956 /*optional_p=*/false);
14957 /* If the next token isn't a `,' or a `>', then this argument wasn't
14958 really finished. */
14959 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14960 cp_parser_error (parser
, "expected template-argument");
14961 if (!cp_parser_error_occurred (parser
))
14963 /* Figure out what is being referred to. If the id-expression
14964 was for a class template specialization, then we will have a
14965 TYPE_DECL at this point. There is no need to do name lookup
14966 at this point in that case. */
14967 if (TREE_CODE (argument
) != TYPE_DECL
)
14968 argument
= cp_parser_lookup_name (parser
, argument
,
14970 /*is_template=*/template_p
,
14971 /*is_namespace=*/false,
14972 /*check_dependency=*/true,
14973 /*ambiguous_decls=*/NULL
,
14974 argument_start_token
->location
);
14975 /* Handle a constrained-type-specifier for a non-type template
14977 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
14979 else if (TREE_CODE (argument
) != TEMPLATE_DECL
14980 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14981 cp_parser_error (parser
, "expected template-name");
14983 if (cp_parser_parse_definitely (parser
))
14985 if (TREE_DEPRECATED (argument
))
14986 warn_deprecated_use (argument
, NULL_TREE
);
14989 /* It must be a non-type argument. In C++17 any constant-expression is
14991 if (cxx_dialect
> cxx14
)
14994 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
14996 -- an integral constant-expression of integral or enumeration
14999 -- the name of a non-type template-parameter; or
15001 -- the name of an object or function with external linkage...
15003 -- the address of an object or function with external linkage...
15005 -- a pointer to member... */
15006 /* Look for a non-type template parameter. */
15007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15009 cp_parser_parse_tentatively (parser
);
15010 argument
= cp_parser_primary_expression (parser
,
15011 /*address_p=*/false,
15013 /*template_arg_p=*/true,
15015 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
15016 || !cp_parser_next_token_ends_template_argument_p (parser
))
15017 cp_parser_simulate_error (parser
);
15018 if (cp_parser_parse_definitely (parser
))
15022 /* If the next token is "&", the argument must be the address of an
15023 object or function with external linkage. */
15024 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
15027 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15028 cp_lexer_consume_token (parser
->lexer
);
15030 /* See if we might have an id-expression. */
15031 token
= cp_lexer_peek_token (parser
->lexer
);
15032 if (token
->type
== CPP_NAME
15033 || token
->keyword
== RID_OPERATOR
15034 || token
->type
== CPP_SCOPE
15035 || token
->type
== CPP_TEMPLATE_ID
15036 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
15038 cp_parser_parse_tentatively (parser
);
15039 argument
= cp_parser_primary_expression (parser
,
15042 /*template_arg_p=*/true,
15044 if (cp_parser_error_occurred (parser
)
15045 || !cp_parser_next_token_ends_template_argument_p (parser
))
15046 cp_parser_abort_tentative_parse (parser
);
15051 if (INDIRECT_REF_P (argument
))
15053 /* Strip the dereference temporarily. */
15054 gcc_assert (REFERENCE_REF_P (argument
));
15055 argument
= TREE_OPERAND (argument
, 0);
15058 /* If we're in a template, we represent a qualified-id referring
15059 to a static data member as a SCOPE_REF even if the scope isn't
15060 dependent so that we can check access control later. */
15062 if (TREE_CODE (probe
) == SCOPE_REF
)
15063 probe
= TREE_OPERAND (probe
, 1);
15066 /* A variable without external linkage might still be a
15067 valid constant-expression, so no error is issued here
15068 if the external-linkage check fails. */
15069 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
15070 cp_parser_simulate_error (parser
);
15072 else if (is_overloaded_fn (argument
))
15073 /* All overloaded functions are allowed; if the external
15074 linkage test does not pass, an error will be issued
15078 && (TREE_CODE (argument
) == OFFSET_REF
15079 || TREE_CODE (argument
) == SCOPE_REF
))
15080 /* A pointer-to-member. */
15082 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
15085 cp_parser_simulate_error (parser
);
15087 if (cp_parser_parse_definitely (parser
))
15090 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
15091 tf_warning_or_error
);
15093 argument
= convert_from_reference (argument
);
15098 /* If the argument started with "&", there are no other valid
15099 alternatives at this point. */
15102 cp_parser_error (parser
, "invalid non-type template argument");
15103 return error_mark_node
;
15107 /* If the argument wasn't successfully parsed as a type-id followed
15108 by '>>', the argument can only be a constant expression now.
15109 Otherwise, we try parsing the constant-expression tentatively,
15110 because the argument could really be a type-id. */
15112 cp_parser_parse_tentatively (parser
);
15113 argument
= cp_parser_constant_expression (parser
);
15115 if (!maybe_type_id
)
15117 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15118 cp_parser_error (parser
, "expected template-argument");
15119 if (cp_parser_parse_definitely (parser
))
15121 /* We did our best to parse the argument as a non type-id, but that
15122 was the only alternative that matched (albeit with a '>' after
15123 it). We can assume it's just a typo from the user, and a
15124 diagnostic will then be issued. */
15125 return cp_parser_template_type_arg (parser
);
15128 /* Parse an explicit-instantiation.
15130 explicit-instantiation:
15131 template declaration
15133 Although the standard says `declaration', what it really means is:
15135 explicit-instantiation:
15136 template decl-specifier-seq [opt] declarator [opt] ;
15138 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15139 supposed to be allowed. A defect report has been filed about this
15144 explicit-instantiation:
15145 storage-class-specifier template
15146 decl-specifier-seq [opt] declarator [opt] ;
15147 function-specifier template
15148 decl-specifier-seq [opt] declarator [opt] ; */
15151 cp_parser_explicit_instantiation (cp_parser
* parser
)
15153 int declares_class_or_enum
;
15154 cp_decl_specifier_seq decl_specifiers
;
15155 tree extension_specifier
= NULL_TREE
;
15157 timevar_push (TV_TEMPLATE_INST
);
15159 /* Look for an (optional) storage-class-specifier or
15160 function-specifier. */
15161 if (cp_parser_allow_gnu_extensions_p (parser
))
15163 extension_specifier
15164 = cp_parser_storage_class_specifier_opt (parser
);
15165 if (!extension_specifier
)
15166 extension_specifier
15167 = cp_parser_function_specifier_opt (parser
,
15168 /*decl_specs=*/NULL
);
15171 /* Look for the `template' keyword. */
15172 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15173 /* Let the front end know that we are processing an explicit
15175 begin_explicit_instantiation ();
15176 /* [temp.explicit] says that we are supposed to ignore access
15177 control while processing explicit instantiation directives. */
15178 push_deferring_access_checks (dk_no_check
);
15179 /* Parse a decl-specifier-seq. */
15180 cp_parser_decl_specifier_seq (parser
,
15181 CP_PARSER_FLAGS_OPTIONAL
,
15183 &declares_class_or_enum
);
15184 /* If there was exactly one decl-specifier, and it declared a class,
15185 and there's no declarator, then we have an explicit type
15187 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
15191 type
= check_tag_decl (&decl_specifiers
,
15192 /*explicit_type_instantiation_p=*/true);
15193 /* Turn access control back on for names used during
15194 template instantiation. */
15195 pop_deferring_access_checks ();
15197 do_type_instantiation (type
, extension_specifier
,
15198 /*complain=*/tf_error
);
15202 cp_declarator
*declarator
;
15205 /* Parse the declarator. */
15207 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15208 /*ctor_dtor_or_conv_p=*/NULL
,
15209 /*parenthesized_p=*/NULL
,
15210 /*member_p=*/false,
15211 /*friend_p=*/false);
15212 if (declares_class_or_enum
& 2)
15213 cp_parser_check_for_definition_in_return_type (declarator
,
15214 decl_specifiers
.type
,
15215 decl_specifiers
.locations
[ds_type_spec
]);
15216 if (declarator
!= cp_error_declarator
)
15218 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
15219 permerror (decl_specifiers
.locations
[ds_inline
],
15220 "explicit instantiation shall not use"
15221 " %<inline%> specifier");
15222 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
15223 permerror (decl_specifiers
.locations
[ds_constexpr
],
15224 "explicit instantiation shall not use"
15225 " %<constexpr%> specifier");
15227 decl
= grokdeclarator (declarator
, &decl_specifiers
,
15228 NORMAL
, 0, &decl_specifiers
.attributes
);
15229 /* Turn access control back on for names used during
15230 template instantiation. */
15231 pop_deferring_access_checks ();
15232 /* Do the explicit instantiation. */
15233 do_decl_instantiation (decl
, extension_specifier
);
15237 pop_deferring_access_checks ();
15238 /* Skip the body of the explicit instantiation. */
15239 cp_parser_skip_to_end_of_statement (parser
);
15242 /* We're done with the instantiation. */
15243 end_explicit_instantiation ();
15245 cp_parser_consume_semicolon_at_end_of_statement (parser
);
15247 timevar_pop (TV_TEMPLATE_INST
);
15250 /* Parse an explicit-specialization.
15252 explicit-specialization:
15253 template < > declaration
15255 Although the standard says `declaration', what it really means is:
15257 explicit-specialization:
15258 template <> decl-specifier [opt] init-declarator [opt] ;
15259 template <> function-definition
15260 template <> explicit-specialization
15261 template <> template-declaration */
15264 cp_parser_explicit_specialization (cp_parser
* parser
)
15266 bool need_lang_pop
;
15267 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15269 /* Look for the `template' keyword. */
15270 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15271 /* Look for the `<'. */
15272 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15273 /* Look for the `>'. */
15274 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15275 /* We have processed another parameter list. */
15276 ++parser
->num_template_parameter_lists
;
15279 A template ... explicit specialization ... shall not have C
15281 if (current_lang_name
== lang_name_c
)
15283 error_at (token
->location
, "template specialization with C linkage");
15284 /* Give it C++ linkage to avoid confusing other parts of the
15286 push_lang_context (lang_name_cplusplus
);
15287 need_lang_pop
= true;
15290 need_lang_pop
= false;
15291 /* Let the front end know that we are beginning a specialization. */
15292 if (!begin_specialization ())
15294 end_specialization ();
15298 /* If the next keyword is `template', we need to figure out whether
15299 or not we're looking a template-declaration. */
15300 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15302 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
15303 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
15304 cp_parser_template_declaration_after_export (parser
,
15305 /*member_p=*/false);
15307 cp_parser_explicit_specialization (parser
);
15310 /* Parse the dependent declaration. */
15311 cp_parser_single_declaration (parser
,
15313 /*member_p=*/false,
15314 /*explicit_specialization_p=*/true,
15315 /*friend_p=*/NULL
);
15316 /* We're done with the specialization. */
15317 end_specialization ();
15318 /* For the erroneous case of a template with C linkage, we pushed an
15319 implicit C++ linkage scope; exit that scope now. */
15321 pop_lang_context ();
15322 /* We're done with this parameter list. */
15323 --parser
->num_template_parameter_lists
;
15326 /* Parse a type-specifier.
15329 simple-type-specifier
15332 elaborated-type-specifier
15340 Returns a representation of the type-specifier. For a
15341 class-specifier, enum-specifier, or elaborated-type-specifier, a
15342 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15344 The parser flags FLAGS is used to control type-specifier parsing.
15346 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15347 in a decl-specifier-seq.
15349 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15350 class-specifier, enum-specifier, or elaborated-type-specifier, then
15351 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15352 if a type is declared; 2 if it is defined. Otherwise, it is set to
15355 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15356 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15357 is set to FALSE. */
15360 cp_parser_type_specifier (cp_parser
* parser
,
15361 cp_parser_flags flags
,
15362 cp_decl_specifier_seq
*decl_specs
,
15363 bool is_declaration
,
15364 int* declares_class_or_enum
,
15365 bool* is_cv_qualifier
)
15367 tree type_spec
= NULL_TREE
;
15370 cp_decl_spec ds
= ds_last
;
15372 /* Assume this type-specifier does not declare a new type. */
15373 if (declares_class_or_enum
)
15374 *declares_class_or_enum
= 0;
15375 /* And that it does not specify a cv-qualifier. */
15376 if (is_cv_qualifier
)
15377 *is_cv_qualifier
= false;
15378 /* Peek at the next token. */
15379 token
= cp_lexer_peek_token (parser
->lexer
);
15381 /* If we're looking at a keyword, we can use that to guide the
15382 production we choose. */
15383 keyword
= token
->keyword
;
15387 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15388 goto elaborated_type_specifier
;
15390 /* Look for the enum-specifier. */
15391 type_spec
= cp_parser_enum_specifier (parser
);
15392 /* If that worked, we're done. */
15395 if (declares_class_or_enum
)
15396 *declares_class_or_enum
= 2;
15398 cp_parser_set_decl_spec_type (decl_specs
,
15401 /*type_definition_p=*/true);
15405 goto elaborated_type_specifier
;
15407 /* Any of these indicate either a class-specifier, or an
15408 elaborated-type-specifier. */
15412 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15413 goto elaborated_type_specifier
;
15415 /* Parse tentatively so that we can back up if we don't find a
15416 class-specifier. */
15417 cp_parser_parse_tentatively (parser
);
15418 /* Look for the class-specifier. */
15419 type_spec
= cp_parser_class_specifier (parser
);
15420 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
15421 /* If that worked, we're done. */
15422 if (cp_parser_parse_definitely (parser
))
15424 if (declares_class_or_enum
)
15425 *declares_class_or_enum
= 2;
15427 cp_parser_set_decl_spec_type (decl_specs
,
15430 /*type_definition_p=*/true);
15434 /* Fall through. */
15435 elaborated_type_specifier
:
15436 /* We're declaring (not defining) a class or enum. */
15437 if (declares_class_or_enum
)
15438 *declares_class_or_enum
= 1;
15440 /* Fall through. */
15442 /* Look for an elaborated-type-specifier. */
15444 = (cp_parser_elaborated_type_specifier
15446 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
15449 cp_parser_set_decl_spec_type (decl_specs
,
15452 /*type_definition_p=*/false);
15457 if (is_cv_qualifier
)
15458 *is_cv_qualifier
= true;
15463 if (is_cv_qualifier
)
15464 *is_cv_qualifier
= true;
15469 if (is_cv_qualifier
)
15470 *is_cv_qualifier
= true;
15474 /* The `__complex__' keyword is a GNU extension. */
15482 /* Handle simple keywords. */
15487 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
15488 decl_specs
->any_specifiers_p
= true;
15490 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15493 /* If we do not already have a type-specifier, assume we are looking
15494 at a simple-type-specifier. */
15495 type_spec
= cp_parser_simple_type_specifier (parser
,
15499 /* If we didn't find a type-specifier, and a type-specifier was not
15500 optional in this context, issue an error message. */
15501 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15503 cp_parser_error (parser
, "expected type specifier");
15504 return error_mark_node
;
15510 /* Parse a simple-type-specifier.
15512 simple-type-specifier:
15513 :: [opt] nested-name-specifier [opt] type-name
15514 :: [opt] nested-name-specifier template template-id
15529 simple-type-specifier:
15531 decltype ( expression )
15534 __underlying_type ( type-id )
15538 simple-type-specifier:
15540 __typeof__ unary-expression
15541 __typeof__ ( type-id )
15542 __typeof__ ( type-id ) { initializer-list , [opt] }
15544 Concepts Extension:
15546 simple-type-specifier:
15547 constrained-type-specifier
15549 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15550 appropriately updated. */
15553 cp_parser_simple_type_specifier (cp_parser
* parser
,
15554 cp_decl_specifier_seq
*decl_specs
,
15555 cp_parser_flags flags
)
15557 tree type
= NULL_TREE
;
15561 /* Peek at the next token. */
15562 token
= cp_lexer_peek_token (parser
->lexer
);
15564 /* If we're looking at a keyword, things are easy. */
15565 switch (token
->keyword
)
15569 decl_specs
->explicit_char_p
= true;
15570 type
= char_type_node
;
15573 type
= char16_type_node
;
15576 type
= char32_type_node
;
15579 type
= wchar_type_node
;
15582 type
= boolean_type_node
;
15585 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
15586 type
= short_integer_type_node
;
15590 decl_specs
->explicit_int_p
= true;
15591 type
= integer_type_node
;
15597 idx
= token
->keyword
- RID_INT_N_0
;
15598 if (! int_n_enabled_p
[idx
])
15602 decl_specs
->explicit_intN_p
= true;
15603 decl_specs
->int_n_idx
= idx
;
15605 type
= int_n_trees
[idx
].signed_type
;
15609 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
15610 type
= long_integer_type_node
;
15613 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
15614 type
= integer_type_node
;
15617 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
15618 type
= unsigned_type_node
;
15621 type
= float_type_node
;
15624 type
= double_type_node
;
15627 type
= void_type_node
;
15631 maybe_warn_cpp0x (CPP0X_AUTO
);
15632 if (parser
->auto_is_implicit_function_template_parm_p
)
15634 /* The 'auto' might be the placeholder return type for a function decl
15635 with trailing return type. */
15636 bool have_trailing_return_fn_decl
= false;
15637 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15640 cp_parser_parse_tentatively (parser
);
15641 cp_lexer_consume_token (parser
->lexer
);
15642 cp_lexer_consume_token (parser
->lexer
);
15643 if (cp_parser_skip_to_closing_parenthesis (parser
,
15644 /*recovering*/false,
15646 /*consume_paren*/true))
15647 have_trailing_return_fn_decl
15648 = cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
);
15649 cp_parser_abort_tentative_parse (parser
);
15652 if (have_trailing_return_fn_decl
)
15654 type
= make_auto ();
15658 if (cxx_dialect
>= cxx14
)
15660 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
15661 type
= TREE_TYPE (type
);
15664 type
= error_mark_node
;
15666 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
15668 if (cxx_dialect
< cxx14
)
15669 error_at (token
->location
,
15670 "use of %<auto%> in lambda parameter declaration "
15671 "only available with "
15672 "-std=c++14 or -std=gnu++14");
15674 else if (cxx_dialect
< cxx14
)
15675 error_at (token
->location
,
15676 "use of %<auto%> in parameter declaration "
15677 "only available with "
15678 "-std=c++14 or -std=gnu++14");
15680 pedwarn (token
->location
, OPT_Wpedantic
,
15681 "ISO C++ forbids use of %<auto%> in parameter "
15685 type
= make_auto ();
15689 /* Since DR 743, decltype can either be a simple-type-specifier by
15690 itself or begin a nested-name-specifier. Parsing it will replace
15691 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15692 handling below decide what to do. */
15693 cp_parser_decltype (parser
);
15694 cp_lexer_set_token_position (parser
->lexer
, token
);
15698 /* Consume the `typeof' token. */
15699 cp_lexer_consume_token (parser
->lexer
);
15700 /* Parse the operand to `typeof'. */
15701 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
15702 /* If it is not already a TYPE, take its type. */
15703 if (!TYPE_P (type
))
15704 type
= finish_typeof (type
);
15707 cp_parser_set_decl_spec_type (decl_specs
, type
,
15709 /*type_definition_p=*/false);
15713 case RID_UNDERLYING_TYPE
:
15714 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
15716 cp_parser_set_decl_spec_type (decl_specs
, type
,
15718 /*type_definition_p=*/false);
15723 case RID_DIRECT_BASES
:
15724 type
= cp_parser_trait_expr (parser
, token
->keyword
);
15726 cp_parser_set_decl_spec_type (decl_specs
, type
,
15728 /*type_definition_p=*/false);
15734 /* If token is an already-parsed decltype not followed by ::,
15735 it's a simple-type-specifier. */
15736 if (token
->type
== CPP_DECLTYPE
15737 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
15739 type
= token
->u
.value
;
15742 cp_parser_set_decl_spec_type (decl_specs
, type
,
15744 /*type_definition_p=*/false);
15745 /* Remember that we are handling a decltype in order to
15746 implement the resolution of DR 1510 when the argument
15747 isn't instantiation dependent. */
15748 decl_specs
->decltype_p
= true;
15750 cp_lexer_consume_token (parser
->lexer
);
15754 /* If the type-specifier was for a built-in type, we're done. */
15757 /* Record the type. */
15759 && (token
->keyword
!= RID_SIGNED
15760 && token
->keyword
!= RID_UNSIGNED
15761 && token
->keyword
!= RID_SHORT
15762 && token
->keyword
!= RID_LONG
))
15763 cp_parser_set_decl_spec_type (decl_specs
,
15766 /*type_definition_p=*/false);
15768 decl_specs
->any_specifiers_p
= true;
15770 /* Consume the token. */
15771 cp_lexer_consume_token (parser
->lexer
);
15773 if (type
== error_mark_node
)
15774 return error_mark_node
;
15776 /* There is no valid C++ program where a non-template type is
15777 followed by a "<". That usually indicates that the user thought
15778 that the type was a template. */
15779 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
15782 return TYPE_NAME (type
);
15785 /* The type-specifier must be a user-defined type. */
15786 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
15791 /* Don't gobble tokens or issue error messages if this is an
15792 optional type-specifier. */
15793 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
15794 cp_parser_parse_tentatively (parser
);
15796 /* Look for the optional `::' operator. */
15798 = (cp_parser_global_scope_opt (parser
,
15799 /*current_scope_valid_p=*/false)
15801 /* Look for the nested-name specifier. */
15803 = (cp_parser_nested_name_specifier_opt (parser
,
15804 /*typename_keyword_p=*/false,
15805 /*check_dependency_p=*/true,
15807 /*is_declaration=*/false)
15809 token
= cp_lexer_peek_token (parser
->lexer
);
15810 /* If we have seen a nested-name-specifier, and the next token
15811 is `template', then we are using the template-id production. */
15813 && cp_parser_optional_template_keyword (parser
))
15815 /* Look for the template-id. */
15816 type
= cp_parser_template_id (parser
,
15817 /*template_keyword_p=*/true,
15818 /*check_dependency_p=*/true,
15820 /*is_declaration=*/false);
15821 /* If the template-id did not name a type, we are out of
15823 if (TREE_CODE (type
) != TYPE_DECL
)
15825 cp_parser_error (parser
, "expected template-id for type");
15829 /* Otherwise, look for a type-name. */
15831 type
= cp_parser_type_name (parser
);
15832 /* Keep track of all name-lookups performed in class scopes. */
15836 && TREE_CODE (type
) == TYPE_DECL
15837 && identifier_p (DECL_NAME (type
)))
15838 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15839 /* If it didn't work out, we don't have a TYPE. */
15840 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15841 && !cp_parser_parse_definitely (parser
))
15843 if (type
&& decl_specs
)
15844 cp_parser_set_decl_spec_type (decl_specs
, type
,
15846 /*type_definition_p=*/false);
15849 /* If we didn't get a type-name, issue an error message. */
15850 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15852 cp_parser_error (parser
, "expected type-name");
15853 return error_mark_node
;
15856 if (type
&& type
!= error_mark_node
)
15858 /* See if TYPE is an Objective-C type, and if so, parse and
15859 accept any protocol references following it. Do this before
15860 the cp_parser_check_for_invalid_template_id() call, because
15861 Objective-C types can be followed by '<...>' which would
15862 enclose protocol names rather than template arguments, and so
15863 everything is fine. */
15864 if (c_dialect_objc () && !parser
->scope
15865 && (objc_is_id (type
) || objc_is_class_name (type
)))
15867 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15868 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15870 /* Clobber the "unqualified" type previously entered into
15871 DECL_SPECS with the new, improved protocol-qualified version. */
15873 decl_specs
->type
= qual_type
;
15878 /* There is no valid C++ program where a non-template type is
15879 followed by a "<". That usually indicates that the user
15880 thought that the type was a template. */
15881 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15889 /* Parse a type-name.
15895 simple-template-id [in c++0x]
15912 Returns a TYPE_DECL for the type. */
15915 cp_parser_type_name (cp_parser
* parser
)
15917 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
15922 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
15926 /* We can't know yet whether it is a class-name or not. */
15927 cp_parser_parse_tentatively (parser
);
15928 /* Try a class-name. */
15929 type_decl
= cp_parser_class_name (parser
,
15930 typename_keyword_p
,
15931 /*template_keyword_p=*/false,
15933 /*check_dependency_p=*/true,
15934 /*class_head_p=*/false,
15935 /*is_declaration=*/false);
15936 /* If it's not a class-name, keep looking. */
15937 if (!cp_parser_parse_definitely (parser
))
15939 if (cxx_dialect
< cxx11
)
15940 /* It must be a typedef-name or an enum-name. */
15941 return cp_parser_nonclass_name (parser
);
15943 cp_parser_parse_tentatively (parser
);
15944 /* It is either a simple-template-id representing an
15945 instantiation of an alias template... */
15946 type_decl
= cp_parser_template_id (parser
,
15947 /*template_keyword_p=*/false,
15948 /*check_dependency_p=*/true,
15950 /*is_declaration=*/false);
15951 /* Note that this must be an instantiation of an alias template
15952 because [temp.names]/6 says:
15954 A template-id that names an alias template specialization
15957 Whereas [temp.names]/7 says:
15959 A simple-template-id that names a class template
15960 specialization is a class-name.
15962 With concepts, this could also be a partial-concept-id that
15963 declares a non-type template parameter. */
15964 if (type_decl
!= NULL_TREE
15965 && TREE_CODE (type_decl
) == TYPE_DECL
15966 && TYPE_DECL_ALIAS_P (type_decl
))
15967 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15968 else if (is_constrained_parameter (type_decl
))
15969 /* Don't do anything. */ ;
15971 cp_parser_simulate_error (parser
);
15973 if (!cp_parser_parse_definitely (parser
))
15974 /* ... Or a typedef-name or an enum-name. */
15975 return cp_parser_nonclass_name (parser
);
15981 /* Check if DECL and ARGS can form a constrained-type-specifier.
15982 If ARGS is non-null, we try to form a concept check of the
15983 form DECL<?, ARGS> where ? is a wildcard that matches any
15984 kind of template argument. If ARGS is NULL, then we try to
15985 form a concept check of the form DECL<?>. */
15988 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
15989 tree decl
, tree args
)
15991 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
15993 /* If we a constrained-type-specifier cannot be deduced. */
15994 if (parser
->prevent_constrained_type_specifiers
)
15997 /* A constrained type specifier can only be found in an
15998 overload set or as a reference to a template declaration.
16000 FIXME: This might be masking a bug. It's possible that
16001 that the deduction below is causing template specializations
16002 to be formed with the wildcard as an argument. */
16003 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
16006 /* Try to build a call expression that evaluates the
16007 concept. This can fail if the overload set refers
16008 only to non-templates. */
16009 tree placeholder
= build_nt (WILDCARD_DECL
);
16010 tree check
= build_concept_check (decl
, placeholder
, args
);
16011 if (check
== error_mark_node
)
16014 /* Deduce the checked constraint and the prototype parameter.
16016 FIXME: In certain cases, failure to deduce should be a
16017 diagnosable error. */
16020 if (!deduce_constrained_parameter (check
, conc
, proto
))
16023 /* In template parameter scope, this results in a constrained
16024 parameter. Return a descriptor of that parm. */
16025 if (processing_template_parmlist
)
16026 return build_constrained_parameter (conc
, proto
, args
);
16028 /* In a parameter-declaration-clause, constrained-type
16029 specifiers result in invented template parameters. */
16030 if (parser
->auto_is_implicit_function_template_parm_p
)
16032 tree x
= build_constrained_parameter (conc
, proto
, args
);
16033 return synthesize_implicit_template_parm (parser
, x
);
16037 /* Otherwise, we're in a context where the constrained
16038 type name is deduced and the constraint applies
16039 after deduction. */
16040 return make_constrained_auto (conc
, args
);
16046 /* If DECL refers to a concept, return a TYPE_DECL representing
16047 the result of using the constrained type specifier in the
16048 current context. DECL refers to a concept if
16050 - it is an overload set containing a function concept taking a single
16053 - it is a variable concept taking a single type argument. */
16056 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
16059 && (TREE_CODE (decl
) == OVERLOAD
16060 || BASELINK_P (decl
)
16061 || variable_concept_p (decl
)))
16062 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
16067 /* Check if DECL and ARGS form a partial-concept-id. If so,
16068 assign ID to the resulting constrained placeholder.
16070 Returns true if the partial-concept-id designates a placeholder
16071 and false otherwise. Note that *id is set to NULL_TREE in
16075 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
16077 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
16080 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16092 Returns a TYPE_DECL for the type. */
16095 cp_parser_nonclass_name (cp_parser
* parser
)
16100 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16101 identifier
= cp_parser_identifier (parser
);
16102 if (identifier
== error_mark_node
)
16103 return error_mark_node
;
16105 /* Look up the type-name. */
16106 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
16108 type_decl
= strip_using_decl (type_decl
);
16110 /* If we found an overload set, then it may refer to a concept-name. */
16111 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
16114 if (TREE_CODE (type_decl
) != TYPE_DECL
16115 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
16117 /* See if this is an Objective-C type. */
16118 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16119 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
16121 type_decl
= TYPE_NAME (type
);
16124 /* Issue an error if we did not find a type-name. */
16125 if (TREE_CODE (type_decl
) != TYPE_DECL
16126 /* In Objective-C, we have the complication that class names are
16127 normally type names and start declarations (eg, the
16128 "NSObject" in "NSObject *object;"), but can be used in an
16129 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16130 is an expression. So, a classname followed by a dot is not a
16131 valid type-name. */
16132 || (objc_is_class_name (TREE_TYPE (type_decl
))
16133 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
16135 if (!cp_parser_simulate_error (parser
))
16136 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
16137 NLE_TYPE
, token
->location
);
16138 return error_mark_node
;
16140 /* Remember that the name was used in the definition of the
16141 current class so that we can check later to see if the
16142 meaning would have been different after the class was
16143 entirely defined. */
16144 else if (type_decl
!= error_mark_node
16146 maybe_note_name_used_in_class (identifier
, type_decl
);
16151 /* Parse an elaborated-type-specifier. Note that the grammar given
16152 here incorporates the resolution to DR68.
16154 elaborated-type-specifier:
16155 class-key :: [opt] nested-name-specifier [opt] identifier
16156 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16157 enum-key :: [opt] nested-name-specifier [opt] identifier
16158 typename :: [opt] nested-name-specifier identifier
16159 typename :: [opt] nested-name-specifier template [opt]
16164 elaborated-type-specifier:
16165 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16166 class-key attributes :: [opt] nested-name-specifier [opt]
16167 template [opt] template-id
16168 enum attributes :: [opt] nested-name-specifier [opt] identifier
16170 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16171 declared `friend'. If IS_DECLARATION is TRUE, then this
16172 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16173 something is being declared.
16175 Returns the TYPE specified. */
16178 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
16180 bool is_declaration
)
16182 enum tag_types tag_type
;
16184 tree type
= NULL_TREE
;
16185 tree attributes
= NULL_TREE
;
16187 cp_token
*token
= NULL
;
16189 /* See if we're looking at the `enum' keyword. */
16190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
16192 /* Consume the `enum' token. */
16193 cp_lexer_consume_token (parser
->lexer
);
16194 /* Remember that it's an enumeration type. */
16195 tag_type
= enum_type
;
16196 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16197 enums) is used here. */
16198 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
16199 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
16201 pedwarn (input_location
, 0, "elaborated-type-specifier "
16202 "for a scoped enum must not use the %<%D%> keyword",
16203 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
16204 /* Consume the `struct' or `class' and parse it anyway. */
16205 cp_lexer_consume_token (parser
->lexer
);
16207 /* Parse the attributes. */
16208 attributes
= cp_parser_attributes_opt (parser
);
16210 /* Or, it might be `typename'. */
16211 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16214 /* Consume the `typename' token. */
16215 cp_lexer_consume_token (parser
->lexer
);
16216 /* Remember that it's a `typename' type. */
16217 tag_type
= typename_type
;
16219 /* Otherwise it must be a class-key. */
16222 tag_type
= cp_parser_class_key (parser
);
16223 if (tag_type
== none_type
)
16224 return error_mark_node
;
16225 /* Parse the attributes. */
16226 attributes
= cp_parser_attributes_opt (parser
);
16229 /* Look for the `::' operator. */
16230 globalscope
= cp_parser_global_scope_opt (parser
,
16231 /*current_scope_valid_p=*/false);
16232 /* Look for the nested-name-specifier. */
16233 if (tag_type
== typename_type
&& !globalscope
)
16235 if (!cp_parser_nested_name_specifier (parser
,
16236 /*typename_keyword_p=*/true,
16237 /*check_dependency_p=*/true,
16240 return error_mark_node
;
16243 /* Even though `typename' is not present, the proposed resolution
16244 to Core Issue 180 says that in `class A<T>::B', `B' should be
16245 considered a type-name, even if `A<T>' is dependent. */
16246 cp_parser_nested_name_specifier_opt (parser
,
16247 /*typename_keyword_p=*/true,
16248 /*check_dependency_p=*/true,
16251 /* For everything but enumeration types, consider a template-id.
16252 For an enumeration type, consider only a plain identifier. */
16253 if (tag_type
!= enum_type
)
16255 bool template_p
= false;
16258 /* Allow the `template' keyword. */
16259 template_p
= cp_parser_optional_template_keyword (parser
);
16260 /* If we didn't see `template', we don't know if there's a
16261 template-id or not. */
16263 cp_parser_parse_tentatively (parser
);
16264 /* Parse the template-id. */
16265 token
= cp_lexer_peek_token (parser
->lexer
);
16266 decl
= cp_parser_template_id (parser
, template_p
,
16267 /*check_dependency_p=*/true,
16270 /* If we didn't find a template-id, look for an ordinary
16272 if (!template_p
&& !cp_parser_parse_definitely (parser
))
16274 /* We can get here when cp_parser_template_id, called by
16275 cp_parser_class_name with tag_type == none_type, succeeds
16276 and caches a BASELINK. Then, when called again here,
16277 instead of failing and returning an error_mark_node
16278 returns it (see template/typename17.C in C++11).
16279 ??? Could we diagnose this earlier? */
16280 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
16282 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
16283 type
= error_mark_node
;
16285 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16286 in effect, then we must assume that, upon instantiation, the
16287 template will correspond to a class. */
16288 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16289 && tag_type
== typename_type
)
16290 type
= make_typename_type (parser
->scope
, decl
,
16292 /*complain=*/tf_error
);
16293 /* If the `typename' keyword is in effect and DECL is not a type
16294 decl, then type is non existent. */
16295 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
16297 else if (TREE_CODE (decl
) == TYPE_DECL
)
16298 type
= check_elaborated_type_specifier (tag_type
, decl
,
16299 /*allow_template_p=*/true);
16300 else if (decl
== error_mark_node
)
16301 type
= error_mark_node
;
16306 token
= cp_lexer_peek_token (parser
->lexer
);
16307 identifier
= cp_parser_identifier (parser
);
16309 if (identifier
== error_mark_node
)
16311 parser
->scope
= NULL_TREE
;
16312 return error_mark_node
;
16315 /* For a `typename', we needn't call xref_tag. */
16316 if (tag_type
== typename_type
16317 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
16318 return cp_parser_make_typename_type (parser
, identifier
,
16321 /* Template parameter lists apply only if we are not within a
16322 function parameter list. */
16323 bool template_parm_lists_apply
16324 = parser
->num_template_parameter_lists
;
16325 if (template_parm_lists_apply
)
16326 for (cp_binding_level
*s
= current_binding_level
;
16327 s
&& s
->kind
!= sk_template_parms
;
16328 s
= s
->level_chain
)
16329 if (s
->kind
== sk_function_parms
)
16330 template_parm_lists_apply
= false;
16332 /* Look up a qualified name in the usual way. */
16336 tree ambiguous_decls
;
16338 decl
= cp_parser_lookup_name (parser
, identifier
,
16340 /*is_template=*/false,
16341 /*is_namespace=*/false,
16342 /*check_dependency=*/true,
16346 /* If the lookup was ambiguous, an error will already have been
16348 if (ambiguous_decls
)
16349 return error_mark_node
;
16351 /* If we are parsing friend declaration, DECL may be a
16352 TEMPLATE_DECL tree node here. However, we need to check
16353 whether this TEMPLATE_DECL results in valid code. Consider
16354 the following example:
16357 template <class T> class C {};
16360 template <class T> friend class N::C; // #1, valid code
16362 template <class T> class Y {
16363 friend class N::C; // #2, invalid code
16366 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16367 name lookup of `N::C'. We see that friend declaration must
16368 be template for the code to be valid. Note that
16369 processing_template_decl does not work here since it is
16370 always 1 for the above two cases. */
16372 decl
= (cp_parser_maybe_treat_template_as_class
16373 (decl
, /*tag_name_p=*/is_friend
16374 && template_parm_lists_apply
));
16376 if (TREE_CODE (decl
) != TYPE_DECL
)
16378 cp_parser_diagnose_invalid_type_name (parser
,
16381 return error_mark_node
;
16384 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
16386 bool allow_template
= (template_parm_lists_apply
16387 || DECL_SELF_REFERENCE_P (decl
));
16388 type
= check_elaborated_type_specifier (tag_type
, decl
,
16391 if (type
== error_mark_node
)
16392 return error_mark_node
;
16395 /* Forward declarations of nested types, such as
16400 are invalid unless all components preceding the final '::'
16401 are complete. If all enclosing types are complete, these
16402 declarations become merely pointless.
16404 Invalid forward declarations of nested types are errors
16405 caught elsewhere in parsing. Those that are pointless arrive
16408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16409 && !is_friend
&& !processing_explicit_instantiation
)
16410 warning (0, "declaration %qD does not declare anything", decl
);
16412 type
= TREE_TYPE (decl
);
16416 /* An elaborated-type-specifier sometimes introduces a new type and
16417 sometimes names an existing type. Normally, the rule is that it
16418 introduces a new type only if there is not an existing type of
16419 the same name already in scope. For example, given:
16422 void f() { struct S s; }
16424 the `struct S' in the body of `f' is the same `struct S' as in
16425 the global scope; the existing definition is used. However, if
16426 there were no global declaration, this would introduce a new
16427 local class named `S'.
16429 An exception to this rule applies to the following code:
16431 namespace N { struct S; }
16433 Here, the elaborated-type-specifier names a new type
16434 unconditionally; even if there is already an `S' in the
16435 containing scope this declaration names a new type.
16436 This exception only applies if the elaborated-type-specifier
16437 forms the complete declaration:
16441 A declaration consisting solely of `class-key identifier ;' is
16442 either a redeclaration of the name in the current scope or a
16443 forward declaration of the identifier as a class name. It
16444 introduces the name into the current scope.
16446 We are in this situation precisely when the next token is a `;'.
16448 An exception to the exception is that a `friend' declaration does
16449 *not* name a new type; i.e., given:
16451 struct S { friend struct T; };
16453 `T' is not a new type in the scope of `S'.
16455 Also, `new struct S' or `sizeof (struct S)' never results in the
16456 definition of a new type; a new type can only be declared in a
16457 declaration context. */
16463 /* Friends have special name lookup rules. */
16464 ts
= ts_within_enclosing_non_class
;
16465 else if (is_declaration
16466 && cp_lexer_next_token_is (parser
->lexer
,
16468 /* This is a `class-key identifier ;' */
16474 (template_parm_lists_apply
16475 && (cp_parser_next_token_starts_class_definition_p (parser
)
16476 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
16477 /* An unqualified name was used to reference this type, so
16478 there were no qualifying templates. */
16479 if (template_parm_lists_apply
16480 && !cp_parser_check_template_parameters (parser
,
16481 /*num_templates=*/0,
16483 /*declarator=*/NULL
))
16484 return error_mark_node
;
16485 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
16489 if (type
== error_mark_node
)
16490 return error_mark_node
;
16492 /* Allow attributes on forward declarations of classes. */
16495 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16496 warning (OPT_Wattributes
,
16497 "attributes ignored on uninstantiated type");
16498 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
16499 && ! processing_explicit_instantiation
)
16500 warning (OPT_Wattributes
,
16501 "attributes ignored on template instantiation");
16502 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
16503 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
16505 warning (OPT_Wattributes
,
16506 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16509 if (tag_type
!= enum_type
)
16511 /* Indicate whether this class was declared as a `class' or as a
16513 if (TREE_CODE (type
) == RECORD_TYPE
)
16514 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
16515 cp_parser_check_class_key (tag_type
, type
);
16518 /* A "<" cannot follow an elaborated type specifier. If that
16519 happens, the user was probably trying to form a template-id. */
16520 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
16526 /* Parse an enum-specifier.
16529 enum-head { enumerator-list [opt] }
16530 enum-head { enumerator-list , } [C++0x]
16533 enum-key identifier [opt] enum-base [opt]
16534 enum-key nested-name-specifier identifier enum-base [opt]
16539 enum struct [C++0x]
16542 : type-specifier-seq
16544 opaque-enum-specifier:
16545 enum-key identifier enum-base [opt] ;
16548 enum-key attributes[opt] identifier [opt] enum-base [opt]
16549 { enumerator-list [opt] }attributes[opt]
16550 enum-key attributes[opt] identifier [opt] enum-base [opt]
16551 { enumerator-list, }attributes[opt] [C++0x]
16553 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16554 if the token stream isn't an enum-specifier after all. */
16557 cp_parser_enum_specifier (cp_parser
* parser
)
16560 tree type
= NULL_TREE
;
16562 tree nested_name_specifier
= NULL_TREE
;
16564 bool scoped_enum_p
= false;
16565 bool has_underlying_type
= false;
16566 bool nested_being_defined
= false;
16567 bool new_value_list
= false;
16568 bool is_new_type
= false;
16569 bool is_anonymous
= false;
16570 tree underlying_type
= NULL_TREE
;
16571 cp_token
*type_start_token
= NULL
;
16572 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
16574 parser
->colon_corrects_to_scope_p
= false;
16576 /* Parse tentatively so that we can back up if we don't find a
16578 cp_parser_parse_tentatively (parser
);
16580 /* Caller guarantees that the current token is 'enum', an identifier
16581 possibly follows, and the token after that is an opening brace.
16582 If we don't have an identifier, fabricate an anonymous name for
16583 the enumeration being defined. */
16584 cp_lexer_consume_token (parser
->lexer
);
16586 /* Parse the "class" or "struct", which indicates a scoped
16587 enumeration type in C++0x. */
16588 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
16589 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
16591 if (cxx_dialect
< cxx11
)
16592 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
16594 /* Consume the `struct' or `class' token. */
16595 cp_lexer_consume_token (parser
->lexer
);
16597 scoped_enum_p
= true;
16600 attributes
= cp_parser_attributes_opt (parser
);
16602 /* Clear the qualification. */
16603 parser
->scope
= NULL_TREE
;
16604 parser
->qualifying_scope
= NULL_TREE
;
16605 parser
->object_scope
= NULL_TREE
;
16607 /* Figure out in what scope the declaration is being placed. */
16608 prev_scope
= current_scope ();
16610 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16612 push_deferring_access_checks (dk_no_check
);
16613 nested_name_specifier
16614 = cp_parser_nested_name_specifier_opt (parser
,
16615 /*typename_keyword_p=*/true,
16616 /*check_dependency_p=*/false,
16618 /*is_declaration=*/false);
16620 if (nested_name_specifier
)
16624 identifier
= cp_parser_identifier (parser
);
16625 name
= cp_parser_lookup_name (parser
, identifier
,
16627 /*is_template=*/false,
16628 /*is_namespace=*/false,
16629 /*check_dependency=*/true,
16630 /*ambiguous_decls=*/NULL
,
16632 if (name
&& name
!= error_mark_node
)
16634 type
= TREE_TYPE (name
);
16635 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16637 /* Are template enums allowed in ISO? */
16638 if (template_parm_scope_p ())
16639 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
16640 "%qD is an enumeration template", name
);
16641 /* ignore a typename reference, for it will be solved by name
16646 else if (nested_name_specifier
== error_mark_node
)
16647 /* We already issued an error. */;
16650 error_at (type_start_token
->location
,
16651 "%qD does not name an enumeration in %qT",
16652 identifier
, nested_name_specifier
);
16653 nested_name_specifier
= error_mark_node
;
16658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16659 identifier
= cp_parser_identifier (parser
);
16662 identifier
= make_anon_name ();
16663 is_anonymous
= true;
16665 error_at (type_start_token
->location
,
16666 "anonymous scoped enum is not allowed");
16669 pop_deferring_access_checks ();
16671 /* Check for the `:' that denotes a specified underlying type in C++0x.
16672 Note that a ':' could also indicate a bitfield width, however. */
16673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16675 cp_decl_specifier_seq type_specifiers
;
16677 /* Consume the `:'. */
16678 cp_lexer_consume_token (parser
->lexer
);
16680 /* Parse the type-specifier-seq. */
16681 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
16682 /*is_trailing_return=*/false,
16685 /* At this point this is surely not elaborated type specifier. */
16686 if (!cp_parser_parse_definitely (parser
))
16689 if (cxx_dialect
< cxx11
)
16690 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
16692 has_underlying_type
= true;
16694 /* If that didn't work, stop. */
16695 if (type_specifiers
.type
!= error_mark_node
)
16697 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
16698 /*initialized=*/0, NULL
);
16699 if (underlying_type
== error_mark_node
16700 || check_for_bare_parameter_packs (underlying_type
))
16701 underlying_type
= NULL_TREE
;
16705 /* Look for the `{' but don't consume it yet. */
16706 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16708 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
16710 cp_parser_error (parser
, "expected %<{%>");
16711 if (has_underlying_type
)
16717 /* An opaque-enum-specifier must have a ';' here. */
16718 if ((scoped_enum_p
|| underlying_type
)
16719 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16721 cp_parser_error (parser
, "expected %<;%> or %<{%>");
16722 if (has_underlying_type
)
16730 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
16733 if (nested_name_specifier
)
16735 if (CLASS_TYPE_P (nested_name_specifier
))
16737 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
16738 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
16739 push_scope (nested_name_specifier
);
16741 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16743 push_nested_namespace (nested_name_specifier
);
16747 /* Issue an error message if type-definitions are forbidden here. */
16748 if (!cp_parser_check_type_definition (parser
))
16749 type
= error_mark_node
;
16751 /* Create the new type. We do this before consuming the opening
16752 brace so the enum will be recorded as being on the line of its
16753 tag (or the 'enum' keyword, if there is no tag). */
16754 type
= start_enum (identifier
, type
, underlying_type
,
16755 scoped_enum_p
, &is_new_type
);
16757 /* If the next token is not '{' it is an opaque-enum-specifier or an
16758 elaborated-type-specifier. */
16759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16761 timevar_push (TV_PARSE_ENUM
);
16762 if (nested_name_specifier
16763 && nested_name_specifier
!= error_mark_node
)
16765 /* The following catches invalid code such as:
16766 enum class S<int>::E { A, B, C }; */
16767 if (!processing_specialization
16768 && CLASS_TYPE_P (nested_name_specifier
)
16769 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
16770 error_at (type_start_token
->location
, "cannot add an enumerator "
16771 "list to a template instantiation");
16773 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
16775 error_at (type_start_token
->location
,
16776 "%<%T::%E%> has not been declared",
16777 TYPE_CONTEXT (nested_name_specifier
),
16778 nested_name_specifier
);
16779 type
= error_mark_node
;
16781 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
16782 && !CLASS_TYPE_P (nested_name_specifier
))
16784 error_at (type_start_token
->location
, "nested name specifier "
16785 "%qT for enum declaration does not name a class "
16786 "or namespace", nested_name_specifier
);
16787 type
= error_mark_node
;
16789 /* If that scope does not contain the scope in which the
16790 class was originally declared, the program is invalid. */
16791 else if (prev_scope
&& !is_ancestor (prev_scope
,
16792 nested_name_specifier
))
16794 if (at_namespace_scope_p ())
16795 error_at (type_start_token
->location
,
16796 "declaration of %qD in namespace %qD which does not "
16798 type
, prev_scope
, nested_name_specifier
);
16800 error_at (type_start_token
->location
,
16801 "declaration of %qD in %qD which does not "
16803 type
, prev_scope
, nested_name_specifier
);
16804 type
= error_mark_node
;
16809 begin_scope (sk_scoped_enum
, type
);
16811 /* Consume the opening brace. */
16812 cp_lexer_consume_token (parser
->lexer
);
16814 if (type
== error_mark_node
)
16815 ; /* Nothing to add */
16816 else if (OPAQUE_ENUM_P (type
)
16817 || (cxx_dialect
> cxx98
&& processing_specialization
))
16819 new_value_list
= true;
16820 SET_OPAQUE_ENUM_P (type
, false);
16821 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
16825 error_at (type_start_token
->location
,
16826 "multiple definition of %q#T", type
);
16827 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
16828 "previous definition here");
16829 type
= error_mark_node
;
16832 if (type
== error_mark_node
)
16833 cp_parser_skip_to_end_of_block_or_statement (parser
);
16834 /* If the next token is not '}', then there are some enumerators. */
16835 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16837 if (is_anonymous
&& !scoped_enum_p
)
16838 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
16839 "ISO C++ forbids empty anonymous enum");
16842 cp_parser_enumerator_list (parser
, type
);
16844 /* Consume the final '}'. */
16845 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16849 timevar_pop (TV_PARSE_ENUM
);
16853 /* If a ';' follows, then it is an opaque-enum-specifier
16854 and additional restrictions apply. */
16855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16858 error_at (type_start_token
->location
,
16859 "opaque-enum-specifier without name");
16860 else if (nested_name_specifier
)
16861 error_at (type_start_token
->location
,
16862 "opaque-enum-specifier must use a simple identifier");
16866 /* Look for trailing attributes to apply to this enumeration, and
16867 apply them if appropriate. */
16868 if (cp_parser_allow_gnu_extensions_p (parser
))
16870 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
16871 trailing_attr
= chainon (trailing_attr
, attributes
);
16872 cplus_decl_attributes (&type
,
16874 (int) ATTR_FLAG_TYPE_IN_PLACE
);
16877 /* Finish up the enumeration. */
16878 if (type
!= error_mark_node
)
16880 if (new_value_list
)
16881 finish_enum_value_list (type
);
16883 finish_enum (type
);
16886 if (nested_name_specifier
)
16888 if (CLASS_TYPE_P (nested_name_specifier
))
16890 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
16891 pop_scope (nested_name_specifier
);
16893 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16895 pop_nested_namespace (nested_name_specifier
);
16899 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
16903 /* Parse an enumerator-list. The enumerators all have the indicated
16907 enumerator-definition
16908 enumerator-list , enumerator-definition */
16911 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
16915 /* Parse an enumerator-definition. */
16916 cp_parser_enumerator_definition (parser
, type
);
16918 /* If the next token is not a ',', we've reached the end of
16920 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16922 /* Otherwise, consume the `,' and keep going. */
16923 cp_lexer_consume_token (parser
->lexer
);
16924 /* If the next token is a `}', there is a trailing comma. */
16925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16927 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
16928 pedwarn (input_location
, OPT_Wpedantic
,
16929 "comma at end of enumerator list");
16935 /* Parse an enumerator-definition. The enumerator has the indicated
16938 enumerator-definition:
16940 enumerator = constant-expression
16947 enumerator-definition:
16948 enumerator attributes [opt]
16949 enumerator attributes [opt] = constant-expression */
16952 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16958 /* Save the input location because we are interested in the location
16959 of the identifier and not the location of the explicit value. */
16960 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16962 /* Look for the identifier. */
16963 identifier
= cp_parser_identifier (parser
);
16964 if (identifier
== error_mark_node
)
16967 /* Parse any specified attributes. */
16968 tree attrs
= cp_parser_attributes_opt (parser
);
16970 /* If the next token is an '=', then there is an explicit value. */
16971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16973 /* Consume the `=' token. */
16974 cp_lexer_consume_token (parser
->lexer
);
16975 /* Parse the value. */
16976 value
= cp_parser_constant_expression (parser
);
16981 /* If we are processing a template, make sure the initializer of the
16982 enumerator doesn't contain any bare template parameter pack. */
16983 if (check_for_bare_parameter_packs (value
))
16984 value
= error_mark_node
;
16986 /* Create the enumerator. */
16987 build_enumerator (identifier
, value
, type
, attrs
, loc
);
16990 /* Parse a namespace-name.
16993 original-namespace-name
16996 Returns the NAMESPACE_DECL for the namespace. */
16999 cp_parser_namespace_name (cp_parser
* parser
)
17002 tree namespace_decl
;
17004 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17006 /* Get the name of the namespace. */
17007 identifier
= cp_parser_identifier (parser
);
17008 if (identifier
== error_mark_node
)
17009 return error_mark_node
;
17011 /* Look up the identifier in the currently active scope. Look only
17012 for namespaces, due to:
17014 [basic.lookup.udir]
17016 When looking up a namespace-name in a using-directive or alias
17017 definition, only namespace names are considered.
17021 [basic.lookup.qual]
17023 During the lookup of a name preceding the :: scope resolution
17024 operator, object, function, and enumerator names are ignored.
17026 (Note that cp_parser_qualifying_entity only calls this
17027 function if the token after the name is the scope resolution
17029 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
17031 /*is_template=*/false,
17032 /*is_namespace=*/true,
17033 /*check_dependency=*/true,
17034 /*ambiguous_decls=*/NULL
,
17036 /* If it's not a namespace, issue an error. */
17037 if (namespace_decl
== error_mark_node
17038 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
17040 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17041 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
17042 cp_parser_error (parser
, "expected namespace-name");
17043 namespace_decl
= error_mark_node
;
17046 return namespace_decl
;
17049 /* Parse a namespace-definition.
17051 namespace-definition:
17052 named-namespace-definition
17053 unnamed-namespace-definition
17055 named-namespace-definition:
17056 original-namespace-definition
17057 extension-namespace-definition
17059 original-namespace-definition:
17060 namespace identifier { namespace-body }
17062 extension-namespace-definition:
17063 namespace original-namespace-name { namespace-body }
17065 unnamed-namespace-definition:
17066 namespace { namespace-body } */
17069 cp_parser_namespace_definition (cp_parser
* parser
)
17071 tree identifier
, attribs
;
17072 bool has_visibility
;
17075 int nested_definition_count
= 0;
17077 cp_ensure_no_omp_declare_simd (parser
);
17078 cp_ensure_no_oacc_routine (parser
);
17079 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
17081 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
17083 cp_lexer_consume_token (parser
->lexer
);
17088 /* Look for the `namespace' keyword. */
17089 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17091 /* Parse any specified attributes before the identifier. */
17092 attribs
= cp_parser_attributes_opt (parser
);
17094 /* Get the name of the namespace. We do not attempt to distinguish
17095 between an original-namespace-definition and an
17096 extension-namespace-definition at this point. The semantic
17097 analysis routines are responsible for that. */
17098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17099 identifier
= cp_parser_identifier (parser
);
17101 identifier
= NULL_TREE
;
17103 /* Parse any specified attributes after the identifier. */
17104 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
17105 if (post_ident_attribs
)
17108 attribs
= chainon (attribs
, post_ident_attribs
);
17110 attribs
= post_ident_attribs
;
17113 /* Start the namespace. */
17114 push_namespace (identifier
);
17116 /* Parse any nested namespace definition. */
17117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17120 error_at (token
->location
, "a nested namespace definition cannot have attributes");
17121 if (cxx_dialect
< cxx1z
)
17122 pedwarn (input_location
, OPT_Wpedantic
,
17123 "nested namespace definitions only available with "
17124 "-std=c++1z or -std=gnu++1z");
17126 error_at (token
->location
, "a nested namespace definition cannot be inline");
17127 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17129 cp_lexer_consume_token (parser
->lexer
);
17130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17131 identifier
= cp_parser_identifier (parser
);
17134 cp_parser_error (parser
, "nested identifier required");
17137 ++nested_definition_count
;
17138 push_namespace (identifier
);
17142 /* Look for the `{' to validate starting the namespace. */
17143 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
17145 /* "inline namespace" is equivalent to a stub namespace definition
17146 followed by a strong using directive. */
17149 tree name_space
= current_namespace
;
17150 /* Set up namespace association. */
17151 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
17152 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
17153 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
17154 /* Import the contents of the inline namespace. */
17156 do_using_directive (name_space
);
17157 push_namespace (identifier
);
17160 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
17162 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
17164 /* Parse the body of the namespace. */
17165 cp_parser_namespace_body (parser
);
17167 if (has_visibility
)
17168 pop_visibility (1);
17170 /* Finish the nested namespace definitions. */
17171 while (nested_definition_count
--)
17174 /* Finish the namespace. */
17176 /* Look for the final `}'. */
17177 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17180 /* Parse a namespace-body.
17183 declaration-seq [opt] */
17186 cp_parser_namespace_body (cp_parser
* parser
)
17188 cp_parser_declaration_seq_opt (parser
);
17191 /* Parse a namespace-alias-definition.
17193 namespace-alias-definition:
17194 namespace identifier = qualified-namespace-specifier ; */
17197 cp_parser_namespace_alias_definition (cp_parser
* parser
)
17200 tree namespace_specifier
;
17202 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17204 /* Look for the `namespace' keyword. */
17205 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17206 /* Look for the identifier. */
17207 identifier
= cp_parser_identifier (parser
);
17208 if (identifier
== error_mark_node
)
17210 /* Look for the `=' token. */
17211 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
17212 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17214 error_at (token
->location
, "%<namespace%> definition is not allowed here");
17215 /* Skip the definition. */
17216 cp_lexer_consume_token (parser
->lexer
);
17217 if (cp_parser_skip_to_closing_brace (parser
))
17218 cp_lexer_consume_token (parser
->lexer
);
17221 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17222 /* Look for the qualified-namespace-specifier. */
17223 namespace_specifier
17224 = cp_parser_qualified_namespace_specifier (parser
);
17225 /* Look for the `;' token. */
17226 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17228 /* Register the alias in the symbol table. */
17229 do_namespace_alias (identifier
, namespace_specifier
);
17232 /* Parse a qualified-namespace-specifier.
17234 qualified-namespace-specifier:
17235 :: [opt] nested-name-specifier [opt] namespace-name
17237 Returns a NAMESPACE_DECL corresponding to the specified
17241 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
17243 /* Look for the optional `::'. */
17244 cp_parser_global_scope_opt (parser
,
17245 /*current_scope_valid_p=*/false);
17247 /* Look for the optional nested-name-specifier. */
17248 cp_parser_nested_name_specifier_opt (parser
,
17249 /*typename_keyword_p=*/false,
17250 /*check_dependency_p=*/true,
17252 /*is_declaration=*/true);
17254 return cp_parser_namespace_name (parser
);
17257 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17258 access declaration.
17261 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17262 using :: unqualified-id ;
17264 access-declaration:
17270 cp_parser_using_declaration (cp_parser
* parser
,
17271 bool access_declaration_p
)
17274 bool typename_p
= false;
17275 bool global_scope_p
;
17279 int oldcount
= errorcount
;
17280 cp_token
*diag_token
= NULL
;
17282 if (access_declaration_p
)
17284 diag_token
= cp_lexer_peek_token (parser
->lexer
);
17285 cp_parser_parse_tentatively (parser
);
17289 /* Look for the `using' keyword. */
17290 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17292 /* Peek at the next token. */
17293 token
= cp_lexer_peek_token (parser
->lexer
);
17294 /* See if it's `typename'. */
17295 if (token
->keyword
== RID_TYPENAME
)
17297 /* Remember that we've seen it. */
17299 /* Consume the `typename' token. */
17300 cp_lexer_consume_token (parser
->lexer
);
17304 /* Look for the optional global scope qualification. */
17306 = (cp_parser_global_scope_opt (parser
,
17307 /*current_scope_valid_p=*/false)
17310 /* If we saw `typename', or didn't see `::', then there must be a
17311 nested-name-specifier present. */
17312 if (typename_p
|| !global_scope_p
)
17314 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
17315 /*check_dependency_p=*/true,
17317 /*is_declaration=*/true);
17318 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
17320 cp_parser_skip_to_end_of_block_or_statement (parser
);
17324 /* Otherwise, we could be in either of the two productions. In that
17325 case, treat the nested-name-specifier as optional. */
17327 qscope
= cp_parser_nested_name_specifier_opt (parser
,
17328 /*typename_keyword_p=*/false,
17329 /*check_dependency_p=*/true,
17331 /*is_declaration=*/true);
17333 qscope
= global_namespace
;
17334 else if (UNSCOPED_ENUM_P (qscope
))
17335 qscope
= CP_TYPE_CONTEXT (qscope
);
17337 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
17338 /* Something has already gone wrong; there's no need to parse
17339 further. Since an error has occurred, the return value of
17340 cp_parser_parse_definitely will be false, as required. */
17341 return cp_parser_parse_definitely (parser
);
17343 token
= cp_lexer_peek_token (parser
->lexer
);
17344 /* Parse the unqualified-id. */
17345 identifier
= cp_parser_unqualified_id (parser
,
17346 /*template_keyword_p=*/false,
17347 /*check_dependency_p=*/true,
17348 /*declarator_p=*/true,
17349 /*optional_p=*/false);
17351 if (access_declaration_p
)
17353 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17354 cp_parser_simulate_error (parser
);
17355 if (!cp_parser_parse_definitely (parser
))
17359 /* The function we call to handle a using-declaration is different
17360 depending on what scope we are in. */
17361 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
17363 else if (!identifier_p (identifier
)
17364 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
17365 /* [namespace.udecl]
17367 A using declaration shall not name a template-id. */
17368 error_at (token
->location
,
17369 "a template-id may not appear in a using-declaration");
17372 if (at_class_scope_p ())
17374 /* Create the USING_DECL. */
17375 decl
= do_class_using_decl (parser
->scope
, identifier
);
17377 if (decl
&& typename_p
)
17378 USING_DECL_TYPENAME_P (decl
) = 1;
17380 if (check_for_bare_parameter_packs (decl
))
17382 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17386 /* Add it to the list of members in this class. */
17387 finish_member_declaration (decl
);
17391 decl
= cp_parser_lookup_name_simple (parser
,
17394 if (decl
== error_mark_node
)
17395 cp_parser_name_lookup_error (parser
, identifier
,
17398 else if (check_for_bare_parameter_packs (decl
))
17400 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17403 else if (!at_namespace_scope_p ())
17404 do_local_using_decl (decl
, qscope
, identifier
);
17406 do_toplevel_using_decl (decl
, qscope
, identifier
);
17410 /* Look for the final `;'. */
17411 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17413 if (access_declaration_p
&& errorcount
== oldcount
)
17414 warning_at (diag_token
->location
, OPT_Wdeprecated
,
17415 "access declarations are deprecated "
17416 "in favour of using-declarations; "
17417 "suggestion: add the %<using%> keyword");
17422 /* Parse an alias-declaration.
17425 using identifier attribute-specifier-seq [opt] = type-id */
17428 cp_parser_alias_declaration (cp_parser
* parser
)
17430 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
17431 location_t id_location
;
17432 cp_declarator
*declarator
;
17433 cp_decl_specifier_seq decl_specs
;
17435 const char *saved_message
= NULL
;
17437 /* Look for the `using' keyword. */
17438 cp_token
*using_token
17439 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17440 if (using_token
== NULL
)
17441 return error_mark_node
;
17443 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17444 id
= cp_parser_identifier (parser
);
17445 if (id
== error_mark_node
)
17446 return error_mark_node
;
17448 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
17449 attributes
= cp_parser_attributes_opt (parser
);
17450 if (attributes
== error_mark_node
)
17451 return error_mark_node
;
17453 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17455 if (cp_parser_error_occurred (parser
))
17456 return error_mark_node
;
17458 cp_parser_commit_to_tentative_parse (parser
);
17460 /* Now we are going to parse the type-id of the declaration. */
17465 "A type-specifier-seq shall not define a class or enumeration
17466 unless it appears in the type-id of an alias-declaration (7.1.3) that
17467 is not the declaration of a template-declaration."
17469 In other words, if we currently are in an alias template, the
17470 type-id should not define a type.
17472 So let's set parser->type_definition_forbidden_message in that
17473 case; cp_parser_check_type_definition (called by
17474 cp_parser_class_specifier) will then emit an error if a type is
17475 defined in the type-id. */
17476 if (parser
->num_template_parameter_lists
)
17478 saved_message
= parser
->type_definition_forbidden_message
;
17479 parser
->type_definition_forbidden_message
=
17480 G_("types may not be defined in alias template declarations");
17483 type
= cp_parser_type_id (parser
);
17485 /* Restore the error message if need be. */
17486 if (parser
->num_template_parameter_lists
)
17487 parser
->type_definition_forbidden_message
= saved_message
;
17489 if (type
== error_mark_node
17490 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
17492 cp_parser_skip_to_end_of_block_or_statement (parser
);
17493 return error_mark_node
;
17496 /* A typedef-name can also be introduced by an alias-declaration. The
17497 identifier following the using keyword becomes a typedef-name. It has
17498 the same semantics as if it were introduced by the typedef
17499 specifier. In particular, it does not define a new type and it shall
17500 not appear in the type-id. */
17502 clear_decl_specs (&decl_specs
);
17503 decl_specs
.type
= type
;
17504 if (attributes
!= NULL_TREE
)
17506 decl_specs
.attributes
= attributes
;
17507 set_and_check_decl_spec_loc (&decl_specs
,
17511 set_and_check_decl_spec_loc (&decl_specs
,
17514 set_and_check_decl_spec_loc (&decl_specs
,
17518 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
17519 declarator
->id_loc
= id_location
;
17521 member_p
= at_class_scope_p ();
17523 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
17524 NULL_TREE
, attributes
);
17526 decl
= start_decl (declarator
, &decl_specs
, 0,
17527 attributes
, NULL_TREE
, &pushed_scope
);
17528 if (decl
== error_mark_node
)
17531 // Attach constraints to the alias declaration.
17532 if (flag_concepts
&& current_template_parms
)
17534 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
17535 tree constr
= build_constraints (reqs
, NULL_TREE
);
17536 set_constraints (decl
, constr
);
17539 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
17542 pop_scope (pushed_scope
);
17544 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17545 added into the symbol table; otherwise, return the TYPE_DECL. */
17546 if (DECL_LANG_SPECIFIC (decl
)
17547 && DECL_TEMPLATE_INFO (decl
)
17548 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
17550 decl
= DECL_TI_TEMPLATE (decl
);
17552 check_member_template (decl
);
17558 /* Parse a using-directive.
17561 using namespace :: [opt] nested-name-specifier [opt]
17562 namespace-name ; */
17565 cp_parser_using_directive (cp_parser
* parser
)
17567 tree namespace_decl
;
17570 /* Look for the `using' keyword. */
17571 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17572 /* And the `namespace' keyword. */
17573 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17574 /* Look for the optional `::' operator. */
17575 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
17576 /* And the optional nested-name-specifier. */
17577 cp_parser_nested_name_specifier_opt (parser
,
17578 /*typename_keyword_p=*/false,
17579 /*check_dependency_p=*/true,
17581 /*is_declaration=*/true);
17582 /* Get the namespace being used. */
17583 namespace_decl
= cp_parser_namespace_name (parser
);
17584 /* And any specified attributes. */
17585 attribs
= cp_parser_attributes_opt (parser
);
17586 /* Update the symbol table. */
17587 parse_using_directive (namespace_decl
, attribs
);
17588 /* Look for the final `;'. */
17589 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17592 /* Parse an asm-definition.
17595 asm ( string-literal ) ;
17600 asm volatile [opt] ( string-literal ) ;
17601 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17602 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17603 : asm-operand-list [opt] ) ;
17604 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17605 : asm-operand-list [opt]
17606 : asm-clobber-list [opt] ) ;
17607 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17608 : asm-clobber-list [opt]
17609 : asm-goto-list ) ; */
17612 cp_parser_asm_definition (cp_parser
* parser
)
17615 tree outputs
= NULL_TREE
;
17616 tree inputs
= NULL_TREE
;
17617 tree clobbers
= NULL_TREE
;
17618 tree labels
= NULL_TREE
;
17620 bool volatile_p
= false;
17621 bool extended_p
= false;
17622 bool invalid_inputs_p
= false;
17623 bool invalid_outputs_p
= false;
17624 bool goto_p
= false;
17625 required_token missing
= RT_NONE
;
17627 /* Look for the `asm' keyword. */
17628 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
17630 if (parser
->in_function_body
17631 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
17633 error ("%<asm%> in %<constexpr%> function");
17634 cp_function_chain
->invalid_constexpr
= true;
17637 /* See if the next token is `volatile'. */
17638 if (cp_parser_allow_gnu_extensions_p (parser
)
17639 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
17641 /* Remember that we saw the `volatile' keyword. */
17643 /* Consume the token. */
17644 cp_lexer_consume_token (parser
->lexer
);
17646 if (cp_parser_allow_gnu_extensions_p (parser
)
17647 && parser
->in_function_body
17648 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
17650 /* Remember that we saw the `goto' keyword. */
17652 /* Consume the token. */
17653 cp_lexer_consume_token (parser
->lexer
);
17655 /* Look for the opening `('. */
17656 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
17658 /* Look for the string. */
17659 string
= cp_parser_string_literal (parser
, false, false);
17660 if (string
== error_mark_node
)
17662 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
17663 /*consume_paren=*/true);
17667 /* If we're allowing GNU extensions, check for the extended assembly
17668 syntax. Unfortunately, the `:' tokens need not be separated by
17669 a space in C, and so, for compatibility, we tolerate that here
17670 too. Doing that means that we have to treat the `::' operator as
17672 if (cp_parser_allow_gnu_extensions_p (parser
)
17673 && parser
->in_function_body
17674 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
17675 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
17677 bool inputs_p
= false;
17678 bool clobbers_p
= false;
17679 bool labels_p
= false;
17681 /* The extended syntax was used. */
17684 /* Look for outputs. */
17685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17687 /* Consume the `:'. */
17688 cp_lexer_consume_token (parser
->lexer
);
17689 /* Parse the output-operands. */
17690 if (cp_lexer_next_token_is_not (parser
->lexer
,
17692 && cp_lexer_next_token_is_not (parser
->lexer
,
17694 && cp_lexer_next_token_is_not (parser
->lexer
,
17698 outputs
= cp_parser_asm_operand_list (parser
);
17699 if (outputs
== error_mark_node
)
17700 invalid_outputs_p
= true;
17703 /* If the next token is `::', there are no outputs, and the
17704 next token is the beginning of the inputs. */
17705 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17706 /* The inputs are coming next. */
17709 /* Look for inputs. */
17711 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17713 /* Consume the `:' or `::'. */
17714 cp_lexer_consume_token (parser
->lexer
);
17715 /* Parse the output-operands. */
17716 if (cp_lexer_next_token_is_not (parser
->lexer
,
17718 && cp_lexer_next_token_is_not (parser
->lexer
,
17720 && cp_lexer_next_token_is_not (parser
->lexer
,
17723 inputs
= cp_parser_asm_operand_list (parser
);
17724 if (inputs
== error_mark_node
)
17725 invalid_inputs_p
= true;
17728 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17729 /* The clobbers are coming next. */
17732 /* Look for clobbers. */
17734 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17737 /* Consume the `:' or `::'. */
17738 cp_lexer_consume_token (parser
->lexer
);
17739 /* Parse the clobbers. */
17740 if (cp_lexer_next_token_is_not (parser
->lexer
,
17742 && cp_lexer_next_token_is_not (parser
->lexer
,
17744 clobbers
= cp_parser_asm_clobber_list (parser
);
17747 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17748 /* The labels are coming next. */
17751 /* Look for labels. */
17753 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
17756 /* Consume the `:' or `::'. */
17757 cp_lexer_consume_token (parser
->lexer
);
17758 /* Parse the labels. */
17759 labels
= cp_parser_asm_label_list (parser
);
17762 if (goto_p
&& !labels_p
)
17763 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
17766 missing
= RT_COLON_SCOPE
;
17768 /* Look for the closing `)'. */
17769 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
17770 missing
? missing
: RT_CLOSE_PAREN
))
17771 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
17772 /*consume_paren=*/true);
17773 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17775 if (!invalid_inputs_p
&& !invalid_outputs_p
)
17777 /* Create the ASM_EXPR. */
17778 if (parser
->in_function_body
)
17780 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
17781 inputs
, clobbers
, labels
);
17782 /* If the extended syntax was not used, mark the ASM_EXPR. */
17785 tree temp
= asm_stmt
;
17786 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
17787 temp
= TREE_OPERAND (temp
, 0);
17789 ASM_INPUT_P (temp
) = 1;
17793 symtab
->finalize_toplevel_asm (string
);
17797 /* Declarators [gram.dcl.decl] */
17799 /* Parse an init-declarator.
17802 declarator initializer [opt]
17807 declarator asm-specification [opt] attributes [opt] initializer [opt]
17809 function-definition:
17810 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17812 decl-specifier-seq [opt] declarator function-try-block
17816 function-definition:
17817 __extension__ function-definition
17821 function-definition:
17822 decl-specifier-seq [opt] declarator function-transaction-block
17824 The DECL_SPECIFIERS apply to this declarator. Returns a
17825 representation of the entity declared. If MEMBER_P is TRUE, then
17826 this declarator appears in a class scope. The new DECL created by
17827 this declarator is returned.
17829 The CHECKS are access checks that should be performed once we know
17830 what entity is being declared (and, therefore, what classes have
17833 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17834 for a function-definition here as well. If the declarator is a
17835 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17836 be TRUE upon return. By that point, the function-definition will
17837 have been completely parsed.
17839 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17842 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17843 parsed declaration if it is an uninitialized single declarator not followed
17844 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17845 if present, will not be consumed. If returned, this declarator will be
17846 created with SD_INITIALIZED but will not call cp_finish_decl.
17848 FIRST indicates if this is the first declarator in a declaration sequence.
17850 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17851 and there is an initializer, the pointed location_t is set to the
17852 location of the '=' or `(', or '{' in C++11 token introducing the
17856 cp_parser_init_declarator (cp_parser
* parser
,
17857 cp_decl_specifier_seq
*decl_specifiers
,
17858 vec
<deferred_access_check
, va_gc
> *checks
,
17859 bool function_definition_allowed_p
,
17861 int declares_class_or_enum
,
17862 bool* function_definition_p
,
17863 tree
* maybe_range_for_decl
,
17865 location_t
* init_loc
)
17867 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
17868 *attributes_start_token
= NULL
;
17869 cp_declarator
*declarator
;
17870 tree prefix_attributes
;
17871 tree attributes
= NULL
;
17872 tree asm_specification
;
17874 tree decl
= NULL_TREE
;
17876 int is_initialized
;
17877 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17878 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17880 enum cpp_ttype initialization_kind
;
17881 bool is_direct_init
= false;
17882 bool is_non_constant_init
;
17883 int ctor_dtor_or_conv_p
;
17884 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
17885 tree pushed_scope
= NULL_TREE
;
17886 bool range_for_decl_p
= false;
17887 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17888 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
17890 /* Gather the attributes that were provided with the
17891 decl-specifiers. */
17892 prefix_attributes
= decl_specifiers
->attributes
;
17894 /* Assume that this is not the declarator for a function
17896 if (function_definition_p
)
17897 *function_definition_p
= false;
17899 /* Default arguments are only permitted for function parameters. */
17900 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
17901 parser
->default_arg_ok_p
= false;
17903 /* Defer access checks while parsing the declarator; we cannot know
17904 what names are accessible until we know what is being
17906 resume_deferring_access_checks ();
17908 /* Parse the declarator. */
17909 token
= cp_lexer_peek_token (parser
->lexer
);
17911 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17912 &ctor_dtor_or_conv_p
,
17913 /*parenthesized_p=*/NULL
,
17914 member_p
, friend_p
);
17915 /* Gather up the deferred checks. */
17916 stop_deferring_access_checks ();
17918 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17920 /* If the DECLARATOR was erroneous, there's no need to go
17922 if (declarator
== cp_error_declarator
)
17923 return error_mark_node
;
17925 /* Check that the number of template-parameter-lists is OK. */
17926 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
17928 return error_mark_node
;
17930 if (declares_class_or_enum
& 2)
17931 cp_parser_check_for_definition_in_return_type (declarator
,
17932 decl_specifiers
->type
,
17933 decl_specifiers
->locations
[ds_type_spec
]);
17935 /* Figure out what scope the entity declared by the DECLARATOR is
17936 located in. `grokdeclarator' sometimes changes the scope, so
17937 we compute it now. */
17938 scope
= get_scope_of_declarator (declarator
);
17940 /* Perform any lookups in the declared type which were thought to be
17941 dependent, but are not in the scope of the declarator. */
17942 decl_specifiers
->type
17943 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
17945 /* If we're allowing GNU extensions, look for an
17946 asm-specification. */
17947 if (cp_parser_allow_gnu_extensions_p (parser
))
17949 /* Look for an asm-specification. */
17950 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
17951 asm_specification
= cp_parser_asm_specification_opt (parser
);
17954 asm_specification
= NULL_TREE
;
17956 /* Look for attributes. */
17957 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
17958 attributes
= cp_parser_attributes_opt (parser
);
17960 /* Peek at the next token. */
17961 token
= cp_lexer_peek_token (parser
->lexer
);
17963 bool bogus_implicit_tmpl
= false;
17965 if (function_declarator_p (declarator
))
17967 /* Check to see if the token indicates the start of a
17968 function-definition. */
17969 if (cp_parser_token_starts_function_definition_p (token
))
17971 if (!function_definition_allowed_p
)
17973 /* If a function-definition should not appear here, issue an
17975 cp_parser_error (parser
,
17976 "a function-definition is not allowed here");
17977 return error_mark_node
;
17980 location_t func_brace_location
17981 = cp_lexer_peek_token (parser
->lexer
)->location
;
17983 /* Neither attributes nor an asm-specification are allowed
17984 on a function-definition. */
17985 if (asm_specification
)
17986 error_at (asm_spec_start_token
->location
,
17987 "an asm-specification is not allowed "
17988 "on a function-definition");
17990 error_at (attributes_start_token
->location
,
17991 "attributes are not allowed "
17992 "on a function-definition");
17993 /* This is a function-definition. */
17994 *function_definition_p
= true;
17996 /* Parse the function definition. */
17998 decl
= cp_parser_save_member_function_body (parser
,
18005 (cp_parser_function_definition_from_specifiers_and_declarator
18006 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
18008 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
18010 /* This is where the prologue starts... */
18011 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
18012 = func_brace_location
;
18018 else if (parser
->fully_implicit_function_template_p
)
18020 /* A non-template declaration involving a function parameter list
18021 containing an implicit template parameter will be made into a
18022 template. If the resulting declaration is not going to be an
18023 actual function then finish the template scope here to prevent it.
18024 An error message will be issued once we have a decl to talk about.
18026 FIXME probably we should do type deduction rather than create an
18027 implicit template, but the standard currently doesn't allow it. */
18028 bogus_implicit_tmpl
= true;
18029 finish_fully_implicit_template (parser
, NULL_TREE
);
18034 Only in function declarations for constructors, destructors, and
18035 type conversions can the decl-specifier-seq be omitted.
18037 We explicitly postpone this check past the point where we handle
18038 function-definitions because we tolerate function-definitions
18039 that are missing their return types in some modes. */
18040 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
18042 cp_parser_error (parser
,
18043 "expected constructor, destructor, or type conversion");
18044 return error_mark_node
;
18047 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18048 if (token
->type
== CPP_EQ
18049 || token
->type
== CPP_OPEN_PAREN
18050 || token
->type
== CPP_OPEN_BRACE
)
18052 is_initialized
= SD_INITIALIZED
;
18053 initialization_kind
= token
->type
;
18054 if (maybe_range_for_decl
)
18055 *maybe_range_for_decl
= error_mark_node
;
18056 tmp_init_loc
= token
->location
;
18057 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
18058 *init_loc
= tmp_init_loc
;
18060 if (token
->type
== CPP_EQ
18061 && function_declarator_p (declarator
))
18063 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18064 if (t2
->keyword
== RID_DEFAULT
)
18065 is_initialized
= SD_DEFAULTED
;
18066 else if (t2
->keyword
== RID_DELETE
)
18067 is_initialized
= SD_DELETED
;
18072 /* If the init-declarator isn't initialized and isn't followed by a
18073 `,' or `;', it's not a valid init-declarator. */
18074 if (token
->type
!= CPP_COMMA
18075 && token
->type
!= CPP_SEMICOLON
)
18077 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
18078 range_for_decl_p
= true;
18081 if (!maybe_range_for_decl
)
18082 cp_parser_error (parser
, "expected initializer");
18083 return error_mark_node
;
18086 is_initialized
= SD_UNINITIALIZED
;
18087 initialization_kind
= CPP_EOF
;
18090 /* Because start_decl has side-effects, we should only call it if we
18091 know we're going ahead. By this point, we know that we cannot
18092 possibly be looking at any other construct. */
18093 cp_parser_commit_to_tentative_parse (parser
);
18095 /* Enter the newly declared entry in the symbol table. If we're
18096 processing a declaration in a class-specifier, we wait until
18097 after processing the initializer. */
18100 if (parser
->in_unbraced_linkage_specification_p
)
18101 decl_specifiers
->storage_class
= sc_extern
;
18102 decl
= start_decl (declarator
, decl_specifiers
,
18103 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
18104 attributes
, prefix_attributes
, &pushed_scope
);
18105 cp_finalize_omp_declare_simd (parser
, decl
);
18106 cp_finalize_oacc_routine (parser
, decl
, false, first
);
18107 /* Adjust location of decl if declarator->id_loc is more appropriate:
18108 set, and decl wasn't merged with another decl, in which case its
18109 location would be different from input_location, and more accurate. */
18111 && declarator
->id_loc
!= UNKNOWN_LOCATION
18112 && DECL_SOURCE_LOCATION (decl
) == input_location
)
18113 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
18116 /* Enter the SCOPE. That way unqualified names appearing in the
18117 initializer will be looked up in SCOPE. */
18118 pushed_scope
= push_scope (scope
);
18120 /* Perform deferred access control checks, now that we know in which
18121 SCOPE the declared entity resides. */
18122 if (!member_p
&& decl
)
18124 tree saved_current_function_decl
= NULL_TREE
;
18126 /* If the entity being declared is a function, pretend that we
18127 are in its scope. If it is a `friend', it may have access to
18128 things that would not otherwise be accessible. */
18129 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18131 saved_current_function_decl
= current_function_decl
;
18132 current_function_decl
= decl
;
18135 /* Perform access checks for template parameters. */
18136 cp_parser_perform_template_parameter_access_checks (checks
);
18138 /* Perform the access control checks for the declarator and the
18139 decl-specifiers. */
18140 perform_deferred_access_checks (tf_warning_or_error
);
18142 /* Restore the saved value. */
18143 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18144 current_function_decl
= saved_current_function_decl
;
18147 /* Parse the initializer. */
18148 initializer
= NULL_TREE
;
18149 is_direct_init
= false;
18150 is_non_constant_init
= true;
18151 if (is_initialized
)
18153 if (function_declarator_p (declarator
))
18155 if (initialization_kind
== CPP_EQ
)
18156 initializer
= cp_parser_pure_specifier (parser
);
18159 /* If the declaration was erroneous, we don't really
18160 know what the user intended, so just silently
18161 consume the initializer. */
18162 if (decl
!= error_mark_node
)
18163 error_at (tmp_init_loc
, "initializer provided for function");
18164 cp_parser_skip_to_closing_parenthesis (parser
,
18165 /*recovering=*/true,
18166 /*or_comma=*/false,
18167 /*consume_paren=*/true);
18172 /* We want to record the extra mangling scope for in-class
18173 initializers of class members and initializers of static data
18174 member templates. The former involves deferring
18175 parsing of the initializer until end of class as with default
18176 arguments. So right here we only handle the latter. */
18177 if (!member_p
&& processing_template_decl
)
18178 start_lambda_scope (decl
);
18179 initializer
= cp_parser_initializer (parser
,
18181 &is_non_constant_init
);
18182 if (!member_p
&& processing_template_decl
)
18183 finish_lambda_scope ();
18184 if (initializer
== error_mark_node
)
18185 cp_parser_skip_to_end_of_statement (parser
);
18189 /* The old parser allows attributes to appear after a parenthesized
18190 initializer. Mark Mitchell proposed removing this functionality
18191 on the GCC mailing lists on 2002-08-13. This parser accepts the
18192 attributes -- but ignores them. */
18193 if (cp_parser_allow_gnu_extensions_p (parser
)
18194 && initialization_kind
== CPP_OPEN_PAREN
)
18195 if (cp_parser_attributes_opt (parser
))
18196 warning (OPT_Wattributes
,
18197 "attributes after parenthesized initializer ignored");
18199 /* And now complain about a non-function implicit template. */
18200 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
18201 error_at (DECL_SOURCE_LOCATION (decl
),
18202 "non-function %qD declared as implicit template", decl
);
18204 /* For an in-class declaration, use `grokfield' to create the
18210 pop_scope (pushed_scope
);
18211 pushed_scope
= NULL_TREE
;
18213 decl
= grokfield (declarator
, decl_specifiers
,
18214 initializer
, !is_non_constant_init
,
18215 /*asmspec=*/NULL_TREE
,
18216 chainon (attributes
, prefix_attributes
));
18217 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18218 cp_parser_save_default_args (parser
, decl
);
18219 cp_finalize_omp_declare_simd (parser
, decl
);
18220 cp_finalize_oacc_routine (parser
, decl
, false, first
);
18223 /* Finish processing the declaration. But, skip member
18225 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
18227 cp_finish_decl (decl
,
18228 initializer
, !is_non_constant_init
,
18230 /* If the initializer is in parentheses, then this is
18231 a direct-initialization, which means that an
18232 `explicit' constructor is OK. Otherwise, an
18233 `explicit' constructor cannot be used. */
18234 ((is_direct_init
|| !is_initialized
)
18235 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
18237 else if ((cxx_dialect
!= cxx98
) && friend_p
18238 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18239 /* Core issue #226 (C++0x only): A default template-argument
18240 shall not be specified in a friend class template
18242 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
18243 /*is_partial=*/false, /*is_friend_decl=*/1);
18245 if (!friend_p
&& pushed_scope
)
18246 pop_scope (pushed_scope
);
18248 if (function_declarator_p (declarator
)
18249 && parser
->fully_implicit_function_template_p
)
18252 decl
= finish_fully_implicit_template (parser
, decl
);
18254 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
18260 /* Parse a declarator.
18264 ptr-operator declarator
18266 abstract-declarator:
18267 ptr-operator abstract-declarator [opt]
18268 direct-abstract-declarator
18273 attributes [opt] direct-declarator
18274 attributes [opt] ptr-operator declarator
18276 abstract-declarator:
18277 attributes [opt] ptr-operator abstract-declarator [opt]
18278 attributes [opt] direct-abstract-declarator
18280 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18281 detect constructor, destructor or conversion operators. It is set
18282 to -1 if the declarator is a name, and +1 if it is a
18283 function. Otherwise it is set to zero. Usually you just want to
18284 test for >0, but internally the negative value is used.
18286 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18287 a decl-specifier-seq unless it declares a constructor, destructor,
18288 or conversion. It might seem that we could check this condition in
18289 semantic analysis, rather than parsing, but that makes it difficult
18290 to handle something like `f()'. We want to notice that there are
18291 no decl-specifiers, and therefore realize that this is an
18292 expression, not a declaration.)
18294 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18295 the declarator is a direct-declarator of the form "(...)".
18297 MEMBER_P is true iff this declarator is a member-declarator.
18299 FRIEND_P is true iff this declarator is a friend. */
18301 static cp_declarator
*
18302 cp_parser_declarator (cp_parser
* parser
,
18303 cp_parser_declarator_kind dcl_kind
,
18304 int* ctor_dtor_or_conv_p
,
18305 bool* parenthesized_p
,
18306 bool member_p
, bool friend_p
)
18308 cp_declarator
*declarator
;
18309 enum tree_code code
;
18310 cp_cv_quals cv_quals
;
18312 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
18314 /* Assume this is not a constructor, destructor, or type-conversion
18316 if (ctor_dtor_or_conv_p
)
18317 *ctor_dtor_or_conv_p
= 0;
18319 if (cp_parser_allow_gnu_extensions_p (parser
))
18320 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
18322 /* Check for the ptr-operator production. */
18323 cp_parser_parse_tentatively (parser
);
18324 /* Parse the ptr-operator. */
18325 code
= cp_parser_ptr_operator (parser
,
18330 /* If that worked, then we have a ptr-operator. */
18331 if (cp_parser_parse_definitely (parser
))
18333 /* If a ptr-operator was found, then this declarator was not
18335 if (parenthesized_p
)
18336 *parenthesized_p
= true;
18337 /* The dependent declarator is optional if we are parsing an
18338 abstract-declarator. */
18339 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18340 cp_parser_parse_tentatively (parser
);
18342 /* Parse the dependent declarator. */
18343 declarator
= cp_parser_declarator (parser
, dcl_kind
,
18344 /*ctor_dtor_or_conv_p=*/NULL
,
18345 /*parenthesized_p=*/NULL
,
18346 /*member_p=*/false,
18349 /* If we are parsing an abstract-declarator, we must handle the
18350 case where the dependent declarator is absent. */
18351 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
18352 && !cp_parser_parse_definitely (parser
))
18355 declarator
= cp_parser_make_indirect_declarator
18356 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
18358 /* Everything else is a direct-declarator. */
18361 if (parenthesized_p
)
18362 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
18364 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
18365 ctor_dtor_or_conv_p
,
18366 member_p
, friend_p
);
18369 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
18370 declarator
->attributes
= gnu_attributes
;
18374 /* Parse a direct-declarator or direct-abstract-declarator.
18378 direct-declarator ( parameter-declaration-clause )
18379 cv-qualifier-seq [opt]
18380 ref-qualifier [opt]
18381 exception-specification [opt]
18382 direct-declarator [ constant-expression [opt] ]
18385 direct-abstract-declarator:
18386 direct-abstract-declarator [opt]
18387 ( parameter-declaration-clause )
18388 cv-qualifier-seq [opt]
18389 ref-qualifier [opt]
18390 exception-specification [opt]
18391 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18392 ( abstract-declarator )
18394 Returns a representation of the declarator. DCL_KIND is
18395 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18396 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18397 we are parsing a direct-declarator. It is
18398 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18399 of ambiguity we prefer an abstract declarator, as per
18400 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18401 as for cp_parser_declarator. */
18403 static cp_declarator
*
18404 cp_parser_direct_declarator (cp_parser
* parser
,
18405 cp_parser_declarator_kind dcl_kind
,
18406 int* ctor_dtor_or_conv_p
,
18407 bool member_p
, bool friend_p
)
18410 cp_declarator
*declarator
= NULL
;
18411 tree scope
= NULL_TREE
;
18412 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18413 bool saved_in_declarator_p
= parser
->in_declarator_p
;
18415 tree pushed_scope
= NULL_TREE
;
18419 /* Peek at the next token. */
18420 token
= cp_lexer_peek_token (parser
->lexer
);
18421 if (token
->type
== CPP_OPEN_PAREN
)
18423 /* This is either a parameter-declaration-clause, or a
18424 parenthesized declarator. When we know we are parsing a
18425 named declarator, it must be a parenthesized declarator
18426 if FIRST is true. For instance, `(int)' is a
18427 parameter-declaration-clause, with an omitted
18428 direct-abstract-declarator. But `((*))', is a
18429 parenthesized abstract declarator. Finally, when T is a
18430 template parameter `(T)' is a
18431 parameter-declaration-clause, and not a parenthesized
18434 We first try and parse a parameter-declaration-clause,
18435 and then try a nested declarator (if FIRST is true).
18437 It is not an error for it not to be a
18438 parameter-declaration-clause, even when FIRST is
18444 The first is the declaration of a function while the
18445 second is the definition of a variable, including its
18448 Having seen only the parenthesis, we cannot know which of
18449 these two alternatives should be selected. Even more
18450 complex are examples like:
18455 The former is a function-declaration; the latter is a
18456 variable initialization.
18458 Thus again, we try a parameter-declaration-clause, and if
18459 that fails, we back out and return. */
18461 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18464 bool is_declarator
= false;
18466 /* In a member-declarator, the only valid interpretation
18467 of a parenthesis is the start of a
18468 parameter-declaration-clause. (It is invalid to
18469 initialize a static data member with a parenthesized
18470 initializer; only the "=" form of initialization is
18473 cp_parser_parse_tentatively (parser
);
18475 /* Consume the `('. */
18476 cp_lexer_consume_token (parser
->lexer
);
18479 /* If this is going to be an abstract declarator, we're
18480 in a declarator and we can't have default args. */
18481 parser
->default_arg_ok_p
= false;
18482 parser
->in_declarator_p
= true;
18485 begin_scope (sk_function_parms
, NULL_TREE
);
18487 /* Parse the parameter-declaration-clause. */
18488 params
= cp_parser_parameter_declaration_clause (parser
);
18490 /* Consume the `)'. */
18491 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18493 /* If all went well, parse the cv-qualifier-seq,
18494 ref-qualifier and the exception-specification. */
18495 if (member_p
|| cp_parser_parse_definitely (parser
))
18497 cp_cv_quals cv_quals
;
18498 cp_virt_specifiers virt_specifiers
;
18499 cp_ref_qualifier ref_qual
;
18500 tree exception_specification
;
18503 bool memfn
= (member_p
|| (pushed_scope
18504 && CLASS_TYPE_P (pushed_scope
)));
18506 is_declarator
= true;
18508 if (ctor_dtor_or_conv_p
)
18509 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
18512 /* Parse the cv-qualifier-seq. */
18513 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18514 /* Parse the ref-qualifier. */
18515 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
18516 /* Parse the tx-qualifier. */
18517 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
18518 /* And the exception-specification. */
18519 exception_specification
18520 = cp_parser_exception_specification_opt (parser
);
18522 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18524 /* In here, we handle cases where attribute is used after
18525 the function declaration. For example:
18526 void func (int x) __attribute__((vector(..))); */
18528 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
18530 cp_parser_parse_tentatively (parser
);
18531 tree attr
= cp_parser_gnu_attributes_opt (parser
);
18532 if (cp_lexer_next_token_is_not (parser
->lexer
,
18534 && cp_lexer_next_token_is_not (parser
->lexer
,
18536 cp_parser_abort_tentative_parse (parser
);
18537 else if (!cp_parser_parse_definitely (parser
))
18540 attrs
= chainon (attr
, attrs
);
18542 tree requires_clause
= NULL_TREE
;
18543 late_return
= (cp_parser_late_return_type_opt
18544 (parser
, declarator
, requires_clause
,
18545 memfn
? cv_quals
: -1));
18547 /* Parse the virt-specifier-seq. */
18548 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
18550 /* Create the function-declarator. */
18551 declarator
= make_call_declarator (declarator
,
18557 exception_specification
,
18560 declarator
->std_attributes
= attrs
;
18561 /* Any subsequent parameter lists are to do with
18562 return type, so are not those of the declared
18564 parser
->default_arg_ok_p
= false;
18567 /* Remove the function parms from scope. */
18568 pop_bindings_and_leave_scope ();
18571 /* Repeat the main loop. */
18575 /* If this is the first, we can try a parenthesized
18579 bool saved_in_type_id_in_expr_p
;
18581 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18582 parser
->in_declarator_p
= saved_in_declarator_p
;
18584 /* Consume the `('. */
18585 cp_lexer_consume_token (parser
->lexer
);
18586 /* Parse the nested declarator. */
18587 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
18588 parser
->in_type_id_in_expr_p
= true;
18590 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
18591 /*parenthesized_p=*/NULL
,
18592 member_p
, friend_p
);
18593 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
18595 /* Expect a `)'. */
18596 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
18597 declarator
= cp_error_declarator
;
18598 if (declarator
== cp_error_declarator
)
18601 goto handle_declarator
;
18603 /* Otherwise, we must be done. */
18607 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18608 && token
->type
== CPP_OPEN_SQUARE
18609 && !cp_next_tokens_can_be_attribute_p (parser
))
18611 /* Parse an array-declarator. */
18612 tree bounds
, attrs
;
18614 if (ctor_dtor_or_conv_p
)
18615 *ctor_dtor_or_conv_p
= 0;
18618 parser
->default_arg_ok_p
= false;
18619 parser
->in_declarator_p
= true;
18620 /* Consume the `['. */
18621 cp_lexer_consume_token (parser
->lexer
);
18622 /* Peek at the next token. */
18623 token
= cp_lexer_peek_token (parser
->lexer
);
18624 /* If the next token is `]', then there is no
18625 constant-expression. */
18626 if (token
->type
!= CPP_CLOSE_SQUARE
)
18628 bool non_constant_p
;
18630 = cp_parser_constant_expression (parser
,
18631 /*allow_non_constant=*/true,
18633 if (!non_constant_p
)
18635 else if (error_operand_p (bounds
))
18636 /* Already gave an error. */;
18637 else if (!parser
->in_function_body
18638 || current_binding_level
->kind
== sk_function_parms
)
18640 /* Normally, the array bound must be an integral constant
18641 expression. However, as an extension, we allow VLAs
18642 in function scopes as long as they aren't part of a
18643 parameter declaration. */
18644 cp_parser_error (parser
,
18645 "array bound is not an integer constant");
18646 bounds
= error_mark_node
;
18648 else if (processing_template_decl
18649 && !type_dependent_expression_p (bounds
))
18651 /* Remember this wasn't a constant-expression. */
18652 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
18653 TREE_SIDE_EFFECTS (bounds
) = 1;
18657 bounds
= NULL_TREE
;
18658 /* Look for the closing `]'. */
18659 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
18661 declarator
= cp_error_declarator
;
18665 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18666 declarator
= make_array_declarator (declarator
, bounds
);
18667 declarator
->std_attributes
= attrs
;
18669 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
18672 tree qualifying_scope
;
18673 tree unqualified_name
;
18675 special_function_kind sfk
;
18677 bool pack_expansion_p
= false;
18678 cp_token
*declarator_id_start_token
;
18680 /* Parse a declarator-id */
18681 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
18684 cp_parser_parse_tentatively (parser
);
18686 /* If we see an ellipsis, we should be looking at a
18688 if (token
->type
== CPP_ELLIPSIS
)
18690 /* Consume the `...' */
18691 cp_lexer_consume_token (parser
->lexer
);
18693 pack_expansion_p
= true;
18697 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
18699 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
18700 qualifying_scope
= parser
->scope
;
18705 if (!unqualified_name
&& pack_expansion_p
)
18707 /* Check whether an error occurred. */
18708 okay
= !cp_parser_error_occurred (parser
);
18710 /* We already consumed the ellipsis to mark a
18711 parameter pack, but we have no way to report it,
18712 so abort the tentative parse. We will be exiting
18713 immediately anyway. */
18714 cp_parser_abort_tentative_parse (parser
);
18717 okay
= cp_parser_parse_definitely (parser
);
18720 unqualified_name
= error_mark_node
;
18721 else if (unqualified_name
18722 && (qualifying_scope
18723 || (!identifier_p (unqualified_name
))))
18725 cp_parser_error (parser
, "expected unqualified-id");
18726 unqualified_name
= error_mark_node
;
18730 if (!unqualified_name
)
18732 if (unqualified_name
== error_mark_node
)
18734 declarator
= cp_error_declarator
;
18735 pack_expansion_p
= false;
18736 declarator
->parameter_pack_p
= false;
18740 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18742 if (qualifying_scope
&& at_namespace_scope_p ()
18743 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
18745 /* In the declaration of a member of a template class
18746 outside of the class itself, the SCOPE will sometimes
18747 be a TYPENAME_TYPE. For example, given:
18749 template <typename T>
18750 int S<T>::R::i = 3;
18752 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18753 this context, we must resolve S<T>::R to an ordinary
18754 type, rather than a typename type.
18756 The reason we normally avoid resolving TYPENAME_TYPEs
18757 is that a specialization of `S' might render
18758 `S<T>::R' not a type. However, if `S' is
18759 specialized, then this `i' will not be used, so there
18760 is no harm in resolving the types here. */
18763 /* Resolve the TYPENAME_TYPE. */
18764 type
= resolve_typename_type (qualifying_scope
,
18765 /*only_current_p=*/false);
18766 /* If that failed, the declarator is invalid. */
18767 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18769 if (typedef_variant_p (type
))
18770 error_at (declarator_id_start_token
->location
,
18771 "cannot define member of dependent typedef "
18774 error_at (declarator_id_start_token
->location
,
18775 "%<%T::%E%> is not a type",
18776 TYPE_CONTEXT (qualifying_scope
),
18777 TYPE_IDENTIFIER (qualifying_scope
));
18779 qualifying_scope
= type
;
18784 if (unqualified_name
)
18788 if (qualifying_scope
18789 && CLASS_TYPE_P (qualifying_scope
))
18790 class_type
= qualifying_scope
;
18792 class_type
= current_class_type
;
18794 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
18796 tree name_type
= TREE_TYPE (unqualified_name
);
18797 if (class_type
&& same_type_p (name_type
, class_type
))
18799 if (qualifying_scope
18800 && CLASSTYPE_USE_TEMPLATE (name_type
))
18802 error_at (declarator_id_start_token
->location
,
18803 "invalid use of constructor as a template");
18804 inform (declarator_id_start_token
->location
,
18805 "use %<%T::%D%> instead of %<%T::%D%> to "
18806 "name the constructor in a qualified name",
18808 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
18809 class_type
, name_type
);
18810 declarator
= cp_error_declarator
;
18814 unqualified_name
= constructor_name (class_type
);
18818 /* We do not attempt to print the declarator
18819 here because we do not have enough
18820 information about its original syntactic
18822 cp_parser_error (parser
, "invalid declarator");
18823 declarator
= cp_error_declarator
;
18830 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
18831 sfk
= sfk_destructor
;
18832 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
18833 sfk
= sfk_conversion
;
18834 else if (/* There's no way to declare a constructor
18835 for an anonymous type, even if the type
18836 got a name for linkage purposes. */
18837 !TYPE_WAS_ANONYMOUS (class_type
)
18838 /* Handle correctly (c++/19200):
18852 friend void N::S();
18855 && class_type
!= qualifying_scope
)
18856 && constructor_name_p (unqualified_name
,
18859 unqualified_name
= constructor_name (class_type
);
18860 sfk
= sfk_constructor
;
18862 else if (is_overloaded_fn (unqualified_name
)
18863 && DECL_CONSTRUCTOR_P (get_first_fn
18864 (unqualified_name
)))
18865 sfk
= sfk_constructor
;
18867 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
18868 *ctor_dtor_or_conv_p
= -1;
18871 declarator
= make_id_declarator (qualifying_scope
,
18874 declarator
->std_attributes
= attrs
;
18875 declarator
->id_loc
= token
->location
;
18876 declarator
->parameter_pack_p
= pack_expansion_p
;
18878 if (pack_expansion_p
)
18879 maybe_warn_variadic_templates ();
18882 handle_declarator
:;
18883 scope
= get_scope_of_declarator (declarator
);
18886 /* Any names that appear after the declarator-id for a
18887 member are looked up in the containing scope. */
18888 if (at_function_scope_p ())
18890 /* But declarations with qualified-ids can't appear in a
18892 cp_parser_error (parser
, "qualified-id in declaration");
18893 declarator
= cp_error_declarator
;
18896 pushed_scope
= push_scope (scope
);
18898 parser
->in_declarator_p
= true;
18899 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
18900 || (declarator
&& declarator
->kind
== cdk_id
))
18901 /* Default args are only allowed on function
18903 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18905 parser
->default_arg_ok_p
= false;
18914 /* For an abstract declarator, we might wind up with nothing at this
18915 point. That's an error; the declarator is not optional. */
18917 cp_parser_error (parser
, "expected declarator");
18919 /* If we entered a scope, we must exit it now. */
18921 pop_scope (pushed_scope
);
18923 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18924 parser
->in_declarator_p
= saved_in_declarator_p
;
18929 /* Parse a ptr-operator.
18932 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18933 * cv-qualifier-seq [opt]
18935 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18936 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18941 & cv-qualifier-seq [opt]
18943 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18944 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18945 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18946 filled in with the TYPE containing the member. *CV_QUALS is
18947 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18948 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18949 Note that the tree codes returned by this function have nothing
18950 to do with the types of trees that will be eventually be created
18951 to represent the pointer or reference type being parsed. They are
18952 just constants with suggestive names. */
18953 static enum tree_code
18954 cp_parser_ptr_operator (cp_parser
* parser
,
18956 cp_cv_quals
*cv_quals
,
18959 enum tree_code code
= ERROR_MARK
;
18961 tree attrs
= NULL_TREE
;
18963 /* Assume that it's not a pointer-to-member. */
18965 /* And that there are no cv-qualifiers. */
18966 *cv_quals
= TYPE_UNQUALIFIED
;
18968 /* Peek at the next token. */
18969 token
= cp_lexer_peek_token (parser
->lexer
);
18971 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18972 if (token
->type
== CPP_MULT
)
18973 code
= INDIRECT_REF
;
18974 else if (token
->type
== CPP_AND
)
18976 else if ((cxx_dialect
!= cxx98
) &&
18977 token
->type
== CPP_AND_AND
) /* C++0x only */
18978 code
= NON_LVALUE_EXPR
;
18980 if (code
!= ERROR_MARK
)
18982 /* Consume the `*', `&' or `&&'. */
18983 cp_lexer_consume_token (parser
->lexer
);
18985 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18986 `&', if we are allowing GNU extensions. (The only qualifier
18987 that can legally appear after `&' is `restrict', but that is
18988 enforced during semantic analysis. */
18989 if (code
== INDIRECT_REF
18990 || cp_parser_allow_gnu_extensions_p (parser
))
18991 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18993 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18994 if (attributes
!= NULL
)
18995 *attributes
= attrs
;
18999 /* Try the pointer-to-member case. */
19000 cp_parser_parse_tentatively (parser
);
19001 /* Look for the optional `::' operator. */
19002 cp_parser_global_scope_opt (parser
,
19003 /*current_scope_valid_p=*/false);
19004 /* Look for the nested-name specifier. */
19005 token
= cp_lexer_peek_token (parser
->lexer
);
19006 cp_parser_nested_name_specifier (parser
,
19007 /*typename_keyword_p=*/false,
19008 /*check_dependency_p=*/true,
19010 /*is_declaration=*/false);
19011 /* If we found it, and the next token is a `*', then we are
19012 indeed looking at a pointer-to-member operator. */
19013 if (!cp_parser_error_occurred (parser
)
19014 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
19016 /* Indicate that the `*' operator was used. */
19017 code
= INDIRECT_REF
;
19019 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19020 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
19021 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
19022 error_at (token
->location
, "cannot form pointer to member of "
19023 "non-class %q#T", parser
->scope
);
19026 /* The type of which the member is a member is given by the
19028 *type
= parser
->scope
;
19029 /* The next name will not be qualified. */
19030 parser
->scope
= NULL_TREE
;
19031 parser
->qualifying_scope
= NULL_TREE
;
19032 parser
->object_scope
= NULL_TREE
;
19033 /* Look for optional c++11 attributes. */
19034 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19035 if (attributes
!= NULL
)
19036 *attributes
= attrs
;
19037 /* Look for the optional cv-qualifier-seq. */
19038 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19041 /* If that didn't work we don't have a ptr-operator. */
19042 if (!cp_parser_parse_definitely (parser
))
19043 cp_parser_error (parser
, "expected ptr-operator");
19049 /* Parse an (optional) cv-qualifier-seq.
19052 cv-qualifier cv-qualifier-seq [opt]
19063 Returns a bitmask representing the cv-qualifiers. */
19066 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
19068 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
19073 cp_cv_quals cv_qualifier
;
19075 /* Peek at the next token. */
19076 token
= cp_lexer_peek_token (parser
->lexer
);
19077 /* See if it's a cv-qualifier. */
19078 switch (token
->keyword
)
19081 cv_qualifier
= TYPE_QUAL_CONST
;
19085 cv_qualifier
= TYPE_QUAL_VOLATILE
;
19089 cv_qualifier
= TYPE_QUAL_RESTRICT
;
19093 cv_qualifier
= TYPE_UNQUALIFIED
;
19100 if (cv_quals
& cv_qualifier
)
19102 error_at (token
->location
, "duplicate cv-qualifier");
19103 cp_lexer_purge_token (parser
->lexer
);
19107 cp_lexer_consume_token (parser
->lexer
);
19108 cv_quals
|= cv_qualifier
;
19115 /* Parse an (optional) ref-qualifier
19121 Returns cp_ref_qualifier representing ref-qualifier. */
19123 static cp_ref_qualifier
19124 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
19126 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
19128 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19129 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
19134 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
19135 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19137 switch (token
->type
)
19140 curr_ref_qual
= REF_QUAL_LVALUE
;
19144 curr_ref_qual
= REF_QUAL_RVALUE
;
19148 curr_ref_qual
= REF_QUAL_NONE
;
19152 if (!curr_ref_qual
)
19156 error_at (token
->location
, "multiple ref-qualifiers");
19157 cp_lexer_purge_token (parser
->lexer
);
19161 ref_qual
= curr_ref_qual
;
19162 cp_lexer_consume_token (parser
->lexer
);
19169 /* Parse an optional tx-qualifier.
19173 transaction_safe_dynamic */
19176 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
19178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19179 if (token
->type
== CPP_NAME
)
19181 tree name
= token
->u
.value
;
19182 const char *p
= IDENTIFIER_POINTER (name
);
19183 const int len
= strlen ("transaction_safe");
19184 if (!strncmp (p
, "transaction_safe", len
))
19188 || !strcmp (p
, "_dynamic"))
19190 cp_lexer_consume_token (parser
->lexer
);
19193 error ("%E requires %<-fgnu-tm%>", name
);
19204 /* Parse an (optional) virt-specifier-seq.
19206 virt-specifier-seq:
19207 virt-specifier virt-specifier-seq [opt]
19213 Returns a bitmask representing the virt-specifiers. */
19215 static cp_virt_specifiers
19216 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
19218 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19223 cp_virt_specifiers virt_specifier
;
19225 /* Peek at the next token. */
19226 token
= cp_lexer_peek_token (parser
->lexer
);
19227 /* See if it's a virt-specifier-qualifier. */
19228 if (token
->type
!= CPP_NAME
)
19230 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
19232 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19233 virt_specifier
= VIRT_SPEC_OVERRIDE
;
19235 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
19237 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19238 virt_specifier
= VIRT_SPEC_FINAL
;
19240 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
19242 virt_specifier
= VIRT_SPEC_FINAL
;
19247 if (virt_specifiers
& virt_specifier
)
19249 error_at (token
->location
, "duplicate virt-specifier");
19250 cp_lexer_purge_token (parser
->lexer
);
19254 cp_lexer_consume_token (parser
->lexer
);
19255 virt_specifiers
|= virt_specifier
;
19258 return virt_specifiers
;
19261 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19262 is in scope even though it isn't real. */
19265 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
19269 if (current_class_ptr
)
19271 /* We don't clear this between NSDMIs. Is it already what we want? */
19272 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
19273 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
19274 && cp_type_quals (type
) == quals
)
19278 this_parm
= build_this_parm (ctype
, quals
);
19279 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19280 current_class_ptr
= NULL_TREE
;
19282 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
19283 current_class_ptr
= this_parm
;
19286 /* Return true iff our current scope is a non-static data member
19290 parsing_nsdmi (void)
19292 /* We recognize NSDMI context by the context-less 'this' pointer set up
19293 by the function above. */
19294 if (current_class_ptr
19295 && TREE_CODE (current_class_ptr
) == PARM_DECL
19296 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
19301 /* Parse a late-specified return type, if any. This is not a separate
19302 non-terminal, but part of a function declarator, which looks like
19304 -> trailing-type-specifier-seq abstract-declarator(opt)
19306 Returns the type indicated by the type-id.
19308 In addition to this, parse any queued up omp declare simd
19309 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19311 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19315 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
19316 tree
& requires_clause
, cp_cv_quals quals
)
19319 tree type
= NULL_TREE
;
19320 bool declare_simd_p
= (parser
->omp_declare_simd
19322 && declarator
->kind
== cdk_id
);
19324 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
19325 && declarator
&& declarator
->kind
== cdk_id
);
19327 /* Peek at the next token. */
19328 token
= cp_lexer_peek_token (parser
->lexer
);
19329 /* A late-specified return type is indicated by an initial '->'. */
19330 if (token
->type
!= CPP_DEREF
19331 && token
->keyword
!= RID_REQUIRES
19332 && !(declare_simd_p
|| cilk_simd_fn_vector_p
))
19335 tree save_ccp
= current_class_ptr
;
19336 tree save_ccr
= current_class_ref
;
19339 /* DR 1207: 'this' is in scope in the trailing return type. */
19340 inject_this_parameter (current_class_type
, quals
);
19343 if (token
->type
== CPP_DEREF
)
19345 /* Consume the ->. */
19346 cp_lexer_consume_token (parser
->lexer
);
19348 type
= cp_parser_trailing_type_id (parser
);
19351 /* Function declarations may be followed by a trailing
19352 requires-clause. */
19353 requires_clause
= cp_parser_requires_clause_opt (parser
);
19355 if (cilk_simd_fn_vector_p
)
19356 declarator
->std_attributes
19357 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
19358 declarator
->std_attributes
);
19359 if (declare_simd_p
)
19360 declarator
->std_attributes
19361 = cp_parser_late_parsing_omp_declare_simd (parser
,
19362 declarator
->std_attributes
);
19366 current_class_ptr
= save_ccp
;
19367 current_class_ref
= save_ccr
;
19373 /* Parse a declarator-id.
19377 :: [opt] nested-name-specifier [opt] type-name
19379 In the `id-expression' case, the value returned is as for
19380 cp_parser_id_expression if the id-expression was an unqualified-id.
19381 If the id-expression was a qualified-id, then a SCOPE_REF is
19382 returned. The first operand is the scope (either a NAMESPACE_DECL
19383 or TREE_TYPE), but the second is still just a representation of an
19387 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
19390 /* The expression must be an id-expression. Assume that qualified
19391 names are the names of types so that:
19394 int S<T>::R::i = 3;
19396 will work; we must treat `S<T>::R' as the name of a type.
19397 Similarly, assume that qualified names are templates, where
19401 int S<T>::R<T>::i = 3;
19404 id
= cp_parser_id_expression (parser
,
19405 /*template_keyword_p=*/false,
19406 /*check_dependency_p=*/false,
19407 /*template_p=*/NULL
,
19408 /*declarator_p=*/true,
19410 if (id
&& BASELINK_P (id
))
19411 id
= BASELINK_FUNCTIONS (id
);
19415 /* Parse a type-id.
19418 type-specifier-seq abstract-declarator [opt]
19420 Returns the TYPE specified. */
19423 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
19424 bool is_trailing_return
)
19426 cp_decl_specifier_seq type_specifier_seq
;
19427 cp_declarator
*abstract_declarator
;
19429 /* Parse the type-specifier-seq. */
19430 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
19431 is_trailing_return
,
19432 &type_specifier_seq
);
19433 if (type_specifier_seq
.type
== error_mark_node
)
19434 return error_mark_node
;
19436 /* There might or might not be an abstract declarator. */
19437 cp_parser_parse_tentatively (parser
);
19438 /* Look for the declarator. */
19439 abstract_declarator
19440 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
19441 /*parenthesized_p=*/NULL
,
19442 /*member_p=*/false,
19443 /*friend_p=*/false);
19444 /* Check to see if there really was a declarator. */
19445 if (!cp_parser_parse_definitely (parser
))
19446 abstract_declarator
= NULL
;
19448 if (type_specifier_seq
.type
19449 /* The concepts TS allows 'auto' as a type-id. */
19451 /* None of the valid uses of 'auto' in C++14 involve the type-id
19452 nonterminal, but it is valid in a trailing-return-type. */
19453 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
19454 && type_uses_auto (type_specifier_seq
.type
))
19456 /* A type-id with type 'auto' is only ok if the abstract declarator
19457 is a function declarator with a late-specified return type.
19459 A type-id with 'auto' is also valid in a trailing-return-type
19460 in a compound-requirement. */
19461 if (abstract_declarator
19462 && abstract_declarator
->kind
== cdk_function
19463 && abstract_declarator
->u
.function
.late_return_type
)
19465 else if (parser
->in_result_type_constraint_p
)
19469 error ("invalid use of %<auto%>");
19470 return error_mark_node
;
19474 return groktypename (&type_specifier_seq
, abstract_declarator
,
19479 cp_parser_type_id (cp_parser
*parser
)
19481 return cp_parser_type_id_1 (parser
, false, false);
19485 cp_parser_template_type_arg (cp_parser
*parser
)
19488 const char *saved_message
= parser
->type_definition_forbidden_message
;
19489 parser
->type_definition_forbidden_message
19490 = G_("types may not be defined in template arguments");
19491 r
= cp_parser_type_id_1 (parser
, true, false);
19492 parser
->type_definition_forbidden_message
= saved_message
;
19493 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
19495 error ("invalid use of %<auto%> in template argument");
19496 r
= error_mark_node
;
19502 cp_parser_trailing_type_id (cp_parser
*parser
)
19504 return cp_parser_type_id_1 (parser
, false, true);
19507 /* Parse a type-specifier-seq.
19509 type-specifier-seq:
19510 type-specifier type-specifier-seq [opt]
19514 type-specifier-seq:
19515 attributes type-specifier-seq [opt]
19517 If IS_DECLARATION is true, we are at the start of a "condition" or
19518 exception-declaration, so we might be followed by a declarator-id.
19520 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19521 i.e. we've just seen "->".
19523 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19526 cp_parser_type_specifier_seq (cp_parser
* parser
,
19527 bool is_declaration
,
19528 bool is_trailing_return
,
19529 cp_decl_specifier_seq
*type_specifier_seq
)
19531 bool seen_type_specifier
= false;
19532 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
19533 cp_token
*start_token
= NULL
;
19535 /* Clear the TYPE_SPECIFIER_SEQ. */
19536 clear_decl_specs (type_specifier_seq
);
19538 /* In the context of a trailing return type, enum E { } is an
19539 elaborated-type-specifier followed by a function-body, not an
19541 if (is_trailing_return
)
19542 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
19544 /* Parse the type-specifiers and attributes. */
19547 tree type_specifier
;
19548 bool is_cv_qualifier
;
19550 /* Check for attributes first. */
19551 if (cp_next_tokens_can_be_attribute_p (parser
))
19553 type_specifier_seq
->attributes
=
19554 chainon (type_specifier_seq
->attributes
,
19555 cp_parser_attributes_opt (parser
));
19559 /* record the token of the beginning of the type specifier seq,
19560 for error reporting purposes*/
19562 start_token
= cp_lexer_peek_token (parser
->lexer
);
19564 /* Look for the type-specifier. */
19565 type_specifier
= cp_parser_type_specifier (parser
,
19567 type_specifier_seq
,
19568 /*is_declaration=*/false,
19571 if (!type_specifier
)
19573 /* If the first type-specifier could not be found, this is not a
19574 type-specifier-seq at all. */
19575 if (!seen_type_specifier
)
19577 /* Set in_declarator_p to avoid skipping to the semicolon. */
19578 int in_decl
= parser
->in_declarator_p
;
19579 parser
->in_declarator_p
= true;
19581 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
19582 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19583 cp_parser_error (parser
, "expected type-specifier");
19585 parser
->in_declarator_p
= in_decl
;
19587 type_specifier_seq
->type
= error_mark_node
;
19590 /* If subsequent type-specifiers could not be found, the
19591 type-specifier-seq is complete. */
19595 seen_type_specifier
= true;
19596 /* The standard says that a condition can be:
19598 type-specifier-seq declarator = assignment-expression
19605 we should treat the "S" as a declarator, not as a
19606 type-specifier. The standard doesn't say that explicitly for
19607 type-specifier-seq, but it does say that for
19608 decl-specifier-seq in an ordinary declaration. Perhaps it
19609 would be clearer just to allow a decl-specifier-seq here, and
19610 then add a semantic restriction that if any decl-specifiers
19611 that are not type-specifiers appear, the program is invalid. */
19612 if (is_declaration
&& !is_cv_qualifier
)
19613 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
19617 /* Return whether the function currently being declared has an associated
19618 template parameter list. */
19621 function_being_declared_is_template_p (cp_parser
* parser
)
19623 if (!current_template_parms
|| processing_template_parmlist
)
19626 if (parser
->implicit_template_scope
)
19629 if (at_class_scope_p ()
19630 && TYPE_BEING_DEFINED (current_class_type
))
19631 return parser
->num_template_parameter_lists
!= 0;
19633 return ((int) parser
->num_template_parameter_lists
> template_class_depth
19634 (current_class_type
));
19637 /* Parse a parameter-declaration-clause.
19639 parameter-declaration-clause:
19640 parameter-declaration-list [opt] ... [opt]
19641 parameter-declaration-list , ...
19643 Returns a representation for the parameter declarations. A return
19644 value of NULL indicates a parameter-declaration-clause consisting
19645 only of an ellipsis. */
19648 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
19657 int auto_is_implicit_function_template_parm_p
;
19659 parser
->auto_is_implicit_function_template_parm_p
19660 = auto_is_implicit_function_template_parm_p
;
19662 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
19666 if (!processing_specialization
19667 && !processing_template_parmlist
19668 && !processing_explicit_instantiation
)
19669 if (!current_function_decl
19670 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
19671 parser
->auto_is_implicit_function_template_parm_p
= true;
19673 /* Peek at the next token. */
19674 token
= cp_lexer_peek_token (parser
->lexer
);
19675 /* Check for trivial parameter-declaration-clauses. */
19676 if (token
->type
== CPP_ELLIPSIS
)
19678 /* Consume the `...' token. */
19679 cp_lexer_consume_token (parser
->lexer
);
19682 else if (token
->type
== CPP_CLOSE_PAREN
)
19683 /* There are no parameters. */
19685 #ifndef NO_IMPLICIT_EXTERN_C
19686 if (in_system_header_at (input_location
)
19687 && current_class_type
== NULL
19688 && current_lang_name
== lang_name_c
)
19692 return void_list_node
;
19694 /* Check for `(void)', too, which is a special case. */
19695 else if (token
->keyword
== RID_VOID
19696 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19697 == CPP_CLOSE_PAREN
))
19699 /* Consume the `void' token. */
19700 cp_lexer_consume_token (parser
->lexer
);
19701 /* There are no parameters. */
19702 return void_list_node
;
19705 /* Parse the parameter-declaration-list. */
19706 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
19707 /* If a parse error occurred while parsing the
19708 parameter-declaration-list, then the entire
19709 parameter-declaration-clause is erroneous. */
19713 /* Peek at the next token. */
19714 token
= cp_lexer_peek_token (parser
->lexer
);
19715 /* If it's a `,', the clause should terminate with an ellipsis. */
19716 if (token
->type
== CPP_COMMA
)
19718 /* Consume the `,'. */
19719 cp_lexer_consume_token (parser
->lexer
);
19720 /* Expect an ellipsis. */
19722 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
19724 /* It might also be `...' if the optional trailing `,' was
19726 else if (token
->type
== CPP_ELLIPSIS
)
19728 /* Consume the `...' token. */
19729 cp_lexer_consume_token (parser
->lexer
);
19730 /* And remember that we saw it. */
19734 ellipsis_p
= false;
19736 /* Finish the parameter list. */
19738 parameters
= chainon (parameters
, void_list_node
);
19743 /* Parse a parameter-declaration-list.
19745 parameter-declaration-list:
19746 parameter-declaration
19747 parameter-declaration-list , parameter-declaration
19749 Returns a representation of the parameter-declaration-list, as for
19750 cp_parser_parameter_declaration_clause. However, the
19751 `void_list_node' is never appended to the list. Upon return,
19752 *IS_ERROR will be true iff an error occurred. */
19755 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
19757 tree parameters
= NULL_TREE
;
19758 tree
*tail
= ¶meters
;
19759 bool saved_in_unbraced_linkage_specification_p
;
19762 /* Assume all will go well. */
19764 /* The special considerations that apply to a function within an
19765 unbraced linkage specifications do not apply to the parameters
19766 to the function. */
19767 saved_in_unbraced_linkage_specification_p
19768 = parser
->in_unbraced_linkage_specification_p
;
19769 parser
->in_unbraced_linkage_specification_p
= false;
19771 /* Look for more parameters. */
19774 cp_parameter_declarator
*parameter
;
19775 tree decl
= error_mark_node
;
19776 bool parenthesized_p
= false;
19777 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
19778 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19779 (current_template_parms
)) : 0);
19781 /* Parse the parameter. */
19783 = cp_parser_parameter_declaration (parser
,
19784 /*template_parm_p=*/false,
19787 /* We don't know yet if the enclosing context is deprecated, so wait
19788 and warn in grokparms if appropriate. */
19789 deprecated_state
= DEPRECATED_SUPPRESS
;
19793 /* If a function parameter pack was specified and an implicit template
19794 parameter was introduced during cp_parser_parameter_declaration,
19795 change any implicit parameters introduced into packs. */
19796 if (parser
->implicit_template_parms
19797 && parameter
->declarator
19798 && parameter
->declarator
->parameter_pack_p
)
19800 int latest_template_parm_idx
= TREE_VEC_LENGTH
19801 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
19803 if (latest_template_parm_idx
!= template_parm_idx
)
19804 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
19805 (parameter
->decl_specifiers
.type
,
19806 template_parm_idx
, latest_template_parm_idx
);
19809 decl
= grokdeclarator (parameter
->declarator
,
19810 ¶meter
->decl_specifiers
,
19812 parameter
->default_argument
!= NULL_TREE
,
19813 ¶meter
->decl_specifiers
.attributes
);
19816 deprecated_state
= DEPRECATED_NORMAL
;
19818 /* If a parse error occurred parsing the parameter declaration,
19819 then the entire parameter-declaration-list is erroneous. */
19820 if (decl
== error_mark_node
)
19823 parameters
= error_mark_node
;
19827 if (parameter
->decl_specifiers
.attributes
)
19828 cplus_decl_attributes (&decl
,
19829 parameter
->decl_specifiers
.attributes
,
19831 if (DECL_NAME (decl
))
19832 decl
= pushdecl (decl
);
19834 if (decl
!= error_mark_node
)
19836 retrofit_lang_decl (decl
);
19837 DECL_PARM_INDEX (decl
) = ++index
;
19838 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
19841 /* Add the new parameter to the list. */
19842 *tail
= build_tree_list (parameter
->default_argument
, decl
);
19843 tail
= &TREE_CHAIN (*tail
);
19845 /* Peek at the next token. */
19846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
19847 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
19848 /* These are for Objective-C++ */
19849 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
19850 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19851 /* The parameter-declaration-list is complete. */
19853 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19857 /* Peek at the next token. */
19858 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19859 /* If it's an ellipsis, then the list is complete. */
19860 if (token
->type
== CPP_ELLIPSIS
)
19862 /* Otherwise, there must be more parameters. Consume the
19864 cp_lexer_consume_token (parser
->lexer
);
19865 /* When parsing something like:
19867 int i(float f, double d)
19869 we can tell after seeing the declaration for "f" that we
19870 are not looking at an initialization of a variable "i",
19871 but rather at the declaration of a function "i".
19873 Due to the fact that the parsing of template arguments
19874 (as specified to a template-id) requires backtracking we
19875 cannot use this technique when inside a template argument
19877 if (!parser
->in_template_argument_list_p
19878 && !parser
->in_type_id_in_expr_p
19879 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19880 /* However, a parameter-declaration of the form
19881 "float(f)" (which is a valid declaration of a
19882 parameter "f") can also be interpreted as an
19883 expression (the conversion of "f" to "float"). */
19884 && !parenthesized_p
)
19885 cp_parser_commit_to_tentative_parse (parser
);
19889 cp_parser_error (parser
, "expected %<,%> or %<...%>");
19890 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19891 cp_parser_skip_to_closing_parenthesis (parser
,
19892 /*recovering=*/true,
19893 /*or_comma=*/false,
19894 /*consume_paren=*/false);
19899 parser
->in_unbraced_linkage_specification_p
19900 = saved_in_unbraced_linkage_specification_p
;
19902 /* Reset implicit_template_scope if we are about to leave the function
19903 parameter list that introduced it. Note that for out-of-line member
19904 definitions, there will be one or more class scopes before we get to
19905 the template parameter scope. */
19907 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
19908 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
19910 while (maybe_its
->kind
== sk_class
)
19911 maybe_its
= maybe_its
->level_chain
;
19912 if (maybe_its
== its
)
19914 parser
->implicit_template_parms
= 0;
19915 parser
->implicit_template_scope
= 0;
19922 /* Parse a parameter declaration.
19924 parameter-declaration:
19925 decl-specifier-seq ... [opt] declarator
19926 decl-specifier-seq declarator = assignment-expression
19927 decl-specifier-seq ... [opt] abstract-declarator [opt]
19928 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19930 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19931 declares a template parameter. (In that case, a non-nested `>'
19932 token encountered during the parsing of the assignment-expression
19933 is not interpreted as a greater-than operator.)
19935 Returns a representation of the parameter, or NULL if an error
19936 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19937 true iff the declarator is of the form "(p)". */
19939 static cp_parameter_declarator
*
19940 cp_parser_parameter_declaration (cp_parser
*parser
,
19941 bool template_parm_p
,
19942 bool *parenthesized_p
)
19944 int declares_class_or_enum
;
19945 cp_decl_specifier_seq decl_specifiers
;
19946 cp_declarator
*declarator
;
19947 tree default_argument
;
19948 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
19949 const char *saved_message
;
19950 bool template_parameter_pack_p
= false;
19952 /* In a template parameter, `>' is not an operator.
19956 When parsing a default template-argument for a non-type
19957 template-parameter, the first non-nested `>' is taken as the end
19958 of the template parameter-list rather than a greater-than
19961 /* Type definitions may not appear in parameter types. */
19962 saved_message
= parser
->type_definition_forbidden_message
;
19963 parser
->type_definition_forbidden_message
19964 = G_("types may not be defined in parameter types");
19966 /* Parse the declaration-specifiers. */
19967 cp_parser_decl_specifier_seq (parser
,
19968 CP_PARSER_FLAGS_NONE
,
19970 &declares_class_or_enum
);
19972 /* Complain about missing 'typename' or other invalid type names. */
19973 if (!decl_specifiers
.any_type_specifiers_p
19974 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19975 decl_specifiers
.type
= error_mark_node
;
19977 /* If an error occurred, there's no reason to attempt to parse the
19978 rest of the declaration. */
19979 if (cp_parser_error_occurred (parser
))
19981 parser
->type_definition_forbidden_message
= saved_message
;
19985 /* Peek at the next token. */
19986 token
= cp_lexer_peek_token (parser
->lexer
);
19988 /* If the next token is a `)', `,', `=', `>', or `...', then there
19989 is no declarator. However, when variadic templates are enabled,
19990 there may be a declarator following `...'. */
19991 if (token
->type
== CPP_CLOSE_PAREN
19992 || token
->type
== CPP_COMMA
19993 || token
->type
== CPP_EQ
19994 || token
->type
== CPP_GREATER
)
19997 if (parenthesized_p
)
19998 *parenthesized_p
= false;
20000 /* Otherwise, there should be a declarator. */
20003 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20004 parser
->default_arg_ok_p
= false;
20006 /* After seeing a decl-specifier-seq, if the next token is not a
20007 "(", there is no possibility that the code is a valid
20008 expression. Therefore, if parsing tentatively, we commit at
20010 if (!parser
->in_template_argument_list_p
20011 /* In an expression context, having seen:
20015 we cannot be sure whether we are looking at a
20016 function-type (taking a "char" as a parameter) or a cast
20017 of some object of type "char" to "int". */
20018 && !parser
->in_type_id_in_expr_p
20019 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
20020 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
20021 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
20022 cp_parser_commit_to_tentative_parse (parser
);
20023 /* Parse the declarator. */
20024 declarator_token_start
= token
;
20025 declarator
= cp_parser_declarator (parser
,
20026 CP_PARSER_DECLARATOR_EITHER
,
20027 /*ctor_dtor_or_conv_p=*/NULL
,
20029 /*member_p=*/false,
20030 /*friend_p=*/false);
20031 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20032 /* After the declarator, allow more attributes. */
20033 decl_specifiers
.attributes
20034 = chainon (decl_specifiers
.attributes
,
20035 cp_parser_attributes_opt (parser
));
20037 /* If the declarator is a template parameter pack, remember that and
20038 clear the flag in the declarator itself so we don't get errors
20039 from grokdeclarator. */
20040 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
20042 declarator
->parameter_pack_p
= false;
20043 template_parameter_pack_p
= true;
20047 /* If the next token is an ellipsis, and we have not seen a declarator
20048 name, and if either the type of the declarator contains parameter
20049 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20050 for, eg, abbreviated integral type names), then we actually have a
20051 parameter pack expansion expression. Otherwise, leave the ellipsis
20052 for a C-style variadic function. */
20053 token
= cp_lexer_peek_token (parser
->lexer
);
20054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20056 tree type
= decl_specifiers
.type
;
20058 if (type
&& DECL_P (type
))
20059 type
= TREE_TYPE (type
);
20062 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
20063 && (template_parm_p
|| uses_parameter_packs (type
)))
20064 || (!type
&& template_parm_p
))
20065 && declarator_can_be_parameter_pack (declarator
))
20067 /* Consume the `...'. */
20068 cp_lexer_consume_token (parser
->lexer
);
20069 maybe_warn_variadic_templates ();
20071 /* Build a pack expansion type */
20072 if (template_parm_p
)
20073 template_parameter_pack_p
= true;
20074 else if (declarator
)
20075 declarator
->parameter_pack_p
= true;
20077 decl_specifiers
.type
= make_pack_expansion (type
);
20081 /* The restriction on defining new types applies only to the type
20082 of the parameter, not to the default argument. */
20083 parser
->type_definition_forbidden_message
= saved_message
;
20085 /* If the next token is `=', then process a default argument. */
20086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20088 tree type
= decl_specifiers
.type
;
20089 token
= cp_lexer_peek_token (parser
->lexer
);
20090 /* If we are defining a class, then the tokens that make up the
20091 default argument must be saved and processed later. */
20092 if (!template_parm_p
&& at_class_scope_p ()
20093 && TYPE_BEING_DEFINED (current_class_type
)
20094 && !LAMBDA_TYPE_P (current_class_type
))
20095 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
20097 // A constrained-type-specifier may declare a type template-parameter.
20098 else if (declares_constrained_type_template_parameter (type
))
20100 = cp_parser_default_type_template_argument (parser
);
20102 // A constrained-type-specifier may declare a template-template-parameter.
20103 else if (declares_constrained_template_template_parameter (type
))
20105 = cp_parser_default_template_template_argument (parser
);
20107 /* Outside of a class definition, we can just parse the
20108 assignment-expression. */
20111 = cp_parser_default_argument (parser
, template_parm_p
);
20113 if (!parser
->default_arg_ok_p
)
20115 permerror (token
->location
,
20116 "default arguments are only "
20117 "permitted for function parameters");
20119 else if ((declarator
&& declarator
->parameter_pack_p
)
20120 || template_parameter_pack_p
20121 || (decl_specifiers
.type
20122 && PACK_EXPANSION_P (decl_specifiers
.type
)))
20124 /* Find the name of the parameter pack. */
20125 cp_declarator
*id_declarator
= declarator
;
20126 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
20127 id_declarator
= id_declarator
->declarator
;
20129 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
20130 error_at (declarator_token_start
->location
,
20132 ? G_("template parameter pack %qD "
20133 "cannot have a default argument")
20134 : G_("parameter pack %qD cannot have "
20135 "a default argument"),
20136 id_declarator
->u
.id
.unqualified_name
);
20138 error_at (declarator_token_start
->location
,
20140 ? G_("template parameter pack cannot have "
20141 "a default argument")
20142 : G_("parameter pack cannot have a "
20143 "default argument"));
20145 default_argument
= NULL_TREE
;
20149 default_argument
= NULL_TREE
;
20151 return make_parameter_declarator (&decl_specifiers
,
20154 template_parameter_pack_p
);
20157 /* Parse a default argument and return it.
20159 TEMPLATE_PARM_P is true if this is a default argument for a
20160 non-type template parameter. */
20162 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
20164 tree default_argument
= NULL_TREE
;
20165 bool saved_greater_than_is_operator_p
;
20166 bool saved_local_variables_forbidden_p
;
20167 bool non_constant_p
, is_direct_init
;
20169 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20171 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
20172 parser
->greater_than_is_operator_p
= !template_parm_p
;
20173 /* Local variable names (and the `this' keyword) may not
20174 appear in a default argument. */
20175 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
20176 parser
->local_variables_forbidden_p
= true;
20177 /* Parse the assignment-expression. */
20178 if (template_parm_p
)
20179 push_deferring_access_checks (dk_no_deferred
);
20180 tree saved_class_ptr
= NULL_TREE
;
20181 tree saved_class_ref
= NULL_TREE
;
20182 /* The "this" pointer is not valid in a default argument. */
20185 saved_class_ptr
= current_class_ptr
;
20186 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
20187 saved_class_ref
= current_class_ref
;
20188 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
20191 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
20192 /* Restore the "this" pointer. */
20195 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
20196 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
20198 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
20199 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20200 if (template_parm_p
)
20201 pop_deferring_access_checks ();
20202 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
20203 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20205 return default_argument
;
20208 /* Parse a function-body.
20211 compound_statement */
20214 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
20216 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
20217 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
20221 /* Parse a ctor-initializer-opt followed by a function-body. Return
20222 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20223 is true we are parsing a function-try-block. */
20226 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
20227 bool in_function_try_block
)
20230 bool ctor_initializer_p
;
20231 const bool check_body_p
=
20232 DECL_CONSTRUCTOR_P (current_function_decl
)
20233 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
20236 /* Begin the function body. */
20237 body
= begin_function_body ();
20238 /* Parse the optional ctor-initializer. */
20239 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
20241 /* If we're parsing a constexpr constructor definition, we need
20242 to check that the constructor body is indeed empty. However,
20243 before we get to cp_parser_function_body lot of junk has been
20244 generated, so we can't just check that we have an empty block.
20245 Rather we take a snapshot of the outermost block, and check whether
20246 cp_parser_function_body changed its state. */
20249 list
= cur_stmt_list
;
20250 if (STATEMENT_LIST_TAIL (list
))
20251 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
20253 /* Parse the function-body. */
20254 cp_parser_function_body (parser
, in_function_try_block
);
20256 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
20257 /* Finish the function body. */
20258 finish_function_body (body
);
20260 return ctor_initializer_p
;
20263 /* Parse an initializer.
20266 = initializer-clause
20267 ( expression-list )
20269 Returns an expression representing the initializer. If no
20270 initializer is present, NULL_TREE is returned.
20272 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20273 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20274 set to TRUE if there is no initializer present. If there is an
20275 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20276 is set to true; otherwise it is set to false. */
20279 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
20280 bool* non_constant_p
)
20285 /* Peek at the next token. */
20286 token
= cp_lexer_peek_token (parser
->lexer
);
20288 /* Let our caller know whether or not this initializer was
20290 *is_direct_init
= (token
->type
!= CPP_EQ
);
20291 /* Assume that the initializer is constant. */
20292 *non_constant_p
= false;
20294 if (token
->type
== CPP_EQ
)
20296 /* Consume the `='. */
20297 cp_lexer_consume_token (parser
->lexer
);
20298 /* Parse the initializer-clause. */
20299 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
20301 else if (token
->type
== CPP_OPEN_PAREN
)
20303 vec
<tree
, va_gc
> *vec
;
20304 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
20306 /*allow_expansion_p=*/true,
20309 return error_mark_node
;
20310 init
= build_tree_list_vec (vec
);
20311 release_tree_vector (vec
);
20313 else if (token
->type
== CPP_OPEN_BRACE
)
20315 cp_lexer_set_source_position (parser
->lexer
);
20316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20317 init
= cp_parser_braced_list (parser
, non_constant_p
);
20318 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
20322 /* Anything else is an error. */
20323 cp_parser_error (parser
, "expected initializer");
20324 init
= error_mark_node
;
20330 /* Parse an initializer-clause.
20332 initializer-clause:
20333 assignment-expression
20336 Returns an expression representing the initializer.
20338 If the `assignment-expression' production is used the value
20339 returned is simply a representation for the expression.
20341 Otherwise, calls cp_parser_braced_list. */
20344 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
20348 /* Assume the expression is constant. */
20349 *non_constant_p
= false;
20351 /* If it is not a `{', then we are looking at an
20352 assignment-expression. */
20353 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
20356 = cp_parser_constant_expression (parser
,
20357 /*allow_non_constant_p=*/true,
20361 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
20363 return initializer
;
20366 /* Parse a brace-enclosed initializer list.
20369 { initializer-list , [opt] }
20372 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20373 the elements of the initializer-list (or NULL, if the last
20374 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20375 NULL_TREE. There is no way to detect whether or not the optional
20376 trailing `,' was provided. NON_CONSTANT_P is as for
20377 cp_parser_initializer. */
20380 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
20384 /* Consume the `{' token. */
20385 cp_lexer_consume_token (parser
->lexer
);
20386 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20387 initializer
= make_node (CONSTRUCTOR
);
20388 /* If it's not a `}', then there is a non-trivial initializer. */
20389 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
20391 /* Parse the initializer list. */
20392 CONSTRUCTOR_ELTS (initializer
)
20393 = cp_parser_initializer_list (parser
, non_constant_p
);
20394 /* A trailing `,' token is allowed. */
20395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20396 cp_lexer_consume_token (parser
->lexer
);
20399 *non_constant_p
= false;
20400 /* Now, there should be a trailing `}'. */
20401 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20402 TREE_TYPE (initializer
) = init_list_type_node
;
20403 return initializer
;
20406 /* Consume tokens up to, and including, the next non-nested closing `]'.
20407 Returns true iff we found a closing `]'. */
20410 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
20412 unsigned square_depth
= 0;
20416 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
20418 switch (token
->type
)
20421 case CPP_PRAGMA_EOL
:
20422 /* If we've run out of tokens, then there is no closing `]'. */
20425 case CPP_OPEN_SQUARE
:
20429 case CPP_CLOSE_SQUARE
:
20430 if (!square_depth
--)
20432 cp_lexer_consume_token (parser
->lexer
);
20441 /* Consume the token. */
20442 cp_lexer_consume_token (parser
->lexer
);
20446 /* Return true if we are looking at an array-designator, false otherwise. */
20449 cp_parser_array_designator_p (cp_parser
*parser
)
20451 /* Consume the `['. */
20452 cp_lexer_consume_token (parser
->lexer
);
20454 cp_lexer_save_tokens (parser
->lexer
);
20456 /* Skip tokens until the next token is a closing square bracket.
20457 If we find the closing `]', and the next token is a `=', then
20458 we are looking at an array designator. */
20459 bool array_designator_p
20460 = (cp_parser_skip_to_closing_square_bracket (parser
)
20461 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
20463 /* Roll back the tokens we skipped. */
20464 cp_lexer_rollback_tokens (parser
->lexer
);
20466 return array_designator_p
;
20469 /* Parse an initializer-list.
20472 initializer-clause ... [opt]
20473 initializer-list , initializer-clause ... [opt]
20478 designation initializer-clause ...[opt]
20479 initializer-list , designation initializer-clause ...[opt]
20484 [ constant-expression ] =
20486 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20487 for the initializer. If the INDEX of the elt is non-NULL, it is the
20488 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20489 as for cp_parser_initializer. */
20491 static vec
<constructor_elt
, va_gc
> *
20492 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
20494 vec
<constructor_elt
, va_gc
> *v
= NULL
;
20496 /* Assume all of the expressions are constant. */
20497 *non_constant_p
= false;
20499 /* Parse the rest of the list. */
20505 bool clause_non_constant_p
;
20507 /* If the next token is an identifier and the following one is a
20508 colon, we are looking at the GNU designated-initializer
20510 if (cp_parser_allow_gnu_extensions_p (parser
)
20511 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
20512 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
20514 /* Warn the user that they are using an extension. */
20515 pedwarn (input_location
, OPT_Wpedantic
,
20516 "ISO C++ does not allow designated initializers");
20517 /* Consume the identifier. */
20518 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
20519 /* Consume the `:'. */
20520 cp_lexer_consume_token (parser
->lexer
);
20522 /* Also handle the C99 syntax, '. id ='. */
20523 else if (cp_parser_allow_gnu_extensions_p (parser
)
20524 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
20525 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
20526 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
20528 /* Warn the user that they are using an extension. */
20529 pedwarn (input_location
, OPT_Wpedantic
,
20530 "ISO C++ does not allow C99 designated initializers");
20531 /* Consume the `.'. */
20532 cp_lexer_consume_token (parser
->lexer
);
20533 /* Consume the identifier. */
20534 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
20535 /* Consume the `='. */
20536 cp_lexer_consume_token (parser
->lexer
);
20538 /* Also handle C99 array designators, '[ const ] ='. */
20539 else if (cp_parser_allow_gnu_extensions_p (parser
)
20540 && !c_dialect_objc ()
20541 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
20543 /* In C++11, [ could start a lambda-introducer. */
20544 bool non_const
= false;
20546 cp_parser_parse_tentatively (parser
);
20548 if (!cp_parser_array_designator_p (parser
))
20550 cp_parser_simulate_error (parser
);
20551 designator
= NULL_TREE
;
20555 designator
= cp_parser_constant_expression (parser
, true,
20557 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
20558 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20561 if (!cp_parser_parse_definitely (parser
))
20562 designator
= NULL_TREE
;
20563 else if (non_const
)
20564 require_potential_rvalue_constant_expression (designator
);
20567 designator
= NULL_TREE
;
20569 /* Parse the initializer. */
20570 initializer
= cp_parser_initializer_clause (parser
,
20571 &clause_non_constant_p
);
20572 /* If any clause is non-constant, so is the entire initializer. */
20573 if (clause_non_constant_p
)
20574 *non_constant_p
= true;
20576 /* If we have an ellipsis, this is an initializer pack
20578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20580 /* Consume the `...'. */
20581 cp_lexer_consume_token (parser
->lexer
);
20583 /* Turn the initializer into an initializer expansion. */
20584 initializer
= make_pack_expansion (initializer
);
20587 /* Add it to the vector. */
20588 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
20590 /* If the next token is not a comma, we have reached the end of
20592 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20595 /* Peek at the next token. */
20596 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20597 /* If the next token is a `}', then we're still done. An
20598 initializer-clause can have a trailing `,' after the
20599 initializer-list and before the closing `}'. */
20600 if (token
->type
== CPP_CLOSE_BRACE
)
20603 /* Consume the `,' token. */
20604 cp_lexer_consume_token (parser
->lexer
);
20610 /* Classes [gram.class] */
20612 /* Parse a class-name.
20618 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20619 to indicate that names looked up in dependent types should be
20620 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20621 keyword has been used to indicate that the name that appears next
20622 is a template. TAG_TYPE indicates the explicit tag given before
20623 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20624 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20625 is the class being defined in a class-head. If ENUM_OK is TRUE,
20626 enum-names are also accepted.
20628 Returns the TYPE_DECL representing the class. */
20631 cp_parser_class_name (cp_parser
*parser
,
20632 bool typename_keyword_p
,
20633 bool template_keyword_p
,
20634 enum tag_types tag_type
,
20635 bool check_dependency_p
,
20637 bool is_declaration
,
20644 tree identifier
= NULL_TREE
;
20646 /* All class-names start with an identifier. */
20647 token
= cp_lexer_peek_token (parser
->lexer
);
20648 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
20650 cp_parser_error (parser
, "expected class-name");
20651 return error_mark_node
;
20654 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20655 to a template-id, so we save it here. */
20656 scope
= parser
->scope
;
20657 if (scope
== error_mark_node
)
20658 return error_mark_node
;
20660 /* Any name names a type if we're following the `typename' keyword
20661 in a qualified name where the enclosing scope is type-dependent. */
20662 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
20663 && dependent_type_p (scope
));
20664 /* Handle the common case (an identifier, but not a template-id)
20666 if (token
->type
== CPP_NAME
20667 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
20669 cp_token
*identifier_token
;
20672 /* Look for the identifier. */
20673 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
20674 ambiguous_p
= identifier_token
->error_reported
;
20675 identifier
= cp_parser_identifier (parser
);
20676 /* If the next token isn't an identifier, we are certainly not
20677 looking at a class-name. */
20678 if (identifier
== error_mark_node
)
20679 decl
= error_mark_node
;
20680 /* If we know this is a type-name, there's no need to look it
20682 else if (typename_p
)
20686 tree ambiguous_decls
;
20687 /* If we already know that this lookup is ambiguous, then
20688 we've already issued an error message; there's no reason
20692 cp_parser_simulate_error (parser
);
20693 return error_mark_node
;
20695 /* If the next token is a `::', then the name must be a type
20698 [basic.lookup.qual]
20700 During the lookup for a name preceding the :: scope
20701 resolution operator, object, function, and enumerator
20702 names are ignored. */
20703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20704 tag_type
= typename_type
;
20705 /* Look up the name. */
20706 decl
= cp_parser_lookup_name (parser
, identifier
,
20708 /*is_template=*/false,
20709 /*is_namespace=*/false,
20710 check_dependency_p
,
20712 identifier_token
->location
);
20713 if (ambiguous_decls
)
20715 if (cp_parser_parsing_tentatively (parser
))
20716 cp_parser_simulate_error (parser
);
20717 return error_mark_node
;
20723 /* Try a template-id. */
20724 decl
= cp_parser_template_id (parser
, template_keyword_p
,
20725 check_dependency_p
,
20728 if (decl
== error_mark_node
)
20729 return error_mark_node
;
20732 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
20734 /* If this is a typename, create a TYPENAME_TYPE. */
20735 if (typename_p
&& decl
!= error_mark_node
)
20737 decl
= make_typename_type (scope
, decl
, typename_type
,
20738 /*complain=*/tf_error
);
20739 if (decl
!= error_mark_node
)
20740 decl
= TYPE_NAME (decl
);
20743 decl
= strip_using_decl (decl
);
20745 /* Check to see that it is really the name of a class. */
20746 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
20747 && identifier_p (TREE_OPERAND (decl
, 0))
20748 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20749 /* Situations like this:
20751 template <typename T> struct A {
20752 typename T::template X<int>::I i;
20755 are problematic. Is `T::template X<int>' a class-name? The
20756 standard does not seem to be definitive, but there is no other
20757 valid interpretation of the following `::'. Therefore, those
20758 names are considered class-names. */
20760 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
20761 if (decl
!= error_mark_node
)
20762 decl
= TYPE_NAME (decl
);
20764 else if (TREE_CODE (decl
) != TYPE_DECL
20765 || TREE_TYPE (decl
) == error_mark_node
20766 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
20767 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
20768 /* In Objective-C 2.0, a classname followed by '.' starts a
20769 dot-syntax expression, and it's not a type-name. */
20770 || (c_dialect_objc ()
20771 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
20772 && objc_is_class_name (decl
)))
20773 decl
= error_mark_node
;
20775 if (decl
== error_mark_node
)
20776 cp_parser_error (parser
, "expected class-name");
20777 else if (identifier
&& !parser
->scope
)
20778 maybe_note_name_used_in_class (identifier
, decl
);
20783 /* Parse a class-specifier.
20786 class-head { member-specification [opt] }
20788 Returns the TREE_TYPE representing the class. */
20791 cp_parser_class_specifier_1 (cp_parser
* parser
)
20794 tree attributes
= NULL_TREE
;
20795 bool nested_name_specifier_p
;
20796 unsigned saved_num_template_parameter_lists
;
20797 bool saved_in_function_body
;
20798 unsigned char in_statement
;
20799 bool in_switch_statement_p
;
20800 bool saved_in_unbraced_linkage_specification_p
;
20801 tree old_scope
= NULL_TREE
;
20802 tree scope
= NULL_TREE
;
20803 cp_token
*closing_brace
;
20805 push_deferring_access_checks (dk_no_deferred
);
20807 /* Parse the class-head. */
20808 type
= cp_parser_class_head (parser
,
20809 &nested_name_specifier_p
);
20810 /* If the class-head was a semantic disaster, skip the entire body
20814 cp_parser_skip_to_end_of_block_or_statement (parser
);
20815 pop_deferring_access_checks ();
20816 return error_mark_node
;
20819 /* Look for the `{'. */
20820 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
20822 pop_deferring_access_checks ();
20823 return error_mark_node
;
20826 cp_ensure_no_omp_declare_simd (parser
);
20827 cp_ensure_no_oacc_routine (parser
);
20829 /* Issue an error message if type-definitions are forbidden here. */
20830 cp_parser_check_type_definition (parser
);
20831 /* Remember that we are defining one more class. */
20832 ++parser
->num_classes_being_defined
;
20833 /* Inside the class, surrounding template-parameter-lists do not
20835 saved_num_template_parameter_lists
20836 = parser
->num_template_parameter_lists
;
20837 parser
->num_template_parameter_lists
= 0;
20838 /* We are not in a function body. */
20839 saved_in_function_body
= parser
->in_function_body
;
20840 parser
->in_function_body
= false;
20841 /* Or in a loop. */
20842 in_statement
= parser
->in_statement
;
20843 parser
->in_statement
= 0;
20844 /* Or in a switch. */
20845 in_switch_statement_p
= parser
->in_switch_statement_p
;
20846 parser
->in_switch_statement_p
= false;
20847 /* We are not immediately inside an extern "lang" block. */
20848 saved_in_unbraced_linkage_specification_p
20849 = parser
->in_unbraced_linkage_specification_p
;
20850 parser
->in_unbraced_linkage_specification_p
= false;
20852 // Associate constraints with the type.
20854 type
= associate_classtype_constraints (type
);
20856 /* Start the class. */
20857 if (nested_name_specifier_p
)
20859 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
20860 old_scope
= push_inner_scope (scope
);
20862 type
= begin_class_definition (type
);
20864 if (type
== error_mark_node
)
20865 /* If the type is erroneous, skip the entire body of the class. */
20866 cp_parser_skip_to_closing_brace (parser
);
20868 /* Parse the member-specification. */
20869 cp_parser_member_specification_opt (parser
);
20871 /* Look for the trailing `}'. */
20872 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20873 /* Look for trailing attributes to apply to this class. */
20874 if (cp_parser_allow_gnu_extensions_p (parser
))
20875 attributes
= cp_parser_gnu_attributes_opt (parser
);
20876 if (type
!= error_mark_node
)
20877 type
= finish_struct (type
, attributes
);
20878 if (nested_name_specifier_p
)
20879 pop_inner_scope (old_scope
, scope
);
20881 /* We've finished a type definition. Check for the common syntax
20882 error of forgetting a semicolon after the definition. We need to
20883 be careful, as we can't just check for not-a-semicolon and be done
20884 with it; the user might have typed:
20886 class X { } c = ...;
20887 class X { } *p = ...;
20889 and so forth. Instead, enumerate all the possible tokens that
20890 might follow this production; if we don't see one of them, then
20891 complain and silently insert the semicolon. */
20893 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20894 bool want_semicolon
= true;
20896 if (cp_next_tokens_can_be_std_attribute_p (parser
))
20897 /* Don't try to parse c++11 attributes here. As per the
20898 grammar, that should be a task for
20899 cp_parser_decl_specifier_seq. */
20900 want_semicolon
= false;
20902 switch (token
->type
)
20905 case CPP_SEMICOLON
:
20908 case CPP_OPEN_PAREN
:
20909 case CPP_CLOSE_PAREN
:
20911 want_semicolon
= false;
20914 /* While it's legal for type qualifiers and storage class
20915 specifiers to follow type definitions in the grammar, only
20916 compiler testsuites contain code like that. Assume that if
20917 we see such code, then what we're really seeing is a case
20921 const <type> var = ...;
20926 static <type> func (...) ...
20928 i.e. the qualifier or specifier applies to the next
20929 declaration. To do so, however, we need to look ahead one
20930 more token to see if *that* token is a type specifier.
20932 This code could be improved to handle:
20935 static const <type> var = ...; */
20937 if (keyword_is_decl_specifier (token
->keyword
))
20939 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20941 /* Handling user-defined types here would be nice, but very
20944 = (lookahead
->type
== CPP_KEYWORD
20945 && keyword_begins_type_specifier (lookahead
->keyword
));
20952 /* If we don't have a type, then something is very wrong and we
20953 shouldn't try to do anything clever. Likewise for not seeing the
20955 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
20957 cp_token_position prev
20958 = cp_lexer_previous_token_position (parser
->lexer
);
20959 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
20960 location_t loc
= prev_token
->location
;
20962 if (CLASSTYPE_DECLARED_CLASS (type
))
20963 error_at (loc
, "expected %<;%> after class definition");
20964 else if (TREE_CODE (type
) == RECORD_TYPE
)
20965 error_at (loc
, "expected %<;%> after struct definition");
20966 else if (TREE_CODE (type
) == UNION_TYPE
)
20967 error_at (loc
, "expected %<;%> after union definition");
20969 gcc_unreachable ();
20971 /* Unget one token and smash it to look as though we encountered
20972 a semicolon in the input stream. */
20973 cp_lexer_set_token_position (parser
->lexer
, prev
);
20974 token
= cp_lexer_peek_token (parser
->lexer
);
20975 token
->type
= CPP_SEMICOLON
;
20976 token
->keyword
= RID_MAX
;
20980 /* If this class is not itself within the scope of another class,
20981 then we need to parse the bodies of all of the queued function
20982 definitions. Note that the queued functions defined in a class
20983 are not always processed immediately following the
20984 class-specifier for that class. Consider:
20987 struct B { void f() { sizeof (A); } };
20990 If `f' were processed before the processing of `A' were
20991 completed, there would be no way to compute the size of `A'.
20992 Note that the nesting we are interested in here is lexical --
20993 not the semantic nesting given by TYPE_CONTEXT. In particular,
20996 struct A { struct B; };
20997 struct A::B { void f() { } };
20999 there is no need to delay the parsing of `A::B::f'. */
21000 if (--parser
->num_classes_being_defined
== 0)
21003 tree class_type
= NULL_TREE
;
21004 tree pushed_scope
= NULL_TREE
;
21006 cp_default_arg_entry
*e
;
21007 tree save_ccp
, save_ccr
;
21009 /* In a first pass, parse default arguments to the functions.
21010 Then, in a second pass, parse the bodies of the functions.
21011 This two-phased approach handles cases like:
21019 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
21022 /* If there are default arguments that have not yet been processed,
21023 take care of them now. */
21024 if (class_type
!= e
->class_type
)
21027 pop_scope (pushed_scope
);
21028 class_type
= e
->class_type
;
21029 pushed_scope
= push_scope (class_type
);
21031 /* Make sure that any template parameters are in scope. */
21032 maybe_begin_member_template_processing (decl
);
21033 /* Parse the default argument expressions. */
21034 cp_parser_late_parsing_default_args (parser
, decl
);
21035 /* Remove any template parameters from the symbol table. */
21036 maybe_end_member_template_processing ();
21038 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
21039 /* Now parse any NSDMIs. */
21040 save_ccp
= current_class_ptr
;
21041 save_ccr
= current_class_ref
;
21042 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
21044 if (class_type
!= DECL_CONTEXT (decl
))
21047 pop_scope (pushed_scope
);
21048 class_type
= DECL_CONTEXT (decl
);
21049 pushed_scope
= push_scope (class_type
);
21051 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
21052 cp_parser_late_parsing_nsdmi (parser
, decl
);
21054 vec_safe_truncate (unparsed_nsdmis
, 0);
21055 current_class_ptr
= save_ccp
;
21056 current_class_ref
= save_ccr
;
21058 pop_scope (pushed_scope
);
21060 /* Now do some post-NSDMI bookkeeping. */
21061 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
21062 after_nsdmi_defaulted_late_checks (class_type
);
21063 vec_safe_truncate (unparsed_classes
, 0);
21064 after_nsdmi_defaulted_late_checks (type
);
21066 /* Now parse the body of the functions. */
21069 /* OpenMP UDRs need to be parsed before all other functions. */
21070 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21071 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
21072 cp_parser_late_parsing_for_member (parser
, decl
);
21073 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21074 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
21075 cp_parser_late_parsing_for_member (parser
, decl
);
21078 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21079 cp_parser_late_parsing_for_member (parser
, decl
);
21080 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
21083 vec_safe_push (unparsed_classes
, type
);
21085 /* Put back any saved access checks. */
21086 pop_deferring_access_checks ();
21088 /* Restore saved state. */
21089 parser
->in_switch_statement_p
= in_switch_statement_p
;
21090 parser
->in_statement
= in_statement
;
21091 parser
->in_function_body
= saved_in_function_body
;
21092 parser
->num_template_parameter_lists
21093 = saved_num_template_parameter_lists
;
21094 parser
->in_unbraced_linkage_specification_p
21095 = saved_in_unbraced_linkage_specification_p
;
21101 cp_parser_class_specifier (cp_parser
* parser
)
21104 timevar_push (TV_PARSE_STRUCT
);
21105 ret
= cp_parser_class_specifier_1 (parser
);
21106 timevar_pop (TV_PARSE_STRUCT
);
21110 /* Parse a class-head.
21113 class-key identifier [opt] base-clause [opt]
21114 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21115 class-key nested-name-specifier [opt] template-id
21118 class-virt-specifier:
21122 class-key attributes identifier [opt] base-clause [opt]
21123 class-key attributes nested-name-specifier identifier base-clause [opt]
21124 class-key attributes nested-name-specifier [opt] template-id
21127 Upon return BASES is initialized to the list of base classes (or
21128 NULL, if there are none) in the same form returned by
21129 cp_parser_base_clause.
21131 Returns the TYPE of the indicated class. Sets
21132 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21133 involving a nested-name-specifier was used, and FALSE otherwise.
21135 Returns error_mark_node if this is not a class-head.
21137 Returns NULL_TREE if the class-head is syntactically valid, but
21138 semantically invalid in a way that means we should skip the entire
21139 body of the class. */
21142 cp_parser_class_head (cp_parser
* parser
,
21143 bool* nested_name_specifier_p
)
21145 tree nested_name_specifier
;
21146 enum tag_types class_key
;
21147 tree id
= NULL_TREE
;
21148 tree type
= NULL_TREE
;
21151 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21152 bool template_id_p
= false;
21153 bool qualified_p
= false;
21154 bool invalid_nested_name_p
= false;
21155 bool invalid_explicit_specialization_p
= false;
21156 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
21157 tree pushed_scope
= NULL_TREE
;
21158 unsigned num_templates
;
21159 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
21160 /* Assume no nested-name-specifier will be present. */
21161 *nested_name_specifier_p
= false;
21162 /* Assume no template parameter lists will be used in defining the
21165 parser
->colon_corrects_to_scope_p
= false;
21167 /* Look for the class-key. */
21168 class_key
= cp_parser_class_key (parser
);
21169 if (class_key
== none_type
)
21170 return error_mark_node
;
21172 /* Parse the attributes. */
21173 attributes
= cp_parser_attributes_opt (parser
);
21175 /* If the next token is `::', that is invalid -- but sometimes
21176 people do try to write:
21180 Handle this gracefully by accepting the extra qualifier, and then
21181 issuing an error about it later if this really is a
21182 class-head. If it turns out just to be an elaborated type
21183 specifier, remain silent. */
21184 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
21185 qualified_p
= true;
21187 push_deferring_access_checks (dk_no_check
);
21189 /* Determine the name of the class. Begin by looking for an
21190 optional nested-name-specifier. */
21191 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
21192 nested_name_specifier
21193 = cp_parser_nested_name_specifier_opt (parser
,
21194 /*typename_keyword_p=*/false,
21195 /*check_dependency_p=*/false,
21197 /*is_declaration=*/false);
21198 /* If there was a nested-name-specifier, then there *must* be an
21200 if (nested_name_specifier
)
21202 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21203 /* Although the grammar says `identifier', it really means
21204 `class-name' or `template-name'. You are only allowed to
21205 define a class that has already been declared with this
21208 The proposed resolution for Core Issue 180 says that wherever
21209 you see `class T::X' you should treat `X' as a type-name.
21211 It is OK to define an inaccessible class; for example:
21213 class A { class B; };
21216 We do not know if we will see a class-name, or a
21217 template-name. We look for a class-name first, in case the
21218 class-name is a template-id; if we looked for the
21219 template-name first we would stop after the template-name. */
21220 cp_parser_parse_tentatively (parser
);
21221 type
= cp_parser_class_name (parser
,
21222 /*typename_keyword_p=*/false,
21223 /*template_keyword_p=*/false,
21225 /*check_dependency_p=*/false,
21226 /*class_head_p=*/true,
21227 /*is_declaration=*/false);
21228 /* If that didn't work, ignore the nested-name-specifier. */
21229 if (!cp_parser_parse_definitely (parser
))
21231 invalid_nested_name_p
= true;
21232 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21233 id
= cp_parser_identifier (parser
);
21234 if (id
== error_mark_node
)
21237 /* If we could not find a corresponding TYPE, treat this
21238 declaration like an unqualified declaration. */
21239 if (type
== error_mark_node
)
21240 nested_name_specifier
= NULL_TREE
;
21241 /* Otherwise, count the number of templates used in TYPE and its
21242 containing scopes. */
21247 for (scope
= TREE_TYPE (type
);
21248 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
21249 scope
= get_containing_scope (scope
))
21251 && CLASS_TYPE_P (scope
)
21252 && CLASSTYPE_TEMPLATE_INFO (scope
)
21253 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
21254 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
21255 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
21259 /* Otherwise, the identifier is optional. */
21262 /* We don't know whether what comes next is a template-id,
21263 an identifier, or nothing at all. */
21264 cp_parser_parse_tentatively (parser
);
21265 /* Check for a template-id. */
21266 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21267 id
= cp_parser_template_id (parser
,
21268 /*template_keyword_p=*/false,
21269 /*check_dependency_p=*/true,
21271 /*is_declaration=*/true);
21272 /* If that didn't work, it could still be an identifier. */
21273 if (!cp_parser_parse_definitely (parser
))
21275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21277 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21278 id
= cp_parser_identifier (parser
);
21285 template_id_p
= true;
21290 pop_deferring_access_checks ();
21294 cp_parser_check_for_invalid_template_id (parser
, id
,
21296 type_start_token
->location
);
21298 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21300 /* If it's not a `:' or a `{' then we can't really be looking at a
21301 class-head, since a class-head only appears as part of a
21302 class-specifier. We have to detect this situation before calling
21303 xref_tag, since that has irreversible side-effects. */
21304 if (!cp_parser_next_token_starts_class_definition_p (parser
))
21306 cp_parser_error (parser
, "expected %<{%> or %<:%>");
21307 type
= error_mark_node
;
21311 /* At this point, we're going ahead with the class-specifier, even
21312 if some other problem occurs. */
21313 cp_parser_commit_to_tentative_parse (parser
);
21314 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
21316 cp_parser_error (parser
,
21317 "cannot specify %<override%> for a class");
21318 type
= error_mark_node
;
21321 /* Issue the error about the overly-qualified name now. */
21324 cp_parser_error (parser
,
21325 "global qualification of class name is invalid");
21326 type
= error_mark_node
;
21329 else if (invalid_nested_name_p
)
21331 cp_parser_error (parser
,
21332 "qualified name does not name a class");
21333 type
= error_mark_node
;
21336 else if (nested_name_specifier
)
21340 /* Reject typedef-names in class heads. */
21341 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
21343 error_at (type_start_token
->location
,
21344 "invalid class name in declaration of %qD",
21350 /* Figure out in what scope the declaration is being placed. */
21351 scope
= current_scope ();
21352 /* If that scope does not contain the scope in which the
21353 class was originally declared, the program is invalid. */
21354 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
21356 if (at_namespace_scope_p ())
21357 error_at (type_start_token
->location
,
21358 "declaration of %qD in namespace %qD which does not "
21360 type
, scope
, nested_name_specifier
);
21362 error_at (type_start_token
->location
,
21363 "declaration of %qD in %qD which does not enclose %qD",
21364 type
, scope
, nested_name_specifier
);
21370 A declarator-id shall not be qualified except for the
21371 definition of a ... nested class outside of its class
21372 ... [or] the definition or explicit instantiation of a
21373 class member of a namespace outside of its namespace. */
21374 if (scope
== nested_name_specifier
)
21376 permerror (nested_name_specifier_token_start
->location
,
21377 "extra qualification not allowed");
21378 nested_name_specifier
= NULL_TREE
;
21382 /* An explicit-specialization must be preceded by "template <>". If
21383 it is not, try to recover gracefully. */
21384 if (at_namespace_scope_p ()
21385 && parser
->num_template_parameter_lists
== 0
21388 error_at (type_start_token
->location
,
21389 "an explicit specialization must be preceded by %<template <>%>");
21390 invalid_explicit_specialization_p
= true;
21391 /* Take the same action that would have been taken by
21392 cp_parser_explicit_specialization. */
21393 ++parser
->num_template_parameter_lists
;
21394 begin_specialization ();
21396 /* There must be no "return" statements between this point and the
21397 end of this function; set "type "to the correct return value and
21398 use "goto done;" to return. */
21399 /* Make sure that the right number of template parameters were
21401 if (!cp_parser_check_template_parameters (parser
, num_templates
,
21402 type_start_token
->location
,
21403 /*declarator=*/NULL
))
21405 /* If something went wrong, there is no point in even trying to
21406 process the class-definition. */
21411 /* Look up the type. */
21414 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
21415 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
21416 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
21418 error_at (type_start_token
->location
,
21419 "function template %qD redeclared as a class template", id
);
21420 type
= error_mark_node
;
21424 type
= TREE_TYPE (id
);
21425 type
= maybe_process_partial_specialization (type
);
21427 if (nested_name_specifier
)
21428 pushed_scope
= push_scope (nested_name_specifier
);
21430 else if (nested_name_specifier
)
21436 template <typename T> struct S { struct T };
21437 template <typename T> struct S<T>::T { };
21439 we will get a TYPENAME_TYPE when processing the definition of
21440 `S::T'. We need to resolve it to the actual type before we
21441 try to define it. */
21442 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
21444 class_type
= resolve_typename_type (TREE_TYPE (type
),
21445 /*only_current_p=*/false);
21446 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
21447 type
= TYPE_NAME (class_type
);
21450 cp_parser_error (parser
, "could not resolve typename type");
21451 type
= error_mark_node
;
21455 if (maybe_process_partial_specialization (TREE_TYPE (type
))
21456 == error_mark_node
)
21462 class_type
= current_class_type
;
21463 /* Enter the scope indicated by the nested-name-specifier. */
21464 pushed_scope
= push_scope (nested_name_specifier
);
21465 /* Get the canonical version of this type. */
21466 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
21467 /* Call push_template_decl if it seems like we should be defining a
21468 template either from the template headers or the type we're
21469 defining, so that we diagnose both extra and missing headers. */
21470 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21471 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
21472 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
21474 type
= push_template_decl (type
);
21475 if (type
== error_mark_node
)
21482 type
= TREE_TYPE (type
);
21483 *nested_name_specifier_p
= true;
21485 else /* The name is not a nested name. */
21487 /* If the class was unnamed, create a dummy name. */
21489 id
= make_anon_name ();
21490 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
21491 parser
->num_template_parameter_lists
);
21494 /* Indicate whether this class was declared as a `class' or as a
21496 if (TREE_CODE (type
) == RECORD_TYPE
)
21497 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
21498 cp_parser_check_class_key (class_key
, type
);
21500 /* If this type was already complete, and we see another definition,
21501 that's an error. */
21502 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
21504 error_at (type_start_token
->location
, "redefinition of %q#T",
21506 error_at (type_start_token
->location
, "previous definition of %q+#T",
21511 else if (type
== error_mark_node
)
21516 /* Apply attributes now, before any use of the class as a template
21517 argument in its base list. */
21518 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
21519 fixup_attribute_variants (type
);
21522 /* We will have entered the scope containing the class; the names of
21523 base classes should be looked up in that context. For example:
21525 struct A { struct B {}; struct C; };
21526 struct A::C : B {};
21530 /* Get the list of base-classes, if there is one. */
21531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21533 /* PR59482: enter the class scope so that base-specifiers are looked
21537 bases
= cp_parser_base_clause (parser
);
21538 /* PR59482: get out of the previously pushed class scope so that the
21539 subsequent pops pop the right thing. */
21546 /* If we're really defining a class, process the base classes.
21547 If they're invalid, fail. */
21548 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
21549 && !xref_basetypes (type
, bases
))
21553 /* Leave the scope given by the nested-name-specifier. We will
21554 enter the class scope itself while processing the members. */
21556 pop_scope (pushed_scope
);
21558 if (invalid_explicit_specialization_p
)
21560 end_specialization ();
21561 --parser
->num_template_parameter_lists
;
21565 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
21566 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
21567 CLASSTYPE_FINAL (type
) = 1;
21569 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21573 /* Parse a class-key.
21580 Returns the kind of class-key specified, or none_type to indicate
21583 static enum tag_types
21584 cp_parser_class_key (cp_parser
* parser
)
21587 enum tag_types tag_type
;
21589 /* Look for the class-key. */
21590 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
21594 /* Check to see if the TOKEN is a class-key. */
21595 tag_type
= cp_parser_token_is_class_key (token
);
21597 cp_parser_error (parser
, "expected class-key");
21601 /* Parse a type-parameter-key.
21603 type-parameter-key:
21609 cp_parser_type_parameter_key (cp_parser
* parser
)
21611 /* Look for the type-parameter-key. */
21612 enum tag_types tag_type
= none_type
;
21613 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21614 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
21616 cp_lexer_consume_token (parser
->lexer
);
21617 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
21618 /* typename is not allowed in a template template parameter
21619 by the standard until C++1Z. */
21620 pedwarn (token
->location
, OPT_Wpedantic
,
21621 "ISO C++ forbids typename key in template template parameter;"
21622 " use -std=c++1z or -std=gnu++1z");
21625 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
21630 /* Parse an (optional) member-specification.
21632 member-specification:
21633 member-declaration member-specification [opt]
21634 access-specifier : member-specification [opt] */
21637 cp_parser_member_specification_opt (cp_parser
* parser
)
21644 /* Peek at the next token. */
21645 token
= cp_lexer_peek_token (parser
->lexer
);
21646 /* If it's a `}', or EOF then we've seen all the members. */
21647 if (token
->type
== CPP_CLOSE_BRACE
21648 || token
->type
== CPP_EOF
21649 || token
->type
== CPP_PRAGMA_EOL
)
21652 /* See if this token is a keyword. */
21653 keyword
= token
->keyword
;
21657 case RID_PROTECTED
:
21659 /* Consume the access-specifier. */
21660 cp_lexer_consume_token (parser
->lexer
);
21661 /* Remember which access-specifier is active. */
21662 current_access_specifier
= token
->u
.value
;
21663 /* Look for the `:'. */
21664 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21668 /* Accept #pragmas at class scope. */
21669 if (token
->type
== CPP_PRAGMA
)
21671 cp_parser_pragma (parser
, pragma_member
);
21675 /* Otherwise, the next construction must be a
21676 member-declaration. */
21677 cp_parser_member_declaration (parser
);
21682 /* Parse a member-declaration.
21684 member-declaration:
21685 decl-specifier-seq [opt] member-declarator-list [opt] ;
21686 function-definition ; [opt]
21687 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21689 template-declaration
21692 member-declarator-list:
21694 member-declarator-list , member-declarator
21697 declarator pure-specifier [opt]
21698 declarator constant-initializer [opt]
21699 identifier [opt] : constant-expression
21703 member-declaration:
21704 __extension__ member-declaration
21707 declarator attributes [opt] pure-specifier [opt]
21708 declarator attributes [opt] constant-initializer [opt]
21709 identifier [opt] attributes [opt] : constant-expression
21713 member-declaration:
21714 static_assert-declaration */
21717 cp_parser_member_declaration (cp_parser
* parser
)
21719 cp_decl_specifier_seq decl_specifiers
;
21720 tree prefix_attributes
;
21722 int declares_class_or_enum
;
21724 cp_token
*token
= NULL
;
21725 cp_token
*decl_spec_token_start
= NULL
;
21726 cp_token
*initializer_token_start
= NULL
;
21727 int saved_pedantic
;
21728 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
21730 /* Check for the `__extension__' keyword. */
21731 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
21734 cp_parser_member_declaration (parser
);
21735 /* Restore the old value of the PEDANTIC flag. */
21736 pedantic
= saved_pedantic
;
21741 /* Check for a template-declaration. */
21742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
21744 /* An explicit specialization here is an error condition, and we
21745 expect the specialization handler to detect and report this. */
21746 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
21747 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
21748 cp_parser_explicit_specialization (parser
);
21750 cp_parser_template_declaration (parser
, /*member_p=*/true);
21754 /* Check for a template introduction. */
21755 else if (cp_parser_template_declaration_after_export (parser
, true))
21758 /* Check for a using-declaration. */
21759 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
21761 if (cxx_dialect
< cxx11
)
21763 /* Parse the using-declaration. */
21764 cp_parser_using_declaration (parser
,
21765 /*access_declaration_p=*/false);
21771 bool alias_decl_expected
;
21772 cp_parser_parse_tentatively (parser
);
21773 decl
= cp_parser_alias_declaration (parser
);
21774 /* Note that if we actually see the '=' token after the
21775 identifier, cp_parser_alias_declaration commits the
21776 tentative parse. In that case, we really expect an
21777 alias-declaration. Otherwise, we expect a using
21779 alias_decl_expected
=
21780 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
21781 cp_parser_parse_definitely (parser
);
21783 if (alias_decl_expected
)
21784 finish_member_declaration (decl
);
21786 cp_parser_using_declaration (parser
,
21787 /*access_declaration_p=*/false);
21792 /* Check for @defs. */
21793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
21796 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
21797 ivar
= ivar_chains
;
21801 ivar
= TREE_CHAIN (member
);
21802 TREE_CHAIN (member
) = NULL_TREE
;
21803 finish_member_declaration (member
);
21808 /* If the next token is `static_assert' we have a static assertion. */
21809 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
21811 cp_parser_static_assert (parser
, /*member_p=*/true);
21815 parser
->colon_corrects_to_scope_p
= false;
21817 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
21820 /* Parse the decl-specifier-seq. */
21821 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21822 cp_parser_decl_specifier_seq (parser
,
21823 CP_PARSER_FLAGS_OPTIONAL
,
21825 &declares_class_or_enum
);
21826 /* Check for an invalid type-name. */
21827 if (!decl_specifiers
.any_type_specifiers_p
21828 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21830 /* If there is no declarator, then the decl-specifier-seq should
21832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21834 /* If there was no decl-specifier-seq, and the next token is a
21835 `;', then we have something like:
21841 Each member-declaration shall declare at least one member
21842 name of the class. */
21843 if (!decl_specifiers
.any_specifiers_p
)
21845 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21846 if (!in_system_header_at (token
->location
))
21847 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
21853 /* See if this declaration is a friend. */
21854 friend_p
= cp_parser_friend_p (&decl_specifiers
);
21855 /* If there were decl-specifiers, check to see if there was
21856 a class-declaration. */
21857 type
= check_tag_decl (&decl_specifiers
,
21858 /*explicit_type_instantiation_p=*/false);
21859 /* Nested classes have already been added to the class, but
21860 a `friend' needs to be explicitly registered. */
21863 /* If the `friend' keyword was present, the friend must
21864 be introduced with a class-key. */
21865 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
21866 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
21867 "in C++03 a class-key must be used "
21868 "when declaring a friend");
21871 template <typename T> struct A {
21872 friend struct A<T>::B;
21875 A<T>::B will be represented by a TYPENAME_TYPE, and
21876 therefore not recognized by check_tag_decl. */
21879 type
= decl_specifiers
.type
;
21880 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
21881 type
= TREE_TYPE (type
);
21883 if (!type
|| !TYPE_P (type
))
21884 error_at (decl_spec_token_start
->location
,
21885 "friend declaration does not name a class or "
21888 make_friend_class (current_class_type
, type
,
21889 /*complain=*/true);
21891 /* If there is no TYPE, an error message will already have
21893 else if (!type
|| type
== error_mark_node
)
21895 /* An anonymous aggregate has to be handled specially; such
21896 a declaration really declares a data member (with a
21897 particular type), as opposed to a nested class. */
21898 else if (ANON_AGGR_TYPE_P (type
))
21901 if (decl_specifiers
.storage_class
!= sc_none
)
21902 error_at (decl_spec_token_start
->location
,
21903 "a storage class on an anonymous aggregate "
21904 "in class scope is not allowed");
21906 /* Remove constructors and such from TYPE, now that we
21907 know it is an anonymous aggregate. */
21908 fixup_anonymous_aggr (type
);
21909 /* And make the corresponding data member. */
21910 decl
= build_decl (decl_spec_token_start
->location
,
21911 FIELD_DECL
, NULL_TREE
, type
);
21912 /* Add it to the class. */
21913 finish_member_declaration (decl
);
21916 cp_parser_check_access_in_redeclaration
21918 decl_spec_token_start
->location
);
21923 bool assume_semicolon
= false;
21926 /* Clear attributes from the decl_specifiers but keep them
21927 around as prefix attributes that apply them to the entity
21929 prefix_attributes
= decl_specifiers
.attributes
;
21930 decl_specifiers
.attributes
= NULL_TREE
;
21932 /* See if these declarations will be friends. */
21933 friend_p
= cp_parser_friend_p (&decl_specifiers
);
21935 /* Keep going until we hit the `;' at the end of the
21937 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21939 tree attributes
= NULL_TREE
;
21940 tree first_attribute
;
21942 /* Peek at the next token. */
21943 token
= cp_lexer_peek_token (parser
->lexer
);
21945 /* Check for a bitfield declaration. */
21946 if (token
->type
== CPP_COLON
21947 || (token
->type
== CPP_NAME
21948 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21954 /* Get the name of the bitfield. Note that we cannot just
21955 check TOKEN here because it may have been invalidated by
21956 the call to cp_lexer_peek_nth_token above. */
21957 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
21958 identifier
= cp_parser_identifier (parser
);
21960 identifier
= NULL_TREE
;
21962 /* Consume the `:' token. */
21963 cp_lexer_consume_token (parser
->lexer
);
21964 /* Get the width of the bitfield. */
21966 = cp_parser_constant_expression (parser
);
21968 /* Look for attributes that apply to the bitfield. */
21969 attributes
= cp_parser_attributes_opt (parser
);
21970 /* Remember which attributes are prefix attributes and
21972 first_attribute
= attributes
;
21973 /* Combine the attributes. */
21974 attributes
= chainon (prefix_attributes
, attributes
);
21976 /* Create the bitfield declaration. */
21977 decl
= grokbitfield (identifier
21978 ? make_id_declarator (NULL_TREE
,
21988 cp_declarator
*declarator
;
21990 tree asm_specification
;
21991 int ctor_dtor_or_conv_p
;
21993 /* Parse the declarator. */
21995 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21996 &ctor_dtor_or_conv_p
,
21997 /*parenthesized_p=*/NULL
,
22001 /* If something went wrong parsing the declarator, make sure
22002 that we at least consume some tokens. */
22003 if (declarator
== cp_error_declarator
)
22005 /* Skip to the end of the statement. */
22006 cp_parser_skip_to_end_of_statement (parser
);
22007 /* If the next token is not a semicolon, that is
22008 probably because we just skipped over the body of
22009 a function. So, we consume a semicolon if
22010 present, but do not issue an error message if it
22012 if (cp_lexer_next_token_is (parser
->lexer
,
22014 cp_lexer_consume_token (parser
->lexer
);
22018 if (declares_class_or_enum
& 2)
22019 cp_parser_check_for_definition_in_return_type
22020 (declarator
, decl_specifiers
.type
,
22021 decl_specifiers
.locations
[ds_type_spec
]);
22023 /* Look for an asm-specification. */
22024 asm_specification
= cp_parser_asm_specification_opt (parser
);
22025 /* Look for attributes that apply to the declaration. */
22026 attributes
= cp_parser_attributes_opt (parser
);
22027 /* Remember which attributes are prefix attributes and
22029 first_attribute
= attributes
;
22030 /* Combine the attributes. */
22031 attributes
= chainon (prefix_attributes
, attributes
);
22033 /* If it's an `=', then we have a constant-initializer or a
22034 pure-specifier. It is not correct to parse the
22035 initializer before registering the member declaration
22036 since the member declaration should be in scope while
22037 its initializer is processed. However, the rest of the
22038 front end does not yet provide an interface that allows
22039 us to handle this correctly. */
22040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22044 A pure-specifier shall be used only in the declaration of
22045 a virtual function.
22047 A member-declarator can contain a constant-initializer
22048 only if it declares a static member of integral or
22051 Therefore, if the DECLARATOR is for a function, we look
22052 for a pure-specifier; otherwise, we look for a
22053 constant-initializer. When we call `grokfield', it will
22054 perform more stringent semantics checks. */
22055 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
22056 if (function_declarator_p (declarator
)
22057 || (decl_specifiers
.type
22058 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
22059 && declarator
->kind
== cdk_id
22060 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
22061 == FUNCTION_TYPE
)))
22062 initializer
= cp_parser_pure_specifier (parser
);
22063 else if (decl_specifiers
.storage_class
!= sc_static
)
22064 initializer
= cp_parser_save_nsdmi (parser
);
22065 else if (cxx_dialect
>= cxx11
)
22068 /* Don't require a constant rvalue in C++11, since we
22069 might want a reference constant. We'll enforce
22070 constancy later. */
22071 cp_lexer_consume_token (parser
->lexer
);
22072 /* Parse the initializer. */
22073 initializer
= cp_parser_initializer_clause (parser
,
22077 /* Parse the initializer. */
22078 initializer
= cp_parser_constant_initializer (parser
);
22080 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
22081 && !function_declarator_p (declarator
))
22084 if (decl_specifiers
.storage_class
!= sc_static
)
22085 initializer
= cp_parser_save_nsdmi (parser
);
22087 initializer
= cp_parser_initializer (parser
, &x
, &x
);
22089 /* Otherwise, there is no initializer. */
22091 initializer
= NULL_TREE
;
22093 /* See if we are probably looking at a function
22094 definition. We are certainly not looking at a
22095 member-declarator. Calling `grokfield' has
22096 side-effects, so we must not do it unless we are sure
22097 that we are looking at a member-declarator. */
22098 if (cp_parser_token_starts_function_definition_p
22099 (cp_lexer_peek_token (parser
->lexer
)))
22101 /* The grammar does not allow a pure-specifier to be
22102 used when a member function is defined. (It is
22103 possible that this fact is an oversight in the
22104 standard, since a pure function may be defined
22105 outside of the class-specifier. */
22106 if (initializer
&& initializer_token_start
)
22107 error_at (initializer_token_start
->location
,
22108 "pure-specifier on function-definition");
22109 decl
= cp_parser_save_member_function_body (parser
,
22116 if (parser
->fully_implicit_function_template_p
)
22117 decl
= finish_fully_implicit_template (parser
, decl
);
22118 /* If the member was not a friend, declare it here. */
22120 finish_member_declaration (decl
);
22121 /* Peek at the next token. */
22122 token
= cp_lexer_peek_token (parser
->lexer
);
22123 /* If the next token is a semicolon, consume it. */
22124 if (token
->type
== CPP_SEMICOLON
)
22125 cp_lexer_consume_token (parser
->lexer
);
22129 if (declarator
->kind
== cdk_function
)
22130 declarator
->id_loc
= token
->location
;
22131 /* Create the declaration. */
22132 decl
= grokfield (declarator
, &decl_specifiers
,
22133 initializer
, /*init_const_expr_p=*/true,
22134 asm_specification
, attributes
);
22135 if (parser
->fully_implicit_function_template_p
)
22138 finish_fully_implicit_template (parser
, 0);
22140 decl
= finish_fully_implicit_template (parser
, decl
);
22144 cp_finalize_omp_declare_simd (parser
, decl
);
22145 cp_finalize_oacc_routine (parser
, decl
, false, first
);
22148 /* Reset PREFIX_ATTRIBUTES. */
22149 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22150 attributes
= TREE_CHAIN (attributes
);
22152 TREE_CHAIN (attributes
) = NULL_TREE
;
22154 /* If there is any qualification still in effect, clear it
22155 now; we will be starting fresh with the next declarator. */
22156 parser
->scope
= NULL_TREE
;
22157 parser
->qualifying_scope
= NULL_TREE
;
22158 parser
->object_scope
= NULL_TREE
;
22159 /* If it's a `,', then there are more declarators. */
22160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22162 cp_lexer_consume_token (parser
->lexer
);
22163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22165 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22166 error_at (token
->location
,
22167 "stray %<,%> at end of member declaration");
22170 /* If the next token isn't a `;', then we have a parse error. */
22171 else if (cp_lexer_next_token_is_not (parser
->lexer
,
22174 /* The next token might be a ways away from where the
22175 actual semicolon is missing. Find the previous token
22176 and use that for our error position. */
22177 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22178 error_at (token
->location
,
22179 "expected %<;%> at end of member declaration");
22181 /* Assume that the user meant to provide a semicolon. If
22182 we were to cp_parser_skip_to_end_of_statement, we might
22183 skip to a semicolon inside a member function definition
22184 and issue nonsensical error messages. */
22185 assume_semicolon
= true;
22190 /* Add DECL to the list of members. */
22192 /* Explicitly include, eg, NSDMIs, for better error
22193 recovery (c++/58650). */
22194 || !DECL_DECLARES_FUNCTION_P (decl
))
22195 finish_member_declaration (decl
);
22197 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22198 cp_parser_save_default_args (parser
, decl
);
22199 else if (TREE_CODE (decl
) == FIELD_DECL
22200 && !DECL_C_BIT_FIELD (decl
)
22201 && DECL_INITIAL (decl
))
22202 /* Add DECL to the queue of NSDMI to be parsed later. */
22203 vec_safe_push (unparsed_nsdmis
, decl
);
22206 if (assume_semicolon
)
22210 /* Reset any OpenACC routine clauses. */
22211 parser
->oacc_routine
= NULL
;
22214 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22216 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22219 /* Parse a pure-specifier.
22224 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22225 Otherwise, ERROR_MARK_NODE is returned. */
22228 cp_parser_pure_specifier (cp_parser
* parser
)
22232 /* Look for the `=' token. */
22233 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22234 return error_mark_node
;
22235 /* Look for the `0' token. */
22236 token
= cp_lexer_peek_token (parser
->lexer
);
22238 if (token
->type
== CPP_EOF
22239 || token
->type
== CPP_PRAGMA_EOL
)
22240 return error_mark_node
;
22242 cp_lexer_consume_token (parser
->lexer
);
22244 /* Accept = default or = delete in c++0x mode. */
22245 if (token
->keyword
== RID_DEFAULT
22246 || token
->keyword
== RID_DELETE
)
22248 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
22249 return token
->u
.value
;
22252 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22253 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
22255 cp_parser_error (parser
,
22256 "invalid pure specifier (only %<= 0%> is allowed)");
22257 cp_parser_skip_to_end_of_statement (parser
);
22258 return error_mark_node
;
22260 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22262 error_at (token
->location
, "templates may not be %<virtual%>");
22263 return error_mark_node
;
22266 return integer_zero_node
;
22269 /* Parse a constant-initializer.
22271 constant-initializer:
22272 = constant-expression
22274 Returns a representation of the constant-expression. */
22277 cp_parser_constant_initializer (cp_parser
* parser
)
22279 /* Look for the `=' token. */
22280 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22281 return error_mark_node
;
22283 /* It is invalid to write:
22285 struct S { static const int i = { 7 }; };
22288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22290 cp_parser_error (parser
,
22291 "a brace-enclosed initializer is not allowed here");
22292 /* Consume the opening brace. */
22293 cp_lexer_consume_token (parser
->lexer
);
22294 /* Skip the initializer. */
22295 cp_parser_skip_to_closing_brace (parser
);
22296 /* Look for the trailing `}'. */
22297 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22299 return error_mark_node
;
22302 return cp_parser_constant_expression (parser
);
22305 /* Derived classes [gram.class.derived] */
22307 /* Parse a base-clause.
22310 : base-specifier-list
22312 base-specifier-list:
22313 base-specifier ... [opt]
22314 base-specifier-list , base-specifier ... [opt]
22316 Returns a TREE_LIST representing the base-classes, in the order in
22317 which they were declared. The representation of each node is as
22318 described by cp_parser_base_specifier.
22320 In the case that no bases are specified, this function will return
22321 NULL_TREE, not ERROR_MARK_NODE. */
22324 cp_parser_base_clause (cp_parser
* parser
)
22326 tree bases
= NULL_TREE
;
22328 /* Look for the `:' that begins the list. */
22329 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22331 /* Scan the base-specifier-list. */
22336 bool pack_expansion_p
= false;
22338 /* Look for the base-specifier. */
22339 base
= cp_parser_base_specifier (parser
);
22340 /* Look for the (optional) ellipsis. */
22341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22343 /* Consume the `...'. */
22344 cp_lexer_consume_token (parser
->lexer
);
22346 pack_expansion_p
= true;
22349 /* Add BASE to the front of the list. */
22350 if (base
&& base
!= error_mark_node
)
22352 if (pack_expansion_p
)
22353 /* Make this a pack expansion type. */
22354 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
22356 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
22358 TREE_CHAIN (base
) = bases
;
22362 /* Peek at the next token. */
22363 token
= cp_lexer_peek_token (parser
->lexer
);
22364 /* If it's not a comma, then the list is complete. */
22365 if (token
->type
!= CPP_COMMA
)
22367 /* Consume the `,'. */
22368 cp_lexer_consume_token (parser
->lexer
);
22371 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22372 base class had a qualified name. However, the next name that
22373 appears is certainly not qualified. */
22374 parser
->scope
= NULL_TREE
;
22375 parser
->qualifying_scope
= NULL_TREE
;
22376 parser
->object_scope
= NULL_TREE
;
22378 return nreverse (bases
);
22381 /* Parse a base-specifier.
22384 :: [opt] nested-name-specifier [opt] class-name
22385 virtual access-specifier [opt] :: [opt] nested-name-specifier
22387 access-specifier virtual [opt] :: [opt] nested-name-specifier
22390 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22391 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22392 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22393 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22396 cp_parser_base_specifier (cp_parser
* parser
)
22400 bool virtual_p
= false;
22401 bool duplicate_virtual_error_issued_p
= false;
22402 bool duplicate_access_error_issued_p
= false;
22403 bool class_scope_p
, template_p
;
22404 tree access
= access_default_node
;
22407 /* Process the optional `virtual' and `access-specifier'. */
22410 /* Peek at the next token. */
22411 token
= cp_lexer_peek_token (parser
->lexer
);
22412 /* Process `virtual'. */
22413 switch (token
->keyword
)
22416 /* If `virtual' appears more than once, issue an error. */
22417 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
22419 cp_parser_error (parser
,
22420 "%<virtual%> specified more than once in base-specified");
22421 duplicate_virtual_error_issued_p
= true;
22426 /* Consume the `virtual' token. */
22427 cp_lexer_consume_token (parser
->lexer
);
22432 case RID_PROTECTED
:
22434 /* If more than one access specifier appears, issue an
22436 if (access
!= access_default_node
22437 && !duplicate_access_error_issued_p
)
22439 cp_parser_error (parser
,
22440 "more than one access specifier in base-specified");
22441 duplicate_access_error_issued_p
= true;
22444 access
= ridpointers
[(int) token
->keyword
];
22446 /* Consume the access-specifier. */
22447 cp_lexer_consume_token (parser
->lexer
);
22456 /* It is not uncommon to see programs mechanically, erroneously, use
22457 the 'typename' keyword to denote (dependent) qualified types
22458 as base classes. */
22459 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
22461 token
= cp_lexer_peek_token (parser
->lexer
);
22462 if (!processing_template_decl
)
22463 error_at (token
->location
,
22464 "keyword %<typename%> not allowed outside of templates");
22466 error_at (token
->location
,
22467 "keyword %<typename%> not allowed in this context "
22468 "(the base class is implicitly a type)");
22469 cp_lexer_consume_token (parser
->lexer
);
22472 /* Look for the optional `::' operator. */
22473 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
22474 /* Look for the nested-name-specifier. The simplest way to
22479 The keyword `typename' is not permitted in a base-specifier or
22480 mem-initializer; in these contexts a qualified name that
22481 depends on a template-parameter is implicitly assumed to be a
22484 is to pretend that we have seen the `typename' keyword at this
22486 cp_parser_nested_name_specifier_opt (parser
,
22487 /*typename_keyword_p=*/true,
22488 /*check_dependency_p=*/true,
22490 /*is_declaration=*/true);
22491 /* If the base class is given by a qualified name, assume that names
22492 we see are type names or templates, as appropriate. */
22493 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
22494 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
22497 && cp_lexer_next_token_is_decltype (parser
->lexer
))
22498 /* DR 950 allows decltype as a base-specifier. */
22499 type
= cp_parser_decltype (parser
);
22502 /* Otherwise, look for the class-name. */
22503 type
= cp_parser_class_name (parser
,
22507 /*check_dependency_p=*/true,
22508 /*class_head_p=*/false,
22509 /*is_declaration=*/true);
22510 type
= TREE_TYPE (type
);
22513 if (type
== error_mark_node
)
22514 return error_mark_node
;
22516 return finish_base_specifier (type
, access
, virtual_p
);
22519 /* Exception handling [gram.exception] */
22521 /* Parse an (optional) noexcept-specification.
22523 noexcept-specification:
22524 noexcept ( constant-expression ) [opt]
22526 If no noexcept-specification is present, returns NULL_TREE.
22527 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22528 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22529 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22530 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22531 in which case a boolean condition is returned instead. */
22534 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
22535 bool require_constexpr
,
22536 bool* consumed_expr
,
22540 const char *saved_message
;
22542 /* Peek at the next token. */
22543 token
= cp_lexer_peek_token (parser
->lexer
);
22545 /* Is it a noexcept-specification? */
22546 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
22549 cp_lexer_consume_token (parser
->lexer
);
22551 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
22553 cp_lexer_consume_token (parser
->lexer
);
22555 if (require_constexpr
)
22557 /* Types may not be defined in an exception-specification. */
22558 saved_message
= parser
->type_definition_forbidden_message
;
22559 parser
->type_definition_forbidden_message
22560 = G_("types may not be defined in an exception-specification");
22562 expr
= cp_parser_constant_expression (parser
);
22564 /* Restore the saved message. */
22565 parser
->type_definition_forbidden_message
= saved_message
;
22569 expr
= cp_parser_expression (parser
);
22570 *consumed_expr
= true;
22573 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22577 expr
= boolean_true_node
;
22578 if (!require_constexpr
)
22579 *consumed_expr
= false;
22582 /* We cannot build a noexcept-spec right away because this will check
22583 that expr is a constexpr. */
22585 return build_noexcept_spec (expr
, tf_warning_or_error
);
22593 /* Parse an (optional) exception-specification.
22595 exception-specification:
22596 throw ( type-id-list [opt] )
22598 Returns a TREE_LIST representing the exception-specification. The
22599 TREE_VALUE of each node is a type. */
22602 cp_parser_exception_specification_opt (cp_parser
* parser
)
22606 const char *saved_message
;
22608 /* Peek at the next token. */
22609 token
= cp_lexer_peek_token (parser
->lexer
);
22611 /* Is it a noexcept-specification? */
22612 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
22614 if (type_id_list
!= NULL_TREE
)
22615 return type_id_list
;
22617 /* If it's not `throw', then there's no exception-specification. */
22618 if (!cp_parser_is_keyword (token
, RID_THROW
))
22622 /* Enable this once a lot of code has transitioned to noexcept? */
22623 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
22624 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
22625 "deprecated in C++0x; use %<noexcept%> instead");
22628 /* Consume the `throw'. */
22629 cp_lexer_consume_token (parser
->lexer
);
22631 /* Look for the `('. */
22632 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22634 /* Peek at the next token. */
22635 token
= cp_lexer_peek_token (parser
->lexer
);
22636 /* If it's not a `)', then there is a type-id-list. */
22637 if (token
->type
!= CPP_CLOSE_PAREN
)
22639 /* Types may not be defined in an exception-specification. */
22640 saved_message
= parser
->type_definition_forbidden_message
;
22641 parser
->type_definition_forbidden_message
22642 = G_("types may not be defined in an exception-specification");
22643 /* Parse the type-id-list. */
22644 type_id_list
= cp_parser_type_id_list (parser
);
22645 /* Restore the saved message. */
22646 parser
->type_definition_forbidden_message
= saved_message
;
22649 type_id_list
= empty_except_spec
;
22651 /* Look for the `)'. */
22652 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22654 return type_id_list
;
22657 /* Parse an (optional) type-id-list.
22661 type-id-list , type-id ... [opt]
22663 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22664 in the order that the types were presented. */
22667 cp_parser_type_id_list (cp_parser
* parser
)
22669 tree types
= NULL_TREE
;
22676 /* Get the next type-id. */
22677 type
= cp_parser_type_id (parser
);
22678 /* Parse the optional ellipsis. */
22679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22681 /* Consume the `...'. */
22682 cp_lexer_consume_token (parser
->lexer
);
22684 /* Turn the type into a pack expansion expression. */
22685 type
= make_pack_expansion (type
);
22687 /* Add it to the list. */
22688 types
= add_exception_specifier (types
, type
, /*complain=*/1);
22689 /* Peek at the next token. */
22690 token
= cp_lexer_peek_token (parser
->lexer
);
22691 /* If it is not a `,', we are done. */
22692 if (token
->type
!= CPP_COMMA
)
22694 /* Consume the `,'. */
22695 cp_lexer_consume_token (parser
->lexer
);
22698 return nreverse (types
);
22701 /* Parse a try-block.
22704 try compound-statement handler-seq */
22707 cp_parser_try_block (cp_parser
* parser
)
22711 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
22712 if (parser
->in_function_body
22713 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
22714 error ("%<try%> in %<constexpr%> function");
22716 try_block
= begin_try_block ();
22717 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
22718 finish_try_block (try_block
);
22719 cp_parser_handler_seq (parser
);
22720 finish_handler_sequence (try_block
);
22725 /* Parse a function-try-block.
22727 function-try-block:
22728 try ctor-initializer [opt] function-body handler-seq */
22731 cp_parser_function_try_block (cp_parser
* parser
)
22733 tree compound_stmt
;
22735 bool ctor_initializer_p
;
22737 /* Look for the `try' keyword. */
22738 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
22740 /* Let the rest of the front end know where we are. */
22741 try_block
= begin_function_try_block (&compound_stmt
);
22742 /* Parse the function-body. */
22743 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22744 (parser
, /*in_function_try_block=*/true);
22745 /* We're done with the `try' part. */
22746 finish_function_try_block (try_block
);
22747 /* Parse the handlers. */
22748 cp_parser_handler_seq (parser
);
22749 /* We're done with the handlers. */
22750 finish_function_handler_sequence (try_block
, compound_stmt
);
22752 return ctor_initializer_p
;
22755 /* Parse a handler-seq.
22758 handler handler-seq [opt] */
22761 cp_parser_handler_seq (cp_parser
* parser
)
22767 /* Parse the handler. */
22768 cp_parser_handler (parser
);
22769 /* Peek at the next token. */
22770 token
= cp_lexer_peek_token (parser
->lexer
);
22771 /* If it's not `catch' then there are no more handlers. */
22772 if (!cp_parser_is_keyword (token
, RID_CATCH
))
22777 /* Parse a handler.
22780 catch ( exception-declaration ) compound-statement */
22783 cp_parser_handler (cp_parser
* parser
)
22788 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
22789 handler
= begin_handler ();
22790 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22791 declaration
= cp_parser_exception_declaration (parser
);
22792 finish_handler_parms (declaration
, handler
);
22793 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22794 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
22795 finish_handler (handler
);
22798 /* Parse an exception-declaration.
22800 exception-declaration:
22801 type-specifier-seq declarator
22802 type-specifier-seq abstract-declarator
22806 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22807 ellipsis variant is used. */
22810 cp_parser_exception_declaration (cp_parser
* parser
)
22812 cp_decl_specifier_seq type_specifiers
;
22813 cp_declarator
*declarator
;
22814 const char *saved_message
;
22816 /* If it's an ellipsis, it's easy to handle. */
22817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22819 /* Consume the `...' token. */
22820 cp_lexer_consume_token (parser
->lexer
);
22824 /* Types may not be defined in exception-declarations. */
22825 saved_message
= parser
->type_definition_forbidden_message
;
22826 parser
->type_definition_forbidden_message
22827 = G_("types may not be defined in exception-declarations");
22829 /* Parse the type-specifier-seq. */
22830 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
22831 /*is_trailing_return=*/false,
22833 /* If it's a `)', then there is no declarator. */
22834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
22837 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
22838 /*ctor_dtor_or_conv_p=*/NULL
,
22839 /*parenthesized_p=*/NULL
,
22840 /*member_p=*/false,
22841 /*friend_p=*/false);
22843 /* Restore the saved message. */
22844 parser
->type_definition_forbidden_message
= saved_message
;
22846 if (!type_specifiers
.any_specifiers_p
)
22847 return error_mark_node
;
22849 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
22852 /* Parse a throw-expression.
22855 throw assignment-expression [opt]
22857 Returns a THROW_EXPR representing the throw-expression. */
22860 cp_parser_throw_expression (cp_parser
* parser
)
22865 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
22866 token
= cp_lexer_peek_token (parser
->lexer
);
22867 /* Figure out whether or not there is an assignment-expression
22868 following the "throw" keyword. */
22869 if (token
->type
== CPP_COMMA
22870 || token
->type
== CPP_SEMICOLON
22871 || token
->type
== CPP_CLOSE_PAREN
22872 || token
->type
== CPP_CLOSE_SQUARE
22873 || token
->type
== CPP_CLOSE_BRACE
22874 || token
->type
== CPP_COLON
)
22875 expression
= NULL_TREE
;
22877 expression
= cp_parser_assignment_expression (parser
);
22879 return build_throw (expression
);
22882 /* GNU Extensions */
22884 /* Parse an (optional) asm-specification.
22887 asm ( string-literal )
22889 If the asm-specification is present, returns a STRING_CST
22890 corresponding to the string-literal. Otherwise, returns
22894 cp_parser_asm_specification_opt (cp_parser
* parser
)
22897 tree asm_specification
;
22899 /* Peek at the next token. */
22900 token
= cp_lexer_peek_token (parser
->lexer
);
22901 /* If the next token isn't the `asm' keyword, then there's no
22902 asm-specification. */
22903 if (!cp_parser_is_keyword (token
, RID_ASM
))
22906 /* Consume the `asm' token. */
22907 cp_lexer_consume_token (parser
->lexer
);
22908 /* Look for the `('. */
22909 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22911 /* Look for the string-literal. */
22912 asm_specification
= cp_parser_string_literal (parser
, false, false);
22914 /* Look for the `)'. */
22915 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22917 return asm_specification
;
22920 /* Parse an asm-operand-list.
22924 asm-operand-list , asm-operand
22927 string-literal ( expression )
22928 [ string-literal ] string-literal ( expression )
22930 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22931 each node is the expression. The TREE_PURPOSE is itself a
22932 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22933 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22934 is a STRING_CST for the string literal before the parenthesis. Returns
22935 ERROR_MARK_NODE if any of the operands are invalid. */
22938 cp_parser_asm_operand_list (cp_parser
* parser
)
22940 tree asm_operands
= NULL_TREE
;
22941 bool invalid_operands
= false;
22945 tree string_literal
;
22949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22951 /* Consume the `[' token. */
22952 cp_lexer_consume_token (parser
->lexer
);
22953 /* Read the operand name. */
22954 name
= cp_parser_identifier (parser
);
22955 if (name
!= error_mark_node
)
22956 name
= build_string (IDENTIFIER_LENGTH (name
),
22957 IDENTIFIER_POINTER (name
));
22958 /* Look for the closing `]'. */
22959 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22963 /* Look for the string-literal. */
22964 string_literal
= cp_parser_string_literal (parser
, false, false);
22966 /* Look for the `('. */
22967 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22968 /* Parse the expression. */
22969 expression
= cp_parser_expression (parser
);
22970 /* Look for the `)'. */
22971 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22973 if (name
== error_mark_node
22974 || string_literal
== error_mark_node
22975 || expression
== error_mark_node
)
22976 invalid_operands
= true;
22978 /* Add this operand to the list. */
22979 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
22982 /* If the next token is not a `,', there are no more
22984 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22986 /* Consume the `,'. */
22987 cp_lexer_consume_token (parser
->lexer
);
22990 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
22993 /* Parse an asm-clobber-list.
22997 asm-clobber-list , string-literal
22999 Returns a TREE_LIST, indicating the clobbers in the order that they
23000 appeared. The TREE_VALUE of each node is a STRING_CST. */
23003 cp_parser_asm_clobber_list (cp_parser
* parser
)
23005 tree clobbers
= NULL_TREE
;
23009 tree string_literal
;
23011 /* Look for the string literal. */
23012 string_literal
= cp_parser_string_literal (parser
, false, false);
23013 /* Add it to the list. */
23014 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
23015 /* If the next token is not a `,', then the list is
23017 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23019 /* Consume the `,' token. */
23020 cp_lexer_consume_token (parser
->lexer
);
23026 /* Parse an asm-label-list.
23030 asm-label-list , identifier
23032 Returns a TREE_LIST, indicating the labels in the order that they
23033 appeared. The TREE_VALUE of each node is a label. */
23036 cp_parser_asm_label_list (cp_parser
* parser
)
23038 tree labels
= NULL_TREE
;
23042 tree identifier
, label
, name
;
23044 /* Look for the identifier. */
23045 identifier
= cp_parser_identifier (parser
);
23046 if (!error_operand_p (identifier
))
23048 label
= lookup_label (identifier
);
23049 if (TREE_CODE (label
) == LABEL_DECL
)
23051 TREE_USED (label
) = 1;
23052 check_goto (label
);
23053 name
= build_string (IDENTIFIER_LENGTH (identifier
),
23054 IDENTIFIER_POINTER (identifier
));
23055 labels
= tree_cons (name
, label
, labels
);
23058 /* If the next token is not a `,', then the list is
23060 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23062 /* Consume the `,' token. */
23063 cp_lexer_consume_token (parser
->lexer
);
23066 return nreverse (labels
);
23069 /* Return TRUE iff the next tokens in the stream are possibly the
23070 beginning of a GNU extension attribute. */
23073 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
23075 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
23078 /* Return TRUE iff the next tokens in the stream are possibly the
23079 beginning of a standard C++-11 attribute specifier. */
23082 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
23084 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
23087 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23088 beginning of a standard C++-11 attribute specifier. */
23091 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
23093 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23095 return (cxx_dialect
>= cxx11
23096 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
23097 || (token
->type
== CPP_OPEN_SQUARE
23098 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
23099 && token
->type
== CPP_OPEN_SQUARE
)));
23102 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23103 beginning of a GNU extension attribute. */
23106 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
23108 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23110 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
23113 /* Return true iff the next tokens can be the beginning of either a
23114 GNU attribute list, or a standard C++11 attribute sequence. */
23117 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
23119 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
23120 || cp_next_tokens_can_be_std_attribute_p (parser
));
23123 /* Return true iff the next Nth tokens can be the beginning of either
23124 a GNU attribute list, or a standard C++11 attribute sequence. */
23127 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
23129 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
23130 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
23133 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23134 of GNU attributes, or return NULL. */
23137 cp_parser_attributes_opt (cp_parser
*parser
)
23139 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
23140 return cp_parser_gnu_attributes_opt (parser
);
23141 return cp_parser_std_attribute_spec_seq (parser
);
23144 #define CILK_SIMD_FN_CLAUSE_MASK \
23145 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23146 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23147 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23148 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23149 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23151 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23152 vector [(<clauses>)] */
23155 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
23157 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
23158 cp_token
*token
= v_token
;
23161 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
23162 parser
->cilk_simd_fn_info
->error_seen
= false;
23163 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
23164 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
23166 int paren_scope
= 0;
23167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23169 cp_lexer_consume_token (parser
->lexer
);
23170 v_token
= cp_lexer_peek_token (parser
->lexer
);
23173 while (paren_scope
> 0)
23175 token
= cp_lexer_peek_token (parser
->lexer
);
23176 if (token
->type
== CPP_OPEN_PAREN
)
23178 else if (token
->type
== CPP_CLOSE_PAREN
)
23180 /* Do not push the last ')' */
23181 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
23182 cp_lexer_consume_token (parser
->lexer
);
23185 token
->type
= CPP_PRAGMA_EOL
;
23186 parser
->lexer
->next_token
= token
;
23187 cp_lexer_consume_token (parser
->lexer
);
23189 struct cp_token_cache
*cp
23190 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
23191 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
23194 /* Parse an (optional) series of attributes.
23197 attributes attribute
23200 __attribute__ (( attribute-list [opt] ))
23202 The return value is as for cp_parser_gnu_attribute_list. */
23205 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
23207 tree attributes
= NULL_TREE
;
23212 tree attribute_list
;
23215 /* Peek at the next token. */
23216 token
= cp_lexer_peek_token (parser
->lexer
);
23217 /* If it's not `__attribute__', then we're done. */
23218 if (token
->keyword
!= RID_ATTRIBUTE
)
23221 /* Consume the `__attribute__' keyword. */
23222 cp_lexer_consume_token (parser
->lexer
);
23223 /* Look for the two `(' tokens. */
23224 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23225 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23227 /* Peek at the next token. */
23228 token
= cp_lexer_peek_token (parser
->lexer
);
23229 if (token
->type
!= CPP_CLOSE_PAREN
)
23230 /* Parse the attribute-list. */
23231 attribute_list
= cp_parser_gnu_attribute_list (parser
);
23233 /* If the next token is a `)', then there is no attribute
23235 attribute_list
= NULL
;
23237 /* Look for the two `)' tokens. */
23238 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23240 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23243 cp_parser_skip_to_end_of_statement (parser
);
23245 /* Add these new attributes to the list. */
23246 attributes
= chainon (attributes
, attribute_list
);
23252 /* Parse a GNU attribute-list.
23256 attribute-list , attribute
23260 identifier ( identifier )
23261 identifier ( identifier , expression-list )
23262 identifier ( expression-list )
23264 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23265 to an attribute. The TREE_PURPOSE of each node is the identifier
23266 indicating which attribute is in use. The TREE_VALUE represents
23267 the arguments, if any. */
23270 cp_parser_gnu_attribute_list (cp_parser
* parser
)
23272 tree attribute_list
= NULL_TREE
;
23273 bool save_translate_strings_p
= parser
->translate_strings_p
;
23275 parser
->translate_strings_p
= false;
23282 /* Look for the identifier. We also allow keywords here; for
23283 example `__attribute__ ((const))' is legal. */
23284 token
= cp_lexer_peek_token (parser
->lexer
);
23285 if (token
->type
== CPP_NAME
23286 || token
->type
== CPP_KEYWORD
)
23288 tree arguments
= NULL_TREE
;
23290 /* Consume the token, but save it since we need it for the
23291 SIMD enabled function parsing. */
23292 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
23294 /* Save away the identifier that indicates which attribute
23296 identifier
= (token
->type
== CPP_KEYWORD
)
23297 /* For keywords, use the canonical spelling, not the
23298 parsed identifier. */
23299 ? ridpointers
[(int) token
->keyword
]
23300 : id_token
->u
.value
;
23302 attribute
= build_tree_list (identifier
, NULL_TREE
);
23304 /* Peek at the next token. */
23305 token
= cp_lexer_peek_token (parser
->lexer
);
23306 /* If it's an `(', then parse the attribute arguments. */
23307 if (token
->type
== CPP_OPEN_PAREN
)
23309 vec
<tree
, va_gc
> *vec
;
23310 int attr_flag
= (attribute_takes_identifier_p (identifier
)
23311 ? id_attr
: normal_attr
);
23312 if (is_cilkplus_vector_p (identifier
))
23314 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23318 vec
= cp_parser_parenthesized_expression_list
23319 (parser
, attr_flag
, /*cast_p=*/false,
23320 /*allow_expansion_p=*/false,
23321 /*non_constant_p=*/NULL
);
23323 arguments
= error_mark_node
;
23326 arguments
= build_tree_list_vec (vec
);
23327 release_tree_vector (vec
);
23329 /* Save the arguments away. */
23330 TREE_VALUE (attribute
) = arguments
;
23332 else if (is_cilkplus_vector_p (identifier
))
23334 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23338 if (arguments
!= error_mark_node
)
23340 /* Add this attribute to the list. */
23341 TREE_CHAIN (attribute
) = attribute_list
;
23342 attribute_list
= attribute
;
23345 token
= cp_lexer_peek_token (parser
->lexer
);
23347 /* Now, look for more attributes. If the next token isn't a
23348 `,', we're done. */
23349 if (token
->type
!= CPP_COMMA
)
23352 /* Consume the comma and keep going. */
23353 cp_lexer_consume_token (parser
->lexer
);
23355 parser
->translate_strings_p
= save_translate_strings_p
;
23357 /* We built up the list in reverse order. */
23358 return nreverse (attribute_list
);
23361 /* Parse a standard C++11 attribute.
23363 The returned representation is a TREE_LIST which TREE_PURPOSE is
23364 the scoped name of the attribute, and the TREE_VALUE is its
23367 Note that the scoped name of the attribute is itself a TREE_LIST
23368 which TREE_PURPOSE is the namespace of the attribute, and
23369 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23370 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23371 and which TREE_PURPOSE is directly the attribute name.
23373 Clients of the attribute code should use get_attribute_namespace
23374 and get_attribute_name to get the actual namespace and name of
23375 attributes, regardless of their being GNU or C++11 attributes.
23378 attribute-token attribute-argument-clause [opt]
23382 attribute-scoped-token
23384 attribute-scoped-token:
23385 attribute-namespace :: identifier
23387 attribute-namespace:
23390 attribute-argument-clause:
23391 ( balanced-token-seq )
23393 balanced-token-seq:
23394 balanced-token [opt]
23395 balanced-token-seq balanced-token
23398 ( balanced-token-seq )
23399 [ balanced-token-seq ]
23400 { balanced-token-seq }. */
23403 cp_parser_std_attribute (cp_parser
*parser
)
23405 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
23408 /* First, parse name of the attribute, a.k.a attribute-token. */
23410 token
= cp_lexer_peek_token (parser
->lexer
);
23411 if (token
->type
== CPP_NAME
)
23412 attr_id
= token
->u
.value
;
23413 else if (token
->type
== CPP_KEYWORD
)
23414 attr_id
= ridpointers
[(int) token
->keyword
];
23415 else if (token
->flags
& NAMED_OP
)
23416 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
23418 if (attr_id
== NULL_TREE
)
23421 cp_lexer_consume_token (parser
->lexer
);
23423 token
= cp_lexer_peek_token (parser
->lexer
);
23424 if (token
->type
== CPP_SCOPE
)
23426 /* We are seeing a scoped attribute token. */
23428 cp_lexer_consume_token (parser
->lexer
);
23431 token
= cp_lexer_consume_token (parser
->lexer
);
23432 if (token
->type
== CPP_NAME
)
23433 attr_id
= token
->u
.value
;
23434 else if (token
->type
== CPP_KEYWORD
)
23435 attr_id
= ridpointers
[(int) token
->keyword
];
23438 error_at (token
->location
,
23439 "expected an identifier for the attribute name");
23440 return error_mark_node
;
23442 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
23444 token
= cp_lexer_peek_token (parser
->lexer
);
23448 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
23450 /* C++11 noreturn attribute is equivalent to GNU's. */
23451 if (is_attribute_p ("noreturn", attr_id
))
23452 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23453 /* C++14 deprecated attribute is equivalent to GNU's. */
23454 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
23456 if (cxx_dialect
== cxx11
)
23457 pedwarn (token
->location
, OPT_Wpedantic
,
23458 "%<deprecated%> is a C++14 feature;"
23459 " use %<gnu::deprecated%>");
23460 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23462 /* Transactional Memory TS optimize_for_synchronized attribute is
23463 equivalent to GNU transaction_callable. */
23464 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
23465 TREE_PURPOSE (attribute
)
23466 = get_identifier ("transaction_callable");
23467 /* Transactional Memory attributes are GNU attributes. */
23468 else if (tm_attr_to_mask (attr_id
))
23469 TREE_PURPOSE (attribute
) = attr_id
;
23472 /* Now parse the optional argument clause of the attribute. */
23474 if (token
->type
!= CPP_OPEN_PAREN
)
23478 vec
<tree
, va_gc
> *vec
;
23479 int attr_flag
= normal_attr
;
23481 if (attr_ns
== get_identifier ("gnu")
23482 && attribute_takes_identifier_p (attr_id
))
23483 /* A GNU attribute that takes an identifier in parameter. */
23484 attr_flag
= id_attr
;
23486 vec
= cp_parser_parenthesized_expression_list
23487 (parser
, attr_flag
, /*cast_p=*/false,
23488 /*allow_expansion_p=*/true,
23489 /*non_constant_p=*/NULL
);
23491 arguments
= error_mark_node
;
23494 arguments
= build_tree_list_vec (vec
);
23495 release_tree_vector (vec
);
23498 if (arguments
== error_mark_node
)
23499 attribute
= error_mark_node
;
23501 TREE_VALUE (attribute
) = arguments
;
23507 /* Check that the attribute ATTRIBUTE appears at most once in the
23508 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23509 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23510 isn't implemented yet in GCC. */
23513 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
23517 tree name
= get_attribute_name (attribute
);
23518 if (is_attribute_p ("noreturn", name
)
23519 && lookup_attribute ("noreturn", attributes
))
23520 error ("attribute noreturn can appear at most once "
23521 "in an attribute-list");
23522 else if (is_attribute_p ("deprecated", name
)
23523 && lookup_attribute ("deprecated", attributes
))
23524 error ("attribute deprecated can appear at most once "
23525 "in an attribute-list");
23529 /* Parse a list of standard C++-11 attributes.
23533 attribute-list , attribute[opt]
23535 attribute-list , attribute ...
23539 cp_parser_std_attribute_list (cp_parser
*parser
)
23541 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
23542 cp_token
*token
= NULL
;
23546 attribute
= cp_parser_std_attribute (parser
);
23547 if (attribute
== error_mark_node
)
23549 if (attribute
!= NULL_TREE
)
23551 cp_parser_check_std_attribute (attributes
, attribute
);
23552 TREE_CHAIN (attribute
) = attributes
;
23553 attributes
= attribute
;
23555 token
= cp_lexer_peek_token (parser
->lexer
);
23556 if (token
->type
== CPP_ELLIPSIS
)
23558 cp_lexer_consume_token (parser
->lexer
);
23559 TREE_VALUE (attribute
)
23560 = make_pack_expansion (TREE_VALUE (attribute
));
23561 token
= cp_lexer_peek_token (parser
->lexer
);
23563 if (token
->type
!= CPP_COMMA
)
23565 cp_lexer_consume_token (parser
->lexer
);
23567 attributes
= nreverse (attributes
);
23571 /* Parse a standard C++-11 attribute specifier.
23573 attribute-specifier:
23574 [ [ attribute-list ] ]
23575 alignment-specifier
23577 alignment-specifier:
23578 alignas ( type-id ... [opt] )
23579 alignas ( alignment-expression ... [opt] ). */
23582 cp_parser_std_attribute_spec (cp_parser
*parser
)
23584 tree attributes
= NULL_TREE
;
23585 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23587 if (token
->type
== CPP_OPEN_SQUARE
23588 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
23590 cp_lexer_consume_token (parser
->lexer
);
23591 cp_lexer_consume_token (parser
->lexer
);
23593 attributes
= cp_parser_std_attribute_list (parser
);
23595 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
23596 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
23597 cp_parser_skip_to_end_of_statement (parser
);
23599 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23600 when we are sure that we have actually parsed them. */
23601 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
23607 /* Look for an alignment-specifier. */
23609 token
= cp_lexer_peek_token (parser
->lexer
);
23611 if (token
->type
!= CPP_KEYWORD
23612 || token
->keyword
!= RID_ALIGNAS
)
23615 cp_lexer_consume_token (parser
->lexer
);
23616 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
23618 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
23620 cp_parser_error (parser
, "expected %<(%>");
23621 return error_mark_node
;
23624 cp_parser_parse_tentatively (parser
);
23625 alignas_expr
= cp_parser_type_id (parser
);
23627 if (!cp_parser_parse_definitely (parser
))
23629 gcc_assert (alignas_expr
== error_mark_node
23630 || alignas_expr
== NULL_TREE
);
23633 cp_parser_assignment_expression (parser
);
23634 if (alignas_expr
== error_mark_node
)
23635 cp_parser_skip_to_end_of_statement (parser
);
23636 if (alignas_expr
== NULL_TREE
23637 || alignas_expr
== error_mark_node
)
23638 return alignas_expr
;
23641 alignas_expr
= cxx_alignas_expr (alignas_expr
);
23642 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
23644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23646 cp_lexer_consume_token (parser
->lexer
);
23647 alignas_expr
= make_pack_expansion (alignas_expr
);
23650 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
23652 cp_parser_error (parser
, "expected %<)%>");
23653 return error_mark_node
;
23656 /* Build the C++-11 representation of an 'aligned'
23659 build_tree_list (build_tree_list (get_identifier ("gnu"),
23660 get_identifier ("aligned")),
23667 /* Parse a standard C++-11 attribute-specifier-seq.
23669 attribute-specifier-seq:
23670 attribute-specifier-seq [opt] attribute-specifier
23674 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
23676 tree attr_specs
= NULL
;
23680 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
23681 if (attr_spec
== NULL_TREE
)
23683 if (attr_spec
== error_mark_node
)
23684 return error_mark_node
;
23686 TREE_CHAIN (attr_spec
) = attr_specs
;
23687 attr_specs
= attr_spec
;
23690 attr_specs
= nreverse (attr_specs
);
23694 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23695 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23696 current value of the PEDANTIC flag, regardless of whether or not
23697 the `__extension__' keyword is present. The caller is responsible
23698 for restoring the value of the PEDANTIC flag. */
23701 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
23703 /* Save the old value of the PEDANTIC flag. */
23704 *saved_pedantic
= pedantic
;
23706 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
23708 /* Consume the `__extension__' token. */
23709 cp_lexer_consume_token (parser
->lexer
);
23710 /* We're not being pedantic while the `__extension__' keyword is
23720 /* Parse a label declaration.
23723 __label__ label-declarator-seq ;
23725 label-declarator-seq:
23726 identifier , label-declarator-seq
23730 cp_parser_label_declaration (cp_parser
* parser
)
23732 /* Look for the `__label__' keyword. */
23733 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
23739 /* Look for an identifier. */
23740 identifier
= cp_parser_identifier (parser
);
23741 /* If we failed, stop. */
23742 if (identifier
== error_mark_node
)
23744 /* Declare it as a label. */
23745 finish_label_decl (identifier
);
23746 /* If the next token is a `;', stop. */
23747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23749 /* Look for the `,' separating the label declarations. */
23750 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
23753 /* Look for the final `;'. */
23754 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23757 // -------------------------------------------------------------------------- //
23760 // Parse a requires clause.
23762 // requires-clause:
23763 // 'requires' logical-or-expression
23765 // The required logical-or-expression must be a constant expression. Note
23766 // that we don't check that the expression is constepxr here. We defer until
23767 // we analyze constraints and then, we only check atomic constraints.
23769 cp_parser_requires_clause (cp_parser
*parser
)
23771 // Parse the requires clause so that it is not automatically folded.
23772 ++processing_template_decl
;
23773 tree expr
= cp_parser_binary_expression (parser
, false, false,
23774 PREC_NOT_OPERATOR
, NULL
);
23775 --processing_template_decl
;
23779 // Optionally parse a requires clause:
23781 cp_parser_requires_clause_opt (cp_parser
*parser
)
23783 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
23785 cp_lexer_consume_token (parser
->lexer
);
23786 return cp_parser_requires_clause (parser
);
23790 /*---------------------------------------------------------------------------
23791 Requires expressions
23792 ---------------------------------------------------------------------------*/
23794 /* Parse a requires expression
23796 requirement-expression:
23797 'requires' requirement-parameter-list [opt] requirement-body */
23799 cp_parser_requires_expression (cp_parser
*parser
)
23801 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
23802 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
23804 /* A requires-expression shall appear only within a concept
23805 definition or a requires-clause.
23807 TODO: Implement this diagnostic correctly. */
23808 if (!processing_template_decl
)
23810 error_at (loc
, "a requires expression cannot appear outside a template");
23811 cp_parser_skip_to_end_of_statement (parser
);
23812 return error_mark_node
;
23817 /* Local parameters are delared as variables within the scope
23818 of the expression. They are not visible past the end of
23819 the expression. Expressions within the requires-expression
23820 are unevaluated. */
23821 struct scope_sentinel
23825 ++cp_unevaluated_operand
;
23826 begin_scope (sk_block
, NULL_TREE
);
23831 pop_bindings_and_leave_scope ();
23832 --cp_unevaluated_operand
;
23836 /* Parse the optional parameter list. */
23837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23839 parms
= cp_parser_requirement_parameter_list (parser
);
23840 if (parms
== error_mark_node
)
23841 return error_mark_node
;
23846 /* Parse the requirement body. */
23847 reqs
= cp_parser_requirement_body (parser
);
23848 if (reqs
== error_mark_node
)
23849 return error_mark_node
;
23852 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23854 grokparms (parms
, &parms
);
23855 return finish_requires_expr (parms
, reqs
);
23858 /* Parse a parameterized requirement.
23860 requirement-parameter-list:
23861 '(' parameter-declaration-clause ')' */
23863 cp_parser_requirement_parameter_list (cp_parser
*parser
)
23865 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23866 return error_mark_node
;
23868 tree parms
= cp_parser_parameter_declaration_clause (parser
);
23870 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23871 return error_mark_node
;
23876 /* Parse the body of a requirement.
23879 '{' requirement-list '}' */
23881 cp_parser_requirement_body (cp_parser
*parser
)
23883 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
23884 return error_mark_node
;
23886 tree reqs
= cp_parser_requirement_list (parser
);
23888 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
23889 return error_mark_node
;
23894 /* Parse a list of requirements.
23898 requirement-list ';' requirement[opt] */
23900 cp_parser_requirement_list (cp_parser
*parser
)
23902 tree result
= NULL_TREE
;
23905 tree req
= cp_parser_requirement (parser
);
23906 if (req
== error_mark_node
)
23907 return error_mark_node
;
23909 result
= tree_cons (NULL_TREE
, req
, result
);
23911 /* If we see a semi-colon, consume it. */
23912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23913 cp_lexer_consume_token (parser
->lexer
);
23915 /* Stop processing at the end of the list. */
23916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
23920 /* Reverse the order of requirements so they are analyzed in
23921 declaration order. */
23922 return nreverse (result
);
23925 /* Parse a syntactic requirement or type requirement.
23929 compound-requirement
23931 nested-requirement */
23933 cp_parser_requirement (cp_parser
*parser
)
23935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23936 return cp_parser_compound_requirement (parser
);
23937 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23938 return cp_parser_type_requirement (parser
);
23939 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
23940 return cp_parser_nested_requirement (parser
);
23942 return cp_parser_simple_requirement (parser
);
23945 /* Parse a simple requirement.
23947 simple-requirement:
23950 cp_parser_simple_requirement (cp_parser
*parser
)
23952 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
23953 if (!expr
|| expr
== error_mark_node
)
23954 return error_mark_node
;
23956 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
23957 return error_mark_node
;
23959 return finish_simple_requirement (expr
);
23962 /* Parse a type requirement
23965 nested-name-specifier [opt] required-type-name ';'
23967 required-type-name:
23969 'template' [opt] simple-template-id */
23971 cp_parser_type_requirement (cp_parser
*parser
)
23973 cp_lexer_consume_token (parser
->lexer
);
23975 // Save the scope before parsing name specifiers.
23976 tree saved_scope
= parser
->scope
;
23977 tree saved_object_scope
= parser
->object_scope
;
23978 tree saved_qualifying_scope
= parser
->qualifying_scope
;
23979 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
23980 cp_parser_nested_name_specifier_opt (parser
,
23981 /*typename_keyword_p=*/true,
23982 /*check_dependency_p=*/false,
23984 /*is_declaration=*/false);
23987 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23989 cp_lexer_consume_token (parser
->lexer
);
23990 type
= cp_parser_template_id (parser
,
23991 /*template_keyword_p=*/true,
23992 /*check_dependency=*/false,
23993 /*tag_type=*/none_type
,
23994 /*is_declaration=*/false);
23995 type
= make_typename_type (parser
->scope
, type
, typename_type
,
23996 /*complain=*/tf_error
);
23999 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
24001 if (TREE_CODE (type
) == TYPE_DECL
)
24002 type
= TREE_TYPE (type
);
24004 parser
->scope
= saved_scope
;
24005 parser
->object_scope
= saved_object_scope
;
24006 parser
->qualifying_scope
= saved_qualifying_scope
;
24008 if (type
== error_mark_node
)
24009 cp_parser_skip_to_end_of_statement (parser
);
24011 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
24012 return error_mark_node
;
24013 if (type
== error_mark_node
)
24014 return error_mark_node
;
24016 return finish_type_requirement (type
);
24019 /* Parse a compound requirement
24021 compound-requirement:
24022 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24024 cp_parser_compound_requirement (cp_parser
*parser
)
24026 /* Parse an expression enclosed in '{ }'s. */
24027 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
24028 return error_mark_node
;
24030 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
24031 if (!expr
|| expr
== error_mark_node
)
24032 return error_mark_node
;
24034 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
24035 return error_mark_node
;
24037 /* Parse the optional noexcept. */
24038 bool noexcept_p
= false;
24039 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
24041 cp_lexer_consume_token (parser
->lexer
);
24045 /* Parse the optional trailing return type. */
24046 tree type
= NULL_TREE
;
24047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
24049 cp_lexer_consume_token (parser
->lexer
);
24050 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
24051 parser
->in_result_type_constraint_p
= true;
24052 type
= cp_parser_trailing_type_id (parser
);
24053 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
24054 if (type
== error_mark_node
)
24055 return error_mark_node
;
24058 return finish_compound_requirement (expr
, type
, noexcept_p
);
24061 /* Parse a nested requirement. This is the same as a requires clause.
24063 nested-requirement:
24066 cp_parser_nested_requirement (cp_parser
*parser
)
24068 cp_lexer_consume_token (parser
->lexer
);
24069 tree req
= cp_parser_requires_clause (parser
);
24070 if (req
== error_mark_node
)
24071 return error_mark_node
;
24072 return finish_nested_requirement (req
);
24075 /* Support Functions */
24077 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24078 NAME should have one of the representations used for an
24079 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24080 is returned. If PARSER->SCOPE is a dependent type, then a
24081 SCOPE_REF is returned.
24083 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24084 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24085 was formed. Abstractly, such entities should not be passed to this
24086 function, because they do not need to be looked up, but it is
24087 simpler to check for this special case here, rather than at the
24090 In cases not explicitly covered above, this function returns a
24091 DECL, OVERLOAD, or baselink representing the result of the lookup.
24092 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24095 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24096 (e.g., "struct") that was used. In that case bindings that do not
24097 refer to types are ignored.
24099 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24102 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24105 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24108 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24109 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24110 NULL_TREE otherwise. */
24113 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
24114 enum tag_types tag_type
,
24117 bool check_dependency
,
24118 tree
*ambiguous_decls
,
24119 location_t name_location
)
24122 tree object_type
= parser
->context
->object_type
;
24124 /* Assume that the lookup will be unambiguous. */
24125 if (ambiguous_decls
)
24126 *ambiguous_decls
= NULL_TREE
;
24128 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24129 no longer valid. Note that if we are parsing tentatively, and
24130 the parse fails, OBJECT_TYPE will be automatically restored. */
24131 parser
->context
->object_type
= NULL_TREE
;
24133 if (name
== error_mark_node
)
24134 return error_mark_node
;
24136 /* A template-id has already been resolved; there is no lookup to
24138 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
24140 if (BASELINK_P (name
))
24142 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
24143 == TEMPLATE_ID_EXPR
);
24147 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24148 it should already have been checked to make sure that the name
24149 used matches the type being destroyed. */
24150 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
24154 /* Figure out to which type this destructor applies. */
24156 type
= parser
->scope
;
24157 else if (object_type
)
24158 type
= object_type
;
24160 type
= current_class_type
;
24161 /* If that's not a class type, there is no destructor. */
24162 if (!type
|| !CLASS_TYPE_P (type
))
24163 return error_mark_node
;
24164 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
24165 lazily_declare_fn (sfk_destructor
, type
);
24166 if (!CLASSTYPE_DESTRUCTORS (type
))
24167 return error_mark_node
;
24168 /* If it was a class type, return the destructor. */
24169 return CLASSTYPE_DESTRUCTORS (type
);
24172 /* By this point, the NAME should be an ordinary identifier. If
24173 the id-expression was a qualified name, the qualifying scope is
24174 stored in PARSER->SCOPE at this point. */
24175 gcc_assert (identifier_p (name
));
24177 /* Perform the lookup. */
24182 if (parser
->scope
== error_mark_node
)
24183 return error_mark_node
;
24185 /* If the SCOPE is dependent, the lookup must be deferred until
24186 the template is instantiated -- unless we are explicitly
24187 looking up names in uninstantiated templates. Even then, we
24188 cannot look up the name if the scope is not a class type; it
24189 might, for example, be a template type parameter. */
24190 dependent_p
= (TYPE_P (parser
->scope
)
24191 && dependent_scope_p (parser
->scope
));
24192 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
24194 /* Defer lookup. */
24195 decl
= error_mark_node
;
24198 tree pushed_scope
= NULL_TREE
;
24200 /* If PARSER->SCOPE is a dependent type, then it must be a
24201 class type, and we must not be checking dependencies;
24202 otherwise, we would have processed this lookup above. So
24203 that PARSER->SCOPE is not considered a dependent base by
24204 lookup_member, we must enter the scope here. */
24206 pushed_scope
= push_scope (parser
->scope
);
24208 /* If the PARSER->SCOPE is a template specialization, it
24209 may be instantiated during name lookup. In that case,
24210 errors may be issued. Even if we rollback the current
24211 tentative parse, those errors are valid. */
24212 decl
= lookup_qualified_name (parser
->scope
, name
,
24213 tag_type
!= none_type
,
24214 /*complain=*/true);
24216 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24217 lookup result and the nested-name-specifier nominates a class C:
24218 * if the name specified after the nested-name-specifier, when
24219 looked up in C, is the injected-class-name of C (Clause 9), or
24220 * if the name specified after the nested-name-specifier is the
24221 same as the identifier or the simple-template-id's template-
24222 name in the last component of the nested-name-specifier,
24223 the name is instead considered to name the constructor of
24224 class C. [ Note: for example, the constructor is not an
24225 acceptable lookup result in an elaborated-type-specifier so
24226 the constructor would not be used in place of the
24227 injected-class-name. --end note ] Such a constructor name
24228 shall be used only in the declarator-id of a declaration that
24229 names a constructor or in a using-declaration. */
24230 if (tag_type
== none_type
24231 && DECL_SELF_REFERENCE_P (decl
)
24232 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
24233 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
24234 tag_type
!= none_type
,
24235 /*complain=*/true);
24237 /* If we have a single function from a using decl, pull it out. */
24238 if (TREE_CODE (decl
) == OVERLOAD
24239 && !really_overloaded_fn (decl
))
24240 decl
= OVL_FUNCTION (decl
);
24243 pop_scope (pushed_scope
);
24246 /* If the scope is a dependent type and either we deferred lookup or
24247 we did lookup but didn't find the name, rememeber the name. */
24248 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
24249 && dependent_type_p (parser
->scope
))
24255 /* The resolution to Core Issue 180 says that `struct
24256 A::B' should be considered a type-name, even if `A'
24258 type
= make_typename_type (parser
->scope
, name
, tag_type
,
24259 /*complain=*/tf_error
);
24260 if (type
!= error_mark_node
)
24261 decl
= TYPE_NAME (type
);
24263 else if (is_template
24264 && (cp_parser_next_token_ends_template_argument_p (parser
)
24265 || cp_lexer_next_token_is (parser
->lexer
,
24267 decl
= make_unbound_class_template (parser
->scope
,
24269 /*complain=*/tf_error
);
24271 decl
= build_qualified_name (/*type=*/NULL_TREE
,
24272 parser
->scope
, name
,
24275 parser
->qualifying_scope
= parser
->scope
;
24276 parser
->object_scope
= NULL_TREE
;
24278 else if (object_type
)
24280 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24281 OBJECT_TYPE is not a class. */
24282 if (CLASS_TYPE_P (object_type
))
24283 /* If the OBJECT_TYPE is a template specialization, it may
24284 be instantiated during name lookup. In that case, errors
24285 may be issued. Even if we rollback the current tentative
24286 parse, those errors are valid. */
24287 decl
= lookup_member (object_type
,
24290 tag_type
!= none_type
,
24291 tf_warning_or_error
);
24296 /* Look it up in the enclosing context. */
24297 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24299 /*block_p=*/true, is_namespace
, 0);
24300 parser
->object_scope
= object_type
;
24301 parser
->qualifying_scope
= NULL_TREE
;
24305 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24307 /*block_p=*/true, is_namespace
, 0);
24308 parser
->qualifying_scope
= NULL_TREE
;
24309 parser
->object_scope
= NULL_TREE
;
24312 /* If the lookup failed, let our caller know. */
24313 if (!decl
|| decl
== error_mark_node
)
24314 return error_mark_node
;
24316 /* Pull out the template from an injected-class-name (or multiple). */
24318 decl
= maybe_get_template_decl_from_type_decl (decl
);
24320 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24321 if (TREE_CODE (decl
) == TREE_LIST
)
24323 if (ambiguous_decls
)
24324 *ambiguous_decls
= decl
;
24325 /* The error message we have to print is too complicated for
24326 cp_parser_error, so we incorporate its actions directly. */
24327 if (!cp_parser_simulate_error (parser
))
24329 error_at (name_location
, "reference to %qD is ambiguous",
24331 print_candidates (decl
);
24333 return error_mark_node
;
24336 gcc_assert (DECL_P (decl
)
24337 || TREE_CODE (decl
) == OVERLOAD
24338 || TREE_CODE (decl
) == SCOPE_REF
24339 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
24340 || BASELINK_P (decl
));
24342 /* If we have resolved the name of a member declaration, check to
24343 see if the declaration is accessible. When the name resolves to
24344 set of overloaded functions, accessibility is checked when
24345 overload resolution is done.
24347 During an explicit instantiation, access is not checked at all,
24348 as per [temp.explicit]. */
24350 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
24352 maybe_record_typedef_use (decl
);
24357 /* Like cp_parser_lookup_name, but for use in the typical case where
24358 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24359 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24362 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
24364 return cp_parser_lookup_name (parser
, name
,
24366 /*is_template=*/false,
24367 /*is_namespace=*/false,
24368 /*check_dependency=*/true,
24369 /*ambiguous_decls=*/NULL
,
24373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24374 the current context, return the TYPE_DECL. If TAG_NAME_P is
24375 true, the DECL indicates the class being defined in a class-head,
24376 or declared in an elaborated-type-specifier.
24378 Otherwise, return DECL. */
24381 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
24383 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24384 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24387 template <typename T> struct B;
24390 template <typename T> struct A::B {};
24392 Similarly, in an elaborated-type-specifier:
24394 namespace N { struct X{}; }
24397 template <typename T> friend struct N::X;
24400 However, if the DECL refers to a class type, and we are in
24401 the scope of the class, then the name lookup automatically
24402 finds the TYPE_DECL created by build_self_reference rather
24403 than a TEMPLATE_DECL. For example, in:
24405 template <class T> struct S {
24409 there is no need to handle such case. */
24411 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
24412 return DECL_TEMPLATE_RESULT (decl
);
24417 /* If too many, or too few, template-parameter lists apply to the
24418 declarator, issue an error message. Returns TRUE if all went well,
24419 and FALSE otherwise. */
24422 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
24423 cp_declarator
*declarator
,
24424 location_t declarator_location
)
24426 switch (declarator
->kind
)
24430 unsigned num_templates
= 0;
24431 tree scope
= declarator
->u
.id
.qualifying_scope
;
24434 num_templates
= num_template_headers_for_class (scope
);
24435 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
24436 == TEMPLATE_ID_EXPR
)
24437 /* If the DECLARATOR has the form `X<y>' then it uses one
24438 additional level of template parameters. */
24441 return cp_parser_check_template_parameters
24442 (parser
, num_templates
, declarator_location
, declarator
);
24448 case cdk_reference
:
24450 return (cp_parser_check_declarator_template_parameters
24451 (parser
, declarator
->declarator
, declarator_location
));
24457 gcc_unreachable ();
24462 /* NUM_TEMPLATES were used in the current declaration. If that is
24463 invalid, return FALSE and issue an error messages. Otherwise,
24464 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24465 declarator and we can print more accurate diagnostics. */
24468 cp_parser_check_template_parameters (cp_parser
* parser
,
24469 unsigned num_templates
,
24470 location_t location
,
24471 cp_declarator
*declarator
)
24473 /* If there are the same number of template classes and parameter
24474 lists, that's OK. */
24475 if (parser
->num_template_parameter_lists
== num_templates
)
24477 /* If there are more, but only one more, then we are referring to a
24478 member template. That's OK too. */
24479 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
24481 /* If there are more template classes than parameter lists, we have
24484 template <class T> void S<T>::R<T>::f (); */
24485 if (parser
->num_template_parameter_lists
< num_templates
)
24487 if (declarator
&& !current_function_decl
)
24488 error_at (location
, "specializing member %<%T::%E%> "
24489 "requires %<template<>%> syntax",
24490 declarator
->u
.id
.qualifying_scope
,
24491 declarator
->u
.id
.unqualified_name
);
24492 else if (declarator
)
24493 error_at (location
, "invalid declaration of %<%T::%E%>",
24494 declarator
->u
.id
.qualifying_scope
,
24495 declarator
->u
.id
.unqualified_name
);
24497 error_at (location
, "too few template-parameter-lists");
24500 /* Otherwise, there are too many template parameter lists. We have
24503 template <class T> template <class U> void S::f(); */
24504 error_at (location
, "too many template-parameter-lists");
24508 /* Parse an optional `::' token indicating that the following name is
24509 from the global namespace. If so, PARSER->SCOPE is set to the
24510 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24511 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24512 Returns the new value of PARSER->SCOPE, if the `::' token is
24513 present, and NULL_TREE otherwise. */
24516 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
24520 /* Peek at the next token. */
24521 token
= cp_lexer_peek_token (parser
->lexer
);
24522 /* If we're looking at a `::' token then we're starting from the
24523 global namespace, not our current location. */
24524 if (token
->type
== CPP_SCOPE
)
24526 /* Consume the `::' token. */
24527 cp_lexer_consume_token (parser
->lexer
);
24528 /* Set the SCOPE so that we know where to start the lookup. */
24529 parser
->scope
= global_namespace
;
24530 parser
->qualifying_scope
= global_namespace
;
24531 parser
->object_scope
= NULL_TREE
;
24533 return parser
->scope
;
24535 else if (!current_scope_valid_p
)
24537 parser
->scope
= NULL_TREE
;
24538 parser
->qualifying_scope
= NULL_TREE
;
24539 parser
->object_scope
= NULL_TREE
;
24545 /* Returns TRUE if the upcoming token sequence is the start of a
24546 constructor declarator. If FRIEND_P is true, the declarator is
24547 preceded by the `friend' specifier. */
24550 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
24552 bool constructor_p
;
24553 bool outside_class_specifier_p
;
24554 tree nested_name_specifier
;
24555 cp_token
*next_token
;
24557 /* The common case is that this is not a constructor declarator, so
24558 try to avoid doing lots of work if at all possible. It's not
24559 valid declare a constructor at function scope. */
24560 if (parser
->in_function_body
)
24562 /* And only certain tokens can begin a constructor declarator. */
24563 next_token
= cp_lexer_peek_token (parser
->lexer
);
24564 if (next_token
->type
!= CPP_NAME
24565 && next_token
->type
!= CPP_SCOPE
24566 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
24567 && next_token
->type
!= CPP_TEMPLATE_ID
)
24570 /* Parse tentatively; we are going to roll back all of the tokens
24572 cp_parser_parse_tentatively (parser
);
24573 /* Assume that we are looking at a constructor declarator. */
24574 constructor_p
= true;
24576 /* Look for the optional `::' operator. */
24577 cp_parser_global_scope_opt (parser
,
24578 /*current_scope_valid_p=*/false);
24579 /* Look for the nested-name-specifier. */
24580 nested_name_specifier
24581 = (cp_parser_nested_name_specifier_opt (parser
,
24582 /*typename_keyword_p=*/false,
24583 /*check_dependency_p=*/false,
24585 /*is_declaration=*/false));
24587 outside_class_specifier_p
= (!at_class_scope_p ()
24588 || !TYPE_BEING_DEFINED (current_class_type
)
24591 /* Outside of a class-specifier, there must be a
24592 nested-name-specifier. */
24593 if (!nested_name_specifier
&& outside_class_specifier_p
)
24594 constructor_p
= false;
24595 else if (nested_name_specifier
== error_mark_node
)
24596 constructor_p
= false;
24598 /* If we have a class scope, this is easy; DR 147 says that S::S always
24599 names the constructor, and no other qualified name could. */
24600 if (constructor_p
&& nested_name_specifier
24601 && CLASS_TYPE_P (nested_name_specifier
))
24603 tree id
= cp_parser_unqualified_id (parser
,
24604 /*template_keyword_p=*/false,
24605 /*check_dependency_p=*/false,
24606 /*declarator_p=*/true,
24607 /*optional_p=*/false);
24608 if (is_overloaded_fn (id
))
24609 id
= DECL_NAME (get_first_fn (id
));
24610 if (!constructor_name_p (id
, nested_name_specifier
))
24611 constructor_p
= false;
24613 /* If we still think that this might be a constructor-declarator,
24614 look for a class-name. */
24615 else if (constructor_p
)
24619 template <typename T> struct S {
24623 we must recognize that the nested `S' names a class. */
24625 type_decl
= cp_parser_class_name (parser
,
24626 /*typename_keyword_p=*/false,
24627 /*template_keyword_p=*/false,
24629 /*check_dependency_p=*/false,
24630 /*class_head_p=*/false,
24631 /*is_declaration=*/false);
24632 /* If there was no class-name, then this is not a constructor.
24633 Otherwise, if we are in a class-specifier and we aren't
24634 handling a friend declaration, check that its type matches
24635 current_class_type (c++/38313). Note: error_mark_node
24636 is left alone for error recovery purposes. */
24637 constructor_p
= (!cp_parser_error_occurred (parser
)
24638 && (outside_class_specifier_p
24639 || type_decl
== error_mark_node
24640 || same_type_p (current_class_type
,
24641 TREE_TYPE (type_decl
))));
24643 /* If we're still considering a constructor, we have to see a `(',
24644 to begin the parameter-declaration-clause, followed by either a
24645 `)', an `...', or a decl-specifier. We need to check for a
24646 type-specifier to avoid being fooled into thinking that:
24650 is a constructor. (It is actually a function named `f' that
24651 takes one parameter (of type `int') and returns a value of type
24654 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
24655 constructor_p
= false;
24658 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
24659 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
24660 /* A parameter declaration begins with a decl-specifier,
24661 which is either the "attribute" keyword, a storage class
24662 specifier, or (usually) a type-specifier. */
24663 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
24666 tree pushed_scope
= NULL_TREE
;
24667 unsigned saved_num_template_parameter_lists
;
24669 /* Names appearing in the type-specifier should be looked up
24670 in the scope of the class. */
24671 if (current_class_type
)
24675 type
= TREE_TYPE (type_decl
);
24676 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24678 type
= resolve_typename_type (type
,
24679 /*only_current_p=*/false);
24680 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24682 cp_parser_abort_tentative_parse (parser
);
24686 pushed_scope
= push_scope (type
);
24689 /* Inside the constructor parameter list, surrounding
24690 template-parameter-lists do not apply. */
24691 saved_num_template_parameter_lists
24692 = parser
->num_template_parameter_lists
;
24693 parser
->num_template_parameter_lists
= 0;
24695 /* Look for the type-specifier. */
24696 cp_parser_type_specifier (parser
,
24697 CP_PARSER_FLAGS_NONE
,
24698 /*decl_specs=*/NULL
,
24699 /*is_declarator=*/true,
24700 /*declares_class_or_enum=*/NULL
,
24701 /*is_cv_qualifier=*/NULL
);
24703 parser
->num_template_parameter_lists
24704 = saved_num_template_parameter_lists
;
24706 /* Leave the scope of the class. */
24708 pop_scope (pushed_scope
);
24710 constructor_p
= !cp_parser_error_occurred (parser
);
24714 /* We did not really want to consume any tokens. */
24715 cp_parser_abort_tentative_parse (parser
);
24717 return constructor_p
;
24720 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24721 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24722 they must be performed once we are in the scope of the function.
24724 Returns the function defined. */
24727 cp_parser_function_definition_from_specifiers_and_declarator
24728 (cp_parser
* parser
,
24729 cp_decl_specifier_seq
*decl_specifiers
,
24731 const cp_declarator
*declarator
)
24736 /* Begin the function-definition. */
24737 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
24739 /* The things we're about to see are not directly qualified by any
24740 template headers we've seen thus far. */
24741 reset_specialization ();
24743 /* If there were names looked up in the decl-specifier-seq that we
24744 did not check, check them now. We must wait until we are in the
24745 scope of the function to perform the checks, since the function
24746 might be a friend. */
24747 perform_deferred_access_checks (tf_warning_or_error
);
24751 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
24752 parser
->omp_declare_simd
= NULL
;
24753 cp_finalize_oacc_routine (parser
, current_function_decl
, true, true);
24754 parser
->oacc_routine
= NULL
;
24759 /* Skip the entire function. */
24760 cp_parser_skip_to_end_of_block_or_statement (parser
);
24761 fn
= error_mark_node
;
24763 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
24765 /* Seen already, skip it. An error message has already been output. */
24766 cp_parser_skip_to_end_of_block_or_statement (parser
);
24767 fn
= current_function_decl
;
24768 current_function_decl
= NULL_TREE
;
24769 /* If this is a function from a class, pop the nested class. */
24770 if (current_class_name
)
24771 pop_nested_class ();
24776 if (DECL_DECLARED_INLINE_P (current_function_decl
))
24777 tv
= TV_PARSE_INLINE
;
24779 tv
= TV_PARSE_FUNC
;
24781 fn
= cp_parser_function_definition_after_declarator (parser
,
24782 /*inline_p=*/false);
24789 /* Parse the part of a function-definition that follows the
24790 declarator. INLINE_P is TRUE iff this function is an inline
24791 function defined within a class-specifier.
24793 Returns the function defined. */
24796 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
24800 bool ctor_initializer_p
= false;
24801 bool saved_in_unbraced_linkage_specification_p
;
24802 bool saved_in_function_body
;
24803 unsigned saved_num_template_parameter_lists
;
24805 bool fully_implicit_function_template_p
24806 = parser
->fully_implicit_function_template_p
;
24807 parser
->fully_implicit_function_template_p
= false;
24808 tree implicit_template_parms
24809 = parser
->implicit_template_parms
;
24810 parser
->implicit_template_parms
= 0;
24811 cp_binding_level
* implicit_template_scope
24812 = parser
->implicit_template_scope
;
24813 parser
->implicit_template_scope
= 0;
24815 saved_in_function_body
= parser
->in_function_body
;
24816 parser
->in_function_body
= true;
24817 /* If the next token is `return', then the code may be trying to
24818 make use of the "named return value" extension that G++ used to
24820 token
= cp_lexer_peek_token (parser
->lexer
);
24821 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
24823 /* Consume the `return' keyword. */
24824 cp_lexer_consume_token (parser
->lexer
);
24825 /* Look for the identifier that indicates what value is to be
24827 cp_parser_identifier (parser
);
24828 /* Issue an error message. */
24829 error_at (token
->location
,
24830 "named return values are no longer supported");
24831 /* Skip tokens until we reach the start of the function body. */
24834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24835 if (token
->type
== CPP_OPEN_BRACE
24836 || token
->type
== CPP_EOF
24837 || token
->type
== CPP_PRAGMA_EOL
)
24839 cp_lexer_consume_token (parser
->lexer
);
24842 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24843 anything declared inside `f'. */
24844 saved_in_unbraced_linkage_specification_p
24845 = parser
->in_unbraced_linkage_specification_p
;
24846 parser
->in_unbraced_linkage_specification_p
= false;
24847 /* Inside the function, surrounding template-parameter-lists do not
24849 saved_num_template_parameter_lists
24850 = parser
->num_template_parameter_lists
;
24851 parser
->num_template_parameter_lists
= 0;
24853 start_lambda_scope (current_function_decl
);
24855 /* If the next token is `try', `__transaction_atomic', or
24856 `__transaction_relaxed`, then we are looking at either function-try-block
24857 or function-transaction-block. Note that all of these include the
24859 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
24860 ctor_initializer_p
= cp_parser_function_transaction (parser
,
24861 RID_TRANSACTION_ATOMIC
);
24862 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
24863 RID_TRANSACTION_RELAXED
))
24864 ctor_initializer_p
= cp_parser_function_transaction (parser
,
24865 RID_TRANSACTION_RELAXED
);
24866 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
24867 ctor_initializer_p
= cp_parser_function_try_block (parser
);
24869 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
24870 (parser
, /*in_function_try_block=*/false);
24872 finish_lambda_scope ();
24874 /* Finish the function. */
24875 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
24876 (inline_p
? 2 : 0));
24877 /* Generate code for it, if necessary. */
24878 expand_or_defer_fn (fn
);
24879 /* Restore the saved values. */
24880 parser
->in_unbraced_linkage_specification_p
24881 = saved_in_unbraced_linkage_specification_p
;
24882 parser
->num_template_parameter_lists
24883 = saved_num_template_parameter_lists
;
24884 parser
->in_function_body
= saved_in_function_body
;
24886 parser
->fully_implicit_function_template_p
24887 = fully_implicit_function_template_p
;
24888 parser
->implicit_template_parms
24889 = implicit_template_parms
;
24890 parser
->implicit_template_scope
24891 = implicit_template_scope
;
24893 if (parser
->fully_implicit_function_template_p
)
24894 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
24899 /* Parse a template-declaration body (following argument list). */
24902 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
24903 tree parameter_list
,
24906 tree decl
= NULL_TREE
;
24907 bool friend_p
= false;
24909 /* We just processed one more parameter list. */
24910 ++parser
->num_template_parameter_lists
;
24912 /* Get the deferred access checks from the parameter list. These
24913 will be checked once we know what is being declared, as for a
24914 member template the checks must be performed in the scope of the
24915 class containing the member. */
24916 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
24918 /* Tentatively parse for a new template parameter list, which can either be
24919 the template keyword or a template introduction. */
24920 if (cp_parser_template_declaration_after_export (parser
, member_p
))
24922 else if (cxx_dialect
>= cxx11
24923 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24924 decl
= cp_parser_alias_declaration (parser
);
24927 /* There are no access checks when parsing a template, as we do not
24928 know if a specialization will be a friend. */
24929 push_deferring_access_checks (dk_no_check
);
24930 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24931 decl
= cp_parser_single_declaration (parser
,
24934 /*explicit_specialization_p=*/false,
24936 pop_deferring_access_checks ();
24938 /* If this is a member template declaration, let the front
24940 if (member_p
&& !friend_p
&& decl
)
24942 if (TREE_CODE (decl
) == TYPE_DECL
)
24943 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
24945 decl
= finish_member_template_decl (decl
);
24947 else if (friend_p
&& decl
24948 && DECL_DECLARES_TYPE_P (decl
))
24949 make_friend_class (current_class_type
, TREE_TYPE (decl
),
24950 /*complain=*/true);
24952 /* We are done with the current parameter list. */
24953 --parser
->num_template_parameter_lists
;
24955 pop_deferring_access_checks ();
24958 finish_template_decl (parameter_list
);
24960 /* Check the template arguments for a literal operator template. */
24962 && DECL_DECLARES_FUNCTION_P (decl
)
24963 && UDLIT_OPER_P (DECL_NAME (decl
)))
24966 if (parameter_list
== NULL_TREE
)
24970 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
24971 if (num_parms
== 1)
24973 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
24974 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
24975 if (TREE_TYPE (parm
) != char_type_node
24976 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
24979 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
24981 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
24982 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
24983 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
24984 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
24985 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
24986 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
24994 if (cxx_dialect
>= cxx14
)
24995 error ("literal operator template %qD has invalid parameter list."
24996 " Expected non-type template argument pack <char...>"
24997 " or <typename CharT, CharT...>",
25000 error ("literal operator template %qD has invalid parameter list."
25001 " Expected non-type template argument pack <char...>",
25006 /* Register member declarations. */
25007 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
25008 finish_member_declaration (decl
);
25009 /* If DECL is a function template, we must return to parse it later.
25010 (Even though there is no definition, there might be default
25011 arguments that need handling.) */
25012 if (member_p
&& decl
25013 && DECL_DECLARES_FUNCTION_P (decl
))
25014 vec_safe_push (unparsed_funs_with_definitions
, decl
);
25017 /* Parse a template introduction header for a template-declaration. Returns
25018 false if tentative parse fails. */
25021 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
25023 cp_parser_parse_tentatively (parser
);
25025 tree saved_scope
= parser
->scope
;
25026 tree saved_object_scope
= parser
->object_scope
;
25027 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25029 /* Look for the optional `::' operator. */
25030 cp_parser_global_scope_opt (parser
,
25031 /*current_scope_valid_p=*/false);
25032 /* Look for the nested-name-specifier. */
25033 cp_parser_nested_name_specifier_opt (parser
,
25034 /*typename_keyword_p=*/false,
25035 /*check_dependency_p=*/true,
25037 /*is_declaration=*/false);
25039 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25040 tree concept_name
= cp_parser_identifier (parser
);
25042 /* Look up the concept for which we will be matching
25043 template parameters. */
25044 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
25046 parser
->scope
= saved_scope
;
25047 parser
->object_scope
= saved_object_scope
;
25048 parser
->qualifying_scope
= saved_qualifying_scope
;
25050 if (concept_name
== error_mark_node
)
25051 cp_parser_simulate_error (parser
);
25053 /* Look for opening brace for introduction. */
25054 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
25056 if (!cp_parser_parse_definitely (parser
))
25059 push_deferring_access_checks (dk_deferred
);
25061 /* Build vector of placeholder parameters and grab
25062 matching identifiers. */
25063 tree introduction_list
= cp_parser_introduction_list (parser
);
25065 /* The introduction-list shall not be empty. */
25066 int nargs
= TREE_VEC_LENGTH (introduction_list
);
25069 error ("empty introduction-list");
25073 /* Look for closing brace for introduction. */
25074 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25077 if (tmpl_decl
== error_mark_node
)
25079 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
25084 /* Build and associate the constraint. */
25085 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
25086 if (parms
&& parms
!= error_mark_node
)
25088 cp_parser_template_declaration_after_parameters (parser
, parms
,
25093 error_at (token
->location
, "no matching concept for template-introduction");
25097 /* Parse a normal template-declaration following the template keyword. */
25100 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
25102 tree parameter_list
;
25103 bool need_lang_pop
;
25104 location_t location
= input_location
;
25106 /* Look for the `<' token. */
25107 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
25109 if (at_class_scope_p () && current_function_decl
)
25111 /* 14.5.2.2 [temp.mem]
25113 A local class shall not have member templates. */
25114 error_at (location
,
25115 "invalid declaration of member template in local class");
25116 cp_parser_skip_to_end_of_block_or_statement (parser
);
25121 A template ... shall not have C linkage. */
25122 if (current_lang_name
== lang_name_c
)
25124 error_at (location
, "template with C linkage");
25125 /* Give it C++ linkage to avoid confusing other parts of the
25127 push_lang_context (lang_name_cplusplus
);
25128 need_lang_pop
= true;
25131 need_lang_pop
= false;
25133 /* We cannot perform access checks on the template parameter
25134 declarations until we know what is being declared, just as we
25135 cannot check the decl-specifier list. */
25136 push_deferring_access_checks (dk_deferred
);
25138 /* If the next token is `>', then we have an invalid
25139 specialization. Rather than complain about an invalid template
25140 parameter, issue an error message here. */
25141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
25143 cp_parser_error (parser
, "invalid explicit specialization");
25144 begin_specialization ();
25145 parameter_list
= NULL_TREE
;
25149 /* Parse the template parameters. */
25150 parameter_list
= cp_parser_template_parameter_list (parser
);
25153 /* Look for the `>'. */
25154 cp_parser_skip_to_end_of_template_parameter_list (parser
);
25156 /* Manage template requirements */
25159 tree reqs
= get_shorthand_constraints (current_template_parms
);
25160 if (tree r
= cp_parser_requires_clause_opt (parser
))
25161 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
25162 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
25165 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
25168 /* For the erroneous case of a template with C linkage, we pushed an
25169 implicit C++ linkage scope; exit that scope now. */
25171 pop_lang_context ();
25174 /* Parse a template-declaration, assuming that the `export' (and
25175 `extern') keywords, if present, has already been scanned. MEMBER_P
25176 is as for cp_parser_template_declaration. */
25179 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
25181 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25183 cp_lexer_consume_token (parser
->lexer
);
25184 cp_parser_explicit_template_declaration (parser
, member_p
);
25187 else if (flag_concepts
)
25188 return cp_parser_template_introduction (parser
, member_p
);
25193 /* Perform the deferred access checks from a template-parameter-list.
25194 CHECKS is a TREE_LIST of access checks, as returned by
25195 get_deferred_access_checks. */
25198 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
25200 ++processing_template_parmlist
;
25201 perform_access_checks (checks
, tf_warning_or_error
);
25202 --processing_template_parmlist
;
25205 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25206 `function-definition' sequence that follows a template header.
25207 If MEMBER_P is true, this declaration appears in a class scope.
25209 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25210 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25213 cp_parser_single_declaration (cp_parser
* parser
,
25214 vec
<deferred_access_check
, va_gc
> *checks
,
25216 bool explicit_specialization_p
,
25219 int declares_class_or_enum
;
25220 tree decl
= NULL_TREE
;
25221 cp_decl_specifier_seq decl_specifiers
;
25222 bool function_definition_p
= false;
25223 cp_token
*decl_spec_token_start
;
25225 /* This function is only used when processing a template
25227 gcc_assert (innermost_scope_kind () == sk_template_parms
25228 || innermost_scope_kind () == sk_template_spec
);
25230 /* Defer access checks until we know what is being declared. */
25231 push_deferring_access_checks (dk_deferred
);
25233 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25235 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25236 cp_parser_decl_specifier_seq (parser
,
25237 CP_PARSER_FLAGS_OPTIONAL
,
25239 &declares_class_or_enum
);
25241 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
25243 /* There are no template typedefs. */
25244 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
25246 error_at (decl_spec_token_start
->location
,
25247 "template declaration of %<typedef%>");
25248 decl
= error_mark_node
;
25251 /* Gather up the access checks that occurred the
25252 decl-specifier-seq. */
25253 stop_deferring_access_checks ();
25255 /* Check for the declaration of a template class. */
25256 if (declares_class_or_enum
)
25258 if (cp_parser_declares_only_class_p (parser
))
25260 // If this is a declaration, but not a definition, associate
25261 // any constraints with the type declaration. Constraints
25262 // are associated with definitions in cp_parser_class_specifier.
25263 if (declares_class_or_enum
== 1)
25264 associate_classtype_constraints (decl_specifiers
.type
);
25266 decl
= shadow_tag (&decl_specifiers
);
25271 friend template <typename T> struct A<T>::B;
25274 A<T>::B will be represented by a TYPENAME_TYPE, and
25275 therefore not recognized by shadow_tag. */
25276 if (friend_p
&& *friend_p
25278 && decl_specifiers
.type
25279 && TYPE_P (decl_specifiers
.type
))
25280 decl
= decl_specifiers
.type
;
25282 if (decl
&& decl
!= error_mark_node
)
25283 decl
= TYPE_NAME (decl
);
25285 decl
= error_mark_node
;
25287 /* Perform access checks for template parameters. */
25288 cp_parser_perform_template_parameter_access_checks (checks
);
25292 /* Complain about missing 'typename' or other invalid type names. */
25293 if (!decl_specifiers
.any_type_specifiers_p
25294 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25296 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25297 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25298 the rest of this declaration. */
25299 decl
= error_mark_node
;
25303 /* If it's not a template class, try for a template function. If
25304 the next token is a `;', then this declaration does not declare
25305 anything. But, if there were errors in the decl-specifiers, then
25306 the error might well have come from an attempted class-specifier.
25307 In that case, there's no need to warn about a missing declarator. */
25309 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
25310 || decl_specifiers
.type
!= error_mark_node
))
25312 decl
= cp_parser_init_declarator (parser
,
25315 /*function_definition_allowed_p=*/true,
25317 declares_class_or_enum
,
25318 &function_definition_p
,
25321 /* 7.1.1-1 [dcl.stc]
25323 A storage-class-specifier shall not be specified in an explicit
25324 specialization... */
25326 && explicit_specialization_p
25327 && decl_specifiers
.storage_class
!= sc_none
)
25329 error_at (decl_spec_token_start
->location
,
25330 "explicit template specialization cannot have a storage class");
25331 decl
= error_mark_node
;
25334 if (decl
&& VAR_P (decl
))
25335 check_template_variable (decl
);
25338 /* Look for a trailing `;' after the declaration. */
25339 if (!function_definition_p
25340 && (decl
== error_mark_node
25341 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
25342 cp_parser_skip_to_end_of_block_or_statement (parser
);
25345 pop_deferring_access_checks ();
25347 /* Clear any current qualification; whatever comes next is the start
25348 of something new. */
25349 parser
->scope
= NULL_TREE
;
25350 parser
->qualifying_scope
= NULL_TREE
;
25351 parser
->object_scope
= NULL_TREE
;
25356 /* Parse a cast-expression that is not the operand of a unary "&". */
25359 cp_parser_simple_cast_expression (cp_parser
*parser
)
25361 return cp_parser_cast_expression (parser
, /*address_p=*/false,
25362 /*cast_p=*/false, /*decltype*/false, NULL
);
25365 /* Parse a functional cast to TYPE. Returns an expression
25366 representing the cast. */
25369 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
25371 vec
<tree
, va_gc
> *vec
;
25372 tree expression_list
;
25377 type
= error_mark_node
;
25379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25381 cp_lexer_set_source_position (parser
->lexer
);
25382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25383 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
25384 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
25385 if (TREE_CODE (type
) == TYPE_DECL
)
25386 type
= TREE_TYPE (type
);
25387 return finish_compound_literal (type
, expression_list
,
25388 tf_warning_or_error
);
25392 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
25394 /*allow_expansion_p=*/true,
25395 /*non_constant_p=*/NULL
);
25397 expression_list
= error_mark_node
;
25400 expression_list
= build_tree_list_vec (vec
);
25401 release_tree_vector (vec
);
25404 cast
= build_functional_cast (type
, expression_list
,
25405 tf_warning_or_error
);
25406 /* [expr.const]/1: In an integral constant expression "only type
25407 conversions to integral or enumeration type can be used". */
25408 if (TREE_CODE (type
) == TYPE_DECL
)
25409 type
= TREE_TYPE (type
);
25410 if (cast
!= error_mark_node
25411 && !cast_valid_in_integral_constant_expression_p (type
)
25412 && cp_parser_non_integral_constant_expression (parser
,
25414 return error_mark_node
;
25418 /* Save the tokens that make up the body of a member function defined
25419 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25420 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25421 specifiers applied to the declaration. FIRST_DECL indicates if
25422 DECLARATOR is the first declarator in a declaration sequence. Returns
25423 the FUNCTION_DECL for the member function. */
25426 cp_parser_save_member_function_body (cp_parser
* parser
,
25427 cp_decl_specifier_seq
*decl_specifiers
,
25428 cp_declarator
*declarator
,
25429 tree attributes
, bool first_decl
)
25435 /* Create the FUNCTION_DECL. */
25436 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
25437 cp_finalize_omp_declare_simd (parser
, fn
);
25438 cp_finalize_oacc_routine (parser
, fn
, true, first_decl
);
25439 /* If something went badly wrong, bail out now. */
25440 if (fn
== error_mark_node
)
25442 /* If there's a function-body, skip it. */
25443 if (cp_parser_token_starts_function_definition_p
25444 (cp_lexer_peek_token (parser
->lexer
)))
25445 cp_parser_skip_to_end_of_block_or_statement (parser
);
25446 return error_mark_node
;
25449 /* Remember it, if there default args to post process. */
25450 cp_parser_save_default_args (parser
, fn
);
25452 /* Save away the tokens that make up the body of the
25454 first
= parser
->lexer
->next_token
;
25455 /* Handle function try blocks. */
25456 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
25457 cp_lexer_consume_token (parser
->lexer
);
25458 /* We can have braced-init-list mem-initializers before the fn body. */
25459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
25461 cp_lexer_consume_token (parser
->lexer
);
25462 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
25464 /* cache_group will stop after an un-nested { } pair, too. */
25465 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
25468 /* variadic mem-inits have ... after the ')'. */
25469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25470 cp_lexer_consume_token (parser
->lexer
);
25473 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25474 /* Handle function try blocks. */
25475 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
25476 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25477 last
= parser
->lexer
->next_token
;
25479 /* Save away the inline definition; we will process it when the
25480 class is complete. */
25481 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
25482 DECL_PENDING_INLINE_P (fn
) = 1;
25484 /* We need to know that this was defined in the class, so that
25485 friend templates are handled correctly. */
25486 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
25488 /* Add FN to the queue of functions to be parsed later. */
25489 vec_safe_push (unparsed_funs_with_definitions
, fn
);
25494 /* Save the tokens that make up the in-class initializer for a non-static
25495 data member. Returns a DEFAULT_ARG. */
25498 cp_parser_save_nsdmi (cp_parser
* parser
)
25500 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
25503 /* Parse a template-argument-list, as well as the trailing ">" (but
25504 not the opening "<"). See cp_parser_template_argument_list for the
25508 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
25512 tree saved_qualifying_scope
;
25513 tree saved_object_scope
;
25514 bool saved_greater_than_is_operator_p
;
25515 int saved_unevaluated_operand
;
25516 int saved_inhibit_evaluation_warnings
;
25520 When parsing a template-id, the first non-nested `>' is taken as
25521 the end of the template-argument-list rather than a greater-than
25523 saved_greater_than_is_operator_p
25524 = parser
->greater_than_is_operator_p
;
25525 parser
->greater_than_is_operator_p
= false;
25526 /* Parsing the argument list may modify SCOPE, so we save it
25528 saved_scope
= parser
->scope
;
25529 saved_qualifying_scope
= parser
->qualifying_scope
;
25530 saved_object_scope
= parser
->object_scope
;
25531 /* We need to evaluate the template arguments, even though this
25532 template-id may be nested within a "sizeof". */
25533 saved_unevaluated_operand
= cp_unevaluated_operand
;
25534 cp_unevaluated_operand
= 0;
25535 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25536 c_inhibit_evaluation_warnings
= 0;
25537 /* Parse the template-argument-list itself. */
25538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
25539 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
25540 arguments
= NULL_TREE
;
25542 arguments
= cp_parser_template_argument_list (parser
);
25543 /* Look for the `>' that ends the template-argument-list. If we find
25544 a '>>' instead, it's probably just a typo. */
25545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
25547 if (cxx_dialect
!= cxx98
)
25549 /* In C++0x, a `>>' in a template argument list or cast
25550 expression is considered to be two separate `>'
25551 tokens. So, change the current token to a `>', but don't
25552 consume it: it will be consumed later when the outer
25553 template argument list (or cast expression) is parsed.
25554 Note that this replacement of `>' for `>>' is necessary
25555 even if we are parsing tentatively: in the tentative
25556 case, after calling
25557 cp_parser_enclosed_template_argument_list we will always
25558 throw away all of the template arguments and the first
25559 closing `>', either because the template argument list
25560 was erroneous or because we are replacing those tokens
25561 with a CPP_TEMPLATE_ID token. The second `>' (which will
25562 not have been thrown away) is needed either to close an
25563 outer template argument list or to complete a new-style
25565 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25566 token
->type
= CPP_GREATER
;
25568 else if (!saved_greater_than_is_operator_p
)
25570 /* If we're in a nested template argument list, the '>>' has
25571 to be a typo for '> >'. We emit the error message, but we
25572 continue parsing and we push a '>' as next token, so that
25573 the argument list will be parsed correctly. Note that the
25574 global source location is still on the token before the
25575 '>>', so we need to say explicitly where we want it. */
25576 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25577 error_at (token
->location
, "%<>>%> should be %<> >%> "
25578 "within a nested template argument list");
25580 token
->type
= CPP_GREATER
;
25584 /* If this is not a nested template argument list, the '>>'
25585 is a typo for '>'. Emit an error message and continue.
25586 Same deal about the token location, but here we can get it
25587 right by consuming the '>>' before issuing the diagnostic. */
25588 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25589 error_at (token
->location
,
25590 "spurious %<>>%>, use %<>%> to terminate "
25591 "a template argument list");
25595 cp_parser_skip_to_end_of_template_parameter_list (parser
);
25596 /* The `>' token might be a greater-than operator again now. */
25597 parser
->greater_than_is_operator_p
25598 = saved_greater_than_is_operator_p
;
25599 /* Restore the SAVED_SCOPE. */
25600 parser
->scope
= saved_scope
;
25601 parser
->qualifying_scope
= saved_qualifying_scope
;
25602 parser
->object_scope
= saved_object_scope
;
25603 cp_unevaluated_operand
= saved_unevaluated_operand
;
25604 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25609 /* MEMBER_FUNCTION is a member function, or a friend. If default
25610 arguments, or the body of the function have not yet been parsed,
25614 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
25616 timevar_push (TV_PARSE_INMETH
);
25617 /* If this member is a template, get the underlying
25619 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
25620 member_function
= DECL_TEMPLATE_RESULT (member_function
);
25622 /* There should not be any class definitions in progress at this
25623 point; the bodies of members are only parsed outside of all class
25625 gcc_assert (parser
->num_classes_being_defined
== 0);
25626 /* While we're parsing the member functions we might encounter more
25627 classes. We want to handle them right away, but we don't want
25628 them getting mixed up with functions that are currently in the
25630 push_unparsed_function_queues (parser
);
25632 /* Make sure that any template parameters are in scope. */
25633 maybe_begin_member_template_processing (member_function
);
25635 /* If the body of the function has not yet been parsed, parse it
25637 if (DECL_PENDING_INLINE_P (member_function
))
25639 tree function_scope
;
25640 cp_token_cache
*tokens
;
25642 /* The function is no longer pending; we are processing it. */
25643 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
25644 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
25645 DECL_PENDING_INLINE_P (member_function
) = 0;
25647 /* If this is a local class, enter the scope of the containing
25649 function_scope
= current_function_decl
;
25650 if (function_scope
)
25651 push_function_context ();
25653 /* Push the body of the function onto the lexer stack. */
25654 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25656 /* Let the front end know that we going to be defining this
25658 start_preparsed_function (member_function
, NULL_TREE
,
25659 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
25661 /* Don't do access checking if it is a templated function. */
25662 if (processing_template_decl
)
25663 push_deferring_access_checks (dk_no_check
);
25665 /* #pragma omp declare reduction needs special parsing. */
25666 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
25668 parser
->lexer
->in_pragma
= true;
25669 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
25670 finish_function (/*inline*/2);
25671 cp_check_omp_declare_reduction (member_function
);
25674 /* Now, parse the body of the function. */
25675 cp_parser_function_definition_after_declarator (parser
,
25676 /*inline_p=*/true);
25678 if (processing_template_decl
)
25679 pop_deferring_access_checks ();
25681 /* Leave the scope of the containing function. */
25682 if (function_scope
)
25683 pop_function_context ();
25684 cp_parser_pop_lexer (parser
);
25687 /* Remove any template parameters from the symbol table. */
25688 maybe_end_member_template_processing ();
25690 /* Restore the queue. */
25691 pop_unparsed_function_queues (parser
);
25692 timevar_pop (TV_PARSE_INMETH
);
25695 /* If DECL contains any default args, remember it on the unparsed
25696 functions queue. */
25699 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
25703 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
25705 probe
= TREE_CHAIN (probe
))
25706 if (TREE_PURPOSE (probe
))
25708 cp_default_arg_entry entry
= {current_class_type
, decl
};
25709 vec_safe_push (unparsed_funs_with_default_args
, entry
);
25714 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25715 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25716 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25717 from the parameter-type-list. */
25720 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
25721 tree default_arg
, tree parmtype
)
25723 cp_token_cache
*tokens
;
25727 if (default_arg
== error_mark_node
)
25728 return error_mark_node
;
25730 /* Push the saved tokens for the default argument onto the parser's
25732 tokens
= DEFARG_TOKENS (default_arg
);
25733 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25735 start_lambda_scope (decl
);
25737 /* Parse the default argument. */
25738 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
25739 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
25740 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25742 finish_lambda_scope ();
25744 if (parsed_arg
== error_mark_node
)
25745 cp_parser_skip_to_end_of_statement (parser
);
25747 if (!processing_template_decl
)
25749 /* In a non-template class, check conversions now. In a template,
25750 we'll wait and instantiate these as needed. */
25751 if (TREE_CODE (decl
) == PARM_DECL
)
25752 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
25753 tf_warning_or_error
);
25755 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
25758 /* If the token stream has not been completely used up, then
25759 there was extra junk after the end of the default
25761 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
25763 if (TREE_CODE (decl
) == PARM_DECL
)
25764 cp_parser_error (parser
, "expected %<,%>");
25766 cp_parser_error (parser
, "expected %<;%>");
25769 /* Revert to the main lexer. */
25770 cp_parser_pop_lexer (parser
);
25775 /* FIELD is a non-static data member with an initializer which we saved for
25776 later; parse it now. */
25779 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
25783 maybe_begin_member_template_processing (field
);
25785 push_unparsed_function_queues (parser
);
25786 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
25787 DECL_INITIAL (field
),
25789 pop_unparsed_function_queues (parser
);
25791 maybe_end_member_template_processing ();
25793 DECL_INITIAL (field
) = def
;
25796 /* FN is a FUNCTION_DECL which may contains a parameter with an
25797 unparsed DEFAULT_ARG. Parse the default args now. This function
25798 assumes that the current scope is the scope in which the default
25799 argument should be processed. */
25802 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
25804 bool saved_local_variables_forbidden_p
;
25805 tree parm
, parmdecl
;
25807 /* While we're parsing the default args, we might (due to the
25808 statement expression extension) encounter more classes. We want
25809 to handle them right away, but we don't want them getting mixed
25810 up with default args that are currently in the queue. */
25811 push_unparsed_function_queues (parser
);
25813 /* Local variable names (and the `this' keyword) may not appear
25814 in a default argument. */
25815 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
25816 parser
->local_variables_forbidden_p
= true;
25818 push_defarg_context (fn
);
25820 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
25821 parmdecl
= DECL_ARGUMENTS (fn
);
25822 parm
&& parm
!= void_list_node
;
25823 parm
= TREE_CHAIN (parm
),
25824 parmdecl
= DECL_CHAIN (parmdecl
))
25826 tree default_arg
= TREE_PURPOSE (parm
);
25828 vec
<tree
, va_gc
> *insts
;
25835 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
25836 /* This can happen for a friend declaration for a function
25837 already declared with default arguments. */
25841 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
25843 TREE_VALUE (parm
));
25844 if (parsed_arg
== error_mark_node
)
25849 TREE_PURPOSE (parm
) = parsed_arg
;
25851 /* Update any instantiations we've already created. */
25852 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
25853 vec_safe_iterate (insts
, ix
, ©
); ix
++)
25854 TREE_PURPOSE (copy
) = parsed_arg
;
25857 pop_defarg_context ();
25859 /* Make sure no default arg is missing. */
25860 check_default_args (fn
);
25862 /* Restore the state of local_variables_forbidden_p. */
25863 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
25865 /* Restore the queue. */
25866 pop_unparsed_function_queues (parser
);
25869 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25871 sizeof ... ( identifier )
25873 where the 'sizeof' token has already been consumed. */
25876 cp_parser_sizeof_pack (cp_parser
*parser
)
25878 /* Consume the `...'. */
25879 cp_lexer_consume_token (parser
->lexer
);
25880 maybe_warn_variadic_templates ();
25882 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
25884 cp_lexer_consume_token (parser
->lexer
);
25886 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
25887 "%<sizeof...%> argument must be surrounded by parentheses");
25889 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25890 tree name
= cp_parser_identifier (parser
);
25891 if (name
== error_mark_node
)
25892 return error_mark_node
;
25893 /* The name is not qualified. */
25894 parser
->scope
= NULL_TREE
;
25895 parser
->qualifying_scope
= NULL_TREE
;
25896 parser
->object_scope
= NULL_TREE
;
25897 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
25898 if (expr
== error_mark_node
)
25899 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
25901 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
25902 expr
= TREE_TYPE (expr
);
25903 else if (TREE_CODE (expr
) == CONST_DECL
)
25904 expr
= DECL_INITIAL (expr
);
25905 expr
= make_pack_expansion (expr
);
25908 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25913 /* Parse the operand of `sizeof' (or a similar operator). Returns
25914 either a TYPE or an expression, depending on the form of the
25915 input. The KEYWORD indicates which kind of expression we have
25919 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
25921 tree expr
= NULL_TREE
;
25922 const char *saved_message
;
25924 bool saved_integral_constant_expression_p
;
25925 bool saved_non_integral_constant_expression_p
;
25927 /* If it's a `...', then we are computing the length of a parameter
25929 if (keyword
== RID_SIZEOF
25930 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25931 return cp_parser_sizeof_pack (parser
);
25933 /* Types cannot be defined in a `sizeof' expression. Save away the
25935 saved_message
= parser
->type_definition_forbidden_message
;
25936 /* And create the new one. */
25937 tmp
= concat ("types may not be defined in %<",
25938 IDENTIFIER_POINTER (ridpointers
[keyword
]),
25939 "%> expressions", NULL
);
25940 parser
->type_definition_forbidden_message
= tmp
;
25942 /* The restrictions on constant-expressions do not apply inside
25943 sizeof expressions. */
25944 saved_integral_constant_expression_p
25945 = parser
->integral_constant_expression_p
;
25946 saved_non_integral_constant_expression_p
25947 = parser
->non_integral_constant_expression_p
;
25948 parser
->integral_constant_expression_p
= false;
25950 /* Do not actually evaluate the expression. */
25951 ++cp_unevaluated_operand
;
25952 ++c_inhibit_evaluation_warnings
;
25953 /* If it's a `(', then we might be looking at the type-id
25955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25957 tree type
= NULL_TREE
;
25959 /* We can't be sure yet whether we're looking at a type-id or an
25961 cp_parser_parse_tentatively (parser
);
25962 /* Note: as a GNU Extension, compound literals are considered
25963 postfix-expressions as they are in C99, so they are valid
25964 arguments to sizeof. See comment in cp_parser_cast_expression
25966 if (cp_parser_compound_literal_p (parser
))
25967 cp_parser_simulate_error (parser
);
25970 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
25971 parser
->in_type_id_in_expr_p
= true;
25972 /* Look for the type-id. */
25973 type
= cp_parser_type_id (parser
);
25974 /* Look for the closing `)'. */
25975 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25976 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
25979 /* If all went well, then we're done. */
25980 if (cp_parser_parse_definitely (parser
))
25982 cp_decl_specifier_seq decl_specs
;
25984 /* Build a trivial decl-specifier-seq. */
25985 clear_decl_specs (&decl_specs
);
25986 decl_specs
.type
= type
;
25988 /* Call grokdeclarator to figure out what type this is. */
25989 expr
= grokdeclarator (NULL
,
25993 /*attrlist=*/NULL
);
25997 /* If the type-id production did not work out, then we must be
25998 looking at the unary-expression production. */
26000 expr
= cp_parser_unary_expression (parser
);
26002 /* Go back to evaluating expressions. */
26003 --cp_unevaluated_operand
;
26004 --c_inhibit_evaluation_warnings
;
26006 /* Free the message we created. */
26008 /* And restore the old one. */
26009 parser
->type_definition_forbidden_message
= saved_message
;
26010 parser
->integral_constant_expression_p
26011 = saved_integral_constant_expression_p
;
26012 parser
->non_integral_constant_expression_p
26013 = saved_non_integral_constant_expression_p
;
26018 /* If the current declaration has no declarator, return true. */
26021 cp_parser_declares_only_class_p (cp_parser
*parser
)
26023 /* If the next token is a `;' or a `,' then there is no
26025 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26026 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
26029 /* Update the DECL_SPECS to reflect the storage class indicated by
26033 cp_parser_set_storage_class (cp_parser
*parser
,
26034 cp_decl_specifier_seq
*decl_specs
,
26038 cp_storage_class storage_class
;
26040 if (parser
->in_unbraced_linkage_specification_p
)
26042 error_at (token
->location
, "invalid use of %qD in linkage specification",
26043 ridpointers
[keyword
]);
26046 else if (decl_specs
->storage_class
!= sc_none
)
26048 decl_specs
->conflicting_specifiers_p
= true;
26052 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
26053 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
26054 && decl_specs
->gnu_thread_keyword_p
)
26056 pedwarn (decl_specs
->locations
[ds_thread
], 0,
26057 "%<__thread%> before %qD", ridpointers
[keyword
]);
26063 storage_class
= sc_auto
;
26066 storage_class
= sc_register
;
26069 storage_class
= sc_static
;
26072 storage_class
= sc_extern
;
26075 storage_class
= sc_mutable
;
26078 gcc_unreachable ();
26080 decl_specs
->storage_class
= storage_class
;
26081 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
26083 /* A storage class specifier cannot be applied alongside a typedef
26084 specifier. If there is a typedef specifier present then set
26085 conflicting_specifiers_p which will trigger an error later
26086 on in grokdeclarator. */
26087 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
26088 decl_specs
->conflicting_specifiers_p
= true;
26091 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26092 is true, the type is a class or enum definition. */
26095 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
26098 bool type_definition_p
)
26100 decl_specs
->any_specifiers_p
= true;
26102 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26103 (with, for example, in "typedef int wchar_t;") we remember that
26104 this is what happened. In system headers, we ignore these
26105 declarations so that G++ can work with system headers that are not
26107 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
26108 && !type_definition_p
26109 && (type_spec
== boolean_type_node
26110 || type_spec
== char16_type_node
26111 || type_spec
== char32_type_node
26112 || type_spec
== wchar_type_node
)
26113 && (decl_specs
->type
26114 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
26115 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
26116 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
26117 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
26119 decl_specs
->redefined_builtin_type
= type_spec
;
26120 set_and_check_decl_spec_loc (decl_specs
,
26121 ds_redefined_builtin_type_spec
,
26123 if (!decl_specs
->type
)
26125 decl_specs
->type
= type_spec
;
26126 decl_specs
->type_definition_p
= false;
26127 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
26130 else if (decl_specs
->type
)
26131 decl_specs
->multiple_types_p
= true;
26134 decl_specs
->type
= type_spec
;
26135 decl_specs
->type_definition_p
= type_definition_p
;
26136 decl_specs
->redefined_builtin_type
= NULL_TREE
;
26137 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
26141 /* True iff TOKEN is the GNU keyword __thread. */
26144 token_is__thread (cp_token
*token
)
26146 gcc_assert (token
->keyword
== RID_THREAD
);
26147 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
26150 /* Set the location for a declarator specifier and check if it is
26153 DECL_SPECS is the sequence of declarator specifiers onto which to
26156 DS is the single declarator specifier to set which location is to
26157 be set onto the existing sequence of declarators.
26159 LOCATION is the location for the declarator specifier to
26163 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
26164 cp_decl_spec ds
, cp_token
*token
)
26166 gcc_assert (ds
< ds_last
);
26168 if (decl_specs
== NULL
)
26171 source_location location
= token
->location
;
26173 if (decl_specs
->locations
[ds
] == 0)
26175 decl_specs
->locations
[ds
] = location
;
26176 if (ds
== ds_thread
)
26177 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
26183 if (decl_specs
->locations
[ds_long_long
] != 0)
26184 error_at (location
,
26185 "%<long long long%> is too long for GCC");
26188 decl_specs
->locations
[ds_long_long
] = location
;
26189 pedwarn_cxx98 (location
,
26191 "ISO C++ 1998 does not support %<long long%>");
26194 else if (ds
== ds_thread
)
26196 bool gnu
= token_is__thread (token
);
26197 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
26198 error_at (location
,
26199 "both %<__thread%> and %<thread_local%> specified");
26201 error_at (location
, "duplicate %qD", token
->u
.value
);
26205 static const char *const decl_spec_names
[] = {
26222 error_at (location
,
26223 "duplicate %qs", decl_spec_names
[ds
]);
26228 /* Return true iff the declarator specifier DS is present in the
26229 sequence of declarator specifiers DECL_SPECS. */
26232 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
26235 gcc_assert (ds
< ds_last
);
26237 if (decl_specs
== NULL
)
26240 return decl_specs
->locations
[ds
] != 0;
26243 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26244 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26247 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
26249 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
26252 /* Issue an error message indicating that TOKEN_DESC was expected.
26253 If KEYWORD is true, it indicated this function is called by
26254 cp_parser_require_keword and the required token can only be
26255 a indicated keyword. */
26258 cp_parser_required_error (cp_parser
*parser
,
26259 required_token token_desc
,
26262 switch (token_desc
)
26265 cp_parser_error (parser
, "expected %<new%>");
26268 cp_parser_error (parser
, "expected %<delete%>");
26271 cp_parser_error (parser
, "expected %<return%>");
26274 cp_parser_error (parser
, "expected %<while%>");
26277 cp_parser_error (parser
, "expected %<extern%>");
26279 case RT_STATIC_ASSERT
:
26280 cp_parser_error (parser
, "expected %<static_assert%>");
26283 cp_parser_error (parser
, "expected %<decltype%>");
26286 cp_parser_error (parser
, "expected %<operator%>");
26289 cp_parser_error (parser
, "expected %<class%>");
26292 cp_parser_error (parser
, "expected %<template%>");
26295 cp_parser_error (parser
, "expected %<namespace%>");
26298 cp_parser_error (parser
, "expected %<using%>");
26301 cp_parser_error (parser
, "expected %<asm%>");
26304 cp_parser_error (parser
, "expected %<try%>");
26307 cp_parser_error (parser
, "expected %<catch%>");
26310 cp_parser_error (parser
, "expected %<throw%>");
26313 cp_parser_error (parser
, "expected %<__label__%>");
26316 cp_parser_error (parser
, "expected %<@try%>");
26318 case RT_AT_SYNCHRONIZED
:
26319 cp_parser_error (parser
, "expected %<@synchronized%>");
26322 cp_parser_error (parser
, "expected %<@throw%>");
26324 case RT_TRANSACTION_ATOMIC
:
26325 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
26327 case RT_TRANSACTION_RELAXED
:
26328 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
26335 switch (token_desc
)
26338 cp_parser_error (parser
, "expected %<;%>");
26340 case RT_OPEN_PAREN
:
26341 cp_parser_error (parser
, "expected %<(%>");
26343 case RT_CLOSE_BRACE
:
26344 cp_parser_error (parser
, "expected %<}%>");
26346 case RT_OPEN_BRACE
:
26347 cp_parser_error (parser
, "expected %<{%>");
26349 case RT_CLOSE_SQUARE
:
26350 cp_parser_error (parser
, "expected %<]%>");
26352 case RT_OPEN_SQUARE
:
26353 cp_parser_error (parser
, "expected %<[%>");
26356 cp_parser_error (parser
, "expected %<,%>");
26359 cp_parser_error (parser
, "expected %<::%>");
26362 cp_parser_error (parser
, "expected %<<%>");
26365 cp_parser_error (parser
, "expected %<>%>");
26368 cp_parser_error (parser
, "expected %<=%>");
26371 cp_parser_error (parser
, "expected %<...%>");
26374 cp_parser_error (parser
, "expected %<*%>");
26377 cp_parser_error (parser
, "expected %<~%>");
26380 cp_parser_error (parser
, "expected %<:%>");
26382 case RT_COLON_SCOPE
:
26383 cp_parser_error (parser
, "expected %<:%> or %<::%>");
26385 case RT_CLOSE_PAREN
:
26386 cp_parser_error (parser
, "expected %<)%>");
26388 case RT_COMMA_CLOSE_PAREN
:
26389 cp_parser_error (parser
, "expected %<,%> or %<)%>");
26391 case RT_PRAGMA_EOL
:
26392 cp_parser_error (parser
, "expected end of line");
26395 cp_parser_error (parser
, "expected identifier");
26398 cp_parser_error (parser
, "expected selection-statement");
26400 case RT_INTERATION
:
26401 cp_parser_error (parser
, "expected iteration-statement");
26404 cp_parser_error (parser
, "expected jump-statement");
26407 cp_parser_error (parser
, "expected class-key");
26409 case RT_CLASS_TYPENAME_TEMPLATE
:
26410 cp_parser_error (parser
,
26411 "expected %<class%>, %<typename%>, or %<template%>");
26414 gcc_unreachable ();
26418 gcc_unreachable ();
26423 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26424 issue an error message indicating that TOKEN_DESC was expected.
26426 Returns the token consumed, if the token had the appropriate type.
26427 Otherwise, returns NULL. */
26430 cp_parser_require (cp_parser
* parser
,
26431 enum cpp_ttype type
,
26432 required_token token_desc
)
26434 if (cp_lexer_next_token_is (parser
->lexer
, type
))
26435 return cp_lexer_consume_token (parser
->lexer
);
26438 /* Output the MESSAGE -- unless we're parsing tentatively. */
26439 if (!cp_parser_simulate_error (parser
))
26440 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
26445 /* An error message is produced if the next token is not '>'.
26446 All further tokens are skipped until the desired token is
26447 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26450 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
26452 /* Current level of '< ... >'. */
26453 unsigned level
= 0;
26454 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26455 unsigned nesting_depth
= 0;
26457 /* Are we ready, yet? If not, issue error message. */
26458 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
26461 /* Skip tokens until the desired token is found. */
26464 /* Peek at the next token. */
26465 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26468 if (!nesting_depth
)
26473 if (cxx_dialect
== cxx98
)
26474 /* C++0x views the `>>' operator as two `>' tokens, but
26477 else if (!nesting_depth
&& level
-- == 0)
26479 /* We've hit a `>>' where the first `>' closes the
26480 template argument list, and the second `>' is
26481 spurious. Just consume the `>>' and stop; we've
26482 already produced at least one error. */
26483 cp_lexer_consume_token (parser
->lexer
);
26486 /* Fall through for C++0x, so we handle the second `>' in
26490 if (!nesting_depth
&& level
-- == 0)
26492 /* We've reached the token we want, consume it and stop. */
26493 cp_lexer_consume_token (parser
->lexer
);
26498 case CPP_OPEN_PAREN
:
26499 case CPP_OPEN_SQUARE
:
26503 case CPP_CLOSE_PAREN
:
26504 case CPP_CLOSE_SQUARE
:
26505 if (nesting_depth
-- == 0)
26510 case CPP_PRAGMA_EOL
:
26511 case CPP_SEMICOLON
:
26512 case CPP_OPEN_BRACE
:
26513 case CPP_CLOSE_BRACE
:
26514 /* The '>' was probably forgotten, don't look further. */
26521 /* Consume this token. */
26522 cp_lexer_consume_token (parser
->lexer
);
26526 /* If the next token is the indicated keyword, consume it. Otherwise,
26527 issue an error message indicating that TOKEN_DESC was expected.
26529 Returns the token consumed, if the token had the appropriate type.
26530 Otherwise, returns NULL. */
26533 cp_parser_require_keyword (cp_parser
* parser
,
26535 required_token token_desc
)
26537 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
26539 if (token
&& token
->keyword
!= keyword
)
26541 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
26548 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26549 function-definition. */
26552 cp_parser_token_starts_function_definition_p (cp_token
* token
)
26554 return (/* An ordinary function-body begins with an `{'. */
26555 token
->type
== CPP_OPEN_BRACE
26556 /* A ctor-initializer begins with a `:'. */
26557 || token
->type
== CPP_COLON
26558 /* A function-try-block begins with `try'. */
26559 || token
->keyword
== RID_TRY
26560 /* A function-transaction-block begins with `__transaction_atomic'
26561 or `__transaction_relaxed'. */
26562 || token
->keyword
== RID_TRANSACTION_ATOMIC
26563 || token
->keyword
== RID_TRANSACTION_RELAXED
26564 /* The named return value extension begins with `return'. */
26565 || token
->keyword
== RID_RETURN
);
26568 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26572 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
26576 token
= cp_lexer_peek_token (parser
->lexer
);
26577 return (token
->type
== CPP_OPEN_BRACE
26578 || (token
->type
== CPP_COLON
26579 && !parser
->colon_doesnt_start_class_def_p
));
26582 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26583 C++0x) ending a template-argument. */
26586 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
26590 token
= cp_lexer_peek_token (parser
->lexer
);
26591 return (token
->type
== CPP_COMMA
26592 || token
->type
== CPP_GREATER
26593 || token
->type
== CPP_ELLIPSIS
26594 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
26597 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26598 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26601 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
26606 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26607 if (token
->type
== CPP_LESS
)
26609 /* Check for the sequence `<::' in the original code. It would be lexed as
26610 `[:', where `[' is a digraph, and there is no whitespace before
26612 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
26615 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
26616 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
26622 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26623 or none_type otherwise. */
26625 static enum tag_types
26626 cp_parser_token_is_class_key (cp_token
* token
)
26628 switch (token
->keyword
)
26633 return record_type
;
26642 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26643 or none_type otherwise or if the token is null. */
26645 static enum tag_types
26646 cp_parser_token_is_type_parameter_key (cp_token
* token
)
26651 switch (token
->keyword
)
26656 return typename_type
;
26663 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26666 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
26668 if (type
== error_mark_node
)
26670 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
26672 if (permerror (input_location
, "%qs tag used in naming %q#T",
26673 class_key
== union_type
? "union"
26674 : class_key
== record_type
? "struct" : "class",
26676 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
26677 "%q#T was previously declared here", type
);
26681 /* Issue an error message if DECL is redeclared with different
26682 access than its original declaration [class.access.spec/3].
26683 This applies to nested classes and nested class templates.
26687 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
26689 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
26692 if ((TREE_PRIVATE (decl
)
26693 != (current_access_specifier
== access_private_node
))
26694 || (TREE_PROTECTED (decl
)
26695 != (current_access_specifier
== access_protected_node
)))
26696 error_at (location
, "%qD redeclared with different access", decl
);
26699 /* Look for the `template' keyword, as a syntactic disambiguator.
26700 Return TRUE iff it is present, in which case it will be
26704 cp_parser_optional_template_keyword (cp_parser
*parser
)
26706 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26708 /* In C++98 the `template' keyword can only be used within templates;
26709 outside templates the parser can always figure out what is a
26710 template and what is not. In C++11, per the resolution of DR 468,
26711 `template' is allowed in cases where it is not strictly necessary. */
26712 if (!processing_template_decl
26713 && pedantic
&& cxx_dialect
== cxx98
)
26715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26716 pedwarn (token
->location
, OPT_Wpedantic
,
26717 "in C++98 %<template%> (as a disambiguator) is only "
26718 "allowed within templates");
26719 /* If this part of the token stream is rescanned, the same
26720 error message would be generated. So, we purge the token
26721 from the stream. */
26722 cp_lexer_purge_token (parser
->lexer
);
26727 /* Consume the `template' keyword. */
26728 cp_lexer_consume_token (parser
->lexer
);
26735 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26736 set PARSER->SCOPE, and perform other related actions. */
26739 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
26742 struct tree_check
*check_value
;
26743 deferred_access_check
*chk
;
26744 vec
<deferred_access_check
, va_gc
> *checks
;
26746 /* Get the stored value. */
26747 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
26748 /* Perform any access checks that were deferred. */
26749 checks
= check_value
->checks
;
26752 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
26753 perform_or_defer_access_check (chk
->binfo
,
26755 chk
->diag_decl
, tf_warning_or_error
);
26757 /* Set the scope from the stored value. */
26758 parser
->scope
= check_value
->value
;
26759 parser
->qualifying_scope
= check_value
->qualifying_scope
;
26760 parser
->object_scope
= NULL_TREE
;
26763 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26764 encounter the end of a block before what we were looking for. */
26767 cp_parser_cache_group (cp_parser
*parser
,
26768 enum cpp_ttype end
,
26773 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26775 /* Abort a parenthesized expression if we encounter a semicolon. */
26776 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
26777 && token
->type
== CPP_SEMICOLON
)
26779 /* If we've reached the end of the file, stop. */
26780 if (token
->type
== CPP_EOF
26781 || (end
!= CPP_PRAGMA_EOL
26782 && token
->type
== CPP_PRAGMA_EOL
))
26784 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
26785 /* We've hit the end of an enclosing block, so there's been some
26786 kind of syntax error. */
26789 /* Consume the token. */
26790 cp_lexer_consume_token (parser
->lexer
);
26791 /* See if it starts a new group. */
26792 if (token
->type
== CPP_OPEN_BRACE
)
26794 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
26795 /* In theory this should probably check end == '}', but
26796 cp_parser_save_member_function_body needs it to exit
26797 after either '}' or ')' when called with ')'. */
26801 else if (token
->type
== CPP_OPEN_PAREN
)
26803 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
26804 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
26807 else if (token
->type
== CPP_PRAGMA
)
26808 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
26809 else if (token
->type
== end
)
26814 /* Like above, for caching a default argument or NSDMI. Both of these are
26815 terminated by a non-nested comma, but it can be unclear whether or not a
26816 comma is nested in a template argument list unless we do more parsing.
26817 In order to handle this ambiguity, when we encounter a ',' after a '<'
26818 we try to parse what follows as a parameter-declaration-list (in the
26819 case of a default argument) or a member-declarator (in the case of an
26820 NSDMI). If that succeeds, then we stop caching. */
26823 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
26825 unsigned depth
= 0;
26826 int maybe_template_id
= 0;
26827 cp_token
*first_token
;
26829 tree default_argument
;
26831 /* Add tokens until we have processed the entire default
26832 argument. We add the range [first_token, token). */
26833 first_token
= cp_lexer_peek_token (parser
->lexer
);
26834 if (first_token
->type
== CPP_OPEN_BRACE
)
26836 /* For list-initialization, this is straightforward. */
26837 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26838 token
= cp_lexer_peek_token (parser
->lexer
);
26844 /* Peek at the next token. */
26845 token
= cp_lexer_peek_token (parser
->lexer
);
26846 /* What we do depends on what token we have. */
26847 switch (token
->type
)
26849 /* In valid code, a default argument must be
26850 immediately followed by a `,' `)', or `...'. */
26852 if (depth
== 0 && maybe_template_id
)
26854 /* If we've seen a '<', we might be in a
26855 template-argument-list. Until Core issue 325 is
26856 resolved, we don't know how this situation ought
26857 to be handled, so try to DTRT. We check whether
26858 what comes after the comma is a valid parameter
26859 declaration list. If it is, then the comma ends
26860 the default argument; otherwise the default
26861 argument continues. */
26862 bool error
= false;
26865 /* Set ITALP so cp_parser_parameter_declaration_list
26866 doesn't decide to commit to this parse. */
26867 bool saved_italp
= parser
->in_template_argument_list_p
;
26868 parser
->in_template_argument_list_p
= true;
26870 cp_parser_parse_tentatively (parser
);
26874 /* Parse declarators until we reach a non-comma or
26875 somthing that cannot be an initializer.
26876 Just checking whether we're looking at a single
26877 declarator is insufficient. Consider:
26878 int var = tuple<T,U>::x;
26879 The template parameter 'U' looks exactly like a
26883 int ctor_dtor_or_conv_p
;
26884 cp_lexer_consume_token (parser
->lexer
);
26885 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26886 &ctor_dtor_or_conv_p
,
26887 /*parenthesized_p=*/NULL
,
26889 /*friend_p=*/false);
26890 peek
= cp_lexer_peek_token (parser
->lexer
);
26891 if (cp_parser_error_occurred (parser
))
26894 while (peek
->type
== CPP_COMMA
);
26895 /* If we met an '=' or ';' then the original comma
26896 was the end of the NSDMI. Otherwise assume
26897 we're still in the NSDMI. */
26898 error
= (peek
->type
!= CPP_EQ
26899 && peek
->type
!= CPP_SEMICOLON
);
26903 cp_lexer_consume_token (parser
->lexer
);
26904 begin_scope (sk_function_parms
, NULL_TREE
);
26905 cp_parser_parameter_declaration_list (parser
, &error
);
26906 pop_bindings_and_leave_scope ();
26908 if (!cp_parser_error_occurred (parser
) && !error
)
26910 cp_parser_abort_tentative_parse (parser
);
26912 parser
->in_template_argument_list_p
= saved_italp
;
26915 case CPP_CLOSE_PAREN
:
26917 /* If we run into a non-nested `;', `}', or `]',
26918 then the code is invalid -- but the default
26919 argument is certainly over. */
26920 case CPP_SEMICOLON
:
26921 case CPP_CLOSE_BRACE
:
26922 case CPP_CLOSE_SQUARE
:
26924 /* Handle correctly int n = sizeof ... ( p ); */
26925 && token
->type
!= CPP_ELLIPSIS
)
26927 /* Update DEPTH, if necessary. */
26928 else if (token
->type
== CPP_CLOSE_PAREN
26929 || token
->type
== CPP_CLOSE_BRACE
26930 || token
->type
== CPP_CLOSE_SQUARE
)
26934 case CPP_OPEN_PAREN
:
26935 case CPP_OPEN_SQUARE
:
26936 case CPP_OPEN_BRACE
:
26942 /* This might be the comparison operator, or it might
26943 start a template argument list. */
26944 ++maybe_template_id
;
26948 if (cxx_dialect
== cxx98
)
26950 /* Fall through for C++0x, which treats the `>>'
26951 operator like two `>' tokens in certain
26957 /* This might be an operator, or it might close a
26958 template argument list. But if a previous '<'
26959 started a template argument list, this will have
26960 closed it, so we can't be in one anymore. */
26961 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
26962 if (maybe_template_id
< 0)
26963 maybe_template_id
= 0;
26967 /* If we run out of tokens, issue an error message. */
26969 case CPP_PRAGMA_EOL
:
26970 error_at (token
->location
, "file ends in default argument");
26976 /* In these cases, we should look for template-ids.
26977 For example, if the default argument is
26978 `X<int, double>()', we need to do name lookup to
26979 figure out whether or not `X' is a template; if
26980 so, the `,' does not end the default argument.
26982 That is not yet done. */
26989 /* If we've reached the end, stop. */
26993 /* Add the token to the token block. */
26994 token
= cp_lexer_consume_token (parser
->lexer
);
26997 /* Create a DEFAULT_ARG to represent the unparsed default
26999 default_argument
= make_node (DEFAULT_ARG
);
27000 DEFARG_TOKENS (default_argument
)
27001 = cp_token_cache_new (first_token
, token
);
27002 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
27004 return default_argument
;
27007 /* Begin parsing tentatively. We always save tokens while parsing
27008 tentatively so that if the tentative parsing fails we can restore the
27012 cp_parser_parse_tentatively (cp_parser
* parser
)
27014 /* Enter a new parsing context. */
27015 parser
->context
= cp_parser_context_new (parser
->context
);
27016 /* Begin saving tokens. */
27017 cp_lexer_save_tokens (parser
->lexer
);
27018 /* In order to avoid repetitive access control error messages,
27019 access checks are queued up until we are no longer parsing
27021 push_deferring_access_checks (dk_deferred
);
27024 /* Commit to the currently active tentative parse. */
27027 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
27029 cp_parser_context
*context
;
27032 /* Mark all of the levels as committed. */
27033 lexer
= parser
->lexer
;
27034 for (context
= parser
->context
; context
->next
; context
= context
->next
)
27036 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27038 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27039 while (!cp_lexer_saving_tokens (lexer
))
27040 lexer
= lexer
->next
;
27041 cp_lexer_commit_tokens (lexer
);
27045 /* Commit to the topmost currently active tentative parse.
27047 Note that this function shouldn't be called when there are
27048 irreversible side-effects while in a tentative state. For
27049 example, we shouldn't create a permanent entry in the symbol
27050 table, or issue an error message that might not apply if the
27051 tentative parse is aborted. */
27054 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
27056 cp_parser_context
*context
= parser
->context
;
27057 cp_lexer
*lexer
= parser
->lexer
;
27061 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27063 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27065 while (!cp_lexer_saving_tokens (lexer
))
27066 lexer
= lexer
->next
;
27067 cp_lexer_commit_tokens (lexer
);
27071 /* Abort the currently active tentative parse. All consumed tokens
27072 will be rolled back, and no diagnostics will be issued. */
27075 cp_parser_abort_tentative_parse (cp_parser
* parser
)
27077 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
27078 || errorcount
> 0);
27079 cp_parser_simulate_error (parser
);
27080 /* Now, pretend that we want to see if the construct was
27081 successfully parsed. */
27082 cp_parser_parse_definitely (parser
);
27085 /* Stop parsing tentatively. If a parse error has occurred, restore the
27086 token stream. Otherwise, commit to the tokens we have consumed.
27087 Returns true if no error occurred; false otherwise. */
27090 cp_parser_parse_definitely (cp_parser
* parser
)
27092 bool error_occurred
;
27093 cp_parser_context
*context
;
27095 /* Remember whether or not an error occurred, since we are about to
27096 destroy that information. */
27097 error_occurred
= cp_parser_error_occurred (parser
);
27098 /* Remove the topmost context from the stack. */
27099 context
= parser
->context
;
27100 parser
->context
= context
->next
;
27101 /* If no parse errors occurred, commit to the tentative parse. */
27102 if (!error_occurred
)
27104 /* Commit to the tokens read tentatively, unless that was
27106 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
27107 cp_lexer_commit_tokens (parser
->lexer
);
27109 pop_to_parent_deferring_access_checks ();
27111 /* Otherwise, if errors occurred, roll back our state so that things
27112 are just as they were before we began the tentative parse. */
27115 cp_lexer_rollback_tokens (parser
->lexer
);
27116 pop_deferring_access_checks ();
27118 /* Add the context to the front of the free list. */
27119 context
->next
= cp_parser_context_free_list
;
27120 cp_parser_context_free_list
= context
;
27122 return !error_occurred
;
27125 /* Returns true if we are parsing tentatively and are not committed to
27126 this tentative parse. */
27129 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
27131 return (cp_parser_parsing_tentatively (parser
)
27132 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
27135 /* Returns nonzero iff an error has occurred during the most recent
27136 tentative parse. */
27139 cp_parser_error_occurred (cp_parser
* parser
)
27141 return (cp_parser_parsing_tentatively (parser
)
27142 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
27145 /* Returns nonzero if GNU extensions are allowed. */
27148 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
27150 return parser
->allow_gnu_extensions_p
;
27153 /* Objective-C++ Productions */
27156 /* Parse an Objective-C expression, which feeds into a primary-expression
27160 objc-message-expression
27161 objc-string-literal
27162 objc-encode-expression
27163 objc-protocol-expression
27164 objc-selector-expression
27166 Returns a tree representation of the expression. */
27169 cp_parser_objc_expression (cp_parser
* parser
)
27171 /* Try to figure out what kind of declaration is present. */
27172 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27176 case CPP_OPEN_SQUARE
:
27177 return cp_parser_objc_message_expression (parser
);
27179 case CPP_OBJC_STRING
:
27180 kwd
= cp_lexer_consume_token (parser
->lexer
);
27181 return objc_build_string_object (kwd
->u
.value
);
27184 switch (kwd
->keyword
)
27186 case RID_AT_ENCODE
:
27187 return cp_parser_objc_encode_expression (parser
);
27189 case RID_AT_PROTOCOL
:
27190 return cp_parser_objc_protocol_expression (parser
);
27192 case RID_AT_SELECTOR
:
27193 return cp_parser_objc_selector_expression (parser
);
27199 error_at (kwd
->location
,
27200 "misplaced %<@%D%> Objective-C++ construct",
27202 cp_parser_skip_to_end_of_block_or_statement (parser
);
27205 return error_mark_node
;
27208 /* Parse an Objective-C message expression.
27210 objc-message-expression:
27211 [ objc-message-receiver objc-message-args ]
27213 Returns a representation of an Objective-C message. */
27216 cp_parser_objc_message_expression (cp_parser
* parser
)
27218 tree receiver
, messageargs
;
27220 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
27221 receiver
= cp_parser_objc_message_receiver (parser
);
27222 messageargs
= cp_parser_objc_message_args (parser
);
27223 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27225 return objc_build_message_expr (receiver
, messageargs
);
27228 /* Parse an objc-message-receiver.
27230 objc-message-receiver:
27232 simple-type-specifier
27234 Returns a representation of the type or expression. */
27237 cp_parser_objc_message_receiver (cp_parser
* parser
)
27241 /* An Objective-C message receiver may be either (1) a type
27242 or (2) an expression. */
27243 cp_parser_parse_tentatively (parser
);
27244 rcv
= cp_parser_expression (parser
);
27246 /* If that worked out, fine. */
27247 if (cp_parser_parse_definitely (parser
))
27250 cp_parser_parse_tentatively (parser
);
27251 rcv
= cp_parser_simple_type_specifier (parser
,
27252 /*decl_specs=*/NULL
,
27253 CP_PARSER_FLAGS_NONE
);
27255 if (cp_parser_parse_definitely (parser
))
27256 return objc_get_class_reference (rcv
);
27258 cp_parser_error (parser
, "objective-c++ message receiver expected");
27259 return error_mark_node
;
27262 /* Parse the arguments and selectors comprising an Objective-C message.
27267 objc-selector-args , objc-comma-args
27269 objc-selector-args:
27270 objc-selector [opt] : assignment-expression
27271 objc-selector-args objc-selector [opt] : assignment-expression
27274 assignment-expression
27275 objc-comma-args , assignment-expression
27277 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27278 selector arguments and TREE_VALUE containing a list of comma
27282 cp_parser_objc_message_args (cp_parser
* parser
)
27284 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
27285 bool maybe_unary_selector_p
= true;
27286 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27288 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
27290 tree selector
= NULL_TREE
, arg
;
27292 if (token
->type
!= CPP_COLON
)
27293 selector
= cp_parser_objc_selector (parser
);
27295 /* Detect if we have a unary selector. */
27296 if (maybe_unary_selector_p
27297 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27298 return build_tree_list (selector
, NULL_TREE
);
27300 maybe_unary_selector_p
= false;
27301 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27302 arg
= cp_parser_assignment_expression (parser
);
27305 = chainon (sel_args
,
27306 build_tree_list (selector
, arg
));
27308 token
= cp_lexer_peek_token (parser
->lexer
);
27311 /* Handle non-selector arguments, if any. */
27312 while (token
->type
== CPP_COMMA
)
27316 cp_lexer_consume_token (parser
->lexer
);
27317 arg
= cp_parser_assignment_expression (parser
);
27320 = chainon (addl_args
,
27321 build_tree_list (NULL_TREE
, arg
));
27323 token
= cp_lexer_peek_token (parser
->lexer
);
27326 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
27328 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
27329 return build_tree_list (error_mark_node
, error_mark_node
);
27332 return build_tree_list (sel_args
, addl_args
);
27335 /* Parse an Objective-C encode expression.
27337 objc-encode-expression:
27338 @encode objc-typename
27340 Returns an encoded representation of the type argument. */
27343 cp_parser_objc_encode_expression (cp_parser
* parser
)
27348 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
27349 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27350 token
= cp_lexer_peek_token (parser
->lexer
);
27351 type
= complete_type (cp_parser_type_id (parser
));
27352 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27356 error_at (token
->location
,
27357 "%<@encode%> must specify a type as an argument");
27358 return error_mark_node
;
27361 /* This happens if we find @encode(T) (where T is a template
27362 typename or something dependent on a template typename) when
27363 parsing a template. In that case, we can't compile it
27364 immediately, but we rather create an AT_ENCODE_EXPR which will
27365 need to be instantiated when the template is used.
27367 if (dependent_type_p (type
))
27369 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
27370 TREE_READONLY (value
) = 1;
27374 return objc_build_encode_expr (type
);
27377 /* Parse an Objective-C @defs expression. */
27380 cp_parser_objc_defs_expression (cp_parser
*parser
)
27384 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
27385 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27386 name
= cp_parser_identifier (parser
);
27387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27389 return objc_get_class_ivars (name
);
27392 /* Parse an Objective-C protocol expression.
27394 objc-protocol-expression:
27395 @protocol ( identifier )
27397 Returns a representation of the protocol expression. */
27400 cp_parser_objc_protocol_expression (cp_parser
* parser
)
27404 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
27405 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27406 proto
= cp_parser_identifier (parser
);
27407 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27409 return objc_build_protocol_expr (proto
);
27412 /* Parse an Objective-C selector expression.
27414 objc-selector-expression:
27415 @selector ( objc-method-signature )
27417 objc-method-signature:
27423 objc-selector-seq objc-selector :
27425 Returns a representation of the method selector. */
27428 cp_parser_objc_selector_expression (cp_parser
* parser
)
27430 tree sel_seq
= NULL_TREE
;
27431 bool maybe_unary_selector_p
= true;
27433 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27435 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
27436 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27437 token
= cp_lexer_peek_token (parser
->lexer
);
27439 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
27440 || token
->type
== CPP_SCOPE
)
27442 tree selector
= NULL_TREE
;
27444 if (token
->type
!= CPP_COLON
27445 || token
->type
== CPP_SCOPE
)
27446 selector
= cp_parser_objc_selector (parser
);
27448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
27449 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
27451 /* Detect if we have a unary selector. */
27452 if (maybe_unary_selector_p
)
27454 sel_seq
= selector
;
27455 goto finish_selector
;
27459 cp_parser_error (parser
, "expected %<:%>");
27462 maybe_unary_selector_p
= false;
27463 token
= cp_lexer_consume_token (parser
->lexer
);
27465 if (token
->type
== CPP_SCOPE
)
27468 = chainon (sel_seq
,
27469 build_tree_list (selector
, NULL_TREE
));
27471 = chainon (sel_seq
,
27472 build_tree_list (NULL_TREE
, NULL_TREE
));
27476 = chainon (sel_seq
,
27477 build_tree_list (selector
, NULL_TREE
));
27479 token
= cp_lexer_peek_token (parser
->lexer
);
27483 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27485 return objc_build_selector_expr (loc
, sel_seq
);
27488 /* Parse a list of identifiers.
27490 objc-identifier-list:
27492 objc-identifier-list , identifier
27494 Returns a TREE_LIST of identifier nodes. */
27497 cp_parser_objc_identifier_list (cp_parser
* parser
)
27503 identifier
= cp_parser_identifier (parser
);
27504 if (identifier
== error_mark_node
)
27505 return error_mark_node
;
27507 list
= build_tree_list (NULL_TREE
, identifier
);
27508 sep
= cp_lexer_peek_token (parser
->lexer
);
27510 while (sep
->type
== CPP_COMMA
)
27512 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27513 identifier
= cp_parser_identifier (parser
);
27514 if (identifier
== error_mark_node
)
27517 list
= chainon (list
, build_tree_list (NULL_TREE
,
27519 sep
= cp_lexer_peek_token (parser
->lexer
);
27525 /* Parse an Objective-C alias declaration.
27527 objc-alias-declaration:
27528 @compatibility_alias identifier identifier ;
27530 This function registers the alias mapping with the Objective-C front end.
27531 It returns nothing. */
27534 cp_parser_objc_alias_declaration (cp_parser
* parser
)
27538 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
27539 alias
= cp_parser_identifier (parser
);
27540 orig
= cp_parser_identifier (parser
);
27541 objc_declare_alias (alias
, orig
);
27542 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27545 /* Parse an Objective-C class forward-declaration.
27547 objc-class-declaration:
27548 @class objc-identifier-list ;
27550 The function registers the forward declarations with the Objective-C
27551 front end. It returns nothing. */
27554 cp_parser_objc_class_declaration (cp_parser
* parser
)
27556 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
27561 id
= cp_parser_identifier (parser
);
27562 if (id
== error_mark_node
)
27565 objc_declare_class (id
);
27567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27568 cp_lexer_consume_token (parser
->lexer
);
27572 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27575 /* Parse a list of Objective-C protocol references.
27577 objc-protocol-refs-opt:
27578 objc-protocol-refs [opt]
27580 objc-protocol-refs:
27581 < objc-identifier-list >
27583 Returns a TREE_LIST of identifiers, if any. */
27586 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
27588 tree protorefs
= NULL_TREE
;
27590 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
27592 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
27593 protorefs
= cp_parser_objc_identifier_list (parser
);
27594 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
27600 /* Parse a Objective-C visibility specification. */
27603 cp_parser_objc_visibility_spec (cp_parser
* parser
)
27605 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
27607 switch (vis
->keyword
)
27609 case RID_AT_PRIVATE
:
27610 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
27612 case RID_AT_PROTECTED
:
27613 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
27615 case RID_AT_PUBLIC
:
27616 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
27618 case RID_AT_PACKAGE
:
27619 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
27625 /* Eat '@private'/'@protected'/'@public'. */
27626 cp_lexer_consume_token (parser
->lexer
);
27629 /* Parse an Objective-C method type. Return 'true' if it is a class
27630 (+) method, and 'false' if it is an instance (-) method. */
27633 cp_parser_objc_method_type (cp_parser
* parser
)
27635 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
27641 /* Parse an Objective-C protocol qualifier. */
27644 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
27646 tree quals
= NULL_TREE
, node
;
27647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27649 node
= token
->u
.value
;
27651 while (node
&& identifier_p (node
)
27652 && (node
== ridpointers
[(int) RID_IN
]
27653 || node
== ridpointers
[(int) RID_OUT
]
27654 || node
== ridpointers
[(int) RID_INOUT
]
27655 || node
== ridpointers
[(int) RID_BYCOPY
]
27656 || node
== ridpointers
[(int) RID_BYREF
]
27657 || node
== ridpointers
[(int) RID_ONEWAY
]))
27659 quals
= tree_cons (NULL_TREE
, node
, quals
);
27660 cp_lexer_consume_token (parser
->lexer
);
27661 token
= cp_lexer_peek_token (parser
->lexer
);
27662 node
= token
->u
.value
;
27668 /* Parse an Objective-C typename. */
27671 cp_parser_objc_typename (cp_parser
* parser
)
27673 tree type_name
= NULL_TREE
;
27675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27677 tree proto_quals
, cp_type
= NULL_TREE
;
27679 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
27680 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
27682 /* An ObjC type name may consist of just protocol qualifiers, in which
27683 case the type shall default to 'id'. */
27684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27686 cp_type
= cp_parser_type_id (parser
);
27688 /* If the type could not be parsed, an error has already
27689 been produced. For error recovery, behave as if it had
27690 not been specified, which will use the default type
27692 if (cp_type
== error_mark_node
)
27694 cp_type
= NULL_TREE
;
27695 /* We need to skip to the closing parenthesis as
27696 cp_parser_type_id() does not seem to do it for
27698 cp_parser_skip_to_closing_parenthesis (parser
,
27699 /*recovering=*/true,
27700 /*or_comma=*/false,
27701 /*consume_paren=*/false);
27705 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27706 type_name
= build_tree_list (proto_quals
, cp_type
);
27712 /* Check to see if TYPE refers to an Objective-C selector name. */
27715 cp_parser_objc_selector_p (enum cpp_ttype type
)
27717 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
27718 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
27719 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
27720 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
27721 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
27724 /* Parse an Objective-C selector. */
27727 cp_parser_objc_selector (cp_parser
* parser
)
27729 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27731 if (!cp_parser_objc_selector_p (token
->type
))
27733 error_at (token
->location
, "invalid Objective-C++ selector name");
27734 return error_mark_node
;
27737 /* C++ operator names are allowed to appear in ObjC selectors. */
27738 switch (token
->type
)
27740 case CPP_AND_AND
: return get_identifier ("and");
27741 case CPP_AND_EQ
: return get_identifier ("and_eq");
27742 case CPP_AND
: return get_identifier ("bitand");
27743 case CPP_OR
: return get_identifier ("bitor");
27744 case CPP_COMPL
: return get_identifier ("compl");
27745 case CPP_NOT
: return get_identifier ("not");
27746 case CPP_NOT_EQ
: return get_identifier ("not_eq");
27747 case CPP_OR_OR
: return get_identifier ("or");
27748 case CPP_OR_EQ
: return get_identifier ("or_eq");
27749 case CPP_XOR
: return get_identifier ("xor");
27750 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
27751 default: return token
->u
.value
;
27755 /* Parse an Objective-C params list. */
27758 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
27760 tree params
= NULL_TREE
;
27761 bool maybe_unary_selector_p
= true;
27762 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27764 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
27766 tree selector
= NULL_TREE
, type_name
, identifier
;
27767 tree parm_attr
= NULL_TREE
;
27769 if (token
->keyword
== RID_ATTRIBUTE
)
27772 if (token
->type
!= CPP_COLON
)
27773 selector
= cp_parser_objc_selector (parser
);
27775 /* Detect if we have a unary selector. */
27776 if (maybe_unary_selector_p
27777 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27779 params
= selector
; /* Might be followed by attributes. */
27783 maybe_unary_selector_p
= false;
27784 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27786 /* Something went quite wrong. There should be a colon
27787 here, but there is not. Stop parsing parameters. */
27790 type_name
= cp_parser_objc_typename (parser
);
27791 /* New ObjC allows attributes on parameters too. */
27792 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
27793 parm_attr
= cp_parser_attributes_opt (parser
);
27794 identifier
= cp_parser_identifier (parser
);
27798 objc_build_keyword_decl (selector
,
27803 token
= cp_lexer_peek_token (parser
->lexer
);
27806 if (params
== NULL_TREE
)
27808 cp_parser_error (parser
, "objective-c++ method declaration is expected");
27809 return error_mark_node
;
27812 /* We allow tail attributes for the method. */
27813 if (token
->keyword
== RID_ATTRIBUTE
)
27815 *attributes
= cp_parser_attributes_opt (parser
);
27816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27817 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27819 cp_parser_error (parser
,
27820 "method attributes must be specified at the end");
27821 return error_mark_node
;
27824 if (params
== NULL_TREE
)
27826 cp_parser_error (parser
, "objective-c++ method declaration is expected");
27827 return error_mark_node
;
27832 /* Parse the non-keyword Objective-C params. */
27835 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
27838 tree params
= make_node (TREE_LIST
);
27839 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27840 *ellipsisp
= false; /* Initially, assume no ellipsis. */
27842 while (token
->type
== CPP_COMMA
)
27844 cp_parameter_declarator
*parmdecl
;
27847 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27848 token
= cp_lexer_peek_token (parser
->lexer
);
27850 if (token
->type
== CPP_ELLIPSIS
)
27852 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
27854 token
= cp_lexer_peek_token (parser
->lexer
);
27858 /* TODO: parse attributes for tail parameters. */
27859 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
27860 parm
= grokdeclarator (parmdecl
->declarator
,
27861 &parmdecl
->decl_specifiers
,
27862 PARM
, /*initialized=*/0,
27863 /*attrlist=*/NULL
);
27865 chainon (params
, build_tree_list (NULL_TREE
, parm
));
27866 token
= cp_lexer_peek_token (parser
->lexer
);
27869 /* We allow tail attributes for the method. */
27870 if (token
->keyword
== RID_ATTRIBUTE
)
27872 if (*attributes
== NULL_TREE
)
27874 *attributes
= cp_parser_attributes_opt (parser
);
27875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27876 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27880 /* We have an error, but parse the attributes, so that we can
27882 *attributes
= cp_parser_attributes_opt (parser
);
27884 cp_parser_error (parser
,
27885 "method attributes must be specified at the end");
27886 return error_mark_node
;
27892 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27895 cp_parser_objc_interstitial_code (cp_parser
* parser
)
27897 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27899 /* If the next token is `extern' and the following token is a string
27900 literal, then we have a linkage specification. */
27901 if (token
->keyword
== RID_EXTERN
27902 && cp_parser_is_pure_string_literal
27903 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
27904 cp_parser_linkage_specification (parser
);
27905 /* Handle #pragma, if any. */
27906 else if (token
->type
== CPP_PRAGMA
)
27907 cp_parser_pragma (parser
, pragma_objc_icode
);
27908 /* Allow stray semicolons. */
27909 else if (token
->type
== CPP_SEMICOLON
)
27910 cp_lexer_consume_token (parser
->lexer
);
27911 /* Mark methods as optional or required, when building protocols. */
27912 else if (token
->keyword
== RID_AT_OPTIONAL
)
27914 cp_lexer_consume_token (parser
->lexer
);
27915 objc_set_method_opt (true);
27917 else if (token
->keyword
== RID_AT_REQUIRED
)
27919 cp_lexer_consume_token (parser
->lexer
);
27920 objc_set_method_opt (false);
27922 else if (token
->keyword
== RID_NAMESPACE
)
27923 cp_parser_namespace_definition (parser
);
27924 /* Other stray characters must generate errors. */
27925 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
27927 cp_lexer_consume_token (parser
->lexer
);
27928 error ("stray %qs between Objective-C++ methods",
27929 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
27931 /* Finally, try to parse a block-declaration, or a function-definition. */
27933 cp_parser_block_declaration (parser
, /*statement_p=*/false);
27936 /* Parse a method signature. */
27939 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
27941 tree rettype
, kwdparms
, optparms
;
27942 bool ellipsis
= false;
27943 bool is_class_method
;
27945 is_class_method
= cp_parser_objc_method_type (parser
);
27946 rettype
= cp_parser_objc_typename (parser
);
27947 *attributes
= NULL_TREE
;
27948 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
27949 if (kwdparms
== error_mark_node
)
27950 return error_mark_node
;
27951 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
27952 if (optparms
== error_mark_node
)
27953 return error_mark_node
;
27955 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
27959 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
27962 cp_lexer_save_tokens (parser
->lexer
);
27963 tattr
= cp_parser_attributes_opt (parser
);
27964 gcc_assert (tattr
) ;
27966 /* If the attributes are followed by a method introducer, this is not allowed.
27967 Dump the attributes and flag the situation. */
27968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
27969 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
27972 /* Otherwise, the attributes introduce some interstitial code, possibly so
27973 rewind to allow that check. */
27974 cp_lexer_rollback_tokens (parser
->lexer
);
27978 /* Parse an Objective-C method prototype list. */
27981 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
27983 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27985 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
27987 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
27989 tree attributes
, sig
;
27990 bool is_class_method
;
27991 if (token
->type
== CPP_PLUS
)
27992 is_class_method
= true;
27994 is_class_method
= false;
27995 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
27996 if (sig
== error_mark_node
)
27998 cp_parser_skip_to_end_of_block_or_statement (parser
);
27999 token
= cp_lexer_peek_token (parser
->lexer
);
28002 objc_add_method_declaration (is_class_method
, sig
, attributes
);
28003 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28005 else if (token
->keyword
== RID_AT_PROPERTY
)
28006 cp_parser_objc_at_property_declaration (parser
);
28007 else if (token
->keyword
== RID_ATTRIBUTE
28008 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
28009 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28011 "prefix attributes are ignored for methods");
28013 /* Allow for interspersed non-ObjC++ code. */
28014 cp_parser_objc_interstitial_code (parser
);
28016 token
= cp_lexer_peek_token (parser
->lexer
);
28019 if (token
->type
!= CPP_EOF
)
28020 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28022 cp_parser_error (parser
, "expected %<@end%>");
28024 objc_finish_interface ();
28027 /* Parse an Objective-C method definition list. */
28030 cp_parser_objc_method_definition_list (cp_parser
* parser
)
28032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28034 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28038 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
28041 tree sig
, attribute
;
28042 bool is_class_method
;
28043 if (token
->type
== CPP_PLUS
)
28044 is_class_method
= true;
28046 is_class_method
= false;
28047 push_deferring_access_checks (dk_deferred
);
28048 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
28049 if (sig
== error_mark_node
)
28051 cp_parser_skip_to_end_of_block_or_statement (parser
);
28052 token
= cp_lexer_peek_token (parser
->lexer
);
28055 objc_start_method_definition (is_class_method
, sig
, attribute
,
28058 /* For historical reasons, we accept an optional semicolon. */
28059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28060 cp_lexer_consume_token (parser
->lexer
);
28062 ptk
= cp_lexer_peek_token (parser
->lexer
);
28063 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
28064 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
28066 perform_deferred_access_checks (tf_warning_or_error
);
28067 stop_deferring_access_checks ();
28068 meth
= cp_parser_function_definition_after_declarator (parser
,
28070 pop_deferring_access_checks ();
28071 objc_finish_method_definition (meth
);
28074 /* The following case will be removed once @synthesize is
28075 completely implemented. */
28076 else if (token
->keyword
== RID_AT_PROPERTY
)
28077 cp_parser_objc_at_property_declaration (parser
);
28078 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
28079 cp_parser_objc_at_synthesize_declaration (parser
);
28080 else if (token
->keyword
== RID_AT_DYNAMIC
)
28081 cp_parser_objc_at_dynamic_declaration (parser
);
28082 else if (token
->keyword
== RID_ATTRIBUTE
28083 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
28084 warning_at (token
->location
, OPT_Wattributes
,
28085 "prefix attributes are ignored for methods");
28087 /* Allow for interspersed non-ObjC++ code. */
28088 cp_parser_objc_interstitial_code (parser
);
28090 token
= cp_lexer_peek_token (parser
->lexer
);
28093 if (token
->type
!= CPP_EOF
)
28094 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28096 cp_parser_error (parser
, "expected %<@end%>");
28098 objc_finish_implementation ();
28101 /* Parse Objective-C ivars. */
28104 cp_parser_objc_class_ivars (cp_parser
* parser
)
28106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28108 if (token
->type
!= CPP_OPEN_BRACE
)
28109 return; /* No ivars specified. */
28111 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
28112 token
= cp_lexer_peek_token (parser
->lexer
);
28114 while (token
->type
!= CPP_CLOSE_BRACE
28115 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28117 cp_decl_specifier_seq declspecs
;
28118 int decl_class_or_enum_p
;
28119 tree prefix_attributes
;
28121 cp_parser_objc_visibility_spec (parser
);
28123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28126 cp_parser_decl_specifier_seq (parser
,
28127 CP_PARSER_FLAGS_OPTIONAL
,
28129 &decl_class_or_enum_p
);
28131 /* auto, register, static, extern, mutable. */
28132 if (declspecs
.storage_class
!= sc_none
)
28134 cp_parser_error (parser
, "invalid type for instance variable");
28135 declspecs
.storage_class
= sc_none
;
28138 /* thread_local. */
28139 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
28141 cp_parser_error (parser
, "invalid type for instance variable");
28142 declspecs
.locations
[ds_thread
] = 0;
28146 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
28148 cp_parser_error (parser
, "invalid type for instance variable");
28149 declspecs
.locations
[ds_typedef
] = 0;
28152 prefix_attributes
= declspecs
.attributes
;
28153 declspecs
.attributes
= NULL_TREE
;
28155 /* Keep going until we hit the `;' at the end of the
28157 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28159 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
28160 cp_declarator
*declarator
= NULL
;
28161 int ctor_dtor_or_conv_p
;
28163 /* Check for a (possibly unnamed) bitfield declaration. */
28164 token
= cp_lexer_peek_token (parser
->lexer
);
28165 if (token
->type
== CPP_COLON
)
28168 if (token
->type
== CPP_NAME
28169 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
28172 /* Get the name of the bitfield. */
28173 declarator
= make_id_declarator (NULL_TREE
,
28174 cp_parser_identifier (parser
),
28178 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28179 /* Get the width of the bitfield. */
28181 = cp_parser_constant_expression (parser
);
28185 /* Parse the declarator. */
28187 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28188 &ctor_dtor_or_conv_p
,
28189 /*parenthesized_p=*/NULL
,
28190 /*member_p=*/false,
28191 /*friend_p=*/false);
28194 /* Look for attributes that apply to the ivar. */
28195 attributes
= cp_parser_attributes_opt (parser
);
28196 /* Remember which attributes are prefix attributes and
28198 first_attribute
= attributes
;
28199 /* Combine the attributes. */
28200 attributes
= chainon (prefix_attributes
, attributes
);
28203 /* Create the bitfield declaration. */
28204 decl
= grokbitfield (declarator
, &declspecs
,
28208 decl
= grokfield (declarator
, &declspecs
,
28209 NULL_TREE
, /*init_const_expr_p=*/false,
28210 NULL_TREE
, attributes
);
28212 /* Add the instance variable. */
28213 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
28214 objc_add_instance_variable (decl
);
28216 /* Reset PREFIX_ATTRIBUTES. */
28217 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
28218 attributes
= TREE_CHAIN (attributes
);
28220 TREE_CHAIN (attributes
) = NULL_TREE
;
28222 token
= cp_lexer_peek_token (parser
->lexer
);
28224 if (token
->type
== CPP_COMMA
)
28226 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28232 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28233 token
= cp_lexer_peek_token (parser
->lexer
);
28236 if (token
->keyword
== RID_AT_END
)
28237 cp_parser_error (parser
, "expected %<}%>");
28239 /* Do not consume the RID_AT_END, so it will be read again as terminating
28240 the @interface of @implementation. */
28241 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28242 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
28244 /* For historical reasons, we accept an optional semicolon. */
28245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28246 cp_lexer_consume_token (parser
->lexer
);
28249 /* Parse an Objective-C protocol declaration. */
28252 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
28254 tree proto
, protorefs
;
28257 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28258 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
28260 tok
= cp_lexer_peek_token (parser
->lexer
);
28261 error_at (tok
->location
, "identifier expected after %<@protocol%>");
28262 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28266 /* See if we have a forward declaration or a definition. */
28267 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28269 /* Try a forward declaration first. */
28270 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
28276 id
= cp_parser_identifier (parser
);
28277 if (id
== error_mark_node
)
28280 objc_declare_protocol (id
, attributes
);
28282 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28283 cp_lexer_consume_token (parser
->lexer
);
28287 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28290 /* Ok, we got a full-fledged definition (or at least should). */
28293 proto
= cp_parser_identifier (parser
);
28294 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
28295 objc_start_protocol (proto
, protorefs
, attributes
);
28296 cp_parser_objc_method_prototype_list (parser
);
28300 /* Parse an Objective-C superclass or category. */
28303 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
28306 tree
*categ
, bool *is_class_extension
)
28308 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
28310 *super
= *categ
= NULL_TREE
;
28311 *is_class_extension
= false;
28312 if (next
->type
== CPP_COLON
)
28314 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28315 *super
= cp_parser_identifier (parser
);
28317 else if (next
->type
== CPP_OPEN_PAREN
)
28319 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
28321 /* If there is no category name, and this is an @interface, we
28322 have a class extension. */
28323 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28325 *categ
= NULL_TREE
;
28326 *is_class_extension
= true;
28329 *categ
= cp_parser_identifier (parser
);
28331 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28335 /* Parse an Objective-C class interface. */
28338 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
28340 tree name
, super
, categ
, protos
;
28341 bool is_class_extension
;
28343 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
28344 name
= cp_parser_identifier (parser
);
28345 if (name
== error_mark_node
)
28347 /* It's hard to recover because even if valid @interface stuff
28348 is to follow, we can't compile it (or validate it) if we
28349 don't even know which class it refers to. Let's assume this
28350 was a stray '@interface' token in the stream and skip it.
28354 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
28355 &is_class_extension
);
28356 protos
= cp_parser_objc_protocol_refs_opt (parser
);
28358 /* We have either a class or a category on our hands. */
28359 if (categ
|| is_class_extension
)
28360 objc_start_category_interface (name
, categ
, protos
, attributes
);
28363 objc_start_class_interface (name
, super
, protos
, attributes
);
28364 /* Handle instance variable declarations, if any. */
28365 cp_parser_objc_class_ivars (parser
);
28366 objc_continue_interface ();
28369 cp_parser_objc_method_prototype_list (parser
);
28372 /* Parse an Objective-C class implementation. */
28375 cp_parser_objc_class_implementation (cp_parser
* parser
)
28377 tree name
, super
, categ
;
28378 bool is_class_extension
;
28380 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
28381 name
= cp_parser_identifier (parser
);
28382 if (name
== error_mark_node
)
28384 /* It's hard to recover because even if valid @implementation
28385 stuff is to follow, we can't compile it (or validate it) if
28386 we don't even know which class it refers to. Let's assume
28387 this was a stray '@implementation' token in the stream and
28392 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
28393 &is_class_extension
);
28395 /* We have either a class or a category on our hands. */
28397 objc_start_category_implementation (name
, categ
);
28400 objc_start_class_implementation (name
, super
);
28401 /* Handle instance variable declarations, if any. */
28402 cp_parser_objc_class_ivars (parser
);
28403 objc_continue_implementation ();
28406 cp_parser_objc_method_definition_list (parser
);
28409 /* Consume the @end token and finish off the implementation. */
28412 cp_parser_objc_end_implementation (cp_parser
* parser
)
28414 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28415 objc_finish_implementation ();
28418 /* Parse an Objective-C declaration. */
28421 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
28423 /* Try to figure out what kind of declaration is present. */
28424 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28427 switch (kwd
->keyword
)
28432 error_at (kwd
->location
, "attributes may not be specified before"
28433 " the %<@%D%> Objective-C++ keyword",
28437 case RID_AT_IMPLEMENTATION
:
28438 warning_at (kwd
->location
, OPT_Wattributes
,
28439 "prefix attributes are ignored before %<@%D%>",
28446 switch (kwd
->keyword
)
28449 cp_parser_objc_alias_declaration (parser
);
28452 cp_parser_objc_class_declaration (parser
);
28454 case RID_AT_PROTOCOL
:
28455 cp_parser_objc_protocol_declaration (parser
, attributes
);
28457 case RID_AT_INTERFACE
:
28458 cp_parser_objc_class_interface (parser
, attributes
);
28460 case RID_AT_IMPLEMENTATION
:
28461 cp_parser_objc_class_implementation (parser
);
28464 cp_parser_objc_end_implementation (parser
);
28467 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
28469 cp_parser_skip_to_end_of_block_or_statement (parser
);
28473 /* Parse an Objective-C try-catch-finally statement.
28475 objc-try-catch-finally-stmt:
28476 @try compound-statement objc-catch-clause-seq [opt]
28477 objc-finally-clause [opt]
28479 objc-catch-clause-seq:
28480 objc-catch-clause objc-catch-clause-seq [opt]
28483 @catch ( objc-exception-declaration ) compound-statement
28485 objc-finally-clause:
28486 @finally compound-statement
28488 objc-exception-declaration:
28489 parameter-declaration
28492 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28496 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28497 for C. Keep them in sync. */
28500 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
28502 location_t location
;
28505 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
28506 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28507 objc_maybe_warn_exceptions (location
);
28508 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28509 node, lest it get absorbed into the surrounding block. */
28510 stmt
= push_stmt_list ();
28511 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28512 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
28514 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
28516 cp_parameter_declarator
*parm
;
28517 tree parameter_declaration
= error_mark_node
;
28518 bool seen_open_paren
= false;
28520 cp_lexer_consume_token (parser
->lexer
);
28521 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28522 seen_open_paren
= true;
28523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28525 /* We have "@catch (...)" (where the '...' are literally
28526 what is in the code). Skip the '...'.
28527 parameter_declaration is set to NULL_TREE, and
28528 objc_being_catch_clauses() knows that that means
28530 cp_lexer_consume_token (parser
->lexer
);
28531 parameter_declaration
= NULL_TREE
;
28535 /* We have "@catch (NSException *exception)" or something
28536 like that. Parse the parameter declaration. */
28537 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
28539 parameter_declaration
= error_mark_node
;
28541 parameter_declaration
= grokdeclarator (parm
->declarator
,
28542 &parm
->decl_specifiers
,
28543 PARM
, /*initialized=*/0,
28544 /*attrlist=*/NULL
);
28546 if (seen_open_paren
)
28547 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28550 /* If there was no open parenthesis, we are recovering from
28551 an error, and we are trying to figure out what mistake
28552 the user has made. */
28554 /* If there is an immediate closing parenthesis, the user
28555 probably forgot the opening one (ie, they typed "@catch
28556 NSException *e)". Parse the closing parenthesis and keep
28558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28559 cp_lexer_consume_token (parser
->lexer
);
28561 /* If these is no immediate closing parenthesis, the user
28562 probably doesn't know that parenthesis are required at
28563 all (ie, they typed "@catch NSException *e"). So, just
28564 forget about the closing parenthesis and keep going. */
28566 objc_begin_catch_clause (parameter_declaration
);
28567 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28568 objc_finish_catch_clause ();
28570 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
28572 cp_lexer_consume_token (parser
->lexer
);
28573 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28574 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28575 node, lest it get absorbed into the surrounding block. */
28576 stmt
= push_stmt_list ();
28577 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28578 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
28581 return objc_finish_try_stmt ();
28584 /* Parse an Objective-C synchronized statement.
28586 objc-synchronized-stmt:
28587 @synchronized ( expression ) compound-statement
28589 Returns NULL_TREE. */
28592 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
28594 location_t location
;
28597 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
28599 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28600 objc_maybe_warn_exceptions (location
);
28601 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28602 lock
= cp_parser_expression (parser
);
28603 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28605 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28606 node, lest it get absorbed into the surrounding block. */
28607 stmt
= push_stmt_list ();
28608 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28610 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
28613 /* Parse an Objective-C throw statement.
28616 @throw assignment-expression [opt] ;
28618 Returns a constructed '@throw' statement. */
28621 cp_parser_objc_throw_statement (cp_parser
*parser
)
28623 tree expr
= NULL_TREE
;
28624 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28626 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
28628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28629 expr
= cp_parser_expression (parser
);
28631 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28633 return objc_build_throw_stmt (loc
, expr
);
28636 /* Parse an Objective-C statement. */
28639 cp_parser_objc_statement (cp_parser
* parser
)
28641 /* Try to figure out what kind of declaration is present. */
28642 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28644 switch (kwd
->keyword
)
28647 return cp_parser_objc_try_catch_finally_statement (parser
);
28648 case RID_AT_SYNCHRONIZED
:
28649 return cp_parser_objc_synchronized_statement (parser
);
28651 return cp_parser_objc_throw_statement (parser
);
28653 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
28655 cp_parser_skip_to_end_of_block_or_statement (parser
);
28658 return error_mark_node
;
28661 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28662 look ahead to see if an objc keyword follows the attributes. This
28663 is to detect the use of prefix attributes on ObjC @interface and
28667 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
28669 cp_lexer_save_tokens (parser
->lexer
);
28670 *attrib
= cp_parser_attributes_opt (parser
);
28671 gcc_assert (*attrib
);
28672 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
28674 cp_lexer_commit_tokens (parser
->lexer
);
28677 cp_lexer_rollback_tokens (parser
->lexer
);
28681 /* This routine is a minimal replacement for
28682 c_parser_struct_declaration () used when parsing the list of
28683 types/names or ObjC++ properties. For example, when parsing the
28686 @property (readonly) int a, b, c;
28688 this function is responsible for parsing "int a, int b, int c" and
28689 returning the declarations as CHAIN of DECLs.
28691 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28692 similar parsing. */
28694 cp_parser_objc_struct_declaration (cp_parser
*parser
)
28696 tree decls
= NULL_TREE
;
28697 cp_decl_specifier_seq declspecs
;
28698 int decl_class_or_enum_p
;
28699 tree prefix_attributes
;
28701 cp_parser_decl_specifier_seq (parser
,
28702 CP_PARSER_FLAGS_NONE
,
28704 &decl_class_or_enum_p
);
28706 if (declspecs
.type
== error_mark_node
)
28707 return error_mark_node
;
28709 /* auto, register, static, extern, mutable. */
28710 if (declspecs
.storage_class
!= sc_none
)
28712 cp_parser_error (parser
, "invalid type for property");
28713 declspecs
.storage_class
= sc_none
;
28716 /* thread_local. */
28717 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
28719 cp_parser_error (parser
, "invalid type for property");
28720 declspecs
.locations
[ds_thread
] = 0;
28724 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
28726 cp_parser_error (parser
, "invalid type for property");
28727 declspecs
.locations
[ds_typedef
] = 0;
28730 prefix_attributes
= declspecs
.attributes
;
28731 declspecs
.attributes
= NULL_TREE
;
28733 /* Keep going until we hit the `;' at the end of the declaration. */
28734 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28736 tree attributes
, first_attribute
, decl
;
28737 cp_declarator
*declarator
;
28740 /* Parse the declarator. */
28741 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28742 NULL
, NULL
, false, false);
28744 /* Look for attributes that apply to the ivar. */
28745 attributes
= cp_parser_attributes_opt (parser
);
28746 /* Remember which attributes are prefix attributes and
28748 first_attribute
= attributes
;
28749 /* Combine the attributes. */
28750 attributes
= chainon (prefix_attributes
, attributes
);
28752 decl
= grokfield (declarator
, &declspecs
,
28753 NULL_TREE
, /*init_const_expr_p=*/false,
28754 NULL_TREE
, attributes
);
28756 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
28757 return error_mark_node
;
28759 /* Reset PREFIX_ATTRIBUTES. */
28760 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
28761 attributes
= TREE_CHAIN (attributes
);
28763 TREE_CHAIN (attributes
) = NULL_TREE
;
28765 DECL_CHAIN (decl
) = decls
;
28768 token
= cp_lexer_peek_token (parser
->lexer
);
28769 if (token
->type
== CPP_COMMA
)
28771 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28780 /* Parse an Objective-C @property declaration. The syntax is:
28782 objc-property-declaration:
28783 '@property' objc-property-attributes[opt] struct-declaration ;
28785 objc-property-attributes:
28786 '(' objc-property-attribute-list ')'
28788 objc-property-attribute-list:
28789 objc-property-attribute
28790 objc-property-attribute-list, objc-property-attribute
28792 objc-property-attribute
28793 'getter' = identifier
28794 'setter' = identifier
28803 @property NSString *name;
28804 @property (readonly) id object;
28805 @property (retain, nonatomic, getter=getTheName) id name;
28806 @property int a, b, c;
28808 PS: This function is identical to
28809 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28811 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
28813 /* The following variables hold the attributes of the properties as
28814 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28815 seen. When we see an attribute, we set them to 'true' (if they
28816 are boolean properties) or to the identifier (if they have an
28817 argument, ie, for getter and setter). Note that here we only
28818 parse the list of attributes, check the syntax and accumulate the
28819 attributes that we find. objc_add_property_declaration() will
28820 then process the information. */
28821 bool property_assign
= false;
28822 bool property_copy
= false;
28823 tree property_getter_ident
= NULL_TREE
;
28824 bool property_nonatomic
= false;
28825 bool property_readonly
= false;
28826 bool property_readwrite
= false;
28827 bool property_retain
= false;
28828 tree property_setter_ident
= NULL_TREE
;
28830 /* 'properties' is the list of properties that we read. Usually a
28831 single one, but maybe more (eg, in "@property int a, b, c;" there
28836 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28838 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
28840 /* Parse the optional attribute list... */
28841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28844 cp_lexer_consume_token (parser
->lexer
);
28848 bool syntax_error
= false;
28849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28852 if (token
->type
!= CPP_NAME
)
28854 cp_parser_error (parser
, "expected identifier");
28857 keyword
= C_RID_CODE (token
->u
.value
);
28858 cp_lexer_consume_token (parser
->lexer
);
28861 case RID_ASSIGN
: property_assign
= true; break;
28862 case RID_COPY
: property_copy
= true; break;
28863 case RID_NONATOMIC
: property_nonatomic
= true; break;
28864 case RID_READONLY
: property_readonly
= true; break;
28865 case RID_READWRITE
: property_readwrite
= true; break;
28866 case RID_RETAIN
: property_retain
= true; break;
28870 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
28872 if (keyword
== RID_GETTER
)
28873 cp_parser_error (parser
,
28874 "missing %<=%> (after %<getter%> attribute)");
28876 cp_parser_error (parser
,
28877 "missing %<=%> (after %<setter%> attribute)");
28878 syntax_error
= true;
28881 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
28882 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
28884 cp_parser_error (parser
, "expected identifier");
28885 syntax_error
= true;
28888 if (keyword
== RID_SETTER
)
28890 if (property_setter_ident
!= NULL_TREE
)
28892 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
28893 cp_lexer_consume_token (parser
->lexer
);
28896 property_setter_ident
= cp_parser_objc_selector (parser
);
28897 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28898 cp_parser_error (parser
, "setter name must terminate with %<:%>");
28900 cp_lexer_consume_token (parser
->lexer
);
28904 if (property_getter_ident
!= NULL_TREE
)
28906 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
28907 cp_lexer_consume_token (parser
->lexer
);
28910 property_getter_ident
= cp_parser_objc_selector (parser
);
28914 cp_parser_error (parser
, "unknown property attribute");
28915 syntax_error
= true;
28922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28923 cp_lexer_consume_token (parser
->lexer
);
28928 /* FIXME: "@property (setter, assign);" will generate a spurious
28929 "error: expected ‘)’ before ‘,’ token". This is because
28930 cp_parser_require, unlike the C counterpart, will produce an
28931 error even if we are in error recovery. */
28932 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28934 cp_parser_skip_to_closing_parenthesis (parser
,
28935 /*recovering=*/true,
28936 /*or_comma=*/false,
28937 /*consume_paren=*/true);
28941 /* ... and the property declaration(s). */
28942 properties
= cp_parser_objc_struct_declaration (parser
);
28944 if (properties
== error_mark_node
)
28946 cp_parser_skip_to_end_of_statement (parser
);
28947 /* If the next token is now a `;', consume it. */
28948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28949 cp_lexer_consume_token (parser
->lexer
);
28953 if (properties
== NULL_TREE
)
28954 cp_parser_error (parser
, "expected identifier");
28957 /* Comma-separated properties are chained together in
28958 reverse order; add them one by one. */
28959 properties
= nreverse (properties
);
28961 for (; properties
; properties
= TREE_CHAIN (properties
))
28962 objc_add_property_declaration (loc
, copy_node (properties
),
28963 property_readonly
, property_readwrite
,
28964 property_assign
, property_retain
,
28965 property_copy
, property_nonatomic
,
28966 property_getter_ident
, property_setter_ident
);
28969 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28972 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28974 objc-synthesize-declaration:
28975 @synthesize objc-synthesize-identifier-list ;
28977 objc-synthesize-identifier-list:
28978 objc-synthesize-identifier
28979 objc-synthesize-identifier-list, objc-synthesize-identifier
28981 objc-synthesize-identifier
28983 identifier = identifier
28986 @synthesize MyProperty;
28987 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28989 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28990 for C. Keep them in sync.
28993 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
28995 tree list
= NULL_TREE
;
28997 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28999 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
29002 tree property
, ivar
;
29003 property
= cp_parser_identifier (parser
);
29004 if (property
== error_mark_node
)
29006 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
29011 cp_lexer_consume_token (parser
->lexer
);
29012 ivar
= cp_parser_identifier (parser
);
29013 if (ivar
== error_mark_node
)
29015 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29021 list
= chainon (list
, build_tree_list (ivar
, property
));
29022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29023 cp_lexer_consume_token (parser
->lexer
);
29027 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29028 objc_add_synthesize_declaration (loc
, list
);
29031 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29033 objc-dynamic-declaration:
29034 @dynamic identifier-list ;
29037 @dynamic MyProperty;
29038 @dynamic MyProperty, AnotherProperty;
29040 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29041 for C. Keep them in sync.
29044 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
29046 tree list
= NULL_TREE
;
29048 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29050 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
29054 property
= cp_parser_identifier (parser
);
29055 if (property
== error_mark_node
)
29057 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29060 list
= chainon (list
, build_tree_list (NULL
, property
));
29061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29062 cp_lexer_consume_token (parser
->lexer
);
29066 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29067 objc_add_dynamic_declaration (loc
, list
);
29071 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29073 /* Returns name of the next clause.
29074 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29075 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29076 returned and the token is consumed. */
29078 static pragma_omp_clause
29079 cp_parser_omp_clause_name (cp_parser
*parser
)
29081 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
29083 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
29084 result
= PRAGMA_OACC_CLAUSE_AUTO
;
29085 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
29086 result
= PRAGMA_OMP_CLAUSE_IF
;
29087 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
29088 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
29089 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
29090 result
= PRAGMA_OACC_CLAUSE_DELETE
;
29091 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
29092 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
29093 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29094 result
= PRAGMA_OMP_CLAUSE_FOR
;
29095 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29097 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29098 const char *p
= IDENTIFIER_POINTER (id
);
29103 if (!strcmp ("aligned", p
))
29104 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
29105 else if (!strcmp ("async", p
))
29106 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
29109 if (!strcmp ("collapse", p
))
29110 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
29111 else if (!strcmp ("copy", p
))
29112 result
= PRAGMA_OACC_CLAUSE_COPY
;
29113 else if (!strcmp ("copyin", p
))
29114 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
29115 else if (!strcmp ("copyout", p
))
29116 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
29117 else if (!strcmp ("copyprivate", p
))
29118 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
29119 else if (!strcmp ("create", p
))
29120 result
= PRAGMA_OACC_CLAUSE_CREATE
;
29123 if (!strcmp ("defaultmap", p
))
29124 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
29125 else if (!strcmp ("depend", p
))
29126 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
29127 else if (!strcmp ("device", p
))
29128 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
29129 else if (!strcmp ("deviceptr", p
))
29130 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
29131 else if (!strcmp ("dist_schedule", p
))
29132 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
29135 if (!strcmp ("final", p
))
29136 result
= PRAGMA_OMP_CLAUSE_FINAL
;
29137 else if (!strcmp ("firstprivate", p
))
29138 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
29139 else if (!strcmp ("from", p
))
29140 result
= PRAGMA_OMP_CLAUSE_FROM
;
29143 if (!strcmp ("gang", p
))
29144 result
= PRAGMA_OACC_CLAUSE_GANG
;
29145 else if (!strcmp ("grainsize", p
))
29146 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
29149 if (!strcmp ("hint", p
))
29150 result
= PRAGMA_OMP_CLAUSE_HINT
;
29151 else if (!strcmp ("host", p
))
29152 result
= PRAGMA_OACC_CLAUSE_HOST
;
29155 if (!strcmp ("inbranch", p
))
29156 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
29157 else if (!strcmp ("independent", p
))
29158 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
29159 else if (!strcmp ("is_device_ptr", p
))
29160 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
29163 if (!strcmp ("lastprivate", p
))
29164 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
29165 else if (!strcmp ("linear", p
))
29166 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
29167 else if (!strcmp ("link", p
))
29168 result
= PRAGMA_OMP_CLAUSE_LINK
;
29171 if (!strcmp ("map", p
))
29172 result
= PRAGMA_OMP_CLAUSE_MAP
;
29173 else if (!strcmp ("mergeable", p
))
29174 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
29175 else if (flag_cilkplus
&& !strcmp ("mask", p
))
29176 result
= PRAGMA_CILK_CLAUSE_MASK
;
29179 if (!strcmp ("nogroup", p
))
29180 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
29181 else if (!strcmp ("notinbranch", p
))
29182 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
29183 else if (!strcmp ("nowait", p
))
29184 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
29185 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
29186 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
29187 else if (!strcmp ("num_gangs", p
))
29188 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
29189 else if (!strcmp ("num_tasks", p
))
29190 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
29191 else if (!strcmp ("num_teams", p
))
29192 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
29193 else if (!strcmp ("num_threads", p
))
29194 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
29195 else if (!strcmp ("num_workers", p
))
29196 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
29199 if (!strcmp ("ordered", p
))
29200 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
29203 if (!strcmp ("parallel", p
))
29204 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
29205 else if (!strcmp ("present", p
))
29206 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
29207 else if (!strcmp ("present_or_copy", p
)
29208 || !strcmp ("pcopy", p
))
29209 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
29210 else if (!strcmp ("present_or_copyin", p
)
29211 || !strcmp ("pcopyin", p
))
29212 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
29213 else if (!strcmp ("present_or_copyout", p
)
29214 || !strcmp ("pcopyout", p
))
29215 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
29216 else if (!strcmp ("present_or_create", p
)
29217 || !strcmp ("pcreate", p
))
29218 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
29219 else if (!strcmp ("priority", p
))
29220 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
29221 else if (!strcmp ("proc_bind", p
))
29222 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
29225 if (!strcmp ("reduction", p
))
29226 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
29229 if (!strcmp ("safelen", p
))
29230 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
29231 else if (!strcmp ("schedule", p
))
29232 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
29233 else if (!strcmp ("sections", p
))
29234 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
29235 else if (!strcmp ("self", p
))
29236 result
= PRAGMA_OACC_CLAUSE_SELF
;
29237 else if (!strcmp ("seq", p
))
29238 result
= PRAGMA_OACC_CLAUSE_SEQ
;
29239 else if (!strcmp ("shared", p
))
29240 result
= PRAGMA_OMP_CLAUSE_SHARED
;
29241 else if (!strcmp ("simd", p
))
29242 result
= PRAGMA_OMP_CLAUSE_SIMD
;
29243 else if (!strcmp ("simdlen", p
))
29244 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
29247 if (!strcmp ("taskgroup", p
))
29248 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
29249 else if (!strcmp ("thread_limit", p
))
29250 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
29251 else if (!strcmp ("threads", p
))
29252 result
= PRAGMA_OMP_CLAUSE_THREADS
;
29253 else if (!strcmp ("tile", p
))
29254 result
= PRAGMA_OACC_CLAUSE_TILE
;
29255 else if (!strcmp ("to", p
))
29256 result
= PRAGMA_OMP_CLAUSE_TO
;
29259 if (!strcmp ("uniform", p
))
29260 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
29261 else if (!strcmp ("untied", p
))
29262 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
29263 else if (!strcmp ("use_device_ptr", p
))
29264 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
29267 if (!strcmp ("vector", p
))
29268 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
29269 else if (!strcmp ("vector_length", p
))
29270 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
29271 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
29272 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
29275 if (!strcmp ("wait", p
))
29276 result
= PRAGMA_OACC_CLAUSE_WAIT
;
29277 else if (!strcmp ("worker", p
))
29278 result
= PRAGMA_OACC_CLAUSE_WORKER
;
29283 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
29284 cp_lexer_consume_token (parser
->lexer
);
29289 /* Validate that a clause of the given type does not already exist. */
29292 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
29293 const char *name
, location_t location
)
29297 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29298 if (OMP_CLAUSE_CODE (c
) == code
)
29300 error_at (location
, "too many %qs clauses", name
);
29308 variable-list , identifier
29310 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29311 colon). An opening parenthesis will have been consumed by the caller.
29313 If KIND is nonzero, create the appropriate node and install the decl
29314 in OMP_CLAUSE_DECL and add the node to the head of the list.
29316 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29317 return the list created.
29319 COLON can be NULL if only closing parenthesis should end the list,
29320 or pointer to bool which will receive false if the list is terminated
29321 by closing parenthesis or true if the list is terminated by colon. */
29324 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
29325 tree list
, bool *colon
)
29328 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
29331 parser
->colon_corrects_to_scope_p
= false;
29338 token
= cp_lexer_peek_token (parser
->lexer
);
29340 && current_class_ptr
29341 && cp_parser_is_keyword (token
, RID_THIS
))
29343 decl
= finish_this_expr ();
29344 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
29345 || CONVERT_EXPR_P (decl
))
29346 decl
= TREE_OPERAND (decl
, 0);
29347 cp_lexer_consume_token (parser
->lexer
);
29351 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
29352 /*check_dependency_p=*/true,
29353 /*template_p=*/NULL
,
29354 /*declarator_p=*/false,
29355 /*optional_p=*/false);
29356 if (name
== error_mark_node
)
29359 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
29360 if (decl
== error_mark_node
)
29361 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
29364 if (decl
== error_mark_node
)
29366 else if (kind
!= 0)
29370 case OMP_CLAUSE__CACHE_
:
29371 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
29373 error_at (token
->location
, "expected %<[%>");
29374 decl
= error_mark_node
;
29378 case OMP_CLAUSE_MAP
:
29379 case OMP_CLAUSE_FROM
:
29380 case OMP_CLAUSE_TO
:
29381 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
29384 = cp_lexer_peek_token (parser
->lexer
)->location
;
29385 cp_id_kind idk
= CP_ID_KIND_NONE
;
29386 cp_lexer_consume_token (parser
->lexer
);
29388 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
29393 case OMP_CLAUSE_DEPEND
:
29394 case OMP_CLAUSE_REDUCTION
:
29395 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
29397 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
29399 parser
->colon_corrects_to_scope_p
= false;
29400 cp_lexer_consume_token (parser
->lexer
);
29401 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29402 low_bound
= cp_parser_expression (parser
);
29404 parser
->colon_corrects_to_scope_p
29405 = saved_colon_corrects_to_scope_p
;
29406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
29407 length
= integer_one_node
;
29410 /* Look for `:'. */
29411 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29413 if (!cp_lexer_next_token_is (parser
->lexer
,
29415 length
= cp_parser_expression (parser
);
29417 /* Look for the closing `]'. */
29418 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
29422 if (kind
== OMP_CLAUSE__CACHE_
)
29424 if (TREE_CODE (low_bound
) != INTEGER_CST
29425 && !TREE_READONLY (low_bound
))
29427 error_at (token
->location
,
29428 "%qD is not a constant", low_bound
);
29429 decl
= error_mark_node
;
29432 if (TREE_CODE (length
) != INTEGER_CST
29433 && !TREE_READONLY (length
))
29435 error_at (token
->location
,
29436 "%qD is not a constant", length
);
29437 decl
= error_mark_node
;
29441 decl
= tree_cons (low_bound
, length
, decl
);
29448 tree u
= build_omp_clause (token
->location
, kind
);
29449 OMP_CLAUSE_DECL (u
) = decl
;
29450 OMP_CLAUSE_CHAIN (u
) = list
;
29454 list
= tree_cons (decl
, NULL_TREE
, list
);
29457 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
29459 cp_lexer_consume_token (parser
->lexer
);
29463 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
29465 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29468 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29472 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29476 /* Try to resync to an unnested comma. Copied from
29477 cp_parser_parenthesized_expression_list. */
29480 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
29481 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
29482 /*recovering=*/true,
29484 /*consume_paren=*/true);
29492 /* Similarly, but expect leading and trailing parenthesis. This is a very
29493 common case for omp clauses. */
29496 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
29498 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29499 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
29504 copy ( variable-list )
29505 copyin ( variable-list )
29506 copyout ( variable-list )
29507 create ( variable-list )
29508 delete ( variable-list )
29509 present ( variable-list )
29510 present_or_copy ( variable-list )
29511 pcopy ( variable-list )
29512 present_or_copyin ( variable-list )
29513 pcopyin ( variable-list )
29514 present_or_copyout ( variable-list )
29515 pcopyout ( variable-list )
29516 present_or_create ( variable-list )
29517 pcreate ( variable-list ) */
29520 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
29523 enum gomp_map_kind kind
;
29526 case PRAGMA_OACC_CLAUSE_COPY
:
29527 kind
= GOMP_MAP_FORCE_TOFROM
;
29529 case PRAGMA_OACC_CLAUSE_COPYIN
:
29530 kind
= GOMP_MAP_FORCE_TO
;
29532 case PRAGMA_OACC_CLAUSE_COPYOUT
:
29533 kind
= GOMP_MAP_FORCE_FROM
;
29535 case PRAGMA_OACC_CLAUSE_CREATE
:
29536 kind
= GOMP_MAP_FORCE_ALLOC
;
29538 case PRAGMA_OACC_CLAUSE_DELETE
:
29539 kind
= GOMP_MAP_FORCE_DEALLOC
;
29541 case PRAGMA_OACC_CLAUSE_DEVICE
:
29542 kind
= GOMP_MAP_FORCE_TO
;
29544 case PRAGMA_OACC_CLAUSE_HOST
:
29545 case PRAGMA_OACC_CLAUSE_SELF
:
29546 kind
= GOMP_MAP_FORCE_FROM
;
29548 case PRAGMA_OACC_CLAUSE_PRESENT
:
29549 kind
= GOMP_MAP_FORCE_PRESENT
;
29551 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
29552 kind
= GOMP_MAP_TOFROM
;
29554 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
29555 kind
= GOMP_MAP_TO
;
29557 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
29558 kind
= GOMP_MAP_FROM
;
29560 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
29561 kind
= GOMP_MAP_ALLOC
;
29564 gcc_unreachable ();
29567 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
29569 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
29570 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
29576 deviceptr ( variable-list ) */
29579 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
29581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29584 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29585 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29586 variable-list must only allow for pointer variables. */
29587 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29588 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
29590 tree v
= TREE_PURPOSE (t
);
29592 /* FIXME diagnostics: Ideally we should keep individual
29593 locations for all the variables in the var list to make the
29594 following errors more precise. Perhaps
29595 c_parser_omp_var_list_parens should construct a list of
29596 locations to go along with the var list. */
29599 error_at (loc
, "%qD is not a variable", v
);
29600 else if (TREE_TYPE (v
) == error_mark_node
)
29602 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
29603 error_at (loc
, "%qD is not a pointer variable", v
);
29605 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
29606 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
29607 OMP_CLAUSE_DECL (u
) = v
;
29608 OMP_CLAUSE_CHAIN (u
) = list
;
29622 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
29623 enum omp_clause_code code
,
29624 tree list
, location_t location
)
29626 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
29627 tree c
= build_omp_clause (location
, code
);
29628 OMP_CLAUSE_CHAIN (c
) = list
;
29633 num_gangs ( expression )
29634 num_workers ( expression )
29635 vector_length ( expression ) */
29638 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
29639 const char *str
, tree list
)
29641 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29643 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29646 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29648 if (t
== error_mark_node
29649 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29651 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29652 /*or_comma=*/false,
29653 /*consume_paren=*/true);
29657 check_no_duplicate_clause (list
, code
, str
, loc
);
29659 tree c
= build_omp_clause (loc
, code
);
29660 OMP_CLAUSE_OPERAND (c
, 0) = t
;
29661 OMP_CLAUSE_CHAIN (c
) = list
;
29667 gang [( gang-arg-list )]
29668 worker [( [num:] int-expr )]
29669 vector [( [length:] int-expr )]
29671 where gang-arg is one of:
29676 and size-expr may be:
29683 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
29684 const char *str
, tree list
)
29686 const char *id
= "num";
29687 cp_lexer
*lexer
= parser
->lexer
;
29688 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
29689 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
29691 if (kind
== OMP_CLAUSE_VECTOR
)
29694 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
29696 cp_lexer_consume_token (lexer
);
29700 cp_token
*next
= cp_lexer_peek_token (lexer
);
29703 /* Gang static argument. */
29704 if (kind
== OMP_CLAUSE_GANG
29705 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
29707 cp_lexer_consume_token (lexer
);
29709 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29710 goto cleanup_error
;
29713 if (ops
[idx
] != NULL
)
29715 cp_parser_error (parser
, "too many %<static%> arguments");
29716 goto cleanup_error
;
29719 /* Check for the '*' argument. */
29720 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
29721 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
29722 || cp_lexer_nth_token_is (parser
->lexer
, 2,
29725 cp_lexer_consume_token (lexer
);
29726 ops
[idx
] = integer_minus_one_node
;
29728 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
29730 cp_lexer_consume_token (lexer
);
29736 /* Worker num: argument and vector length: arguments. */
29737 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
29738 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
29739 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
29741 cp_lexer_consume_token (lexer
); /* id */
29742 cp_lexer_consume_token (lexer
); /* ':' */
29745 /* Now collect the actual argument. */
29746 if (ops
[idx
] != NULL_TREE
)
29748 cp_parser_error (parser
, "unexpected argument");
29749 goto cleanup_error
;
29752 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
29754 if (expr
== error_mark_node
)
29755 goto cleanup_error
;
29757 mark_exp_read (expr
);
29760 if (kind
== OMP_CLAUSE_GANG
29761 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
29763 cp_lexer_consume_token (lexer
);
29770 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29771 goto cleanup_error
;
29774 check_no_duplicate_clause (list
, kind
, str
, loc
);
29776 c
= build_omp_clause (loc
, kind
);
29779 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
29781 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
29782 OMP_CLAUSE_CHAIN (c
) = list
;
29787 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
29792 tile ( size-expr-list ) */
29795 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
29797 tree c
, expr
= error_mark_node
;
29798 tree tile
= NULL_TREE
;
29800 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
29802 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
29808 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
29809 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
29811 cp_lexer_consume_token (parser
->lexer
);
29812 expr
= integer_minus_one_node
;
29815 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29817 if (expr
== error_mark_node
)
29820 tile
= tree_cons (NULL_TREE
, expr
, tile
);
29822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29823 cp_lexer_consume_token (parser
->lexer
);
29825 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
29827 /* Consume the trailing ')'. */
29828 cp_lexer_consume_token (parser
->lexer
);
29830 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
29831 tile
= nreverse (tile
);
29832 OMP_CLAUSE_TILE_LIST (c
) = tile
;
29833 OMP_CLAUSE_CHAIN (c
) = list
;
29838 Parse wait clause or directive parameters. */
29841 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
29843 vec
<tree
, va_gc
> *args
;
29846 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
29848 /*allow_expansion_p=*/true,
29849 /*non_constant_p=*/NULL
);
29851 if (args
== NULL
|| args
->length () == 0)
29853 cp_parser_error (parser
, "expected integer expression before ')'");
29855 release_tree_vector (args
);
29859 args_tree
= build_tree_list_vec (args
);
29861 release_tree_vector (args
);
29863 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
29865 tree targ
= TREE_VALUE (t
);
29867 if (targ
!= error_mark_node
)
29869 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
29870 error ("%<wait%> expression must be integral");
29873 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
29875 mark_rvalue_use (targ
);
29876 OMP_CLAUSE_DECL (c
) = targ
;
29877 OMP_CLAUSE_CHAIN (c
) = list
;
29887 wait ( int-expr-list ) */
29890 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
29892 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29894 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
29897 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
29903 collapse ( constant-expression ) */
29906 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
29912 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29913 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29916 num
= cp_parser_constant_expression (parser
);
29918 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29919 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29920 /*or_comma=*/false,
29921 /*consume_paren=*/true);
29923 if (num
== error_mark_node
)
29925 num
= fold_non_dependent_expr (num
);
29926 if (!tree_fits_shwi_p (num
)
29927 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
29928 || (n
= tree_to_shwi (num
)) <= 0
29931 error_at (loc
, "collapse argument needs positive constant integer expression");
29935 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
29936 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
29937 OMP_CLAUSE_CHAIN (c
) = list
;
29938 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
29944 default ( shared | none )
29950 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
29951 location_t location
, bool is_oacc
)
29953 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
29956 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29960 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29961 const char *p
= IDENTIFIER_POINTER (id
);
29966 if (strcmp ("none", p
) != 0)
29968 kind
= OMP_CLAUSE_DEFAULT_NONE
;
29972 if (strcmp ("shared", p
) != 0 || is_oacc
)
29974 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
29981 cp_lexer_consume_token (parser
->lexer
);
29987 cp_parser_error (parser
, "expected %<none%>");
29989 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
29992 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29993 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29994 /*or_comma=*/false,
29995 /*consume_paren=*/true);
29997 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
30000 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
30001 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
30002 OMP_CLAUSE_CHAIN (c
) = list
;
30003 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
30009 final ( expression ) */
30012 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
30016 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30019 t
= cp_parser_condition (parser
);
30021 if (t
== error_mark_node
30022 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30023 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30024 /*or_comma=*/false,
30025 /*consume_paren=*/true);
30027 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
30029 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
30030 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
30031 OMP_CLAUSE_CHAIN (c
) = list
;
30040 if ( directive-name-modifier : expression )
30042 directive-name-modifier:
30043 parallel | task | taskloop | target data | target | target update
30044 | target enter data | target exit data */
30047 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
30051 enum tree_code if_modifier
= ERROR_MARK
;
30053 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30056 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30058 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30059 const char *p
= IDENTIFIER_POINTER (id
);
30062 if (strcmp ("parallel", p
) == 0)
30063 if_modifier
= OMP_PARALLEL
;
30064 else if (strcmp ("task", p
) == 0)
30065 if_modifier
= OMP_TASK
;
30066 else if (strcmp ("taskloop", p
) == 0)
30067 if_modifier
= OMP_TASKLOOP
;
30068 else if (strcmp ("target", p
) == 0)
30070 if_modifier
= OMP_TARGET
;
30071 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
30073 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
30074 p
= IDENTIFIER_POINTER (id
);
30075 if (strcmp ("data", p
) == 0)
30076 if_modifier
= OMP_TARGET_DATA
;
30077 else if (strcmp ("update", p
) == 0)
30078 if_modifier
= OMP_TARGET_UPDATE
;
30079 else if (strcmp ("enter", p
) == 0)
30080 if_modifier
= OMP_TARGET_ENTER_DATA
;
30081 else if (strcmp ("exit", p
) == 0)
30082 if_modifier
= OMP_TARGET_EXIT_DATA
;
30083 if (if_modifier
!= OMP_TARGET
)
30088 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
30089 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
30091 if_modifier
= ERROR_MARK
;
30093 if (if_modifier
== OMP_TARGET_ENTER_DATA
30094 || if_modifier
== OMP_TARGET_EXIT_DATA
)
30096 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
30098 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
30099 p
= IDENTIFIER_POINTER (id
);
30100 if (strcmp ("data", p
) == 0)
30106 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
30107 error_at (loc
, "expected %<data%>");
30108 if_modifier
= ERROR_MARK
;
30113 if (if_modifier
!= ERROR_MARK
)
30115 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
30118 cp_lexer_consume_token (parser
->lexer
);
30125 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
30126 error_at (loc
, "expected %<:%>");
30128 if_modifier
= ERROR_MARK
;
30133 t
= cp_parser_condition (parser
);
30135 if (t
== error_mark_node
30136 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30137 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30138 /*or_comma=*/false,
30139 /*consume_paren=*/true);
30141 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30142 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
30144 if (if_modifier
!= ERROR_MARK
30145 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30147 const char *p
= NULL
;
30148 switch (if_modifier
)
30150 case OMP_PARALLEL
: p
= "parallel"; break;
30151 case OMP_TASK
: p
= "task"; break;
30152 case OMP_TASKLOOP
: p
= "taskloop"; break;
30153 case OMP_TARGET_DATA
: p
= "target data"; break;
30154 case OMP_TARGET
: p
= "target"; break;
30155 case OMP_TARGET_UPDATE
: p
= "target update"; break;
30156 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
30157 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
30158 default: gcc_unreachable ();
30160 error_at (location
, "too many %<if%> clauses with %qs modifier",
30164 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30167 error_at (location
, "too many %<if%> clauses");
30169 error_at (location
, "too many %<if%> clauses without modifier");
30172 else if (if_modifier
== ERROR_MARK
30173 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
30175 error_at (location
, "if any %<if%> clause has modifier, then all "
30176 "%<if%> clauses have to use modifier");
30181 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
30182 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
30183 OMP_CLAUSE_IF_EXPR (c
) = t
;
30184 OMP_CLAUSE_CHAIN (c
) = list
;
30193 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
30194 tree list
, location_t location
)
30198 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
30201 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
30202 OMP_CLAUSE_CHAIN (c
) = list
;
30210 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
30211 tree list
, location_t location
)
30215 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
30217 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
30218 OMP_CLAUSE_CHAIN (c
) = list
;
30223 num_threads ( expression ) */
30226 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
30227 location_t location
)
30231 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30234 t
= cp_parser_expression (parser
);
30236 if (t
== error_mark_node
30237 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30238 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30239 /*or_comma=*/false,
30240 /*consume_paren=*/true);
30242 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
30243 "num_threads", location
);
30245 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
30246 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
30247 OMP_CLAUSE_CHAIN (c
) = list
;
30253 num_tasks ( expression ) */
30256 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
30257 location_t location
)
30261 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30264 t
= cp_parser_expression (parser
);
30266 if (t
== error_mark_node
30267 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30268 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30269 /*or_comma=*/false,
30270 /*consume_paren=*/true);
30272 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
30273 "num_tasks", location
);
30275 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
30276 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
30277 OMP_CLAUSE_CHAIN (c
) = list
;
30283 grainsize ( expression ) */
30286 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
30287 location_t location
)
30291 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30294 t
= cp_parser_expression (parser
);
30296 if (t
== error_mark_node
30297 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30298 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30299 /*or_comma=*/false,
30300 /*consume_paren=*/true);
30302 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
30303 "grainsize", location
);
30305 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
30306 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
30307 OMP_CLAUSE_CHAIN (c
) = list
;
30313 priority ( expression ) */
30316 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
30317 location_t location
)
30321 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30324 t
= cp_parser_expression (parser
);
30326 if (t
== error_mark_node
30327 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30328 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30329 /*or_comma=*/false,
30330 /*consume_paren=*/true);
30332 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
30333 "priority", location
);
30335 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
30336 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
30337 OMP_CLAUSE_CHAIN (c
) = list
;
30343 hint ( expression ) */
30346 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
30347 location_t location
)
30351 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30354 t
= cp_parser_expression (parser
);
30356 if (t
== error_mark_node
30357 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30358 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30359 /*or_comma=*/false,
30360 /*consume_paren=*/true);
30362 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
30364 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
30365 OMP_CLAUSE_HINT_EXPR (c
) = t
;
30366 OMP_CLAUSE_CHAIN (c
) = list
;
30372 defaultmap ( tofrom : scalar ) */
30375 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
30376 location_t location
)
30381 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30384 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30386 cp_parser_error (parser
, "expected %<tofrom%>");
30389 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30390 p
= IDENTIFIER_POINTER (id
);
30391 if (strcmp (p
, "tofrom") != 0)
30393 cp_parser_error (parser
, "expected %<tofrom%>");
30396 cp_lexer_consume_token (parser
->lexer
);
30397 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30400 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30402 cp_parser_error (parser
, "expected %<scalar%>");
30405 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30406 p
= IDENTIFIER_POINTER (id
);
30407 if (strcmp (p
, "scalar") != 0)
30409 cp_parser_error (parser
, "expected %<scalar%>");
30412 cp_lexer_consume_token (parser
->lexer
);
30413 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30416 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
30419 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
30420 OMP_CLAUSE_CHAIN (c
) = list
;
30424 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30425 /*or_comma=*/false,
30426 /*consume_paren=*/true);
30434 ordered ( constant-expression ) */
30437 cp_parser_omp_clause_ordered (cp_parser
*parser
,
30438 tree list
, location_t location
)
30440 tree c
, num
= NULL_TREE
;
30443 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
30444 "ordered", location
);
30446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30448 cp_lexer_consume_token (parser
->lexer
);
30450 num
= cp_parser_constant_expression (parser
);
30452 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30453 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30454 /*or_comma=*/false,
30455 /*consume_paren=*/true);
30457 if (num
== error_mark_node
)
30459 num
= fold_non_dependent_expr (num
);
30460 if (!tree_fits_shwi_p (num
)
30461 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
30462 || (n
= tree_to_shwi (num
)) <= 0
30465 error_at (location
,
30466 "ordered argument needs positive constant integer "
30472 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
30473 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
30474 OMP_CLAUSE_CHAIN (c
) = list
;
30479 reduction ( reduction-operator : variable-list )
30481 reduction-operator:
30482 One of: + * - & ^ | && ||
30486 reduction-operator:
30487 One of: + * - & ^ | && || min max
30491 reduction-operator:
30492 One of: + * - & ^ | && ||
30496 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
30498 enum tree_code code
= ERROR_MARK
;
30499 tree nlist
, c
, id
= NULL_TREE
;
30501 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30504 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30506 case CPP_PLUS
: code
= PLUS_EXPR
; break;
30507 case CPP_MULT
: code
= MULT_EXPR
; break;
30508 case CPP_MINUS
: code
= MINUS_EXPR
; break;
30509 case CPP_AND
: code
= BIT_AND_EXPR
; break;
30510 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
30511 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
30512 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
30513 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
30517 if (code
!= ERROR_MARK
)
30518 cp_lexer_consume_token (parser
->lexer
);
30521 bool saved_colon_corrects_to_scope_p
;
30522 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30523 parser
->colon_corrects_to_scope_p
= false;
30524 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
30525 /*check_dependency_p=*/true,
30526 /*template_p=*/NULL
,
30527 /*declarator_p=*/false,
30528 /*optional_p=*/false);
30529 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30530 if (identifier_p (id
))
30532 const char *p
= IDENTIFIER_POINTER (id
);
30534 if (strcmp (p
, "min") == 0)
30536 else if (strcmp (p
, "max") == 0)
30538 else if (id
== ansi_opname (PLUS_EXPR
))
30540 else if (id
== ansi_opname (MULT_EXPR
))
30542 else if (id
== ansi_opname (MINUS_EXPR
))
30544 else if (id
== ansi_opname (BIT_AND_EXPR
))
30545 code
= BIT_AND_EXPR
;
30546 else if (id
== ansi_opname (BIT_IOR_EXPR
))
30547 code
= BIT_IOR_EXPR
;
30548 else if (id
== ansi_opname (BIT_XOR_EXPR
))
30549 code
= BIT_XOR_EXPR
;
30550 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
30551 code
= TRUTH_ANDIF_EXPR
;
30552 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
30553 code
= TRUTH_ORIF_EXPR
;
30554 id
= omp_reduction_id (code
, id
, NULL_TREE
);
30555 tree scope
= parser
->scope
;
30557 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
30558 parser
->scope
= NULL_TREE
;
30559 parser
->qualifying_scope
= NULL_TREE
;
30560 parser
->object_scope
= NULL_TREE
;
30564 error ("invalid reduction-identifier");
30566 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30567 /*or_comma=*/false,
30568 /*consume_paren=*/true);
30573 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30576 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
30578 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30580 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
30581 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
30588 schedule ( schedule-kind )
30589 schedule ( schedule-kind , expression )
30592 static | dynamic | guided | runtime | auto
30595 schedule ( schedule-modifier : schedule-kind )
30596 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
30604 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
30607 int modifiers
= 0, nmodifiers
= 0;
30609 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30612 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
30614 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30616 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30617 const char *p
= IDENTIFIER_POINTER (id
);
30618 if (strcmp ("simd", p
) == 0)
30619 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
30620 else if (strcmp ("monotonic", p
) == 0)
30621 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
30622 else if (strcmp ("nonmonotonic", p
) == 0)
30623 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
30626 cp_lexer_consume_token (parser
->lexer
);
30627 if (nmodifiers
++ == 0
30628 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30629 cp_lexer_consume_token (parser
->lexer
);
30632 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30639 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30640 const char *p
= IDENTIFIER_POINTER (id
);
30645 if (strcmp ("dynamic", p
) != 0)
30647 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
30651 if (strcmp ("guided", p
) != 0)
30653 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
30657 if (strcmp ("runtime", p
) != 0)
30659 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
30666 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
30667 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
30668 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30669 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
30672 cp_lexer_consume_token (parser
->lexer
);
30674 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
30675 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
30676 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
30677 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
30679 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
30684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30687 cp_lexer_consume_token (parser
->lexer
);
30689 token
= cp_lexer_peek_token (parser
->lexer
);
30690 t
= cp_parser_assignment_expression (parser
);
30692 if (t
== error_mark_node
)
30694 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
30695 error_at (token
->location
, "schedule %<runtime%> does not take "
30696 "a %<chunk_size%> parameter");
30697 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
30698 error_at (token
->location
, "schedule %<auto%> does not take "
30699 "a %<chunk_size%> parameter");
30701 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
30703 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30706 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
30709 OMP_CLAUSE_SCHEDULE_KIND (c
)
30710 = (enum omp_clause_schedule_kind
)
30711 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
30713 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
30714 OMP_CLAUSE_CHAIN (c
) = list
;
30718 cp_parser_error (parser
, "invalid schedule kind");
30720 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30721 /*or_comma=*/false,
30722 /*consume_paren=*/true);
30730 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
30731 tree list
, location_t location
)
30735 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
30737 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
30738 OMP_CLAUSE_CHAIN (c
) = list
;
30747 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
30748 tree list
, location_t location
)
30750 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
30751 tree c
= build_omp_clause (location
, code
);
30752 OMP_CLAUSE_CHAIN (c
) = list
;
30763 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
30764 enum omp_clause_code code
,
30765 tree list
, location_t location
)
30767 tree c
= build_omp_clause (location
, code
);
30768 OMP_CLAUSE_CHAIN (c
) = list
;
30776 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
30777 tree list
, location_t location
)
30779 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
30780 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
30781 OMP_CLAUSE_CHAIN (c
) = list
;
30790 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
30791 enum omp_clause_code code
,
30792 tree list
, location_t location
)
30794 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
30795 tree c
= build_omp_clause (location
, code
);
30796 OMP_CLAUSE_CHAIN (c
) = list
;
30801 num_teams ( expression ) */
30804 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
30805 location_t location
)
30809 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30812 t
= cp_parser_expression (parser
);
30814 if (t
== error_mark_node
30815 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30816 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30817 /*or_comma=*/false,
30818 /*consume_paren=*/true);
30820 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
30821 "num_teams", location
);
30823 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
30824 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
30825 OMP_CLAUSE_CHAIN (c
) = list
;
30831 thread_limit ( expression ) */
30834 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
30835 location_t location
)
30839 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30842 t
= cp_parser_expression (parser
);
30844 if (t
== error_mark_node
30845 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30846 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30847 /*or_comma=*/false,
30848 /*consume_paren=*/true);
30850 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
30851 "thread_limit", location
);
30853 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
30854 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
30855 OMP_CLAUSE_CHAIN (c
) = list
;
30861 aligned ( variable-list )
30862 aligned ( variable-list : constant-expression ) */
30865 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
30867 tree nlist
, c
, alignment
= NULL_TREE
;
30870 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30873 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
30878 alignment
= cp_parser_constant_expression (parser
);
30880 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30881 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30882 /*or_comma=*/false,
30883 /*consume_paren=*/true);
30885 if (alignment
== error_mark_node
)
30886 alignment
= NULL_TREE
;
30889 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30890 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
30896 linear ( variable-list )
30897 linear ( variable-list : expression )
30900 linear ( modifier ( variable-list ) )
30901 linear ( modifier ( variable-list ) : expression ) */
30904 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
30905 bool is_cilk_simd_fn
, bool declare_simd
)
30907 tree nlist
, c
, step
= integer_one_node
;
30909 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
30911 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30914 if (!is_cilk_simd_fn
30915 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30917 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30918 const char *p
= IDENTIFIER_POINTER (id
);
30920 if (strcmp ("ref", p
) == 0)
30921 kind
= OMP_CLAUSE_LINEAR_REF
;
30922 else if (strcmp ("val", p
) == 0)
30923 kind
= OMP_CLAUSE_LINEAR_VAL
;
30924 else if (strcmp ("uval", p
) == 0)
30925 kind
= OMP_CLAUSE_LINEAR_UVAL
;
30926 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
30927 cp_lexer_consume_token (parser
->lexer
);
30929 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
30932 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
30933 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
30937 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
30938 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
30940 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30941 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30942 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30943 /*or_comma=*/false,
30944 /*consume_paren=*/true);
30951 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
30952 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
30954 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30955 cp_parser_parse_tentatively (parser
);
30956 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
30957 /*check_dependency_p=*/true,
30958 /*template_p=*/NULL
,
30959 /*declarator_p=*/false,
30960 /*optional_p=*/false);
30961 if (step
!= error_mark_node
)
30962 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
30963 if (step
== error_mark_node
)
30966 cp_parser_abort_tentative_parse (parser
);
30968 else if (!cp_parser_parse_definitely (parser
))
30972 step
= cp_parser_expression (parser
);
30974 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
30976 sorry ("using parameters for %<linear%> step is not supported yet");
30977 step
= integer_one_node
;
30979 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30980 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30981 /*or_comma=*/false,
30982 /*consume_paren=*/true);
30984 if (step
== error_mark_node
)
30988 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30990 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
30991 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
30998 safelen ( constant-expression ) */
31001 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
31002 location_t location
)
31006 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31009 t
= cp_parser_constant_expression (parser
);
31011 if (t
== error_mark_node
31012 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31013 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31014 /*or_comma=*/false,
31015 /*consume_paren=*/true);
31017 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
31019 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
31020 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
31021 OMP_CLAUSE_CHAIN (c
) = list
;
31027 simdlen ( constant-expression ) */
31030 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
31031 location_t location
)
31035 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31038 t
= cp_parser_constant_expression (parser
);
31040 if (t
== error_mark_node
31041 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31042 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31043 /*or_comma=*/false,
31044 /*consume_paren=*/true);
31046 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
31048 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
31049 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
31050 OMP_CLAUSE_CHAIN (c
) = list
;
31057 identifier [+/- integer]
31058 vec , identifier [+/- integer]
31062 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
31067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31069 cp_parser_error (parser
, "expected identifier");
31073 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31075 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31076 tree t
, identifier
= cp_parser_identifier (parser
);
31077 tree addend
= NULL
;
31079 if (identifier
== error_mark_node
)
31080 t
= error_mark_node
;
31083 t
= cp_parser_lookup_name_simple
31084 (parser
, identifier
,
31085 cp_lexer_peek_token (parser
->lexer
)->location
);
31086 if (t
== error_mark_node
)
31087 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
31092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31094 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
31096 addend
= integer_zero_node
;
31097 goto add_to_vector
;
31099 cp_lexer_consume_token (parser
->lexer
);
31101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
31103 cp_parser_error (parser
, "expected integer");
31107 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31108 if (TREE_CODE (addend
) != INTEGER_CST
)
31110 cp_parser_error (parser
, "expected integer");
31113 cp_lexer_consume_token (parser
->lexer
);
31116 if (t
!= error_mark_node
)
31118 vec
= tree_cons (addend
, t
, vec
);
31120 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
31123 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31126 cp_lexer_consume_token (parser
->lexer
);
31129 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
31131 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
31132 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
31133 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
31134 OMP_CLAUSE_CHAIN (u
) = list
;
31141 depend ( depend-kind : variable-list )
31149 depend ( sink : vec ) */
31152 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
31155 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
31157 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31162 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31163 const char *p
= IDENTIFIER_POINTER (id
);
31165 if (strcmp ("in", p
) == 0)
31166 kind
= OMP_CLAUSE_DEPEND_IN
;
31167 else if (strcmp ("inout", p
) == 0)
31168 kind
= OMP_CLAUSE_DEPEND_INOUT
;
31169 else if (strcmp ("out", p
) == 0)
31170 kind
= OMP_CLAUSE_DEPEND_OUT
;
31171 else if (strcmp ("source", p
) == 0)
31172 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
31173 else if (strcmp ("sink", p
) == 0)
31174 kind
= OMP_CLAUSE_DEPEND_SINK
;
31181 cp_lexer_consume_token (parser
->lexer
);
31183 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
31185 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
31186 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31187 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
31188 OMP_CLAUSE_CHAIN (c
) = list
;
31189 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31190 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31191 /*or_comma=*/false,
31192 /*consume_paren=*/true);
31196 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31199 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
31200 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
31203 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
31206 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31207 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31212 cp_parser_error (parser
, "invalid depend kind");
31214 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31215 /*or_comma=*/false,
31216 /*consume_paren=*/true);
31221 map ( map-kind : variable-list )
31222 map ( variable-list )
31225 alloc | to | from | tofrom
31229 alloc | to | from | tofrom | release | delete
31231 map ( always [,] map-kind: variable-list ) */
31234 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
31237 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
31238 bool always
= false;
31240 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31245 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31246 const char *p
= IDENTIFIER_POINTER (id
);
31248 if (strcmp ("always", p
) == 0)
31251 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
31253 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
31254 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
31256 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
31260 cp_lexer_consume_token (parser
->lexer
);
31262 cp_lexer_consume_token (parser
->lexer
);
31267 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
31268 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31270 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31271 const char *p
= IDENTIFIER_POINTER (id
);
31273 if (strcmp ("alloc", p
) == 0)
31274 kind
= GOMP_MAP_ALLOC
;
31275 else if (strcmp ("to", p
) == 0)
31276 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
31277 else if (strcmp ("from", p
) == 0)
31278 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
31279 else if (strcmp ("tofrom", p
) == 0)
31280 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
31281 else if (strcmp ("release", p
) == 0)
31282 kind
= GOMP_MAP_RELEASE
;
31285 cp_parser_error (parser
, "invalid map kind");
31286 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31287 /*or_comma=*/false,
31288 /*consume_paren=*/true);
31291 cp_lexer_consume_token (parser
->lexer
);
31292 cp_lexer_consume_token (parser
->lexer
);
31294 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
31295 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31297 kind
= GOMP_MAP_DELETE
;
31298 cp_lexer_consume_token (parser
->lexer
);
31299 cp_lexer_consume_token (parser
->lexer
);
31302 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
31305 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31306 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31312 device ( expression ) */
31315 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
31316 location_t location
)
31320 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31323 t
= cp_parser_expression (parser
);
31325 if (t
== error_mark_node
31326 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31327 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31328 /*or_comma=*/false,
31329 /*consume_paren=*/true);
31331 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
31332 "device", location
);
31334 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
31335 OMP_CLAUSE_DEVICE_ID (c
) = t
;
31336 OMP_CLAUSE_CHAIN (c
) = list
;
31342 dist_schedule ( static )
31343 dist_schedule ( static , expression ) */
31346 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
31347 location_t location
)
31351 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31354 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
31356 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
31358 cp_lexer_consume_token (parser
->lexer
);
31360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31362 cp_lexer_consume_token (parser
->lexer
);
31364 t
= cp_parser_assignment_expression (parser
);
31366 if (t
== error_mark_node
)
31368 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
31370 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31373 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31376 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
31378 OMP_CLAUSE_CHAIN (c
) = list
;
31382 cp_parser_error (parser
, "invalid dist_schedule kind");
31384 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31385 /*or_comma=*/false,
31386 /*consume_paren=*/true);
31391 proc_bind ( proc-bind-kind )
31394 master | close | spread */
31397 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
31398 location_t location
)
31401 enum omp_clause_proc_bind_kind kind
;
31403 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31408 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31409 const char *p
= IDENTIFIER_POINTER (id
);
31411 if (strcmp ("master", p
) == 0)
31412 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
31413 else if (strcmp ("close", p
) == 0)
31414 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
31415 else if (strcmp ("spread", p
) == 0)
31416 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
31423 cp_lexer_consume_token (parser
->lexer
);
31424 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31427 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
31428 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
31430 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
31431 OMP_CLAUSE_CHAIN (c
) = list
;
31435 cp_parser_error (parser
, "invalid depend kind");
31437 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31438 /*or_comma=*/false,
31439 /*consume_paren=*/true);
31444 async [( int-expr )] */
31447 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
31450 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31452 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
31454 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31456 cp_lexer_consume_token (parser
->lexer
);
31458 t
= cp_parser_expression (parser
);
31459 if (t
== error_mark_node
31460 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31461 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31462 /*or_comma=*/false,
31463 /*consume_paren=*/true);
31466 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
31468 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
31469 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
31470 OMP_CLAUSE_CHAIN (c
) = list
;
31476 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31477 is a bitmask in MASK. Return the list of clauses found. */
31480 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
31481 const char *where
, cp_token
*pragma_tok
,
31482 bool finish_p
= true)
31484 tree clauses
= NULL
;
31487 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31490 pragma_omp_clause c_kind
;
31491 omp_clause_code code
;
31492 const char *c_name
;
31493 tree prev
= clauses
;
31495 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31496 cp_lexer_consume_token (parser
->lexer
);
31498 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
31499 c_kind
= cp_parser_omp_clause_name (parser
);
31503 case PRAGMA_OACC_CLAUSE_ASYNC
:
31504 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
31507 case PRAGMA_OACC_CLAUSE_AUTO
:
31508 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
31512 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
31513 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
31514 c_name
= "collapse";
31516 case PRAGMA_OACC_CLAUSE_COPY
:
31517 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31520 case PRAGMA_OACC_CLAUSE_COPYIN
:
31521 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31524 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31525 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31526 c_name
= "copyout";
31528 case PRAGMA_OACC_CLAUSE_CREATE
:
31529 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31532 case PRAGMA_OACC_CLAUSE_DELETE
:
31533 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31536 case PRAGMA_OMP_CLAUSE_DEFAULT
:
31537 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
31538 c_name
= "default";
31540 case PRAGMA_OACC_CLAUSE_DEVICE
:
31541 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31544 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
31545 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
31546 c_name
= "deviceptr";
31548 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
31549 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31551 c_name
= "firstprivate";
31553 case PRAGMA_OACC_CLAUSE_GANG
:
31555 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
31558 case PRAGMA_OACC_CLAUSE_HOST
:
31559 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31562 case PRAGMA_OACC_CLAUSE_IF
:
31563 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
31566 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
31567 clauses
= cp_parser_oacc_simple_clause (parser
,
31568 OMP_CLAUSE_INDEPENDENT
,
31570 c_name
= "independent";
31572 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
31573 code
= OMP_CLAUSE_NUM_GANGS
;
31574 c_name
= "num_gangs";
31575 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31578 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
31579 c_name
= "num_workers";
31580 code
= OMP_CLAUSE_NUM_WORKERS
;
31581 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31584 case PRAGMA_OACC_CLAUSE_PRESENT
:
31585 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31586 c_name
= "present";
31588 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31589 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31590 c_name
= "present_or_copy";
31592 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31593 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31594 c_name
= "present_or_copyin";
31596 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31597 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31598 c_name
= "present_or_copyout";
31600 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31601 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31602 c_name
= "present_or_create";
31604 case PRAGMA_OACC_CLAUSE_PRIVATE
:
31605 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
31607 c_name
= "private";
31609 case PRAGMA_OACC_CLAUSE_REDUCTION
:
31610 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31611 c_name
= "reduction";
31613 case PRAGMA_OACC_CLAUSE_SELF
:
31614 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31617 case PRAGMA_OACC_CLAUSE_SEQ
:
31618 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
31622 case PRAGMA_OACC_CLAUSE_TILE
:
31623 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
31626 case PRAGMA_OACC_CLAUSE_VECTOR
:
31628 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
31631 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
31632 c_name
= "vector_length";
31633 code
= OMP_CLAUSE_VECTOR_LENGTH
;
31634 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31637 case PRAGMA_OACC_CLAUSE_WAIT
:
31638 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
31641 case PRAGMA_OACC_CLAUSE_WORKER
:
31643 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
31647 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
31653 if (((mask
>> c_kind
) & 1) == 0)
31655 /* Remove the invalid clause(s) from the list to avoid
31656 confusing the rest of the compiler. */
31658 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
31663 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31666 return finish_omp_clauses (clauses
, false);
31671 /* Parse all OpenMP clauses. The set clauses allowed by the directive
31672 is a bitmask in MASK. Return the list of clauses found; the result
31673 of clause default goes in *pdefault. */
31676 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
31677 const char *where
, cp_token
*pragma_tok
,
31678 bool finish_p
= true)
31680 tree clauses
= NULL
;
31682 cp_token
*token
= NULL
;
31684 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31686 pragma_omp_clause c_kind
;
31687 const char *c_name
;
31688 tree prev
= clauses
;
31690 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31691 cp_lexer_consume_token (parser
->lexer
);
31693 token
= cp_lexer_peek_token (parser
->lexer
);
31694 c_kind
= cp_parser_omp_clause_name (parser
);
31698 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
31699 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
31701 c_name
= "collapse";
31703 case PRAGMA_OMP_CLAUSE_COPYIN
:
31704 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
31707 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
31708 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
31710 c_name
= "copyprivate";
31712 case PRAGMA_OMP_CLAUSE_DEFAULT
:
31713 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
31714 token
->location
, false);
31715 c_name
= "default";
31717 case PRAGMA_OMP_CLAUSE_FINAL
:
31718 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
31721 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
31722 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31724 c_name
= "firstprivate";
31726 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
31727 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
31729 c_name
= "grainsize";
31731 case PRAGMA_OMP_CLAUSE_HINT
:
31732 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
31736 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
31737 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
31739 c_name
= "defaultmap";
31741 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
31742 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
31744 c_name
= "use_device_ptr";
31746 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
31747 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
31749 c_name
= "is_device_ptr";
31751 case PRAGMA_OMP_CLAUSE_IF
:
31752 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
31756 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
31757 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
31759 c_name
= "lastprivate";
31761 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
31762 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
31764 c_name
= "mergeable";
31766 case PRAGMA_OMP_CLAUSE_NOWAIT
:
31767 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
31770 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
31771 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
31773 c_name
= "num_tasks";
31775 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
31776 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
31778 c_name
= "num_threads";
31780 case PRAGMA_OMP_CLAUSE_ORDERED
:
31781 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
31783 c_name
= "ordered";
31785 case PRAGMA_OMP_CLAUSE_PRIORITY
:
31786 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
31788 c_name
= "priority";
31790 case PRAGMA_OMP_CLAUSE_PRIVATE
:
31791 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
31793 c_name
= "private";
31795 case PRAGMA_OMP_CLAUSE_REDUCTION
:
31796 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31797 c_name
= "reduction";
31799 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
31800 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
31802 c_name
= "schedule";
31804 case PRAGMA_OMP_CLAUSE_SHARED
:
31805 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
31809 case PRAGMA_OMP_CLAUSE_UNTIED
:
31810 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
31814 case PRAGMA_OMP_CLAUSE_INBRANCH
:
31815 case PRAGMA_CILK_CLAUSE_MASK
:
31816 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
31817 clauses
, token
->location
);
31818 c_name
= "inbranch";
31820 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
31821 case PRAGMA_CILK_CLAUSE_NOMASK
:
31822 clauses
= cp_parser_omp_clause_branch (parser
,
31823 OMP_CLAUSE_NOTINBRANCH
,
31824 clauses
, token
->location
);
31825 c_name
= "notinbranch";
31827 case PRAGMA_OMP_CLAUSE_PARALLEL
:
31828 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
31829 clauses
, token
->location
);
31830 c_name
= "parallel";
31834 error_at (token
->location
, "%qs must be the first clause of %qs",
31839 case PRAGMA_OMP_CLAUSE_FOR
:
31840 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
31841 clauses
, token
->location
);
31844 goto clause_not_first
;
31846 case PRAGMA_OMP_CLAUSE_SECTIONS
:
31847 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
31848 clauses
, token
->location
);
31849 c_name
= "sections";
31851 goto clause_not_first
;
31853 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
31854 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
31855 clauses
, token
->location
);
31856 c_name
= "taskgroup";
31858 goto clause_not_first
;
31860 case PRAGMA_OMP_CLAUSE_LINK
:
31861 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
31864 case PRAGMA_OMP_CLAUSE_TO
:
31865 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
31866 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
31869 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
31872 case PRAGMA_OMP_CLAUSE_FROM
:
31873 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
31876 case PRAGMA_OMP_CLAUSE_UNIFORM
:
31877 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
31879 c_name
= "uniform";
31881 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
31882 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
31884 c_name
= "num_teams";
31886 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
31887 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
31889 c_name
= "thread_limit";
31891 case PRAGMA_OMP_CLAUSE_ALIGNED
:
31892 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
31893 c_name
= "aligned";
31895 case PRAGMA_OMP_CLAUSE_LINEAR
:
31897 bool cilk_simd_fn
= false, declare_simd
= false;
31898 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
31899 cilk_simd_fn
= true;
31900 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
31901 declare_simd
= true;
31902 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
31903 cilk_simd_fn
, declare_simd
);
31907 case PRAGMA_OMP_CLAUSE_DEPEND
:
31908 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
31912 case PRAGMA_OMP_CLAUSE_MAP
:
31913 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
31916 case PRAGMA_OMP_CLAUSE_DEVICE
:
31917 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
31921 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
31922 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
31924 c_name
= "dist_schedule";
31926 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
31927 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
31929 c_name
= "proc_bind";
31931 case PRAGMA_OMP_CLAUSE_SAFELEN
:
31932 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
31934 c_name
= "safelen";
31936 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
31937 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
31939 c_name
= "simdlen";
31941 case PRAGMA_OMP_CLAUSE_NOGROUP
:
31942 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
31944 c_name
= "nogroup";
31946 case PRAGMA_OMP_CLAUSE_THREADS
:
31948 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
31949 clauses
, token
->location
);
31950 c_name
= "threads";
31952 case PRAGMA_OMP_CLAUSE_SIMD
:
31954 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
31955 clauses
, token
->location
);
31958 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
31959 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
31960 c_name
= "simdlen";
31963 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
31969 if (((mask
>> c_kind
) & 1) == 0)
31971 /* Remove the invalid clause(s) from the list to avoid
31972 confusing the rest of the compiler. */
31974 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
31978 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
31979 no reason to skip to the end. */
31980 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
31981 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31984 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
31985 return finish_omp_clauses (clauses
, false, true);
31987 return finish_omp_clauses (clauses
, true);
31996 In practice, we're also interested in adding the statement to an
31997 outer node. So it is convenient if we work around the fact that
31998 cp_parser_statement calls add_stmt. */
32001 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
32003 unsigned save
= parser
->in_statement
;
32005 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32006 This preserves the "not within loop or switch" style error messages
32007 for nonsense cases like
32013 if (parser
->in_statement
)
32014 parser
->in_statement
= IN_OMP_BLOCK
;
32020 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
32022 parser
->in_statement
= save
;
32026 cp_parser_omp_structured_block (cp_parser
*parser
)
32028 tree stmt
= begin_omp_structured_block ();
32029 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
32031 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
32033 cp_parser_end_omp_structured_block (parser
, save
);
32034 return finish_omp_structured_block (stmt
);
32038 # pragma omp atomic new-line
32042 x binop= expr | x++ | ++x | x-- | --x
32044 +, *, -, /, &, ^, |, <<, >>
32046 where x is an lvalue expression with scalar type.
32049 # pragma omp atomic new-line
32052 # pragma omp atomic read new-line
32055 # pragma omp atomic write new-line
32058 # pragma omp atomic update new-line
32061 # pragma omp atomic capture new-line
32064 # pragma omp atomic capture new-line
32072 expression-stmt | x = x binop expr
32074 v = expression-stmt
32076 { v = x; update-stmt; } | { update-stmt; v = x; }
32080 expression-stmt | x = x binop expr | x = expr binop x
32084 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32086 where x and v are lvalue expressions with scalar type. */
32089 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
32091 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
32092 tree rhs1
= NULL_TREE
, orig_lhs
;
32093 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
32094 bool structured_block
= false;
32095 bool seq_cst
= false;
32097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32099 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32100 const char *p
= IDENTIFIER_POINTER (id
);
32102 if (!strcmp (p
, "seq_cst"))
32105 cp_lexer_consume_token (parser
->lexer
);
32106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32107 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32108 cp_lexer_consume_token (parser
->lexer
);
32111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32113 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32114 const char *p
= IDENTIFIER_POINTER (id
);
32116 if (!strcmp (p
, "read"))
32117 code
= OMP_ATOMIC_READ
;
32118 else if (!strcmp (p
, "write"))
32120 else if (!strcmp (p
, "update"))
32122 else if (!strcmp (p
, "capture"))
32123 code
= OMP_ATOMIC_CAPTURE_NEW
;
32127 cp_lexer_consume_token (parser
->lexer
);
32131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32132 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32133 cp_lexer_consume_token (parser
->lexer
);
32135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32137 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32138 const char *p
= IDENTIFIER_POINTER (id
);
32140 if (!strcmp (p
, "seq_cst"))
32143 cp_lexer_consume_token (parser
->lexer
);
32147 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32151 case OMP_ATOMIC_READ
:
32152 case NOP_EXPR
: /* atomic write */
32153 v
= cp_parser_unary_expression (parser
);
32154 if (v
== error_mark_node
)
32156 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32158 if (code
== NOP_EXPR
)
32159 lhs
= cp_parser_expression (parser
);
32161 lhs
= cp_parser_unary_expression (parser
);
32162 if (lhs
== error_mark_node
)
32164 if (code
== NOP_EXPR
)
32166 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32174 case OMP_ATOMIC_CAPTURE_NEW
:
32175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32177 cp_lexer_consume_token (parser
->lexer
);
32178 structured_block
= true;
32182 v
= cp_parser_unary_expression (parser
);
32183 if (v
== error_mark_node
)
32185 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32193 lhs
= cp_parser_unary_expression (parser
);
32195 switch (TREE_CODE (lhs
))
32200 case POSTINCREMENT_EXPR
:
32201 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32202 code
= OMP_ATOMIC_CAPTURE_OLD
;
32204 case PREINCREMENT_EXPR
:
32205 lhs
= TREE_OPERAND (lhs
, 0);
32206 opcode
= PLUS_EXPR
;
32207 rhs
= integer_one_node
;
32210 case POSTDECREMENT_EXPR
:
32211 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32212 code
= OMP_ATOMIC_CAPTURE_OLD
;
32214 case PREDECREMENT_EXPR
:
32215 lhs
= TREE_OPERAND (lhs
, 0);
32216 opcode
= MINUS_EXPR
;
32217 rhs
= integer_one_node
;
32220 case COMPOUND_EXPR
:
32221 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
32222 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
32223 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
32224 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
32225 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32226 (TREE_OPERAND (lhs
, 1), 0), 0)))
32228 /* Undo effects of boolean_increment for post {in,de}crement. */
32229 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
32232 if (TREE_CODE (lhs
) == MODIFY_EXPR
32233 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
32235 /* Undo effects of boolean_increment. */
32236 if (integer_onep (TREE_OPERAND (lhs
, 1)))
32238 /* This is pre or post increment. */
32239 rhs
= TREE_OPERAND (lhs
, 1);
32240 lhs
= TREE_OPERAND (lhs
, 0);
32242 if (code
== OMP_ATOMIC_CAPTURE_NEW
32243 && !structured_block
32244 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
32245 code
= OMP_ATOMIC_CAPTURE_OLD
;
32251 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32254 opcode
= MULT_EXPR
;
32257 opcode
= TRUNC_DIV_EXPR
;
32260 opcode
= PLUS_EXPR
;
32263 opcode
= MINUS_EXPR
;
32265 case CPP_LSHIFT_EQ
:
32266 opcode
= LSHIFT_EXPR
;
32268 case CPP_RSHIFT_EQ
:
32269 opcode
= RSHIFT_EXPR
;
32272 opcode
= BIT_AND_EXPR
;
32275 opcode
= BIT_IOR_EXPR
;
32278 opcode
= BIT_XOR_EXPR
;
32281 enum cp_parser_prec oprec
;
32283 cp_lexer_consume_token (parser
->lexer
);
32284 cp_parser_parse_tentatively (parser
);
32285 rhs1
= cp_parser_simple_cast_expression (parser
);
32286 if (rhs1
== error_mark_node
)
32288 cp_parser_abort_tentative_parse (parser
);
32289 cp_parser_simple_cast_expression (parser
);
32292 token
= cp_lexer_peek_token (parser
->lexer
);
32293 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
32295 cp_parser_abort_tentative_parse (parser
);
32296 cp_parser_parse_tentatively (parser
);
32297 rhs
= cp_parser_binary_expression (parser
, false, true,
32298 PREC_NOT_OPERATOR
, NULL
);
32299 if (rhs
== error_mark_node
)
32301 cp_parser_abort_tentative_parse (parser
);
32302 cp_parser_binary_expression (parser
, false, true,
32303 PREC_NOT_OPERATOR
, NULL
);
32306 switch (TREE_CODE (rhs
))
32309 case TRUNC_DIV_EXPR
:
32318 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
32320 if (cp_parser_parse_definitely (parser
))
32322 opcode
= TREE_CODE (rhs
);
32323 rhs1
= TREE_OPERAND (rhs
, 0);
32324 rhs
= TREE_OPERAND (rhs
, 1);
32334 cp_parser_abort_tentative_parse (parser
);
32335 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
32337 rhs
= cp_parser_expression (parser
);
32338 if (rhs
== error_mark_node
)
32344 cp_parser_error (parser
,
32345 "invalid form of %<#pragma omp atomic%>");
32348 if (!cp_parser_parse_definitely (parser
))
32350 switch (token
->type
)
32352 case CPP_SEMICOLON
:
32353 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
32355 code
= OMP_ATOMIC_CAPTURE_OLD
;
32360 cp_lexer_consume_token (parser
->lexer
);
32363 else if (structured_block
)
32370 cp_parser_error (parser
,
32371 "invalid form of %<#pragma omp atomic%>");
32374 opcode
= MULT_EXPR
;
32377 opcode
= TRUNC_DIV_EXPR
;
32380 opcode
= PLUS_EXPR
;
32383 opcode
= MINUS_EXPR
;
32386 opcode
= LSHIFT_EXPR
;
32389 opcode
= RSHIFT_EXPR
;
32392 opcode
= BIT_AND_EXPR
;
32395 opcode
= BIT_IOR_EXPR
;
32398 opcode
= BIT_XOR_EXPR
;
32401 cp_parser_error (parser
,
32402 "invalid operator for %<#pragma omp atomic%>");
32405 oprec
= TOKEN_PRECEDENCE (token
);
32406 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
32407 if (commutative_tree_code (opcode
))
32408 oprec
= (enum cp_parser_prec
) (oprec
- 1);
32409 cp_lexer_consume_token (parser
->lexer
);
32410 rhs
= cp_parser_binary_expression (parser
, false, false,
32412 if (rhs
== error_mark_node
)
32417 cp_parser_error (parser
,
32418 "invalid operator for %<#pragma omp atomic%>");
32421 cp_lexer_consume_token (parser
->lexer
);
32423 rhs
= cp_parser_expression (parser
);
32424 if (rhs
== error_mark_node
)
32429 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
32431 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
32433 v
= cp_parser_unary_expression (parser
);
32434 if (v
== error_mark_node
)
32436 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32438 lhs1
= cp_parser_unary_expression (parser
);
32439 if (lhs1
== error_mark_node
)
32442 if (structured_block
)
32444 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32445 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
32448 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
32449 if (!structured_block
)
32450 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32454 cp_parser_skip_to_end_of_block_or_statement (parser
);
32455 if (structured_block
)
32457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
32458 cp_lexer_consume_token (parser
->lexer
);
32459 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
32461 cp_parser_skip_to_end_of_block_or_statement (parser
);
32462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
32463 cp_lexer_consume_token (parser
->lexer
);
32470 # pragma omp barrier new-line */
32473 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
32475 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32476 finish_omp_barrier ();
32480 # pragma omp critical [(name)] new-line
32484 # pragma omp critical [(name) [hint(expression)]] new-line
32485 structured-block */
32487 #define OMP_CRITICAL_CLAUSE_MASK \
32488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
32491 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
32493 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
32495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32497 cp_lexer_consume_token (parser
->lexer
);
32499 name
= cp_parser_identifier (parser
);
32501 if (name
== error_mark_node
32502 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32504 /*or_comma=*/false,
32505 /*consume_paren=*/true);
32506 if (name
== error_mark_node
)
32509 clauses
= cp_parser_omp_all_clauses (parser
,
32510 OMP_CRITICAL_CLAUSE_MASK
,
32511 "#pragma omp critical", pragma_tok
);
32514 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32516 stmt
= cp_parser_omp_structured_block (parser
);
32517 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
32521 # pragma omp flush flush-vars[opt] new-line
32524 ( variable-list ) */
32527 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
32529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32530 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32531 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32533 finish_omp_flush ();
32536 /* Helper function, to parse omp for increment expression. */
32539 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
32541 tree cond
= cp_parser_binary_expression (parser
, false, true,
32542 PREC_NOT_OPERATOR
, NULL
);
32543 if (cond
== error_mark_node
32544 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
32546 cp_parser_skip_to_end_of_statement (parser
);
32547 return error_mark_node
;
32550 switch (TREE_CODE (cond
))
32558 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
32560 /* Fall through: OpenMP disallows NE_EXPR. */
32562 return error_mark_node
;
32565 /* If decl is an iterator, preserve LHS and RHS of the relational
32566 expr until finish_omp_for. */
32568 && (type_dependent_expression_p (decl
)
32569 || CLASS_TYPE_P (TREE_TYPE (decl
))))
32572 return build_x_binary_op (input_location
, TREE_CODE (cond
),
32573 TREE_OPERAND (cond
, 0), ERROR_MARK
,
32574 TREE_OPERAND (cond
, 1), ERROR_MARK
,
32575 /*overload=*/NULL
, tf_warning_or_error
);
32578 /* Helper function, to parse omp for increment expression. */
32581 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
32583 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32589 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
32591 op
= (token
->type
== CPP_PLUS_PLUS
32592 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
32593 cp_lexer_consume_token (parser
->lexer
);
32594 lhs
= cp_parser_simple_cast_expression (parser
);
32596 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
32597 return error_mark_node
;
32598 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
32601 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
32603 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
32604 return error_mark_node
;
32606 token
= cp_lexer_peek_token (parser
->lexer
);
32607 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
32609 op
= (token
->type
== CPP_PLUS_PLUS
32610 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
32611 cp_lexer_consume_token (parser
->lexer
);
32612 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
32615 op
= cp_parser_assignment_operator_opt (parser
);
32616 if (op
== ERROR_MARK
)
32617 return error_mark_node
;
32619 if (op
!= NOP_EXPR
)
32621 rhs
= cp_parser_assignment_expression (parser
);
32622 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
32623 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
32626 lhs
= cp_parser_binary_expression (parser
, false, false,
32627 PREC_ADDITIVE_EXPRESSION
, NULL
);
32628 token
= cp_lexer_peek_token (parser
->lexer
);
32629 decl_first
= (lhs
== decl
32630 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
32633 if (token
->type
!= CPP_PLUS
32634 && token
->type
!= CPP_MINUS
)
32635 return error_mark_node
;
32639 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
32640 cp_lexer_consume_token (parser
->lexer
);
32641 rhs
= cp_parser_binary_expression (parser
, false, false,
32642 PREC_ADDITIVE_EXPRESSION
, NULL
);
32643 token
= cp_lexer_peek_token (parser
->lexer
);
32644 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
32646 if (lhs
== NULL_TREE
)
32648 if (op
== PLUS_EXPR
)
32651 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
32652 tf_warning_or_error
);
32655 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
32656 ERROR_MARK
, NULL
, tf_warning_or_error
);
32659 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
32664 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
32665 || op
== MINUS_EXPR
)
32666 return error_mark_node
;
32667 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
32670 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
32672 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
32675 /* Parse the initialization statement of either an OpenMP for loop or
32676 a Cilk Plus for loop.
32678 Return true if the resulting construct should have an
32679 OMP_CLAUSE_PRIVATE added to it. */
32682 cp_parser_omp_for_loop_init (cp_parser
*parser
,
32683 enum tree_code code
,
32684 tree
&this_pre_body
,
32685 vec
<tree
, va_gc
> *for_block
,
32691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32694 tree add_private_clause
= NULL_TREE
;
32696 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
32700 integer-type var = lb
32701 random-access-iterator-type var = lb
32702 pointer-type var = lb
32704 cp_decl_specifier_seq type_specifiers
;
32706 /* First, try to parse as an initialized declaration. See
32707 cp_parser_condition, from whence the bulk of this is copied. */
32709 cp_parser_parse_tentatively (parser
);
32710 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
32711 /*is_trailing_return=*/false,
32713 if (cp_parser_parse_definitely (parser
))
32715 /* If parsing a type specifier seq succeeded, then this
32716 MUST be a initialized declaration. */
32717 tree asm_specification
, attributes
;
32718 cp_declarator
*declarator
;
32720 declarator
= cp_parser_declarator (parser
,
32721 CP_PARSER_DECLARATOR_NAMED
,
32722 /*ctor_dtor_or_conv_p=*/NULL
,
32723 /*parenthesized_p=*/NULL
,
32724 /*member_p=*/false,
32725 /*friend_p=*/false);
32726 attributes
= cp_parser_attributes_opt (parser
);
32727 asm_specification
= cp_parser_asm_specification_opt (parser
);
32729 if (declarator
== cp_error_declarator
)
32730 cp_parser_skip_to_end_of_statement (parser
);
32734 tree pushed_scope
, auto_node
;
32736 decl
= start_decl (declarator
, &type_specifiers
,
32737 SD_INITIALIZED
, attributes
,
32738 /*prefix_attributes=*/NULL_TREE
,
32741 auto_node
= type_uses_auto (TREE_TYPE (decl
));
32742 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
32744 if (cp_lexer_next_token_is (parser
->lexer
,
32747 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
32748 error ("parenthesized initialization is not allowed in "
32749 "OpenMP %<for%> loop");
32751 error ("parenthesized initialization is "
32752 "not allowed in for-loop");
32755 /* Trigger an error. */
32756 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
32758 init
= error_mark_node
;
32759 cp_parser_skip_to_end_of_statement (parser
);
32761 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
32762 || type_dependent_expression_p (decl
)
32765 bool is_direct_init
, is_non_constant_init
;
32767 init
= cp_parser_initializer (parser
,
32769 &is_non_constant_init
);
32774 = do_auto_deduction (TREE_TYPE (decl
), init
,
32777 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
32778 && !type_dependent_expression_p (decl
))
32782 cp_finish_decl (decl
, init
, !is_non_constant_init
,
32784 LOOKUP_ONLYCONVERTING
);
32786 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
32788 vec_safe_push (for_block
, this_pre_body
);
32792 init
= pop_stmt_list (this_pre_body
);
32793 this_pre_body
= NULL_TREE
;
32798 cp_lexer_consume_token (parser
->lexer
);
32799 init
= cp_parser_assignment_expression (parser
);
32802 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
32803 init
= error_mark_node
;
32805 cp_finish_decl (decl
, NULL_TREE
,
32806 /*init_const_expr_p=*/false,
32808 LOOKUP_ONLYCONVERTING
);
32812 pop_scope (pushed_scope
);
32818 /* If parsing a type specifier sequence failed, then
32819 this MUST be a simple expression. */
32820 if (code
== CILK_FOR
)
32821 error ("%<_Cilk_for%> allows expression instead of declaration only "
32822 "in C, not in C++");
32823 cp_parser_parse_tentatively (parser
);
32824 decl
= cp_parser_primary_expression (parser
, false, false,
32826 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
32827 if (!cp_parser_error_occurred (parser
)
32829 && (TREE_CODE (decl
) == COMPONENT_REF
32830 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
32832 cp_parser_abort_tentative_parse (parser
);
32833 cp_parser_parse_tentatively (parser
);
32834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32835 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32836 /*check_dependency_p=*/true,
32837 /*template_p=*/NULL
,
32838 /*declarator_p=*/false,
32839 /*optional_p=*/false);
32840 if (name
!= error_mark_node
32841 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
32843 decl
= cp_parser_lookup_name_simple (parser
, name
,
32845 if (TREE_CODE (decl
) == FIELD_DECL
)
32846 add_private_clause
= omp_privatize_field (decl
, false);
32848 cp_parser_abort_tentative_parse (parser
);
32849 cp_parser_parse_tentatively (parser
);
32850 decl
= cp_parser_primary_expression (parser
, false, false,
32853 if (!cp_parser_error_occurred (parser
)
32856 && CLASS_TYPE_P (TREE_TYPE (decl
)))
32860 cp_parser_parse_definitely (parser
);
32861 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
32862 rhs
= cp_parser_assignment_expression (parser
);
32864 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
32867 tf_warning_or_error
));
32868 if (!add_private_clause
)
32869 add_private_clause
= decl
;
32874 cp_parser_abort_tentative_parse (parser
);
32875 init
= cp_parser_expression (parser
);
32878 if (TREE_CODE (init
) == MODIFY_EXPR
32879 || TREE_CODE (init
) == MODOP_EXPR
)
32880 real_decl
= TREE_OPERAND (init
, 0);
32884 return add_private_clause
;
32887 /* Parse the restricted form of the for statement allowed by OpenMP. */
32890 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
32893 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
32894 tree real_decl
, initv
, condv
, incrv
, declv
;
32895 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
32896 location_t loc_first
;
32897 bool collapse_err
= false;
32898 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
32899 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
32900 auto_vec
<tree
, 4> orig_inits
;
32902 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
32903 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
32904 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
32905 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
32906 && OMP_CLAUSE_ORDERED_EXPR (cl
))
32909 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
32912 if (ordered
&& ordered
< collapse
)
32914 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
32915 "%<ordered%> clause parameter is less than %<collapse%>");
32916 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
32917 = build_int_cst (NULL_TREE
, collapse
);
32918 ordered
= collapse
;
32922 for (tree
*pc
= &clauses
; *pc
; )
32923 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
32925 error_at (OMP_CLAUSE_LOCATION (*pc
),
32926 "%<linear%> clause may not be specified together "
32927 "with %<ordered%> clause with a parameter");
32928 *pc
= OMP_CLAUSE_CHAIN (*pc
);
32931 pc
= &OMP_CLAUSE_CHAIN (*pc
);
32934 gcc_assert (collapse
>= 1 && ordered
>= 0);
32935 count
= ordered
? ordered
: collapse
;
32937 declv
= make_tree_vec (count
);
32938 initv
= make_tree_vec (count
);
32939 condv
= make_tree_vec (count
);
32940 incrv
= make_tree_vec (count
);
32942 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
32944 for (i
= 0; i
< count
; i
++)
32946 int bracecount
= 0;
32947 tree add_private_clause
= NULL_TREE
;
32950 if (code
!= CILK_FOR
32951 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32953 cp_parser_error (parser
, "for statement expected");
32956 if (code
== CILK_FOR
32957 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32959 cp_parser_error (parser
, "_Cilk_for statement expected");
32962 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
32964 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32967 init
= orig_init
= decl
= real_decl
= NULL
;
32968 this_pre_body
= push_stmt_list ();
32971 = cp_parser_omp_for_loop_init (parser
, code
,
32972 this_pre_body
, for_block
,
32973 init
, orig_init
, decl
, real_decl
);
32975 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32978 this_pre_body
= pop_stmt_list (this_pre_body
);
32982 pre_body
= push_stmt_list ();
32984 add_stmt (this_pre_body
);
32985 pre_body
= pop_stmt_list (pre_body
);
32988 pre_body
= this_pre_body
;
32993 if (cclauses
!= NULL
32994 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
32995 && real_decl
!= NULL_TREE
)
32998 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
32999 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
33000 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33002 error_at (loc
, "iteration variable %qD"
33003 " should not be firstprivate", real_decl
);
33004 *c
= OMP_CLAUSE_CHAIN (*c
);
33006 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
33007 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33009 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33011 *c
= OMP_CLAUSE_CHAIN (*c
);
33012 if (code
== OMP_SIMD
)
33014 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33015 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
33019 OMP_CLAUSE_CHAIN (l
) = clauses
;
33022 add_private_clause
= NULL_TREE
;
33026 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
33027 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33028 add_private_clause
= NULL_TREE
;
33029 c
= &OMP_CLAUSE_CHAIN (*c
);
33033 if (add_private_clause
)
33036 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33038 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
33039 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
33040 && OMP_CLAUSE_DECL (c
) == decl
)
33042 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
33043 && OMP_CLAUSE_DECL (c
) == decl
)
33044 error_at (loc
, "iteration variable %qD "
33045 "should not be firstprivate",
33047 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
33048 && OMP_CLAUSE_DECL (c
) == decl
)
33049 error_at (loc
, "iteration variable %qD should not be reduction",
33054 if (code
!= OMP_SIMD
)
33055 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
33056 else if (collapse
== 1)
33057 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33059 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
33060 OMP_CLAUSE_DECL (c
) = add_private_clause
;
33061 c
= finish_omp_clauses (c
, true);
33064 OMP_CLAUSE_CHAIN (c
) = clauses
;
33066 /* For linear, signal that we need to fill up
33067 the so far unknown linear step. */
33068 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
33069 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
33075 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33076 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
33077 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
33080 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
33082 /* If decl is an iterator, preserve the operator on decl
33083 until finish_omp_for. */
33085 && ((processing_template_decl
33086 && (TREE_TYPE (real_decl
) == NULL_TREE
33087 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
33088 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
33089 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
33091 incr
= cp_parser_expression (parser
);
33092 if (!EXPR_HAS_LOCATION (incr
))
33093 protected_set_expr_location (incr
, input_location
);
33096 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33097 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33098 /*or_comma=*/false,
33099 /*consume_paren=*/true);
33101 TREE_VEC_ELT (declv
, i
) = decl
;
33102 TREE_VEC_ELT (initv
, i
) = init
;
33103 TREE_VEC_ELT (condv
, i
) = cond
;
33104 TREE_VEC_ELT (incrv
, i
) = incr
;
33107 orig_inits
.safe_grow_cleared (i
+ 1);
33108 orig_inits
[i
] = orig_init
;
33111 if (i
== count
- 1)
33114 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33115 in between the collapsed for loops to be still considered perfectly
33116 nested. Hopefully the final version clarifies this.
33117 For now handle (multiple) {'s and empty statements. */
33118 cp_parser_parse_tentatively (parser
);
33121 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33123 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33125 cp_lexer_consume_token (parser
->lexer
);
33128 else if (bracecount
33129 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33130 cp_lexer_consume_token (parser
->lexer
);
33133 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33134 error_at (loc
, "not enough collapsed for loops");
33135 collapse_err
= true;
33136 cp_parser_abort_tentative_parse (parser
);
33145 cp_parser_parse_definitely (parser
);
33146 nbraces
+= bracecount
;
33150 /* Note that we saved the original contents of this flag when we entered
33151 the structured block, and so we don't need to re-save it here. */
33152 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
33153 parser
->in_statement
= IN_CILK_SIMD_FOR
;
33155 parser
->in_statement
= IN_OMP_FOR
;
33157 /* Note that the grammar doesn't call for a structured block here,
33158 though the loop as a whole is a structured block. */
33159 body
= push_stmt_list ();
33160 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33161 body
= pop_stmt_list (body
);
33163 if (declv
== NULL_TREE
)
33166 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
33167 body
, pre_body
, &orig_inits
, clauses
);
33171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33173 cp_lexer_consume_token (parser
->lexer
);
33176 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33177 cp_lexer_consume_token (parser
->lexer
);
33182 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33183 "collapsed loops not perfectly nested");
33185 collapse_err
= true;
33186 cp_parser_statement_seq_opt (parser
, NULL
);
33187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
33192 while (!for_block
->is_empty ())
33193 add_stmt (pop_stmt_list (for_block
->pop ()));
33194 release_tree_vector (for_block
);
33199 /* Helper function for OpenMP parsing, split clauses and call
33200 finish_omp_clauses on each of the set of clauses afterwards. */
33203 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
33204 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
33207 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
33208 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
33210 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], true);
33214 #pragma omp simd simd-clause[optseq] new-line
33217 #define OMP_SIMD_CLAUSE_MASK \
33218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33228 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
33229 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33231 tree clauses
, sb
, ret
;
33233 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33235 strcat (p_name
, " simd");
33236 mask
|= OMP_SIMD_CLAUSE_MASK
;
33238 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33242 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
33243 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
33244 tree c
= find_omp_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
33245 OMP_CLAUSE_ORDERED
);
33246 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
33248 error_at (OMP_CLAUSE_LOCATION (c
),
33249 "%<ordered%> clause with parameter may not be specified "
33250 "on %qs construct", p_name
);
33251 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
33255 sb
= begin_omp_structured_block ();
33256 save
= cp_parser_begin_omp_structured_block (parser
);
33258 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
33260 cp_parser_end_omp_structured_block (parser
, save
);
33261 add_stmt (finish_omp_structured_block (sb
));
33267 #pragma omp for for-clause[optseq] new-line
33271 #pragma omp for simd for-simd-clause[optseq] new-line
33274 #define OMP_FOR_CLAUSE_MASK \
33275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33286 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
33287 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33289 tree clauses
, sb
, ret
;
33291 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33293 strcat (p_name
, " for");
33294 mask
|= OMP_FOR_CLAUSE_MASK
;
33296 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
33297 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33298 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33299 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
33301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33304 const char *p
= IDENTIFIER_POINTER (id
);
33306 if (strcmp (p
, "simd") == 0)
33308 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33309 if (cclauses
== NULL
)
33310 cclauses
= cclauses_buf
;
33312 cp_lexer_consume_token (parser
->lexer
);
33313 if (!flag_openmp
) /* flag_openmp_simd */
33314 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33316 sb
= begin_omp_structured_block ();
33317 save
= cp_parser_begin_omp_structured_block (parser
);
33318 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33320 cp_parser_end_omp_structured_block (parser
, save
);
33321 tree body
= finish_omp_structured_block (sb
);
33324 ret
= make_node (OMP_FOR
);
33325 TREE_TYPE (ret
) = void_type_node
;
33326 OMP_FOR_BODY (ret
) = body
;
33327 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33328 SET_EXPR_LOCATION (ret
, loc
);
33333 if (!flag_openmp
) /* flag_openmp_simd */
33335 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33339 /* Composite distribute parallel for disallows linear clause. */
33340 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33341 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
33343 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33347 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
33348 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33351 sb
= begin_omp_structured_block ();
33352 save
= cp_parser_begin_omp_structured_block (parser
);
33354 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
33356 cp_parser_end_omp_structured_block (parser
, save
);
33357 add_stmt (finish_omp_structured_block (sb
));
33363 # pragma omp master new-line
33364 structured-block */
33367 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
33369 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33370 return c_finish_omp_master (input_location
,
33371 cp_parser_omp_structured_block (parser
));
33375 # pragma omp ordered new-line
33379 # pragma omp ordered ordered-clauses new-line
33380 structured-block */
33382 #define OMP_ORDERED_CLAUSE_MASK \
33383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33386 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33387 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33390 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
33391 enum pragma_context context
)
33393 location_t loc
= pragma_tok
->location
;
33395 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
33397 cp_parser_error (parser
, "expected declaration specifiers");
33398 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33404 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33405 const char *p
= IDENTIFIER_POINTER (id
);
33407 if (strcmp (p
, "depend") == 0)
33409 if (context
== pragma_stmt
)
33411 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
33412 "%<depend%> clause may only be used in compound "
33414 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33418 = cp_parser_omp_all_clauses (parser
,
33419 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
33420 "#pragma omp ordered", pragma_tok
);
33421 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
33427 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
33428 "#pragma omp ordered", pragma_tok
);
33429 c_finish_omp_ordered (loc
, clauses
,
33430 cp_parser_omp_structured_block (parser
));
33437 { section-sequence }
33440 section-directive[opt] structured-block
33441 section-sequence section-directive structured-block */
33444 cp_parser_omp_sections_scope (cp_parser
*parser
)
33446 tree stmt
, substmt
;
33447 bool error_suppress
= false;
33450 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
33453 stmt
= push_stmt_list ();
33455 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
33457 substmt
= cp_parser_omp_structured_block (parser
);
33458 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
33459 add_stmt (substmt
);
33464 tok
= cp_lexer_peek_token (parser
->lexer
);
33465 if (tok
->type
== CPP_CLOSE_BRACE
)
33467 if (tok
->type
== CPP_EOF
)
33470 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
33472 cp_lexer_consume_token (parser
->lexer
);
33473 cp_parser_require_pragma_eol (parser
, tok
);
33474 error_suppress
= false;
33476 else if (!error_suppress
)
33478 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
33479 error_suppress
= true;
33482 substmt
= cp_parser_omp_structured_block (parser
);
33483 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
33484 add_stmt (substmt
);
33486 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33488 substmt
= pop_stmt_list (stmt
);
33490 stmt
= make_node (OMP_SECTIONS
);
33491 TREE_TYPE (stmt
) = void_type_node
;
33492 OMP_SECTIONS_BODY (stmt
) = substmt
;
33499 # pragma omp sections sections-clause[optseq] newline
33502 #define OMP_SECTIONS_CLAUSE_MASK \
33503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33510 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
33511 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33514 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33516 strcat (p_name
, " sections");
33517 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
33519 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
33521 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33525 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
33526 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
33529 ret
= cp_parser_omp_sections_scope (parser
);
33531 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
33537 # pragma omp parallel parallel-clause[optseq] new-line
33539 # pragma omp parallel for parallel-for-clause[optseq] new-line
33541 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
33545 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
33546 structured-block */
33548 #define OMP_PARALLEL_CLAUSE_MASK \
33549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
33555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
33557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
33560 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
33561 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33563 tree stmt
, clauses
, block
;
33565 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33567 strcat (p_name
, " parallel");
33568 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
33569 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
33570 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
33571 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
33572 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
33574 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33576 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33577 if (cclauses
== NULL
)
33578 cclauses
= cclauses_buf
;
33580 cp_lexer_consume_token (parser
->lexer
);
33581 if (!flag_openmp
) /* flag_openmp_simd */
33582 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33583 block
= begin_omp_parallel ();
33584 save
= cp_parser_begin_omp_structured_block (parser
);
33585 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33586 cp_parser_end_omp_structured_block (parser
, save
);
33587 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
33589 if (ret
== NULL_TREE
)
33591 OMP_PARALLEL_COMBINED (stmt
) = 1;
33594 /* When combined with distribute, parallel has to be followed by for.
33595 #pragma omp target parallel is allowed though. */
33597 && (mask
& (OMP_CLAUSE_MASK_1
33598 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33600 error_at (loc
, "expected %<for%> after %qs", p_name
);
33601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33604 else if (!flag_openmp
) /* flag_openmp_simd */
33606 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33609 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33611 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33612 const char *p
= IDENTIFIER_POINTER (id
);
33613 if (strcmp (p
, "sections") == 0)
33615 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33616 cclauses
= cclauses_buf
;
33618 cp_lexer_consume_token (parser
->lexer
);
33619 block
= begin_omp_parallel ();
33620 save
= cp_parser_begin_omp_structured_block (parser
);
33621 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33622 cp_parser_end_omp_structured_block (parser
, save
);
33623 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
33625 OMP_PARALLEL_COMBINED (stmt
) = 1;
33630 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
33633 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
33634 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
33637 block
= begin_omp_parallel ();
33638 save
= cp_parser_begin_omp_structured_block (parser
);
33639 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33640 cp_parser_end_omp_structured_block (parser
, save
);
33641 stmt
= finish_omp_parallel (clauses
, block
);
33646 # pragma omp single single-clause[optseq] new-line
33647 structured-block */
33649 #define OMP_SINGLE_CLAUSE_MASK \
33650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
33653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33656 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
33658 tree stmt
= make_node (OMP_SINGLE
);
33659 TREE_TYPE (stmt
) = void_type_node
;
33661 OMP_SINGLE_CLAUSES (stmt
)
33662 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
33663 "#pragma omp single", pragma_tok
);
33664 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
33666 return add_stmt (stmt
);
33670 # pragma omp task task-clause[optseq] new-line
33671 structured-block */
33673 #define OMP_TASK_CLAUSE_MASK \
33674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
33683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33686 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
33688 tree clauses
, block
;
33691 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
33692 "#pragma omp task", pragma_tok
);
33693 block
= begin_omp_task ();
33694 save
= cp_parser_begin_omp_structured_block (parser
);
33695 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33696 cp_parser_end_omp_structured_block (parser
, save
);
33697 return finish_omp_task (clauses
, block
);
33701 # pragma omp taskwait new-line */
33704 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
33706 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33707 finish_omp_taskwait ();
33711 # pragma omp taskyield new-line */
33714 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
33716 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33717 finish_omp_taskyield ();
33721 # pragma omp taskgroup new-line
33722 structured-block */
33725 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
33727 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33728 return c_finish_omp_taskgroup (input_location
,
33729 cp_parser_omp_structured_block (parser
));
33734 # pragma omp threadprivate (variable-list) */
33737 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
33741 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
33742 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33744 finish_omp_threadprivate (vars
);
33748 # pragma omp cancel cancel-clause[optseq] new-line */
33750 #define OMP_CANCEL_CLAUSE_MASK \
33751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
33755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
33758 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
33760 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
33761 "#pragma omp cancel", pragma_tok
);
33762 finish_omp_cancel (clauses
);
33766 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
33768 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
33769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
33775 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
33778 bool point_seen
= false;
33780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33782 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33783 const char *p
= IDENTIFIER_POINTER (id
);
33785 if (strcmp (p
, "point") == 0)
33787 cp_lexer_consume_token (parser
->lexer
);
33793 cp_parser_error (parser
, "expected %<point%>");
33794 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33798 clauses
= cp_parser_omp_all_clauses (parser
,
33799 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
33800 "#pragma omp cancellation point",
33802 finish_omp_cancellation_point (clauses
);
33806 #pragma omp distribute distribute-clause[optseq] new-line
33809 #define OMP_DISTRIBUTE_CLAUSE_MASK \
33810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
33814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33817 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
33818 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33820 tree clauses
, sb
, ret
;
33822 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33824 strcat (p_name
, " distribute");
33825 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
33827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33829 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33830 const char *p
= IDENTIFIER_POINTER (id
);
33832 bool parallel
= false;
33834 if (strcmp (p
, "simd") == 0)
33837 parallel
= strcmp (p
, "parallel") == 0;
33838 if (parallel
|| simd
)
33840 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33841 if (cclauses
== NULL
)
33842 cclauses
= cclauses_buf
;
33843 cp_lexer_consume_token (parser
->lexer
);
33844 if (!flag_openmp
) /* flag_openmp_simd */
33847 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33850 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
33853 sb
= begin_omp_structured_block ();
33854 save
= cp_parser_begin_omp_structured_block (parser
);
33856 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33859 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
33861 cp_parser_end_omp_structured_block (parser
, save
);
33862 tree body
= finish_omp_structured_block (sb
);
33865 ret
= make_node (OMP_DISTRIBUTE
);
33866 TREE_TYPE (ret
) = void_type_node
;
33867 OMP_FOR_BODY (ret
) = body
;
33868 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
33869 SET_EXPR_LOCATION (ret
, loc
);
33874 if (!flag_openmp
) /* flag_openmp_simd */
33876 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33880 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33884 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
33885 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
33888 sb
= begin_omp_structured_block ();
33889 save
= cp_parser_begin_omp_structured_block (parser
);
33891 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
33893 cp_parser_end_omp_structured_block (parser
, save
);
33894 add_stmt (finish_omp_structured_block (sb
));
33900 # pragma omp teams teams-clause[optseq] new-line
33901 structured-block */
33903 #define OMP_TEAMS_CLAUSE_MASK \
33904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
33909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
33910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
33913 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
33914 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33916 tree clauses
, sb
, ret
;
33918 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33920 strcat (p_name
, " teams");
33921 mask
|= OMP_TEAMS_CLAUSE_MASK
;
33923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33925 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33926 const char *p
= IDENTIFIER_POINTER (id
);
33927 if (strcmp (p
, "distribute") == 0)
33929 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33930 if (cclauses
== NULL
)
33931 cclauses
= cclauses_buf
;
33933 cp_lexer_consume_token (parser
->lexer
);
33934 if (!flag_openmp
) /* flag_openmp_simd */
33935 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
33937 sb
= begin_omp_structured_block ();
33938 save
= cp_parser_begin_omp_structured_block (parser
);
33939 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
33941 cp_parser_end_omp_structured_block (parser
, save
);
33942 tree body
= finish_omp_structured_block (sb
);
33945 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
33946 ret
= make_node (OMP_TEAMS
);
33947 TREE_TYPE (ret
) = void_type_node
;
33948 OMP_TEAMS_CLAUSES (ret
) = clauses
;
33949 OMP_TEAMS_BODY (ret
) = body
;
33950 OMP_TEAMS_COMBINED (ret
) = 1;
33951 return add_stmt (ret
);
33954 if (!flag_openmp
) /* flag_openmp_simd */
33956 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33960 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33964 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
33965 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
33968 tree stmt
= make_node (OMP_TEAMS
);
33969 TREE_TYPE (stmt
) = void_type_node
;
33970 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
33971 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
33973 return add_stmt (stmt
);
33977 # pragma omp target data target-data-clause[optseq] new-line
33978 structured-block */
33980 #define OMP_TARGET_DATA_CLAUSE_MASK \
33981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
33982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
33983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
33987 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
33990 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
33991 "#pragma omp target data", pragma_tok
);
33993 for (tree
*pc
= &clauses
; *pc
;)
33995 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
33996 switch (OMP_CLAUSE_MAP_KIND (*pc
))
33999 case GOMP_MAP_ALWAYS_TO
:
34000 case GOMP_MAP_FROM
:
34001 case GOMP_MAP_ALWAYS_FROM
:
34002 case GOMP_MAP_TOFROM
:
34003 case GOMP_MAP_ALWAYS_TOFROM
:
34004 case GOMP_MAP_ALLOC
:
34007 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34008 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34009 case GOMP_MAP_ALWAYS_POINTER
:
34013 error_at (OMP_CLAUSE_LOCATION (*pc
),
34014 "%<#pragma omp target data%> with map-type other "
34015 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34016 "on %<map%> clause");
34017 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34020 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34026 error_at (pragma_tok
->location
,
34027 "%<#pragma omp target data%> must contain at least "
34028 "one %<map%> clause");
34032 tree stmt
= make_node (OMP_TARGET_DATA
);
34033 TREE_TYPE (stmt
) = void_type_node
;
34034 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
34036 keep_next_level (true);
34037 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
34039 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34040 return add_stmt (stmt
);
34044 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34045 structured-block */
34047 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34055 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34056 enum pragma_context context
)
34058 bool data_seen
= false;
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
, "data") == 0)
34066 cp_lexer_consume_token (parser
->lexer
);
34072 cp_parser_error (parser
, "expected %<data%>");
34073 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34077 if (context
== pragma_stmt
)
34079 error_at (pragma_tok
->location
,
34080 "%<#pragma omp target enter data%> may only be "
34081 "used in compound statements");
34082 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34087 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
34088 "#pragma omp target enter data", pragma_tok
);
34090 for (tree
*pc
= &clauses
; *pc
;)
34092 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34093 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34096 case GOMP_MAP_ALWAYS_TO
:
34097 case GOMP_MAP_ALLOC
:
34100 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34101 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34102 case GOMP_MAP_ALWAYS_POINTER
:
34106 error_at (OMP_CLAUSE_LOCATION (*pc
),
34107 "%<#pragma omp target enter data%> with map-type other "
34108 "than %<to%> or %<alloc%> on %<map%> clause");
34109 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34112 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34118 error_at (pragma_tok
->location
,
34119 "%<#pragma omp target enter data%> must contain at least "
34120 "one %<map%> clause");
34124 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
34125 TREE_TYPE (stmt
) = void_type_node
;
34126 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
34127 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34128 return add_stmt (stmt
);
34132 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34133 structured-block */
34135 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34143 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34144 enum pragma_context context
)
34146 bool data_seen
= false;
34147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34150 const char *p
= IDENTIFIER_POINTER (id
);
34152 if (strcmp (p
, "data") == 0)
34154 cp_lexer_consume_token (parser
->lexer
);
34160 cp_parser_error (parser
, "expected %<data%>");
34161 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34165 if (context
== pragma_stmt
)
34167 error_at (pragma_tok
->location
,
34168 "%<#pragma omp target exit data%> may only be "
34169 "used in compound statements");
34170 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34175 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
34176 "#pragma omp target exit data", pragma_tok
);
34178 for (tree
*pc
= &clauses
; *pc
;)
34180 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34181 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34183 case GOMP_MAP_FROM
:
34184 case GOMP_MAP_ALWAYS_FROM
:
34185 case GOMP_MAP_RELEASE
:
34186 case GOMP_MAP_DELETE
:
34189 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34190 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34191 case GOMP_MAP_ALWAYS_POINTER
:
34195 error_at (OMP_CLAUSE_LOCATION (*pc
),
34196 "%<#pragma omp target exit data%> with map-type other "
34197 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34199 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34202 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34208 error_at (pragma_tok
->location
,
34209 "%<#pragma omp target exit data%> must contain at least "
34210 "one %<map%> clause");
34214 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
34215 TREE_TYPE (stmt
) = void_type_node
;
34216 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
34217 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34218 return add_stmt (stmt
);
34222 # pragma omp target update target-update-clause[optseq] new-line */
34224 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34225 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34233 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
34234 enum pragma_context context
)
34236 if (context
== pragma_stmt
)
34238 error_at (pragma_tok
->location
,
34239 "%<#pragma omp target update%> may only be "
34240 "used in compound statements");
34241 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34246 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
34247 "#pragma omp target update", pragma_tok
);
34248 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
34249 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
34251 error_at (pragma_tok
->location
,
34252 "%<#pragma omp target update%> must contain at least one "
34253 "%<from%> or %<to%> clauses");
34257 tree stmt
= make_node (OMP_TARGET_UPDATE
);
34258 TREE_TYPE (stmt
) = void_type_node
;
34259 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
34260 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34266 # pragma omp target target-clause[optseq] new-line
34267 structured-block */
34269 #define OMP_TARGET_CLAUSE_MASK \
34270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34281 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
34282 enum pragma_context context
)
34284 tree
*pc
= NULL
, stmt
;
34286 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
34288 cp_parser_error (parser
, "expected declaration specifiers");
34289 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34295 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34296 const char *p
= IDENTIFIER_POINTER (id
);
34297 enum tree_code ccode
= ERROR_MARK
;
34299 if (strcmp (p
, "teams") == 0)
34301 else if (strcmp (p
, "parallel") == 0)
34302 ccode
= OMP_PARALLEL
;
34303 else if (strcmp (p
, "simd") == 0)
34305 if (ccode
!= ERROR_MARK
)
34307 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
34308 char p_name
[sizeof ("#pragma omp target teams distribute "
34309 "parallel for simd")];
34311 cp_lexer_consume_token (parser
->lexer
);
34312 strcpy (p_name
, "#pragma omp target");
34313 if (!flag_openmp
) /* flag_openmp_simd */
34319 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34320 OMP_TARGET_CLAUSE_MASK
,
34324 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34325 OMP_TARGET_CLAUSE_MASK
,
34329 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34330 OMP_TARGET_CLAUSE_MASK
,
34334 gcc_unreachable ();
34336 return stmt
!= NULL_TREE
;
34338 keep_next_level (true);
34339 tree sb
= begin_omp_structured_block (), ret
;
34340 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
34344 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34345 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34348 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34349 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34352 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34353 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34356 gcc_unreachable ();
34358 cp_parser_end_omp_structured_block (parser
, save
);
34359 tree body
= finish_omp_structured_block (sb
);
34360 if (ret
== NULL_TREE
)
34362 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
34364 /* For combined target teams, ensure the num_teams and
34365 thread_limit clause expressions are evaluated on the host,
34366 before entering the target construct. */
34368 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
34369 c
; c
= OMP_CLAUSE_CHAIN (c
))
34370 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
34371 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
34372 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
34374 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
34375 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
34376 if (expr
== error_mark_node
)
34378 tree tmp
= TARGET_EXPR_SLOT (expr
);
34380 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
34381 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
34382 OMP_CLAUSE_FIRSTPRIVATE
);
34383 OMP_CLAUSE_DECL (tc
) = tmp
;
34384 OMP_CLAUSE_CHAIN (tc
)
34385 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
34386 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
34389 tree stmt
= make_node (OMP_TARGET
);
34390 TREE_TYPE (stmt
) = void_type_node
;
34391 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
34392 OMP_TARGET_BODY (stmt
) = body
;
34393 OMP_TARGET_COMBINED (stmt
) = 1;
34395 pc
= &OMP_TARGET_CLAUSES (stmt
);
34396 goto check_clauses
;
34398 else if (!flag_openmp
) /* flag_openmp_simd */
34400 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34403 else if (strcmp (p
, "data") == 0)
34405 cp_lexer_consume_token (parser
->lexer
);
34406 cp_parser_omp_target_data (parser
, pragma_tok
);
34409 else if (strcmp (p
, "enter") == 0)
34411 cp_lexer_consume_token (parser
->lexer
);
34412 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
34415 else if (strcmp (p
, "exit") == 0)
34417 cp_lexer_consume_token (parser
->lexer
);
34418 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
34421 else if (strcmp (p
, "update") == 0)
34423 cp_lexer_consume_token (parser
->lexer
);
34424 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
34428 stmt
= make_node (OMP_TARGET
);
34429 TREE_TYPE (stmt
) = void_type_node
;
34431 OMP_TARGET_CLAUSES (stmt
)
34432 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
34433 "#pragma omp target", pragma_tok
);
34434 pc
= &OMP_TARGET_CLAUSES (stmt
);
34435 keep_next_level (true);
34436 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
34438 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34444 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34445 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34448 case GOMP_MAP_ALWAYS_TO
:
34449 case GOMP_MAP_FROM
:
34450 case GOMP_MAP_ALWAYS_FROM
:
34451 case GOMP_MAP_TOFROM
:
34452 case GOMP_MAP_ALWAYS_TOFROM
:
34453 case GOMP_MAP_ALLOC
:
34454 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34455 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34456 case GOMP_MAP_ALWAYS_POINTER
:
34459 error_at (OMP_CLAUSE_LOCATION (*pc
),
34460 "%<#pragma omp target%> with map-type other "
34461 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34462 "on %<map%> clause");
34463 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34466 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34472 # pragma acc cache (variable-list) new-line
34476 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
34478 tree stmt
, clauses
;
34480 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
34481 clauses
= finish_omp_clauses (clauses
, false);
34483 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
34485 stmt
= make_node (OACC_CACHE
);
34486 TREE_TYPE (stmt
) = void_type_node
;
34487 OACC_CACHE_CLAUSES (stmt
) = clauses
;
34488 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34495 # pragma acc data oacc-data-clause[optseq] new-line
34496 structured-block */
34498 #define OACC_DATA_CLAUSE_MASK \
34499 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34512 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
34514 tree stmt
, clauses
, block
;
34517 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
34518 "#pragma acc data", pragma_tok
);
34520 block
= begin_omp_parallel ();
34521 save
= cp_parser_begin_omp_structured_block (parser
);
34522 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
34523 cp_parser_end_omp_structured_block (parser
, save
);
34524 stmt
= finish_oacc_data (clauses
, block
);
34529 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
34533 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
34535 LOC is the location of the #pragma token.
34538 #define OACC_ENTER_DATA_CLAUSE_MASK \
34539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34547 #define OACC_EXIT_DATA_CLAUSE_MASK \
34548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
34552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34555 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34558 tree stmt
, clauses
;
34560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
34561 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34563 cp_parser_error (parser
, enter
34564 ? "expected %<data%> in %<#pragma acc enter data%>"
34565 : "expected %<data%> in %<#pragma acc exit data%>");
34566 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34571 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
34572 if (strcmp (p
, "data") != 0)
34574 cp_parser_error (parser
, "invalid pragma");
34575 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34579 cp_lexer_consume_token (parser
->lexer
);
34582 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
34583 "#pragma acc enter data", pragma_tok
);
34585 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
34586 "#pragma acc exit data", pragma_tok
);
34588 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
34590 error_at (pragma_tok
->location
,
34591 "%<#pragma acc enter data%> has no data movement clause");
34595 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
34596 TREE_TYPE (stmt
) = void_type_node
;
34597 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
34598 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34604 # pragma acc loop oacc-loop-clause[optseq] new-line
34605 structured-block */
34607 #define OACC_LOOP_CLAUSE_MASK \
34608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
34609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
34612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
34613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
34614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
34615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
34616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
34617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
34620 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
34621 omp_clause_mask mask
, tree
*cclauses
)
34623 strcat (p_name
, " loop");
34624 mask
|= OACC_LOOP_CLAUSE_MASK
;
34626 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34630 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
);
34632 finish_omp_clauses (*cclauses
, false);
34634 finish_omp_clauses (clauses
, false);
34637 tree block
= begin_omp_structured_block ();
34638 int save
= cp_parser_begin_omp_structured_block (parser
);
34639 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
34640 cp_parser_end_omp_structured_block (parser
, save
);
34641 add_stmt (finish_omp_structured_block (block
));
34647 # pragma acc kernels oacc-kernels-clause[optseq] new-line
34652 # pragma acc parallel oacc-parallel-clause[optseq] new-line
34656 #define OACC_KERNELS_CLAUSE_MASK \
34657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34672 #define OACC_PARALLEL_CLAUSE_MASK \
34673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
34681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
34683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
34684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
34692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34695 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
34698 omp_clause_mask mask
;
34699 enum tree_code code
;
34700 switch (pragma_tok
->pragma_kind
)
34702 case PRAGMA_OACC_KERNELS
:
34703 strcat (p_name
, " kernels");
34704 mask
= OACC_KERNELS_CLAUSE_MASK
;
34705 code
= OACC_KERNELS
;
34707 case PRAGMA_OACC_PARALLEL
:
34708 strcat (p_name
, " parallel");
34709 mask
= OACC_PARALLEL_CLAUSE_MASK
;
34710 code
= OACC_PARALLEL
;
34713 gcc_unreachable ();
34716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34719 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
34720 if (strcmp (p
, "loop") == 0)
34722 cp_lexer_consume_token (parser
->lexer
);
34723 mask
|= OACC_LOOP_CLAUSE_MASK
;
34725 tree block
= begin_omp_parallel ();
34727 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
);
34728 return finish_omp_construct (code
, block
, clauses
);
34732 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
34734 tree block
= begin_omp_parallel ();
34735 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34736 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
34737 cp_parser_end_omp_structured_block (parser
, save
);
34738 return finish_omp_construct (code
, block
, clauses
);
34742 # pragma acc update oacc-update-clause[optseq] new-line
34745 #define OACC_UPDATE_CLAUSE_MASK \
34746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
34748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
34749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
34751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
34754 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
34756 tree stmt
, clauses
;
34758 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
34759 "#pragma acc update", pragma_tok
);
34761 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
34763 error_at (pragma_tok
->location
,
34764 "%<#pragma acc update%> must contain at least one "
34765 "%<device%> or %<host%> or %<self%> clause");
34769 stmt
= make_node (OACC_UPDATE
);
34770 TREE_TYPE (stmt
) = void_type_node
;
34771 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
34772 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34778 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
34780 LOC is the location of the #pragma token.
34783 #define OACC_WAIT_CLAUSE_MASK \
34784 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
34787 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
34789 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
34790 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34792 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34793 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
34795 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
34796 "#pragma acc wait", pragma_tok
);
34798 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
34804 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
34806 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
34807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
34815 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
34816 enum pragma_context context
)
34818 bool first_p
= parser
->omp_declare_simd
== NULL
;
34819 cp_omp_declare_simd_data data
;
34822 data
.error_seen
= false;
34823 data
.fndecl_seen
= false;
34824 data
.tokens
= vNULL
;
34825 parser
->omp_declare_simd
= &data
;
34827 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
34828 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
34829 cp_lexer_consume_token (parser
->lexer
);
34830 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34831 parser
->omp_declare_simd
->error_seen
= true;
34832 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34833 struct cp_token_cache
*cp
34834 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
34835 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
34838 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
34839 cp_parser_pragma (parser
, context
);
34842 case pragma_external
:
34843 cp_parser_declaration (parser
);
34845 case pragma_member
:
34846 cp_parser_member_declaration (parser
);
34848 case pragma_objc_icode
:
34849 cp_parser_block_declaration (parser
, /*statement_p=*/false);
34852 cp_parser_declaration_statement (parser
);
34855 if (parser
->omp_declare_simd
34856 && !parser
->omp_declare_simd
->error_seen
34857 && !parser
->omp_declare_simd
->fndecl_seen
)
34858 error_at (pragma_tok
->location
,
34859 "%<#pragma omp declare simd%> not immediately followed by "
34860 "function declaration or definition");
34861 data
.tokens
.release ();
34862 parser
->omp_declare_simd
= NULL
;
34866 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
34867 This function is modelled similar to the late parsing of omp declare
34871 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
34873 struct cp_token_cache
*ce
;
34874 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
34877 if (parser
->omp_declare_simd
!= NULL
)
34879 error ("%<#pragma omp declare simd%> cannot be used in the same function"
34880 " marked as a Cilk Plus SIMD-enabled function");
34881 XDELETE (parser
->cilk_simd_fn_info
);
34882 parser
->cilk_simd_fn_info
= NULL
;
34885 if (!info
->error_seen
&& info
->fndecl_seen
)
34887 error ("vector attribute not immediately followed by a single function"
34888 " declaration or definition");
34889 info
->error_seen
= true;
34891 if (info
->error_seen
)
34894 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
34898 cp_parser_push_lexer_for_tokens (parser
, ce
);
34899 parser
->lexer
->in_pragma
= true;
34900 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
34901 "SIMD-enabled functions attribute",
34903 cp_parser_pop_lexer (parser
);
34905 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
34907 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
34908 TREE_CHAIN (c
) = attrs
;
34911 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
34912 TREE_CHAIN (c
) = attrs
;
34913 if (processing_template_decl
)
34914 ATTR_IS_DEPENDENT (c
) = 1;
34917 info
->fndecl_seen
= true;
34918 XDELETE (parser
->cilk_simd_fn_info
);
34919 parser
->cilk_simd_fn_info
= NULL
;
34923 /* Finalize #pragma omp declare simd clauses after direct declarator has
34924 been parsed, and put that into "omp declare simd" attribute. */
34927 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
34929 struct cp_token_cache
*ce
;
34930 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
34933 if (!data
->error_seen
&& data
->fndecl_seen
)
34935 error ("%<#pragma omp declare simd%> not immediately followed by "
34936 "a single function declaration or definition");
34937 data
->error_seen
= true;
34940 if (data
->error_seen
)
34943 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
34947 cp_parser_push_lexer_for_tokens (parser
, ce
);
34948 parser
->lexer
->in_pragma
= true;
34949 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
34950 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
34951 cp_lexer_consume_token (parser
->lexer
);
34952 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
34953 "#pragma omp declare simd", pragma_tok
);
34954 cp_parser_pop_lexer (parser
);
34956 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
34957 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
34958 TREE_CHAIN (c
) = attrs
;
34959 if (processing_template_decl
)
34960 ATTR_IS_DEPENDENT (c
) = 1;
34964 data
->fndecl_seen
= true;
34970 # pragma omp declare target new-line
34971 declarations and definitions
34972 # pragma omp end declare target new-line
34975 # pragma omp declare target ( extended-list ) new-line
34977 # pragma omp declare target declare-target-clauses[seq] new-line */
34979 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
34980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
34984 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
34986 tree clauses
= NULL_TREE
;
34987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34989 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
34990 "#pragma omp declare target", pragma_tok
);
34991 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34993 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34995 clauses
= finish_omp_clauses (clauses
, true);
34996 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35000 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35001 scope_chain
->omp_declare_target_attribute
++;
35004 if (scope_chain
->omp_declare_target_attribute
)
35005 error_at (pragma_tok
->location
,
35006 "%<#pragma omp declare target%> with clauses in between "
35007 "%<#pragma omp declare target%> without clauses and "
35008 "%<#pragma omp end declare target%>");
35009 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35011 tree t
= OMP_CLAUSE_DECL (c
), id
;
35012 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
35013 tree at2
= lookup_attribute ("omp declare target link",
35014 DECL_ATTRIBUTES (t
));
35015 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
35017 id
= get_identifier ("omp declare target link");
35018 std::swap (at1
, at2
);
35021 id
= get_identifier ("omp declare target");
35024 error_at (OMP_CLAUSE_LOCATION (c
),
35025 "%qD specified both in declare target %<link%> and %<to%>"
35031 symtab_node
*node
= symtab_node::get (t
);
35032 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
35035 node
->offloadable
= 1;
35036 #ifdef ENABLE_OFFLOADING
35037 g
->have_offload
= true;
35038 if (is_a
<varpool_node
*> (node
))
35040 vec_safe_push (offload_vars
, t
);
35041 node
->force_output
= 1;
35050 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
35052 const char *p
= "";
35053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35055 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35056 p
= IDENTIFIER_POINTER (id
);
35058 if (strcmp (p
, "declare") == 0)
35060 cp_lexer_consume_token (parser
->lexer
);
35062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35064 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35065 p
= IDENTIFIER_POINTER (id
);
35067 if (strcmp (p
, "target") == 0)
35068 cp_lexer_consume_token (parser
->lexer
);
35071 cp_parser_error (parser
, "expected %<target%>");
35072 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35078 cp_parser_error (parser
, "expected %<declare%>");
35079 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35082 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35083 if (!scope_chain
->omp_declare_target_attribute
)
35084 error_at (pragma_tok
->location
,
35085 "%<#pragma omp end declare target%> without corresponding "
35086 "%<#pragma omp declare target%>");
35088 scope_chain
->omp_declare_target_attribute
--;
35091 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35092 expression and optional initializer clause of
35093 #pragma omp declare reduction. We store the expression(s) as
35094 either 3, 6 or 7 special statements inside of the artificial function's
35095 body. The first two statements are DECL_EXPRs for the artificial
35096 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35097 expression that uses those variables.
35098 If there was any INITIALIZER clause, this is followed by further statements,
35099 the fourth and fifth statements are DECL_EXPRs for the artificial
35100 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35101 constructor variant (first token after open paren is not omp_priv),
35102 then the sixth statement is a statement with the function call expression
35103 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35104 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35105 to initialize the OMP_PRIV artificial variable and there is seventh
35106 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35109 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
35111 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
35112 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
35113 type
= TREE_TYPE (type
);
35114 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
35115 DECL_ARTIFICIAL (omp_out
) = 1;
35116 pushdecl (omp_out
);
35117 add_decl_expr (omp_out
);
35118 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
35119 DECL_ARTIFICIAL (omp_in
) = 1;
35121 add_decl_expr (omp_in
);
35123 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
35125 keep_next_level (true);
35126 tree block
= begin_omp_structured_block ();
35127 combiner
= cp_parser_expression (parser
);
35128 finish_expr_stmt (combiner
);
35129 block
= finish_omp_structured_block (block
);
35132 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35135 const char *p
= "";
35136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35138 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35139 p
= IDENTIFIER_POINTER (id
);
35142 if (strcmp (p
, "initializer") == 0)
35144 cp_lexer_consume_token (parser
->lexer
);
35145 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35151 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35152 p
= IDENTIFIER_POINTER (id
);
35155 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
35156 DECL_ARTIFICIAL (omp_priv
) = 1;
35157 pushdecl (omp_priv
);
35158 add_decl_expr (omp_priv
);
35159 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
35160 DECL_ARTIFICIAL (omp_orig
) = 1;
35161 pushdecl (omp_orig
);
35162 add_decl_expr (omp_orig
);
35164 keep_next_level (true);
35165 block
= begin_omp_structured_block ();
35168 if (strcmp (p
, "omp_priv") == 0)
35170 bool is_direct_init
, is_non_constant_init
;
35172 cp_lexer_consume_token (parser
->lexer
);
35173 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
35175 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
35176 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
35178 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
35179 == CPP_CLOSE_PAREN
))
35181 finish_omp_structured_block (block
);
35182 error ("invalid initializer clause");
35185 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
35186 &is_non_constant_init
);
35187 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
35188 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
35192 cp_parser_parse_tentatively (parser
);
35193 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35194 /*check_dependency_p=*/true,
35195 /*template_p=*/NULL
,
35196 /*declarator_p=*/false,
35197 /*optional_p=*/false);
35198 vec
<tree
, va_gc
> *args
;
35199 if (fn_name
== error_mark_node
35200 || cp_parser_error_occurred (parser
)
35201 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
35202 || ((args
= cp_parser_parenthesized_expression_list
35203 (parser
, non_attr
, /*cast_p=*/false,
35204 /*allow_expansion_p=*/true,
35205 /*non_constant_p=*/NULL
)),
35206 cp_parser_error_occurred (parser
)))
35208 finish_omp_structured_block (block
);
35209 cp_parser_abort_tentative_parse (parser
);
35210 cp_parser_error (parser
, "expected id-expression (arguments)");
35215 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
35216 if (arg
== omp_priv
35217 || (TREE_CODE (arg
) == ADDR_EXPR
35218 && TREE_OPERAND (arg
, 0) == omp_priv
))
35220 cp_parser_abort_tentative_parse (parser
);
35221 if (arg
== NULL_TREE
)
35222 error ("one of the initializer call arguments should be %<omp_priv%>"
35223 " or %<&omp_priv%>");
35224 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
35226 finish_expr_stmt (initializer
);
35229 block
= finish_omp_structured_block (block
);
35230 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
35234 add_decl_expr (omp_orig
);
35236 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35240 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
35241 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
35247 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35248 initializer-clause[opt] new-line
35250 initializer-clause:
35251 initializer (omp_priv initializer)
35252 initializer (function-name (argument-list)) */
35255 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
35256 enum pragma_context
)
35258 auto_vec
<tree
> types
;
35259 enum tree_code reduc_code
= ERROR_MARK
;
35260 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
35262 cp_token
*first_token
;
35263 cp_token_cache
*cp
;
35267 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35268 p
= obstack_alloc (&declarator_obstack
, 0);
35270 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35273 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35276 reduc_code
= PLUS_EXPR
;
35279 reduc_code
= MULT_EXPR
;
35282 reduc_code
= MINUS_EXPR
;
35285 reduc_code
= BIT_AND_EXPR
;
35288 reduc_code
= BIT_XOR_EXPR
;
35291 reduc_code
= BIT_IOR_EXPR
;
35294 reduc_code
= TRUTH_ANDIF_EXPR
;
35297 reduc_code
= TRUTH_ORIF_EXPR
;
35300 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
35303 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
35304 "%<|%>, %<&&%>, %<||%> or identifier");
35308 if (reduc_code
!= ERROR_MARK
)
35309 cp_lexer_consume_token (parser
->lexer
);
35311 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
35312 if (reduc_id
== error_mark_node
)
35315 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35318 /* Types may not be defined in declare reduction type list. */
35319 const char *saved_message
;
35320 saved_message
= parser
->type_definition_forbidden_message
;
35321 parser
->type_definition_forbidden_message
35322 = G_("types may not be defined in declare reduction type list");
35323 bool saved_colon_corrects_to_scope_p
;
35324 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
35325 parser
->colon_corrects_to_scope_p
= false;
35326 bool saved_colon_doesnt_start_class_def_p
;
35327 saved_colon_doesnt_start_class_def_p
35328 = parser
->colon_doesnt_start_class_def_p
;
35329 parser
->colon_doesnt_start_class_def_p
= true;
35333 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35334 type
= cp_parser_type_id (parser
);
35335 if (type
== error_mark_node
)
35337 else if (ARITHMETIC_TYPE_P (type
)
35338 && (orig_reduc_id
== NULL_TREE
35339 || (TREE_CODE (type
) != COMPLEX_TYPE
35340 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
35342 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
35344 error_at (loc
, "predeclared arithmetic type %qT in "
35345 "%<#pragma omp declare reduction%>", type
);
35346 else if (TREE_CODE (type
) == FUNCTION_TYPE
35347 || TREE_CODE (type
) == METHOD_TYPE
35348 || TREE_CODE (type
) == ARRAY_TYPE
)
35349 error_at (loc
, "function or array type %qT in "
35350 "%<#pragma omp declare reduction%>", type
);
35351 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
35352 error_at (loc
, "reference type %qT in "
35353 "%<#pragma omp declare reduction%>", type
);
35354 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
35355 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
35356 "%<#pragma omp declare reduction%>", type
);
35358 types
.safe_push (type
);
35360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35361 cp_lexer_consume_token (parser
->lexer
);
35366 /* Restore the saved message. */
35367 parser
->type_definition_forbidden_message
= saved_message
;
35368 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35369 parser
->colon_doesnt_start_class_def_p
35370 = saved_colon_doesnt_start_class_def_p
;
35372 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
35373 || types
.is_empty ())
35376 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35380 first_token
= cp_lexer_peek_token (parser
->lexer
);
35383 FOR_EACH_VEC_ELT (types
, i
, type
)
35386 = build_function_type_list (void_type_node
,
35387 cp_build_reference_type (type
, false),
35389 tree this_reduc_id
= reduc_id
;
35390 if (!dependent_type_p (type
))
35391 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
35392 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
35393 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
35394 DECL_ARTIFICIAL (fndecl
) = 1;
35395 DECL_EXTERNAL (fndecl
) = 1;
35396 DECL_DECLARED_INLINE_P (fndecl
) = 1;
35397 DECL_IGNORED_P (fndecl
) = 1;
35398 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
35399 DECL_ATTRIBUTES (fndecl
)
35400 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
35401 DECL_ATTRIBUTES (fndecl
));
35402 if (processing_template_decl
)
35403 fndecl
= push_template_decl (fndecl
);
35404 bool block_scope
= false;
35405 tree block
= NULL_TREE
;
35406 if (current_function_decl
)
35408 block_scope
= true;
35409 DECL_CONTEXT (fndecl
) = global_namespace
;
35410 if (!processing_template_decl
)
35413 else if (current_class_type
)
35417 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
35418 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
35419 cp_lexer_consume_token (parser
->lexer
);
35420 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35422 cp
= cp_token_cache_new (first_token
,
35423 cp_lexer_peek_nth_token (parser
->lexer
,
35426 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
35427 finish_member_declaration (fndecl
);
35428 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
35429 DECL_PENDING_INLINE_P (fndecl
) = 1;
35430 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
35435 DECL_CONTEXT (fndecl
) = current_namespace
;
35439 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
35441 block
= begin_omp_structured_block ();
35444 cp_parser_push_lexer_for_tokens (parser
, cp
);
35445 parser
->lexer
->in_pragma
= true;
35447 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
35450 finish_function (0);
35452 DECL_CONTEXT (fndecl
) = current_function_decl
;
35454 cp_parser_pop_lexer (parser
);
35458 cp_parser_pop_lexer (parser
);
35460 finish_function (0);
35463 DECL_CONTEXT (fndecl
) = current_function_decl
;
35464 block
= finish_omp_structured_block (block
);
35465 if (TREE_CODE (block
) == BIND_EXPR
)
35466 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
35467 else if (TREE_CODE (block
) == STATEMENT_LIST
)
35468 DECL_SAVED_TREE (fndecl
) = block
;
35469 if (processing_template_decl
)
35470 add_decl_expr (fndecl
);
35472 cp_check_omp_declare_reduction (fndecl
);
35473 if (cp
== NULL
&& types
.length () > 1)
35474 cp
= cp_token_cache_new (first_token
,
35475 cp_lexer_peek_nth_token (parser
->lexer
, 2));
35476 if (errs
!= errorcount
)
35480 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35483 /* Free any declarators allocated. */
35484 obstack_free (&declarator_obstack
, p
);
35488 #pragma omp declare simd declare-simd-clauses[optseq] new-line
35489 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35490 initializer-clause[opt] new-line
35491 #pragma omp declare target new-line */
35494 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
35495 enum pragma_context context
)
35497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35499 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35500 const char *p
= IDENTIFIER_POINTER (id
);
35502 if (strcmp (p
, "simd") == 0)
35504 cp_lexer_consume_token (parser
->lexer
);
35505 cp_parser_omp_declare_simd (parser
, pragma_tok
,
35509 cp_ensure_no_omp_declare_simd (parser
);
35510 if (strcmp (p
, "reduction") == 0)
35512 cp_lexer_consume_token (parser
->lexer
);
35513 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
35517 if (!flag_openmp
) /* flag_openmp_simd */
35519 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35522 if (strcmp (p
, "target") == 0)
35524 cp_lexer_consume_token (parser
->lexer
);
35525 cp_parser_omp_declare_target (parser
, pragma_tok
);
35529 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
35531 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35535 #pragma omp taskloop taskloop-clause[optseq] new-line
35538 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
35541 #define OMP_TASKLOOP_CLAUSE_MASK \
35542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35558 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
35559 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35561 tree clauses
, sb
, ret
;
35563 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35565 strcat (p_name
, " taskloop");
35566 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
35568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35570 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35571 const char *p
= IDENTIFIER_POINTER (id
);
35573 if (strcmp (p
, "simd") == 0)
35575 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35576 if (cclauses
== NULL
)
35577 cclauses
= cclauses_buf
;
35579 cp_lexer_consume_token (parser
->lexer
);
35580 if (!flag_openmp
) /* flag_openmp_simd */
35581 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35583 sb
= begin_omp_structured_block ();
35584 save
= cp_parser_begin_omp_structured_block (parser
);
35585 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35587 cp_parser_end_omp_structured_block (parser
, save
);
35588 tree body
= finish_omp_structured_block (sb
);
35591 ret
= make_node (OMP_TASKLOOP
);
35592 TREE_TYPE (ret
) = void_type_node
;
35593 OMP_FOR_BODY (ret
) = body
;
35594 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
35595 SET_EXPR_LOCATION (ret
, loc
);
35600 if (!flag_openmp
) /* flag_openmp_simd */
35602 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35606 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35610 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
35611 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
35614 sb
= begin_omp_structured_block ();
35615 save
= cp_parser_begin_omp_structured_block (parser
);
35617 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
);
35619 cp_parser_end_omp_structured_block (parser
, save
);
35620 add_stmt (finish_omp_structured_block (sb
));
35627 # pragma acc routine oacc-routine-clause[optseq] new-line
35628 function-definition
35630 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
35633 #define OACC_ROUTINE_CLAUSE_MASK \
35634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
35639 /* Finalize #pragma acc routine clauses after direct declarator has
35640 been parsed, and put that into "omp declare target" attribute. */
35643 cp_parser_finish_oacc_routine (cp_parser
*ARG_UNUSED (parser
), tree fndecl
,
35644 tree clauses
, bool named
, bool is_defn
,
35647 location_t loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses
));
35649 if (named
&& fndecl
&& is_overloaded_fn (fndecl
)
35650 && (TREE_CODE (fndecl
) != FUNCTION_DECL
35651 || DECL_FUNCTION_TEMPLATE_P (fndecl
)))
35653 error_at (loc
, "%<#pragma acc routine%> names a set of overloads");
35657 if (!fndecl
|| TREE_CODE (fndecl
) != FUNCTION_DECL
35658 || (!named
&& !first
))
35660 error_at (loc
, "%<#pragma acc routine%> %s",
35661 named
? "does not refer to a function"
35662 : "not followed by single function");
35666 /* Perhaps we should use the same rule as declarations in different
35668 if (named
&& !DECL_NAMESPACE_SCOPE_P (fndecl
))
35670 error_at (loc
, "%<#pragma acc routine%> does not refer to a"
35671 " namespace scope function");
35675 if (get_oacc_fn_attrib (fndecl
))
35676 error_at (loc
, "%<#pragma acc routine%> already applied to %D", fndecl
);
35678 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
35679 error_at (OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses
)),
35680 "%<#pragma acc routine%> must be applied before %s",
35681 TREE_USED (fndecl
) ? "use" : "definition");
35683 /* Process for function attrib */
35684 tree dims
= build_oacc_routine_dims (TREE_VALUE (clauses
));
35685 replace_oacc_fn_attrib (fndecl
, dims
);
35687 /* Also attach as a declare. */
35688 DECL_ATTRIBUTES (fndecl
)
35689 = tree_cons (get_identifier ("omp declare target"),
35690 clauses
, DECL_ATTRIBUTES (fndecl
));
35693 /* Parse the OpenACC routine pragma. This has an optional '( name )'
35694 component, which must resolve to a declared namespace-scope
35695 function. The clauses are either processed directly (for a named
35696 function), or defered until the immediatley following declaration
35700 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
35701 enum pragma_context context
)
35703 tree decl
= NULL_TREE
;
35704 /* Create a dummy claue, to record location. */
35705 tree c_head
= build_omp_clause (pragma_tok
->location
, OMP_CLAUSE_SEQ
);
35707 if (context
!= pragma_external
)
35708 cp_parser_error (parser
, "%<#pragma acc routine%> not at file scope");
35710 /* Look for optional '( name )'. */
35711 if (cp_lexer_next_token_is (parser
->lexer
,CPP_OPEN_PAREN
))
35713 cp_lexer_consume_token (parser
->lexer
);
35714 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35716 /* We parse the name as an id-expression. If it resolves to
35717 anything other than a non-overloaded function at namespace
35718 scope, it's an error. */
35719 tree id
= cp_parser_id_expression (parser
,
35720 /*template_keyword_p=*/false,
35721 /*check_dependency_p=*/false,
35722 /*template_p=*/NULL
,
35723 /*declarator_p=*/false,
35724 /*optional_p=*/false);
35725 decl
= cp_parser_lookup_name_simple (parser
, id
, token
->location
);
35726 if (id
!= error_mark_node
&& decl
== error_mark_node
)
35727 cp_parser_name_lookup_error (parser
, id
, decl
, NLE_NULL
,
35730 if (decl
== error_mark_node
35731 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35733 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35738 /* Build a chain of clauses. */
35739 parser
->lexer
->in_pragma
= true;
35740 tree clauses
= NULL_TREE
;
35741 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
35742 "#pragma acc routine",
35743 cp_lexer_peek_token (parser
->lexer
));
35745 /* Force clauses to be non-null, by attaching context to it. */
35746 clauses
= tree_cons (c_head
, clauses
, NULL_TREE
);
35749 cp_parser_finish_oacc_routine (parser
, decl
, clauses
, true, false, 0);
35751 parser
->oacc_routine
= clauses
;
35754 /* Apply any saved OpenACC routine clauses to a just-parsed
35758 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
,
35761 if (parser
->oacc_routine
)
35762 cp_parser_finish_oacc_routine (parser
, fndecl
, parser
->oacc_routine
,
35763 false, is_defn
, first
);
35766 /* Main entry point to OpenMP statement pragmas. */
35769 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
35772 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
35773 omp_clause_mask
mask (0);
35775 switch (pragma_tok
->pragma_kind
)
35777 case PRAGMA_OACC_ATOMIC
:
35778 cp_parser_omp_atomic (parser
, pragma_tok
);
35780 case PRAGMA_OACC_CACHE
:
35781 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
35783 case PRAGMA_OACC_DATA
:
35784 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
35786 case PRAGMA_OACC_ENTER_DATA
:
35787 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
35789 case PRAGMA_OACC_EXIT_DATA
:
35790 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
35792 case PRAGMA_OACC_KERNELS
:
35793 case PRAGMA_OACC_PARALLEL
:
35794 strcpy (p_name
, "#pragma acc");
35795 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
);
35797 case PRAGMA_OACC_LOOP
:
35798 strcpy (p_name
, "#pragma acc");
35799 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
);
35801 case PRAGMA_OACC_UPDATE
:
35802 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
35804 case PRAGMA_OACC_WAIT
:
35805 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
35807 case PRAGMA_OMP_ATOMIC
:
35808 cp_parser_omp_atomic (parser
, pragma_tok
);
35810 case PRAGMA_OMP_CRITICAL
:
35811 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
35813 case PRAGMA_OMP_DISTRIBUTE
:
35814 strcpy (p_name
, "#pragma omp");
35815 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
35817 case PRAGMA_OMP_FOR
:
35818 strcpy (p_name
, "#pragma omp");
35819 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
35821 case PRAGMA_OMP_MASTER
:
35822 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
35824 case PRAGMA_OMP_PARALLEL
:
35825 strcpy (p_name
, "#pragma omp");
35826 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
35828 case PRAGMA_OMP_SECTIONS
:
35829 strcpy (p_name
, "#pragma omp");
35830 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
35832 case PRAGMA_OMP_SIMD
:
35833 strcpy (p_name
, "#pragma omp");
35834 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
35836 case PRAGMA_OMP_SINGLE
:
35837 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
35839 case PRAGMA_OMP_TASK
:
35840 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
35842 case PRAGMA_OMP_TASKGROUP
:
35843 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
35845 case PRAGMA_OMP_TASKLOOP
:
35846 strcpy (p_name
, "#pragma omp");
35847 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
);
35849 case PRAGMA_OMP_TEAMS
:
35850 strcpy (p_name
, "#pragma omp");
35851 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
35854 gcc_unreachable ();
35857 protected_set_expr_location (stmt
, pragma_tok
->location
);
35860 /* Transactional Memory parsing routines. */
35862 /* Parse a transaction attribute.
35868 We use this instead of cp_parser_attributes_opt for transactions to avoid
35869 the pedwarn in C++98 mode. */
35872 cp_parser_txn_attribute_opt (cp_parser
*parser
)
35875 tree attr_name
, attr
= NULL
;
35877 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
35878 return cp_parser_attributes_opt (parser
);
35880 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
35882 cp_lexer_consume_token (parser
->lexer
);
35883 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
35886 token
= cp_lexer_peek_token (parser
->lexer
);
35887 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
35889 token
= cp_lexer_consume_token (parser
->lexer
);
35891 attr_name
= (token
->type
== CPP_KEYWORD
35892 /* For keywords, use the canonical spelling,
35893 not the parsed identifier. */
35894 ? ridpointers
[(int) token
->keyword
]
35896 attr
= build_tree_list (attr_name
, NULL_TREE
);
35899 cp_parser_error (parser
, "expected identifier");
35901 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
35903 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
35907 /* Parse a __transaction_atomic or __transaction_relaxed statement.
35909 transaction-statement:
35910 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
35912 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
35916 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
35918 unsigned char old_in
= parser
->in_transaction
;
35919 unsigned char this_in
= 1, new_in
;
35920 enum rid keyword
= token
->keyword
;
35921 tree stmt
, attrs
, noex
;
35923 cp_lexer_consume_token (parser
->lexer
);
35925 if (keyword
== RID_TRANSACTION_RELAXED
35926 || keyword
== RID_SYNCHRONIZED
)
35927 this_in
|= TM_STMT_ATTR_RELAXED
;
35930 attrs
= cp_parser_txn_attribute_opt (parser
);
35932 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
35935 /* Parse a noexcept specification. */
35936 if (keyword
== RID_ATOMIC_NOEXCEPT
)
35937 noex
= boolean_true_node
;
35938 else if (keyword
== RID_ATOMIC_CANCEL
)
35940 /* cancel-and-throw is unimplemented. */
35941 sorry ("atomic_cancel");
35945 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
35947 /* Keep track if we're in the lexical scope of an outer transaction. */
35948 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
35950 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
35952 parser
->in_transaction
= new_in
;
35953 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
35954 parser
->in_transaction
= old_in
;
35956 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
35961 /* Parse a __transaction_atomic or __transaction_relaxed expression.
35963 transaction-expression:
35964 __transaction_atomic txn-noexcept-spec[opt] ( expression )
35965 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
35969 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
35971 unsigned char old_in
= parser
->in_transaction
;
35972 unsigned char this_in
= 1;
35977 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
35978 || keyword
== RID_TRANSACTION_RELAXED
);
35981 error (keyword
== RID_TRANSACTION_RELAXED
35982 ? G_("%<__transaction_relaxed%> without transactional memory "
35984 : G_("%<__transaction_atomic%> without transactional memory "
35985 "support enabled"));
35987 token
= cp_parser_require_keyword (parser
, keyword
,
35988 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
35989 : RT_TRANSACTION_RELAXED
));
35990 gcc_assert (token
!= NULL
);
35992 if (keyword
== RID_TRANSACTION_RELAXED
)
35993 this_in
|= TM_STMT_ATTR_RELAXED
;
35995 /* Set this early. This might mean that we allow transaction_cancel in
35996 an expression that we find out later actually has to be a constexpr.
35997 However, we expect that cxx_constant_value will be able to deal with
35998 this; also, if the noexcept has no constexpr, then what we parse next
35999 really is a transaction's body. */
36000 parser
->in_transaction
= this_in
;
36002 /* Parse a noexcept specification. */
36003 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
36006 if (!noex
|| !noex_expr
36007 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36009 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
36011 expr
= cp_parser_expression (parser
);
36012 expr
= finish_parenthesized_expr (expr
);
36014 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
36018 /* The only expression that is available got parsed for the noexcept
36019 already. noexcept is true then. */
36021 noex
= boolean_true_node
;
36024 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
36025 parser
->in_transaction
= old_in
;
36027 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
36028 return error_mark_node
;
36030 return (flag_tm
? expr
: error_mark_node
);
36033 /* Parse a function-transaction-block.
36035 function-transaction-block:
36036 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36038 __transaction_atomic txn-attribute[opt] function-try-block
36039 __transaction_relaxed ctor-initializer[opt] function-body
36040 __transaction_relaxed function-try-block
36044 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
36046 unsigned char old_in
= parser
->in_transaction
;
36047 unsigned char new_in
= 1;
36048 tree compound_stmt
, stmt
, attrs
;
36049 bool ctor_initializer_p
;
36052 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
36053 || keyword
== RID_TRANSACTION_RELAXED
);
36054 token
= cp_parser_require_keyword (parser
, keyword
,
36055 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
36056 : RT_TRANSACTION_RELAXED
));
36057 gcc_assert (token
!= NULL
);
36059 if (keyword
== RID_TRANSACTION_RELAXED
)
36060 new_in
|= TM_STMT_ATTR_RELAXED
;
36063 attrs
= cp_parser_txn_attribute_opt (parser
);
36065 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
36068 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
36070 parser
->in_transaction
= new_in
;
36072 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
36073 ctor_initializer_p
= cp_parser_function_try_block (parser
);
36075 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
36076 (parser
, /*in_function_try_block=*/false);
36078 parser
->in_transaction
= old_in
;
36080 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
36082 return ctor_initializer_p
;
36085 /* Parse a __transaction_cancel statement.
36088 __transaction_cancel txn-attribute[opt] ;
36089 __transaction_cancel txn-attribute[opt] throw-expression ;
36091 ??? Cancel and throw is not yet implemented. */
36094 cp_parser_transaction_cancel (cp_parser
*parser
)
36097 bool is_outer
= false;
36100 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
36101 RT_TRANSACTION_CANCEL
);
36102 gcc_assert (token
!= NULL
);
36104 attrs
= cp_parser_txn_attribute_opt (parser
);
36106 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
36108 /* ??? Parse cancel-and-throw here. */
36110 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36114 error_at (token
->location
, "%<__transaction_cancel%> without "
36115 "transactional memory support enabled");
36116 return error_mark_node
;
36118 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
36120 error_at (token
->location
, "%<__transaction_cancel%> within a "
36121 "%<__transaction_relaxed%>");
36122 return error_mark_node
;
36126 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
36127 && !is_tm_may_cancel_outer (current_function_decl
))
36129 error_at (token
->location
, "outer %<__transaction_cancel%> not "
36130 "within outer %<__transaction_atomic%>");
36131 error_at (token
->location
,
36132 " or a %<transaction_may_cancel_outer%> function");
36133 return error_mark_node
;
36136 else if (parser
->in_transaction
== 0)
36138 error_at (token
->location
, "%<__transaction_cancel%> not within "
36139 "%<__transaction_atomic%>");
36140 return error_mark_node
;
36143 stmt
= build_tm_abort_call (token
->location
, is_outer
);
36151 static GTY (()) cp_parser
*the_parser
;
36154 /* Special handling for the first token or line in the file. The first
36155 thing in the file might be #pragma GCC pch_preprocess, which loads a
36156 PCH file, which is a GC collection point. So we need to handle this
36157 first pragma without benefit of an existing lexer structure.
36159 Always returns one token to the caller in *FIRST_TOKEN. This is
36160 either the true first token of the file, or the first token after
36161 the initial pragma. */
36164 cp_parser_initial_pragma (cp_token
*first_token
)
36168 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36169 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
36172 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36173 if (first_token
->type
== CPP_STRING
)
36175 name
= first_token
->u
.value
;
36177 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36178 if (first_token
->type
!= CPP_PRAGMA_EOL
)
36179 error_at (first_token
->location
,
36180 "junk at end of %<#pragma GCC pch_preprocess%>");
36183 error_at (first_token
->location
, "expected string literal");
36185 /* Skip to the end of the pragma. */
36186 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
36187 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36189 /* Now actually load the PCH file. */
36191 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
36193 /* Read one more token to return to our caller. We have to do this
36194 after reading the PCH file in, since its pointers have to be
36196 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36199 /* Parses the grainsize pragma for the _Cilk_for statement.
36201 #pragma cilk grainsize = <VALUE>. */
36204 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
36206 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36208 tree exp
= cp_parser_binary_expression (parser
, false, false,
36209 PREC_NOT_OPERATOR
, NULL
);
36210 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36211 if (!exp
|| exp
== error_mark_node
)
36213 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
36217 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
36218 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
36219 cp_parser_cilk_for (parser
, exp
);
36221 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
36222 "%<#pragma cilk grainsize%> is not followed by "
36226 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36229 /* Normal parsing of a pragma token. Here we can (and must) use the
36233 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
36235 cp_token
*pragma_tok
;
36240 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36241 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
36242 parser
->lexer
->in_pragma
= true;
36244 id
= pragma_tok
->pragma_kind
;
36245 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
&& id
!= PRAGMA_OACC_ROUTINE
)
36246 cp_ensure_no_omp_declare_simd (parser
);
36247 cp_ensure_no_oacc_routine (parser
);
36250 case PRAGMA_GCC_PCH_PREPROCESS
:
36251 error_at (pragma_tok
->location
,
36252 "%<#pragma GCC pch_preprocess%> must be first");
36255 case PRAGMA_OMP_BARRIER
:
36258 case pragma_compound
:
36259 cp_parser_omp_barrier (parser
, pragma_tok
);
36262 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
36263 "used in compound statements");
36270 case PRAGMA_OMP_FLUSH
:
36273 case pragma_compound
:
36274 cp_parser_omp_flush (parser
, pragma_tok
);
36277 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
36278 "used in compound statements");
36285 case PRAGMA_OMP_TASKWAIT
:
36288 case pragma_compound
:
36289 cp_parser_omp_taskwait (parser
, pragma_tok
);
36292 error_at (pragma_tok
->location
,
36293 "%<#pragma omp taskwait%> may only be "
36294 "used in compound statements");
36301 case PRAGMA_OMP_TASKYIELD
:
36304 case pragma_compound
:
36305 cp_parser_omp_taskyield (parser
, pragma_tok
);
36308 error_at (pragma_tok
->location
,
36309 "%<#pragma omp taskyield%> may only be "
36310 "used in compound statements");
36317 case PRAGMA_OMP_CANCEL
:
36320 case pragma_compound
:
36321 cp_parser_omp_cancel (parser
, pragma_tok
);
36324 error_at (pragma_tok
->location
,
36325 "%<#pragma omp cancel%> may only be "
36326 "used in compound statements");
36333 case PRAGMA_OMP_CANCELLATION_POINT
:
36336 case pragma_compound
:
36337 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
36340 error_at (pragma_tok
->location
,
36341 "%<#pragma omp cancellation point%> may only be "
36342 "used in compound statements");
36349 case PRAGMA_OMP_THREADPRIVATE
:
36350 cp_parser_omp_threadprivate (parser
, pragma_tok
);
36353 case PRAGMA_OMP_DECLARE_REDUCTION
:
36354 cp_parser_omp_declare (parser
, pragma_tok
, context
);
36357 case PRAGMA_OACC_ROUTINE
:
36358 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
36361 case PRAGMA_OACC_ATOMIC
:
36362 case PRAGMA_OACC_CACHE
:
36363 case PRAGMA_OACC_DATA
:
36364 case PRAGMA_OACC_ENTER_DATA
:
36365 case PRAGMA_OACC_EXIT_DATA
:
36366 case PRAGMA_OACC_KERNELS
:
36367 case PRAGMA_OACC_PARALLEL
:
36368 case PRAGMA_OACC_LOOP
:
36369 case PRAGMA_OACC_UPDATE
:
36370 case PRAGMA_OACC_WAIT
:
36371 case PRAGMA_OMP_ATOMIC
:
36372 case PRAGMA_OMP_CRITICAL
:
36373 case PRAGMA_OMP_DISTRIBUTE
:
36374 case PRAGMA_OMP_FOR
:
36375 case PRAGMA_OMP_MASTER
:
36376 case PRAGMA_OMP_PARALLEL
:
36377 case PRAGMA_OMP_SECTIONS
:
36378 case PRAGMA_OMP_SIMD
:
36379 case PRAGMA_OMP_SINGLE
:
36380 case PRAGMA_OMP_TASK
:
36381 case PRAGMA_OMP_TASKGROUP
:
36382 case PRAGMA_OMP_TASKLOOP
:
36383 case PRAGMA_OMP_TEAMS
:
36384 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
36386 stmt
= push_omp_privatization_clauses (false);
36387 cp_parser_omp_construct (parser
, pragma_tok
);
36388 pop_omp_privatization_clauses (stmt
);
36391 case PRAGMA_OMP_ORDERED
:
36392 stmt
= push_omp_privatization_clauses (false);
36393 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
);
36394 pop_omp_privatization_clauses (stmt
);
36397 case PRAGMA_OMP_TARGET
:
36398 stmt
= push_omp_privatization_clauses (false);
36399 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
);
36400 pop_omp_privatization_clauses (stmt
);
36403 case PRAGMA_OMP_END_DECLARE_TARGET
:
36404 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
36407 case PRAGMA_OMP_SECTION
:
36408 error_at (pragma_tok
->location
,
36409 "%<#pragma omp section%> may only be used in "
36410 "%<#pragma omp sections%> construct");
36415 if (context
== pragma_external
)
36417 error_at (pragma_tok
->location
,
36418 "%<#pragma GCC ivdep%> must be inside a function");
36421 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36423 tok
= cp_lexer_peek_token (the_parser
->lexer
);
36424 if (tok
->type
!= CPP_KEYWORD
36425 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
36426 && tok
->keyword
!= RID_DO
))
36428 cp_parser_error (parser
, "for, while or do statement expected");
36431 cp_parser_iteration_statement (parser
, true);
36435 case PRAGMA_CILK_SIMD
:
36436 if (context
== pragma_external
)
36438 error_at (pragma_tok
->location
,
36439 "%<#pragma simd%> must be inside a function");
36442 stmt
= push_omp_privatization_clauses (false);
36443 cp_parser_cilk_simd (parser
, pragma_tok
);
36444 pop_omp_privatization_clauses (stmt
);
36447 case PRAGMA_CILK_GRAINSIZE
:
36448 if (context
== pragma_external
)
36450 error_at (pragma_tok
->location
,
36451 "%<#pragma cilk grainsize%> must be inside a function");
36455 /* Ignore the pragma if Cilk Plus is not enabled. */
36458 cp_parser_cilk_grainsize (parser
, pragma_tok
);
36463 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
36464 "%<#pragma cilk grainsize%>");
36469 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
36470 c_invoke_pragma_handler (id
);
36474 cp_parser_error (parser
, "expected declaration specifiers");
36478 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36482 /* The interface the pragma parsers have to the lexer. */
36485 pragma_lex (tree
*value
, location_t
*loc
)
36487 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
36488 enum cpp_ttype ret
= tok
->type
;
36490 *value
= tok
->u
.value
;
36492 *loc
= tok
->location
;
36494 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
36496 else if (ret
== CPP_STRING
)
36497 *value
= cp_parser_string_literal (the_parser
, false, false);
36500 if (ret
== CPP_KEYWORD
)
36502 cp_lexer_consume_token (the_parser
->lexer
);
36509 /* External interface. */
36511 /* Parse one entire translation unit. */
36514 c_parse_file (void)
36516 static bool already_called
= false;
36518 if (already_called
)
36519 fatal_error (input_location
,
36520 "inter-module optimizations not implemented for C++");
36521 already_called
= true;
36523 the_parser
= cp_parser_new ();
36524 push_deferring_access_checks (flag_access_control
36525 ? dk_no_deferred
: dk_no_check
);
36526 cp_parser_translation_unit (the_parser
);
36530 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
36531 vectorlength clause:
36533 vectorlength ( constant-expression ) */
36536 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
36539 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36541 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
36542 safelen clause. Thus, vectorlength is represented as OMP 4.0
36543 safelen. For SIMD-enabled function it is represented by OMP 4.0
36546 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
36549 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
36552 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36553 return error_mark_node
;
36555 expr
= cp_parser_constant_expression (parser
);
36556 expr
= maybe_constant_value (expr
);
36558 /* If expr == error_mark_node, then don't emit any errors nor
36559 create a clause. if any of the above functions returns
36560 error mark node then they would have emitted an error message. */
36561 if (expr
== error_mark_node
)
36563 else if (!TREE_TYPE (expr
)
36564 || !TREE_CONSTANT (expr
)
36565 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
36566 error_at (loc
, "vectorlength must be an integer constant");
36567 else if (TREE_CONSTANT (expr
)
36568 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
36569 error_at (loc
, "vectorlength must be a power of 2");
36575 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
36576 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
36577 OMP_CLAUSE_CHAIN (c
) = clauses
;
36582 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
36583 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
36584 OMP_CLAUSE_CHAIN (c
) = clauses
;
36589 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36590 return error_mark_node
;
36594 /* Handles the Cilk Plus #pragma simd linear clause.
36596 linear ( simd-linear-variable-list )
36598 simd-linear-variable-list:
36599 simd-linear-variable
36600 simd-linear-variable-list , simd-linear-variable
36602 simd-linear-variable:
36604 id-expression : simd-linear-step
36607 conditional-expression */
36610 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
36612 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36614 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36616 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36618 cp_parser_error (parser
, "expected identifier");
36619 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
36620 return error_mark_node
;
36623 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36624 parser
->colon_corrects_to_scope_p
= false;
36627 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36630 cp_parser_error (parser
, "expected variable-name");
36631 clauses
= error_mark_node
;
36635 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
36637 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
36639 if (decl
== error_mark_node
)
36641 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
36643 clauses
= error_mark_node
;
36647 tree e
= NULL_TREE
;
36648 tree step_size
= integer_one_node
;
36650 /* If present, parse the linear step. Otherwise, assume the default
36652 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
36654 cp_lexer_consume_token (parser
->lexer
);
36656 e
= cp_parser_assignment_expression (parser
);
36657 e
= maybe_constant_value (e
);
36659 if (e
== error_mark_node
)
36661 /* If an error has occurred, then the whole pragma is
36662 considered ill-formed. Thus, no reason to keep
36664 clauses
= error_mark_node
;
36667 else if (type_dependent_expression_p (e
)
36668 || value_dependent_expression_p (e
)
36670 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
36671 && (TREE_CONSTANT (e
)
36675 cp_parser_error (parser
,
36676 "step size must be an integer constant "
36677 "expression or an integer variable");
36680 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
36681 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36682 OMP_CLAUSE_DECL (l
) = decl
;
36683 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
36684 OMP_CLAUSE_CHAIN (l
) = clauses
;
36687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36688 cp_lexer_consume_token (parser
->lexer
);
36689 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
36693 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36694 "expected %<,%> or %<)%> after %qE", decl
);
36695 clauses
= error_mark_node
;
36699 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36700 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
36704 /* Returns the name of the next clause. If the clause is not
36705 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
36706 token is not consumed. Otherwise, the appropriate enum from the
36707 pragma_simd_clause is returned and the token is consumed. */
36709 static pragma_omp_clause
36710 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
36712 pragma_omp_clause clause_type
;
36713 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36715 if (token
->keyword
== RID_PRIVATE
)
36716 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
36717 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
36718 return PRAGMA_CILK_CLAUSE_NONE
;
36719 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
36720 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
36721 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
36722 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
36723 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
36724 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
36725 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
36726 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
36727 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
36728 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
36730 return PRAGMA_CILK_CLAUSE_NONE
;
36732 cp_lexer_consume_token (parser
->lexer
);
36733 return clause_type
;
36736 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
36739 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
36741 tree clauses
= NULL_TREE
;
36743 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36744 && clauses
!= error_mark_node
)
36746 pragma_omp_clause c_kind
;
36747 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
36748 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
36749 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
36750 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
36751 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
36752 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
36753 /* Use the OpenMP 4.0 equivalent function. */
36754 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
36755 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
36756 /* Use the OpenMP 4.0 equivalent function. */
36757 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
36759 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
36760 /* Use the OMP 4.0 equivalent function. */
36761 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
36763 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
36764 /* Use the OMP 4.0 equivalent function. */
36765 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
36768 clauses
= error_mark_node
;
36769 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
36774 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
36776 if (clauses
== error_mark_node
)
36777 return error_mark_node
;
36779 return c_finish_cilk_clauses (clauses
);
36782 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
36785 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
36787 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
36789 if (clauses
== error_mark_node
)
36792 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
36794 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36795 "for statement expected");
36799 tree sb
= begin_omp_structured_block ();
36800 int save
= cp_parser_begin_omp_structured_block (parser
);
36801 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
36803 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
36804 cp_parser_end_omp_structured_block (parser
, save
);
36805 add_stmt (finish_omp_structured_block (sb
));
36808 /* Main entry-point for parsing Cilk Plus _Cilk_for
36809 loops. The return value is error_mark_node
36810 when errors happen and CILK_FOR tree on success. */
36813 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
36815 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
36816 gcc_unreachable ();
36818 tree sb
= begin_omp_structured_block ();
36819 int save
= cp_parser_begin_omp_structured_block (parser
);
36821 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
36822 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
36823 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
36824 clauses
= finish_omp_clauses (clauses
, false);
36826 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
36828 cpp_validate_cilk_plus_loop (ret
);
36830 ret
= error_mark_node
;
36832 cp_parser_end_omp_structured_block (parser
, save
);
36833 add_stmt (finish_omp_structured_block (sb
));
36837 /* Create an identifier for a generic parameter type (a synthesized
36838 template parameter implied by `auto' or a concept identifier). */
36840 static GTY(()) int generic_parm_count
;
36842 make_generic_type_name ()
36845 sprintf (buf
, "auto:%d", ++generic_parm_count
);
36846 return get_identifier (buf
);
36849 /* Predicate that behaves as is_auto_or_concept but matches the parent
36850 node of the generic type rather than the generic type itself. This
36851 allows for type transformation in add_implicit_template_parms. */
36854 tree_type_is_auto_or_concept (const_tree t
)
36856 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
36859 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
36860 (creating a new template parameter list if necessary). Returns the newly
36861 created template type parm. */
36864 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
36866 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
36868 /* Before committing to modifying any scope, if we're in an
36869 implicit template scope, and we're trying to synthesize a
36870 constrained parameter, try to find a previous parameter with
36871 the same name. This is the same-type rule for abbreviated
36872 function templates. */
36873 if (parser
->implicit_template_scope
&& constr
)
36875 tree t
= parser
->implicit_template_parms
;
36878 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
36880 tree d
= TREE_VALUE (t
);
36881 if (TREE_CODE (d
) == PARM_DECL
)
36882 /* Return the TEMPLATE_PARM_INDEX. */
36883 d
= DECL_INITIAL (d
);
36886 t
= TREE_CHAIN (t
);
36890 /* We are either continuing a function template that already contains implicit
36891 template parameters, creating a new fully-implicit function template, or
36892 extending an existing explicit function template with implicit template
36895 cp_binding_level
*const entry_scope
= current_binding_level
;
36897 bool become_template
= false;
36898 cp_binding_level
*parent_scope
= 0;
36900 if (parser
->implicit_template_scope
)
36902 gcc_assert (parser
->implicit_template_parms
);
36904 current_binding_level
= parser
->implicit_template_scope
;
36908 /* Roll back to the existing template parameter scope (in the case of
36909 extending an explicit function template) or introduce a new template
36910 parameter scope ahead of the function parameter scope (or class scope
36911 in the case of out-of-line member definitions). The function scope is
36912 added back after template parameter synthesis below. */
36914 cp_binding_level
*scope
= entry_scope
;
36916 while (scope
->kind
== sk_function_parms
)
36918 parent_scope
= scope
;
36919 scope
= scope
->level_chain
;
36921 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
36923 /* If not defining a class, then any class scope is a scope level in
36924 an out-of-line member definition. In this case simply wind back
36925 beyond the first such scope to inject the template parameter list.
36926 Otherwise wind back to the class being defined. The latter can
36927 occur in class member friend declarations such as:
36933 friend void A::foo (auto);
36936 The template parameter list synthesized for the friend declaration
36937 must be injected in the scope of 'B'. This can also occur in
36938 erroneous cases such as:
36944 void B::foo (auto) {}
36947 Here the attempted definition of 'B::foo' within 'A' is ill-formed
36948 but, nevertheless, the template parameter list synthesized for the
36949 declarator should be injected into the scope of 'A' as if the
36950 ill-formed template was specified explicitly. */
36952 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
36954 parent_scope
= scope
;
36955 scope
= scope
->level_chain
;
36959 current_binding_level
= scope
;
36961 if (scope
->kind
!= sk_template_parms
36962 || !function_being_declared_is_template_p (parser
))
36964 /* Introduce a new template parameter list for implicit template
36967 become_template
= true;
36969 parser
->implicit_template_scope
36970 = begin_scope (sk_template_parms
, NULL
);
36972 ++processing_template_decl
;
36974 parser
->fully_implicit_function_template_p
= true;
36975 ++parser
->num_template_parameter_lists
;
36979 /* Synthesize implicit template parameters at the end of the explicit
36980 template parameter list. */
36982 gcc_assert (current_template_parms
);
36984 parser
->implicit_template_scope
= scope
;
36986 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
36987 parser
->implicit_template_parms
36988 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
36992 /* Synthesize a new template parameter and track the current template
36993 parameter chain with implicit_template_parms. */
36995 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
36996 tree synth_id
= make_generic_type_name ();
36997 tree synth_tmpl_parm
;
36998 bool non_type
= false;
37000 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
37002 = finish_template_type_parm (class_type_node
, synth_id
);
37003 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
37005 = finish_constrained_template_template_parm (proto
, synth_id
);
37008 synth_tmpl_parm
= copy_decl (proto
);
37009 DECL_NAME (synth_tmpl_parm
) = synth_id
;
37013 // Attach the constraint to the parm before processing.
37014 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
37015 TREE_TYPE (node
) = constr
;
37017 = process_template_parm (parser
->implicit_template_parms
,
37020 /*non_type=*/non_type
,
37021 /*param_pack=*/false);
37023 // Chain the new parameter to the list of implicit parameters.
37024 if (parser
->implicit_template_parms
)
37025 parser
->implicit_template_parms
37026 = TREE_CHAIN (parser
->implicit_template_parms
);
37028 parser
->implicit_template_parms
= new_parm
;
37030 tree new_decl
= getdecls ();
37032 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37033 new_decl
= DECL_INITIAL (new_decl
);
37035 /* If creating a fully implicit function template, start the new implicit
37036 template parameter list with this synthesized type, otherwise grow the
37037 current template parameter list. */
37039 if (become_template
)
37041 parent_scope
->level_chain
= current_binding_level
;
37043 tree new_parms
= make_tree_vec (1);
37044 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
37045 current_template_parms
= tree_cons (size_int (processing_template_decl
),
37046 new_parms
, current_template_parms
);
37050 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
37051 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
37052 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
37053 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
37056 // If the new parameter was constrained, we need to add that to the
37057 // constraints in the template parameter list.
37058 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
37060 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
37061 reqs
= conjoin_constraints (reqs
, req
);
37062 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
37065 current_binding_level
= entry_scope
;
37070 /* Finish the declaration of a fully implicit function template. Such a
37071 template has no explicit template parameter list so has not been through the
37072 normal template head and tail processing. synthesize_implicit_template_parm
37073 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37074 provided if the declaration is a class member such that its template
37075 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37076 form is returned. Otherwise NULL_TREE is returned. */
37079 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
37081 gcc_assert (parser
->fully_implicit_function_template_p
);
37083 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
37084 && DECL_VIRTUAL_P (member_decl_opt
))
37086 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
37087 "implicit templates may not be %<virtual%>");
37088 DECL_VIRTUAL_P (member_decl_opt
) = false;
37091 if (member_decl_opt
)
37092 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
37093 end_template_decl ();
37095 parser
->fully_implicit_function_template_p
= false;
37096 --parser
->num_template_parameter_lists
;
37098 return member_decl_opt
;
37101 #include "gt-cp-parser.h"